Makefile.in (stmp-docobjdir): New target; ensure $docobjdir exists.
[official-gcc.git] / gcc / pretty-print.c
blobb1190632b27fcb60f49019f40351b030e54511c1
1 /* Various declarations for language-independent pretty-print subroutines.
2 Copyright (C) 2003 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 "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) \
40 do \
41 switch (PREC) \
42 { \
43 case 0: \
44 pp_scalar (PP, "%" F, va_arg (ARG, T)); \
45 break; \
47 case 1: \
48 pp_scalar (PP, "%l" F, va_arg (ARG, long T)); \
49 break; \
51 case 2: \
52 pp_scalar (PP, "%ll" F, va_arg (ARG, long long T)); \
53 break; \
55 default: \
56 break; \
57 } \
58 while (0)
61 /* Subroutine of pp_set_maximum_length. Set up PRETTY-PRINTER's
62 internal maximum characters per line. */
63 static void
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);
73 else
75 int prefix_length = pp->prefix ? strlen (pp->prefix) : 0;
76 /* If the prefix is ridiculously too long, output at least
77 32 characters. */
78 if (pp_line_cutoff (pp) - prefix_length < 32)
79 pp->maximum_length = pp_line_cutoff (pp) + 32;
80 else
81 pp->maximum_length = pp_line_cutoff (pp);
85 /* Clear PRETTY-PRINTER's output state. */
86 static inline void
87 pp_clear_state (pretty_printer *pp)
89 pp->emitted_prefix = false;
90 pp_indentation (pp) = 0;
93 /* Insert enough spaces into the output area of PRETTY-PRINTER to bring
94 the column position to the current indentation level, assuming that a
95 newline has just been written to the buffer. */
96 static void
97 pp_indent (pretty_printer *pp)
99 int n = pp_indentation (pp);
100 int i;
102 for (i = 0; i < n; ++i)
103 pp_space (pp);
106 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream. */
107 static inline void
108 pp_write_text_to_stream (pretty_printer *pp)
110 const char *text = pp_formatted_text (pp);
111 fputs (text, pp->buffer->stream);
112 pp_clear_output_area (pp);
115 /* Wrap a text delimited by START and END into PRETTY-PRINTER. */
116 static void
117 pp_wrap_text (pretty_printer *pp, const char *start, const char *end)
119 bool wrapping_line = pp_is_wrapping_line (pp);
121 while (start != end)
123 /* Dump anything bordered by whitespaces. */
125 const char *p = start;
126 while (p != end && !ISBLANK (*p) && *p != '\n')
127 ++p;
128 if (wrapping_line
129 && p - start >= pp_remaining_character_count_for_line (pp))
130 pp_newline (pp);
131 pp_append_text (pp, start, p);
132 start = p;
135 if (start != end && ISBLANK (*start))
137 pp_space (pp);
138 ++start;
140 if (start != end && *start == '\n')
142 pp_newline (pp);
143 ++start;
148 /* Same as pp_wrap_text but wrap text only when in line-wrapping mode. */
149 static inline void
150 pp_maybe_wrap_text (pretty_printer *pp, const char *start, const char *end)
152 if (pp_is_wrapping_line (pp))
153 pp_wrap_text (pp, start, end);
154 else
155 pp_append_text (pp, start, end);
158 /* Append to the output area of PRETTY-PRINTER a string specified by its
159 STARTing character and LENGTH. */
160 static inline void
161 pp_append_r (pretty_printer *pp, const char *start, int length)
163 obstack_grow (&pp->buffer->obstack, start, length);
164 pp->buffer->line_length += length;
167 /* Format a message pointed to by TEXT. The following format specifiers are
168 recognized as being client independent:
169 %d, %i: (signed) integer in base ten.
170 %u: unsigned integer in base ten.
171 %o: unsigned integer in base eight.
172 %x: unsigned integer in base sixteen.
173 %ld, %li, %lo, %lu, %lx: long versions of the above.
174 %lld, %lli, %llo, %llu, %llx: long long versions.
175 %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
176 %c: character.
177 %s: string.
178 %p: pointer.
179 %m: strerror(text->err_no) - does not consume a value from args_ptr.
180 %%: `%'.
181 %*.s: a substring the length of which is specified by an integer.
182 %H: location_t. */
183 void
184 pp_format_text (pretty_printer *pp, text_info *text)
186 for (; *text->format_spec; ++text->format_spec)
188 int precision = 0;
189 bool wide = false;
191 /* Ignore text. */
193 const char *p = text->format_spec;
194 while (*p && *p != '%')
195 ++p;
196 pp_wrap_text (pp, text->format_spec, p);
197 text->format_spec = p;
200 if (*text->format_spec == '\0')
201 break;
203 /* We got a '%'. Parse precision modifiers, if any. */
204 switch (*++text->format_spec)
206 case 'w':
207 wide = true;
208 ++text->format_spec;
209 break;
211 case 'l':
213 ++precision;
214 while (*++text->format_spec == 'l');
215 break;
217 default:
218 break;
220 /* We don't support precision behond that of "long long". */
221 if (precision > 2)
222 abort();
224 switch (*text->format_spec)
226 case 'c':
227 pp_character (pp, va_arg (*text->args_ptr, int));
228 break;
230 case 'd':
231 case 'i':
232 if (wide)
233 pp_wide_integer (pp, va_arg (*text->args_ptr, HOST_WIDE_INT));
234 else
235 pp_integer_with_precision
236 (pp, *text->args_ptr, precision, int, "d");
237 break;
239 case 'o':
240 if (wide)
241 pp_scalar (pp, "%" HOST_WIDE_INT_PRINT "o",
242 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
243 else
244 pp_integer_with_precision
245 (pp, *text->args_ptr, precision, unsigned, "u");
246 break;
248 case 's':
249 pp_string (pp, va_arg (*text->args_ptr, const char *));
250 break;
252 case 'p':
253 pp_pointer (pp, va_arg (*text->args_ptr, void *));
254 break;
256 case 'u':
257 if (wide)
258 pp_scalar (pp, HOST_WIDE_INT_PRINT_UNSIGNED,
259 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
260 else
261 pp_integer_with_precision
262 (pp, *text->args_ptr, precision, unsigned, "u");
263 break;
265 case 'x':
266 if (wide)
267 pp_scalar (pp, HOST_WIDE_INT_PRINT_HEX,
268 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
269 else
270 pp_integer_with_precision
271 (pp, *text->args_ptr, precision, unsigned, "x");
272 break;
274 case 'm':
275 pp_string (pp, xstrerror (text->err_no));
276 break;
278 case '%':
279 pp_character (pp, '%');
280 break;
282 case 'H':
284 const location_t *locus = va_arg (*text->args_ptr, location_t *);
285 pp_string (pp, "file '");
286 pp_string (pp, locus->file);
287 pp_string (pp, "', line ");
288 pp_decimal_int (pp, locus->line);
290 break;
292 case '.':
294 int n;
295 const char *s;
296 /* We handle no precision specifier but `%.*s'. */
297 if (*++text->format_spec != '*')
298 abort ();
299 else if (*++text->format_spec != 's')
300 abort ();
301 n = va_arg (*text->args_ptr, int);
302 s = va_arg (*text->args_ptr, const char *);
303 pp_append_text (pp, s, s + n);
305 break;
307 default:
308 if (!pp_format_decoder (pp) || !(*pp_format_decoder (pp)) (pp, text))
310 /* Hmmm. The client failed to install a format translator
311 but called us with an unrecognized format. Or, maybe, the
312 translated string just contains an invalid format, or
313 has formats in the wrong order. Sorry. */
314 abort ();
320 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
321 settings needed by BUFFER for a verbatim formatting. */
322 void
323 pp_format_verbatim (pretty_printer *pp, text_info *text)
325 diagnostic_prefixing_rule_t rule = pp_prefixing_rule (pp);
326 int line_cutoff = pp_line_cutoff (pp);
328 /* Set verbatim mode. */
329 pp->prefixing_rule = DIAGNOSTICS_SHOW_PREFIX_NEVER;
330 pp_line_cutoff (pp) = 0;
331 /* Do the actual formatting. */
332 pp_format_text (pp, text);
333 /* Restore previous settings. */
334 pp_prefixing_rule (pp) = rule;
335 pp_line_cutoff (pp) = line_cutoff;
338 /* Flush the content of BUFFER onto the attached stream. */
339 void
340 pp_flush (pretty_printer *pp)
342 pp_write_text_to_stream (pp);
343 pp_clear_state (pp);
344 fputc ('\n', pp->buffer->stream);
345 fflush (pp->buffer->stream);
348 /* Sets the number of maximum characters per line PRETTY-PRINTER can
349 output in line-wrapping mode. A LENGTH value 0 suppresses
350 line-wrapping. */
351 void
352 pp_set_line_maximum_length (pretty_printer *pp, int length)
354 pp_line_cutoff (pp) = length;
355 pp_set_real_maximum_length (pp);
358 /* Clear PRETTY-PRINTER output area text info. */
359 void
360 pp_clear_output_area (pretty_printer *pp)
362 obstack_free (&pp->buffer->obstack, obstack_base (&pp->buffer->obstack));
363 pp->buffer->line_length = 0;
366 /* Set PREFIX for PRETTY-PRINTER. */
367 void
368 pp_set_prefix (pretty_printer *pp, const char *prefix)
370 pp->prefix = prefix;
371 pp_set_real_maximum_length (pp);
372 pp->emitted_prefix = false;
373 pp_indentation (pp) = 0;
376 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
377 void
378 pp_destroy_prefix (pretty_printer *pp)
380 if (pp->prefix != NULL)
382 free ((char *) pp->prefix);
383 pp->prefix = NULL;
387 /* Write out PRETTY-PRINTER's prefix. */
388 void
389 pp_emit_prefix (pretty_printer *pp)
391 if (pp->prefix != NULL)
393 switch (pp_prefixing_rule (pp))
395 default:
396 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
397 break;
399 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
400 if (pp->emitted_prefix)
402 pp_indent (pp);
403 break;
405 pp_indentation (pp) += 3;
406 /* Fall through. */
408 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
410 int prefix_length = strlen (pp->prefix);
411 pp_append_r (pp, pp->prefix, prefix_length);
412 pp->emitted_prefix = true;
414 break;
419 /* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
420 characters per line. */
421 void
422 pp_construct (pretty_printer *pp, const char *prefix, int maximum_length)
424 memset (pp, 0, sizeof (pretty_printer));
425 pp->buffer = xmalloc (sizeof (output_buffer));
426 obstack_init (&pp->buffer->obstack);
427 pp->buffer->stream = stderr;
428 pp_line_cutoff (pp) = maximum_length;
429 pp_prefixing_rule (pp) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
430 pp_set_prefix (pp, prefix);
433 /* Append a string delimited by START and END to the output area of
434 PRETTY-PRINTER. No line wrapping is done. However, if beginning a
435 new line then emit PRETTY-PRINTER's prefix and skip any leading
436 whitespace if appropriate. The caller must ensure that it is
437 safe to do so. */
438 void
439 pp_append_text (pretty_printer *pp, const char *start, const char *end)
441 /* Emit prefix and skip whitespace if we're starting a new line. */
442 if (pp->buffer->line_length == 0)
444 pp_emit_prefix (pp);
445 if (pp_is_wrapping_line (pp))
446 while (start != end && *start == ' ')
447 ++start;
449 pp_append_r (pp, start, end - start);
452 /* Finishes constructing a NULL-terminated character string representing
453 the PRETTY-PRINTED text. */
454 const char *
455 pp_formatted_text (pretty_printer *pp)
457 obstack_1grow (&pp->buffer->obstack, '\0');
458 return pp_formatted_text_data (pp);
461 /* Return a pointer to the last character emitted in PRETTY-PRINTER's
462 output area. A NULL pointer means no character available. */
463 const char *
464 pp_last_position_in_text (const pretty_printer *pp)
466 const char *p = NULL;
467 struct obstack *text = &pp->buffer->obstack;
469 if (obstack_base (text) != obstack_next_free (text))
470 p = ((const char *) obstack_next_free (text)) - 1;
471 return p;
474 /* Return the amount of characters PRETTY-PRINTER can accept to
475 make a full line. Meaningfull only in line-wrapping mode. */
477 pp_remaining_character_count_for_line (pretty_printer *pp)
479 return pp->maximum_length - pp->buffer->line_length;
483 /* Format a message into BUFFER a la printf. */
484 void
485 pp_printf (pretty_printer *pp, const char *msg, ...)
487 text_info text;
488 va_list ap;
490 va_start (ap, msg);
491 text.err_no = errno;
492 text.args_ptr = &ap;
493 text.format_spec = msg;
494 pp_format_text (pp, &text);
495 va_end (ap);
499 /* Output MESSAGE verbatim into BUFFER. */
500 void
501 pp_verbatim (pretty_printer *pp, const char *msg, ...)
503 text_info text;
504 va_list ap;
506 va_start (ap, msg);
507 text.err_no = errno;
508 text.args_ptr = &ap;
509 text.format_spec = msg;
510 pp_format_verbatim (pp, &text);
511 va_end (ap);
516 /* Have PRETTY-PRINTER start a new line. */
517 void
518 pp_newline (pretty_printer *pp)
520 obstack_1grow (&pp->buffer->obstack, '\n');
521 pp->buffer->line_length = 0;
524 /* Have PRETTY-PRINTER add a CHARACTER. */
525 void
526 pp_character (pretty_printer *pp, int c)
528 if (pp_is_wrapping_line (pp)
529 && pp_remaining_character_count_for_line (pp) <= 0)
531 pp_newline (pp);
532 if (ISSPACE (c))
533 return;
535 obstack_1grow (&pp->buffer->obstack, c);
536 ++pp->buffer->line_length;
539 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
540 be line-wrapped if in appropriate mode. */
541 void
542 pp_string (pretty_printer *pp, const char *str)
544 pp_maybe_wrap_text (pp, str, str + (str ? strlen (str) : 0));