1 /* Various declarations for language-independent pretty-print subroutines.
2    Copyright (C) 2003-2016 Free Software Foundation, Inc.
3    Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4 
5 This file is part of GCC.
6 
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11 
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20 
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "intl.h"
25 #include "pretty-print.h"
26 #include "diagnostic-color.h"
27 
28 #if HAVE_ICONV
29 #include <iconv.h>
30 #endif
31 
32 /* Overwrite the given location/range within this text_info's rich_location.
33    For use e.g. when implementing "+" in client format decoders.  */
34 
35 void
set_location(unsigned int idx,location_t loc,bool show_caret_p)36 text_info::set_location (unsigned int idx, location_t loc, bool show_caret_p)
37 {
38   gcc_checking_assert (m_richloc);
39   m_richloc->set_range (line_table, idx, loc, show_caret_p);
40 }
41 
42 location_t
get_location(unsigned int index_of_location)43 text_info::get_location (unsigned int index_of_location) const
44 {
45   gcc_checking_assert (m_richloc);
46 
47   if (index_of_location == 0)
48     return m_richloc->get_loc ();
49   else
50     return UNKNOWN_LOCATION;
51 }
52 
53 // Default construct an output buffer.
54 
output_buffer()55 output_buffer::output_buffer ()
56   : formatted_obstack (),
57     chunk_obstack (),
58     obstack (&formatted_obstack),
59     cur_chunk_array (),
60     stream (stderr),
61     line_length (),
62     digit_buffer (),
63     flush_p (true)
64 {
65   obstack_init (&formatted_obstack);
66   obstack_init (&chunk_obstack);
67 }
68 
69 // Release resources owned by an output buffer at the end of lifetime.
70 
~output_buffer()71 output_buffer::~output_buffer ()
72 {
73   obstack_free (&chunk_obstack, NULL);
74   obstack_free (&formatted_obstack, NULL);
75 }
76 
77 
78 /* Format an integer given by va_arg (ARG, type-specifier T) where
79    type-specifier is a precision modifier as indicated by PREC.  F is
80    a string used to construct the appropriate format-specifier.  */
81 #define pp_integer_with_precision(PP, ARG, PREC, T, F)       \
82   do                                                         \
83     switch (PREC)                                            \
84       {                                                      \
85       case 0:                                                \
86         pp_scalar (PP, "%" F, va_arg (ARG, T));              \
87         break;                                               \
88                                                              \
89       case 1:                                                \
90         pp_scalar (PP, "%l" F, va_arg (ARG, long T));        \
91         break;                                               \
92                                                              \
93       case 2:                                                \
94         pp_scalar (PP, "%" HOST_LONG_LONG_FORMAT F, va_arg (ARG, long long T));  \
95         break;                                               \
96                                                              \
97       default:                                               \
98         break;                                               \
99       }                                                      \
100   while (0)
101 
102 
103 /* Subroutine of pp_set_maximum_length.  Set up PRETTY-PRINTER's
104    internal maximum characters per line.  */
105 static void
pp_set_real_maximum_length(pretty_printer * pp)106 pp_set_real_maximum_length (pretty_printer *pp)
107 {
108   /* If we're told not to wrap lines then do the obvious thing.  In case
109      we'll emit prefix only once per message, it is appropriate
110      not to increase unnecessarily the line-length cut-off.  */
111   if (!pp_is_wrapping_line (pp)
112       || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_ONCE
113       || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
114     pp->maximum_length = pp_line_cutoff (pp);
115   else
116     {
117       int prefix_length = pp->prefix ? strlen (pp->prefix) : 0;
118       /* If the prefix is ridiculously too long, output at least
119          32 characters.  */
120       if (pp_line_cutoff (pp) - prefix_length < 32)
121 	pp->maximum_length = pp_line_cutoff (pp) + 32;
122       else
123 	pp->maximum_length = pp_line_cutoff (pp);
124     }
125 }
126 
127 /* Clear PRETTY-PRINTER's output state.  */
128 static inline void
pp_clear_state(pretty_printer * pp)129 pp_clear_state (pretty_printer *pp)
130 {
131   pp->emitted_prefix = false;
132   pp_indentation (pp) = 0;
133 }
134 
135 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream.  */
136 void
pp_write_text_to_stream(pretty_printer * pp)137 pp_write_text_to_stream (pretty_printer *pp)
138 {
139   const char *text = pp_formatted_text (pp);
140   fputs (text, pp_buffer (pp)->stream);
141   pp_clear_output_area (pp);
142 }
143 
144 /* As pp_write_text_to_stream, but for GraphViz label output.
145 
146    Flush the formatted text of pretty-printer PP onto the attached stream.
147    Replace characters in PPF that have special meaning in a GraphViz .dot
148    file.
149 
150    This routine is not very fast, but it doesn't have to be as this is only
151    be used by routines dumping intermediate representations in graph form.  */
152 
153 void
pp_write_text_as_dot_label_to_stream(pretty_printer * pp,bool for_record)154 pp_write_text_as_dot_label_to_stream (pretty_printer *pp, bool for_record)
155 {
156   const char *text = pp_formatted_text (pp);
157   const char *p = text;
158   FILE *fp = pp_buffer (pp)->stream;
159 
160   while (*p)
161     {
162       switch (*p)
163 	{
164 	/* Print newlines as a left-aligned newline.  */
165 	case '\n':
166 	  fputs ("\\l\\\n", fp);
167 	  break;
168 
169 	/* A pipe is only special for record-shape nodes.  */
170 	case '|':
171 	  if (for_record)
172 	    fputc ('\\', fp);
173 	  fputc (*p, fp);
174 	  break;
175 
176 	/* The following characters always have to be escaped
177 	   for use in labels.  */
178 	case '{':
179 	case '}':
180 	case '<':
181 	case '>':
182 	case '"':
183 	case ' ':
184 	  fputc ('\\', fp);
185 	  /* fall through */
186 	default:
187 	  fputc (*p, fp);
188 	  break;
189 	}
190       p++;
191     }
192 
193   pp_clear_output_area (pp);
194 }
195 
196 /* Wrap a text delimited by START and END into PRETTY-PRINTER.  */
197 static void
pp_wrap_text(pretty_printer * pp,const char * start,const char * end)198 pp_wrap_text (pretty_printer *pp, const char *start, const char *end)
199 {
200   bool wrapping_line = pp_is_wrapping_line (pp);
201 
202   while (start != end)
203     {
204       /* Dump anything bordered by whitespaces.  */
205       {
206 	const char *p = start;
207 	while (p != end && !ISBLANK (*p) && *p != '\n')
208 	  ++p;
209 	if (wrapping_line
210             && p - start >= pp_remaining_character_count_for_line (pp))
211 	  pp_newline (pp);
212 	pp_append_text (pp, start, p);
213 	start = p;
214       }
215 
216       if (start != end && ISBLANK (*start))
217 	{
218 	  pp_space (pp);
219 	  ++start;
220 	}
221       if (start != end && *start == '\n')
222 	{
223 	  pp_newline (pp);
224 	  ++start;
225 	}
226     }
227 }
228 
229 /* Same as pp_wrap_text but wrap text only when in line-wrapping mode.  */
230 static inline void
pp_maybe_wrap_text(pretty_printer * pp,const char * start,const char * end)231 pp_maybe_wrap_text (pretty_printer *pp, const char *start, const char *end)
232 {
233   if (pp_is_wrapping_line (pp))
234     pp_wrap_text (pp, start, end);
235   else
236     pp_append_text (pp, start, end);
237 }
238 
239 /* Append to the output area of PRETTY-PRINTER a string specified by its
240    STARTing character and LENGTH.  */
241 static inline void
pp_append_r(pretty_printer * pp,const char * start,int length)242 pp_append_r (pretty_printer *pp, const char *start, int length)
243 {
244   output_buffer_append_r (pp_buffer (pp), start, length);
245 }
246 
247 /* Insert enough spaces into the output area of PRETTY-PRINTER to bring
248    the column position to the current indentation level, assuming that a
249    newline has just been written to the buffer.  */
250 void
pp_indent(pretty_printer * pp)251 pp_indent (pretty_printer *pp)
252 {
253   int n = pp_indentation (pp);
254   int i;
255 
256   for (i = 0; i < n; ++i)
257     pp_space (pp);
258 }
259 
260 /* The following format specifiers are recognized as being client independent:
261    %d, %i: (signed) integer in base ten.
262    %u: unsigned integer in base ten.
263    %o: unsigned integer in base eight.
264    %x: unsigned integer in base sixteen.
265    %ld, %li, %lo, %lu, %lx: long versions of the above.
266    %lld, %lli, %llo, %llu, %llx: long long versions.
267    %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
268    %c: character.
269    %s: string.
270    %p: pointer.
271    %r: if pp_show_color(pp), switch to color identified by const char *.
272    %R: if pp_show_color(pp), reset color.
273    %m: strerror(text->err_no) - does not consume a value from args_ptr.
274    %%: '%'.
275    %<: opening quote.
276    %>: closing quote.
277    %': apostrophe (should only be used in untranslated messages;
278        translations should use appropriate punctuation directly).
279    %.*s: a substring the length of which is specified by an argument
280 	 integer.
281    %Ns: likewise, but length specified as constant in the format string.
282    Flag 'q': quote formatted text (must come immediately after '%').
283 
284    Arguments can be used sequentially, or through %N$ resp. *N$
285    notation Nth argument after the format string.  If %N$ / *N$
286    notation is used, it must be used for all arguments, except %m, %%,
287    %<, %> and %', which may not have a number, as they do not consume
288    an argument.  When %M$.*N$s is used, M must be N + 1.  (This may
289    also be written %M$.*s, provided N is not otherwise used.)  The
290    format string must have conversion specifiers with argument numbers
291    1 up to highest argument; each argument may only be used once.
292    A format string can have at most 30 arguments.  */
293 
294 /* Formatting phases 1 and 2: render TEXT->format_spec plus
295    TEXT->args_ptr into a series of chunks in pp_buffer (PP)->args[].
296    Phase 3 is in pp_format_text.  */
297 
298 void
pp_format(pretty_printer * pp,text_info * text)299 pp_format (pretty_printer *pp, text_info *text)
300 {
301   output_buffer *buffer = pp_buffer (pp);
302   const char *p;
303   const char **args;
304   struct chunk_info *new_chunk_array;
305 
306   unsigned int curarg = 0, chunk = 0, argno;
307   pp_wrapping_mode_t old_wrapping_mode;
308   bool any_unnumbered = false, any_numbered = false;
309   const char **formatters[PP_NL_ARGMAX];
310 
311   /* Allocate a new chunk structure.  */
312   new_chunk_array = XOBNEW (&buffer->chunk_obstack, struct chunk_info);
313   new_chunk_array->prev = buffer->cur_chunk_array;
314   buffer->cur_chunk_array = new_chunk_array;
315   args = new_chunk_array->args;
316 
317   /* Formatting phase 1: split up TEXT->format_spec into chunks in
318      pp_buffer (PP)->args[].  Even-numbered chunks are to be output
319      verbatim, odd-numbered chunks are format specifiers.
320      %m, %%, %<, %>, and %' are replaced with the appropriate text at
321      this point.  */
322 
323   memset (formatters, 0, sizeof formatters);
324 
325   for (p = text->format_spec; *p; )
326     {
327       while (*p != '\0' && *p != '%')
328 	{
329 	  obstack_1grow (&buffer->chunk_obstack, *p);
330 	  p++;
331 	}
332 
333       if (*p == '\0')
334 	break;
335 
336       switch (*++p)
337 	{
338 	case '\0':
339 	  gcc_unreachable ();
340 
341 	case '%':
342 	  obstack_1grow (&buffer->chunk_obstack, '%');
343 	  p++;
344 	  continue;
345 
346 	case '<':
347 	  {
348 	    obstack_grow (&buffer->chunk_obstack,
349 			  open_quote, strlen (open_quote));
350 	    const char *colorstr
351 	      = colorize_start (pp_show_color (pp), "quote");
352 	    obstack_grow (&buffer->chunk_obstack, colorstr, strlen (colorstr));
353 	    p++;
354 	    continue;
355 	  }
356 
357 	case '>':
358 	  {
359 	    const char *colorstr = colorize_stop (pp_show_color (pp));
360 	    obstack_grow (&buffer->chunk_obstack, colorstr, strlen (colorstr));
361 	  }
362 	  /* FALLTHRU */
363 	case '\'':
364 	  obstack_grow (&buffer->chunk_obstack,
365 			close_quote, strlen (close_quote));
366 	  p++;
367 	  continue;
368 
369 	case 'R':
370 	  {
371 	    const char *colorstr = colorize_stop (pp_show_color (pp));
372 	    obstack_grow (&buffer->chunk_obstack, colorstr,
373 			  strlen (colorstr));
374 	    p++;
375 	    continue;
376 	  }
377 
378 	case 'm':
379 	  {
380 	    const char *errstr = xstrerror (text->err_no);
381 	    obstack_grow (&buffer->chunk_obstack, errstr, strlen (errstr));
382 	  }
383 	  p++;
384 	  continue;
385 
386 	default:
387 	  /* Handled in phase 2.  Terminate the plain chunk here.  */
388 	  obstack_1grow (&buffer->chunk_obstack, '\0');
389 	  gcc_assert (chunk < PP_NL_ARGMAX * 2);
390 	  args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
391 	  break;
392 	}
393 
394       if (ISDIGIT (*p))
395 	{
396 	  char *end;
397 	  argno = strtoul (p, &end, 10) - 1;
398 	  p = end;
399 	  gcc_assert (*p == '$');
400 	  p++;
401 
402 	  any_numbered = true;
403 	  gcc_assert (!any_unnumbered);
404 	}
405       else
406 	{
407 	  argno = curarg++;
408 	  any_unnumbered = true;
409 	  gcc_assert (!any_numbered);
410 	}
411       gcc_assert (argno < PP_NL_ARGMAX);
412       gcc_assert (!formatters[argno]);
413       formatters[argno] = &args[chunk];
414       do
415 	{
416 	  obstack_1grow (&buffer->chunk_obstack, *p);
417 	  p++;
418 	}
419       while (strchr ("qwl+#", p[-1]));
420 
421       if (p[-1] == '.')
422 	{
423 	  /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
424 	     (where M == N + 1).  */
425 	  if (ISDIGIT (*p))
426 	    {
427 	      do
428 		{
429 		  obstack_1grow (&buffer->chunk_obstack, *p);
430 		  p++;
431 		}
432 	      while (ISDIGIT (p[-1]));
433 	      gcc_assert (p[-1] == 's');
434 	    }
435 	  else
436 	    {
437 	      gcc_assert (*p == '*');
438 	      obstack_1grow (&buffer->chunk_obstack, '*');
439 	      p++;
440 
441 	      if (ISDIGIT (*p))
442 		{
443 		  char *end;
444 		  unsigned int argno2 = strtoul (p, &end, 10) - 1;
445 		  p = end;
446 		  gcc_assert (argno2 == argno - 1);
447 		  gcc_assert (!any_unnumbered);
448 		  gcc_assert (*p == '$');
449 
450 		  p++;
451 		  formatters[argno2] = formatters[argno];
452 		}
453 	      else
454 		{
455 		  gcc_assert (!any_numbered);
456 		  formatters[argno+1] = formatters[argno];
457 		  curarg++;
458 		}
459 	      gcc_assert (*p == 's');
460 	      obstack_1grow (&buffer->chunk_obstack, 's');
461 	      p++;
462 	    }
463 	}
464       if (*p == '\0')
465 	break;
466 
467       obstack_1grow (&buffer->chunk_obstack, '\0');
468       gcc_assert (chunk < PP_NL_ARGMAX * 2);
469       args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
470     }
471 
472   obstack_1grow (&buffer->chunk_obstack, '\0');
473   gcc_assert (chunk < PP_NL_ARGMAX * 2);
474   args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
475   args[chunk] = 0;
476 
477   /* Set output to the argument obstack, and switch line-wrapping and
478      prefixing off.  */
479   buffer->obstack = &buffer->chunk_obstack;
480   old_wrapping_mode = pp_set_verbatim_wrapping (pp);
481 
482   /* Second phase.  Replace each formatter with the formatted text it
483      corresponds to.  */
484 
485   for (argno = 0; formatters[argno]; argno++)
486     {
487       int precision = 0;
488       bool wide = false;
489       bool plus = false;
490       bool hash = false;
491       bool quote = false;
492 
493       /* We do not attempt to enforce any ordering on the modifier
494 	 characters.  */
495 
496       for (p = *formatters[argno];; p++)
497 	{
498 	  switch (*p)
499 	    {
500 	    case 'q':
501 	      gcc_assert (!quote);
502 	      quote = true;
503 	      continue;
504 
505 	    case '+':
506 	      gcc_assert (!plus);
507 	      plus = true;
508 	      continue;
509 
510 	    case '#':
511 	      gcc_assert (!hash);
512 	      hash = true;
513 	      continue;
514 
515 	    case 'w':
516 	      gcc_assert (!wide);
517 	      wide = true;
518 	      continue;
519 
520 	    case 'l':
521 	      /* We don't support precision beyond that of "long long".  */
522 	      gcc_assert (precision < 2);
523 	      precision++;
524 	      continue;
525 	    }
526 	  break;
527 	}
528 
529       gcc_assert (!wide || precision == 0);
530 
531       if (quote)
532 	{
533 	  pp_string (pp, open_quote);
534 	  pp_string (pp, colorize_start (pp_show_color (pp), "quote"));
535 	}
536 
537       switch (*p)
538 	{
539 	case 'r':
540 	  pp_string (pp, colorize_start (pp_show_color (pp),
541 					 va_arg (*text->args_ptr,
542 						 const char *)));
543 	  break;
544 
545 	case 'c':
546 	  pp_character (pp, va_arg (*text->args_ptr, int));
547 	  break;
548 
549 	case 'd':
550 	case 'i':
551 	  if (wide)
552 	    pp_wide_integer (pp, va_arg (*text->args_ptr, HOST_WIDE_INT));
553 	  else
554 	    pp_integer_with_precision
555 	      (pp, *text->args_ptr, precision, int, "d");
556 	  break;
557 
558 	case 'o':
559 	  if (wide)
560 	    pp_scalar (pp, "%" HOST_WIDE_INT_PRINT "o",
561 		       va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
562 	  else
563 	    pp_integer_with_precision
564 	      (pp, *text->args_ptr, precision, unsigned, "o");
565 	  break;
566 
567 	case 's':
568 	  pp_string (pp, va_arg (*text->args_ptr, const char *));
569 	  break;
570 
571 	case 'p':
572 	  pp_pointer (pp, va_arg (*text->args_ptr, void *));
573 	  break;
574 
575 	case 'u':
576 	  if (wide)
577 	    pp_scalar (pp, HOST_WIDE_INT_PRINT_UNSIGNED,
578 		       va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
579 	  else
580 	    pp_integer_with_precision
581 	      (pp, *text->args_ptr, precision, unsigned, "u");
582 	  break;
583 
584 	case 'x':
585 	  if (wide)
586 	    pp_scalar (pp, HOST_WIDE_INT_PRINT_HEX,
587 		       va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
588 	  else
589 	    pp_integer_with_precision
590 	      (pp, *text->args_ptr, precision, unsigned, "x");
591 	  break;
592 
593 	case '.':
594 	  {
595 	    int n;
596 	    const char *s;
597 
598 	    /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
599 	       (where M == N + 1).  The format string should be verified
600 	       already from the first phase.  */
601 	    p++;
602 	    if (ISDIGIT (*p))
603 	      {
604 		char *end;
605 		n = strtoul (p, &end, 10);
606 		p = end;
607 		gcc_assert (*p == 's');
608 	      }
609 	    else
610 	      {
611 		gcc_assert (*p == '*');
612 		p++;
613 		gcc_assert (*p == 's');
614 		n = va_arg (*text->args_ptr, int);
615 
616 		/* This consumes a second entry in the formatters array.  */
617 		gcc_assert (formatters[argno] == formatters[argno+1]);
618 		argno++;
619 	      }
620 
621 	    s = va_arg (*text->args_ptr, const char *);
622 	    pp_append_text (pp, s, s + n);
623 	  }
624 	  break;
625 
626 	default:
627 	  {
628 	    bool ok;
629 
630 	    gcc_assert (pp_format_decoder (pp));
631 	    ok = pp_format_decoder (pp) (pp, text, p,
632 					 precision, wide, plus, hash);
633 	    gcc_assert (ok);
634 	  }
635 	}
636 
637       if (quote)
638 	{
639 	  pp_string (pp, colorize_stop (pp_show_color (pp)));
640 	  pp_string (pp, close_quote);
641 	}
642 
643       obstack_1grow (&buffer->chunk_obstack, '\0');
644       *formatters[argno] = XOBFINISH (&buffer->chunk_obstack, const char *);
645     }
646 
647   if (CHECKING_P)
648     for (; argno < PP_NL_ARGMAX; argno++)
649       gcc_assert (!formatters[argno]);
650 
651   /* Revert to normal obstack and wrapping mode.  */
652   buffer->obstack = &buffer->formatted_obstack;
653   buffer->line_length = 0;
654   pp_wrapping_mode (pp) = old_wrapping_mode;
655   pp_clear_state (pp);
656 }
657 
658 /* Format of a message pointed to by TEXT.  */
659 void
pp_output_formatted_text(pretty_printer * pp)660 pp_output_formatted_text (pretty_printer *pp)
661 {
662   unsigned int chunk;
663   output_buffer *buffer = pp_buffer (pp);
664   struct chunk_info *chunk_array = buffer->cur_chunk_array;
665   const char **args = chunk_array->args;
666 
667   gcc_assert (buffer->obstack == &buffer->formatted_obstack);
668   gcc_assert (buffer->line_length == 0);
669 
670   /* This is a third phase, first 2 phases done in pp_format_args.
671      Now we actually print it.  */
672   for (chunk = 0; args[chunk]; chunk++)
673     pp_string (pp, args[chunk]);
674 
675   /* Deallocate the chunk structure and everything after it (i.e. the
676      associated series of formatted strings).  */
677   buffer->cur_chunk_array = chunk_array->prev;
678   obstack_free (&buffer->chunk_obstack, chunk_array);
679 }
680 
681 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
682    settings needed by BUFFER for a verbatim formatting.  */
683 void
pp_format_verbatim(pretty_printer * pp,text_info * text)684 pp_format_verbatim (pretty_printer *pp, text_info *text)
685 {
686   /* Set verbatim mode.  */
687   pp_wrapping_mode_t oldmode = pp_set_verbatim_wrapping (pp);
688 
689   /* Do the actual formatting.  */
690   pp_format (pp, text);
691   pp_output_formatted_text (pp);
692 
693   /* Restore previous settings.  */
694   pp_wrapping_mode (pp) = oldmode;
695 }
696 
697 /* Flush the content of BUFFER onto the attached stream.  This
698    function does nothing unless pp->output_buffer->flush_p.  */
699 void
pp_flush(pretty_printer * pp)700 pp_flush (pretty_printer *pp)
701 {
702   pp_clear_state (pp);
703   if (!pp->buffer->flush_p)
704     return;
705   pp_write_text_to_stream (pp);
706   fflush (pp_buffer (pp)->stream);
707 }
708 
709 /* Flush the content of BUFFER onto the attached stream independently
710    of the value of pp->output_buffer->flush_p.  */
711 void
pp_really_flush(pretty_printer * pp)712 pp_really_flush (pretty_printer *pp)
713 {
714   pp_clear_state (pp);
715   pp_write_text_to_stream (pp);
716   fflush (pp_buffer (pp)->stream);
717 }
718 
719 /* Sets the number of maximum characters per line PRETTY-PRINTER can
720    output in line-wrapping mode.  A LENGTH value 0 suppresses
721    line-wrapping.  */
722 void
pp_set_line_maximum_length(pretty_printer * pp,int length)723 pp_set_line_maximum_length (pretty_printer *pp, int length)
724 {
725   pp_line_cutoff (pp) = length;
726   pp_set_real_maximum_length (pp);
727 }
728 
729 /* Clear PRETTY-PRINTER output area text info.  */
730 void
pp_clear_output_area(pretty_printer * pp)731 pp_clear_output_area (pretty_printer *pp)
732 {
733   obstack_free (pp_buffer (pp)->obstack,
734                 obstack_base (pp_buffer (pp)->obstack));
735   pp_buffer (pp)->line_length = 0;
736 }
737 
738 /* Set PREFIX for PRETTY-PRINTER.  */
739 void
pp_set_prefix(pretty_printer * pp,const char * prefix)740 pp_set_prefix (pretty_printer *pp, const char *prefix)
741 {
742   pp->prefix = prefix;
743   pp_set_real_maximum_length (pp);
744   pp->emitted_prefix = false;
745   pp_indentation (pp) = 0;
746 }
747 
748 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string.  */
749 void
pp_destroy_prefix(pretty_printer * pp)750 pp_destroy_prefix (pretty_printer *pp)
751 {
752   if (pp->prefix != NULL)
753     {
754       free (CONST_CAST (char *, pp->prefix));
755       pp->prefix = NULL;
756     }
757 }
758 
759 /* Write out PRETTY-PRINTER's prefix.  */
760 void
pp_emit_prefix(pretty_printer * pp)761 pp_emit_prefix (pretty_printer *pp)
762 {
763   if (pp->prefix != NULL)
764     {
765       switch (pp_prefixing_rule (pp))
766 	{
767 	default:
768 	case DIAGNOSTICS_SHOW_PREFIX_NEVER:
769 	  break;
770 
771 	case DIAGNOSTICS_SHOW_PREFIX_ONCE:
772 	  if (pp->emitted_prefix)
773 	    {
774 	      pp_indent (pp);
775 	      break;
776 	    }
777 	  pp_indentation (pp) += 3;
778 	  /* Fall through.  */
779 
780 	case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
781 	  {
782 	    int prefix_length = strlen (pp->prefix);
783 	    pp_append_r (pp, pp->prefix, prefix_length);
784 	    pp->emitted_prefix = true;
785 	  }
786 	  break;
787 	}
788     }
789 }
790 
791 /* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
792    characters per line.  */
793 
pretty_printer(const char * p,int l)794 pretty_printer::pretty_printer (const char *p, int l)
795   : buffer (new (XCNEW (output_buffer)) output_buffer ()),
796     prefix (),
797     padding (pp_none),
798     maximum_length (),
799     indent_skip (),
800     wrapping (),
801     format_decoder (),
802     emitted_prefix (),
803     need_newline (),
804     translate_identifiers (true),
805     show_color ()
806 {
807   pp_line_cutoff (this) = l;
808   /* By default, we emit prefixes once per message.  */
809   pp_prefixing_rule (this) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
810   pp_set_prefix (this, p);
811 }
812 
~pretty_printer()813 pretty_printer::~pretty_printer ()
814 {
815   buffer->~output_buffer ();
816   XDELETE (buffer);
817 }
818 
819 /* Append a string delimited by START and END to the output area of
820    PRETTY-PRINTER.  No line wrapping is done.  However, if beginning a
821    new line then emit PRETTY-PRINTER's prefix and skip any leading
822    whitespace if appropriate.  The caller must ensure that it is
823    safe to do so.  */
824 void
pp_append_text(pretty_printer * pp,const char * start,const char * end)825 pp_append_text (pretty_printer *pp, const char *start, const char *end)
826 {
827   /* Emit prefix and skip whitespace if we're starting a new line.  */
828   if (pp_buffer (pp)->line_length == 0)
829     {
830       pp_emit_prefix (pp);
831       if (pp_is_wrapping_line (pp))
832 	while (start != end && *start == ' ')
833 	  ++start;
834     }
835   pp_append_r (pp, start, end - start);
836 }
837 
838 /* Finishes constructing a NULL-terminated character string representing
839    the PRETTY-PRINTED text.  */
840 const char *
pp_formatted_text(pretty_printer * pp)841 pp_formatted_text (pretty_printer *pp)
842 {
843   return output_buffer_formatted_text (pp_buffer (pp));
844 }
845 
846 /*  Return a pointer to the last character emitted in PRETTY-PRINTER's
847     output area.  A NULL pointer means no character available.  */
848 const char *
pp_last_position_in_text(const pretty_printer * pp)849 pp_last_position_in_text (const pretty_printer *pp)
850 {
851   return output_buffer_last_position_in_text (pp_buffer (pp));
852 }
853 
854 /* Return the amount of characters PRETTY-PRINTER can accept to
855    make a full line.  Meaningful only in line-wrapping mode.  */
856 int
pp_remaining_character_count_for_line(pretty_printer * pp)857 pp_remaining_character_count_for_line (pretty_printer *pp)
858 {
859   return pp->maximum_length - pp_buffer (pp)->line_length;
860 }
861 
862 
863 /* Format a message into BUFFER a la printf.  */
864 void
pp_printf(pretty_printer * pp,const char * msg,...)865 pp_printf (pretty_printer *pp, const char *msg, ...)
866 {
867   text_info text;
868   va_list ap;
869 
870   va_start (ap, msg);
871   text.err_no = errno;
872   text.args_ptr = &ap;
873   text.format_spec = msg;
874   pp_format (pp, &text);
875   pp_output_formatted_text (pp);
876   va_end (ap);
877 }
878 
879 
880 /* Output MESSAGE verbatim into BUFFER.  */
881 void
pp_verbatim(pretty_printer * pp,const char * msg,...)882 pp_verbatim (pretty_printer *pp, const char *msg, ...)
883 {
884   text_info text;
885   va_list ap;
886 
887   va_start (ap, msg);
888   text.err_no = errno;
889   text.args_ptr = &ap;
890   text.format_spec = msg;
891   pp_format_verbatim (pp, &text);
892   va_end (ap);
893 }
894 
895 
896 
897 /* Have PRETTY-PRINTER start a new line.  */
898 void
pp_newline(pretty_printer * pp)899 pp_newline (pretty_printer *pp)
900 {
901   obstack_1grow (pp_buffer (pp)->obstack, '\n');
902   pp_needs_newline (pp) = false;
903   pp_buffer (pp)->line_length = 0;
904 }
905 
906 /* Have PRETTY-PRINTER add a CHARACTER.  */
907 void
pp_character(pretty_printer * pp,int c)908 pp_character (pretty_printer *pp, int c)
909 {
910   if (pp_is_wrapping_line (pp)
911       && pp_remaining_character_count_for_line (pp) <= 0)
912     {
913       pp_newline (pp);
914       if (ISSPACE (c))
915         return;
916     }
917   obstack_1grow (pp_buffer (pp)->obstack, c);
918   ++pp_buffer (pp)->line_length;
919 }
920 
921 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
922    be line-wrapped if in appropriate mode.  */
923 void
pp_string(pretty_printer * pp,const char * str)924 pp_string (pretty_printer *pp, const char *str)
925 {
926   gcc_checking_assert (str);
927   pp_maybe_wrap_text (pp, str, str + strlen (str));
928 }
929 
930 /* Maybe print out a whitespace if needed.  */
931 
932 void
pp_maybe_space(pretty_printer * pp)933 pp_maybe_space (pretty_printer *pp)
934 {
935   if (pp->padding != pp_none)
936     {
937       pp_space (pp);
938       pp->padding = pp_none;
939     }
940 }
941 
942 // Add a newline to the pretty printer PP and flush formatted text.
943 
944 void
pp_newline_and_flush(pretty_printer * pp)945 pp_newline_and_flush (pretty_printer *pp)
946 {
947   pp_newline (pp);
948   pp_flush (pp);
949   pp_needs_newline (pp) = false;
950 }
951 
952 // Add a newline to the pretty printer PP, followed by indentation.
953 
954 void
pp_newline_and_indent(pretty_printer * pp,int n)955 pp_newline_and_indent (pretty_printer *pp, int n)
956 {
957   pp_indentation (pp) += n;
958   pp_newline (pp);
959   pp_indent (pp);
960   pp_needs_newline (pp) = false;
961 }
962 
963 // Add separator C, followed by a single whitespace.
964 
965 void
pp_separate_with(pretty_printer * pp,char c)966 pp_separate_with (pretty_printer *pp, char c)
967 {
968   pp_character (pp, c);
969   pp_space (pp);
970 }
971 
972 
973 /* The string starting at P has LEN (at least 1) bytes left; if they
974    start with a valid UTF-8 sequence, return the length of that
975    sequence and set *VALUE to the value of that sequence, and
976    otherwise return 0 and set *VALUE to (unsigned int) -1.  */
977 
978 static int
decode_utf8_char(const unsigned char * p,size_t len,unsigned int * value)979 decode_utf8_char (const unsigned char *p, size_t len, unsigned int *value)
980 {
981   unsigned int t = *p;
982 
983   if (len == 0)
984     abort ();
985   if (t & 0x80)
986     {
987       size_t utf8_len = 0;
988       unsigned int ch;
989       size_t i;
990       for (t = *p; t & 0x80; t <<= 1)
991 	utf8_len++;
992 
993       if (utf8_len > len || utf8_len < 2 || utf8_len > 6)
994 	{
995 	  *value = (unsigned int) -1;
996 	  return 0;
997 	}
998       ch = *p & ((1 << (7 - utf8_len)) - 1);
999       for (i = 1; i < utf8_len; i++)
1000 	{
1001 	  unsigned int u = p[i];
1002 	  if ((u & 0xC0) != 0x80)
1003 	    {
1004 	      *value = (unsigned int) -1;
1005 	      return 0;
1006 	    }
1007 	  ch = (ch << 6) | (u & 0x3F);
1008 	}
1009       if (   (ch <=      0x7F && utf8_len > 1)
1010 	  || (ch <=     0x7FF && utf8_len > 2)
1011 	  || (ch <=    0xFFFF && utf8_len > 3)
1012 	  || (ch <=  0x1FFFFF && utf8_len > 4)
1013 	  || (ch <= 0x3FFFFFF && utf8_len > 5)
1014 	  || (ch >= 0xD800 && ch <= 0xDFFF))
1015 	{
1016 	  *value = (unsigned int) -1;
1017 	  return 0;
1018 	}
1019       *value = ch;
1020       return utf8_len;
1021     }
1022   else
1023     {
1024       *value = t;
1025       return 1;
1026     }
1027 }
1028 
1029 /* Allocator for identifier_to_locale and corresponding function to
1030    free memory.  */
1031 
1032 void *(*identifier_to_locale_alloc) (size_t) = xmalloc;
1033 void (*identifier_to_locale_free) (void *) = free;
1034 
1035 /* Given IDENT, an identifier in the internal encoding, return a
1036    version of IDENT suitable for diagnostics in the locale character
1037    set: either IDENT itself, or a string, allocated using
1038    identifier_to_locale_alloc, converted to the locale character set
1039    and using escape sequences if not representable in the locale
1040    character set or containing control characters or invalid byte
1041    sequences.  Existing backslashes in IDENT are not doubled, so the
1042    result may not uniquely specify the contents of an arbitrary byte
1043    sequence identifier.  */
1044 
1045 const char *
identifier_to_locale(const char * ident)1046 identifier_to_locale (const char *ident)
1047 {
1048   const unsigned char *uid = (const unsigned char *) ident;
1049   size_t idlen = strlen (ident);
1050   bool valid_printable_utf8 = true;
1051   bool all_ascii = true;
1052   size_t i;
1053 
1054   for (i = 0; i < idlen;)
1055     {
1056       unsigned int c;
1057       size_t utf8_len = decode_utf8_char (&uid[i], idlen - i, &c);
1058       if (utf8_len == 0 || c <= 0x1F || (c >= 0x7F && c <= 0x9F))
1059 	{
1060 	  valid_printable_utf8 = false;
1061 	  break;
1062 	}
1063       if (utf8_len > 1)
1064 	all_ascii = false;
1065       i += utf8_len;
1066     }
1067 
1068   /* If IDENT contains invalid UTF-8 sequences (which may occur with
1069      attributes putting arbitrary byte sequences in identifiers), or
1070      control characters, we use octal escape sequences for all bytes
1071      outside printable ASCII.  */
1072   if (!valid_printable_utf8)
1073     {
1074       char *ret = (char *) identifier_to_locale_alloc (4 * idlen + 1);
1075       char *p = ret;
1076       for (i = 0; i < idlen; i++)
1077 	{
1078 	  if (uid[i] > 0x1F && uid[i] < 0x7F)
1079 	    *p++ = uid[i];
1080 	  else
1081 	    {
1082 	      sprintf (p, "\\%03o", uid[i]);
1083 	      p += 4;
1084 	    }
1085 	}
1086       *p = 0;
1087       return ret;
1088     }
1089 
1090   /* Otherwise, if it is valid printable ASCII, or printable UTF-8
1091      with the locale character set being UTF-8, IDENT is used.  */
1092   if (all_ascii || locale_utf8)
1093     return ident;
1094 
1095   /* Otherwise IDENT is converted to the locale character set if
1096      possible.  */
1097 #if defined ENABLE_NLS && defined HAVE_LANGINFO_CODESET && HAVE_ICONV
1098   if (locale_encoding != NULL)
1099     {
1100       iconv_t cd = iconv_open (locale_encoding, "UTF-8");
1101       bool conversion_ok = true;
1102       char *ret = NULL;
1103       if (cd != (iconv_t) -1)
1104 	{
1105 	  size_t ret_alloc = 4 * idlen + 1;
1106 	  for (;;)
1107 	    {
1108 	      /* Repeat the whole conversion process as needed with
1109 		 larger buffers so non-reversible transformations can
1110 		 always be detected.  */
1111 	      ICONV_CONST char *inbuf = CONST_CAST (char *, ident);
1112 	      char *outbuf;
1113 	      size_t inbytesleft = idlen;
1114 	      size_t outbytesleft = ret_alloc - 1;
1115 	      size_t iconv_ret;
1116 
1117 	      ret = (char *) identifier_to_locale_alloc (ret_alloc);
1118 	      outbuf = ret;
1119 
1120 	      if (iconv (cd, 0, 0, 0, 0) == (size_t) -1)
1121 		{
1122 		  conversion_ok = false;
1123 		  break;
1124 		}
1125 
1126 	      iconv_ret = iconv (cd, &inbuf, &inbytesleft,
1127 				 &outbuf, &outbytesleft);
1128 	      if (iconv_ret == (size_t) -1 || inbytesleft != 0)
1129 		{
1130 		  if (errno == E2BIG)
1131 		    {
1132 		      ret_alloc *= 2;
1133 		      identifier_to_locale_free (ret);
1134 		      ret = NULL;
1135 		      continue;
1136 		    }
1137 		  else
1138 		    {
1139 		      conversion_ok = false;
1140 		      break;
1141 		    }
1142 		}
1143 	      else if (iconv_ret != 0)
1144 		{
1145 		  conversion_ok = false;
1146 		  break;
1147 		}
1148 	      /* Return to initial shift state.  */
1149 	      if (iconv (cd, 0, 0, &outbuf, &outbytesleft) == (size_t) -1)
1150 		{
1151 		  if (errno == E2BIG)
1152 		    {
1153 		      ret_alloc *= 2;
1154 		      identifier_to_locale_free (ret);
1155 		      ret = NULL;
1156 		      continue;
1157 		    }
1158 		  else
1159 		    {
1160 		      conversion_ok = false;
1161 		      break;
1162 		    }
1163 		}
1164 	      *outbuf = 0;
1165 	      break;
1166 	    }
1167 	  iconv_close (cd);
1168 	  if (conversion_ok)
1169 	    return ret;
1170 	}
1171     }
1172 #endif
1173 
1174   /* Otherwise, convert non-ASCII characters in IDENT to UCNs.  */
1175   {
1176     char *ret = (char *) identifier_to_locale_alloc (10 * idlen + 1);
1177     char *p = ret;
1178     for (i = 0; i < idlen;)
1179       {
1180 	unsigned int c;
1181 	size_t utf8_len = decode_utf8_char (&uid[i], idlen - i, &c);
1182 	if (utf8_len == 1)
1183 	  *p++ = uid[i];
1184 	else
1185 	  {
1186 	    sprintf (p, "\\U%08x", c);
1187 	    p += 10;
1188 	  }
1189 	i += utf8_len;
1190       }
1191     *p = 0;
1192     return ret;
1193   }
1194 }
1195