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]

fix padding bug with computed #include


Neil found an obscure bug where stringification produced a different
string in the context of a computed #include than in running text.  He
asked me to fix it since he doesn't have copyright papers yet.  This
is a minimally-invasive fix -- the long term "right" change is to get
rid of padding tokens altogether, but I wanted something safe for the
release branch.

Bootstrapped i686-linux; applied mainline and 3.3.

zw

        * cpphash.h (struct lexer_state): Add directive_wants_padding.
        * cpplib.c (_cpp_handle_directive): Set directive_wants_padding
        for directives of type INCL.
        (glue_header_name, parse_include): Use get_token_no_padding.
        * cppmacro.c (replace_args): If directive_wants_padding,
        provide padding tokens.

        * testsuite/gcc.dg/cpp/include3.c: New test.
        * testsuite/gcc.dg/cpp/inc/foo.h: New file.

===================================================================
Index: cpphash.h
--- cpphash.h	21 Jan 2003 00:12:52 -0000	1.175
+++ cpphash.h	21 Feb 2003 17:59:46 -0000
@@ -214,6 +214,11 @@ struct lexer_state
   /* Nonzero if first token on line is CPP_HASH.  */
   unsigned char in_directive;
 
+  /* Nonzero if in a directive that will handle padding tokens itself.
+     #include needs this to avoid problems with computed include and
+     spacing between tokens.  */
+  unsigned char directive_wants_padding;
+
   /* True if we are skipping a failed conditional group.  */
   unsigned char skipping;
 
===================================================================
Index: cpplib.c
--- cpplib.c	12 Feb 2003 17:01:53 -0000	1.330
+++ cpplib.c	21 Feb 2003 17:59:47 -0000
@@ -414,6 +414,7 @@ _cpp_handle_directive (pfile, indented)
 	     skipping or not, we should lex angle-bracketed headers
 	     correctly, and maybe output some diagnostics.  */
 	  pfile->state.angled_headers = dir->flags & INCL;
+	  pfile->state.directive_wants_padding = dir->flags & INCL;
 	  if (! CPP_OPTION (pfile, preprocessed))
 	    directive_diagnostics (pfile, dir, indented);
 	  if (pfile->state.skipping && !(dir->flags & COND))
@@ -582,7 +583,7 @@ glue_header_name (pfile)
   buffer = (unsigned char *) xmalloc (capacity);
   for (;;)
     {
-      token = cpp_get_token (pfile);
+      token = get_token_no_padding (pfile);
 
       if (token->type == CPP_GREATER || token->type == CPP_EOF)
 	break;
@@ -634,7 +635,7 @@ parse_include (pfile)
     dir = pfile->directive->name;
 
   /* Allow macro expansion.  */
-  header = cpp_get_token (pfile);
+  header = get_token_no_padding (pfile);
   if (header->type != CPP_STRING && header->type != CPP_HEADER_NAME)
     {
       if (header->type != CPP_LESS)
===================================================================
Index: cppmacro.c
--- cppmacro.c	16 Dec 2002 18:19:18 -0000	1.127
+++ cppmacro.c	21 Feb 2003 17:59:47 -0000
@@ -899,7 +899,7 @@ replace_args (pfile, node, macro, args)
 	count = arg->expanded_count, from = arg->expanded;
 
       /* Padding on the left of an argument (unless RHS of ##).  */
-      if (!pfile->state.in_directive
+      if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
 	  && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
 	*dest++ = padding_token (pfile, src);
 
===================================================================
Index: testsuite/gcc.dg/cpp/include3.c
--- testsuite/gcc.dg/cpp/include3.c	1 Jan 1970 00:00:00 -0000
+++ testsuite/gcc.dg/cpp/include3.c	21 Feb 2003 17:59:50 -0000
@@ -0,0 +1,7 @@
+/* Unpatched, this file would include "inc/ foo.h" (note the space)  */
+
+#define PREINC_XSTR(str)             #str
+#define PREINC_STR(str)              PREINC_XSTR(str)
+#define COMP_INC(comp,file)          PREINC_STR(comp/file)
+
+#include COMP_INC(inc, foo.h)
===================================================================
Index: testsuite/gcc.dg/cpp/inc/foo.h
--- testsuite/gcc.dg/cpp/inc/foo.h	1 Jan 1970 00:00:00 -0000
+++ testsuite/gcc.dg/cpp/inc/foo.h	21 Feb 2003 17:59:50 -0000
@@ -0,0 +1 @@
+extern int a;


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