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