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, 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
);
371 gcc_assert (pp_format_decoder (pp
));
372 ok
= pp_format_decoder (pp
) (pp
, text
);
377 pp_string (pp
, close_quote
);
381 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
382 settings needed by BUFFER for a verbatim formatting. */
384 pp_base_format_verbatim (pretty_printer
*pp
, text_info
*text
)
386 diagnostic_prefixing_rule_t rule
= pp_prefixing_rule (pp
);
387 int line_cutoff
= pp_line_cutoff (pp
);
389 /* Set verbatim mode. */
390 pp
->prefixing_rule
= DIAGNOSTICS_SHOW_PREFIX_NEVER
;
391 pp_line_cutoff (pp
) = 0;
392 /* Do the actual formatting. */
393 pp_format_text (pp
, text
);
394 /* Restore previous settings. */
395 pp_prefixing_rule (pp
) = rule
;
396 pp_line_cutoff (pp
) = line_cutoff
;
399 /* Flush the content of BUFFER onto the attached stream. */
401 pp_base_flush (pretty_printer
*pp
)
403 pp_write_text_to_stream (pp
);
405 fputc ('\n', pp
->buffer
->stream
);
406 fflush (pp
->buffer
->stream
);
407 pp_needs_newline (pp
) = false;
410 /* Sets the number of maximum characters per line PRETTY-PRINTER can
411 output in line-wrapping mode. A LENGTH value 0 suppresses
414 pp_base_set_line_maximum_length (pretty_printer
*pp
, int length
)
416 pp_line_cutoff (pp
) = length
;
417 pp_set_real_maximum_length (pp
);
420 /* Clear PRETTY-PRINTER output area text info. */
422 pp_base_clear_output_area (pretty_printer
*pp
)
424 obstack_free (&pp
->buffer
->obstack
, obstack_base (&pp
->buffer
->obstack
));
425 pp
->buffer
->line_length
= 0;
428 /* Set PREFIX for PRETTY-PRINTER. */
430 pp_base_set_prefix (pretty_printer
*pp
, const char *prefix
)
433 pp_set_real_maximum_length (pp
);
434 pp
->emitted_prefix
= false;
435 pp_indentation (pp
) = 0;
438 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
440 pp_base_destroy_prefix (pretty_printer
*pp
)
442 if (pp
->prefix
!= NULL
)
444 free ((char *) pp
->prefix
);
449 /* Write out PRETTY-PRINTER's prefix. */
451 pp_base_emit_prefix (pretty_printer
*pp
)
453 if (pp
->prefix
!= NULL
)
455 switch (pp_prefixing_rule (pp
))
458 case DIAGNOSTICS_SHOW_PREFIX_NEVER
:
461 case DIAGNOSTICS_SHOW_PREFIX_ONCE
:
462 if (pp
->emitted_prefix
)
467 pp_indentation (pp
) += 3;
470 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
:
472 int prefix_length
= strlen (pp
->prefix
);
473 pp_append_r (pp
, pp
->prefix
, prefix_length
);
474 pp
->emitted_prefix
= true;
481 /* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
482 characters per line. */
484 pp_construct (pretty_printer
*pp
, const char *prefix
, int maximum_length
)
486 memset (pp
, 0, sizeof (pretty_printer
));
487 pp
->buffer
= xcalloc (1, sizeof (output_buffer
));
488 obstack_init (&pp
->buffer
->obstack
);
489 pp
->buffer
->stream
= stderr
;
490 pp_line_cutoff (pp
) = maximum_length
;
491 pp_prefixing_rule (pp
) = DIAGNOSTICS_SHOW_PREFIX_ONCE
;
492 pp_set_prefix (pp
, prefix
);
495 /* Append a string delimited by START and END to the output area of
496 PRETTY-PRINTER. No line wrapping is done. However, if beginning a
497 new line then emit PRETTY-PRINTER's prefix and skip any leading
498 whitespace if appropriate. The caller must ensure that it is
501 pp_base_append_text (pretty_printer
*pp
, const char *start
, const char *end
)
503 /* Emit prefix and skip whitespace if we're starting a new line. */
504 if (pp
->buffer
->line_length
== 0)
507 if (pp_is_wrapping_line (pp
))
508 while (start
!= end
&& *start
== ' ')
511 pp_append_r (pp
, start
, end
- start
);
514 /* Finishes constructing a NULL-terminated character string representing
515 the PRETTY-PRINTED text. */
517 pp_base_formatted_text (pretty_printer
*pp
)
519 obstack_1grow (&pp
->buffer
->obstack
, '\0');
520 return pp_formatted_text_data (pp
);
523 /* Return a pointer to the last character emitted in PRETTY-PRINTER's
524 output area. A NULL pointer means no character available. */
526 pp_base_last_position_in_text (const pretty_printer
*pp
)
528 const char *p
= NULL
;
529 struct obstack
*text
= &pp
->buffer
->obstack
;
531 if (obstack_base (text
) != obstack_next_free (text
))
532 p
= ((const char *) obstack_next_free (text
)) - 1;
536 /* Return the amount of characters PRETTY-PRINTER can accept to
537 make a full line. Meaningful only in line-wrapping mode. */
539 pp_base_remaining_character_count_for_line (pretty_printer
*pp
)
541 return pp
->maximum_length
- pp
->buffer
->line_length
;
545 /* Format a message into BUFFER a la printf. */
547 pp_printf (pretty_printer
*pp
, const char *msg
, ...)
555 text
.format_spec
= msg
;
556 pp_format_text (pp
, &text
);
561 /* Output MESSAGE verbatim into BUFFER. */
563 pp_verbatim (pretty_printer
*pp
, const char *msg
, ...)
571 text
.format_spec
= msg
;
572 pp_format_verbatim (pp
, &text
);
578 /* Have PRETTY-PRINTER start a new line. */
580 pp_base_newline (pretty_printer
*pp
)
582 obstack_1grow (&pp
->buffer
->obstack
, '\n');
583 pp
->buffer
->line_length
= 0;
586 /* Have PRETTY-PRINTER add a CHARACTER. */
588 pp_base_character (pretty_printer
*pp
, int c
)
590 if (pp_is_wrapping_line (pp
)
591 && pp_remaining_character_count_for_line (pp
) <= 0)
597 obstack_1grow (&pp
->buffer
->obstack
, c
);
598 ++pp
->buffer
->line_length
;
601 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
602 be line-wrapped if in appropriate mode. */
604 pp_base_string (pretty_printer
*pp
, const char *str
)
606 pp_maybe_wrap_text (pp
, str
, str
+ (str
? strlen (str
) : 0));
609 /* Maybe print out a whitespace if needed. */
612 pp_base_maybe_space (pretty_printer
*pp
)
614 if (pp_base (pp
)->padding
!= pp_none
)
617 pp_base (pp
)->padding
= pp_none
;