Update gcc-50 to SVN version 221845
[dragonfly.git] / contrib / gcc-5.0 / gcc / pretty-print.h
1 /* Various declarations for language-independent pretty-print subroutines.
2    Copyright (C) 2002-2015 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 3, 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 COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #ifndef GCC_PRETTY_PRINT_H
22 #define GCC_PRETTY_PRINT_H
23
24 #include "obstack.h"
25 #include "input.h"
26 #include "wide-int-print.h"
27
28 /* Maximum number of format string arguments.  */
29 #define PP_NL_ARGMAX   30
30
31 /* The type of a text to be formatted according a format specification
32    along with a list of things.  */
33 struct text_info
34 {
35   const char *format_spec;
36   va_list *args_ptr;
37   int err_no;  /* for %m */
38   location_t *locus;
39   void **x_data;
40 };
41
42 /* How often diagnostics are prefixed by their locations:
43    o DIAGNOSTICS_SHOW_PREFIX_NEVER: never - not yet supported;
44    o DIAGNOSTICS_SHOW_PREFIX_ONCE: emit only once;
45    o DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE: emit each time a physical
46    line is started.  */
47 enum diagnostic_prefixing_rule_t
48 {
49   DIAGNOSTICS_SHOW_PREFIX_ONCE       = 0x0,
50   DIAGNOSTICS_SHOW_PREFIX_NEVER      = 0x1,
51   DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE = 0x2
52 };
53
54 /* The chunk_info data structure forms a stack of the results from the
55    first phase of formatting (pp_format) which have not yet been
56    output (pp_output_formatted_text).  A stack is necessary because
57    the diagnostic starter may decide to generate its own output by way
58    of the formatter.  */
59 struct chunk_info
60 {
61   /* Pointer to previous chunk on the stack.  */
62   struct chunk_info *prev;
63
64   /* Array of chunks to output.  Each chunk is a NUL-terminated string.
65      In the first phase of formatting, even-numbered chunks are
66      to be output verbatim, odd-numbered chunks are format specifiers.
67      The second phase replaces all odd-numbered chunks with formatted
68      text, and the third phase simply emits all the chunks in sequence
69      with appropriate line-wrapping.  */
70   const char *args[PP_NL_ARGMAX * 2];
71 };
72
73 /* The output buffer datatype.  This is best seen as an abstract datatype
74    whose fields should not be accessed directly by clients.  */
75 struct output_buffer
76 {
77   output_buffer ();
78   ~output_buffer ();
79
80   /* Obstack where the text is built up.  */
81   struct obstack formatted_obstack;
82
83   /* Obstack containing a chunked representation of the format
84      specification plus arguments.  */
85   struct obstack chunk_obstack;
86
87   /* Currently active obstack: one of the above two.  This is used so
88      that the text formatters don't need to know which phase we're in.  */
89   struct obstack *obstack;
90
91   /* Stack of chunk arrays.  These come from the chunk_obstack.  */
92   struct chunk_info *cur_chunk_array;
93
94   /* Where to output formatted text.  */
95   FILE *stream;
96
97   /* The amount of characters output so far.  */
98   int line_length;
99
100   /* This must be large enough to hold any printed integer or
101      floating-point value.  */
102   char digit_buffer[128];
103
104   /* Nonzero means that text should be flushed when
105      appropriate. Otherwise, text is buffered until either
106      pp_really_flush or pp_clear_output_area are called.  */
107   bool flush_p;
108 };
109
110 /* Finishes constructing a NULL-terminated character string representing
111    the buffered text.  */
112 static inline const char *
113 output_buffer_formatted_text (output_buffer *buff)
114 {
115   obstack_1grow (buff->obstack, '\0');
116   return (const char *) obstack_base (buff->obstack);
117 }
118
119 /* Append to the output buffer a string specified by its
120    STARTing character and LENGTH.  */
121 static inline void
122 output_buffer_append_r (output_buffer *buff, const char *start, int length)
123 {
124   obstack_grow (buff->obstack, start, length);
125   buff->line_length += length;
126 }
127
128 /*  Return a pointer to the last character emitted in the
129     output_buffer.  A NULL pointer means no character available.  */
130 static inline const char *
131 output_buffer_last_position_in_text (const output_buffer *buff)
132 {
133   const char *p = NULL;
134   struct obstack *text = buff->obstack;
135
136   if (obstack_base (text) != obstack_next_free (text))
137     p = ((const char *) obstack_next_free (text)) - 1;
138   return p;
139 }
140
141
142 /* The type of pretty-printer flags passed to clients.  */
143 typedef unsigned int pp_flags;
144
145 enum pp_padding
146 {
147   pp_none, pp_before, pp_after
148 };
149
150 /* Structure for switching in and out of verbatim mode in a convenient
151    manner.  */
152 struct pp_wrapping_mode_t
153 {
154   /* Current prefixing rule.  */
155   diagnostic_prefixing_rule_t rule;
156
157   /* The ideal upper bound of number of characters per line, as suggested
158      by front-end.  */
159   int line_cutoff;
160 };
161
162 /* Maximum characters per line in automatic line wrapping mode.
163    Zero means don't wrap lines.  */
164 #define pp_line_cutoff(PP)  (PP)->wrapping.line_cutoff
165
166 /* Prefixing rule used in formatting a diagnostic message.  */
167 #define pp_prefixing_rule(PP)  (PP)->wrapping.rule
168
169 /* Get or set the wrapping mode as a single entity.  */
170 #define pp_wrapping_mode(PP) (PP)->wrapping
171
172 /* The type of a hook that formats client-specific data onto a pretty_pinter.
173    A client-supplied formatter returns true if everything goes well,
174    otherwise it returns false.  */
175 typedef bool (*printer_fn) (pretty_printer *, text_info *, const char *,
176                             int, bool, bool, bool);
177
178 /* Client supplied function used to decode formats.  */
179 #define pp_format_decoder(PP) (PP)->format_decoder
180
181 /* TRUE if a newline character needs to be added before further
182    formatting.  */
183 #define pp_needs_newline(PP)  (PP)->need_newline
184
185 /* True if PRETTY-PRINTER is in line-wrapping mode.  */
186 #define pp_is_wrapping_line(PP) (pp_line_cutoff (PP) > 0)
187
188 /* The amount of whitespace to be emitted when starting a new line.  */
189 #define pp_indentation(PP) (PP)->indent_skip
190
191 /* True if identifiers are translated to the locale character set on
192    output.  */
193 #define pp_translate_identifiers(PP) (PP)->translate_identifiers
194
195 /* True if colors should be shown.  */
196 #define pp_show_color(PP) (PP)->show_color
197
198 /* The data structure that contains the bare minimum required to do
199    proper pretty-printing.  Clients may derived from this structure
200    and add additional fields they need.  */
201 struct pretty_printer
202 {
203   // Default construct a pretty printer with specified prefix
204   // and a maximum line length cut off limit.
205   explicit pretty_printer (const char* = NULL, int = 0);
206
207   virtual ~pretty_printer ();
208
209   /* Where we print external representation of ENTITY.  */
210   output_buffer *buffer;
211
212   /* The prefix for each new line.  */
213   const char *prefix;
214
215   /* Where to put whitespace around the entity being formatted.  */
216   pp_padding padding;
217
218   /* The real upper bound of number of characters per line, taking into
219      account the case of a very very looong prefix.  */
220   int maximum_length;
221
222   /* Indentation count.  */
223   int indent_skip;
224
225   /* Current wrapping mode.  */
226   pp_wrapping_mode_t wrapping;
227
228   /* If non-NULL, this function formats a TEXT into the BUFFER.  When called,
229      TEXT->format_spec points to a format code.  FORMAT_DECODER should call
230      pp_string (and related functions) to add data to the BUFFER.
231      FORMAT_DECODER can read arguments from *TEXT->args_pts using VA_ARG.
232      If the BUFFER needs additional characters from the format string, it
233      should advance the TEXT->format_spec as it goes.  When FORMAT_DECODER
234      returns, TEXT->format_spec should point to the last character processed.
235   */
236   printer_fn format_decoder;
237
238   /* Nonzero if current PREFIX was emitted at least once.  */
239   bool emitted_prefix;
240
241   /* Nonzero means one should emit a newline before outputting anything.  */
242   bool need_newline;
243
244   /* Nonzero means identifiers are translated to the locale character
245      set on output.  */
246   bool translate_identifiers;
247
248   /* Nonzero means that text should be colorized.  */
249   bool show_color;
250 };
251
252 static inline const char *
253 pp_get_prefix (const pretty_printer *pp) { return pp->prefix; }
254
255 #define pp_space(PP)            pp_character (PP, ' ')
256 #define pp_left_paren(PP)       pp_character (PP, '(')
257 #define pp_right_paren(PP)      pp_character (PP, ')')
258 #define pp_left_bracket(PP)     pp_character (PP, '[')
259 #define pp_right_bracket(PP)    pp_character (PP, ']')
260 #define pp_left_brace(PP)       pp_character (PP, '{')
261 #define pp_right_brace(PP)      pp_character (PP, '}')
262 #define pp_semicolon(PP)        pp_character (PP, ';')
263 #define pp_comma(PP)            pp_character (PP, ',')
264 #define pp_dot(PP)              pp_character (PP, '.')
265 #define pp_colon(PP)            pp_character (PP, ':')
266 #define pp_colon_colon(PP)      pp_string (PP, "::")
267 #define pp_arrow(PP)            pp_string (PP, "->")
268 #define pp_equal(PP)            pp_character (PP, '=')
269 #define pp_question(PP)         pp_character (PP, '?')
270 #define pp_bar(PP)              pp_character (PP, '|')
271 #define pp_bar_bar(PP)          pp_string (PP, "||")
272 #define pp_carret(PP)           pp_character (PP, '^')
273 #define pp_ampersand(PP)        pp_character (PP, '&')
274 #define pp_ampersand_ampersand(PP) pp_string (PP, "&&")
275 #define pp_less(PP)             pp_character (PP, '<')
276 #define pp_less_equal(PP)       pp_string (PP, "<=")
277 #define pp_greater(PP)          pp_character (PP, '>')
278 #define pp_greater_equal(PP)    pp_string (PP, ">=")
279 #define pp_plus(PP)             pp_character (PP, '+')
280 #define pp_minus(PP)            pp_character (PP, '-')
281 #define pp_star(PP)             pp_character (PP, '*')
282 #define pp_slash(PP)            pp_character (PP, '/')
283 #define pp_modulo(PP)           pp_character (PP, '%')
284 #define pp_exclamation(PP)      pp_character (PP, '!')
285 #define pp_complement(PP)       pp_character (PP, '~')
286 #define pp_quote(PP)            pp_character (PP, '\'')
287 #define pp_backquote(PP)        pp_character (PP, '`')
288 #define pp_doublequote(PP)      pp_character (PP, '"')
289 #define pp_underscore(PP)       pp_character (PP, '_')
290 #define pp_maybe_newline_and_indent(PP, N) \
291   if (pp_needs_newline (PP)) pp_newline_and_indent (PP, N)
292 #define pp_scalar(PP, FORMAT, SCALAR)                         \
293   do                                                          \
294     {                                                         \
295       sprintf (pp_buffer (PP)->digit_buffer, FORMAT, SCALAR); \
296       pp_string (PP, pp_buffer (PP)->digit_buffer);           \
297     }                                                         \
298   while (0)
299 #define pp_decimal_int(PP, I)  pp_scalar (PP, "%d", I)
300 #define pp_unsigned_wide_integer(PP, I) \
301    pp_scalar (PP, HOST_WIDE_INT_PRINT_UNSIGNED, (unsigned HOST_WIDE_INT) I)
302 #define pp_wide_int(PP, W, SGN)                                 \
303   do                                                            \
304     {                                                           \
305       print_dec (W, pp_buffer (PP)->digit_buffer, SGN);         \
306       pp_string (PP, pp_buffer (PP)->digit_buffer);             \
307     }                                                           \
308   while (0)
309 #define pp_wide_integer(PP, I) \
310    pp_scalar (PP, HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT) I)
311 #define pp_pointer(PP, P)      pp_scalar (PP, "%p", P)
312
313 #define pp_identifier(PP, ID)  pp_string (PP, (pp_translate_identifiers (PP) \
314                                           ? identifier_to_locale (ID)   \
315                                           : (ID)))
316
317
318 #define pp_buffer(PP) (PP)->buffer
319
320 extern void pp_set_line_maximum_length (pretty_printer *, int);
321 extern void pp_set_prefix (pretty_printer *, const char *);
322 extern void pp_destroy_prefix (pretty_printer *);
323 extern int pp_remaining_character_count_for_line (pretty_printer *);
324 extern void pp_clear_output_area (pretty_printer *);
325 extern const char *pp_formatted_text (pretty_printer *);
326 extern const char *pp_last_position_in_text (const pretty_printer *);
327 extern void pp_emit_prefix (pretty_printer *);
328 extern void pp_append_text (pretty_printer *, const char *, const char *);
329 extern void pp_newline_and_flush (pretty_printer *);
330 extern void pp_newline_and_indent (pretty_printer *, int);
331 extern void pp_separate_with (pretty_printer *, char);
332
333 /* If we haven't already defined a front-end-specific diagnostics
334    style, use the generic one.  */
335 #ifdef GCC_DIAG_STYLE
336 #define GCC_PPDIAG_STYLE GCC_DIAG_STYLE
337 #else
338 #define GCC_PPDIAG_STYLE __gcc_diag__
339 #endif
340
341 /* This header may be included before diagnostics-core.h, hence the duplicate
342    definitions to allow for GCC-specific formats.  */
343 #if GCC_VERSION >= 3005
344 #define ATTRIBUTE_GCC_PPDIAG(m, n) __attribute__ ((__format__ (GCC_PPDIAG_STYLE, m ,n))) ATTRIBUTE_NONNULL(m)
345 #else
346 #define ATTRIBUTE_GCC_PPDIAG(m, n) ATTRIBUTE_NONNULL(m)
347 #endif
348 extern void pp_printf (pretty_printer *, const char *, ...)
349      ATTRIBUTE_GCC_PPDIAG(2,3);
350
351 extern void pp_verbatim (pretty_printer *, const char *, ...)
352      ATTRIBUTE_GCC_PPDIAG(2,3);
353 extern void pp_flush (pretty_printer *);
354 extern void pp_really_flush (pretty_printer *);
355 extern void pp_format (pretty_printer *, text_info *);
356 extern void pp_output_formatted_text (pretty_printer *);
357 extern void pp_format_verbatim (pretty_printer *, text_info *);
358
359 extern void pp_indent (pretty_printer *);
360 extern void pp_newline (pretty_printer *);
361 extern void pp_character (pretty_printer *, int);
362 extern void pp_string (pretty_printer *, const char *);
363 extern void pp_write_text_to_stream (pretty_printer *);
364 extern void pp_write_text_as_dot_label_to_stream (pretty_printer *, bool);
365 extern void pp_maybe_space (pretty_printer *);
366
367 /* Switch into verbatim mode and return the old mode.  */
368 static inline pp_wrapping_mode_t
369 pp_set_verbatim_wrapping_ (pretty_printer *pp)
370 {
371   pp_wrapping_mode_t oldmode = pp_wrapping_mode (pp);
372   pp_line_cutoff (pp) = 0;
373   pp_prefixing_rule (pp) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
374   return oldmode;
375 }
376 #define pp_set_verbatim_wrapping(PP) pp_set_verbatim_wrapping_ (PP)
377
378 extern const char *identifier_to_locale (const char *);
379 extern void *(*identifier_to_locale_alloc) (size_t);
380 extern void (*identifier_to_locale_free) (void *);
381
382 #endif /* GCC_PRETTY_PRINT_H */