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  <meiss...@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  <meiss...@linux.vnet.ibm.com>

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

-- 
Michael Meissner, IBM
5 Technology Place Drive, M/S 2757, Westford, MA 01886-3141, USA
meiss...@linux.vnet.ibm.com     fax +1 (978) 399-6899
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;

Reply via email to