CRLF translations by CPP?

Bruno Haible haible@ilog.fr
Tue Jan 27 04:02:00 GMT 1998


> Is there a way to enable egcs to properly parse files with lines ending
> in CarriageReturn-LineFeed?

This issue was discussed between Paul Eggert, Richard Stallman and the gcc2
list in May 1997. The outcome of the discussion was:

1) The issue of CR LF and the issue of whitespace after backslash should
   be treated separately.
2) CR LF should be treated like LF (newline) on all platforms.
3) Inside macro definitions, whitespace between a backslash and a newline
   character deserves a warning, but this whitespace should not be ignored
   (because it is part of the source file).

Here is a patch which implements this. I think we can put this in.

                      Bruno


Tue Jan 27 02:43:01 1998  Bruno Haible  <bruno@linuix.mathematik.uni-karlsruhe.de>

        * cccp.c (crlf_pcp): New function.
          (main, finclude): Call it.
        * cccp.c (handle_directive): Emit warning for trailing whitespace
          after backslash.

*** egcs-980122/gcc/cccp.c.bak	Sat Jan 24 12:52:16 1998
--- egcs-980122/gcc/cccp.c	Tue Jan 27 02:38:32 1998
*************** static void path_include PROTO((char *))
*** 983,988 ****
--- 983,989 ----
  
  static U_CHAR *index0 PROTO((U_CHAR *, int, size_t));
  
+ static void crlf_pcp PROTO((FILE_BUF *));
  static void trigraph_pcp PROTO((FILE_BUF *));
  
  static void newline_fix PROTO((U_CHAR *));
*************** main (argc, argv)
*** 2129,2134 ****
--- 2130,2139 ----
    }
    fp->buf[fp->length] = '\0';
  
+   /* Convert DOS/VAX CR/LF sequences to NL, even on Unix.  */
+ 
+   crlf_pcp (fp);
+ 
    /* Unless inhibited, convert trigraphs in the input.  */
  
    if (!no_trigraphs)
*************** index0 (s, c, n)
*** 2269,2274 ****
--- 2274,2314 ----
    }
  }
  
+ /* Pre-C-Preprocessor to translate CR/LF sequences in BUF into NLs
+    before main CCCP processing.  */
+ 
+ static void
+ crlf_pcp (buf)
+      FILE_BUF *buf;
+ {
+   register /*const*/ U_CHAR *fptr, *sptr, *lptr;
+   register U_CHAR *bptr;
+   int len;
+ 
+   fptr = sptr = bptr = buf->buf;
+   lptr = fptr + buf->length;
+   while ((sptr = index0 (sptr, '\r', (size_t) (lptr - sptr))) != NULL) {
+     if (*++sptr != '\n')
+       continue;
+ 
+     len = sptr - fptr - 1;
+ 
+     /* BSD doc says bcopy () works right for overlapping strings.  In ANSI
+        C, this will be memmove ().  */
+     if (bptr != fptr && len > 0)
+       bcopy ((char *) fptr, (char *) bptr, len);
+ 
+     bptr += len;
+     *bptr++ = '\n';
+     fptr = ++sptr;
+   }
+   len = buf->length - (fptr - buf->buf);
+   if (bptr != fptr && len > 0)
+     bcopy ((char *) fptr, (char *) bptr, len);
+   buf->length -= fptr - bptr;
+   buf->buf[buf->length] = '\0';
+ }
+ 
  /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
     before main CCCP processing.  Name `pcp' is also in honor of the
     drugs the trigraph designers must have been on.
*************** handle_directive (ip, op)
*** 3661,3666 ****
--- 3701,3715 ----
  	switch (c) {
  	case '\\':
  	  if (bp < limit) {
+ 	    /* Backslash followed by whitespace and then newline deserves a
+ 	       warning.  */
+ 	    if (warn_comments)
+ 	      {
+ 		cp = bp;
+ 		while (cp < limit && is_hor_space[*cp]) cp++;
+ 		if (cp < limit && *cp == '\n' && bp != cp)
+ 		  warning ("trailing whitespace after backslash within macro definition");
+ 	      }
  	    if (*bp == '\n') {
  	      ip->lineno++;
  	      copy_directive = 1;
*************** finclude (f, inc, op, system_header_p, d
*** 5045,5050 ****
--- 5094,5101 ----
  
    indepth++;
    input_file_stack_tick++;
+ 
+   crlf_pcp (fp);
  
    if (!no_trigraphs)
      trigraph_pcp (fp);



More information about the Gcc mailing list