[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / Sema / warn-overlap.c
blob1eddfd1077fd9254a2e9865cbf18bd3d0e62ac87
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
4 #define mydefine 2
6 enum Choices {
7 CHOICE_0 = 0,
8 CHOICE_1 = 1
9 };
11 enum Unchoices {
12 UNCHOICE_0 = 0,
13 UNCHOICE_1 = 1
16 void f(int x) {
17 int y = 0;
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) { }
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}}
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}}
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}}
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}}
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) { }
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}}
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) { }
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}}
66 if (x == mydefine && x > 3) { }
67 if (x == (mydefine + 1) && x > 3) { }
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}}
72 // Don't warn if comparing x to different types
73 if (x == CHOICE_0 && x == 1) { }
74 if (x != CHOICE_0 || x != 1) { }
76 // "Different types" includes different enums
77 if (x == CHOICE_0 && x == UNCHOICE_1) { }
78 if (x != CHOICE_0 || x != UNCHOICE_1) { }
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}}
85 // Don't warn if comparing x to different types
86 if (c == CHOICE_0 && c == 1) { }
87 if (c != CHOICE_0 || c != 1) { }
89 // "Different types" includes different enums
90 if (c == CHOICE_0 && c == UNCHOICE_1) { }
91 if (c != CHOICE_0 || c != UNCHOICE_1) { }
94 // Don't generate a warning here.
95 void array_out_of_bounds(void) {
96 int x;
97 int buffer[4];
98 x = (-7 > 0) ? (buffer[-7]) : 0;
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}}
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}}
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}}
127 int e = x < 1 || x != 0;
128 // expected-warning@-1{{overlapping comparisons always evaluate to true}}
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}}
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}}
142 return x < 1 || x != 0;
143 // expected-warning@-1{{overlapping comparisons always evaluate to true}}
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}}
153 int negative_compare(int x) {
154 return x > -1 || x < 1;
155 // expected-warning@-1{{overlapping comparisons always evaluate to true}}
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.
163 struct A {
164 int x;
165 int y;
168 int struct_test(struct A a) {
169 return a.x > 5 && a.y < 1; // no warning, different variables
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}}