uninit-sometimes.cpp 10.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
// RUN: %clang_cc1 -std=gnu++11 -Wsometimes-uninitialized -verify %s
// RUN: %clang_cc1 -std=gnu++11 -Wsometimes-uninitialized -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s

bool maybe();

int test_if_false(bool b) {
  int x; // expected-note {{variable}}
  if (b) // expected-warning {{whenever 'if' condition is false}} \
         // expected-note {{remove the 'if' if its condition is always true}}
    x = 1;
  return x; // expected-note {{uninitialized use}}
}

// CHECK: fix-it:"{{.*}}":{8:3-10:5}:""
// CHECK: fix-it:"{{.*}}":{7:8-7:8}:" = 0"


int test_if_true(bool b) {
  int x; // expected-note {{variable}}
  if (b) {} // expected-warning {{whenever 'if' condition is true}} \
            // expected-note {{remove the 'if' if its condition is always false}}
  else x = 1;
  return x; // expected-note {{uninitialized use}}
}

// CHECK: fix-it:"{{.*}}":{20:3-22:8}:""
// CHECK: fix-it:"{{.*}}":{19:8-19:8}:" = 0"


int test_while_false(bool b) {
  int x; // expected-note {{variable}}
  while (b) { // expected-warning {{whenever 'while' loop exits because its condition is false}} \
              // expected-note {{remove the condition if it is always true}}
    if (maybe()) {
      x = 1;
      break;
    }
  };
  return x; // expected-note {{uninitialized use}}
}

// CHECK: fix-it:"{{.*}}":{32:10-32:11}:"true"
// CHECK: fix-it:"{{.*}}":{31:8-31:8}:" = 0"


int test_while_true(bool b) {
  int x; // expected-note {{variable}}
  while (b) { // expected-warning {{whenever 'while' loop is entered}} \
              // expected-note {{remove the condition if it is always false}}
label:
    return x; // expected-note {{uninitialized use}}
  }
  x = 0;
  goto label;
}

// CHECK: fix-it:"{{.*}}":{48:10-48:11}:"false"
// CHECK: fix-it:"{{.*}}":{47:8-47:8}:" = 0"


int test_do_while_false(bool b) {
  int x; // expected-note {{variable}}
  do {
    if (maybe()) {
      x = 1;
      break;
    }
  } while (b); // expected-warning {{whenever 'do' loop exits because its condition is false}} \
               // expected-note {{remove the condition if it is always true}}
  return x; // expected-note {{uninitialized use}}
}

// CHECK: fix-it:"{{.*}}":{68:12-68:13}:"true"
// CHECK: fix-it:"{{.*}}":{62:8-62:8}:" = 0"


int test_do_while_true(bool b) {
  int x; // expected-note {{variable}}
goto label2;
  do {
label1:
    return x; // expected-note {{uninitialized use}}
label2: ;
  } while (b); // expected-warning {{whenever 'do' loop condition is true}} \
               // expected-note {{remove the condition if it is always false}}
  x = 0;
  goto label1;
}

// CHECK: fix-it:"{{.*}}":{84:12-84:13}:"false"
// CHECK: fix-it:"{{.*}}":{78:8-78:8}:" = 0"


int test_for_false(int k) {
  int x; // expected-note {{variable}}
  for (int n = 0;
       n < k; // expected-warning {{whenever 'for' loop exits because its condition is false}} \
              // expected-note {{remove the condition if it is always true}}
       ++n) {
    if (maybe()) {
      x = n;
      break;
    }
  }
  return x; // expected-note {{uninitialized use}}
}

// CHECK: fix-it:"{{.*}}":{97:8-97:13}:""
// CHECK: fix-it:"{{.*}}":{95:8-95:8}:" = 0"


int test_for_true(int k) {
  int x; // expected-note {{variable}}
  int n = 0;
  for (;
       n < k; // expected-warning {{whenever 'for' loop is entered}} \
              // expected-note {{remove the condition if it is always false}}
       ++n) {
label:
    return x; // expected-note {{uninitialized use}}
  }
  x = 1;
  goto label;
}

