1 /* Various declarations for language-independent pretty-print subroutines.
2 Copyright (C) 2002-2014 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
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
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
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/>. */
21 #ifndef GCC_PRETTY_PRINT_H
22 #define GCC_PRETTY_PRINT_H
27 /* Maximum number of format string arguments. */
28 #define PP_NL_ARGMAX 30
30 /* The type of a text to be formatted according a format specification
31 along with a list of things. */
34 const char *format_spec
;
36 int err_no
; /* for %m */
41 /* How often diagnostics are prefixed by their locations:
42 o DIAGNOSTICS_SHOW_PREFIX_NEVER: never - not yet supported;
43 o DIAGNOSTICS_SHOW_PREFIX_ONCE: emit only once;
44 o DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE: emit each time a physical
46 enum diagnostic_prefixing_rule_t
48 DIAGNOSTICS_SHOW_PREFIX_ONCE
= 0x0,
49 DIAGNOSTICS_SHOW_PREFIX_NEVER
= 0x1,
50 DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE
= 0x2
53 /* The chunk_info data structure forms a stack of the results from the
54 first phase of formatting (pp_format) which have not yet been
55 output (pp_output_formatted_text). A stack is necessary because
56 the diagnostic starter may decide to generate its own output by way
60 /* Pointer to previous chunk on the stack. */
61 struct chunk_info
*prev
;
63 /* Array of chunks to output. Each chunk is a NUL-terminated string.
64 In the first phase of formatting, even-numbered chunks are
65 to be output verbatim, odd-numbered chunks are format specifiers.
66 The second phase replaces all odd-numbered chunks with formatted
67 text, and the third phase simply emits all the chunks in sequence
68 with appropriate line-wrapping. */
69 const char *args
[PP_NL_ARGMAX
* 2];
72 /* The output buffer datatype. This is best seen as an abstract datatype
73 whose fields should not be accessed directly by clients. */
79 /* Obstack where the text is built up. */
80 struct obstack formatted_obstack
;
82 /* Obstack containing a chunked representation of the format
83 specification plus arguments. */
84 struct obstack chunk_obstack
;
86 /* Currently active obstack: one of the above two. This is used so
87 that the text formatters don't need to know which phase we're in. */
88 struct obstack
*obstack
;
90 /* Stack of chunk arrays. These come from the chunk_obstack. */
91 struct chunk_info
*cur_chunk_array
;
93 /* Where to output formatted text. */
96 /* The amount of characters output so far. */
99 /* This must be large enough to hold any printed integer or
100 floating-point value. */
101 char digit_buffer
[128];
104 /* The type of pretty-printer flags passed to clients. */
105 typedef unsigned int pp_flags
;
109 pp_none
, pp_before
, pp_after
112 /* Structure for switching in and out of verbatim mode in a convenient
114 struct pp_wrapping_mode_t
116 /* Current prefixing rule. */
117 diagnostic_prefixing_rule_t rule
;
119 /* The ideal upper bound of number of characters per line, as suggested
124 /* Maximum characters per line in automatic line wrapping mode.
125 Zero means don't wrap lines. */
126 #define pp_line_cutoff(PP) (PP)->wrapping.line_cutoff
128 /* Prefixing rule used in formatting a diagnostic message. */
129 #define pp_prefixing_rule(PP) (PP)->wrapping.rule
131 /* Get or set the wrapping mode as a single entity. */
132 #define pp_wrapping_mode(PP) (PP)->wrapping
134 /* The type of a hook that formats client-specific data onto a pretty_pinter.
135 A client-supplied formatter returns true if everything goes well,
136 otherwise it returns false. */
137 typedef bool (*printer_fn
) (pretty_printer
*, text_info
*, const char *,
138 int, bool, bool, bool);
140 /* Client supplied function used to decode formats. */
141 #define pp_format_decoder(PP) (PP)->format_decoder
143 /* TRUE if a newline character needs to be added before further
145 #define pp_needs_newline(PP) (PP)->need_newline
147 /* True if PRETTY-PRINTER is in line-wrapping mode. */
148 #define pp_is_wrapping_line(PP) (pp_line_cutoff (PP) > 0)
150 /* The amount of whitespace to be emitted when starting a new line. */
151 #define pp_indentation(PP) (PP)->indent_skip
153 /* True if identifiers are translated to the locale character set on
155 #define pp_translate_identifiers(PP) (PP)->translate_identifiers
157 /* True if colors should be shown. */
158 #define pp_show_color(PP) (PP)->show_color
160 /* The data structure that contains the bare minimum required to do
161 proper pretty-printing. Clients may derived from this structure
162 and add additional fields they need. */
163 struct pretty_printer
165 // Default construct a pretty printer with specified prefix
166 // and a maximum line length cut off limit.
167 explicit pretty_printer (const char* = NULL
, int = 0);
169 virtual ~pretty_printer ();
171 /* Where we print external representation of ENTITY. */
172 output_buffer
*buffer
;
174 /* The prefix for each new line. */
177 /* Where to put whitespace around the entity being formatted. */
180 /* The real upper bound of number of characters per line, taking into
181 account the case of a very very looong prefix. */
184 /* Indentation count. */
187 /* Current wrapping mode. */
188 pp_wrapping_mode_t wrapping
;
190 /* If non-NULL, this function formats a TEXT into the BUFFER. When called,
191 TEXT->format_spec points to a format code. FORMAT_DECODER should call
192 pp_string (and related functions) to add data to the BUFFER.
193 FORMAT_DECODER can read arguments from *TEXT->args_pts using VA_ARG.
194 If the BUFFER needs additional characters from the format string, it
195 should advance the TEXT->format_spec as it goes. When FORMAT_DECODER
196 returns, TEXT->format_spec should point to the last character processed.
198 printer_fn format_decoder
;
200 /* Nonzero if current PREFIX was emitted at least once. */
203 /* Nonzero means one should emit a newline before outputting anything. */
206 /* Nonzero means identifiers are translated to the locale character
208 bool translate_identifiers
;
210 /* Nonzero means that text should be colorized. */
214 static inline const char *
215 pp_get_prefix (const pretty_printer
*pp
) { return pp
->prefix
; }
217 #define pp_space(PP) pp_character (PP, ' ')
218 #define pp_left_paren(PP) pp_character (PP, '(')
219 #define pp_right_paren(PP) pp_character (PP, ')')
220 #define pp_left_bracket(PP) pp_character (PP, '[')
221 #define pp_right_bracket(PP) pp_character (PP, ']')
222 #define pp_left_brace(PP) pp_character (PP, '{')
223 #define pp_right_brace(PP) pp_character (PP, '}')
224 #define pp_semicolon(PP) pp_character (PP, ';')
225 #define pp_comma(PP) pp_character (PP, ',')
226 #define pp_dot(PP) pp_character (PP, '.')
227 #define pp_colon(PP) pp_character (PP, ':')
228 #define pp_colon_colon(PP) pp_string (PP, "::")
229 #define pp_arrow(PP) pp_string (PP, "->")
230 #define pp_equal(PP) pp_character (PP, '=')
231 #define pp_question(PP) pp_character (PP, '?')
232 #define pp_bar(PP) pp_character (PP, '|')
233 #define pp_bar_bar(PP) pp_string (PP, "||")
234 #define pp_carret(PP) pp_character (PP, '^')
235 #define pp_ampersand(PP) pp_character (PP, '&')
236 #define pp_ampersand_ampersand(PP) pp_string (PP, "&&")
237 #define pp_less(PP) pp_character (PP, '<')
238 #define pp_less_equal(PP) pp_string (PP, "<=")
239 #define pp_greater(PP) pp_character (PP, '>')
240 #define pp_greater_equal(PP) pp_string (PP, ">=")
241 #define pp_plus(PP) pp_character (PP, '+')
242 #define pp_minus(PP) pp_character (PP, '-')
243 #define pp_star(PP) pp_character (PP, '*')
244 #define pp_slash(PP) pp_character (PP, '/')
245 #define pp_modulo(PP) pp_character (PP, '%')
246 #define pp_exclamation(PP) pp_character (PP, '!')
247 #define pp_complement(PP) pp_character (PP, '~')
248 #define pp_quote(PP) pp_character (PP, '\'')
249 #define pp_backquote(PP) pp_character (PP, '`')
250 #define pp_doublequote(PP) pp_character (PP, '"')
251 #define pp_underscore(PP) pp_character (PP, '_')
252 #define pp_maybe_newline_and_indent(PP, N) \
253 if (pp_needs_newline (PP)) pp_newline_and_indent (PP, N)
254 #define pp_scalar(PP, FORMAT, SCALAR) \
257 sprintf (pp_buffer (PP)->digit_buffer, FORMAT, SCALAR); \
258 pp_string (PP, pp_buffer (PP)->digit_buffer); \
261 #define pp_decimal_int(PP, I) pp_scalar (PP, "%d", I)
262 #define pp_unsigned_wide_integer(PP, I) \
263 pp_scalar (PP, HOST_WIDE_INT_PRINT_UNSIGNED, (unsigned HOST_WIDE_INT) I)
264 #define pp_wide_integer(PP, I) \
265 pp_scalar (PP, HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT) I)
266 #define pp_widest_integer(PP, I) \
267 pp_scalar (PP, HOST_WIDEST_INT_PRINT_DEC, (HOST_WIDEST_INT) I)
268 #define pp_pointer(PP, P) pp_scalar (PP, "%p", P)
270 #define pp_identifier(PP, ID) pp_string (PP, (pp_translate_identifiers (PP) \
271 ? identifier_to_locale (ID) \
275 #define pp_buffer(PP) (PP)->buffer
277 extern void pp_set_line_maximum_length (pretty_printer
*, int);
278 extern void pp_set_prefix (pretty_printer
*, const char *);
279 extern void pp_destroy_prefix (pretty_printer
*);
280 extern int pp_remaining_character_count_for_line (pretty_printer
*);
281 extern void pp_clear_output_area (pretty_printer
*);
282 extern const char *pp_formatted_text (pretty_printer
*);
283 extern const char *pp_last_position_in_text (const pretty_printer
*);
284 extern void pp_emit_prefix (pretty_printer
*);
285 extern void pp_append_text (pretty_printer
*, const char *, const char *);
286 extern void pp_newline_and_flush (pretty_printer
*);
287 extern void pp_newline_and_indent (pretty_printer
*, int);
288 extern void pp_separate_with (pretty_printer
*, char);
290 /* If we haven't already defined a front-end-specific diagnostics
291 style, use the generic one. */
292 #ifdef GCC_DIAG_STYLE
293 #define GCC_PPDIAG_STYLE GCC_DIAG_STYLE
295 #define GCC_PPDIAG_STYLE __gcc_diag__
298 /* This header may be included before diagnostics-core.h, hence the duplicate
299 definitions to allow for GCC-specific formats. */
300 #if GCC_VERSION >= 3005
301 #define ATTRIBUTE_GCC_PPDIAG(m, n) __attribute__ ((__format__ (GCC_PPDIAG_STYLE, m ,n))) ATTRIBUTE_NONNULL(m)
303 #define ATTRIBUTE_GCC_PPDIAG(m, n) ATTRIBUTE_NONNULL(m)
305 extern void pp_printf (pretty_printer
*, const char *, ...)
306 ATTRIBUTE_GCC_PPDIAG(2,3);
308 extern void pp_verbatim (pretty_printer
*, const char *, ...)
309 ATTRIBUTE_GCC_PPDIAG(2,3);
310 extern void pp_flush (pretty_printer
*);
311 extern void pp_format (pretty_printer
*, text_info
*);
312 extern void pp_output_formatted_text (pretty_printer
*);
313 extern void pp_format_verbatim (pretty_printer
*, text_info
*);
315 extern void pp_indent (pretty_printer
*);
316 extern void pp_newline (pretty_printer
*);
317 extern void pp_character (pretty_printer
*, int);
318 extern void pp_string (pretty_printer
*, const char *);
319 extern void pp_write_text_to_stream (pretty_printer
*);
320 extern void pp_write_text_as_dot_label_to_stream (pretty_printer
*, bool);
321 extern void pp_maybe_space (pretty_printer
*);
323 /* Switch into verbatim mode and return the old mode. */
324 static inline pp_wrapping_mode_t
325 pp_set_verbatim_wrapping_ (pretty_printer
*pp
)
327 pp_wrapping_mode_t oldmode
= pp_wrapping_mode (pp
);
328 pp_line_cutoff (pp
) = 0;
329 pp_prefixing_rule (pp
) = DIAGNOSTICS_SHOW_PREFIX_NEVER
;
332 #define pp_set_verbatim_wrapping(PP) pp_set_verbatim_wrapping_ (PP)
334 extern const char *identifier_to_locale (const char *);
335 extern void *(*identifier_to_locale_alloc
) (size_t);
336 extern void (*identifier_to_locale_free
) (void *);
338 #endif /* GCC_PRETTY_PRINT_H */