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