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 "pretty-print-urlifier.h"
27 #include "diagnostic-color.h"
28 #include "diagnostic-event-id.h"
37 /* Replacement for fputs() that handles ANSI escape codes on Windows NT.
38 Contributed by: Liu Hao (lh_mouse at 126 dot com)
40 XXX: This file is compiled into libcommon.a that will be self-contained.
41 It looks like that these functions can be put nowhere else. */
44 #define WIN32_LEAN_AND_MEAN 1
47 /* Write all bytes in [s,s+n) into the specified stream.
48 Errors are ignored. */
50 write_all (HANDLE h
, const char *s
, size_t n
)
61 if (!WriteFile (h
, s
+ n
- rem
, step
, &step
, NULL
))
67 /* Find the beginning of an escape sequence.
69 1. If the sequence begins with an ESC character (0x1B) and a second
70 character X in [0x40,0x5F], returns X and stores a pointer to
71 the third character into *head.
72 2. If the sequence begins with a character X in [0x80,0x9F], returns
73 (X-0x40) and stores a pointer to the second character into *head.
74 Stores the number of ESC character(s) in *prefix_len.
75 Returns 0 if no such sequence can be found. */
77 find_esc_head (int *prefix_len
, const char **head
, const char *str
)
85 c
= (unsigned char) *r
;
91 if (escaped
&& 0x40 <= c
&& c
<= 0x5F)
98 if (0x80 <= c
&& c
<= 0x9F)
100 /* Found (case 2). */
110 /* Find the terminator of an escape sequence.
111 str should be the value stored in *head by a previous successful
112 call to find_esc_head().
113 Returns 0 if no such sequence can be found. */
115 find_esc_terminator (const char **term
, const char *str
)
122 c
= (unsigned char) *r
;
128 if (0x40 <= c
&& c
<= 0x7E)
138 /* Handle a sequence of codes. Sequences that are invalid, reserved,
139 unrecognized or unimplemented are ignored silently.
140 There isn't much we can do because of lameness of Windows consoles. */
142 eat_esc_sequence (HANDLE h
, int esc_code
,
143 const char *esc_head
, const char *esc_term
)
145 /* Numbers in an escape sequence cannot be negative, because
146 a minus sign in the middle of it would have terminated it. */
149 CONSOLE_SCREEN_BUFFER_INFO sb
;
151 /* ED and EL parameters. */
154 /* SGR parameters. */
155 WORD attrib_add
, attrib_rm
;
158 switch (MAKEWORD (esc_code
, *esc_term
))
161 Move the cursor up by n1 characters. */
162 case MAKEWORD ('[', 'A'):
163 if (esc_head
== esc_term
)
167 n1
= strtol (esc_head
, &eptr
, 10);
168 if (eptr
!= esc_term
)
172 if (GetConsoleScreenBufferInfo (h
, &sb
))
174 cr
= sb
.dwCursorPosition
;
175 /* Stop at the topmost boundary. */
180 SetConsoleCursorPosition (h
, cr
);
185 Move the cursor down by n1 characters. */
186 case MAKEWORD ('[', 'B'):
187 if (esc_head
== esc_term
)
191 n1
= strtol (esc_head
, &eptr
, 10);
192 if (eptr
!= esc_term
)
196 if (GetConsoleScreenBufferInfo (h
, &sb
))
198 cr
= sb
.dwCursorPosition
;
199 /* Stop at the bottommost boundary. */
200 if (sb
.dwSize
.Y
- cr
.Y
> n1
)
204 SetConsoleCursorPosition (h
, cr
);
209 Move the cursor right by n1 characters. */
210 case MAKEWORD ('[', 'C'):
211 if (esc_head
== esc_term
)
215 n1
= strtol (esc_head
, &eptr
, 10);
216 if (eptr
!= esc_term
)
220 if (GetConsoleScreenBufferInfo (h
, &sb
))
222 cr
= sb
.dwCursorPosition
;
223 /* Stop at the rightmost boundary. */
224 if (sb
.dwSize
.X
- cr
.X
> n1
)
228 SetConsoleCursorPosition (h
, cr
);
233 Move the cursor left by n1 characters. */
234 case MAKEWORD ('[', 'D'):
235 if (esc_head
== esc_term
)
239 n1
= strtol (esc_head
, &eptr
, 10);
240 if (eptr
!= esc_term
)
244 if (GetConsoleScreenBufferInfo (h
, &sb
))
246 cr
= sb
.dwCursorPosition
;
247 /* Stop at the leftmost boundary. */
252 SetConsoleCursorPosition (h
, cr
);
257 Move the cursor to the beginning of the n1-th line downwards. */
258 case MAKEWORD ('[', 'E'):
259 if (esc_head
== esc_term
)
263 n1
= strtol (esc_head
, &eptr
, 10);
264 if (eptr
!= esc_term
)
268 if (GetConsoleScreenBufferInfo (h
, &sb
))
270 cr
= sb
.dwCursorPosition
;
272 /* Stop at the bottommost boundary. */
273 if (sb
.dwSize
.Y
- cr
.Y
> n1
)
277 SetConsoleCursorPosition (h
, cr
);
282 Move the cursor to the beginning of the n1-th line upwards. */
283 case MAKEWORD ('[', 'F'):
284 if (esc_head
== esc_term
)
288 n1
= strtol (esc_head
, &eptr
, 10);
289 if (eptr
!= esc_term
)
293 if (GetConsoleScreenBufferInfo (h
, &sb
))
295 cr
= sb
.dwCursorPosition
;
297 /* Stop at the topmost boundary. */
302 SetConsoleCursorPosition (h
, cr
);
307 Move the cursor to the (1-based) n1-th column. */
308 case MAKEWORD ('[', 'G'):
309 if (esc_head
== esc_term
)
313 n1
= strtol (esc_head
, &eptr
, 10);
314 if (eptr
!= esc_term
)
318 if (GetConsoleScreenBufferInfo (h
, &sb
))
320 cr
= sb
.dwCursorPosition
;
322 /* Stop at the leftmost or rightmost boundary. */
325 else if (n1
> sb
.dwSize
.X
)
329 SetConsoleCursorPosition (h
, cr
);
333 /* ESC [ n1 ';' n2 'H'
335 Move the cursor to the (1-based) n1-th row and
336 (also 1-based) n2-th column. */
337 case MAKEWORD ('[', 'H'):
338 case MAKEWORD ('[', 'f'):
339 if (esc_head
== esc_term
)
341 /* Both parameters are omitted and set to 1 by default. */
345 else if (!(delim
= (char *) memchr (esc_head
, ';',
346 esc_term
- esc_head
)))
348 /* Only the first parameter is given. The second one is
349 set to 1 by default. */
350 n1
= strtol (esc_head
, &eptr
, 10);
351 if (eptr
!= esc_term
)
357 /* Both parameters are given. The first one shall be
358 terminated by the semicolon. */
359 n1
= strtol (esc_head
, &eptr
, 10);
362 n2
= strtol (delim
+ 1, &eptr
, 10);
363 if (eptr
!= esc_term
)
367 if (GetConsoleScreenBufferInfo (h
, &sb
))
369 cr
= sb
.dwCursorPosition
;
372 /* The cursor position shall be relative to the view coord of
373 the console window, which is usually smaller than the actual
374 buffer. FWIW, the 'appropriate' solution will be shrinking
375 the buffer to match the size of the console window,
376 destroying scrollback in the process. */
377 n1
+= sb
.srWindow
.Top
;
378 n2
+= sb
.srWindow
.Left
;
379 /* Stop at the topmost or bottommost boundary. */
382 else if (n1
> sb
.dwSize
.Y
)
386 /* Stop at the leftmost or rightmost boundary. */
389 else if (n2
> sb
.dwSize
.X
)
393 SetConsoleCursorPosition (h
, cr
);
399 case MAKEWORD ('[', 'J'):
400 if (esc_head
== esc_term
)
401 /* This is one of the very few codes whose parameters have
402 a default value of zero. */
406 n1
= strtol (esc_head
, &eptr
, 10);
407 if (eptr
!= esc_term
)
411 if (GetConsoleScreenBufferInfo (h
, &sb
))
413 /* The cursor is not necessarily in the console window, which
414 makes the behavior of this code harder to define. */
418 /* If the cursor is in or above the window, erase from
419 it to the bottom of the window; otherwise, do nothing. */
420 cr
= sb
.dwCursorPosition
;
421 cnt
= sb
.dwSize
.X
- sb
.dwCursorPosition
.X
;
422 rows
= sb
.srWindow
.Bottom
- sb
.dwCursorPosition
.Y
;
425 /* If the cursor is in or under the window, erase from
426 it to the top of the window; otherwise, do nothing. */
428 cr
.Y
= sb
.srWindow
.Top
;
429 cnt
= sb
.dwCursorPosition
.X
+ 1;
430 rows
= sb
.dwCursorPosition
.Y
- sb
.srWindow
.Top
;
433 /* Erase the entire window. */
434 cr
.X
= sb
.srWindow
.Left
;
435 cr
.Y
= sb
.srWindow
.Top
;
437 rows
= sb
.srWindow
.Bottom
- sb
.srWindow
.Top
+ 1;
440 /* Erase the entire buffer. */
449 cnt
+= rows
* sb
.dwSize
.X
;
450 FillConsoleOutputCharacterW (h
, L
' ', cnt
, cr
, &step
);
451 FillConsoleOutputAttribute (h
, sb
.wAttributes
, cnt
, cr
, &step
);
457 case MAKEWORD ('[', 'K'):
458 if (esc_head
== esc_term
)
459 /* This is one of the very few codes whose parameters have
460 a default value of zero. */
464 n1
= strtol (esc_head
, &eptr
, 10);
465 if (eptr
!= esc_term
)
469 if (GetConsoleScreenBufferInfo (h
, &sb
))
474 /* Erase from the cursor to the end. */
475 cr
= sb
.dwCursorPosition
;
476 cnt
= sb
.dwSize
.X
- sb
.dwCursorPosition
.X
;
479 /* Erase from the cursor to the beginning. */
480 cr
= sb
.dwCursorPosition
;
482 cnt
= sb
.dwCursorPosition
.X
+ 1;
485 /* Erase the entire line. */
486 cr
= sb
.dwCursorPosition
;
491 FillConsoleOutputCharacterW (h
, L
' ', cnt
, cr
, &step
);
492 FillConsoleOutputAttribute (h
, sb
.wAttributes
, cnt
, cr
, &step
);
496 /* ESC [ n1 ';' n2 'm'
497 Set SGR parameters. Zero or more parameters will follow. */
498 case MAKEWORD ('[', 'm'):
501 if (esc_head
== esc_term
)
503 /* When no parameter is given, reset the console. */
504 attrib_add
|= (FOREGROUND_RED
| FOREGROUND_GREEN
506 attrib_rm
= -1; /* Removes everything. */
512 /* Parse a parameter. */
513 n1
= strtol (param
, &eptr
, 10);
514 if (*eptr
!= ';' && eptr
!= esc_term
)
521 attrib_add
|= (FOREGROUND_RED
| FOREGROUND_GREEN
523 attrib_rm
= -1; /* Removes everything. */
527 attrib_add
|= FOREGROUND_INTENSITY
;
531 attrib_add
|= COMMON_LVB_UNDERSCORE
;
535 /* XXX: It is not BLINKING at all! */
536 attrib_add
|= BACKGROUND_INTENSITY
;
540 attrib_add
|= COMMON_LVB_REVERSE_VIDEO
;
544 attrib_add
&= ~FOREGROUND_INTENSITY
;
545 attrib_rm
|= FOREGROUND_INTENSITY
;
549 attrib_add
&= ~COMMON_LVB_UNDERSCORE
;
550 attrib_rm
|= COMMON_LVB_UNDERSCORE
;
554 /* XXX: It is not BLINKING at all! */
555 attrib_add
&= ~BACKGROUND_INTENSITY
;
556 attrib_rm
|= BACKGROUND_INTENSITY
;
560 attrib_add
&= ~COMMON_LVB_REVERSE_VIDEO
;
561 attrib_rm
|= COMMON_LVB_REVERSE_VIDEO
;
571 /* Foreground color. */
572 attrib_add
&= ~(FOREGROUND_RED
| FOREGROUND_GREEN
576 attrib_add
|= FOREGROUND_RED
;
578 attrib_add
|= FOREGROUND_GREEN
;
580 attrib_add
|= FOREGROUND_BLUE
;
581 attrib_rm
|= (FOREGROUND_RED
| FOREGROUND_GREEN
585 /* Reserved for extended foreground color.
586 Don't know how to handle parameters remaining.
590 /* Reset foreground color. */
592 attrib_add
|= (FOREGROUND_RED
| FOREGROUND_GREEN
594 attrib_rm
|= (FOREGROUND_RED
| FOREGROUND_GREEN
605 /* Background color. */
606 attrib_add
&= ~(BACKGROUND_RED
| BACKGROUND_GREEN
610 attrib_add
|= BACKGROUND_RED
;
612 attrib_add
|= BACKGROUND_GREEN
;
614 attrib_add
|= BACKGROUND_BLUE
;
615 attrib_rm
|= (BACKGROUND_RED
| BACKGROUND_GREEN
619 /* Reserved for extended background color.
620 Don't know how to handle parameters remaining.
624 /* Reset background color. */
626 attrib_add
&= ~(BACKGROUND_RED
| BACKGROUND_GREEN
628 attrib_rm
|= (BACKGROUND_RED
| BACKGROUND_GREEN
633 /* Prepare the next parameter. */
636 while (param
!= esc_term
);
639 /* 0xFFFF removes everything. If it is not the case,
640 care must be taken to preserve old attributes. */
641 if (attrib_rm
!= 0xFFFF && GetConsoleScreenBufferInfo (h
, &sb
))
643 attrib_add
|= sb
.wAttributes
& ~attrib_rm
;
645 if (attrib_add
& COMMON_LVB_REVERSE_VIDEO
)
647 /* COMMON_LVB_REVERSE_VIDEO is only effective for DBCS.
648 * Swap foreground and background colors by hand.
650 attrib_add
= (attrib_add
& 0xFF00)
651 | ((attrib_add
& 0x00F0) >> 4)
652 | ((attrib_add
& 0x000F) << 4);
653 attrib_add
&= ~COMMON_LVB_REVERSE_VIDEO
;
655 SetConsoleTextAttribute (h
, attrib_add
);
661 mingw_ansi_fputs (const char *str
, FILE *fp
)
663 const char *read
= str
;
666 int esc_code
, prefix_len
;
667 const char *esc_head
, *esc_term
;
669 h
= (HANDLE
) _get_osfhandle (_fileno (fp
));
670 if (h
== INVALID_HANDLE_VALUE
)
673 /* Don't mess up stdio functions with Windows APIs. */
676 if (GetConsoleMode (h
, &mode
))
677 /* If it is a console, translate ANSI escape codes as needed. */
680 if ((esc_code
= find_esc_head (&prefix_len
, &esc_head
, read
)) == 0)
682 /* Write all remaining characters, then exit. */
683 write_all (h
, read
, strlen (read
));
686 if (find_esc_terminator (&esc_term
, esc_head
) == 0)
687 /* Ignore incomplete escape sequences at the moment.
688 FIXME: The escape state shall be cached for further calls
691 write_all (h
, read
, esc_head
- prefix_len
- read
);
692 eat_esc_sequence (h
, esc_code
, esc_head
, esc_term
);
696 /* If it is not a console, write everything as-is. */
697 write_all (h
, read
, strlen (read
));
702 #endif /* __MINGW32__ */
705 decode_utf8_char (const unsigned char *, size_t len
, unsigned int *);
706 static void pp_quoted_string (pretty_printer
*, const char *, size_t = -1);
708 /* Overwrite the given location/range within this text_info's rich_location.
709 For use e.g. when implementing "+" in client format decoders. */
712 text_info::set_location (unsigned int idx
, location_t loc
,
713 enum range_display_kind range_display_kind
)
715 gcc_checking_assert (m_richloc
);
716 m_richloc
->set_range (idx
, loc
, range_display_kind
);
720 text_info::get_location (unsigned int index_of_location
) const
722 gcc_checking_assert (m_richloc
);
724 if (index_of_location
== 0)
725 return m_richloc
->get_loc ();
727 return UNKNOWN_LOCATION
;
730 // Default construct an output buffer.
732 output_buffer::output_buffer ()
733 : formatted_obstack (),
735 obstack (&formatted_obstack
),
742 obstack_init (&formatted_obstack
);
743 obstack_init (&chunk_obstack
);
746 // Release resources owned by an output buffer at the end of lifetime.
748 output_buffer::~output_buffer ()
750 obstack_free (&chunk_obstack
, NULL
);
751 obstack_free (&formatted_obstack
, NULL
);
755 /* Format an integer given by va_arg (ARG, type-specifier T) where
756 type-specifier is a precision modifier as indicated by PREC. F is
757 a string used to construct the appropriate format-specifier. */
758 #define pp_integer_with_precision(PP, ARG, PREC, T, F) \
763 pp_scalar (PP, "%" F, va_arg (ARG, T)); \
767 pp_scalar (PP, "%l" F, va_arg (ARG, long T)); \
771 pp_scalar (PP, "%" HOST_LONG_LONG_FORMAT F, va_arg (ARG, long long T)); \
780 /* Subroutine of pp_set_maximum_length. Set up PRETTY-PRINTER's
781 internal maximum characters per line. */
783 pp_set_real_maximum_length (pretty_printer
*pp
)
785 /* If we're told not to wrap lines then do the obvious thing. In case
786 we'll emit prefix only once per message, it is appropriate
787 not to increase unnecessarily the line-length cut-off. */
788 if (!pp_is_wrapping_line (pp
)
789 || pp_prefixing_rule (pp
) == DIAGNOSTICS_SHOW_PREFIX_ONCE
790 || pp_prefixing_rule (pp
) == DIAGNOSTICS_SHOW_PREFIX_NEVER
)
791 pp
->maximum_length
= pp_line_cutoff (pp
);
794 int prefix_length
= pp
->prefix
? strlen (pp
->prefix
) : 0;
795 /* If the prefix is ridiculously too long, output at least
797 if (pp_line_cutoff (pp
) - prefix_length
< 32)
798 pp
->maximum_length
= pp_line_cutoff (pp
) + 32;
800 pp
->maximum_length
= pp_line_cutoff (pp
);
804 /* Clear PRETTY-PRINTER's output state. */
806 pp_clear_state (pretty_printer
*pp
)
808 pp
->emitted_prefix
= false;
809 pp_indentation (pp
) = 0;
812 /* Print X to PP in decimal. */
813 template<unsigned int N
, typename T
>
815 pp_wide_integer (pretty_printer
*pp
, const poly_int
<N
, T
> &x
)
817 if (x
.is_constant ())
818 pp_wide_integer (pp
, x
.coeffs
[0]);
821 pp_left_bracket (pp
);
822 for (unsigned int i
= 0; i
< N
; ++i
)
826 pp_wide_integer (pp
, x
.coeffs
[i
]);
828 pp_right_bracket (pp
);
832 template void pp_wide_integer (pretty_printer
*, const poly_uint16
&);
833 template void pp_wide_integer (pretty_printer
*, const poly_int64
&);
834 template void pp_wide_integer (pretty_printer
*, const poly_uint64
&);
836 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream. */
838 pp_write_text_to_stream (pretty_printer
*pp
)
840 const char *text
= pp_formatted_text (pp
);
842 mingw_ansi_fputs (text
, pp_buffer (pp
)->stream
);
844 fputs (text
, pp_buffer (pp
)->stream
);
846 pp_clear_output_area (pp
);
849 /* As pp_write_text_to_stream, but for GraphViz label output.
851 Flush the formatted text of pretty-printer PP onto the attached stream.
852 Replace characters in PPF that have special meaning in a GraphViz .dot
855 This routine is not very fast, but it doesn't have to be as this is only
856 be used by routines dumping intermediate representations in graph form. */
859 pp_write_text_as_dot_label_to_stream (pretty_printer
*pp
, bool for_record
)
861 const char *text
= pp_formatted_text (pp
);
862 const char *p
= text
;
863 FILE *fp
= pp_buffer (pp
)->stream
;
870 /* Print newlines as a left-aligned newline. */
876 /* The following characters are only special for record-shape nodes. */
883 escape_char
= for_record
;
886 /* The following characters always have to be escaped
887 for use in labels. */
889 /* There is a bug in some (f.i. 2.36.0) versions of graphiz
890 ( http://www.graphviz.org/mantisbt/view.php?id=2524 ) related to
891 backslash as last char in label. Let's avoid triggering it. */
892 gcc_assert (*(p
+ 1) != '\0');
909 pp_clear_output_area (pp
);
912 /* As pp_write_text_to_stream, but for GraphViz HTML-like strings.
914 Flush the formatted text of pretty-printer PP onto the attached stream,
915 escaping these characters
917 using XML escape sequences.
919 http://www.graphviz.org/doc/info/lang.html#html states:
920 special XML escape sequences for ", &, <, and > may be necessary in
921 order to embed these characters in attribute values or raw text
922 This doesn't list "'" (which would normally be escaped in XML
923 as "'" or in HTML as "'");.
925 Experiments show that escaping "'" doesn't seem to be necessary. */
928 pp_write_text_as_html_like_dot_to_stream (pretty_printer
*pp
)
930 const char *text
= pp_formatted_text (pp
);
931 const char *p
= text
;
932 FILE *fp
= pp_buffer (pp
)->stream
;
939 fputs (""", fp
);
957 pp_clear_output_area (pp
);
960 /* Wrap a text delimited by START and END into PRETTY-PRINTER. */
962 pp_wrap_text (pretty_printer
*pp
, const char *start
, const char *end
)
964 bool wrapping_line
= pp_is_wrapping_line (pp
);
968 /* Dump anything bordered by whitespaces. */
970 const char *p
= start
;
971 while (p
!= end
&& !ISBLANK (*p
) && *p
!= '\n')
974 && p
- start
>= pp_remaining_character_count_for_line (pp
))
976 pp_append_text (pp
, start
, p
);
980 if (start
!= end
&& ISBLANK (*start
))
985 if (start
!= end
&& *start
== '\n')
993 /* Same as pp_wrap_text but wrap text only when in line-wrapping mode. */
995 pp_maybe_wrap_text (pretty_printer
*pp
, const char *start
, const char *end
)
997 if (pp_is_wrapping_line (pp
))
998 pp_wrap_text (pp
, start
, end
);
1000 pp_append_text (pp
, start
, end
);
1003 /* Append to the output area of PRETTY-PRINTER a string specified by its
1004 STARTing character and LENGTH. */
1006 pp_append_r (pretty_printer
*pp
, const char *start
, int length
)
1008 output_buffer_append_r (pp_buffer (pp
), start
, length
);
1011 /* Insert enough spaces into the output area of PRETTY-PRINTER to bring
1012 the column position to the current indentation level, assuming that a
1013 newline has just been written to the buffer. */
1015 pp_indent (pretty_printer
*pp
)
1017 int n
= pp_indentation (pp
);
1020 for (i
= 0; i
< n
; ++i
)
1024 static const char *get_end_url_string (pretty_printer
*);
1026 /* Append STR to OSTACK, without a null-terminator. */
1029 obstack_append_string (obstack
*ostack
, const char *str
)
1031 obstack_grow (ostack
, str
, strlen (str
));
1034 /* Given quoted text starting at QUOTED_TEXT_START_IDX within PP's buffer,
1035 potentially use URLIFIER (if non-null) to see if there's a URL for the
1038 If so, replace the quoted part of the text in the buffer with a URLified
1039 version of the text, using PP's settings.
1041 For example, given this is the buffer:
1042 "this is a test `hello world"
1043 .................^~~~~~~~~~~
1044 with the quoted text starting at the 'h' of "hello world", the buffer
1046 "this is a test `BEGIN_URL(URL)hello worldEND(URL)"
1047 .................^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1048 .................-----------replacement-----------
1052 urlify_quoted_string (pretty_printer
*pp
,
1053 const urlifier
*urlifier
,
1054 size_t quoted_text_start_idx
)
1056 if (pp
->url_format
== URL_FORMAT_NONE
)
1061 output_buffer
* const buffer
= pp_buffer (pp
);
1063 /* Get end of quoted string. */
1064 const size_t close_quote_idx
1065 = obstack_object_size (&buffer
->chunk_obstack
);
1066 gcc_assert (close_quote_idx
>= quoted_text_start_idx
);
1067 if (close_quote_idx
== quoted_text_start_idx
)
1068 /* Empty quoted string; do nothing. */
1070 const size_t len
= close_quote_idx
- quoted_text_start_idx
;
1071 const char *start
= (buffer
->chunk_obstack
.object_base
1072 + quoted_text_start_idx
);
1073 char *url
= urlifier
->get_url_for_quoted_text (start
, len
);
1075 /* No URL for this quoted text; do nothing. */
1078 /* Stash a copy of the quoted text. */
1079 char *text
= xstrndup (start
, len
);
1081 /* Replace quoted text... */
1082 buffer
->chunk_obstack
.next_free
-= len
;
1084 /* ...with URLified version of the text. */
1086 switch (pp
->url_format
)
1089 case URL_FORMAT_NONE
:
1092 obstack_append_string (&buffer
->chunk_obstack
,
1094 obstack_append_string (&buffer
->chunk_obstack
, url
);
1095 obstack_append_string (&buffer
->chunk_obstack
,
1098 case URL_FORMAT_BEL
:
1099 obstack_append_string (&buffer
->chunk_obstack
,
1101 obstack_append_string (&buffer
->chunk_obstack
, url
);
1102 obstack_append_string (&buffer
->chunk_obstack
,
1106 /* Add the text back. */
1107 obstack_append_string (&buffer
->chunk_obstack
, text
);
1109 obstack_append_string (&buffer
->chunk_obstack
,
1110 get_end_url_string (pp
));
1115 /* The following format specifiers are recognized as being client independent:
1116 %d, %i: (signed) integer in base ten.
1117 %u: unsigned integer in base ten.
1118 %o: unsigned integer in base eight.
1119 %x: unsigned integer in base sixteen.
1120 %ld, %li, %lo, %lu, %lx: long versions of the above.
1121 %lld, %lli, %llo, %llu, %llx: long long versions.
1122 %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
1126 %p: pointer (printed in a host-dependent manner).
1127 %r: if pp_show_color(pp), switch to color identified by const char *.
1128 %R: if pp_show_color(pp), reset color.
1129 %m: strerror(text->err_no) - does not consume a value from args_ptr.
1133 %{: URL start. Consumes a const char * argument for the URL.
1134 %}: URL end. Does not consume any arguments.
1135 %': apostrophe (should only be used in untranslated messages;
1136 translations should use appropriate punctuation directly).
1137 %@: diagnostic_event_id_ptr, for which event_id->known_p () must be true.
1138 %.*s: a substring the length of which is specified by an argument
1140 %Ns: likewise, but length specified as constant in the format string.
1141 Flag 'q': quote formatted text (must come immediately after '%').
1142 %Z: Requires two arguments - array of int, and len. Prints elements
1145 Arguments can be used sequentially, or through %N$ resp. *N$
1146 notation Nth argument after the format string. If %N$ / *N$
1147 notation is used, it must be used for all arguments, except %m, %%,
1148 %<, %>, %} and %', which may not have a number, as they do not consume
1149 an argument. When %M$.*N$s is used, M must be N + 1. (This may
1150 also be written %M$.*s, provided N is not otherwise used.) The
1151 format string must have conversion specifiers with argument numbers
1152 1 up to highest argument; each argument may only be used once.
1153 A format string can have at most 30 arguments. */
1155 /* Formatting phases 1 and 2: render TEXT->format_spec plus
1156 text->m_args_ptr into a series of chunks in pp_buffer (PP)->args[].
1157 Phase 3 is in pp_output_formatted_text.
1159 If URLIFIER is non-NULL, then use it to add URLs for quoted
1160 strings, so that e.g.
1161 "before %<quoted%> after"
1162 with a URLIFIER that has a URL for "quoted" might be emitted as:
1163 "before `BEGIN_URL(http://example.com)quotedEND_URL' after"
1164 This only works for message fragments that are:
1165 - quoted entirely in phase 1 (e.g. "%<this is quoted%>"), or
1166 - quoted entirely in phase 2 (e.g. "%qs"),
1167 but *not* in strings that use a mixture of both phases
1168 (e.g. "%<this is a mixture: %s %>"). */
1171 pp_format (pretty_printer
*pp
,
1173 const urlifier
*urlifier
)
1175 output_buffer
* const buffer
= pp_buffer (pp
);
1178 struct chunk_info
*new_chunk_array
;
1180 unsigned int curarg
= 0, chunk
= 0, argno
;
1181 pp_wrapping_mode_t old_wrapping_mode
;
1182 bool any_unnumbered
= false, any_numbered
= false;
1183 const char **formatters
[PP_NL_ARGMAX
];
1185 /* Keep track of location of last "%", if any. */
1186 size_t quoted_text_start_idx
= 0;
1188 /* Allocate a new chunk structure. */
1189 new_chunk_array
= XOBNEW (&buffer
->chunk_obstack
, struct chunk_info
);
1190 new_chunk_array
->prev
= buffer
->cur_chunk_array
;
1191 buffer
->cur_chunk_array
= new_chunk_array
;
1192 args
= new_chunk_array
->args
;
1194 /* Formatting phase 1: split up TEXT->format_spec into chunks in
1195 pp_buffer (PP)->args[]. Even-numbered chunks are to be output
1196 verbatim, odd-numbered chunks are format specifiers.
1197 %m, %%, %<, %>, %} and %' are replaced with the appropriate text at
1200 memset (formatters
, 0, sizeof formatters
);
1202 for (p
= text
->m_format_spec
; *p
; )
1204 while (*p
!= '\0' && *p
!= '%')
1206 obstack_1grow (&buffer
->chunk_obstack
, *p
);
1219 obstack_1grow (&buffer
->chunk_obstack
, '%');
1225 obstack_grow (&buffer
->chunk_obstack
,
1226 open_quote
, strlen (open_quote
));
1227 const char *colorstr
1228 = colorize_start (pp_show_color (pp
), "quote");
1229 obstack_grow (&buffer
->chunk_obstack
, colorstr
, strlen (colorstr
));
1232 /* Stash offset of start of quoted string. */
1233 quoted_text_start_idx
1234 = obstack_object_size (&buffer
->chunk_obstack
);
1241 if (quoted_text_start_idx
)
1243 urlify_quoted_string (pp
, urlifier
, quoted_text_start_idx
);
1244 quoted_text_start_idx
= 0;
1246 const char *colorstr
= colorize_stop (pp_show_color (pp
));
1247 obstack_grow (&buffer
->chunk_obstack
, colorstr
, strlen (colorstr
));
1251 obstack_grow (&buffer
->chunk_obstack
,
1252 close_quote
, strlen (close_quote
));
1258 const char *endurlstr
= get_end_url_string (pp
);
1259 obstack_grow (&buffer
->chunk_obstack
, endurlstr
,
1260 strlen (endurlstr
));
1267 const char *colorstr
= colorize_stop (pp_show_color (pp
));
1268 obstack_grow (&buffer
->chunk_obstack
, colorstr
,
1276 const char *errstr
= xstrerror (text
->m_err_no
);
1277 obstack_grow (&buffer
->chunk_obstack
, errstr
, strlen (errstr
));
1283 /* Handled in phase 2. Terminate the plain chunk here. */
1284 obstack_1grow (&buffer
->chunk_obstack
, '\0');
1285 gcc_assert (chunk
< PP_NL_ARGMAX
* 2);
1286 args
[chunk
++] = XOBFINISH (&buffer
->chunk_obstack
, const char *);
1287 /* We can't yet handle urlifying quoted strings that use
1288 a combination of phase 1 and phase 2 e.g.
1289 "did you mean %<-%s%>".
1290 Stop any phase 1 quoted text if there are going to be any
1291 phase 2 quoted chunks. */
1292 quoted_text_start_idx
= 0;
1299 argno
= strtoul (p
, &end
, 10) - 1;
1301 gcc_assert (*p
== '$');
1304 any_numbered
= true;
1305 gcc_assert (!any_unnumbered
);
1310 any_unnumbered
= true;
1311 gcc_assert (!any_numbered
);
1313 gcc_assert (argno
< PP_NL_ARGMAX
);
1314 gcc_assert (!formatters
[argno
]);
1315 formatters
[argno
] = &args
[chunk
];
1318 obstack_1grow (&buffer
->chunk_obstack
, *p
);
1321 while (strchr ("qwl+#", p
[-1]));
1325 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
1326 (where M == N + 1). */
1331 obstack_1grow (&buffer
->chunk_obstack
, *p
);
1334 while (ISDIGIT (p
[-1]));
1335 gcc_assert (p
[-1] == 's');
1339 gcc_assert (*p
== '*');
1340 obstack_1grow (&buffer
->chunk_obstack
, '*');
1346 unsigned int argno2
= strtoul (p
, &end
, 10) - 1;
1348 gcc_assert (argno2
== argno
- 1);
1349 gcc_assert (!any_unnumbered
);
1350 gcc_assert (*p
== '$');
1353 formatters
[argno2
] = formatters
[argno
];
1357 gcc_assert (!any_numbered
);
1358 formatters
[argno
+1] = formatters
[argno
];
1361 gcc_assert (*p
== 's');
1362 obstack_1grow (&buffer
->chunk_obstack
, 's');
1369 obstack_1grow (&buffer
->chunk_obstack
, '\0');
1370 gcc_assert (chunk
< PP_NL_ARGMAX
* 2);
1371 args
[chunk
++] = XOBFINISH (&buffer
->chunk_obstack
, const char *);
1374 obstack_1grow (&buffer
->chunk_obstack
, '\0');
1375 gcc_assert (chunk
< PP_NL_ARGMAX
* 2);
1376 args
[chunk
++] = XOBFINISH (&buffer
->chunk_obstack
, const char *);
1379 /* Set output to the argument obstack, and switch line-wrapping and
1381 buffer
->obstack
= &buffer
->chunk_obstack
;
1382 const int old_line_length
= buffer
->line_length
;
1383 old_wrapping_mode
= pp_set_verbatim_wrapping (pp
);
1385 /* Second phase. Replace each formatter with the formatted text it
1388 for (argno
= 0; formatters
[argno
]; argno
++)
1395 quoted_text_start_idx
= 0;
1397 /* We do not attempt to enforce any ordering on the modifier
1400 for (p
= *formatters
[argno
];; p
++)
1405 gcc_assert (!quote
);
1425 /* We don't support precision beyond that of "long long". */
1426 gcc_assert (precision
< 2);
1433 gcc_assert (!wide
|| precision
== 0);
1437 pp_begin_quote (pp
, pp_show_color (pp
));
1438 quoted_text_start_idx
1439 = obstack_object_size (&buffer
->chunk_obstack
);
1445 pp_string (pp
, colorize_start (pp_show_color (pp
),
1446 va_arg (*text
->m_args_ptr
,
1452 /* When quoting, print alphanumeric, punctuation, and the space
1453 character unchanged, and all others in hexadecimal with the
1454 "\x" prefix. Otherwise print them all unchanged. */
1455 int chr
= va_arg (*text
->m_args_ptr
, int);
1456 if (ISPRINT (chr
) || !quote
)
1457 pp_character (pp
, chr
);
1460 const char str
[2] = { chr
, '\0' };
1461 pp_quoted_string (pp
, str
, 1);
1469 pp_wide_integer (pp
, va_arg (*text
->m_args_ptr
, HOST_WIDE_INT
));
1471 pp_integer_with_precision
1472 (pp
, *text
->m_args_ptr
, precision
, int, "d");
1477 pp_scalar (pp
, "%" HOST_WIDE_INT_PRINT
"o",
1478 va_arg (*text
->m_args_ptr
, unsigned HOST_WIDE_INT
));
1480 pp_integer_with_precision
1481 (pp
, *text
->m_args_ptr
, precision
, unsigned, "o");
1486 pp_quoted_string (pp
, va_arg (*text
->m_args_ptr
, const char *));
1488 pp_string (pp
, va_arg (*text
->m_args_ptr
, const char *));
1492 pp_pointer (pp
, va_arg (*text
->m_args_ptr
, void *));
1497 pp_scalar (pp
, HOST_WIDE_INT_PRINT_UNSIGNED
,
1498 va_arg (*text
->m_args_ptr
, unsigned HOST_WIDE_INT
));
1500 pp_integer_with_precision
1501 (pp
, *text
->m_args_ptr
, precision
, unsigned, "u");
1505 pp_double (pp
, va_arg (*text
->m_args_ptr
, double));
1510 int *v
= va_arg (*text
->m_args_ptr
, int *);
1511 unsigned len
= va_arg (*text
->m_args_ptr
, unsigned);
1513 for (unsigned i
= 0; i
< len
; ++i
)
1515 pp_scalar (pp
, "%i", v
[i
]);
1527 pp_scalar (pp
, HOST_WIDE_INT_PRINT_HEX
,
1528 va_arg (*text
->m_args_ptr
, unsigned HOST_WIDE_INT
));
1530 pp_integer_with_precision
1531 (pp
, *text
->m_args_ptr
, precision
, unsigned, "x");
1539 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
1540 (where M == N + 1). The format string should be verified
1541 already from the first phase. */
1546 n
= strtoul (p
, &end
, 10);
1548 gcc_assert (*p
== 's');
1552 gcc_assert (*p
== '*');
1554 gcc_assert (*p
== 's');
1555 n
= va_arg (*text
->m_args_ptr
, int);
1557 /* This consumes a second entry in the formatters array. */
1558 gcc_assert (formatters
[argno
] == formatters
[argno
+1]);
1562 s
= va_arg (*text
->m_args_ptr
, const char *);
1564 /* Append the lesser of precision and strlen (s) characters
1565 from the array (which need not be a nul-terminated string).
1566 Negative precision is treated as if it were omitted. */
1567 size_t len
= n
< 0 ? strlen (s
) : strnlen (s
, n
);
1569 pp_append_text (pp
, s
, s
+ len
);
1575 /* diagnostic_event_id_t *. */
1576 diagnostic_event_id_ptr event_id
1577 = va_arg (*text
->m_args_ptr
, diagnostic_event_id_ptr
);
1578 gcc_assert (event_id
->known_p ());
1580 pp_string (pp
, colorize_start (pp_show_color (pp
), "path"));
1581 pp_character (pp
, '(');
1582 pp_decimal_int (pp
, event_id
->one_based ());
1583 pp_character (pp
, ')');
1584 pp_string (pp
, colorize_stop (pp_show_color (pp
)));
1589 pp_begin_url (pp
, va_arg (*text
->m_args_ptr
, const char *));
1596 /* Call the format decoder.
1597 Pass the address of "quote" so that format decoders can
1598 potentially disable printing of the closing quote
1599 (e.g. when printing "'TYPEDEF' aka 'TYPE'" in the C family
1601 gcc_assert (pp_format_decoder (pp
));
1602 ok
= pp_format_decoder (pp
) (pp
, text
, p
,
1603 precision
, wide
, plus
, hash
, "e
,
1611 if (quoted_text_start_idx
)
1613 urlify_quoted_string (pp
, urlifier
, quoted_text_start_idx
);
1614 quoted_text_start_idx
= 0;
1616 pp_end_quote (pp
, pp_show_color (pp
));
1619 obstack_1grow (&buffer
->chunk_obstack
, '\0');
1620 *formatters
[argno
] = XOBFINISH (&buffer
->chunk_obstack
, const char *);
1624 for (; argno
< PP_NL_ARGMAX
; argno
++)
1625 gcc_assert (!formatters
[argno
]);
1627 /* If the client supplied a postprocessing object, call its "handle"
1629 if (pp
->m_format_postprocessor
)
1630 pp
->m_format_postprocessor
->handle (pp
);
1632 /* Revert to normal obstack and wrapping mode. */
1633 buffer
->obstack
= &buffer
->formatted_obstack
;
1634 buffer
->line_length
= old_line_length
;
1635 pp_wrapping_mode (pp
) = old_wrapping_mode
;
1636 pp_clear_state (pp
);
1639 /* Format of a message pointed to by TEXT. */
1641 pp_output_formatted_text (pretty_printer
*pp
)
1644 output_buffer
* const buffer
= pp_buffer (pp
);
1645 struct chunk_info
*chunk_array
= buffer
->cur_chunk_array
;
1646 const char **args
= chunk_array
->args
;
1648 gcc_assert (buffer
->obstack
== &buffer
->formatted_obstack
);
1650 /* This is a third phase, first 2 phases done in pp_format_args.
1651 Now we actually print it. */
1652 for (chunk
= 0; args
[chunk
]; chunk
++)
1653 pp_string (pp
, args
[chunk
]);
1655 /* Deallocate the chunk structure and everything after it (i.e. the
1656 associated series of formatted strings). */
1657 buffer
->cur_chunk_array
= chunk_array
->prev
;
1658 obstack_free (&buffer
->chunk_obstack
, chunk_array
);
1661 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
1662 settings needed by BUFFER for a verbatim formatting. */
1664 pp_format_verbatim (pretty_printer
*pp
, text_info
*text
)
1666 /* Set verbatim mode. */
1667 pp_wrapping_mode_t oldmode
= pp_set_verbatim_wrapping (pp
);
1669 /* Do the actual formatting. */
1670 pp_format (pp
, text
);
1671 pp_output_formatted_text (pp
);
1673 /* Restore previous settings. */
1674 pp_wrapping_mode (pp
) = oldmode
;
1677 /* Flush the content of BUFFER onto the attached stream. This
1678 function does nothing unless pp->output_buffer->flush_p. */
1680 pp_flush (pretty_printer
*pp
)
1682 pp_clear_state (pp
);
1683 if (!pp
->buffer
->flush_p
)
1685 pp_write_text_to_stream (pp
);
1686 fflush (pp_buffer (pp
)->stream
);
1689 /* Flush the content of BUFFER onto the attached stream independently
1690 of the value of pp->output_buffer->flush_p. */
1692 pp_really_flush (pretty_printer
*pp
)
1694 pp_clear_state (pp
);
1695 pp_write_text_to_stream (pp
);
1696 fflush (pp_buffer (pp
)->stream
);
1699 /* Sets the number of maximum characters per line PRETTY-PRINTER can
1700 output in line-wrapping mode. A LENGTH value 0 suppresses
1703 pp_set_line_maximum_length (pretty_printer
*pp
, int length
)
1705 pp_line_cutoff (pp
) = length
;
1706 pp_set_real_maximum_length (pp
);
1709 /* Clear PRETTY-PRINTER output area text info. */
1711 pp_clear_output_area (pretty_printer
*pp
)
1713 obstack_free (pp_buffer (pp
)->obstack
,
1714 obstack_base (pp_buffer (pp
)->obstack
));
1715 pp_buffer (pp
)->line_length
= 0;
1718 /* Set PREFIX for PRETTY-PRINTER, taking ownership of PREFIX, which
1719 will eventually be free-ed. */
1722 pp_set_prefix (pretty_printer
*pp
, char *prefix
)
1725 pp
->prefix
= prefix
;
1726 pp_set_real_maximum_length (pp
);
1727 pp
->emitted_prefix
= false;
1728 pp_indentation (pp
) = 0;
1731 /* Take ownership of PP's prefix, setting it to NULL.
1732 This allows clients to save, override, and then restore an existing
1733 prefix, without it being free-ed. */
1736 pp_take_prefix (pretty_printer
*pp
)
1738 char *result
= pp
->prefix
;
1743 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
1745 pp_destroy_prefix (pretty_printer
*pp
)
1747 if (pp
->prefix
!= NULL
)
1754 /* Write out PRETTY-PRINTER's prefix. */
1756 pp_emit_prefix (pretty_printer
*pp
)
1758 if (pp
->prefix
!= NULL
)
1760 switch (pp_prefixing_rule (pp
))
1763 case DIAGNOSTICS_SHOW_PREFIX_NEVER
:
1766 case DIAGNOSTICS_SHOW_PREFIX_ONCE
:
1767 if (pp
->emitted_prefix
)
1772 pp_indentation (pp
) += 3;
1775 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
:
1777 int prefix_length
= strlen (pp
->prefix
);
1778 pp_append_r (pp
, pp
->prefix
, prefix_length
);
1779 pp
->emitted_prefix
= true;
1786 /* Construct a PRETTY-PRINTER of MAXIMUM_LENGTH characters per line. */
1788 pretty_printer::pretty_printer (int maximum_length
)
1789 : buffer (new (XCNEW (output_buffer
)) output_buffer ()),
1796 m_format_postprocessor (NULL
),
1799 translate_identifiers (true),
1801 url_format (URL_FORMAT_NONE
),
1802 m_skipping_null_url (false)
1804 pp_line_cutoff (this) = maximum_length
;
1805 /* By default, we emit prefixes once per message. */
1806 pp_prefixing_rule (this) = DIAGNOSTICS_SHOW_PREFIX_ONCE
;
1807 pp_set_prefix (this, NULL
);
1810 /* Copy constructor for pretty_printer. */
1812 pretty_printer::pretty_printer (const pretty_printer
&other
)
1813 : buffer (new (XCNEW (output_buffer
)) output_buffer ()),
1815 padding (other
.padding
),
1816 maximum_length (other
.maximum_length
),
1817 indent_skip (other
.indent_skip
),
1818 wrapping (other
.wrapping
),
1819 format_decoder (other
.format_decoder
),
1820 m_format_postprocessor (NULL
),
1821 emitted_prefix (other
.emitted_prefix
),
1822 need_newline (other
.need_newline
),
1823 translate_identifiers (other
.translate_identifiers
),
1824 show_color (other
.show_color
),
1825 url_format (other
.url_format
),
1826 m_skipping_null_url (false)
1828 pp_line_cutoff (this) = maximum_length
;
1829 /* By default, we emit prefixes once per message. */
1830 pp_prefixing_rule (this) = pp_prefixing_rule (&other
);
1831 pp_set_prefix (this, NULL
);
1833 if (other
.m_format_postprocessor
)
1834 m_format_postprocessor
= other
.m_format_postprocessor
->clone ();
1837 pretty_printer::~pretty_printer ()
1839 if (m_format_postprocessor
)
1840 delete m_format_postprocessor
;
1841 buffer
->~output_buffer ();
1846 /* Base class implementation of pretty_printer::clone vfunc. */
1849 pretty_printer::clone () const
1851 return new pretty_printer (*this);
1854 /* Append a string delimited by START and END to the output area of
1855 PRETTY-PRINTER. No line wrapping is done. However, if beginning a
1856 new line then emit PRETTY-PRINTER's prefix and skip any leading
1857 whitespace if appropriate. The caller must ensure that it is
1860 pp_append_text (pretty_printer
*pp
, const char *start
, const char *end
)
1862 /* Emit prefix and skip whitespace if we're starting a new line. */
1863 if (pp_buffer (pp
)->line_length
== 0)
1865 pp_emit_prefix (pp
);
1866 if (pp_is_wrapping_line (pp
))
1867 while (start
!= end
&& *start
== ' ')
1870 pp_append_r (pp
, start
, end
- start
);
1873 /* Finishes constructing a NULL-terminated character string representing
1874 the PRETTY-PRINTED text. */
1876 pp_formatted_text (pretty_printer
*pp
)
1878 return output_buffer_formatted_text (pp_buffer (pp
));
1881 /* Return a pointer to the last character emitted in PRETTY-PRINTER's
1882 output area. A NULL pointer means no character available. */
1884 pp_last_position_in_text (const pretty_printer
*pp
)
1886 return output_buffer_last_position_in_text (pp_buffer (pp
));
1889 /* Return the amount of characters PRETTY-PRINTER can accept to
1890 make a full line. Meaningful only in line-wrapping mode. */
1892 pp_remaining_character_count_for_line (pretty_printer
*pp
)
1894 return pp
->maximum_length
- pp_buffer (pp
)->line_length
;
1898 /* Format a message into BUFFER a la printf. */
1900 pp_printf (pretty_printer
*pp
, const char *msg
, ...)
1905 text_info
text (msg
, &ap
, errno
);
1906 pp_format (pp
, &text
);
1907 pp_output_formatted_text (pp
);
1912 /* Output MESSAGE verbatim into BUFFER. */
1914 pp_verbatim (pretty_printer
*pp
, const char *msg
, ...)
1919 text_info
text (msg
, &ap
, errno
);
1920 pp_format_verbatim (pp
, &text
);
1926 /* Have PRETTY-PRINTER start a new line. */
1928 pp_newline (pretty_printer
*pp
)
1930 obstack_1grow (pp_buffer (pp
)->obstack
, '\n');
1931 pp_needs_newline (pp
) = false;
1932 pp_buffer (pp
)->line_length
= 0;
1935 /* Have PRETTY-PRINTER add a CHARACTER. */
1937 pp_character (pretty_printer
*pp
, int c
)
1939 if (pp_is_wrapping_line (pp
)
1940 /* If printing UTF-8, don't wrap in the middle of a sequence. */
1941 && (((unsigned int) c
) & 0xC0) != 0x80
1942 && pp_remaining_character_count_for_line (pp
) <= 0)
1948 obstack_1grow (pp_buffer (pp
)->obstack
, c
);
1949 ++pp_buffer (pp
)->line_length
;
1952 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
1953 be line-wrapped if in appropriate mode. */
1955 pp_string (pretty_printer
*pp
, const char *str
)
1957 gcc_checking_assert (str
);
1958 pp_maybe_wrap_text (pp
, str
, str
+ strlen (str
));
1961 /* Append code point C to the output area of PRETTY-PRINTER, encoding it
1965 pp_unicode_character (pretty_printer
*pp
, unsigned c
)
1967 static const uchar masks
[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
1968 static const uchar limits
[6] = { 0x80, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE };
1970 uchar buf
[6], *p
= &buf
[6];
1979 *--p
= ((c
& 0x3F) | 0x80);
1983 while (c
>= 0x3F || (c
& limits
[nbytes
-1]));
1984 *--p
= (c
| masks
[nbytes
-1]);
1987 pp_append_r (pp
, (const char *)p
, nbytes
);
1990 /* Append the leading N characters of STRING to the output area of
1991 PRETTY-PRINTER, quoting in hexadecimal non-printable characters.
1992 Setting N = -1 is as if N were set to strlen (STRING). The STRING
1993 may be line-wrapped if in appropriate mode. */
1995 pp_quoted_string (pretty_printer
*pp
, const char *str
, size_t n
/* = -1 */)
1997 gcc_checking_assert (str
);
1999 const char *last
= str
;
2002 /* Compute the length if not specified. */
2003 if (n
== (size_t) -1)
2006 for (ps
= str
; n
; ++ps
, --n
)
2011 /* Don't escape a valid UTF-8 extended char. */
2012 const unsigned char *ups
= (const unsigned char *) ps
;
2015 unsigned int extended_char
;
2016 const int valid_utf8_len
= decode_utf8_char (ups
, n
, &extended_char
);
2017 if (valid_utf8_len
> 0)
2019 ps
+= valid_utf8_len
- 1;
2020 n
-= valid_utf8_len
- 1;
2026 pp_maybe_wrap_text (pp
, last
, ps
);
2028 /* Append the hexadecimal value of the character. Allocate a buffer
2029 that's large enough for a 32-bit char plus the hex prefix. */
2031 int n
= sprintf (buf
, "\\x%02x", (unsigned char)*ps
);
2032 pp_maybe_wrap_text (pp
, buf
, buf
+ n
);
2036 pp_maybe_wrap_text (pp
, last
, ps
);
2039 /* Maybe print out a whitespace if needed. */
2042 pp_maybe_space (pretty_printer
*pp
)
2044 if (pp
->padding
!= pp_none
)
2047 pp
->padding
= pp_none
;
2051 // Add a newline to the pretty printer PP and flush formatted text.
2054 pp_newline_and_flush (pretty_printer
*pp
)
2058 pp_needs_newline (pp
) = false;
2061 // Add a newline to the pretty printer PP, followed by indentation.
2064 pp_newline_and_indent (pretty_printer
*pp
, int n
)
2066 pp_indentation (pp
) += n
;
2069 pp_needs_newline (pp
) = false;
2072 // Add separator C, followed by a single whitespace.
2075 pp_separate_with (pretty_printer
*pp
, char c
)
2077 pp_character (pp
, c
);
2081 /* Add a localized open quote, and if SHOW_COLOR is true, begin colorizing
2082 using the "quote" color. */
2085 pp_begin_quote (pretty_printer
*pp
, bool show_color
)
2087 pp_string (pp
, open_quote
);
2088 pp_string (pp
, colorize_start (show_color
, "quote"));
2091 /* If SHOW_COLOR is true, stop colorizing.
2092 Add a localized close quote. */
2095 pp_end_quote (pretty_printer
*pp
, bool show_color
)
2097 pp_string (pp
, colorize_stop (show_color
));
2098 pp_string (pp
, close_quote
);
2102 /* The string starting at P has LEN (at least 1) bytes left; if they
2103 start with a valid UTF-8 sequence, return the length of that
2104 sequence and set *VALUE to the value of that sequence, and
2105 otherwise return 0 and set *VALUE to (unsigned int) -1. */
2108 decode_utf8_char (const unsigned char *p
, size_t len
, unsigned int *value
)
2110 unsigned int t
= *p
;
2116 size_t utf8_len
= 0;
2119 for (t
= *p
; t
& 0x80; t
<<= 1)
2122 if (utf8_len
> len
|| utf8_len
< 2 || utf8_len
> 6)
2124 *value
= (unsigned int) -1;
2127 ch
= *p
& ((1 << (7 - utf8_len
)) - 1);
2128 for (i
= 1; i
< utf8_len
; i
++)
2130 unsigned int u
= p
[i
];
2131 if ((u
& 0xC0) != 0x80)
2133 *value
= (unsigned int) -1;
2136 ch
= (ch
<< 6) | (u
& 0x3F);
2138 if ( (ch
<= 0x7F && utf8_len
> 1)
2139 || (ch
<= 0x7FF && utf8_len
> 2)
2140 || (ch
<= 0xFFFF && utf8_len
> 3)
2141 || (ch
<= 0x1FFFFF && utf8_len
> 4)
2142 || (ch
<= 0x3FFFFFF && utf8_len
> 5)
2143 || (ch
>= 0xD800 && ch
<= 0xDFFF))
2145 *value
= (unsigned int) -1;
2158 /* Allocator for identifier_to_locale and corresponding function to
2161 void *(*identifier_to_locale_alloc
) (size_t) = xmalloc
;
2162 void (*identifier_to_locale_free
) (void *) = free
;
2164 /* Given IDENT, an identifier in the internal encoding, return a
2165 version of IDENT suitable for diagnostics in the locale character
2166 set: either IDENT itself, or a string, allocated using
2167 identifier_to_locale_alloc, converted to the locale character set
2168 and using escape sequences if not representable in the locale
2169 character set or containing control characters or invalid byte
2170 sequences. Existing backslashes in IDENT are not doubled, so the
2171 result may not uniquely specify the contents of an arbitrary byte
2172 sequence identifier. */
2175 identifier_to_locale (const char *ident
)
2177 const unsigned char *uid
= (const unsigned char *) ident
;
2178 size_t idlen
= strlen (ident
);
2179 bool valid_printable_utf8
= true;
2180 bool all_ascii
= true;
2183 for (i
= 0; i
< idlen
;)
2186 size_t utf8_len
= decode_utf8_char (&uid
[i
], idlen
- i
, &c
);
2187 if (utf8_len
== 0 || c
<= 0x1F || (c
>= 0x7F && c
<= 0x9F))
2189 valid_printable_utf8
= false;
2197 /* If IDENT contains invalid UTF-8 sequences (which may occur with
2198 attributes putting arbitrary byte sequences in identifiers), or
2199 control characters, we use octal escape sequences for all bytes
2200 outside printable ASCII. */
2201 if (!valid_printable_utf8
)
2203 char *ret
= (char *) identifier_to_locale_alloc (4 * idlen
+ 1);
2205 for (i
= 0; i
< idlen
; i
++)
2207 if (uid
[i
] > 0x1F && uid
[i
] < 0x7F)
2211 sprintf (p
, "\\%03o", uid
[i
]);
2219 /* Otherwise, if it is valid printable ASCII, or printable UTF-8
2220 with the locale character set being UTF-8, IDENT is used. */
2221 if (all_ascii
|| locale_utf8
)
2224 /* Otherwise IDENT is converted to the locale character set if
2226 #if defined ENABLE_NLS && defined HAVE_LANGINFO_CODESET && HAVE_ICONV
2227 if (locale_encoding
!= NULL
)
2229 iconv_t cd
= iconv_open (locale_encoding
, "UTF-8");
2230 bool conversion_ok
= true;
2232 if (cd
!= (iconv_t
) -1)
2234 size_t ret_alloc
= 4 * idlen
+ 1;
2237 /* Repeat the whole conversion process as needed with
2238 larger buffers so non-reversible transformations can
2239 always be detected. */
2240 ICONV_CONST
char *inbuf
= CONST_CAST (char *, ident
);
2242 size_t inbytesleft
= idlen
;
2243 size_t outbytesleft
= ret_alloc
- 1;
2246 ret
= (char *) identifier_to_locale_alloc (ret_alloc
);
2249 if (iconv (cd
, 0, 0, 0, 0) == (size_t) -1)
2251 conversion_ok
= false;
2255 iconv_ret
= iconv (cd
, &inbuf
, &inbytesleft
,
2256 &outbuf
, &outbytesleft
);
2257 if (iconv_ret
== (size_t) -1 || inbytesleft
!= 0)
2262 identifier_to_locale_free (ret
);
2268 conversion_ok
= false;
2272 else if (iconv_ret
!= 0)
2274 conversion_ok
= false;
2277 /* Return to initial shift state. */
2278 if (iconv (cd
, 0, 0, &outbuf
, &outbytesleft
) == (size_t) -1)
2283 identifier_to_locale_free (ret
);
2289 conversion_ok
= false;
2303 /* Otherwise, convert non-ASCII characters in IDENT to UCNs. */
2305 char *ret
= (char *) identifier_to_locale_alloc (10 * idlen
+ 1);
2307 for (i
= 0; i
< idlen
;)
2310 size_t utf8_len
= decode_utf8_char (&uid
[i
], idlen
- i
, &c
);
2315 sprintf (p
, "\\U%08x", c
);
2325 /* Support for encoding URLs.
2326 See egmontkob/Hyperlinks_in_Terminal_Emulators.md
2327 ( https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda ).
2329 > A hyperlink is opened upon encountering an OSC 8 escape sequence with
2330 > the target URI. The syntax is
2332 > OSC 8 ; params ; URI ST
2334 > A hyperlink is closed with the same escape sequence, omitting the
2335 > parameters and the URI but keeping the separators:
2339 > OSC (operating system command) is typically ESC ].
2341 Use BEL instead of ST, as that is currently rendered better in some
2342 terminal emulators that don't support OSC 8, like konsole. */
2344 /* If URL-printing is enabled, write an "open URL" escape sequence to PP
2345 for the given URL. */
2348 pp_begin_url (pretty_printer
*pp
, const char *url
)
2352 /* Handle null URL by skipping all output here,
2353 and in the next pp_end_url. */
2354 pp
->m_skipping_null_url
= true;
2357 switch (pp
->url_format
)
2359 case URL_FORMAT_NONE
:
2362 pp_string (pp
, "\33]8;;");
2363 pp_string (pp
, url
);
2364 pp_string (pp
, "\33\\");
2366 case URL_FORMAT_BEL
:
2367 pp_string (pp
, "\33]8;;");
2368 pp_string (pp
, url
);
2369 pp_string (pp
, "\a");
2376 /* Helper function for pp_end_url and pp_format, return the "close URL" escape
2380 get_end_url_string (pretty_printer
*pp
)
2382 switch (pp
->url_format
)
2384 case URL_FORMAT_NONE
:
2387 return "\33]8;;\33\\";
2388 case URL_FORMAT_BEL
:
2395 /* If URL-printing is enabled, write a "close URL" escape sequence to PP. */
2398 pp_end_url (pretty_printer
*pp
)
2400 if (pp
->m_skipping_null_url
)
2402 /* We gracefully handle pp_begin_url (NULL) by omitting output for
2403 both begin and end. Here we handle the latter. */
2404 pp
->m_skipping_null_url
= false;
2407 if (pp
->url_format
!= URL_FORMAT_NONE
)
2408 pp_string (pp
, get_end_url_string (pp
));
2413 namespace selftest
{
2415 /* Smoketest for pretty_printer. */
2418 test_basic_printing ()
2421 pp_string (&pp
, "hello");
2423 pp_string (&pp
, "world");
2425 ASSERT_STREQ ("hello world", pp_formatted_text (&pp
));
2428 /* Helper function for testing pp_format.
2429 Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
2430 prints EXPECTED, assuming that pp_show_color is SHOW_COLOR. */
2433 assert_pp_format_va (const location
&loc
, const char *expected
,
2434 bool show_color
, const char *fmt
, va_list *ap
)
2437 rich_location
rich_loc (line_table
, UNKNOWN_LOCATION
);
2439 text_info
ti (fmt
, ap
, 0, nullptr, &rich_loc
);
2441 pp_show_color (&pp
) = show_color
;
2442 pp_format (&pp
, &ti
);
2443 pp_output_formatted_text (&pp
);
2444 ASSERT_STREQ_AT (loc
, expected
, pp_formatted_text (&pp
));
2447 /* Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
2448 prints EXPECTED, with show_color disabled. */
2451 assert_pp_format (const location
&loc
, const char *expected
,
2452 const char *fmt
, ...)
2457 assert_pp_format_va (loc
, expected
, false, fmt
, &ap
);
2461 /* As above, but with colorization enabled. */
2464 assert_pp_format_colored (const location
&loc
, const char *expected
,
2465 const char *fmt
, ...)
2467 /* The tests of colorization assume the default color scheme.
2468 If GCC_COLORS is set, then the colors have potentially been
2469 overridden; skip the test. */
2470 if (getenv ("GCC_COLORS"))
2476 assert_pp_format_va (loc
, expected
, true, fmt
, &ap
);
2480 /* Helper function for calling testing pp_format,
2481 by calling assert_pp_format with various numbers of arguments.
2482 These exist mostly to avoid having to write SELFTEST_LOCATION
2483 throughout test_pp_format. */
2485 #define ASSERT_PP_FORMAT_1(EXPECTED, FMT, ARG1) \
2486 SELFTEST_BEGIN_STMT \
2487 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
2491 #define ASSERT_PP_FORMAT_2(EXPECTED, FMT, ARG1, ARG2) \
2492 SELFTEST_BEGIN_STMT \
2493 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
2497 #define ASSERT_PP_FORMAT_3(EXPECTED, FMT, ARG1, ARG2, ARG3) \
2498 SELFTEST_BEGIN_STMT \
2499 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
2500 (ARG1), (ARG2), (ARG3)); \
2503 /* Verify that pp_format works, for various format codes. */
2508 /* Avoid introducing locale-specific differences in the results
2509 by hardcoding open_quote and close_quote. */
2510 auto_fix_quotes fix_quotes
;
2512 /* Verify that plain text is passed through unchanged. */
2513 assert_pp_format (SELFTEST_LOCATION
, "unformatted", "unformatted");
2515 /* Verify various individual format codes, in the order listed in the
2516 comment for pp_format above. For each code, we append a second
2517 argument with a known bit pattern (0x12345678), to ensure that we
2518 are consuming arguments correctly. */
2519 ASSERT_PP_FORMAT_2 ("-27 12345678", "%d %x", -27, 0x12345678);
2520 ASSERT_PP_FORMAT_2 ("-5 12345678", "%i %x", -5, 0x12345678);
2521 ASSERT_PP_FORMAT_2 ("10 12345678", "%u %x", 10, 0x12345678);
2522 ASSERT_PP_FORMAT_2 ("17 12345678", "%o %x", 15, 0x12345678);
2523 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%x %x", 0xcafebabe, 0x12345678);
2524 ASSERT_PP_FORMAT_2 ("-27 12345678", "%ld %x", (long)-27, 0x12345678);
2525 ASSERT_PP_FORMAT_2 ("-5 12345678", "%li %x", (long)-5, 0x12345678);
2526 ASSERT_PP_FORMAT_2 ("10 12345678", "%lu %x", (long)10, 0x12345678);
2527 ASSERT_PP_FORMAT_2 ("17 12345678", "%lo %x", (long)15, 0x12345678);
2528 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%lx %x", (long)0xcafebabe,
2530 ASSERT_PP_FORMAT_2 ("-27 12345678", "%lld %x", (long long)-27, 0x12345678);
2531 ASSERT_PP_FORMAT_2 ("-5 12345678", "%lli %x", (long long)-5, 0x12345678);
2532 ASSERT_PP_FORMAT_2 ("10 12345678", "%llu %x", (long long)10, 0x12345678);
2533 ASSERT_PP_FORMAT_2 ("17 12345678", "%llo %x", (long long)15, 0x12345678);
2534 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%llx %x", (long long)0xcafebabe,
2536 ASSERT_PP_FORMAT_2 ("-27 12345678", "%wd %x", (HOST_WIDE_INT
)-27, 0x12345678);
2537 ASSERT_PP_FORMAT_2 ("-5 12345678", "%wi %x", (HOST_WIDE_INT
)-5, 0x12345678);
2538 ASSERT_PP_FORMAT_2 ("10 12345678", "%wu %x", (unsigned HOST_WIDE_INT
)10,
2540 ASSERT_PP_FORMAT_2 ("17 12345678", "%wo %x", (HOST_WIDE_INT
)15, 0x12345678);
2541 ASSERT_PP_FORMAT_2 ("0xcafebabe 12345678", "%wx %x", (HOST_WIDE_INT
)0xcafebabe,
2543 ASSERT_PP_FORMAT_2 ("1.000000 12345678", "%f %x", 1.0, 0x12345678);
2544 ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678);
2545 ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello world",
2548 /* Not nul-terminated. */
2549 char arr
[5] = { '1', '2', '3', '4', '5' };
2550 ASSERT_PP_FORMAT_3 ("123 12345678", "%.*s %x", 3, arr
, 0x12345678);
2551 ASSERT_PP_FORMAT_3 ("1234 12345678", "%.*s %x", -1, "1234", 0x12345678);
2552 ASSERT_PP_FORMAT_3 ("12345 12345678", "%.*s %x", 7, "12345", 0x12345678);
2554 /* We can't test for %p; the pointer is printed in an implementation-defined
2556 ASSERT_PP_FORMAT_2 ("normal colored normal 12345678",
2557 "normal %rcolored%R normal %x",
2558 "error", 0x12345678);
2559 assert_pp_format_colored
2561 "normal \33[01;31m\33[Kcolored\33[m\33[K normal 12345678",
2562 "normal %rcolored%R normal %x", "error", 0x12345678);
2564 %m: strerror(text->err_no) - does not consume a value from args_ptr. */
2565 ASSERT_PP_FORMAT_1 ("% 12345678", "%% %x", 0x12345678);
2566 ASSERT_PP_FORMAT_1 ("` 12345678", "%< %x", 0x12345678);
2567 ASSERT_PP_FORMAT_1 ("' 12345678", "%> %x", 0x12345678);
2568 ASSERT_PP_FORMAT_1 ("' 12345678", "%' %x", 0x12345678);
2569 ASSERT_PP_FORMAT_3 ("abc 12345678", "%.*s %x", 3, "abcdef", 0x12345678);
2570 ASSERT_PP_FORMAT_2 ("abc 12345678", "%.3s %x", "abcdef", 0x12345678);
2572 /* Verify flag 'q'. */
2573 ASSERT_PP_FORMAT_2 ("`foo' 12345678", "%qs %x", "foo", 0x12345678);
2574 assert_pp_format_colored (SELFTEST_LOCATION
,
2575 "`\33[01m\33[Kfoo\33[m\33[K' 12345678", "%qs %x",
2579 diagnostic_event_id_t
first (2);
2580 diagnostic_event_id_t
second (7);
2582 ASSERT_PP_FORMAT_2 ("first `free' at (3); second `free' at (8)",
2583 "first %<free%> at %@; second %<free%> at %@",
2585 assert_pp_format_colored
2587 "first `\e[01m\e[Kfree\e[m\e[K' at \e[01;36m\e[K(3)\e[m\e[K;"
2588 " second `\e[01m\e[Kfree\e[m\e[K' at \e[01;36m\e[K(8)\e[m\e[K",
2589 "first %<free%> at %@; second %<free%> at %@",
2594 int v
[] = { 1, 2, 3 };
2595 ASSERT_PP_FORMAT_3 ("1, 2, 3 12345678", "%Z %x", v
, 3, 0x12345678);
2598 ASSERT_PP_FORMAT_3 ("0 12345678", "%Z %x", v2
, 1, 0x12345678);
2600 /* Verify that combinations work, along with unformatted text. */
2601 assert_pp_format (SELFTEST_LOCATION
,
2602 "the quick brown fox jumps over the lazy dog",
2603 "the %s %s %s jumps over the %s %s",
2604 "quick", "brown", "fox", "lazy", "dog");
2605 assert_pp_format (SELFTEST_LOCATION
, "item 3 of 7", "item %i of %i", 3, 7);
2606 assert_pp_format (SELFTEST_LOCATION
, "problem with `bar' at line 10",
2607 "problem with %qs at line %i", "bar", 10);
2610 /* A subclass of pretty_printer for use by test_prefixes_and_wrapping. */
2612 class test_pretty_printer
: public pretty_printer
2615 test_pretty_printer (enum diagnostic_prefixing_rule_t rule
,
2616 int max_line_length
)
2618 pp_set_prefix (this, xstrdup ("PREFIX: "));
2619 wrapping
.rule
= rule
;
2620 pp_set_line_maximum_length (this, max_line_length
);
2624 /* Verify that the various values of enum diagnostic_prefixing_rule_t work
2625 as expected, with and without line wrapping. */
2628 test_prefixes_and_wrapping ()
2630 /* Tests of the various prefixing rules, without wrapping.
2631 Newlines embedded in pp_string don't affect it; we have to
2632 explicitly call pp_newline. */
2634 test_pretty_printer
pp (DIAGNOSTICS_SHOW_PREFIX_ONCE
, 0);
2635 pp_string (&pp
, "the quick brown fox");
2637 pp_string (&pp
, "jumps over the lazy dog");
2639 ASSERT_STREQ (pp_formatted_text (&pp
),
2640 "PREFIX: the quick brown fox\n"
2641 " jumps over the lazy dog\n");
2644 test_pretty_printer
pp (DIAGNOSTICS_SHOW_PREFIX_NEVER
, 0);
2645 pp_string (&pp
, "the quick brown fox");
2647 pp_string (&pp
, "jumps over the lazy dog");
2649 ASSERT_STREQ (pp_formatted_text (&pp
),
2650 "the quick brown fox\n"
2651 "jumps over the lazy dog\n");
2654 test_pretty_printer
pp (DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
, 0);
2655 pp_string (&pp
, "the quick brown fox");
2657 pp_string (&pp
, "jumps over the lazy dog");
2659 ASSERT_STREQ (pp_formatted_text (&pp
),
2660 "PREFIX: the quick brown fox\n"
2661 "PREFIX: jumps over the lazy dog\n");
2664 /* Tests of the various prefixing rules, with wrapping. */
2666 test_pretty_printer
pp (DIAGNOSTICS_SHOW_PREFIX_ONCE
, 20);
2667 pp_string (&pp
, "the quick brown fox jumps over the lazy dog");
2669 pp_string (&pp
, "able was I ere I saw elba");
2671 ASSERT_STREQ (pp_formatted_text (&pp
),
2672 "PREFIX: the quick \n"
2673 " brown fox jumps \n"
2676 " able was I ere I \n"
2680 test_pretty_printer
pp (DIAGNOSTICS_SHOW_PREFIX_NEVER
, 20);
2681 pp_string (&pp
, "the quick brown fox jumps over the lazy dog");
2683 pp_string (&pp
, "able was I ere I saw elba");
2685 ASSERT_STREQ (pp_formatted_text (&pp
),
2686 "the quick brown fox \n"
2687 "jumps over the lazy \n"
2689 "able was I ere I \n"
2693 test_pretty_printer
pp (DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
, 20);
2694 pp_string (&pp
, "the quick brown fox jumps over the lazy dog");
2696 pp_string (&pp
, "able was I ere I saw elba");
2698 ASSERT_STREQ (pp_formatted_text (&pp
),
2699 "PREFIX: the quick brown fox jumps over the lazy dog\n"
2700 "PREFIX: able was I ere I saw elba\n");
2705 /* Verify that URL-printing works as expected. */
2712 pp
.url_format
= URL_FORMAT_NONE
;
2713 pp_begin_url (&pp
, "http://example.com");
2714 pp_string (&pp
, "This is a link");
2716 ASSERT_STREQ ("This is a link",
2717 pp_formatted_text (&pp
));
2722 pp
.url_format
= URL_FORMAT_ST
;
2723 pp_begin_url (&pp
, "http://example.com");
2724 pp_string (&pp
, "This is a link");
2726 ASSERT_STREQ ("\33]8;;http://example.com\33\\This is a link\33]8;;\33\\",
2727 pp_formatted_text (&pp
));
2732 pp
.url_format
= URL_FORMAT_BEL
;
2733 pp_begin_url (&pp
, "http://example.com");
2734 pp_string (&pp
, "This is a link");
2736 ASSERT_STREQ ("\33]8;;http://example.com\aThis is a link\33]8;;\a",
2737 pp_formatted_text (&pp
));
2741 /* Verify that we gracefully reject null URLs. */
2748 pp
.url_format
= URL_FORMAT_NONE
;
2749 pp_begin_url (&pp
, nullptr);
2750 pp_string (&pp
, "This isn't a link");
2752 ASSERT_STREQ ("This isn't a link",
2753 pp_formatted_text (&pp
));
2758 pp
.url_format
= URL_FORMAT_ST
;
2759 pp_begin_url (&pp
, nullptr);
2760 pp_string (&pp
, "This isn't a link");
2762 ASSERT_STREQ ("This isn't a link",
2763 pp_formatted_text (&pp
));
2768 pp
.url_format
= URL_FORMAT_BEL
;
2769 pp_begin_url (&pp
, nullptr);
2770 pp_string (&pp
, "This isn't a link");
2772 ASSERT_STREQ ("This isn't a link",
2773 pp_formatted_text (&pp
));
2777 /* Verify that URLification works as expected. */
2780 pp_printf_with_urlifier (pretty_printer
*pp
,
2781 const urlifier
*urlifier
,
2782 const char *msg
, ...)
2787 text_info
text (msg
, &ap
, errno
);
2788 pp_format (pp
, &text
, urlifier
);
2789 pp_output_formatted_text (pp
);
2795 test_urlification ()
2797 class test_urlifier
: public urlifier
2801 get_url_for_quoted_text (const char *p
, size_t sz
) const final override
2803 if (!strncmp (p
, "-foption", sz
))
2804 return xstrdup ("http://example.com");
2809 auto_fix_quotes fix_quotes
;
2810 const test_urlifier urlifier
;
2812 /* Uses of "%<" and "%>". */
2816 pp
.url_format
= URL_FORMAT_NONE
;
2817 pp_printf_with_urlifier (&pp
, &urlifier
,
2818 "foo %<-foption%> %<unrecognized%> bar");
2819 ASSERT_STREQ ("foo `-foption' `unrecognized' bar",
2820 pp_formatted_text (&pp
));
2824 pp
.url_format
= URL_FORMAT_ST
;
2825 pp_printf_with_urlifier (&pp
, &urlifier
,
2826 "foo %<-foption%> %<unrecognized%> bar");
2828 ("foo `\33]8;;http://example.com\33\\-foption\33]8;;\33\\'"
2829 " `unrecognized' bar",
2830 pp_formatted_text (&pp
));
2834 pp
.url_format
= URL_FORMAT_BEL
;
2835 pp_printf_with_urlifier (&pp
, &urlifier
,
2836 "foo %<-foption%> %<unrecognized%> bar");
2838 ("foo `\33]8;;http://example.com\a-foption\33]8;;\a'"
2839 " `unrecognized' bar",
2840 pp_formatted_text (&pp
));
2847 pp
.url_format
= URL_FORMAT_ST
;
2848 pp_printf_with_urlifier (&pp
, &urlifier
,
2850 "-foption", "unrecognized");
2852 ("foo `\33]8;;http://example.com\33\\-foption\33]8;;\33\\'"
2853 " `unrecognized' bar",
2854 pp_formatted_text (&pp
));
2857 /* Mixed usage of %< and %s, where the quoted string is built between
2858 a mixture of phase 1 and phase 2. */
2861 pp
.url_format
= URL_FORMAT_ST
;
2862 pp_printf_with_urlifier (&pp
, &urlifier
,
2865 /* We don't support this, but make sure we don't crash. */
2867 ("foo `-foption' bar",
2868 pp_formatted_text (&pp
));
2872 /* Test multibyte awareness. */
2873 static void test_utf8 ()
2876 /* Check that pp_quoted_string leaves valid UTF-8 alone. */
2879 const char *s
= "\xf0\x9f\x98\x82";
2880 pp_quoted_string (&pp
, s
);
2881 ASSERT_STREQ (pp_formatted_text (&pp
), s
);
2884 /* Check that pp_quoted_string escapes non-UTF-8 nonprintable bytes. */
2887 pp_quoted_string (&pp
, "\xf0!\x9f\x98\x82");
2888 ASSERT_STREQ (pp_formatted_text (&pp
),
2889 "\\xf0!\\x9f\\x98\\x82");
2892 /* Check that pp_character will line-wrap at the beginning of a UTF-8
2893 sequence, but not in the middle. */
2895 pretty_printer
pp (3);
2896 const char s
[] = "---\xf0\x9f\x98\x82";
2897 for (int i
= 0; i
!= sizeof (s
) - 1; ++i
)
2898 pp_character (&pp
, s
[i
]);
2900 for (int i
= 1; i
!= sizeof (s
) - 1; ++i
)
2901 pp_character (&pp
, s
[i
]);
2902 pp_character (&pp
, '-');
2903 ASSERT_STREQ (pp_formatted_text (&pp
),
2905 "\xf0\x9f\x98\x82\n"
2906 "--\xf0\x9f\x98\x82\n"
2912 /* Run all of the selftests within this file. */
2915 pretty_print_cc_tests ()
2917 test_basic_printing ();
2919 test_prefixes_and_wrapping ();
2922 test_urlification ();
2926 } // namespace selftest
2928 #endif /* CHECKING_P */