1 // RUN: %clang_cc1 -fsyntax-only -Wuninitialized -fsyntax-only -fcxx-exceptions %s -verify 2 3 // Stub out types for 'typeid' to work. 4 namespace std { class type_info {}; } 5 6 int test1_aux(int &x); 7 int test1() { 8 int x; 9 test1_aux(x); 10 return x; // no-warning 11 } 12 13 int test2_aux() { 14 int x; 15 int &y = x; 16 return x; // no-warning 17 } 18 19 // Don't warn on unevaluated contexts. 20 void unevaluated_tests() { 21 int x; 22 (void)sizeof(x); 23 (void)typeid(x); 24 } 25 26 // Warn for glvalue arguments to typeid whose type is polymorphic. 27 struct A { virtual ~A() {} }; 28 void polymorphic_test() { 29 A *a; // expected-note{{declared here}} expected-note{{add initialization}} 30 (void)typeid(*a); // expected-warning{{variable 'a' is uninitialized when used here }} 31 } 32 33 // Handle cases where the CFG may constant fold some branches, thus 34 // mitigating the need for some path-sensitivity in the analysis. 35 unsigned test3_aux(); 36 unsigned test3() { 37 unsigned x = 0; 38 const bool flag = true; 39 if (flag && (x = test3_aux()) == 0) { 40 return x; 41 } 42 return x; 43 } 44 unsigned test3_b() { 45 unsigned x ; 46 const bool flag = true; 47 if (flag && (x = test3_aux()) == 0) { 48 x = 1; 49 } 50 return x; // no-warning 51 } 52 unsigned test3_c() { 53 unsigned x; // expected-note{{declared here}} expected-note{{add initialization}} 54 const bool flag = false; 55 if (flag && (x = test3_aux()) == 0) { 56 x = 1; 57 } 58 return x; // expected-warning{{variable 'x' is uninitialized when used here}} 59 } 60 61 enum test4_A { 62 test4_A_a, test_4_A_b 63 }; 64 test4_A test4() { 65 test4_A a; // expected-note{{variable 'a' is declared here}} 66 return a; // expected-warning{{variable 'a' is uninitialized when used here}} 67 } 68 69 // This test previously crashed Sema. 70 class Rdar9188004A { 71 public: 72 virtual ~Rdar9188004A(); 73 }; 74 75 template< typename T > class Rdar9188004B : public Rdar9188004A { 76 virtual double *foo(Rdar9188004B *next) const { 77 double *values = next->foo(0); 78 try { 79 } 80 catch(double e) { 81 values[0] = e; 82 } 83 return 0; 84 } 85 }; 86 class Rdar9188004C : public Rdar9188004B<Rdar9188004A> { 87 virtual void bar(void) const; 88 }; 89 void Rdar9188004C::bar(void) const {} 90 91 // Don't warn about uninitialized variables in unreachable code. 92 void PR9625() { 93 if (false) { 94 int x; 95 (void)static_cast<float>(x); // no-warning 96 } 97 } 98 99 // Don't warn about variables declared in "catch" 100 void RDar9251392_bar(const char *msg); 101 102 void RDar9251392() { 103 try { 104 throw "hi"; 105 } 106 catch (const char* msg) { 107 RDar9251392_bar(msg); // no-warning 108 } 109 } 110 111 112