2016-09-30 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / pretty-print.c
bloba39815ea511e1fbd65bd7abd5d4dd95af8e6664b
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>
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 '%').
298 Arguments can be used sequentially, or through %N$ resp. *N$
299 notation Nth argument after the format string. If %N$ / *N$
300 notation is used, it must be used for all arguments, except %m, %%,
301 %<, %> and %', which may not have a number, as they do not consume
302 an argument. When %M$.*N$s is used, M must be N + 1. (This may
303 also be written %M$.*s, provided N is not otherwise used.) The
304 format string must have conversion specifiers with argument numbers
305 1 up to highest argument; each argument may only be used once.
306 A format string can have at most 30 arguments. */
308 /* Formatting phases 1 and 2: render TEXT->format_spec plus
309 TEXT->args_ptr into a series of chunks in pp_buffer (PP)->args[].
310 Phase 3 is in pp_output_formatted_text. */
312 void
313 pp_format (pretty_printer *pp, text_info *text)
315 output_buffer *buffer = pp_buffer (pp);
316 const char *p;
317 const char **args;
318 struct chunk_info *new_chunk_array;
320 unsigned int curarg = 0, chunk = 0, argno;
321 pp_wrapping_mode_t old_wrapping_mode;
322 bool any_unnumbered = false, any_numbered = false;
323 const char **formatters[PP_NL_ARGMAX];
325 /* Allocate a new chunk structure. */
326 new_chunk_array = XOBNEW (&buffer->chunk_obstack, struct chunk_info);
327 new_chunk_array->prev = buffer->cur_chunk_array;
328 buffer->cur_chunk_array = new_chunk_array;
329 args = new_chunk_array->args;
331 /* Formatting phase 1: split up TEXT->format_spec into chunks in
332 pp_buffer (PP)->args[]. Even-numbered chunks are to be output
333 verbatim, odd-numbered chunks are format specifiers.
334 %m, %%, %<, %>, and %' are replaced with the appropriate text at
335 this point. */
337 memset (formatters, 0, sizeof formatters);
339 for (p = text->format_spec; *p; )
341 while (*p != '\0' && *p != '%')
343 obstack_1grow (&buffer->chunk_obstack, *p);
344 p++;
347 if (*p == '\0')
348 break;
350 switch (*++p)
352 case '\0':
353 gcc_unreachable ();
355 case '%':
356 obstack_1grow (&buffer->chunk_obstack, '%');
357 p++;
358 continue;
360 case '<':
362 obstack_grow (&buffer->chunk_obstack,
363 open_quote, strlen (open_quote));
364 const char *colorstr
365 = colorize_start (pp_show_color (pp), "quote");
366 obstack_grow (&buffer->chunk_obstack, colorstr, strlen (colorstr));
367 p++;
368 continue;
371 case '>':
373 const char *colorstr = colorize_stop (pp_show_color (pp));
374 obstack_grow (&buffer->chunk_obstack, colorstr, strlen (colorstr));
376 /* FALLTHRU */
377 case '\'':
378 obstack_grow (&buffer->chunk_obstack,
379 close_quote, strlen (close_quote));
380 p++;
381 continue;
383 case 'R':
385 const char *colorstr = colorize_stop (pp_show_color (pp));
386 obstack_grow (&buffer->chunk_obstack, colorstr,
387 strlen (colorstr));
388 p++;
389 continue;
392 case 'm':
394 const char *errstr = xstrerror (text->err_no);
395 obstack_grow (&buffer->chunk_obstack, errstr, strlen (errstr));
397 p++;
398 continue;
400 default:
401 /* Handled in phase 2. Terminate the plain chunk here. */
402 obstack_1grow (&buffer->chunk_obstack, '\0');
403 gcc_assert (chunk < PP_NL_ARGMAX * 2);
404 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
405 break;
408 if (ISDIGIT (*p))
410 char *end;
411 argno = strtoul (p, &end, 10) - 1;
412 p = end;
413 gcc_assert (*p == '$');
414 p++;
416 any_numbered = true;
417 gcc_assert (!any_unnumbered);
419 else
421 argno = curarg++;
422 any_unnumbered = true;
423 gcc_assert (!any_numbered);
425 gcc_assert (argno < PP_NL_ARGMAX);
426 gcc_assert (!formatters[argno]);
427 formatters[argno] = &args[chunk];
430 obstack_1grow (&buffer->chunk_obstack, *p);
431 p++;
433 while (strchr ("qwl+#", p[-1]));
435 if (p[-1] == '.')
437 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
438 (where M == N + 1). */
439 if (ISDIGIT (*p))
443 obstack_1grow (&buffer->chunk_obstack, *p);
444 p++;
446 while (ISDIGIT (p[-1]));
447 gcc_assert (p[-1] == 's');
449 else
451 gcc_assert (*p == '*');
452 obstack_1grow (&buffer->chunk_obstack, '*');
453 p++;
455 if (ISDIGIT (*p))
457 char *end;
458 unsigned int argno2 = strtoul (p, &end, 10) - 1;
459 p = end;
460 gcc_assert (argno2 == argno - 1);
461 gcc_assert (!any_unnumbered);
462 gcc_assert (*p == '$');
464 p++;
465 formatters[argno2] = formatters[argno];
467 else
469 gcc_assert (!any_numbered);
470 formatters[argno+1] = formatters[argno];
471 curarg++;
473 gcc_assert (*p == 's');
474 obstack_1grow (&buffer->chunk_obstack, 's');
475 p++;
478 if (*p == '\0')
479 break;
481 obstack_1grow (&buffer->chunk_obstack, '\0');
482 gcc_assert (chunk < PP_NL_ARGMAX * 2);
483 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
486 obstack_1grow (&buffer->chunk_obstack, '\0');
487 gcc_assert (chunk < PP_NL_ARGMAX * 2);
488 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
489 args[chunk] = 0;
491 /* Set output to the argument obstack, and switch line-wrapping and
492 prefixing off. */
493 buffer->obstack = &buffer->chunk_obstack;
494 old_wrapping_mode = pp_set_verbatim_wrapping (pp);
496 /* Second phase. Replace each formatter with the formatted text it
497 corresponds to. */
499 for (argno = 0; formatters[argno]; argno++)
501 int precision = 0;
502 bool wide = false;
503 bool plus = false;
504 bool hash = false;
505 bool quote = false;
507 /* We do not attempt to enforce any ordering on the modifier
508 characters. */
510 for (p = *formatters[argno];; p++)
512 switch (*p)
514 case 'q':
515 gcc_assert (!quote);
516 quote = true;
517 continue;
519 case '+':
520 gcc_assert (!plus);
521 plus = true;
522 continue;
524 case '#':
525 gcc_assert (!hash);
526 hash = true;
527 continue;
529 case 'w':
530 gcc_assert (!wide);
531 wide = true;
532 continue;
534 case 'l':
535 /* We don't support precision beyond that of "long long". */
536 gcc_assert (precision < 2);
537 precision++;
538 continue;
540 break;
543 gcc_assert (!wide || precision == 0);
545 if (quote)
547 pp_string (pp, open_quote);
548 pp_string (pp, colorize_start (pp_show_color (pp), "quote"));
551 switch (*p)
553 case 'r':
554 pp_string (pp, colorize_start (pp_show_color (pp),
555 va_arg (*text->args_ptr,
556 const char *)));
557 break;
559 case 'c':
561 /* When quoting, print alphanumeric, punctuation, and the space
562 character unchanged, and all others in hexadecimal with the
563 "\x" prefix. Otherwise print them all unchanged. */
564 int chr = va_arg (*text->args_ptr, int);
565 if (ISPRINT (chr) || !quote)
566 pp_character (pp, chr);
567 else
569 const char str [2] = { chr, '\0' };
570 pp_quoted_string (pp, str, 1);
572 break;
575 case 'd':
576 case 'i':
577 if (wide)
578 pp_wide_integer (pp, va_arg (*text->args_ptr, HOST_WIDE_INT));
579 else
580 pp_integer_with_precision
581 (pp, *text->args_ptr, precision, int, "d");
582 break;
584 case 'o':
585 if (wide)
586 pp_scalar (pp, "%" HOST_WIDE_INT_PRINT "o",
587 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
588 else
589 pp_integer_with_precision
590 (pp, *text->args_ptr, precision, unsigned, "o");
591 break;
593 case 's':
594 if (quote)
595 pp_quoted_string (pp, va_arg (*text->args_ptr, const char *));
596 else
597 pp_string (pp, va_arg (*text->args_ptr, const char *));
598 break;
600 case 'p':
601 pp_pointer (pp, va_arg (*text->args_ptr, void *));
602 break;
604 case 'u':
605 if (wide)
606 pp_scalar (pp, HOST_WIDE_INT_PRINT_UNSIGNED,
607 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
608 else
609 pp_integer_with_precision
610 (pp, *text->args_ptr, precision, unsigned, "u");
611 break;
613 case 'x':
614 if (wide)
615 pp_scalar (pp, HOST_WIDE_INT_PRINT_HEX,
616 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
617 else
618 pp_integer_with_precision
619 (pp, *text->args_ptr, precision, unsigned, "x");
620 break;
622 case '.':
624 int n;
625 const char *s;
627 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
628 (where M == N + 1). The format string should be verified
629 already from the first phase. */
630 p++;
631 if (ISDIGIT (*p))
633 char *end;
634 n = strtoul (p, &end, 10);
635 p = end;
636 gcc_assert (*p == 's');
638 else
640 gcc_assert (*p == '*');
641 p++;
642 gcc_assert (*p == 's');
643 n = va_arg (*text->args_ptr, int);
645 /* This consumes a second entry in the formatters array. */
646 gcc_assert (formatters[argno] == formatters[argno+1]);
647 argno++;
650 s = va_arg (*text->args_ptr, const char *);
651 pp_append_text (pp, s, s + n);
653 break;
655 default:
657 bool ok;
659 gcc_assert (pp_format_decoder (pp));
660 ok = pp_format_decoder (pp) (pp, text, p,
661 precision, wide, plus, hash);
662 gcc_assert (ok);
666 if (quote)
668 pp_string (pp, colorize_stop (pp_show_color (pp)));
669 pp_string (pp, close_quote);
672 obstack_1grow (&buffer->chunk_obstack, '\0');
673 *formatters[argno] = XOBFINISH (&buffer->chunk_obstack, const char *);
676 if (CHECKING_P)
677 for (; argno < PP_NL_ARGMAX; argno++)
678 gcc_assert (!formatters[argno]);
680 /* Revert to normal obstack and wrapping mode. */
681 buffer->obstack = &buffer->formatted_obstack;
682 buffer->line_length = 0;
683 pp_wrapping_mode (pp) = old_wrapping_mode;
684 pp_clear_state (pp);
687 /* Format of a message pointed to by TEXT. */
688 void
689 pp_output_formatted_text (pretty_printer *pp)
691 unsigned int chunk;
692 output_buffer *buffer = pp_buffer (pp);
693 struct chunk_info *chunk_array = buffer->cur_chunk_array;
694 const char **args = chunk_array->args;
696 gcc_assert (buffer->obstack == &buffer->formatted_obstack);
697 gcc_assert (buffer->line_length == 0);
699 /* This is a third phase, first 2 phases done in pp_format_args.
700 Now we actually print it. */
701 for (chunk = 0; args[chunk]; chunk++)
702 pp_string (pp, args[chunk]);
704 /* Deallocate the chunk structure and everything after it (i.e. the
705 associated series of formatted strings). */
706 buffer->cur_chunk_array = chunk_array->prev;
707 obstack_free (&buffer->chunk_obstack, chunk_array);
710 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
711 settings needed by BUFFER for a verbatim formatting. */
712 void
713 pp_format_verbatim (pretty_printer *pp, text_info *text)
715 /* Set verbatim mode. */
716 pp_wrapping_mode_t oldmode = pp_set_verbatim_wrapping (pp);
718 /* Do the actual formatting. */
719 pp_format (pp, text);
720 pp_output_formatted_text (pp);
722 /* Restore previous settings. */
723 pp_wrapping_mode (pp) = oldmode;
726 /* Flush the content of BUFFER onto the attached stream. This
727 function does nothing unless pp->output_buffer->flush_p. */
728 void
729 pp_flush (pretty_printer *pp)
731 pp_clear_state (pp);
732 if (!pp->buffer->flush_p)
733 return;
734 pp_write_text_to_stream (pp);
735 fflush (pp_buffer (pp)->stream);
738 /* Flush the content of BUFFER onto the attached stream independently
739 of the value of pp->output_buffer->flush_p. */
740 void
741 pp_really_flush (pretty_printer *pp)
743 pp_clear_state (pp);
744 pp_write_text_to_stream (pp);
745 fflush (pp_buffer (pp)->stream);
748 /* Sets the number of maximum characters per line PRETTY-PRINTER can
749 output in line-wrapping mode. A LENGTH value 0 suppresses
750 line-wrapping. */
751 void
752 pp_set_line_maximum_length (pretty_printer *pp, int length)
754 pp_line_cutoff (pp) = length;
755 pp_set_real_maximum_length (pp);
758 /* Clear PRETTY-PRINTER output area text info. */
759 void
760 pp_clear_output_area (pretty_printer *pp)
762 obstack_free (pp_buffer (pp)->obstack,
763 obstack_base (pp_buffer (pp)->obstack));
764 pp_buffer (pp)->line_length = 0;
767 /* Set PREFIX for PRETTY-PRINTER. */
768 void
769 pp_set_prefix (pretty_printer *pp, const char *prefix)
771 pp->prefix = prefix;
772 pp_set_real_maximum_length (pp);
773 pp->emitted_prefix = false;
774 pp_indentation (pp) = 0;
777 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
778 void
779 pp_destroy_prefix (pretty_printer *pp)
781 if (pp->prefix != NULL)
783 free (CONST_CAST (char *, pp->prefix));
784 pp->prefix = NULL;
788 /* Write out PRETTY-PRINTER's prefix. */
789 void
790 pp_emit_prefix (pretty_printer *pp)
792 if (pp->prefix != NULL)
794 switch (pp_prefixing_rule (pp))
796 default:
797 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
798 break;
800 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
801 if (pp->emitted_prefix)
803 pp_indent (pp);
804 break;
806 pp_indentation (pp) += 3;
807 /* Fall through. */
809 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
811 int prefix_length = strlen (pp->prefix);
812 pp_append_r (pp, pp->prefix, prefix_length);
813 pp->emitted_prefix = true;
815 break;
820 /* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
821 characters per line. */
823 pretty_printer::pretty_printer (const char *p, int l)
824 : buffer (new (XCNEW (output_buffer)) output_buffer ()),
825 prefix (),
826 padding (pp_none),
827 maximum_length (),
828 indent_skip (),
829 wrapping (),
830 format_decoder (),
831 emitted_prefix (),
832 need_newline (),
833 translate_identifiers (true),
834 show_color ()
836 pp_line_cutoff (this) = l;
837 /* By default, we emit prefixes once per message. */
838 pp_prefixing_rule (this) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
839 pp_set_prefix (this, p);
842 pretty_printer::~pretty_printer ()
844 buffer->~output_buffer ();
845 XDELETE (buffer);
848 /* Append a string delimited by START and END to the output area of
849 PRETTY-PRINTER. No line wrapping is done. However, if beginning a
850 new line then emit PRETTY-PRINTER's prefix and skip any leading
851 whitespace if appropriate. The caller must ensure that it is
852 safe to do so. */
853 void
854 pp_append_text (pretty_printer *pp, const char *start, const char *end)
856 /* Emit prefix and skip whitespace if we're starting a new line. */
857 if (pp_buffer (pp)->line_length == 0)
859 pp_emit_prefix (pp);
860 if (pp_is_wrapping_line (pp))
861 while (start != end && *start == ' ')
862 ++start;
864 pp_append_r (pp, start, end - start);
867 /* Finishes constructing a NULL-terminated character string representing
868 the PRETTY-PRINTED text. */
869 const char *
870 pp_formatted_text (pretty_printer *pp)
872 return output_buffer_formatted_text (pp_buffer (pp));
875 /* Return a pointer to the last character emitted in PRETTY-PRINTER's
876 output area. A NULL pointer means no character available. */
877 const char *
878 pp_last_position_in_text (const pretty_printer *pp)
880 return output_buffer_last_position_in_text (pp_buffer (pp));
883 /* Return the amount of characters PRETTY-PRINTER can accept to
884 make a full line. Meaningful only in line-wrapping mode. */
886 pp_remaining_character_count_for_line (pretty_printer *pp)
888 return pp->maximum_length - pp_buffer (pp)->line_length;
892 /* Format a message into BUFFER a la printf. */
893 void
894 pp_printf (pretty_printer *pp, const char *msg, ...)
896 text_info text;
897 va_list ap;
899 va_start (ap, msg);
900 text.err_no = errno;
901 text.args_ptr = &ap;
902 text.format_spec = msg;
903 pp_format (pp, &text);
904 pp_output_formatted_text (pp);
905 va_end (ap);
909 /* Output MESSAGE verbatim into BUFFER. */
910 void
911 pp_verbatim (pretty_printer *pp, const char *msg, ...)
913 text_info text;
914 va_list ap;
916 va_start (ap, msg);
917 text.err_no = errno;
918 text.args_ptr = &ap;
919 text.format_spec = msg;
920 pp_format_verbatim (pp, &text);
921 va_end (ap);
926 /* Have PRETTY-PRINTER start a new line. */
927 void
928 pp_newline (pretty_printer *pp)
930 obstack_1grow (pp_buffer (pp)->obstack, '\n');
931 pp_needs_newline (pp) = false;
932 pp_buffer (pp)->line_length = 0;
935 /* Have PRETTY-PRINTER add a CHARACTER. */
936 void
937 pp_character (pretty_printer *pp, int c)
939 if (pp_is_wrapping_line (pp)
940 && pp_remaining_character_count_for_line (pp) <= 0)
942 pp_newline (pp);
943 if (ISSPACE (c))
944 return;
946 obstack_1grow (pp_buffer (pp)->obstack, c);
947 ++pp_buffer (pp)->line_length;
950 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
951 be line-wrapped if in appropriate mode. */
952 void
953 pp_string (pretty_printer *pp, const char *str)
955 gcc_checking_assert (str);
956 pp_maybe_wrap_text (pp, str, str + strlen (str));
959 /* Append the leading N characters of STRING to the output area of
960 PRETTY-PRINTER, quoting in hexadecimal non-printable characters.
961 Setting N = -1 is as if N were set to strlen (STRING). The STRING
962 may be line-wrapped if in appropriate mode. */
963 static void
964 pp_quoted_string (pretty_printer *pp, const char *str, size_t n /* = -1 */)
966 gcc_checking_assert (str);
968 const char *last = str;
969 const char *ps;
971 /* Compute the length if not specified. */
972 if (n == (size_t) -1)
973 n = strlen (str);
975 for (ps = str; n; ++ps, --n)
977 if (ISPRINT (*ps))
978 continue;
980 if (last < ps)
981 pp_maybe_wrap_text (pp, last, ps - 1);
983 /* Append the hexadecimal value of the character. Allocate a buffer
984 that's large enough for a 32-bit char plus the hex prefix. */
985 char buf [11];
986 int n = sprintf (buf, "\\x%02x", (unsigned char)*ps);
987 pp_maybe_wrap_text (pp, buf, buf + n);
988 last = ps + 1;
991 pp_maybe_wrap_text (pp, last, ps);
994 /* Maybe print out a whitespace if needed. */
996 void
997 pp_maybe_space (pretty_printer *pp)
999 if (pp->padding != pp_none)
1001 pp_space (pp);
1002 pp->padding = pp_none;
1006 // Add a newline to the pretty printer PP and flush formatted text.
1008 void
1009 pp_newline_and_flush (pretty_printer *pp)
1011 pp_newline (pp);
1012 pp_flush (pp);
1013 pp_needs_newline (pp) = false;
1016 // Add a newline to the pretty printer PP, followed by indentation.
1018 void
1019 pp_newline_and_indent (pretty_printer *pp, int n)
1021 pp_indentation (pp) += n;
1022 pp_newline (pp);
1023 pp_indent (pp);
1024 pp_needs_newline (pp) = false;
1027 // Add separator C, followed by a single whitespace.
1029 void
1030 pp_separate_with (pretty_printer *pp, char c)
1032 pp_character (pp, c);
1033 pp_space (pp);
1037 /* The string starting at P has LEN (at least 1) bytes left; if they
1038 start with a valid UTF-8 sequence, return the length of that
1039 sequence and set *VALUE to the value of that sequence, and
1040 otherwise return 0 and set *VALUE to (unsigned int) -1. */
1042 static int
1043 decode_utf8_char (const unsigned char *p, size_t len, unsigned int *value)
1045 unsigned int t = *p;
1047 if (len == 0)
1048 abort ();
1049 if (t & 0x80)
1051 size_t utf8_len = 0;
1052 unsigned int ch;
1053 size_t i;
1054 for (t = *p; t & 0x80; t <<= 1)
1055 utf8_len++;
1057 if (utf8_len > len || utf8_len < 2 || utf8_len > 6)
1059 *value = (unsigned int) -1;
1060 return 0;
1062 ch = *p & ((1 << (7 - utf8_len)) - 1);
1063 for (i = 1; i < utf8_len; i++)
1065 unsigned int u = p[i];
1066 if ((u & 0xC0) != 0x80)
1068 *value = (unsigned int) -1;
1069 return 0;
1071 ch = (ch << 6) | (u & 0x3F);
1073 if ( (ch <= 0x7F && utf8_len > 1)
1074 || (ch <= 0x7FF && utf8_len > 2)
1075 || (ch <= 0xFFFF && utf8_len > 3)
1076 || (ch <= 0x1FFFFF && utf8_len > 4)
1077 || (ch <= 0x3FFFFFF && utf8_len > 5)
1078 || (ch >= 0xD800 && ch <= 0xDFFF))
1080 *value = (unsigned int) -1;
1081 return 0;
1083 *value = ch;
1084 return utf8_len;
1086 else
1088 *value = t;
1089 return 1;
1093 /* Allocator for identifier_to_locale and corresponding function to
1094 free memory. */
1096 void *(*identifier_to_locale_alloc) (size_t) = xmalloc;
1097 void (*identifier_to_locale_free) (void *) = free;
1099 /* Given IDENT, an identifier in the internal encoding, return a
1100 version of IDENT suitable for diagnostics in the locale character
1101 set: either IDENT itself, or a string, allocated using
1102 identifier_to_locale_alloc, converted to the locale character set
1103 and using escape sequences if not representable in the locale
1104 character set or containing control characters or invalid byte
1105 sequences. Existing backslashes in IDENT are not doubled, so the
1106 result may not uniquely specify the contents of an arbitrary byte
1107 sequence identifier. */
1109 const char *
1110 identifier_to_locale (const char *ident)
1112 const unsigned char *uid = (const unsigned char *) ident;
1113 size_t idlen = strlen (ident);
1114 bool valid_printable_utf8 = true;
1115 bool all_ascii = true;
1116 size_t i;
1118 for (i = 0; i < idlen;)
1120 unsigned int c;
1121 size_t utf8_len = decode_utf8_char (&uid[i], idlen - i, &c);
1122 if (utf8_len == 0 || c <= 0x1F || (c >= 0x7F && c <= 0x9F))
1124 valid_printable_utf8 = false;
1125 break;
1127 if (utf8_len > 1)
1128 all_ascii = false;
1129 i += utf8_len;
1132 /* If IDENT contains invalid UTF-8 sequences (which may occur with
1133 attributes putting arbitrary byte sequences in identifiers), or
1134 control characters, we use octal escape sequences for all bytes
1135 outside printable ASCII. */
1136 if (!valid_printable_utf8)
1138 char *ret = (char *) identifier_to_locale_alloc (4 * idlen + 1);
1139 char *p = ret;
1140 for (i = 0; i < idlen; i++)
1142 if (uid[i] > 0x1F && uid[i] < 0x7F)
1143 *p++ = uid[i];
1144 else
1146 sprintf (p, "\\%03o", uid[i]);
1147 p += 4;
1150 *p = 0;
1151 return ret;
1154 /* Otherwise, if it is valid printable ASCII, or printable UTF-8
1155 with the locale character set being UTF-8, IDENT is used. */
1156 if (all_ascii || locale_utf8)
1157 return ident;
1159 /* Otherwise IDENT is converted to the locale character set if
1160 possible. */
1161 #if defined ENABLE_NLS && defined HAVE_LANGINFO_CODESET && HAVE_ICONV
1162 if (locale_encoding != NULL)
1164 iconv_t cd = iconv_open (locale_encoding, "UTF-8");
1165 bool conversion_ok = true;
1166 char *ret = NULL;
1167 if (cd != (iconv_t) -1)
1169 size_t ret_alloc = 4 * idlen + 1;
1170 for (;;)
1172 /* Repeat the whole conversion process as needed with
1173 larger buffers so non-reversible transformations can
1174 always be detected. */
1175 ICONV_CONST char *inbuf = CONST_CAST (char *, ident);
1176 char *outbuf;
1177 size_t inbytesleft = idlen;
1178 size_t outbytesleft = ret_alloc - 1;
1179 size_t iconv_ret;
1181 ret = (char *) identifier_to_locale_alloc (ret_alloc);
1182 outbuf = ret;
1184 if (iconv (cd, 0, 0, 0, 0) == (size_t) -1)
1186 conversion_ok = false;
1187 break;
1190 iconv_ret = iconv (cd, &inbuf, &inbytesleft,
1191 &outbuf, &outbytesleft);
1192 if (iconv_ret == (size_t) -1 || inbytesleft != 0)
1194 if (errno == E2BIG)
1196 ret_alloc *= 2;
1197 identifier_to_locale_free (ret);
1198 ret = NULL;
1199 continue;
1201 else
1203 conversion_ok = false;
1204 break;
1207 else if (iconv_ret != 0)
1209 conversion_ok = false;
1210 break;
1212 /* Return to initial shift state. */
1213 if (iconv (cd, 0, 0, &outbuf, &outbytesleft) == (size_t) -1)
1215 if (errno == E2BIG)
1217 ret_alloc *= 2;
1218 identifier_to_locale_free (ret);
1219 ret = NULL;
1220 continue;
1222 else
1224 conversion_ok = false;
1225 break;
1228 *outbuf = 0;
1229 break;
1231 iconv_close (cd);
1232 if (conversion_ok)
1233 return ret;
1236 #endif
1238 /* Otherwise, convert non-ASCII characters in IDENT to UCNs. */
1240 char *ret = (char *) identifier_to_locale_alloc (10 * idlen + 1);
1241 char *p = ret;
1242 for (i = 0; i < idlen;)
1244 unsigned int c;
1245 size_t utf8_len = decode_utf8_char (&uid[i], idlen - i, &c);
1246 if (utf8_len == 1)
1247 *p++ = uid[i];
1248 else
1250 sprintf (p, "\\U%08x", c);
1251 p += 10;
1253 i += utf8_len;
1255 *p = 0;
1256 return ret;
1260 #if CHECKING_P
1262 namespace selftest {
1264 /* Smoketest for pretty_printer. */
1266 static void
1267 test_basic_printing ()
1269 pretty_printer pp;
1270 pp_string (&pp, "hello");
1271 pp_space (&pp);
1272 pp_string (&pp, "world");
1274 ASSERT_STREQ ("hello world", pp_formatted_text (&pp));
1277 /* Helper function for testing pp_format.
1278 Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
1279 prints EXPECTED, assuming that pp_show_color is SHOW_COLOR. */
1281 static void
1282 assert_pp_format_va (const location &loc, const char *expected,
1283 bool show_color, const char *fmt, va_list *ap)
1285 pretty_printer pp;
1286 text_info ti;
1287 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1289 ti.format_spec = fmt;
1290 ti.args_ptr = ap;
1291 ti.err_no = 0;
1292 ti.x_data = NULL;
1293 ti.m_richloc = &rich_loc;
1295 pp_show_color (&pp) = show_color;
1296 pp_format (&pp, &ti);
1297 pp_output_formatted_text (&pp);
1298 ASSERT_STREQ_AT (loc, expected, pp_formatted_text (&pp));
1301 /* Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
1302 prints EXPECTED, with show_color disabled. */
1304 static void
1305 assert_pp_format (const location &loc, const char *expected,
1306 const char *fmt, ...)
1308 va_list ap;
1310 va_start (ap, fmt);
1311 assert_pp_format_va (loc, expected, false, fmt, &ap);
1312 va_end (ap);
1315 /* As above, but with colorization enabled. */
1317 static void
1318 assert_pp_format_colored (const location &loc, const char *expected,
1319 const char *fmt, ...)
1321 /* The tests of colorization assume the default color scheme.
1322 If GCC_COLORS is set, then the colors have potentially been
1323 overridden; skip the test. */
1324 if (getenv ("GCC_COLORS"))
1325 return;
1327 va_list ap;
1329 va_start (ap, fmt);
1330 assert_pp_format_va (loc, expected, true, fmt, &ap);
1331 va_end (ap);
1334 /* Helper function for calling testing pp_format,
1335 by calling assert_pp_format with various numbers of arguments.
1336 These exist mostly to avoid having to write SELFTEST_LOCATION
1337 throughout test_pp_format. */
1339 #define ASSERT_PP_FORMAT_1(EXPECTED, FMT, ARG1) \
1340 SELFTEST_BEGIN_STMT \
1341 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
1342 (ARG1)); \
1343 SELFTEST_END_STMT
1345 #define ASSERT_PP_FORMAT_2(EXPECTED, FMT, ARG1, ARG2) \
1346 SELFTEST_BEGIN_STMT \
1347 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
1348 (ARG1), (ARG2)); \
1349 SELFTEST_END_STMT
1351 #define ASSERT_PP_FORMAT_3(EXPECTED, FMT, ARG1, ARG2, ARG3) \
1352 SELFTEST_BEGIN_STMT \
1353 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
1354 (ARG1), (ARG2), (ARG3)); \
1355 SELFTEST_END_STMT
1357 /* Verify that pp_format works, for various format codes. */
1359 static void
1360 test_pp_format ()
1362 /* Avoid introducing locale-specific differences in the results
1363 by hardcoding open_quote and close_quote. */
1364 const char *old_open_quote = open_quote;
1365 const char *old_close_quote = close_quote;
1366 open_quote = "`";
1367 close_quote = "'";
1369 /* Verify that plain text is passed through unchanged. */
1370 assert_pp_format (SELFTEST_LOCATION, "unformatted", "unformatted");
1372 /* Verify various individual format codes, in the order listed in the
1373 comment for pp_format above. For each code, we append a second
1374 argument with a known bit pattern (0x12345678), to ensure that we
1375 are consuming arguments correctly. */
1376 ASSERT_PP_FORMAT_2 ("-27 12345678", "%d %x", -27, 0x12345678);
1377 ASSERT_PP_FORMAT_2 ("-5 12345678", "%i %x", -5, 0x12345678);
1378 ASSERT_PP_FORMAT_2 ("10 12345678", "%u %x", 10, 0x12345678);
1379 ASSERT_PP_FORMAT_2 ("17 12345678", "%o %x", 15, 0x12345678);
1380 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%x %x", 0xcafebabe, 0x12345678);
1381 ASSERT_PP_FORMAT_2 ("-27 12345678", "%ld %x", (long)-27, 0x12345678);
1382 ASSERT_PP_FORMAT_2 ("-5 12345678", "%li %x", (long)-5, 0x12345678);
1383 ASSERT_PP_FORMAT_2 ("10 12345678", "%lu %x", (long)10, 0x12345678);
1384 ASSERT_PP_FORMAT_2 ("17 12345678", "%lo %x", (long)15, 0x12345678);
1385 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%lx %x", (long)0xcafebabe,
1386 0x12345678);
1387 ASSERT_PP_FORMAT_2 ("-27 12345678", "%lld %x", (long long)-27, 0x12345678);
1388 ASSERT_PP_FORMAT_2 ("-5 12345678", "%lli %x", (long long)-5, 0x12345678);
1389 ASSERT_PP_FORMAT_2 ("10 12345678", "%llu %x", (long long)10, 0x12345678);
1390 ASSERT_PP_FORMAT_2 ("17 12345678", "%llo %x", (long long)15, 0x12345678);
1391 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%llx %x", (long long)0xcafebabe,
1392 0x12345678);
1393 ASSERT_PP_FORMAT_2 ("-27 12345678", "%wd %x", (HOST_WIDE_INT)-27, 0x12345678);
1394 ASSERT_PP_FORMAT_2 ("-5 12345678", "%wi %x", (HOST_WIDE_INT)-5, 0x12345678);
1395 ASSERT_PP_FORMAT_2 ("10 12345678", "%wu %x", (unsigned HOST_WIDE_INT)10,
1396 0x12345678);
1397 ASSERT_PP_FORMAT_2 ("17 12345678", "%wo %x", (HOST_WIDE_INT)15, 0x12345678);
1398 ASSERT_PP_FORMAT_2 ("0xcafebabe 12345678", "%wx %x", (HOST_WIDE_INT)0xcafebabe,
1399 0x12345678);
1400 ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678);
1401 ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello world",
1402 0x12345678);
1403 /* We can't test for %p; the pointer is printed in an implementation-defined
1404 manner. */
1405 ASSERT_PP_FORMAT_2 ("normal colored normal 12345678",
1406 "normal %rcolored%R normal %x",
1407 "error", 0x12345678);
1408 assert_pp_format_colored
1409 (SELFTEST_LOCATION,
1410 "normal \33[01;31m\33[Kcolored\33[m\33[K normal 12345678",
1411 "normal %rcolored%R normal %x", "error", 0x12345678);
1412 /* TODO:
1413 %m: strerror(text->err_no) - does not consume a value from args_ptr. */
1414 ASSERT_PP_FORMAT_1 ("% 12345678", "%% %x", 0x12345678);
1415 ASSERT_PP_FORMAT_1 ("` 12345678", "%< %x", 0x12345678);
1416 ASSERT_PP_FORMAT_1 ("' 12345678", "%> %x", 0x12345678);
1417 ASSERT_PP_FORMAT_1 ("' 12345678", "%' %x", 0x12345678);
1418 ASSERT_PP_FORMAT_3 ("abc 12345678", "%.*s %x", 3, "abcdef", 0x12345678);
1419 ASSERT_PP_FORMAT_2 ("abc 12345678", "%.3s %x", "abcdef", 0x12345678);
1421 /* Verify flag 'q'. */
1422 ASSERT_PP_FORMAT_2 ("`foo' 12345678", "%qs %x", "foo", 0x12345678);
1423 assert_pp_format_colored (SELFTEST_LOCATION,
1424 "`\33[01m\33[Kfoo\33[m\33[K' 12345678", "%qs %x",
1425 "foo", 0x12345678);
1427 /* Verify that combinations work, along with unformatted text. */
1428 assert_pp_format (SELFTEST_LOCATION,
1429 "the quick brown fox jumps over the lazy dog",
1430 "the %s %s %s jumps over the %s %s",
1431 "quick", "brown", "fox", "lazy", "dog");
1432 assert_pp_format (SELFTEST_LOCATION, "item 3 of 7", "item %i of %i", 3, 7);
1433 assert_pp_format (SELFTEST_LOCATION, "problem with `bar' at line 10",
1434 "problem with %qs at line %i", "bar", 10);
1436 /* Restore old values of open_quote and close_quote. */
1437 open_quote = old_open_quote;
1438 close_quote = old_close_quote;
1441 /* Run all of the selftests within this file. */
1443 void
1444 pretty_print_c_tests ()
1446 test_basic_printing ();
1447 test_pp_format ();
1450 } // namespace selftest
1452 #endif /* CHECKING_P */