diff options
author | dim <dim@FreeBSD.org> | 2011-10-20 21:14:49 +0000 |
---|---|---|
committer | dim <dim@FreeBSD.org> | 2011-10-20 21:14:49 +0000 |
commit | 3963a48221351c61c17fb3f382341ab04809a3d3 (patch) | |
tree | ee2483e98b09cac943dc93a6969d83ca737ff139 /test/Sema/uninit-variables.c | |
parent | 611ba3ea3300b71eb95dc4e45f20eee5dddd32e1 (diff) | |
download | FreeBSD-src-3963a48221351c61c17fb3f382341ab04809a3d3.zip FreeBSD-src-3963a48221351c61c17fb3f382341ab04809a3d3.tar.gz |
Vendor import of clang release_30 branch r142614:
http://llvm.org/svn/llvm-project/cfe/branches/release_30@142614
Diffstat (limited to 'test/Sema/uninit-variables.c')
-rw-r--r-- | test/Sema/uninit-variables.c | 117 |
1 files changed, 83 insertions, 34 deletions
diff --git a/test/Sema/uninit-variables.c b/test/Sema/uninit-variables.c index 0caab3d..49af4f3 100644 --- a/test/Sema/uninit-variables.c +++ b/test/Sema/uninit-variables.c @@ -1,7 +1,10 @@ // RUN: %clang_cc1 -fsyntax-only -Wuninitialized -Wconditional-uninitialized -fsyntax-only -fblocks %s -verify +typedef __typeof(sizeof(int)) size_t; +void *malloc(size_t); + int test1() { - int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}} + int x; // expected-note{{initialize the variable 'x' to silence this warning}} return x; // expected-warning{{variable 'x' is uninitialized when used here}} } @@ -17,25 +20,25 @@ int test3() { } int test4() { - int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}} + int x; // expected-note{{initialize the variable 'x' to silence this warning}} ++x; // expected-warning{{variable 'x' is uninitialized when used here}} return x; } int test5() { - int x, y; // expected-note{{variable 'y' is declared here}} expected-note{{add initialization to silence this warning}} + int x, y; // expected-note{{initialize the variable 'y' to silence this warning}} x = y; // expected-warning{{variable 'y' is uninitialized when used here}} return x; } int test6() { - int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}} + int x; // expected-note{{initialize the variable 'x' to silence this warning}} x += 2; // expected-warning{{variable 'x' is uninitialized when used here}} return x; } int test7(int y) { - int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}} + int x; // expected-note{{initialize the variable 'x' to silence this warning}} if (y) x = 1; return x; // expected-warning{{variable 'x' may be uninitialized when used here}} @@ -51,7 +54,7 @@ int test8(int y) { } int test9(int n) { - int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}} + int x; // expected-note{{initialize the variable 'x' to silence this warning}} for (unsigned i = 0 ; i < n; ++i) { if (i == n - 1) break; @@ -61,7 +64,7 @@ int test9(int n) { } int test10(unsigned n) { - int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}} + int x; // expected-note{{initialize the variable 'x' to silence this warning}} for (unsigned i = 0 ; i < n; ++i) { x = 1; } @@ -69,7 +72,7 @@ int test10(unsigned n) { } int test11(unsigned n) { - int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}} + int x; // expected-note{{initialize the variable 'x' to silence this warning}} for (unsigned i = 0 ; i <= n; ++i) { x = 1; } @@ -77,7 +80,7 @@ int test11(unsigned n) { } void test12(unsigned n) { - for (unsigned i ; n ; ++i) ; // expected-warning{{variable 'i' may be uninitialized when used here}} expected-note{{variable 'i' is declared here}} expected-note{{add initialization to silence this warning}} + for (unsigned i ; n ; ++i) ; // expected-warning{{variable 'i' is uninitialized when used here}} expected-note{{initialize the variable 'i' to silence this warning}} } int test13() { @@ -91,10 +94,15 @@ void test14() { for (;;) {} } -int test15() { - int x = x; // no-warning: signals intended lack of initialization. \ - // expected-note{{variable 'x' is declared here}} - return x; // expected-warning{{variable 'x' is uninitialized when used here}} +void test15() { + int x = x; // no-warning: signals intended lack of initialization. +} + +int test15b() { + // Warn here with the self-init, since it does result in a use of + // an unintialized variable and this is the root cause. + int x = x; // expected-warning {{variable 'x' is uninitialized when used within its own initialization}} + return x; } // Don't warn in the following example; shows dataflow confluence. @@ -108,7 +116,7 @@ void test16() { void test17() { // Don't warn multiple times about the same uninitialized variable // along the same path. - int *x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}} + int *x; // expected-note{{initialize the variable 'x' to silence this warning}} *x = 1; // expected-warning{{variable 'x' is uninitialized when used here}} *x = 1; // no-warning } @@ -132,14 +140,14 @@ int test19() { } int test20() { - int z; // expected-note{{variable 'z' is declared here}} expected-note{{add initialization to silence this warning}} + int z; // expected-note{{initialize the variable 'z' to silence this warning}} if ((test19_aux1() + test19_aux2() && test19_aux1()) || test19_aux3(&z)) return z; // expected-warning{{variable 'z' may be uninitialized when used here}} return 0; } int test21(int x, int y) { - int z; // expected-note{{variable 'z' is declared here}} expected-note{{add initialization to silence this warning}} + int z; // expected-note{{initialize the variable 'z' to silence this warning}} if ((x && y) || test19_aux3(&z) || test19_aux2()) return z; // expected-warning{{variable 'z' may be uninitialized when used here}} return 0; @@ -164,7 +172,7 @@ int test23() { // conditionals. This possibly can be handled by making the CFG itself // represent such control-dependencies, but it is a niche case. int test24(int flag) { - unsigned val; // expected-note{{variable 'val' is declared here}} expected-note{{add initialization to silence this warning}} + unsigned val; // expected-note{{initialize the variable 'val' to silence this warning}} if (flag) val = 1; if (!flag) @@ -173,13 +181,13 @@ int test24(int flag) { } float test25() { - float x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}} + float x; // expected-note{{initialize the variable 'x' to silence this warning}} return x; // expected-warning{{variable 'x' is uninitialized when used here}} } typedef int MyInt; MyInt test26() { - MyInt x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}} + MyInt x; // expected-note{{initialize the variable 'x' to silence this warning}} return x; // expected-warning{{variable 'x' is uninitialized when used here}} } @@ -190,12 +198,12 @@ int test27() { } int test28() { - int len; // expected-note{{variable 'len' is declared here}} expected-note{{add initialization to silence this warning}} + int len; // expected-note{{initialize the variable 'len' to silence this warning}} return sizeof(int[len]); // expected-warning{{variable 'len' is uninitialized when used here}} } void test29() { - int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}} + int x; // expected-note{{initialize the variable 'x' to silence this warning}} (void) ^{ (void) x; }; // expected-warning{{variable 'x' is uninitialized when captured by block}} } @@ -220,7 +228,7 @@ void test_33() { } int test_34() { - int x; // expected-note{{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}} + int x; // expected-note{{initialize the variable 'x' to silence this warning}} (void) x; return x; // expected-warning{{variable 'x' is uninitialized when used here}} } @@ -234,10 +242,10 @@ void test35(int x) { // Test handling of indirect goto. void test36() { - void **pc; // expected-note{{variable 'pc' is declared here}} expected-note{{add initialization to silence this warning}} + void **pc; // expected-note{{initialize the variable 'pc' to silence this warning}} void *dummy[] = { &&L1, &&L2 }; L1: - goto *pc; // expected-warning{{variable 'pc' may be uninitialized when used here}} + goto *pc; // expected-warning{{variable 'pc' is uninitialized when used here}} L2: goto *pc; } @@ -263,19 +271,19 @@ int test38(int r, int x, int y) } int test39(int x) { - int y; // expected-note {{variable 'y' is declared here}} expected-note{{add initialization to silence this warning}} + int y; // expected-note{{initialize the variable 'y' to silence this warning}} int z = x + y; // expected-warning {{variable 'y' is uninitialized when used here}} return z; } int test40(int x) { - int y; // expected-note {{variable 'y' is declared here}} expected-note{{add initialization to silence this warning}} + int y; // expected-note{{initialize the variable 'y' to silence this warning}} return x ? 1 : y; // expected-warning {{variable 'y' is uninitialized when used here}} } int test41(int x) { - int y; // expected-note {{variable 'y' is declared here}} expected-note{{add initialization to silence this warning}} + int y; // expected-note{{initialize the variable 'y' to silence this warning}} if (x) y = 1; // no-warning return y; // expected-warning {{variable 'y' may be uninitialized when used here}} } @@ -287,17 +295,17 @@ void test42() { void test43_aux(int x); void test43(int i) { - int x; // expected-note {{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}} + int x; // expected-note{{initialize the variable 'x' to silence this warning}} for (i = 0 ; i < 10; i++) - test43_aux(x++); // expected-warning {{variable 'x' may be uninitialized when used here}} + test43_aux(x++); // expected-warning {{variable 'x' is uninitialized when used here}} } void test44(int i) { int x = i; - int y; // expected-note {{variable 'y' is declared here}} expected-note{{add initialization to silence this warning}} + int y; // expected-note{{initialize the variable 'y' to silence this warning}} for (i = 0; i < 10; i++ ) { test43_aux(x++); // no-warning - x += y; // expected-warning {{variable 'y' may be uninitialized when used here}} + x += y; // expected-warning {{variable 'y' is uninitialized when used here}} } } @@ -310,7 +318,7 @@ int test45(int j) { void test46() { - int i; // expected-note {{variable 'i' is declared here}} expected-note{{add initialization to silence this warning}} + int i; // expected-note{{initialize the variable 'i' to silence this warning}} int j = i ? : 1; // expected-warning {{variable 'i' is uninitialized when used here}} } @@ -341,7 +349,7 @@ int test51(void) // FIXME: This is a false positive, but it tests logical operations in switch statements. int test52(int a, int b) { - int x; // expected-note {{variable 'x' is declared here}} expected-note {{add initialization to silence this warning}} + int x; // expected-note {{initialize the variable 'x' to silence this warning}} switch (a || b) { // expected-warning {{switch condition has boolean value}} case 0: x = 1; @@ -353,10 +361,15 @@ int test52(int a, int b) { return x; // expected-warning {{variable 'x' may be uninitialized when used here}} } +void test53() { + int x; // expected-note {{initialize the variable 'x' to silence this warning}} + int y = (x); // expected-warning {{variable 'x' is uninitialized when used here}} +} + // This CFG caused the uninitialized values warning to inf-loop. extern int PR10379_g(); void PR10379_f(int *len) { - int new_len; // expected-note {{variable 'new_len' is declared here}} expected-note{{add initialization to silence this warning}} + int new_len; // expected-note{{initialize the variable 'new_len' to silence this warning}} for (int i = 0; i < 42 && PR10379_g() == 0; i++) { if (PR10379_g() == 1) continue; @@ -367,3 +380,39 @@ void PR10379_f(int *len) { *len += new_len; // expected-warning {{variable 'new_len' may be uninitialized when used here}} } } + +// Test that sizeof(VLA) doesn't trigger a warning. +void test_vla_sizeof(int x) { + double (*memory)[2][x] = malloc(sizeof(*memory)); // no-warning +} + +// Test absurd case of deadcode + use of blocks. This previously was a false positive +// due to an analysis bug. +int test_block_and_dead_code() { + __block int x; + ^{ x = 1; }(); + if (0) + return x; + return x; // no-warning +} + +// This previously triggered an infinite loop in the analysis. +void PR11069(int a, int b) { + unsigned long flags; + for (;;) { + if (a && !b) + break; + } + for (;;) { + // This does not trigger a warning because it isn't a real use. + (void)(flags); // no-warning + } +} + +// Test uninitialized value used in loop condition. +void rdar9432305(float *P) { + int i; // expected-note {{initialize the variable 'i' to silence this warning}} + for (; i < 10000; ++i) // expected-warning {{variable 'i' is uninitialized when used here}} + P[i] = 0.0f; +} + |