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