1 /* Various declarations for language-independent pretty-print subroutines.
2 Copyright (C) 2003-2023 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"
27 #include "diagnostic-event-id.h"
36 /* Replacement for fputs() that handles ANSI escape codes on Windows NT.
37 Contributed by: Liu Hao (lh_mouse at 126 dot com)
39 XXX: This file is compiled into libcommon.a that will be self-contained.
40 It looks like that these functions can be put nowhere else. */
43 #define WIN32_LEAN_AND_MEAN 1
46 /* Write all bytes in [s,s+n) into the specified stream.
47 Errors are ignored. */
49 write_all (HANDLE h
, const char *s
, size_t n
)
60 if (!WriteFile (h
, s
+ n
- rem
, step
, &step
, NULL
))
66 /* Find the beginning of an escape sequence.
68 1. If the sequence begins with an ESC character (0x1B) and a second
69 character X in [0x40,0x5F], returns X and stores a pointer to
70 the third character into *head.
71 2. If the sequence begins with a character X in [0x80,0x9F], returns
72 (X-0x40) and stores a pointer to the second character into *head.
73 Stores the number of ESC character(s) in *prefix_len.
74 Returns 0 if no such sequence can be found. */
76 find_esc_head (int *prefix_len
, const char **head
, const char *str
)
84 c
= (unsigned char) *r
;
90 if (escaped
&& 0x40 <= c
&& c
<= 0x5F)
97 if (0x80 <= c
&& c
<= 0x9F)
109 /* Find the terminator of an escape sequence.
110 str should be the value stored in *head by a previous successful
111 call to find_esc_head().
112 Returns 0 if no such sequence can be found. */
114 find_esc_terminator (const char **term
, const char *str
)
121 c
= (unsigned char) *r
;
127 if (0x40 <= c
&& c
<= 0x7E)
137 /* Handle a sequence of codes. Sequences that are invalid, reserved,
138 unrecognized or unimplemented are ignored silently.
139 There isn't much we can do because of lameness of Windows consoles. */
141 eat_esc_sequence (HANDLE h
, int esc_code
,
142 const char *esc_head
, const char *esc_term
)
144 /* Numbers in an escape sequence cannot be negative, because
145 a minus sign in the middle of it would have terminated it. */
148 CONSOLE_SCREEN_BUFFER_INFO sb
;
150 /* ED and EL parameters. */
153 /* SGR parameters. */
154 WORD attrib_add
, attrib_rm
;
157 switch (MAKEWORD (esc_code
, *esc_term
))
160 Move the cursor up by n1 characters. */
161 case MAKEWORD ('[', 'A'):
162 if (esc_head
== esc_term
)
166 n1
= strtol (esc_head
, &eptr
, 10);
167 if (eptr
!= esc_term
)
171 if (GetConsoleScreenBufferInfo (h
, &sb
))
173 cr
= sb
.dwCursorPosition
;
174 /* Stop at the topmost boundary. */
179 SetConsoleCursorPosition (h
, cr
);
184 Move the cursor down by n1 characters. */
185 case MAKEWORD ('[', 'B'):
186 if (esc_head
== esc_term
)
190 n1
= strtol (esc_head
, &eptr
, 10);
191 if (eptr
!= esc_term
)
195 if (GetConsoleScreenBufferInfo (h
, &sb
))
197 cr
= sb
.dwCursorPosition
;
198 /* Stop at the bottommost boundary. */
199 if (sb
.dwSize
.Y
- cr
.Y
> n1
)
203 SetConsoleCursorPosition (h
, cr
);
208 Move the cursor right by n1 characters. */
209 case MAKEWORD ('[', 'C'):
210 if (esc_head
== esc_term
)
214 n1
= strtol (esc_head
, &eptr
, 10);
215 if (eptr
!= esc_term
)
219 if (GetConsoleScreenBufferInfo (h
, &sb
))
221 cr
= sb
.dwCursorPosition
;
222 /* Stop at the rightmost boundary. */
223 if (sb
.dwSize
.X
- cr
.X
> n1
)
227 SetConsoleCursorPosition (h
, cr
);
232 Move the cursor left by n1 characters. */
233 case MAKEWORD ('[', 'D'):
234 if (esc_head
== esc_term
)
238 n1
= strtol (esc_head
, &eptr
, 10);
239 if (eptr
!= esc_term
)
243 if (GetConsoleScreenBufferInfo (h
, &sb
))
245 cr
= sb
.dwCursorPosition
;
246 /* Stop at the leftmost boundary. */
251 SetConsoleCursorPosition (h
, cr
);
256 Move the cursor to the beginning of the n1-th line downwards. */
257 case MAKEWORD ('[', 'E'):
258 if (esc_head
== esc_term
)
262 n1
= strtol (esc_head
, &eptr
, 10);
263 if (eptr
!= esc_term
)
267 if (GetConsoleScreenBufferInfo (h
, &sb
))
269 cr
= sb
.dwCursorPosition
;
271 /* Stop at the bottommost boundary. */
272 if (sb
.dwSize
.Y
- cr
.Y
> n1
)
276 SetConsoleCursorPosition (h
, cr
);
281 Move the cursor to the beginning of the n1-th line upwards. */
282 case MAKEWORD ('[', 'F'):
283 if (esc_head
== esc_term
)
287 n1
= strtol (esc_head
, &eptr
, 10);
288 if (eptr
!= esc_term
)
292 if (GetConsoleScreenBufferInfo (h
, &sb
))
294 cr
= sb
.dwCursorPosition
;
296 /* Stop at the topmost boundary. */
301 SetConsoleCursorPosition (h
, cr
);
306 Move the cursor to the (1-based) n1-th column. */
307 case MAKEWORD ('[', 'G'):
308 if (esc_head
== esc_term
)
312 n1
= strtol (esc_head
, &eptr
, 10);
313 if (eptr
!= esc_term
)
317 if (GetConsoleScreenBufferInfo (h
, &sb
))
319 cr
= sb
.dwCursorPosition
;
321 /* Stop at the leftmost or rightmost boundary. */
324 else if (n1
> sb
.dwSize
.X
)
328 SetConsoleCursorPosition (h
, cr
);
332 /* ESC [ n1 ';' n2 'H'
334 Move the cursor to the (1-based) n1-th row and
335 (also 1-based) n2-th column. */
336 case MAKEWORD ('[', 'H'):
337 case MAKEWORD ('[', 'f'):
338 if (esc_head
== esc_term
)
340 /* Both parameters are omitted and set to 1 by default. */
344 else if (!(delim
= (char *) memchr (esc_head
, ';',
345 esc_term
- esc_head
)))
347 /* Only the first parameter is given. The second one is
348 set to 1 by default. */
349 n1
= strtol (esc_head
, &eptr
, 10);
350 if (eptr
!= esc_term
)
356 /* Both parameters are given. The first one shall be
357 terminated by the semicolon. */
358 n1
= strtol (esc_head
, &eptr
, 10);
361 n2
= strtol (delim
+ 1, &eptr
, 10);
362 if (eptr
!= esc_term
)
366 if (GetConsoleScreenBufferInfo (h
, &sb
))
368 cr
= sb
.dwCursorPosition
;
371 /* The cursor position shall be relative to the view coord of
372 the console window, which is usually smaller than the actual
373 buffer. FWIW, the 'appropriate' solution will be shrinking
374 the buffer to match the size of the console window,
375 destroying scrollback in the process. */
376 n1
+= sb
.srWindow
.Top
;
377 n2
+= sb
.srWindow
.Left
;
378 /* Stop at the topmost or bottommost boundary. */
381 else if (n1
> sb
.dwSize
.Y
)
385 /* Stop at the leftmost or rightmost boundary. */
388 else if (n2
> sb
.dwSize
.X
)
392 SetConsoleCursorPosition (h
, cr
);
398 case MAKEWORD ('[', 'J'):
399 if (esc_head
== esc_term
)
400 /* This is one of the very few codes whose parameters have
401 a default value of zero. */
405 n1
= strtol (esc_head
, &eptr
, 10);
406 if (eptr
!= esc_term
)
410 if (GetConsoleScreenBufferInfo (h
, &sb
))
412 /* The cursor is not necessarily in the console window, which
413 makes the behavior of this code harder to define. */
417 /* If the cursor is in or above the window, erase from
418 it to the bottom of the window; otherwise, do nothing. */
419 cr
= sb
.dwCursorPosition
;
420 cnt
= sb
.dwSize
.X
- sb
.dwCursorPosition
.X
;
421 rows
= sb
.srWindow
.Bottom
- sb
.dwCursorPosition
.Y
;
424 /* If the cursor is in or under the window, erase from
425 it to the top of the window; otherwise, do nothing. */
427 cr
.Y
= sb
.srWindow
.Top
;
428 cnt
= sb
.dwCursorPosition
.X
+ 1;
429 rows
= sb
.dwCursorPosition
.Y
- sb
.srWindow
.Top
;
432 /* Erase the entire window. */
433 cr
.X
= sb
.srWindow
.Left
;
434 cr
.Y
= sb
.srWindow
.Top
;
436 rows
= sb
.srWindow
.Bottom
- sb
.srWindow
.Top
+ 1;
439 /* Erase the entire buffer. */
448 cnt
+= rows
* sb
.dwSize
.X
;
449 FillConsoleOutputCharacterW (h
, L
' ', cnt
, cr
, &step
);
450 FillConsoleOutputAttribute (h
, sb
.wAttributes
, cnt
, cr
, &step
);
456 case MAKEWORD ('[', 'K'):
457 if (esc_head
== esc_term
)
458 /* This is one of the very few codes whose parameters have
459 a default value of zero. */
463 n1
= strtol (esc_head
, &eptr
, 10);
464 if (eptr
!= esc_term
)
468 if (GetConsoleScreenBufferInfo (h
, &sb
))
473 /* Erase from the cursor to the end. */
474 cr
= sb
.dwCursorPosition
;
475 cnt
= sb
.dwSize
.X
- sb
.dwCursorPosition
.X
;
478 /* Erase from the cursor to the beginning. */
479 cr
= sb
.dwCursorPosition
;
481 cnt
= sb
.dwCursorPosition
.X
+ 1;
484 /* Erase the entire line. */
485 cr
= sb
.dwCursorPosition
;
490 FillConsoleOutputCharacterW (h
, L
' ', cnt
, cr
, &step
);
491 FillConsoleOutputAttribute (h
, sb
.wAttributes
, cnt
, cr
, &step
);
495 /* ESC [ n1 ';' n2 'm'
496 Set SGR parameters. Zero or more parameters will follow. */
497 case MAKEWORD ('[', 'm'):
500 if (esc_head
== esc_term
)
502 /* When no parameter is given, reset the console. */
503 attrib_add
|= (FOREGROUND_RED
| FOREGROUND_GREEN
505 attrib_rm
= -1; /* Removes everything. */
511 /* Parse a parameter. */
512 n1
= strtol (param
, &eptr
, 10);
513 if (*eptr
!= ';' && eptr
!= esc_term
)
520 attrib_add
|= (FOREGROUND_RED
| FOREGROUND_GREEN
522 attrib_rm
= -1; /* Removes everything. */
526 attrib_add
|= FOREGROUND_INTENSITY
;
530 attrib_add
|= COMMON_LVB_UNDERSCORE
;
534 /* XXX: It is not BLINKING at all! */
535 attrib_add
|= BACKGROUND_INTENSITY
;
539 attrib_add
|= COMMON_LVB_REVERSE_VIDEO
;
543 attrib_add
&= ~FOREGROUND_INTENSITY
;
544 attrib_rm
|= FOREGROUND_INTENSITY
;
548 attrib_add
&= ~COMMON_LVB_UNDERSCORE
;
549 attrib_rm
|= COMMON_LVB_UNDERSCORE
;
553 /* XXX: It is not BLINKING at all! */
554 attrib_add
&= ~BACKGROUND_INTENSITY
;
555 attrib_rm
|= BACKGROUND_INTENSITY
;
559 attrib_add
&= ~COMMON_LVB_REVERSE_VIDEO
;
560 attrib_rm
|= COMMON_LVB_REVERSE_VIDEO
;
570 /* Foreground color. */
571 attrib_add
&= ~(FOREGROUND_RED
| FOREGROUND_GREEN
575 attrib_add
|= FOREGROUND_RED
;
577 attrib_add
|= FOREGROUND_GREEN
;
579 attrib_add
|= FOREGROUND_BLUE
;
580 attrib_rm
|= (FOREGROUND_RED
| FOREGROUND_GREEN
584 /* Reserved for extended foreground color.
585 Don't know how to handle parameters remaining.
589 /* Reset foreground color. */
591 attrib_add
|= (FOREGROUND_RED
| FOREGROUND_GREEN
593 attrib_rm
|= (FOREGROUND_RED
| FOREGROUND_GREEN
604 /* Background color. */
605 attrib_add
&= ~(BACKGROUND_RED
| BACKGROUND_GREEN
609 attrib_add
|= BACKGROUND_RED
;
611 attrib_add
|= BACKGROUND_GREEN
;
613 attrib_add
|= BACKGROUND_BLUE
;
614 attrib_rm
|= (BACKGROUND_RED
| BACKGROUND_GREEN
618 /* Reserved for extended background color.
619 Don't know how to handle parameters remaining.
623 /* Reset background color. */
625 attrib_add
&= ~(BACKGROUND_RED
| BACKGROUND_GREEN
627 attrib_rm
|= (BACKGROUND_RED
| BACKGROUND_GREEN
632 /* Prepare the next parameter. */
635 while (param
!= esc_term
);
638 /* 0xFFFF removes everything. If it is not the case,
639 care must be taken to preserve old attributes. */
640 if (attrib_rm
!= 0xFFFF && GetConsoleScreenBufferInfo (h
, &sb
))
642 attrib_add
|= sb
.wAttributes
& ~attrib_rm
;
644 if (attrib_add
& COMMON_LVB_REVERSE_VIDEO
)
646 /* COMMON_LVB_REVERSE_VIDEO is only effective for DBCS.
647 * Swap foreground and background colors by hand.
649 attrib_add
= (attrib_add
& 0xFF00)
650 | ((attrib_add
& 0x00F0) >> 4)
651 | ((attrib_add
& 0x000F) << 4);
652 attrib_add
&= ~COMMON_LVB_REVERSE_VIDEO
;
654 SetConsoleTextAttribute (h
, attrib_add
);
660 mingw_ansi_fputs (const char *str
, FILE *fp
)
662 const char *read
= str
;
665 int esc_code
, prefix_len
;
666 const char *esc_head
, *esc_term
;
668 h
= (HANDLE
) _get_osfhandle (_fileno (fp
));
669 if (h
== INVALID_HANDLE_VALUE
)
672 /* Don't mess up stdio functions with Windows APIs. */
675 if (GetConsoleMode (h
, &mode
))
676 /* If it is a console, translate ANSI escape codes as needed. */
679 if ((esc_code
= find_esc_head (&prefix_len
, &esc_head
, read
)) == 0)
681 /* Write all remaining characters, then exit. */
682 write_all (h
, read
, strlen (read
));
685 if (find_esc_terminator (&esc_term
, esc_head
) == 0)
686 /* Ignore incomplete escape sequences at the moment.
687 FIXME: The escape state shall be cached for further calls
690 write_all (h
, read
, esc_head
- prefix_len
- read
);
691 eat_esc_sequence (h
, esc_code
, esc_head
, esc_term
);
695 /* If it is not a console, write everything as-is. */
696 write_all (h
, read
, strlen (read
));
701 #endif /* __MINGW32__ */
704 decode_utf8_char (const unsigned char *, size_t len
, unsigned int *);
705 static void pp_quoted_string (pretty_printer
*, const char *, size_t = -1);
707 /* Overwrite the given location/range within this text_info's rich_location.
708 For use e.g. when implementing "+" in client format decoders. */
711 text_info::set_location (unsigned int idx
, location_t loc
,
712 enum range_display_kind range_display_kind
)
714 gcc_checking_assert (m_richloc
);
715 m_richloc
->set_range (idx
, loc
, range_display_kind
);
719 text_info::get_location (unsigned int index_of_location
) const
721 gcc_checking_assert (m_richloc
);
723 if (index_of_location
== 0)
724 return m_richloc
->get_loc ();
726 return UNKNOWN_LOCATION
;
729 // Default construct an output buffer.
731 output_buffer::output_buffer ()
732 : formatted_obstack (),
734 obstack (&formatted_obstack
),
741 obstack_init (&formatted_obstack
);
742 obstack_init (&chunk_obstack
);
745 // Release resources owned by an output buffer at the end of lifetime.
747 output_buffer::~output_buffer ()
749 obstack_free (&chunk_obstack
, NULL
);
750 obstack_free (&formatted_obstack
, NULL
);
754 /* Format an integer given by va_arg (ARG, type-specifier T) where
755 type-specifier is a precision modifier as indicated by PREC. F is
756 a string used to construct the appropriate format-specifier. */
757 #define pp_integer_with_precision(PP, ARG, PREC, T, F) \
762 pp_scalar (PP, "%" F, va_arg (ARG, T)); \
766 pp_scalar (PP, "%l" F, va_arg (ARG, long T)); \
770 pp_scalar (PP, "%" HOST_LONG_LONG_FORMAT F, va_arg (ARG, long long T)); \
779 /* Subroutine of pp_set_maximum_length. Set up PRETTY-PRINTER's
780 internal maximum characters per line. */
782 pp_set_real_maximum_length (pretty_printer
*pp
)
784 /* If we're told not to wrap lines then do the obvious thing. In case
785 we'll emit prefix only once per message, it is appropriate
786 not to increase unnecessarily the line-length cut-off. */
787 if (!pp_is_wrapping_line (pp
)
788 || pp_prefixing_rule (pp
) == DIAGNOSTICS_SHOW_PREFIX_ONCE
789 || pp_prefixing_rule (pp
) == DIAGNOSTICS_SHOW_PREFIX_NEVER
)
790 pp
->maximum_length
= pp_line_cutoff (pp
);
793 int prefix_length
= pp
->prefix
? strlen (pp
->prefix
) : 0;
794 /* If the prefix is ridiculously too long, output at least
796 if (pp_line_cutoff (pp
) - prefix_length
< 32)
797 pp
->maximum_length
= pp_line_cutoff (pp
) + 32;
799 pp
->maximum_length
= pp_line_cutoff (pp
);
803 /* Clear PRETTY-PRINTER's output state. */
805 pp_clear_state (pretty_printer
*pp
)
807 pp
->emitted_prefix
= false;
808 pp_indentation (pp
) = 0;
811 /* Print X to PP in decimal. */
812 template<unsigned int N
, typename T
>
814 pp_wide_integer (pretty_printer
*pp
, const poly_int
<N
, T
> &x
)
816 if (x
.is_constant ())
817 pp_wide_integer (pp
, x
.coeffs
[0]);
820 pp_left_bracket (pp
);
821 for (unsigned int i
= 0; i
< N
; ++i
)
825 pp_wide_integer (pp
, x
.coeffs
[i
]);
827 pp_right_bracket (pp
);
831 template void pp_wide_integer (pretty_printer
*, const poly_uint16
&);
832 template void pp_wide_integer (pretty_printer
*, const poly_int64
&);
833 template void pp_wide_integer (pretty_printer
*, const poly_uint64
&);
835 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream. */
837 pp_write_text_to_stream (pretty_printer
*pp
)
839 const char *text
= pp_formatted_text (pp
);
841 mingw_ansi_fputs (text
, pp_buffer (pp
)->stream
);
843 fputs (text
, pp_buffer (pp
)->stream
);
845 pp_clear_output_area (pp
);
848 /* As pp_write_text_to_stream, but for GraphViz label output.
850 Flush the formatted text of pretty-printer PP onto the attached stream.
851 Replace characters in PPF that have special meaning in a GraphViz .dot
854 This routine is not very fast, but it doesn't have to be as this is only
855 be used by routines dumping intermediate representations in graph form. */
858 pp_write_text_as_dot_label_to_stream (pretty_printer
*pp
, bool for_record
)
860 const char *text
= pp_formatted_text (pp
);
861 const char *p
= text
;
862 FILE *fp
= pp_buffer (pp
)->stream
;
869 /* Print newlines as a left-aligned newline. */
875 /* The following characters are only special for record-shape nodes. */
882 escape_char
= for_record
;
885 /* The following characters always have to be escaped
886 for use in labels. */
888 /* There is a bug in some (f.i. 2.36.0) versions of graphiz
889 ( http://www.graphviz.org/mantisbt/view.php?id=2524 ) related to
890 backslash as last char in label. Let's avoid triggering it. */
891 gcc_assert (*(p
+ 1) != '\0');
908 pp_clear_output_area (pp
);
911 /* As pp_write_text_to_stream, but for GraphViz HTML-like strings.
913 Flush the formatted text of pretty-printer PP onto the attached stream,
914 escaping these characters
916 using XML escape sequences.
918 http://www.graphviz.org/doc/info/lang.html#html states:
919 special XML escape sequences for ", &, <, and > may be necessary in
920 order to embed these characters in attribute values or raw text
921 This doesn't list "'" (which would normally be escaped in XML
922 as "'" or in HTML as "'");.
924 Experiments show that escaping "'" doesn't seem to be necessary. */
927 pp_write_text_as_html_like_dot_to_stream (pretty_printer
*pp
)
929 const char *text
= pp_formatted_text (pp
);
930 const char *p
= text
;
931 FILE *fp
= pp_buffer (pp
)->stream
;
938 fputs (""", fp
);
956 pp_clear_output_area (pp
);
959 /* Wrap a text delimited by START and END into PRETTY-PRINTER. */
961 pp_wrap_text (pretty_printer
*pp
, const char *start
, const char *end
)
963 bool wrapping_line
= pp_is_wrapping_line (pp
);
967 /* Dump anything bordered by whitespaces. */
969 const char *p
= start
;
970 while (p
!= end
&& !ISBLANK (*p
) && *p
!= '\n')
973 && p
- start
>= pp_remaining_character_count_for_line (pp
))
975 pp_append_text (pp
, start
, p
);
979 if (start
!= end
&& ISBLANK (*start
))
984 if (start
!= end
&& *start
== '\n')
992 /* Same as pp_wrap_text but wrap text only when in line-wrapping mode. */
994 pp_maybe_wrap_text (pretty_printer
*pp
, const char *start
, const char *end
)
996 if (pp_is_wrapping_line (pp
))
997 pp_wrap_text (pp
, start
, end
);
999 pp_append_text (pp
, start
, end
);
1002 /* Append to the output area of PRETTY-PRINTER a string specified by its
1003 STARTing character and LENGTH. */
1005 pp_append_r (pretty_printer
*pp
, const char *start
, int length
)
1007 output_buffer_append_r (pp_buffer (pp
), start
, length
);
1010 /* Insert enough spaces into the output area of PRETTY-PRINTER to bring
1011 the column position to the current indentation level, assuming that a
1012 newline has just been written to the buffer. */
1014 pp_indent (pretty_printer
*pp
)
1016 int n
= pp_indentation (pp
);
1019 for (i
= 0; i
< n
; ++i
)
1023 static const char *get_end_url_string (pretty_printer
*);
1025 /* The following format specifiers are recognized as being client independent:
1026 %d, %i: (signed) integer in base ten.
1027 %u: unsigned integer in base ten.
1028 %o: unsigned integer in base eight.
1029 %x: unsigned integer in base sixteen.
1030 %ld, %li, %lo, %lu, %lx: long versions of the above.
1031 %lld, %lli, %llo, %llu, %llx: long long versions.
1032 %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
1036 %p: pointer (printed in a host-dependent manner).
1037 %r: if pp_show_color(pp), switch to color identified by const char *.
1038 %R: if pp_show_color(pp), reset color.
1039 %m: strerror(text->err_no) - does not consume a value from args_ptr.
1043 %{: URL start. Consumes a const char * argument for the URL.
1044 %}: URL end. Does not consume any arguments.
1045 %': apostrophe (should only be used in untranslated messages;
1046 translations should use appropriate punctuation directly).
1047 %@: diagnostic_event_id_ptr, for which event_id->known_p () must be true.
1048 %.*s: a substring the length of which is specified by an argument
1050 %Ns: likewise, but length specified as constant in the format string.
1051 Flag 'q': quote formatted text (must come immediately after '%').
1052 %Z: Requires two arguments - array of int, and len. Prints elements
1055 Arguments can be used sequentially, or through %N$ resp. *N$
1056 notation Nth argument after the format string. If %N$ / *N$
1057 notation is used, it must be used for all arguments, except %m, %%,
1058 %<, %>, %} and %', which may not have a number, as they do not consume
1059 an argument. When %M$.*N$s is used, M must be N + 1. (This may
1060 also be written %M$.*s, provided N is not otherwise used.) The
1061 format string must have conversion specifiers with argument numbers
1062 1 up to highest argument; each argument may only be used once.
1063 A format string can have at most 30 arguments. */
1065 /* Formatting phases 1 and 2: render TEXT->format_spec plus
1066 text->m_args_ptr into a series of chunks in pp_buffer (PP)->args[].
1067 Phase 3 is in pp_output_formatted_text. */
1070 pp_format (pretty_printer
*pp
, text_info
*text
)
1072 output_buffer
*buffer
= pp_buffer (pp
);
1075 struct chunk_info
*new_chunk_array
;
1077 unsigned int curarg
= 0, chunk
= 0, argno
;
1078 pp_wrapping_mode_t old_wrapping_mode
;
1079 bool any_unnumbered
= false, any_numbered
= false;
1080 const char **formatters
[PP_NL_ARGMAX
];
1082 /* Allocate a new chunk structure. */
1083 new_chunk_array
= XOBNEW (&buffer
->chunk_obstack
, struct chunk_info
);
1084 new_chunk_array
->prev
= buffer
->cur_chunk_array
;
1085 buffer
->cur_chunk_array
= new_chunk_array
;
1086 args
= new_chunk_array
->args
;
1088 /* Formatting phase 1: split up TEXT->format_spec into chunks in
1089 pp_buffer (PP)->args[]. Even-numbered chunks are to be output
1090 verbatim, odd-numbered chunks are format specifiers.
1091 %m, %%, %<, %>, %} and %' are replaced with the appropriate text at
1094 memset (formatters
, 0, sizeof formatters
);
1096 for (p
= text
->m_format_spec
; *p
; )
1098 while (*p
!= '\0' && *p
!= '%')
1100 obstack_1grow (&buffer
->chunk_obstack
, *p
);
1113 obstack_1grow (&buffer
->chunk_obstack
, '%');
1119 obstack_grow (&buffer
->chunk_obstack
,
1120 open_quote
, strlen (open_quote
));
1121 const char *colorstr
1122 = colorize_start (pp_show_color (pp
), "quote");
1123 obstack_grow (&buffer
->chunk_obstack
, colorstr
, strlen (colorstr
));
1130 const char *colorstr
= colorize_stop (pp_show_color (pp
));
1131 obstack_grow (&buffer
->chunk_obstack
, colorstr
, strlen (colorstr
));
1135 obstack_grow (&buffer
->chunk_obstack
,
1136 close_quote
, strlen (close_quote
));
1142 const char *endurlstr
= get_end_url_string (pp
);
1143 obstack_grow (&buffer
->chunk_obstack
, endurlstr
,
1144 strlen (endurlstr
));
1151 const char *colorstr
= colorize_stop (pp_show_color (pp
));
1152 obstack_grow (&buffer
->chunk_obstack
, colorstr
,
1160 const char *errstr
= xstrerror (text
->m_err_no
);
1161 obstack_grow (&buffer
->chunk_obstack
, errstr
, strlen (errstr
));
1167 /* Handled in phase 2. Terminate the plain chunk here. */
1168 obstack_1grow (&buffer
->chunk_obstack
, '\0');
1169 gcc_assert (chunk
< PP_NL_ARGMAX
* 2);
1170 args
[chunk
++] = XOBFINISH (&buffer
->chunk_obstack
, const char *);
1177 argno
= strtoul (p
, &end
, 10) - 1;
1179 gcc_assert (*p
== '$');
1182 any_numbered
= true;
1183 gcc_assert (!any_unnumbered
);
1188 any_unnumbered
= true;
1189 gcc_assert (!any_numbered
);
1191 gcc_assert (argno
< PP_NL_ARGMAX
);
1192 gcc_assert (!formatters
[argno
]);
1193 formatters
[argno
] = &args
[chunk
];
1196 obstack_1grow (&buffer
->chunk_obstack
, *p
);
1199 while (strchr ("qwl+#", p
[-1]));
1203 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
1204 (where M == N + 1). */
1209 obstack_1grow (&buffer
->chunk_obstack
, *p
);
1212 while (ISDIGIT (p
[-1]));
1213 gcc_assert (p
[-1] == 's');
1217 gcc_assert (*p
== '*');
1218 obstack_1grow (&buffer
->chunk_obstack
, '*');
1224 unsigned int argno2
= strtoul (p
, &end
, 10) - 1;
1226 gcc_assert (argno2
== argno
- 1);
1227 gcc_assert (!any_unnumbered
);
1228 gcc_assert (*p
== '$');
1231 formatters
[argno2
] = formatters
[argno
];
1235 gcc_assert (!any_numbered
);
1236 formatters
[argno
+1] = formatters
[argno
];
1239 gcc_assert (*p
== 's');
1240 obstack_1grow (&buffer
->chunk_obstack
, 's');
1247 obstack_1grow (&buffer
->chunk_obstack
, '\0');
1248 gcc_assert (chunk
< PP_NL_ARGMAX
* 2);
1249 args
[chunk
++] = XOBFINISH (&buffer
->chunk_obstack
, const char *);
1252 obstack_1grow (&buffer
->chunk_obstack
, '\0');
1253 gcc_assert (chunk
< PP_NL_ARGMAX
* 2);
1254 args
[chunk
++] = XOBFINISH (&buffer
->chunk_obstack
, const char *);
1257 /* Set output to the argument obstack, and switch line-wrapping and
1259 buffer
->obstack
= &buffer
->chunk_obstack
;
1260 const int old_line_length
= buffer
->line_length
;
1261 old_wrapping_mode
= pp_set_verbatim_wrapping (pp
);
1263 /* Second phase. Replace each formatter with the formatted text it
1266 for (argno
= 0; formatters
[argno
]; argno
++)
1274 /* We do not attempt to enforce any ordering on the modifier
1277 for (p
= *formatters
[argno
];; p
++)
1282 gcc_assert (!quote
);
1302 /* We don't support precision beyond that of "long long". */
1303 gcc_assert (precision
< 2);
1310 gcc_assert (!wide
|| precision
== 0);
1313 pp_begin_quote (pp
, pp_show_color (pp
));
1318 pp_string (pp
, colorize_start (pp_show_color (pp
),
1319 va_arg (*text
->m_args_ptr
,
1325 /* When quoting, print alphanumeric, punctuation, and the space
1326 character unchanged, and all others in hexadecimal with the
1327 "\x" prefix. Otherwise print them all unchanged. */
1328 int chr
= va_arg (*text
->m_args_ptr
, int);
1329 if (ISPRINT (chr
) || !quote
)
1330 pp_character (pp
, chr
);
1333 const char str
[2] = { chr
, '\0' };
1334 pp_quoted_string (pp
, str
, 1);
1342 pp_wide_integer (pp
, va_arg (*text
->m_args_ptr
, HOST_WIDE_INT
));
1344 pp_integer_with_precision
1345 (pp
, *text
->m_args_ptr
, precision
, int, "d");
1350 pp_scalar (pp
, "%" HOST_WIDE_INT_PRINT
"o",
1351 va_arg (*text
->m_args_ptr
, unsigned HOST_WIDE_INT
));
1353 pp_integer_with_precision
1354 (pp
, *text
->m_args_ptr
, precision
, unsigned, "o");
1359 pp_quoted_string (pp
, va_arg (*text
->m_args_ptr
, const char *));
1361 pp_string (pp
, va_arg (*text
->m_args_ptr
, const char *));
1365 pp_pointer (pp
, va_arg (*text
->m_args_ptr
, void *));
1370 pp_scalar (pp
, HOST_WIDE_INT_PRINT_UNSIGNED
,
1371 va_arg (*text
->m_args_ptr
, unsigned HOST_WIDE_INT
));
1373 pp_integer_with_precision
1374 (pp
, *text
->m_args_ptr
, precision
, unsigned, "u");
1378 pp_double (pp
, va_arg (*text
->m_args_ptr
, double));
1383 int *v
= va_arg (*text
->m_args_ptr
, int *);
1384 unsigned len
= va_arg (*text
->m_args_ptr
, unsigned);
1386 for (unsigned i
= 0; i
< len
; ++i
)
1388 pp_scalar (pp
, "%i", v
[i
]);
1400 pp_scalar (pp
, HOST_WIDE_INT_PRINT_HEX
,
1401 va_arg (*text
->m_args_ptr
, unsigned HOST_WIDE_INT
));
1403 pp_integer_with_precision
1404 (pp
, *text
->m_args_ptr
, precision
, unsigned, "x");
1412 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
1413 (where M == N + 1). The format string should be verified
1414 already from the first phase. */
1419 n
= strtoul (p
, &end
, 10);
1421 gcc_assert (*p
== 's');
1425 gcc_assert (*p
== '*');
1427 gcc_assert (*p
== 's');
1428 n
= va_arg (*text
->m_args_ptr
, int);
1430 /* This consumes a second entry in the formatters array. */
1431 gcc_assert (formatters
[argno
] == formatters
[argno
+1]);
1435 s
= va_arg (*text
->m_args_ptr
, const char *);
1437 /* Append the lesser of precision and strlen (s) characters
1438 from the array (which need not be a nul-terminated string).
1439 Negative precision is treated as if it were omitted. */
1440 size_t len
= n
< 0 ? strlen (s
) : strnlen (s
, n
);
1442 pp_append_text (pp
, s
, s
+ len
);
1448 /* diagnostic_event_id_t *. */
1449 diagnostic_event_id_ptr event_id
1450 = va_arg (*text
->m_args_ptr
, diagnostic_event_id_ptr
);
1451 gcc_assert (event_id
->known_p ());
1453 pp_string (pp
, colorize_start (pp_show_color (pp
), "path"));
1454 pp_character (pp
, '(');
1455 pp_decimal_int (pp
, event_id
->one_based ());
1456 pp_character (pp
, ')');
1457 pp_string (pp
, colorize_stop (pp_show_color (pp
)));
1462 pp_begin_url (pp
, va_arg (*text
->m_args_ptr
, const char *));
1469 /* Call the format decoder.
1470 Pass the address of "quote" so that format decoders can
1471 potentially disable printing of the closing quote
1472 (e.g. when printing "'TYPEDEF' aka 'TYPE'" in the C family
1474 gcc_assert (pp_format_decoder (pp
));
1475 ok
= pp_format_decoder (pp
) (pp
, text
, p
,
1476 precision
, wide
, plus
, hash
, "e
,
1483 pp_end_quote (pp
, pp_show_color (pp
));
1485 obstack_1grow (&buffer
->chunk_obstack
, '\0');
1486 *formatters
[argno
] = XOBFINISH (&buffer
->chunk_obstack
, const char *);
1490 for (; argno
< PP_NL_ARGMAX
; argno
++)
1491 gcc_assert (!formatters
[argno
]);
1493 /* If the client supplied a postprocessing object, call its "handle"
1495 if (pp
->m_format_postprocessor
)
1496 pp
->m_format_postprocessor
->handle (pp
);
1498 /* Revert to normal obstack and wrapping mode. */
1499 buffer
->obstack
= &buffer
->formatted_obstack
;
1500 buffer
->line_length
= old_line_length
;
1501 pp_wrapping_mode (pp
) = old_wrapping_mode
;
1502 pp_clear_state (pp
);
1505 /* Format of a message pointed to by TEXT. */
1507 pp_output_formatted_text (pretty_printer
*pp
)
1510 output_buffer
*buffer
= pp_buffer (pp
);
1511 struct chunk_info
*chunk_array
= buffer
->cur_chunk_array
;
1512 const char **args
= chunk_array
->args
;
1514 gcc_assert (buffer
->obstack
== &buffer
->formatted_obstack
);
1516 /* This is a third phase, first 2 phases done in pp_format_args.
1517 Now we actually print it. */
1518 for (chunk
= 0; args
[chunk
]; chunk
++)
1519 pp_string (pp
, args
[chunk
]);
1521 /* Deallocate the chunk structure and everything after it (i.e. the
1522 associated series of formatted strings). */
1523 buffer
->cur_chunk_array
= chunk_array
->prev
;
1524 obstack_free (&buffer
->chunk_obstack
, chunk_array
);
1527 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
1528 settings needed by BUFFER for a verbatim formatting. */
1530 pp_format_verbatim (pretty_printer
*pp
, text_info
*text
)
1532 /* Set verbatim mode. */
1533 pp_wrapping_mode_t oldmode
= pp_set_verbatim_wrapping (pp
);
1535 /* Do the actual formatting. */
1536 pp_format (pp
, text
);
1537 pp_output_formatted_text (pp
);
1539 /* Restore previous settings. */
1540 pp_wrapping_mode (pp
) = oldmode
;
1543 /* Flush the content of BUFFER onto the attached stream. This
1544 function does nothing unless pp->output_buffer->flush_p. */
1546 pp_flush (pretty_printer
*pp
)
1548 pp_clear_state (pp
);
1549 if (!pp
->buffer
->flush_p
)
1551 pp_write_text_to_stream (pp
);
1552 fflush (pp_buffer (pp
)->stream
);
1555 /* Flush the content of BUFFER onto the attached stream independently
1556 of the value of pp->output_buffer->flush_p. */
1558 pp_really_flush (pretty_printer
*pp
)
1560 pp_clear_state (pp
);
1561 pp_write_text_to_stream (pp
);
1562 fflush (pp_buffer (pp
)->stream
);
1565 /* Sets the number of maximum characters per line PRETTY-PRINTER can
1566 output in line-wrapping mode. A LENGTH value 0 suppresses
1569 pp_set_line_maximum_length (pretty_printer
*pp
, int length
)
1571 pp_line_cutoff (pp
) = length
;
1572 pp_set_real_maximum_length (pp
);
1575 /* Clear PRETTY-PRINTER output area text info. */
1577 pp_clear_output_area (pretty_printer
*pp
)
1579 obstack_free (pp_buffer (pp
)->obstack
,
1580 obstack_base (pp_buffer (pp
)->obstack
));
1581 pp_buffer (pp
)->line_length
= 0;
1584 /* Set PREFIX for PRETTY-PRINTER, taking ownership of PREFIX, which
1585 will eventually be free-ed. */
1588 pp_set_prefix (pretty_printer
*pp
, char *prefix
)
1591 pp
->prefix
= prefix
;
1592 pp_set_real_maximum_length (pp
);
1593 pp
->emitted_prefix
= false;
1594 pp_indentation (pp
) = 0;
1597 /* Take ownership of PP's prefix, setting it to NULL.
1598 This allows clients to save, override, and then restore an existing
1599 prefix, without it being free-ed. */
1602 pp_take_prefix (pretty_printer
*pp
)
1604 char *result
= pp
->prefix
;
1609 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
1611 pp_destroy_prefix (pretty_printer
*pp
)
1613 if (pp
->prefix
!= NULL
)
1620 /* Write out PRETTY-PRINTER's prefix. */
1622 pp_emit_prefix (pretty_printer
*pp
)
1624 if (pp
->prefix
!= NULL
)
1626 switch (pp_prefixing_rule (pp
))
1629 case DIAGNOSTICS_SHOW_PREFIX_NEVER
:
1632 case DIAGNOSTICS_SHOW_PREFIX_ONCE
:
1633 if (pp
->emitted_prefix
)
1638 pp_indentation (pp
) += 3;
1641 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
:
1643 int prefix_length
= strlen (pp
->prefix
);
1644 pp_append_r (pp
, pp
->prefix
, prefix_length
);
1645 pp
->emitted_prefix
= true;
1652 /* Construct a PRETTY-PRINTER of MAXIMUM_LENGTH characters per line. */
1654 pretty_printer::pretty_printer (int maximum_length
)
1655 : buffer (new (XCNEW (output_buffer
)) output_buffer ()),
1662 m_format_postprocessor (NULL
),
1665 translate_identifiers (true),
1667 url_format (URL_FORMAT_NONE
),
1668 m_skipping_null_url (false)
1670 pp_line_cutoff (this) = maximum_length
;
1671 /* By default, we emit prefixes once per message. */
1672 pp_prefixing_rule (this) = DIAGNOSTICS_SHOW_PREFIX_ONCE
;
1673 pp_set_prefix (this, NULL
);
1676 /* Copy constructor for pretty_printer. */
1678 pretty_printer::pretty_printer (const pretty_printer
&other
)
1679 : buffer (new (XCNEW (output_buffer
)) output_buffer ()),
1681 padding (other
.padding
),
1682 maximum_length (other
.maximum_length
),
1683 indent_skip (other
.indent_skip
),
1684 wrapping (other
.wrapping
),
1685 format_decoder (other
.format_decoder
),
1686 m_format_postprocessor (NULL
),
1687 emitted_prefix (other
.emitted_prefix
),
1688 need_newline (other
.need_newline
),
1689 translate_identifiers (other
.translate_identifiers
),
1690 show_color (other
.show_color
),
1691 url_format (other
.url_format
),
1692 m_skipping_null_url (false)
1694 pp_line_cutoff (this) = maximum_length
;
1695 /* By default, we emit prefixes once per message. */
1696 pp_prefixing_rule (this) = pp_prefixing_rule (&other
);
1697 pp_set_prefix (this, NULL
);
1699 if (other
.m_format_postprocessor
)
1700 m_format_postprocessor
= other
.m_format_postprocessor
->clone ();
1703 pretty_printer::~pretty_printer ()
1705 if (m_format_postprocessor
)
1706 delete m_format_postprocessor
;
1707 buffer
->~output_buffer ();
1712 /* Base class implementation of pretty_printer::clone vfunc. */
1715 pretty_printer::clone () const
1717 return new pretty_printer (*this);
1720 /* Append a string delimited by START and END to the output area of
1721 PRETTY-PRINTER. No line wrapping is done. However, if beginning a
1722 new line then emit PRETTY-PRINTER's prefix and skip any leading
1723 whitespace if appropriate. The caller must ensure that it is
1726 pp_append_text (pretty_printer
*pp
, const char *start
, const char *end
)
1728 /* Emit prefix and skip whitespace if we're starting a new line. */
1729 if (pp_buffer (pp
)->line_length
== 0)
1731 pp_emit_prefix (pp
);
1732 if (pp_is_wrapping_line (pp
))
1733 while (start
!= end
&& *start
== ' ')
1736 pp_append_r (pp
, start
, end
- start
);
1739 /* Finishes constructing a NULL-terminated character string representing
1740 the PRETTY-PRINTED text. */
1742 pp_formatted_text (pretty_printer
*pp
)
1744 return output_buffer_formatted_text (pp_buffer (pp
));
1747 /* Return a pointer to the last character emitted in PRETTY-PRINTER's
1748 output area. A NULL pointer means no character available. */
1750 pp_last_position_in_text (const pretty_printer
*pp
)
1752 return output_buffer_last_position_in_text (pp_buffer (pp
));
1755 /* Return the amount of characters PRETTY-PRINTER can accept to
1756 make a full line. Meaningful only in line-wrapping mode. */
1758 pp_remaining_character_count_for_line (pretty_printer
*pp
)
1760 return pp
->maximum_length
- pp_buffer (pp
)->line_length
;
1764 /* Format a message into BUFFER a la printf. */
1766 pp_printf (pretty_printer
*pp
, const char *msg
, ...)
1771 text_info
text (msg
, &ap
, errno
);
1772 pp_format (pp
, &text
);
1773 pp_output_formatted_text (pp
);
1778 /* Output MESSAGE verbatim into BUFFER. */
1780 pp_verbatim (pretty_printer
*pp
, const char *msg
, ...)
1785 text_info
text (msg
, &ap
, errno
);
1786 pp_format_verbatim (pp
, &text
);
1792 /* Have PRETTY-PRINTER start a new line. */
1794 pp_newline (pretty_printer
*pp
)
1796 obstack_1grow (pp_buffer (pp
)->obstack
, '\n');
1797 pp_needs_newline (pp
) = false;
1798 pp_buffer (pp
)->line_length
= 0;
1801 /* Have PRETTY-PRINTER add a CHARACTER. */
1803 pp_character (pretty_printer
*pp
, int c
)
1805 if (pp_is_wrapping_line (pp
)
1806 /* If printing UTF-8, don't wrap in the middle of a sequence. */
1807 && (((unsigned int) c
) & 0xC0) != 0x80
1808 && pp_remaining_character_count_for_line (pp
) <= 0)
1814 obstack_1grow (pp_buffer (pp
)->obstack
, c
);
1815 ++pp_buffer (pp
)->line_length
;
1818 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
1819 be line-wrapped if in appropriate mode. */
1821 pp_string (pretty_printer
*pp
, const char *str
)
1823 gcc_checking_assert (str
);
1824 pp_maybe_wrap_text (pp
, str
, str
+ strlen (str
));
1827 /* Append code point C to the output area of PRETTY-PRINTER, encoding it
1831 pp_unicode_character (pretty_printer
*pp
, unsigned c
)
1833 static const uchar masks
[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
1834 static const uchar limits
[6] = { 0x80, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE };
1836 uchar buf
[6], *p
= &buf
[6];
1845 *--p
= ((c
& 0x3F) | 0x80);
1849 while (c
>= 0x3F || (c
& limits
[nbytes
-1]));
1850 *--p
= (c
| masks
[nbytes
-1]);
1853 pp_append_r (pp
, (const char *)p
, nbytes
);
1856 /* Append the leading N characters of STRING to the output area of
1857 PRETTY-PRINTER, quoting in hexadecimal non-printable characters.
1858 Setting N = -1 is as if N were set to strlen (STRING). The STRING
1859 may be line-wrapped if in appropriate mode. */
1861 pp_quoted_string (pretty_printer
*pp
, const char *str
, size_t n
/* = -1 */)
1863 gcc_checking_assert (str
);
1865 const char *last
= str
;
1868 /* Compute the length if not specified. */
1869 if (n
== (size_t) -1)
1872 for (ps
= str
; n
; ++ps
, --n
)
1877 /* Don't escape a valid UTF-8 extended char. */
1878 const unsigned char *ups
= (const unsigned char *) ps
;
1881 unsigned int extended_char
;
1882 const int valid_utf8_len
= decode_utf8_char (ups
, n
, &extended_char
);
1883 if (valid_utf8_len
> 0)
1885 ps
+= valid_utf8_len
- 1;
1886 n
-= valid_utf8_len
- 1;
1892 pp_maybe_wrap_text (pp
, last
, ps
);
1894 /* Append the hexadecimal value of the character. Allocate a buffer
1895 that's large enough for a 32-bit char plus the hex prefix. */
1897 int n
= sprintf (buf
, "\\x%02x", (unsigned char)*ps
);
1898 pp_maybe_wrap_text (pp
, buf
, buf
+ n
);
1902 pp_maybe_wrap_text (pp
, last
, ps
);
1905 /* Maybe print out a whitespace if needed. */
1908 pp_maybe_space (pretty_printer
*pp
)
1910 if (pp
->padding
!= pp_none
)
1913 pp
->padding
= pp_none
;
1917 // Add a newline to the pretty printer PP and flush formatted text.
1920 pp_newline_and_flush (pretty_printer
*pp
)
1924 pp_needs_newline (pp
) = false;
1927 // Add a newline to the pretty printer PP, followed by indentation.
1930 pp_newline_and_indent (pretty_printer
*pp
, int n
)
1932 pp_indentation (pp
) += n
;
1935 pp_needs_newline (pp
) = false;
1938 // Add separator C, followed by a single whitespace.
1941 pp_separate_with (pretty_printer
*pp
, char c
)
1943 pp_character (pp
, c
);
1947 /* Add a localized open quote, and if SHOW_COLOR is true, begin colorizing
1948 using the "quote" color. */
1951 pp_begin_quote (pretty_printer
*pp
, bool show_color
)
1953 pp_string (pp
, open_quote
);
1954 pp_string (pp
, colorize_start (show_color
, "quote"));
1957 /* If SHOW_COLOR is true, stop colorizing.
1958 Add a localized close quote. */
1961 pp_end_quote (pretty_printer
*pp
, bool show_color
)
1963 pp_string (pp
, colorize_stop (show_color
));
1964 pp_string (pp
, close_quote
);
1968 /* The string starting at P has LEN (at least 1) bytes left; if they
1969 start with a valid UTF-8 sequence, return the length of that
1970 sequence and set *VALUE to the value of that sequence, and
1971 otherwise return 0 and set *VALUE to (unsigned int) -1. */
1974 decode_utf8_char (const unsigned char *p
, size_t len
, unsigned int *value
)
1976 unsigned int t
= *p
;
1982 size_t utf8_len
= 0;
1985 for (t
= *p
; t
& 0x80; t
<<= 1)
1988 if (utf8_len
> len
|| utf8_len
< 2 || utf8_len
> 6)
1990 *value
= (unsigned int) -1;
1993 ch
= *p
& ((1 << (7 - utf8_len
)) - 1);
1994 for (i
= 1; i
< utf8_len
; i
++)
1996 unsigned int u
= p
[i
];
1997 if ((u
& 0xC0) != 0x80)
1999 *value
= (unsigned int) -1;
2002 ch
= (ch
<< 6) | (u
& 0x3F);
2004 if ( (ch
<= 0x7F && utf8_len
> 1)
2005 || (ch
<= 0x7FF && utf8_len
> 2)
2006 || (ch
<= 0xFFFF && utf8_len
> 3)
2007 || (ch
<= 0x1FFFFF && utf8_len
> 4)
2008 || (ch
<= 0x3FFFFFF && utf8_len
> 5)
2009 || (ch
>= 0xD800 && ch
<= 0xDFFF))
2011 *value
= (unsigned int) -1;
2024 /* Allocator for identifier_to_locale and corresponding function to
2027 void *(*identifier_to_locale_alloc
) (size_t) = xmalloc
;
2028 void (*identifier_to_locale_free
) (void *) = free
;
2030 /* Given IDENT, an identifier in the internal encoding, return a
2031 version of IDENT suitable for diagnostics in the locale character
2032 set: either IDENT itself, or a string, allocated using
2033 identifier_to_locale_alloc, converted to the locale character set
2034 and using escape sequences if not representable in the locale
2035 character set or containing control characters or invalid byte
2036 sequences. Existing backslashes in IDENT are not doubled, so the
2037 result may not uniquely specify the contents of an arbitrary byte
2038 sequence identifier. */
2041 identifier_to_locale (const char *ident
)
2043 const unsigned char *uid
= (const unsigned char *) ident
;
2044 size_t idlen
= strlen (ident
);
2045 bool valid_printable_utf8
= true;
2046 bool all_ascii
= true;
2049 for (i
= 0; i
< idlen
;)
2052 size_t utf8_len
= decode_utf8_char (&uid
[i
], idlen
- i
, &c
);
2053 if (utf8_len
== 0 || c
<= 0x1F || (c
>= 0x7F && c
<= 0x9F))
2055 valid_printable_utf8
= false;
2063 /* If IDENT contains invalid UTF-8 sequences (which may occur with
2064 attributes putting arbitrary byte sequences in identifiers), or
2065 control characters, we use octal escape sequences for all bytes
2066 outside printable ASCII. */
2067 if (!valid_printable_utf8
)
2069 char *ret
= (char *) identifier_to_locale_alloc (4 * idlen
+ 1);
2071 for (i
= 0; i
< idlen
; i
++)
2073 if (uid
[i
] > 0x1F && uid
[i
] < 0x7F)
2077 sprintf (p
, "\\%03o", uid
[i
]);
2085 /* Otherwise, if it is valid printable ASCII, or printable UTF-8
2086 with the locale character set being UTF-8, IDENT is used. */
2087 if (all_ascii
|| locale_utf8
)
2090 /* Otherwise IDENT is converted to the locale character set if
2092 #if defined ENABLE_NLS && defined HAVE_LANGINFO_CODESET && HAVE_ICONV
2093 if (locale_encoding
!= NULL
)
2095 iconv_t cd
= iconv_open (locale_encoding
, "UTF-8");
2096 bool conversion_ok
= true;
2098 if (cd
!= (iconv_t
) -1)
2100 size_t ret_alloc
= 4 * idlen
+ 1;
2103 /* Repeat the whole conversion process as needed with
2104 larger buffers so non-reversible transformations can
2105 always be detected. */
2106 ICONV_CONST
char *inbuf
= CONST_CAST (char *, ident
);
2108 size_t inbytesleft
= idlen
;
2109 size_t outbytesleft
= ret_alloc
- 1;
2112 ret
= (char *) identifier_to_locale_alloc (ret_alloc
);
2115 if (iconv (cd
, 0, 0, 0, 0) == (size_t) -1)
2117 conversion_ok
= false;
2121 iconv_ret
= iconv (cd
, &inbuf
, &inbytesleft
,
2122 &outbuf
, &outbytesleft
);
2123 if (iconv_ret
== (size_t) -1 || inbytesleft
!= 0)
2128 identifier_to_locale_free (ret
);
2134 conversion_ok
= false;
2138 else if (iconv_ret
!= 0)
2140 conversion_ok
= false;
2143 /* Return to initial shift state. */
2144 if (iconv (cd
, 0, 0, &outbuf
, &outbytesleft
) == (size_t) -1)
2149 identifier_to_locale_free (ret
);
2155 conversion_ok
= false;
2169 /* Otherwise, convert non-ASCII characters in IDENT to UCNs. */
2171 char *ret
= (char *) identifier_to_locale_alloc (10 * idlen
+ 1);
2173 for (i
= 0; i
< idlen
;)
2176 size_t utf8_len
= decode_utf8_char (&uid
[i
], idlen
- i
, &c
);
2181 sprintf (p
, "\\U%08x", c
);
2191 /* Support for encoding URLs.
2192 See egmontkob/Hyperlinks_in_Terminal_Emulators.md
2193 ( https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda ).
2195 > A hyperlink is opened upon encountering an OSC 8 escape sequence with
2196 > the target URI. The syntax is
2198 > OSC 8 ; params ; URI ST
2200 > A hyperlink is closed with the same escape sequence, omitting the
2201 > parameters and the URI but keeping the separators:
2205 > OSC (operating system command) is typically ESC ].
2207 Use BEL instead of ST, as that is currently rendered better in some
2208 terminal emulators that don't support OSC 8, like konsole. */
2210 /* If URL-printing is enabled, write an "open URL" escape sequence to PP
2211 for the given URL. */
2214 pp_begin_url (pretty_printer
*pp
, const char *url
)
2218 /* Handle null URL by skipping all output here,
2219 and in the next pp_end_url. */
2220 pp
->m_skipping_null_url
= true;
2223 switch (pp
->url_format
)
2225 case URL_FORMAT_NONE
:
2228 pp_string (pp
, "\33]8;;");
2229 pp_string (pp
, url
);
2230 pp_string (pp
, "\33\\");
2232 case URL_FORMAT_BEL
:
2233 pp_string (pp
, "\33]8;;");
2234 pp_string (pp
, url
);
2235 pp_string (pp
, "\a");
2242 /* Helper function for pp_end_url and pp_format, return the "close URL" escape
2246 get_end_url_string (pretty_printer
*pp
)
2248 switch (pp
->url_format
)
2250 case URL_FORMAT_NONE
:
2253 return "\33]8;;\33\\";
2254 case URL_FORMAT_BEL
:
2261 /* If URL-printing is enabled, write a "close URL" escape sequence to PP. */
2264 pp_end_url (pretty_printer
*pp
)
2266 if (pp
->m_skipping_null_url
)
2268 /* We gracefully handle pp_begin_url (NULL) by omitting output for
2269 both begin and end. Here we handle the latter. */
2270 pp
->m_skipping_null_url
= false;
2273 if (pp
->url_format
!= URL_FORMAT_NONE
)
2274 pp_string (pp
, get_end_url_string (pp
));
2279 namespace selftest
{
2281 /* Smoketest for pretty_printer. */
2284 test_basic_printing ()
2287 pp_string (&pp
, "hello");
2289 pp_string (&pp
, "world");
2291 ASSERT_STREQ ("hello world", pp_formatted_text (&pp
));
2294 /* Helper function for testing pp_format.
2295 Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
2296 prints EXPECTED, assuming that pp_show_color is SHOW_COLOR. */
2299 assert_pp_format_va (const location
&loc
, const char *expected
,
2300 bool show_color
, const char *fmt
, va_list *ap
)
2303 rich_location
rich_loc (line_table
, UNKNOWN_LOCATION
);
2305 text_info
ti (fmt
, ap
, 0, nullptr, &rich_loc
);
2307 pp_show_color (&pp
) = show_color
;
2308 pp_format (&pp
, &ti
);
2309 pp_output_formatted_text (&pp
);
2310 ASSERT_STREQ_AT (loc
, expected
, pp_formatted_text (&pp
));
2313 /* Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
2314 prints EXPECTED, with show_color disabled. */
2317 assert_pp_format (const location
&loc
, const char *expected
,
2318 const char *fmt
, ...)
2323 assert_pp_format_va (loc
, expected
, false, fmt
, &ap
);
2327 /* As above, but with colorization enabled. */
2330 assert_pp_format_colored (const location
&loc
, const char *expected
,
2331 const char *fmt
, ...)
2333 /* The tests of colorization assume the default color scheme.
2334 If GCC_COLORS is set, then the colors have potentially been
2335 overridden; skip the test. */
2336 if (getenv ("GCC_COLORS"))
2342 assert_pp_format_va (loc
, expected
, true, fmt
, &ap
);
2346 /* Helper function for calling testing pp_format,
2347 by calling assert_pp_format with various numbers of arguments.
2348 These exist mostly to avoid having to write SELFTEST_LOCATION
2349 throughout test_pp_format. */
2351 #define ASSERT_PP_FORMAT_1(EXPECTED, FMT, ARG1) \
2352 SELFTEST_BEGIN_STMT \
2353 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
2357 #define ASSERT_PP_FORMAT_2(EXPECTED, FMT, ARG1, ARG2) \
2358 SELFTEST_BEGIN_STMT \
2359 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
2363 #define ASSERT_PP_FORMAT_3(EXPECTED, FMT, ARG1, ARG2, ARG3) \
2364 SELFTEST_BEGIN_STMT \
2365 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
2366 (ARG1), (ARG2), (ARG3)); \
2369 /* Verify that pp_format works, for various format codes. */
2374 /* Avoid introducing locale-specific differences in the results
2375 by hardcoding open_quote and close_quote. */
2376 auto_fix_quotes fix_quotes
;
2378 /* Verify that plain text is passed through unchanged. */
2379 assert_pp_format (SELFTEST_LOCATION
, "unformatted", "unformatted");
2381 /* Verify various individual format codes, in the order listed in the
2382 comment for pp_format above. For each code, we append a second
2383 argument with a known bit pattern (0x12345678), to ensure that we
2384 are consuming arguments correctly. */
2385 ASSERT_PP_FORMAT_2 ("-27 12345678", "%d %x", -27, 0x12345678);
2386 ASSERT_PP_FORMAT_2 ("-5 12345678", "%i %x", -5, 0x12345678);
2387 ASSERT_PP_FORMAT_2 ("10 12345678", "%u %x", 10, 0x12345678);
2388 ASSERT_PP_FORMAT_2 ("17 12345678", "%o %x", 15, 0x12345678);
2389 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%x %x", 0xcafebabe, 0x12345678);
2390 ASSERT_PP_FORMAT_2 ("-27 12345678", "%ld %x", (long)-27, 0x12345678);
2391 ASSERT_PP_FORMAT_2 ("-5 12345678", "%li %x", (long)-5, 0x12345678);
2392 ASSERT_PP_FORMAT_2 ("10 12345678", "%lu %x", (long)10, 0x12345678);
2393 ASSERT_PP_FORMAT_2 ("17 12345678", "%lo %x", (long)15, 0x12345678);
2394 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%lx %x", (long)0xcafebabe,
2396 ASSERT_PP_FORMAT_2 ("-27 12345678", "%lld %x", (long long)-27, 0x12345678);
2397 ASSERT_PP_FORMAT_2 ("-5 12345678", "%lli %x", (long long)-5, 0x12345678);
2398 ASSERT_PP_FORMAT_2 ("10 12345678", "%llu %x", (long long)10, 0x12345678);
2399 ASSERT_PP_FORMAT_2 ("17 12345678", "%llo %x", (long long)15, 0x12345678);
2400 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%llx %x", (long long)0xcafebabe,
2402 ASSERT_PP_FORMAT_2 ("-27 12345678", "%wd %x", (HOST_WIDE_INT
)-27, 0x12345678);
2403 ASSERT_PP_FORMAT_2 ("-5 12345678", "%wi %x", (HOST_WIDE_INT
)-5, 0x12345678);
2404 ASSERT_PP_FORMAT_2 ("10 12345678", "%wu %x", (unsigned HOST_WIDE_INT
)10,
2406 ASSERT_PP_FORMAT_2 ("17 12345678", "%wo %x", (HOST_WIDE_INT
)15, 0x12345678);
2407 ASSERT_PP_FORMAT_2 ("0xcafebabe 12345678", "%wx %x", (HOST_WIDE_INT
)0xcafebabe,
2409 ASSERT_PP_FORMAT_2 ("1.000000 12345678", "%f %x", 1.0, 0x12345678);
2410 ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678);
2411 ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello world",
2414 /* Not nul-terminated. */
2415 char arr
[5] = { '1', '2', '3', '4', '5' };
2416 ASSERT_PP_FORMAT_3 ("123 12345678", "%.*s %x", 3, arr
, 0x12345678);
2417 ASSERT_PP_FORMAT_3 ("1234 12345678", "%.*s %x", -1, "1234", 0x12345678);
2418 ASSERT_PP_FORMAT_3 ("12345 12345678", "%.*s %x", 7, "12345", 0x12345678);
2420 /* We can't test for %p; the pointer is printed in an implementation-defined
2422 ASSERT_PP_FORMAT_2 ("normal colored normal 12345678",
2423 "normal %rcolored%R normal %x",
2424 "error", 0x12345678);
2425 assert_pp_format_colored
2427 "normal \33[01;31m\33[Kcolored\33[m\33[K normal 12345678",
2428 "normal %rcolored%R normal %x", "error", 0x12345678);
2430 %m: strerror(text->err_no) - does not consume a value from args_ptr. */
2431 ASSERT_PP_FORMAT_1 ("% 12345678", "%% %x", 0x12345678);
2432 ASSERT_PP_FORMAT_1 ("` 12345678", "%< %x", 0x12345678);
2433 ASSERT_PP_FORMAT_1 ("' 12345678", "%> %x", 0x12345678);
2434 ASSERT_PP_FORMAT_1 ("' 12345678", "%' %x", 0x12345678);
2435 ASSERT_PP_FORMAT_3 ("abc 12345678", "%.*s %x", 3, "abcdef", 0x12345678);
2436 ASSERT_PP_FORMAT_2 ("abc 12345678", "%.3s %x", "abcdef", 0x12345678);
2438 /* Verify flag 'q'. */
2439 ASSERT_PP_FORMAT_2 ("`foo' 12345678", "%qs %x", "foo", 0x12345678);
2440 assert_pp_format_colored (SELFTEST_LOCATION
,
2441 "`\33[01m\33[Kfoo\33[m\33[K' 12345678", "%qs %x",
2445 diagnostic_event_id_t
first (2);
2446 diagnostic_event_id_t
second (7);
2448 ASSERT_PP_FORMAT_2 ("first `free' at (3); second `free' at (8)",
2449 "first %<free%> at %@; second %<free%> at %@",
2451 assert_pp_format_colored
2453 "first `\e[01m\e[Kfree\e[m\e[K' at \e[01;36m\e[K(3)\e[m\e[K;"
2454 " second `\e[01m\e[Kfree\e[m\e[K' at \e[01;36m\e[K(8)\e[m\e[K",
2455 "first %<free%> at %@; second %<free%> at %@",
2460 int v
[] = { 1, 2, 3 };
2461 ASSERT_PP_FORMAT_3 ("1, 2, 3 12345678", "%Z %x", v
, 3, 0x12345678);
2464 ASSERT_PP_FORMAT_3 ("0 12345678", "%Z %x", v2
, 1, 0x12345678);
2466 /* Verify that combinations work, along with unformatted text. */
2467 assert_pp_format (SELFTEST_LOCATION
,
2468 "the quick brown fox jumps over the lazy dog",
2469 "the %s %s %s jumps over the %s %s",
2470 "quick", "brown", "fox", "lazy", "dog");
2471 assert_pp_format (SELFTEST_LOCATION
, "item 3 of 7", "item %i of %i", 3, 7);
2472 assert_pp_format (SELFTEST_LOCATION
, "problem with `bar' at line 10",
2473 "problem with %qs at line %i", "bar", 10);
2476 /* A subclass of pretty_printer for use by test_prefixes_and_wrapping. */
2478 class test_pretty_printer
: public pretty_printer
2481 test_pretty_printer (enum diagnostic_prefixing_rule_t rule
,
2482 int max_line_length
)
2484 pp_set_prefix (this, xstrdup ("PREFIX: "));
2485 wrapping
.rule
= rule
;
2486 pp_set_line_maximum_length (this, max_line_length
);
2490 /* Verify that the various values of enum diagnostic_prefixing_rule_t work
2491 as expected, with and without line wrapping. */
2494 test_prefixes_and_wrapping ()
2496 /* Tests of the various prefixing rules, without wrapping.
2497 Newlines embedded in pp_string don't affect it; we have to
2498 explicitly call pp_newline. */
2500 test_pretty_printer
pp (DIAGNOSTICS_SHOW_PREFIX_ONCE
, 0);
2501 pp_string (&pp
, "the quick brown fox");
2503 pp_string (&pp
, "jumps over the lazy dog");
2505 ASSERT_STREQ (pp_formatted_text (&pp
),
2506 "PREFIX: the quick brown fox\n"
2507 " jumps over the lazy dog\n");
2510 test_pretty_printer
pp (DIAGNOSTICS_SHOW_PREFIX_NEVER
, 0);
2511 pp_string (&pp
, "the quick brown fox");
2513 pp_string (&pp
, "jumps over the lazy dog");
2515 ASSERT_STREQ (pp_formatted_text (&pp
),
2516 "the quick brown fox\n"
2517 "jumps over the lazy dog\n");
2520 test_pretty_printer
pp (DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
, 0);
2521 pp_string (&pp
, "the quick brown fox");
2523 pp_string (&pp
, "jumps over the lazy dog");
2525 ASSERT_STREQ (pp_formatted_text (&pp
),
2526 "PREFIX: the quick brown fox\n"
2527 "PREFIX: jumps over the lazy dog\n");
2530 /* Tests of the various prefixing rules, with wrapping. */
2532 test_pretty_printer
pp (DIAGNOSTICS_SHOW_PREFIX_ONCE
, 20);
2533 pp_string (&pp
, "the quick brown fox jumps over the lazy dog");
2535 pp_string (&pp
, "able was I ere I saw elba");
2537 ASSERT_STREQ (pp_formatted_text (&pp
),
2538 "PREFIX: the quick \n"
2539 " brown fox jumps \n"
2542 " able was I ere I \n"
2546 test_pretty_printer
pp (DIAGNOSTICS_SHOW_PREFIX_NEVER
, 20);
2547 pp_string (&pp
, "the quick brown fox jumps over the lazy dog");
2549 pp_string (&pp
, "able was I ere I saw elba");
2551 ASSERT_STREQ (pp_formatted_text (&pp
),
2552 "the quick brown fox \n"
2553 "jumps over the lazy \n"
2555 "able was I ere I \n"
2559 test_pretty_printer
pp (DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
, 20);
2560 pp_string (&pp
, "the quick brown fox jumps over the lazy dog");
2562 pp_string (&pp
, "able was I ere I saw elba");
2564 ASSERT_STREQ (pp_formatted_text (&pp
),
2565 "PREFIX: the quick brown fox jumps over the lazy dog\n"
2566 "PREFIX: able was I ere I saw elba\n");
2571 /* Verify that URL-printing works as expected. */
2578 pp
.url_format
= URL_FORMAT_NONE
;
2579 pp_begin_url (&pp
, "http://example.com");
2580 pp_string (&pp
, "This is a link");
2582 ASSERT_STREQ ("This is a link",
2583 pp_formatted_text (&pp
));
2588 pp
.url_format
= URL_FORMAT_ST
;
2589 pp_begin_url (&pp
, "http://example.com");
2590 pp_string (&pp
, "This is a link");
2592 ASSERT_STREQ ("\33]8;;http://example.com\33\\This is a link\33]8;;\33\\",
2593 pp_formatted_text (&pp
));
2598 pp
.url_format
= URL_FORMAT_BEL
;
2599 pp_begin_url (&pp
, "http://example.com");
2600 pp_string (&pp
, "This is a link");
2602 ASSERT_STREQ ("\33]8;;http://example.com\aThis is a link\33]8;;\a",
2603 pp_formatted_text (&pp
));
2607 /* Verify that we gracefully reject null URLs. */
2614 pp
.url_format
= URL_FORMAT_NONE
;
2615 pp_begin_url (&pp
, nullptr);
2616 pp_string (&pp
, "This isn't a link");
2618 ASSERT_STREQ ("This isn't a link",
2619 pp_formatted_text (&pp
));
2624 pp
.url_format
= URL_FORMAT_ST
;
2625 pp_begin_url (&pp
, nullptr);
2626 pp_string (&pp
, "This isn't a link");
2628 ASSERT_STREQ ("This isn't a link",
2629 pp_formatted_text (&pp
));
2634 pp
.url_format
= URL_FORMAT_BEL
;
2635 pp_begin_url (&pp
, nullptr);
2636 pp_string (&pp
, "This isn't a link");
2638 ASSERT_STREQ ("This isn't a link",
2639 pp_formatted_text (&pp
));
2643 /* Test multibyte awareness. */
2644 static void test_utf8 ()
2647 /* Check that pp_quoted_string leaves valid UTF-8 alone. */
2650 const char *s
= "\xf0\x9f\x98\x82";
2651 pp_quoted_string (&pp
, s
);
2652 ASSERT_STREQ (pp_formatted_text (&pp
), s
);
2655 /* Check that pp_quoted_string escapes non-UTF-8 nonprintable bytes. */
2658 pp_quoted_string (&pp
, "\xf0!\x9f\x98\x82");
2659 ASSERT_STREQ (pp_formatted_text (&pp
),
2660 "\\xf0!\\x9f\\x98\\x82");
2663 /* Check that pp_character will line-wrap at the beginning of a UTF-8
2664 sequence, but not in the middle. */
2666 pretty_printer
pp (3);
2667 const char s
[] = "---\xf0\x9f\x98\x82";
2668 for (int i
= 0; i
!= sizeof (s
) - 1; ++i
)
2669 pp_character (&pp
, s
[i
]);
2671 for (int i
= 1; i
!= sizeof (s
) - 1; ++i
)
2672 pp_character (&pp
, s
[i
]);
2673 pp_character (&pp
, '-');
2674 ASSERT_STREQ (pp_formatted_text (&pp
),
2676 "\xf0\x9f\x98\x82\n"
2677 "--\xf0\x9f\x98\x82\n"
2683 /* Run all of the selftests within this file. */
2686 pretty_print_cc_tests ()
2688 test_basic_printing ();
2690 test_prefixes_and_wrapping ();
2696 } // namespace selftest
2698 #endif /* CHECKING_P */