This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Patch for constant expression constraints in preprocessor


Although constant expression constraints in the compiler are extremely
complicated, those in the preprocessor are simple (largely because of
the lack of casts, sizeof and floating point constants).  (The
_semantics_ are also simple once the correction of DR#265 is applied.)
I think the following patch is all that is needed to follow the
constraints of the various standard versions: unlike C99, C90 and C++
do not permit comma expressions in constant expressions at all outside
of sizeof, even in other unevaluated subexpressions.  (Note that this
patch only addresses a small part of bug 456.)

Boostrapped with no regressions on i686-pc-linux-gnu.  OK to commit to
mainline and 3.4 branch (this is a regression in C90 mode relative to
3.2.3, 3.0.4 and 2.95.3)?

2004-02-11  Joseph S. Myers  <jsm@polyomino.org.uk>

	PR c/456
	* cppexp.c (num_binary_op): Don't allow comma operators in #if
	constant expressions at all outside C99 mode if pedantic.

testsuite:
2004-02-11  Joseph S. Myers  <jsm@polyomino.org.uk>

	PR c/456
	* gcc.dg/cpp/c90-if-comma-1.c, gcc.dg/c99-if-comma-1.c: New tests.

--- GCC/gcc/cppexp.c.orig	2003-11-02 10:06:48.000000000 +0000
+++ GCC/gcc/cppexp.c	2004-02-10 19:48:49.000000000 +0000
@@ -1347,7 +1347,8 @@ num_binary_op (cpp_reader *pfile, cpp_nu
 
       /* Comma.  */
     default: /* case CPP_COMMA: */
-      if (CPP_PEDANTIC (pfile) && !pfile->state.skip_eval)
+      if (CPP_PEDANTIC (pfile) && (!CPP_OPTION (pfile, c99)
+				   || !pfile->state.skip_eval))
 	cpp_error (pfile, CPP_DL_PEDWARN,
 		   "comma operator in operand of #if");
       lhs = rhs;
--- GCC/gcc/testsuite/gcc.dg/cpp/c90-if-comma-1.c	2002-08-26 16:21:36.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/cpp/c90-if-comma-1.c	2004-02-10 19:53:04.000000000 +0000
@@ -0,0 +1,11 @@
+/* Test for commas in constant expressions in #if: not permitted in C90
+   but permitted in unevaluated subexpressions in C99.  */
+/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
+/* { dg-do preprocess } */
+/* { dg-options "-std=iso9899:1990 -pedantic-errors" } */
+
+#if (1, 2) /* { dg-error "comma" "evaluated comma" } */
+#endif
+
+#if 1 || (1, 2) /* { dg-error "comma" "unevaluated comma" } */
+#endif
--- GCC/gcc/testsuite/gcc.dg/cpp/c99-if-comma-1.c	2002-08-26 16:21:36.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/cpp/c99-if-comma-1.c	2004-02-10 19:53:26.000000000 +0000
@@ -0,0 +1,11 @@
+/* Test for commas in constant expressions in #if: not permitted in C90
+   but permitted in unevaluated subexpressions in C99.  */
+/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
+/* { dg-do preprocess } */
+/* { dg-options "-std=iso9899:1999 -pedantic-errors" } */
+
+#if (1, 2) /* { dg-error "comma" "evaluated comma" } */
+#endif
+
+#if 1 || (1, 2)
+#endif

-- 
Joseph S. Myers
jsm@polyomino.org.uk


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]