PR rtl-optimization/71673
[official-gcc.git] / gcc / pretty-print.c
blob325263efd6a53507ca45a368d1cbd4641284bbf1
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 /* Overwrite the given location/range within this text_info's rich_location.
34 For use e.g. when implementing "+" in client format decoders. */
36 void
37 text_info::set_location (unsigned int idx, location_t loc, bool show_caret_p)
39 gcc_checking_assert (m_richloc);
40 m_richloc->set_range (line_table, idx, loc, show_caret_p);
43 location_t
44 text_info::get_location (unsigned int index_of_location) const
46 gcc_checking_assert (m_richloc);
48 if (index_of_location == 0)
49 return m_richloc->get_loc ();
50 else
51 return UNKNOWN_LOCATION;
54 // Default construct an output buffer.
56 output_buffer::output_buffer ()
57 : formatted_obstack (),
58 chunk_obstack (),
59 obstack (&formatted_obstack),
60 cur_chunk_array (),
61 stream (stderr),
62 line_length (),
63 digit_buffer (),
64 flush_p (true)
66 obstack_init (&formatted_obstack);
67 obstack_init (&chunk_obstack);
70 // Release resources owned by an output buffer at the end of lifetime.
72 output_buffer::~output_buffer ()
74 obstack_free (&chunk_obstack, NULL);
75 obstack_free (&formatted_obstack, NULL);
79 /* Format an integer given by va_arg (ARG, type-specifier T) where
80 type-specifier is a precision modifier as indicated by PREC. F is
81 a string used to construct the appropriate format-specifier. */
82 #define pp_integer_with_precision(PP, ARG, PREC, T, F) \
83 do \
84 switch (PREC) \
85 { \
86 case 0: \
87 pp_scalar (PP, "%" F, va_arg (ARG, T)); \
88 break; \
90 case 1: \
91 pp_scalar (PP, "%l" F, va_arg (ARG, long T)); \
92 break; \
94 case 2: \
95 pp_scalar (PP, "%" HOST_LONG_LONG_FORMAT F, va_arg (ARG, long long T)); \
96 break; \
98 default: \
99 break; \
101 while (0)
104 /* Subroutine of pp_set_maximum_length. Set up PRETTY-PRINTER's
105 internal maximum characters per line. */
106 static void
107 pp_set_real_maximum_length (pretty_printer *pp)
109 /* If we're told not to wrap lines then do the obvious thing. In case
110 we'll emit prefix only once per message, it is appropriate
111 not to increase unnecessarily the line-length cut-off. */
112 if (!pp_is_wrapping_line (pp)
113 || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_ONCE
114 || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
115 pp->maximum_length = pp_line_cutoff (pp);
116 else
118 int prefix_length = pp->prefix ? strlen (pp->prefix) : 0;
119 /* If the prefix is ridiculously too long, output at least
120 32 characters. */
121 if (pp_line_cutoff (pp) - prefix_length < 32)
122 pp->maximum_length = pp_line_cutoff (pp) + 32;
123 else
124 pp->maximum_length = pp_line_cutoff (pp);
128 /* Clear PRETTY-PRINTER's output state. */
129 static inline void
130 pp_clear_state (pretty_printer *pp)
132 pp->emitted_prefix = false;
133 pp_indentation (pp) = 0;
136 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream. */
137 void
138 pp_write_text_to_stream (pretty_printer *pp)
140 const char *text = pp_formatted_text (pp);
141 fputs (text, pp_buffer (pp)->stream);
142 pp_clear_output_area (pp);
145 /* As pp_write_text_to_stream, but for GraphViz label output.
147 Flush the formatted text of pretty-printer PP onto the attached stream.
148 Replace characters in PPF that have special meaning in a GraphViz .dot
149 file.
151 This routine is not very fast, but it doesn't have to be as this is only
152 be used by routines dumping intermediate representations in graph form. */
154 void
155 pp_write_text_as_dot_label_to_stream (pretty_printer *pp, bool for_record)
157 const char *text = pp_formatted_text (pp);
158 const char *p = text;
159 FILE *fp = pp_buffer (pp)->stream;
161 for (;*p; p++)
163 bool escape_char;
164 switch (*p)
166 /* Print newlines as a left-aligned newline. */
167 case '\n':
168 fputs ("\\l", fp);
169 escape_char = true;
170 break;
172 /* The following characters are only special for record-shape nodes. */
173 case '|':
174 case '{':
175 case '}':
176 case '<':
177 case '>':
178 case ' ':
179 escape_char = for_record;
180 break;
182 /* The following characters always have to be escaped
183 for use in labels. */
184 case '\\':
185 /* There is a bug in some (f.i. 2.36.0) versions of graphiz
186 ( http://www.graphviz.org/mantisbt/view.php?id=2524 ) related to
187 backslash as last char in label. Let's avoid triggering it. */
188 gcc_assert (*(p + 1) != '\0');
189 /* Fall through. */
190 case '"':
191 escape_char = true;
192 break;
194 default:
195 escape_char = false;
196 break;
199 if (escape_char)
200 fputc ('\\', fp);
202 fputc (*p, fp);
205 pp_clear_output_area (pp);
208 /* Wrap a text delimited by START and END into PRETTY-PRINTER. */
209 static void
210 pp_wrap_text (pretty_printer *pp, const char *start, const char *end)
212 bool wrapping_line = pp_is_wrapping_line (pp);
214 while (start != end)
216 /* Dump anything bordered by whitespaces. */
218 const char *p = start;
219 while (p != end && !ISBLANK (*p) && *p != '\n')
220 ++p;
221 if (wrapping_line
222 && p - start >= pp_remaining_character_count_for_line (pp))
223 pp_newline (pp);
224 pp_append_text (pp, start, p);
225 start = p;
228 if (start != end && ISBLANK (*start))
230 pp_space (pp);
231 ++start;
233 if (start != end && *start == '\n')
235 pp_newline (pp);
236 ++start;
241 /* Same as pp_wrap_text but wrap text only when in line-wrapping mode. */
242 static inline void
243 pp_maybe_wrap_text (pretty_printer *pp, const char *start, const char *end)
245 if (pp_is_wrapping_line (pp))
246 pp_wrap_text (pp, start, end);
247 else
248 pp_append_text (pp, start, end);
251 /* Append to the output area of PRETTY-PRINTER a string specified by its
252 STARTing character and LENGTH. */
253 static inline void
254 pp_append_r (pretty_printer *pp, const char *start, int length)
256 output_buffer_append_r (pp_buffer (pp), start, length);
259 /* Insert enough spaces into the output area of PRETTY-PRINTER to bring
260 the column position to the current indentation level, assuming that a
261 newline has just been written to the buffer. */
262 void
263 pp_indent (pretty_printer *pp)
265 int n = pp_indentation (pp);
266 int i;
268 for (i = 0; i < n; ++i)
269 pp_space (pp);
272 /* The following format specifiers are recognized as being client independent:
273 %d, %i: (signed) integer in base ten.
274 %u: unsigned integer in base ten.
275 %o: unsigned integer in base eight.
276 %x: unsigned integer in base sixteen.
277 %ld, %li, %lo, %lu, %lx: long versions of the above.
278 %lld, %lli, %llo, %llu, %llx: long long versions.
279 %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
280 %c: character.
281 %s: string.
282 %p: pointer (printed in a host-dependent manner).
283 %r: if pp_show_color(pp), switch to color identified by const char *.
284 %R: if pp_show_color(pp), reset color.
285 %m: strerror(text->err_no) - does not consume a value from args_ptr.
286 %%: '%'.
287 %<: opening quote.
288 %>: closing quote.
289 %': apostrophe (should only be used in untranslated messages;
290 translations should use appropriate punctuation directly).
291 %.*s: a substring the length of which is specified by an argument
292 integer.
293 %Ns: likewise, but length specified as constant in the format string.
294 Flag 'q': quote formatted text (must come immediately after '%').
296 Arguments can be used sequentially, or through %N$ resp. *N$
297 notation Nth argument after the format string. If %N$ / *N$
298 notation is used, it must be used for all arguments, except %m, %%,
299 %<, %> and %', which may not have a number, as they do not consume
300 an argument. When %M$.*N$s is used, M must be N + 1. (This may
301 also be written %M$.*s, provided N is not otherwise used.) The
302 format string must have conversion specifiers with argument numbers
303 1 up to highest argument; each argument may only be used once.
304 A format string can have at most 30 arguments. */
306 /* Formatting phases 1 and 2: render TEXT->format_spec plus
307 TEXT->args_ptr into a series of chunks in pp_buffer (PP)->args[].
308 Phase 3 is in pp_output_formatted_text. */
310 void
311 pp_format (pretty_printer *pp, text_info *text)
313 output_buffer *buffer = pp_buffer (pp);
314 const char *p;
315 const char **args;
316 struct chunk_info *new_chunk_array;
318 unsigned int curarg = 0, chunk = 0, argno;
319 pp_wrapping_mode_t old_wrapping_mode;
320 bool any_unnumbered = false, any_numbered = false;
321 const char **formatters[PP_NL_ARGMAX];
323 /* Allocate a new chunk structure. */
324 new_chunk_array = XOBNEW (&buffer->chunk_obstack, struct chunk_info);
325 new_chunk_array->prev = buffer->cur_chunk_array;
326 buffer->cur_chunk_array = new_chunk_array;
327 args = new_chunk_array->args;
329 /* Formatting phase 1: split up TEXT->format_spec into chunks in
330 pp_buffer (PP)->args[]. Even-numbered chunks are to be output
331 verbatim, odd-numbered chunks are format specifiers.
332 %m, %%, %<, %>, and %' are replaced with the appropriate text at
333 this point. */
335 memset (formatters, 0, sizeof formatters);
337 for (p = text->format_spec; *p; )
339 while (*p != '\0' && *p != '%')
341 obstack_1grow (&buffer->chunk_obstack, *p);
342 p++;
345 if (*p == '\0')
346 break;
348 switch (*++p)
350 case '\0':
351 gcc_unreachable ();
353 case '%':
354 obstack_1grow (&buffer->chunk_obstack, '%');
355 p++;
356 continue;
358 case '<':
360 obstack_grow (&buffer->chunk_obstack,
361 open_quote, strlen (open_quote));
362 const char *colorstr
363 = colorize_start (pp_show_color (pp), "quote");
364 obstack_grow (&buffer->chunk_obstack, colorstr, strlen (colorstr));
365 p++;
366 continue;
369 case '>':
371 const char *colorstr = colorize_stop (pp_show_color (pp));
372 obstack_grow (&buffer->chunk_obstack, colorstr, strlen (colorstr));
374 /* FALLTHRU */
375 case '\'':
376 obstack_grow (&buffer->chunk_obstack,
377 close_quote, strlen (close_quote));
378 p++;
379 continue;
381 case 'R':
383 const char *colorstr = colorize_stop (pp_show_color (pp));
384 obstack_grow (&buffer->chunk_obstack, colorstr,
385 strlen (colorstr));
386 p++;
387 continue;
390 case 'm':
392 const char *errstr = xstrerror (text->err_no);
393 obstack_grow (&buffer->chunk_obstack, errstr, strlen (errstr));
395 p++;
396 continue;
398 default:
399 /* Handled in phase 2. Terminate the plain chunk here. */
400 obstack_1grow (&buffer->chunk_obstack, '\0');
401 gcc_assert (chunk < PP_NL_ARGMAX * 2);
402 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
403 break;
406 if (ISDIGIT (*p))
408 char *end;
409 argno = strtoul (p, &end, 10) - 1;
410 p = end;
411 gcc_assert (*p == '$');
412 p++;
414 any_numbered = true;
415 gcc_assert (!any_unnumbered);
417 else
419 argno = curarg++;
420 any_unnumbered = true;
421 gcc_assert (!any_numbered);
423 gcc_assert (argno < PP_NL_ARGMAX);
424 gcc_assert (!formatters[argno]);
425 formatters[argno] = &args[chunk];
428 obstack_1grow (&buffer->chunk_obstack, *p);
429 p++;
431 while (strchr ("qwl+#", p[-1]));
433 if (p[-1] == '.')
435 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
436 (where M == N + 1). */
437 if (ISDIGIT (*p))
441 obstack_1grow (&buffer->chunk_obstack, *p);
442 p++;
444 while (ISDIGIT (p[-1]));
445 gcc_assert (p[-1] == 's');
447 else
449 gcc_assert (*p == '*');
450 obstack_1grow (&buffer->chunk_obstack, '*');
451 p++;
453 if (ISDIGIT (*p))
455 char *end;
456 unsigned int argno2 = strtoul (p, &end, 10) - 1;
457 p = end;
458 gcc_assert (argno2 == argno - 1);
459 gcc_assert (!any_unnumbered);
460 gcc_assert (*p == '$');
462 p++;
463 formatters[argno2] = formatters[argno];
465 else
467 gcc_assert (!any_numbered);
468 formatters[argno+1] = formatters[argno];
469 curarg++;
471 gcc_assert (*p == 's');
472 obstack_1grow (&buffer->chunk_obstack, 's');
473 p++;
476 if (*p == '\0')
477 break;
479 obstack_1grow (&buffer->chunk_obstack, '\0');
480 gcc_assert (chunk < PP_NL_ARGMAX * 2);
481 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
484 obstack_1grow (&buffer->chunk_obstack, '\0');
485 gcc_assert (chunk < PP_NL_ARGMAX * 2);
486 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
487 args[chunk] = 0;
489 /* Set output to the argument obstack, and switch line-wrapping and
490 prefixing off. */
491 buffer->obstack = &buffer->chunk_obstack;
492 old_wrapping_mode = pp_set_verbatim_wrapping (pp);
494 /* Second phase. Replace each formatter with the formatted text it
495 corresponds to. */
497 for (argno = 0; formatters[argno]; argno++)
499 int precision = 0;
500 bool wide = false;
501 bool plus = false;
502 bool hash = false;
503 bool quote = false;
505 /* We do not attempt to enforce any ordering on the modifier
506 characters. */
508 for (p = *formatters[argno];; p++)
510 switch (*p)
512 case 'q':
513 gcc_assert (!quote);
514 quote = true;
515 continue;
517 case '+':
518 gcc_assert (!plus);
519 plus = true;
520 continue;
522 case '#':
523 gcc_assert (!hash);
524 hash = true;
525 continue;
527 case 'w':
528 gcc_assert (!wide);
529 wide = true;
530 continue;
532 case 'l':
533 /* We don't support precision beyond that of "long long". */
534 gcc_assert (precision < 2);
535 precision++;
536 continue;
538 break;
541 gcc_assert (!wide || precision == 0);
543 if (quote)
545 pp_string (pp, open_quote);
546 pp_string (pp, colorize_start (pp_show_color (pp), "quote"));
549 switch (*p)
551 case 'r':
552 pp_string (pp, colorize_start (pp_show_color (pp),
553 va_arg (*text->args_ptr,
554 const char *)));
555 break;
557 case 'c':
558 pp_character (pp, va_arg (*text->args_ptr, int));
559 break;
561 case 'd':
562 case 'i':
563 if (wide)
564 pp_wide_integer (pp, va_arg (*text->args_ptr, HOST_WIDE_INT));
565 else
566 pp_integer_with_precision
567 (pp, *text->args_ptr, precision, int, "d");
568 break;
570 case 'o':
571 if (wide)
572 pp_scalar (pp, "%" HOST_WIDE_INT_PRINT "o",
573 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
574 else
575 pp_integer_with_precision
576 (pp, *text->args_ptr, precision, unsigned, "o");
577 break;
579 case 's':
580 pp_string (pp, va_arg (*text->args_ptr, const char *));
581 break;
583 case 'p':
584 pp_pointer (pp, va_arg (*text->args_ptr, void *));
585 break;
587 case 'u':
588 if (wide)
589 pp_scalar (pp, HOST_WIDE_INT_PRINT_UNSIGNED,
590 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
591 else
592 pp_integer_with_precision
593 (pp, *text->args_ptr, precision, unsigned, "u");
594 break;
596 case 'x':
597 if (wide)
598 pp_scalar (pp, HOST_WIDE_INT_PRINT_HEX,
599 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
600 else
601 pp_integer_with_precision
602 (pp, *text->args_ptr, precision, unsigned, "x");
603 break;
605 case '.':
607 int n;
608 const char *s;
610 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
611 (where M == N + 1). The format string should be verified
612 already from the first phase. */
613 p++;
614 if (ISDIGIT (*p))
616 char *end;
617 n = strtoul (p, &end, 10);
618 p = end;
619 gcc_assert (*p == 's');
621 else
623 gcc_assert (*p == '*');
624 p++;
625 gcc_assert (*p == 's');
626 n = va_arg (*text->args_ptr, int);
628 /* This consumes a second entry in the formatters array. */
629 gcc_assert (formatters[argno] == formatters[argno+1]);
630 argno++;
633 s = va_arg (*text->args_ptr, const char *);
634 pp_append_text (pp, s, s + n);
636 break;
638 default:
640 bool ok;
642 gcc_assert (pp_format_decoder (pp));
643 ok = pp_format_decoder (pp) (pp, text, p,
644 precision, wide, plus, hash);
645 gcc_assert (ok);
649 if (quote)
651 pp_string (pp, colorize_stop (pp_show_color (pp)));
652 pp_string (pp, close_quote);
655 obstack_1grow (&buffer->chunk_obstack, '\0');
656 *formatters[argno] = XOBFINISH (&buffer->chunk_obstack, const char *);
659 if (CHECKING_P)
660 for (; argno < PP_NL_ARGMAX; argno++)
661 gcc_assert (!formatters[argno]);
663 /* Revert to normal obstack and wrapping mode. */
664 buffer->obstack = &buffer->formatted_obstack;
665 buffer->line_length = 0;
666 pp_wrapping_mode (pp) = old_wrapping_mode;
667 pp_clear_state (pp);
670 /* Format of a message pointed to by TEXT. */
671 void
672 pp_output_formatted_text (pretty_printer *pp)
674 unsigned int chunk;
675 output_buffer *buffer = pp_buffer (pp);
676 struct chunk_info *chunk_array = buffer->cur_chunk_array;
677 const char **args = chunk_array->args;
679 gcc_assert (buffer->obstack == &buffer->formatted_obstack);
680 gcc_assert (buffer->line_length == 0);
682 /* This is a third phase, first 2 phases done in pp_format_args.
683 Now we actually print it. */
684 for (chunk = 0; args[chunk]; chunk++)
685 pp_string (pp, args[chunk]);
687 /* Deallocate the chunk structure and everything after it (i.e. the
688 associated series of formatted strings). */
689 buffer->cur_chunk_array = chunk_array->prev;
690 obstack_free (&buffer->chunk_obstack, chunk_array);
693 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
694 settings needed by BUFFER for a verbatim formatting. */
695 void
696 pp_format_verbatim (pretty_printer *pp, text_info *text)
698 /* Set verbatim mode. */
699 pp_wrapping_mode_t oldmode = pp_set_verbatim_wrapping (pp);
701 /* Do the actual formatting. */
702 pp_format (pp, text);
703 pp_output_formatted_text (pp);
705 /* Restore previous settings. */
706 pp_wrapping_mode (pp) = oldmode;
709 /* Flush the content of BUFFER onto the attached stream. This
710 function does nothing unless pp->output_buffer->flush_p. */
711 void
712 pp_flush (pretty_printer *pp)
714 pp_clear_state (pp);
715 if (!pp->buffer->flush_p)
716 return;
717 pp_write_text_to_stream (pp);
718 fflush (pp_buffer (pp)->stream);
721 /* Flush the content of BUFFER onto the attached stream independently
722 of the value of pp->output_buffer->flush_p. */
723 void
724 pp_really_flush (pretty_printer *pp)
726 pp_clear_state (pp);
727 pp_write_text_to_stream (pp);
728 fflush (pp_buffer (pp)->stream);
731 /* Sets the number of maximum characters per line PRETTY-PRINTER can
732 output in line-wrapping mode. A LENGTH value 0 suppresses
733 line-wrapping. */
734 void
735 pp_set_line_maximum_length (pretty_printer *pp, int length)
737 pp_line_cutoff (pp) = length;
738 pp_set_real_maximum_length (pp);
741 /* Clear PRETTY-PRINTER output area text info. */
742 void
743 pp_clear_output_area (pretty_printer *pp)
745 obstack_free (pp_buffer (pp)->obstack,
746 obstack_base (pp_buffer (pp)->obstack));
747 pp_buffer (pp)->line_length = 0;
750 /* Set PREFIX for PRETTY-PRINTER. */
751 void
752 pp_set_prefix (pretty_printer *pp, const char *prefix)
754 pp->prefix = prefix;
755 pp_set_real_maximum_length (pp);
756 pp->emitted_prefix = false;
757 pp_indentation (pp) = 0;
760 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
761 void
762 pp_destroy_prefix (pretty_printer *pp)
764 if (pp->prefix != NULL)
766 free (CONST_CAST (char *, pp->prefix));
767 pp->prefix = NULL;
771 /* Write out PRETTY-PRINTER's prefix. */
772 void
773 pp_emit_prefix (pretty_printer *pp)
775 if (pp->prefix != NULL)
777 switch (pp_prefixing_rule (pp))
779 default:
780 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
781 break;
783 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
784 if (pp->emitted_prefix)
786 pp_indent (pp);
787 break;
789 pp_indentation (pp) += 3;
790 /* Fall through. */
792 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
794 int prefix_length = strlen (pp->prefix);
795 pp_append_r (pp, pp->prefix, prefix_length);
796 pp->emitted_prefix = true;
798 break;
803 /* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
804 characters per line. */
806 pretty_printer::pretty_printer (const char *p, int l)
807 : buffer (new (XCNEW (output_buffer)) output_buffer ()),
808 prefix (),
809 padding (pp_none),
810 maximum_length (),
811 indent_skip (),
812 wrapping (),
813 format_decoder (),
814 emitted_prefix (),
815 need_newline (),
816 translate_identifiers (true),
817 show_color ()
819 pp_line_cutoff (this) = l;
820 /* By default, we emit prefixes once per message. */
821 pp_prefixing_rule (this) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
822 pp_set_prefix (this, p);
825 pretty_printer::~pretty_printer ()
827 buffer->~output_buffer ();
828 XDELETE (buffer);
831 /* Append a string delimited by START and END to the output area of
832 PRETTY-PRINTER. No line wrapping is done. However, if beginning a
833 new line then emit PRETTY-PRINTER's prefix and skip any leading
834 whitespace if appropriate. The caller must ensure that it is
835 safe to do so. */
836 void
837 pp_append_text (pretty_printer *pp, const char *start, const char *end)
839 /* Emit prefix and skip whitespace if we're starting a new line. */
840 if (pp_buffer (pp)->line_length == 0)
842 pp_emit_prefix (pp);
843 if (pp_is_wrapping_line (pp))
844 while (start != end && *start == ' ')
845 ++start;
847 pp_append_r (pp, start, end - start);
850 /* Finishes constructing a NULL-terminated character string representing
851 the PRETTY-PRINTED text. */
852 const char *
853 pp_formatted_text (pretty_printer *pp)
855 return output_buffer_formatted_text (pp_buffer (pp));
858 /* Return a pointer to the last character emitted in PRETTY-PRINTER's
859 output area. A NULL pointer means no character available. */
860 const char *
861 pp_last_position_in_text (const pretty_printer *pp)
863 return output_buffer_last_position_in_text (pp_buffer (pp));
866 /* Return the amount of characters PRETTY-PRINTER can accept to
867 make a full line. Meaningful only in line-wrapping mode. */
869 pp_remaining_character_count_for_line (pretty_printer *pp)
871 return pp->maximum_length - pp_buffer (pp)->line_length;
875 /* Format a message into BUFFER a la printf. */
876 void
877 pp_printf (pretty_printer *pp, const char *msg, ...)
879 text_info text;
880 va_list ap;
882 va_start (ap, msg);
883 text.err_no = errno;
884 text.args_ptr = &ap;
885 text.format_spec = msg;
886 pp_format (pp, &text);
887 pp_output_formatted_text (pp);
888 va_end (ap);
892 /* Output MESSAGE verbatim into BUFFER. */
893 void
894 pp_verbatim (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_verbatim (pp, &text);
904 va_end (ap);
909 /* Have PRETTY-PRINTER start a new line. */
910 void
911 pp_newline (pretty_printer *pp)
913 obstack_1grow (pp_buffer (pp)->obstack, '\n');
914 pp_needs_newline (pp) = false;
915 pp_buffer (pp)->line_length = 0;
918 /* Have PRETTY-PRINTER add a CHARACTER. */
919 void
920 pp_character (pretty_printer *pp, int c)
922 if (pp_is_wrapping_line (pp)
923 && pp_remaining_character_count_for_line (pp) <= 0)
925 pp_newline (pp);
926 if (ISSPACE (c))
927 return;
929 obstack_1grow (pp_buffer (pp)->obstack, c);
930 ++pp_buffer (pp)->line_length;
933 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
934 be line-wrapped if in appropriate mode. */
935 void
936 pp_string (pretty_printer *pp, const char *str)
938 gcc_checking_assert (str);
939 pp_maybe_wrap_text (pp, str, str + strlen (str));
942 /* Maybe print out a whitespace if needed. */
944 void
945 pp_maybe_space (pretty_printer *pp)
947 if (pp->padding != pp_none)
949 pp_space (pp);
950 pp->padding = pp_none;
954 // Add a newline to the pretty printer PP and flush formatted text.
956 void
957 pp_newline_and_flush (pretty_printer *pp)
959 pp_newline (pp);
960 pp_flush (pp);
961 pp_needs_newline (pp) = false;
964 // Add a newline to the pretty printer PP, followed by indentation.
966 void
967 pp_newline_and_indent (pretty_printer *pp, int n)
969 pp_indentation (pp) += n;
970 pp_newline (pp);
971 pp_indent (pp);
972 pp_needs_newline (pp) = false;
975 // Add separator C, followed by a single whitespace.
977 void
978 pp_separate_with (pretty_printer *pp, char c)
980 pp_character (pp, c);
981 pp_space (pp);
985 /* The string starting at P has LEN (at least 1) bytes left; if they
986 start with a valid UTF-8 sequence, return the length of that
987 sequence and set *VALUE to the value of that sequence, and
988 otherwise return 0 and set *VALUE to (unsigned int) -1. */
990 static int
991 decode_utf8_char (const unsigned char *p, size_t len, unsigned int *value)
993 unsigned int t = *p;
995 if (len == 0)
996 abort ();
997 if (t & 0x80)
999 size_t utf8_len = 0;
1000 unsigned int ch;
1001 size_t i;
1002 for (t = *p; t & 0x80; t <<= 1)
1003 utf8_len++;
1005 if (utf8_len > len || utf8_len < 2 || utf8_len > 6)
1007 *value = (unsigned int) -1;
1008 return 0;
1010 ch = *p & ((1 << (7 - utf8_len)) - 1);
1011 for (i = 1; i < utf8_len; i++)
1013 unsigned int u = p[i];
1014 if ((u & 0xC0) != 0x80)
1016 *value = (unsigned int) -1;
1017 return 0;
1019 ch = (ch << 6) | (u & 0x3F);
1021 if ( (ch <= 0x7F && utf8_len > 1)
1022 || (ch <= 0x7FF && utf8_len > 2)
1023 || (ch <= 0xFFFF && utf8_len > 3)
1024 || (ch <= 0x1FFFFF && utf8_len > 4)
1025 || (ch <= 0x3FFFFFF && utf8_len > 5)
1026 || (ch >= 0xD800 && ch <= 0xDFFF))
1028 *value = (unsigned int) -1;
1029 return 0;
1031 *value = ch;
1032 return utf8_len;
1034 else
1036 *value = t;
1037 return 1;
1041 /* Allocator for identifier_to_locale and corresponding function to
1042 free memory. */
1044 void *(*identifier_to_locale_alloc) (size_t) = xmalloc;
1045 void (*identifier_to_locale_free) (void *) = free;
1047 /* Given IDENT, an identifier in the internal encoding, return a
1048 version of IDENT suitable for diagnostics in the locale character
1049 set: either IDENT itself, or a string, allocated using
1050 identifier_to_locale_alloc, converted to the locale character set
1051 and using escape sequences if not representable in the locale
1052 character set or containing control characters or invalid byte
1053 sequences. Existing backslashes in IDENT are not doubled, so the
1054 result may not uniquely specify the contents of an arbitrary byte
1055 sequence identifier. */
1057 const char *
1058 identifier_to_locale (const char *ident)
1060 const unsigned char *uid = (const unsigned char *) ident;
1061 size_t idlen = strlen (ident);
1062 bool valid_printable_utf8 = true;
1063 bool all_ascii = true;
1064 size_t i;
1066 for (i = 0; i < idlen;)
1068 unsigned int c;
1069 size_t utf8_len = decode_utf8_char (&uid[i], idlen - i, &c);
1070 if (utf8_len == 0 || c <= 0x1F || (c >= 0x7F && c <= 0x9F))
1072 valid_printable_utf8 = false;
1073 break;
1075 if (utf8_len > 1)
1076 all_ascii = false;
1077 i += utf8_len;
1080 /* If IDENT contains invalid UTF-8 sequences (which may occur with
1081 attributes putting arbitrary byte sequences in identifiers), or
1082 control characters, we use octal escape sequences for all bytes
1083 outside printable ASCII. */
1084 if (!valid_printable_utf8)
1086 char *ret = (char *) identifier_to_locale_alloc (4 * idlen + 1);
1087 char *p = ret;
1088 for (i = 0; i < idlen; i++)
1090 if (uid[i] > 0x1F && uid[i] < 0x7F)
1091 *p++ = uid[i];
1092 else
1094 sprintf (p, "\\%03o", uid[i]);
1095 p += 4;
1098 *p = 0;
1099 return ret;
1102 /* Otherwise, if it is valid printable ASCII, or printable UTF-8
1103 with the locale character set being UTF-8, IDENT is used. */
1104 if (all_ascii || locale_utf8)
1105 return ident;
1107 /* Otherwise IDENT is converted to the locale character set if
1108 possible. */
1109 #if defined ENABLE_NLS && defined HAVE_LANGINFO_CODESET && HAVE_ICONV
1110 if (locale_encoding != NULL)
1112 iconv_t cd = iconv_open (locale_encoding, "UTF-8");
1113 bool conversion_ok = true;
1114 char *ret = NULL;
1115 if (cd != (iconv_t) -1)
1117 size_t ret_alloc = 4 * idlen + 1;
1118 for (;;)
1120 /* Repeat the whole conversion process as needed with
1121 larger buffers so non-reversible transformations can
1122 always be detected. */
1123 ICONV_CONST char *inbuf = CONST_CAST (char *, ident);
1124 char *outbuf;
1125 size_t inbytesleft = idlen;
1126 size_t outbytesleft = ret_alloc - 1;
1127 size_t iconv_ret;
1129 ret = (char *) identifier_to_locale_alloc (ret_alloc);
1130 outbuf = ret;
1132 if (iconv (cd, 0, 0, 0, 0) == (size_t) -1)
1134 conversion_ok = false;
1135 break;
1138 iconv_ret = iconv (cd, &inbuf, &inbytesleft,
1139 &outbuf, &outbytesleft);
1140 if (iconv_ret == (size_t) -1 || inbytesleft != 0)
1142 if (errno == E2BIG)
1144 ret_alloc *= 2;
1145 identifier_to_locale_free (ret);
1146 ret = NULL;
1147 continue;
1149 else
1151 conversion_ok = false;
1152 break;
1155 else if (iconv_ret != 0)
1157 conversion_ok = false;
1158 break;
1160 /* Return to initial shift state. */
1161 if (iconv (cd, 0, 0, &outbuf, &outbytesleft) == (size_t) -1)
1163 if (errno == E2BIG)
1165 ret_alloc *= 2;
1166 identifier_to_locale_free (ret);
1167 ret = NULL;
1168 continue;
1170 else
1172 conversion_ok = false;
1173 break;
1176 *outbuf = 0;
1177 break;
1179 iconv_close (cd);
1180 if (conversion_ok)
1181 return ret;
1184 #endif
1186 /* Otherwise, convert non-ASCII characters in IDENT to UCNs. */
1188 char *ret = (char *) identifier_to_locale_alloc (10 * idlen + 1);
1189 char *p = ret;
1190 for (i = 0; i < idlen;)
1192 unsigned int c;
1193 size_t utf8_len = decode_utf8_char (&uid[i], idlen - i, &c);
1194 if (utf8_len == 1)
1195 *p++ = uid[i];
1196 else
1198 sprintf (p, "\\U%08x", c);
1199 p += 10;
1201 i += utf8_len;
1203 *p = 0;
1204 return ret;
1208 #if CHECKING_P
1210 namespace selftest {
1212 /* Smoketest for pretty_printer. */
1214 static void
1215 test_basic_printing ()
1217 pretty_printer pp;
1218 pp_string (&pp, "hello");
1219 pp_space (&pp);
1220 pp_string (&pp, "world");
1222 ASSERT_STREQ ("hello world", pp_formatted_text (&pp));
1225 /* Helper function for testing pp_format.
1226 Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
1227 prints EXPECTED, assuming that pp_show_color is SHOW_COLOR. */
1229 static void
1230 assert_pp_format_va (const location &loc, const char *expected,
1231 bool show_color, const char *fmt, va_list *ap)
1233 pretty_printer pp;
1234 text_info ti;
1235 rich_location rich_loc (line_table, UNKNOWN_LOCATION);
1237 ti.format_spec = fmt;
1238 ti.args_ptr = ap;
1239 ti.err_no = 0;
1240 ti.x_data = NULL;
1241 ti.m_richloc = &rich_loc;
1243 pp_show_color (&pp) = show_color;
1244 pp_format (&pp, &ti);
1245 pp_output_formatted_text (&pp);
1246 ASSERT_STREQ_AT (loc, expected, pp_formatted_text (&pp));
1249 /* Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
1250 prints EXPECTED, with show_color disabled. */
1252 static void
1253 assert_pp_format (const location &loc, const char *expected,
1254 const char *fmt, ...)
1256 va_list ap;
1258 va_start (ap, fmt);
1259 assert_pp_format_va (loc, expected, false, fmt, &ap);
1260 va_end (ap);
1263 /* As above, but with colorization enabled. */
1265 static void
1266 assert_pp_format_colored (const location &loc, const char *expected,
1267 const char *fmt, ...)
1269 /* The tests of colorization assume the default color scheme.
1270 If GCC_COLORS is set, then the colors have potentially been
1271 overridden; skip the test. */
1272 if (getenv ("GCC_COLORS"))
1273 return;
1275 va_list ap;
1277 va_start (ap, fmt);
1278 assert_pp_format_va (loc, expected, true, fmt, &ap);
1279 va_end (ap);
1282 /* Helper function for calling testing pp_format,
1283 by calling assert_pp_format with various numbers of arguments.
1284 These exist mostly to avoid having to write SELFTEST_LOCATION
1285 throughout test_pp_format. */
1287 #define ASSERT_PP_FORMAT_1(EXPECTED, FMT, ARG1) \
1288 SELFTEST_BEGIN_STMT \
1289 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
1290 (ARG1)); \
1291 SELFTEST_END_STMT
1293 #define ASSERT_PP_FORMAT_2(EXPECTED, FMT, ARG1, ARG2) \
1294 SELFTEST_BEGIN_STMT \
1295 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
1296 (ARG1), (ARG2)); \
1297 SELFTEST_END_STMT
1299 #define ASSERT_PP_FORMAT_3(EXPECTED, FMT, ARG1, ARG2, ARG3) \
1300 SELFTEST_BEGIN_STMT \
1301 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
1302 (ARG1), (ARG2), (ARG3)); \
1303 SELFTEST_END_STMT
1305 /* Verify that pp_format works, for various format codes. */
1307 static void
1308 test_pp_format ()
1310 /* Avoid introducing locale-specific differences in the results
1311 by hardcoding open_quote and close_quote. */
1312 const char *old_open_quote = open_quote;
1313 const char *old_close_quote = close_quote;
1314 open_quote = "`";
1315 close_quote = "'";
1317 /* Verify that plain text is passed through unchanged. */
1318 assert_pp_format (SELFTEST_LOCATION, "unformatted", "unformatted");
1320 /* Verify various individual format codes, in the order listed in the
1321 comment for pp_format above. For each code, we append a second
1322 argument with a known bit pattern (0x12345678), to ensure that we
1323 are consuming arguments correctly. */
1324 ASSERT_PP_FORMAT_2 ("-27 12345678", "%d %x", -27, 0x12345678);
1325 ASSERT_PP_FORMAT_2 ("-5 12345678", "%i %x", -5, 0x12345678);
1326 ASSERT_PP_FORMAT_2 ("10 12345678", "%u %x", 10, 0x12345678);
1327 ASSERT_PP_FORMAT_2 ("17 12345678", "%o %x", 15, 0x12345678);
1328 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%x %x", 0xcafebabe, 0x12345678);
1329 ASSERT_PP_FORMAT_2 ("-27 12345678", "%ld %x", (long)-27, 0x12345678);
1330 ASSERT_PP_FORMAT_2 ("-5 12345678", "%li %x", (long)-5, 0x12345678);
1331 ASSERT_PP_FORMAT_2 ("10 12345678", "%lu %x", (long)10, 0x12345678);
1332 ASSERT_PP_FORMAT_2 ("17 12345678", "%lo %x", (long)15, 0x12345678);
1333 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%lx %x", (long)0xcafebabe,
1334 0x12345678);
1335 ASSERT_PP_FORMAT_2 ("-27 12345678", "%lld %x", (long long)-27, 0x12345678);
1336 ASSERT_PP_FORMAT_2 ("-5 12345678", "%lli %x", (long long)-5, 0x12345678);
1337 ASSERT_PP_FORMAT_2 ("10 12345678", "%llu %x", (long long)10, 0x12345678);
1338 ASSERT_PP_FORMAT_2 ("17 12345678", "%llo %x", (long long)15, 0x12345678);
1339 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%llx %x", (long long)0xcafebabe,
1340 0x12345678);
1341 ASSERT_PP_FORMAT_2 ("-27 12345678", "%wd %x", (HOST_WIDE_INT)-27, 0x12345678);
1342 ASSERT_PP_FORMAT_2 ("-5 12345678", "%wi %x", (HOST_WIDE_INT)-5, 0x12345678);
1343 ASSERT_PP_FORMAT_2 ("10 12345678", "%wu %x", (unsigned HOST_WIDE_INT)10,
1344 0x12345678);
1345 ASSERT_PP_FORMAT_2 ("17 12345678", "%wo %x", (HOST_WIDE_INT)15, 0x12345678);
1346 ASSERT_PP_FORMAT_2 ("0xcafebabe 12345678", "%wx %x", (HOST_WIDE_INT)0xcafebabe,
1347 0x12345678);
1348 ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678);
1349 ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello world",
1350 0x12345678);
1351 /* We can't test for %p; the pointer is printed in an implementation-defined
1352 manner. */
1353 ASSERT_PP_FORMAT_2 ("normal colored normal 12345678",
1354 "normal %rcolored%R normal %x",
1355 "error", 0x12345678);
1356 assert_pp_format_colored
1357 (SELFTEST_LOCATION,
1358 "normal \33[01;31m\33[Kcolored\33[m\33[K normal 12345678",
1359 "normal %rcolored%R normal %x", "error", 0x12345678);
1360 /* TODO:
1361 %m: strerror(text->err_no) - does not consume a value from args_ptr. */
1362 ASSERT_PP_FORMAT_1 ("% 12345678", "%% %x", 0x12345678);
1363 ASSERT_PP_FORMAT_1 ("` 12345678", "%< %x", 0x12345678);
1364 ASSERT_PP_FORMAT_1 ("' 12345678", "%> %x", 0x12345678);
1365 ASSERT_PP_FORMAT_1 ("' 12345678", "%' %x", 0x12345678);
1366 ASSERT_PP_FORMAT_3 ("abc 12345678", "%.*s %x", 3, "abcdef", 0x12345678);
1367 ASSERT_PP_FORMAT_2 ("abc 12345678", "%.3s %x", "abcdef", 0x12345678);
1369 /* Verify flag 'q'. */
1370 ASSERT_PP_FORMAT_2 ("`foo' 12345678", "%qs %x", "foo", 0x12345678);
1371 assert_pp_format_colored (SELFTEST_LOCATION,
1372 "`\33[01m\33[Kfoo\33[m\33[K' 12345678", "%qs %x",
1373 "foo", 0x12345678);
1375 /* Verify that combinations work, along with unformatted text. */
1376 assert_pp_format (SELFTEST_LOCATION,
1377 "the quick brown fox jumps over the lazy dog",
1378 "the %s %s %s jumps over the %s %s",
1379 "quick", "brown", "fox", "lazy", "dog");
1380 assert_pp_format (SELFTEST_LOCATION, "item 3 of 7", "item %i of %i", 3, 7);
1381 assert_pp_format (SELFTEST_LOCATION, "problem with `bar' at line 10",
1382 "problem with %qs at line %i", "bar", 10);
1384 /* Restore old values of open_quote and close_quote. */
1385 open_quote = old_open_quote;
1386 close_quote = old_close_quote;
1389 /* Run all of the selftests within this file. */
1391 void
1392 pretty_print_c_tests ()
1394 test_basic_printing ();
1395 test_pp_format ();
1398 } // namespace selftest
1400 #endif /* CHECKING_P */