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