diff options
Diffstat (limited to 'clang-tools-extra/test/cpp11-migrate/UseNullptr/macros.cpp')
| -rw-r--r-- | clang-tools-extra/test/cpp11-migrate/UseNullptr/macros.cpp | 99 |
1 files changed, 74 insertions, 25 deletions
diff --git a/clang-tools-extra/test/cpp11-migrate/UseNullptr/macros.cpp b/clang-tools-extra/test/cpp11-migrate/UseNullptr/macros.cpp index 8e19e4ab6dc..de0996ad0ae 100644 --- a/clang-tools-extra/test/cpp11-migrate/UseNullptr/macros.cpp +++ b/clang-tools-extra/test/cpp11-migrate/UseNullptr/macros.cpp @@ -79,29 +79,78 @@ void test_macro_expansion4() { #undef MY_NULL } -// Test function-like macros where the parameter to the macro (expression) -// results in a nullptr. -void __dummy_assert_fail() {} - -void test_function_like_macro1() { - // This tests that the CastSequenceVisitor is working properly. -#define my_assert(expr) \ - ((expr) ? static_cast<void>(expr) : __dummy_assert_fail()) - // CHECK: ((expr) ? static_cast<void>(expr) : __dummy_assert_fail()) - - int *p; - my_assert(p != 0); - // CHECK: my_assert(p != nullptr); -#undef my_assert -} - -void test_function_like_macro2() { - // This tests that the implicit cast is working properly. -#define my_macro(expr) \ - (expr) - - int *p; - my_macro(p != 0); - // CHECK: my_macro(p != nullptr); -#undef my_macro +#define IS_EQ(x, y) if (x != y) return; +void test_macro_args() { + int i = 0; + int *Ptr; + + IS_EQ(static_cast<int*>(0), Ptr); + // CHECK: IS_EQ(static_cast<int*>(nullptr), Ptr); + IS_EQ(0, Ptr); // literal + // CHECK: IS_EQ(nullptr, Ptr); + IS_EQ(NULL, Ptr); // macro + // CHECK: IS_EQ(nullptr, Ptr); + + // These are ok since the null literal is not spelled within a macro. +#define myassert(x) if (!(x)) return; + myassert(0 == Ptr); + // CHECK: myassert(nullptr == Ptr); + myassert(NULL == Ptr); + // CHECK: myassert(nullptr == Ptr); + + // These are bad as the null literal is buried in a macro. +#define BLAH(X) myassert(0 == (X)); + // CHECK: #define BLAH(X) myassert(0 == (X)); +#define BLAH2(X) myassert(NULL == (X)); + // CHECK: #define BLAH2(X) myassert(NULL == (X)); + BLAH(Ptr); + // CHECK: BLAH(Ptr); + BLAH2(Ptr); + // CHECK: BLAH2(Ptr); + + // Same as above but testing extra macro expansion. +#define EXPECT_NULL(X) IS_EQ(0, X); + // CHECK: #define EXPECT_NULL(X) IS_EQ(0, X); +#define EXPECT_NULL2(X) IS_EQ(NULL, X); + // CHECK: #define EXPECT_NULL2(X) IS_EQ(NULL, X); + EXPECT_NULL(Ptr); + // CHECK: EXPECT_NULL(Ptr); + EXPECT_NULL2(Ptr); + // CHECK: EXPECT_NULL2(Ptr); + + // Almost the same as above but now null literal is not in a macro so ok + // to transform. +#define EQUALS_PTR(X) IS_EQ(X, Ptr); + EQUALS_PTR(0); + EQUALS_PTR(NULL); + + // Same as above but testing extra macro expansion. +#define EQUALS_PTR_I(X) EQUALS_PTR(X) + EQUALS_PTR_I(0); + // CHECK: EQUALS_PTR_I(nullptr); + EQUALS_PTR_I(NULL); + // CHECK: EQUALS_PTR_I(nullptr); + + // Ok since null literal not within macro. However, now testing macro + // used as arg to another macro. +#define decorate(EXPR) side_effect(); EXPR; + decorate(IS_EQ(NULL, Ptr)); + // CHECK: decorate(IS_EQ(nullptr, Ptr)); + decorate(IS_EQ(0, Ptr)); + // CHECK: decorate(IS_EQ(nullptr, Ptr)); + + // This macro causes a NullToPointer cast to happen where 0 is assigned to z + // but the 0 literal cannot be replaced because it is also used as an + // integer in the comparison. +#define INT_AND_PTR_USE(X) do { int *z = X; if (X == 4) break; } while(false) + INT_AND_PTR_USE(0); + // CHECK: INT_AND_PTR_USE(0); + + // Both uses of X in this case result in NullToPointer casts so replacement + // is possible. +#define PTR_AND_PTR_USE(X) do { int *z = X; if (X != z) break; } while(false) + PTR_AND_PTR_USE(0); + // CHECK: PTR_AND_PTR_USE(nullptr); + PTR_AND_PTR_USE(NULL); + // CHECK: PTR_AND_PTR_USE(nullptr); } |

