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