Daily bump.
[official-gcc.git] / gcc / pretty-print.c
blob570dec77dc1b9df56bddb320c543ab2201c9798c
1 /* Various declarations for language-independent pretty-print subroutines.
2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
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.
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.
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/>. */
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 #include "selftest.h"
29 #if HAVE_ICONV
30 #include <iconv.h>
31 #endif
33 static void pp_quoted_string (pretty_printer *, const char *, size_t = -1);
35 /* Overwrite the given location/range within this text_info's rich_location.
36 For use e.g. when implementing "+" in client format decoders. */
38 void
39 text_info::set_location (unsigned int idx, location_t loc, bool show_caret_p)
41 gcc_checking_assert (m_richloc);
42 m_richloc->set_range (line_table, idx, loc, show_caret_p);
45 location_t
46 text_info::get_location (unsigned int index_of_location) const
48 gcc_checking_assert (m_richloc);
50 if (index_of_location == 0)
51 return m_richloc->get_loc ();
52 else
53 return UNKNOWN_LOCATION;
56 // Default construct an output buffer.
58 output_buffer::output_buffer ()
59 : formatted_obstack (),
60 chunk_obstack (),
61 obstack (&formatted_obstack),
62 cur_chunk_array (),
63 stream (stderr),
64 line_length (),
65 digit_buffer (),
66 flush_p (true)
68 obstack_init (&formatted_obstack);
69 obstack_init (&chunk_obstack);
72 // Release resources owned by an output buffer at the end of lifetime.
74 output_buffer::~output_buffer ()
76 obstack_free (&chunk_obstack, NULL);
77 obstack_free (&formatted_obstack, NULL);
81 /* Format an integer given by va_arg (ARG, type-specifier T) where
82 type-specifier is a precision modifier as indicated by PREC. F is
83 a string used to construct the appropriate format-specifier. */
84 #define pp_integer_with_precision(PP, ARG, PREC, T, F) \
85 do \
86 switch (PREC) \
87 { \
88 case 0: \
89 pp_scalar (PP, "%" F, va_arg (ARG, T)); \
90 break; \
92 case 1: \
93 pp_scalar (PP, "%l" F, va_arg (ARG, long T)); \
94 break; \
96 case 2: \
97 pp_scalar (PP, "%" HOST_LONG_LONG_FORMAT F, va_arg (ARG, long long T)); \
98 break; \
100 default: \
101 break; \
103 while (0)
106 /* Subroutine of pp_set_maximum_length. Set up PRETTY-PRINTER's
107 internal maximum characters per line. */
108 static void
109 pp_set_real_maximum_length (pretty_printer *pp)
111 /* If we're told not to wrap lines then do the obvious thing. In case
112 we'll emit prefix only once per message, it is appropriate
113 not to increase unnecessarily the line-length cut-off. */
114 if (!pp_is_wrapping_line (pp)
115 || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_ONCE
116 || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
117 pp->maximum_length = pp_line_cutoff (pp);
118 else
120 int prefix_length = pp->prefix ? strlen (pp->prefix) : 0;
121 /* If the prefix is ridiculously too long, output at least
122 32 characters. */
123 if (pp_line_cutoff (pp) - prefix_length < 32)
124 pp->maximum_length = pp_line_cutoff (pp) + 32;
125 else
126 pp->maximum_length = pp_line_cutoff (pp);
130 /* Clear PRETTY-PRINTER's output state. */
131 static inline void
132 pp_clear_state (pretty_printer *pp)
134 pp->emitted_prefix = false;
135 pp_indentation (pp) = 0;
138 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream. */
139 void
140 pp_write_text_to_stream (pretty_printer *pp)
142 const char *text = pp_formatted_text (pp);
143 fputs (text, pp_buffer (pp)->stream);
144 pp_clear_output_area (pp);
147 /* As pp_write_text_to_stream, but for GraphViz label output.
149 Flush the formatted text of pretty-printer PP onto the attached stream.
150 Replace characters in PPF that have special meaning in a GraphViz .dot
151 file.
153 This routine is not very fast, but it doesn't have to be as this is only
154 be used by routines dumping intermediate representations in graph form. */
156 void
157 pp_write_text_as_dot_label_to_stream (pretty_printer *pp, bool for_record)
159 const char *text = pp_formatted_text (pp);
160 const char *p = text;
161 FILE *fp = pp_buffer (pp)->stream;
163 for (;*p; p++)
165 bool escape_char;
166 switch (*p)
168 /* Print newlines as a left-aligned newline. */
169 case '\n':
170 fputs ("\\l", fp);
171 escape_char = true;
172 break;
174 /* The following characters are only special for record-shape nodes. */
175 case '|':
176 case '{':
177 case '}':
178 case '<':
179 case '>':
180 case ' ':
181 escape_char = for_record;
182 break;
184 /* The following characters always have to be escaped
185 for use in labels. */
186 case '\\':
187 /* There is a bug in some (f.i. 2.36.0) versions of graphiz
188 ( http://www.graphviz.org/mantisbt/view.php?id=2524 ) related to
189 backslash as last char in label. Let's avoid triggering it. */
190 gcc_assert (*(p + 1) != '\0');
191 /* Fall through. */
192 case '"':
193 escape_char = true;
194 break;
196 default:
197 escape_char = false;
198 break;
201 if (escape_char)
202 fputc ('\\', fp);
204 fputc (*p, fp);
207 pp_clear_output_area (pp);
210 /* Wrap a text delimited by START and END into PRETTY-PRINTER. */
211 static void
212 pp_wrap_text (pretty_printer *pp, const char *start, const char *end)
214 bool wrapping_line = pp_is_wrapping_line (pp);
216 while (start != end)
218 /* Dump anything bordered by whitespaces. */
220 const char *p = start;
221 while (p != end && !ISBLANK (*p) && *p != '\n')
222 ++p;
223 if (wrapping_line
224 && p - start >= pp_remaining_character_count_for_line (pp))
225 pp_newline (pp);
226 pp_append_text (pp, start, p);
227 start = p;
230 if (start != end && ISBLANK (*start))
232 pp_space (pp);
233 ++start;
235 if (start != end && *start == '\n')
237 pp_newline (pp);
238 ++start;
243 /* Same as pp_wrap_text but wrap text only when in line-wrapping mode. */
244 static inline void
245 pp_maybe_wrap_text (pretty_printer *pp, const char *start, const char *end)
247 if (pp_is_wrapping_line (pp))
248 pp_wrap_text (pp, start, end);
249 else
250 pp_append_text (pp, start, end);
253 /* Append to the output area of PRETTY-PRINTER a string specified by its
254 STARTing character and LENGTH. */
255 static inline void
256 pp_append_r (pretty_printer *pp, const char *start, int length)
258 output_buffer_append_r (pp_buffer (pp), start, length);
261 /* Insert enough spaces into the output area of PRETTY-PRINTER to bring
262 the column position to the current indentation level, assuming that a
263 newline has just been written to the buffer. */
264 void
265 pp_indent (pretty_printer *pp)
267 int n = pp_indentation (pp);
268 int i;
270 for (i = 0; i < n; ++i)
271 pp_space (pp);
274 /* The following format specifiers are recognized as being client independent:
275 %d, %i: (signed) integer in base ten.
276 %u: unsigned integer in base ten.
277 %o: unsigned integer in base eight.
278 %x: unsigned integer in base sixteen.
279 %ld, %li, %lo, %lu, %lx: long versions of the above.
280 %lld, %lli, %llo, %llu, %llx: long long versions.
281 %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
282 %c: character.
283 %s: string.
284 %p: pointer (printed in a host-dependent manner).
285 %r: if pp_show_color(pp), switch to color identified by const char *.
286 %R: if pp_show_color(pp), reset color.
287 %m: strerror(text->err_no) - does not consume a value from args_ptr.
288 %%: '%'.
289 %<: opening quote.
290 %>: closing quote.
291 %': apostrophe (should only be used in untranslated messages;
292 translations should use appropriate punctuation directly).
293 %.*s: a substring the length of which is specified by an argument
294 integer.
295 %Ns: likewise, but length specified as constant in the format string.
296 Flag 'q': quote formatted text (must come immediately after '%').
297 %Z: Requires two arguments - array of int, and len. Prints elements
298 of the array.
300 Arguments can be used sequentially, or through %N$ resp. *N$
301 notation Nth argument after the format string. If %N$ / *N$
302 notation is used, it must be used for all arguments, except %m, %%,
303 %<, %> and %', which may not have a number, as they do not consume
304 an argument. When %M$.*N$s is used, M must be N + 1. (This may
305 also be written %M$.*s, provided N is not otherwise used.) The
306 format string must have conversion specifiers with argument numbers
307 1 up to highest argument; each argument may only be used once.
308 A format string can have at most 30 arguments. */
310 /* Formatting phases 1 and 2: render TEXT->format_spec plus
311 TEXT->args_ptr into a series of chunks in pp_buffer (PP)->args[].
312 Phase 3 is in pp_output_formatted_text. */
314 void
315 pp_format (pretty_printer *pp, text_info *text)
317 output_buffer *buffer = pp_buffer (pp);
318 const char *p;
319 const char **args;
320 struct chunk_info *new_chunk_array;
322 unsigned int curarg = 0, chunk = 0, argno;
323 pp_wrapping_mode_t old_wrapping_mode;
324 bool any_unnumbered = false, any_numbered = false;
325 const char **formatters[PP_NL_ARGMAX];
327 /* Allocate a new chunk structure. */
328 new_chunk_array = XOBNEW (&buffer->chunk_obstack, struct chunk_info);
329 new_chunk_array->prev = buffer->cur_chunk_array;
330 buffer->cur_chunk_array = new_chunk_array;
331 args = new_chunk_array->args;
333 /* Formatting phase 1: split up TEXT->format_spec into chunks in
334 pp_buffer (PP)->args[]. Even-numbered chunks are to be output
335 verbatim, odd-numbered chunks are format specifiers.
336 %m, %%, %<, %>, and %' are replaced with the appropriate text at
337 this point. */
339 memset (formatters, 0, sizeof formatters);
341 for (p = text->format_spec; *p; )
343 while (*p != '\0' && *p != '%')
345 obstack_1grow (&buffer->chunk_obstack, *p);
346 p++;
349 if (*p == '\0')
350 break;
352 switch (*++p)
354 case '\0':
355 gcc_unreachable ();
357 case '%':
358 obstack_1grow (&buffer->chunk_obstack, '%');
359 p++;
360 continue;
362 case '<':
364 obstack_grow (&buffer->chunk_obstack,
365 open_quote, strlen (open_quote));
366 const char *colorstr
367 = colorize_start (pp_show_color (pp), "quote");
368 obstack_grow (&buffer->chunk_obstack, colorstr, strlen (colorstr));
369 p++;
370 continue;
373 case '>':
375 const char *colorstr = colorize_stop (pp_show_color (pp));
376 obstack_grow (&buffer->chunk_obstack, colorstr, strlen (colorstr));
378 /* FALLTHRU */
379 case '\'':
380 obstack_grow (&buffer->chunk_obstack,
381 close_quote, strlen (close_quote));
382 p++;
383 continue;
385 case 'R':
387 const char *colorstr = colorize_stop (pp_show_color (pp));
388 obstack_grow (&buffer->chunk_obstack, colorstr,
389 strlen (colorstr));
390 p++;
391 continue;
394 case 'm':
396 const char *errstr = xstrerror (text->err_no);
397 obstack_grow (&buffer->chunk_obstack, errstr, strlen (errstr));
399 p++;
400 continue;
402 default:
403 /* Handled in phase 2. Terminate the plain chunk here. */
404 obstack_1grow (&buffer->chunk_obstack, '\0');
405 gcc_assert (chunk < PP_NL_ARGMAX * 2);
406 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
407 break;
410 if (ISDIGIT (*p))
412 char *end;
413 argno = strtoul (p, &end, 10) - 1;
414 p = end;
415 gcc_assert (*p == '$');
416 p++;
418 any_numbered = true;
419 gcc_assert (!any_unnumbered);
421 else
423 argno = curarg++;
424 any_unnumbered = true;
425 gcc_assert (!any_numbered);
427 gcc_assert (argno < PP_NL_ARGMAX);
428 gcc_assert (!formatters[argno]);
429 formatters[argno] = &args[chunk];
432 obstack_1grow (&buffer->chunk_obstack, *p);
433 p++;
435 while (strchr ("qwl+#", p[-1]));
437 if (p[-1] == '.')
439 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
440 (where M == N + 1). */
441 if (ISDIGIT (*p))
445 obstack_1grow (&buffer->chunk_obstack, *p);
446 p++;
448 while (ISDIGIT (p[-1]));
449 gcc_assert (p[-1] == 's');
451 else
453 gcc_assert (*p == '*');
454 obstack_1grow (&buffer->chunk_obstack, '*');
455 p++;
457 if (ISDIGIT (*p))
459 char *end;
460 unsigned int argno2 = strtoul (p, &end, 10) - 1;
461 p = end;
462 gcc_assert (argno2 == argno - 1);
463 gcc_assert (!any_unnumbered);
464 gcc_assert (*p == '$');
466 p++;
467 formatters[argno2] = formatters[argno];
469 else
471 gcc_assert (!any_numbered);
472 formatters[argno+1] = formatters[argno];
473 curarg++;
475 gcc_assert (*p == 's');
476 obstack_1grow (&buffer->chunk_obstack, 's');
477 p++;
480 if (*p == '\0')
481 break;
483 obstack_1grow (&buffer->chunk_obstack, '\0');
484 gcc_assert (chunk < PP_NL_ARGMAX * 2);
485 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
488 obstack_1grow (&buffer->chunk_obstack, '\0');
489 gcc_assert (chunk < PP_NL_ARGMAX * 2);
490 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
491 args[chunk] = 0;
493 /* Set output to the argument obstack, and switch line-wrapping and
494 prefixing off. */
495 buffer->obstack = &buffer->chunk_obstack;
496 old_wrapping_mode = pp_set_verbatim_wrapping (pp);
498 /* Second phase. Replace each formatter with the formatted text it
499 corresponds to. */
501 for (argno = 0; formatters[argno]; argno++)
503 int precision = 0;
504 bool wide = false;
505 bool plus = false;
506 bool hash = false;
507 bool quote = false;
509 /* We do not attempt to enforce any ordering on the modifier
510 characters. */
512 for (p = *formatters[argno];; p++)
514 switch (*p)
516 case 'q':
517 gcc_assert (!quote);
518 quote = true;
519 continue;
521 case '+':
522 gcc_assert (!plus);
523 plus = true;
524 continue;
526 case '#':
527 gcc_assert (!hash);
528 hash = true;
529 continue;
531 case 'w':
532 gcc_assert (!wide);
533 wide = true;
534 continue;
536 case 'l':
537 /* We don't support precision beyond that of "long long". */
538 gcc_assert (precision < 2);
539 precision++;
540 continue;
542 break;
545 gcc_assert (!wide || precision == 0);
547 if (quote)
549 pp_string (pp, open_quote);
550 pp_string (pp, colorize_start (pp_show_color (pp), "quote"));
553 switch (*p)
555 case 'r':
556 pp_string (pp, colorize_start (pp_show_color (pp),
557 va_arg (*text->args_ptr,
558 const char *)));
559 break;
561 case 'c':
563 /* When quoting, print alphanumeric, punctuation, and the space
564 character unchanged, and all others in hexadecimal with the
565 "\x" prefix. Otherwise print them all unchanged. */
566 int chr = va_arg (*text->args_ptr, int);
567 if (ISPRINT (chr) || !quote)
568 pp_character (pp, chr);
569 else
571 const char str [2] = { chr, '\0' };
572 pp_quoted_string (pp, str, 1);
574 break;
577 case 'd':
578 case 'i':
579 if (wide)
580 pp_wide_integer (pp, va_arg (*text->args_ptr, HOST_WIDE_INT));
581 else
582 pp_integer_with_precision
583 (pp, *text->args_ptr, precision, int, "d");
584 break;
586 case 'o':
587 if (wide)
588 pp_scalar (pp, "%" HOST_WIDE_INT_PRINT "o",
589 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
590 else
591 pp_integer_with_precision
592 (pp, *text->args_ptr, precision, unsigned, "o");
593 break;
595 case 's':
596 if (quote)
597 pp_quoted_string (pp, va_arg (*text->args_ptr, const char *));
598 else
599 pp_string (pp, va_arg (*text->args_ptr, const char *));
600 break;
602 case 'p':
603 pp_pointer (pp, va_arg (*text->args_ptr, void *));
604 break;
606 case 'u':
607 if (wide)
608 pp_scalar (pp, HOST_WIDE_INT_PRINT_UNSIGNED,
609 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
610 else
611 pp_integer_with_precision
612 (pp, *text->args_ptr, precision, unsigned, "u");
613 break;
615 case 'Z':
617 int *v = va_arg (*text->args_ptr, int *);
618 unsigned len = va_arg (*text->args_ptr, unsigned);
620 for (unsigned i = 0; i < len; ++i)
622 pp_scalar (pp, "%i", v[i]);
623 if (i < len - 1)
625 pp_comma (pp);
626 pp_space (pp);
629 break;
632 case 'x':
633 if (wide)
634 pp_scalar (pp, HOST_WIDE_INT_PRINT_HEX,
635 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
636 else
637 pp_integer_with_precision
638 (pp, *text->args_ptr, precision, unsigned, "x");
639 break;
641 case '.':
643 int n;
644 const char *s;
646 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
647 (where M == N + 1). The format string should be verified
648 already from the first phase. */
649 p++;
650 if (ISDIGIT (*p))
652 char *end;
653 n = strtoul (p, &end, 10);
654 p = end;
655 gcc_assert (*p == 's');
657 else
659 gcc_assert (*p == '*');
660 p++;
661 gcc_assert (*p == 's');
662 n = va_arg (*text->args_ptr, int);
664 /* This consumes a second entry in the formatters array. */
665 gcc_assert (formatters[argno] == formatters[argno+1]);
666 argno++;
669 s = va_arg (*text->args_ptr, const char *);
670 pp_append_text (pp, s, s + n);
672 break;
674 default:
676 bool ok;
678 gcc_assert (pp_format_decoder (pp));
679 ok = pp_format_decoder (pp) (pp, text, p,
680 precision, wide, plus, hash, quote,
681 formatters[argno]);
682 gcc_assert (ok);
686 if (quote)
688 pp_string (pp, colorize_stop (pp_show_color (pp)));
689 pp_string (pp, close_quote);
692 obstack_1grow (&buffer->chunk_obstack, '\0');
693 *formatters[argno] = XOBFINISH (&buffer->chunk_obstack, const char *);
696 if (CHECKING_P)
697 for (; argno < PP_NL_ARGMAX; argno++)
698 gcc_assert (!formatters[argno]);
700 /* If the client supplied a postprocessing object, call its "handle"
701 hook here. */
702 if (pp->m_format_postprocessor)
703 pp->m_format_postprocessor->handle (pp);
705 /* Revert to normal obstack and wrapping mode. */
706 buffer->obstack = &buffer->formatted_obstack;
707 buffer->line_length = 0;
708 pp_wrapping_mode (pp) = old_wrapping_mode;
709 pp_clear_state (pp);
712 /* Format of a message pointed to by TEXT. */
713 void
714 pp_output_formatted_text (pretty_printer *pp)
716 unsigned int chunk;
717 output_buffer *buffer = pp_buffer (pp);
718 struct chunk_info *chunk_array = buffer->cur_chunk_array;
719 const char **args = chunk_array->args;
721 gcc_assert (buffer->obstack == &buffer->formatted_obstack);
722 gcc_assert (buffer->line_length == 0);
724 /* This is a third phase, first 2 phases done in pp_format_args.
725 Now we actually print it. */
726 for (chunk = 0; args[chunk]; chunk++)
727 pp_string (pp, args[chunk]);
729 /* Deallocate the chunk structure and everything after it (i.e. the
730 associated series of formatted strings). */
731 buffer->cur_chunk_array = chunk_array->prev;
732 obstack_free (&buffer->chunk_obstack, chunk_array);
735 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
736 settings needed by BUFFER for a verbatim formatting. */
737 void
738 pp_format_verbatim (pretty_printer *pp, text_info *text)
740 /* Set verbatim mode. */
741 pp_wrapping_mode_t oldmode = pp_set_verbatim_wrapping (pp);
743 /* Do the actual formatting. */
744 pp_format (pp, text);
745 pp_output_formatted_text (pp);
747 /* Restore previous settings. */
748 pp_wrapping_mode (pp) = oldmode;
751 /* Flush the content of BUFFER onto the attached stream. This
752 function does nothing unless pp->output_buffer->flush_p. */
753 void
754 pp_flush (pretty_printer *pp)
756 pp_clear_state (pp);
757 if (!pp->buffer->flush_p)
758 return;
759 pp_write_text_to_stream (pp);
760 fflush (pp_buffer (pp)->stream);
763 /* Flush the content of BUFFER onto the attached stream independently
764 of the value of pp->output_buffer->flush_p. */
765 void
766 pp_really_flush (pretty_printer *pp)
768 pp_clear_state (pp);
769 pp_write_text_to_stream (pp);
770 fflush (pp_buffer (pp)->stream);
773 /* Sets the number of maximum characters per line PRETTY-PRINTER can
774 output in line-wrapping mode. A LENGTH value 0 suppresses
775 line-wrapping. */
776 void
777 pp_set_line_maximum_length (pretty_printer *pp, int length)
779 pp_line_cutoff (pp) = length;
780 pp_set_real_maximum_length (pp);
783 /* Clear PRETTY-PRINTER output area text info. */
784 void
785 pp_clear_output_area (pretty_printer *pp)
787 obstack_free (pp_buffer (pp)->obstack,
788 obstack_base (pp_buffer (pp)->obstack));
789 pp_buffer (pp)->line_length = 0;
792 /* Set PREFIX for PRETTY-PRINTER. */
793 void
794 pp_set_prefix (pretty_printer *pp, const char *prefix)
796 pp->prefix = prefix;
797 pp_set_real_maximum_length (pp);
798 pp->emitted_prefix = false;
799 pp_indentation (pp) = 0;
802 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
803 void
804 pp_destroy_prefix (pretty_printer *pp)
806 if (pp->prefix != NULL)
808 free (CONST_CAST (char *, pp->prefix));
809 pp->prefix = NULL;
813 /* Write out PRETTY-PRINTER's prefix. */
814 void
815 pp_emit_prefix (pretty_printer *pp)
817 if (pp->prefix != NULL)
819 switch (pp_prefixing_rule (pp))
821 default:
822 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
823 break;
825 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
826 if (pp->emitted_prefix)
828 pp_indent (pp);
829 break;
831 pp_indentation (pp) += 3;
832 /* Fall through. */
834 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
836 int prefix_length = strlen (pp->prefix);
837 pp_append_r (pp, pp->prefix, prefix_length);
838 pp->emitted_prefix = true;
840 break;
845 /* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
846 characters per line. */
848 pretty_printer::pretty_printer (const char *p, int l)
849 : buffer (new (XCNEW (output_buffer)) output_buffer ()),
850 prefix (),
851 padding (pp_none),
852 maximum_length (),
853 indent_skip (),
854 wrapping (),
855 format_decoder (),
856 m_format_postprocessor (NULL),
857 emitted_prefix (),
858 need_newline (),
859 translate_identifiers (true),
860 show_color ()
862 pp_line_cutoff (this) = l;
863 /* By default, we emit prefixes once per message. */
864 pp_prefixing_rule (this) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
865 pp_set_prefix (this, p);
868 pretty_printer::~pretty_printer ()
870 if (m_format_postprocessor)
871 delete m_format_postprocessor;
872 buffer->~output_buffer ();
873 XDELETE (buffer);
876 /* Append a string delimited by START and END to the output area of
877 PRETTY-PRINTER. No line wrapping is done. However, if beginning a
878 new line then emit PRETTY-PRINTER's prefix and skip any leading
879 whitespace if appropriate. The caller must ensure that it is
880 safe to do so. */
881 void
882 pp_append_text (pretty_printer *pp, const char *start, const char *end)
884 /* Emit prefix and skip whitespace if we're starting a new line. */
885 if (pp_buffer (pp)->line_length == 0)
887 pp_emit_prefix (pp);
888 if (pp_is_wrapping_line (pp))
889 while (start != end && *start == ' ')
890 ++start;
892 pp_append_r (pp, start, end - start);
895 /* Finishes constructing a NULL-terminated character string representing
896 the PRETTY-PRINTED text. */
897 const char *
898 pp_formatted_text (pretty_printer *pp)
900 return output_buffer_formatted_text (pp_buffer (pp));
903 /* Return a pointer to the last character emitted in PRETTY-PRINTER's
904 output area. A NULL pointer means no character available. */
905 const char *
906 pp_last_position_in_text (const pretty_printer *pp)
908 return output_buffer_last_position_in_text (pp_buffer (pp));
911 /* Return the amount of characters PRETTY-PRINTER can accept to
912 make a full line. Meaningful only in line-wrapping mode. */
914 pp_remaining_character_count_for_line (pretty_printer *pp)
916 return pp->maximum_length - pp_buffer (pp)->line_length;
920 /* Format a message into BUFFER a la printf. */
921 void
922 pp_printf (pretty_printer *pp, const char *msg, ...)
924 text_info text;
925 va_list ap;
927 va_start (ap, msg);
928 text.err_no = errno;
929 text.args_ptr = &ap;
930 text.format_spec = msg;
931 pp_format (pp, &text);
932 pp_output_formatted_text (pp);
933 va_end (ap);
937 /* Output MESSAGE verbatim into BUFFER. */
938 void
939 pp_verbatim (pretty_printer *pp, const char *msg, ...)
941 text_info text;
942 va_list ap;
944 va_start (ap, msg);
945 text.err_no = errno;
946 text.args_ptr = &ap;
947 text.format_spec = msg;
948 pp_format_verbatim (pp, &text);
949 va_end (ap);
954 /* Have PRETTY-PRINTER start a new line. */
955 void
956 pp_newline (pretty_printer *pp)
958 obstack_1grow (pp_buffer (pp)->obstack, '\n');
959 pp_needs_newline (pp) = false;
960 pp_buffer (pp)->line_length = 0;
963 /* Have PRETTY-PRINTER add a CHARACTER. */
964 void
965 pp_character (pretty_printer *pp, int c)
967 if (pp_is_wrapping_line (pp)
968 && pp_remaining_character_count_for_line (pp) <= 0)
970 pp_newline (pp);
971 if (ISSPACE (c))
972 return;
974 obstack_1grow (pp_buffer (pp)->obstack, c);
975 ++pp_buffer (pp)->line_length;
978 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
979 be line-wrapped if in appropriate mode. */
980 void
981 pp_string (pretty_printer *pp, const char *str)
983 gcc_checking_assert (str);
984 pp_maybe_wrap_text (pp, str, str + strlen (str));
987 /* Append the leading N characters of STRING to the output area of
988 PRETTY-PRINTER, quoting in hexadecimal non-printable characters.
989 Setting N = -1 is as if N were set to strlen (STRING). The STRING
990 may be line-wrapped if in appropriate mode. */
991 static void
992 pp_quoted_string (pretty_printer *pp, const char *str, size_t n /* = -1 */)
994 gcc_checking_assert (str);
996 const char *last = str;
997 const char *ps;
999 /* Compute the length if not specified. */
1000 if (n == (size_t) -1)
1001 n = strlen (str);
1003 for (ps = str; n; ++ps, --n)
1005 if (ISPRINT (*ps))
1006 continue;
1008 if (last < ps)
1009 pp_maybe_wrap_text (pp, last, ps - 1);
1011 /* Append the hexadecimal value of the character. Allocate a buffer
1012 that's large enough for a 32-bit char plus the hex prefix. */
1013 char buf [11];
1014 int n = sprintf (buf, "\\x%02x", (unsigned char)*ps);
1015 pp_maybe_wrap_text (pp, buf, buf + n);
1016 last = ps + 1;
1019 pp_maybe_wrap_text (pp, last, ps);
1022 /* Maybe print out a whitespace if needed. */
1024 void
1025 pp_maybe_space (pretty_printer *pp)
1027 if (pp->padding != pp_none)
1029 pp_space (pp);
1030 pp->padding = pp_none;
1034 // Add a newline to the pretty printer PP and flush formatted text.
1036 void
1037 pp_newline_and_flush (pretty_printer *pp)
1039 pp_newline (pp);
1040 pp_flush (pp);
1041 pp_needs_newline (pp) = false;
1044 // Add a newline to the pretty printer PP, followed by indentation.
1046 void
1047 pp_newline_and_indent (pretty_printer *pp, int n)
1049 pp_indentation (pp) += n;
1050 pp_newline (pp);
1051 pp_indent (pp);
1052 pp_needs_newline (pp) = false;
1055 // Add separator C, followed by a single whitespace.
1057 void
1058 pp_separate_with (pretty_printer *pp, char c)
1060 pp_character (pp, c);
1061 pp_space (pp);
1065 /* The string starting at P has LEN (at least 1) bytes left; if they
1066 start with a valid UTF-8 sequence, return the length of that
1067 sequence and set *VALUE to the value of that sequence, and
1068 otherwise return 0 and set *VALUE to (unsigned int) -1. */
1070 static int
1071 decode_utf8_char (const unsigned char *p, size_t len, unsigned int *value)
1073 unsigned int t = *p;
1075 if (len == 0)
1076 abort ();
1077 if (t & 0x80)
1079 size_t utf8_len = 0;
1080 unsigned int ch;
1081 size_t i;
1082 for (t = *p; t & 0x80; t <<= 1)
1083 utf8_len++;
1085 if (utf8_len > len || utf8_len < 2 || utf8_len > 6)
1087 *value = (unsigned int) -1;
1088 return 0;
1090 ch = *p & ((1 << (7 - utf8_len)) - 1);
1091 for (i = 1; i < utf8_len; i++)
1093 unsigned int u = p[i];
1094 if ((u & 0xC0) != 0x80)
1096 *value = (unsigned int) -1;
1097 return 0;
1099 ch = (ch << 6) | (u & 0x3F);
1101 if ( (ch <= 0x7F && utf8_len > 1)
1102 || (ch <= 0x7FF && utf8_len > 2)
1103 || (ch <= 0xFFFF && utf8_len > 3)
1104 || (ch <= 0x1FFFFF && utf8_len > 4)
1105 || (ch <= 0x3FFFFFF && utf8_len > 5)
1106 || (ch >= 0xD800 && ch <= 0xDFFF))
1108 *value = (unsigned int) -1;
1109 return 0;
1111 *value = ch;
1112 return utf8_len;
1114 else
1116 *value = t;
1117 return 1;
1121 /* Allocator for identifier_to_locale and corresponding function to
1122 free memory. */
1124 void *(*identifier_to_locale_alloc) (size_t) = xmalloc;
1125 void (*identifier_to_locale_free) (void *) = free;
1127 /* Given IDENT, an identifier in the internal encoding, return a
1128 version of IDENT suitable for diagnostics in the locale character
1129 set: either IDENT itself, or a string, allocated using
1130 identifier_to_locale_alloc, converted to the locale character set
1131 and using escape sequences if not representable in the locale
1132 character set or containing control characters or invalid byte
1133 sequences. Existing backslashes in IDENT are not doubled, so the
1134 result may not uniquely specify the contents of an arbitrary byte
1135 sequence identifier. */
1137 const char *
1138 identifier_to_locale (const char *ident)
1140 const unsigned char *uid = (const unsigned char *) ident;
1141 size_t idlen = strlen (ident);
1142 bool valid_printable_utf8 = true;
1143 bool all_ascii = true;
1144 size_t i;
1146 for (i = 0; i < idlen;)
1148 unsigned int c;
1149 size_t utf8_len = decode_utf8_char (&uid[i], idlen - i, &c);
1150 if (utf8_len == 0 || c <= 0x1F || (c >= 0x7F && c <= 0x9F))
1152 valid_printable_utf8 = false;
1153 break;
1155 if (utf8_len > 1)
1156 all_ascii = false;
1157 i += utf8_len;
1160 /* If IDENT contains invalid UTF-8 sequences (which may occur with
1161 attributes putting arbitrary byte sequences in identifiers), or
1162 control characters, we use octal escape sequences for all bytes
1163 outside printable ASCII. */
1164 if (!valid_printable_utf8)
1166 char *ret = (char *) identifier_to_locale_alloc (4 * idlen + 1);
1167 char *p = ret;
1168 for (i = 0; i < idlen; i++)
1170 if (uid[i] > 0x1F && uid[i] < 0x7F)
1171 *p++ = uid[i];
1172 else
1174 sprintf (p, "\\%03o", uid[i]);
1175 p += 4;
1178 *p = 0;
1179 return ret;
1182 /* Otherwise, if it is valid printable ASCII, or printable UTF-8
1183 with the locale character set being UTF-8, IDENT is used. */
1184 if (all_ascii || locale_utf8)
1185 return ident;
1187 /* Otherwise IDENT is converted to the locale character set if
1188 possible. */
1189 #if defined ENABLE_NLS && defined HAVE_LANGINFO_CODESET && HAVE_ICONV
1190 if (locale_encoding != NULL)
1192 iconv_t cd = iconv_open (locale_encoding, "UTF-8");
1193 bool conversion_ok = true;
1194 char *ret = NULL;
1195 if (cd != (iconv_t) -1)
1197 size_t ret_alloc = 4 * idlen + 1;
1198 for (;;)
1200 /* Repeat the whole conversion process as needed with
1201 larger buffers so non-reversible transformations can
1202 always be detected. */
1203 ICONV_CONST char *inbuf = CONST_CAST (char *, ident);
1204 char *outbuf;
1205 size_t inbytesleft = idlen;
1206 size_t outbytesleft = ret_alloc - 1;
1207 size_t iconv_ret;
1209 ret = (char *) identifier_to_locale_alloc (ret_alloc);
1210 outbuf = ret;
1212 if (iconv (cd, 0, 0, 0, 0) == (size_t) -1)
1214 conversion_ok = false;
1215 break;
1218 iconv_ret = iconv (cd, &inbuf, &inbytesleft,
1219 &outbuf, &outbytesleft);
1220 if (iconv_ret == (size_t) -1 || inbytesleft != 0)
1222 if (errno == E2BIG)
1224 ret_alloc *= 2;
1225 identifier_to_locale_free (ret);
1226 ret = NULL;
1227 continue;
1229 else
1231 conversion_ok = false;
1232 break;
1235 else if (iconv_ret != 0)
1237 conversion_ok = false;
1238 break;
1240 /* Return to initial shift state. */
1241 if (iconv (cd, 0, 0, &outbuf, &outbytesleft) == (size_t) -1)
1243 if (errno == E2BIG)
1245 ret_alloc *= 2;
1246 identifier_to_locale_free (ret);
1247 ret = NULL;
1248 continue;
1250 else
1252 conversion_ok = false;
1253 break;
1256 *outbuf = 0;
1257 break;
1259 iconv_close (cd);
1260 if (conversion_ok)
1261 return ret;
1264 #endif
1266 /* Otherwise, convert non-ASCII characters in IDENT to UCNs. */
1268 char *ret = (char *) identifier_to_locale_alloc (10 * idlen + 1);
1269 char *p = ret;
1270 for (i = 0; i < idlen;)
1272 unsigned int c;
1273 size_t utf8_len = decode_utf8_char (&uid[i], idlen - i, &c);
1274 if (utf8_len == 1)
1275 *p++ = uid[i];
1276 else
1278 sprintf (p, "\\U%08x", c);
1279 p += 10;
1281 i += utf8_len;
1283 *p = 0;
1284 return ret;
1288 #if CHECKING_P
1290 namespace selftest {
1292 /* Smoketest for pretty_printer. */
1294 static void
1295 test_basic_printing ()
1297 pretty_printer pp;
1298 pp_string (&pp, "hello");
1299 pp_space (&pp);
1300 pp_string (&pp, "world");
1302 ASSERT_STREQ ("hello world", pp_formatted_text (&pp));
1305 /* Helper function for testing pp_format.
1306 Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
1307 prints EXPECTED, assuming that pp_show_color is SHOW_COLOR. */
1309 static void
1310 assert_pp_format_va (const location &loc, const char *expected,
1311 bool show_color, const char *fmt, va_list *ap)
1313 pretty_printer pp;
1314 text_info ti;
1315 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1317 ti.format_spec = fmt;
1318 ti.args_ptr = ap;
1319 ti.err_no = 0;
1320 ti.x_data = NULL;
1321 ti.m_richloc = &rich_loc;
1323 pp_show_color (&pp) = show_color;
1324 pp_format (&pp, &ti);
1325 pp_output_formatted_text (&pp);
1326 ASSERT_STREQ_AT (loc, expected, pp_formatted_text (&pp));
1329 /* Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
1330 prints EXPECTED, with show_color disabled. */
1332 static void
1333 assert_pp_format (const location &loc, const char *expected,
1334 const char *fmt, ...)
1336 va_list ap;
1338 va_start (ap, fmt);
1339 assert_pp_format_va (loc, expected, false, fmt, &ap);
1340 va_end (ap);
1343 /* As above, but with colorization enabled. */
1345 static void
1346 assert_pp_format_colored (const location &loc, const char *expected,
1347 const char *fmt, ...)
1349 /* The tests of colorization assume the default color scheme.
1350 If GCC_COLORS is set, then the colors have potentially been
1351 overridden; skip the test. */
1352 if (getenv ("GCC_COLORS"))
1353 return;
1355 va_list ap;
1357 va_start (ap, fmt);
1358 assert_pp_format_va (loc, expected, true, fmt, &ap);
1359 va_end (ap);
1362 /* Helper function for calling testing pp_format,
1363 by calling assert_pp_format with various numbers of arguments.
1364 These exist mostly to avoid having to write SELFTEST_LOCATION
1365 throughout test_pp_format. */
1367 #define ASSERT_PP_FORMAT_1(EXPECTED, FMT, ARG1) \
1368 SELFTEST_BEGIN_STMT \
1369 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
1370 (ARG1)); \
1371 SELFTEST_END_STMT
1373 #define ASSERT_PP_FORMAT_2(EXPECTED, FMT, ARG1, ARG2) \
1374 SELFTEST_BEGIN_STMT \
1375 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
1376 (ARG1), (ARG2)); \
1377 SELFTEST_END_STMT
1379 #define ASSERT_PP_FORMAT_3(EXPECTED, FMT, ARG1, ARG2, ARG3) \
1380 SELFTEST_BEGIN_STMT \
1381 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
1382 (ARG1), (ARG2), (ARG3)); \
1383 SELFTEST_END_STMT
1385 /* Verify that pp_format works, for various format codes. */
1387 static void
1388 test_pp_format ()
1390 /* Avoid introducing locale-specific differences in the results
1391 by hardcoding open_quote and close_quote. */
1392 const char *old_open_quote = open_quote;
1393 const char *old_close_quote = close_quote;
1394 open_quote = "`";
1395 close_quote = "'";
1397 /* Verify that plain text is passed through unchanged. */
1398 assert_pp_format (SELFTEST_LOCATION, "unformatted", "unformatted");
1400 /* Verify various individual format codes, in the order listed in the
1401 comment for pp_format above. For each code, we append a second
1402 argument with a known bit pattern (0x12345678), to ensure that we
1403 are consuming arguments correctly. */
1404 ASSERT_PP_FORMAT_2 ("-27 12345678", "%d %x", -27, 0x12345678);
1405 ASSERT_PP_FORMAT_2 ("-5 12345678", "%i %x", -5, 0x12345678);
1406 ASSERT_PP_FORMAT_2 ("10 12345678", "%u %x", 10, 0x12345678);
1407 ASSERT_PP_FORMAT_2 ("17 12345678", "%o %x", 15, 0x12345678);
1408 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%x %x", 0xcafebabe, 0x12345678);
1409 ASSERT_PP_FORMAT_2 ("-27 12345678", "%ld %x", (long)-27, 0x12345678);
1410 ASSERT_PP_FORMAT_2 ("-5 12345678", "%li %x", (long)-5, 0x12345678);
1411 ASSERT_PP_FORMAT_2 ("10 12345678", "%lu %x", (long)10, 0x12345678);
1412 ASSERT_PP_FORMAT_2 ("17 12345678", "%lo %x", (long)15, 0x12345678);
1413 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%lx %x", (long)0xcafebabe,
1414 0x12345678);
1415 ASSERT_PP_FORMAT_2 ("-27 12345678", "%lld %x", (long long)-27, 0x12345678);
1416 ASSERT_PP_FORMAT_2 ("-5 12345678", "%lli %x", (long long)-5, 0x12345678);
1417 ASSERT_PP_FORMAT_2 ("10 12345678", "%llu %x", (long long)10, 0x12345678);
1418 ASSERT_PP_FORMAT_2 ("17 12345678", "%llo %x", (long long)15, 0x12345678);
1419 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%llx %x", (long long)0xcafebabe,
1420 0x12345678);
1421 ASSERT_PP_FORMAT_2 ("-27 12345678", "%wd %x", (HOST_WIDE_INT)-27, 0x12345678);
1422 ASSERT_PP_FORMAT_2 ("-5 12345678", "%wi %x", (HOST_WIDE_INT)-5, 0x12345678);
1423 ASSERT_PP_FORMAT_2 ("10 12345678", "%wu %x", (unsigned HOST_WIDE_INT)10,
1424 0x12345678);
1425 ASSERT_PP_FORMAT_2 ("17 12345678", "%wo %x", (HOST_WIDE_INT)15, 0x12345678);
1426 ASSERT_PP_FORMAT_2 ("0xcafebabe 12345678", "%wx %x", (HOST_WIDE_INT)0xcafebabe,
1427 0x12345678);
1428 ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678);
1429 ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello world",
1430 0x12345678);
1431 /* We can't test for %p; the pointer is printed in an implementation-defined
1432 manner. */
1433 ASSERT_PP_FORMAT_2 ("normal colored normal 12345678",
1434 "normal %rcolored%R normal %x",
1435 "error", 0x12345678);
1436 assert_pp_format_colored
1437 (SELFTEST_LOCATION,
1438 "normal \33[01;31m\33[Kcolored\33[m\33[K normal 12345678",
1439 "normal %rcolored%R normal %x", "error", 0x12345678);
1440 /* TODO:
1441 %m: strerror(text->err_no) - does not consume a value from args_ptr. */
1442 ASSERT_PP_FORMAT_1 ("% 12345678", "%% %x", 0x12345678);
1443 ASSERT_PP_FORMAT_1 ("` 12345678", "%< %x", 0x12345678);
1444 ASSERT_PP_FORMAT_1 ("' 12345678", "%> %x", 0x12345678);
1445 ASSERT_PP_FORMAT_1 ("' 12345678", "%' %x", 0x12345678);
1446 ASSERT_PP_FORMAT_3 ("abc 12345678", "%.*s %x", 3, "abcdef", 0x12345678);
1447 ASSERT_PP_FORMAT_2 ("abc 12345678", "%.3s %x", "abcdef", 0x12345678);
1449 /* Verify flag 'q'. */
1450 ASSERT_PP_FORMAT_2 ("`foo' 12345678", "%qs %x", "foo", 0x12345678);
1451 assert_pp_format_colored (SELFTEST_LOCATION,
1452 "`\33[01m\33[Kfoo\33[m\33[K' 12345678", "%qs %x",
1453 "foo", 0x12345678);
1455 /* Verify %Z. */
1456 int v[] = { 1, 2, 3 };
1457 ASSERT_PP_FORMAT_3 ("1, 2, 3 12345678", "%Z %x", v, 3, 0x12345678);
1459 int v2[] = { 0 };
1460 ASSERT_PP_FORMAT_3 ("0 12345678", "%Z %x", v2, 1, 0x12345678);
1462 /* Verify that combinations work, along with unformatted text. */
1463 assert_pp_format (SELFTEST_LOCATION,
1464 "the quick brown fox jumps over the lazy dog",
1465 "the %s %s %s jumps over the %s %s",
1466 "quick", "brown", "fox", "lazy", "dog");
1467 assert_pp_format (SELFTEST_LOCATION, "item 3 of 7", "item %i of %i", 3, 7);
1468 assert_pp_format (SELFTEST_LOCATION, "problem with `bar' at line 10",
1469 "problem with %qs at line %i", "bar", 10);
1471 /* Restore old values of open_quote and close_quote. */
1472 open_quote = old_open_quote;
1473 close_quote = old_close_quote;
1476 /* Run all of the selftests within this file. */
1478 void
1479 pretty_print_c_tests ()
1481 test_basic_printing ();
1482 test_pp_format ();
1485 } // namespace selftest
1487 #endif /* CHECKING_P */