1*67e74705SXin Li // RUN: %clang_cc1 -std=gnu++11 -Wsometimes-uninitialized -verify %s 2*67e74705SXin Li // RUN: %clang_cc1 -std=gnu++11 -Wsometimes-uninitialized -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s 3*67e74705SXin Li 4*67e74705SXin Li bool maybe(); 5*67e74705SXin Li test_if_false(bool b)6*67e74705SXin Liint test_if_false(bool b) { 7*67e74705SXin Li int x; // expected-note {{variable}} 8*67e74705SXin Li if (b) // expected-warning {{whenever 'if' condition is false}} \ 9*67e74705SXin Li // expected-note {{remove the 'if' if its condition is always true}} 10*67e74705SXin Li x = 1; 11*67e74705SXin Li return x; // expected-note {{uninitialized use}} 12*67e74705SXin Li } 13*67e74705SXin Li 14*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{8:3-10:5}:"" 15*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{7:8-7:8}:" = 0" 16*67e74705SXin Li 17*67e74705SXin Li test_if_true(bool b)18*67e74705SXin Liint test_if_true(bool b) { 19*67e74705SXin Li int x; // expected-note {{variable}} 20*67e74705SXin Li if (b) {} // expected-warning {{whenever 'if' condition is true}} \ 21*67e74705SXin Li // expected-note {{remove the 'if' if its condition is always false}} 22*67e74705SXin Li else x = 1; 23*67e74705SXin Li return x; // expected-note {{uninitialized use}} 24*67e74705SXin Li } 25*67e74705SXin Li 26*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{20:3-22:8}:"" 27*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{19:8-19:8}:" = 0" 28*67e74705SXin Li 29*67e74705SXin Li test_while_false(bool b)30*67e74705SXin Liint test_while_false(bool b) { 31*67e74705SXin Li int x; // expected-note {{variable}} 32*67e74705SXin Li while (b) { // expected-warning {{whenever 'while' loop exits because its condition is false}} \ 33*67e74705SXin Li // expected-note {{remove the condition if it is always true}} 34*67e74705SXin Li if (maybe()) { 35*67e74705SXin Li x = 1; 36*67e74705SXin Li break; 37*67e74705SXin Li } 38*67e74705SXin Li }; 39*67e74705SXin Li return x; // expected-note {{uninitialized use}} 40*67e74705SXin Li } 41*67e74705SXin Li 42*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{32:10-32:11}:"true" 43*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{31:8-31:8}:" = 0" 44*67e74705SXin Li 45*67e74705SXin Li test_while_true(bool b)46*67e74705SXin Liint test_while_true(bool b) { 47*67e74705SXin Li int x; // expected-note {{variable}} 48*67e74705SXin Li while (b) { // expected-warning {{whenever 'while' loop is entered}} \ 49*67e74705SXin Li // expected-note {{remove the condition if it is always false}} 50*67e74705SXin Li label: 51*67e74705SXin Li return x; // expected-note {{uninitialized use}} 52*67e74705SXin Li } 53*67e74705SXin Li x = 0; 54*67e74705SXin Li goto label; 55*67e74705SXin Li } 56*67e74705SXin Li 57*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{48:10-48:11}:"false" 58*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{47:8-47:8}:" = 0" 59*67e74705SXin Li 60*67e74705SXin Li test_do_while_false(bool b)61*67e74705SXin Liint test_do_while_false(bool b) { 62*67e74705SXin Li int x; // expected-note {{variable}} 63*67e74705SXin Li do { 64*67e74705SXin Li if (maybe()) { 65*67e74705SXin Li x = 1; 66*67e74705SXin Li break; 67*67e74705SXin Li } 68*67e74705SXin Li } while (b); // expected-warning {{whenever 'do' loop exits because its condition is false}} \ 69*67e74705SXin Li // expected-note {{remove the condition if it is always true}} 70*67e74705SXin Li return x; // expected-note {{uninitialized use}} 71*67e74705SXin Li } 72*67e74705SXin Li 73*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{68:12-68:13}:"true" 74*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{62:8-62:8}:" = 0" 75*67e74705SXin Li 76*67e74705SXin Li test_do_while_true(bool b)77*67e74705SXin Liint test_do_while_true(bool b) { 78*67e74705SXin Li int x; // expected-note {{variable}} 79*67e74705SXin Li goto label2; 80*67e74705SXin Li do { 81*67e74705SXin Li label1: 82*67e74705SXin Li return x; // expected-note {{uninitialized use}} 83*67e74705SXin Li label2: ; 84*67e74705SXin Li } while (b); // expected-warning {{whenever 'do' loop condition is true}} \ 85*67e74705SXin Li // expected-note {{remove the condition if it is always false}} 86*67e74705SXin Li x = 0; 87*67e74705SXin Li goto label1; 88*67e74705SXin Li } 89*67e74705SXin Li 90*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{84:12-84:13}:"false" 91*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{78:8-78:8}:" = 0" 92*67e74705SXin Li 93*67e74705SXin Li test_for_false(int k)94*67e74705SXin Liint test_for_false(int k) { 95*67e74705SXin Li int x; // expected-note {{variable}} 96*67e74705SXin Li for (int n = 0; 97*67e74705SXin Li n < k; // expected-warning {{whenever 'for' loop exits because its condition is false}} \ 98*67e74705SXin Li // expected-note {{remove the condition if it is always true}} 99*67e74705SXin Li ++n) { 100*67e74705SXin Li if (maybe()) { 101*67e74705SXin Li x = n; 102*67e74705SXin Li break; 103*67e74705SXin Li } 104*67e74705SXin Li } 105*67e74705SXin Li return x; // expected-note {{uninitialized use}} 106*67e74705SXin Li } 107*67e74705SXin Li 108*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{97:8-97:13}:"" 109*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{95:8-95:8}:" = 0" 110*67e74705SXin Li 111*67e74705SXin Li test_for_true(int k)112*67e74705SXin Liint test_for_true(int k) { 113*67e74705SXin Li int x; // expected-note {{variable}} 114*67e74705SXin Li int n = 0; 115*67e74705SXin Li for (; 116*67e74705SXin Li n < k; // expected-warning {{whenever 'for' loop is entered}} \ 117*67e74705SXin Li // expected-note {{remove the condition if it is always false}} 118*67e74705SXin Li ++n) { 119*67e74705SXin Li label: 120*67e74705SXin Li return x; // expected-note {{uninitialized use}} 121*67e74705SXin Li } 122*67e74705SXin Li x = 1; 123*67e74705SXin Li goto label; 124*67e74705SXin Li } 125*67e74705SXin Li 126*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{116:8-116:13}:"false" 127*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{113:8-113:8}:" = 0" 128*67e74705SXin Li 129*67e74705SXin Li test_for_range_false(int k)130*67e74705SXin Liint test_for_range_false(int k) { 131*67e74705SXin Li int arr[3] = { 1, 2, 3 }; 132*67e74705SXin Li int x; 133*67e74705SXin Li for (int &a : arr) { // no-warning, condition was not explicitly specified 134*67e74705SXin Li if (a == k) { 135*67e74705SXin Li x = &a - arr; 136*67e74705SXin Li break; 137*67e74705SXin Li } 138*67e74705SXin Li } 139*67e74705SXin Li return x; 140*67e74705SXin Li } 141*67e74705SXin Li 142*67e74705SXin Li 143*67e74705SXin Li 144*67e74705SXin Li 145*67e74705SXin Li test_for_range_true(int k)146*67e74705SXin Liint test_for_range_true(int k) { 147*67e74705SXin Li int arr[3] = { 1, 2, 3 }; 148*67e74705SXin Li int x; // expected-note {{variable}} 149*67e74705SXin Li for (int &a : arr) { // expected-warning {{variable 'x' is used uninitialized whenever 'for' loop is entered}} 150*67e74705SXin Li goto label; 151*67e74705SXin Li } 152*67e74705SXin Li x = 0; 153*67e74705SXin Li label: 154*67e74705SXin Li return x; // expected-note {{uninitialized use}} 155*67e74705SXin Li } 156*67e74705SXin Li 157*67e74705SXin Li 158*67e74705SXin Li 159*67e74705SXin Li 160*67e74705SXin Li test_conditional_false(int k)161*67e74705SXin Liint test_conditional_false(int k) { 162*67e74705SXin Li int x; // expected-note {{variable}} 163*67e74705SXin Li (void)( 164*67e74705SXin Li maybe() // expected-warning {{whenever '?:' condition is false}} \ 165*67e74705SXin Li // expected-note {{remove the '?:' if its condition is always true}} 166*67e74705SXin Li ? x = 1 : 0); 167*67e74705SXin Li return x; // expected-note {{uninitialized use}} 168*67e74705SXin Li } 169*67e74705SXin Li 170*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{164:7-166:9}:"" 171*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{166:14-166:18}:"" 172*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{162:8-162:8}:" = 0" 173*67e74705SXin Li test_conditional_true(int k)174*67e74705SXin Liint test_conditional_true(int k) { 175*67e74705SXin Li int x; // expected-note {{variable}} 176*67e74705SXin Li (void)( 177*67e74705SXin Li maybe() // expected-warning {{whenever '?:' condition is true}} \ 178*67e74705SXin Li // expected-note {{remove the '?:' if its condition is always false}} 179*67e74705SXin Li ? 0 : x = 1); 180*67e74705SXin Li return x; // expected-note {{uninitialized use}} 181*67e74705SXin Li } 182*67e74705SXin Li 183*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{177:7-179:13}:"" 184*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{175:8-175:8}:" = 0" 185*67e74705SXin Li 186*67e74705SXin Li test_logical_and_false(int k)187*67e74705SXin Liint test_logical_and_false(int k) { 188*67e74705SXin Li int x; // expected-note {{variable}} 189*67e74705SXin Li maybe() // expected-warning {{whenever '&&' condition is false}} \ 190*67e74705SXin Li // expected-note {{remove the '&&' if its condition is always true}} 191*67e74705SXin Li && (x = 1); 192*67e74705SXin Li return x; // expected-note {{uninitialized use}} 193*67e74705SXin Li } 194*67e74705SXin Li 195*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{189:3-191:10}:"" 196*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{188:8-188:8}:" = 0" 197*67e74705SXin Li 198*67e74705SXin Li test_logical_and_true(int k)199*67e74705SXin Liint test_logical_and_true(int k) { 200*67e74705SXin Li int x; // expected-note {{variable}} 201*67e74705SXin Li maybe() // expected-warning {{whenever '&&' condition is true}} \ 202*67e74705SXin Li // expected-note {{remove the '&&' if its condition is always false}} 203*67e74705SXin Li && ({ goto skip_init; 0; }); 204*67e74705SXin Li x = 1; 205*67e74705SXin Li skip_init: 206*67e74705SXin Li return x; // expected-note {{uninitialized use}} 207*67e74705SXin Li } 208*67e74705SXin Li 209*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{201:3-203:34}:"false" 210*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{200:8-200:8}:" = 0" 211*67e74705SXin Li 212*67e74705SXin Li test_logical_or_false(int k)213*67e74705SXin Liint test_logical_or_false(int k) { 214*67e74705SXin Li int x; // expected-note {{variable}} 215*67e74705SXin Li maybe() // expected-warning {{whenever '||' condition is false}} \ 216*67e74705SXin Li // expected-note {{remove the '||' if its condition is always true}} 217*67e74705SXin Li || ({ goto skip_init; 0; }); 218*67e74705SXin Li x = 1; 219*67e74705SXin Li skip_init: 220*67e74705SXin Li return x; // expected-note {{uninitialized use}} 221*67e74705SXin Li } 222*67e74705SXin Li 223*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{215:3-217:34}:"true" 224*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{214:8-214:8}:" = 0" 225*67e74705SXin Li 226*67e74705SXin Li test_logical_or_true(int k)227*67e74705SXin Liint test_logical_or_true(int k) { 228*67e74705SXin Li int x; // expected-note {{variable}} 229*67e74705SXin Li maybe() // expected-warning {{whenever '||' condition is true}} \ 230*67e74705SXin Li // expected-note {{remove the '||' if its condition is always false}} 231*67e74705SXin Li || (x = 1); 232*67e74705SXin Li return x; // expected-note {{uninitialized use}} 233*67e74705SXin Li } 234*67e74705SXin Li 235*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{229:3-231:10}:"" 236*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{228:8-228:8}:" = 0" 237*67e74705SXin Li 238*67e74705SXin Li test_switch_case(int k)239*67e74705SXin Liint test_switch_case(int k) { 240*67e74705SXin Li int x; // expected-note {{variable}} 241*67e74705SXin Li switch (k) { 242*67e74705SXin Li case 0: 243*67e74705SXin Li x = 0; 244*67e74705SXin Li break; 245*67e74705SXin Li case 1: // expected-warning {{whenever switch case is taken}} 246*67e74705SXin Li break; 247*67e74705SXin Li } 248*67e74705SXin Li return x; // expected-note {{uninitialized use}} 249*67e74705SXin Li } 250*67e74705SXin Li 251*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{240:8-240:8}:" = 0" 252*67e74705SXin Li 253*67e74705SXin Li 254*67e74705SXin Li test_switch_default(int k)255*67e74705SXin Liint test_switch_default(int k) { 256*67e74705SXin Li int x; // expected-note {{variable}} 257*67e74705SXin Li switch (k) { 258*67e74705SXin Li case 0: 259*67e74705SXin Li x = 0; 260*67e74705SXin Li break; 261*67e74705SXin Li case 1: 262*67e74705SXin Li x = 1; 263*67e74705SXin Li break; 264*67e74705SXin Li default: // expected-warning {{whenever switch default is taken}} 265*67e74705SXin Li break; 266*67e74705SXin Li } 267*67e74705SXin Li return x; // expected-note {{uninitialized use}} 268*67e74705SXin Li } 269*67e74705SXin Li 270*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{256:8-256:8}:" = 0" 271*67e74705SXin Li 272*67e74705SXin Li 273*67e74705SXin Li test_switch_suppress_1(int k)274*67e74705SXin Liint test_switch_suppress_1(int k) { 275*67e74705SXin Li int x; 276*67e74705SXin Li switch (k) { 277*67e74705SXin Li case 0: 278*67e74705SXin Li x = 0; 279*67e74705SXin Li break; 280*67e74705SXin Li case 1: 281*67e74705SXin Li x = 1; 282*67e74705SXin Li break; 283*67e74705SXin Li } 284*67e74705SXin Li return x; // no-warning 285*67e74705SXin Li } 286*67e74705SXin Li 287*67e74705SXin Li 288*67e74705SXin Li 289*67e74705SXin Li 290*67e74705SXin Li test_switch_suppress_2(int k)291*67e74705SXin Liint test_switch_suppress_2(int k) { 292*67e74705SXin Li int x; 293*67e74705SXin Li switch (k) { 294*67e74705SXin Li case 0: 295*67e74705SXin Li case 1: 296*67e74705SXin Li switch (k) { 297*67e74705SXin Li case 0: 298*67e74705SXin Li return 0; 299*67e74705SXin Li case 1: 300*67e74705SXin Li return 1; 301*67e74705SXin Li } 302*67e74705SXin Li case 2: 303*67e74705SXin Li case 3: 304*67e74705SXin Li x = 1; 305*67e74705SXin Li } 306*67e74705SXin Li return x; // no-warning 307*67e74705SXin Li } 308*67e74705SXin Li 309*67e74705SXin Li 310*67e74705SXin Li 311*67e74705SXin Li 312*67e74705SXin Li test_multiple_notes(int k)313*67e74705SXin Liint test_multiple_notes(int k) { 314*67e74705SXin Li int x; // expected-note {{variable}} 315*67e74705SXin Li if (k > 0) { 316*67e74705SXin Li if (k == 5) 317*67e74705SXin Li x = 1; 318*67e74705SXin Li else if (k == 2) // expected-warning {{whenever 'if' condition is false}} \ 319*67e74705SXin Li // expected-note {{remove the 'if' if its condition is always true}} 320*67e74705SXin Li x = 2; 321*67e74705SXin Li } else { 322*67e74705SXin Li if (k == -5) 323*67e74705SXin Li x = 3; 324*67e74705SXin Li else if (k == -2) // expected-warning {{whenever 'if' condition is false}} \ 325*67e74705SXin Li // expected-note {{remove the 'if' if its condition is always true}} 326*67e74705SXin Li x = 4; 327*67e74705SXin Li } 328*67e74705SXin Li return x; // expected-note 2{{uninitialized use}} 329*67e74705SXin Li } 330*67e74705SXin Li 331*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{324:10-326:7}:"" 332*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{318:10-320:7}:"" 333*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{314:8-314:8}:" = 0" 334*67e74705SXin Li test_no_false_positive_1(int k)335*67e74705SXin Liint test_no_false_positive_1(int k) { 336*67e74705SXin Li int x; 337*67e74705SXin Li if (k) 338*67e74705SXin Li x = 5; 339*67e74705SXin Li while (!k) 340*67e74705SXin Li maybe(); 341*67e74705SXin Li return x; 342*67e74705SXin Li } 343*67e74705SXin Li 344*67e74705SXin Li 345*67e74705SXin Li 346*67e74705SXin Li 347*67e74705SXin Li test_no_false_positive_2()348*67e74705SXin Liint test_no_false_positive_2() { 349*67e74705SXin Li int x; 350*67e74705SXin Li bool b = false; 351*67e74705SXin Li if (maybe()) { 352*67e74705SXin Li x = 5; 353*67e74705SXin Li b = true; 354*67e74705SXin Li } 355*67e74705SXin Li return b ? x : 0; 356*67e74705SXin Li } 357*67e74705SXin Li 358*67e74705SXin Li 359*67e74705SXin Li 360*67e74705SXin Li 361*67e74705SXin Li test_null_pred_succ()362*67e74705SXin Livoid test_null_pred_succ() { 363*67e74705SXin Li int x; // expected-note {{variable}} expected-warning {{used uninitialized whenever function 'test_null_pred_succ' is called}} 364*67e74705SXin Li if (0) 365*67e74705SXin Li foo: x = 0; 366*67e74705SXin Li if (x) // expected-note {{use}} 367*67e74705SXin Li goto foo; 368*67e74705SXin Li } 369*67e74705SXin Li 370*67e74705SXin Li 371*67e74705SXin Li 372*67e74705SXin Li 373*67e74705SXin Li void foo(); PR13360(bool b)374*67e74705SXin Liint PR13360(bool b) { 375*67e74705SXin Li int x; // expected-note {{variable}} 376*67e74705SXin Li if (b) { // expected-warning {{variable 'x' is used uninitialized whenever 'if' condition is true}} expected-note {{remove}} 377*67e74705SXin Li do { 378*67e74705SXin Li foo(); 379*67e74705SXin Li } while (0); 380*67e74705SXin Li } else { 381*67e74705SXin Li x = 1; 382*67e74705SXin Li } 383*67e74705SXin Li return x; // expected-note {{uninitialized use occurs here}} 384*67e74705SXin Li } 385*67e74705SXin Li 386*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{376:3-380:10}:"" 387*67e74705SXin Li // CHECK: fix-it:"{{.*}}":{375:8-375:8}:" = 0" 388*67e74705SXin Li test_jump_init()389*67e74705SXin Livoid test_jump_init() { 390*67e74705SXin Li goto later; 391*67e74705SXin Li int x; // expected-note {{variable}} expected-warning {{used uninitialized whenever function 'test_jump_init'}} 392*67e74705SXin Li later: 393*67e74705SXin Li while (x) x = 0; // expected-note {{use}} 394*67e74705SXin Li } 395*67e74705SXin Li PR16054()396*67e74705SXin Livoid PR16054() { 397*67e74705SXin Li int x; // expected-note {{variable}} expected-warning {{used uninitialized whenever function 'PR16054}} 398*67e74705SXin Li while (x != 0) { // expected-note {{use}} 399*67e74705SXin Li (void)&x; 400*67e74705SXin Li } 401*67e74705SXin Li } 402*67e74705SXin Li test_loop_uninit()403*67e74705SXin Livoid test_loop_uninit() { 404*67e74705SXin Li for (int n = 0; n < 10; ++n) { 405*67e74705SXin Li int k; // expected-warning {{variable 'k' is used uninitialized whenever its declaration is reached}} expected-note {{variable}} 406*67e74705SXin Li do { 407*67e74705SXin Li k = k + 1; // expected-note {{use}} 408*67e74705SXin Li } while (k != 5); 409*67e74705SXin Li } 410*67e74705SXin Li } 411*67e74705SXin Li 412*67e74705SXin Li // FIXME: We should warn here, because the variable is used uninitialized 413*67e74705SXin Li // the first time we encounter the use. test_loop_with_assignment()414*67e74705SXin Livoid test_loop_with_assignment() { 415*67e74705SXin Li double d; 416*67e74705SXin Li for (int n = 0; n < 10; ++n) { 417*67e74705SXin Li d = d + n; 418*67e74705SXin Li } 419*67e74705SXin Li } 420*67e74705SXin Li 421*67e74705SXin Li // FIXME: We should warn here, because the variable is used uninitialized 422*67e74705SXin Li // the first time we encounter the use. test_loop_with_ref_bind()423*67e74705SXin Livoid test_loop_with_ref_bind() { 424*67e74705SXin Li double d; 425*67e74705SXin Li for (int n = 0; n < 10; ++n) { 426*67e74705SXin Li d += n; 427*67e74705SXin Li const double &r = d; 428*67e74705SXin Li } 429*67e74705SXin Li } 430