PR tree-optimization/54321
[official-gcc.git] / gcc / pretty-print.h
blobe0fd2e98c9d5465fd075ee450fee2ea398c8f070
1 /* Various declarations for language-independent pretty-print subroutines.
2 Copyright (C) 2002, 2003, 2004, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #ifndef GCC_PRETTY_PRINT_H
23 #define GCC_PRETTY_PRINT_H
25 #include "obstack.h"
26 #include "input.h"
28 /* Maximum number of format string arguments. */
29 #define PP_NL_ARGMAX 30
31 /* The type of a text to be formatted according a format specification
32 along with a list of things. */
33 typedef struct
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 } text_info;
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 typedef enum
49 DIAGNOSTICS_SHOW_PREFIX_ONCE = 0x0,
50 DIAGNOSTICS_SHOW_PREFIX_NEVER = 0x1,
51 DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE = 0x2
52 } diagnostic_prefixing_rule_t;
54 /* The chunk_info data structure forms a stack of the results from the
55 first phase of formatting (pp_base_format) which have not yet been
56 output (pp_base_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
61 /* Pointer to previous chunk on the stack. */
62 struct chunk_info *prev;
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];
73 /* The output buffer datatype. This is best seen as an abstract datatype
74 whose fields should not be accessed directly by clients. */
75 typedef struct
77 /* Obstack where the text is built up. */
78 struct obstack formatted_obstack;
80 /* Obstack containing a chunked representation of the format
81 specification plus arguments. */
82 struct obstack chunk_obstack;
84 /* Currently active obstack: one of the above two. This is used so
85 that the text formatters don't need to know which phase we're in. */
86 struct obstack *obstack;
88 /* Stack of chunk arrays. These come from the chunk_obstack. */
89 struct chunk_info *cur_chunk_array;
91 /* Where to output formatted text. */
92 FILE *stream;
94 /* The amount of characters output so far. */
95 int line_length;
97 /* This must be large enough to hold any printed integer or
98 floating-point value. */
99 char digit_buffer[128];
100 } output_buffer;
102 /* The type of pretty-printer flags passed to clients. */
103 typedef unsigned int pp_flags;
105 typedef enum
107 pp_none, pp_before, pp_after
108 } pp_padding;
110 /* Structure for switching in and out of verbatim mode in a convenient
111 manner. */
112 typedef struct
114 /* Current prefixing rule. */
115 diagnostic_prefixing_rule_t rule;
117 /* The ideal upper bound of number of characters per line, as suggested
118 by front-end. */
119 int line_cutoff;
120 } pp_wrapping_mode_t;
122 /* Maximum characters per line in automatic line wrapping mode.
123 Zero means don't wrap lines. */
124 #define pp_line_cutoff(PP) pp_base (PP)->wrapping.line_cutoff
126 /* Prefixing rule used in formatting a diagnostic message. */
127 #define pp_prefixing_rule(PP) pp_base (PP)->wrapping.rule
129 /* Get or set the wrapping mode as a single entity. */
130 #define pp_wrapping_mode(PP) pp_base (PP)->wrapping
132 /* The type of a hook that formats client-specific data onto a pretty_pinter.
133 A client-supplied formatter returns true if everything goes well,
134 otherwise it returns false. */
135 typedef struct pretty_print_info pretty_printer;
136 typedef bool (*printer_fn) (pretty_printer *, text_info *, const char *,
137 int, bool, bool, bool);
139 /* Client supplied function used to decode formats. */
140 #define pp_format_decoder(PP) pp_base (PP)->format_decoder
142 /* TRUE if a newline character needs to be added before further
143 formatting. */
144 #define pp_needs_newline(PP) pp_base (PP)->need_newline
146 /* True if PRETTY-PRINTER is in line-wrapping mode. */
147 #define pp_is_wrapping_line(PP) (pp_line_cutoff (PP) > 0)
149 /* The amount of whitespace to be emitted when starting a new line. */
150 #define pp_indentation(PP) pp_base (PP)->indent_skip
152 /* True if identifiers are translated to the locale character set on
153 output. */
154 #define pp_translate_identifiers(PP) pp_base (PP)->translate_identifiers
156 /* The data structure that contains the bare minimum required to do
157 proper pretty-printing. Clients may derived from this structure
158 and add additional fields they need. */
159 struct pretty_print_info
161 /* Where we print external representation of ENTITY. */
162 output_buffer *buffer;
164 /* The prefix for each new line. */
165 const char *prefix;
167 /* Where to put whitespace around the entity being formatted. */
168 pp_padding padding;
170 /* The real upper bound of number of characters per line, taking into
171 account the case of a very very looong prefix. */
172 int maximum_length;
174 /* Indentation count. */
175 int indent_skip;
177 /* Current wrapping mode. */
178 pp_wrapping_mode_t wrapping;
180 /* If non-NULL, this function formats a TEXT into the BUFFER. When called,
181 TEXT->format_spec points to a format code. FORMAT_DECODER should call
182 pp_string (and related functions) to add data to the BUFFER.
183 FORMAT_DECODER can read arguments from *TEXT->args_pts using VA_ARG.
184 If the BUFFER needs additional characters from the format string, it
185 should advance the TEXT->format_spec as it goes. When FORMAT_DECODER
186 returns, TEXT->format_spec should point to the last character processed.
188 printer_fn format_decoder;
190 /* Nonzero if current PREFIX was emitted at least once. */
191 bool emitted_prefix;
193 /* Nonzero means one should emit a newline before outputting anything. */
194 bool need_newline;
196 /* Nonzero means identifiers are translated to the locale character
197 set on output. */
198 bool translate_identifiers;
201 #define pp_set_line_maximum_length(PP, L) \
202 pp_base_set_line_maximum_length (pp_base (PP), L)
203 #define pp_set_prefix(PP, P) pp_base_set_prefix (pp_base (PP), P)
204 #define pp_get_prefix(PP) pp_base_get_prefix (pp_base (PP))
205 static inline const char *
206 pp_base_get_prefix (const pretty_printer *pp) { return pp->prefix; }
207 #define pp_destroy_prefix(PP) pp_base_destroy_prefix (pp_base (PP))
208 #define pp_remaining_character_count_for_line(PP) \
209 pp_base_remaining_character_count_for_line (pp_base (PP))
210 #define pp_clear_output_area(PP) \
211 pp_base_clear_output_area (pp_base (PP))
212 #define pp_formatted_text(PP) pp_base_formatted_text (pp_base (PP))
213 #define pp_last_position_in_text(PP) \
214 pp_base_last_position_in_text (pp_base (PP))
215 #define pp_emit_prefix(PP) pp_base_emit_prefix (pp_base (PP))
216 #define pp_append_text(PP, B, E) \
217 pp_base_append_text (pp_base (PP), B, E)
218 #define pp_flush(PP) pp_base_flush (pp_base (PP))
219 #define pp_format(PP, TI) pp_base_format (pp_base (PP), TI)
220 #define pp_output_formatted_text(PP) \
221 pp_base_output_formatted_text (pp_base (PP))
222 #define pp_format_verbatim(PP, TI) \
223 pp_base_format_verbatim (pp_base (PP), TI)
225 #define pp_character(PP, C) pp_base_character (pp_base (PP), C)
226 #define pp_string(PP, S) pp_base_string (pp_base (PP), S)
227 #define pp_newline(PP) pp_base_newline (pp_base (PP))
229 #define pp_space(PP) pp_character (PP, ' ')
230 #define pp_left_paren(PP) pp_character (PP, '(')
231 #define pp_right_paren(PP) pp_character (PP, ')')
232 #define pp_left_bracket(PP) pp_character (PP, '[')
233 #define pp_right_bracket(PP) pp_character (PP, ']')
234 #define pp_left_brace(PP) pp_character (PP, '{')
235 #define pp_right_brace(PP) pp_character (PP, '}')
236 #define pp_semicolon(PP) pp_character (PP, ';')
237 #define pp_comma(PP) pp_string (PP, ", ")
238 #define pp_dot(PP) pp_character (PP, '.')
239 #define pp_colon(PP) pp_character (PP, ':')
240 #define pp_colon_colon(PP) pp_string (PP, "::")
241 #define pp_arrow(PP) pp_string (PP, "->")
242 #define pp_equal(PP) pp_character (PP, '=')
243 #define pp_question(PP) pp_character (PP, '?')
244 #define pp_bar(PP) pp_character (PP, '|')
245 #define pp_carret(PP) pp_character (PP, '^')
246 #define pp_ampersand(PP) pp_character (PP, '&')
247 #define pp_less(PP) pp_character (PP, '<')
248 #define pp_greater(PP) pp_character (PP, '>')
249 #define pp_plus(PP) pp_character (PP, '+')
250 #define pp_minus(PP) pp_character (PP, '-')
251 #define pp_star(PP) pp_character (PP, '*')
252 #define pp_slash(PP) pp_character (PP, '/')
253 #define pp_modulo(PP) pp_character (PP, '%')
254 #define pp_exclamation(PP) pp_character (PP, '!')
255 #define pp_complement(PP) pp_character (PP, '~')
256 #define pp_quote(PP) pp_character (PP, '\'')
257 #define pp_backquote(PP) pp_character (PP, '`')
258 #define pp_doublequote(PP) pp_character (PP, '"')
259 #define pp_newline_and_flush(PP) \
260 do { \
261 pp_newline (PP); \
262 pp_flush (PP); \
263 pp_needs_newline (PP) = false; \
264 } while (0)
265 #define pp_newline_and_indent(PP, N) \
266 do { \
267 pp_indentation (PP) += N; \
268 pp_newline (PP); \
269 pp_base_indent (pp_base (PP)); \
270 pp_needs_newline (PP) = false; \
271 } while (0)
272 #define pp_maybe_newline_and_indent(PP, N) \
273 if (pp_needs_newline (PP)) pp_newline_and_indent (PP, N)
274 #define pp_maybe_space(PP) pp_base_maybe_space (pp_base (PP))
275 #define pp_separate_with(PP, C) \
276 do { \
277 pp_character (PP, C); \
278 pp_space (PP); \
279 } while (0)
280 #define pp_scalar(PP, FORMAT, SCALAR) \
281 do \
283 sprintf (pp_buffer (PP)->digit_buffer, FORMAT, SCALAR); \
284 pp_string (PP, pp_buffer (PP)->digit_buffer); \
286 while (0)
287 #define pp_decimal_int(PP, I) pp_scalar (PP, "%d", I)
288 #define pp_unsigned_wide_integer(PP, I) \
289 pp_scalar (PP, HOST_WIDE_INT_PRINT_UNSIGNED, (unsigned HOST_WIDE_INT) I)
290 #define pp_wide_integer(PP, I) \
291 pp_scalar (PP, HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT) I)
292 #define pp_widest_integer(PP, I) \
293 pp_scalar (PP, HOST_WIDEST_INT_PRINT_DEC, (HOST_WIDEST_INT) I)
294 #define pp_pointer(PP, P) pp_scalar (PP, "%p", P)
296 #define pp_identifier(PP, ID) pp_string (PP, (pp_translate_identifiers (PP) \
297 ? identifier_to_locale (ID) \
298 : (ID)))
301 #define pp_buffer(PP) pp_base (PP)->buffer
302 /* Clients that directly derive from pretty_printer need to override
303 this macro to return a pointer to the base pretty_printer structure. */
304 #define pp_base(PP) (PP)
306 extern void pp_construct (pretty_printer *, const char *, int);
307 extern void pp_base_set_line_maximum_length (pretty_printer *, int);
308 extern void pp_base_set_prefix (pretty_printer *, const char *);
309 extern void pp_base_destroy_prefix (pretty_printer *);
310 extern int pp_base_remaining_character_count_for_line (pretty_printer *);
311 extern void pp_base_clear_output_area (pretty_printer *);
312 extern const char *pp_base_formatted_text (pretty_printer *);
313 extern const char *pp_base_last_position_in_text (const pretty_printer *);
314 extern void pp_base_emit_prefix (pretty_printer *);
315 extern void pp_base_append_text (pretty_printer *, const char *, const char *);
317 /* If we haven't already defined a front-end-specific diagnostics
318 style, use the generic one. */
319 #ifdef GCC_DIAG_STYLE
320 #define GCC_PPDIAG_STYLE GCC_DIAG_STYLE
321 #else
322 #define GCC_PPDIAG_STYLE __gcc_diag__
323 #endif
325 /* This header may be included before diagnostics-core.h, hence the duplicate
326 definitions to allow for GCC-specific formats. */
327 #if GCC_VERSION >= 3005
328 #define ATTRIBUTE_GCC_PPDIAG(m, n) __attribute__ ((__format__ (GCC_PPDIAG_STYLE, m ,n))) ATTRIBUTE_NONNULL(m)
329 #else
330 #define ATTRIBUTE_GCC_PPDIAG(m, n) ATTRIBUTE_NONNULL(m)
331 #endif
332 extern void pp_printf (pretty_printer *, const char *, ...)
333 ATTRIBUTE_GCC_PPDIAG(2,3);
335 extern void pp_verbatim (pretty_printer *, const char *, ...)
336 ATTRIBUTE_GCC_PPDIAG(2,3);
337 extern void pp_base_flush (pretty_printer *);
338 extern void pp_base_format (pretty_printer *, text_info *);
339 extern void pp_base_output_formatted_text (pretty_printer *);
340 extern void pp_base_format_verbatim (pretty_printer *, text_info *);
342 extern void pp_base_indent (pretty_printer *);
343 extern void pp_base_newline (pretty_printer *);
344 extern void pp_base_character (pretty_printer *, int);
345 extern void pp_base_string (pretty_printer *, const char *);
346 extern void pp_write_text_to_stream (pretty_printer *pp);
347 extern void pp_base_maybe_space (pretty_printer *);
349 /* Switch into verbatim mode and return the old mode. */
350 static inline pp_wrapping_mode_t
351 pp_set_verbatim_wrapping_ (pretty_printer *pp)
353 pp_wrapping_mode_t oldmode = pp_wrapping_mode (pp);
354 pp_line_cutoff (pp) = 0;
355 pp_prefixing_rule (pp) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
356 return oldmode;
358 #define pp_set_verbatim_wrapping(PP) pp_set_verbatim_wrapping_ (pp_base (PP))
360 extern const char *identifier_to_locale (const char *);
361 extern void *(*identifier_to_locale_alloc) (size_t);
362 extern void (*identifier_to_locale_free) (void *);
364 #endif /* GCC_PRETTY_PRINT_H */