blob: d88e3bae415e0528d5189c2540c15d4b014ee2a7 [file] [log] [blame]
Richard Smithc70f1d62017-12-14 15:16:18 +00001// Force x86-64 because some of our heuristics are actually based
2// on integer sizes.
3
4// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify -Wsign-compare -std=c++2a %s
5
Richard Smith32f39732018-01-07 22:03:44 +00006void self_compare() {
7 int a;
8 int b[3], c[3];
9 (void)(a <=> a); // expected-warning {{self-comparison always evaluates to 'std::strong_ordering::equal'}}
10 (void)(b <=> b); // expected-warning {{self-comparison always evaluates to 'std::strong_ordering::equal'}}
11 (void)(b <=> c); // expected-warning {{array comparison always evaluates to a constant}}
12}
13
Richard Smithc70f1d62017-12-14 15:16:18 +000014void test0(long a, unsigned long b) {
15 enum EnumA {A};
16 enum EnumB {B};
17 enum EnumC {C = 0x10000};
18 // (a,b)
19
20 // FIXME: <=> should never produce -Wsign-compare warnings. All the possible error
21 // cases involve narrowing conversions and so are ill-formed.
22 (void)(a <=> (unsigned long) b); // expected-warning {{comparison of integers of different signs}}
23 (void)(a <=> (unsigned int) b);
24 (void)(a <=> (unsigned short) b);
25 (void)(a <=> (unsigned char) b);
26 (void)((long) a <=> b); // expected-warning {{comparison of integers of different signs}}
27 (void)((int) a <=> b); // expected-warning {{comparison of integers of different signs}}
28 (void)((short) a <=> b); // expected-warning {{comparison of integers of different signs}}
29 (void)((signed char) a <=> b); // expected-warning {{comparison of integers of different signs}}
30 (void)((long) a <=> (unsigned long) b); // expected-warning {{comparison of integers of different signs}}
31 (void)((int) a <=> (unsigned int) b); // expected-warning {{comparison of integers of different signs}}
32 (void)((short) a <=> (unsigned short) b);
33 (void)((signed char) a <=> (unsigned char) b);
34
35#if 0
36 // (A,b)
37 (void)(A <=> (unsigned long) b);
38 (void)(A <=> (unsigned int) b);
39 (void)(A <=> (unsigned short) b);
40 (void)(A <=> (unsigned char) b);
41 (void)((long) A <=> b);
42 (void)((int) A <=> b);
43 (void)((short) A <=> b);
44 (void)((signed char) A <=> b);
45 (void)((long) A <=> (unsigned long) b);
46 (void)((int) A <=> (unsigned int) b);
47 (void)((short) A <=> (unsigned short) b);
48 (void)((signed char) A <=> (unsigned char) b);
49
50 // (a,B)
51 (void)(a <=> (unsigned long) B);
52 (void)(a <=> (unsigned int) B);
53 (void)(a <=> (unsigned short) B);
54 (void)(a <=> (unsigned char) B);
55 (void)((long) a <=> B);
56 (void)((int) a <=> B);
57 (void)((short) a <=> B);
58 (void)((signed char) a <=> B);
59 (void)((long) a <=> (unsigned long) B);
60 (void)((int) a <=> (unsigned int) B);
61 (void)((short) a <=> (unsigned short) B);
62 (void)((signed char) a <=> (unsigned char) B);
63
64 // (C,b)
65 (void)(C <=> (unsigned long) b);
66 (void)(C <=> (unsigned int) b);
67 (void)(C <=> (unsigned short) b); // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always 'std::strong_ordering::greater'}}
68 (void)(C <=> (unsigned char) b); // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always 'std::strong_ordering::greater'}}
69 (void)((long) C <=> b);
70 (void)((int) C <=> b);
71 (void)((short) C <=> b);
72 (void)((signed char) C <=> b);
73 (void)((long) C <=> (unsigned long) b);
74 (void)((int) C <=> (unsigned int) b);
75 (void)((short) C <=> (unsigned short) b);
76 (void)((signed char) C <=> (unsigned char) b);
77
78 // (a,C)
79 (void)(a <=> (unsigned long) C);
80 (void)(a <=> (unsigned int) C);
81 (void)(a <=> (unsigned short) C);
82 (void)(a <=> (unsigned char) C);
83 (void)((long) a <=> C);
84 (void)((int) a <=> C);
85 (void)((short) a <=> C); // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always 'std::strong_ordering::less'}}
86 (void)((signed char) a <=> C); // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always 'std::strong_ordering::less'}}
87 (void)((long) a <=> (unsigned long) C);
88 (void)((int) a <=> (unsigned int) C);
89 (void)((short) a <=> (unsigned short) C);
90 (void)((signed char) a <=> (unsigned char) C);
91#endif
92
93 // (0x80000,b)
94 (void)(0x80000 <=> (unsigned long) b);
95 (void)(0x80000 <=> (unsigned int) b);
96 (void)(0x80000 <=> (unsigned short) b); // expected-warning {{result of comparison of constant 524288 with expression of type 'unsigned short' is always 'std::strong_ordering::greater'}}
97 (void)(0x80000 <=> (unsigned char) b); // expected-warning {{result of comparison of constant 524288 with expression of type 'unsigned char' is always 'std::strong_ordering::greater'}}
98 (void)((long) 0x80000 <=> b);
99 (void)((int) 0x80000 <=> b);
100 (void)((short) 0x80000 <=> b);
101 (void)((signed char) 0x80000 <=> b);
102 (void)((long) 0x80000 <=> (unsigned long) b);
103 (void)((int) 0x80000 <=> (unsigned int) b);
104 (void)((short) 0x80000 <=> (unsigned short) b);
105 (void)((signed char) 0x80000 <=> (unsigned char) b);
106
107 // (a,0x80000)
108 (void)(a <=> (unsigned long) 0x80000); // expected-warning {{comparison of integers of different signs}}
109 (void)(a <=> (unsigned int) 0x80000);
110 (void)(a <=> (unsigned short) 0x80000);
111 (void)(a <=> (unsigned char) 0x80000);
112 (void)((long) a <=> 0x80000);
113 (void)((int) a <=> 0x80000);
114 (void)((short) a <=> 0x80000); // expected-warning {{comparison of constant 524288 with expression of type 'short' is always 'std::strong_ordering::less'}}
115 (void)((signed char) a <=> 0x80000); // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always 'std::strong_ordering::less'}}
116 (void)((long) a <=> (unsigned long) 0x80000); // expected-warning {{comparison of integers of different signs}}
117 (void)((int) a <=> (unsigned int) 0x80000); // expected-warning {{comparison of integers of different signs}}
118 (void)((short) a <=> (unsigned short) 0x80000);
119 (void)((signed char) a <=> (unsigned char) 0x80000);
120}
121
122void test5(bool b) {
123 (void) (b <=> -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always 'std::strong_ordering::greater'}}
124 (void) (b <=> -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always 'std::strong_ordering::greater'}}
125 (void) (b <=> 0);
126 (void) (b <=> 1);
127 (void) (b <=> 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always 'std::strong_ordering::less'}}
128 (void) (b <=> 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always 'std::strong_ordering::less'}}
129}
130
131void test6(signed char sc) {
132 (void)(sc <=> 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always 'std::strong_ordering::less'}}
133 (void)(200 <=> sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always 'std::strong_ordering::greater'}}
134}
135
136// Test many signedness combinations.
137void test7(unsigned long other) {
138 // Common unsigned, other unsigned, constant unsigned
139 (void)((unsigned)other <=> (unsigned long)(0x1'ffff'ffff)); // expected-warning{{less}}
140 (void)((unsigned)other <=> (unsigned long)(0xffff'ffff));
141 (void)((unsigned long)other <=> (unsigned)(0x1'ffff'ffff));
142 (void)((unsigned long)other <=> (unsigned)(0xffff'ffff));
143
144 // Common unsigned, other signed, constant unsigned
145 (void)((int)other <=> (unsigned long)(0xffff'ffff'ffff'ffff)); // expected-warning{{different signs}}
146 (void)((int)other <=> (unsigned long)(0x0000'0000'ffff'ffff)); // expected-warning{{different signs}}
147 (void)((int)other <=> (unsigned long)(0x0000'0000'0fff'ffff)); // expected-warning{{different signs}}
148 (void)((int)other <=> (unsigned)(0x8000'0000)); // expected-warning{{different signs}}
149
150 // Common unsigned, other unsigned, constant signed
151 (void)((unsigned long)other <=> (int)(0xffff'ffff)); // expected-warning{{different signs}}
152
153 // Common unsigned, other signed, constant signed
154 // Should not be possible as the common type should also be signed.
155
156 // Common signed, other signed, constant signed
157 (void)((int)other <=> (long)(0xffff'ffff)); // expected-warning{{less}}
158 (void)((int)other <=> (long)(0xffff'ffff'0000'0000)); // expected-warning{{greater}}
159 (void)((int)other <=> (long)(0x0fff'ffff));
160 (void)((int)other <=> (long)(0xffff'ffff'f000'0000));
161
162 // Common signed, other signed, constant unsigned
163 (void)((int)other <=> (unsigned char)(0xffff));
164 (void)((int)other <=> (unsigned char)(0xff));
165
166 // Common signed, other unsigned, constant signed
167 (void)((unsigned char)other <=> (int)(0xff));
168 (void)((unsigned char)other <=> (int)(0xffff)); // expected-warning{{less}}
169
170 // Common signed, other unsigned, constant unsigned
171 (void)((unsigned char)other <=> (unsigned short)(0xff));
172 (void)((unsigned char)other <=> (unsigned short)(0x100)); // expected-warning{{less}}
173 (void)((unsigned short)other <=> (unsigned char)(0xff));
174}