linux-unwind.h: Add exception clause to copyright.
[official-gcc.git] / gcc / pretty-print.c
blob3a10e7600c6788c4f4064248a1b201901580a20c
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
10 version.
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
15 for more details.
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
20 02111-1307, USA. */
22 #include "config.h"
23 #undef FLOAT /* This is for hpux. They should change hpux. */
24 #undef FFS /* Some systems define this in param.h. */
25 #include "system.h"
26 #include "coretypes.h"
27 #include "intl.h"
28 #include "pretty-print.h"
29 #include "tree.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) \
42 do \
43 switch (PREC) \
44 { \
45 case 0: \
46 pp_scalar (PP, "%" F, va_arg (ARG, T)); \
47 break; \
49 case 1: \
50 pp_scalar (PP, "%l" F, va_arg (ARG, long T)); \
51 break; \
53 case 2: \
54 pp_scalar (PP, "%ll" F, va_arg (ARG, long long T)); \
55 break; \
57 default: \
58 break; \
59 } \
60 while (0)
63 /* Subroutine of pp_set_maximum_length. Set up PRETTY-PRINTER's
64 internal maximum characters per line. */
65 static void
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);
75 else
77 int prefix_length = pp->prefix ? strlen (pp->prefix) : 0;
78 /* If the prefix is ridiculously too long, output at least
79 32 characters. */
80 if (pp_line_cutoff (pp) - prefix_length < 32)
81 pp->maximum_length = pp_line_cutoff (pp) + 32;
82 else
83 pp->maximum_length = pp_line_cutoff (pp);
87 /* Clear PRETTY-PRINTER's output state. */
88 static inline void
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. */
96 void
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. */
105 static void
106 pp_wrap_text (pretty_printer *pp, const char *start, const char *end)
108 bool wrapping_line = pp_is_wrapping_line (pp);
110 while (start != end)
112 /* Dump anything bordered by whitespaces. */
114 const char *p = start;
115 while (p != end && !ISBLANK (*p) && *p != '\n')
116 ++p;
117 if (wrapping_line
118 && p - start >= pp_remaining_character_count_for_line (pp))
119 pp_newline (pp);
120 pp_append_text (pp, start, p);
121 start = p;
124 if (start != end && ISBLANK (*start))
126 pp_space (pp);
127 ++start;
129 if (start != end && *start == '\n')
131 pp_newline (pp);
132 ++start;
137 /* Same as pp_wrap_text but wrap text only when in line-wrapping mode. */
138 static inline void
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);
143 else
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. */
149 static inline void
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. */
159 void
160 pp_base_indent (pretty_printer *pp)
162 int n = pp_indentation (pp);
163 int i;
165 for (i = 0; i < n; ++i)
166 pp_space (pp);
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. */
177 void
178 pp_base_prepare_to_format (pretty_printer *pp ATTRIBUTE_UNUSED,
179 text_info *text,
180 location_t *locus)
182 const char *p = text->format_spec;
183 tree t;
185 /* Extract the location information if any. */
186 if (p[0] == '%')
187 switch (p[1])
189 case 'H':
190 *locus = *va_arg (*text->args_ptr, location_t *);
191 text->format_spec = p + 2;
192 break;
194 case 'J':
195 t = va_arg (*text->args_ptr, tree);
196 *locus = DECL_SOURCE_LOCATION (t);
197 text->format_spec = p + 2;
198 break;
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.
212 %c: character.
213 %s: string.
214 %p: pointer.
215 %m: strerror(text->err_no) - does not consume a value from args_ptr.
216 %%: '%'.
217 %<: opening quote.
218 %>: closing quote.
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.
222 %H: location_t.
223 Flag 'q': quote formatted text (must come immediately after '%'). */
224 void
225 pp_base_format_text (pretty_printer *pp, text_info *text)
227 for (; *text->format_spec; ++text->format_spec)
229 int precision = 0;
230 bool wide = false;
231 bool quoted = false;
233 /* Ignore text. */
235 const char *p = text->format_spec;
236 while (*p && *p != '%')
237 ++p;
238 pp_wrap_text (pp, text->format_spec, p);
239 text->format_spec = p;
242 if (*text->format_spec == '\0')
243 break;
245 /* We got a '%'. Check for 'q', then parse precision modifiers,
246 if any. */
247 if (*++text->format_spec == 'q')
249 quoted = true;
250 ++text->format_spec;
252 switch (*text->format_spec)
254 case 'w':
255 wide = true;
256 ++text->format_spec;
257 break;
259 case 'l':
261 ++precision;
262 while (*++text->format_spec == 'l');
263 break;
265 default:
266 break;
268 /* We don't support precision beyond that of "long long". */
269 gcc_assert (precision <= 2);
271 if (quoted)
272 pp_string (pp, open_quote);
273 switch (*text->format_spec)
275 case 'c':
276 pp_character (pp, va_arg (*text->args_ptr, int));
277 break;
279 case 'd':
280 case 'i':
281 if (wide)
282 pp_wide_integer (pp, va_arg (*text->args_ptr, HOST_WIDE_INT));
283 else
284 pp_integer_with_precision
285 (pp, *text->args_ptr, precision, int, "d");
286 break;
288 case 'o':
289 if (wide)
290 pp_scalar (pp, "%" HOST_WIDE_INT_PRINT "o",
291 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
292 else
293 pp_integer_with_precision
294 (pp, *text->args_ptr, precision, unsigned, "u");
295 break;
297 case 's':
298 pp_string (pp, va_arg (*text->args_ptr, const char *));
299 break;
301 case 'p':
302 pp_pointer (pp, va_arg (*text->args_ptr, void *));
303 break;
305 case 'u':
306 if (wide)
307 pp_scalar (pp, HOST_WIDE_INT_PRINT_UNSIGNED,
308 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
309 else
310 pp_integer_with_precision
311 (pp, *text->args_ptr, precision, unsigned, "u");
312 break;
314 case 'x':
315 if (wide)
316 pp_scalar (pp, HOST_WIDE_INT_PRINT_HEX,
317 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
318 else
319 pp_integer_with_precision
320 (pp, *text->args_ptr, precision, unsigned, "x");
321 break;
323 case 'm':
324 pp_string (pp, xstrerror (text->err_no));
325 break;
327 case '%':
328 pp_character (pp, '%');
329 break;
331 case '<':
332 pp_string (pp, open_quote);
333 break;
335 case '>':
336 case '\'':
337 pp_string (pp, close_quote);
338 break;
340 case 'H':
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);
349 break;
351 case '.':
353 int n;
354 const char *s;
355 /* We handle no precision specifier but '%.*s'. */
356 ++text->format_spec;
357 gcc_assert (*text->format_spec == '*');
358 ++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);
365 break;
367 default:
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. */
374 abort ();
377 if (quoted)
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. */
384 void
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. */
401 void
402 pp_base_flush (pretty_printer *pp)
404 pp_write_text_to_stream (pp);
405 pp_clear_state (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
413 line-wrapping. */
414 void
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. */
422 void
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. */
430 void
431 pp_base_set_prefix (pretty_printer *pp, const char *prefix)
433 pp->prefix = 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. */
440 void
441 pp_base_destroy_prefix (pretty_printer *pp)
443 if (pp->prefix != NULL)
445 free ((char *) pp->prefix);
446 pp->prefix = NULL;
450 /* Write out PRETTY-PRINTER's prefix. */
451 void
452 pp_base_emit_prefix (pretty_printer *pp)
454 if (pp->prefix != NULL)
456 switch (pp_prefixing_rule (pp))
458 default:
459 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
460 break;
462 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
463 if (pp->emitted_prefix)
465 pp_base_indent (pp);
466 break;
468 pp_indentation (pp) += 3;
469 /* Fall through. */
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;
477 break;
482 /* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
483 characters per line. */
484 void
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
500 safe to do so. */
501 void
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)
507 pp_emit_prefix (pp);
508 if (pp_is_wrapping_line (pp))
509 while (start != end && *start == ' ')
510 ++start;
512 pp_append_r (pp, start, end - start);
515 /* Finishes constructing a NULL-terminated character string representing
516 the PRETTY-PRINTED text. */
517 const char *
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. */
526 const char *
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;
534 return p;
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. */
547 void
548 pp_printf (pretty_printer *pp, const char *msg, ...)
550 text_info text;
551 va_list ap;
553 va_start (ap, msg);
554 text.err_no = errno;
555 text.args_ptr = &ap;
556 text.format_spec = msg;
557 pp_format_text (pp, &text);
558 va_end (ap);
562 /* Output MESSAGE verbatim into BUFFER. */
563 void
564 pp_verbatim (pretty_printer *pp, const char *msg, ...)
566 text_info text;
567 va_list ap;
569 va_start (ap, msg);
570 text.err_no = errno;
571 text.args_ptr = &ap;
572 text.format_spec = msg;
573 pp_format_verbatim (pp, &text);
574 va_end (ap);
579 /* Have PRETTY-PRINTER start a new line. */
580 void
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. */
588 void
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)
594 pp_newline (pp);
595 if (ISSPACE (c))
596 return;
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. */
604 void
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. */
612 void
613 pp_base_maybe_space (pretty_printer *pp)
615 if (pp_base (pp)->padding != pp_none)
617 pp_space (pp);
618 pp_base (pp)->padding = pp_none;