]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/pretty-print.c
gcc_update (libbanshee/configure): Depend on libbanshee/configure.ac instead of libba...
[thirdparty/gcc.git] / gcc / pretty-print.c
CommitLineData
b6fe0bb8 1/* Various declarations for language-independent pretty-print subroutines.
ef335eb8 2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
b6fe0bb8
GDR
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
21
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"
a668adb2 27#include "intl.h"
b6fe0bb8
GDR
28#include "pretty-print.h"
29
30#define obstack_chunk_alloc xmalloc
31#define obstack_chunk_free free
32
33/* A pointer to the formatted diagnostic message. */
34#define pp_formatted_text_data(PP) \
35 ((const char *) obstack_base (&pp_base (PP)->buffer->obstack))
36
37/* Format an integer given by va_arg (ARG, type-specifier T) where
38 type-specifier is a precision modifier as indicated by PREC. F is
39 a string used to construct the appropriate format-specifier. */
40#define pp_integer_with_precision(PP, ARG, PREC, T, F) \
41 do \
42 switch (PREC) \
43 { \
44 case 0: \
45 pp_scalar (PP, "%" F, va_arg (ARG, T)); \
46 break; \
47 \
48 case 1: \
49 pp_scalar (PP, "%l" F, va_arg (ARG, long T)); \
50 break; \
51 \
52 case 2: \
53 pp_scalar (PP, "%ll" F, va_arg (ARG, long long T)); \
54 break; \
55 \
56 default: \
57 break; \
58 } \
59 while (0)
60
61
62/* Subroutine of pp_set_maximum_length. Set up PRETTY-PRINTER's
63 internal maximum characters per line. */
64static void
65pp_set_real_maximum_length (pretty_printer *pp)
66{
67 /* If we're told not to wrap lines then do the obvious thing. In case
68 we'll emit prefix only once per message, it is appropriate
69 not to increase unnecessarily the line-length cut-off. */
70 if (!pp_is_wrapping_line (pp)
71 || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_ONCE
72 || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
73 pp->maximum_length = pp_line_cutoff (pp);
74 else
75 {
76 int prefix_length = pp->prefix ? strlen (pp->prefix) : 0;
77 /* If the prefix is ridiculously too long, output at least
78 32 characters. */
79 if (pp_line_cutoff (pp) - prefix_length < 32)
80 pp->maximum_length = pp_line_cutoff (pp) + 32;
81 else
82 pp->maximum_length = pp_line_cutoff (pp);
83 }
84}
85
86/* Clear PRETTY-PRINTER's output state. */
87static inline void
88pp_clear_state (pretty_printer *pp)
89{
90 pp->emitted_prefix = false;
91 pp_indentation (pp) = 0;
92}
93
b6fe0bb8 94/* Flush the formatted text of PRETTY-PRINTER onto the attached stream. */
6de9cd9a 95void
b6fe0bb8
GDR
96pp_write_text_to_stream (pretty_printer *pp)
97{
98 const char *text = pp_formatted_text (pp);
99 fputs (text, pp->buffer->stream);
100 pp_clear_output_area (pp);
101}
102
103/* Wrap a text delimited by START and END into PRETTY-PRINTER. */
104static void
105pp_wrap_text (pretty_printer *pp, const char *start, const char *end)
106{
107 bool wrapping_line = pp_is_wrapping_line (pp);
108
109 while (start != end)
110 {
111 /* Dump anything bordered by whitespaces. */
112 {
113 const char *p = start;
114 while (p != end && !ISBLANK (*p) && *p != '\n')
115 ++p;
116 if (wrapping_line
117 && p - start >= pp_remaining_character_count_for_line (pp))
118 pp_newline (pp);
119 pp_append_text (pp, start, p);
120 start = p;
121 }
122
123 if (start != end && ISBLANK (*start))
124 {
125 pp_space (pp);
126 ++start;
127 }
128 if (start != end && *start == '\n')
129 {
130 pp_newline (pp);
131 ++start;
132 }
133 }
134}
135
136/* Same as pp_wrap_text but wrap text only when in line-wrapping mode. */
137static inline void
138pp_maybe_wrap_text (pretty_printer *pp, const char *start, const char *end)
139{
140 if (pp_is_wrapping_line (pp))
141 pp_wrap_text (pp, start, end);
142 else
143 pp_append_text (pp, start, end);
144}
145
146/* Append to the output area of PRETTY-PRINTER a string specified by its
147 STARTing character and LENGTH. */
148static inline void
149pp_append_r (pretty_printer *pp, const char *start, int length)
150{
151 obstack_grow (&pp->buffer->obstack, start, length);
152 pp->buffer->line_length += length;
153}
154
4b780675
GDR
155/* Insert enough spaces into the output area of PRETTY-PRINTER to bring
156 the column position to the current indentation level, assuming that a
157 newline has just been written to the buffer. */
158void
159pp_base_indent (pretty_printer *pp)
160{
161 int n = pp_indentation (pp);
162 int i;
163
164 for (i = 0; i < n; ++i)
165 pp_space (pp);
166}
167
b6fe0bb8
GDR
168/* Format a message pointed to by TEXT. The following format specifiers are
169 recognized as being client independent:
170 %d, %i: (signed) integer in base ten.
171 %u: unsigned integer in base ten.
172 %o: unsigned integer in base eight.
173 %x: unsigned integer in base sixteen.
174 %ld, %li, %lo, %lu, %lx: long versions of the above.
175 %lld, %lli, %llo, %llu, %llx: long long versions.
176 %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
177 %c: character.
178 %s: string.
179 %p: pointer.
180 %m: strerror(text->err_no) - does not consume a value from args_ptr.
a668adb2
JM
181 %%: '%'.
182 %`: opening quote.
183 %': closing quote.
cb83302c 184 %.*s: a substring the length of which is specified by an integer.
a668adb2
JM
185 %H: location_t.
186 Flag 'q': quote formatted text (must come immediately after '%'). */
b6fe0bb8 187void
e1a4dd13 188pp_base_format_text (pretty_printer *pp, text_info *text)
b6fe0bb8
GDR
189{
190 for (; *text->format_spec; ++text->format_spec)
191 {
192 int precision = 0;
193 bool wide = false;
a668adb2 194 bool quoted = false;
b6fe0bb8
GDR
195
196 /* Ignore text. */
197 {
198 const char *p = text->format_spec;
199 while (*p && *p != '%')
200 ++p;
201 pp_wrap_text (pp, text->format_spec, p);
202 text->format_spec = p;
203 }
204
205 if (*text->format_spec == '\0')
206 break;
207
a668adb2
JM
208 /* We got a '%'. Check for 'q', then parse precision modifiers,
209 if any. */
210 if (*++text->format_spec == 'q')
211 {
212 quoted = true;
213 ++text->format_spec;
214 }
215 switch (*text->format_spec)
b6fe0bb8
GDR
216 {
217 case 'w':
218 wide = true;
219 ++text->format_spec;
220 break;
221
222 case 'l':
223 do
224 ++precision;
225 while (*++text->format_spec == 'l');
226 break;
227
228 default:
229 break;
230 }
a98ebe2e 231 /* We don't support precision beyond that of "long long". */
b6fe0bb8
GDR
232 if (precision > 2)
233 abort();
234
a668adb2
JM
235 if (quoted)
236 pp_string (pp, open_quote);
b6fe0bb8
GDR
237 switch (*text->format_spec)
238 {
239 case 'c':
240 pp_character (pp, va_arg (*text->args_ptr, int));
241 break;
242
243 case 'd':
244 case 'i':
245 if (wide)
246 pp_wide_integer (pp, va_arg (*text->args_ptr, HOST_WIDE_INT));
247 else
248 pp_integer_with_precision
249 (pp, *text->args_ptr, precision, int, "d");
250 break;
251
252 case 'o':
253 if (wide)
254 pp_scalar (pp, "%" HOST_WIDE_INT_PRINT "o",
255 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
256 else
257 pp_integer_with_precision
258 (pp, *text->args_ptr, precision, unsigned, "u");
259 break;
260
261 case 's':
262 pp_string (pp, va_arg (*text->args_ptr, const char *));
263 break;
264
265 case 'p':
266 pp_pointer (pp, va_arg (*text->args_ptr, void *));
267 break;
268
269 case 'u':
270 if (wide)
271 pp_scalar (pp, HOST_WIDE_INT_PRINT_UNSIGNED,
272 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
273 else
274 pp_integer_with_precision
275 (pp, *text->args_ptr, precision, unsigned, "u");
276 break;
277
278 case 'x':
279 if (wide)
280 pp_scalar (pp, HOST_WIDE_INT_PRINT_HEX,
281 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
282 else
283 pp_integer_with_precision
284 (pp, *text->args_ptr, precision, unsigned, "x");
285 break;
286
287 case 'm':
288 pp_string (pp, xstrerror (text->err_no));
289 break;
290
291 case '%':
292 pp_character (pp, '%');
293 break;
294
a668adb2
JM
295 case '`':
296 pp_string (pp, open_quote);
297 break;
298
299 case '\'':
300 pp_string (pp, close_quote);
301 break;
302
b6fe0bb8
GDR
303 case 'H':
304 {
305 const location_t *locus = va_arg (*text->args_ptr, location_t *);
306 pp_string (pp, "file '");
307 pp_string (pp, locus->file);
308 pp_string (pp, "', line ");
309 pp_decimal_int (pp, locus->line);
310 }
311 break;
312
313 case '.':
314 {
315 int n;
316 const char *s;
a668adb2 317 /* We handle no precision specifier but '%.*s'. */
b6fe0bb8
GDR
318 if (*++text->format_spec != '*')
319 abort ();
320 else if (*++text->format_spec != 's')
321 abort ();
322 n = va_arg (*text->args_ptr, int);
323 s = va_arg (*text->args_ptr, const char *);
324 pp_append_text (pp, s, s + n);
325 }
326 break;
327
328 default:
329 if (!pp_format_decoder (pp) || !(*pp_format_decoder (pp)) (pp, text))
330 {
331 /* Hmmm. The client failed to install a format translator
332 but called us with an unrecognized format. Or, maybe, the
333 translated string just contains an invalid format, or
334 has formats in the wrong order. Sorry. */
335 abort ();
336 }
337 }
a668adb2
JM
338 if (quoted)
339 pp_string (pp, close_quote);
b6fe0bb8
GDR
340 }
341}
342
343/* Helper subroutine of output_verbatim and verbatim. Do the appropriate
344 settings needed by BUFFER for a verbatim formatting. */
345void
e1a4dd13 346pp_base_format_verbatim (pretty_printer *pp, text_info *text)
b6fe0bb8
GDR
347{
348 diagnostic_prefixing_rule_t rule = pp_prefixing_rule (pp);
349 int line_cutoff = pp_line_cutoff (pp);
350
351 /* Set verbatim mode. */
352 pp->prefixing_rule = DIAGNOSTICS_SHOW_PREFIX_NEVER;
353 pp_line_cutoff (pp) = 0;
354 /* Do the actual formatting. */
355 pp_format_text (pp, text);
356 /* Restore previous settings. */
357 pp_prefixing_rule (pp) = rule;
358 pp_line_cutoff (pp) = line_cutoff;
359}
360
361/* Flush the content of BUFFER onto the attached stream. */
362void
e1a4dd13 363pp_base_flush (pretty_printer *pp)
b6fe0bb8
GDR
364{
365 pp_write_text_to_stream (pp);
366 pp_clear_state (pp);
367 fputc ('\n', pp->buffer->stream);
368 fflush (pp->buffer->stream);
7b330e08 369 pp_needs_newline (pp) = false;
b6fe0bb8
GDR
370}
371
372/* Sets the number of maximum characters per line PRETTY-PRINTER can
373 output in line-wrapping mode. A LENGTH value 0 suppresses
374 line-wrapping. */
375void
e1a4dd13 376pp_base_set_line_maximum_length (pretty_printer *pp, int length)
b6fe0bb8
GDR
377{
378 pp_line_cutoff (pp) = length;
379 pp_set_real_maximum_length (pp);
380}
381
382/* Clear PRETTY-PRINTER output area text info. */
383void
e1a4dd13 384pp_base_clear_output_area (pretty_printer *pp)
b6fe0bb8
GDR
385{
386 obstack_free (&pp->buffer->obstack, obstack_base (&pp->buffer->obstack));
387 pp->buffer->line_length = 0;
388}
389
390/* Set PREFIX for PRETTY-PRINTER. */
391void
e1a4dd13 392pp_base_set_prefix (pretty_printer *pp, const char *prefix)
b6fe0bb8
GDR
393{
394 pp->prefix = prefix;
395 pp_set_real_maximum_length (pp);
396 pp->emitted_prefix = false;
397 pp_indentation (pp) = 0;
398}
399
400/* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
401void
e1a4dd13 402pp_base_destroy_prefix (pretty_printer *pp)
b6fe0bb8
GDR
403{
404 if (pp->prefix != NULL)
405 {
406 free ((char *) pp->prefix);
407 pp->prefix = NULL;
408 }
409}
410
411/* Write out PRETTY-PRINTER's prefix. */
412void
e1a4dd13 413pp_base_emit_prefix (pretty_printer *pp)
b6fe0bb8
GDR
414{
415 if (pp->prefix != NULL)
416 {
417 switch (pp_prefixing_rule (pp))
418 {
419 default:
420 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
421 break;
422
423 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
424 if (pp->emitted_prefix)
425 {
4b780675 426 pp_base_indent (pp);
b6fe0bb8
GDR
427 break;
428 }
429 pp_indentation (pp) += 3;
430 /* Fall through. */
431
432 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
433 {
434 int prefix_length = strlen (pp->prefix);
435 pp_append_r (pp, pp->prefix, prefix_length);
436 pp->emitted_prefix = true;
437 }
438 break;
439 }
440 }
441}
442
443/* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
444 characters per line. */
445void
446pp_construct (pretty_printer *pp, const char *prefix, int maximum_length)
447{
448 memset (pp, 0, sizeof (pretty_printer));
c4555dd9 449 pp->buffer = xcalloc (1, sizeof (output_buffer));
b6fe0bb8
GDR
450 obstack_init (&pp->buffer->obstack);
451 pp->buffer->stream = stderr;
452 pp_line_cutoff (pp) = maximum_length;
453 pp_prefixing_rule (pp) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
454 pp_set_prefix (pp, prefix);
455}
456
457/* Append a string delimited by START and END to the output area of
458 PRETTY-PRINTER. No line wrapping is done. However, if beginning a
459 new line then emit PRETTY-PRINTER's prefix and skip any leading
460 whitespace if appropriate. The caller must ensure that it is
461 safe to do so. */
462void
e1a4dd13 463pp_base_append_text (pretty_printer *pp, const char *start, const char *end)
b6fe0bb8
GDR
464{
465 /* Emit prefix and skip whitespace if we're starting a new line. */
466 if (pp->buffer->line_length == 0)
467 {
468 pp_emit_prefix (pp);
469 if (pp_is_wrapping_line (pp))
470 while (start != end && *start == ' ')
471 ++start;
472 }
473 pp_append_r (pp, start, end - start);
474}
475
476/* Finishes constructing a NULL-terminated character string representing
477 the PRETTY-PRINTED text. */
478const char *
e1a4dd13 479pp_base_formatted_text (pretty_printer *pp)
b6fe0bb8
GDR
480{
481 obstack_1grow (&pp->buffer->obstack, '\0');
482 return pp_formatted_text_data (pp);
483}
484
485/* Return a pointer to the last character emitted in PRETTY-PRINTER's
486 output area. A NULL pointer means no character available. */
487const char *
e1a4dd13 488pp_base_last_position_in_text (const pretty_printer *pp)
b6fe0bb8
GDR
489{
490 const char *p = NULL;
491 struct obstack *text = &pp->buffer->obstack;
492
493 if (obstack_base (text) != obstack_next_free (text))
494 p = ((const char *) obstack_next_free (text)) - 1;
495 return p;
496}
497
498/* Return the amount of characters PRETTY-PRINTER can accept to
ba228239 499 make a full line. Meaningful only in line-wrapping mode. */
b6fe0bb8 500int
e1a4dd13 501pp_base_remaining_character_count_for_line (pretty_printer *pp)
b6fe0bb8
GDR
502{
503 return pp->maximum_length - pp->buffer->line_length;
504}
505
506
507/* Format a message into BUFFER a la printf. */
508void
509pp_printf (pretty_printer *pp, const char *msg, ...)
510{
511 text_info text;
512 va_list ap;
513
514 va_start (ap, msg);
515 text.err_no = errno;
516 text.args_ptr = &ap;
517 text.format_spec = msg;
518 pp_format_text (pp, &text);
519 va_end (ap);
520}
521
522
523/* Output MESSAGE verbatim into BUFFER. */
524void
525pp_verbatim (pretty_printer *pp, const char *msg, ...)
526{
527 text_info text;
528 va_list ap;
529
530 va_start (ap, msg);
531 text.err_no = errno;
532 text.args_ptr = &ap;
533 text.format_spec = msg;
534 pp_format_verbatim (pp, &text);
535 va_end (ap);
536}
537
538
539
540/* Have PRETTY-PRINTER start a new line. */
541void
8f7ace48 542pp_base_newline (pretty_printer *pp)
b6fe0bb8
GDR
543{
544 obstack_1grow (&pp->buffer->obstack, '\n');
545 pp->buffer->line_length = 0;
546}
547
548/* Have PRETTY-PRINTER add a CHARACTER. */
549void
8f7ace48 550pp_base_character (pretty_printer *pp, int c)
b6fe0bb8
GDR
551{
552 if (pp_is_wrapping_line (pp)
553 && pp_remaining_character_count_for_line (pp) <= 0)
554 {
555 pp_newline (pp);
556 if (ISSPACE (c))
557 return;
558 }
559 obstack_1grow (&pp->buffer->obstack, c);
560 ++pp->buffer->line_length;
561}
562
563/* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
564 be line-wrapped if in appropriate mode. */
565void
8f7ace48 566pp_base_string (pretty_printer *pp, const char *str)
b6fe0bb8
GDR
567{
568 pp_maybe_wrap_text (pp, str, str + (str ? strlen (str) : 0));
569}
570
b9b44fb9 571/* Maybe print out a whitespace if needed. */
b6fe0bb8 572
b9b44fb9
GDR
573void
574pp_base_maybe_space (pretty_printer *pp)
575{
576 if (pp_base (pp)->padding != pp_none)
577 {
578 pp_space (pp);
579 pp_base (pp)->padding = pp_none;
580 }
581}