1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -Wuninitialized -fsyntax-only -fcxx-exceptions %s -verify -std=c++1y 2*67e74705SXin Li 3*67e74705SXin Li // Stub out types for 'typeid' to work. 4*67e74705SXin Li namespace std { class type_info {}; } 5*67e74705SXin Li 6*67e74705SXin Li int test1_aux(int &x); test1()7*67e74705SXin Liint test1() { 8*67e74705SXin Li int x; 9*67e74705SXin Li test1_aux(x); 10*67e74705SXin Li return x; // no-warning 11*67e74705SXin Li } 12*67e74705SXin Li test2_aux()13*67e74705SXin Liint test2_aux() { 14*67e74705SXin Li int x; 15*67e74705SXin Li int &y = x; 16*67e74705SXin Li return x; // no-warning 17*67e74705SXin Li } 18*67e74705SXin Li 19*67e74705SXin Li // Don't warn on unevaluated contexts. unevaluated_tests()20*67e74705SXin Livoid unevaluated_tests() { 21*67e74705SXin Li int x; 22*67e74705SXin Li (void)sizeof(x); 23*67e74705SXin Li (void)typeid(x); 24*67e74705SXin Li } 25*67e74705SXin Li 26*67e74705SXin Li // Warn for glvalue arguments to typeid whose type is polymorphic. ~AA27*67e74705SXin Listruct A { virtual ~A() {} }; polymorphic_test()28*67e74705SXin Livoid polymorphic_test() { 29*67e74705SXin Li A *a; // expected-note{{initialize the variable 'a' to silence this warning}} 30*67e74705SXin Li (void)typeid(*a); // expected-warning{{variable 'a' is uninitialized when used here}} 31*67e74705SXin Li } 32*67e74705SXin Li 33*67e74705SXin Li // Handle cases where the CFG may constant fold some branches, thus 34*67e74705SXin Li // mitigating the need for some path-sensitivity in the analysis. 35*67e74705SXin Li unsigned test3_aux(); test3()36*67e74705SXin Liunsigned test3() { 37*67e74705SXin Li unsigned x = 0; 38*67e74705SXin Li const bool flag = true; 39*67e74705SXin Li if (flag && (x = test3_aux()) == 0) { 40*67e74705SXin Li return x; 41*67e74705SXin Li } 42*67e74705SXin Li return x; 43*67e74705SXin Li } test3_b()44*67e74705SXin Liunsigned test3_b() { 45*67e74705SXin Li unsigned x ; 46*67e74705SXin Li const bool flag = true; 47*67e74705SXin Li if (flag && (x = test3_aux()) == 0) { 48*67e74705SXin Li x = 1; 49*67e74705SXin Li } 50*67e74705SXin Li return x; // no-warning 51*67e74705SXin Li } test3_c()52*67e74705SXin Liunsigned test3_c() { 53*67e74705SXin Li unsigned x; // expected-note{{initialize the variable 'x' to silence this warning}} 54*67e74705SXin Li const bool flag = false; 55*67e74705SXin Li if (flag && (x = test3_aux()) == 0) { 56*67e74705SXin Li x = 1; 57*67e74705SXin Li } 58*67e74705SXin Li return x; // expected-warning{{variable 'x' is uninitialized when used here}} 59*67e74705SXin Li } 60*67e74705SXin Li 61*67e74705SXin Li enum test4_A { 62*67e74705SXin Li test4_A_a, test_4_A_b 63*67e74705SXin Li }; test4()64*67e74705SXin Litest4_A test4() { 65*67e74705SXin Li test4_A a; // expected-note{{variable 'a' is declared here}} 66*67e74705SXin Li return a; // expected-warning{{variable 'a' is uninitialized when used here}} 67*67e74705SXin Li } 68*67e74705SXin Li 69*67e74705SXin Li // Test variables getting invalidated by function calls with reference arguments 70*67e74705SXin Li // *AND* there are multiple invalidated arguments. 71*67e74705SXin Li void test5_aux(int &, int &); 72*67e74705SXin Li test5()73*67e74705SXin Liint test5() { 74*67e74705SXin Li int x, y; 75*67e74705SXin Li test5_aux(x, y); 76*67e74705SXin Li return x + y; // no-warning 77*67e74705SXin Li } 78*67e74705SXin Li 79*67e74705SXin Li // This test previously crashed Sema. 80*67e74705SXin Li class Rdar9188004A { 81*67e74705SXin Li public: 82*67e74705SXin Li virtual ~Rdar9188004A(); 83*67e74705SXin Li }; 84*67e74705SXin Li 85*67e74705SXin Li template< typename T > class Rdar9188004B : public Rdar9188004A { foo(Rdar9188004B * next) const86*67e74705SXin Livirtual double *foo(Rdar9188004B *next) const { 87*67e74705SXin Li double *values = next->foo(0); 88*67e74705SXin Li try { 89*67e74705SXin Li } 90*67e74705SXin Li catch(double e) { 91*67e74705SXin Li values[0] = e; 92*67e74705SXin Li } 93*67e74705SXin Li return 0; 94*67e74705SXin Li } 95*67e74705SXin Li }; 96*67e74705SXin Li class Rdar9188004C : public Rdar9188004B<Rdar9188004A> { 97*67e74705SXin Li virtual void bar(void) const; 98*67e74705SXin Li }; bar(void) const99*67e74705SXin Livoid Rdar9188004C::bar(void) const {} 100*67e74705SXin Li 101*67e74705SXin Li // Don't warn about uninitialized variables in unreachable code. PR9625()102*67e74705SXin Livoid PR9625() { 103*67e74705SXin Li if (false) { 104*67e74705SXin Li int x; 105*67e74705SXin Li (void)static_cast<float>(x); // no-warning 106*67e74705SXin Li } 107*67e74705SXin Li } 108*67e74705SXin Li 109*67e74705SXin Li // Don't warn about variables declared in "catch" 110*67e74705SXin Li void RDar9251392_bar(const char *msg); 111*67e74705SXin Li RDar9251392()112*67e74705SXin Livoid RDar9251392() { 113*67e74705SXin Li try { 114*67e74705SXin Li throw "hi"; 115*67e74705SXin Li } 116*67e74705SXin Li catch (const char* msg) { 117*67e74705SXin Li RDar9251392_bar(msg); // no-warning 118*67e74705SXin Li } 119*67e74705SXin Li } 120*67e74705SXin Li 121*67e74705SXin Li // Test handling of "no-op" casts. test_noop_cast()122*67e74705SXin Livoid test_noop_cast() 123*67e74705SXin Li { 124*67e74705SXin Li int x = 1; 125*67e74705SXin Li int y = (int&)x; // no-warning 126*67e74705SXin Li } 127*67e74705SXin Li test_noop_cast2()128*67e74705SXin Livoid test_noop_cast2() { 129*67e74705SXin Li int x; // expected-note {{initialize the variable 'x' to silence this warning}} 130*67e74705SXin Li int y = (int&)x; // expected-warning {{uninitialized when used here}} 131*67e74705SXin Li } 132*67e74705SXin Li 133*67e74705SXin Li // Test handling of bit casts. test_bitcasts()134*67e74705SXin Livoid test_bitcasts() { 135*67e74705SXin Li int x = 1; 136*67e74705SXin Li int y = (float &)x; // no-warning 137*67e74705SXin Li } 138*67e74705SXin Li test_bitcasts_2()139*67e74705SXin Livoid test_bitcasts_2() { 140*67e74705SXin Li int x; // expected-note {{initialize the variable 'x' to silence this warning}} 141*67e74705SXin Li int y = (float &)x; // expected-warning {{uninitialized when used here}} 142*67e74705SXin Li } 143*67e74705SXin Li 144*67e74705SXin Li void consume_const_ref(const int &n); test_const_ref()145*67e74705SXin Liint test_const_ref() { 146*67e74705SXin Li int n; // expected-note {{variable}} 147*67e74705SXin Li consume_const_ref(n); 148*67e74705SXin Li return n; // expected-warning {{uninitialized when used here}} 149*67e74705SXin Li } 150*67e74705SXin Li 151*67e74705SXin Li // Don't crash here. __anon7d8c94600102null152*67e74705SXin Liauto PR19996 = [a=0]{int t; return a;}; 153