Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -fsyntax-only -DUNSIGNED -verify %s |
| 2 | // RUN: %clang_cc1 -triple=x86_64-pc-win32 -fsyntax-only -DSIGNED -verify %s |
| 3 | // RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -fsyntax-only -DUNSIGNED -DSILENCE -Wno-tautological-constant-compare -verify %s |
| 4 | // RUN: %clang_cc1 -triple=x86_64-pc-win32 -fsyntax-only -DSIGNED -DSILENCE -Wno-tautological-constant-compare -verify %s |
| 5 | |
| 6 | int main() { |
| 7 | enum A { A_a = 2 }; |
| 8 | enum A a; |
| 9 | |
| 10 | #ifdef SILENCE |
| 11 | // expected-no-diagnostics |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 12 | #else |
| 13 | // If we promote to unsigned, it doesn't matter whether the enum's underlying |
| 14 | // type was signed. |
Hans Wennborg | 5791ce7 | 2017-12-08 16:54:08 +0000 | [diff] [blame] | 15 | if (a < 0U) // expected-warning {{comparison of unsigned enum expression < 0 is always false}} |
| 16 | return 0; |
| 17 | if (0U >= a) |
| 18 | return 0; |
| 19 | if (a > 0U) |
| 20 | return 0; |
| 21 | if (0U <= a) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}} |
| 22 | return 0; |
| 23 | if (a <= 0U) |
| 24 | return 0; |
| 25 | if (0U > a) // expected-warning {{comparison of 0 > unsigned enum expression is always false}} |
| 26 | return 0; |
| 27 | if (a >= 0U) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}} |
| 28 | return 0; |
| 29 | if (0U < a) |
| 30 | return 0; |
| 31 | |
Hans Wennborg | 5791ce7 | 2017-12-08 16:54:08 +0000 | [diff] [blame] | 32 | if (a < 4294967295U) |
| 33 | return 0; |
| 34 | if (4294967295U >= a) // expected-warning {{comparison 4294967295 >= 'enum A' is always true}} |
| 35 | return 0; |
| 36 | if (a > 4294967295U) // expected-warning {{comparison 'enum A' > 4294967295 is always false}} |
| 37 | return 0; |
| 38 | if (4294967295U <= a) |
| 39 | return 0; |
| 40 | if (a <= 4294967295U) // expected-warning {{comparison 'enum A' <= 4294967295 is always true}} |
| 41 | return 0; |
| 42 | if (4294967295U > a) |
| 43 | return 0; |
| 44 | if (a >= 4294967295U) |
| 45 | return 0; |
| 46 | if (4294967295U < a) // expected-warning {{comparison 4294967295 < 'enum A' is always false}} |
| 47 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 48 | |
| 49 | if (a < 2147483647U) |
| 50 | return 0; |
| 51 | if (2147483647U >= a) |
| 52 | return 0; |
| 53 | if (a > 2147483647U) |
| 54 | return 0; |
| 55 | if (2147483647U <= a) |
| 56 | return 0; |
| 57 | if (a <= 2147483647U) |
| 58 | return 0; |
| 59 | if (2147483647U > a) |
| 60 | return 0; |
| 61 | if (a >= 2147483647U) |
| 62 | return 0; |
| 63 | if (2147483647U < a) |
| 64 | return 0; |
| 65 | #endif |
| 66 | |
| 67 | #if defined(UNSIGNED) && !defined(SILENCE) |
| 68 | if (a < 0) // expected-warning {{comparison of unsigned enum expression < 0 is always false}} |
| 69 | return 0; |
| 70 | if (0 >= a) |
| 71 | return 0; |
| 72 | if (a > 0) |
| 73 | return 0; |
| 74 | if (0 <= a) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}} |
| 75 | return 0; |
| 76 | if (a <= 0) |
| 77 | return 0; |
| 78 | if (0 > a) // expected-warning {{comparison of 0 > unsigned enum expression is always false}} |
| 79 | return 0; |
| 80 | if (a >= 0) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}} |
| 81 | return 0; |
| 82 | if (0 < a) |
| 83 | return 0; |
| 84 | |
| 85 | if (a < 4294967295) |
| 86 | return 0; |
| 87 | if (4294967295 >= a) // expected-warning {{comparison 4294967295 >= 'enum A' is always true}} |
| 88 | return 0; |
| 89 | if (a > 4294967295) // expected-warning {{comparison 'enum A' > 4294967295 is always false}} |
| 90 | return 0; |
| 91 | if (4294967295 <= a) |
| 92 | return 0; |
| 93 | if (a <= 4294967295) // expected-warning {{comparison 'enum A' <= 4294967295 is always true}} |
| 94 | return 0; |
| 95 | if (4294967295 > a) |
| 96 | return 0; |
| 97 | if (a >= 4294967295) |
| 98 | return 0; |
| 99 | if (4294967295 < a) // expected-warning {{comparison 4294967295 < 'enum A' is always false}} |
| 100 | return 0; |
| 101 | #else // SIGNED || SILENCE |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 102 | if (a < 0) |
| 103 | return 0; |
| 104 | if (0 >= a) |
| 105 | return 0; |
| 106 | if (a > 0) |
| 107 | return 0; |
| 108 | if (0 <= a) |
| 109 | return 0; |
| 110 | if (a <= 0) |
| 111 | return 0; |
| 112 | if (0 > a) |
| 113 | return 0; |
| 114 | if (a >= 0) |
| 115 | return 0; |
| 116 | if (0 < a) |
| 117 | return 0; |
| 118 | |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 119 | #ifndef SILENCE |
| 120 | if (a < 4294967295) // expected-warning {{comparison of constant 4294967295 with expression of type 'enum A' is always true}} |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 121 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 122 | if (4294967295 >= a) // expected-warning {{comparison of constant 4294967295 with expression of type 'enum A' is always true}} |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 123 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 124 | if (a > 4294967295) // expected-warning {{comparison of constant 4294967295 with expression of type 'enum A' is always false}} |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 125 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 126 | if (4294967295 <= a) // expected-warning {{comparison of constant 4294967295 with expression of type 'enum A' is always false}} |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 127 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 128 | if (a <= 4294967295) // expected-warning {{comparison of constant 4294967295 with expression of type 'enum A' is always true}} |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 129 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 130 | if (4294967295 > a) // expected-warning {{comparison of constant 4294967295 with expression of type 'enum A' is always true}} |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 131 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 132 | if (a >= 4294967295) // expected-warning {{comparison of constant 4294967295 with expression of type 'enum A' is always false}} |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 133 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 134 | if (4294967295 < a) // expected-warning {{comparison of constant 4294967295 with expression of type 'enum A' is always false}} |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 135 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 136 | #else |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 137 | if (a < 4294967295) |
| 138 | return 0; |
| 139 | if (4294967295 >= a) |
| 140 | return 0; |
| 141 | if (a > 4294967295) |
| 142 | return 0; |
| 143 | if (4294967295 <= a) |
| 144 | return 0; |
| 145 | if (a <= 4294967295) |
| 146 | return 0; |
| 147 | if (4294967295 > a) |
| 148 | return 0; |
| 149 | if (a >= 4294967295) |
| 150 | return 0; |
| 151 | if (4294967295 < a) |
| 152 | return 0; |
Hans Wennborg | 5791ce7 | 2017-12-08 16:54:08 +0000 | [diff] [blame] | 153 | #endif |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 154 | #endif |
| 155 | |
| 156 | #if defined(SIGNED) && !defined(SILENCE) |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 157 | if (a < -2147483648) // expected-warning {{comparison 'enum A' < -2147483648 is always false}} |
| 158 | return 0; |
| 159 | if (-2147483648 >= a) |
| 160 | return 0; |
| 161 | if (a > -2147483648) |
| 162 | return 0; |
| 163 | if (-2147483648 <= a) // expected-warning {{comparison -2147483648 <= 'enum A' is always true}} |
| 164 | return 0; |
| 165 | if (a <= -2147483648) |
| 166 | return 0; |
| 167 | if (-2147483648 > a) // expected-warning {{comparison -2147483648 > 'enum A' is always false}} |
| 168 | return 0; |
| 169 | if (a >= -2147483648) // expected-warning {{comparison 'enum A' >= -2147483648 is always true}} |
| 170 | return 0; |
| 171 | if (-2147483648 < a) |
| 172 | return 0; |
| 173 | |
| 174 | if (a < 2147483647) |
| 175 | return 0; |
| 176 | if (2147483647 >= a) // expected-warning {{comparison 2147483647 >= 'enum A' is always true}} |
| 177 | return 0; |
| 178 | if (a > 2147483647) // expected-warning {{comparison 'enum A' > 2147483647 is always false}} |
| 179 | return 0; |
| 180 | if (2147483647 <= a) |
| 181 | return 0; |
| 182 | if (a <= 2147483647) // expected-warning {{comparison 'enum A' <= 2147483647 is always true}} |
| 183 | return 0; |
| 184 | if (2147483647 > a) |
| 185 | return 0; |
| 186 | if (a >= 2147483647) |
| 187 | return 0; |
| 188 | if (2147483647 < a) // expected-warning {{comparison 2147483647 < 'enum A' is always false}} |
| 189 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 190 | #elif defined(UNSIGNED) && !defined(SILENCE) |
| 191 | #ifndef SILENCE |
| 192 | if (a < -2147483648) // expected-warning {{comparison of constant -2147483648 with expression of type 'enum A' is always false}} |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 193 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 194 | if (-2147483648 >= a) // expected-warning {{comparison of constant -2147483648 with expression of type 'enum A' is always false}} |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 195 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 196 | if (a > -2147483648) // expected-warning {{comparison of constant -2147483648 with expression of type 'enum A' is always true}} |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 197 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 198 | if (-2147483648 <= a) // expected-warning {{comparison of constant -2147483648 with expression of type 'enum A' is always true}} |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 199 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 200 | if (a <= -2147483648) // expected-warning {{comparison of constant -2147483648 with expression of type 'enum A' is always false}} |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 201 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 202 | if (-2147483648 > a) // expected-warning {{comparison of constant -2147483648 with expression of type 'enum A' is always false}} |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 203 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 204 | if (a >= -2147483648) // expected-warning {{comparison of constant -2147483648 with expression of type 'enum A' is always true}} |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 205 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 206 | if (-2147483648 < a) // expected-warning {{comparison of constant -2147483648 with expression of type 'enum A' is always true}} |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 207 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 208 | #else |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 209 | if (a < -2147483648) |
| 210 | return 0; |
| 211 | if (-2147483648 >= a) |
| 212 | return 0; |
| 213 | if (a > -2147483648) |
| 214 | return 0; |
| 215 | if (-2147483648 <= a) |
| 216 | return 0; |
| 217 | if (a <= -2147483648) |
| 218 | return 0; |
| 219 | if (-2147483648 > a) |
| 220 | return 0; |
| 221 | if (a >= -2147483648) |
| 222 | return 0; |
| 223 | if (-2147483648 < a) |
| 224 | return 0; |
Richard Smith | a5370fb | 2017-12-08 22:57:11 +0000 | [diff] [blame^] | 225 | #endif |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 226 | |
| 227 | if (a < 2147483647) |
| 228 | return 0; |
| 229 | if (2147483647 >= a) |
| 230 | return 0; |
| 231 | if (a > 2147483647) |
| 232 | return 0; |
| 233 | if (2147483647 <= a) |
| 234 | return 0; |
| 235 | if (a <= 2147483647) |
| 236 | return 0; |
| 237 | if (2147483647 > a) |
| 238 | return 0; |
| 239 | if (a >= 2147483647) |
| 240 | return 0; |
| 241 | if (2147483647 < a) |
| 242 | return 0; |
Roman Lebedev | ca1aaac | 2017-10-21 16:44:03 +0000 | [diff] [blame] | 243 | #endif |
| 244 | |
| 245 | return 1; |
| 246 | } |
| 247 | |
| 248 | // https://bugs.llvm.org/show_bug.cgi?id=35009 |
| 249 | int PR35009() { |
| 250 | enum A { A_a = 2 }; |
| 251 | enum A a; |
| 252 | |
| 253 | // in C, this should not warn. |
| 254 | |
| 255 | if (a < 1) |
| 256 | return 0; |
| 257 | if (1 >= a) |
| 258 | return 0; |
| 259 | if (a > 1) |
| 260 | return 0; |
| 261 | if (1 <= a) |
| 262 | return 0; |
| 263 | if (a <= 1) |
| 264 | return 0; |
| 265 | if (1 > a) |
| 266 | return 0; |
| 267 | if (a >= 1) |
| 268 | return 0; |
| 269 | if (1 < a) |
| 270 | return 0; |
| 271 | if (a == 1) |
| 272 | return 0; |
| 273 | if (1 != a) |
| 274 | return 0; |
| 275 | if (a != 1) |
| 276 | return 0; |
| 277 | if (1 == a) |
| 278 | return 0; |
| 279 | |
| 280 | if (a < 1U) |
| 281 | return 0; |
| 282 | if (1U >= a) |
| 283 | return 0; |
| 284 | if (a > 1U) |
| 285 | return 0; |
| 286 | if (1U <= a) |
| 287 | return 0; |
| 288 | if (a <= 1U) |
| 289 | return 0; |
| 290 | if (1U > a) |
| 291 | return 0; |
| 292 | if (a >= 1U) |
| 293 | return 0; |
| 294 | if (1U < a) |
| 295 | return 0; |
| 296 | if (a == 1U) |
| 297 | return 0; |
| 298 | if (1U != a) |
| 299 | return 0; |
| 300 | if (a != 1U) |
| 301 | return 0; |
| 302 | if (1U == a) |
| 303 | return 0; |
| 304 | |
| 305 | if (a < 2) |
| 306 | return 0; |
| 307 | if (2 >= a) |
| 308 | return 0; |
| 309 | if (a > 2) |
| 310 | return 0; |
| 311 | if (2 <= a) |
| 312 | return 0; |
| 313 | if (a <= 2) |
| 314 | return 0; |
| 315 | if (2 > a) |
| 316 | return 0; |
| 317 | if (a >= 2) |
| 318 | return 0; |
| 319 | if (2 < a) |
| 320 | return 0; |
| 321 | if (a == 2) |
| 322 | return 0; |
| 323 | if (2 != a) |
| 324 | return 0; |
| 325 | if (a != 2) |
| 326 | return 0; |
| 327 | if (2 == a) |
| 328 | return 0; |
| 329 | |
| 330 | if (a < 2U) |
| 331 | return 0; |
| 332 | if (2U >= a) |
| 333 | return 0; |
| 334 | if (a > 2U) |
| 335 | return 0; |
| 336 | if (2U <= a) |
| 337 | return 0; |
| 338 | if (a <= 2U) |
| 339 | return 0; |
| 340 | if (2U > a) |
| 341 | return 0; |
| 342 | if (a >= 2U) |
| 343 | return 0; |
| 344 | if (2U < a) |
| 345 | return 0; |
| 346 | if (a == 2U) |
| 347 | return 0; |
| 348 | if (2U != a) |
| 349 | return 0; |
| 350 | if (a != 2U) |
| 351 | return 0; |
| 352 | if (2U == a) |
| 353 | return 0; |
| 354 | |
| 355 | if (a < 3) |
| 356 | return 0; |
| 357 | if (3 >= a) |
| 358 | return 0; |
| 359 | if (a > 3) |
| 360 | return 0; |
| 361 | if (3 <= a) |
| 362 | return 0; |
| 363 | if (a <= 3) |
| 364 | return 0; |
| 365 | if (3 > a) |
| 366 | return 0; |
| 367 | if (a >= 3) |
| 368 | return 0; |
| 369 | if (3 < a) |
| 370 | return 0; |
| 371 | if (a == 3) |
| 372 | return 0; |
| 373 | if (3 != a) |
| 374 | return 0; |
| 375 | if (a != 3) |
| 376 | return 0; |
| 377 | if (3 == a) |
| 378 | return 0; |
| 379 | |
| 380 | if (a < 3U) |
| 381 | return 0; |
| 382 | if (3U >= a) |
| 383 | return 0; |
| 384 | if (a > 3U) |
| 385 | return 0; |
| 386 | if (3U <= a) |
| 387 | return 0; |
| 388 | if (a <= 3U) |
| 389 | return 0; |
| 390 | if (3U > a) |
| 391 | return 0; |
| 392 | if (a >= 3U) |
| 393 | return 0; |
| 394 | if (3U < a) |
| 395 | return 0; |
| 396 | if (a == 3U) |
| 397 | return 0; |
| 398 | if (3U != a) |
| 399 | return 0; |
| 400 | if (a != 3U) |
| 401 | return 0; |
| 402 | if (3U == a) |
| 403 | return 0; |
| 404 | |
| 405 | return 1; |
| 406 | } |