Skip to content

Commit e477e56

Browse files
authored
[libc] Make test macros suppress the -Wdangling-else warnings (llvm#127149)
Use the trick from gtest to allow `ASSERT_...` and `EXPECT_...` macros to be used in braceless `if` without producing warnings about the nested `if`-`else` that results.
1 parent 910be4f commit e477e56

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

libc/test/UnitTest/LibcTest.h

+9
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,14 @@ CString libc_make_test_file_path_func(const char *file_name);
400400
SuiteClass##_##TestName SuiteClass##_##TestName##_Instance; \
401401
void SuiteClass##_##TestName::Run()
402402

403+
// Helper to trick the compiler into ignoring lack of braces on the else
404+
// branch. We cannot introduce braces at this point, since it would prevent
405+
// using `<< ...` after the test macro for additional failure output.
406+
#define LIBC_TEST_DISABLE_DANGLING_ELSE \
407+
switch (0) \
408+
case 0: \
409+
default: // NOLINT
410+
403411
// If RET_OR_EMPTY is the 'return' keyword we perform an early return which
404412
// corresponds to an assert. If it is empty the execution continues, this
405413
// corresponds to an expect.
@@ -411,6 +419,7 @@ CString libc_make_test_file_path_func(const char *file_name);
411419
// returning a boolean. This expression is responsible for logging the
412420
// diagnostic in case of failure.
413421
#define LIBC_TEST_SCAFFOLDING_(TEST, RET_OR_EMPTY) \
422+
LIBC_TEST_DISABLE_DANGLING_ELSE \
414423
if (TEST) \
415424
; \
416425
else \

0 commit comments

Comments
 (0)