diff mbox

[4.7] PR 48192, Make conditional macros not defined for #ifdef

Message ID 20110318232657.GA11627@hungry-tiger.westford.ibm.com
State New
Headers show

Commit Message

Michael Meissner March 18, 2011, 11:26 p.m. UTC
This patch makes conditional macros that are used in the PowerPC and SPU ports
not be 'defined' for the #ifdef, #ifndef, and #if defined(...) tests.  The way
the PowerPC and SPU uses conditional macros is provide conditional keywords to
support the vector systen (i.e. if the next token after vector is not bool,
pixel, or a standard type, the vector macro is not expanded).

Some users have run into this when compiling for Power7, which enables the
Altivec support by default in code fragments like:

#ifndef bool
#define bool char
#endif

On the powerpc if you compile with -mcpu=power7, the #ifndef test does not
succeed, because 'bool' is defined as a macro, it never defines bool to be char
and a syntax error will come up.

I bootstraped 4.7 and tested that the 3 PowerPC conditional macros are not
defined.  Is this ok to install in 4.7?

I would like to backport this to 4.6, 4.5 and maybe 4.4.  Are there objections
to backporting it?

[libcpp]
2011-03-18  Michael Meissner  <meissner@linux.vnet.ibm.com>

	PR preprocessor/48192
	* directives.c (do_ifdef): Do not consider conditional macros as
	being defined.
	(do_ifndef): Ditto.
	* expr.c (parse_defined): Ditto.

[gcc/testsuite]
2011-03-18  Michael Meissner  <meissner@linux.vnet.ibm.com>

	PR preprocessor/48192
	* gcc.target/powerpc/pr48192.c: New file.

Comments

Mark Mitchell March 20, 2011, 2:56 p.m. UTC | #1
On 3/18/2011 4:26 PM, Michael Meissner wrote:

> This patch makes conditional macros that are used in the PowerPC and SPU ports
> not be 'defined' for the #ifdef, #ifndef, and #if defined(...) tests. 

I think that's a desirable change, from a language semantics point of
view, but I would defer to Joseph on this issue; he has a much more
comprehensive understanding of the C preprocessing rules than I do.

> I would like to backport this to 4.6, 4.5 and maybe 4.4.  Are there objections
> to backporting it?

I have no objection to a backport.

Thank you,
Joseph Myers March 20, 2011, 11:36 p.m. UTC | #2
On Sun, 20 Mar 2011, Mark Mitchell wrote:

> On 3/18/2011 4:26 PM, Michael Meissner wrote:
> 
> > This patch makes conditional macros that are used in the PowerPC and SPU ports
> > not be 'defined' for the #ifdef, #ifndef, and #if defined(...) tests. 
> 
> I think that's a desirable change, from a language semantics point of
> view, but I would defer to Joseph on this issue; he has a much more
> comprehensive understanding of the C preprocessing rules than I do.

The argument for the change makes sense to me (in terms of what code is 
likely to do when testing for a macro being defined); this is outside the 
scope of the standard in any case.
diff mbox

Patch

Index: libcpp/directives.c
===================================================================
--- libcpp/directives.c	(revision 171162)
+++ libcpp/directives.c	(working copy)
@@ -1819,7 +1819,12 @@  do_ifdef (cpp_reader *pfile)
 
       if (node)
 	{
-	  skip = node->type != NT_MACRO;
+	  /* Do not treat conditional macros as being defined.  This is due to
+	     the powerpc and spu ports using conditional macros for 'vector',
+	     'bool', and 'pixel' to act as conditional keywords.  This messes
+	     up tests like #ifndef bool.  */
+	  skip = (node->type != NT_MACRO
+		  || ((node->flags & NODE_CONDITIONAL) != 0));
 	  _cpp_mark_macro_used (node);
 	  if (!(node->flags & NODE_USED))
 	    {
@@ -1860,7 +1865,12 @@  do_ifndef (cpp_reader *pfile)
 
       if (node)
 	{
-	  skip = node->type == NT_MACRO;
+	  /* Do not treat conditional macros as being defined.  This is due to
+	     the powerpc and spu ports using conditional macros for 'vector',
+	     'bool', and 'pixel' to act as conditional keywords.  This messes
+	     up tests like #ifndef bool.  */
+	  skip = (node->type == NT_MACRO
+		  && ((node->flags & NODE_CONDITIONAL) == 0));
 	  _cpp_mark_macro_used (node);
 	  if (!(node->flags & NODE_USED))
 	    {
Index: libcpp/expr.c
===================================================================
--- libcpp/expr.c	(revision 171162)
+++ libcpp/expr.c	(working copy)
@@ -720,10 +720,15 @@  parse_defined (cpp_reader *pfile)
 
   pfile->state.prevent_expansion--;
 
+  /* Do not treat conditional macros as being defined.  This is due to the
+     powerpc and spu ports using conditional macros for 'vector', 'bool', and
+     'pixel' to act as conditional keywords.  This messes up tests like #ifndef
+     bool.  */
   result.unsignedp = false;
   result.high = 0;
   result.overflow = false;
-  result.low = node && node->type == NT_MACRO;
+  result.low = (node && node->type == NT_MACRO
+		&& (node->flags & NODE_CONDITIONAL) == 0);
   return result;
 }
 
Index: gcc/testsuite/gcc.target/powerpc/pr48192.c
===================================================================
--- gcc/testsuite/gcc.target/powerpc/pr48182.c	(revision 0)
+++ gcc/testsuite/gcc.target/powerpc/pr48182.c	(revision 0)
@@ -0,0 +1,49 @@ 
+/* { dg-do compile } */
+/* { dg-skip-if "" { powerpc*-*-darwin* } { "*" } { "" } } */
+/* { dg-require-effective-target powerpc_vsx_ok } */
+/* { dg-options "-O3 -mcpu=power7 -std=gnu89" } */
+
+/* Make sure that the conditional macros vector, bool, and pixel are not
+   considered as being defined.  */
+
+#ifdef bool
+#error "bool is considered defined"
+#endif
+
+#ifdef vector
+#error "vector is considered defined"
+#endif
+
+#ifdef pixel
+#error "pixel is condsidered defined"
+#endif
+
+#if defined(bool)
+#error "bool is considered defined"
+#endif
+
+#if defined(vector)
+#error "vector is considered defined"
+#endif
+
+#if defined(pixel)
+#error "pixel is condsidered defined"
+#endif
+
+#ifndef bool
+#else
+#error "bool is considered defined"
+#endif
+
+#ifndef vector
+#else
+#error "vector is considered defined"
+#endif
+
+#ifndef pixel
+#else
+#error "pixel is condsidered defined"
+#endif
+
+#define bool long double
+bool pixel = 0;