summaryrefslogtreecommitdiffstats
path: root/clang/test/SemaCXX/warn-bool-conversion.cpp
diff options
context:
space:
mode:
authorRichard Trieu <rtrieu@google.com>2014-08-08 22:41:43 +0000
committerRichard Trieu <rtrieu@google.com>2014-08-08 22:41:43 +0000
commit4cbff5c76fb3236195a93dc595fbf1faa0e9e53f (patch)
tree92f5bb1474ae7ead79f955fa5120c4557c5b7d0b /clang/test/SemaCXX/warn-bool-conversion.cpp
parent0b1d28866c369422c7be0c817ea9b9b7e0cfa12c (diff)
downloadbcm5719-llvm-4cbff5c76fb3236195a93dc595fbf1faa0e9e53f.tar.gz
bcm5719-llvm-4cbff5c76fb3236195a93dc595fbf1faa0e9e53f.zip
Extend tautological pointer compare and pointer to bool conversion warnings to
macro arguments. Previously, these warnings skipped any code in a macro expansion. Preform an additional check and warn when the expression and context locations are both in the macro argument. The most obvious case not caught is passing a pointer directly to a macro, i.e 'assert(&array)' but 'assert(&array && "valid array")' is caught. This is because macro arguments are not typed and the conversion happens inside the macro. llvm-svn: 215251
Diffstat (limited to 'clang/test/SemaCXX/warn-bool-conversion.cpp')
-rw-r--r--clang/test/SemaCXX/warn-bool-conversion.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/clang/test/SemaCXX/warn-bool-conversion.cpp b/clang/test/SemaCXX/warn-bool-conversion.cpp
index b4628947f06..b0c8d0d19d1 100644
--- a/clang/test/SemaCXX/warn-bool-conversion.cpp
+++ b/clang/test/SemaCXX/warn-bool-conversion.cpp
@@ -118,3 +118,30 @@ namespace Pointer {
// expected-warning@-1{{address of 'S::a' will always evaluate to 'true'}}
}
}
+
+namespace macros {
+ #define assert(x) if (x) {}
+ #define zero_on_null(x) ((x) ? *(x) : 0)
+
+ int array[5];
+ void fun();
+ int x;
+
+ void test() {
+ assert(array);
+ assert(array && "expecting null pointer");
+ // expected-warning@-1{{address of array 'array' will always evaluate to 'true'}}
+
+ assert(fun);
+ assert(fun && "expecting null pointer");
+ // expected-warning@-1{{address of function 'fun' will always evaluate to 'true'}}
+ // expected-note@-2 {{prefix with the address-of operator to silence this warning}}
+
+ // TODO: warn on assert(&x) while not warning on zero_on_null(&x)
+ zero_on_null(&x);
+ assert(zero_on_null(&x));
+ assert(&x);
+ assert(&x && "expecting null pointer");
+ // expected-warning@-1{{address of 'x' will always evaluate to 'true'}}
+ }
+}
OpenPOWER on IntegriCloud