Skip to content

Commit 714b4c8

Browse files
[libc][NFC] Fix -Wdangling-else when compiling libc with gcc >= 7 (#67833)
Explicit braces were added to fix the "suggest explicit braces to avoid ambiguous ‘else’" warning since the current solution (switch (0) case 0: default:) doesn't work since gcc 7 (see google/googletest#1119) gcc 13 generates about 5000 of these warnings when building libc without this patch.
1 parent 8899b71 commit 714b4c8

File tree

5 files changed

+70
-49
lines changed

5 files changed

+70
-49
lines changed

libc/test/UnitTest/FPMatcher.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,21 @@ template <TestCond C, typename T> FPMatcher<T, C> getMatcher(T expectedValue) {
156156
do { \
157157
using namespace LIBC_NAMESPACE::fputil::testing; \
158158
ForceRoundingMode __r1(RoundingMode::Nearest); \
159-
if (__r1.success) \
159+
if (__r1.success) { \
160160
EXPECT_FP_EQ((expected), (actual)); \
161+
} \
161162
ForceRoundingMode __r2(RoundingMode::Upward); \
162-
if (__r2.success) \
163+
if (__r2.success) { \
163164
EXPECT_FP_EQ((expected), (actual)); \
165+
} \
164166
ForceRoundingMode __r3(RoundingMode::Downward); \
165-
if (__r3.success) \
167+
if (__r3.success) { \
166168
EXPECT_FP_EQ((expected), (actual)); \
169+
} \
167170
ForceRoundingMode __r4(RoundingMode::TowardZero); \
168-
if (__r4.success) \
171+
if (__r4.success) { \
169172
EXPECT_FP_EQ((expected), (actual)); \
173+
} \
170174
} while (0)
171175

172176
#endif // LLVM_LIBC_UTILS_UNITTEST_FPMATCHER_H

libc/test/UnitTest/LibcTest.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -378,19 +378,6 @@ CString libc_make_test_file_path_func(const char *file_name);
378378
SuiteClass##_##TestName SuiteClass##_##TestName##_Instance; \
379379
void SuiteClass##_##TestName::Run()
380380

381-
// The GNU compiler emits a warning if nested "if" statements are followed by
382-
// an "else" statement and braces are not used to explicitly disambiguate the
383-
// "else" binding. This leads to problems with code like:
384-
//
385-
// if (gate)
386-
// ASSERT_*(condition) << "Some message";
387-
//
388-
// The "switch (0) case 0:" idiom is used to suppress this.
389-
#define LIBC_AMBIGUOUS_ELSE_BLOCKER_ \
390-
switch (0) \
391-
case 0: \
392-
default:
393-
394381
// If RET_OR_EMPTY is the 'return' keyword we perform an early return which
395382
// corresponds to an assert. If it is empty the execution continues, this
396383
// corresponds to an expect.
@@ -402,7 +389,6 @@ CString libc_make_test_file_path_func(const char *file_name);
402389
// returning a boolean. This expression is responsible for logging the
403390
// diagnostic in case of failure.
404391
#define LIBC_TEST_SCAFFOLDING_(TEST, RET_OR_EMPTY) \
405-
LIBC_AMBIGUOUS_ELSE_BLOCKER_ \
406392
if (TEST) \
407393
; \
408394
else \

libc/test/src/__support/FPUtil/rounding_mode_test.cpp

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,118 +19,138 @@ TEST(LlvmLibcFEnvImplTest, QuickRoundingUpTest) {
1919
using LIBC_NAMESPACE::fputil::fenv_is_round_up;
2020
{
2121
ForceRoundingMode __r(RoundingMode::Upward);
22-
if (__r.success)
22+
if (__r.success) {
2323
ASSERT_TRUE(fenv_is_round_up());
24+
}
2425
}
2526
{
2627
ForceRoundingMode __r(RoundingMode::Downward);
27-
if (__r.success)
28+
if (__r.success) {
2829
ASSERT_FALSE(fenv_is_round_up());
30+
}
2931
}
3032
{
3133
ForceRoundingMode __r(RoundingMode::Nearest);
32-
if (__r.success)
34+
if (__r.success) {
3335
ASSERT_FALSE(fenv_is_round_up());
36+
}
3437
}
3538
{
3639
ForceRoundingMode __r(RoundingMode::TowardZero);
37-
if (__r.success)
40+
if (__r.success) {
3841
ASSERT_FALSE(fenv_is_round_up());
42+
}
3943
}
4044
}
4145

4246
TEST(LlvmLibcFEnvImplTest, QuickRoundingDownTest) {
4347
using LIBC_NAMESPACE::fputil::fenv_is_round_down;
4448
{
4549
ForceRoundingMode __r(RoundingMode::Upward);
46-
if (__r.success)
50+
if (__r.success) {
4751
ASSERT_FALSE(fenv_is_round_down());
52+
}
4853
}
4954
{
5055
ForceRoundingMode __r(RoundingMode::Downward);
51-
if (__r.success)
56+
if (__r.success) {
5257
ASSERT_TRUE(fenv_is_round_down());
58+
}
5359
}
5460
{
5561
ForceRoundingMode __r(RoundingMode::Nearest);
56-
if (__r.success)
62+
if (__r.success) {
5763
ASSERT_FALSE(fenv_is_round_down());
64+
}
5865
}
5966
{
6067
ForceRoundingMode __r(RoundingMode::TowardZero);
61-
if (__r.success)
68+
if (__r.success) {
6269
ASSERT_FALSE(fenv_is_round_down());
70+
}
6371
}
6472
}
6573

6674
TEST(LlvmLibcFEnvImplTest, QuickRoundingNearestTest) {
6775
using LIBC_NAMESPACE::fputil::fenv_is_round_to_nearest;
6876
{
6977
ForceRoundingMode __r(RoundingMode::Upward);
70-
if (__r.success)
78+
if (__r.success) {
7179
ASSERT_FALSE(fenv_is_round_to_nearest());
80+
}
7281
}
7382
{
7483
ForceRoundingMode __r(RoundingMode::Downward);
75-
if (__r.success)
84+
if (__r.success) {
7685
ASSERT_FALSE(fenv_is_round_to_nearest());
86+
}
7787
}
7888
{
7989
ForceRoundingMode __r(RoundingMode::Nearest);
80-
if (__r.success)
90+
if (__r.success) {
8191
ASSERT_TRUE(fenv_is_round_to_nearest());
92+
}
8293
}
8394
{
8495
ForceRoundingMode __r(RoundingMode::TowardZero);
85-
if (__r.success)
96+
if (__r.success) {
8697
ASSERT_FALSE(fenv_is_round_to_nearest());
98+
}
8799
}
88100
}
89101

90102
TEST(LlvmLibcFEnvImplTest, QuickRoundingTowardZeroTest) {
91103
using LIBC_NAMESPACE::fputil::fenv_is_round_to_zero;
92104
{
93105
ForceRoundingMode __r(RoundingMode::Upward);
94-
if (__r.success)
106+
if (__r.success) {
95107
ASSERT_FALSE(fenv_is_round_to_zero());
108+
}
96109
}
97110
{
98111
ForceRoundingMode __r(RoundingMode::Downward);
99-
if (__r.success)
112+
if (__r.success) {
100113
ASSERT_FALSE(fenv_is_round_to_zero());
114+
}
101115
}
102116
{
103117
ForceRoundingMode __r(RoundingMode::Nearest);
104-
if (__r.success)
118+
if (__r.success) {
105119
ASSERT_FALSE(fenv_is_round_to_zero());
120+
}
106121
}
107122
{
108123
ForceRoundingMode __r(RoundingMode::TowardZero);
109-
if (__r.success)
124+
if (__r.success) {
110125
ASSERT_TRUE(fenv_is_round_to_zero());
126+
}
111127
}
112128
}
113129

114130
TEST(LlvmLibcFEnvImplTest, QuickGetRoundTest) {
115131
using LIBC_NAMESPACE::fputil::quick_get_round;
116132
{
117133
ForceRoundingMode __r(RoundingMode::Upward);
118-
if (__r.success)
134+
if (__r.success) {
119135
ASSERT_EQ(quick_get_round(), FE_UPWARD);
136+
}
120137
}
121138
{
122139
ForceRoundingMode __r(RoundingMode::Downward);
123-
if (__r.success)
140+
if (__r.success) {
124141
ASSERT_EQ(quick_get_round(), FE_DOWNWARD);
142+
}
125143
}
126144
{
127145
ForceRoundingMode __r(RoundingMode::Nearest);
128-
if (__r.success)
146+
if (__r.success) {
129147
ASSERT_EQ(quick_get_round(), FE_TONEAREST);
148+
}
130149
}
131150
{
132151
ForceRoundingMode __r(RoundingMode::TowardZero);
133-
if (__r.success)
152+
if (__r.success) {
134153
ASSERT_EQ(quick_get_round(), FE_TOWARDZERO);
154+
}
135155
}
136156
}

libc/test/src/spawn/posix_spawn_file_actions_test.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ TEST(LlvmLibcPosixSpawnFileActionsTest, AddActions) {
4040
int action_count = 0;
4141
while (act != nullptr) {
4242
++action_count;
43-
if (action_count == 1)
43+
if (action_count == 1) {
4444
ASSERT_EQ(act->type, LIBC_NAMESPACE::BaseSpawnFileAction::CLOSE);
45-
if (action_count == 2)
45+
}
46+
if (action_count == 2) {
4647
ASSERT_EQ(act->type, LIBC_NAMESPACE::BaseSpawnFileAction::DUP2);
47-
if (action_count == 3)
48+
}
49+
if (action_count == 3) {
4850
ASSERT_EQ(act->type, LIBC_NAMESPACE::BaseSpawnFileAction::OPEN);
51+
}
4952
act = act->next;
5053
}
5154
ASSERT_EQ(action_count, 3);

libc/utils/MPFRWrapper/MPFRUtils.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -356,21 +356,25 @@ template <typename T> bool round_to_long(T x, RoundingMode mode, long &result);
356356
{ \
357357
namespace mpfr = LIBC_NAMESPACE::testing::mpfr; \
358358
mpfr::ForceRoundingMode __r1(mpfr::RoundingMode::Nearest); \
359-
if (__r1.success) \
359+
if (__r1.success) { \
360360
EXPECT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
361361
mpfr::RoundingMode::Nearest); \
362+
} \
362363
mpfr::ForceRoundingMode __r2(mpfr::RoundingMode::Upward); \
363-
if (__r2.success) \
364+
if (__r2.success) { \
364365
EXPECT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
365366
mpfr::RoundingMode::Upward); \
367+
} \
366368
mpfr::ForceRoundingMode __r3(mpfr::RoundingMode::Downward); \
367-
if (__r3.success) \
369+
if (__r3.success) { \
368370
EXPECT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
369371
mpfr::RoundingMode::Downward); \
372+
} \
370373
mpfr::ForceRoundingMode __r4(mpfr::RoundingMode::TowardZero); \
371-
if (__r4.success) \
374+
if (__r4.success) { \
372375
EXPECT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
373376
mpfr::RoundingMode::TowardZero); \
377+
} \
374378
}
375379

