GCC 3.4.3 Preprocessor -traditional bug + patch

David Flynn davidf@woaf.net
Sun Jan 23 17:33:00 GMT 2005


Take the following three lines of code.  Please note it is haskell code
not C however that is irrelevant since this is a preprocessor issue:

$ cat nl-escape.hs
-- stuff
infix 5 \\ 
-- more stuff
$

Please note, there is a single space after the double backslash.


using gcc-3.4.3:

$ gcc-3.4.3 -v
Reading specs from /tool/gcc-3.4.3/i486-Linux/bin/../lib/gcc/i486-linux/3.4.3/specs
Configured with: ../gcc-3.4.3/configure --host=i486-linux --prefix=/tool/gcc-3.4.3 --exec-prefix=/tool/gcc-3.4.3/i486-Linux/
Thread model: posix
gcc version 3.4.3

$ gcc-3.4.3 -P -E -traditional-cpp -xc nl-escape.hs
nl-escape.hs:2: warning: backslash and newline separated by space
-- stuff
infix 5 \-- more stuff

$ gcc-3.4.3 -P -E -xc nl-escape.hs    
-- stuff
nl-escape.hs:2:10: warning: backslash and newline separated by space
infix 5 \-- more stuff



using gcc-3.3.4:

$ gcc-3.3.4 -v
Reading specs from /usr/lib/gcc-lib/i486-slackware-linux/3.3.4/specs
Configured with: ../gcc-3.3.4/configure --prefix=/usr --enable-shared --enable-threads=posix --enable-__cxa_atexit --disable-checking --with-gnu-ld --verbose --target=i486-slackware-linux --host=i486-slackware-linux
Thread model: posix
gcc version 3.3.4


$ gcc-3.3.4 -P -E -traditional-cpp -xc nl-escape.hs
-- stuff
infix 5 \\ 
-- more stuff

$ gcc-3.3.4 -P -E -xc nl-escape.hs 
-- stuff
nl-escape.hs:2:10: warning: backslash and newline separated by space
infix 5 \-- more stuff



While i don't agree at all with this idea of fudging
`backslash, spaces, newline', into a line continuation escape, since it
breaks the law of least supprise. ie `backslash, newline' is the way it
is done everywehere else i have *ever* seen AND the way you stop that
escape from happening is to insert whitespace between the backslash and
newline.

I do agree that the warning issued is useful - it allows people to spot
the fact that they may have entered a space after a backslash when they
did not intend it, however fudging it so that you are trying to work out
what they meant to write is wrong.

(As an aside, I do agree that white space is not the most sensbile thing
to use to stop `escape' a line continuation escape.  A single comment is
good.  However it does fly in the face of may years of doing the probably
wrong thing -- especially when many other systems use the C preprocessor
to preprocess random files.)

The -traditional-cpp argument used to inhibit this works in 3.3.4 but not
3.4.3.

Here is a patch that appears to correct the use of -traditional-cpp:


--- gcc-3.4.3/gcc/cpplex.c      2004-02-18 22:10:13.000000000 +0000
+++ gcc-3.4.3-df/gcc/cpplex.c   2005-01-23 15:18:19.000000000 +0000
@@ -136,7 +136,8 @@
 
              /* check for escaped newline */
              p = d;
-             while (p != buffer->next_line && is_nvspace (p[-1]))
+             while (p != buffer->next_line
+                    && !CPP_OPTION(pfile, traditional) && is_nvspace (p[-1]))
                p--;
              if (p == buffer->next_line || p[-1] != '\\')
                goto done;
@@ -181,7 +182,8 @@
 
              /* Escaped?  */
              p = d;
-             while (p != buffer->next_line && is_nvspace (p[-1]))
+             while (p != buffer->next_line
+                    && !CPP_OPTION(pfile, traditional) && is_nvspace (p[-1]))
                p--;
              if (p == buffer->next_line || p[-1] != '\\')
                break;


After building with the above:

$ gcc-3.4.3-df -P -E -traditional-cpp -xc nl-escape.hs 
-- stuff
infix 5 \\ 
-- more stuff

$ gcc-3.4.3-df -P -E -xc nl-escape.hs                  
-- stuff
nl-escape.hs:2:10: warning: backslash and newline separated by space
infix 5 \-- more stuff

$ gcc-3.4.3-df -v                     
Reading specs from /tool/gcc-3.4.3-df/i586-Linux/lib/gcc/i586-linux/3.4.3-df/specs
Configured with: ../gcc-3.4.3-df/configure --host=i586-linux --target=i586-linux --prefix=/tool/gcc-3.4.3-df --exec-prefix=/tool/gcc-3.4.3-df/i586-Linux --program-suffix=-3.4.3-df --with-cpu=i586 --with-arch=i586 --with-tune=i586 --enable-__cxa_atexit --enable-languages=c,c++,f77
Thread model: posix
gcc version 3.4.3-df


..david



More information about the Gcc-bugs mailing list