diff options
author | Edwin Vane <edwin.vane@intel.com> | 2013-03-19 18:47:03 +0000 |
---|---|---|
committer | Edwin Vane <edwin.vane@intel.com> | 2013-03-19 18:47:03 +0000 |
commit | c383960be9ef8e9e761555cb66917c5d6c9595f0 (patch) | |
tree | 8ed3e47ff6378cc72a427d863dc31ce67852a3b6 /clang-tools-extra/test/cpp11-migrate | |
parent | 0f1bc60d513e7bd6e6da5bb86a24c7bdcb822c96 (diff) | |
download | bcm5719-llvm-c383960be9ef8e9e761555cb66917c5d6c9595f0.tar.gz bcm5719-llvm-c383960be9ef8e9e761555cb66917c5d6c9595f0.zip |
Don't replace macro usage if macro body has NULL
In case of macro body expansion, check to see if the macro is named NULL and
don't replace inside the macro body. This fixes the case when NULL appears
inside the macro body and the transform replaces the usage of the macro with
nullptr. This is an easy fix for the problem for now and we should analyze the
macro body to see if it expands to only NullToPointer in the future for a more
robust solution that takes care of user defined macros that behaves like NULL.
Other changes:
- Moved complex macro tests to macros.cpp
- Added new test cases.
- Added checks to make sure that the macro bodies are not modified by the tool.
Fixes: PR15396
Author: Tareq A Siraj <tareq.a.siraj@intel.com>
llvm-svn: 177422
Diffstat (limited to 'clang-tools-extra/test/cpp11-migrate')
-rw-r--r-- | clang-tools-extra/test/cpp11-migrate/UseNullptr/basic.cpp | 27 | ||||
-rw-r--r-- | clang-tools-extra/test/cpp11-migrate/UseNullptr/macros.cpp | 93 |
2 files changed, 94 insertions, 26 deletions
diff --git a/clang-tools-extra/test/cpp11-migrate/UseNullptr/basic.cpp b/clang-tools-extra/test/cpp11-migrate/UseNullptr/basic.cpp index 46cc9cf83dc..c84758e1cd2 100644 --- a/clang-tools-extra/test/cpp11-migrate/UseNullptr/basic.cpp +++ b/clang-tools-extra/test/cpp11-migrate/UseNullptr/basic.cpp @@ -8,6 +8,7 @@ const unsigned int g_null = 0; #define NULL 0 +// CHECK: #define NULL 0 void test_assignment() { int *p1 = 0; @@ -179,32 +180,6 @@ int test_function_return6() { // CHECK: return g_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()) - - 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 -} - // Test parentheses expressions resulting in a nullptr. int *test_parentheses_expression1() { return(0); diff --git a/clang-tools-extra/test/cpp11-migrate/UseNullptr/macros.cpp b/clang-tools-extra/test/cpp11-migrate/UseNullptr/macros.cpp new file mode 100644 index 00000000000..7b3c2f0c5b9 --- /dev/null +++ b/clang-tools-extra/test/cpp11-migrate/UseNullptr/macros.cpp @@ -0,0 +1,93 @@ +// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp +// RUN: cpp11-migrate -use-nullptr %t.cpp -- -I %S +// RUN: FileCheck -input-file=%t.cpp %s + +#define NULL 0 +// CHECK: #define NULL 0 + +void dummy(int*) {} +void side_effect() {} + +#define MACRO_EXPANSION_HAS_NULL \ + void foo() { \ + dummy(0); \ + dummy(NULL); \ + side_effect(); \ + } + // CHECK: void foo() { \ + // CHECK-NEXT: dummy(0); \ + // CHECK-NEXT: dummy(NULL); \ + +MACRO_EXPANSION_HAS_NULL; +// CHECK: MACRO_EXPANSION_HAS_NULL; +#undef MACRO_EXPANSION_HAS_NULL + + +void test_macro_expansion1() { +#define MACRO_EXPANSION_HAS_NULL \ + dummy(NULL); \ + side_effect(); + // CHECK: dummy(NULL); \ + // CHECK-NEXT: side_effect(); + + MACRO_EXPANSION_HAS_NULL; + // CHECK: MACRO_EXPANSION_HAS_NULL; + +#undef MACRO_EXPANSION_HAS_NULL +} + +void test_macro_expansion2() { +#define MACRO_EXPANSION_HAS_NULL \ + dummy(NULL); \ + side_effect(); + // CHECK: dummy(NULL); \ + // CHECK-NEXT: side_effect(); + +#define OUTER_MACRO \ + MACRO_EXPANSION_HAS_NULL; \ + side_effect(); + + OUTER_MACRO; + // CHECK: OUTER_MACRO; + +#undef OUTER_MACRO +#undef MACRO_EXPANSION_HAS_NULL +} + +void test_macro_expansion3() { +#define MY_NULL NULL + // TODO: Eventually we should fix the transform to detect cases like this so + // that we can replace MY_NULL with nullptr. + int *p = MY_NULL; + // CHECK: int *p = MY_NULL; +#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 +} + + |