[libc-commits] [libc] [libc] Make test macros suppress the -Wdangling-else warnings (PR #127149)
Roland McGrath via libc-commits
libc-commits at lists.llvm.org
Thu Feb 13 16:16:26 PST 2025
https://github.com/frobtech created https://github.com/llvm/llvm-project/pull/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.
>From 9bb32ad6f2ad522aaff2b53124182f9eaad1b6b2 Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Thu, 13 Feb 2025 16:15:08 -0800
Subject: [PATCH] [libc] Make test macros suppress the -Wdangling-else warnings
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.
---
libc/test/UnitTest/LibcTest.h | 9 +++++++++
1 file changed, 9 insertions(+)
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