1 /* Various declarations for language-independent pretty-print subroutines.
2 Copyright (C) 2003, 2004 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, 59 Temple Place - Suite 330, Boston, MA
23 #undef FLOAT /* This is for hpux. They should change hpux. */
24 #undef FFS /* Some systems define this in param.h. */
26 #include "coretypes.h"
28 #include "pretty-print.h"
31 #define obstack_chunk_alloc xmalloc
32 #define obstack_chunk_free free
34 /* A pointer to the formatted diagnostic message. */
35 #define pp_formatted_text_data(PP) \
36 ((const char *) obstack_base (&pp_base (PP)->buffer->obstack))
38 /* Format an integer given by va_arg (ARG, type-specifier T) where
39 type-specifier is a precision modifier as indicated by PREC. F is
40 a string used to construct the appropriate format-specifier. */
41 #define pp_integer_with_precision(PP, ARG, PREC, T, F) \
46 pp_scalar (PP, "%" F, va_arg (ARG, T)); \
50 pp_scalar (PP, "%l" F, va_arg (ARG, long T)); \
54 pp_scalar (PP, "%ll" F, va_arg (ARG, long long T)); \
63 /* Subroutine of pp_set_maximum_length. Set up PRETTY-PRINTER's
64 internal maximum characters per line. */
66 pp_set_real_maximum_length (pretty_printer
*pp
)
68 /* If we're told not to wrap lines then do the obvious thing. In case
69 we'll emit prefix only once per message, it is appropriate
70 not to increase unnecessarily the line-length cut-off. */
71 if (!pp_is_wrapping_line (pp
)
72 || pp_prefixing_rule (pp
) == DIAGNOSTICS_SHOW_PREFIX_ONCE
73 || pp_prefixing_rule (pp
) == DIAGNOSTICS_SHOW_PREFIX_NEVER
)
74 pp
->maximum_length
= pp_line_cutoff (pp
);
77 int prefix_length
= pp
->prefix
? strlen (pp
->prefix
) : 0;
78 /* If the prefix is ridiculously too long, output at least
80 if (pp_line_cutoff (pp
) - prefix_length
< 32)
81 pp
->maximum_length
= pp_line_cutoff (pp
) + 32;
83 pp
->maximum_length
= pp_line_cutoff (pp
);
87 /* Clear PRETTY-PRINTER's output state. */
89 pp_clear_state (pretty_printer
*pp
)
91 pp
->emitted_prefix
= false;
92 pp_indentation (pp
) = 0;
95 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream. */
97 pp_write_text_to_stream (pretty_printer
*pp
)
99 const char *text
= pp_formatted_text (pp
);
100 fputs (text
, pp
->buffer
->stream
);
101 pp_clear_output_area (pp
);
104 /* Wrap a text delimited by START and END into PRETTY-PRINTER. */
106 pp_wrap_text (pretty_printer
*pp
, const char *start
, const char *end
)
108 bool wrapping_line
= pp_is_wrapping_line (pp
);
112 /* Dump anything bordered by whitespaces. */
114 const char *p
= start
;
115 while (p
!= end
&& !ISBLANK (*p
) && *p
!= '\n')
118 && p
- start
>= pp_remaining_character_count_for_line (pp
))
120 pp_append_text (pp
, start
, p
);
124 if (start
!= end
&& ISBLANK (*start
))
129 if (start
!= end
&& *start
== '\n')
137 /* Same as pp_wrap_text but wrap text only when in line-wrapping mode. */
139 pp_maybe_wrap_text (pretty_printer
*pp
, const char *start
, const char *end
)
141 if (pp_is_wrapping_line (pp
))
142 pp_wrap_text (pp
, start
, end
);
144 pp_append_text (pp
, start
, end
);
147 /* Append to the output area of PRETTY-PRINTER a string specified by its
148 STARTing character and LENGTH. */
150 pp_append_r (pretty_printer
*pp
, const char *start
, int length
)
152 obstack_grow (&pp
->buffer
->obstack
, start
, length
);
153 pp
->buffer
->line_length
+= length
;
156 /* Insert enough spaces into the output area of PRETTY-PRINTER to bring
157 the column position to the current indentation level, assuming that a
158 newline has just been written to the buffer. */
160 pp_base_indent (pretty_printer
*pp
)
162 int n
= pp_indentation (pp
);
165 for (i
= 0; i
< n
; ++i
)
169 /* Prepare PP to format a message pointed to by TEXT, with tentative
170 location LOCUS. It is expected that a call to pp_format_text with
171 exactly the same PP and TEXT arguments will follow. This routine
172 may modify the data in memory at TEXT and LOCP, and if it does,
173 caller is expected to notice.
175 Currently, all this does is notice a %H or %J escape at the beginning
176 of the string, and update LOCUS to match. */
178 pp_base_prepare_to_format (pretty_printer
*pp ATTRIBUTE_UNUSED
,
182 const char *p
= text
->format_spec
;
185 /* Extract the location information if any. */
190 *locus
= *va_arg (*text
->args_ptr
, location_t
*);
191 text
->format_spec
= p
+ 2;
195 t
= va_arg (*text
->args_ptr
, tree
);
196 *locus
= DECL_SOURCE_LOCATION (t
);
197 text
->format_spec
= p
+ 2;
203 /* Format a message pointed to by TEXT. The following format specifiers are
204 recognized as being client independent:
205 %d, %i: (signed) integer in base ten.
206 %u: unsigned integer in base ten.
207 %o: unsigned integer in base eight.
208 %x: unsigned integer in base sixteen.
209 %ld, %li, %lo, %lu, %lx: long versions of the above.
210 %lld, %lli, %llo, %llu, %llx: long long versions.
211 %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
215 %m: strerror(text->err_no) - does not consume a value from args_ptr.
219 %': apostrophe (should only be used in untranslated messages;
220 translations should use appropriate punctuation directly).
221 %.*s: a substring the length of which is specified by an integer.
223 Flag 'q': quote formatted text (must come immediately after '%'). */
225 pp_base_format_text (pretty_printer
*pp
, text_info
*text
)
227 for (; *text
->format_spec
; ++text
->format_spec
)
235 const char *p
= text
->format_spec
;
236 while (*p
&& *p
!= '%')
238 pp_wrap_text (pp
, text
->format_spec
, p
);
239 text
->format_spec
= p
;
242 if (*text
->format_spec
== '\0')
245 /* We got a '%'. Check for 'q', then parse precision modifiers,
247 if (*++text
->format_spec
== 'q')
252 switch (*text
->format_spec
)
262 while (*++text
->format_spec
== 'l');
268 /* We don't support precision beyond that of "long long". */
269 gcc_assert (precision
<= 2);
272 pp_string (pp
, open_quote
);
273 switch (*text
->format_spec
)
276 pp_character (pp
, va_arg (*text
->args_ptr
, int));
282 pp_wide_integer (pp
, va_arg (*text
->args_ptr
, HOST_WIDE_INT
));
284 pp_integer_with_precision
285 (pp
, *text
->args_ptr
, precision
, int, "d");
290 pp_scalar (pp
, "%" HOST_WIDE_INT_PRINT
"o",
291 va_arg (*text
->args_ptr
, unsigned HOST_WIDE_INT
));
293 pp_integer_with_precision
294 (pp
, *text
->args_ptr
, precision
, unsigned, "u");
298 pp_string (pp
, va_arg (*text
->args_ptr
, const char *));
302 pp_pointer (pp
, va_arg (*text
->args_ptr
, void *));
307 pp_scalar (pp
, HOST_WIDE_INT_PRINT_UNSIGNED
,
308 va_arg (*text
->args_ptr
, unsigned HOST_WIDE_INT
));
310 pp_integer_with_precision
311 (pp
, *text
->args_ptr
, precision
, unsigned, "u");
316 pp_scalar (pp
, HOST_WIDE_INT_PRINT_HEX
,
317 va_arg (*text
->args_ptr
, unsigned HOST_WIDE_INT
));
319 pp_integer_with_precision
320 (pp
, *text
->args_ptr
, precision
, unsigned, "x");
324 pp_string (pp
, xstrerror (text
->err_no
));
328 pp_character (pp
, '%');
332 pp_string (pp
, open_quote
);
337 pp_string (pp
, close_quote
);
342 location_t
*locus
= va_arg (*text
->args_ptr
, location_t
*);
343 expanded_location s
= expand_location (*locus
);
344 pp_string (pp
, "file '");
345 pp_string (pp
, s
.file
);
346 pp_string (pp
, "', line ");
347 pp_decimal_int (pp
, s
.line
);
355 /* We handle no precision specifier but '%.*s'. */
357 gcc_assert (*text
->format_spec
== '*');
359 gcc_assert (*text
->format_spec
== 's');
361 n
= va_arg (*text
->args_ptr
, int);
362 s
= va_arg (*text
->args_ptr
, const char *);
363 pp_append_text (pp
, s
, s
+ n
);
368 if (!pp_format_decoder (pp
) || !(*pp_format_decoder (pp
)) (pp
, text
))
370 /* Hmmm. The client failed to install a format translator
371 but called us with an unrecognized format. Or, maybe, the
372 translated string just contains an invalid format, or
373 has formats in the wrong order. Sorry. */
378 pp_string (pp
, close_quote
);
382 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
383 settings needed by BUFFER for a verbatim formatting. */
385 pp_base_format_verbatim (pretty_printer
*pp
, text_info
*text
)
387 diagnostic_prefixing_rule_t rule
= pp_prefixing_rule (pp
);
388 int line_cutoff
= pp_line_cutoff (pp
);
390 /* Set verbatim mode. */
391 pp
->prefixing_rule
= DIAGNOSTICS_SHOW_PREFIX_NEVER
;
392 pp_line_cutoff (pp
) = 0;
393 /* Do the actual formatting. */
394 pp_format_text (pp
, text
);
395 /* Restore previous settings. */
396 pp_prefixing_rule (pp
) = rule
;
397 pp_line_cutoff (pp
) = line_cutoff
;
400 /* Flush the content of BUFFER onto the attached stream. */
402 pp_base_flush (pretty_printer
*pp
)
404 pp_write_text_to_stream (pp
);
406 fputc ('\n', pp
->buffer
->stream
);
407 fflush (pp
->buffer
->stream
);
408 pp_needs_newline (pp
) = false;
411 /* Sets the number of maximum characters per line PRETTY-PRINTER can
412 output in line-wrapping mode. A LENGTH value 0 suppresses
415 pp_base_set_line_maximum_length (pretty_printer
*pp
, int length
)
417 pp_line_cutoff (pp
) = length
;
418 pp_set_real_maximum_length (pp
);
421 /* Clear PRETTY-PRINTER output area text info. */
423 pp_base_clear_output_area (pretty_printer
*pp
)
425 obstack_free (&pp
->buffer
->obstack
, obstack_base (&pp
->buffer
->obstack
));
426 pp
->buffer
->line_length
= 0;
429 /* Set PREFIX for PRETTY-PRINTER. */
431 pp_base_set_prefix (pretty_printer
*pp
, const char *prefix
)
434 pp_set_real_maximum_length (pp
);
435 pp
->emitted_prefix
= false;
436 pp_indentation (pp
) = 0;
439 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
441 pp_base_destroy_prefix (pretty_printer
*pp
)
443 if (pp
->prefix
!= NULL
)
445 free ((char *) pp
->prefix
);
450 /* Write out PRETTY-PRINTER's prefix. */
452 pp_base_emit_prefix (pretty_printer
*pp
)
454 if (pp
->prefix
!= NULL
)
456 switch (pp_prefixing_rule (pp
))
459 case DIAGNOSTICS_SHOW_PREFIX_NEVER
:
462 case DIAGNOSTICS_SHOW_PREFIX_ONCE
:
463 if (pp
->emitted_prefix
)
468 pp_indentation (pp
) += 3;
471 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
:
473 int prefix_length
= strlen (pp
->prefix
);
474 pp_append_r (pp
, pp
->prefix
, prefix_length
);
475 pp
->emitted_prefix
= true;
482 /* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
483 characters per line. */
485 pp_construct (pretty_printer
*pp
, const char *prefix
, int maximum_length
)
487 memset (pp
, 0, sizeof (pretty_printer
));
488 pp
->buffer
= xcalloc (1, sizeof (output_buffer
));
489 obstack_init (&pp
->buffer
->obstack
);
490 pp
->buffer
->stream
= stderr
;
491 pp_line_cutoff (pp
) = maximum_length
;
492 pp_prefixing_rule (pp
) = DIAGNOSTICS_SHOW_PREFIX_ONCE
;
493 pp_set_prefix (pp
, prefix
);
496 /* Append a string delimited by START and END to the output area of
497 PRETTY-PRINTER. No line wrapping is done. However, if beginning a
498 new line then emit PRETTY-PRINTER's prefix and skip any leading
499 whitespace if appropriate. The caller must ensure that it is
502 pp_base_append_text (pretty_printer
*pp
, const char *start
, const char *end
)
504 /* Emit prefix and skip whitespace if we're starting a new line. */
505 if (pp
->buffer
->line_length
== 0)
508 if (pp_is_wrapping_line (pp
))
509 while (start
!= end
&& *start
== ' ')
512 pp_append_r (pp
, start
, end
- start
);
515 /* Finishes constructing a NULL-terminated character string representing
516 the PRETTY-PRINTED text. */
518 pp_base_formatted_text (pretty_printer
*pp
)
520 obstack_1grow (&pp
->buffer
->obstack
, '\0');
521 return pp_formatted_text_data (pp
);
524 /* Return a pointer to the last character emitted in PRETTY-PRINTER's
525 output area. A NULL pointer means no character available. */
527 pp_base_last_position_in_text (const pretty_printer
*pp
)
529 const char *p
= NULL
;
530 struct obstack
*text
= &pp
->buffer
->obstack
;
532 if (obstack_base (text
) != obstack_next_free (text
))
533 p
= ((const char *) obstack_next_free (text
)) - 1;
537 /* Return the amount of characters PRETTY-PRINTER can accept to
538 make a full line. Meaningful only in line-wrapping mode. */
540 pp_base_remaining_character_count_for_line (pretty_printer
*pp
)
542 return pp
->maximum_length
- pp
->buffer
->line_length
;
546 /* Format a message into BUFFER a la printf. */
548 pp_printf (pretty_printer
*pp
, const char *msg
, ...)
556 text
.format_spec
= msg
;
557 pp_format_text (pp
, &text
);
562 /* Output MESSAGE verbatim into BUFFER. */
564 pp_verbatim (pretty_printer
*pp
, const char *msg
, ...)
572 text
.format_spec
= msg
;
573 pp_format_verbatim (pp
, &text
);
579 /* Have PRETTY-PRINTER start a new line. */
581 pp_base_newline (pretty_printer
*pp
)
583 obstack_1grow (&pp
->buffer
->obstack
, '\n');
584 pp
->buffer
->line_length
= 0;
587 /* Have PRETTY-PRINTER add a CHARACTER. */
589 pp_base_character (pretty_printer
*pp
, int c
)
591 if (pp_is_wrapping_line (pp
)
592 && pp_remaining_character_count_for_line (pp
) <= 0)
598 obstack_1grow (&pp
->buffer
->obstack
, c
);
599 ++pp
->buffer
->line_length
;
602 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
603 be line-wrapped if in appropriate mode. */
605 pp_base_string (pretty_printer
*pp
, const char *str
)
607 pp_maybe_wrap_text (pp
, str
, str
+ (str
? strlen (str
) : 0));
610 /* Maybe print out a whitespace if needed. */
613 pp_base_maybe_space (pretty_printer
*pp
)
615 if (pp_base (pp
)->padding
!= pp_none
)
618 pp_base (pp
)->padding
= pp_none
;