From b5f1794d4db77c954de7c9258344defe4a6e9bcb Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Tue, 19 Dec 2023 14:30:12 +0100 Subject: [PATCH] TestLib: consistently wrap comparison helper macros into do {} while The CHECK_SINGLE_OPERATOR macro is used inside FOR_EACH_CREF macro, which, like all other FOR_EACH_* macros in Qt (see qdebug.h or qmetatype.h), expects its parameter to have a scope or a semicolon. All other CHECK_* macros are *not* used inside FOR_EACH_CREF, so they can follow a more traditional approach with the `do { } while (false)` scope without a trailing semicolon. This allows to use them in the code like: if (condition) MACRO(args); else OTHER_MACRO(args); In the example above, if MACRO ended with the semicolon, adding one more semicolon would have discarded the else part of the conditon, leading to compilation error. Amends bfb237d19a5319bfa020ad0cefaff72e8d94a9be. Task-number: QTBUG-119433 Change-Id: I9a7f17416ba7c37a50f022f685b54e2643e4a9e2 Reviewed-by: Marc Mutz (cherry picked from commit ecafbc4d5d9a4c12f44de856dec544404c633a6e) Reviewed-by: Qt Cherry-pick Bot --- src/testlib/qcomparisontesthelper_p.h | 78 ++++++++++++++------------- 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/src/testlib/qcomparisontesthelper_p.h b/src/testlib/qcomparisontesthelper_p.h index da92aeb9857..9658db9b882 100644 --- a/src/testlib/qcomparisontesthelper_p.h +++ b/src/testlib/qcomparisontesthelper_p.h @@ -129,20 +129,22 @@ void testAllComparisonOperatorsCompile() #undef CHECK_SINGLE_OPERATOR #undef FOR_EACH_CREF +#define CHECK_RUNTIME_CREF(Func, Left, Right, Op, Expected) \ + do { \ + Func(Left, Right, Op, Expected); \ + Func(std::as_const(Left), Right, Op, Expected); \ + Func(Left, std::as_const(Right), Op, Expected); \ + Func(std::as_const(Left), std::as_const(Right), Op, Expected); \ + } while (false) \ + /* END */ + #define CHECK_RUNTIME_LR(Left, Right, Op, Expected) \ do { \ QCOMPARE_EQ(Left Op Right, Expected); \ QCOMPARE_EQ(std::move(Left) Op Right, Expected); \ QCOMPARE_EQ(Left Op std::move(Right), Expected); \ QCOMPARE_EQ(std::move(Left) Op std::move(Right), Expected); \ - } while (false); \ - /* END */ - -#define CHECK_RUNTIME_CREF(Func, Left, Right, Op, Expected) \ - Func(Left, Right, Op, Expected); \ - Func(std::as_const(Left), Right, Op, Expected); \ - Func(Left, std::as_const(Right), Op, Expected); \ - Func(std::as_const(Left), std::as_const(Right), Op, Expected); \ + } while (false) \ /* END */ #ifdef __cpp_lib_three_way_comparison @@ -155,7 +157,7 @@ void testAllComparisonOperatorsCompile() QCOMPARE_EQ((std::move(Left) <=> Right) Op 0, Expected); \ QCOMPARE_EQ((Left <=> std::move(Right)) Op 0, Expected); \ QCOMPARE_EQ((std::move(Left) <=> std::move(Right)) Op 0, Expected); \ - } while (false); \ + } while (false) \ /* END */ #endif // __cpp_lib_three_way_comparison @@ -183,11 +185,11 @@ void testAllComparisonOperatorsCompile() template void testEqualityOperators(LeftType lhs, RightType rhs, bool expectedEqual) { - CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, ==, expectedEqual) - CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, !=, !expectedEqual) + CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, ==, expectedEqual); + CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, !=, !expectedEqual); if constexpr (!std::is_same_v) { - CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, ==, expectedEqual) - CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, !=, !expectedEqual) + CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, ==, expectedEqual); + CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, !=, !expectedEqual); } } @@ -246,17 +248,17 @@ void testAllComparisonOperators(LeftType lhs, RightType rhs, OrderingType expect const bool expectedUnordered = expectedOrdering == Qt::partial_ordering::unordered; CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, ==, - !expectedUnordered && expectedEqual) + !expectedUnordered && expectedEqual); CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, !=, - expectedUnordered || !expectedEqual) + expectedUnordered || !expectedEqual); CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, <, - !expectedUnordered && expectedLess) + !expectedUnordered && expectedLess); CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, >, - !expectedUnordered && !expectedLess && !expectedEqual) + !expectedUnordered && !expectedLess && !expectedEqual); CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, <=, - !expectedUnordered && (expectedEqual || expectedLess)) + !expectedUnordered && (expectedEqual || expectedLess)); CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, >=, - !expectedUnordered && !expectedLess) + !expectedUnordered && !expectedLess); #ifdef __cpp_lib_three_way_comparison if constexpr (implementsThreeWayComparisonOp_v) { if constexpr (std::is_convertible_v) @@ -267,33 +269,33 @@ void testAllComparisonOperators(LeftType lhs, RightType rhs, OrderingType expect static_assert(std::is_same_v rhs), std::partial_ordering>); CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, lhs, rhs, ==, - !expectedUnordered && expectedEqual) + !expectedUnordered && expectedEqual); CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, lhs, rhs, !=, - expectedUnordered || !expectedEqual) + expectedUnordered || !expectedEqual); CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, lhs, rhs, <, - !expectedUnordered && expectedLess) + !expectedUnordered && expectedLess); CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, lhs, rhs, >, - !expectedUnordered && !expectedLess && !expectedEqual) + !expectedUnordered && !expectedLess && !expectedEqual); CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, lhs, rhs, <=, - !expectedUnordered && (expectedEqual || expectedLess)) + !expectedUnordered && (expectedEqual || expectedLess)); CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, lhs, rhs, >=, - !expectedUnordered && !expectedLess) + !expectedUnordered && !expectedLess); } #endif if constexpr (!std::is_same_v) { CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, ==, - !expectedUnordered && expectedEqual) + !expectedUnordered && expectedEqual); CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, !=, - expectedUnordered || !expectedEqual) + expectedUnordered || !expectedEqual); CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, <, - !expectedUnordered && !expectedLess && !expectedEqual) + !expectedUnordered && !expectedLess && !expectedEqual); CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, >, - !expectedUnordered && expectedLess) + !expectedUnordered && expectedLess); CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, <=, - !expectedUnordered && !expectedLess) + !expectedUnordered && !expectedLess); CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, >=, - !expectedUnordered && (expectedEqual || expectedLess)) + !expectedUnordered && (expectedEqual || expectedLess)); #ifdef __cpp_lib_three_way_comparison if constexpr (implementsThreeWayComparisonOp_v) { if constexpr (std::is_convertible_v) @@ -304,17 +306,17 @@ void testAllComparisonOperators(LeftType lhs, RightType rhs, OrderingType expect static_assert(std::is_same_v lhs), std::partial_ordering>); CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, rhs, lhs, ==, - !expectedUnordered && expectedEqual) + !expectedUnordered && expectedEqual); CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, rhs, lhs, !=, - expectedUnordered || !expectedEqual) + expectedUnordered || !expectedEqual); CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, rhs, lhs, <, - !expectedUnordered && !expectedLess && !expectedEqual) + !expectedUnordered && !expectedLess && !expectedEqual); CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, rhs, lhs, >, - !expectedUnordered && expectedLess) + !expectedUnordered && expectedLess); CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, rhs, lhs, <=, - !expectedUnordered && !expectedLess) + !expectedUnordered && !expectedLess); CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, rhs, lhs, >=, - !expectedUnordered && (expectedEqual || expectedLess)) + !expectedUnordered && (expectedEqual || expectedLess)); } #endif } @@ -323,8 +325,8 @@ void testAllComparisonOperators(LeftType lhs, RightType rhs, OrderingType expect #ifdef __cpp_lib_three_way_comparison #undef CHECK_RUNTIME_3WAY #endif -#undef CHECK_RUNTIME_CREF #undef CHECK_RUNTIME_LR +#undef CHECK_RUNTIME_CREF } // namespace QTestPrivate