From 88a2c245e528b7ee728d7426982d24543485036b Mon Sep 17 00:00:00 2001 From: Paul Scharnofske <31994781+asynts@users.noreply.github.com> Date: Tue, 25 Aug 2020 16:20:52 +0200 Subject: [PATCH] AK: TestSuite: Define assert macros with do { } while(0). (#3292) Consider the following scenario: if(condition) FOO(); else bar(); Suppose FOO is defined as follows: #define FOO() { bar(); baz(); } Then it expands to the following: if(condition) // Syntax error, we are not allowed to put a semicolon at the end. { bar(); baz(); }; else bar(); If we define FOO as follows: #define FOO() do { bar(); baz(); } while(false) Then it expands to the following: if(condition) do { bar(); baz(); } while(false); else bar(); Which is correct. --- AK/TestSuite.h | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/AK/TestSuite.h b/AK/TestSuite.h index 797a7b4219..44f8c0cc11 100644 --- a/AK/TestSuite.h +++ b/AK/TestSuite.h @@ -29,28 +29,28 @@ #define AK_TEST_SUITE #define ASSERT(x) \ - { \ + do { \ if (!(x)) \ fprintf(stderr, "\033[31;1mFAIL\033[0m: %s:%d: ASSERT(%s) failed\n", __FILE__, __LINE__, #x); \ - } + } while (false) #define RELEASE_ASSERT(x) \ - { \ + do { \ if (!(x)) \ fprintf(stderr, "\033[31;1mFAIL\033[0m: %s:%d: RELEASE_ASSERT(%s) failed\n", __FILE__, __LINE__, #x); \ - } + } while (false) #define ASSERT_NOT_REACHED() \ - { \ + do { \ fprintf(stderr, "\033[31;1mFAIL\033[0m: %s:%d: ASSERT_NOT_REACHED() called\n", __FILE__, __LINE__); \ abort(); \ - } + } while (false) #define TODO() \ - { \ + do { \ fprintf(stderr, "\033[31;1mFAIL\033[0m: %s:%d: TODO() called\n", __FILE__, __LINE__); \ abort(); \ - } + } while (false) #include @@ -293,25 +293,25 @@ using AK::TestSuite; } #define EXPECT_EQ(a, b) \ - { \ + do { \ auto lhs = (a); \ auto rhs = (b); \ if (lhs != rhs) \ AK::maybe_print_rhs_lhs(warn() << "\033[31;1mFAIL\033[0m: " __FILE__ ":" << __LINE__ << ": EXPECT_EQ(" #a ", " #b ") failed", lhs, rhs); \ - } + } while (false) // If you're stuck and `EXPECT_EQ` seems to refuse to print anything useful, // try this: It'll spit out a nice compiler error telling you why it doesn't print. #define EXPECT_EQ_FORCE(a, b) \ - { \ + do { \ auto lhs = (a); \ auto rhs = (b); \ if (lhs != rhs) \ AK::force_print_rhs_lhs(warn() << "\033[31;1mFAIL\033[0m: " __FILE__ ":" << __LINE__ << ": EXPECT_EQ(" #a ", " #b ") failed", lhs, rhs); \ - } + } while (false) #define EXPECT(x) \ - { \ + do { \ if (!(x)) \ warn() << "\033[31;1mFAIL\033[0m: " __FILE__ ":" << __LINE__ << ": EXPECT(" #x ") failed"; \ - } + } while (false)