annotate clang/test/Sema/warn-overlap.c @ 222:81f6424ef0e3 llvm-original

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