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