OSDN Git Service

Prevent sharing of commit calls among transactions.
[pf3gnuchains/gcc-fork.git] / gcc / pretty-print.c
index f5a1a77..8aa9978 100644 (file)
@@ -1,12 +1,13 @@
 /* Various declarations for language-independent pretty-print subroutines.
 /* Various declarations for language-independent pretty-print subroutines.
-   Copyright (C) 2003, 2004 Free Software Foundation, Inc.
+   Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010
+   Free Software Foundation, Inc.
    Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
 
 This file is part of GCC.
 
 GCC is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
    Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
 
 This file is part of GCC.
 
 GCC is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
-Software Foundation; either version 2, or (at your option) any later
+Software Foundation; either version 3, or (at your option) any later
 version.
 
 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
 version.
 
 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
@@ -15,24 +16,22 @@ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.
 
 You should have received a copy of the GNU General Public License
 for more details.
 
 You should have received a copy of the GNU General Public License
-along with GCC; see the file COPYING.  If not, write to the Free
-Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.  */
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
 
 #include "config.h"
 
 #include "config.h"
-#undef FLOAT /* This is for hpux. They should change hpux.  */
-#undef FFS  /* Some systems define this in param.h.  */
 #include "system.h"
 #include "coretypes.h"
 #include "intl.h"
 #include "pretty-print.h"
 
 #include "system.h"
 #include "coretypes.h"
 #include "intl.h"
 #include "pretty-print.h"
 
-#define obstack_chunk_alloc xmalloc
-#define obstack_chunk_free  free
+#if HAVE_ICONV
+#include <iconv.h>
+#endif
 
 /* A pointer to the formatted diagnostic message.  */
 #define pp_formatted_text_data(PP) \
 
 /* A pointer to the formatted diagnostic message.  */
 #define pp_formatted_text_data(PP) \
-   ((const char *) obstack_base (&pp_base (PP)->buffer->obstack))
+   ((const char *) obstack_base (pp_base (PP)->buffer->obstack))
 
 /* Format an integer given by va_arg (ARG, type-specifier T) where
    type-specifier is a precision modifier as indicated by PREC.  F is
 
 /* Format an integer given by va_arg (ARG, type-specifier T) where
    type-specifier is a precision modifier as indicated by PREC.  F is
@@ -50,7 +49,7 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
         break;                                               \
                                                              \
       case 2:                                                \
         break;                                               \
                                                              \
       case 2:                                                \
-        pp_scalar (PP, "%ll" F, va_arg (ARG, long long T));  \
+        pp_scalar (PP, "%" HOST_LONG_LONG_FORMAT F, va_arg (ARG, long long T));  \
         break;                                               \
                                                              \
       default:                                               \
         break;                                               \
                                                              \
       default:                                               \
@@ -148,7 +147,7 @@ pp_maybe_wrap_text (pretty_printer *pp, const char *start, const char *end)
 static inline void
 pp_append_r (pretty_printer *pp, const char *start, int length)
 {
 static inline void
 pp_append_r (pretty_printer *pp, const char *start, int length)
 {
-  obstack_grow (&pp->buffer->obstack, start, length);
+  obstack_grow (pp->buffer->obstack, start, length);
   pp->buffer->line_length += length;
 }
 
   pp->buffer->line_length += length;
 }
 
@@ -165,8 +164,7 @@ pp_base_indent (pretty_printer *pp)
     pp_space (pp);
 }
 
     pp_space (pp);
 }
 
-/* Format a message pointed to by TEXT.  The following format specifiers are
-   recognized as being client independent:
+/* The following format specifiers are recognized as being client independent:
    %d, %i: (signed) integer in base ten.
    %u: unsigned integer in base ten.
    %o: unsigned integer in base eight.
    %d, %i: (signed) integer in base ten.
    %u: unsigned integer in base ten.
    %o: unsigned integer in base eight.
@@ -183,60 +181,243 @@ pp_base_indent (pretty_printer *pp)
    %>: closing quote.
    %': apostrophe (should only be used in untranslated messages;
        translations should use appropriate punctuation directly).
    %>: closing quote.
    %': apostrophe (should only be used in untranslated messages;
        translations should use appropriate punctuation directly).
-   %.*s: a substring the length of which is specified by an integer.
-   %H: location_t.
-   Flag 'q': quote formatted text (must come immediately after '%').  */
+   %.*s: a substring the length of which is specified by an argument
+        integer.
+   %Ns: likewise, but length specified as constant in the format string.
+   Flag 'q': quote formatted text (must come immediately after '%').
+
+   Arguments can be used sequentially, or through %N$ resp. *N$
+   notation Nth argument after the format string.  If %N$ / *N$
+   notation is used, it must be used for all arguments, except %m, %%,
+   %<, %> and %', which may not have a number, as they do not consume
+   an argument.  When %M$.*N$s is used, M must be N + 1.  (This may
+   also be written %M$.*s, provided N is not otherwise used.)  The
+   format string must have conversion specifiers with argument numbers
+   1 up to highest argument; each argument may only be used once.
+   A format string can have at most 30 arguments.  */
+
+/* Formatting phases 1 and 2: render TEXT->format_spec plus
+   TEXT->args_ptr into a series of chunks in PP->buffer->args[].
+   Phase 3 is in pp_base_format_text.  */
+
 void
 void
