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