2011-01-26 03:13:48 +08:00
// RUN: %clang_cc1 -fsyntax-only -Wuninitialized-experimental -fsyntax-only -fblocks %s -verify
2011-01-15 10:58:47 +08:00
int test1 ( ) {
2011-01-22 06:49:49 +08:00
int x ; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}}
2011-01-22 03:41:41 +08:00
return x ; // expected-note{{variable 'x' is possibly uninitialized when used here}}
2011-01-15 10:58:47 +08:00
}
int test2 ( ) {
int x = 0 ;
return x ; // no-warning
}
int test3 ( ) {
int x ;
x = 0 ;
return x ; // no-warning
}
int test4 ( ) {
2011-01-22 06:49:49 +08:00
int x ; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}}
2011-01-22 03:41:41 +08:00
+ + x ; // expected-note{{variable 'x' is possibly uninitialized when used here}}
2011-01-15 10:58:47 +08:00
return x ;
}
int test5 ( ) {
2011-01-22 06:49:49 +08:00
int x , y ; // expected-warning{{use of uninitialized variable 'y'}} expected-note{{add initialization to silence this warning}}
2011-01-22 03:41:41 +08:00
x = y ; // expected-note{{variable 'y' is possibly uninitialized when used here}}
2011-01-15 10:58:47 +08:00
return x ;
}
int test6 ( ) {
2011-01-22 06:49:49 +08:00
int x ; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}}
2011-01-22 03:41:41 +08:00
x + = 2 ; // expected-note{{variable 'x' is possibly uninitialized when used here}}
2011-01-15 10:58:47 +08:00
return x ;
}
int test7 ( int y ) {
2011-01-22 06:49:49 +08:00
int x ; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}}
2011-01-15 10:58:47 +08:00
if ( y )
x = 1 ;
2011-01-22 03:41:41 +08:00
return x ; // expected-note{{variable 'x' is possibly uninitialized when used here}}
2011-01-15 10:58:47 +08:00
}
int test8 ( int y ) {
int x ;
if ( y )
x = 1 ;
else
x = 0 ;
return x ; // no-warning
}
int test9 ( int n ) {
2011-01-22 06:49:49 +08:00
int x ; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}}
2011-01-15 10:58:47 +08:00
for ( unsigned i = 0 ; i < n ; + + i ) {
if ( i = = n - 1 )
break ;
2011-01-22 03:41:41 +08:00
x = 1 ;
2011-01-15 10:58:47 +08:00
}
2011-01-22 03:41:41 +08:00
return x ; // expected-note{{variable 'x' is possibly uninitialized when used here}}
2011-01-15 10:58:47 +08:00
}
int test10 ( unsigned n ) {
2011-01-22 06:49:49 +08:00
int x ; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}}
2011-01-15 10:58:47 +08:00
for ( unsigned i = 0 ; i < n ; + + i ) {
x = 1 ;
}
2011-01-22 03:41:41 +08:00
return x ; // expected-note{{variable 'x' is possibly uninitialized when used here}}
2011-01-15 10:58:47 +08:00
}
int test11 ( unsigned n ) {
2011-01-22 06:49:49 +08:00
int x ; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}}
2011-01-15 10:58:47 +08:00
for ( unsigned i = 0 ; i < = n ; + + i ) {
x = 1 ;
}
2011-01-22 03:41:41 +08:00
return x ; //expected-note{{variable 'x' is possibly uninitialized when used here}}
2011-01-15 10:58:47 +08:00
}
void test12 ( unsigned n ) {
2011-01-22 06:49:49 +08:00
for ( unsigned i ; n ; + + i ) ; // expected-warning{{use of uninitialized variable 'i'}} expected-note{{variable 'i' is possibly uninitialized when used here}}} expected-note{{add initialization to silence this warning}}
2011-01-15 10:58:47 +08:00
}
int test13 ( ) {
static int i ;
return i ; // no-warning
}
2011-01-18 12:53:25 +08:00
// Simply don't crash on this test case.
void test14 ( ) {
const char * p = 0 ;
for ( ; ; ) { }
}
void test15 ( ) {
2011-01-22 06:49:49 +08:00
int x = x ; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{variable 'x' is possibly uninitialized when used here}} expected-note{{add initialization to silence this warning}}
2011-01-18 12:53:25 +08:00
}
2011-01-15 10:58:47 +08:00
2011-01-18 12:53:25 +08:00
// Don't warn in the following example; shows dataflow confluence.
char * test16_aux ( ) ;
void test16 ( ) {
char * p = test16_aux ( ) ;
for ( unsigned i = 0 ; i < 100 ; i + + )
p [ i ] = ' a ' ; // no-warning
}
2011-01-19 05:18:58 +08:00
void test17 ( ) {
// Don't warn multiple times about the same uninitialized variable
// along the same path.
2011-01-22 03:41:46 +08:00
int * x ; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}}
2011-01-22 03:41:41 +08:00
* x = 1 ; // expected-note{{variable 'x' is possibly uninitialized when used here}}
2011-01-19 05:18:58 +08:00
* x = 1 ; // no-warning
}
2011-01-21 01:37:17 +08:00
int test18 ( int x , int y ) {
int z ;
if ( x & & y & & ( z = 1 ) ) {
return z ; // no-warning
}
return 0 ;
}
int test19_aux1 ( ) ;
int test19_aux2 ( ) ;
int test19_aux3 ( int * x ) ;
int test19 ( ) {
int z ;
if ( test19_aux1 ( ) + test19_aux2 ( ) & & test19_aux1 ( ) & & test19_aux3 ( & z ) )
return z ; // no-warning
return 0 ;
}
int test20 ( ) {
2011-01-22 06:49:49 +08:00
int z ; // expected-warning{{use of uninitialized variable 'z'}} expected-note{{add initialization to silence this warning}}
2011-01-21 01:37:17 +08:00
if ( ( test19_aux1 ( ) + test19_aux2 ( ) & & test19_aux1 ( ) ) | | test19_aux3 ( & z ) )
2011-01-22 03:41:41 +08:00
return z ; // expected-note{{variable 'z' is possibly uninitialized when used here}}
2011-01-21 01:37:17 +08:00
return 0 ;
}
int test21 ( int x , int y ) {
2011-01-22 06:49:49 +08:00
int z ; // expected-warning{{use of uninitialized variable 'z'}} expected-note{{add initialization to silence this warning}}
2011-01-21 01:37:17 +08:00
if ( ( x & & y ) | | test19_aux3 ( & z ) | | test19_aux2 ( ) )
2011-01-22 03:41:41 +08:00
return z ; // expected-note{{variable 'z' is possibly uninitialized when used here}}
2011-01-21 01:37:17 +08:00
return 0 ;
}
int test22 ( ) {
int z ;
while ( test19_aux1 ( ) + test19_aux2 ( ) & & test19_aux1 ( ) & & test19_aux3 ( & z ) )
return z ; // no-warning
return 0 ;
}
int test23 ( ) {
int z ;
for ( ; test19_aux1 ( ) + test19_aux2 ( ) & & test19_aux1 ( ) & & test19_aux3 ( & z ) ; )
return z ; // no-warning
return 0 ;
}
// The basic uninitialized value analysis doesn't have enough path-sensitivity
// to catch initializations relying on control-dependencies spanning multiple
// 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 ) {
2011-01-22 06:49:49 +08:00
unsigned val ; // expected-warning{{use of uninitialized variable 'val'}} expected-note{{add initialization to silence this warning}}
2011-01-21 01:37:17 +08:00
if ( flag )
val = 1 ;
if ( ! flag )
val = 1 ;
2011-01-22 03:41:41 +08:00
return val ; // expected-note{{variable 'val' is possibly uninitialized when used here}}
2011-01-21 01:37:17 +08:00
}
2011-01-22 06:49:49 +08:00
float test25 ( ) {
float x ; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}}
return x ; // expected-note{{variable 'x' is possibly uninitialized when used here}}
}
typedef int MyInt ;
MyInt test26 ( ) {
MyInt x ; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}}
return x ; // expected-note{{variable 'x' is possibly uninitialized when used here}}
}
2011-01-24 01:53:04 +08:00
// Test handling of sizeof().
int test27 ( ) {
struct test_27 { int x ; } * y ;
return sizeof ( y - > x ) ; // no-warning
}
int test28 ( ) {
int len ; // expected-warning{{use of uninitialized variable 'len'}} expected-note{{add initialization to silence this warning}}
return sizeof ( int [ len ] ) ; // expected-note{{variable 'len' is possibly uninitialized when used here}}
}
2011-01-26 03:13:48 +08:00
void test29 ( ) {
int x ; // expected-warning{{use of uninitialized variable 'x'}} expected-note{{add initialization to silence this warning}}
( void ) ^ { ( void ) x ; } ; // expected-note{{variable 'x' is possibly uninitialized when captured by block}}
}
void test30 ( ) {
static int x ; // no-warning
( void ) ^ { ( void ) x ; } ;
}
void test31 ( ) {
__block int x ; // no-warning
( void ) ^ { ( void ) x ; } ;
}
int test32_x ;
void test32 ( ) {
( void ) ^ { ( void ) test32_x ; } ; // no-warning
}