* SDCPP synchronized with GCC CPP release version 3.4.6,
[fw/sdcc] / support / cpp2 / pretty-print.c
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>
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 "pretty-print.h"
27
28 #define obstack_chunk_alloc xmalloc
29 #define obstack_chunk_free  free
30
31 /* A pointer to the formatted diagnostic message.  */
32 #define pp_formatted_text_data(PP) \
33    ((const char *) obstack_base (&pp_base (PP)->buffer->obstack))
34
35 /* Format an integer given by va_arg (ARG, type-specifier T) where
36    type-specifier is a precision modifier as indicated by PREC.  F is
37    a string used to construct the appropriate format-specifier.  */
38 #define pp_integer_with_precision(PP, ARG, PREC, T, F)       \
39   do                                                         \
40     switch (PREC)                                            \
41       {                                                      \
42       case 0:                                                \
43         pp_scalar (PP, "%" F, va_arg (ARG, T));              \
44         break;                                               \
45                                                              \
46       case 1:                                                \
47         pp_scalar (PP, "%l" F, va_arg (ARG, long T));        \
48         break;                                               \
49                                                              \
50       case 2:                                                \
51         pp_scalar (PP, "%" PRINTF_INT64_MODIFIER F, va_arg (ARG, long_long)); \
52         break;                                               \
53                                                              \
54       default:                                               \
55         break;                                               \
56       }                                                      \
57   while (0)
58
59
60 /* Subroutine of pp_set_maximum_length.  Set up PRETTY-PRINTER's
61    internal maximum characters per line.  */
62 static void
63 pp_set_real_maximum_length (pretty_printer *pp)
64 {
65   /* If we're told not to wrap lines then do the obvious thing.  In case
66      we'll emit prefix only once per message, it is appropriate
67      not to increase unnecessarily the line-length cut-off.  */
68   if (!pp_is_wrapping_line (pp)
69       || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_ONCE
70       || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
71     pp->maximum_length = pp_line_cutoff (pp);
72   else
73     {
74       int prefix_length = pp->prefix ? strlen (pp->prefix) : 0;
75       /* If the prefix is ridiculously too long, output at least
76          32 characters.  */
77       if (pp_line_cutoff (pp) - prefix_length < 32)
78         pp->maximum_length = pp_line_cutoff (pp) + 32;
79       else
80         pp->maximum_length = pp_line_cutoff (pp);
81     }
82 }
83
84 /* Clear PRETTY-PRINTER's output state.  */
85 static inline void
86 pp_clear_state (pretty_printer *pp)
87 {
88   pp->emitted_prefix = false;
89   pp_indentation (pp) = 0;
90 }
91
92 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream.  */
93 static inline void
94 pp_write_text_to_stream (pretty_printer *pp)
95 {
96   const char *text = pp_formatted_text (pp);
97   fputs (text, pp->buffer->stream);
98   pp_clear_output_area (pp);
99 }
100
101 /* Wrap a text delimited by START and END into PRETTY-PRINTER.  */
102 static void
103 pp_wrap_text (pretty_printer *pp, const char *start, const char *end)
104 {
105   bool wrapping_line = pp_is_wrapping_line (pp);
106
107   while (start != end)
108     {
109       /* Dump anything bordered by whitespaces.  */
110       {
111         const char *p = start;
112         while (p != end && !ISBLANK (*p) && *p != '\n')
113           ++p;
114         if (wrapping_line
115             && p - start >= pp_remaining_character_count_for_line (pp))
116           pp_newline (pp);
117         pp_append_text (pp, start, p);
118         start = p;
119       }
120
121       if (start != end && ISBLANK (*start))
122         {
123           pp_space (pp);
124           ++start;
125         }
126       if (start != end && *start == '\n')
127         {
128           pp_newline (pp);
129           ++start;
130         }
131     }
132 }
133
134 /* Same as pp_wrap_text but wrap text only when in line-wrapping mode.  */
135 static inline void
136 pp_maybe_wrap_text (pretty_printer *pp, const char *start, const char *end)
137 {
138   if (pp_is_wrapping_line (pp))
139     pp_wrap_text (pp, start, end);
140   else
141     pp_append_text (pp, start, end);
142 }
143
144 /* Append to the output area of PRETTY-PRINTER a string specified by its
145    STARTing character and LENGTH.  */
146 static inline void
147 pp_append_r (pretty_printer *pp, const char *start, int length)
148 {
149   obstack_grow (&pp->buffer->obstack, start, length);
150   pp->buffer->line_length += length;
151 }
152
153 /* Insert enough spaces into the output area of PRETTY-PRINTER to bring
154    the column position to the current indentation level, assuming that a
155    newline has just been written to the buffer.  */
156 void
157 pp_base_indent (pretty_printer *pp)
158 {
159   int n = pp_indentation (pp);
160   int i;
161
162   for (i = 0; i < n; ++i)
163     pp_space (pp);
164 }
165
166 /* Format a message pointed to by TEXT.  The following format specifiers are
167    recognized as being client independent:
168    %d, %i: (signed) integer in base ten.
169    %u: unsigned integer in base ten.
170    %o: unsigned integer in base eight.
171    %x: unsigned integer in base sixteen.
172    %ld, %li, %lo, %lu, %lx: long versions of the above.
173    %lld, %lli, %llo, %llu, %llx: long long versions.
174    %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
175    %c: character.
176    %s: string.
177    %p: pointer.
178    %m: strerror(text->err_no) - does not consume a value from args_ptr.
179    %%: `%'.
180    %.*s: a substring the length of which is specified by an integer.
181    %H: location_t.  */
182 void
183 pp_base_format_text (pretty_printer *pp, text_info *text)
184 {
185   for (; *text->format_spec; ++text->format_spec)
186     {
187       int precision = 0;
188       bool wide = false;
189
190       /* Ignore text.  */
191       {
192         const char *p = text->format_spec;
193         while (*p && *p != '%')
194           ++p;
195         pp_wrap_text (pp, text->format_spec, p);
196         text->format_spec = p;
197       }
198
199       if (*text->format_spec == '\0')
200         break;
201
202       /* We got a '%'.  Parse precision modifiers, if any.  */
203       switch (*++text->format_spec)
204         {
205         case 'w':
206           wide = true;
207           ++text->format_spec;
208           break;
209
210         case 'l':
211           do
212             ++precision;
213           while (*++text->format_spec == 'l');
214           break;
215
216         default:
217           break;
218         }
219       /* We don't support precision beyond that of "long long".  */
220       if (precision > 2)
221         abort();
222
223       switch (*text->format_spec)
224         {
225         case 'c':
226           pp_character (pp, va_arg (*text->args_ptr, int));
227           break;
228
229         case 'd':
230         case 'i':
231           if (wide)
232             pp_wide_integer (pp, va_arg (*text->args_ptr, HOST_WIDE_INT));
233           else
234             pp_integer_with_precision
235               (pp, *text->args_ptr, precision, int, "d");
236           break;
237
238         case 'o':
239           if (wide)
240             pp_scalar (pp, "%" HOST_WIDE_INT_PRINT "o",
241                        va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
242           else
243             pp_integer_with_precision
244               (pp, *text->args_ptr, precision, unsigned, "u");
245           break;
246
247         case 's':
248           pp_string (pp, va_arg (*text->args_ptr, const char *));
249           break;
250
251         case 'p':
252           pp_pointer (pp, va_arg (*text->args_ptr, void *));
253           break;
254
255         case 'u':
256           if (wide)
257             pp_scalar (pp, HOST_WIDE_INT_PRINT_UNSIGNED,
258                        va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
259           else
260             pp_integer_with_precision
261               (pp, *text->args_ptr, precision, unsigned, "u");
262           break;
263
264         case 'x':
265           if (wide)
266             pp_scalar (pp, HOST_WIDE_INT_PRINT_HEX,
267                        va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
268           else
269             pp_integer_with_precision
270               (pp, *text->args_ptr, precision, unsigned, "x");
271           break;
272
273         case 'm':
274           pp_string (pp, xstrerror (text->err_no));
275           break;
276
277         case '%':
278           pp_character (pp, '%');
279           break;
280
281         case 'H':
282           {
283             const location_t *locus = va_arg (*text->args_ptr, location_t *);
284             pp_string (pp, "file '");
285             pp_string (pp, locus->file);
286             pp_string (pp, "', line ");
287             pp_decimal_int (pp, locus->line);
288           }
289           break;
290
291         case '.':
292           {
293             int n;
294             const char *s;
295             /* We handle no precision specifier but `%.*s'.  */
296             if (*++text->format_spec != '*')
297               abort ();
298             else if (*++text->format_spec != 's')
299               abort ();
300             n = va_arg (*text->args_ptr, int);
301             s = va_arg (*text->args_ptr, const char *);
302             pp_append_text (pp, s, s + n);
303           }
304           break;
305
306         default:
307           if (!pp_format_decoder (pp) || !(*pp_format_decoder (pp)) (pp, text))
308             {
309               /* Hmmm.  The client failed to install a format translator
310                  but called us with an unrecognized format.  Or, maybe, the
311                  translated string just contains an invalid format, or
312                  has formats in the wrong order.  Sorry.  */
313               abort ();
314             }
315         }
316     }
317 }
318
319 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
320    settings needed by BUFFER for a verbatim formatting.  */
321 void
322 pp_base_format_verbatim (pretty_printer *pp, text_info *text)
323 {
324   diagnostic_prefixing_rule_t rule = pp_prefixing_rule (pp);
325   int line_cutoff = pp_line_cutoff (pp);
326
327   /* Set verbatim mode.  */
328   pp->prefixing_rule = DIAGNOSTICS_SHOW_PREFIX_NEVER;
329   pp_line_cutoff (pp) = 0;
330   /* Do the actual formatting.  */
331   pp_format_text (pp, text);
332   /* Restore previous settings.  */
333   pp_prefixing_rule (pp) = rule;
334   pp_line_cutoff (pp) = line_cutoff;
335 }
336
337 /* Flush the content of BUFFER onto the attached stream.  */
338 void
339 pp_base_flush (pretty_printer *pp)
340 {
341   pp_write_text_to_stream (pp);
342   pp_clear_state (pp);
343   fputc ('\n', pp->buffer->stream);
344   fflush (pp->buffer->stream);
345   pp_needs_newline (pp) = false;
346 }
347
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_base_set_line_maximum_length (pretty_printer *pp, int length)
353 {
354   pp_line_cutoff (pp) = length;
355   pp_set_real_maximum_length (pp);
356 }
357
358 /* Clear PRETTY-PRINTER output area text info.  */
359 void
360 pp_base_clear_output_area (pretty_printer *pp)
361 {
362   obstack_free (&pp->buffer->obstack, obstack_base (&pp->buffer->obstack));
363   pp->buffer->line_length = 0;
364 }
365
366 /* Set PREFIX for PRETTY-PRINTER.  */
367 void
368 pp_base_set_prefix (pretty_printer *pp, const char *prefix)
369 {
370   pp->prefix = prefix;
371   pp_set_real_maximum_length (pp);
372   pp->emitted_prefix = false;
373   pp_indentation (pp) = 0;
374 }
375
376 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string.  */
377 void
378 pp_base_destroy_prefix (pretty_printer *pp)
379 {
380   if (pp->prefix != NULL)
381     {
382       free ((char *) pp->prefix);
383       pp->prefix = NULL;
384     }
385 }
386
387 /* Write out PRETTY-PRINTER's prefix.  */
388 void
389 pp_base_emit_prefix (pretty_printer *pp)
390 {
391   if (pp->prefix != NULL)
392     {
393       switch (pp_prefixing_rule (pp))
394         {
395         default:
396         case DIAGNOSTICS_SHOW_PREFIX_NEVER:
397           break;
398
399         case DIAGNOSTICS_SHOW_PREFIX_ONCE:
400           if (pp->emitted_prefix)
401             {
402               pp_base_indent (pp);
403               break;
404             }
405           pp_indentation (pp) += 3;
406           /* Fall through.  */
407
408         case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
409           {
410             int prefix_length = strlen (pp->prefix);
411             pp_append_r (pp, pp->prefix, prefix_length);
412             pp->emitted_prefix = true;
413           }
414           break;
415         }
416     }
417 }
418
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)
423 {
424   memset (pp, 0, sizeof (pretty_printer));
425   pp->buffer = xcalloc (1, 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);
431 }
432
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_base_append_text (pretty_printer *pp, const char *start, const char *end)
440 {
441   /* Emit prefix and skip whitespace if we're starting a new line.  */
442   if (pp->buffer->line_length == 0)
443     {
444       pp_emit_prefix (pp);
445       if (pp_is_wrapping_line (pp))
446         while (start != end && *start == ' ')
447           ++start;
448     }
449   pp_append_r (pp, start, end - start);
450 }
451
452 /* Finishes constructing a NULL-terminated character string representing
453    the PRETTY-PRINTED text.  */
454 const char *
455 pp_base_formatted_text (pretty_printer *pp)
456 {
457   obstack_1grow (&pp->buffer->obstack, '\0');
458   return pp_formatted_text_data (pp);
459 }
460
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_base_last_position_in_text (const pretty_printer *pp)
465 {
466   const char *p = NULL;
467   struct obstack *text = &pp->buffer->obstack;
468
469   if (obstack_base (text) != obstack_next_free (text))
470     p = ((const char *) obstack_next_free (text)) - 1;
471   return p;
472 }
473
474 /* Return the amount of characters PRETTY-PRINTER can accept to
475    make a full line.  Meaningful only in line-wrapping mode.  */
476 int
477 pp_base_remaining_character_count_for_line (pretty_printer *pp)
478 {
479   return pp->maximum_length - pp->buffer->line_length;
480 }
481
482
483 /* Format a message into BUFFER a la printf.  */
484 void
485 pp_printf (pretty_printer *pp, const char *msg, ...)
486 {
487   text_info text;
488   va_list ap;
489
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);
496 }
497
498
499 /* Output MESSAGE verbatim into BUFFER.  */
500 void
501 pp_verbatim (pretty_printer *pp, const char *msg, ...)
502 {
503   text_info text;
504   va_list ap;
505
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);
512 }
513
514
515
516 /* Have PRETTY-PRINTER start a new line.  */
517 void
518 pp_base_newline (pretty_printer *pp)
519 {
520   obstack_1grow (&pp->buffer->obstack, '\n');
521   pp->buffer->line_length = 0;
522 }
523
524 /* Have PRETTY-PRINTER add a CHARACTER.  */
525 void
526 pp_base_character (pretty_printer *pp, int c)
527 {
528   if (pp_is_wrapping_line (pp)
529       && pp_remaining_character_count_for_line (pp) <= 0)
530     {
531       pp_newline (pp);
532       if (ISSPACE (c))
533         return;
534     }
535   obstack_1grow (&pp->buffer->obstack, c);
536   ++pp->buffer->line_length;
537 }
538
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_base_string (pretty_printer *pp, const char *str)
543 {
544   pp_maybe_wrap_text (pp, str, str + (str ? strlen (str) : 0));
545 }
546
547