Ted Kremenek | 4dccb90 | 2011-01-18 05:00:42 +0000 | [diff] [blame^] | 1 | // RUN: %clang_cc1 -fsyntax-only -Wuninitialized-experimental -fsyntax-only %s -verify |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 2 | |
| 3 | int test1() { |
| 4 | int x; |
| 5 | return x; // expected-warning{{use of uninitialized variable 'x'}} |
| 6 | } |
| 7 | |
| 8 | int test2() { |
| 9 | int x = 0; |
| 10 | return x; // no-warning |
| 11 | } |
| 12 | |
| 13 | int test3() { |
| 14 | int x; |
| 15 | x = 0; |
| 16 | return x; // no-warning |
| 17 | } |
| 18 | |
| 19 | int test4() { |
| 20 | int x; |
| 21 | ++x; // expected-warning{{use of uninitialized variable 'x'}} |
| 22 | return x; |
| 23 | } |
| 24 | |
| 25 | int test5() { |
| 26 | int x, y; |
| 27 | x = y; // expected-warning{{use of uninitialized variable 'y'}} |
| 28 | return x; |
| 29 | } |
| 30 | |
| 31 | int test6() { |
| 32 | int x; |
| 33 | x += 2; // expected-warning{{use of uninitialized variable 'x'}} |
| 34 | return x; |
| 35 | } |
| 36 | |
| 37 | int test7(int y) { |
| 38 | int x; |
| 39 | if (y) |
| 40 | x = 1; |
| 41 | return x; // expected-warning{{use of uninitialized variable 'x'}} |
| 42 | } |
| 43 | |
| 44 | int test8(int y) { |
| 45 | int x; |
| 46 | if (y) |
| 47 | x = 1; |
| 48 | else |
| 49 | x = 0; |
| 50 | return x; // no-warning |
| 51 | } |
| 52 | |
| 53 | int test9(int n) { |
| 54 | int x; |
| 55 | for (unsigned i = 0 ; i < n; ++i) { |
| 56 | if (i == n - 1) |
| 57 | break; |
| 58 | x = 1; |
| 59 | } |
| 60 | return x; // expected-warning{{use of uninitialized variable 'x'}} |
| 61 | } |
| 62 | |
| 63 | int test10(unsigned n) { |
| 64 | int x; |
| 65 | for (unsigned i = 0 ; i < n; ++i) { |
| 66 | x = 1; |
| 67 | } |
| 68 | return x; // expected-warning{{use of uninitialized variable 'x'}} |
| 69 | } |
| 70 | |
| 71 | int test11(unsigned n) { |
| 72 | int x; |
| 73 | for (unsigned i = 0 ; i <= n; ++i) { |
| 74 | x = 1; |
| 75 | } |
| 76 | return x; // expected-warning{{use of uninitialized variable 'x'}} |
| 77 | } |
| 78 | |
| 79 | void test12(unsigned n) { |
| 80 | for (unsigned i ; n ; ++i) ; // expected-warning{{use of uninitialized variable 'i'}} |
| 81 | } |
| 82 | |
| 83 | int test13() { |
| 84 | static int i; |
| 85 | return i; // no-warning |
| 86 | } |
| 87 | |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 88 | // Simply don't crash on this test case. |
| 89 | void test14() { |
| 90 | const char *p = 0; |
| 91 | for (;;) {} |
| 92 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 94 | void test15() { |
| 95 | int x = x; // expected-warning{{use of uninitialized variable 'x'}} |
| 96 | } |
| 97 | |
| 98 | // Don't warn in the following example; shows dataflow confluence. |
| 99 | char *test16_aux(); |
| 100 | void test16() { |
| 101 | char *p = test16_aux(); |
| 102 | for (unsigned i = 0 ; i < 100 ; i++) |
| 103 | p[i] = 'a'; // no-warning |
| 104 | } |