Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -DTEST -verify %s |
| 2 | // RUN: %clang_cc1 -fsyntax-only -Wno-tautological-unsigned-zero-compare -verify %s |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame^] | 3 | // RUN: %clang_cc1 -fsyntax-only -DTEST -verify -x c++ %s |
| 4 | // RUN: %clang_cc1 -fsyntax-only -Wno-tautological-unsigned-zero-compare -verify -x c++ %s |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 5 | |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame^] | 6 | unsigned uvalue(void); |
| 7 | signed int svalue(void); |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 8 | |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame^] | 9 | #define macro(val) val |
| 10 | |
| 11 | #ifdef __cplusplus |
| 12 | template<typename T> |
| 13 | void TFunc() { |
| 14 | // Make sure that we do warn for normal variables in template functions ! |
| 15 | unsigned char c = svalue(); |
| 16 | #ifdef TEST |
| 17 | if (c < 0) // expected-warning {{comparison of unsigned expression < 0 is always false}} |
| 18 | return; |
| 19 | #else |
| 20 | if (c < 0) |
| 21 | return; |
| 22 | #endif |
| 23 | |
| 24 | if (c < macro(0)) |
| 25 | return; |
| 26 | |
| 27 | T v = svalue(); |
| 28 | if (v < 0) |
| 29 | return; |
| 30 | } |
| 31 | #endif |
| 32 | |
| 33 | int main() |
| 34 | { |
| 35 | #ifdef __cplusplus |
| 36 | TFunc<unsigned char>(); |
| 37 | TFunc<unsigned short>(); |
| 38 | #endif |
| 39 | |
| 40 | unsigned un = uvalue(); |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 41 | |
| 42 | #ifdef TEST |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame^] | 43 | if (un == 0) |
| 44 | return 0; |
| 45 | if (un != 0) |
| 46 | return 0; |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 47 | if (un < 0) // expected-warning {{comparison of unsigned expression < 0 is always false}} |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame^] | 48 | return 0; |
| 49 | if (un <= 0) |
| 50 | return 0; |
| 51 | if (un > 0) |
| 52 | return 0; |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 53 | if (un >= 0) // expected-warning {{comparison of unsigned expression >= 0 is always true}} |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame^] | 54 | return 0; |
| 55 | |
| 56 | if (0 == un) |
| 57 | return 0; |
| 58 | if (0 != un) |
| 59 | return 0; |
| 60 | if (0 < un) |
| 61 | return 0; |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 62 | if (0 <= un) // expected-warning {{comparison of 0 <= unsigned expression is always true}} |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame^] | 63 | return 0; |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 64 | if (0 > un) // expected-warning {{comparison of 0 > unsigned expression is always false}} |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame^] | 65 | return 0; |
| 66 | if (0 >= un) |
| 67 | return 0; |
| 68 | |
| 69 | if (un == 0UL) |
| 70 | return 0; |
| 71 | if (un != 0UL) |
| 72 | return 0; |
| 73 | if (un < 0UL) // expected-warning {{comparison of unsigned expression < 0 is always false}} |
| 74 | return 0; |
| 75 | if (un <= 0UL) |
| 76 | return 0; |
| 77 | if (un > 0UL) |
| 78 | return 0; |
| 79 | if (un >= 0UL) // expected-warning {{comparison of unsigned expression >= 0 is always true}} |
| 80 | return 0; |
| 81 | |
| 82 | if (0UL == un) |
| 83 | return 0; |
| 84 | if (0UL != un) |
| 85 | return 0; |
| 86 | if (0UL < un) |
| 87 | return 0; |
| 88 | if (0UL <= un) // expected-warning {{comparison of 0 <= unsigned expression is always true}} |
| 89 | return 0; |
| 90 | if (0UL > un) // expected-warning {{comparison of 0 > unsigned expression is always false}} |
| 91 | return 0; |
| 92 | if (0UL >= un) |
| 93 | return 0; |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 94 | #else |
| 95 | // expected-no-diagnostics |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame^] | 96 | if (un == 0) |
| 97 | return 0; |
| 98 | if (un != 0) |
| 99 | return 0; |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 100 | if (un < 0) |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame^] | 101 | return 0; |
| 102 | if (un <= 0) |
| 103 | return 0; |
| 104 | if (un > 0) |
| 105 | return 0; |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 106 | if (un >= 0) |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame^] | 107 | return 0; |
| 108 | |
| 109 | if (0 == un) |
| 110 | return 0; |
| 111 | if (0 != un) |
| 112 | return 0; |
| 113 | if (0 < un) |
| 114 | return 0; |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 115 | if (0 <= un) |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame^] | 116 | return 0; |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 117 | if (0 > un) |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame^] | 118 | return 0; |
| 119 | if (0 >= un) |
| 120 | return 0; |
| 121 | |
| 122 | if (un == 0UL) |
| 123 | return 0; |
| 124 | if (un != 0UL) |
| 125 | return 0; |
| 126 | if (un < 0UL) |
| 127 | return 0; |
| 128 | if (un <= 0UL) |
| 129 | return 0; |
| 130 | if (un > 0UL) |
| 131 | return 0; |
| 132 | if (un >= 0UL) |
| 133 | return 0; |
| 134 | |
| 135 | if (0UL == un) |
| 136 | return 0; |
| 137 | if (0UL != un) |
| 138 | return 0; |
| 139 | if (0UL < un) |
| 140 | return 0; |
| 141 | if (0UL <= un) |
| 142 | return 0; |
| 143 | if (0UL > un) |
| 144 | return 0; |
| 145 | if (0UL >= un) |
| 146 | return 0; |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 147 | #endif |
| 148 | |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame^] | 149 | |
| 150 | signed int a = svalue(); |
| 151 | |
| 152 | #ifdef TEST |
| 153 | if (a == 0) |
| 154 | return 0; |
| 155 | if (a != 0) |
| 156 | return 0; |
| 157 | if (a < 0) |
| 158 | return 0; |
| 159 | if (a <= 0) |
| 160 | return 0; |
| 161 | if (a > 0) |
| 162 | return 0; |
| 163 | if (a >= 0) |
| 164 | return 0; |
| 165 | |
| 166 | if (0 == a) |
| 167 | return 0; |
| 168 | if (0 != a) |
| 169 | return 0; |
| 170 | if (0 < a) |
| 171 | return 0; |
| 172 | if (0 <= a) |
| 173 | return 0; |
| 174 | if (0 > a) |
| 175 | return 0; |
| 176 | if (0 >= a) |
| 177 | return 0; |
| 178 | |
| 179 | if (a == 0UL) |
| 180 | return 0; |
| 181 | if (a != 0UL) |
| 182 | return 0; |
| 183 | if (a < 0UL) // expected-warning {{comparison of unsigned expression < 0 is always false}} |
| 184 | return 0; |
| 185 | if (a <= 0UL) |
| 186 | return 0; |
| 187 | if (a > 0UL) |
| 188 | return 0; |
| 189 | if (a >= 0UL) // expected-warning {{comparison of unsigned expression >= 0 is always true}} |
| 190 | return 0; |
| 191 | |
| 192 | if (0UL == a) |
| 193 | return 0; |
| 194 | if (0UL != a) |
| 195 | return 0; |
| 196 | if (0UL < a) |
| 197 | return 0; |
| 198 | if (0UL <= a) // expected-warning {{comparison of 0 <= unsigned expression is always true}} |
| 199 | return 0; |
| 200 | if (0UL > a) // expected-warning {{comparison of 0 > unsigned expression is always false}} |
| 201 | return 0; |
| 202 | if (0UL >= a) |
| 203 | return 0; |
| 204 | #else |
| 205 | // expected-no-diagnostics |
| 206 | if (a == 0) |
| 207 | return 0; |
| 208 | if (a != 0) |
| 209 | return 0; |
| 210 | if (a < 0) |
| 211 | return 0; |
| 212 | if (a <= 0) |
| 213 | return 0; |
| 214 | if (a > 0) |
| 215 | return 0; |
| 216 | if (a >= 0) |
| 217 | return 0; |
| 218 | |
| 219 | if (0 == a) |
| 220 | return 0; |
| 221 | if (0 != a) |
| 222 | return 0; |
| 223 | if (0 < a) |
| 224 | return 0; |
| 225 | if (0 <= a) |
| 226 | return 0; |
| 227 | if (0 > a) |
| 228 | return 0; |
| 229 | if (0 >= a) |
| 230 | return 0; |
| 231 | |
| 232 | if (a == 0UL) |
| 233 | return 0; |
| 234 | if (a != 0UL) |
| 235 | return 0; |
| 236 | if (a < 0UL) |
| 237 | return 0; |
| 238 | if (a <= 0UL) |
| 239 | return 0; |
| 240 | if (a > 0UL) |
| 241 | return 0; |
| 242 | if (a >= 0UL) |
| 243 | return 0; |
| 244 | |
| 245 | if (0UL == a) |
| 246 | return 0; |
| 247 | if (0UL != a) |
| 248 | return 0; |
| 249 | if (0UL < a) |
| 250 | return 0; |
| 251 | if (0UL <= a) |
| 252 | return 0; |
| 253 | if (0UL > a) |
| 254 | return 0; |
| 255 | if (0UL >= a) |
| 256 | return 0; |
| 257 | #endif |
| 258 | |
| 259 | |
| 260 | float fl = 0; |
| 261 | |
| 262 | if (fl == 0) |
| 263 | return 0; |
| 264 | if (fl != 0) |
| 265 | return 0; |
| 266 | if (fl < 0) |
| 267 | return 0; |
| 268 | if (fl <= 0) |
| 269 | return 0; |
| 270 | if (fl > 0) |
| 271 | return 0; |
| 272 | if (fl >= 0) |
| 273 | return 0; |
| 274 | |
| 275 | if (0 == fl) |
| 276 | return 0; |
| 277 | if (0 != fl) |
| 278 | return 0; |
| 279 | if (0 < fl) |
| 280 | return 0; |
| 281 | if (0 <= fl) |
| 282 | return 0; |
| 283 | if (0 > fl) |
| 284 | return 0; |
| 285 | if (0 >= fl) |
| 286 | return 0; |
| 287 | |
| 288 | if (fl == 0UL) |
| 289 | return 0; |
| 290 | if (fl != 0UL) |
| 291 | return 0; |
| 292 | if (fl < 0UL) |
| 293 | return 0; |
| 294 | if (fl <= 0UL) |
| 295 | return 0; |
| 296 | if (fl > 0UL) |
| 297 | return 0; |
| 298 | if (fl >= 0UL) |
| 299 | return 0; |
| 300 | |
| 301 | if (0UL == fl) |
| 302 | return 0; |
| 303 | if (0UL != fl) |
| 304 | return 0; |
| 305 | if (0UL < fl) |
| 306 | return 0; |
| 307 | if (0UL <= fl) |
| 308 | return 0; |
| 309 | if (0UL > fl) |
| 310 | return 0; |
| 311 | if (0UL >= fl) |
| 312 | return 0; |
| 313 | |
| 314 | |
| 315 | double dl = 0; |
| 316 | |
| 317 | if (dl == 0) |
| 318 | return 0; |
| 319 | if (dl != 0) |
| 320 | return 0; |
| 321 | if (dl < 0) |
| 322 | return 0; |
| 323 | if (dl <= 0) |
| 324 | return 0; |
| 325 | if (dl > 0) |
| 326 | return 0; |
| 327 | if (dl >= 0) |
| 328 | return 0; |
| 329 | |
| 330 | if (0 == dl) |
| 331 | return 0; |
| 332 | if (0 != dl) |
| 333 | return 0; |
| 334 | if (0 < dl) |
| 335 | return 0; |
| 336 | if (0 <= dl) |
| 337 | return 0; |
| 338 | if (0 > dl) |
| 339 | return 0; |
| 340 | if (0 >= dl) |
| 341 | return 0; |
| 342 | |
| 343 | if (dl == 0UL) |
| 344 | return 0; |
| 345 | if (dl != 0UL) |
| 346 | return 0; |
| 347 | if (dl < 0UL) |
| 348 | return 0; |
| 349 | if (dl <= 0UL) |
| 350 | return 0; |
| 351 | if (dl > 0UL) |
| 352 | return 0; |
| 353 | if (dl >= 0UL) |
| 354 | return 0; |
| 355 | |
| 356 | if (0UL == dl) |
| 357 | return 0; |
| 358 | if (0UL != dl) |
| 359 | return 0; |
| 360 | if (0UL < dl) |
| 361 | return 0; |
| 362 | if (0UL <= dl) |
| 363 | return 0; |
| 364 | if (0UL > dl) |
| 365 | return 0; |
| 366 | if (0UL >= dl) |
| 367 | return 0; |
| 368 | |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 369 | return 1; |
| 370 | } |