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]

Patch: FYI: fix PR preprocessor/27777


I'm checking this in.

This fixes PR preprocessor/27777.  The bug here is that libcpp emits
an error message about digraphs in the middle of emitting an error
message from #error, resulting in scrambled output.

The fix I chose is to create the #error message in memory and then
emit it after processing the tokens in the #error.  This also let me
hide _cpp_begin_message, and furthermore is a step toward getting
libcpp to use gcc's error code.

Test case included.  Bootstrapped and regtested on the compile farm
(x86-64).

Tom

gcc/testsuite/ChangeLog:
2008-05-21  Tom Tromey  <tromey@redhat.com>

	PR preprocessor/27777:
	* gcc.dg/cpp/pr27777.c: New file.

libcpp/ChangeLog:
2008-05-21  Tom Tromey  <tromey@redhat.com>

	PR preprocessor/27777:
	* lex.c (cpp_output_line_to_string): New function.
	* internal.h (_cpp_begin_message): Don't declare.
	* errors.c (_cpp_begin_message): Now static.
	* include/cpplib.h (cpp_output_line_to_string): Declare.
	* directives.c (do_diagnostic): Rewrote.  Use
	cpp_output_line_to_string.  Don't use _cpp_begin_message.

Index: gcc/testsuite/gcc.dg/cpp/pr27777.c
===================================================================
--- gcc/testsuite/gcc.dg/cpp/pr27777.c	(revision 0)
+++ gcc/testsuite/gcc.dg/cpp/pr27777.c	(revision 0)
@@ -0,0 +1,8 @@
+/* PR preprocessor/27777 */
+/* { dg-do preprocess } */
+/* { dg-options { -trigraphs -Wall } } */
+
+#error "BUG??!"
+
+/* { dg-error "BUG" "" { target *-*-* } 5 } */
+/* { dg-warning "trigraph" "" { target *-*-* } 5 } */
Index: libcpp/directives.c
===================================================================
--- libcpp/directives.c	(revision 135719)
+++ libcpp/directives.c	(working copy)
@@ -1016,14 +1016,20 @@
 static void
 do_diagnostic (cpp_reader *pfile, int code, int print_dir)
 {
-  if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0))
-    {
-      if (print_dir)
-	fprintf (stderr, "#%s ", pfile->directive->name);
-      pfile->state.prevent_expansion++;
-      cpp_output_line (pfile, stderr);
-      pfile->state.prevent_expansion--;
-    }
+  const unsigned char *dir_name;
+  unsigned char *line;
+  source_location src_loc = pfile->cur_token[-1].src_loc;
+
+  if (print_dir)
+    dir_name = pfile->directive->name;
+  else
+    dir_name = NULL;
+  pfile->state.prevent_expansion++;
+  line = cpp_output_line_to_string (pfile, dir_name);
+  pfile->state.prevent_expansion--;
+
+  cpp_error_with_line (pfile, code, src_loc, 0, "%s", line);
+  free (line);
 }
 
 static void
Index: libcpp/include/cpplib.h
===================================================================
--- libcpp/include/cpplib.h	(revision 135719)
+++ libcpp/include/cpplib.h	(working copy)
@@ -844,6 +844,8 @@
 /* In lex.c */
 extern int cpp_ideq (const cpp_token *, const char *);
 extern void cpp_output_line (cpp_reader *, FILE *);
+extern unsigned char *cpp_output_line_to_string (cpp_reader *,
+						 const unsigned char *);
 extern void cpp_output_token (const cpp_token *, FILE *);
 extern const char *cpp_type2name (enum cpp_ttype);
 /* Returns the value of an escape sequence, truncated to the correct
Index: libcpp/errors.c
===================================================================
--- libcpp/errors.c	(revision 135719)
+++ libcpp/errors.c	(working copy)
@@ -76,7 +76,7 @@
    big enough max_column_hint.)
 
    Returns 0 if the error has been suppressed.  */
-int
+static int
 _cpp_begin_message (cpp_reader *pfile, int code,
 		    source_location src_loc, unsigned int column)
 {
Index: libcpp/internal.h
===================================================================
--- libcpp/internal.h	(revision 135719)
+++ libcpp/internal.h	(working copy)
@@ -518,10 +518,6 @@
   return pfile->line_table->depth == 1;
 }
 
-/* In errors.c  */
-extern int _cpp_begin_message (cpp_reader *, int,
-			       source_location, unsigned int);
-
 /* In macro.c */
 extern void _cpp_free_definition (cpp_hashnode *);
 extern bool _cpp_create_definition (cpp_reader *, cpp_hashnode *);
Index: libcpp/lex.c
===================================================================
--- libcpp/lex.c	(revision 135719)
+++ libcpp/lex.c	(working copy)
@@ -1,5 +1,5 @@
 /* CPP Library - lexical analysis.
-   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
+   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
    Contributed by Per Bothner, 1994-95.
    Based on CCCP program by Paul Rubin, June 1986
    Adapted to ANSI C, Richard Stallman, Jan 1987
@@ -1539,6 +1539,51 @@
   putc ('\n', fp);
 }
 
+/* Return a string representation of all the remaining tokens on the
+   current line.  The result is allocated using xmalloc and must be
+   freed by the caller.  */
+unsigned char *
+cpp_output_line_to_string (cpp_reader *pfile, const unsigned char *dir_name)
+{
+  const cpp_token *token;
+  unsigned int out = dir_name ? ustrlen (dir_name) : 0;
+  unsigned int alloced = 120 + out;
+  unsigned char *result = (unsigned char *) xmalloc (alloced);
+
+  /* If DIR_NAME is empty, there are no initial contents.  */
+  if (dir_name)
+    {
+      sprintf ((char *) result, "#%s ", dir_name);
+      out += 2;
+    }
+
+  token = cpp_get_token (pfile);
+  while (token->type != CPP_EOF)
+    {
+      unsigned char *last;
+      /* Include room for a possible space and the terminating nul.  */
+      unsigned int len = cpp_token_len (token) + 2;
+
+      if (out + len > alloced)
+	{
+	  alloced *= 2;
+	  if (out + len > alloced)
+	    alloced = out + len;
+	  result = (unsigned char *) xrealloc (result, alloced);
+	}
+
+      last = cpp_spell_token (pfile, token, &result[out], 0);
+      out = last - result;
+
+      token = cpp_get_token (pfile);
+      if (token->flags & PREV_WHITE)
+	result[out++] = ' ';
+    }
+
+  result[out] = '\0';
+  return result;
+}
+
 /* Memory buffers.  Changing these three constants can have a dramatic
    effect on performance.  The values here are reasonable defaults,
    but might be tuned.  If you adjust them, be sure to test across a


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