diff options
author | Paul Scharnofske <31994781+asynts@users.noreply.github.com> | 2020-08-25 16:20:52 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-25 16:20:52 +0200 |
commit | 88a2c245e528b7ee728d7426982d24543485036b (patch) | |
tree | 08c6f96ad983ed6cf5dd3a68234538bfb79c8e7f /AK | |
parent | c2b9f8857c7b9189dab5eca8be06ecc8811968eb (diff) | |
download | serenity-88a2c245e528b7ee728d7426982d24543485036b.zip |
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.
Diffstat (limited to 'AK')
-rw-r--r-- | AK/TestSuite.h | 28 |
1 files 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 <stdio.h> @@ -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) |