1 /* Various declarations for language-independent pretty-print subroutines.
2 Copyright (C) 2003-2019 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"
35 /* Replacement for fputs() that handles ANSI escape codes on Windows NT.
36 Contributed by: Liu Hao (lh_mouse at 126 dot com)
38 XXX: This file is compiled into libcommon.a that will be self-contained.
39 It looks like that these functions can be put nowhere else. */
42 #define WIN32_LEAN_AND_MEAN 1
45 /* Write all bytes in [s,s+n) into the specified stream.
46 Errors are ignored. */
48 write_all (HANDLE h
, const char *s
, size_t n
)
59 if (!WriteFile (h
, s
+ n
- rem
, step
, &step
, NULL
))
65 /* Find the beginning of an escape sequence.
67 1. If the sequence begins with an ESC character (0x1B) and a second
68 character X in [0x40,0x5F], returns X and stores a pointer to
69 the third character into *head.
70 2. If the sequence begins with a character X in [0x80,0x9F], returns
71 (X-0x40) and stores a pointer to the second character into *head.
72 Stores the number of ESC character(s) in *prefix_len.
73 Returns 0 if no such sequence can be found. */
75 find_esc_head (int *prefix_len
, const char **head
, const char *str
)
83 c
= (unsigned char) *r
;
89 if (escaped
&& 0x40 <= c
&& c
<= 0x5F)
96 if (0x80 <= c
&& c
<= 0x9F)
108 /* Find the terminator of an escape sequence.
109 str should be the value stored in *head by a previous successful
110 call to find_esc_head().
111 Returns 0 if no such sequence can be found. */
113 find_esc_terminator (const char **term
, const char *str
)
120 c
= (unsigned char) *r
;
126 if (0x40 <= c
&& c
<= 0x7E)
136 /* Handle a sequence of codes. Sequences that are invalid, reserved,
137 unrecognized or unimplemented are ignored silently.
138 There isn't much we can do because of lameness of Windows consoles. */
140 eat_esc_sequence (HANDLE h
, int esc_code
,
141 const char *esc_head
, const char *esc_term
)
143 /* Numbers in an escape sequence cannot be negative, because
144 a minus sign in the middle of it would have terminated it. */
147 CONSOLE_SCREEN_BUFFER_INFO sb
;
149 /* ED and EL parameters. */
152 /* SGR parameters. */
153 WORD attrib_add
, attrib_rm
;
156 switch (MAKEWORD (esc_code
, *esc_term
))
159 Move the cursor up by n1 characters. */
160 case MAKEWORD ('[', 'A'):
161 if (esc_head
== esc_term
)
165 n1
= strtol (esc_head
, &eptr
, 10);
166 if (eptr
!= esc_term
)
170 if (GetConsoleScreenBufferInfo (h
, &sb
))
172 cr
= sb
.dwCursorPosition
;
173 /* Stop at the topmost boundary. */
178 SetConsoleCursorPosition (h
, cr
);
183 Move the cursor down by n1 characters. */
184 case MAKEWORD ('[', 'B'):
185 if (esc_head
== esc_term
)
189 n1
= strtol (esc_head
, &eptr
, 10);
190 if (eptr
!= esc_term
)
194 if (GetConsoleScreenBufferInfo (h
, &sb
))
196 cr
= sb
.dwCursorPosition
;
197 /* Stop at the bottommost boundary. */
198 if (sb
.dwSize
.Y
- cr
.Y
> n1
)
202 SetConsoleCursorPosition (h
, cr
);
207 Move the cursor right by n1 characters. */
208 case MAKEWORD ('[', 'C'):
209 if (esc_head
== esc_term
)
213 n1
= strtol (esc_head
, &eptr
, 10);
214 if (eptr
!= esc_term
)
218 if (GetConsoleScreenBufferInfo (h
, &sb
))
220 cr
= sb
.dwCursorPosition
;
221 /* Stop at the rightmost boundary. */
222 if (sb
.dwSize
.X
- cr
.X
> n1
)
226 SetConsoleCursorPosition (h
, cr
);
231 Move the cursor left by n1 characters. */
232 case MAKEWORD ('[', 'D'):
233 if (esc_head
== esc_term
)
237 n1
= strtol (esc_head
, &eptr
, 10);
238 if (eptr
!= esc_term
)
242 if (GetConsoleScreenBufferInfo (h
, &sb
))
244 cr
= sb
.dwCursorPosition
;
245 /* Stop at the leftmost boundary. */
250 SetConsoleCursorPosition (h
, cr
);
255 Move the cursor to the beginning of the n1-th line downwards. */
256 case MAKEWORD ('[', 'E'):
257 if (esc_head
== esc_term
)
261 n1
= strtol (esc_head
, &eptr
, 10);
262 if (eptr
!= esc_term
)
266 if (GetConsoleScreenBufferInfo (h
, &sb
))
268 cr
= sb
.dwCursorPosition
;
270 /* Stop at the bottommost boundary. */
271 if (sb
.dwSize
.Y
- cr
.Y
> n1
)
275 SetConsoleCursorPosition (h
, cr
);
280 Move the cursor to the beginning of the n1-th line upwards. */
281 case MAKEWORD ('[', 'F'):
282 if (esc_head
== esc_term
)
286 n1
= strtol (esc_head
, &eptr
, 10);
287 if (eptr
!= esc_term
)
291 if (GetConsoleScreenBufferInfo (h
, &sb
))
293 cr
= sb
.dwCursorPosition
;
295 /* Stop at the topmost boundary. */
300 SetConsoleCursorPosition (h
, cr
);
305 Move the cursor to the (1-based) n1-th column. */
306 case MAKEWORD ('[', 'G'):
307 if (esc_head
== esc_term
)
311 n1
= strtol (esc_head
, &eptr
, 10);
312 if (eptr
!= esc_term
)
316 if (GetConsoleScreenBufferInfo (h
, &sb
))
318 cr
= sb
.dwCursorPosition
;
320 /* Stop at the leftmost or rightmost boundary. */
323 else if (n1
> sb
.dwSize
.X
)
327 SetConsoleCursorPosition (h
, cr
);
331 /* ESC [ n1 ';' n2 'H'
333 Move the cursor to the (1-based) n1-th row and
334 (also 1-based) n2-th column. */
335 case MAKEWORD ('[', 'H'):
336 case MAKEWORD ('[', 'f'):
337 if (esc_head
== esc_term
)
339 /* Both parameters are omitted and set to 1 by default. */
343 else if (!(delim
= (char *) memchr (esc_head
, ';',
344 esc_term
- esc_head
)))
346 /* Only the first parameter is given. The second one is
347 set to 1 by default. */
348 n1
= strtol (esc_head
, &eptr
, 10);
349 if (eptr
!= esc_term
)
355 /* Both parameters are given. The first one shall be
356 terminated by the semicolon. */
357 n1
= strtol (esc_head
, &eptr
, 10);
360 n2
= strtol (delim
+ 1, &eptr
, 10);
361 if (eptr
!= esc_term
)
365 if (GetConsoleScreenBufferInfo (h
, &sb
))
367 cr
= sb
.dwCursorPosition
;
370 /* The cursor position shall be relative to the view coord of
371 the console window, which is usually smaller than the actual
372 buffer. FWIW, the 'appropriate' solution will be shrinking
373 the buffer to match the size of the console window,
374 destroying scrollback in the process. */
375 n1
+= sb
.srWindow
.Top
;
376 n2
+= sb
.srWindow
.Left
;
377 /* Stop at the topmost or bottommost boundary. */
380 else if (n1
> sb
.dwSize
.Y
)
384 /* Stop at the leftmost or rightmost boundary. */
387 else if (n2
> sb
.dwSize
.X
)
391 SetConsoleCursorPosition (h
, cr
);
397 case MAKEWORD ('[', 'J'):
398 if (esc_head
== esc_term
)
399 /* This is one of the very few codes whose parameters have
400 a default value of zero. */
404 n1
= strtol (esc_head
, &eptr
, 10);
405 if (eptr
!= esc_term
)
409 if (GetConsoleScreenBufferInfo (h
, &sb
))
411 /* The cursor is not necessarily in the console window, which
412 makes the behavior of this code harder to define. */
416 /* If the cursor is in or above the window, erase from
417 it to the bottom of the window; otherwise, do nothing. */
418 cr
= sb
.dwCursorPosition
;
419 cnt
= sb
.dwSize
.X
- sb
.dwCursorPosition
.X
;
420 rows
= sb
.srWindow
.Bottom
- sb
.dwCursorPosition
.Y
;
423 /* If the cursor is in or under the window, erase from
424 it to the top of the window; otherwise, do nothing. */
426 cr
.Y
= sb
.srWindow
.Top
;
427 cnt
= sb
.dwCursorPosition
.X
+ 1;
428 rows
= sb
.dwCursorPosition
.Y
- sb
.srWindow
.Top
;
431 /* Erase the entire window. */
432 cr
.X
= sb
.srWindow
.Left
;
433 cr
.Y
= sb
.srWindow
.Top
;
435 rows
= sb
.srWindow
.Bottom
- sb
.srWindow
.Top
+ 1;
438 /* Erase the entire buffer. */
447 cnt
+= rows
* sb
.dwSize
.X
;
448 FillConsoleOutputCharacterW (h
, L
' ', cnt
, cr
, &step
);
449 FillConsoleOutputAttribute (h
, sb
.wAttributes
, cnt
, cr
, &step
);
455 case MAKEWORD ('[', 'K'):
456 if (esc_head
== esc_term
)
457 /* This is one of the very few codes whose parameters have
458 a default value of zero. */
462 n1
= strtol (esc_head
, &eptr
, 10);
463 if (eptr
!= esc_term
)
467 if (GetConsoleScreenBufferInfo (h
, &sb
))
472 /* Erase from the cursor to the end. */
473 cr
= sb
.dwCursorPosition
;
474 cnt
= sb
.dwSize
.X
- sb
.dwCursorPosition
.X
;
477 /* Erase from the cursor to the beginning. */
478 cr
= sb
.dwCursorPosition
;
480 cnt
= sb
.dwCursorPosition
.X
+ 1;
483 /* Erase the entire line. */
484 cr
= sb
.dwCursorPosition
;
489 FillConsoleOutputCharacterW (h
, L
' ', cnt
, cr
, &step
);
490 FillConsoleOutputAttribute (h
, sb
.wAttributes
, cnt
, cr
, &step
);
494 /* ESC [ n1 ';' n2 'm'
495 Set SGR parameters. Zero or more parameters will follow. */
496 case MAKEWORD ('[', 'm'):
499 if (esc_head
== esc_term
)
501 /* When no parameter is given, reset the console. */
502 attrib_add
|= (FOREGROUND_RED
| FOREGROUND_GREEN
504 attrib_rm
= -1; /* Removes everything. */
510 /* Parse a parameter. */
511 n1
= strtol (param
, &eptr
, 10);
512 if (*eptr
!= ';' && eptr
!= esc_term
)
519 attrib_add
|= (FOREGROUND_RED
| FOREGROUND_GREEN
521 attrib_rm
= -1; /* Removes everything. */
525 attrib_add
|= FOREGROUND_INTENSITY
;
529 attrib_add
|= COMMON_LVB_UNDERSCORE
;
533 /* XXX: It is not BLINKING at all! */
534 attrib_add
|= BACKGROUND_INTENSITY
;
538 attrib_add
|= COMMON_LVB_REVERSE_VIDEO
;
542 attrib_add
&= ~FOREGROUND_INTENSITY
;
543 attrib_rm
|= FOREGROUND_INTENSITY
;
547 attrib_add
&= ~COMMON_LVB_UNDERSCORE
;
548 attrib_rm
|= COMMON_LVB_UNDERSCORE
;
552 /* XXX: It is not BLINKING at all! */
553 attrib_add
&= ~BACKGROUND_INTENSITY
;
554 attrib_rm
|= BACKGROUND_INTENSITY
;
558 attrib_add
&= ~COMMON_LVB_REVERSE_VIDEO
;
559 attrib_rm
|= COMMON_LVB_REVERSE_VIDEO
;
569 /* Foreground color. */
570 attrib_add
&= ~(FOREGROUND_RED
| FOREGROUND_GREEN
574 attrib_add
|= FOREGROUND_RED
;
576 attrib_add
|= FOREGROUND_GREEN
;
578 attrib_add
|= FOREGROUND_BLUE
;
579 attrib_rm
|= (FOREGROUND_RED
| FOREGROUND_GREEN
583 /* Reserved for extended foreground color.
584 Don't know how to handle parameters remaining.
588 /* Reset foreground color. */
590 attrib_add
|= (FOREGROUND_RED
| FOREGROUND_GREEN
592 attrib_rm
|= (FOREGROUND_RED
| FOREGROUND_GREEN
603 /* Background color. */
604 attrib_add
&= ~(BACKGROUND_RED
| BACKGROUND_GREEN
608 attrib_add
|= BACKGROUND_RED
;
610 attrib_add
|= BACKGROUND_GREEN
;
612 attrib_add
|= BACKGROUND_BLUE
;
613 attrib_rm
|= (BACKGROUND_RED
| BACKGROUND_GREEN
617 /* Reserved for extended background color.
618 Don't know how to handle parameters remaining.
622 /* Reset background color. */
624 attrib_add
&= ~(BACKGROUND_RED
| BACKGROUND_GREEN
626 attrib_rm
|= (BACKGROUND_RED
| BACKGROUND_GREEN
631 /* Prepare the next parameter. */
634 while (param
!= esc_term
);
637 /* 0xFFFF removes everything. If it is not the case,
638 care must be taken to preserve old attributes. */
639 if (attrib_rm
!= 0xFFFF && GetConsoleScreenBufferInfo (h
, &sb
))
641 attrib_add
|= sb
.wAttributes
& ~attrib_rm
;
643 if (attrib_add
& COMMON_LVB_REVERSE_VIDEO
)
645 /* COMMON_LVB_REVERSE_VIDEO is only effective for DBCS.
646 * Swap foreground and background colors by hand.
648 attrib_add
= (attrib_add
& 0xFF00)
649 | ((attrib_add
& 0x00F0) >> 4)
650 | ((attrib_add
& 0x000F) << 4);
651 attrib_add
&= ~COMMON_LVB_REVERSE_VIDEO
;
653 SetConsoleTextAttribute (h
, attrib_add
);
659 mingw_ansi_fputs (const char *str
, FILE *fp
)
661 const char *read
= str
;
664 int esc_code
, prefix_len
;
665 const char *esc_head
, *esc_term
;
667 h
= (HANDLE
) _get_osfhandle (_fileno (fp
));
668 if (h
== INVALID_HANDLE_VALUE
)
671 /* Don't mess up stdio functions with Windows APIs. */
674 if (GetConsoleMode (h
, &mode
))
675 /* If it is a console, translate ANSI escape codes as needed. */
678 if ((esc_code
= find_esc_head (&prefix_len
, &esc_head
, read
)) == 0)
680 /* Write all remaining characters, then exit. */
681 write_all (h
, read
, strlen (read
));
684 if (find_esc_terminator (&esc_term
, esc_head
) == 0)
685 /* Ignore incomplete escape sequences at the moment.
686 FIXME: The escape state shall be cached for further calls
689 write_all (h
, read
, esc_head
- prefix_len
- read
);
690 eat_esc_sequence (h
, esc_code
, esc_head
, esc_term
);
694 /* If it is not a console, write everything as-is. */
695 write_all (h
, read
, strlen (read
));
700 #endif /* __MINGW32__ */
702 static void pp_quoted_string (pretty_printer
*, const char *, size_t = -1);
704 /* Overwrite the given location/range within this text_info's rich_location.
705 For use e.g. when implementing "+" in client format decoders. */
708 text_info::set_location (unsigned int idx
, location_t loc
,
709 enum range_display_kind range_display_kind
)
711 gcc_checking_assert (m_richloc
);
712 m_richloc
->set_range (idx
, loc
, range_display_kind
);
716 text_info::get_location (unsigned int index_of_location
) const
718 gcc_checking_assert (m_richloc
);
720 if (index_of_location
== 0)
721 return m_richloc
->get_loc ();
723 return UNKNOWN_LOCATION
;
726 // Default construct an output buffer.
728 output_buffer::output_buffer ()
729 : formatted_obstack (),
731 obstack (&formatted_obstack
),
738 obstack_init (&formatted_obstack
);
739 obstack_init (&chunk_obstack
);
742 // Release resources owned by an output buffer at the end of lifetime.
744 output_buffer::~output_buffer ()
746 obstack_free (&chunk_obstack
, NULL
);
747 obstack_free (&formatted_obstack
, NULL
);
751 /* Format an integer given by va_arg (ARG, type-specifier T) where
752 type-specifier is a precision modifier as indicated by PREC. F is
753 a string used to construct the appropriate format-specifier. */
754 #define pp_integer_with_precision(PP, ARG, PREC, T, F) \
759 pp_scalar (PP, "%" F, va_arg (ARG, T)); \
763 pp_scalar (PP, "%l" F, va_arg (ARG, long T)); \
767 pp_scalar (PP, "%" HOST_LONG_LONG_FORMAT F, va_arg (ARG, long long T)); \
776 /* Subroutine of pp_set_maximum_length. Set up PRETTY-PRINTER's
777 internal maximum characters per line. */
779 pp_set_real_maximum_length (pretty_printer
*pp
)
781 /* If we're told not to wrap lines then do the obvious thing. In case
782 we'll emit prefix only once per message, it is appropriate
783 not to increase unnecessarily the line-length cut-off. */
784 if (!pp_is_wrapping_line (pp
)
785 || pp_prefixing_rule (pp
) == DIAGNOSTICS_SHOW_PREFIX_ONCE
786 || pp_prefixing_rule (pp
) == DIAGNOSTICS_SHOW_PREFIX_NEVER
)
787 pp
->maximum_length
= pp_line_cutoff (pp
);
790 int prefix_length
= pp
->prefix
? strlen (pp
->prefix
) : 0;
791 /* If the prefix is ridiculously too long, output at least
793 if (pp_line_cutoff (pp
) - prefix_length
< 32)
794 pp
->maximum_length
= pp_line_cutoff (pp
) + 32;
796 pp
->maximum_length
= pp_line_cutoff (pp
);
800 /* Clear PRETTY-PRINTER's output state. */
802 pp_clear_state (pretty_printer
*pp
)
804 pp
->emitted_prefix
= false;
805 pp_indentation (pp
) = 0;
808 /* Print X to PP in decimal. */
809 template<unsigned int N
, typename T
>
811 pp_wide_integer (pretty_printer
*pp
, const poly_int_pod
<N
, T
> &x
)
813 if (x
.is_constant ())
814 pp_wide_integer (pp
, x
.coeffs
[0]);
817 pp_left_bracket (pp
);
818 for (unsigned int i
= 0; i
< N
; ++i
)
822 pp_wide_integer (pp
, x
.coeffs
[i
]);
824 pp_right_bracket (pp
);
828 template void pp_wide_integer (pretty_printer
*, const poly_uint16_pod
&);
829 template void pp_wide_integer (pretty_printer
*, const poly_int64_pod
&);
830 template void pp_wide_integer (pretty_printer
*, const poly_uint64_pod
&);
832 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream. */
834 pp_write_text_to_stream (pretty_printer
*pp
)
836 const char *text
= pp_formatted_text (pp
);
838 mingw_ansi_fputs (text
, pp_buffer (pp
)->stream
);
840 fputs (text
, pp_buffer (pp
)->stream
);
842 pp_clear_output_area (pp
);
845 /* As pp_write_text_to_stream, but for GraphViz label output.
847 Flush the formatted text of pretty-printer PP onto the attached stream.
848 Replace characters in PPF that have special meaning in a GraphViz .dot
851 This routine is not very fast, but it doesn't have to be as this is only
852 be used by routines dumping intermediate representations in graph form. */
855 pp_write_text_as_dot_label_to_stream (pretty_printer
*pp
, bool for_record
)
857 const char *text
= pp_formatted_text (pp
);
858 const char *p
= text
;
859 FILE *fp
= pp_buffer (pp
)->stream
;
866 /* Print newlines as a left-aligned newline. */
872 /* The following characters are only special for record-shape nodes. */
879 escape_char
= for_record
;
882 /* The following characters always have to be escaped
883 for use in labels. */
885 /* There is a bug in some (f.i. 2.36.0) versions of graphiz
886 ( http://www.graphviz.org/mantisbt/view.php?id=2524 ) related to
887 backslash as last char in label. Let's avoid triggering it. */
888 gcc_assert (*(p
+ 1) != '\0');
905 pp_clear_output_area (pp
);
908 /* Wrap a text delimited by START and END into PRETTY-PRINTER. */
910 pp_wrap_text (pretty_printer
*pp
, const char *start
, const char *end
)
912 bool wrapping_line
= pp_is_wrapping_line (pp
);
916 /* Dump anything bordered by whitespaces. */
918 const char *p
= start
;
919 while (p
!= end
&& !ISBLANK (*p
) && *p
!= '\n')
922 && p
- start
>= pp_remaining_character_count_for_line (pp
))
924 pp_append_text (pp
, start
, p
);
928 if (start
!= end
&& ISBLANK (*start
))
933 if (start
!= end
&& *start
== '\n')
941 /* Same as pp_wrap_text but wrap text only when in line-wrapping mode. */
943 pp_maybe_wrap_text (pretty_printer
*pp
, const char *start
, const char *end
)
945 if (pp_is_wrapping_line (pp
))
946 pp_wrap_text (pp
, start
, end
);
948 pp_append_text (pp
, start
, end
);
951 /* Append to the output area of PRETTY-PRINTER a string specified by its
952 STARTing character and LENGTH. */
954 pp_append_r (pretty_printer
*pp
, const char *start
, int length
)
956 output_buffer_append_r (pp_buffer (pp
), start
, length
);
959 /* Insert enough spaces into the output area of PRETTY-PRINTER to bring
960 the column position to the current indentation level, assuming that a
961 newline has just been written to the buffer. */
963 pp_indent (pretty_printer
*pp
)
965 int n
= pp_indentation (pp
);
968 for (i
= 0; i
< n
; ++i
)
972 /* The following format specifiers are recognized as being client independent:
973 %d, %i: (signed) integer in base ten.
974 %u: unsigned integer in base ten.
975 %o: unsigned integer in base eight.
976 %x: unsigned integer in base sixteen.
977 %ld, %li, %lo, %lu, %lx: long versions of the above.
978 %lld, %lli, %llo, %llu, %llx: long long versions.
979 %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
983 %p: pointer (printed in a host-dependent manner).
984 %r: if pp_show_color(pp), switch to color identified by const char *.
985 %R: if pp_show_color(pp), reset color.
986 %m: strerror(text->err_no) - does not consume a value from args_ptr.
990 %': apostrophe (should only be used in untranslated messages;
991 translations should use appropriate punctuation directly).
992 %.*s: a substring the length of which is specified by an argument
994 %Ns: likewise, but length specified as constant in the format string.
995 Flag 'q': quote formatted text (must come immediately after '%').
996 %Z: Requires two arguments - array of int, and len. Prints elements
999 Arguments can be used sequentially, or through %N$ resp. *N$
1000 notation Nth argument after the format string. If %N$ / *N$
1001 notation is used, it must be used for all arguments, except %m, %%,
1002 %<, %> and %', which may not have a number, as they do not consume
1003 an argument. When %M$.*N$s is used, M must be N + 1. (This may
1004 also be written %M$.*s, provided N is not otherwise used.) The
1005 format string must have conversion specifiers with argument numbers
1006 1 up to highest argument; each argument may only be used once.
1007 A format string can have at most 30 arguments. */
1009 /* Formatting phases 1 and 2: render TEXT->format_spec plus
1010 TEXT->args_ptr into a series of chunks in pp_buffer (PP)->args[].
1011 Phase 3 is in pp_output_formatted_text. */
1014 pp_format (pretty_printer
*pp
, text_info
*text
)
1016 output_buffer
*buffer
= pp_buffer (pp
);
1019 struct chunk_info
*new_chunk_array
;
1021 unsigned int curarg
= 0, chunk
= 0, argno
;
1022 pp_wrapping_mode_t old_wrapping_mode
;
1023 bool any_unnumbered
= false, any_numbered
= false;
1024 const char **formatters
[PP_NL_ARGMAX
];
1026 /* Allocate a new chunk structure. */
1027 new_chunk_array
= XOBNEW (&buffer
->chunk_obstack
, struct chunk_info
);
1028 new_chunk_array
->prev
= buffer
->cur_chunk_array
;
1029 buffer
->cur_chunk_array
= new_chunk_array
;
1030 args
= new_chunk_array
->args
;
1032 /* Formatting phase 1: split up TEXT->format_spec into chunks in
1033 pp_buffer (PP)->args[]. Even-numbered chunks are to be output
1034 verbatim, odd-numbered chunks are format specifiers.
1035 %m, %%, %<, %>, and %' are replaced with the appropriate text at
1038 memset (formatters
, 0, sizeof formatters
);
1040 for (p
= text
->format_spec
; *p
; )
1042 while (*p
!= '\0' && *p
!= '%')
1044 obstack_1grow (&buffer
->chunk_obstack
, *p
);
1057 obstack_1grow (&buffer
->chunk_obstack
, '%');
1063 obstack_grow (&buffer
->chunk_obstack
,
1064 open_quote
, strlen (open_quote
));
1065 const char *colorstr
1066 = colorize_start (pp_show_color (pp
), "quote");
1067 obstack_grow (&buffer
->chunk_obstack
, colorstr
, strlen (colorstr
));
1074 const char *colorstr
= colorize_stop (pp_show_color (pp
));
1075 obstack_grow (&buffer
->chunk_obstack
, colorstr
, strlen (colorstr
));
1079 obstack_grow (&buffer
->chunk_obstack
,
1080 close_quote
, strlen (close_quote
));
1086 const char *colorstr
= colorize_stop (pp_show_color (pp
));
1087 obstack_grow (&buffer
->chunk_obstack
, colorstr
,
1095 const char *errstr
= xstrerror (text
->err_no
);
1096 obstack_grow (&buffer
->chunk_obstack
, errstr
, strlen (errstr
));
1102 /* Handled in phase 2. Terminate the plain chunk here. */
1103 obstack_1grow (&buffer
->chunk_obstack
, '\0');
1104 gcc_assert (chunk
< PP_NL_ARGMAX
* 2);
1105 args
[chunk
++] = XOBFINISH (&buffer
->chunk_obstack
, const char *);
1112 argno
= strtoul (p
, &end
, 10) - 1;
1114 gcc_assert (*p
== '$');
1117 any_numbered
= true;
1118 gcc_assert (!any_unnumbered
);
1123 any_unnumbered
= true;
1124 gcc_assert (!any_numbered
);
1126 gcc_assert (argno
< PP_NL_ARGMAX
);
1127 gcc_assert (!formatters
[argno
]);
1128 formatters
[argno
] = &args
[chunk
];
1131 obstack_1grow (&buffer
->chunk_obstack
, *p
);
1134 while (strchr ("qwl+#", p
[-1]));
1138 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
1139 (where M == N + 1). */
1144 obstack_1grow (&buffer
->chunk_obstack
, *p
);
1147 while (ISDIGIT (p
[-1]));
1148 gcc_assert (p
[-1] == 's');
1152 gcc_assert (*p
== '*');
1153 obstack_1grow (&buffer
->chunk_obstack
, '*');
1159 unsigned int argno2
= strtoul (p
, &end
, 10) - 1;
1161 gcc_assert (argno2
== argno
- 1);
1162 gcc_assert (!any_unnumbered
);
1163 gcc_assert (*p
== '$');
1166 formatters
[argno2
] = formatters
[argno
];
1170 gcc_assert (!any_numbered
);
1171 formatters
[argno
+1] = formatters
[argno
];
1174 gcc_assert (*p
== 's');
1175 obstack_1grow (&buffer
->chunk_obstack
, 's');
1182 obstack_1grow (&buffer
->chunk_obstack
, '\0');
1183 gcc_assert (chunk
< PP_NL_ARGMAX
* 2);
1184 args
[chunk
++] = XOBFINISH (&buffer
->chunk_obstack
, const char *);
1187 obstack_1grow (&buffer
->chunk_obstack
, '\0');
1188 gcc_assert (chunk
< PP_NL_ARGMAX
* 2);
1189 args
[chunk
++] = XOBFINISH (&buffer
->chunk_obstack
, const char *);
1192 /* Set output to the argument obstack, and switch line-wrapping and
1194 buffer
->obstack
= &buffer
->chunk_obstack
;
1195 old_wrapping_mode
= pp_set_verbatim_wrapping (pp
);
1197 /* Second phase. Replace each formatter with the formatted text it
1200 for (argno
= 0; formatters
[argno
]; argno
++)
1208 /* We do not attempt to enforce any ordering on the modifier
1211 for (p
= *formatters
[argno
];; p
++)
1216 gcc_assert (!quote
);
1236 /* We don't support precision beyond that of "long long". */
1237 gcc_assert (precision
< 2);
1244 gcc_assert (!wide
|| precision
== 0);
1247 pp_begin_quote (pp
, pp_show_color (pp
));
1252 pp_string (pp
, colorize_start (pp_show_color (pp
),
1253 va_arg (*text
->args_ptr
,
1259 /* When quoting, print alphanumeric, punctuation, and the space
1260 character unchanged, and all others in hexadecimal with the
1261 "\x" prefix. Otherwise print them all unchanged. */
1262 int chr
= va_arg (*text
->args_ptr
, int);
1263 if (ISPRINT (chr
) || !quote
)
1264 pp_character (pp
, chr
);
1267 const char str
[2] = { chr
, '\0' };
1268 pp_quoted_string (pp
, str
, 1);
1276 pp_wide_integer (pp
, va_arg (*text
->args_ptr
, HOST_WIDE_INT
));
1278 pp_integer_with_precision
1279 (pp
, *text
->args_ptr
, precision
, int, "d");
1284 pp_scalar (pp
, "%" HOST_WIDE_INT_PRINT
"o",
1285 va_arg (*text
->args_ptr
, unsigned HOST_WIDE_INT
));
1287 pp_integer_with_precision
1288 (pp
, *text
->args_ptr
, precision
, unsigned, "o");
1293 pp_quoted_string (pp
, va_arg (*text
->args_ptr
, const char *));
1295 pp_string (pp
, va_arg (*text
->args_ptr
, const char *));
1299 pp_pointer (pp
, va_arg (*text
->args_ptr
, void *));
1304 pp_scalar (pp
, HOST_WIDE_INT_PRINT_UNSIGNED
,
1305 va_arg (*text
->args_ptr
, unsigned HOST_WIDE_INT
));
1307 pp_integer_with_precision
1308 (pp
, *text
->args_ptr
, precision
, unsigned, "u");
1312 pp_double (pp
, va_arg (*text
->args_ptr
, double));
1317 int *v
= va_arg (*text
->args_ptr
, int *);
1318 unsigned len
= va_arg (*text
->args_ptr
, unsigned);
1320 for (unsigned i
= 0; i
< len
; ++i
)
1322 pp_scalar (pp
, "%i", v
[i
]);
1334 pp_scalar (pp
, HOST_WIDE_INT_PRINT_HEX
,
1335 va_arg (*text
->args_ptr
, unsigned HOST_WIDE_INT
));
1337 pp_integer_with_precision
1338 (pp
, *text
->args_ptr
, precision
, unsigned, "x");
1346 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
1347 (where M == N + 1). The format string should be verified
1348 already from the first phase. */
1353 n
= strtoul (p
, &end
, 10);
1355 gcc_assert (*p
== 's');
1359 gcc_assert (*p
== '*');
1361 gcc_assert (*p
== 's');
1362 n
= va_arg (*text
->args_ptr
, int);
1364 /* This consumes a second entry in the formatters array. */
1365 gcc_assert (formatters
[argno
] == formatters
[argno
+1]);
1369 s
= va_arg (*text
->args_ptr
, const char *);
1371 /* Append the lesser of precision and strlen (s) characters
1372 from the array (which need not be a nul-terminated string).
1373 Negative precision is treated as if it were omitted. */
1374 size_t len
= n
< 0 ? strlen (s
) : strnlen (s
, n
);
1376 pp_append_text (pp
, s
, s
+ len
);
1384 /* Call the format decoder.
1385 Pass the address of "quote" so that format decoders can
1386 potentially disable printing of the closing quote
1387 (e.g. when printing "'TYPEDEF' aka 'TYPE'" in the C family
1389 gcc_assert (pp_format_decoder (pp
));
1390 ok
= pp_format_decoder (pp
) (pp
, text
, p
,
1391 precision
, wide
, plus
, hash
, "e
,
1398 pp_end_quote (pp
, pp_show_color (pp
));
1400 obstack_1grow (&buffer
->chunk_obstack
, '\0');
1401 *formatters
[argno
] = XOBFINISH (&buffer
->chunk_obstack
, const char *);
1405 for (; argno
< PP_NL_ARGMAX
; argno
++)
1406 gcc_assert (!formatters
[argno
]);
1408 /* If the client supplied a postprocessing object, call its "handle"
1410 if (pp
->m_format_postprocessor
)
1411 pp
->m_format_postprocessor
->handle (pp
);
1413 /* Revert to normal obstack and wrapping mode. */
1414 buffer
->obstack
= &buffer
->formatted_obstack
;
1415 buffer
->line_length
= 0;
1416 pp_wrapping_mode (pp
) = old_wrapping_mode
;
1417 pp_clear_state (pp
);
1420 /* Format of a message pointed to by TEXT. */
1422 pp_output_formatted_text (pretty_printer
*pp
)
1425 output_buffer
*buffer
= pp_buffer (pp
);
1426 struct chunk_info
*chunk_array
= buffer
->cur_chunk_array
;
1427 const char **args
= chunk_array
->args
;
1429 gcc_assert (buffer
->obstack
== &buffer
->formatted_obstack
);
1430 gcc_assert (buffer
->line_length
== 0);
1432 /* This is a third phase, first 2 phases done in pp_format_args.
1433 Now we actually print it. */
1434 for (chunk
= 0; args
[chunk
]; chunk
++)
1435 pp_string (pp
, args
[chunk
]);
1437 /* Deallocate the chunk structure and everything after it (i.e. the
1438 associated series of formatted strings). */
1439 buffer
->cur_chunk_array
= chunk_array
->prev
;
1440 obstack_free (&buffer
->chunk_obstack
, chunk_array
);
1443 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
1444 settings needed by BUFFER for a verbatim formatting. */
1446 pp_format_verbatim (pretty_printer
*pp
, text_info
*text
)
1448 /* Set verbatim mode. */
1449 pp_wrapping_mode_t oldmode
= pp_set_verbatim_wrapping (pp
);
1451 /* Do the actual formatting. */
1452 pp_format (pp
, text
);
1453 pp_output_formatted_text (pp
);
1455 /* Restore previous settings. */
1456 pp_wrapping_mode (pp
) = oldmode
;
1459 /* Flush the content of BUFFER onto the attached stream. This
1460 function does nothing unless pp->output_buffer->flush_p. */
1462 pp_flush (pretty_printer
*pp
)
1464 pp_clear_state (pp
);
1465 if (!pp
->buffer
->flush_p
)
1467 pp_write_text_to_stream (pp
);
1468 fflush (pp_buffer (pp
)->stream
);
1471 /* Flush the content of BUFFER onto the attached stream independently
1472 of the value of pp->output_buffer->flush_p. */
1474 pp_really_flush (pretty_printer
*pp
)
1476 pp_clear_state (pp
);
1477 pp_write_text_to_stream (pp
);
1478 fflush (pp_buffer (pp
)->stream
);
1481 /* Sets the number of maximum characters per line PRETTY-PRINTER can
1482 output in line-wrapping mode. A LENGTH value 0 suppresses
1485 pp_set_line_maximum_length (pretty_printer
*pp
, int length
)
1487 pp_line_cutoff (pp
) = length
;
1488 pp_set_real_maximum_length (pp
);
1491 /* Clear PRETTY-PRINTER output area text info. */
1493 pp_clear_output_area (pretty_printer
*pp
)
1495 obstack_free (pp_buffer (pp
)->obstack
,
1496 obstack_base (pp_buffer (pp
)->obstack
));
1497 pp_buffer (pp
)->line_length
= 0;
1500 /* Set PREFIX for PRETTY-PRINTER, taking ownership of PREFIX, which
1501 will eventually be free-ed. */
1504 pp_set_prefix (pretty_printer
*pp
, char *prefix
)
1507 pp
->prefix
= prefix
;
1508 pp_set_real_maximum_length (pp
);
1509 pp
->emitted_prefix
= false;
1510 pp_indentation (pp
) = 0;
1513 /* Take ownership of PP's prefix, setting it to NULL.
1514 This allows clients to save, overide, and then restore an existing
1515 prefix, without it being free-ed. */
1518 pp_take_prefix (pretty_printer
*pp
)
1520 char *result
= pp
->prefix
;
1525 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
1527 pp_destroy_prefix (pretty_printer
*pp
)
1529 if (pp
->prefix
!= NULL
)
1536 /* Write out PRETTY-PRINTER's prefix. */
1538 pp_emit_prefix (pretty_printer
*pp
)
1540 if (pp
->prefix
!= NULL
)
1542 switch (pp_prefixing_rule (pp
))
1545 case DIAGNOSTICS_SHOW_PREFIX_NEVER
:
1548 case DIAGNOSTICS_SHOW_PREFIX_ONCE
:
1549 if (pp
->emitted_prefix
)
1554 pp_indentation (pp
) += 3;
1557 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
:
1559 int prefix_length
= strlen (pp
->prefix
);
1560 pp_append_r (pp
, pp
->prefix
, prefix_length
);
1561 pp
->emitted_prefix
= true;
1568 /* Construct a PRETTY-PRINTER of MAXIMUM_LENGTH characters per line. */
1570 pretty_printer::pretty_printer (int maximum_length
)
1571 : buffer (new (XCNEW (output_buffer
)) output_buffer ()),
1578 m_format_postprocessor (NULL
),
1581 translate_identifiers (true),
1584 pp_line_cutoff (this) = maximum_length
;
1585 /* By default, we emit prefixes once per message. */
1586 pp_prefixing_rule (this) = DIAGNOSTICS_SHOW_PREFIX_ONCE
;
1587 pp_set_prefix (this, NULL
);
1590 pretty_printer::~pretty_printer ()
1592 if (m_format_postprocessor
)
1593 delete m_format_postprocessor
;
1594 buffer
->~output_buffer ();
1599 /* Append a string delimited by START and END to the output area of
1600 PRETTY-PRINTER. No line wrapping is done. However, if beginning a
1601 new line then emit PRETTY-PRINTER's prefix and skip any leading
1602 whitespace if appropriate. The caller must ensure that it is
1605 pp_append_text (pretty_printer
*pp
, const char *start
, const char *end
)
1607 /* Emit prefix and skip whitespace if we're starting a new line. */
1608 if (pp_buffer (pp
)->line_length
== 0)
1610 pp_emit_prefix (pp
);
1611 if (pp_is_wrapping_line (pp
))
1612 while (start
!= end
&& *start
== ' ')
1615 pp_append_r (pp
, start
, end
- start
);
1618 /* Finishes constructing a NULL-terminated character string representing
1619 the PRETTY-PRINTED text. */
1621 pp_formatted_text (pretty_printer
*pp
)
1623 return output_buffer_formatted_text (pp_buffer (pp
));
1626 /* Return a pointer to the last character emitted in PRETTY-PRINTER's
1627 output area. A NULL pointer means no character available. */
1629 pp_last_position_in_text (const pretty_printer
*pp
)
1631 return output_buffer_last_position_in_text (pp_buffer (pp
));
1634 /* Return the amount of characters PRETTY-PRINTER can accept to
1635 make a full line. Meaningful only in line-wrapping mode. */
1637 pp_remaining_character_count_for_line (pretty_printer
*pp
)
1639 return pp
->maximum_length
- pp_buffer (pp
)->line_length
;
1643 /* Format a message into BUFFER a la printf. */
1645 pp_printf (pretty_printer
*pp
, const char *msg
, ...)
1651 text
.err_no
= errno
;
1652 text
.args_ptr
= &ap
;
1653 text
.format_spec
= msg
;
1654 pp_format (pp
, &text
);
1655 pp_output_formatted_text (pp
);
1660 /* Output MESSAGE verbatim into BUFFER. */
1662 pp_verbatim (pretty_printer
*pp
, const char *msg
, ...)
1668 text
.err_no
= errno
;
1669 text
.args_ptr
= &ap
;
1670 text
.format_spec
= msg
;
1671 pp_format_verbatim (pp
, &text
);
1677 /* Have PRETTY-PRINTER start a new line. */
1679 pp_newline (pretty_printer
*pp
)
1681 obstack_1grow (pp_buffer (pp
)->obstack
, '\n');
1682 pp_needs_newline (pp
) = false;
1683 pp_buffer (pp
)->line_length
= 0;
1686 /* Have PRETTY-PRINTER add a CHARACTER. */
1688 pp_character (pretty_printer
*pp
, int c
)
1690 if (pp_is_wrapping_line (pp
)
1691 && pp_remaining_character_count_for_line (pp
) <= 0)
1697 obstack_1grow (pp_buffer (pp
)->obstack
, c
);
1698 ++pp_buffer (pp
)->line_length
;
1701 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
1702 be line-wrapped if in appropriate mode. */
1704 pp_string (pretty_printer
*pp
, const char *str
)
1706 gcc_checking_assert (str
);
1707 pp_maybe_wrap_text (pp
, str
, str
+ strlen (str
));
1710 /* Append the leading N characters of STRING to the output area of
1711 PRETTY-PRINTER, quoting in hexadecimal non-printable characters.
1712 Setting N = -1 is as if N were set to strlen (STRING). The STRING
1713 may be line-wrapped if in appropriate mode. */
1715 pp_quoted_string (pretty_printer
*pp
, const char *str
, size_t n
/* = -1 */)
1717 gcc_checking_assert (str
);
1719 const char *last
= str
;
1722 /* Compute the length if not specified. */
1723 if (n
== (size_t) -1)
1726 for (ps
= str
; n
; ++ps
, --n
)
1732 pp_maybe_wrap_text (pp
, last
, ps
- 1);
1734 /* Append the hexadecimal value of the character. Allocate a buffer
1735 that's large enough for a 32-bit char plus the hex prefix. */
1737 int n
= sprintf (buf
, "\\x%02x", (unsigned char)*ps
);
1738 pp_maybe_wrap_text (pp
, buf
, buf
+ n
);
1742 pp_maybe_wrap_text (pp
, last
, ps
);
1745 /* Maybe print out a whitespace if needed. */
1748 pp_maybe_space (pretty_printer
*pp
)
1750 if (pp
->padding
!= pp_none
)
1753 pp
->padding
= pp_none
;
1757 // Add a newline to the pretty printer PP and flush formatted text.
1760 pp_newline_and_flush (pretty_printer
*pp
)
1764 pp_needs_newline (pp
) = false;
1767 // Add a newline to the pretty printer PP, followed by indentation.
1770 pp_newline_and_indent (pretty_printer
*pp
, int n
)
1772 pp_indentation (pp
) += n
;
1775 pp_needs_newline (pp
) = false;
1778 // Add separator C, followed by a single whitespace.
1781 pp_separate_with (pretty_printer
*pp
, char c
)
1783 pp_character (pp
, c
);
1787 /* Add a localized open quote, and if SHOW_COLOR is true, begin colorizing
1788 using the "quote" color. */
1791 pp_begin_quote (pretty_printer
*pp
, bool show_color
)
1793 pp_string (pp
, open_quote
);
1794 pp_string (pp
, colorize_start (show_color
, "quote"));
1797 /* If SHOW_COLOR is true, stop colorizing.
1798 Add a localized close quote. */
1801 pp_end_quote (pretty_printer
*pp
, bool show_color
)
1803 pp_string (pp
, colorize_stop (show_color
));
1804 pp_string (pp
, close_quote
);
1808 /* The string starting at P has LEN (at least 1) bytes left; if they
1809 start with a valid UTF-8 sequence, return the length of that
1810 sequence and set *VALUE to the value of that sequence, and
1811 otherwise return 0 and set *VALUE to (unsigned int) -1. */
1814 decode_utf8_char (const unsigned char *p
, size_t len
, unsigned int *value
)
1816 unsigned int t
= *p
;
1822 size_t utf8_len
= 0;
1825 for (t
= *p
; t
& 0x80; t
<<= 1)
1828 if (utf8_len
> len
|| utf8_len
< 2 || utf8_len
> 6)
1830 *value
= (unsigned int) -1;
1833 ch
= *p
& ((1 << (7 - utf8_len
)) - 1);
1834 for (i
= 1; i
< utf8_len
; i
++)
1836 unsigned int u
= p
[i
];
1837 if ((u
& 0xC0) != 0x80)
1839 *value
= (unsigned int) -1;
1842 ch
= (ch
<< 6) | (u
& 0x3F);
1844 if ( (ch
<= 0x7F && utf8_len
> 1)
1845 || (ch
<= 0x7FF && utf8_len
> 2)
1846 || (ch
<= 0xFFFF && utf8_len
> 3)
1847 || (ch
<= 0x1FFFFF && utf8_len
> 4)
1848 || (ch
<= 0x3FFFFFF && utf8_len
> 5)
1849 || (ch
>= 0xD800 && ch
<= 0xDFFF))
1851 *value
= (unsigned int) -1;
1864 /* Allocator for identifier_to_locale and corresponding function to
1867 void *(*identifier_to_locale_alloc
) (size_t) = xmalloc
;
1868 void (*identifier_to_locale_free
) (void *) = free
;
1870 /* Given IDENT, an identifier in the internal encoding, return a
1871 version of IDENT suitable for diagnostics in the locale character
1872 set: either IDENT itself, or a string, allocated using
1873 identifier_to_locale_alloc, converted to the locale character set
1874 and using escape sequences if not representable in the locale
1875 character set or containing control characters or invalid byte
1876 sequences. Existing backslashes in IDENT are not doubled, so the
1877 result may not uniquely specify the contents of an arbitrary byte
1878 sequence identifier. */
1881 identifier_to_locale (const char *ident
)
1883 const unsigned char *uid
= (const unsigned char *) ident
;
1884 size_t idlen
= strlen (ident
);
1885 bool valid_printable_utf8
= true;
1886 bool all_ascii
= true;
1889 for (i
= 0; i
< idlen
;)
1892 size_t utf8_len
= decode_utf8_char (&uid
[i
], idlen
- i
, &c
);
1893 if (utf8_len
== 0 || c
<= 0x1F || (c
>= 0x7F && c
<= 0x9F))
1895 valid_printable_utf8
= false;
1903 /* If IDENT contains invalid UTF-8 sequences (which may occur with
1904 attributes putting arbitrary byte sequences in identifiers), or
1905 control characters, we use octal escape sequences for all bytes
1906 outside printable ASCII. */
1907 if (!valid_printable_utf8
)
1909 char *ret
= (char *) identifier_to_locale_alloc (4 * idlen
+ 1);
1911 for (i
= 0; i
< idlen
; i
++)
1913 if (uid
[i
] > 0x1F && uid
[i
] < 0x7F)
1917 sprintf (p
, "\\%03o", uid
[i
]);
1925 /* Otherwise, if it is valid printable ASCII, or printable UTF-8
1926 with the locale character set being UTF-8, IDENT is used. */
1927 if (all_ascii
|| locale_utf8
)
1930 /* Otherwise IDENT is converted to the locale character set if
1932 #if defined ENABLE_NLS && defined HAVE_LANGINFO_CODESET && HAVE_ICONV
1933 if (locale_encoding
!= NULL
)
1935 iconv_t cd
= iconv_open (locale_encoding
, "UTF-8");
1936 bool conversion_ok
= true;
1938 if (cd
!= (iconv_t
) -1)
1940 size_t ret_alloc
= 4 * idlen
+ 1;
1943 /* Repeat the whole conversion process as needed with
1944 larger buffers so non-reversible transformations can
1945 always be detected. */
1946 ICONV_CONST
char *inbuf
= CONST_CAST (char *, ident
);
1948 size_t inbytesleft
= idlen
;
1949 size_t outbytesleft
= ret_alloc
- 1;
1952 ret
= (char *) identifier_to_locale_alloc (ret_alloc
);
1955 if (iconv (cd
, 0, 0, 0, 0) == (size_t) -1)
1957 conversion_ok
= false;
1961 iconv_ret
= iconv (cd
, &inbuf
, &inbytesleft
,
1962 &outbuf
, &outbytesleft
);
1963 if (iconv_ret
== (size_t) -1 || inbytesleft
!= 0)
1968 identifier_to_locale_free (ret
);
1974 conversion_ok
= false;
1978 else if (iconv_ret
!= 0)
1980 conversion_ok
= false;
1983 /* Return to initial shift state. */
1984 if (iconv (cd
, 0, 0, &outbuf
, &outbytesleft
) == (size_t) -1)
1989 identifier_to_locale_free (ret
);
1995 conversion_ok
= false;
2009 /* Otherwise, convert non-ASCII characters in IDENT to UCNs. */
2011 char *ret
= (char *) identifier_to_locale_alloc (10 * idlen
+ 1);
2013 for (i
= 0; i
< idlen
;)
2016 size_t utf8_len
= decode_utf8_char (&uid
[i
], idlen
- i
, &c
);
2021 sprintf (p
, "\\U%08x", c
);
2033 namespace selftest
{
2035 /* Smoketest for pretty_printer. */
2038 test_basic_printing ()
2041 pp_string (&pp
, "hello");
2043 pp_string (&pp
, "world");
2045 ASSERT_STREQ ("hello world", pp_formatted_text (&pp
));
2048 /* Helper function for testing pp_format.
2049 Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
2050 prints EXPECTED, assuming that pp_show_color is SHOW_COLOR. */
2053 assert_pp_format_va (const location
&loc
, const char *expected
,
2054 bool show_color
, const char *fmt
, va_list *ap
)
2058 rich_location
rich_loc (line_table
, UNKNOWN_LOCATION
);
2060 ti
.format_spec
= fmt
;
2064 ti
.m_richloc
= &rich_loc
;
2066 pp_show_color (&pp
) = show_color
;
2067 pp_format (&pp
, &ti
);
2068 pp_output_formatted_text (&pp
);
2069 ASSERT_STREQ_AT (loc
, expected
, pp_formatted_text (&pp
));
2072 /* Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
2073 prints EXPECTED, with show_color disabled. */
2076 assert_pp_format (const location
&loc
, const char *expected
,
2077 const char *fmt
, ...)
2082 assert_pp_format_va (loc
, expected
, false, fmt
, &ap
);
2086 /* As above, but with colorization enabled. */
2089 assert_pp_format_colored (const location
&loc
, const char *expected
,
2090 const char *fmt
, ...)
2092 /* The tests of colorization assume the default color scheme.
2093 If GCC_COLORS is set, then the colors have potentially been
2094 overridden; skip the test. */
2095 if (getenv ("GCC_COLORS"))
2101 assert_pp_format_va (loc
, expected
, true, fmt
, &ap
);
2105 /* Helper function for calling testing pp_format,
2106 by calling assert_pp_format with various numbers of arguments.
2107 These exist mostly to avoid having to write SELFTEST_LOCATION
2108 throughout test_pp_format. */
2110 #define ASSERT_PP_FORMAT_1(EXPECTED, FMT, ARG1) \
2111 SELFTEST_BEGIN_STMT \
2112 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
2116 #define ASSERT_PP_FORMAT_2(EXPECTED, FMT, ARG1, ARG2) \
2117 SELFTEST_BEGIN_STMT \
2118 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
2122 #define ASSERT_PP_FORMAT_3(EXPECTED, FMT, ARG1, ARG2, ARG3) \
2123 SELFTEST_BEGIN_STMT \
2124 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
2125 (ARG1), (ARG2), (ARG3)); \
2128 /* Verify that pp_format works, for various format codes. */
2133 /* Avoid introducing locale-specific differences in the results
2134 by hardcoding open_quote and close_quote. */
2135 auto_fix_quotes fix_quotes
;
2137 /* Verify that plain text is passed through unchanged. */
2138 assert_pp_format (SELFTEST_LOCATION
, "unformatted", "unformatted");
2140 /* Verify various individual format codes, in the order listed in the
2141 comment for pp_format above. For each code, we append a second
2142 argument with a known bit pattern (0x12345678), to ensure that we
2143 are consuming arguments correctly. */
2144 ASSERT_PP_FORMAT_2 ("-27 12345678", "%d %x", -27, 0x12345678);
2145 ASSERT_PP_FORMAT_2 ("-5 12345678", "%i %x", -5, 0x12345678);
2146 ASSERT_PP_FORMAT_2 ("10 12345678", "%u %x", 10, 0x12345678);
2147 ASSERT_PP_FORMAT_2 ("17 12345678", "%o %x", 15, 0x12345678);
2148 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%x %x", 0xcafebabe, 0x12345678);
2149 ASSERT_PP_FORMAT_2 ("-27 12345678", "%ld %x", (long)-27, 0x12345678);
2150 ASSERT_PP_FORMAT_2 ("-5 12345678", "%li %x", (long)-5, 0x12345678);
2151 ASSERT_PP_FORMAT_2 ("10 12345678", "%lu %x", (long)10, 0x12345678);
2152 ASSERT_PP_FORMAT_2 ("17 12345678", "%lo %x", (long)15, 0x12345678);
2153 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%lx %x", (long)0xcafebabe,
2155 ASSERT_PP_FORMAT_2 ("-27 12345678", "%lld %x", (long long)-27, 0x12345678);
2156 ASSERT_PP_FORMAT_2 ("-5 12345678", "%lli %x", (long long)-5, 0x12345678);
2157 ASSERT_PP_FORMAT_2 ("10 12345678", "%llu %x", (long long)10, 0x12345678);
2158 ASSERT_PP_FORMAT_2 ("17 12345678", "%llo %x", (long long)15, 0x12345678);
2159 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%llx %x", (long long)0xcafebabe,
2161 ASSERT_PP_FORMAT_2 ("-27 12345678", "%wd %x", (HOST_WIDE_INT
)-27, 0x12345678);
2162 ASSERT_PP_FORMAT_2 ("-5 12345678", "%wi %x", (HOST_WIDE_INT
)-5, 0x12345678);
2163 ASSERT_PP_FORMAT_2 ("10 12345678", "%wu %x", (unsigned HOST_WIDE_INT
)10,
2165 ASSERT_PP_FORMAT_2 ("17 12345678", "%wo %x", (HOST_WIDE_INT
)15, 0x12345678);
2166 ASSERT_PP_FORMAT_2 ("0xcafebabe 12345678", "%wx %x", (HOST_WIDE_INT
)0xcafebabe,
2168 ASSERT_PP_FORMAT_2 ("1.000000 12345678", "%f %x", 1.0, 0x12345678);
2169 ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678);
2170 ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello world",
2173 /* Not nul-terminated. */
2174 char arr
[5] = { '1', '2', '3', '4', '5' };
2175 ASSERT_PP_FORMAT_3 ("123 12345678", "%.*s %x", 3, arr
, 0x12345678);
2176 ASSERT_PP_FORMAT_3 ("1234 12345678", "%.*s %x", -1, "1234", 0x12345678);
2177 ASSERT_PP_FORMAT_3 ("12345 12345678", "%.*s %x", 7, "12345", 0x12345678);
2179 /* We can't test for %p; the pointer is printed in an implementation-defined
2181 ASSERT_PP_FORMAT_2 ("normal colored normal 12345678",
2182 "normal %rcolored%R normal %x",
2183 "error", 0x12345678);
2184 assert_pp_format_colored
2186 "normal \33[01;31m\33[Kcolored\33[m\33[K normal 12345678",
2187 "normal %rcolored%R normal %x", "error", 0x12345678);
2189 %m: strerror(text->err_no) - does not consume a value from args_ptr. */
2190 ASSERT_PP_FORMAT_1 ("% 12345678", "%% %x", 0x12345678);
2191 ASSERT_PP_FORMAT_1 ("` 12345678", "%< %x", 0x12345678);
2192 ASSERT_PP_FORMAT_1 ("' 12345678", "%> %x", 0x12345678);
2193 ASSERT_PP_FORMAT_1 ("' 12345678", "%' %x", 0x12345678);
2194 ASSERT_PP_FORMAT_3 ("abc 12345678", "%.*s %x", 3, "abcdef", 0x12345678);
2195 ASSERT_PP_FORMAT_2 ("abc 12345678", "%.3s %x", "abcdef", 0x12345678);
2197 /* Verify flag 'q'. */
2198 ASSERT_PP_FORMAT_2 ("`foo' 12345678", "%qs %x", "foo", 0x12345678);
2199 assert_pp_format_colored (SELFTEST_LOCATION
,
2200 "`\33[01m\33[Kfoo\33[m\33[K' 12345678", "%qs %x",
2204 int v
[] = { 1, 2, 3 };
2205 ASSERT_PP_FORMAT_3 ("1, 2, 3 12345678", "%Z %x", v
, 3, 0x12345678);
2208 ASSERT_PP_FORMAT_3 ("0 12345678", "%Z %x", v2
, 1, 0x12345678);
2210 /* Verify that combinations work, along with unformatted text. */
2211 assert_pp_format (SELFTEST_LOCATION
,
2212 "the quick brown fox jumps over the lazy dog",
2213 "the %s %s %s jumps over the %s %s",
2214 "quick", "brown", "fox", "lazy", "dog");
2215 assert_pp_format (SELFTEST_LOCATION
, "item 3 of 7", "item %i of %i", 3, 7);
2216 assert_pp_format (SELFTEST_LOCATION
, "problem with `bar' at line 10",
2217 "problem with %qs at line %i", "bar", 10);
2220 /* A subclass of pretty_printer for use by test_prefixes_and_wrapping. */
2222 class test_pretty_printer
: public pretty_printer
2225 test_pretty_printer (enum diagnostic_prefixing_rule_t rule
,
2226 int max_line_length
)
2228 pp_set_prefix (this, xstrdup ("PREFIX: "));
2229 wrapping
.rule
= rule
;
2230 pp_set_line_maximum_length (this, max_line_length
);
2234 /* Verify that the various values of enum diagnostic_prefixing_rule_t work
2235 as expected, with and without line wrapping. */
2238 test_prefixes_and_wrapping ()
2240 /* Tests of the various prefixing rules, without wrapping.
2241 Newlines embedded in pp_string don't affect it; we have to
2242 explicitly call pp_newline. */
2244 test_pretty_printer
pp (DIAGNOSTICS_SHOW_PREFIX_ONCE
, 0);
2245 pp_string (&pp
, "the quick brown fox");
2247 pp_string (&pp
, "jumps over the lazy dog");
2249 ASSERT_STREQ (pp_formatted_text (&pp
),
2250 "PREFIX: the quick brown fox\n"
2251 " jumps over the lazy dog\n");
2254 test_pretty_printer
pp (DIAGNOSTICS_SHOW_PREFIX_NEVER
, 0);
2255 pp_string (&pp
, "the quick brown fox");
2257 pp_string (&pp
, "jumps over the lazy dog");
2259 ASSERT_STREQ (pp_formatted_text (&pp
),
2260 "the quick brown fox\n"
2261 "jumps over the lazy dog\n");
2264 test_pretty_printer
pp (DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
, 0);
2265 pp_string (&pp
, "the quick brown fox");
2267 pp_string (&pp
, "jumps over the lazy dog");
2269 ASSERT_STREQ (pp_formatted_text (&pp
),
2270 "PREFIX: the quick brown fox\n"
2271 "PREFIX: jumps over the lazy dog\n");
2274 /* Tests of the various prefixing rules, with wrapping. */
2276 test_pretty_printer
pp (DIAGNOSTICS_SHOW_PREFIX_ONCE
, 20);
2277 pp_string (&pp
, "the quick brown fox jumps over the lazy dog");
2279 pp_string (&pp
, "able was I ere I saw elba");
2281 ASSERT_STREQ (pp_formatted_text (&pp
),
2282 "PREFIX: the quick \n"
2283 " brown fox jumps \n"
2286 " able was I ere I \n"
2290 test_pretty_printer
pp (DIAGNOSTICS_SHOW_PREFIX_NEVER
, 20);
2291 pp_string (&pp
, "the quick brown fox jumps over the lazy dog");
2293 pp_string (&pp
, "able was I ere I saw elba");
2295 ASSERT_STREQ (pp_formatted_text (&pp
),
2296 "the quick brown fox \n"
2297 "jumps over the lazy \n"
2299 "able was I ere I \n"
2303 test_pretty_printer
pp (DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
, 20);
2304 pp_string (&pp
, "the quick brown fox jumps over the lazy dog");
2306 pp_string (&pp
, "able was I ere I saw elba");
2308 ASSERT_STREQ (pp_formatted_text (&pp
),
2309 "PREFIX: the quick brown fox jumps over the lazy dog\n"
2310 "PREFIX: able was I ere I saw elba\n");
2315 /* Run all of the selftests within this file. */
2318 pretty_print_c_tests ()
2320 test_basic_printing ();
2322 test_prefixes_and_wrapping ();
2325 } // namespace selftest
2327 #endif /* CHECKING_P */