Commit 6895bac8 by Joseph Myers Committed by Joseph Myers

c-decl.c (build_enumerator): Allow values folding to integer constants but not…

c-decl.c (build_enumerator): Allow values folding to integer constants but not integer constant expressions...

	* c-decl.c (build_enumerator): Allow values folding to integer
	constants but not integer constant expressions with a pedwarn if
	pedantic.

testsuite:
	* gcc.dg/enum-const-1.c, gcc.dg/enum-const-2.c,
	gcc.dg/enum-const-3.c: New tests.
	* gcc.dg/gnu89-const-expr-1.c, gcc.dg/gnu99-const-expr-1.c: Use
	-pedantic-errors.  Update expected diagnostics.

From-SVN: r146789
parent 24070fcb
2009-04-25 Joseph Myers <joseph@codesourcery.com> 2009-04-25 Joseph Myers <joseph@codesourcery.com>
* c-decl.c (build_enumerator): Allow values folding to integer
constants but not integer constant expressions with a pedwarn if
pedantic.
2009-04-25 Joseph Myers <joseph@codesourcery.com>
PR c/39582 PR c/39582
* c-typeck.c (c_expr_sizeof_type): Create a C_MAYBE_CONST_EXPR * c-typeck.c (c_expr_sizeof_type): Create a C_MAYBE_CONST_EXPR
with non-null C_MAYBE_CONST_EXPR_PRE if size of a variable-length with non-null C_MAYBE_CONST_EXPR_PRE if size of a variable-length
......
...@@ -6077,16 +6077,32 @@ build_enumerator (struct c_enum_contents *the_enum, tree name, tree value, ...@@ -6077,16 +6077,32 @@ build_enumerator (struct c_enum_contents *the_enum, tree name, tree value,
undeclared identifier) - just ignore the value expression. */ undeclared identifier) - just ignore the value expression. */
if (value == error_mark_node) if (value == error_mark_node)
value = 0; value = 0;
else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)) else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
|| TREE_CODE (value) != INTEGER_CST)
{ {
error ("enumerator value for %qE is not an integer constant", name); error ("enumerator value for %qE is not an integer constant", name);
value = 0; value = 0;
} }
else else
{ {
value = default_conversion (value); if (TREE_CODE (value) != INTEGER_CST)
constant_expression_warning (value); {
value = c_fully_fold (value, false, NULL);
if (TREE_CODE (value) == INTEGER_CST)
pedwarn (value_loc, OPT_pedantic,
"enumerator value for %qE is not an integer "
"constant expression", name);
}
if (TREE_CODE (value) != INTEGER_CST)
{
error ("enumerator value for %qE is not an integer constant",
name);
value = 0;
}
else
{
value = default_conversion (value);
constant_expression_warning (value);
}
} }
} }
......
2009-04-25 Joseph Myers <joseph@codesourcery.com> 2009-04-25 Joseph Myers <joseph@codesourcery.com>
* gcc.dg/enum-const-1.c, gcc.dg/enum-const-2.c,
gcc.dg/enum-const-3.c: New tests.
* gcc.dg/gnu89-const-expr-1.c, gcc.dg/gnu99-const-expr-1.c: Use
-pedantic-errors. Update expected diagnostics.
2009-04-25 Joseph Myers <joseph@codesourcery.com>
PR c/39582 PR c/39582
* gcc.dg/vla-20.c: New test. * gcc.dg/vla-20.c: New test.
......
/* Test for enumeration constants not integer constant expressions but
folding to integer constants (used in Linux kernel,
<http://gcc.gnu.org/ml/gcc/2009-04/msg00677.html>). */
/* { dg-do compile } */
/* { dg-options "" } */
extern int i;
enum e { E = (1 ? 1 : i) };
/* Test for enumeration constants not integer constant expressions but
folding to integer constants (used in Linux kernel,
<http://gcc.gnu.org/ml/gcc/2009-04/msg00677.html>). */
/* { dg-do compile } */
/* { dg-options "-pedantic" } */
extern int i;
enum e { E = (1 ? 1 : i) }; /* { dg-warning "not an integer constant expression" } */
/* Test for enumeration constants not integer constant expressions but
folding to integer constants (used in Linux kernel,
<http://gcc.gnu.org/ml/gcc/2009-04/msg00677.html>). */
/* { dg-do compile } */
/* { dg-options "-pedantic-errors" } */
extern int i;
enum e { E = (1 ? 1 : i) }; /* { dg-error "not an integer constant expression" } */
/* Test for constant expressions: GNU extensions. */ /* Test for constant expressions: GNU extensions. */
/* Origin: Joseph Myers <joseph@codesourcery.com> */ /* Origin: Joseph Myers <joseph@codesourcery.com> */
/* { dg-do compile } */ /* { dg-do compile } */
/* { dg-options "-std=gnu89" } */ /* { dg-options "-std=gnu89 -pedantic-errors" } */
int n; int n;
...@@ -9,39 +9,48 @@ void ...@@ -9,39 +9,48 @@ void
f (void) f (void)
{ {
int i = 0; int i = 0;
int a[n]; int a[n]; /* { dg-error "ISO C90 forbids variable length array" } */
enum e1 { enum e1 {
/* Integer constant expressions may not contain statement /* Integer constant expressions may not contain statement
expressions (not a permitted operand). */ expressions (not a permitted operand). */
E1 = (1 ? 0 : ({ 0; })), /* { dg-error "constant" } */ E1 = (1 ? 0 : ({ 0; })), /* { dg-error "constant expression" } */
/* { dg-error "ISO C forbids braced-groups" "ISO" { target *-*-* } 16 } */
/* Real and imaginary parts act like other arithmetic /* Real and imaginary parts act like other arithmetic
operators. */ operators. */
E2 = __real__ (1 ? 0 : i++), /* { dg-error "constant" } */ E2 = __real__ (1 ? 0 : i++), /* { dg-error "constant expression" } */
E3 = __real__ 0, E3 = __real__ 0,
E4 = __imag__ (1 ? 0 : i++), /* { dg-error "constant" } */ E4 = __imag__ (1 ? 0 : i++), /* { dg-error "constant" } */
E5 = __imag__ 0, E5 = __imag__ 0,
/* __alignof__ always constant. */ /* __alignof__ always constant. */
E6 = __alignof__ (int[n]), E6 = __alignof__ (int[n]), /* { dg-error "ISO C90 forbids variable length array" } */
E7 = __alignof__ (a), E7 = __alignof__ (a),
/* __extension__ ignored for constant expression purposes. */ /* __extension__ ignored for constant expression purposes. */
E8 = __extension__ (1 ? 0 : i++), /* { dg-error "constant" } */ E8 = __extension__ (1 ? 0 : i++), /* { dg-error "constant expression" } */
E9 = __extension__ 0, E9 = __extension__ 0,
/* Conditional expressions with omitted arguments act like the /* Conditional expressions with omitted arguments act like the
standard type. */ standard type. */
E10 = (1 ? : i++), /* { dg-error "constant" } */ E10 = (1 ? : i++), /* { dg-error "constant expression" } */
E11 = (1 ? : 0) /* { dg-error "ISO C forbids omitting" "ISO" { target *-*-* } 32 } */
E11 = (1 ? : 0) /* { dg-error "ISO C forbids omitting" } */
}; };
enum e2 { enum e2 {
/* Complex integer constants may be cast directly to integer /* Complex integer constants may be cast directly to integer
types, but not after further arithmetic on them. */ types, but not after further arithmetic on them. */
F1 = (int) (_Complex int) 2i, /* { dg-error "constant" } */ F1 = (int) (_Complex int) 2i, /* { dg-error "constant expression" } */
F2 = (int) +2i, /* { dg-error "constant" } */ /* { dg-error "complex" "complex" { target *-*-* } 39 } */
F3 = (int) (1 + 2i), /* { dg-error "constant" } */ /* { dg-error "imaginary" "imaginary" { target *-*-* } 39 } */
F4 = (int) 2i F2 = (int) +2i, /* { dg-error "constant expression" } */
/* { dg-error "imaginary" "ISO" { target *-*-* } 42 } */
F3 = (int) (1 + 2i), /* { dg-error "constant expression" } */
/* { dg-error "imaginary" "ISO" { target *-*-* } 44 } */
F4 = (int) 2i /* { dg-error "imaginary" } */
}; };
static double dr = __real__ (1.0 + 2.0i); static double dr = __real__ (1.0 + 2.0i);
/* { dg-error "imaginary" "ISO" { target *-*-* } 48 } */
static double di = __imag__ (1.0 + 2.0i); static double di = __imag__ (1.0 + 2.0i);
/* { dg-error "imaginary" "ISO" { target *-*-* } 50 } */
/* Statement expressions allowed in unevaluated subexpressions in /* Statement expressions allowed in unevaluated subexpressions in
initializers in gnu99 but not gnu89. */ initializers in gnu99 but not gnu89. */
static int j = (1 ? 0 : ({ 0; })); /* { dg-warning "constant expression" } */ static int j = (1 ? 0 : ({ 0; })); /* { dg-error "constant expression" } */
/* { dg-error "braced" "ISO" { target *-*-* } 54 } */
} }
/* Test for constant expressions: GNU extensions. */ /* Test for constant expressions: GNU extensions. */
/* Origin: Joseph Myers <joseph@codesourcery.com> */ /* Origin: Joseph Myers <joseph@codesourcery.com> */
/* { dg-do compile } */ /* { dg-do compile } */
/* { dg-options "-std=gnu99" } */ /* { dg-options "-std=gnu99 -pedantic-errors" } */
int n; int n;
...@@ -13,10 +13,11 @@ f (void) ...@@ -13,10 +13,11 @@ f (void)
enum e1 { enum e1 {
/* Integer constant expressions may not contain statement /* Integer constant expressions may not contain statement
expressions (not a permitted operand). */ expressions (not a permitted operand). */
E1 = (1 ? 0 : ({ 0; })), /* { dg-error "constant" } */ E1 = (1 ? 0 : ({ 0; })), /* { dg-error "constant expression" } */
/* { dg-error "ISO C forbids braced-groups" "ISO" { target *-*-* } 16 } */
/* Real and imaginary parts act like other arithmetic /* Real and imaginary parts act like other arithmetic
operators. */ operators. */
E2 = __real__ (1 ? 0 : i++), /* { dg-error "constant" } */ E2 = __real__ (1 ? 0 : i++), /* { dg-error "constant expression" } */
E3 = __real__ 0, E3 = __real__ 0,
E4 = __imag__ (1 ? 0 : i++), /* { dg-error "constant" } */ E4 = __imag__ (1 ? 0 : i++), /* { dg-error "constant" } */
E5 = __imag__ 0, E5 = __imag__ 0,
...@@ -24,24 +25,32 @@ f (void) ...@@ -24,24 +25,32 @@ f (void)
E6 = __alignof__ (int[n]), E6 = __alignof__ (int[n]),
E7 = __alignof__ (a), E7 = __alignof__ (a),
/* __extension__ ignored for constant expression purposes. */ /* __extension__ ignored for constant expression purposes. */
E8 = __extension__ (1 ? 0 : i++), /* { dg-error "constant" } */ E8 = __extension__ (1 ? 0 : i++), /* { dg-error "constant expression" } */
E9 = __extension__ 0, E9 = __extension__ 0,
/* Conditional expressions with omitted arguments act like the /* Conditional expressions with omitted arguments act like the
standard type. */ standard type. */
E10 = (1 ? : i++), /* { dg-error "constant" } */ E10 = (1 ? : i++), /* { dg-error "constant expression" } */
E11 = (1 ? : 0) /* { dg-error "ISO C forbids omitting" "ISO" { target *-*-* } 32 } */
E11 = (1 ? : 0) /* { dg-error "ISO C forbids omitting" } */
}; };
enum e2 { enum e2 {
/* Complex integer constants may be cast directly to integer /* Complex integer constants may be cast directly to integer
types, but not after further arithmetic on them. */ types, but not after further arithmetic on them. */
F1 = (int) (_Complex int) 2i, /* { dg-error "constant" } */ F1 = (int) (_Complex int) 2i, /* { dg-error "constant expression" } */
F2 = (int) +2i, /* { dg-error "constant" } */ /* { dg-error "complex" "complex" { target *-*-* } 39 } */
F3 = (int) (1 + 2i), /* { dg-error "constant" } */ /* { dg-error "imaginary" "imaginary" { target *-*-* } 39 } */
F4 = (int) 2i F2 = (int) +2i, /* { dg-error "constant expression" } */
/* { dg-error "imaginary" "ISO" { target *-*-* } 42 } */
F3 = (int) (1 + 2i), /* { dg-error "constant expression" } */
/* { dg-error "imaginary" "ISO" { target *-*-* } 44 } */
F4 = (int) 2i /* { dg-error "imaginary" } */
}; };
static double dr = __real__ (1.0 + 2.0i); static double dr = __real__ (1.0 + 2.0i);
/* { dg-error "imaginary" "ISO" { target *-*-* } 48 } */
static double di = __imag__ (1.0 + 2.0i); static double di = __imag__ (1.0 + 2.0i);
/* { dg-error "imaginary" "ISO" { target *-*-* } 50 } */
/* Statement expressions allowed in unevaluated subexpressions in /* Statement expressions allowed in unevaluated subexpressions in
initializers in gnu99 but not gnu89. */ initializers in gnu99 but not gnu89. */
static int j = (1 ? 0 : ({ 0; })); static int j = (1 ? 0 : ({ 0; }));
/* { dg-error "braced" "ISO" { target *-*-* } 54 } */
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment