1*67e74705SXin Li // Force x86-64 because some of our heuristics are actually based
2*67e74705SXin Li // on integer sizes.
3*67e74705SXin Li
4*67e74705SXin Li // RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify -Wsign-compare -std=c++11 %s
5*67e74705SXin Li
test0(long a,unsigned long b)6*67e74705SXin Li int test0(long a, unsigned long b) {
7*67e74705SXin Li enum EnumA {A};
8*67e74705SXin Li enum EnumB {B};
9*67e74705SXin Li enum EnumC {C = 0x10000};
10*67e74705SXin Li return
11*67e74705SXin Li // (a,b)
12*67e74705SXin Li (a == (unsigned long) b) + // expected-warning {{comparison of integers of different signs}}
13*67e74705SXin Li (a == (unsigned int) b) +
14*67e74705SXin Li (a == (unsigned short) b) +
15*67e74705SXin Li (a == (unsigned char) b) +
16*67e74705SXin Li ((long) a == b) + // expected-warning {{comparison of integers of different signs}}
17*67e74705SXin Li ((int) a == b) + // expected-warning {{comparison of integers of different signs}}
18*67e74705SXin Li ((short) a == b) + // expected-warning {{comparison of integers of different signs}}
19*67e74705SXin Li ((signed char) a == b) + // expected-warning {{comparison of integers of different signs}}
20*67e74705SXin Li ((long) a == (unsigned long) b) + // expected-warning {{comparison of integers of different signs}}
21*67e74705SXin Li ((int) a == (unsigned int) b) + // expected-warning {{comparison of integers of different signs}}
22*67e74705SXin Li ((short) a == (unsigned short) b) +
23*67e74705SXin Li ((signed char) a == (unsigned char) b) +
24*67e74705SXin Li (a < (unsigned long) b) + // expected-warning {{comparison of integers of different signs}}
25*67e74705SXin Li (a < (unsigned int) b) +
26*67e74705SXin Li (a < (unsigned short) b) +
27*67e74705SXin Li (a < (unsigned char) b) +
28*67e74705SXin Li ((long) a < b) + // expected-warning {{comparison of integers of different signs}}
29*67e74705SXin Li ((int) a < b) + // expected-warning {{comparison of integers of different signs}}
30*67e74705SXin Li ((short) a < b) + // expected-warning {{comparison of integers of different signs}}
31*67e74705SXin Li ((signed char) a < b) + // expected-warning {{comparison of integers of different signs}}
32*67e74705SXin Li ((long) a < (unsigned long) b) + // expected-warning {{comparison of integers of different signs}}
33*67e74705SXin Li ((int) a < (unsigned int) b) + // expected-warning {{comparison of integers of different signs}}
34*67e74705SXin Li ((short) a < (unsigned short) b) +
35*67e74705SXin Li ((signed char) a < (unsigned char) b) +
36*67e74705SXin Li
37*67e74705SXin Li // (A,b)
38*67e74705SXin Li (A == (unsigned long) b) +
39*67e74705SXin Li (A == (unsigned int) b) +
40*67e74705SXin Li (A == (unsigned short) b) +
41*67e74705SXin Li (A == (unsigned char) b) +
42*67e74705SXin Li ((long) A == b) +
43*67e74705SXin Li ((int) A == b) +
44*67e74705SXin Li ((short) A == b) +
45*67e74705SXin Li ((signed char) A == b) +
46*67e74705SXin Li ((long) A == (unsigned long) b) +
47*67e74705SXin Li ((int) A == (unsigned int) b) +
48*67e74705SXin Li ((short) A == (unsigned short) b) +
49*67e74705SXin Li ((signed char) A == (unsigned char) b) +
50*67e74705SXin Li (A < (unsigned long) b) +
51*67e74705SXin Li (A < (unsigned int) b) +
52*67e74705SXin Li (A < (unsigned short) b) +
53*67e74705SXin Li (A < (unsigned char) b) +
54*67e74705SXin Li ((long) A < b) +
55*67e74705SXin Li ((int) A < b) +
56*67e74705SXin Li ((short) A < b) +
57*67e74705SXin Li ((signed char) A < b) +
58*67e74705SXin Li ((long) A < (unsigned long) b) +
59*67e74705SXin Li ((int) A < (unsigned int) b) +
60*67e74705SXin Li ((short) A < (unsigned short) b) +
61*67e74705SXin Li ((signed char) A < (unsigned char) b) +
62*67e74705SXin Li
63*67e74705SXin Li // (a,B)
64*67e74705SXin Li (a == (unsigned long) B) +
65*67e74705SXin Li (a == (unsigned int) B) +
66*67e74705SXin Li (a == (unsigned short) B) +
67*67e74705SXin Li (a == (unsigned char) B) +
68*67e74705SXin Li ((long) a == B) +
69*67e74705SXin Li ((int) a == B) +
70*67e74705SXin Li ((short) a == B) +
71*67e74705SXin Li ((signed char) a == B) +
72*67e74705SXin Li ((long) a == (unsigned long) B) +
73*67e74705SXin Li ((int) a == (unsigned int) B) +
74*67e74705SXin Li ((short) a == (unsigned short) B) +
75*67e74705SXin Li ((signed char) a == (unsigned char) B) +
76*67e74705SXin Li (a < (unsigned long) B) + // expected-warning {{comparison of integers of different signs}}
77*67e74705SXin Li (a < (unsigned int) B) +
78*67e74705SXin Li (a < (unsigned short) B) +
79*67e74705SXin Li (a < (unsigned char) B) +
80*67e74705SXin Li ((long) a < B) +
81*67e74705SXin Li ((int) a < B) +
82*67e74705SXin Li ((short) a < B) +
83*67e74705SXin Li ((signed char) a < B) +
84*67e74705SXin Li ((long) a < (unsigned long) B) + // expected-warning {{comparison of integers of different signs}}
85*67e74705SXin Li ((int) a < (unsigned int) B) + // expected-warning {{comparison of integers of different signs}}
86*67e74705SXin Li ((short) a < (unsigned short) B) +
87*67e74705SXin Li ((signed char) a < (unsigned char) B) +
88*67e74705SXin Li
89*67e74705SXin Li // (C,b)
90*67e74705SXin Li (C == (unsigned long) b) +
91*67e74705SXin Li (C == (unsigned int) b) +
92*67e74705SXin Li (C == (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}}
93*67e74705SXin Li (C == (unsigned char) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}}
94*67e74705SXin Li ((long) C == b) +
95*67e74705SXin Li ((int) C == b) +
96*67e74705SXin Li ((short) C == b) +
97*67e74705SXin Li ((signed char) C == b) +
98*67e74705SXin Li ((long) C == (unsigned long) b) +
99*67e74705SXin Li ((int) C == (unsigned int) b) +
100*67e74705SXin Li ((short) C == (unsigned short) b) +
101*67e74705SXin Li ((signed char) C == (unsigned char) b) +
102*67e74705SXin Li (C < (unsigned long) b) +
103*67e74705SXin Li (C < (unsigned int) b) +
104*67e74705SXin Li (C < (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}}
105*67e74705SXin Li (C < (unsigned char) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}}
106*67e74705SXin Li ((long) C < b) +
107*67e74705SXin Li ((int) C < b) +
108*67e74705SXin Li ((short) C < b) +
109*67e74705SXin Li ((signed char) C < b) +
110*67e74705SXin Li ((long) C < (unsigned long) b) +
111*67e74705SXin Li ((int) C < (unsigned int) b) +
112*67e74705SXin Li ((short) C < (unsigned short) b) +
113*67e74705SXin Li ((signed char) C < (unsigned char) b) +
114*67e74705SXin Li
115*67e74705SXin Li // (a,C)
116*67e74705SXin Li (a == (unsigned long) C) +
117*67e74705SXin Li (a == (unsigned int) C) +
118*67e74705SXin Li (a == (unsigned short) C) +
119*67e74705SXin Li (a == (unsigned char) C) +
120*67e74705SXin Li ((long) a == C) +
121*67e74705SXin Li ((int) a == C) +
122*67e74705SXin Li ((short) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always false}}
123*67e74705SXin Li ((signed char) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always false}}
124*67e74705SXin Li ((long) a == (unsigned long) C) +
125*67e74705SXin Li ((int) a == (unsigned int) C) +
126*67e74705SXin Li ((short) a == (unsigned short) C) +
127*67e74705SXin Li ((signed char) a == (unsigned char) C) +
128*67e74705SXin Li (a < (unsigned long) C) + // expected-warning {{comparison of integers of different signs}}
129*67e74705SXin Li (a < (unsigned int) C) +
130*67e74705SXin Li (a < (unsigned short) C) +
131*67e74705SXin Li (a < (unsigned char) C) +
132*67e74705SXin Li ((long) a < C) +
133*67e74705SXin Li ((int) a < C) +
134*67e74705SXin Li ((short) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always true}}
135*67e74705SXin Li ((signed char) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always true}}
136*67e74705SXin Li ((long) a < (unsigned long) C) + // expected-warning {{comparison of integers of different signs}}
137*67e74705SXin Li ((int) a < (unsigned int) C) + // expected-warning {{comparison of integers of different signs}}
138*67e74705SXin Li ((short) a < (unsigned short) C) +
139*67e74705SXin Li ((signed char) a < (unsigned char) C) +
140*67e74705SXin Li
141*67e74705SXin Li // (0x80000,b)
142*67e74705SXin Li (0x80000 == (unsigned long) b) +
143*67e74705SXin Li (0x80000 == (unsigned int) b) +
144*67e74705SXin Li (0x80000 == (unsigned short) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned short' is always false}}
145*67e74705SXin Li (0x80000 == (unsigned char) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned char' is always false}}
146*67e74705SXin Li ((long) 0x80000 == b) +
147*67e74705SXin Li ((int) 0x80000 == b) +
148*67e74705SXin Li ((short) 0x80000 == b) +
149*67e74705SXin Li ((signed char) 0x80000 == b) +
150*67e74705SXin Li ((long) 0x80000 == (unsigned long) b) +
151*67e74705SXin Li ((int) 0x80000 == (unsigned int) b) +
152*67e74705SXin Li ((short) 0x80000 == (unsigned short) b) +
153*67e74705SXin Li ((signed char) 0x80000 == (unsigned char) b) +
154*67e74705SXin Li (0x80000 < (unsigned long) b) +
155*67e74705SXin Li (0x80000 < (unsigned int) b) +
156*67e74705SXin Li (0x80000 < (unsigned short) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned short' is always false}}
157*67e74705SXin Li (0x80000 < (unsigned char) b) + // expected-warning {{comparison of constant 524288 with expression of type 'unsigned char' is always false}}
158*67e74705SXin Li ((long) 0x80000 < b) +
159*67e74705SXin Li ((int) 0x80000 < b) +
160*67e74705SXin Li ((short) 0x80000 < b) +
161*67e74705SXin Li ((signed char) 0x80000 < b) +
162*67e74705SXin Li ((long) 0x80000 < (unsigned long) b) +
163*67e74705SXin Li ((int) 0x80000 < (unsigned int) b) +
164*67e74705SXin Li ((short) 0x80000 < (unsigned short) b) +
165*67e74705SXin Li ((signed char) 0x80000 < (unsigned char) b) +
166*67e74705SXin Li
167*67e74705SXin Li // (a,0x80000)
168*67e74705SXin Li (a == (unsigned long) 0x80000) +
169*67e74705SXin Li (a == (unsigned int) 0x80000) +
170*67e74705SXin Li (a == (unsigned short) 0x80000) +
171*67e74705SXin Li (a == (unsigned char) 0x80000) +
172*67e74705SXin Li ((long) a == 0x80000) +
173*67e74705SXin Li ((int) a == 0x80000) +
174*67e74705SXin Li ((short) a == 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'short' is always false}}
175*67e74705SXin Li ((signed char) a == 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always false}}
176*67e74705SXin Li ((long) a == (unsigned long) 0x80000) +
177*67e74705SXin Li ((int) a == (unsigned int) 0x80000) +
178*67e74705SXin Li ((short) a == (unsigned short) 0x80000) +
179*67e74705SXin Li ((signed char) a == (unsigned char) 0x80000) +
180*67e74705SXin Li (a < (unsigned long) 0x80000) + // expected-warning {{comparison of integers of different signs}}
181*67e74705SXin Li (a < (unsigned int) 0x80000) +
182*67e74705SXin Li (a < (unsigned short) 0x80000) +
183*67e74705SXin Li (a < (unsigned char) 0x80000) +
184*67e74705SXin Li ((long) a < 0x80000) +
185*67e74705SXin Li ((int) a < 0x80000) +
186*67e74705SXin Li ((short) a < 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'short' is always true}}
187*67e74705SXin Li ((signed char) a < 0x80000) + // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always true}}
188*67e74705SXin Li ((long) a < (unsigned long) 0x80000) + // expected-warning {{comparison of integers of different signs}}
189*67e74705SXin Li ((int) a < (unsigned int) 0x80000) + // expected-warning {{comparison of integers of different signs}}
190*67e74705SXin Li ((short) a < (unsigned short) 0x80000) +
191*67e74705SXin Li ((signed char) a < (unsigned char) 0x80000) +
192*67e74705SXin Li
193*67e74705SXin Li 10
194*67e74705SXin Li ;
195*67e74705SXin Li }
196*67e74705SXin Li
test1(int i)197*67e74705SXin Li int test1(int i) {
198*67e74705SXin Li enum en { zero };
199*67e74705SXin Li return i > zero;
200*67e74705SXin Li }
201*67e74705SXin Li
202*67e74705SXin Li enum E { e };
test2(int i,void * vp)203*67e74705SXin Li void test2(int i, void *vp) {
204*67e74705SXin Li if (test1 == vp) { } // expected-warning{{equality comparison between function pointer and void pointer}}
205*67e74705SXin Li if (test1 == e) { } // expected-error{{comparison between pointer and integer}}
206*67e74705SXin Li if (vp < 0) { }
207*67e74705SXin Li if (test1 < e) { } // expected-error{{comparison between pointer and integer}}
208*67e74705SXin Li }
209*67e74705SXin Li
210*67e74705SXin Li // PR7536
211*67e74705SXin Li static const unsigned int kMax = 0;
pr7536()212*67e74705SXin Li int pr7536() {
213*67e74705SXin Li return (kMax > 0);
214*67e74705SXin Li }
215*67e74705SXin Li
216*67e74705SXin Li // -Wsign-compare should not warn when ?: operands have different signedness.
217*67e74705SXin Li // This will be caught by -Wsign-conversion
test3()218*67e74705SXin Li void test3() {
219*67e74705SXin Li unsigned long a;
220*67e74705SXin Li signed long b;
221*67e74705SXin Li (void) (true ? a : b);
222*67e74705SXin Li (void) (true ? (unsigned int)a : (signed int)b);
223*67e74705SXin Li (void) (true ? b : a);
224*67e74705SXin Li (void) (true ? (unsigned char)b : (signed char)a);
225*67e74705SXin Li }
226*67e74705SXin Li
227*67e74705SXin Li // Test comparison of short to unsigned. If tautological compare does not
228*67e74705SXin Li // trigger, then the signed comparison warning will.
test4(short s)229*67e74705SXin Li void test4(short s) {
230*67e74705SXin Li // A is max short plus 1. All zero and positive shorts are smaller than it.
231*67e74705SXin Li // All negative shorts are cast towards the max unsigned range. Relation
232*67e74705SXin Li // comparisons are possible, but equality comparisons are tautological.
233*67e74705SXin Li const unsigned A = 32768;
234*67e74705SXin Li void (s < A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
235*67e74705SXin Li void (s > A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
236*67e74705SXin Li void (s <= A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
237*67e74705SXin Li void (s >= A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
238*67e74705SXin Li
239*67e74705SXin Li void (s == A); // expected-warning{{comparison of constant 32768 with expression of type 'short' is always false}}
240*67e74705SXin Li void (s != A); // expected-warning{{comparison of constant 32768 with expression of type 'short' is always true}}
241*67e74705SXin Li
242*67e74705SXin Li // When negative one is converted to an unsigned value, it becomes the max
243*67e74705SXin Li // unsigned. Likewise, a negative one short can also be converted to max
244*67e74705SXin Li // unsigned.
245*67e74705SXin Li const unsigned B = -1;
246*67e74705SXin Li void (s < B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
247*67e74705SXin Li void (s > B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
248*67e74705SXin Li void (s <= B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
249*67e74705SXin Li void (s >= B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
250*67e74705SXin Li void (s == B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
251*67e74705SXin Li void (s != B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}}
252*67e74705SXin Li
253*67e74705SXin Li }
254*67e74705SXin Li
test5(bool b)255*67e74705SXin Li void test5(bool b) {
256*67e74705SXin Li (void) (b < -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always false}}
257*67e74705SXin Li (void) (b > -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always true}}
258*67e74705SXin Li (void) (b == -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always false}}
259*67e74705SXin Li (void) (b != -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always true}}
260*67e74705SXin Li (void) (b <= -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always false}}
261*67e74705SXin Li (void) (b >= -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always true}}
262*67e74705SXin Li
263*67e74705SXin Li (void) (b < -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always false}}
264*67e74705SXin Li (void) (b > -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always true}}
265*67e74705SXin Li (void) (b == -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always false}}
266*67e74705SXin Li (void) (b != -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always true}}
267*67e74705SXin Li (void) (b <= -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always false}}
268*67e74705SXin Li (void) (b >= -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always true}}
269*67e74705SXin Li
270*67e74705SXin Li (void) (b < 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always true}}
271*67e74705SXin Li (void) (b > 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always false}}
272*67e74705SXin Li (void) (b == 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always false}}
273*67e74705SXin Li (void) (b != 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always true}}
274*67e74705SXin Li (void) (b <= 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always true}}
275*67e74705SXin Li (void) (b >= 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always false}}
276*67e74705SXin Li
277*67e74705SXin Li (void) (b < 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always true}}
278*67e74705SXin Li (void) (b > 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always false}}
279*67e74705SXin Li (void) (b == 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always false}}
280*67e74705SXin Li (void) (b != 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always true}}
281*67e74705SXin Li (void) (b <= 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always true}}
282*67e74705SXin Li (void) (b >= 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always false}}
283*67e74705SXin Li }
284*67e74705SXin Li
test6(signed char sc)285*67e74705SXin Li void test6(signed char sc) {
286*67e74705SXin Li (void)(sc < 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}}
287*67e74705SXin Li (void)(sc > 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}}
288*67e74705SXin Li (void)(sc <= 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}}
289*67e74705SXin Li (void)(sc >= 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}}
290*67e74705SXin Li (void)(sc == 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}}
291*67e74705SXin Li (void)(sc != 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}}
292*67e74705SXin Li
293*67e74705SXin Li (void)(200 < sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}}
294*67e74705SXin Li (void)(200 > sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}}
295*67e74705SXin Li (void)(200 <= sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}}
296*67e74705SXin Li (void)(200 >= sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}}
297*67e74705SXin Li (void)(200 == sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}}
298*67e74705SXin Li (void)(200 != sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}}
299*67e74705SXin Li }
300*67e74705SXin Li
301*67e74705SXin Li // Test many signedness combinations.
test7(unsigned long other)302*67e74705SXin Li void test7(unsigned long other) {
303*67e74705SXin Li // Common unsigned, other unsigned, constant unsigned
304*67e74705SXin Li (void)((unsigned)other != (unsigned long)(0x1ffffffff)); // expected-warning{{true}}
305*67e74705SXin Li (void)((unsigned)other != (unsigned long)(0xffffffff));
306*67e74705SXin Li (void)((unsigned long)other != (unsigned)(0x1ffffffff));
307*67e74705SXin Li (void)((unsigned long)other != (unsigned)(0xffffffff));
308*67e74705SXin Li
309*67e74705SXin Li // Common unsigned, other signed, constant unsigned
310*67e74705SXin Li (void)((int)other != (unsigned long)(0xffffffffffffffff)); // expected-warning{{different signs}}
311*67e74705SXin Li (void)((int)other != (unsigned long)(0x00000000ffffffff)); // expected-warning{{true}}
312*67e74705SXin Li (void)((int)other != (unsigned long)(0x000000000fffffff));
313*67e74705SXin Li (void)((int)other < (unsigned long)(0x00000000ffffffff)); // expected-warning{{different signs}}
314*67e74705SXin Li (void)((int)other == (unsigned)(0x800000000));
315*67e74705SXin Li
316*67e74705SXin Li // Common unsigned, other unsigned, constant signed
317*67e74705SXin Li (void)((unsigned long)other != (int)(0xffffffff)); // expected-warning{{different signs}}
318*67e74705SXin Li
319*67e74705SXin Li // Common unsigned, other signed, constant signed
320*67e74705SXin Li // Should not be possible as the common type should also be signed.
321*67e74705SXin Li
322*67e74705SXin Li // Common signed, other signed, constant signed
323*67e74705SXin Li (void)((int)other != (long)(0xffffffff)); // expected-warning{{true}}
324*67e74705SXin Li (void)((int)other != (long)(0xffffffff00000000)); // expected-warning{{true}}
325*67e74705SXin Li (void)((int)other != (long)(0xfffffff));
326*67e74705SXin Li (void)((int)other != (long)(0xfffffffff0000000));
327*67e74705SXin Li
328*67e74705SXin Li // Common signed, other signed, constant unsigned
329*67e74705SXin Li (void)((int)other != (unsigned char)(0xffff));
330*67e74705SXin Li (void)((int)other != (unsigned char)(0xff));
331*67e74705SXin Li
332*67e74705SXin Li // Common signed, other unsigned, constant signed
333*67e74705SXin Li (void)((unsigned char)other != (int)(0xff));
334*67e74705SXin Li (void)((unsigned char)other != (int)(0xffff)); // expected-warning{{true}}
335*67e74705SXin Li
336*67e74705SXin Li // Common signed, other unsigned, constant unsigned
337*67e74705SXin Li (void)((unsigned char)other != (unsigned short)(0xff));
338*67e74705SXin Li (void)((unsigned char)other != (unsigned short)(0x100)); // expected-warning{{true}}
339*67e74705SXin Li (void)((unsigned short)other != (unsigned char)(0xff));
340*67e74705SXin Li }
341*67e74705SXin Li
test8(int x)342*67e74705SXin Li void test8(int x) {
343*67e74705SXin Li enum E {
344*67e74705SXin Li Negative = -1,
345*67e74705SXin Li Positive = 1
346*67e74705SXin Li };
347*67e74705SXin Li
348*67e74705SXin Li (void)((E)x == 1);
349*67e74705SXin Li (void)((E)x == -1);
350*67e74705SXin Li }
351*67e74705SXin Li
test9(int x)352*67e74705SXin Li void test9(int x) {
353*67e74705SXin Li enum E : int {
354*67e74705SXin Li Positive = 1
355*67e74705SXin Li };
356*67e74705SXin Li (void)((E)x == 1);
357*67e74705SXin Li }
358*67e74705SXin Li
359*67e74705SXin Li namespace templates {
360*67e74705SXin Li template<class T> T max();
361*67e74705SXin Li
max()362*67e74705SXin Li template<> constexpr int max<int>() { return 2147483647; };
363*67e74705SXin Li
364*67e74705SXin Li template<typename T>
less_than_max(short num,T value)365*67e74705SXin Li bool less_than_max(short num, T value) {
366*67e74705SXin Li const T vmax = max<T>();
367*67e74705SXin Li return (vmax >= num); // no warning
368*67e74705SXin Li }
369*67e74705SXin Li
370*67e74705SXin Li template<typename T>
less_than_max(short num)371*67e74705SXin Li bool less_than_max(short num) {
372*67e74705SXin Li // This should trigger one warning on the template pattern, and not a
373*67e74705SXin Li // warning per specialization.
374*67e74705SXin Li return num < max<int>(); // expected-warning{{comparison of constant 2147483647 with expression of type 'short' is always true}}
375*67e74705SXin Li }
376*67e74705SXin Li
test10(short num,int x)377*67e74705SXin Li void test10(short num, int x) {
378*67e74705SXin Li less_than_max(num, x);
379*67e74705SXin Li less_than_max<int>(num);
380*67e74705SXin Li less_than_max<long>(num);
381*67e74705SXin Li less_than_max<short>(num);
382*67e74705SXin Li }
383*67e74705SXin Li
384*67e74705SXin Li template<typename T>
less_than_zero(T num,T value)385*67e74705SXin Li inline bool less_than_zero(T num, T value) {
386*67e74705SXin Li return num < 0; // no warning
387*67e74705SXin Li }
388*67e74705SXin Li
389*67e74705SXin Li template<typename T>
less_than_zero(unsigned num)390*67e74705SXin Li inline bool less_than_zero(unsigned num) {
391*67e74705SXin Li // This should trigger one warning on the template pattern, and not a
392*67e74705SXin Li // warning per specialization.
393*67e74705SXin Li return num < 0; // expected-warning{{comparison of unsigned expression < 0 is always false}}
394*67e74705SXin Li }
395*67e74705SXin Li
test11(unsigned num)396*67e74705SXin Li void test11(unsigned num) {
397*67e74705SXin Li less_than_zero(num, num);
398*67e74705SXin Li less_than_zero<int>(num);
399*67e74705SXin Li less_than_zero<long>(num);
400*67e74705SXin Li less_than_zero<short>(num);
401*67e74705SXin Li }
402*67e74705SXin Li
compare(unsigned k)403*67e74705SXin Li template<unsigned n> bool compare(unsigned k) { return k >= n; }
404*67e74705SXin Li
test12()405*67e74705SXin Li void test12() {
406*67e74705SXin Li compare<0>(42);
407*67e74705SXin Li }
408*67e74705SXin Li
409*67e74705SXin Li struct A { static int x; };
410*67e74705SXin Li struct B { static int x; };
411*67e74705SXin Li typedef A otherA;
412*67e74705SXin Li
413*67e74705SXin Li template <typename T>
testx()414*67e74705SXin Li void testx() {
415*67e74705SXin Li if (A::x == T::x && // no warning
416*67e74705SXin Li A::x == otherA::x) // expected-warning{{self-comparison always evaluates to true}}
417*67e74705SXin Li return;
418*67e74705SXin Li }
419*67e74705SXin Li
test13()420*67e74705SXin Li void test13() {
421*67e74705SXin Li testx<A>();
422*67e74705SXin Li testx<B>();
423*67e74705SXin Li }
424*67e74705SXin Li }
425