| 1 |
// RUN: %clang_cc1 -fsyntax-only -verify -fblocks -pedantic %s |
| 2 |
// REQUIRES: LP64 |
| 3 |
|
| 4 |
struct foo { int a, b; }; |
| 5 |
|
| 6 |
static struct foo t = (struct foo){0,0}; |
| 7 |
static struct foo t1 = __builtin_choose_expr(0, (struct foo){0,0}, (struct foo){0,0}); |
| 8 |
static struct foo t2 = {0,0}; |
| 9 |
static struct foo t3 = t2; // expected-error {{initializer element is not a compile-time constant}} |
| 10 |
static int *p = (int []){2,4}; |
| 11 |
static int x = (int){1}; |
| 12 |
|
| 13 |
static int *p2 = (int []){2,x}; // expected-error {{initializer element is not a compile-time constant}} |
| 14 |
static long *p3 = (long []){2,"x"}; // expected-error {{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char[2]'}} |
| 15 |
|
| 16 |
typedef struct { } cache_t; // expected-warning{{empty struct is a GNU extension}} |
| 17 |
static cache_t clo_I1_cache = ((cache_t) { } ); // expected-warning{{use of an empty initializer is a C23 extension}} |
| 18 |
|
| 19 |
typedef struct Test {int a;int b;} Test; |
| 20 |
static Test* ll = &(Test) {0,0}; |
| 21 |
|
| 22 |
extern void fooFunc(struct foo *pfoo); |
| 23 |
|
| 24 |
int main(int argc, char **argv) { |
| 25 |
int *l = (int []){x, *p, *p2}; |
| 26 |
fooFunc(&(struct foo){ 1, 2 }); |
| 27 |
} |
| 28 |
|
| 29 |
struct Incomplete; // expected-note{{forward declaration of 'struct Incomplete'}} |
| 30 |
struct Incomplete* I1 = &(struct Incomplete){1, 2, 3}; // expected-error {{variable has incomplete type}} |
| 31 |
void IncompleteFunc(unsigned x) { |
| 32 |
struct Incomplete* I2 = (struct foo[x]){1, 2, 3}; // expected-error {{compound literal cannot be of variable-length array type}} |
| 33 |
(void){1,2,3}; // expected-error {{variable has incomplete type}} |
| 34 |
(void(void)) { 0 }; // expected-error{{illegal initializer type 'void (void)'}} |
| 35 |
} |
| 36 |
|
| 37 |
// PR6080 |
| 38 |
int array[(sizeof(int[3]) == sizeof( (int[]) {0,1,2} )) ? 1 : -1]; |
| 39 |
|
| 40 |
// Constant restriction should not apply to compound literals in blocks |
| 41 |
int (^block)(int) = ^(int i) { |
| 42 |
int *array = (int[]) {i, i + 2, i + 4}; |
| 43 |
return array[i]; |
| 44 |
}; |
| 45 |
|
| 46 |
// C99 6.5.2.5 Compound literals constraint 1: The type name shall specify an object type or an array of unknown size, but not a variable length array type. |
| 47 |
// So check that VLA type compound literals are rejected (see https://github.com/llvm/llvm-project/issues/89835). |
| 48 |
void vla(int n) { |
| 49 |
int size = 5; |
| 50 |
(void)(int[size]){}; // expected-warning {{use of an empty initializer is a C23 extension}} |
| 51 |
// expected-error@-1 {{compound literal cannot be of variable-length array type}} |
| 52 |
(void)(int[size]){1}; // expected-error {{compound literal cannot be of variable-length array type}} |
| 53 |
(void)(int[size]){1,2,3}; // expected-error {{compound literal cannot be of variable-length array type}} |
| 54 |
(void)(int[size]){1,2,3,4,5}; // expected-error {{compound literal cannot be of variable-length array type}} |
| 55 |
} |