1 /* Various declarations for language-independent pretty-print subroutines.
2 Copyright (C) 2003, 2004, 2005 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 2, 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 COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 #undef FLOAT /* This is for hpux. They should change hpux. */
24 #undef FFS /* Some systems define this in param.h. */
27 #include "pretty-print.h"
29 #define obstack_chunk_alloc xmalloc
30 #define obstack_chunk_free free
32 /* A pointer to the formatted diagnostic message. */
33 #define pp_formatted_text_data(PP) \
34 ((const char *) obstack_base (pp_base (PP)->buffer->obstack))
36 /* Format an integer given by va_arg (ARG, type-specifier T) where
37 type-specifier is a precision modifier as indicated by PREC. F is
38 a string used to construct the appropriate format-specifier. */
39 #define pp_integer_with_precision(PP, ARG, PREC, T, F) \
44 pp_scalar (PP, "%" F, va_arg (ARG, T)); \
48 pp_scalar (PP, "%l" F, va_arg (ARG, long T)); \
52 pp_scalar (PP, "%" PRINTF_INT64_MODIFIER F, va_arg (ARG, long_long)); \
61 /* Subroutine of pp_set_maximum_length. Set up PRETTY-PRINTER's
62 internal maximum characters per line. */
64 pp_set_real_maximum_length (pretty_printer *pp)
66 /* If we're told not to wrap lines then do the obvious thing. In case
67 we'll emit prefix only once per message, it is appropriate
68 not to increase unnecessarily the line-length cut-off. */
69 if (!pp_is_wrapping_line (pp)
70 || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_ONCE
71 || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
72 pp->maximum_length = pp_line_cutoff (pp);
75 int prefix_length = pp->prefix ? strlen (pp->prefix) : 0;
76 /* If the prefix is ridiculously too long, output at least
78 if (pp_line_cutoff (pp) - prefix_length < 32)
79 pp->maximum_length = pp_line_cutoff (pp) + 32;
81 pp->maximum_length = pp_line_cutoff (pp);
85 /* Clear PRETTY-PRINTER's output state. */
87 pp_clear_state (pretty_printer *pp)
89 pp->emitted_prefix = false;
90 pp_indentation (pp) = 0;
93 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream. */
95 pp_write_text_to_stream (pretty_printer *pp)
97 const char *text = pp_formatted_text (pp);
98 fputs (text, pp->buffer->stream);
99 pp_clear_output_area (pp);
102 /* Wrap a text delimited by START and END into PRETTY-PRINTER. */
104 pp_wrap_text (pretty_printer *pp, const char *start, const char *end)
106 bool wrapping_line = pp_is_wrapping_line (pp);
110 /* Dump anything bordered by whitespaces. */
112 const char *p = start;
113 while (p != end && !ISBLANK (*p) && *p != '\n')
116 && p - start >= pp_remaining_character_count_for_line (pp))
118 pp_append_text (pp, start, p);
122 if (start != end && ISBLANK (*start))
127 if (start != end && *start == '\n')
135 /* Same as pp_wrap_text but wrap text only when in line-wrapping mode. */
137 pp_maybe_wrap_text (pretty_printer *pp, const char *start, const char *end)
139 if (pp_is_wrapping_line (pp))
140 pp_wrap_text (pp, start, end);
142 pp_append_text (pp, start, end);
145 /* Append to the output area of PRETTY-PRINTER a string specified by its
146 STARTing character and LENGTH. */
148 pp_append_r (pretty_printer *pp, const char *start, int length)
150 obstack_grow (pp->buffer->obstack, start, length);
151 pp->buffer->line_length += length;
154 /* Insert enough spaces into the output area of PRETTY-PRINTER to bring
155 the column position to the current indentation level, assuming that a
156 newline has just been written to the buffer. */
158 pp_base_indent (pretty_printer *pp)
160 int n = pp_indentation (pp);
163 for (i = 0; i < n; ++i)
167 /* The following format specifiers are recognized as being client independent:
168 %d, %i: (signed) integer in base ten.
169 %u: unsigned integer in base ten.
170 %o: unsigned integer in base eight.
171 %x: unsigned integer in base sixteen.
172 %ld, %li, %lo, %lu, %lx: long versions of the above.
173 %lld, %lli, %llo, %llu, %llx: long long versions.
174 %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
178 %m: strerror(text->err_no) - does not consume a value from args_ptr.
182 %': apostrophe (should only be used in untranslated messages;
183 translations should use appropriate punctuation directly).
184 %.*s: a substring the length of which is specified by an argument
186 %Ns: likewise, but length specified as constant in the format string.
188 %J: a decl tree, from which DECL_SOURCE_LOCATION will be recorded.
189 Flag 'q': quote formatted text (must come immediately after '%').
191 Arguments can be used sequentially, or through %N$ resp. *N$
192 notation Nth argument after the format string. If %N$ / *N$
193 notation is used, it must be used for all arguments, except %m, %%,
194 %<, %> and %', which may not have a number, as they do not consume
195 an argument. When %M$.*N$s is used, M must be N + 1. (This may
196 also be written %M$.*s, provided N is not otherwise used.) The
197 format string must have conversion specifiers with argument numbers
198 1 up to highest argument; each argument may only be used once.
199 A format string can have at most 30 arguments. */
201 /* Formatting phases 1 and 2: render TEXT->format_spec plus
202 TEXT->args_ptr into a series of chunks in PP->buffer->args[].
203 Phase 3 is in pp_base_format_text. */
206 pp_base_format (pretty_printer *pp, text_info *text)
208 output_buffer *buffer = pp->buffer;
211 struct chunk_info *new_chunk_array;
213 unsigned int curarg = 0, chunk = 0, argno;
214 pp_wrapping_mode_t old_wrapping_mode;
215 bool any_unnumbered = false, any_numbered = false;
216 const char **formatters[PP_NL_ARGMAX];
218 /* Allocate a new chunk structure. */
219 new_chunk_array = XOBNEW (&buffer->chunk_obstack, struct chunk_info);
220 new_chunk_array->prev = buffer->cur_chunk_array;
221 buffer->cur_chunk_array = new_chunk_array;
222 args = new_chunk_array->args;
224 /* Formatting phase 1: split up TEXT->format_spec into chunks in
225 PP->buffer->args[]. Even-numbered chunks are to be output
226 verbatim, odd-numbered chunks are format specifiers.
227 %m, %%, %<, %>, and %' are replaced with the appropriate text at
230 memset (formatters, 0, sizeof formatters);
232 for (p = text->format_spec; *p; )
234 while (*p != '\0' && *p != '%')
236 obstack_1grow (&buffer->chunk_obstack, *p);
249 obstack_1grow (&buffer->chunk_obstack, '%');
254 obstack_grow (&buffer->chunk_obstack,
255 open_quote, strlen (open_quote));
261 obstack_grow (&buffer->chunk_obstack,
262 close_quote, strlen (close_quote));
268 const char *errstr = xstrerror (text->err_no);
269 obstack_grow (&buffer->chunk_obstack, errstr, strlen (errstr));
275 /* Handled in phase 2. Terminate the plain chunk here. */
276 obstack_1grow (&buffer->chunk_obstack, '\0');
277 gcc_assert (chunk < PP_NL_ARGMAX * 2);
278 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
285 argno = strtoul (p, &end, 10) - 1;
287 gcc_assert (*p == '$');
291 gcc_assert (!any_unnumbered);
296 any_unnumbered = true;
297 gcc_assert (!any_numbered);
299 gcc_assert (argno < PP_NL_ARGMAX);
300 gcc_assert (!formatters[argno]);
301 formatters[argno] = &args[chunk];
304 obstack_1grow (&buffer->chunk_obstack, *p);
307 while (strchr ("qwl+#", p[-1]));
311 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
312 (where M == N + 1). */
317 obstack_1grow (&buffer->chunk_obstack, *p);
320 while (ISDIGIT (p[-1]));
321 gcc_assert (p[-1] == 's');
325 gcc_assert (*p == '*');
326 obstack_1grow (&buffer->chunk_obstack, '*');
332 unsigned int argno2 = strtoul (p, &end, 10) - 1;
334 gcc_assert (argno2 == argno - 1);
335 gcc_assert (!any_unnumbered);
336 gcc_assert (*p == '$');
339 formatters[argno2] = formatters[argno];
343 gcc_assert (!any_numbered);
344 formatters[argno+1] = formatters[argno];
347 gcc_assert (*p == 's');
348 obstack_1grow (&buffer->chunk_obstack, 's');
355 obstack_1grow (&buffer->chunk_obstack, '\0');
356 gcc_assert (chunk < PP_NL_ARGMAX * 2);
357 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
360 obstack_1grow (&buffer->chunk_obstack, '\0');
361 gcc_assert (chunk < PP_NL_ARGMAX * 2);
362 args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
365 /* Set output to the argument obstack, and switch line-wrapping and
367 buffer->obstack = &buffer->chunk_obstack;
368 old_wrapping_mode = pp_set_verbatim_wrapping (pp);
370 /* Second phase. Replace each formatter with the formatted text it
373 for (argno = 0; formatters[argno]; argno++)
381 /* We do not attempt to enforce any ordering on the modifier
384 for (p = *formatters[argno];; p++)
409 /* We don't support precision beyond that of "long long". */
410 gcc_assert (precision < 2);
417 gcc_assert (!wide || precision == 0);
420 pp_string (pp, open_quote);
425 pp_character (pp, va_arg (*text->args_ptr, int));
431 pp_wide_integer (pp, va_arg (*text->args_ptr, HOST_WIDE_INT));
433 pp_integer_with_precision
434 (pp, *text->args_ptr, precision, int, "d");
439 pp_scalar (pp, "%" HOST_WIDE_INT_PRINT "o",
440 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
442 pp_integer_with_precision
443 (pp, *text->args_ptr, precision, unsigned, "o");
447 pp_string (pp, va_arg (*text->args_ptr, const char *));
451 pp_pointer (pp, va_arg (*text->args_ptr, void *));
456 pp_scalar (pp, HOST_WIDE_INT_PRINT_UNSIGNED,
457 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
459 pp_integer_with_precision
460 (pp, *text->args_ptr, precision, unsigned, "u");
465 pp_scalar (pp, HOST_WIDE_INT_PRINT_HEX,
466 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
468 pp_integer_with_precision
469 (pp, *text->args_ptr, precision, unsigned, "x");
474 location_t *locus = va_arg (*text->args_ptr, location_t *);
475 gcc_assert (text->locus != NULL);
476 *text->locus = *locus;
482 tree t = va_arg (*text->args_ptr, tree);
483 gcc_assert (text->locus != NULL);
484 *text->locus = DECL_SOURCE_LOCATION (t);
493 /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
494 (where M == N + 1). The format string should be verified
495 already from the first phase. */
500 n = strtoul (p, &end, 10);
502 gcc_assert (*p == 's');
506 gcc_assert (*p == '*');
508 gcc_assert (*p == 's');
509 n = va_arg (*text->args_ptr, int);
511 /* This consumes a second entry in the formatters array. */
512 gcc_assert (formatters[argno] == formatters[argno+1]);
516 s = va_arg (*text->args_ptr, const char *);
517 pp_append_text (pp, s, s + n);
525 gcc_assert (pp_format_decoder (pp));
526 ok = pp_format_decoder (pp) (pp, text, p,
527 precision, wide, plus, hash);
533 pp_string (pp, close_quote);
535 obstack_1grow (&buffer->chunk_obstack, '\0');
536 *formatters[argno] = XOBFINISH (&buffer->chunk_obstack, const char *);
539 #ifdef ENABLE_CHECKING
540 for (; argno < PP_NL_ARGMAX; argno++)
541 gcc_assert (!formatters[argno]);
544 /* Revert to normal obstack and wrapping mode. */
545 buffer->obstack = &buffer->formatted_obstack;
546 buffer->line_length = 0;
547 pp_wrapping_mode (pp) = old_wrapping_mode;
551 /* Format of a message pointed to by TEXT. */
553 pp_base_output_formatted_text (pretty_printer *pp)
556 output_buffer *buffer = pp_buffer (pp);
557 struct chunk_info *chunk_array = buffer->cur_chunk_array;
558 const char **args = chunk_array->args;
560 gcc_assert (buffer->obstack == &buffer->formatted_obstack);
561 gcc_assert (buffer->line_length == 0);
563 /* This is a third phase, first 2 phases done in pp_base_format_args.
564 Now we actually print it. */
565 for (chunk = 0; args[chunk]; chunk++)
566 pp_string (pp, args[chunk]);
568 /* Deallocate the chunk structure and everything after it (i.e. the
569 associated series of formatted strings). */
570 buffer->cur_chunk_array = chunk_array->prev;
571 obstack_free (&buffer->chunk_obstack, chunk_array);
574 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
575 settings needed by BUFFER for a verbatim formatting. */
577 pp_base_format_verbatim (pretty_printer *pp, text_info *text)
579 /* Set verbatim mode. */
580 pp_wrapping_mode_t oldmode = pp_set_verbatim_wrapping (pp);
582 /* Do the actual formatting. */
583 pp_format (pp, text);
584 pp_output_formatted_text (pp);
586 /* Restore previous settings. */
587 pp_wrapping_mode (pp) = oldmode;
590 /* Flush the content of BUFFER onto the attached stream. */
592 pp_base_flush (pretty_printer *pp)
594 pp_write_text_to_stream (pp);
596 fputc ('\n', pp->buffer->stream);
597 fflush (pp->buffer->stream);
598 pp_needs_newline (pp) = false;
601 /* Sets the number of maximum characters per line PRETTY-PRINTER can
602 output in line-wrapping mode. A LENGTH value 0 suppresses
605 pp_base_set_line_maximum_length (pretty_printer *pp, int length)
607 pp_line_cutoff (pp) = length;
608 pp_set_real_maximum_length (pp);
611 /* Clear PRETTY-PRINTER output area text info. */
613 pp_base_clear_output_area (pretty_printer *pp)
615 obstack_free (pp->buffer->obstack, obstack_base (pp->buffer->obstack));
616 pp->buffer->line_length = 0;
619 /* Set PREFIX for PRETTY-PRINTER. */
621 pp_base_set_prefix (pretty_printer *pp, const char *prefix)
624 pp_set_real_maximum_length (pp);
625 pp->emitted_prefix = false;
626 pp_indentation (pp) = 0;
629 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
631 pp_base_destroy_prefix (pretty_printer *pp)
633 if (pp->prefix != NULL)
635 free ((char *) pp->prefix);
640 /* Write out PRETTY-PRINTER's prefix. */
642 pp_base_emit_prefix (pretty_printer *pp)
644 if (pp->prefix != NULL)
646 switch (pp_prefixing_rule (pp))
649 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
652 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
653 if (pp->emitted_prefix)
658 pp_indentation (pp) += 3;
661 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
663 int prefix_length = strlen (pp->prefix);
664 pp_append_r (pp, pp->prefix, prefix_length);
665 pp->emitted_prefix = true;
672 /* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
673 characters per line. */
675 pp_construct (pretty_printer *pp, const char *prefix, int maximum_length)
677 memset (pp, 0, sizeof (pretty_printer));
678 pp->buffer = xcalloc (1, sizeof (output_buffer));
679 obstack_init (&pp->buffer->chunk_obstack);
680 obstack_init (&pp->buffer->formatted_obstack);
681 pp->buffer->obstack = &pp->buffer->formatted_obstack;
682 pp->buffer->stream = stderr;
683 pp_line_cutoff (pp) = maximum_length;
684 pp_prefixing_rule (pp) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
685 pp_set_prefix (pp, prefix);
688 /* Append a string delimited by START and END to the output area of
689 PRETTY-PRINTER. No line wrapping is done. However, if beginning a
690 new line then emit PRETTY-PRINTER's prefix and skip any leading
691 whitespace if appropriate. The caller must ensure that it is
694 pp_base_append_text (pretty_printer *pp, const char *start, const char *end)
696 /* Emit prefix and skip whitespace if we're starting a new line. */
697 if (pp->buffer->line_length == 0)
700 if (pp_is_wrapping_line (pp))
701 while (start != end && *start == ' ')
704 pp_append_r (pp, start, end - start);
707 /* Finishes constructing a NULL-terminated character string representing
708 the PRETTY-PRINTED text. */
710 pp_base_formatted_text (pretty_printer *pp)
712 obstack_1grow (pp->buffer->obstack, '\0');
713 return pp_formatted_text_data (pp);
716 /* Return a pointer to the last character emitted in PRETTY-PRINTER's
717 output area. A NULL pointer means no character available. */
719 pp_base_last_position_in_text (const pretty_printer *pp)
721 const char *p = NULL;
722 struct obstack *text = pp->buffer->obstack;
724 if (obstack_base (text) != obstack_next_free (text))
725 p = ((const char *) obstack_next_free (text)) - 1;
729 /* Return the amount of characters PRETTY-PRINTER can accept to
730 make a full line. Meaningful only in line-wrapping mode. */
732 pp_base_remaining_character_count_for_line (pretty_printer *pp)
734 return pp->maximum_length - pp->buffer->line_length;
738 /* Format a message into BUFFER a la printf. */
740 pp_printf (pretty_printer *pp, const char *msg, ...)
748 text.format_spec = msg;
750 pp_format (pp, &text);
751 pp_output_formatted_text (pp);
756 /* Output MESSAGE verbatim into BUFFER. */
758 pp_verbatim (pretty_printer *pp, const char *msg, ...)
766 text.format_spec = msg;
768 pp_format_verbatim (pp, &text);
774 /* Have PRETTY-PRINTER start a new line. */
776 pp_base_newline (pretty_printer *pp)
778 obstack_1grow (pp->buffer->obstack, '\n');
779 pp->buffer->line_length = 0;
782 /* Have PRETTY-PRINTER add a CHARACTER. */
784 pp_base_character (pretty_printer *pp, int c)
786 if (pp_is_wrapping_line (pp)
787 && pp_remaining_character_count_for_line (pp) <= 0)
793 obstack_1grow (pp->buffer->obstack, c);
794 ++pp->buffer->line_length;
797 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
798 be line-wrapped if in appropriate mode. */
800 pp_base_string (pretty_printer *pp, const char *str)
802 pp_maybe_wrap_text (pp, str, str + (str ? strlen (str) : 0));
805 /* Maybe print out a whitespace if needed. */
808 pp_base_maybe_space (pretty_printer *pp)
810 if (pp_base (pp)->padding != pp_none)
813 pp_base (pp)->padding = pp_none;