]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/pretty-print.c
altivec-7.c: Don't use 'vector long'.
[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"
27#include "pretty-print.h"
28
29#define obstack_chunk_alloc xmalloc
30#define obstack_chunk_free free
31
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))
35
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; \
46 \
47 case 1: \
48 pp_scalar (PP, "%l" F, va_arg (ARG, long T)); \
49 break; \
50 \
51 case 2: \
52 pp_scalar (PP, "%ll" F, va_arg (ARG, long long T)); \
53 break; \
54 \
55 default: \
56 break; \
57 } \
58 while (0)
59
60
61/* Subroutine of pp_set_maximum_length. Set up PRETTY-PRINTER's
62 internal maximum characters per line. */
63static void
64pp_set_real_maximum_length (pretty_printer *pp)
65{
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
74 {
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);
82 }
83}
84
85/* Clear PRETTY-PRINTER's output state. */
86static inline void
87pp_clear_state (pretty_printer *pp)
88{
89 pp->emitted_prefix = false;
90 pp_indentation (pp) = 0;
91}
92
b6fe0bb8 93/* Flush the formatted text of PRETTY-PRINTER onto the attached stream. */
6de9cd9a 94void
b6fe0bb8
GDR
95pp_write_text_to_stream (pretty_printer *pp)
96{
97 const char *text = pp_formatted_text (pp);
98 fputs (text, pp->buffer->stream);
99 pp_clear_output_area (pp);
100}
101
102/* Wrap a text delimited by START and END into PRETTY-PRINTER. */
103static void
104pp_wrap_text (pretty_printer *pp, const char *start, const char *end)
105{
106 bool wrapping_line = pp_is_wrapping_line (pp);
107
108 while (start != end)
109 {
110 /* Dump anything bordered by whitespaces. */
111 {
112 const char *p = start;
113 while (p != end && !ISBLANK (*p) && *p != '\n')
114 ++p;
115 if (wrapping_line
116 && p - start >= pp_remaining_character_count_for_line (pp))
117 pp_newline (pp);
118 pp_append_text (pp, start, p);
119 start = p;
120 }
121
122 if (start != end && ISBLANK (*start))
123 {
124 pp_space (pp);
125 ++start;
126 }
127 if (start != end && *start == '\n')
128 {
129 pp_newline (pp);
130 ++start;
131 }
132 }
133}
134
135/* Same as pp_wrap_text but wrap text only when in line-wrapping mode. */
136static inline void
137pp_maybe_wrap_text (pretty_printer *pp, const char *start, const char *end)
138{
139 if (pp_is_wrapping_line (pp))
140 pp_wrap_text (pp, start, end);
141 else
142 pp_append_text (pp, start, end);
143}
144
145/* Append to the output area of PRETTY-PRINTER a string specified by its
146 STARTing character and LENGTH. */
147static inline void
148pp_append_r (pretty_printer *pp, const char *start, int length)
149{
150 obstack_grow (&pp->buffer->obstack, start, length);
151 pp->buffer->line_length += length;
152}
153
4b780675
GDR
154/* Insert enough spaces into the output area of PRETTY-PRINTER to bring
155 the column position to the current indentation level, assuming that a
156 newline has just been written to the buffer. */
157void
158pp_base_indent (pretty_printer *pp)
159{
160 int n = pp_indentation (pp);
161 int i;
162
163 for (i = 0; i < n; ++i)
164 pp_space (pp);
165}
166
b6fe0bb8
GDR
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 %%: `%'.
cb83302c 181 %.*s: a substring the length of which is specified by an integer.
b6fe0bb8
GDR
182 %H: location_t. */
183void
e1a4dd13 184pp_base_format_text (pretty_printer *pp, text_info *text)
b6fe0bb8
GDR
185{
186 for (; *text->format_spec; ++text->format_spec)
187 {
188 int precision = 0;
189 bool wide = false;
190
191 /* Ignore text. */
192 {
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;
198 }
199
200 if (*text->format_spec == '\0')
201 break;
202
203 /* We got a '%'. Parse precision modifiers, if any. */
204 switch (*++text->format_spec)
205 {
206 case 'w':
207 wide = true;
208 ++text->format_spec;
209 break;
210
211 case 'l':
212 do
213 ++precision;
214 while (*++text->format_spec == 'l');
215 break;
216
217 default:
218 break;
219 }
a98ebe2e 220 /* We don't support precision beyond that of "long long". */
b6fe0bb8
GDR
221 if (precision > 2)
222 abort();
223
224 switch (*text->format_spec)
225 {
226 case 'c':
227 pp_character (pp, va_arg (*text->args_ptr, int));
228 break;
229
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;
238
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;
247
248 case 's':
249 pp_string (pp, va_arg (*text->args_ptr, const char *));
250 break;
251
252 case 'p':
253 pp_pointer (pp, va_arg (*text->args_ptr, void *));
254 break;
255
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;
264
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;
273
274 case 'm':
275 pp_string (pp, xstrerror (text->err_no));
276 break;
277
278 case '%':
279 pp_character (pp, '%');
280 break;
281
282 case 'H':
283 {
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);
289 }
290 break;
291
292 case '.':
293 {
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);
304 }
305 break;
306
307 default:
308 if (!pp_format_decoder (pp) || !(*pp_format_decoder (pp)) (pp, text))
309 {
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 ();
315 }
316 }
317 }
318}
319
320/* Helper subroutine of output_verbatim and verbatim. Do the appropriate
321 settings needed by BUFFER for a verbatim formatting. */
322void
e1a4dd13 323pp_base_format_verbatim (pretty_printer *pp, text_info *text)
b6fe0bb8
GDR
324{
325 diagnostic_prefixing_rule_t rule = pp_prefixing_rule (pp);
326 int line_cutoff = pp_line_cutoff (pp);
327
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;
336}
337
338/* Flush the content of BUFFER onto the attached stream. */
339void
e1a4dd13 340pp_base_flush (pretty_printer *pp)
b6fe0bb8
GDR
341{
342 pp_write_text_to_stream (pp);
343 pp_clear_state (pp);
344 fputc ('\n', pp->buffer->stream);
345 fflush (pp->buffer->stream);
7b330e08 346 pp_needs_newline (pp) = false;
b6fe0bb8
GDR
347}
348
349/* Sets the number of maximum characters per line PRETTY-PRINTER can
350 output in line-wrapping mode. A LENGTH value 0 suppresses
351 line-wrapping. */
352void
e1a4dd13 353pp_base_set_line_maximum_length (pretty_printer *pp, int length)
b6fe0bb8
GDR
354{
355 pp_line_cutoff (pp) = length;
356 pp_set_real_maximum_length (pp);
357}
358
359/* Clear PRETTY-PRINTER output area text info. */
360void
e1a4dd13 361pp_base_clear_output_area (pretty_printer *pp)
b6fe0bb8
GDR
362{
363 obstack_free (&pp->buffer->obstack, obstack_base (&pp->buffer->obstack));
364 pp->buffer->line_length = 0;
365}
366
367/* Set PREFIX for PRETTY-PRINTER. */
368void
e1a4dd13 369pp_base_set_prefix (pretty_printer *pp, const char *prefix)
b6fe0bb8
GDR
370{
371 pp->prefix = prefix;
372 pp_set_real_maximum_length (pp);
373 pp->emitted_prefix = false;
374 pp_indentation (pp) = 0;
375}
376
377/* Free PRETTY-PRINTER's prefix, a previously malloc()'d string. */
378void
e1a4dd13 379pp_base_destroy_prefix (pretty_printer *pp)
b6fe0bb8
GDR
380{
381 if (pp->prefix != NULL)
382 {
383 free ((char *) pp->prefix);
384 pp->prefix = NULL;
385 }
386}
387
388/* Write out PRETTY-PRINTER's prefix. */
389void
e1a4dd13 390pp_base_emit_prefix (pretty_printer *pp)
b6fe0bb8
GDR
391{
392 if (pp->prefix != NULL)
393 {
394 switch (pp_prefixing_rule (pp))
395 {
396 default:
397 case DIAGNOSTICS_SHOW_PREFIX_NEVER:
398 break;
399
400 case DIAGNOSTICS_SHOW_PREFIX_ONCE:
401 if (pp->emitted_prefix)
402 {
4b780675 403 pp_base_indent (pp);
b6fe0bb8
GDR
404 break;
405 }
406 pp_indentation (pp) += 3;
407 /* Fall through. */
408
409 case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
410 {
411 int prefix_length = strlen (pp->prefix);
412 pp_append_r (pp, pp->prefix, prefix_length);
413 pp->emitted_prefix = true;
414 }
415 break;
416 }
417 }
418}
419
420/* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
421 characters per line. */
422void
423pp_construct (pretty_printer *pp, const char *prefix, int maximum_length)
424{
425 memset (pp, 0, sizeof (pretty_printer));
c4555dd9 426 pp->buffer = xcalloc (1, sizeof (output_buffer));
b6fe0bb8
GDR
427 obstack_init (&pp->buffer->obstack);
428 pp->buffer->stream = stderr;
429 pp_line_cutoff (pp) = maximum_length;
430 pp_prefixing_rule (pp) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
431 pp_set_prefix (pp, prefix);
432}
433
434/* Append a string delimited by START and END to the output area of
435 PRETTY-PRINTER. No line wrapping is done. However, if beginning a
436 new line then emit PRETTY-PRINTER's prefix and skip any leading
437 whitespace if appropriate. The caller must ensure that it is
438 safe to do so. */
439void
e1a4dd13 440pp_base_append_text (pretty_printer *pp, const char *start, const char *end)
b6fe0bb8
GDR
441{
442 /* Emit prefix and skip whitespace if we're starting a new line. */
443 if (pp->buffer->line_length == 0)
444 {
445 pp_emit_prefix (pp);
446 if (pp_is_wrapping_line (pp))
447 while (start != end && *start == ' ')
448 ++start;
449 }
450 pp_append_r (pp, start, end - start);
451}
452
453/* Finishes constructing a NULL-terminated character string representing
454 the PRETTY-PRINTED text. */
455const char *
e1a4dd13 456pp_base_formatted_text (pretty_printer *pp)
b6fe0bb8
GDR
457{
458 obstack_1grow (&pp->buffer->obstack, '\0');
459 return pp_formatted_text_data (pp);
460}
461
462/* Return a pointer to the last character emitted in PRETTY-PRINTER's
463 output area. A NULL pointer means no character available. */
464const char *
e1a4dd13 465pp_base_last_position_in_text (const pretty_printer *pp)
b6fe0bb8
GDR
466{
467 const char *p = NULL;
468 struct obstack *text = &pp->buffer->obstack;
469
470 if (obstack_base (text) != obstack_next_free (text))
471 p = ((const char *) obstack_next_free (text)) - 1;
472 return p;
473}
474
475/* Return the amount of characters PRETTY-PRINTER can accept to
ba228239 476 make a full line. Meaningful only in line-wrapping mode. */
b6fe0bb8 477int
e1a4dd13 478pp_base_remaining_character_count_for_line (pretty_printer *pp)
b6fe0bb8
GDR
479{
480 return pp->maximum_length - pp->buffer->line_length;
481}
482
483
484/* Format a message into BUFFER a la printf. */
485void
486pp_printf (pretty_printer *pp, const char *msg, ...)
487{
488 text_info text;
489 va_list ap;
490
491 va_start (ap, msg);
492 text.err_no = errno;
493 text.args_ptr = &ap;
494 text.format_spec = msg;
495 pp_format_text (pp, &text);
496 va_end (ap);
497}
498
499
500/* Output MESSAGE verbatim into BUFFER. */
501void
502pp_verbatim (pretty_printer *pp, const char *msg, ...)
503{
504 text_info text;
505 va_list ap;
506
507 va_start (ap, msg);
508 text.err_no = errno;
509 text.args_ptr = &ap;
510 text.format_spec = msg;
511 pp_format_verbatim (pp, &text);
512 va_end (ap);
513}
514
515
516
517/* Have PRETTY-PRINTER start a new line. */
518void
8f7ace48 519pp_base_newline (pretty_printer *pp)
b6fe0bb8
GDR
520{
521 obstack_1grow (&pp->buffer->obstack, '\n');
522 pp->buffer->line_length = 0;
523}
524
525/* Have PRETTY-PRINTER add a CHARACTER. */
526void
8f7ace48 527pp_base_character (pretty_printer *pp, int c)
b6fe0bb8
GDR
528{
529 if (pp_is_wrapping_line (pp)
530 && pp_remaining_character_count_for_line (pp) <= 0)
531 {
532 pp_newline (pp);
533 if (ISSPACE (c))
534 return;
535 }
536 obstack_1grow (&pp->buffer->obstack, c);
537 ++pp->buffer->line_length;
538}
539
540/* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
541 be line-wrapped if in appropriate mode. */
542void
8f7ace48 543pp_base_string (pretty_printer *pp, const char *str)
b6fe0bb8
GDR
544{
545 pp_maybe_wrap_text (pp, str, str + (str ? strlen (str) : 0));
546}
547
b9b44fb9 548/* Maybe print out a whitespace if needed. */
b6fe0bb8 549
b9b44fb9
GDR
550void
551pp_base_maybe_space (pretty_printer *pp)
552{
553 if (pp_base (pp)->padding != pp_none)
554 {
555 pp_space (pp);
556 pp_base (pp)->padding = pp_none;
557 }
558}