[PATCH] Fix PR preprocessor/6489 (take 3)

Jakub Jelinek jakub@redhat.com
Thu May 2 15:48:00 GMT 2002


On Thu, May 02, 2002 at 01:33:40PM -0700, Zack Weinberg wrote:
> On Thu, May 02, 2002 at 09:39:41AM -0400, Jakub Jelinek wrote:
> > 
> > It handles stuff like #include <foo.h>^M but not \^M. \^M is handled in
> > > 50 cases in that spaghetti code, I've started working on a patch but
> > it would be huge (what I wrote was about 14K patch and was far from
> > finishing it).
> > Is cpp -traditional actually performance critical?
> > Wonder if a simple pass over a newly read buffer which would
> > canonicalize line endings wouldn't be much cleaner and shorter
> > (and obviously correct)...
> 
> No, it's not performance critical -- and Neil wants to fold it back
> into cpplib, which would make the whole issue go away.  A prescan pass
> would do in the meantime, and as you say, be much easier to validate.

Ok, here is the patch to do that.
Ok to commit?
Mark, ok for branch?

2002-05-03  Jakub Jelinek  <jakub@redhat.com>

	PR preprocessor/6489
	* tradcpp.c (fixup_newlines): New.
	(main, finclude): Use it.

--- gcc/tradcpp.c.jj	Mon Feb  4 11:41:44 2002
+++ gcc/tradcpp.c	Fri May  3 00:18:21 2002
@@ -430,6 +430,7 @@ static void grow_outbuf 	PARAMS ((FILE_B
 static int handle_directive 	PARAMS ((FILE_BUF *, FILE_BUF *));
 static void process_include	PARAMS ((struct file_name_list *,
 					 const U_CHAR *, int, int, FILE_BUF *));
+static void fixup_newlines	PARAMS ((FILE_BUF *));
 static void finclude		PARAMS ((int, const char *,
 					 struct file_name_list *, FILE_BUF *));
 static void init_dependency_output PARAMS ((void));
@@ -948,6 +949,7 @@ main (argc, argv)
   }
   fp->bufp = fp->buf;
   fp->if_stack = if_stack;
+  fixup_newlines (fp);
 
   /* Make sure data ends with a newline.  And put a null after it.  */
 
@@ -2590,6 +2592,42 @@ process_include (stackp, fbeg, flen, sys
   }
 }
 
+/* Replace all CR NL, NL CR and CR sequences with NL.  */
+
+static void
+fixup_newlines (FILE_BUF *fp)
+{
+  U_CHAR *p, *q, *end;
+
+  if (fp->length <= 0)
+    return;
+
+  end = fp->buf + fp->length;
+  *end = '\r';
+  p = (U_CHAR *) strchr ((const char *) fp->buf, '\r');
+  *end = '\0';
+  if (p == end)
+    return;
+
+  if (p > fp->buf && p[-1] == '\n')
+    p--;
+  q = p;
+  while (p < end)
+    switch (*p)
+      {
+      default:
+	*q++ = *p++;
+	break;
+      case '\n':
+      case '\r':
+	p += 1 + (p[0] + p[1] == '\n' + '\r');
+	*q++ = '\n';
+	break;
+      }
+
+  fp->length = q - fp->buf;
+}
+
 /* Process the contents of include file FNAME, already open on descriptor F,
    with output to OP.  */
 
@@ -2664,6 +2702,7 @@ finclude (f, fname, nhd, op)
     fp->length = st_size;
   }
   close (f);
+  fixup_newlines (fp);
 
   /* Make sure data ends with a newline.  And put a null after it.  */
 


	Jakub



More information about the Gcc-patches mailing list