OSDN Git Service

PR other/21052
[pf3gnuchains/gcc-fork.git] / gcc / pretty-print.c
1 /* Various declarations for language-independent pretty-print subroutines.
2    Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3    Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
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 "intl.h"
28 #include "pretty-print.h"
29 #include "tree.h"
30
31 #define obstack_chunk_alloc xmalloc
32 #define obstack_chunk_free  free
33
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))
37
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;                                               \
48                                                              \
49       case 1:                                                \
50         pp_scalar (PP, "%l" F, va_arg (ARG, long T));        \
51         break;                                               \
52                                                              \
53       case 2:                                                \
54         pp_scalar (PP, "%ll" F, va_arg (ARG, long long T));  \
55         break;                                               \
56                                                              \
57       default:                                               \
58         break;                                               \
59       }                                                      \
60   while (0)
61
62
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)
67 {
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
76     {
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);
84     }
85 }
86
87 /* Clear PRETTY-PRINTER's output state.  */
88 static inline void
89 pp_clear_state (pretty_printer *pp)
90 {
91   pp->emitted_prefix = false;
92   pp_indentation (pp) = 0;
93 }
94
95 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream.  */
96 void
97 pp_write_text_to_stream (pretty_printer *pp)
98 {
99   const char *text = pp_formatted_text (pp);
100   fputs (text, pp->buffer->stream);
101   pp_clear_output_area (pp);
102 }
103
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)
107 {
108   bool wrapping_line = pp_is_wrapping_line (pp);
109
110   while (start != end)
111     {
112       /* Dump anything bordered by whitespaces.  */
113       {
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;
122       }
123
124       if (start != end && ISBLANK (*start))
125         {
126           pp_space (pp);
127           ++start;
128         }
129       if (start != end && *start == '\n')
130         {
131           pp_newline (pp);
132           ++start;
133         }
134     }
135 }
136
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)
140 {
141   if (pp_is_wrapping_line (pp))
142     pp_wrap_text (pp, start, end);
143   else
144     pp_append_text (pp, start, end);
145 }
146
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)
151 {
152   obstack_grow (&pp->buffer->obstack, start, length);
153   pp->buffer->line_length += length;
154 }
155
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)
161 {
162   int n = pp_indentation (pp);
163   int i;
164
165   for (i = 0; i < n; ++i)
166     pp_space (pp);
167 }
168
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.
174
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)
181 {
182   const char *p = text->format_spec;
183   tree t;
184
185   /* Extract the location information if any.  */
186   if (p[0] == '%')
187     switch (p[1])
188       {
189       case 'H':
190         *locus = *va_arg (*text->args_ptr, location_t *);
191         text->format_spec = p + 2;
192         break;
193
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;
199       }
200 }
201
202
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)
226 {
227   for (; *text->format_spec; ++text->format_spec)
228     {
229       int precision = 0;
230       bool wide = false;
231       bool quoted = false;
232
233       /* Ignore text.  */
234       {
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;
240       }
241
242       if (*text->format_spec == '\0')
243         break;
244
245       /* We got a '%'.  Check for 'q', then parse precision modifiers,
246          if any.  */
247       if (*++text->format_spec == 'q')
248         {
249           quoted = true;
250           ++text->format_spec;
251         }
252       switch (*text->format_spec)
253         {
254         case 'w':
255           wide = true;
256           ++text->format_spec;
257           break;
258
259         case 'l':
260           do
261             ++precision;
262           while (*++text->format_spec == 'l');
263           break;
264
265         default:
266           break;
267         }
268       /* We don't support precision beyond that of "long long".  */
269       gcc_assert (precision <= 2);
270
271       if (quoted)
272         pp_string (pp, open_quote);
273       switch (*text->format_spec)
274         {
275         case 'c':
276           pp_character (pp, va_arg (*text->args_ptr, int));
277           break;
278
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;
287
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;
296
297         case 's':
298           pp_string (pp, va_arg (*text->args_ptr, const char *));
299           break;
300
301         case 'p':
302           pp_pointer (pp, va_arg (*text->args_ptr, void *));
303           break;
304
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;
313
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;
322
323         case 'm':
324           pp_string (pp, xstrerror (text->err_no));
325           break;
326
327         case '%':
328           pp_character (pp, '%');
329           break;
330
331         case '<':
332           pp_string (pp, open_quote);
333           break;
334
335         case '>':
336         case '\'':
337           pp_string (pp, close_quote);
338           break;
339
340         case 'H':
341           {
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);
348           }
349           break;
350
351         case '.':
352           {
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');
360
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);
364           }
365           break;
366
367         default:
368           {
369             bool ok;
370             
371             gcc_assert (pp_format_decoder (pp));
372             ok = pp_format_decoder (pp) (pp, text);
373             gcc_assert (ok);
374           }
375         }
376       if (quoted)
377         pp_string (pp, close_quote);
378     }
379 }
380
381 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
382    settings needed by BUFFER for a verbatim formatting.  */
383 void
384 pp_base_format_verbatim (pretty_printer *pp, text_info *text)
385 {
386   diagnostic_prefixing_rule_t rule = pp_prefixing_rule (pp);
387   int line_cutoff = pp_line_cutoff (pp);
388
389   /* Set verbatim mode.  */
390   pp->prefixing_rule = DIAGNOSTICS_SHOW_PREFIX_NEVER;
391   pp_line_cutoff (pp) = 0;
392   /* Do the actual formatting.  */
393   pp_format_text (pp, text);
394   /* Restore previous settings.  */
395   pp_prefixing_rule (pp) = rule;
396   pp_line_cutoff (pp) = line_cutoff;
397 }
398
399 /* Flush the content of BUFFER onto the attached stream.  */
400 void
401 pp_base_flush (pretty_printer *pp)
402 {
403   pp_write_text_to_stream (pp);
404   pp_clear_state (pp);
405   fputc ('\n', pp->buffer->stream);
406   fflush (pp->buffer->stream);
407   pp_needs_newline (pp) = false;
408 }
409
410 /* Sets the number of maximum characters per line PRETTY-PRINTER can
411    output in line-wrapping mode.  A LENGTH value 0 suppresses
412    line-wrapping.  */
413 void
414 pp_base_set_line_maximum_length (pretty_printer *pp, int length)
415 {
416   pp_line_cutoff (pp) = length;
417   pp_set_real_maximum_length (pp);
418 }
419
420 /* Clear PRETTY-PRINTER output area text info.  */
421 void
422 pp_base_clear_output_area (pretty_printer *pp)
423 {
424   obstack_free (&pp->buffer->obstack, obstack_base (&pp->buffer->obstack));
425   pp->buffer->line_length = 0;
426 }
427
428 /* Set PREFIX for PRETTY-PRINTER.  */
429 void
430 pp_base_set_prefix (pretty_printer *pp, const char *prefix)
431 {
432   pp->prefix = prefix;
433   pp_set_real_maximum_length (pp);
434   pp->emitted_prefix = false;
435   pp_indentation (pp) = 0;
436 }
437
438 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string.  */
439 void
440 pp_base_destroy_prefix (pretty_printer *pp)
441 {
442   if (pp->prefix != NULL)
443     {
444       free ((char *) pp->prefix);
445       pp->prefix = NULL;
446     }
447 }
448
449 /* Write out PRETTY-PRINTER's prefix.  */
450 void
451 pp_base_emit_prefix (pretty_printer *pp)
452 {
453   if (pp->prefix != NULL)
454     {
455       switch (pp_prefixing_rule (pp))
456         {
457         default:
458         case DIAGNOSTICS_SHOW_PREFIX_NEVER:
459           break;
460
461         case DIAGNOSTICS_SHOW_PREFIX_ONCE:
462           if (pp->emitted_prefix)
463             {
464               pp_base_indent (pp);
465               break;
466             }
467           pp_indentation (pp) += 3;
468           /* Fall through.  */
469
470         case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
471           {
472             int prefix_length = strlen (pp->prefix);
473             pp_append_r (pp, pp->prefix, prefix_length);
474             pp->emitted_prefix = true;
475           }
476           break;
477         }
478     }
479 }
480
481 /* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
482    characters per line.  */
483 void
484 pp_construct (pretty_printer *pp, const char *prefix, int maximum_length)
485 {
486   memset (pp, 0, sizeof (pretty_printer));
487   pp->buffer = xcalloc (1, sizeof (output_buffer));
488   obstack_init (&pp->buffer->obstack);
489   pp->buffer->stream = stderr;
490   pp_line_cutoff (pp) = maximum_length;
491   pp_prefixing_rule (pp) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
492   pp_set_prefix (pp, prefix);
493 }
494
495 /* Append a string delimited by START and END to the output area of
496    PRETTY-PRINTER.  No line wrapping is done.  However, if beginning a
497    new line then emit PRETTY-PRINTER's prefix and skip any leading
498    whitespace if appropriate.  The caller must ensure that it is
499    safe to do so.  */
500 void
501 pp_base_append_text (pretty_printer *pp, const char *start, const char *end)
502 {
503   /* Emit prefix and skip whitespace if we're starting a new line.  */
504   if (pp->buffer->line_length == 0)
505     {
506       pp_emit_prefix (pp);
507       if (pp_is_wrapping_line (pp))
508         while (start != end && *start == ' ')
509           ++start;
510     }
511   pp_append_r (pp, start, end - start);
512 }
513
514 /* Finishes constructing a NULL-terminated character string representing
515    the PRETTY-PRINTED text.  */
516 const char *
517 pp_base_formatted_text (pretty_printer *pp)
518 {
519   obstack_1grow (&pp->buffer->obstack, '\0');
520   return pp_formatted_text_data (pp);
521 }
522
523 /*  Return a pointer to the last character emitted in PRETTY-PRINTER's
524     output area.  A NULL pointer means no character available.  */
525 const char *
526 pp_base_last_position_in_text (const pretty_printer *pp)
527 {
528   const char *p = NULL;
529   struct obstack *text = &pp->buffer->obstack;
530
531   if (obstack_base (text) != obstack_next_free (text))
532     p = ((const char *) obstack_next_free (text)) - 1;
533   return p;
534 }
535
536 /* Return the amount of characters PRETTY-PRINTER can accept to
537    make a full line.  Meaningful only in line-wrapping mode.  */
538 int
539 pp_base_remaining_character_count_for_line (pretty_printer *pp)
540 {
541   return pp->maximum_length - pp->buffer->line_length;
542 }
543
544
545 /* Format a message into BUFFER a la printf.  */
546 void
547 pp_printf (pretty_printer *pp, const char *msg, ...)
548 {
549   text_info text;
550   va_list ap;
551
552   va_start (ap, msg);
553   text.err_no = errno;
554   text.args_ptr = &ap;
555   text.format_spec = msg;
556   pp_format_text (pp, &text);
557   va_end (ap);
558 }
559
560
561 /* Output MESSAGE verbatim into BUFFER.  */
562 void
563 pp_verbatim (pretty_printer *pp, const char *msg, ...)
564 {
565   text_info text;
566   va_list ap;
567
568   va_start (ap, msg);
569   text.err_no = errno;
570   text.args_ptr = &ap;
571   text.format_spec = msg;
572   pp_format_verbatim (pp, &text);
573   va_end (ap);
574 }
575
576
577
578 /* Have PRETTY-PRINTER start a new line.  */
579 void
580 pp_base_newline (pretty_printer *pp)
581 {
582   obstack_1grow (&pp->buffer->obstack, '\n');
583   pp->buffer->line_length = 0;
584 }
585
586 /* Have PRETTY-PRINTER add a CHARACTER.  */
587 void
588 pp_base_character (pretty_printer *pp, int c)
589 {
590   if (pp_is_wrapping_line (pp)
591       && pp_remaining_character_count_for_line (pp) <= 0)
592     {
593       pp_newline (pp);
594       if (ISSPACE (c))
595         return;
596     }
597   obstack_1grow (&pp->buffer->obstack, c);
598   ++pp->buffer->line_length;
599 }
600
601 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
602    be line-wrapped if in appropriate mode.  */
603 void
604 pp_base_string (pretty_printer *pp, const char *str)
605 {
606   pp_maybe_wrap_text (pp, str, str + (str ? strlen (str) : 0));
607 }
608
609 /* Maybe print out a whitespace if needed.  */
610
611 void
612 pp_base_maybe_space (pretty_printer *pp)
613 {
614   if (pp_base (pp)->padding != pp_none)
615     {
616       pp_space (pp);
617       pp_base (pp)->padding = pp_none;
618     }
619 }