1 // RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-overlap-compare %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -Wall -Wno-unused -Wno-loop-analysis %s
3 
4 #define mydefine 2
5 
6 enum Choices {
7   CHOICE_0 = 0,
8   CHOICE_1 = 1
9 };
10 
11 enum Unchoices {
12   UNCHOICE_0 = 0,
13   UNCHOICE_1 = 1
14 };
15 
f(int x)16 void f(int x) {
17   int y = 0;
18 
19   // > || <
20   if (x > 2 || x < 1) { }
21   if (x > 2 || x < 2) { }
22   if (x != 2 || x != 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
23   if (x > 2 || x < 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
24   if (x > 0 || x < 0) { }
25 
26   if (x > 2 || x <= 1) { }
27   if (x > 2 || x <= 2) { } // expected-warning {{overlapping comparisons always evaluate to true}}
28   if (x > 2 || x <= 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
29 
30   if (x >= 2 || x < 1) { }
31   if (x >= 2 || x < 2) { } // expected-warning {{overlapping comparisons always evaluate to true}}
32   if (x >= 2 || x < 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
33 
34   if (x >= 2 || x <= 1) { } // expected-warning {{overlapping comparisons always evaluate to true}}
35   if (x >= 2 || x <= 2) { } // expected-warning {{overlapping comparisons always evaluate to true}}
36   if (x >= 2 || x <= 3) { } // expected-warning {{overlapping comparisons always evaluate to true}}
37   if (x >= 0 || x <= 0) { } // expected-warning {{overlapping comparisons always evaluate to true}}
38 
39   // > && <
40   if (x > 2 && x < 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
41   if (x > 2 && x < 2) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
42   if (x > 2 && x < 3) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
43   if (x > 0 && x < 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
44 
45   if (x > 2 && x <= 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
46   if (x > 2 && x <= 2) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
47   if (x > 2 && x <= 3) { }
48 
49   if (x >= 2 && x < 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
50   if (x >= 2 && x < 2) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
51   if (x >= 2 && x < 3) { }
52   if (x >= 0 && x < 0) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
53 
54   if (x >= 2 && x <= 1) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
55   if (x >= 2 && x <= 2) { }
56   if (x >= 2 && x <= 3) { }
57 
58   // !=, ==, ..
59   if (x != 2 || x != 3) { }  // expected-warning {{overlapping comparisons always evaluate to true}}
60   if (x != 2 || x < 3) { }   // expected-warning {{overlapping comparisons always evaluate to true}}
61   if (x == 2 && x == 3) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
62   if (x == 2 && x > 3) { }   // expected-warning {{overlapping comparisons always evaluate to false}}
63   if (x == 3 && x < 0) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
64   if (3 == x && x < 0) { }  // expected-warning {{overlapping comparisons always evaluate to false}}
65 
66   if (x == mydefine && x > 3) { }
67   if (x == (mydefine + 1) && x > 3) { }
68 
69   if (x != CHOICE_0 || x != CHOICE_1) { } // expected-warning {{overlapping comparisons always evaluate to true}}
70   if (x == CHOICE_0 && x == CHOICE_1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
71 
72   // Don't warn if comparing x to different types
73   if (x == CHOICE_0 && x == 1) { }
74   if (x != CHOICE_0 || x != 1) { }
75 
76   // "Different types" includes different enums
77   if (x == CHOICE_0 && x == UNCHOICE_1) { }
78   if (x != CHOICE_0 || x != UNCHOICE_1) { }
79 }
80 
enums(enum Choices c)81 void enums(enum Choices c) {
82   if (c != CHOICE_0 || c != CHOICE_1) { } // expected-warning {{overlapping comparisons always evaluate to true}}
83   if (c == CHOICE_0 && c == CHOICE_1) { } // expected-warning {{overlapping comparisons always evaluate to false}}
84 
85   // Don't warn if comparing x to different types
86   if (c == CHOICE_0 && c == 1) { }
87   if (c != CHOICE_0 || c != 1) { }
88 
89   // "Different types" includes different enums
90   if (c == CHOICE_0 && c == UNCHOICE_1) { }
91   if (c != CHOICE_0 || c != UNCHOICE_1) { }
92 }
93 
94 // Don't generate a warning here.
array_out_of_bounds()95 void array_out_of_bounds() {
96   int x;
97   int buffer[4];
98   x = (-7 > 0) ? (buffer[-7]) : 0;
99 }
100 
bool_contexts(int x)101 void bool_contexts(int x) {
102   if (x > 4 || x < 10) {}
103   // expected-warning@-1{{overlapping comparisons always evaluate to true}}
104   for (;x > 4 || x < 10;) {}
105   // expected-warning@-1{{overlapping comparisons always evaluate to true}}
106   while (x > 4 || x < 10) {}
107   // expected-warning@-1{{overlapping comparisons always evaluate to true}}
108   do {} while (x > 4 || x < 10);
109   // expected-warning@-1{{overlapping comparisons always evaluate to true}}
110   x = (x > 4 || x < 10) ? 1 : 2;
111   // expected-warning@-1{{overlapping comparisons always evaluate to true}}
112   if ((void)5, x > 4 || x < 10) {}
113   // expected-warning@-1{{overlapping comparisons always evaluate to true}}
114 }
115 
assignment(int x)116 void assignment(int x) {
117   int a = x > 4 || x < 10;
118   // expected-warning@-1{{overlapping comparisons always evaluate to true}}
119   int b = x < 2 && x > 5;
120   // expected-warning@-1{{overlapping comparisons always evaluate to false}}
121 
122   int c = x != 1 || x != 3;
123   // expected-warning@-1{{overlapping comparisons always evaluate to true}}
124   int d = x == 1 && x == 2;
125   // expected-warning@-1{{overlapping comparisons always evaluate to false}}
126 
127   int e = x < 1 || x != 0;
128   // expected-warning@-1{{overlapping comparisons always evaluate to true}}
129 }
130 
returns(int x)131 int returns(int x) {
132   return x > 4 || x < 10;
133   // expected-warning@-1{{overlapping comparisons always evaluate to true}}
134   return x < 2 && x > 5;
135   // expected-warning@-1{{overlapping comparisons always evaluate to false}}
136 
137   return x != 1 || x != 3;
138   // expected-warning@-1{{overlapping comparisons always evaluate to true}}
139   return x == 1 && x == 2;
140   // expected-warning@-1{{overlapping comparisons always evaluate to false}}
141 
142   return x < 1 || x != 0;
143   // expected-warning@-1{{overlapping comparisons always evaluate to true}}
144 }
145 
integer_conversion(unsigned x,int y)146 int integer_conversion(unsigned x, int y) {
147   return x > 4 || x < 10;
148   // expected-warning@-1{{overlapping comparisons always evaluate to true}}
149   return y > 4u || y < 10u;
150   // expected-warning@-1{{overlapping comparisons always evaluate to true}}
151 }
152 
negative_compare(int x)153 int negative_compare(int x) {
154   return x > -1 || x < 1;
155   // expected-warning@-1{{overlapping comparisons always evaluate to true}}
156 }
157 
no_warning(unsigned x)158 int no_warning(unsigned x) {
159   return x >= 0 || x == 1;
160   // no warning since "x >= 0" is caught by a different tautological warning.
161 }
162 
163 struct A {
164   int x;
165   int y;
166 };
167 
struct_test(struct A a)168 int struct_test(struct A a) {
169   return a.x > 5 && a.y < 1;  // no warning, different variables
170 
171   return a.x > 5 && a.x < 1;
172   // expected-warning@-1{{overlapping comparisons always evaluate to false}}
173   return a.y == 1 || a.y != 1;
174   // expected-warning@-1{{overlapping comparisons always evaluate to true}}
175 }
176