]>
Commit | Line | Data |
---|---|---|
75e76012 | 1 | /* Various declarations for language-independent pretty-print subroutines. |
8c4c00c1 | 2 | Copyright (C) 2002, 2003, 2004, 2007 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" |
25 | #include "input.h" | |
26 | ||
c907c5b1 | 27 | /* Maximum number of format string arguments. */ |
28 | #define PP_NL_ARGMAX 30 | |
29 | ||
aa6db498 | 30 | /* The type of a text to be formatted according a format specification |
31 | along with a list of things. */ | |
32 | typedef struct | |
33 | { | |
34 | const char *format_spec; | |
35 | va_list *args_ptr; | |
36 | int err_no; /* for %m */ | |
c907c5b1 | 37 | location_t *locus; |
b8c23db3 | 38 | tree *abstract_origin; |
aa6db498 | 39 | } text_info; |
40 | ||
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 | |
45 | line is started. */ | |
46 | typedef enum | |
47 | { | |
48 | DIAGNOSTICS_SHOW_PREFIX_ONCE = 0x0, | |
49 | DIAGNOSTICS_SHOW_PREFIX_NEVER = 0x1, | |
50 | DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE = 0x2 | |
51 | } diagnostic_prefixing_rule_t; | |
52 | ||
c907c5b1 | 53 | /* The chunk_info data structure forms a stack of the results from the |
54 | first phase of formatting (pp_base_format) which have not yet been | |
55 | output (pp_base_output_formatted_text). A stack is necessary because | |
56 | the diagnostic starter may decide to generate its own output by way | |
57 | of the formatter. */ | |
58 | struct chunk_info | |
59 | { | |
60 | /* Pointer to previous chunk on the stack. */ | |
61 | struct chunk_info *prev; | |
62 | ||
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]; | |
70 | }; | |
71 | ||
aa6db498 | 72 | /* The output buffer datatype. This is best seen as an abstract datatype |
73 | whose fields should not be accessed directly by clients. */ | |
74 | typedef struct | |
75 | { | |
c907c5b1 | 76 | /* Obstack where the text is built up. */ |
77 | struct obstack formatted_obstack; | |
78 | ||
79 | /* Obstack containing a chunked representation of the format | |
80 | specification plus arguments. */ | |
81 | struct obstack chunk_obstack; | |
82 | ||
83 | /* Currently active obstack: one of the above two. This is used so | |
84 | that the text formatters don't need to know which phase we're in. */ | |
85 | struct obstack *obstack; | |
86 | ||
87 | /* Stack of chunk arrays. These come from the chunk_obstack. */ | |
88 | struct chunk_info *cur_chunk_array; | |
aa6db498 | 89 | |
90 | /* Where to output formatted text. */ | |
91 | FILE *stream; | |
92 | ||
93 | /* The amount of characters output so far. */ | |
94 | int line_length; | |
95 | ||
96 | /* This must be large enough to hold any printed integer or | |
97 | floating-point value. */ | |
98 | char digit_buffer[128]; | |
99 | } output_buffer; | |
75e76012 | 100 | |
75e76012 | 101 | /* The type of pretty-printer flags passed to clients. */ |
102 | typedef unsigned int pp_flags; | |
103 | ||
b38e286c | 104 | typedef enum |
75e76012 | 105 | { |
71a36b9f | 106 | pp_none, pp_before, pp_after |
b38e286c | 107 | } pp_padding; |
75e76012 | 108 | |
c907c5b1 | 109 | /* Structure for switching in and out of verbatim mode in a convenient |
110 | manner. */ | |
111 | typedef struct | |
112 | { | |
113 | /* Current prefixing rule. */ | |
114 | diagnostic_prefixing_rule_t rule; | |
115 | ||
116 | /* The ideal upper bound of number of characters per line, as suggested | |
117 | by front-end. */ | |
118 | int line_cutoff; | |
119 | } pp_wrapping_mode_t; | |
120 | ||
121 | /* Maximum characters per line in automatic line wrapping mode. | |
122 | Zero means don't wrap lines. */ | |
123 | #define pp_line_cutoff(PP) pp_base (PP)->wrapping.line_cutoff | |
124 | ||
125 | /* Prefixing rule used in formatting a diagnostic message. */ | |
126 | #define pp_prefixing_rule(PP) pp_base (PP)->wrapping.rule | |
127 | ||
128 | /* Get or set the wrapping mode as a single entity. */ | |
129 | #define pp_wrapping_mode(PP) pp_base (PP)->wrapping | |
130 | ||
aa6db498 | 131 | /* The type of a hook that formats client-specific data onto a pretty_pinter. |
132 | A client-supplied formatter returns true if everything goes well, | |
133 | otherwise it returns false. */ | |
134 | typedef struct pretty_print_info pretty_printer; | |
c907c5b1 | 135 | typedef bool (*printer_fn) (pretty_printer *, text_info *, const char *, |
136 | int, bool, bool, bool); | |
aa6db498 | 137 | |
138 | /* Client supplied function used to decode formats. */ | |
139 | #define pp_format_decoder(PP) pp_base (PP)->format_decoder | |
140 | ||
141 | /* TRUE if a newline character needs to be added before further | |
142 | formatting. */ | |
143 | #define pp_needs_newline(PP) pp_base (PP)->need_newline | |
144 | ||
aa6db498 | 145 | /* True if PRETTY-PTINTER is in line-wrapping mode. */ |
146 | #define pp_is_wrapping_line(PP) (pp_line_cutoff (PP) > 0) | |
147 | ||
aa6db498 | 148 | /* The amount of whitespace to be emitted when starting a new line. */ |
149 | #define pp_indentation(PP) pp_base (PP)->indent_skip | |
150 | ||
151 | /* The data structure that contains the bare minimum required to do | |
152 | proper pretty-printing. Clients may derived from this structure | |
153 | and add additional fields they need. */ | |
75e76012 | 154 | struct pretty_print_info |
155 | { | |
75e76012 | 156 | /* Where we print external representation of ENTITY. */ |
157 | output_buffer *buffer; | |
aa6db498 | 158 | |
159 | /* The prefix for each new line. */ | |
160 | const char *prefix; | |
aa6db498 | 161 | |
b38e286c | 162 | /* Where to put whitespace around the entity being formatted. */ |
163 | pp_padding padding; | |
aa6db498 | 164 | |
165 | /* The real upper bound of number of characters per line, taking into | |
166 | account the case of a very very looong prefix. */ | |
167 | int maximum_length; | |
168 | ||
aa6db498 | 169 | /* Indentation count. */ |
170 | int indent_skip; | |
171 | ||
c907c5b1 | 172 | /* Current wrapping mode. */ |
173 | pp_wrapping_mode_t wrapping; | |
aa6db498 | 174 | |
175 | /* If non-NULL, this function formats a TEXT into the BUFFER. When called, | |
176 | TEXT->format_spec points to a format code. FORMAT_DECODER should call | |
177 | pp_string (and related functions) to add data to the BUFFER. | |
178 | FORMAT_DECODER can read arguments from *TEXT->args_pts using VA_ARG. | |
179 | If the BUFFER needs additional characters from the format string, it | |
180 | should advance the TEXT->format_spec as it goes. When FORMAT_DECODER | |
181 | returns, TEXT->format_spec should point to the last character processed. | |
182 | */ | |
183 | printer_fn format_decoder; | |
184 | ||
185 | /* Nonzero if current PREFIX was emitted at least once. */ | |
186 | bool emitted_prefix; | |
187 | ||
188 | /* Nonzero means one should emit a newline before outputting anything. */ | |
189 | bool need_newline; | |
75e76012 | 190 | }; |
191 | ||
0de2b732 | 192 | #define pp_set_line_maximum_length(PP, L) \ |
193 | pp_base_set_line_maximum_length (pp_base (PP), L) | |
194 | #define pp_set_prefix(PP, P) pp_base_set_prefix (pp_base (PP), P) | |
195 | #define pp_destroy_prefix(PP) pp_base_destroy_prefix (pp_base (PP)) | |
196 | #define pp_remaining_character_count_for_line(PP) \ | |
197 | pp_base_remaining_character_count_for_line (pp_base (PP)) | |
198 | #define pp_clear_output_area(PP) \ | |
199 | pp_base_clear_output_area (pp_base (PP)) | |
200 | #define pp_formatted_text(PP) pp_base_formatted_text (pp_base (PP)) | |
201 | #define pp_last_position_in_text(PP) \ | |
202 | pp_base_last_position_in_text (pp_base (PP)) | |
203 | #define pp_emit_prefix(PP) pp_base_emit_prefix (pp_base (PP)) | |
204 | #define pp_append_text(PP, B, E) \ | |
205 | pp_base_append_text (pp_base (PP), B, E) | |
206 | #define pp_flush(PP) pp_base_flush (pp_base (PP)) | |
c907c5b1 | 207 | #define pp_format(PP, TI) pp_base_format (pp_base (PP), TI) |
208 | #define pp_output_formatted_text(PP) \ | |
209 | pp_base_output_formatted_text (pp_base (PP)) | |
0de2b732 | 210 | #define pp_format_verbatim(PP, TI) \ |
211 | pp_base_format_verbatim (pp_base (PP), TI) | |
212 | ||
76a6451b | 213 | #define pp_character(PP, C) pp_base_character (pp_base (PP), C) |
214 | #define pp_string(PP, S) pp_base_string (pp_base (PP), S) | |
215 | #define pp_newline(PP) pp_base_newline (pp_base (PP)) | |
0de2b732 | 216 | |
76a6451b | 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_string (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_carret(PP) pp_character (PP, '^') | |
234 | #define pp_ampersand(PP) pp_character (PP, '&') | |
235 | #define pp_less(PP) pp_character (PP, '<') | |
236 | #define pp_greater(PP) pp_character (PP, '>') | |
237 | #define pp_plus(PP) pp_character (PP, '+') | |
238 | #define pp_minus(PP) pp_character (PP, '-') | |
239 | #define pp_star(PP) pp_character (PP, '*') | |
240 | #define pp_slash(PP) pp_character (PP, '/') | |
241 | #define pp_modulo(PP) pp_character (PP, '%') | |
242 | #define pp_exclamation(PP) pp_character (PP, '!') | |
243 | #define pp_complement(PP) pp_character (PP, '~') | |
244 | #define pp_quote(PP) pp_character (PP, '\'') | |
245 | #define pp_backquote(PP) pp_character (PP, '`') | |
246 | #define pp_doublequote(PP) pp_character (PP, '"') | |
aa6db498 | 247 | #define pp_newline_and_indent(PP, N) \ |
248 | do { \ | |
249 | pp_indentation (PP) += N; \ | |
250 | pp_newline (PP); \ | |
bfdec0d1 | 251 | pp_base_indent (pp_base (PP)); \ |
252 | pp_needs_newline (PP) = false; \ | |
b38e286c | 253 | } while (0) |
c10de5e7 | 254 | #define pp_maybe_newline_and_indent(PP, N) \ |
255 | if (pp_needs_newline (PP)) pp_newline_and_indent (PP, N) | |
69cb846f | 256 | #define pp_maybe_space(PP) pp_base_maybe_space (pp_base (PP)) |
aa6db498 | 257 | #define pp_separate_with(PP, C) \ |
258 | do { \ | |
76a6451b | 259 | pp_character (PP, C); \ |
aa6db498 | 260 | pp_space (PP); \ |
b38e286c | 261 | } while (0) |
76a6451b | 262 | #define pp_scalar(PP, FORMAT, SCALAR) \ |
263 | do \ | |
264 | { \ | |
265 | sprintf (pp_buffer (PP)->digit_buffer, FORMAT, SCALAR); \ | |
266 | pp_string (PP, pp_buffer (PP)->digit_buffer); \ | |
267 | } \ | |
aa6db498 | 268 | while (0) |
269 | #define pp_decimal_int(PP, I) pp_scalar (PP, "%d", I) | |
270 | #define pp_wide_integer(PP, I) \ | |
271 | pp_scalar (PP, HOST_WIDE_INT_PRINT_DEC, (HOST_WIDE_INT) I) | |
327a38c3 | 272 | #define pp_widest_integer(PP, I) \ |
273 | pp_scalar (PP, HOST_WIDEST_INT_PRINT_DEC, (HOST_WIDEST_INT) I) | |
aa6db498 | 274 | #define pp_pointer(PP, P) pp_scalar (PP, "%p", P) |
75e76012 | 275 | |
76a6451b | 276 | #define pp_identifier(PP, ID) pp_string (PP, ID) |
aa6db498 | 277 | #define pp_tree_identifier(PP, T) \ |
0de2b732 | 278 | pp_append_text(PP, IDENTIFIER_POINTER (T), \ |
aa6db498 | 279 | IDENTIFIER_POINTER (T) + IDENTIFIER_LENGTH (T)) |
71a36b9f | 280 | |
76a6451b | 281 | #define pp_unsupported_tree(PP, T) \ |
1e5fcbe2 | 282 | pp_verbatim (pp_base (PP), "#%qs not supported by %s#", \ |
76a6451b | 283 | tree_code_name[(int) TREE_CODE (T)], __FUNCTION__) |
71a36b9f | 284 | |
aa6db498 | 285 | |
76a6451b | 286 | #define pp_buffer(PP) pp_base (PP)->buffer |
aa6db498 | 287 | /* Clients that directly derive from pretty_printer need to override |
40e55fbb | 288 | this macro to return a pointer to the base pretty_printer structure. */ |
aa6db498 | 289 | #define pp_base(PP) (PP) |
290 | ||
291 | extern void pp_construct (pretty_printer *, const char *, int); | |
0de2b732 | 292 | extern void pp_base_set_line_maximum_length (pretty_printer *, int); |
293 | extern void pp_base_set_prefix (pretty_printer *, const char *); | |
294 | extern void pp_base_destroy_prefix (pretty_printer *); | |
295 | extern int pp_base_remaining_character_count_for_line (pretty_printer *); | |
296 | extern void pp_base_clear_output_area (pretty_printer *); | |
297 | extern const char *pp_base_formatted_text (pretty_printer *); | |
298 | extern const char *pp_base_last_position_in_text (const pretty_printer *); | |
299 | extern void pp_base_emit_prefix (pretty_printer *); | |
300 | extern void pp_base_append_text (pretty_printer *, const char *, const char *); | |
eb586f2c | 301 | |
302 | /* This header may be included before toplev.h, hence the duplicate | |
303 | definitions to allow for GCC-specific formats. */ | |
304 | #if GCC_VERSION >= 3005 | |
305 | #define ATTRIBUTE_GCC_PPDIAG(m, n) __attribute__ ((__format__ (__gcc_diag__, m ,n))) ATTRIBUTE_NONNULL(m) | |
306 | #else | |
307 | #define ATTRIBUTE_GCC_PPDIAG(m, n) ATTRIBUTE_NONNULL(m) | |
308 | #endif | |
309 | extern void pp_printf (pretty_printer *, const char *, ...) | |
310 | ATTRIBUTE_GCC_PPDIAG(2,3); | |
311 | ||
a208cd0d | 312 | extern void pp_verbatim (pretty_printer *, const char *, ...) |
313 | ATTRIBUTE_GCC_PPDIAG(2,3); | |
0de2b732 | 314 | extern void pp_base_flush (pretty_printer *); |
c907c5b1 | 315 | extern void pp_base_format (pretty_printer *, text_info *); |
316 | extern void pp_base_output_formatted_text (pretty_printer *); | |
0de2b732 | 317 | extern void pp_base_format_verbatim (pretty_printer *, text_info *); |
aa6db498 | 318 | |
bfdec0d1 | 319 | extern void pp_base_indent (pretty_printer *); |
76a6451b | 320 | extern void pp_base_newline (pretty_printer *); |
321 | extern void pp_base_character (pretty_printer *, int); | |
322 | extern void pp_base_string (pretty_printer *, const char *); | |
4ee9c684 | 323 | extern void pp_write_text_to_stream (pretty_printer *pp); |
69cb846f | 324 | extern void pp_base_maybe_space (pretty_printer *); |
aa6db498 | 325 | |
c907c5b1 | 326 | /* Switch into verbatim mode and return the old mode. */ |
327 | static inline pp_wrapping_mode_t | |
328 | pp_set_verbatim_wrapping_ (pretty_printer *pp) | |
329 | { | |
330 | pp_wrapping_mode_t oldmode = pp_wrapping_mode (pp); | |
331 | pp_line_cutoff (pp) = 0; | |
332 | pp_prefixing_rule (pp) = DIAGNOSTICS_SHOW_PREFIX_NEVER; | |
333 | return oldmode; | |
334 | } | |
335 | #define pp_set_verbatim_wrapping(PP) pp_set_verbatim_wrapping_ (pp_base (PP)) | |
336 | ||
75e76012 | 337 | #endif /* GCC_PRETTY_PRINT_H */ |