[4.5 C] More __builtin_constant_p tests

Joseph S. Myers joseph@codesourcery.com
Wed Dec 10 13:56:00 GMT 2008


Following the discussion in PR 38377, I've added these further tests to 
c-4_5-branch of the rules for __builtin_constant_p calls as the condition 
in conditional expressions.

Index: ChangeLog.c45
===================================================================
--- ChangeLog.c45	(revision 142591)
+++ ChangeLog.c45	(working copy)
@@ -1,3 +1,7 @@
+2008-12-10  Joseph Myers  <joseph@codesourcery.com>
+
+	* gcc.dg/bconstp-3.c, gcc.dg/bconstp-4.c: New tests.
+
 2008-11-09  Joseph Myers  <joseph@codesourcery.com>
 
 	* gcc.c-torture/compile/20081108-1.c,
Index: gcc.dg/bconstp-3.c
===================================================================
--- gcc.dg/bconstp-3.c	(revision 0)
+++ gcc.dg/bconstp-3.c	(revision 0)
@@ -0,0 +1,27 @@
+/* As bconstp-1.c, but with the __builtin_constant_p calls inside
+   __builtin_choose_expr.  */
+/* { dg-do compile } */
+
+/* This test checks that builtin_constant_p can be used safely in
+   initializers for static data.  The macro X() defined below should
+   be an acceptable initializer expression no matter how complex its
+   argument is.  */
+
+extern int a;
+extern int b;
+
+extern int foo(void);
+extern int bar(void);
+
+#define X(exp) (__builtin_choose_expr(1, __builtin_constant_p(exp), 1) ? (exp) : -1)
+
+const short tests[] = {
+  X(0),
+  X(a),
+  X(0 && a),
+  X(a && b),
+  X(foo()),
+  X(0 && foo()),
+  X(a && foo()),
+  X(foo() && bar())
+};
Index: gcc.dg/bconstp-4.c
===================================================================
--- gcc.dg/bconstp-4.c	(revision 0)
+++ gcc.dg/bconstp-4.c	(revision 0)
@@ -0,0 +1,10 @@
+/* Test that a condition is not counted as a call to
+   __builtin_constant_p if that call is itself inside a conditional
+   expression with __builtin_constant_p condition.  */
+/* { dg-do compile } */
+
+extern int foo(void);
+
+#define X(exp) (__builtin_choose_expr(1, __builtin_constant_p(exp), 1) ? (exp) : -1)
+
+const int x = ((__builtin_constant_p(1) ? __builtin_constant_p (0 && foo()) : 0) ? (0 && foo()) : -1); /* { dg-error "initializer element is not a constant expression" } */

-- 
Joseph S. Myers
joseph@codesourcery.com



More information about the Gcc-patches mailing list