* config/i386/i386.c (output_fp_compare): Add generation
[official-gcc.git] / gcc / pretty-print.c
blobd6098a7474513422de22f970fd58f67fa91e5627
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"
30 #define obstack_chunk_alloc xmalloc
31 #define obstack_chunk_free free
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))
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; \
48 case 1: \
49 pp_scalar (PP, "%l" F, va_arg (ARG, long T)); \
50 break; \
52 case 2: \
53 pp_scalar (PP, "%ll" F, va_arg (ARG, long long T)); \
54 break; \
56 default: \
57 break; \
58 } \
59 while (0)
62 /* Subroutine of pp_set_maximum_length. Set up PRETTY-PRINTER's
63 internal maximum characters per line. */
64 static void
65 pp_set_real_maximum_length (pretty_printer *pp)
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
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);
86 /* Clear PRETTY-PRINTER's output state. */
87 static inline void
88 pp_clear_state (pretty_printer *pp)
90 pp->emitted_prefix = false;
91 pp_indentation (pp) = 0;
94 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream. */
95 void
96 pp_write_text_to_stream (pretty_printer *pp)
98 const char *text = pp_formatted_text (pp);
99 fputs (text, pp->buffer->stream);
100 pp_clear_output_area (pp);
103 /* Wrap a text delimited by START and END into PRETTY-PRINTER. */
104 static void
105 pp_wrap_text (pretty_printer *pp, const char *start, const char *end)
107 bool wrapping_line = pp_is_wrapping_line (pp);
109 while (start != end)
111 /* Dump anything bordered by whitespaces. */
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;
123 if (start != end && ISBLANK (*start))
125 pp_space (pp);
126 ++start;
128 if (start != end && *start == '\n')
130 pp_newline (pp);
131 ++start;
136 /* Same as pp_wrap_text but wrap text only when in line-wrapping mode. */
137 static inline void
138 pp_maybe_wrap_text (pretty_printer *pp, const char *start, const char *end)
140 if (pp_is_wrapping_line (pp))
141 pp_wrap_text (pp, start, end);
142 else
143 pp_append_text (pp, start, end);
146 /* Append to the output area of PRETTY-PRINTER a string specified by its
147 STARTing character and LENGTH. */
148 static inline void
149 pp_append_r (pretty_printer *pp, const char *start, int length)
151 obstack_grow (&pp->buffer->obstack, start, length);
152 pp->buffer->line_length += length;
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. */
158 void
159 pp_base_indent (pretty_printer *pp)
161 int n = pp_indentation (pp);
162 int i;
164 for (i = 0; i < n; ++i)
165 pp_space (pp);
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.
181 %%: '%'.
182 %<: opening quote.
183 %>: closing quote.
184 %': apostrophe (should only be used in untranslated messages;
185 translations should use appropriate punctuation directly).
186 %.*s: a substring the length of which is specified by an integer.
187 %H: location_t.
188 Flag 'q': quote formatted text (must come immediately after '%'). */
189 void
190 pp_base_format_text (pretty_printer *pp, text_info *text)
192 for (; *text->format_spec; ++text->format_spec)
194 int precision = 0;
195 bool wide = false;
196 bool quoted = false;
198 /* Ignore text. */
200 const char *p = text->format_spec;
201 while (*p && *p != '%')
202 ++p;
203 pp_wrap_text (pp, text->format_spec, p);
204 text->format_spec = p;
207 if (*text->format_spec == '\0')
208 break;
210 /* We got a '%'. Check for 'q', then parse precision modifiers,
211 if any. */
212 if (*++text->format_spec == 'q')
214 quoted = true;
215 ++text->format_spec;
217 switch (*text->format_spec)
219 case 'w':
220 wide = true;
221 ++text->format_spec;
222 break;
224 case 'l':
226 ++precision;
227 while (*++text->format_spec == 'l');
228 break;
230 default:
231 break;
233 /* We don't support precision beyond that of "long long". */
234 if (precision > 2)
235 abort();
237 if (quoted)
238 pp_string (pp, open_quote);
239 switch (*text->format_spec)
241 case 'c':
242 pp_character (pp, va_arg (*text->args_ptr, int));
243 break;
245 case 'd':
246 case 'i':
247 if (wide)
248 pp_wide_integer (pp, va_arg (*text->args_ptr, HOST_WIDE_INT));
249 else
250 pp_integer_with_precision
251 (pp, *text->args_ptr, precision, int, "d");
252 break;
254 case 'o':
255 if (wide)
256 pp_scalar (pp, "%" HOST_WIDE_INT_PRINT "o",
257 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
258 else
259 pp_integer_with_precision
260 (pp, *text->args_ptr, precision, unsigned, "u");
261 break;
263 case 's':
264 pp_string (pp, va_arg (*text->args_ptr, const char *));
265 break;
267 case 'p':
268 pp_pointer (pp, va_arg (*text->args_ptr, void *));
269 break;
271 case 'u':
272 if (wide)
273 pp_scalar (pp, HOST_WIDE_INT_PRINT_UNSIGNED,
274 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
275 else
276 pp_integer_with_precision
277 (pp, *text->args_ptr, precision, unsigned, "u");
278 break;
280 case 'x':
281 if (wide)
282 pp_scalar (pp, HOST_WIDE_INT_PRINT_HEX,
283 va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
284 else
285 pp_integer_with_precision
286 (pp, *text->args_ptr, precision, unsigned, "x");
287 break;
289 case 'm':
290 pp_string (pp, xstrerror (text->err_no));
291 break;
293 case '%':
294 pp_character (pp, '%');
295 break;
297 case '<':
298 pp_string (pp, open_quote);
299 break;
301 case '>':
302 case '\'':
303 pp_string (pp, close_quote);
304 break;
306 case 'H':
308 location_t *locus = va_arg (*text->args_ptr, location_t *);
309 expanded_location s = expand_location (*locus);
310 pp_string (pp, "file '");
311 pp_string (pp, s.file);
312 pp_string (pp, "', line ");
313 pp_decimal_int (pp, s.line);
315 break;
317 case '.':
319 int n;
320 const char *s;
321 /* We handle no precision specifier but '%.*s'. */
322 if (*++text->format_spec != '*')
323 abort ();
324 else if (*++text->format_spec != 's')
325 abort ();
326 n = va_arg (*text->args_ptr, int);
327 s = va_arg (*text->args_ptr, const char *);
328 pp_append_text (pp, s, s + n);
330 break;
332 default:
333 if (!pp_format_decoder (pp) || !(*pp_format_decoder (pp)) (pp, text))
335 /* Hmmm. The client failed to install a format translator
336 but called us with an unrecognized format. Or, maybe, the
337 translated string just contains an invalid format, or
338 has formats in the wrong order. Sorry. */
339 abort ();
342 if (quoted)
343 pp_string (pp, close_quote);
347 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
348 settings needed by BUFFER for a verbatim formatting. */
349 void
350 pp_base_format_verbatim (pretty_printer *pp, text_info *text)
352 diagnostic_prefixing_rule_t rule = pp_prefixing_rule (pp);
353 int line_cutoff = pp_line_cutoff (pp);
355 /* Set verbatim mode. */
356 pp->prefixing_rule = DIAGNOSTICS_SHOW_PREFIX_NEVER;
357 pp_line_cutoff (pp) = 0;
358 /* Do the actual formatting. */
359 pp_format_text (pp, text);
360 /* Restore previous settings. */
361 pp_prefixing_rule (pp) = rule;
362 pp_line_cutoff (pp) = line_cutoff;
365 /* Flush the content of BUFFER onto the attached stream. */
366 void
367 pp_base_flush (pretty_printer *pp)
369 pp_write_text_to_stream (pp);
370 pp_clear_state (pp);
371 fputc ('\n', pp->buffer->stream);
372 fflush (pp->buffer->stream);
373 pp_needs_newline (pp) = false;
376 /* Sets the number of maximum characters per line PRETTY-PRINTER can
377 output in line-wrapping mode. A LENGTH value 0 suppresses
378 line-wrapping. */
379 void
380 pp_base_set_line_maximum_length (pretty_printer *pp, int length)
382 pp_line_cutoff (pp) = length;
383 pp_set_real_maximum_length (pp);
386 /* Clear PRETTY-PRINTER output area text info. */
387 void
388 pp_base_clear_output_area (pretty_printer *pp)
390 obstack_free (&pp->buffer->obstack, obstack_base (&pp->buffer->obstack));
391 pp->buffer->line_length = 0;
394 /* Set PREFIX for PRETTY-PRINTER. */
395 void
396 pp_base_set_prefix (pretty_printer *pp, const char *prefix)
398 pp->prefix = prefix;
399 pp_set_real_maximum_length (pp);
400 pp->emitted_prefix = false;
401 pp_indentation (pp) = 0;
404 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
405 void
406 pp_base_destroy_prefix (pretty_printer *pp)
408 if (pp->prefix != NULL)
410 free ((char *) pp->prefix);
411 pp->prefix = NULL;
415 /* Write out PRETTY-PRINTER's prefix. */
416 void
417 pp_base_emit_prefix (pretty_printer *pp)
419 if (pp->prefix != NULL)
421 switch (pp_prefixing_rule (pp))
423 default:
424 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
425 break;
427 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
428 if (pp->emitted_prefix)
430 pp_base_indent (pp);
431 break;
433 pp_indentation (pp) += 3;
434 /* Fall through. */
436 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
438 int prefix_length = strlen (pp->prefix);
439 pp_append_r (pp, pp->prefix, prefix_length);
440 pp->emitted_prefix = true;
442 break;
447 /* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
448 characters per line. */
449 void
450 pp_construct (pretty_printer *pp, const char *prefix, int maximum_length)
452 memset (pp, 0, sizeof (pretty_printer));
453 pp->buffer = xcalloc (1, sizeof (output_buffer));
454 obstack_init (&pp->buffer->obstack);
455 pp->buffer->stream = stderr;
456 pp_line_cutoff (pp) = maximum_length;
457 pp_prefixing_rule (pp) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
458 pp_set_prefix (pp, prefix);
461 /* Append a string delimited by START and END to the output area of
462 PRETTY-PRINTER. No line wrapping is done. However, if beginning a
463 new line then emit PRETTY-PRINTER's prefix and skip any leading
464 whitespace if appropriate. The caller must ensure that it is
465 safe to do so. */
466 void
467 pp_base_append_text (pretty_printer *pp, const char *start, const char *end)
469 /* Emit prefix and skip whitespace if we're starting a new line. */
470 if (pp->buffer->line_length == 0)
472 pp_emit_prefix (pp);
473 if (pp_is_wrapping_line (pp))
474 while (start != end && *start == ' ')
475 ++start;
477 pp_append_r (pp, start, end - start);
480 /* Finishes constructing a NULL-terminated character string representing
481 the PRETTY-PRINTED text. */
482 const char *
483 pp_base_formatted_text (pretty_printer *pp)
485 obstack_1grow (&pp->buffer->obstack, '\0');
486 return pp_formatted_text_data (pp);
489 /* Return a pointer to the last character emitted in PRETTY-PRINTER's
490 output area. A NULL pointer means no character available. */
491 const char *
492 pp_base_last_position_in_text (const pretty_printer *pp)
494 const char *p = NULL;
495 struct obstack *text = &pp->buffer->obstack;
497 if (obstack_base (text) != obstack_next_free (text))
498 p = ((const char *) obstack_next_free (text)) - 1;
499 return p;
502 /* Return the amount of characters PRETTY-PRINTER can accept to
503 make a full line. Meaningful only in line-wrapping mode. */
505 pp_base_remaining_character_count_for_line (pretty_printer *pp)
507 return pp->maximum_length - pp->buffer->line_length;
511 /* Format a message into BUFFER a la printf. */
512 void
513 pp_printf (pretty_printer *pp, const char *msg, ...)
515 text_info text;
516 va_list ap;
518 va_start (ap, msg);
519 text.err_no = errno;
520 text.args_ptr = &ap;
521 text.format_spec = msg;
522 pp_format_text (pp, &text);
523 va_end (ap);
527 /* Output MESSAGE verbatim into BUFFER. */
528 void
529 pp_verbatim (pretty_printer *pp, const char *msg, ...)
531 text_info text;
532 va_list ap;
534 va_start (ap, msg);
535 text.err_no = errno;
536 text.args_ptr = &ap;
537 text.format_spec = msg;
538 pp_format_verbatim (pp, &text);
539 va_end (ap);
544 /* Have PRETTY-PRINTER start a new line. */
545 void
546 pp_base_newline (pretty_printer *pp)
548 obstack_1grow (&pp->buffer->obstack, '\n');
549 pp->buffer->line_length = 0;
552 /* Have PRETTY-PRINTER add a CHARACTER. */
553 void
554 pp_base_character (pretty_printer *pp, int c)
556 if (pp_is_wrapping_line (pp)
557 && pp_remaining_character_count_for_line (pp) <= 0)
559 pp_newline (pp);
560 if (ISSPACE (c))
561 return;
563 obstack_1grow (&pp->buffer->obstack, c);
564 ++pp->buffer->line_length;
567 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
568 be line-wrapped if in appropriate mode. */
569 void
570 pp_base_string (pretty_printer *pp, const char *str)
572 pp_maybe_wrap_text (pp, str, str + (str ? strlen (str) : 0));
575 /* Maybe print out a whitespace if needed. */
577 void
578 pp_base_maybe_space (pretty_printer *pp)
580 if (pp_base (pp)->padding != pp_none)
582 pp_space (pp);
583 pp_base (pp)->padding = pp_none;