Ted Kremenek | b831c67 | 2011-03-29 01:40:00 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -Wuninitialized -fsyntax-only -fcxx-exceptions %s -verify |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 2 | |
| 3 | int test1_aux(int &x); |
| 4 | int test1() { |
| 5 | int x; |
| 6 | test1_aux(x); |
| 7 | return x; // no-warning |
| 8 | } |
| 9 | |
| 10 | int test2_aux() { |
| 11 | int x; |
| 12 | int &y = x; |
| 13 | return x; // no-warning |
| 14 | } |
| 15 | |
Ted Kremenek | 2d4bed1 | 2011-01-20 21:25:31 +0000 | [diff] [blame] | 16 | // Handle cases where the CFG may constant fold some branches, thus |
| 17 | // mitigating the need for some path-sensitivity in the analysis. |
| 18 | unsigned test3_aux(); |
| 19 | unsigned test3() { |
| 20 | unsigned x = 0; |
| 21 | const bool flag = true; |
| 22 | if (flag && (x = test3_aux()) == 0) { |
| 23 | return x; |
| 24 | } |
| 25 | return x; |
| 26 | } |
| 27 | unsigned test3_b() { |
| 28 | unsigned x ; |
| 29 | const bool flag = true; |
| 30 | if (flag && (x = test3_aux()) == 0) { |
| 31 | x = 1; |
| 32 | } |
| 33 | return x; // no-warning |
| 34 | } |
| 35 | unsigned test3_c() { |
Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 36 | unsigned x; // expected-note{{declared here}} expected-note{{add initialization}} |
Ted Kremenek | 2d4bed1 | 2011-01-20 21:25:31 +0000 | [diff] [blame] | 37 | const bool flag = false; |
| 38 | if (flag && (x = test3_aux()) == 0) { |
| 39 | x = 1; |
| 40 | } |
Ted Kremenek | 609e317 | 2011-02-02 23:35:53 +0000 | [diff] [blame] | 41 | return x; // expected-warning{{variable 'x' is possibly uninitialized when used here}} |
Ted Kremenek | 2d4bed1 | 2011-01-20 21:25:31 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Ted Kremenek | 09f57b9 | 2011-02-05 01:18:18 +0000 | [diff] [blame] | 44 | enum test4_A { |
| 45 | test4_A_a, test_4_A_b |
| 46 | }; |
| 47 | test4_A test4() { |
| 48 | test4_A a; // expected-note{{variable 'a' is declared here}} |
| 49 | return a; // expected-warning{{variable 'a' is possibly uninitialized when used here}} |
| 50 | } |
| 51 | |
Ted Kremenek | b831c67 | 2011-03-29 01:40:00 +0000 | [diff] [blame] | 52 | // This test previously crashed Sema. |
| 53 | class Rdar9188004A { |
| 54 | public: |
| 55 | virtual ~Rdar9188004A(); |
| 56 | }; |
| 57 | |
| 58 | template< typename T > class Rdar9188004B : public Rdar9188004A { |
| 59 | virtual double *foo(Rdar9188004B *next) const { |
| 60 | double *values = next->foo(0); |
| 61 | try { |
| 62 | } |
| 63 | catch(double e) { |
| 64 | values[0] = e; |
| 65 | } |
| 66 | return 0; |
| 67 | } |
| 68 | }; |
| 69 | class Rdar9188004C : public Rdar9188004B<Rdar9188004A> { |
| 70 | virtual void bar(void) const; |
| 71 | }; |
| 72 | void Rdar9188004C::bar(void) const {} |
Ted Kremenek | f8adeef | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 73 | |
| 74 | // Don't warn about uninitialized variables in unreachable code. |
| 75 | void PR9625() { |
| 76 | if (false) { |
| 77 | int x; |
| 78 | (void)static_cast<float>(x); // no-warning |
| 79 | } |
| 80 | } |
Ted Kremenek | a21612f | 2011-04-07 20:02:56 +0000 | [diff] [blame] | 81 | |
| 82 | // Don't warn about variables declared in "catch" |
| 83 | void RDar9251392_bar(const char *msg); |
| 84 | |
| 85 | void RDar9251392() { |
| 86 | try { |
| 87 | throw "hi"; |
| 88 | } |
| 89 | catch (const char* msg) { |
| 90 | RDar9251392_bar(msg); // no-warning |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |