2009-12-16 04:14:24 +08:00
// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
2007-09-02 10:04:30 +08:00
2007-09-13 04:13:48 +08:00
extern int foof ( ) = 1 ; // expected-error{{illegal initializer (only variables can be initialized)}}
2007-09-02 23:34:30 +08:00
static int x , y , z ;
2008-08-10 09:58:45 +08:00
static int ary [ ] = { x , y , z } ; // expected-error{{initializer element is not a compile-time constant}}
int ary2 [ ] = { x , y , z } ; // expected-error{{initializer element is not a compile-time constant}}
2007-09-02 23:34:30 +08:00
extern int fileScopeExtern [ 3 ] = { 1 , 3 , 5 } ; // expected-warning{{'extern' variable has an initializer}}
2010-04-10 01:53:29 +08:00
static long ary3 [ ] = { 1 , " abc " , 3 , 4 } ; // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
2007-09-03 04:30:18 +08:00
2007-09-02 10:04:30 +08:00
void func ( ) {
int x = 1 ;
2007-09-13 04:13:48 +08:00
typedef int TInt = 1 ; // expected-error{{illegal initializer (only variables can be initialized)}}
2007-09-03 09:24:23 +08:00
int xComputeSize [ ] = { 1 , 3 , 5 } ;
2007-09-02 10:04:30 +08:00
2007-09-03 04:30:18 +08:00
int x3 [ x ] = { 1 , 2 } ; // expected-error{{variable-sized object may not be initialized}}
2007-09-02 10:04:30 +08:00
2009-05-16 19:45:48 +08:00
int x4 = { 1 , 2 } ; // expected-warning{{excess elements in scalar initializer}}
2007-09-02 10:04:30 +08:00
int y [ 4 ] [ 3 ] = {
{ 1 , 3 , 5 } ,
{ 2 , 4 , 6 } ,
{ 3 , 5 , 7 } ,
} ;
int y2 [ 4 ] [ 3 ] = {
1 , 3 , 5 , 2 , 4 , 6 , 3 , 5 , 7
} ;
2007-09-03 09:24:23 +08:00
int y3 [ 4 ] [ 3 ] = {
{ 1 , 3 , 5 } ,
{ 2 , 4 , 6 } ,
{ 3 , 5 , 7 } ,
{ 4 , 6 , 8 } ,
2009-02-19 06:23:55 +08:00
{ 5 } , // expected-warning{{excess elements in array initializer}}
2007-09-03 09:24:23 +08:00
} ;
2007-09-02 10:04:30 +08:00
struct threeElements {
int a , b , c ;
} z = { 1 } ;
2010-04-10 01:53:29 +08:00
struct threeElements * p = 7 ; // expected-warning{{incompatible integer to pointer conversion initializing 'struct threeElements *' with an expression of type 'int'}}
2007-09-02 23:34:30 +08:00
extern int blockScopeExtern [ 3 ] = { 1 , 3 , 5 } ; // expected-error{{'extern' variable cannot have an initializer}}
2007-09-03 04:30:18 +08:00
2012-04-05 08:16:44 +08:00
static long x2 [ 3 ] = { 1.0 ,
" abc " , // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
2012-05-16 00:56:36 +08:00
5.8 } ; // expected-warning {{implicit conversion from 'double' to 'long' changes value from 5.8 to 5}}
2007-09-02 10:04:30 +08:00
}
2007-09-04 10:20:04 +08:00
void test ( ) {
int y1 [ 3 ] = {
2011-09-20 07:17:44 +08:00
{ 1 , 2 , 3 } // expected-warning{{excess elements in scalar initializer}}
2007-09-04 10:20:04 +08:00
} ;
int y3 [ 4 ] [ 3 ] = {
{ 1 , 3 , 5 } ,
{ 2 , 4 , 6 } ,
{ 3 , 5 , 7 } ,
{ 4 , 6 , 8 } ,
2009-02-19 06:23:55 +08:00
{ } , // expected-warning{{use of GNU empty initializer extension}} expected-warning{{excess elements in array initializer}}
2007-09-04 10:20:04 +08:00
} ;
int y4 [ 4 ] [ 3 ] = {
2009-02-19 06:23:55 +08:00
{ 1 , 3 , 5 , 2 } , // expected-warning{{excess elements in array initializer}}
2007-09-04 10:20:04 +08:00
{ 4 , 6 } ,
{ 3 , 5 , 7 } ,
2008-01-25 08:51:06 +08:00
{ 4 , 6 , 8 } ,
2007-09-04 10:20:04 +08:00
} ;
}
void allLegalAndSynonymous ( ) {
short q [ 4 ] [ 3 ] [ 2 ] = {
{ 1 } ,
{ 2 , 3 } ,
{ 4 , 5 , 6 }
} ;
short q2 [ 4 ] [ 3 ] [ 2 ] = {
{ 1 , 0 , 0 , 0 , 0 , 0 } ,
{ 2 , 3 , 0 , 0 , 0 , 0 } ,
{ 4 , 5 , 6 }
} ;
short q3 [ 4 ] [ 3 ] [ 2 ] = {
{
{ 1 } ,
} ,
{
{ 2 , 3 } ,
} ,
{
{ 4 , 5 } ,
{ 6 } ,
} ,
} ;
}
void legal ( ) {
short q [ ] [ 3 ] [ 2 ] = {
{ 1 } ,
{ 2 , 3 } ,
{ 4 , 5 , 6 }
} ;
2009-01-29 05:54:33 +08:00
int q_sizecheck [ ( sizeof ( q ) / sizeof ( short [ 3 ] [ 2 ] ) ) = = 3 ? 1 : - 1 ] ;
2007-09-04 10:20:04 +08:00
}
2007-10-18 11:27:23 +08:00
unsigned char asso_values [ ] = { 34 } ;
int legal2 ( ) {
return asso_values [ 0 ] ;
}
2007-09-04 10:20:04 +08:00
void illegal ( ) {
short q2 [ 4 ] [ ] [ 2 ] = { // expected-error{{array has incomplete element type 'short [][2]'}}
{ 1 , 0 , 0 , 0 , 0 , 0 } ,
{ 2 , 3 , 0 , 0 , 0 , 0 } ,
{ 4 , 5 , 6 }
} ;
short q3 [ 4 ] [ 3 ] [ ] = { // expected-error{{array has incomplete element type 'short []'}}
{
{ 1 } ,
} ,
{
{ 2 , 3 } ,
} ,
{
{ 4 , 5 } ,
{ 6 } ,
} ,
} ;
2008-04-02 09:05:10 +08:00
int a [ ] [ ] = { 1 , 2 } ; // expected-error{{array has incomplete element type 'int []'}}
2007-09-04 10:20:04 +08:00
}
typedef int AryT [ ] ;
void testTypedef ( )
{
AryT a = { 1 , 2 } , b = { 3 , 4 , 5 } ;
2009-01-29 05:54:33 +08:00
int a_sizecheck [ ( sizeof ( a ) / sizeof ( int ) ) = = 2 ? 1 : - 1 ] ;
int b_sizecheck [ ( sizeof ( b ) / sizeof ( int ) ) = = 3 ? 1 : - 1 ] ;
2007-09-04 10:20:04 +08:00
}
2007-12-11 06:44:33 +08:00
static char const xx [ ] = " test " ;
2009-01-29 05:54:33 +08:00
int xx_sizecheck [ ( sizeof ( xx ) / sizeof ( char ) ) = = 5 ? 1 : - 1 ] ;
2007-12-11 06:44:33 +08:00
static char const yy [ 5 ] = " test " ;
static char const zz [ 3 ] = " test " ; // expected-warning{{initializer-string for char array is too long}}
2009-09-09 23:08:12 +08:00
void charArrays ( ) {
static char const test [ ] = " test " ;
int test_sizecheck [ ( sizeof ( test ) / sizeof ( char ) ) = = 5 ? 1 : - 1 ] ;
static char const test2 [ ] = { " weird stuff " } ;
static char const test3 [ ] = { " test " , " excess stuff " } ; // expected-warning{{excess elements in char array initializer}}
2007-12-11 06:44:33 +08:00
char * cp [ ] = { " Hello " } ;
char c [ ] = { " Hello " } ;
int l [ sizeof ( c ) = = 6 ? 1 : - 1 ] ;
2010-04-10 01:53:29 +08:00
int i [ ] = { " Hello " } ; // expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [7]'}}
2009-02-19 06:23:55 +08:00
char c2 [ ] = { " Hello " , " Good bye " } ; //expected-warning{{excess elements in char array initializer}}
2007-12-11 06:44:33 +08:00
2010-04-10 01:53:29 +08:00
int i2 [ 1 ] = { " Hello " } ; //expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [6]'}}
2007-12-11 06:44:33 +08:00
char c3 [ 5 ] = { " Hello " } ;
char c4 [ 4 ] = { " Hello " } ; //expected-warning{{initializer-string for char array is too long}}
2008-08-19 04:28:46 +08:00
int i3 [ ] = { } ; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
2007-12-11 06:44:33 +08:00
}
2008-01-22 07:53:58 +08:00
void variableArrayInit ( ) {
int a = 4 ;
2012-01-05 08:13:19 +08:00
char strlit [ a ] = " foo " ; //expected-error{{variable-sized object may not be initialized}}
2008-01-22 07:53:58 +08:00
int b [ a ] = { 1 , 2 , 4 } ; //expected-error{{variable-sized object may not be initialized}}
}
2008-01-25 08:51:06 +08:00
// Pure array tests
float r1 [ 10 ] = { { 7 } } ; //expected-warning{{braces around scalar initializer}}
float r2 [ ] = { { 8 } } ; //expected-warning{{braces around scalar initializer}}
char r3 [ ] [ 5 ] = { 1 , 2 , 3 , 4 , 5 , 6 } ;
2009-01-29 05:54:33 +08:00
int r3_sizecheck [ ( sizeof ( r3 ) / sizeof ( char [ 5 ] ) ) = = 2 ? 1 : - 1 ] ;
2008-01-25 08:51:06 +08:00
char r3_2 [ sizeof r3 = = 10 ? 1 : - 1 ] ;
2009-02-19 06:23:55 +08:00
float r4 [ 1 ] [ 2 ] = { 1 , { 2 } , 3 , 4 } ; //expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}}
2008-01-25 08:51:06 +08:00
char r5 [ ] [ 5 ] = { " aa " , " bbb " , " ccccc " } ;
char r6 [ sizeof r5 = = 15 ? 1 : - 1 ] ;
const char r7 [ ] = " zxcv " ;
char r8 [ 5 ] = " 5char " ;
char r9 [ 5 ] = " 6chars " ; //expected-warning{{initializer-string for char array is too long}}
int r11 [ 0 ] = { } ; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}}
// Some struct tests
void autoStructTest ( ) {
struct s1 { char a ; char b ; } t1 ;
struct s2 { struct s1 c ; } t2 = { t1 } ;
// The following is a less than great diagnostic (though it's on par with EDG).
2010-04-10 01:53:29 +08:00
struct s1 t3 [ ] = { t1 , t1 , " abc " , 0 } ; //expected-warning{{incompatible pointer to integer conversion initializing 'char' with an expression of type 'char [4]'}}
2008-01-25 08:51:06 +08:00
int t4 [ sizeof t3 = = 6 ? 1 : - 1 ] ;
}
2008-01-28 10:00:41 +08:00
struct foo { int z ; } w ;
int bar ( void ) {
2010-04-10 01:53:29 +08:00
struct foo z = { w } ; //expected-error{{initializing 'int' with an expression of incompatible type 'struct foo'}}
2008-01-28 10:00:41 +08:00
return z . z ;
}
2008-01-25 08:51:06 +08:00
struct s3 { void ( * a ) ( void ) ; } t5 = { autoStructTest } ;
2009-03-20 08:32:56 +08:00
struct { int a ; int b [ ] ; } t6 = { 1 , { 1 , 2 , 3 } } ; / / expected - warning { { flexible array initialization is a GNU extension } } \
// expected-note{{initialized flexible array member 'b' is here}}
2008-01-25 08:51:06 +08:00
union { char a ; int b ; } t7 [ ] = { 1 , 2 , 3 } ;
int t8 [ sizeof t7 = = ( 3 * sizeof ( int ) ) ? 1 : - 1 ] ;
struct bittest { int : 31 , a , : 21 , : 12 , b ; } ;
2009-02-19 06:23:55 +08:00
struct bittest bittestvar = { 1 , 2 , 3 , 4 } ; //expected-warning{{excess elements in struct initializer}}
2008-01-25 08:51:06 +08:00
// Not completely sure what should happen here...
2008-05-20 04:29:35 +08:00
int u1 = { } ; //expected-warning{{use of GNU empty initializer extension}} expected-error{{scalar initializer cannot be empty}}
2010-08-14 11:14:53 +08:00
int u2 = { { 3 } } ; //expected-warning{{too many braces around scalar initializer}}
2008-01-25 08:51:06 +08:00
2008-05-25 21:22:35 +08:00
// PR2362
void varArray ( ) {
int c [ ] [ x ] = { 0 } ; //expected-error{{variable-sized object may not be initialized}}
}
2008-05-25 21:49:22 +08:00
// PR2151
2011-12-30 05:57:33 +08:00
void emptyInit ( ) { struct { } x [ ] = { 6 } ; } / / expected - warning { { empty struct is a GNU extension } } \
2010-07-29 22:29:34 +08:00
// expected-error{{initializer for aggregate with no elements}}
2008-05-25 21:49:22 +08:00
2009-07-22 08:43:08 +08:00
void noNamedInit ( ) {
struct { int : 5 ; } x [ ] = { 6 } ; //expected-error{{initializer for aggregate with no elements}}
2008-05-25 22:03:31 +08:00
}
struct { int a ; int : 5 ; } noNamedImplicit [ ] = { 1 , 2 , 3 } ;
int noNamedImplicitCheck [ sizeof ( noNamedImplicit ) = = 3 * sizeof ( * noNamedImplicit ) ? 1 : - 1 ] ;
2008-09-02 06:28:55 +08:00
// ptrs are constant
struct soft_segment_descriptor {
2009-09-09 23:08:12 +08:00
long ssd_base ;
2008-09-02 06:28:55 +08:00
} ;
static int dblfault_tss ;
union uniao { int ola ; } xpto [ 1 ] ;
struct soft_segment_descriptor gdt_segs [ ] = {
2009-09-09 23:08:12 +08:00
{ ( long ) & dblfault_tss } ,
{ ( long ) xpto } ,
2008-09-02 06:28:55 +08:00
} ;
2008-09-02 02:42:41 +08:00
static void sppp_ipv6cp_up ( ) ;
2011-12-30 05:57:33 +08:00
const struct { } ipcp = { sppp_ipv6cp_up } ; / / expected - warning { { empty struct is a GNU extension } } \
2010-07-29 22:29:34 +08:00
// expected-warning{{excess elements in struct initializer}}
2009-01-29 05:54:33 +08:00
2012-02-03 23:47:04 +08:00
struct _Matrix { union { float m [ 4 ] [ 4 ] ; } ; } ; //expected-warning{{anonymous unions are a C11 extension}}
2009-01-29 05:54:33 +08:00
typedef struct _Matrix Matrix ;
void test_matrix ( ) {
const Matrix mat1 = {
{ { 1.0f , 2.0f , 3.0f , 4.0f ,
5.0f , 6.0f , 7.0f , 8.0f ,
9.0f , 10.0f , 11.0f , 12.0f ,
13.0f , 14.0f , 15.0f , 16.0f } }
} ;
const Matrix mat2 = {
1.0f , 2.0f , 3.0f , 4.0f ,
5.0f , 6.0f , 7.0f , 8.0f ,
9.0f , 10.0f , 11.0f , 12.0f ,
13.0f , 14.0f , 15.0f , 16.0f
} ;
}
2009-05-30 04:20:05 +08:00
char badchararray [ 1 ] = { badchararray [ 0 ] , " asdf " } ; // expected-warning {{excess elements in array initializer}} expected-error {{initializer element is not a compile-time constant}}
2011-02-23 02:29:51 +08:00
// Test the GNU extension for initializing an array from an array
// compound literal. PR9261.
typedef int int5 [ 5 ] ;
int a1 [ 5 ] = ( int [ ] ) { 1 , 2 , 3 , 4 , 5 } ; // expected-warning{{initialization of an array of type 'int [5]' from a compound literal of type 'int [5]' is a GNU extension}}
int a2 [ 5 ] = ( int [ 5 ] ) { 1 , 2 , 3 , 4 , 5 } ; // expected-warning{{initialization of an array of type 'int [5]' from a compound literal of type 'int [5]' is a GNU extension}}
int a3 [ ] = ( ( int [ ] ) { 1 , 2 , 3 , 4 , 5 } ) ; // expected-warning{{initialization of an array of type 'int []' from a compound literal of type 'int [5]' is a GNU extension}}
int a4 [ ] = ( int [ 5 ] ) { 1 , 2 , 3 , 4 , 5 } ; // expected-warning{{initialization of an array of type 'int []' from a compound literal of type 'int [5]' is a GNU extension}}
int a5 [ ] = ( int5 ) { 1 , 2 , 3 , 4 , 5 } ; // expected-warning{{initialization of an array of type 'int []' from a compound literal of type 'int5' (aka 'int [5]') is a GNU extension}}
int a6 [ 5 ] = ( int [ ] ) { 1 , 2 , 3 } ; // expected-error{{cannot initialize array of type 'int [5]' with array of type 'int [3]'}}
int nonconst_value ( ) ;
int a7 [ 5 ] = ( int [ 5 ] ) { 1 , 2 , 3 , 4 , nonconst_value ( ) } ; // expected-error{{initializer element is not a compile-time constant}}
2012-01-05 08:13:19 +08:00
// <rdar://problem/10636946>
__attribute__ ( ( weak ) ) const unsigned int test10_bound = 10 ;
char test10_global [ test10_bound ] ; // expected-error {{variable length array declaration not allowed at file scope}}
void test10 ( ) {
char test10_local [ test10_bound ] = " help " ; // expected-error {{variable-sized object may not be initialized}}
}