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]

Re: CPP outputs wrong number of newlines sometimes


Jamie Lokier wrote:
> The line number is not properly maintained when the multi-line string is
> printed.  Something similar happens with multi-line comments when -C is
> used.

Patch attached.  Let me know if it's useful (I haven't yet signed papers).

enjoy,
-- Jamie

2000-09-25  Jamie Lokier  <jamie.lokier@cern.ch>

	* cpplex.c (output_token): Return the count of printed newlines
	in multi-line strings and comments; return type changed from void.
	(cpp_scan_buffer): Update line number for newlines embedded in
	strings and comment tokens.  Move setting of print->need_newline
	to before output_token, so that output_token can clear it.

Index: cpplex.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cpplex.c,v
retrieving revision 1.101
diff -u -w -r1.101 cpplex.c
--- cpplex.c	2000/09/24 10:42:09	1.101
+++ cpplex.c	2000/09/25 20:19:03
@@ -123,7 +123,8 @@
 static void expand_context_stack PARAMS ((cpp_reader *));
 static unsigned char * spell_token PARAMS ((cpp_reader *, const cpp_token *,
 					    unsigned char *));
-static void output_token PARAMS ((cpp_reader *, FILE *, const cpp_token *,
+static unsigned int output_token PARAMS ((cpp_reader *, FILE *,
+					  const cpp_token *,
 				  const cpp_token *, int));
 typedef unsigned int (* speller) PARAMS ((unsigned char *, cpp_toklist *,
 					  cpp_token *));
@@ -347,8 +348,8 @@
 
       if (token->type != CPP_PLACEMARKER)
 	{
-	  output_token (pfile, print->outf, token, prev, 1);
 	  pfile->need_newline = 1;
+	  print->lineno += output_token (pfile, print->outf, token, prev, 1);
 	}
 
       prev = token;
@@ -1718,14 +1750,19 @@
    whitespace before it, to FP.  PREV is the previous token, which
    is used to determine if we need to shove in an extra space in order
    to avoid accidental token paste.  If WHITE is 0, do not insert any
-   leading whitespace.  */
-static void
+   leading whitespace.
+
+   Returns the number of newlines printed.  May also clear
+   pfile->need_newline if the last character output was \n. */
+static unsigned int
 output_token (pfile, fp, token, prev, white)
      cpp_reader *pfile;
      FILE *fp;
      const cpp_token *token, *prev;
      int white;
 {
+  unsigned int newlines = 0;
+
   if (white)
     {
       int dummy;
@@ -1782,6 +1819,7 @@
     case SPELL_STRING:
       {
 	int left, right, tag;
+	unsigned int i;
 	switch (token->type)
 	  {
 	  case CPP_STRING:	left = '"';  right = '"';  tag = '\0'; break;
@@ -1796,6 +1834,13 @@
 	if (left) putc (left, fp);
 	fwrite (token->val.str.text, 1, token->val.str.len, fp);
 	if (right) putc (right, fp);
+
+	/* Maintain the line number for multi-line strings and comments. */
+	for (i = 0; i < token->val.str.len; i++)
+	  if (token->val.str.text [i] == '\n')
+	    newlines++;
+	if (i > 0 && token->val.str.text [i-1] == '\n')
+	  pfile->need_newline = 0;
       }
       break;
 
@@ -1808,6 +1853,8 @@
          elsewhere.  */
       break;
     }
+
+  return newlines;
 }
 
 /* Dump the original user's spelling of argument index ARG_NO to the


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