xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/warn-overlap.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-overlap-compare %s
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc #define mydefine 2
4*0a6a1f1dSLionel Sambuc 
f(int x)5*0a6a1f1dSLionel Sambuc void f(int x) {
6*0a6a1f1dSLionel Sambuc   int y = 0;
7*0a6a1f1dSLionel Sambuc 
8*0a6a1f1dSLionel Sambuc   // > || <
9*0a6a1f1dSLionel Sambuc   if (x > 2 || x < 1) { }
10*0a6a1f1dSLionel Sambuc   if (x > 2 || x < 2) { }
11*0a6a1f1dSLionel Sambuc   if (x != 2 || x != 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
12*0a6a1f1dSLionel Sambuc   if (x > 2 || x < 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
13*0a6a1f1dSLionel Sambuc   if (x > 0 || x < 0) { }
14*0a6a1f1dSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc   if (x > 2 || x <= 1) { }
16*0a6a1f1dSLionel Sambuc   if (x > 2 || x <= 2) { } // expected-warning {{overlapping comparisons always evaluate to true}}
17*0a6a1f1dSLionel Sambuc   if (x > 2 || x <= 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
18*0a6a1f1dSLionel Sambuc 
19*0a6a1f1dSLionel Sambuc   if (x >= 2 || x < 1) { }
20*0a6a1f1dSLionel Sambuc   if (x >= 2 || x < 2) { } // expected-warning {{overlapping comparisons always evaluate to true}}
21*0a6a1f1dSLionel Sambuc   if (x >= 2 || x < 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
22*0a6a1f1dSLionel Sambuc 
23*0a6a1f1dSLionel Sambuc   if (x >= 2 || x <= 1) { } // expected-warning {{overlapping comparisons always evaluate to true}}
24*0a6a1f1dSLionel Sambuc   if (x >= 2 || x <= 2) { } // expected-warning {{overlapping comparisons always evaluate to true}}
25*0a6a1f1dSLionel Sambuc   if (x >= 2 || x <= 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
26*0a6a1f1dSLionel Sambuc   if (x >= 0 || x <= 0) { } // expected-warning {{overlapping comparisons always evaluate to true}}
27*0a6a1f1dSLionel Sambuc 
28*0a6a1f1dSLionel Sambuc   // > && <
29*0a6a1f1dSLionel Sambuc   if (x > 2 && x < 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
30*0a6a1f1dSLionel Sambuc   if (x > 2 && x < 2) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
31*0a6a1f1dSLionel Sambuc   if (x > 2 && x < 3) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
32*0a6a1f1dSLionel Sambuc   if (x > 0 && x < 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
33*0a6a1f1dSLionel Sambuc 
34*0a6a1f1dSLionel Sambuc   if (x > 2 && x <= 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
35*0a6a1f1dSLionel Sambuc   if (x > 2 && x <= 2) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
36*0a6a1f1dSLionel Sambuc   if (x > 2 && x <= 3) { }
37*0a6a1f1dSLionel Sambuc 
38*0a6a1f1dSLionel Sambuc   if (x >= 2 && x < 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
39*0a6a1f1dSLionel Sambuc   if (x >= 2 && x < 2) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
40*0a6a1f1dSLionel Sambuc   if (x >= 2 && x < 3) { }
41*0a6a1f1dSLionel Sambuc   if (x >= 0 && x < 0) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
42*0a6a1f1dSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc   if (x >= 2 && x <= 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
44*0a6a1f1dSLionel Sambuc   if (x >= 2 && x <= 2) { }
45*0a6a1f1dSLionel Sambuc   if (x >= 2 && x <= 3) { }
46*0a6a1f1dSLionel Sambuc 
47*0a6a1f1dSLionel Sambuc   // !=, ==, ..
48*0a6a1f1dSLionel Sambuc   if (x != 2 || x != 3) { }  // expected-warning {{overlapping comparisons always evaluate to true}}
49*0a6a1f1dSLionel Sambuc   if (x != 2 || x < 3) { }   // expected-warning {{overlapping comparisons always evaluate to true}}
50*0a6a1f1dSLionel Sambuc   if (x == 2 && x == 3) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
51*0a6a1f1dSLionel Sambuc   if (x == 2 && x > 3) { }   // expected-warning {{overlapping comparisons always evaluate to false}}
52*0a6a1f1dSLionel Sambuc   if (x == 3 && x < 0) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
53*0a6a1f1dSLionel Sambuc   if (3 == x && x < 0) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
54*0a6a1f1dSLionel Sambuc 
55*0a6a1f1dSLionel Sambuc   if (x == mydefine && x > 3) { }
56*0a6a1f1dSLionel Sambuc   if (x == (mydefine + 1) && x > 3) { }
57*0a6a1f1dSLionel Sambuc }
58*0a6a1f1dSLionel Sambuc 
59*0a6a1f1dSLionel Sambuc // Don't generate a warning here.
array_out_of_bounds()60*0a6a1f1dSLionel Sambuc void array_out_of_bounds() {
61*0a6a1f1dSLionel Sambuc   int x;
62*0a6a1f1dSLionel Sambuc   int buffer[4];
63*0a6a1f1dSLionel Sambuc   x = (-7 > 0) ? (buffer[-7]) : 0;
64*0a6a1f1dSLionel Sambuc }
65