* c-common.def (COMPOUND_LITERAL_EXPR): New. * c-common.c (c_expand_expr): Handle COMPOUND_LITERAL_EXPR. (c_staticp): New function. * c-common.h (COMPOUND_LITERAL_EXPR_DECL): New. (c_staticp): Declare. * c-typeck.c (default_function_array_conversion, build_unary_op): Don't handle CONSTRUCTOR specially. (lvalue_p, mark_addressable): Handle COMPOUND_LITERAL_EXPR. * c-decl.c (build_compound_literal): New function. * c-tree.h (build_compound_literal): Declare. * c-parse.in (primary): Use build_compound_literal. * c-lang.c (LANG_HOOKS_STATICP): Define. * objc/objc-lang.c (LANG_HOOKS_STATICP): Likewise. * doc/c-tree.texi: Document COMPOUND_LITERAL_EXPR. * doc/extend.texi: Update documentation of compound literals. Fixes PR c/4787. testsuite: * gcc.c-torture/execute/20000722-1.x, gcc.c-torture/execute/20010123-1.x: Remove. * gcc.c-torture/compile/init-3.c: Don't use a compound literal. * gcc.dg/c90-complit-1.c, gcc.dg/c99-complit-1.c, gcc.dg/c99-complit-2.c: New tests. From-SVN: r47629
113 lines
2.8 KiB
C
113 lines
2.8 KiB
C
/* Test for compound literals: in C99 only. Test for valid uses. */
|
|
/* Origin: Joseph Myers <jsm28@cam.ac.uk> */
|
|
/* { dg-do run } */
|
|
/* { dg-options "-std=iso9899:1999 -pedantic-errors" } */
|
|
|
|
extern void abort (void);
|
|
extern void exit (int);
|
|
|
|
struct s { int a; int b; };
|
|
union u { int c; int d; };
|
|
|
|
int *i0a = &(int) { 0 };
|
|
int *i0b = &(int) { 0 };
|
|
int *i1a = &(int) { 1 };
|
|
int *i1b = &(int) { 1 };
|
|
const int *i0c = &(const int) { 0 };
|
|
|
|
struct s *s0 = &(struct s) { 1, 2 };
|
|
struct s *s1 = &(struct s) { 1, 2 };
|
|
const struct s *s2 = &(const struct s) { 1, 2 };
|
|
|
|
union u *u0 = &(union u) { 3 };
|
|
union u *u1 = &(union u) { 3 };
|
|
const union u *u2 = &(const union u) { 3 };
|
|
|
|
int *a0 = (int []) { 1, 2, 3 };
|
|
const int *a1 = (const int []) { 1, 2, 3 };
|
|
|
|
char *p = (char []){ "foo" };
|
|
|
|
int
|
|
main (void)
|
|
{
|
|
if (i0a == i0b || i0a == i0c || i0b == i0c)
|
|
abort ();
|
|
if (i1a == i1b)
|
|
abort ();
|
|
if (*i0a != 0 || *i0b != 0 || *i1a != 1 || *i1b != 1 || *i0c != 0)
|
|
abort ();
|
|
*i0a = 1;
|
|
*i1a = 0;
|
|
if (*i0a != 1 || *i0b != 0 || *i1a != 0 || *i1b != 1 || *i0c != 0)
|
|
abort ();
|
|
if (s0 == s1 || s1 == s2 || s2 == s0)
|
|
abort ();
|
|
if (s0->a != 1 || s0->b != 2 || s1->a != 1 || s1->b != 2
|
|
|| s2->a != 1 || s2->b != 2)
|
|
abort ();
|
|
s0->a = 2;
|
|
s1->b = 1;
|
|
if (s0->a != 2 || s0->b != 2 || s1->a != 1 || s1->b != 1
|
|
|| s2->a != 1 || s2->b != 2)
|
|
abort ();
|
|
if (u0 == u1 || u1 == u2 || u2 == u0)
|
|
abort ();
|
|
if (u0->c != 3 || u1->c != 3 || u2->c != 3)
|
|
abort ();
|
|
u0->d = 2;
|
|
if (u0->d != 2 || u1->c != 3 || u2->c != 3)
|
|
abort ();
|
|
if (a0 == a1)
|
|
abort ();
|
|
if (a0[0] != 1 || a0[1] != 2 || a0[2] != 3
|
|
|| a1[0] != 1 || a1[1] != 2 || a1[2] != 3)
|
|
abort ();
|
|
a0[0] = 3;
|
|
if (a0[0] != 3 || a0[1] != 2 || a0[2] != 3
|
|
|| a1[0] != 1 || a1[1] != 2 || a1[2] != 3)
|
|
abort ();
|
|
if (p[0] != 'f' || p[1] != 'o' || p[2] != 'o' || p[3] != 0)
|
|
abort ();
|
|
p[0] = 'g';
|
|
if (p[0] != 'g' || p[1] != 'o' || p[2] != 'o' || p[3] != 0)
|
|
abort ();
|
|
if (sizeof((int []) { 1, 2 ,3 }) != 3 * sizeof(int))
|
|
abort ();
|
|
if (sizeof((int []) { [3] = 4 }) != 4 * sizeof(int))
|
|
abort ();
|
|
struct s *y;
|
|
for (int i = 0; i < 3; i++) {
|
|
struct s *x = &(struct s) { 1, i };
|
|
if (x->a != 1 || x->b != i)
|
|
abort ();
|
|
x->a++;
|
|
x->b--;
|
|
if (x->a != 2 || x->b != i - 1)
|
|
abort ();
|
|
if (i && y != x)
|
|
abort ();
|
|
y = x;
|
|
}
|
|
int *z;
|
|
for (int i = 0; i < 4; i++) {
|
|
int *x = (int []){ 0, i, i + 2, i - 3 };
|
|
if (x[0] != 0 || x[1] != i || x[2] != i + 2 || x[3] != i - 3)
|
|
abort ();
|
|
x[0] = x[1];
|
|
x[1] *= x[2];
|
|
x[2] -= x[3];
|
|
x[3] += 7;
|
|
if (x[0] != i || x[1] != i * (i + 2) || x[2] != 5 || x[3] != i + 4)
|
|
abort ();
|
|
if (i && z != x)
|
|
abort ();
|
|
z = x;
|
|
}
|
|
(int) { 0 } = 1;
|
|
(struct s) { 0, 1 }.a = 3;
|
|
(union u) { 3 }.c = 4;
|
|
(int []){ 1, 2 }[0] = 0;
|
|
exit (0);
|
|
}
|