// CHECK: fix-it:"{{.*}}":{116:8-116:13}:"false"
// CHECK: fix-it:"{{.*}}":{113:8-113:8}:" = 0"


int test_for_range_false(int k) {
  int arr[3] = { 1, 2, 3 };
  int x;
  for (int &a : arr) { // no-warning, condition was not explicitly specified
    if (a == k) {
      x = &a - arr;
      break;
    }
  }
  return x;
}





int test_for_range_true(int k) {
  int arr[3] = { 1, 2, 3 };
  int x; // expected-note {{variable}}
  for (int &a : arr) { // expected-warning {{variable 'x' is used uninitialized whenever 'for' loop is entered}}
    goto label;
  }
  x = 0;
label:
  return x; // expected-note {{uninitialized use}}
}





int test_conditional_false(int k) {
  int x; // expected-note {{variable}}
  (void)(
      maybe() // expected-warning {{whenever '?:' condition is false}} \
              // expected-note {{remove the '?:' if its condition is always true}}
      ? x = 1 : 0);
  return x; // expected-note {{uninitialized use}}
}

// CHECK: fix-it:"{{.*}}":{164:7-166:9}:""
// CHECK: fix-it:"{{.*}}":{166:14-166:18}:""
// CHECK: fix-it:"{{.*}}":{162:8-162:8}:" = 0"

int test_conditional_true(int k) {
  int x; // expected-note {{variable}}
  (void)(
      maybe() // expected-warning {{whenever '?:' condition is true}} \
              // expected-note {{remove the '?:' if its condition is always false}}
      ? 0 : x = 1);
  return x; // expected-note {{uninitialized use}}
}

// CHECK: fix-it:"{{.*}}":{177:7-179:13}:""
// CHECK: fix-it:"{{.*}}":{175:8-175:8}:" = 0"


int test_logical_and_false(int k) {
  int x; // expected-note {{variable}}
  maybe() // expected-warning {{whenever '&&' condition is false}} \
          // expected-note {{remove the '&&' if its condition is always true}}
      && (x = 1);
  return x; // expected-note {{uninitialized use}}
}

// CHECK: fix-it:"{{.*}}":{189:3-191:10}:""
// CHECK: fix-it:"{{.*}}":{188:8-188:8}:" = 0"


int test_logical_and_true(int k) {
  int x; // expected-note {{variable}}
  maybe() // expected-warning {{whenever '&&' condition is true}} \
          // expected-note {{remove the '&&' if its condition is always false}}
      && ({ goto skip_init; 0; });
  x = 1;
skip_init:
  return x; // expected-note {{uninitialized use}}
}

// CHECK: fix-it:"{{.*}}":{201:3-203:34}:"false"
// CHECK: fix-it:"{{.*}}":{200:8-200:8}:" = 0"


int test_logical_or_false(int k) {
  int x; // expected-note {{variable}}
  maybe() // expected-warning {{whenever '||' condition is false}} \
          // expected-note {{remove the '||' if its condition is always true}}
      || ({ goto skip_init; 0; });
  x = 1;
skip_init:
  return x; // expected-note {{uninitialized use}}
}

// CHECK: fix-it:"{{.*}}":{215:3-217:34}:"true"
// CHECK: fix-it:"{{.*}}":{214:8-214:8}:" = 0"


int test_logical_or_true(int k) {
  int x; // expected-note {{variable}}
  maybe() // expected-warning {{whenever '||' condition is true}} \
          // expected-note {{remove the '||' if its condition is always false}}
      || (x = 1);
  return x; // expected-note {{uninitialized use}}
}

// CHECK: fix-it:"{{.*}}":{229:3-231:10}:""
// CHECK: fix-it:"{{.*}}":{228:8-228:8}:" = 0"


int test_switch_case(int k) {
  int x; // expected-note {{variable}}
  switch (k) {
  case 0:
    x = 0;
    break;
  case 1: // expected-warning {{whenever switch case is taken}}
    break;
  }
  return x; // expected-note {{uninitialized use}}
}

