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
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
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/>. */
23 #include "coretypes.h"
25 #include "pretty-print.h"
26 #include "diagnostic-color.h"
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. */
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
);
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 ();
53 return UNKNOWN_LOCATION
;
56 // Default construct an output buffer.
58 output_buffer::output_buffer ()
59 : formatted_obstack (),
61 obstack (&formatted_obstack
),
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) \
89 pp_scalar (PP, "%" F, va_arg (ARG, T)); \
93 pp_scalar (PP, "%l" F, va_arg (ARG, long T)); \
97 pp_scalar (PP, "%" HOST_LONG_LONG_FORMAT F, va_arg (ARG, long long T)); \
106 /* Subroutine of pp_set_maximum_length. Set up PRETTY-PRINTER's
107 internal maximum characters per line. */
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
);
120 int prefix_length
= pp
->prefix
? strlen (pp
->prefix
) : 0;
121 /* If the prefix is ridiculously too long, output at least
123 if (pp_line_cutoff (pp
) - prefix_length
< 32)
124 pp
->maximum_length
= pp_line_cutoff (pp
) + 32;
126 pp
->maximum_length
= pp_line_cutoff (pp
);
130 /* Clear PRETTY-PRINTER's output state. */
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. */
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
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. */
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
;
168 /* Print newlines as a left-aligned newline. */
174 /* The following characters are only special for record-shape nodes. */
181 escape_char
= for_record
;
184 /* The following characters always have to be escaped
185 for use in labels. */
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');
207 pp_clear_output_area (pp
);
210 /* Wrap a text delimited by START and END into PRETTY-PRINTER. */
212 pp_wrap_text (pretty_printer
*pp
, const char *start
, const char *end
)
214 bool wrapping_line
= pp_is_wrapping_line (pp
);
218 /* Dump anything bordered by whitespaces. */
220 const char *p
= start
;
221 while (p
!= end
&& !ISBLANK (*p
) && *p
!= '\n')
224 && p
- start
>= pp_remaining_character_count_for_line (pp
))
226 pp_append_text (pp
, start
, p
);
230 if (start
!= end
&& ISBLANK (*start
))
235 if (start
!= end
&& *start
== '\n')
243 /* Same as pp_wrap_text but wrap text only when in line-wrapping mode. */
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
);
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. */
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. */
265 pp_indent (pretty_printer
*pp
)
267 int n
= pp_indentation (pp
);
270 for (i
= 0; i
< n
; ++i
)
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.
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.
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
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
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. */
315 pp_format (pretty_printer
*pp
, text_info
*text
)
317 output_buffer
*buffer
= pp_buffer (pp
);
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
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
);
358 obstack_1grow (&buffer
->chunk_obstack
, '%');
364 obstack_grow (&buffer
->chunk_obstack
,
365 open_quote
, strlen (open_quote
));
367 = colorize_start (pp_show_color (pp
), "quote");
368 obstack_grow (&buffer
->chunk_obstack
, colorstr
, strlen (colorstr
));
375 const char *colorstr
= colorize_stop (pp_show_color (pp
));
376 obstack_grow (&buffer
->chunk_obstack
, colorstr
, strlen (colorstr
));
380 obstack_grow (&buffer
->chunk_obstack
,
381 close_quote
, strlen (close_quote
));
387 const char *colorstr
= colorize_stop (pp_show_color (pp
));
388 obstack_grow (&buffer
->chunk_obstack
, colorstr
,
396 const char *errstr
= xstrerror (text
->err_no
);
397 obstack_grow (&buffer
->chunk_obstack
, errstr
, strlen (errstr
));
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 *);
413 argno
= strtoul (p
, &end
, 10) - 1;
415 gcc_assert (*p
== '$');
419 gcc_assert (!any_unnumbered
);
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
);
435 while (strchr ("qwl+#", p
[-1]));
439 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
440 (where M == N + 1). */
445 obstack_1grow (&buffer
->chunk_obstack
, *p
);
448 while (ISDIGIT (p
[-1]));
449 gcc_assert (p
[-1] == 's');
453 gcc_assert (*p
== '*');
454 obstack_1grow (&buffer
->chunk_obstack
, '*');
460 unsigned int argno2
= strtoul (p
, &end
, 10) - 1;
462 gcc_assert (argno2
== argno
- 1);
463 gcc_assert (!any_unnumbered
);
464 gcc_assert (*p
== '$');
467 formatters
[argno2
] = formatters
[argno
];
471 gcc_assert (!any_numbered
);
472 formatters
[argno
+1] = formatters
[argno
];
475 gcc_assert (*p
== 's');
476 obstack_1grow (&buffer
->chunk_obstack
, 's');
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 *);
493 /* Set output to the argument obstack, and switch line-wrapping and
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
501 for (argno
= 0; formatters
[argno
]; argno
++)
509 /* We do not attempt to enforce any ordering on the modifier
512 for (p
= *formatters
[argno
];; p
++)
537 /* We don't support precision beyond that of "long long". */
538 gcc_assert (precision
< 2);
545 gcc_assert (!wide
|| precision
== 0);
549 pp_string (pp
, open_quote
);
550 pp_string (pp
, colorize_start (pp_show_color (pp
), "quote"));
556 pp_string (pp
, colorize_start (pp_show_color (pp
),
557 va_arg (*text
->args_ptr
,
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
);
571 const char str
[2] = { chr
, '\0' };
572 pp_quoted_string (pp
, str
, 1);
580 pp_wide_integer (pp
, va_arg (*text
->args_ptr
, HOST_WIDE_INT
));
582 pp_integer_with_precision
583 (pp
, *text
->args_ptr
, precision
, int, "d");
588 pp_scalar (pp
, "%" HOST_WIDE_INT_PRINT
"o",
589 va_arg (*text
->args_ptr
, unsigned HOST_WIDE_INT
));
591 pp_integer_with_precision
592 (pp
, *text
->args_ptr
, precision
, unsigned, "o");
597 pp_quoted_string (pp
, va_arg (*text
->args_ptr
, const char *));
599 pp_string (pp
, va_arg (*text
->args_ptr
, const char *));
603 pp_pointer (pp
, va_arg (*text
->args_ptr
, void *));
608 pp_scalar (pp
, HOST_WIDE_INT_PRINT_UNSIGNED
,
609 va_arg (*text
->args_ptr
, unsigned HOST_WIDE_INT
));
611 pp_integer_with_precision
612 (pp
, *text
->args_ptr
, precision
, unsigned, "u");
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
]);
634 pp_scalar (pp
, HOST_WIDE_INT_PRINT_HEX
,
635 va_arg (*text
->args_ptr
, unsigned HOST_WIDE_INT
));
637 pp_integer_with_precision
638 (pp
, *text
->args_ptr
, precision
, unsigned, "x");
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. */
653 n
= strtoul (p
, &end
, 10);
655 gcc_assert (*p
== 's');
659 gcc_assert (*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]);
669 s
= va_arg (*text
->args_ptr
, const char *);
671 /* Append the lesser of precision and strlen (s) characters
672 from the array (which need not be a nul-terminated string).
673 Negative precision is treated as if it were omitted. */
674 size_t len
= n
< 0 ? strlen (s
) : strnlen (s
, n
);
676 pp_append_text (pp
, s
, s
+ len
);
684 gcc_assert (pp_format_decoder (pp
));
685 ok
= pp_format_decoder (pp
) (pp
, text
, p
,
686 precision
, wide
, plus
, hash
, quote
,
694 pp_string (pp
, colorize_stop (pp_show_color (pp
)));
695 pp_string (pp
, close_quote
);
698 obstack_1grow (&buffer
->chunk_obstack
, '\0');
699 *formatters
[argno
] = XOBFINISH (&buffer
->chunk_obstack
, const char *);
703 for (; argno
< PP_NL_ARGMAX
; argno
++)
704 gcc_assert (!formatters
[argno
]);
706 /* If the client supplied a postprocessing object, call its "handle"
708 if (pp
->m_format_postprocessor
)
709 pp
->m_format_postprocessor
->handle (pp
);
711 /* Revert to normal obstack and wrapping mode. */
712 buffer
->obstack
= &buffer
->formatted_obstack
;
713 buffer
->line_length
= 0;
714 pp_wrapping_mode (pp
) = old_wrapping_mode
;
718 /* Format of a message pointed to by TEXT. */
720 pp_output_formatted_text (pretty_printer
*pp
)
723 output_buffer
*buffer
= pp_buffer (pp
);
724 struct chunk_info
*chunk_array
= buffer
->cur_chunk_array
;
725 const char **args
= chunk_array
->args
;
727 gcc_assert (buffer
->obstack
== &buffer
->formatted_obstack
);
728 gcc_assert (buffer
->line_length
== 0);
730 /* This is a third phase, first 2 phases done in pp_format_args.
731 Now we actually print it. */
732 for (chunk
= 0; args
[chunk
]; chunk
++)
733 pp_string (pp
, args
[chunk
]);
735 /* Deallocate the chunk structure and everything after it (i.e. the
736 associated series of formatted strings). */
737 buffer
->cur_chunk_array
= chunk_array
->prev
;
738 obstack_free (&buffer
->chunk_obstack
, chunk_array
);
741 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
742 settings needed by BUFFER for a verbatim formatting. */
744 pp_format_verbatim (pretty_printer
*pp
, text_info
*text
)
746 /* Set verbatim mode. */
747 pp_wrapping_mode_t oldmode
= pp_set_verbatim_wrapping (pp
);
749 /* Do the actual formatting. */
750 pp_format (pp
, text
);
751 pp_output_formatted_text (pp
);
753 /* Restore previous settings. */
754 pp_wrapping_mode (pp
) = oldmode
;
757 /* Flush the content of BUFFER onto the attached stream. This
758 function does nothing unless pp->output_buffer->flush_p. */
760 pp_flush (pretty_printer
*pp
)
763 if (!pp
->buffer
->flush_p
)
765 pp_write_text_to_stream (pp
);
766 fflush (pp_buffer (pp
)->stream
);
769 /* Flush the content of BUFFER onto the attached stream independently
770 of the value of pp->output_buffer->flush_p. */
772 pp_really_flush (pretty_printer
*pp
)
775 pp_write_text_to_stream (pp
);
776 fflush (pp_buffer (pp
)->stream
);
779 /* Sets the number of maximum characters per line PRETTY-PRINTER can
780 output in line-wrapping mode. A LENGTH value 0 suppresses
783 pp_set_line_maximum_length (pretty_printer
*pp
, int length
)
785 pp_line_cutoff (pp
) = length
;
786 pp_set_real_maximum_length (pp
);
789 /* Clear PRETTY-PRINTER output area text info. */
791 pp_clear_output_area (pretty_printer
*pp
)
793 obstack_free (pp_buffer (pp
)->obstack
,
794 obstack_base (pp_buffer (pp
)->obstack
));
795 pp_buffer (pp
)->line_length
= 0;
798 /* Set PREFIX for PRETTY-PRINTER. */
800 pp_set_prefix (pretty_printer
*pp
, const char *prefix
)
803 pp_set_real_maximum_length (pp
);
804 pp
->emitted_prefix
= false;
805 pp_indentation (pp
) = 0;
808 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
810 pp_destroy_prefix (pretty_printer
*pp
)
812 if (pp
->prefix
!= NULL
)
814 free (CONST_CAST (char *, pp
->prefix
));
819 /* Write out PRETTY-PRINTER's prefix. */
821 pp_emit_prefix (pretty_printer
*pp
)
823 if (pp
->prefix
!= NULL
)
825 switch (pp_prefixing_rule (pp
))
828 case DIAGNOSTICS_SHOW_PREFIX_NEVER
:
831 case DIAGNOSTICS_SHOW_PREFIX_ONCE
:
832 if (pp
->emitted_prefix
)
837 pp_indentation (pp
) += 3;
840 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
:
842 int prefix_length
= strlen (pp
->prefix
);
843 pp_append_r (pp
, pp
->prefix
, prefix_length
);
844 pp
->emitted_prefix
= true;
851 /* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
852 characters per line. */
854 pretty_printer::pretty_printer (const char *p
, int l
)
855 : buffer (new (XCNEW (output_buffer
)) output_buffer ()),
862 m_format_postprocessor (NULL
),
865 translate_identifiers (true),
868 pp_line_cutoff (this) = l
;
869 /* By default, we emit prefixes once per message. */
870 pp_prefixing_rule (this) = DIAGNOSTICS_SHOW_PREFIX_ONCE
;
871 pp_set_prefix (this, p
);
874 pretty_printer::~pretty_printer ()
876 if (m_format_postprocessor
)
877 delete m_format_postprocessor
;
878 buffer
->~output_buffer ();
882 /* Append a string delimited by START and END to the output area of
883 PRETTY-PRINTER. No line wrapping is done. However, if beginning a
884 new line then emit PRETTY-PRINTER's prefix and skip any leading
885 whitespace if appropriate. The caller must ensure that it is
888 pp_append_text (pretty_printer
*pp
, const char *start
, const char *end
)
890 /* Emit prefix and skip whitespace if we're starting a new line. */
891 if (pp_buffer (pp
)->line_length
== 0)
894 if (pp_is_wrapping_line (pp
))
895 while (start
!= end
&& *start
== ' ')
898 pp_append_r (pp
, start
, end
- start
);
901 /* Finishes constructing a NULL-terminated character string representing
902 the PRETTY-PRINTED text. */
904 pp_formatted_text (pretty_printer
*pp
)
906 return output_buffer_formatted_text (pp_buffer (pp
));
909 /* Return a pointer to the last character emitted in PRETTY-PRINTER's
910 output area. A NULL pointer means no character available. */
912 pp_last_position_in_text (const pretty_printer
*pp
)
914 return output_buffer_last_position_in_text (pp_buffer (pp
));
917 /* Return the amount of characters PRETTY-PRINTER can accept to
918 make a full line. Meaningful only in line-wrapping mode. */
920 pp_remaining_character_count_for_line (pretty_printer
*pp
)
922 return pp
->maximum_length
- pp_buffer (pp
)->line_length
;
926 /* Format a message into BUFFER a la printf. */
928 pp_printf (pretty_printer
*pp
, const char *msg
, ...)
936 text
.format_spec
= msg
;
937 pp_format (pp
, &text
);
938 pp_output_formatted_text (pp
);
943 /* Output MESSAGE verbatim into BUFFER. */
945 pp_verbatim (pretty_printer
*pp
, const char *msg
, ...)
953 text
.format_spec
= msg
;
954 pp_format_verbatim (pp
, &text
);
960 /* Have PRETTY-PRINTER start a new line. */
962 pp_newline (pretty_printer
*pp
)
964 obstack_1grow (pp_buffer (pp
)->obstack
, '\n');
965 pp_needs_newline (pp
) = false;
966 pp_buffer (pp
)->line_length
= 0;
969 /* Have PRETTY-PRINTER add a CHARACTER. */
971 pp_character (pretty_printer
*pp
, int c
)
973 if (pp_is_wrapping_line (pp
)
974 && pp_remaining_character_count_for_line (pp
) <= 0)
980 obstack_1grow (pp_buffer (pp
)->obstack
, c
);
981 ++pp_buffer (pp
)->line_length
;
984 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
985 be line-wrapped if in appropriate mode. */
987 pp_string (pretty_printer
*pp
, const char *str
)
989 gcc_checking_assert (str
);
990 pp_maybe_wrap_text (pp
, str
, str
+ strlen (str
));
993 /* Append the leading N characters of STRING to the output area of
994 PRETTY-PRINTER, quoting in hexadecimal non-printable characters.
995 Setting N = -1 is as if N were set to strlen (STRING). The STRING
996 may be line-wrapped if in appropriate mode. */
998 pp_quoted_string (pretty_printer
*pp
, const char *str
, size_t n
/* = -1 */)
1000 gcc_checking_assert (str
);
1002 const char *last
= str
;
1005 /* Compute the length if not specified. */
1006 if (n
== (size_t) -1)
1009 for (ps
= str
; n
; ++ps
, --n
)
1015 pp_maybe_wrap_text (pp
, last
, ps
- 1);
1017 /* Append the hexadecimal value of the character. Allocate a buffer
1018 that's large enough for a 32-bit char plus the hex prefix. */
1020 int n
= sprintf (buf
, "\\x%02x", (unsigned char)*ps
);
1021 pp_maybe_wrap_text (pp
, buf
, buf
+ n
);
1025 pp_maybe_wrap_text (pp
, last
, ps
);
1028 /* Maybe print out a whitespace if needed. */
1031 pp_maybe_space (pretty_printer
*pp
)
1033 if (pp
->padding
!= pp_none
)
1036 pp
->padding
= pp_none
;
1040 // Add a newline to the pretty printer PP and flush formatted text.
1043 pp_newline_and_flush (pretty_printer
*pp
)
1047 pp_needs_newline (pp
) = false;
1050 // Add a newline to the pretty printer PP, followed by indentation.
1053 pp_newline_and_indent (pretty_printer
*pp
, int n
)
1055 pp_indentation (pp
) += n
;
1058 pp_needs_newline (pp
) = false;
1061 // Add separator C, followed by a single whitespace.
1064 pp_separate_with (pretty_printer
*pp
, char c
)
1066 pp_character (pp
, c
);
1071 /* The string starting at P has LEN (at least 1) bytes left; if they
1072 start with a valid UTF-8 sequence, return the length of that
1073 sequence and set *VALUE to the value of that sequence, and
1074 otherwise return 0 and set *VALUE to (unsigned int) -1. */
1077 decode_utf8_char (const unsigned char *p
, size_t len
, unsigned int *value
)
1079 unsigned int t
= *p
;
1085 size_t utf8_len
= 0;
1088 for (t
= *p
; t
& 0x80; t
<<= 1)
1091 if (utf8_len
> len
|| utf8_len
< 2 || utf8_len
> 6)
1093 *value
= (unsigned int) -1;
1096 ch
= *p
& ((1 << (7 - utf8_len
)) - 1);
1097 for (i
= 1; i
< utf8_len
; i
++)
1099 unsigned int u
= p
[i
];
1100 if ((u
& 0xC0) != 0x80)
1102 *value
= (unsigned int) -1;
1105 ch
= (ch
<< 6) | (u
& 0x3F);
1107 if ( (ch
<= 0x7F && utf8_len
> 1)
1108 || (ch
<= 0x7FF && utf8_len
> 2)
1109 || (ch
<= 0xFFFF && utf8_len
> 3)
1110 || (ch
<= 0x1FFFFF && utf8_len
> 4)
1111 || (ch
<= 0x3FFFFFF && utf8_len
> 5)
1112 || (ch
>= 0xD800 && ch
<= 0xDFFF))
1114 *value
= (unsigned int) -1;
1127 /* Allocator for identifier_to_locale and corresponding function to
1130 void *(*identifier_to_locale_alloc
) (size_t) = xmalloc
;
1131 void (*identifier_to_locale_free
) (void *) = free
;
1133 /* Given IDENT, an identifier in the internal encoding, return a
1134 version of IDENT suitable for diagnostics in the locale character
1135 set: either IDENT itself, or a string, allocated using
1136 identifier_to_locale_alloc, converted to the locale character set
1137 and using escape sequences if not representable in the locale
1138 character set or containing control characters or invalid byte
1139 sequences. Existing backslashes in IDENT are not doubled, so the
1140 result may not uniquely specify the contents of an arbitrary byte
1141 sequence identifier. */
1144 identifier_to_locale (const char *ident
)
1146 const unsigned char *uid
= (const unsigned char *) ident
;
1147 size_t idlen
= strlen (ident
);
1148 bool valid_printable_utf8
= true;
1149 bool all_ascii
= true;
1152 for (i
= 0; i
< idlen
;)
1155 size_t utf8_len
= decode_utf8_char (&uid
[i
], idlen
- i
, &c
);
1156 if (utf8_len
== 0 || c
<= 0x1F || (c
>= 0x7F && c
<= 0x9F))
1158 valid_printable_utf8
= false;
1166 /* If IDENT contains invalid UTF-8 sequences (which may occur with
1167 attributes putting arbitrary byte sequences in identifiers), or
1168 control characters, we use octal escape sequences for all bytes
1169 outside printable ASCII. */
1170 if (!valid_printable_utf8
)
1172 char *ret
= (char *) identifier_to_locale_alloc (4 * idlen
+ 1);
1174 for (i
= 0; i
< idlen
; i
++)
1176 if (uid
[i
] > 0x1F && uid
[i
] < 0x7F)
1180 sprintf (p
, "\\%03o", uid
[i
]);
1188 /* Otherwise, if it is valid printable ASCII, or printable UTF-8
1189 with the locale character set being UTF-8, IDENT is used. */
1190 if (all_ascii
|| locale_utf8
)
1193 /* Otherwise IDENT is converted to the locale character set if
1195 #if defined ENABLE_NLS && defined HAVE_LANGINFO_CODESET && HAVE_ICONV
1196 if (locale_encoding
!= NULL
)
1198 iconv_t cd
= iconv_open (locale_encoding
, "UTF-8");
1199 bool conversion_ok
= true;
1201 if (cd
!= (iconv_t
) -1)
1203 size_t ret_alloc
= 4 * idlen
+ 1;
1206 /* Repeat the whole conversion process as needed with
1207 larger buffers so non-reversible transformations can
1208 always be detected. */
1209 ICONV_CONST
char *inbuf
= CONST_CAST (char *, ident
);
1211 size_t inbytesleft
= idlen
;
1212 size_t outbytesleft
= ret_alloc
- 1;
1215 ret
= (char *) identifier_to_locale_alloc (ret_alloc
);
1218 if (iconv (cd
, 0, 0, 0, 0) == (size_t) -1)
1220 conversion_ok
= false;
1224 iconv_ret
= iconv (cd
, &inbuf
, &inbytesleft
,
1225 &outbuf
, &outbytesleft
);
1226 if (iconv_ret
== (size_t) -1 || inbytesleft
!= 0)
1231 identifier_to_locale_free (ret
);
1237 conversion_ok
= false;
1241 else if (iconv_ret
!= 0)
1243 conversion_ok
= false;
1246 /* Return to initial shift state. */
1247 if (iconv (cd
, 0, 0, &outbuf
, &outbytesleft
) == (size_t) -1)
1252 identifier_to_locale_free (ret
);
1258 conversion_ok
= false;
1272 /* Otherwise, convert non-ASCII characters in IDENT to UCNs. */
1274 char *ret
= (char *) identifier_to_locale_alloc (10 * idlen
+ 1);
1276 for (i
= 0; i
< idlen
;)
1279 size_t utf8_len
= decode_utf8_char (&uid
[i
], idlen
- i
, &c
);
1284 sprintf (p
, "\\U%08x", c
);
1296 namespace selftest
{
1298 /* Smoketest for pretty_printer. */
1301 test_basic_printing ()
1304 pp_string (&pp
, "hello");
1306 pp_string (&pp
, "world");
1308 ASSERT_STREQ ("hello world", pp_formatted_text (&pp
));
1311 /* Helper function for testing pp_format.
1312 Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
1313 prints EXPECTED, assuming that pp_show_color is SHOW_COLOR. */
1316 assert_pp_format_va (const location
&loc
, const char *expected
,
1317 bool show_color
, const char *fmt
, va_list *ap
)
1321 rich_location
rich_loc (line_table
, UNKNOWN_LOCATION
);
1323 ti
.format_spec
= fmt
;
1327 ti
.m_richloc
= &rich_loc
;
1329 pp_show_color (&pp
) = show_color
;
1330 pp_format (&pp
, &ti
);
1331 pp_output_formatted_text (&pp
);
1332 ASSERT_STREQ_AT (loc
, expected
, pp_formatted_text (&pp
));
1335 /* Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
1336 prints EXPECTED, with show_color disabled. */
1339 assert_pp_format (const location
&loc
, const char *expected
,
1340 const char *fmt
, ...)
1345 assert_pp_format_va (loc
, expected
, false, fmt
, &ap
);
1349 /* As above, but with colorization enabled. */
1352 assert_pp_format_colored (const location
&loc
, const char *expected
,
1353 const char *fmt
, ...)
1355 /* The tests of colorization assume the default color scheme.
1356 If GCC_COLORS is set, then the colors have potentially been
1357 overridden; skip the test. */
1358 if (getenv ("GCC_COLORS"))
1364 assert_pp_format_va (loc
, expected
, true, fmt
, &ap
);
1368 /* Helper function for calling testing pp_format,
1369 by calling assert_pp_format with various numbers of arguments.
1370 These exist mostly to avoid having to write SELFTEST_LOCATION
1371 throughout test_pp_format. */
1373 #define ASSERT_PP_FORMAT_1(EXPECTED, FMT, ARG1) \
1374 SELFTEST_BEGIN_STMT \
1375 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
1379 #define ASSERT_PP_FORMAT_2(EXPECTED, FMT, ARG1, ARG2) \
1380 SELFTEST_BEGIN_STMT \
1381 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
1385 #define ASSERT_PP_FORMAT_3(EXPECTED, FMT, ARG1, ARG2, ARG3) \
1386 SELFTEST_BEGIN_STMT \
1387 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
1388 (ARG1), (ARG2), (ARG3)); \
1391 /* Verify that pp_format works, for various format codes. */
1396 /* Avoid introducing locale-specific differences in the results
1397 by hardcoding open_quote and close_quote. */
1398 const char *old_open_quote
= open_quote
;
1399 const char *old_close_quote
= close_quote
;
1403 /* Verify that plain text is passed through unchanged. */
1404 assert_pp_format (SELFTEST_LOCATION
, "unformatted", "unformatted");
1406 /* Verify various individual format codes, in the order listed in the
1407 comment for pp_format above. For each code, we append a second
1408 argument with a known bit pattern (0x12345678), to ensure that we
1409 are consuming arguments correctly. */
1410 ASSERT_PP_FORMAT_2 ("-27 12345678", "%d %x", -27, 0x12345678);
1411 ASSERT_PP_FORMAT_2 ("-5 12345678", "%i %x", -5, 0x12345678);
1412 ASSERT_PP_FORMAT_2 ("10 12345678", "%u %x", 10, 0x12345678);
1413 ASSERT_PP_FORMAT_2 ("17 12345678", "%o %x", 15, 0x12345678);
1414 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%x %x", 0xcafebabe, 0x12345678);
1415 ASSERT_PP_FORMAT_2 ("-27 12345678", "%ld %x", (long)-27, 0x12345678);
1416 ASSERT_PP_FORMAT_2 ("-5 12345678", "%li %x", (long)-5, 0x12345678);
1417 ASSERT_PP_FORMAT_2 ("10 12345678", "%lu %x", (long)10, 0x12345678);
1418 ASSERT_PP_FORMAT_2 ("17 12345678", "%lo %x", (long)15, 0x12345678);
1419 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%lx %x", (long)0xcafebabe,
1421 ASSERT_PP_FORMAT_2 ("-27 12345678", "%lld %x", (long long)-27, 0x12345678);
1422 ASSERT_PP_FORMAT_2 ("-5 12345678", "%lli %x", (long long)-5, 0x12345678);
1423 ASSERT_PP_FORMAT_2 ("10 12345678", "%llu %x", (long long)10, 0x12345678);
1424 ASSERT_PP_FORMAT_2 ("17 12345678", "%llo %x", (long long)15, 0x12345678);
1425 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%llx %x", (long long)0xcafebabe,
1427 ASSERT_PP_FORMAT_2 ("-27 12345678", "%wd %x", (HOST_WIDE_INT
)-27, 0x12345678);
1428 ASSERT_PP_FORMAT_2 ("-5 12345678", "%wi %x", (HOST_WIDE_INT
)-5, 0x12345678);
1429 ASSERT_PP_FORMAT_2 ("10 12345678", "%wu %x", (unsigned HOST_WIDE_INT
)10,
1431 ASSERT_PP_FORMAT_2 ("17 12345678", "%wo %x", (HOST_WIDE_INT
)15, 0x12345678);
1432 ASSERT_PP_FORMAT_2 ("0xcafebabe 12345678", "%wx %x", (HOST_WIDE_INT
)0xcafebabe,
1434 ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678);
1435 ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello world",
1438 /* Not nul-terminated. */
1439 char arr
[5] = { '1', '2', '3', '4', '5' };
1440 ASSERT_PP_FORMAT_3 ("123 12345678", "%.*s %x", 3, arr
, 0x12345678);
1441 ASSERT_PP_FORMAT_3 ("1234 12345678", "%.*s %x", -1, "1234", 0x12345678);
1442 ASSERT_PP_FORMAT_3 ("12345 12345678", "%.*s %x", 7, "12345", 0x12345678);
1444 /* We can't test for %p; the pointer is printed in an implementation-defined
1446 ASSERT_PP_FORMAT_2 ("normal colored normal 12345678",
1447 "normal %rcolored%R normal %x",
1448 "error", 0x12345678);
1449 assert_pp_format_colored
1451 "normal \33[01;31m\33[Kcolored\33[m\33[K normal 12345678",
1452 "normal %rcolored%R normal %x", "error", 0x12345678);
1454 %m: strerror(text->err_no) - does not consume a value from args_ptr. */
1455 ASSERT_PP_FORMAT_1 ("% 12345678", "%% %x", 0x12345678);
1456 ASSERT_PP_FORMAT_1 ("` 12345678", "%< %x", 0x12345678);
1457 ASSERT_PP_FORMAT_1 ("' 12345678", "%> %x", 0x12345678);
1458 ASSERT_PP_FORMAT_1 ("' 12345678", "%' %x", 0x12345678);
1459 ASSERT_PP_FORMAT_3 ("abc 12345678", "%.*s %x", 3, "abcdef", 0x12345678);
1460 ASSERT_PP_FORMAT_2 ("abc 12345678", "%.3s %x", "abcdef", 0x12345678);
1462 /* Verify flag 'q'. */
1463 ASSERT_PP_FORMAT_2 ("`foo' 12345678", "%qs %x", "foo", 0x12345678);
1464 assert_pp_format_colored (SELFTEST_LOCATION
,
1465 "`\33[01m\33[Kfoo\33[m\33[K' 12345678", "%qs %x",
1469 int v
[] = { 1, 2, 3 };
1470 ASSERT_PP_FORMAT_3 ("1, 2, 3 12345678", "%Z %x", v
, 3, 0x12345678);
1473 ASSERT_PP_FORMAT_3 ("0 12345678", "%Z %x", v2
, 1, 0x12345678);
1475 /* Verify that combinations work, along with unformatted text. */
1476 assert_pp_format (SELFTEST_LOCATION
,
1477 "the quick brown fox jumps over the lazy dog",
1478 "the %s %s %s jumps over the %s %s",
1479 "quick", "brown", "fox", "lazy", "dog");
1480 assert_pp_format (SELFTEST_LOCATION
, "item 3 of 7", "item %i of %i", 3, 7);
1481 assert_pp_format (SELFTEST_LOCATION
, "problem with `bar' at line 10",
1482 "problem with %qs at line %i", "bar", 10);
1484 /* Restore old values of open_quote and close_quote. */
1485 open_quote
= old_open_quote
;
1486 close_quote
= old_close_quote
;
1489 /* Run all of the selftests within this file. */
1492 pretty_print_c_tests ()
1494 test_basic_printing ();
1498 } // namespace selftest
1500 #endif /* CHECKING_P */