376380
#define TEST_MPFR_MATCH_ROUNDING_SILENTLY(op, input, match_value, \
@@ -400,21 +404,25 @@ template <typename T> bool round_to_long(T x, RoundingMode mode, long &result);
400404
{ \
401405
namespace mpfr = LIBC_NAMESPACE::testing::mpfr; \
402406
mpfr::ForceRoundingMode __r1(mpfr::RoundingMode::Nearest); \
403-
if (__r1.success) \
407+
if (__r1.success) { \
404408
ASSERT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
405409
mpfr::RoundingMode::Nearest); \
410+
} \
406411
mpfr::ForceRoundingMode __r2(mpfr::RoundingMode::Upward); \
407-
if (__r2.success) \
412+
if (__r2.success) { \
408413
ASSERT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
409414
mpfr::RoundingMode::Upward); \
415+
} \
410416
mpfr::ForceRoundingMode __r3(mpfr::RoundingMode::Downward); \
411-
if (__r3.success) \
417+
if (__r3.success) { \
412418
ASSERT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
413419
mpfr::RoundingMode::Downward); \
420+
} \
414421
mpfr::ForceRoundingMode __r4(mpfr::RoundingMode::TowardZero); \
415-
if (__r4.success) \
422+
if (__r4.success) { \
416423
ASSERT_MPFR_MATCH(op, input, match_value, ulp_tolerance, \
417424
mpfr::RoundingMode::TowardZero); \
425+
} \
418426
}
419427

420428
#endif // LLVM_LIBC_UTILS_TESTUTILS_MPFRUTILS_H

0 commit comments

Comments
 (0)