// CHECK: fix-it:"{{.*}}":{240:8-240:8}:" = 0"



int test_switch_default(int k) {
  int x; // expected-note {{variable}}
  switch (k) {
  case 0:
    x = 0;
    break;
  case 1:
    x = 1;
    break;
  default: // expected-warning {{whenever switch default is taken}}
    break;
  }
  return x; // expected-note {{uninitialized use}}
}

// CHECK: fix-it:"{{.*}}":{256:8-256:8}:" = 0"



int test_switch_suppress_1(int k) {
  int x;
  switch (k) {
  case 0:
    x = 0;
    break;
  case 1:
    x = 1;
    break;
  }
  return x; // no-warning
}





int test_switch_suppress_2(int k) {
  int x;
  switch (k) {
  case 0:
  case 1:
    switch (k) {
    case 0:
      return 0;
    case 1:
      return 1;
    }
  case 2:
  case 3:
    x = 1;
  }
  return x; // no-warning
}





int test_multiple_notes(int k) {
  int x; // expected-note {{variable}}
  if (k > 0) {
    if (k == 5)
      x = 1;
    else if (k == 2) // expected-warning {{whenever 'if' condition is false}} \
                     // expected-note {{remove the 'if' if its condition is always true}}
      x = 2;
  } else {
    if (k == -5)
      x = 3;
    else if (k == -2) // expected-warning {{whenever 'if' condition is false}} \
                      // expected-note {{remove the 'if' if its condition is always true}}
      x = 4;
  }
  return x; // expected-note 2{{uninitialized use}}
}

// CHECK: fix-it:"{{.*}}":{324:10-326:7}:""
// CHECK: fix-it:"{{.*}}":{318:10-320:7}:""
// CHECK: fix-it:"{{.*}}":{314:8-314:8}:" = 0"

int test_no_false_positive_1(int k) {
  int x;
  if (k)
    x = 5;
  while (!k)
    maybe();
  return x;
}





int test_no_false_positive_2() {
  int x;
  bool b = false;
  if (maybe()) {
    x = 5;
    b = true;
  }
  return b ? x : 0;
}





void test_null_pred_succ() {
  int x; // expected-note {{variable}} expected-warning {{used uninitialized whenever function 'test_null_pred_succ' is called}}
  if (0)
    foo: x = 0;
  if (x) // expected-note {{use}}
    goto foo;
}




void foo();
int PR13360(bool b) {
  int x; // expected-note {{variable}}
  if (b) { // expected-warning {{variable 'x' is used uninitialized whenever 'if' condition is true}} expected-note {{remove}}
    do {
      foo();
    } while (0);
  } else {
    x = 1;
  }
  return x; // expected-note {{uninitialized use occurs here}}
}

// CHECK: fix-it:"{{.*}}":{376:3-380:10}:""
// CHECK: fix-it:"{{.*}}":{375:8-375:8}:" = 0"

void test_jump_init() {
goto later;
  int x; // expected-note {{variable}} expected-warning {{used uninitialized whenever function 'test_jump_init'}}
later:
  while (x) x = 0; // expected-note {{use}}
}

void PR16054() {
  int x; // expected-note {{variable}} expected-warning {{used uninitialized whenever function 'PR16054}}
  while (x != 0) { // expected-note {{use}}
    (void)&x;
  }
}

void test_loop_uninit() {
  for (int n = 0; n < 10; ++n) {
    int k; // expected-warning {{variable 'k' is used uninitialized whenever its declaration is reached}} expected-note {{variable}}
    do {
      k = k + 1; // expected-note {{use}}
    } while (k != 5);
  }
}

// FIXME: We should warn here, because the variable is used uninitialized
// the first time we encounter the use.
void test_loop_with_assignment() {
  double d;
  for (int n = 0; n < 10; ++n) {
    d = d + n;
  }
}

// FIXME: We should warn here, because the variable is used uninitialized
// the first time we encounter the use.
void test_loop_with_ref_bind() {
  double d;
  for (int n = 0; n < 10; ++n) {
    d += n;
    const double &r = d;
  }
}