xref: /aosp_15_r20/external/clang/test/Sema/warn-overlap.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-overlap-compare %s
2*67e74705SXin Li 
3*67e74705SXin Li #define mydefine 2
4*67e74705SXin Li 
5*67e74705SXin Li enum Choices {
6*67e74705SXin Li   CHOICE_0 = 0,
7*67e74705SXin Li   CHOICE_1 = 1
8*67e74705SXin Li };
9*67e74705SXin Li 
10*67e74705SXin Li enum Unchoices {
11*67e74705SXin Li   UNCHOICE_0 = 0,
12*67e74705SXin Li   UNCHOICE_1 = 1
13*67e74705SXin Li };
14*67e74705SXin Li 
f(int x)15*67e74705SXin Li void f(int x) {
16*67e74705SXin Li   int y = 0;
17*67e74705SXin Li 
18*67e74705SXin Li   // > || <
19*67e74705SXin Li   if (x > 2 || x < 1) { }
20*67e74705SXin Li   if (x > 2 || x < 2) { }
21*67e74705SXin Li   if (x != 2 || x != 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
22*67e74705SXin Li   if (x > 2 || x < 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
23*67e74705SXin Li   if (x > 0 || x < 0) { }
24*67e74705SXin Li 
25*67e74705SXin Li   if (x > 2 || x <= 1) { }
26*67e74705SXin Li   if (x > 2 || x <= 2) { } // expected-warning {{overlapping comparisons always evaluate to true}}
27*67e74705SXin Li   if (x > 2 || x <= 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
28*67e74705SXin Li 
29*67e74705SXin Li   if (x >= 2 || x < 1) { }
30*67e74705SXin Li   if (x >= 2 || x < 2) { } // expected-warning {{overlapping comparisons always evaluate to true}}
31*67e74705SXin Li   if (x >= 2 || x < 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
32*67e74705SXin Li 
33*67e74705SXin Li   if (x >= 2 || x <= 1) { } // expected-warning {{overlapping comparisons always evaluate to true}}
34*67e74705SXin Li   if (x >= 2 || x <= 2) { } // expected-warning {{overlapping comparisons always evaluate to true}}
35*67e74705SXin Li   if (x >= 2 || x <= 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
36*67e74705SXin Li   if (x >= 0 || x <= 0) { } // expected-warning {{overlapping comparisons always evaluate to true}}
37*67e74705SXin Li 
38*67e74705SXin Li   // > && <
39*67e74705SXin Li   if (x > 2 && x < 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
40*67e74705SXin Li   if (x > 2 && x < 2) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
41*67e74705SXin Li   if (x > 2 && x < 3) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
42*67e74705SXin Li   if (x > 0 && x < 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
43*67e74705SXin Li 
44*67e74705SXin Li   if (x > 2 && x <= 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
45*67e74705SXin Li   if (x > 2 && x <= 2) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
46*67e74705SXin Li   if (x > 2 && x <= 3) { }
47*67e74705SXin Li 
48*67e74705SXin Li   if (x >= 2 && x < 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
49*67e74705SXin Li   if (x >= 2 && x < 2) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
50*67e74705SXin Li   if (x >= 2 && x < 3) { }
51*67e74705SXin Li   if (x >= 0 && x < 0) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
52*67e74705SXin Li 
53*67e74705SXin Li   if (x >= 2 && x <= 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
54*67e74705SXin Li   if (x >= 2 && x <= 2) { }
55*67e74705SXin Li   if (x >= 2 && x <= 3) { }
56*67e74705SXin Li 
57*67e74705SXin Li   // !=, ==, ..
58*67e74705SXin Li   if (x != 2 || x != 3) { }  // expected-warning {{overlapping comparisons always evaluate to true}}
59*67e74705SXin Li   if (x != 2 || x < 3) { }   // expected-warning {{overlapping comparisons always evaluate to true}}
60*67e74705SXin Li   if (x == 2 && x == 3) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
61*67e74705SXin Li   if (x == 2 && x > 3) { }   // expected-warning {{overlapping comparisons always evaluate to false}}
62*67e74705SXin Li   if (x == 3 && x < 0) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
63*67e74705SXin Li   if (3 == x && x < 0) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
64*67e74705SXin Li 
65*67e74705SXin Li   if (x == mydefine && x > 3) { }
66*67e74705SXin Li   if (x == (mydefine + 1) && x > 3) { }
67*67e74705SXin Li 
68*67e74705SXin Li   if (x != CHOICE_0 || x != CHOICE_1) { } // expected-warning {{overlapping comparisons always evaluate to true}}
69*67e74705SXin Li   if (x == CHOICE_0 && x == CHOICE_1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
70*67e74705SXin Li 
71*67e74705SXin Li   // Don't warn if comparing x to different types
72*67e74705SXin Li   if (x == CHOICE_0 && x == 1) { }
73*67e74705SXin Li   if (x != CHOICE_0 || x != 1) { }
74*67e74705SXin Li 
75*67e74705SXin Li   // "Different types" includes different enums
76*67e74705SXin Li   if (x == CHOICE_0 && x == UNCHOICE_1) { }
77*67e74705SXin Li   if (x != CHOICE_0 || x != UNCHOICE_1) { }
78*67e74705SXin Li }
79*67e74705SXin Li 
enums(enum Choices c)80*67e74705SXin Li void enums(enum Choices c) {
81*67e74705SXin Li   if (c != CHOICE_0 || c != CHOICE_1) { } // expected-warning {{overlapping comparisons always evaluate to true}}
82*67e74705SXin Li   if (c == CHOICE_0 && c == CHOICE_1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
83*67e74705SXin Li 
84*67e74705SXin Li   // Don't warn if comparing x to different types
85*67e74705SXin Li   if (c == CHOICE_0 && c == 1) { }
86*67e74705SXin Li   if (c != CHOICE_0 || c != 1) { }
87*67e74705SXin Li 
88*67e74705SXin Li   // "Different types" includes different enums
89*67e74705SXin Li   if (c == CHOICE_0 && c == UNCHOICE_1) { }
90*67e74705SXin Li   if (c != CHOICE_0 || c != UNCHOICE_1) { }
91*67e74705SXin Li }
92*67e74705SXin Li 
93*67e74705SXin Li // Don't generate a warning here.
array_out_of_bounds()94*67e74705SXin Li void array_out_of_bounds() {
95*67e74705SXin Li   int x;
96*67e74705SXin Li   int buffer[4];
97*67e74705SXin Li   x = (-7 > 0) ? (buffer[-7]) : 0;
98*67e74705SXin Li }
99