Roman Lebedev | c5417aa | 2018-01-03 08:45:19 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only \ |
| 2 | // RUN: -Wtautological-unsigned-zero-compare \ |
| 3 | // RUN: -verify %s |
| 4 | // RUN: %clang_cc1 -fsyntax-only \ |
| 5 | // RUN: -verify=silence %s |
| 6 | // RUN: %clang_cc1 -fsyntax-only \ |
| 7 | // RUN: -Wtautological-unsigned-zero-compare \ |
| 8 | // RUN: -verify -x c++ %s |
| 9 | // RUN: %clang_cc1 -fsyntax-only \ |
| 10 | // RUN: -verify=silence -x c++ %s |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 11 | |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame] | 12 | unsigned uvalue(void); |
| 13 | signed int svalue(void); |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 14 | |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame] | 15 | #define macro(val) val |
| 16 | |
| 17 | #ifdef __cplusplus |
| 18 | template<typename T> |
| 19 | void TFunc() { |
| 20 | // Make sure that we do warn for normal variables in template functions ! |
| 21 | unsigned char c = svalue(); |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame] | 22 | if (c < 0) // expected-warning {{comparison of unsigned expression < 0 is always false}} |
| 23 | return; |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame] | 24 | |
| 25 | if (c < macro(0)) |
| 26 | return; |
| 27 | |
| 28 | T v = svalue(); |
| 29 | if (v < 0) |
| 30 | return; |
| 31 | } |
| 32 | #endif |
| 33 | |
| 34 | int main() |
| 35 | { |
| 36 | #ifdef __cplusplus |
| 37 | TFunc<unsigned char>(); |
| 38 | TFunc<unsigned short>(); |
| 39 | #endif |
| 40 | |
Roman Lebedev | c5417aa | 2018-01-03 08:45:19 +0000 | [diff] [blame] | 41 | short s = svalue(); |
| 42 | |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame] | 43 | unsigned un = uvalue(); |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 44 | |
Hal Finkel | 05e4648 | 2017-12-16 02:23:22 +0000 | [diff] [blame] | 45 | // silence-no-diagnostics |
| 46 | |
Roman Lebedev | c5417aa | 2018-01-03 08:45:19 +0000 | [diff] [blame] | 47 | // Note: both sides are promoted to unsigned long prior to the comparison. |
| 48 | if (s == 0UL) |
| 49 | return 0; |
| 50 | if (s != 0UL) |
| 51 | return 0; |
| 52 | if (s < 0UL) // expected-warning {{comparison of unsigned expression < 0 is always false}} |
| 53 | return 0; |
| 54 | if (s <= 0UL) |
| 55 | return 0; |
| 56 | if (s > 0UL) |
| 57 | return 0; |
| 58 | if (s >= 0UL) // expected-warning {{comparison of unsigned expression >= 0 is always true}} |
| 59 | return 0; |
| 60 | |
| 61 | if (0UL == s) |
| 62 | return 0; |
| 63 | if (0UL != s) |
| 64 | return 0; |
| 65 | if (0UL < s) |
| 66 | return 0; |
| 67 | if (0UL <= s) // expected-warning {{comparison of 0 <= unsigned expression is always true}} |
| 68 | return 0; |
| 69 | if (0UL > s) // expected-warning {{comparison of 0 > unsigned expression is always false}} |
| 70 | return 0; |
| 71 | if (0UL >= s) |
| 72 | return 0; |
| 73 | |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame] | 74 | if (un == 0) |
| 75 | return 0; |
| 76 | if (un != 0) |
| 77 | return 0; |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 78 | 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] | 79 | return 0; |
| 80 | if (un <= 0) |
| 81 | return 0; |
| 82 | if (un > 0) |
| 83 | return 0; |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 84 | 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] | 85 | return 0; |
| 86 | |
| 87 | if (0 == un) |
| 88 | return 0; |
| 89 | if (0 != un) |
| 90 | return 0; |
| 91 | if (0 < un) |
| 92 | return 0; |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 93 | 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] | 94 | return 0; |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 95 | 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] | 96 | return 0; |
| 97 | if (0 >= un) |
| 98 | return 0; |
| 99 | |
| 100 | if (un == 0UL) |
| 101 | return 0; |
| 102 | if (un != 0UL) |
| 103 | return 0; |
| 104 | if (un < 0UL) // expected-warning {{comparison of unsigned expression < 0 is always false}} |
| 105 | return 0; |
| 106 | if (un <= 0UL) |
| 107 | return 0; |
| 108 | if (un > 0UL) |
| 109 | return 0; |
| 110 | if (un >= 0UL) // expected-warning {{comparison of unsigned expression >= 0 is always true}} |
| 111 | return 0; |
| 112 | |
| 113 | if (0UL == un) |
| 114 | return 0; |
| 115 | if (0UL != un) |
| 116 | return 0; |
| 117 | if (0UL < un) |
| 118 | return 0; |
| 119 | if (0UL <= un) // expected-warning {{comparison of 0 <= unsigned expression is always true}} |
| 120 | return 0; |
| 121 | if (0UL > un) // expected-warning {{comparison of 0 > unsigned expression is always false}} |
| 122 | return 0; |
| 123 | if (0UL >= un) |
| 124 | return 0; |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 125 | |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame] | 126 | |
| 127 | signed int a = svalue(); |
| 128 | |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame] | 129 | if (a == 0) |
| 130 | return 0; |
| 131 | if (a != 0) |
| 132 | return 0; |
| 133 | if (a < 0) |
| 134 | return 0; |
| 135 | if (a <= 0) |
| 136 | return 0; |
| 137 | if (a > 0) |
| 138 | return 0; |
| 139 | if (a >= 0) |
| 140 | return 0; |
| 141 | |
| 142 | if (0 == a) |
| 143 | return 0; |
| 144 | if (0 != a) |
| 145 | return 0; |
| 146 | if (0 < a) |
| 147 | return 0; |
| 148 | if (0 <= a) |
| 149 | return 0; |
| 150 | if (0 > a) |
| 151 | return 0; |
| 152 | if (0 >= a) |
| 153 | return 0; |
| 154 | |
| 155 | if (a == 0UL) |
| 156 | return 0; |
| 157 | if (a != 0UL) |
| 158 | return 0; |
| 159 | if (a < 0UL) // expected-warning {{comparison of unsigned expression < 0 is always false}} |
| 160 | return 0; |
| 161 | if (a <= 0UL) |
| 162 | return 0; |
| 163 | if (a > 0UL) |
| 164 | return 0; |
| 165 | if (a >= 0UL) // expected-warning {{comparison of unsigned expression >= 0 is always true}} |
| 166 | return 0; |
| 167 | |
| 168 | if (0UL == a) |
| 169 | return 0; |
| 170 | if (0UL != a) |
| 171 | return 0; |
| 172 | if (0UL < a) |
| 173 | return 0; |
| 174 | if (0UL <= a) // expected-warning {{comparison of 0 <= unsigned expression is always true}} |
| 175 | return 0; |
| 176 | if (0UL > a) // expected-warning {{comparison of 0 > unsigned expression is always false}} |
| 177 | return 0; |
| 178 | if (0UL >= a) |
| 179 | return 0; |
Roman Lebedev | 6de129e | 2017-10-15 20:13:17 +0000 | [diff] [blame] | 180 | |
| 181 | |
| 182 | float fl = 0; |
| 183 | |
| 184 | if (fl == 0) |
| 185 | return 0; |
| 186 | if (fl != 0) |
| 187 | return 0; |
| 188 | if (fl < 0) |
| 189 | return 0; |
| 190 | if (fl <= 0) |
| 191 | return 0; |
| 192 | if (fl > 0) |
| 193 | return 0; |
| 194 | if (fl >= 0) |
| 195 | return 0; |
| 196 | |
| 197 | if (0 == fl) |
| 198 | return 0; |
| 199 | if (0 != fl) |
| 200 | return 0; |
| 201 | if (0 < fl) |
| 202 | return 0; |
| 203 | if (0 <= fl) |
| 204 | return 0; |
| 205 | if (0 > fl) |
| 206 | return 0; |
| 207 | if (0 >= fl) |
| 208 | return 0; |
| 209 | |
| 210 | if (fl == 0UL) |
| 211 | return 0; |
| 212 | if (fl != 0UL) |
| 213 | return 0; |
| 214 | if (fl < 0UL) |
| 215 | return 0; |
| 216 | if (fl <= 0UL) |
| 217 | return 0; |
| 218 | if (fl > 0UL) |
| 219 | return 0; |
| 220 | if (fl >= 0UL) |
| 221 | return 0; |
| 222 | |
| 223 | if (0UL == fl) |
| 224 | return 0; |
| 225 | if (0UL != fl) |
| 226 | return 0; |
| 227 | if (0UL < fl) |
| 228 | return 0; |
| 229 | if (0UL <= fl) |
| 230 | return 0; |
| 231 | if (0UL > fl) |
| 232 | return 0; |
| 233 | if (0UL >= fl) |
| 234 | return 0; |
| 235 | |
| 236 | |
| 237 | double dl = 0; |
| 238 | |
| 239 | if (dl == 0) |
| 240 | return 0; |
| 241 | if (dl != 0) |
| 242 | return 0; |
| 243 | if (dl < 0) |
| 244 | return 0; |
| 245 | if (dl <= 0) |
| 246 | return 0; |
| 247 | if (dl > 0) |
| 248 | return 0; |
| 249 | if (dl >= 0) |
| 250 | return 0; |
| 251 | |
| 252 | if (0 == dl) |
| 253 | return 0; |
| 254 | if (0 != dl) |
| 255 | return 0; |
| 256 | if (0 < dl) |
| 257 | return 0; |
| 258 | if (0 <= dl) |
| 259 | return 0; |
| 260 | if (0 > dl) |
| 261 | return 0; |
| 262 | if (0 >= dl) |
| 263 | return 0; |
| 264 | |
| 265 | if (dl == 0UL) |
| 266 | return 0; |
| 267 | if (dl != 0UL) |
| 268 | return 0; |
| 269 | if (dl < 0UL) |
| 270 | return 0; |
| 271 | if (dl <= 0UL) |
| 272 | return 0; |
| 273 | if (dl > 0UL) |
| 274 | return 0; |
| 275 | if (dl >= 0UL) |
| 276 | return 0; |
| 277 | |
| 278 | if (0UL == dl) |
| 279 | return 0; |
| 280 | if (0UL != dl) |
| 281 | return 0; |
| 282 | if (0UL < dl) |
| 283 | return 0; |
| 284 | if (0UL <= dl) |
| 285 | return 0; |
| 286 | if (0UL > dl) |
| 287 | return 0; |
| 288 | if (0UL >= dl) |
| 289 | return 0; |
| 290 | |
Roman Lebedev | 309b7f5 | 2017-09-08 13:56:45 +0000 | [diff] [blame] | 291 | return 1; |
| 292 | } |