[libc-commits] [libc] e477e56 - [libc] Make test macros suppress the -Wdangling-else warnings (#127149)

via libc-commits libc-commits at lists.llvm.org
Thu Feb 13 20:05:27 PST 2025


Author: Roland McGrath
Date: 2025-02-13T20:05:24-08:00
New Revision: e477e568e6d745c98cb81a856988a99fe3a989e3

URL: https://github.com/llvm/llvm-project/commit/e477e568e6d745c98cb81a856988a99fe3a989e3
DIFF: https://github.com/llvm/llvm-project/commit/e477e568e6d745c98cb81a856988a99fe3a989e3.diff

LOG: [libc] Make test macros suppress the -Wdangling-else warnings (#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.

Added: 
    

Modified: 
    libc/test/UnitTest/LibcTest.h

Removed: 
    


################################################################################
diff  --git a/libc/test/UnitTest/LibcTest.h b/libc/test/UnitTest/LibcTest.h
index b4e3819ea958d..fbeafd0bacb75 100644
--- a/libc/test/UnitTest/LibcTest.h
+++ b/libc/test/UnitTest/LibcTest.h
@@ -400,6 +400,14 @@ CString libc_make_test_file_path_func(const char *file_name);
   SuiteClass##_##TestName SuiteClass##_##TestName##_Instance;                  \
   void SuiteClass##_##TestName::Run()
 
+// Helper to trick the compiler into ignoring lack of braces on the else
+// branch.  We cannot introduce braces at this point, since it would prevent
+// using `<< ...` after the test macro for additional failure output.
+#define LIBC_TEST_DISABLE_DANGLING_ELSE                                        \
+  switch (0)                                                                   \
+  case 0:                                                                      \
+  default: // NOLINT
+
 // If RET_OR_EMPTY is the 'return' keyword we perform an early return which
 // corresponds to an assert. If it is empty the execution continues, this
 // corresponds to an expect.
@@ -411,6 +419,7 @@ CString libc_make_test_file_path_func(const char *file_name);
 // returning a boolean. This expression is responsible for logging the
 // diagnostic in case of failure.
 #define LIBC_TEST_SCAFFOLDING_(TEST, RET_OR_EMPTY)                             \
+  LIBC_TEST_DISABLE_DANGLING_ELSE                                              \
   if (TEST)                                                                    \
     ;                                                                          \
   else                                                                         \


        


More information about the libc-commits mailing list