diff options
| author | Richard Trieu <rtrieu@google.com> | 2014-02-26 02:36:06 +0000 |
|---|---|---|
| committer | Richard Trieu <rtrieu@google.com> | 2014-02-26 02:36:06 +0000 |
| commit | 3bb8b56a5d90c535e7dc466f924d00ce798c28e0 (patch) | |
| tree | 643e7740ab5e1fb92239f642327a28a9052c8490 /clang/test/SemaCXX/warn-bool-conversion.cpp | |
| parent | f9be75f538f4712acf98cde444e1176ce775b806 (diff) | |
| download | bcm5719-llvm-3bb8b56a5d90c535e7dc466f924d00ce798c28e0.tar.gz bcm5719-llvm-3bb8b56a5d90c535e7dc466f924d00ce798c28e0.zip | |
PR16074, implement warnings to catch pointer to boolean true and pointer to
null comparison when the pointer is known to be non-null.
This catches the array to pointer decay, function to pointer decay and
address of variables. This does not catch address of function since this
has been previously used to silence a warning.
Pointer to bool conversion is under -Wbool-conversion.
Pointer to null comparison is under -Wtautological-pointer-compare, a sub-group
of -Wtautological-compare.
void foo() {
int arr[5];
int x;
// warn on these conditionals
if (foo);
if (arr);
if (&x);
if (foo == null);
if (arr == null);
if (&x == null);
if (&foo); // no warning
}
llvm-svn: 202216
Diffstat (limited to 'clang/test/SemaCXX/warn-bool-conversion.cpp')
| -rw-r--r-- | clang/test/SemaCXX/warn-bool-conversion.cpp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/clang/test/SemaCXX/warn-bool-conversion.cpp b/clang/test/SemaCXX/warn-bool-conversion.cpp index b3d136ecf2b..b4628947f06 100644 --- a/clang/test/SemaCXX/warn-bool-conversion.cpp +++ b/clang/test/SemaCXX/warn-bool-conversion.cpp @@ -1,5 +1,6 @@ // RUN: %clang_cc1 -fsyntax-only -verify %s +namespace BooleanFalse { int* j = false; // expected-warning{{initialization of pointer of type 'int *' to null from a constant boolean expression}} void foo(int* i, int *j=(false)) // expected-warning{{initialization of pointer of type 'int *' to null from a constant boolean expression}} @@ -22,3 +23,98 @@ double f(...); // isn't flagged. template <int N> struct S {}; S<sizeof(f(false))> s; + +} + +namespace Function { +void f1(); + +struct S { + static void f2(); +}; + +extern void f3() __attribute__((weak_import)); + +struct S2 { + static void f4() __attribute__((weak_import)); +}; + +bool f5(); +bool f6(int); + +void bar() { + bool b; + + b = f1; // expected-warning {{address of function 'f1' will always evaluate to 'true'}} \ + expected-note {{prefix with the address-of operator to silence this warning}} + if (f1) {} // expected-warning {{address of function 'f1' will always evaluate to 'true'}} \ + expected-note {{prefix with the address-of operator to silence this warning}} + b = S::f2; // expected-warning {{address of function 'S::f2' will always evaluate to 'true'}} \ + expected-note {{prefix with the address-of operator to silence this warning}} + if (S::f2) {} // expected-warning {{address of function 'S::f2' will always evaluate to 'true'}} \ + expected-note {{prefix with the address-of operator to silence this warning}} + b = f5; // expected-warning {{address of function 'f5' will always evaluate to 'true'}} \ + expected-note {{prefix with the address-of operator to silence this warning}} \ + expected-note {{suffix with parentheses to turn this into a function call}} + b = f6; // expected-warning {{address of function 'f6' will always evaluate to 'true'}} \ + expected-note {{prefix with the address-of operator to silence this warning}} + + // implicit casts of weakly imported symbols are ok: + b = f3; + if (f3) {} + b = S2::f4; + if (S2::f4) {} +} +} + +namespace Array { + #define GetValue(ptr) ((ptr) ? ptr[0] : 0) + extern int a[] __attribute__((weak)); + int b[] = {8,13,21}; + struct { + int x[10]; + } c; + const char str[] = "text"; + void ignore() { + if (a) {} + if (a) {} + (void)GetValue(b); + } + void test() { + if (b) {} + // expected-warning@-1{{address of array 'b' will always evaluate to 'true'}} + if (b) {} + // expected-warning@-1{{address of array 'b' will always evaluate to 'true'}} + if (c.x) {} + // expected-warning@-1{{address of array 'c.x' will always evaluate to 'true'}} + if (str) {} + // expected-warning@-1{{address of array 'str' will always evaluate to 'true'}} + } +} + +namespace Pointer { + extern int a __attribute__((weak)); + int b; + static int c; + class S { + public: + static int a; + int b; + }; + void ignored() { + if (&a) {} + } + void test() { + S s; + if (&b) {} + // expected-warning@-1{{address of 'b' will always evaluate to 'true'}} + if (&c) {} + // expected-warning@-1{{address of 'c' will always evaluate to 'true'}} + if (&s.a) {} + // expected-warning@-1{{address of 's.a' will always evaluate to 'true'}} + if (&s.b) {} + // expected-warning@-1{{address of 's.b' will always evaluate to 'true'}} + if (&S::a) {} + // expected-warning@-1{{address of 'S::a' will always evaluate to 'true'}} + } +} |

