[libc-commits] [libc] [libc] Make test macros suppress the -Wdangling-else warnings (PR #127149)
via libc-commits
libc-commits at lists.llvm.org
Thu Feb 13 16:18:42 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Roland McGrath (frobtech)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/127149.diff
1 Files Affected:
- (modified) libc/test/UnitTest/LibcTest.h (+9)
``````````diff
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 \
``````````
</details>
https://github.com/llvm/llvm-project/pull/127149
More information about the libc-commits
mailing list