-pp_base_format_text (pretty_printer *pp, text_info *text)
+pp_base_format (pretty_printer *pp, text_info *text)
 {
 {
-  for (; *text->format_spec; ++text->format_spec)
+  output_buffer *buffer = pp->buffer;
+  const char *p;
+  const char **args;
+  struct chunk_info *new_chunk_array;
+
+  unsigned int curarg = 0, chunk = 0, argno;
+  pp_wrapping_mode_t old_wrapping_mode;
+  bool any_unnumbered = false, any_numbered = false;
+  const char **formatters[PP_NL_ARGMAX];
+
+  /* Allocate a new chunk structure.  */
+  new_chunk_array = XOBNEW (&buffer->chunk_obstack, struct chunk_info);
+  new_chunk_array->prev = buffer->cur_chunk_array;
+  buffer->cur_chunk_array = new_chunk_array;
+  args = new_chunk_array->args;
+
+  /* Formatting phase 1: split up TEXT->format_spec into chunks in
+     PP->buffer->args[].  Even-numbered chunks are to be output
+     verbatim, odd-numbered chunks are format specifiers.
+     %m, %%, %<, %>, and %' are replaced with the appropriate text at
+     this point.  */
+
+  memset (formatters, 0, sizeof formatters);
+
+  for (p = text->format_spec; *p; )
     {
     {
-      int precision = 0;
-      bool wide = false;
-      bool quoted = false;
+      while (*p != '\0' && *p != '%')
+       {
+         obstack_1grow (&buffer->chunk_obstack, *p);
+         p++;
+       }
 
 
-      /* Ignore text.  */
-      {
-       const char *p = text->format_spec;
-       while (*p && *p != '%')
-         ++p;
-       pp_wrap_text (pp, text->format_spec, p);
-        text->format_spec = p;
-      }
+      if (*p == '\0')
+       break;
+
+      switch (*++p)
+       {
+       case '\0':
+         gcc_unreachable ();
+
+       case '%':
+         obstack_1grow (&buffer->chunk_obstack, '%');
+         p++;
+         continue;
+
+       case '<':
+         obstack_grow (&buffer->chunk_obstack,
+                       open_quote, strlen (open_quote));
+         p++;
+         continue;
+
+       case '>':
+       case '\'':
+         obstack_grow (&buffer->chunk_obstack,
+                       close_quote, strlen (close_quote));
+         p++;
+         continue;
 
 
-      if (*text->format_spec == '\0')
+       case 'm':
+         {
+           const char *errstr = xstrerror (text->err_no);
+           obstack_grow (&buffer->chunk_obstack, errstr, strlen (errstr));
+         }
+         p++;
+         continue;
+
+       default:
+         /* Handled in phase 2.  Terminate the plain chunk here.  */
+         obstack_1grow (&buffer->chunk_obstack, '\0');
+         gcc_assert (chunk < PP_NL_ARGMAX * 2);
+         args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
+         break;
+       }
+
+      if (ISDIGIT (*p))
+       {
+         char *end;
+         argno = strtoul (p, &end, 10) - 1;
+         p = end;
+         gcc_assert (*p == '$');
+         p++;
+
+         any_numbered = true;
+         gcc_assert (!any_unnumbered);
+       }
+      else
+       {
+         argno = curarg++;
+         any_unnumbered = true;
+         gcc_assert (!any_numbered);
+       }
+      gcc_assert (argno < PP_NL_ARGMAX);
+      gcc_assert (!formatters[argno]);
+      formatters[argno] = &args[chunk];
+      do
+       {
+         obstack_1grow (&buffer->chunk_obstack, *p);
+         p++;
+       }
+      while (strchr ("qwl+#", p[-1]));
+
+      if (p[-1] == '.')
+       {
+         /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
+            (where M == N + 1).  */
+         if (ISDIGIT (*p))
+           {
+             do
+               {
+                 obstack_1grow (&buffer->chunk_obstack, *p);
+                 p++;
+               }
+             while (ISDIGIT (p[-1]));
+             gcc_assert (p[-1] == 's');
+           }
+         else
+           {
+             gcc_assert (*p == '*');
+             obstack_1grow (&buffer->chunk_obstack, '*');
+             p++;
+
+             if (ISDIGIT (*p))
+               {
+                 char *end;
+                 unsigned int argno2 = strtoul (p, &end, 10) - 1;
+                 p = end;
+                 gcc_assert (argno2 == argno - 1);
+                 gcc_assert (!any_unnumbered);
+                 gcc_assert (*p == '$');
+
+                 p++;
+                 formatters[argno2] = formatters[argno];
+               }
+             else
+               {
+                 gcc_assert (!any_numbered);
+                 formatters[argno+1] = formatters[argno];
+                 curarg++;
+               }
+             gcc_assert (*p == 's');
+             obstack_1grow (&buffer->chunk_obstack, 's');
+             p++;
+           }
+       }
+      if (*p == '\0')
        break;
 
        break;
 
-      /* We got a '%'.  Check for 'q', then parse precision modifiers,
-        if any.  */
-      if (*++text->format_spec == 'q')
+      obstack_1grow (&buffer->chunk_obstack, '\0');
+      gcc_assert (chunk < PP_NL_ARGMAX * 2);
+      args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
+    }
+
+  obstack_1grow (&buffer->chunk_obstack, '\0');
+  gcc_assert (chunk < PP_NL_ARGMAX * 2);
+  args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
+  args[chunk] = 0;
+
+  /* Set output to the argument obstack, and switch line-wrapping and
+     prefixing off.  */
+  buffer->obstack = &buffer->chunk_obstack;
+  old_wrapping_mode = pp_set_verbatim_wrapping (pp);
+
+  /* Second phase.  Replace each formatter with the formatted text it
+     corresponds to.  */
+
+  for (argno = 0; formatters[argno]; argno++)
+    {
+      int precision = 0;
+      bool wide = false;
+      bool plus = false;
+      bool hash = false;
+      bool quote = false;
+
+      /* We do not attempt to enforce any ordering on the modifier
+        characters.  */
+
+      for (p = *formatters[argno];; p++)
        {
        {
-         quoted = true;
-         ++text->format_spec;
+         switch (*p)
+           {
+           case 'q':
+             gcc_assert (!quote);
+             quote = true;
+             continue;
+
+           case '+':
+             gcc_assert (!plus);
+             plus = true;
+             continue;
+
+           case '#':
+             gcc_assert (!hash);
+             hash = true;
+             continue;
+
+           case 'w':
+             gcc_assert (!wide);
+             wide = true;
+             continue;
+
+           case 'l':
+             /* We don't support precision beyond that of "long long".  */
+             gcc_assert (precision < 2);
+             precision++;
+             continue;
+           }
+         break;
        }
        }
-      switch (*text->format_spec)
-        {
-        case 'w':
-          wide = true;
-          ++text->format_spec;
-          break;
-
-        case 'l':
-          do
-            ++precision;
-          while (*++text->format_spec == 'l');
-          break;
-
-        default:
-          break;
-        }
-      /* We don't support precision beyond that of "long long".  */
-      if (precision > 2)
-        abort();
-
-      if (quoted)
+
+      gcc_assert (!wide || precision == 0);
+
+      if (quote)
        pp_string (pp, open_quote);
        pp_string (pp, open_quote);
-      switch (*text->format_spec)
+
+      switch (*p)
        {
        case 'c':
          pp_character (pp, va_arg (*text->args_ptr, int));
        {
        case 'c':
          pp_character (pp, va_arg (*text->args_ptr, int));
@@ -244,103 +425,132 @@ pp_base_format_text (pretty_printer *pp, text_info *text)
 
        case 'd':
        case 'i':
 
        case 'd':
        case 'i':
-          if (wide)
-            pp_wide_integer (pp, va_arg (*text->args_ptr, HOST_WIDE_INT));
-          else
-            pp_integer_with_precision
-              (pp, *text->args_ptr, precision, int, "d");
+         if (wide)
+           pp_wide_integer (pp, va_arg (*text->args_ptr, HOST_WIDE_INT));
+         else
+           pp_integer_with_precision
+             (pp, *text->args_ptr, precision, int, "d");
          break;
 
        case 'o':
          break;
 
        case 'o':
-          if (wide)
-            pp_scalar (pp, "%" HOST_WIDE_INT_PRINT "o",
-                       va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
-          else
-            pp_integer_with_precision
-              (pp, *text->args_ptr, precision, unsigned, "u");
+         if (wide)
+           pp_scalar (pp, "%" HOST_WIDE_INT_PRINT "o",
+                      va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
+         else
+           pp_integer_with_precision
+             (pp, *text->args_ptr, precision, unsigned, "o");
          break;
 
        case 's':
          pp_string (pp, va_arg (*text->args_ptr, const char *));
          break;
 
          break;
 
        case 's':
          pp_string (pp, va_arg (*text->args_ptr, const char *));
          break;
 
-        case 'p':
-          pp_pointer (pp, va_arg (*text->args_ptr, void *));
-          break;
+       case 'p':
+         pp_pointer (pp, va_arg (*text->args_ptr, void *));
+         break;
 
        case 'u':
 
        case 'u':
-          if (wide)
-            pp_scalar (pp, HOST_WIDE_INT_PRINT_UNSIGNED,
-                       va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
-          else
-            pp_integer_with_precision
-              (pp, *text->args_ptr, precision, unsigned, "u");
+         if (wide)
+           pp_scalar (pp, HOST_WIDE_INT_PRINT_UNSIGNED,
+                      va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
+         else
+           pp_integer_with_precision
+             (pp, *text->args_ptr, precision, unsigned, "u");
          break;
 
        case 'x':
          break;
 
        case 'x':
-          if (wide)
-            pp_scalar (pp, HOST_WIDE_INT_PRINT_HEX,
-                       va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
-          else
-            pp_integer_with_precision
-              (pp, *text->args_ptr, precision, unsigned, "x");
-         break;
-
-       case 'm':
-         pp_string (pp, xstrerror (text->err_no));
-         break;
-
-       case '%':
-         pp_character (pp, '%');
+         if (wide)
+           pp_scalar (pp, HOST_WIDE_INT_PRINT_HEX,
+                      va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
+         else
+           pp_integer_with_precision
+             (pp, *text->args_ptr, precision, unsigned, "x");
          break;
 
          break;
 
-       case '<':
-         pp_string (pp, open_quote);
-         break;
-
-       case '>':
-       case '\'':
-         pp_string (pp, close_quote);
-         break;
-
-        case 'H':
-          {
-            const location_t *locus = va_arg (*text->args_ptr, location_t *);
-            pp_string (pp, "file '");
-            pp_string (pp, locus->file);
-            pp_string (pp, "', line ");
-            pp_decimal_int (pp, locus->line);
-          }
-          break;
-
        case '.':
          {
            int n;
            const char *s;
        case '.':
          {
            int n;
            const char *s;
-           /* We handle no precision specifier but '%.*s'.  */
-           if (*++text->format_spec != '*')
-             abort ();
-           else if (*++text->format_spec != 's')
-             abort ();
-           n = va_arg (*text->args_ptr, int);
+
+           /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
+              (where M == N + 1).  The format string should be verified
+              already from the first phase.  */
+           p++;
+           if (ISDIGIT (*p))
+             {
+               char *end;
+               n = strtoul (p, &end, 10);
+               p = end;
+               gcc_assert (*p == 's');
+             }
+           else
+             {
+               gcc_assert (*p == '*');
+               p++;
+               gcc_assert (*p == 's');
+               n = va_arg (*text->args_ptr, int);
+
+               /* This consumes a second entry in the formatters array.  */
+               gcc_assert (formatters[argno] == formatters[argno+1]);
+               argno++;
+             }
+
            s = va_arg (*text->args_ptr, const char *);
            pp_append_text (pp, s, s + n);
          }
          break;
 
        default:
            s = va_arg (*text->args_ptr, const char *);
            pp_append_text (pp, s, s + n);
          }
          break;
 
        default:
-          if (!pp_format_decoder (pp) || !(*pp_format_decoder (pp)) (pp, text))
-           {
-             /* Hmmm.  The client failed to install a format translator
-                 but called us with an unrecognized format.  Or, maybe, the
-                 translated string just contains an invalid format, or
-                 has formats in the wrong order.  Sorry.  */
-             abort ();
-           }
+         {
+           bool ok;
+
+           gcc_assert (pp_format_decoder (pp));
+           ok = pp_format_decoder (pp) (pp, text, p,
+                                        precision, wide, plus, hash);
+           gcc_assert (ok);
+         }
        }
        }
-      if (quoted)
+
+      if (quote)
        pp_string (pp, close_quote);
        pp_string (pp, close_quote);
+
+      obstack_1grow (&buffer->chunk_obstack, '\0');
+      *formatters[argno] = XOBFINISH (&buffer->chunk_obstack, const char *);
     }
     }
+
+#ifdef ENABLE_CHECKING
+  for (; argno < PP_NL_ARGMAX; argno++)
+    gcc_assert (!formatters[argno]);
+#endif
+
+  /* Revert to normal obstack and wrapping mode.  */
+  buffer->obstack = &buffer->formatted_obstack;
+  buffer->line_length = 0;
+  pp_wrapping_mode (pp) = old_wrapping_mode;
+  pp_clear_state (pp);
+}
+
+/* Format of a message pointed to by TEXT.  */
+void
+pp_base_output_formatted_text (pretty_printer *pp)
+{
+  unsigned int chunk;
+  output_buffer *buffer = pp_buffer (pp);
+  struct chunk_info *chunk_array = buffer->cur_chunk_array;
+  const char **args = chunk_array->args;
+
+  gcc_assert (buffer->obstack == &buffer->formatted_obstack);
+  gcc_assert (buffer->line_length == 0);
+
+  /* This is a third phase, first 2 phases done in pp_base_format_args.
+     Now we actually print it.  */
+  for (chunk = 0; args[chunk]; chunk++)
+    pp_string (pp, args[chunk]);
+
+  /* Deallocate the chunk structure and everything after it (i.e. the
+     associated series of formatted strings).  */
+  buffer->cur_chunk_array = chunk_array->prev;
+  obstack_free (&buffer->chunk_obstack, chunk_array);
 }
 
 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
 }
 
 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
@@ -348,17 +558,15 @@ pp_base_format_text (pretty_printer *pp, text_info *text)
 void
 pp_base_format_verbatim (pretty_printer *pp, text_info *text)
 {
 void
 pp_base_format_verbatim (pretty_printer *pp, text_info *text)
 {
-  diagnostic_prefixing_rule_t rule = pp_prefixing_rule (pp);
-  int line_cutoff = pp_line_cutoff (pp);
-
   /* Set verbatim mode.  */
   /* Set verbatim mode.  */
-  pp->prefixing_rule = DIAGNOSTICS_SHOW_PREFIX_NEVER;
-  pp_line_cutoff (pp) = 0;
+  pp_wrapping_mode_t oldmode = pp_set_verbatim_wrapping (pp);
+
   /* Do the actual formatting.  */
   /* Do the actual formatting.  */
-  pp_format_text (pp, text);
+  pp_format (pp, text);
+  pp_output_formatted_text (pp);
+
   /* Restore previous settings.  */
   /* Restore previous settings.  */
-  pp_prefixing_rule (pp) = rule;
-  pp_line_cutoff (pp) = line_cutoff;
+  pp_wrapping_mode (pp) = oldmode;
 }
 
 /* Flush the content of BUFFER onto the attached stream.  */
 }
 
 /* Flush the content of BUFFER onto the attached stream.  */
@@ -386,7 +594,7 @@ pp_base_set_line_maximum_length (pretty_printer *pp, int length)
 void
 pp_base_clear_output_area (pretty_printer *pp)
 {
 void
 pp_base_clear_output_area (pretty_printer *pp)
 {
-  obstack_free (&pp->buffer->obstack, obstack_base (&pp->buffer->obstack));
+  obstack_free (pp->buffer->obstack, obstack_base (pp->buffer->obstack));
   pp->buffer->line_length = 0;
 }
 
   pp->buffer->line_length = 0;
 }
 
@@ -406,7 +614,7 @@ pp_base_destroy_prefix (pretty_printer *pp)
 {
   if (pp->prefix != NULL)
     {
 {
   if (pp->prefix != NULL)
     {
-      free ((char *) pp->prefix);
+      free (CONST_CAST (char *, pp->prefix));
       pp->prefix = NULL;
     }
 }
       pp->prefix = NULL;
     }
 }
@@ -449,12 +657,15 @@ void
 pp_construct (pretty_printer *pp, const char *prefix, int maximum_length)
 {
   memset (pp, 0, sizeof (pretty_printer));
 pp_construct (pretty_printer *pp, const char *prefix, int maximum_length)
 {
   memset (pp, 0, sizeof (pretty_printer));
-  pp->buffer = xcalloc (1, sizeof (output_buffer));
-  obstack_init (&pp->buffer->obstack);
+  pp->buffer = XCNEW (output_buffer);
+  obstack_init (&pp->buffer->chunk_obstack);
+  obstack_init (&pp->buffer->formatted_obstack);
+  pp->buffer->obstack = &pp->buffer->formatted_obstack;
   pp->buffer->stream = stderr;
   pp_line_cutoff (pp) = maximum_length;
   pp_prefixing_rule (pp) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
   pp_set_prefix (pp, prefix);
   pp->buffer->stream = stderr;
   pp_line_cutoff (pp) = maximum_length;
   pp_prefixing_rule (pp) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
   pp_set_prefix (pp, prefix);
+  pp_translate_identifiers (pp) = true;
 }
 
 /* Append a string delimited by START and END to the output area of
 }
 
 /* Append a string delimited by START and END to the output area of
@@ -481,7 +692,7 @@ pp_base_append_text (pretty_printer *pp, const char *start, const char *end)
 const char *
 pp_base_formatted_text (pretty_printer *pp)
 {
 const char *
 pp_base_formatted_text (pretty_printer *pp)
 {
-  obstack_1grow (&pp->buffer->obstack, '\0');
+  obstack_1grow (pp->buffer->obstack, '\0');
   return pp_formatted_text_data (pp);
 }
 
   return pp_formatted_text_data (pp);
 }
 
@@ -491,7 +702,7 @@ const char *
 pp_base_last_position_in_text (const pretty_printer *pp)
 {
   const char *p = NULL;
 pp_base_last_position_in_text (const pretty_printer *pp)
 {
   const char *p = NULL;
-  struct obstack *text = &pp->buffer->obstack;
+  struct obstack *text = pp->buffer->obstack;
 
   if (obstack_base (text) != obstack_next_free (text))
     p = ((const char *) obstack_next_free (text)) - 1;
 
   if (obstack_base (text) != obstack_next_free (text))
     p = ((const char *) obstack_next_free (text)) - 1;
@@ -518,7 +729,9 @@ pp_printf (pretty_printer *pp, const char *msg, ...)
   text.err_no = errno;
   text.args_ptr = &ap;
   text.format_spec = msg;
   text.err_no = errno;
   text.args_ptr = &ap;
   text.format_spec = msg;
-  pp_format_text (pp, &text);
+  text.locus = NULL;
+  pp_format (pp, &text);
+  pp_output_formatted_text (pp);
   va_end (ap);
 }
 
   va_end (ap);
 }
 
@@ -534,6 +747,7 @@ pp_verbatim (pretty_printer *pp, const char *msg, ...)
   text.err_no = errno;
   text.args_ptr = &ap;
   text.format_spec = msg;
   text.err_no = errno;
   text.args_ptr = &ap;
   text.format_spec = msg;
+  text.locus = NULL;
   pp_format_verbatim (pp, &text);
   va_end (ap);
 }
   pp_format_verbatim (pp, &text);
   va_end (ap);
 }
@@ -544,7 +758,7 @@ pp_verbatim (pretty_printer *pp, const char *msg, ...)
 void
 pp_base_newline (pretty_printer *pp)
 {
 void
 pp_base_newline (pretty_printer *pp)
 {
-  obstack_1grow (&pp->buffer->obstack, '\n');
+  obstack_1grow (pp->buffer->obstack, '\n');
   pp->buffer->line_length = 0;
 }
 
   pp->buffer->line_length = 0;
 }
 
@@ -559,7 +773,7 @@ pp_base_character (pretty_printer *pp, int c)
       if (ISSPACE (c))
         return;
     }
       if (ISSPACE (c))
         return;
     }
-  obstack_1grow (&pp->buffer->obstack, c);
+  obstack_1grow (pp->buffer->obstack, c);
   ++pp->buffer->line_length;
 }
 
   ++pp->buffer->line_length;
 }
 
@@ -571,7 +785,7 @@ pp_base_string (pretty_printer *pp, const char *str)
   pp_maybe_wrap_text (pp, str, str + (str ? strlen (str) : 0));
 }
 
   pp_maybe_wrap_text (pp, str, str + (str ? strlen (str) : 0));
 }
 
-/* Maybe print out a whitespace if needed.   */
+/* Maybe print out a whitespace if needed.  */
 
 void
 pp_base_maybe_space (pretty_printer *pp)
 
 void
 pp_base_maybe_space (pretty_printer *pp)
@@ -582,3 +796,226 @@ pp_base_maybe_space (pretty_printer *pp)
       pp_base (pp)->padding = pp_none;
     }
 }
       pp_base (pp)->padding = pp_none;
     }
 }
+\f
+/* The string starting at P has LEN (at least 1) bytes left; if they
+   start with a valid UTF-8 sequence, return the length of that
+   sequence and set *VALUE to the value of that sequence, and
+   otherwise return 0 and set *VALUE to (unsigned int) -1.  */
+
+static int
+decode_utf8_char (const unsigned char *p, size_t len, unsigned int *value)
+{
+  unsigned int t = *p;
+
+  if (len == 0)
+    abort ();
+  if (t & 0x80)
+    {
+      size_t utf8_len = 0;
+      unsigned int ch;
+      size_t i;
+      for (t = *p; t & 0x80; t <<= 1)
+       utf8_len++;
+
+      if (utf8_len > len || utf8_len < 2 || utf8_len > 6)
+       {
+         *value = (unsigned int) -1;
+         return 0;
+       }
+      ch = *p & ((1 << (7 - utf8_len)) - 1);
+      for (i = 1; i < utf8_len; i++)
+       {
+         unsigned int u = p[i];
+         if ((u & 0xC0) != 0x80)
+           {
+             *value = (unsigned int) -1;
+             return 0;
+           }
+         ch = (ch << 6) | (u & 0x3F);
+       }
+      if (   (ch <=      0x7F && utf8_len > 1)
+         || (ch <=     0x7FF && utf8_len > 2)
+         || (ch <=    0xFFFF && utf8_len > 3)
+         || (ch <=  0x1FFFFF && utf8_len > 4)
+         || (ch <= 0x3FFFFFF && utf8_len > 5)
+         || (ch >= 0xD800 && ch <= 0xDFFF))
+       {
+         *value = (unsigned int) -1;
+         return 0;
+       }
+      *value = ch;
+      return utf8_len;
+    }
+  else
+    {
+      *value = t;
+      return 1;
+    }
+}
+
+/* Allocator for identifier_to_locale and corresponding function to
+   free memory.  */
+
+void *(*identifier_to_locale_alloc) (size_t) = xmalloc;
+void (*identifier_to_locale_free) (void *) = free;
+
+/* Given IDENT, an identifier in the internal encoding, return a
+   version of IDENT suitable for diagnostics in the locale character
+   set: either IDENT itself, or a string, allocated using
+   identifier_to_locale_alloc, converted to the locale character set
+   and using escape sequences if not representable in the locale
+   character set or containing control characters or invalid byte
+   sequences.  Existing backslashes in IDENT are not doubled, so the
+   result may not uniquely specify the contents of an arbitrary byte
+   sequence identifier.  */
+
+const char *
+identifier_to_locale (const char *ident)
+{
+  const unsigned char *uid = (const unsigned char *) ident;
+  size_t idlen = strlen (ident);
+  bool valid_printable_utf8 = true;
+  bool all_ascii = true;
+  size_t i;
+
+  for (i = 0; i < idlen;)
+    {
+      unsigned int c;
+      size_t utf8_len = decode_utf8_char (&uid[i], idlen - i, &c);
+      if (utf8_len == 0 || c <= 0x1F || (c >= 0x7F && c <= 0x9F))
+       {
+         valid_printable_utf8 = false;
+         break;
+       }
+      if (utf8_len > 1)
+       all_ascii = false;
+      i += utf8_len;
+    }
+
+  /* If IDENT contains invalid UTF-8 sequences (which may occur with
+     attributes putting arbitrary byte sequences in identifiers), or
+     control characters, we use octal escape sequences for all bytes
+     outside printable ASCII.  */
+  if (!valid_printable_utf8)
+    {
+      char *ret = (char *) identifier_to_locale_alloc (4 * idlen + 1);
+      char *p = ret;
+      for (i = 0; i < idlen; i++)
+       {
+         if (uid[i] > 0x1F && uid[i] < 0x7F)
+           *p++ = uid[i];
+         else
+           {
+             sprintf (p, "\\%03o", uid[i]);
+             p += 4;
+           }
+       }
+      *p = 0;
+      return ret;
+    }
+
+  /* Otherwise, if it is valid printable ASCII, or printable UTF-8
+     with the locale character set being UTF-8, IDENT is used.  */
+  if (all_ascii || locale_utf8)
+    return ident;
+
+  /* Otherwise IDENT is converted to the locale character set if
+     possible.  */
+#if defined ENABLE_NLS && defined HAVE_LANGINFO_CODESET && HAVE_ICONV
+  if (locale_encoding != NULL)
+    {
+      iconv_t cd = iconv_open (locale_encoding, "UTF-8");
+      bool conversion_ok = true;
+      char *ret = NULL;
+      if (cd != (iconv_t) -1)
+       {
+         size_t ret_alloc = 4 * idlen + 1;
+         for (;;)
+           {
+             /* Repeat the whole conversion process as needed with
+                larger buffers so non-reversible transformations can
+                always be detected.  */
+             ICONV_CONST char *inbuf = CONST_CAST (char *, ident);
+             char *outbuf;
+             size_t inbytesleft = idlen;
+             size_t outbytesleft = ret_alloc - 1;
+             size_t iconv_ret;
+
+             ret = (char *) identifier_to_locale_alloc (ret_alloc);
+             outbuf = ret;
+
+             if (iconv (cd, 0, 0, 0, 0) == (size_t) -1)
+               {
+                 conversion_ok = false;
+                 break;
+               }
+
+             iconv_ret = iconv (cd, &inbuf, &inbytesleft,
+                                &outbuf, &outbytesleft);
+             if (iconv_ret == (size_t) -1 || inbytesleft != 0)
+               {
+                 if (errno == E2BIG)
+                   {
+                     ret_alloc *= 2;
+                     identifier_to_locale_free (ret);
+                     ret = NULL;
+                     continue;
+                   }
+                 else
+                   {
+                     conversion_ok = false;
+                     break;
+                   }
+               }
+             else if (iconv_ret != 0)
+               {
+                 conversion_ok = false;
+                 break;
+               }
+             /* Return to initial shift state.  */
+             if (iconv (cd, 0, 0, &outbuf, &outbytesleft) == (size_t) -1)
+               {
+                 if (errno == E2BIG)
+                   {
+                     ret_alloc *= 2;
+                     identifier_to_locale_free (ret);
+                     ret = NULL;
+                     continue;
+                   }
+                 else
+                   {
+                     conversion_ok = false;
+                     break;
+                   }
+               }
+             *outbuf = 0;
+             break;
+           }
+         iconv_close (cd);
+         if (conversion_ok)
+           return ret;
+       }
+    }
+#endif
+
+  /* Otherwise, convert non-ASCII characters in IDENT to UCNs.  */
+  {
+    char *ret = (char *) identifier_to_locale_alloc (10 * idlen + 1);
+    char *p = ret;
+    for (i = 0; i < idlen;)
+      {
+       unsigned int c;
+       size_t utf8_len = decode_utf8_char (&uid[i], idlen - i, &c);
+       if (utf8_len == 1)
+         *p++ = uid[i];
+       else
+         {
+           sprintf (p, "\\U%08x", c);
+           p += 10;
+         }
+       i += utf8_len;
+      }
+    *p = 0;
+    return ret;
+  }
+}