xref: /llvm-project/clang/test/Sema/warn-tautological-compare.c (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1 // RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -verify  %s
2 
3 extern int a[] __attribute__((weak));
4 int b[] = {8,13,21};
5 struct {
6   int x[10];
7 } c;
8 const char str[] = "text";
9 
ignore(void)10 void ignore(void) {
11   if (!a) {}
12 }
test(void)13 void test(void) {
14   if (!b) {} // expected-warning {{address of array 'b' will always evaluate to 'true'}}
15   if (b == 0) {} // expected-warning {{comparison of array 'b' equal to a null pointer is always false}}
16   if (!c.x) {} // expected-warning {{address of array 'c.x' will always evaluate to 'true'}}
17   if (c.x == 0) {} // expected-warning {{comparison of array 'c.x' equal to a null pointer is always false}}
18   if (!str) {} // expected-warning {{address of array 'str' will always evaluate to 'true'}}
19   if (0 == str) {} // expected-warning {{comparison of array 'str' equal to a null pointer is always false}}
20 }
21 
22 int array[2];
test1(void)23 int test1(void)
24 {
25   if (!array) { // expected-warning {{address of array 'array' will always evaluate to 'true'}}
26     return array[0];
27   } else if (array != 0) { // expected-warning {{comparison of array 'array' not equal to a null pointer is always true}}
28     return array[1];
29   }
30   if (array == 0) // expected-warning {{comparison of array 'array' equal to a null pointer is always false}}
31     return 1;
32   return 0;
33 }
34 
35 #define NULL (void*)0
36 
test2(int * pointer,char ch,void * pv)37 int test2(int* pointer, char ch, void * pv) {
38    if (!&pointer) {  // expected-warning {{address of 'pointer' will always evaluate to 'true'}}
39      return 0;
40    }
41 
42    if (&pointer) {  // expected-warning {{address of 'pointer' will always evaluate to 'true'}}
43      return 0;
44    }
45 
46    if (&pointer == NULL) {} // expected-warning {{comparison of address of 'pointer' equal to a null pointer is always false}}
47 
48    if (&pointer != NULL) {} // expected-warning {{comparison of address of 'pointer' not equal to a null pointer is always true}}
49 
50    return 1;
51 }
52 
test3(void)53 void test3(void) {
54    if (array) { } // expected-warning {{address of array 'array' will always evaluate to 'true'}}
55    if (array != 0) {} // expected-warning {{comparison of array 'array' not equal to a null pointer is always true}}
56    if (!array) { } // expected-warning {{address of array 'array' will always evaluate to 'true'}}
57    if (array == 0) {} // expected-warning {{comparison of array 'array' equal to a null pointer is always false}}
58 
59    if (array[0] &&
60        array) {} // expected-warning {{address of array 'array' will always evaluate to 'true'}}
61 
62    if (array[0] ||
63        array) {} // expected-warning {{address of array 'array' will always evaluate to 'true'}}
64 
65    if (array[0] &&
66        !array) {} // expected-warning {{address of array 'array' will always evaluate to 'true'}}
67    if (array[0] ||
68        !array) {} // expected-warning {{address of array 'array' will always evaluate to 'true'}}
69 
70    if (array && // expected-warning {{address of array 'array' will always evaluate to 'true'}}
71        array[0]) {}
72    if (!array || // expected-warning {{address of array 'array' will always evaluate to 'true'}}
73        array[0]) {}
74 
75    if (array ||  // expected-warning {{address of array 'array' will always evaluate to 'true'}}
76        (!array && array[0])) {} // expected-warning {{address of array 'array' will always evaluate to 'true'}}
77  }
78 
79 #define SAVE_READ(PTR) if( (PTR) && (&result) ) *result=*PTR;
_HTTPClientErrorHandler(int me)80 void _HTTPClientErrorHandler(int me)
81 {
82   int *result;
83   SAVE_READ(&me);
84 }
85 
test_conditional_operator(void)86 void test_conditional_operator(void) {
87   int x;
88   x = b ? 1 : 0;     // expected-warning {{address of array}}
89   x = c.x ? 1 : 0;   // expected-warning {{address of array}}
90   x = str ? 1 : 0;   // expected-warning {{address of array}}
91   x = array ? 1 : 0; // expected-warning {{address of array}}
92   x = &x ? 1 : 0;    // expected-warning {{address of 'x'}}
93 }
94 
test4(void)95 void test4(void) {
96   int *a = (void *) 0;
97   int b = (&a) == ((void *) 0); // expected-warning {{comparison of address of 'a' equal to a null pointer is always false}}
98 }
99