1 /* Various declarations for language-independent pretty-print subroutines.
2 Copyright (C) 2003-2016 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
25 #include "pretty-print.h"
26 #include "diagnostic-color.h"
33 /* Overwrite the given location/range within this text_info's rich_location.
34 For use e.g. when implementing "+" in client format decoders. */
37 text_info::set_location (unsigned int idx
, location_t loc
, bool show_caret_p
)
39 gcc_checking_assert (m_richloc
);
40 m_richloc
->set_range (line_table
, idx
, loc
, show_caret_p
);
44 text_info::get_location (unsigned int index_of_location
) const
46 gcc_checking_assert (m_richloc
);
48 if (index_of_location
== 0)
49 return m_richloc
->get_loc ();
51 return UNKNOWN_LOCATION
;
54 // Default construct an output buffer.
56 output_buffer::output_buffer ()
57 : formatted_obstack (),
59 obstack (&formatted_obstack
),
66 obstack_init (&formatted_obstack
);
67 obstack_init (&chunk_obstack
);
70 // Release resources owned by an output buffer at the end of lifetime.
72 output_buffer::~output_buffer ()
74 obstack_free (&chunk_obstack
, NULL
);
75 obstack_free (&formatted_obstack
, NULL
);
79 /* Format an integer given by va_arg (ARG, type-specifier T) where
80 type-specifier is a precision modifier as indicated by PREC. F is
81 a string used to construct the appropriate format-specifier. */
82 #define pp_integer_with_precision(PP, ARG, PREC, T, F) \
87 pp_scalar (PP, "%" F, va_arg (ARG, T)); \
91 pp_scalar (PP, "%l" F, va_arg (ARG, long T)); \
95 pp_scalar (PP, "%" HOST_LONG_LONG_FORMAT F, va_arg (ARG, long long T)); \
104 /* Subroutine of pp_set_maximum_length. Set up PRETTY-PRINTER's
105 internal maximum characters per line. */
107 pp_set_real_maximum_length (pretty_printer
*pp
)
109 /* If we're told not to wrap lines then do the obvious thing. In case
110 we'll emit prefix only once per message, it is appropriate
111 not to increase unnecessarily the line-length cut-off. */
112 if (!pp_is_wrapping_line (pp
)
113 || pp_prefixing_rule (pp
) == DIAGNOSTICS_SHOW_PREFIX_ONCE
114 || pp_prefixing_rule (pp
) == DIAGNOSTICS_SHOW_PREFIX_NEVER
)
115 pp
->maximum_length
= pp_line_cutoff (pp
);
118 int prefix_length
= pp
->prefix
? strlen (pp
->prefix
) : 0;
119 /* If the prefix is ridiculously too long, output at least
121 if (pp_line_cutoff (pp
) - prefix_length
< 32)
122 pp
->maximum_length
= pp_line_cutoff (pp
) + 32;
124 pp
->maximum_length
= pp_line_cutoff (pp
);
128 /* Clear PRETTY-PRINTER's output state. */
130 pp_clear_state (pretty_printer
*pp
)
132 pp
->emitted_prefix
= false;
133 pp_indentation (pp
) = 0;
136 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream. */
138 pp_write_text_to_stream (pretty_printer
*pp
)
140 const char *text
= pp_formatted_text (pp
);
141 fputs (text
, pp_buffer (pp
)->stream
);
142 pp_clear_output_area (pp
);
145 /* As pp_write_text_to_stream, but for GraphViz label output.
147 Flush the formatted text of pretty-printer PP onto the attached stream.
148 Replace characters in PPF that have special meaning in a GraphViz .dot
151 This routine is not very fast, but it doesn't have to be as this is only
152 be used by routines dumping intermediate representations in graph form. */
155 pp_write_text_as_dot_label_to_stream (pretty_printer
*pp
, bool for_record
)
157 const char *text
= pp_formatted_text (pp
);
158 const char *p
= text
;
159 FILE *fp
= pp_buffer (pp
)->stream
;
166 /* Print newlines as a left-aligned newline. */
172 /* The following characters are only special for record-shape nodes. */
179 escape_char
= for_record
;
182 /* The following characters always have to be escaped
183 for use in labels. */
185 /* There is a bug in some (f.i. 2.36.0) versions of graphiz
186 ( http://www.graphviz.org/mantisbt/view.php?id=2524 ) related to
187 backslash as last char in label. Let's avoid triggering it. */
188 gcc_assert (*(p
+ 1) != '\0');
205 pp_clear_output_area (pp
);
208 /* Wrap a text delimited by START and END into PRETTY-PRINTER. */
210 pp_wrap_text (pretty_printer
*pp
, const char *start
, const char *end
)
212 bool wrapping_line
= pp_is_wrapping_line (pp
);
216 /* Dump anything bordered by whitespaces. */
218 const char *p
= start
;
219 while (p
!= end
&& !ISBLANK (*p
) && *p
!= '\n')
222 && p
- start
>= pp_remaining_character_count_for_line (pp
))
224 pp_append_text (pp
, start
, p
);
228 if (start
!= end
&& ISBLANK (*start
))
233 if (start
!= end
&& *start
== '\n')
241 /* Same as pp_wrap_text but wrap text only when in line-wrapping mode. */
243 pp_maybe_wrap_text (pretty_printer
*pp
, const char *start
, const char *end
)
245 if (pp_is_wrapping_line (pp
))
246 pp_wrap_text (pp
, start
, end
);
248 pp_append_text (pp
, start
, end
);
251 /* Append to the output area of PRETTY-PRINTER a string specified by its
252 STARTing character and LENGTH. */
254 pp_append_r (pretty_printer
*pp
, const char *start
, int length
)
256 output_buffer_append_r (pp_buffer (pp
), start
, length
);
259 /* Insert enough spaces into the output area of PRETTY-PRINTER to bring
260 the column position to the current indentation level, assuming that a
261 newline has just been written to the buffer. */
263 pp_indent (pretty_printer
*pp
)
265 int n
= pp_indentation (pp
);
268 for (i
= 0; i
< n
; ++i
)
272 /* The following format specifiers are recognized as being client independent:
273 %d, %i: (signed) integer in base ten.
274 %u: unsigned integer in base ten.
275 %o: unsigned integer in base eight.
276 %x: unsigned integer in base sixteen.
277 %ld, %li, %lo, %lu, %lx: long versions of the above.
278 %lld, %lli, %llo, %llu, %llx: long long versions.
279 %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
282 %p: pointer (printed in a host-dependent manner).
283 %r: if pp_show_color(pp), switch to color identified by const char *.
284 %R: if pp_show_color(pp), reset color.
285 %m: strerror(text->err_no) - does not consume a value from args_ptr.
289 %': apostrophe (should only be used in untranslated messages;
290 translations should use appropriate punctuation directly).
291 %.*s: a substring the length of which is specified by an argument
293 %Ns: likewise, but length specified as constant in the format string.
294 Flag 'q': quote formatted text (must come immediately after '%').
296 Arguments can be used sequentially, or through %N$ resp. *N$
297 notation Nth argument after the format string. If %N$ / *N$
298 notation is used, it must be used for all arguments, except %m, %%,
299 %<, %> and %', which may not have a number, as they do not consume
300 an argument. When %M$.*N$s is used, M must be N + 1. (This may
301 also be written %M$.*s, provided N is not otherwise used.) The
302 format string must have conversion specifiers with argument numbers
303 1 up to highest argument; each argument may only be used once.
304 A format string can have at most 30 arguments. */
306 /* Formatting phases 1 and 2: render TEXT->format_spec plus
307 TEXT->args_ptr into a series of chunks in pp_buffer (PP)->args[].
308 Phase 3 is in pp_output_formatted_text. */
311 pp_format (pretty_printer
*pp
, text_info
*text
)
313 output_buffer
*buffer
= pp_buffer (pp
);
316 struct chunk_info
*new_chunk_array
;
318 unsigned int curarg
= 0, chunk
= 0, argno
;
319 pp_wrapping_mode_t old_wrapping_mode
;
320 bool any_unnumbered
= false, any_numbered
= false;
321 const char **formatters
[PP_NL_ARGMAX
];
323 /* Allocate a new chunk structure. */
324 new_chunk_array
= XOBNEW (&buffer
->chunk_obstack
, struct chunk_info
);
325 new_chunk_array
->prev
= buffer
->cur_chunk_array
;
326 buffer
->cur_chunk_array
= new_chunk_array
;
327 args
= new_chunk_array
->args
;
329 /* Formatting phase 1: split up TEXT->format_spec into chunks in
330 pp_buffer (PP)->args[]. Even-numbered chunks are to be output
331 verbatim, odd-numbered chunks are format specifiers.
332 %m, %%, %<, %>, and %' are replaced with the appropriate text at
335 memset (formatters
, 0, sizeof formatters
);
337 for (p
= text
->format_spec
; *p
; )
339 while (*p
!= '\0' && *p
!= '%')
341 obstack_1grow (&buffer
->chunk_obstack
, *p
);
354 obstack_1grow (&buffer
->chunk_obstack
, '%');
360 obstack_grow (&buffer
->chunk_obstack
,
361 open_quote
, strlen (open_quote
));
363 = colorize_start (pp_show_color (pp
), "quote");
364 obstack_grow (&buffer
->chunk_obstack
, colorstr
, strlen (colorstr
));
371 const char *colorstr
= colorize_stop (pp_show_color (pp
));
372 obstack_grow (&buffer
->chunk_obstack
, colorstr
, strlen (colorstr
));
376 obstack_grow (&buffer
->chunk_obstack
,
377 close_quote
, strlen (close_quote
));
383 const char *colorstr
= colorize_stop (pp_show_color (pp
));
384 obstack_grow (&buffer
->chunk_obstack
, colorstr
,
392 const char *errstr
= xstrerror (text
->err_no
);
393 obstack_grow (&buffer
->chunk_obstack
, errstr
, strlen (errstr
));
399 /* Handled in phase 2. Terminate the plain chunk here. */
400 obstack_1grow (&buffer
->chunk_obstack
, '\0');
401 gcc_assert (chunk
< PP_NL_ARGMAX
* 2);
402 args
[chunk
++] = XOBFINISH (&buffer
->chunk_obstack
, const char *);
409 argno
= strtoul (p
, &end
, 10) - 1;
411 gcc_assert (*p
== '$');
415 gcc_assert (!any_unnumbered
);
420 any_unnumbered
= true;
421 gcc_assert (!any_numbered
);
423 gcc_assert (argno
< PP_NL_ARGMAX
);
424 gcc_assert (!formatters
[argno
]);
425 formatters
[argno
] = &args
[chunk
];
428 obstack_1grow (&buffer
->chunk_obstack
, *p
);
431 while (strchr ("qwl+#", p
[-1]));
435 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
436 (where M == N + 1). */
441 obstack_1grow (&buffer
->chunk_obstack
, *p
);
444 while (ISDIGIT (p
[-1]));
445 gcc_assert (p
[-1] == 's');
449 gcc_assert (*p
== '*');
450 obstack_1grow (&buffer
->chunk_obstack
, '*');
456 unsigned int argno2
= strtoul (p
, &end
, 10) - 1;
458 gcc_assert (argno2
== argno
- 1);
459 gcc_assert (!any_unnumbered
);
460 gcc_assert (*p
== '$');
463 formatters
[argno2
] = formatters
[argno
];
467 gcc_assert (!any_numbered
);
468 formatters
[argno
+1] = formatters
[argno
];
471 gcc_assert (*p
== 's');
472 obstack_1grow (&buffer
->chunk_obstack
, 's');
479 obstack_1grow (&buffer
->chunk_obstack
, '\0');
480 gcc_assert (chunk
< PP_NL_ARGMAX
* 2);
481 args
[chunk
++] = XOBFINISH (&buffer
->chunk_obstack
, const char *);
484 obstack_1grow (&buffer
->chunk_obstack
, '\0');
485 gcc_assert (chunk
< PP_NL_ARGMAX
* 2);
486 args
[chunk
++] = XOBFINISH (&buffer
->chunk_obstack
, const char *);
489 /* Set output to the argument obstack, and switch line-wrapping and
491 buffer
->obstack
= &buffer
->chunk_obstack
;
492 old_wrapping_mode
= pp_set_verbatim_wrapping (pp
);
494 /* Second phase. Replace each formatter with the formatted text it
497 for (argno
= 0; formatters
[argno
]; argno
++)
505 /* We do not attempt to enforce any ordering on the modifier
508 for (p
= *formatters
[argno
];; p
++)
533 /* We don't support precision beyond that of "long long". */
534 gcc_assert (precision
< 2);
541 gcc_assert (!wide
|| precision
== 0);
545 pp_string (pp
, open_quote
);
546 pp_string (pp
, colorize_start (pp_show_color (pp
), "quote"));
552 pp_string (pp
, colorize_start (pp_show_color (pp
),
553 va_arg (*text
->args_ptr
,
558 pp_character (pp
, va_arg (*text
->args_ptr
, int));
564 pp_wide_integer (pp
, va_arg (*text
->args_ptr
, HOST_WIDE_INT
));
566 pp_integer_with_precision
567 (pp
, *text
->args_ptr
, precision
, int, "d");
572 pp_scalar (pp
, "%" HOST_WIDE_INT_PRINT
"o",
573 va_arg (*text
->args_ptr
, unsigned HOST_WIDE_INT
));
575 pp_integer_with_precision
576 (pp
, *text
->args_ptr
, precision
, unsigned, "o");
580 pp_string (pp
, va_arg (*text
->args_ptr
, const char *));
584 pp_pointer (pp
, va_arg (*text
->args_ptr
, void *));
589 pp_scalar (pp
, HOST_WIDE_INT_PRINT_UNSIGNED
,
590 va_arg (*text
->args_ptr
, unsigned HOST_WIDE_INT
));
592 pp_integer_with_precision
593 (pp
, *text
->args_ptr
, precision
, unsigned, "u");
598 pp_scalar (pp
, HOST_WIDE_INT_PRINT_HEX
,
599 va_arg (*text
->args_ptr
, unsigned HOST_WIDE_INT
));
601 pp_integer_with_precision
602 (pp
, *text
->args_ptr
, precision
, unsigned, "x");
610 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
611 (where M == N + 1). The format string should be verified
612 already from the first phase. */
617 n
= strtoul (p
, &end
, 10);
619 gcc_assert (*p
== 's');
623 gcc_assert (*p
== '*');
625 gcc_assert (*p
== 's');
626 n
= va_arg (*text
->args_ptr
, int);
628 /* This consumes a second entry in the formatters array. */
629 gcc_assert (formatters
[argno
] == formatters
[argno
+1]);
633 s
= va_arg (*text
->args_ptr
, const char *);
634 pp_append_text (pp
, s
, s
+ n
);
642 gcc_assert (pp_format_decoder (pp
));
643 ok
= pp_format_decoder (pp
) (pp
, text
, p
,
644 precision
, wide
, plus
, hash
);
651 pp_string (pp
, colorize_stop (pp_show_color (pp
)));
652 pp_string (pp
, close_quote
);
655 obstack_1grow (&buffer
->chunk_obstack
, '\0');
656 *formatters
[argno
] = XOBFINISH (&buffer
->chunk_obstack
, const char *);
660 for (; argno
< PP_NL_ARGMAX
; argno
++)
661 gcc_assert (!formatters
[argno
]);
663 /* Revert to normal obstack and wrapping mode. */
664 buffer
->obstack
= &buffer
->formatted_obstack
;
665 buffer
->line_length
= 0;
666 pp_wrapping_mode (pp
) = old_wrapping_mode
;
670 /* Format of a message pointed to by TEXT. */
672 pp_output_formatted_text (pretty_printer
*pp
)
675 output_buffer
*buffer
= pp_buffer (pp
);
676 struct chunk_info
*chunk_array
= buffer
->cur_chunk_array
;
677 const char **args
= chunk_array
->args
;
679 gcc_assert (buffer
->obstack
== &buffer
->formatted_obstack
);
680 gcc_assert (buffer
->line_length
== 0);
682 /* This is a third phase, first 2 phases done in pp_format_args.
683 Now we actually print it. */
684 for (chunk
= 0; args
[chunk
]; chunk
++)
685 pp_string (pp
, args
[chunk
]);
687 /* Deallocate the chunk structure and everything after it (i.e. the
688 associated series of formatted strings). */
689 buffer
->cur_chunk_array
= chunk_array
->prev
;
690 obstack_free (&buffer
->chunk_obstack
, chunk_array
);
693 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
694 settings needed by BUFFER for a verbatim formatting. */
696 pp_format_verbatim (pretty_printer
*pp
, text_info
*text
)
698 /* Set verbatim mode. */
699 pp_wrapping_mode_t oldmode
= pp_set_verbatim_wrapping (pp
);
701 /* Do the actual formatting. */
702 pp_format (pp
, text
);
703 pp_output_formatted_text (pp
);
705 /* Restore previous settings. */
706 pp_wrapping_mode (pp
) = oldmode
;
709 /* Flush the content of BUFFER onto the attached stream. This
710 function does nothing unless pp->output_buffer->flush_p. */
712 pp_flush (pretty_printer
*pp
)
715 if (!pp
->buffer
->flush_p
)
717 pp_write_text_to_stream (pp
);
718 fflush (pp_buffer (pp
)->stream
);
721 /* Flush the content of BUFFER onto the attached stream independently
722 of the value of pp->output_buffer->flush_p. */
724 pp_really_flush (pretty_printer
*pp
)
727 pp_write_text_to_stream (pp
);
728 fflush (pp_buffer (pp
)->stream
);
731 /* Sets the number of maximum characters per line PRETTY-PRINTER can
732 output in line-wrapping mode. A LENGTH value 0 suppresses
735 pp_set_line_maximum_length (pretty_printer
*pp
, int length
)
737 pp_line_cutoff (pp
) = length
;
738 pp_set_real_maximum_length (pp
);
741 /* Clear PRETTY-PRINTER output area text info. */
743 pp_clear_output_area (pretty_printer
*pp
)
745 obstack_free (pp_buffer (pp
)->obstack
,
746 obstack_base (pp_buffer (pp
)->obstack
));
747 pp_buffer (pp
)->line_length
= 0;
750 /* Set PREFIX for PRETTY-PRINTER. */
752 pp_set_prefix (pretty_printer
*pp
, const char *prefix
)
755 pp_set_real_maximum_length (pp
);
756 pp
->emitted_prefix
= false;
757 pp_indentation (pp
) = 0;
760 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
762 pp_destroy_prefix (pretty_printer
*pp
)
764 if (pp
->prefix
!= NULL
)
766 free (CONST_CAST (char *, pp
->prefix
));
771 /* Write out PRETTY-PRINTER's prefix. */
773 pp_emit_prefix (pretty_printer
*pp
)
775 if (pp
->prefix
!= NULL
)
777 switch (pp_prefixing_rule (pp
))
780 case DIAGNOSTICS_SHOW_PREFIX_NEVER
:
783 case DIAGNOSTICS_SHOW_PREFIX_ONCE
:
784 if (pp
->emitted_prefix
)
789 pp_indentation (pp
) += 3;
792 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
:
794 int prefix_length
= strlen (pp
->prefix
);
795 pp_append_r (pp
, pp
->prefix
, prefix_length
);
796 pp
->emitted_prefix
= true;
803 /* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
804 characters per line. */
806 pretty_printer::pretty_printer (const char *p
, int l
)
807 : buffer (new (XCNEW (output_buffer
)) output_buffer ()),
816 translate_identifiers (true),
819 pp_line_cutoff (this) = l
;
820 /* By default, we emit prefixes once per message. */
821 pp_prefixing_rule (this) = DIAGNOSTICS_SHOW_PREFIX_ONCE
;
822 pp_set_prefix (this, p
);
825 pretty_printer::~pretty_printer ()
827 buffer
->~output_buffer ();
831 /* Append a string delimited by START and END to the output area of
832 PRETTY-PRINTER. No line wrapping is done. However, if beginning a
833 new line then emit PRETTY-PRINTER's prefix and skip any leading
834 whitespace if appropriate. The caller must ensure that it is
837 pp_append_text (pretty_printer
*pp
, const char *start
, const char *end
)
839 /* Emit prefix and skip whitespace if we're starting a new line. */
840 if (pp_buffer (pp
)->line_length
== 0)
843 if (pp_is_wrapping_line (pp
))
844 while (start
!= end
&& *start
== ' ')
847 pp_append_r (pp
, start
, end
- start
);
850 /* Finishes constructing a NULL-terminated character string representing
851 the PRETTY-PRINTED text. */
853 pp_formatted_text (pretty_printer
*pp
)
855 return output_buffer_formatted_text (pp_buffer (pp
));
858 /* Return a pointer to the last character emitted in PRETTY-PRINTER's
859 output area. A NULL pointer means no character available. */
861 pp_last_position_in_text (const pretty_printer
*pp
)
863 return output_buffer_last_position_in_text (pp_buffer (pp
));
866 /* Return the amount of characters PRETTY-PRINTER can accept to
867 make a full line. Meaningful only in line-wrapping mode. */
869 pp_remaining_character_count_for_line (pretty_printer
*pp
)
871 return pp
->maximum_length
- pp_buffer (pp
)->line_length
;
875 /* Format a message into BUFFER a la printf. */
877 pp_printf (pretty_printer
*pp
, const char *msg
, ...)
885 text
.format_spec
= msg
;
886 pp_format (pp
, &text
);
887 pp_output_formatted_text (pp
);
892 /* Output MESSAGE verbatim into BUFFER. */
894 pp_verbatim (pretty_printer
*pp
, const char *msg
, ...)
902 text
.format_spec
= msg
;
903 pp_format_verbatim (pp
, &text
);
909 /* Have PRETTY-PRINTER start a new line. */
911 pp_newline (pretty_printer
*pp
)
913 obstack_1grow (pp_buffer (pp
)->obstack
, '\n');
914 pp_needs_newline (pp
) = false;
915 pp_buffer (pp
)->line_length
= 0;
918 /* Have PRETTY-PRINTER add a CHARACTER. */
920 pp_character (pretty_printer
*pp
, int c
)
922 if (pp_is_wrapping_line (pp
)
923 && pp_remaining_character_count_for_line (pp
) <= 0)
929 obstack_1grow (pp_buffer (pp
)->obstack
, c
);
930 ++pp_buffer (pp
)->line_length
;
933 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
934 be line-wrapped if in appropriate mode. */
936 pp_string (pretty_printer
*pp
, const char *str
)
938 gcc_checking_assert (str
);
939 pp_maybe_wrap_text (pp
, str
, str
+ strlen (str
));
942 /* Maybe print out a whitespace if needed. */
945 pp_maybe_space (pretty_printer
*pp
)
947 if (pp
->padding
!= pp_none
)
950 pp
->padding
= pp_none
;
954 // Add a newline to the pretty printer PP and flush formatted text.
957 pp_newline_and_flush (pretty_printer
*pp
)
961 pp_needs_newline (pp
) = false;
964 // Add a newline to the pretty printer PP, followed by indentation.
967 pp_newline_and_indent (pretty_printer
*pp
, int n
)
969 pp_indentation (pp
) += n
;
972 pp_needs_newline (pp
) = false;
975 // Add separator C, followed by a single whitespace.
978 pp_separate_with (pretty_printer
*pp
, char c
)
980 pp_character (pp
, c
);
985 /* The string starting at P has LEN (at least 1) bytes left; if they
986 start with a valid UTF-8 sequence, return the length of that
987 sequence and set *VALUE to the value of that sequence, and
988 otherwise return 0 and set *VALUE to (unsigned int) -1. */
991 decode_utf8_char (const unsigned char *p
, size_t len
, unsigned int *value
)
1002 for (t
= *p
; t
& 0x80; t
<<= 1)
1005 if (utf8_len
> len
|| utf8_len
< 2 || utf8_len
> 6)
1007 *value
= (unsigned int) -1;
1010 ch
= *p
& ((1 << (7 - utf8_len
)) - 1);
1011 for (i
= 1; i
< utf8_len
; i
++)
1013 unsigned int u
= p
[i
];
1014 if ((u
& 0xC0) != 0x80)
1016 *value
= (unsigned int) -1;
1019 ch
= (ch
<< 6) | (u
& 0x3F);
1021 if ( (ch
<= 0x7F && utf8_len
> 1)
1022 || (ch
<= 0x7FF && utf8_len
> 2)
1023 || (ch
<= 0xFFFF && utf8_len
> 3)
1024 || (ch
<= 0x1FFFFF && utf8_len
> 4)
1025 || (ch
<= 0x3FFFFFF && utf8_len
> 5)
1026 || (ch
>= 0xD800 && ch
<= 0xDFFF))
1028 *value
= (unsigned int) -1;
1041 /* Allocator for identifier_to_locale and corresponding function to
1044 void *(*identifier_to_locale_alloc
) (size_t) = xmalloc
;
1045 void (*identifier_to_locale_free
) (void *) = free
;
1047 /* Given IDENT, an identifier in the internal encoding, return a
1048 version of IDENT suitable for diagnostics in the locale character
1049 set: either IDENT itself, or a string, allocated using
1050 identifier_to_locale_alloc, converted to the locale character set
1051 and using escape sequences if not representable in the locale
1052 character set or containing control characters or invalid byte
1053 sequences. Existing backslashes in IDENT are not doubled, so the
1054 result may not uniquely specify the contents of an arbitrary byte
1055 sequence identifier. */
1058 identifier_to_locale (const char *ident
)
1060 const unsigned char *uid
= (const unsigned char *) ident
;
1061 size_t idlen
= strlen (ident
);
1062 bool valid_printable_utf8
= true;
1063 bool all_ascii
= true;
1066 for (i
= 0; i
< idlen
;)
1069 size_t utf8_len
= decode_utf8_char (&uid
[i
], idlen
- i
, &c
);
1070 if (utf8_len
== 0 || c
<= 0x1F || (c
>= 0x7F && c
<= 0x9F))
1072 valid_printable_utf8
= false;
1080 /* If IDENT contains invalid UTF-8 sequences (which may occur with
1081 attributes putting arbitrary byte sequences in identifiers), or
1082 control characters, we use octal escape sequences for all bytes
1083 outside printable ASCII. */
1084 if (!valid_printable_utf8
)
1086 char *ret
= (char *) identifier_to_locale_alloc (4 * idlen
+ 1);
1088 for (i
= 0; i
< idlen
; i
++)
1090 if (uid
[i
] > 0x1F && uid
[i
] < 0x7F)
1094 sprintf (p
, "\\%03o", uid
[i
]);
1102 /* Otherwise, if it is valid printable ASCII, or printable UTF-8
1103 with the locale character set being UTF-8, IDENT is used. */
1104 if (all_ascii
|| locale_utf8
)
1107 /* Otherwise IDENT is converted to the locale character set if
1109 #if defined ENABLE_NLS && defined HAVE_LANGINFO_CODESET && HAVE_ICONV
1110 if (locale_encoding
!= NULL
)
1112 iconv_t cd
= iconv_open (locale_encoding
, "UTF-8");
1113 bool conversion_ok
= true;
1115 if (cd
!= (iconv_t
) -1)
1117 size_t ret_alloc
= 4 * idlen
+ 1;
1120 /* Repeat the whole conversion process as needed with
1121 larger buffers so non-reversible transformations can
1122 always be detected. */
1123 ICONV_CONST
char *inbuf
= CONST_CAST (char *, ident
);
1125 size_t inbytesleft
= idlen
;
1126 size_t outbytesleft
= ret_alloc
- 1;
1129 ret
= (char *) identifier_to_locale_alloc (ret_alloc
);
1132 if (iconv (cd
, 0, 0, 0, 0) == (size_t) -1)
1134 conversion_ok
= false;
1138 iconv_ret
= iconv (cd
, &inbuf
, &inbytesleft
,
1139 &outbuf
, &outbytesleft
);
1140 if (iconv_ret
== (size_t) -1 || inbytesleft
!= 0)
1145 identifier_to_locale_free (ret
);
1151 conversion_ok
= false;
1155 else if (iconv_ret
!= 0)
1157 conversion_ok
= false;
1160 /* Return to initial shift state. */
1161 if (iconv (cd
, 0, 0, &outbuf
, &outbytesleft
) == (size_t) -1)
1166 identifier_to_locale_free (ret
);
1172 conversion_ok
= false;
1186 /* Otherwise, convert non-ASCII characters in IDENT to UCNs. */
1188 char *ret
= (char *) identifier_to_locale_alloc (10 * idlen
+ 1);
1190 for (i
= 0; i
< idlen
;)
1193 size_t utf8_len
= decode_utf8_char (&uid
[i
], idlen
- i
, &c
);
1198 sprintf (p
, "\\U%08x", c
);
1210 namespace selftest
{
1212 /* Smoketest for pretty_printer. */
1215 test_basic_printing ()
1218 pp_string (&pp
, "hello");
1220 pp_string (&pp
, "world");
1222 ASSERT_STREQ ("hello world", pp_formatted_text (&pp
));
1225 /* Helper function for testing pp_format.
1226 Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
1227 prints EXPECTED, assuming that pp_show_color is SHOW_COLOR. */
1230 assert_pp_format_va (const location
&loc
, const char *expected
,
1231 bool show_color
, const char *fmt
, va_list *ap
)
1235 rich_location
rich_loc (line_table
, UNKNOWN_LOCATION
);
1237 ti
.format_spec
= fmt
;
1241 ti
.m_richloc
= &rich_loc
;
1243 pp_show_color (&pp
) = show_color
;
1244 pp_format (&pp
, &ti
);
1245 pp_output_formatted_text (&pp
);
1246 ASSERT_STREQ_AT (loc
, expected
, pp_formatted_text (&pp
));
1249 /* Verify that pp_format (FMT, ...) followed by pp_output_formatted_text
1250 prints EXPECTED, with show_color disabled. */
1253 assert_pp_format (const location
&loc
, const char *expected
,
1254 const char *fmt
, ...)
1259 assert_pp_format_va (loc
, expected
, false, fmt
, &ap
);
1263 /* As above, but with colorization enabled. */
1266 assert_pp_format_colored (const location
&loc
, const char *expected
,
1267 const char *fmt
, ...)
1269 /* The tests of colorization assume the default color scheme.
1270 If GCC_COLORS is set, then the colors have potentially been
1271 overridden; skip the test. */
1272 if (getenv ("GCC_COLORS"))
1278 assert_pp_format_va (loc
, expected
, true, fmt
, &ap
);
1282 /* Helper function for calling testing pp_format,
1283 by calling assert_pp_format with various numbers of arguments.
1284 These exist mostly to avoid having to write SELFTEST_LOCATION
1285 throughout test_pp_format. */
1287 #define ASSERT_PP_FORMAT_1(EXPECTED, FMT, ARG1) \
1288 SELFTEST_BEGIN_STMT \
1289 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
1293 #define ASSERT_PP_FORMAT_2(EXPECTED, FMT, ARG1, ARG2) \
1294 SELFTEST_BEGIN_STMT \
1295 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
1299 #define ASSERT_PP_FORMAT_3(EXPECTED, FMT, ARG1, ARG2, ARG3) \
1300 SELFTEST_BEGIN_STMT \
1301 assert_pp_format ((SELFTEST_LOCATION), (EXPECTED), (FMT), \
1302 (ARG1), (ARG2), (ARG3)); \
1305 /* Verify that pp_format works, for various format codes. */
1310 /* Avoid introducing locale-specific differences in the results
1311 by hardcoding open_quote and close_quote. */
1312 const char *old_open_quote
= open_quote
;
1313 const char *old_close_quote
= close_quote
;
1317 /* Verify that plain text is passed through unchanged. */
1318 assert_pp_format (SELFTEST_LOCATION
, "unformatted", "unformatted");
1320 /* Verify various individual format codes, in the order listed in the
1321 comment for pp_format above. For each code, we append a second
1322 argument with a known bit pattern (0x12345678), to ensure that we
1323 are consuming arguments correctly. */
1324 ASSERT_PP_FORMAT_2 ("-27 12345678", "%d %x", -27, 0x12345678);
1325 ASSERT_PP_FORMAT_2 ("-5 12345678", "%i %x", -5, 0x12345678);
1326 ASSERT_PP_FORMAT_2 ("10 12345678", "%u %x", 10, 0x12345678);
1327 ASSERT_PP_FORMAT_2 ("17 12345678", "%o %x", 15, 0x12345678);
1328 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%x %x", 0xcafebabe, 0x12345678);
1329 ASSERT_PP_FORMAT_2 ("-27 12345678", "%ld %x", (long)-27, 0x12345678);
1330 ASSERT_PP_FORMAT_2 ("-5 12345678", "%li %x", (long)-5, 0x12345678);
1331 ASSERT_PP_FORMAT_2 ("10 12345678", "%lu %x", (long)10, 0x12345678);
1332 ASSERT_PP_FORMAT_2 ("17 12345678", "%lo %x", (long)15, 0x12345678);
1333 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%lx %x", (long)0xcafebabe,
1335 ASSERT_PP_FORMAT_2 ("-27 12345678", "%lld %x", (long long)-27, 0x12345678);
1336 ASSERT_PP_FORMAT_2 ("-5 12345678", "%lli %x", (long long)-5, 0x12345678);
1337 ASSERT_PP_FORMAT_2 ("10 12345678", "%llu %x", (long long)10, 0x12345678);
1338 ASSERT_PP_FORMAT_2 ("17 12345678", "%llo %x", (long long)15, 0x12345678);
1339 ASSERT_PP_FORMAT_2 ("cafebabe 12345678", "%llx %x", (long long)0xcafebabe,
1341 ASSERT_PP_FORMAT_2 ("-27 12345678", "%wd %x", (HOST_WIDE_INT
)-27, 0x12345678);
1342 ASSERT_PP_FORMAT_2 ("-5 12345678", "%wi %x", (HOST_WIDE_INT
)-5, 0x12345678);
1343 ASSERT_PP_FORMAT_2 ("10 12345678", "%wu %x", (unsigned HOST_WIDE_INT
)10,
1345 ASSERT_PP_FORMAT_2 ("17 12345678", "%wo %x", (HOST_WIDE_INT
)15, 0x12345678);
1346 ASSERT_PP_FORMAT_2 ("0xcafebabe 12345678", "%wx %x", (HOST_WIDE_INT
)0xcafebabe,
1348 ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678);
1349 ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello world",
1351 /* We can't test for %p; the pointer is printed in an implementation-defined
1353 ASSERT_PP_FORMAT_2 ("normal colored normal 12345678",
1354 "normal %rcolored%R normal %x",
1355 "error", 0x12345678);
1356 assert_pp_format_colored
1358 "normal \33[01;31m\33[Kcolored\33[m\33[K normal 12345678",
1359 "normal %rcolored%R normal %x", "error", 0x12345678);
1361 %m: strerror(text->err_no) - does not consume a value from args_ptr. */
1362 ASSERT_PP_FORMAT_1 ("% 12345678", "%% %x", 0x12345678);
1363 ASSERT_PP_FORMAT_1 ("` 12345678", "%< %x", 0x12345678);
1364 ASSERT_PP_FORMAT_1 ("' 12345678", "%> %x", 0x12345678);
1365 ASSERT_PP_FORMAT_1 ("' 12345678", "%' %x", 0x12345678);
1366 ASSERT_PP_FORMAT_3 ("abc 12345678", "%.*s %x", 3, "abcdef", 0x12345678);
1367 ASSERT_PP_FORMAT_2 ("abc 12345678", "%.3s %x", "abcdef", 0x12345678);
1369 /* Verify flag 'q'. */
1370 ASSERT_PP_FORMAT_2 ("`foo' 12345678", "%qs %x", "foo", 0x12345678);
1371 assert_pp_format_colored (SELFTEST_LOCATION
,
1372 "`\33[01m\33[Kfoo\33[m\33[K' 12345678", "%qs %x",
1375 /* Verify that combinations work, along with unformatted text. */
1376 assert_pp_format (SELFTEST_LOCATION
,
1377 "the quick brown fox jumps over the lazy dog",
1378 "the %s %s %s jumps over the %s %s",
1379 "quick", "brown", "fox", "lazy", "dog");
1380 assert_pp_format (SELFTEST_LOCATION
, "item 3 of 7", "item %i of %i", 3, 7);
1381 assert_pp_format (SELFTEST_LOCATION
, "problem with `bar' at line 10",
1382 "problem with %qs at line %i", "bar", 10);
1384 /* Restore old values of open_quote and close_quote. */
1385 open_quote
= old_open_quote
;
1386 close_quote
= old_close_quote
;
1389 /* Run all of the selftests within this file. */
1392 pretty_print_c_tests ()
1394 test_basic_printing ();
1398 } // namespace selftest
1400 #endif /* CHECKING_P */