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