* lambda-mat.c (lambda_matrix_inverse_hard): Use gcc_assert
[platform/upstream/linaro-gcc.git] / gcc / pretty-print.c
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>
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
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.  */
64 static void
65 pp_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.  */
87 static inline void
88 pp_clear_state (pretty_printer *pp)
89 {
90   pp->emitted_prefix = false;
91   pp_indentation (pp) = 0;
92 }
93
94 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream.  */
95 void
96 pp_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.  */
104 static void
105 pp_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.  */
137 static inline void
138 pp_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.  */
148 static inline void
149 pp_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
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)
160 {
161   int n = pp_indentation (pp);
162   int i;
163
164   for (i = 0; i < n; ++i)
165     pp_space (pp);
166 }
167
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)
191 {
192   for (; *text->format_spec; ++text->format_spec)
193     {
194       int precision = 0;
195       bool wide = false;
196       bool quoted = false;
197
198       /* Ignore text.  */
199       {
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;
205       }
206
207       if (*text->format_spec == '\0')
208         break;
209
210       /* We got a '%'.  Check for 'q', then parse precision modifiers,
211          if any.  */
212       if (*++text->format_spec == 'q')
213         {
214           quoted = true;
215           ++text->format_spec;
216         }
217       switch (*text->format_spec)
218         {
219         case 'w':
220           wide = true;
221           ++text->format_spec;
222           break;
223
224         case 'l':
225           do
226             ++precision;
227           while (*++text->format_spec == 'l');
228           break;
229
230         default:
231           break;
232         }
233       /* We don't support precision beyond that of "long long".  */
234       gcc_assert (precision <= 2);
235
236       if (quoted)
237         pp_string (pp, open_quote);
238       switch (*text->format_spec)
239         {
240         case 'c':
241           pp_character (pp, va_arg (*text->args_ptr, int));
242           break;
243
244         case 'd':
245         case 'i':
246           if (wide)
247             pp_wide_integer (pp, va_arg (*text->args_ptr, HOST_WIDE_INT));
248           else
249             pp_integer_with_precision
250               (pp, *text->args_ptr, precision, int, "d");
251           break;
252
253         case 'o':
254           if (wide)
255             pp_scalar (pp, "%" HOST_WIDE_INT_PRINT "o",
256                        va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
257           else
258             pp_integer_with_precision
259               (pp, *text->args_ptr, precision, unsigned, "u");
260           break;
261
262         case 's':
263           pp_string (pp, va_arg (*text->args_ptr, const char *));
264           break;
265
266         case 'p':
267           pp_pointer (pp, va_arg (*text->args_ptr, void *));
268           break;
269
270         case 'u':
271           if (wide)
272             pp_scalar (pp, HOST_WIDE_INT_PRINT_UNSIGNED,
273                        va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
274           else
275             pp_integer_with_precision
276               (pp, *text->args_ptr, precision, unsigned, "u");
277           break;
278
279         case 'x':
280           if (wide)
281             pp_scalar (pp, HOST_WIDE_INT_PRINT_HEX,
282                        va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
283           else
284             pp_integer_with_precision
285               (pp, *text->args_ptr, precision, unsigned, "x");
286           break;
287
288         case 'm':
289           pp_string (pp, xstrerror (text->err_no));
290           break;
291
292         case '%':
293           pp_character (pp, '%');
294           break;
295
296         case '<':
297           pp_string (pp, open_quote);
298           break;
299
300         case '>':
301         case '\'':
302           pp_string (pp, close_quote);
303           break;
304
305         case 'H':
306           {
307             location_t *locus = va_arg (*text->args_ptr, location_t *);
308             expanded_location s = expand_location (*locus);
309             pp_string (pp, "file '");
310             pp_string (pp, s.file);
311             pp_string (pp, "', line ");
312             pp_decimal_int (pp, s.line);
313           }
314           break;
315
316         case '.':
317           {
318             int n;
319             const char *s;
320             /* We handle no precision specifier but '%.*s'.  */
321             ++text->format_spec;
322             gcc_assert (*text->format_spec == '*');
323             ++text->format_spec;
324             gcc_assert (*text->format_spec == 's');
325             n = va_arg (*text->args_ptr, int);
326             s = va_arg (*text->args_ptr, const char *);
327             pp_append_text (pp, s, s + n);
328           }
329           break;
330
331         default:
332           {
333             bool ok;
334
335             /* Make sure there's a format translator. */
336             gcc_assert (pp_format_decoder (pp));
337             ok = pp_format_decoder (pp) (pp, text);
338             /* and make sure it recognized the format.  */
339             gcc_assert (ok);
340             break;
341           }
342         }
343       if (quoted)
344         pp_string (pp, close_quote);
345     }
346 }
347
348 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
349    settings needed by BUFFER for a verbatim formatting.  */
350 void
351 pp_base_format_verbatim (pretty_printer *pp, text_info *text)
352 {
353   diagnostic_prefixing_rule_t rule = pp_prefixing_rule (pp);
354   int line_cutoff = pp_line_cutoff (pp);
355
356   /* Set verbatim mode.  */
357   pp->prefixing_rule = DIAGNOSTICS_SHOW_PREFIX_NEVER;
358   pp_line_cutoff (pp) = 0;
359   /* Do the actual formatting.  */
360   pp_format_text (pp, text);
361   /* Restore previous settings.  */
362   pp_prefixing_rule (pp) = rule;
363   pp_line_cutoff (pp) = line_cutoff;
364 }
365
366 /* Flush the content of BUFFER onto the attached stream.  */
367 void
368 pp_base_flush (pretty_printer *pp)
369 {
370   pp_write_text_to_stream (pp);
371   pp_clear_state (pp);
372   fputc ('\n', pp->buffer->stream);
373   fflush (pp->buffer->stream);
374   pp_needs_newline (pp) = false;
375 }
376
377 /* Sets the number of maximum characters per line PRETTY-PRINTER can
378    output in line-wrapping mode.  A LENGTH value 0 suppresses
379    line-wrapping.  */
380 void
381 pp_base_set_line_maximum_length (pretty_printer *pp, int length)
382 {
383   pp_line_cutoff (pp) = length;
384   pp_set_real_maximum_length (pp);
385 }
386
387 /* Clear PRETTY-PRINTER output area text info.  */
388 void
389 pp_base_clear_output_area (pretty_printer *pp)
390 {
391   obstack_free (&pp->buffer->obstack, obstack_base (&pp->buffer->obstack));
392   pp->buffer->line_length = 0;
393 }
394
395 /* Set PREFIX for PRETTY-PRINTER.  */
396 void
397 pp_base_set_prefix (pretty_printer *pp, const char *prefix)
398 {
399   pp->prefix = prefix;
400   pp_set_real_maximum_length (pp);
401   pp->emitted_prefix = false;
402   pp_indentation (pp) = 0;
403 }
404
405 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string.  */
406 void
407 pp_base_destroy_prefix (pretty_printer *pp)
408 {
409   if (pp->prefix != NULL)
410     {
411       free ((char *) pp->prefix);
412       pp->prefix = NULL;
413     }
414 }
415
416 /* Write out PRETTY-PRINTER's prefix.  */
417 void
418 pp_base_emit_prefix (pretty_printer *pp)
419 {
420   if (pp->prefix != NULL)
421     {
422       switch (pp_prefixing_rule (pp))
423         {
424         default:
425         case DIAGNOSTICS_SHOW_PREFIX_NEVER:
426           break;
427
428         case DIAGNOSTICS_SHOW_PREFIX_ONCE:
429           if (pp->emitted_prefix)
430             {
431               pp_base_indent (pp);
432               break;
433             }
434           pp_indentation (pp) += 3;
435           /* Fall through.  */
436
437         case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
438           {
439             int prefix_length = strlen (pp->prefix);
440             pp_append_r (pp, pp->prefix, prefix_length);
441             pp->emitted_prefix = true;
442           }
443           break;
444         }
445     }
446 }
447
448 /* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
449    characters per line.  */
450 void
451 pp_construct (pretty_printer *pp, const char *prefix, int maximum_length)
452 {
453   memset (pp, 0, sizeof (pretty_printer));
454   pp->buffer = xcalloc (1, sizeof (output_buffer));
455   obstack_init (&pp->buffer->obstack);
456   pp->buffer->stream = stderr;
457   pp_line_cutoff (pp) = maximum_length;
458   pp_prefixing_rule (pp) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
459   pp_set_prefix (pp, prefix);
460 }
461
462 /* Append a string delimited by START and END to the output area of
463    PRETTY-PRINTER.  No line wrapping is done.  However, if beginning a
464    new line then emit PRETTY-PRINTER's prefix and skip any leading
465    whitespace if appropriate.  The caller must ensure that it is
466    safe to do so.  */
467 void
468 pp_base_append_text (pretty_printer *pp, const char *start, const char *end)
469 {
470   /* Emit prefix and skip whitespace if we're starting a new line.  */
471   if (pp->buffer->line_length == 0)
472     {
473       pp_emit_prefix (pp);
474       if (pp_is_wrapping_line (pp))
475         while (start != end && *start == ' ')
476           ++start;
477     }
478   pp_append_r (pp, start, end - start);
479 }
480
481 /* Finishes constructing a NULL-terminated character string representing
482    the PRETTY-PRINTED text.  */
483 const char *
484 pp_base_formatted_text (pretty_printer *pp)
485 {
486   obstack_1grow (&pp->buffer->obstack, '\0');
487   return pp_formatted_text_data (pp);
488 }
489
490 /*  Return a pointer to the last character emitted in PRETTY-PRINTER's
491     output area.  A NULL pointer means no character available.  */
492 const char *
493 pp_base_last_position_in_text (const pretty_printer *pp)
494 {
495   const char *p = NULL;
496   struct obstack *text = &pp->buffer->obstack;
497
498   if (obstack_base (text) != obstack_next_free (text))
499     p = ((const char *) obstack_next_free (text)) - 1;
500   return p;
501 }
502
503 /* Return the amount of characters PRETTY-PRINTER can accept to
504    make a full line.  Meaningful only in line-wrapping mode.  */
505 int
506 pp_base_remaining_character_count_for_line (pretty_printer *pp)
507 {
508   return pp->maximum_length - pp->buffer->line_length;
509 }
510
511
512 /* Format a message into BUFFER a la printf.  */
513 void
514 pp_printf (pretty_printer *pp, const char *msg, ...)
515 {
516   text_info text;
517   va_list ap;
518
519   va_start (ap, msg);
520   text.err_no = errno;
521   text.args_ptr = &ap;
522   text.format_spec = msg;
523   pp_format_text (pp, &text);
524   va_end (ap);
525 }
526
527
528 /* Output MESSAGE verbatim into BUFFER.  */
529 void
530 pp_verbatim (pretty_printer *pp, const char *msg, ...)
531 {
532   text_info text;
533   va_list ap;
534
535   va_start (ap, msg);
536   text.err_no = errno;
537   text.args_ptr = &ap;
538   text.format_spec = msg;
539   pp_format_verbatim (pp, &text);
540   va_end (ap);
541 }
542
543
544
545 /* Have PRETTY-PRINTER start a new line.  */
546 void
547 pp_base_newline (pretty_printer *pp)
548 {
549   obstack_1grow (&pp->buffer->obstack, '\n');
550   pp->buffer->line_length = 0;
551 }
552
553 /* Have PRETTY-PRINTER add a CHARACTER.  */
554 void
555 pp_base_character (pretty_printer *pp, int c)
556 {
557   if (pp_is_wrapping_line (pp)
558       && pp_remaining_character_count_for_line (pp) <= 0)
559     {
560       pp_newline (pp);
561       if (ISSPACE (c))
562         return;
563     }
564   obstack_1grow (&pp->buffer->obstack, c);
565   ++pp->buffer->line_length;
566 }
567
568 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
569    be line-wrapped if in appropriate mode.  */
570 void
571 pp_base_string (pretty_printer *pp, const char *str)
572 {
573   pp_maybe_wrap_text (pp, str, str + (str ? strlen (str) : 0));
574 }
575
576 /* Maybe print out a whitespace if needed.   */
577
578 void
579 pp_base_maybe_space (pretty_printer *pp)
580 {
581   if (pp_base (pp)->padding != pp_none)
582     {
583       pp_space (pp);
584       pp_base (pp)->padding = pp_none;
585     }
586 }