[PATCH] RISC-V: Move UNSPEC_SSP_SET and UNSPEC_SSP_TEST to correct enum
[gcc.git] / gcc / pretty-print.h
blob066d19d4cdacaf0b15de69f1b5268c03703bdbd0
1 /* Various declarations for language-independent pretty-print subroutines.
2 Copyright (C) 2002-2025 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
10 version.
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.
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
24 #include "obstack.h"
25 #include "rich-location.h"
26 #include "diagnostic-url.h"
28 /* Maximum number of format string arguments. */
29 #define PP_NL_ARGMAX 30
31 /* The type of a text to be formatted according a format specification
32 along with a list of things. */
33 struct text_info
35 text_info () = default;
36 text_info (const char *format_spec,
37 va_list *args_ptr,
38 int err_no,
39 void **data = nullptr,
40 rich_location *rich_loc = nullptr)
41 : m_format_spec (format_spec),
42 m_args_ptr (args_ptr),
43 m_err_no (err_no),
44 m_data (data),
45 m_richloc (rich_loc)
49 void set_location (unsigned int idx, location_t loc,
50 enum range_display_kind range_display_kind);
51 location_t get_location (unsigned int index_of_location) const;
53 const char *m_format_spec;
54 va_list *m_args_ptr;
55 int m_err_no; /* for %m */
56 void **m_data;
57 rich_location *m_richloc;
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. */
65 enum diagnostic_prefixing_rule_t
67 DIAGNOSTICS_SHOW_PREFIX_ONCE = 0x0,
68 DIAGNOSTICS_SHOW_PREFIX_NEVER = 0x1,
69 DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE = 0x2
72 class pp_formatted_chunks;
73 class output_buffer;
74 class pp_token_list;
75 class urlifier;
77 namespace pp_markup {
78 class context;
79 } // namespace pp_markup
81 /* The output buffer datatype. This is best seen as an abstract datatype
82 whose fields should not be accessed directly by clients. */
83 class output_buffer
85 public:
86 output_buffer ();
87 output_buffer (const output_buffer &) = delete;
88 output_buffer (output_buffer &&) = delete;
89 ~output_buffer ();
90 output_buffer & operator= (const output_buffer &) = delete;
91 output_buffer & operator= (output_buffer &&) = delete;
93 pp_formatted_chunks *push_formatted_chunks ();
94 void pop_formatted_chunks ();
96 void dump (FILE *out, int indent) const;
97 void DEBUG_FUNCTION dump () const { dump (stderr, 0); }
99 /* Obstack where the text is built up. */
100 struct obstack m_formatted_obstack;
102 /* Obstack containing a chunked representation of the format
103 specification plus arguments. */
104 struct obstack m_chunk_obstack;
106 /* Currently active obstack: one of the above two. This is used so
107 that the text formatters don't need to know which phase we're in. */
108 struct obstack *m_obstack;
110 /* Topmost element in a stack of arrays of formatted chunks.
111 These come from the chunk_obstack. */
112 pp_formatted_chunks *m_cur_formatted_chunks;
114 /* Where to output formatted text. */
115 FILE *m_stream;
117 /* The amount of characters output so far. */
118 int m_line_length;
120 /* This must be large enough to hold any printed integer or
121 floating-point value. */
122 char m_digit_buffer[128];
124 /* Nonzero means that text should be flushed when
125 appropriate. Otherwise, text is buffered until either
126 pp_really_flush or pp_clear_output_area are called. */
127 bool m_flush_p;
130 /* Finishes constructing a NULL-terminated character string representing
131 the buffered text. */
132 inline const char *
133 output_buffer_formatted_text (output_buffer *buff)
135 obstack_1grow (buff->m_obstack, '\0');
136 return (const char *) obstack_base (buff->m_obstack);
139 /* Append to the output buffer a string specified by its
140 STARTing character and LENGTH. */
141 inline void
142 output_buffer_append_r (output_buffer *buff, const char *start, int length)
144 gcc_checking_assert (start);
145 obstack_grow (buff->m_obstack, start, length);
146 for (int i = 0; i < length; i++)
147 if (start[i] == '\n')
148 buff->m_line_length = 0;
149 else
150 buff->m_line_length++;
153 /* Return a pointer to the last character emitted in the
154 output_buffer. A NULL pointer means no character available. */
155 inline const char *
156 output_buffer_last_position_in_text (const output_buffer *buff)
158 const char *p = NULL;
159 struct obstack *text = buff->m_obstack;
161 if (obstack_base (text) != obstack_next_free (text))
162 p = ((const char *) obstack_next_free (text)) - 1;
163 return p;
167 /* The type of pretty-printer flags passed to clients. */
168 typedef unsigned int pp_flags;
170 enum pp_padding
172 pp_none, pp_before, pp_after
175 /* Structure for switching in and out of verbatim mode in a convenient
176 manner. */
177 struct pp_wrapping_mode_t
179 /* Current prefixing rule. */
180 diagnostic_prefixing_rule_t rule;
182 /* The ideal upper bound of number of characters per line, as suggested
183 by front-end. */
184 int line_cutoff;
187 /* The type of a hook that formats client-specific data onto a pretty_printer.
188 A client-supplied formatter returns true if everything goes well,
189 otherwise it returns false. */
190 typedef bool (*printer_fn) (pretty_printer *, text_info *, const char *,
191 int, bool, bool, bool, bool *, pp_token_list &);
193 /* Base class for an optional client-supplied object for doing additional
194 processing between stages 2 and 3 of formatted printing. */
195 class format_postprocessor
197 public:
198 virtual ~format_postprocessor () {}
199 virtual format_postprocessor *clone() const = 0;
200 virtual void handle (pretty_printer *) = 0;
203 /* Abstract base class for writing formatted tokens to the pretty_printer's
204 text buffer, allowing for output formats and dumpfiles to override
205 how different kinds of tokens are handled. */
207 class token_printer
209 public:
210 virtual ~token_printer () {}
211 virtual void print_tokens (pretty_printer *pp,
212 const pp_token_list &tokens) = 0;
215 inline bool & pp_needs_newline (pretty_printer *pp);
217 /* True if PRETTY-PRINTER is in line-wrapping mode. */
218 #define pp_is_wrapping_line(PP) (pp_line_cutoff (PP) > 0)
220 inline output_buffer *&pp_buffer (pretty_printer *pp);
221 inline output_buffer *pp_buffer (const pretty_printer *pp);
222 inline const char *pp_get_prefix (const pretty_printer *pp);
223 extern char *pp_take_prefix (pretty_printer *);
224 extern void pp_destroy_prefix (pretty_printer *);
225 inline int &pp_line_cutoff (pretty_printer *pp);
226 inline diagnostic_prefixing_rule_t &pp_prefixing_rule (pretty_printer *pp);
227 inline pp_wrapping_mode_t &pp_wrapping_mode (pretty_printer *pp);
228 inline int & pp_indentation (pretty_printer *pp);
229 inline bool & pp_translate_identifiers (pretty_printer *pp);
230 inline bool & pp_show_color (pretty_printer *pp);
231 inline printer_fn &pp_format_decoder (pretty_printer *pp);
232 inline format_postprocessor *& pp_format_postprocessor (pretty_printer *pp);
233 inline bool & pp_show_highlight_colors (pretty_printer *pp);
235 class urlifier;
237 /* The data structure that contains the bare minimum required to do
238 proper pretty-printing. Clients may derive from this structure
239 and add additional fields they need. */
240 class pretty_printer
242 public:
243 friend inline output_buffer *&pp_buffer (pretty_printer *pp);
244 friend inline output_buffer *pp_buffer (const pretty_printer *pp);
245 friend inline const char *pp_get_prefix (const pretty_printer *pp);
246 friend char *pp_take_prefix (pretty_printer *);
247 friend void pp_destroy_prefix (pretty_printer *);
248 friend inline int &pp_line_cutoff (pretty_printer *pp);
249 friend inline diagnostic_prefixing_rule_t &
250 pp_prefixing_rule (pretty_printer *pp);
251 friend inline const diagnostic_prefixing_rule_t &
252 pp_prefixing_rule (const pretty_printer *pp);
253 friend inline pp_wrapping_mode_t &pp_wrapping_mode (pretty_printer *pp);
254 friend bool & pp_needs_newline (pretty_printer *pp);
255 friend int & pp_indentation (pretty_printer *pp);
256 friend bool & pp_translate_identifiers (pretty_printer *pp);
257 friend bool & pp_show_color (pretty_printer *pp);
258 friend printer_fn &pp_format_decoder (pretty_printer *pp);
259 friend format_postprocessor *& pp_format_postprocessor (pretty_printer *pp);
260 friend bool & pp_show_highlight_colors (pretty_printer *pp);
262 friend void pp_output_formatted_text (pretty_printer *,
263 const urlifier *);
265 /* Default construct a pretty printer with specified
266 maximum line length cut off limit. */
267 explicit pretty_printer (int = 0);
268 explicit pretty_printer (const pretty_printer &other);
270 virtual ~pretty_printer ();
272 virtual std::unique_ptr<pretty_printer> clone () const;
274 void set_output_stream (FILE *outfile)
276 m_buffer->m_stream = outfile;
279 void set_token_printer (token_printer* tp)
281 m_token_printer = tp; // borrowed
284 void set_prefix (char *prefix);
286 void emit_prefix ();
288 void format (text_info &text);
290 void maybe_space ();
292 bool supports_urls_p () const { return m_url_format != URL_FORMAT_NONE; }
293 diagnostic_url_format get_url_format () const { return m_url_format; }
294 void set_url_format (diagnostic_url_format url_format)
296 m_url_format = url_format;
299 void begin_url (const char *url);
300 void end_url ();
302 /* Switch into verbatim mode and return the old mode. */
303 pp_wrapping_mode_t
304 set_verbatim_wrapping ()
306 const pp_wrapping_mode_t oldmode = pp_wrapping_mode (this);
307 pp_line_cutoff (this) = 0;
308 pp_prefixing_rule (this) = DIAGNOSTICS_SHOW_PREFIX_NEVER;
309 return oldmode;
312 void set_padding (pp_padding padding) { m_padding = padding; }
313 pp_padding get_padding () const { return m_padding; }
315 void clear_state ();
316 void set_real_maximum_length ();
317 int remaining_character_count_for_line ();
319 void dump (FILE *out, int indent) const;
320 void DEBUG_FUNCTION dump () const { dump (stderr, 0); }
322 private:
323 /* Where we print external representation of ENTITY. */
324 output_buffer *m_buffer;
326 /* The prefix for each new line. If non-NULL, this is "owned" by the
327 pretty_printer, and will eventually be free-ed. */
328 char *m_prefix;
330 /* Where to put whitespace around the entity being formatted. */
331 pp_padding m_padding;
333 /* The real upper bound of number of characters per line, taking into
334 account the case of a very very looong prefix. */
335 int m_maximum_length;
337 /* Indentation count. */
338 int m_indent_skip;
340 /* Current wrapping mode. */
341 pp_wrapping_mode_t m_wrapping;
343 /* If non-NULL, this function formats a TEXT into the BUFFER. When called,
344 TEXT->format_spec points to a format code. FORMAT_DECODER should call
345 pp_string (and related functions) to add data to the BUFFER.
346 FORMAT_DECODER can read arguments from *TEXT->args_pts using VA_ARG.
347 If the BUFFER needs additional characters from the format string, it
348 should advance the TEXT->format_spec as it goes. When FORMAT_DECODER
349 returns, TEXT->format_spec should point to the last character processed.
350 The QUOTE and FORMATTED_TOKEN_LIST are passed in, to allow for
351 deferring-handling of format codes (e.g. %H and %I in
352 the C++ frontend). */
353 printer_fn m_format_decoder;
355 /* If non-NULL, this is called by pp_format once after all format codes
356 have been processed, to allow for client-specific postprocessing.
357 This is used by the C++ frontend for handling the %H and %I
358 format codes (which interract with each other). */
359 format_postprocessor *m_format_postprocessor;
361 /* This is used by pp_output_formatted_text after it has converted all
362 formatted chunks into a single list of tokens.
363 Can be nullptr.
364 Borrowed from the output format or from dump_pretty_printer. */
365 token_printer *m_token_printer;
367 /* Nonzero if current PREFIX was emitted at least once. */
368 bool m_emitted_prefix;
370 /* Nonzero means one should emit a newline before outputting anything. */
371 bool m_need_newline;
373 /* Nonzero means identifiers are translated to the locale character
374 set on output. */
375 bool m_translate_identifiers;
377 /* Nonzero means that text should be colorized. */
378 bool m_show_color;
380 /* True means that pertinent sections within the text should be
381 highlighted with color. */
382 bool m_show_highlight_colors;
384 /* Whether URLs should be emitted, and which terminator to use. */
385 diagnostic_url_format m_url_format;
387 /* If true, then we've had a begin_url (nullptr), and so the
388 next end_url should be a no-op. */
389 bool m_skipping_null_url;
392 inline output_buffer *&
393 pp_buffer (pretty_printer *pp)
395 return pp->m_buffer;
398 inline output_buffer *
399 pp_buffer (const pretty_printer *pp)
401 return pp->m_buffer;
404 inline const char *
405 pp_get_prefix (const pretty_printer *pp)
407 return pp->m_prefix;
410 /* TRUE if a newline character needs to be added before further
411 formatting. */
412 inline bool &
413 pp_needs_newline (pretty_printer *pp)
415 return pp->m_need_newline;
418 /* The amount of whitespace to be emitted when starting a new line. */
419 inline int &
420 pp_indentation (pretty_printer *pp)
422 return pp->m_indent_skip;
425 /* True if identifiers are translated to the locale character set on
426 output. */
427 inline bool &
428 pp_translate_identifiers (pretty_printer *pp)
430 return pp->m_translate_identifiers;
433 /* True if colors should be shown. */
434 inline bool &
435 pp_show_color (pretty_printer *pp)
437 return pp->m_show_color;
440 inline printer_fn &
441 pp_format_decoder (pretty_printer *pp)
443 return pp->m_format_decoder;
446 inline format_postprocessor *&
447 pp_format_postprocessor (pretty_printer *pp)
449 return pp->m_format_postprocessor;
452 inline bool &
453 pp_show_highlight_colors (pretty_printer *pp)
455 return pp->m_show_highlight_colors;
458 /* Maximum characters per line in automatic line wrapping mode.
459 Zero means don't wrap lines. */
460 inline int &
461 pp_line_cutoff (pretty_printer *pp)
463 return pp->m_wrapping.line_cutoff;
466 /* Prefixing rule used in formatting a diagnostic message. */
467 inline diagnostic_prefixing_rule_t &
468 pp_prefixing_rule (pretty_printer *pp)
470 return pp->m_wrapping.rule;
472 inline const diagnostic_prefixing_rule_t &
473 pp_prefixing_rule (const pretty_printer *pp)
475 return pp->m_wrapping.rule;
478 /* Get or set the wrapping mode as a single entity. */
479 inline pp_wrapping_mode_t &
480 pp_wrapping_mode (pretty_printer *pp)
482 return pp->m_wrapping;
485 #define pp_space(PP) pp_character (PP, ' ')
486 #define pp_left_paren(PP) pp_character (PP, '(')
487 #define pp_right_paren(PP) pp_character (PP, ')')
488 #define pp_left_bracket(PP) pp_character (PP, '[')
489 #define pp_right_bracket(PP) pp_character (PP, ']')
490 #define pp_left_brace(PP) pp_character (PP, '{')
491 #define pp_right_brace(PP) pp_character (PP, '}')
492 #define pp_semicolon(PP) pp_character (PP, ';')
493 #define pp_comma(PP) pp_character (PP, ',')
494 #define pp_dot(PP) pp_character (PP, '.')
495 #define pp_colon(PP) pp_character (PP, ':')
496 #define pp_colon_colon(PP) pp_string (PP, "::")
497 #define pp_arrow(PP) pp_string (PP, "->")
498 #define pp_equal(PP) pp_character (PP, '=')
499 #define pp_question(PP) pp_character (PP, '?')
500 #define pp_bar(PP) pp_character (PP, '|')
501 #define pp_bar_bar(PP) pp_string (PP, "||")
502 #define pp_carret(PP) pp_character (PP, '^')
503 #define pp_ampersand(PP) pp_character (PP, '&')
504 #define pp_ampersand_ampersand(PP) pp_string (PP, "&&")
505 #define pp_less(PP) pp_character (PP, '<')
506 #define pp_less_equal(PP) pp_string (PP, "<=")
507 #define pp_greater(PP) pp_character (PP, '>')
508 #define pp_greater_equal(PP) pp_string (PP, ">=")
509 #define pp_plus(PP) pp_character (PP, '+')
510 #define pp_minus(PP) pp_character (PP, '-')
511 #define pp_star(PP) pp_character (PP, '*')
512 #define pp_slash(PP) pp_character (PP, '/')
513 #define pp_modulo(PP) pp_character (PP, '%')
514 #define pp_exclamation(PP) pp_character (PP, '!')
515 #define pp_complement(PP) pp_character (PP, '~')
516 #define pp_quote(PP) pp_character (PP, '\'')
517 #define pp_backquote(PP) pp_character (PP, '`')
518 #define pp_doublequote(PP) pp_character (PP, '"')
519 #define pp_underscore(PP) pp_character (PP, '_')
520 #define pp_maybe_newline_and_indent(PP, N) \
521 if (pp_needs_newline (PP)) pp_newline_and_indent (PP, N)
522 #define pp_scalar(PP, FORMAT, SCALAR) \
523 do \
525 sprintf (pp_buffer (PP)->m_digit_buffer, FORMAT, SCALAR); \
526 pp_string (PP, pp_buffer (PP)->m_digit_buffer); \
528 while (0)
529 #define pp_decimal_int(PP, I) pp_scalar (PP, "%d", I)
530 #define pp_unsigned_wide_integer(PP, I) \
531 pp_scalar (PP, HOST_WIDE_INT_PRINT_UNSIGNED, (unsigned HOST_WIDE_INT) I)
532 #define pp_vrange(PP, R) \
533 do \
535 vrange_printer vrange_pp (PP); \
536 (R)->accept (vrange_pp); \
538 while (0)
539 #define pp_double(PP, F) pp_scalar (PP, "%f", F)
540 #define pp_pointer(PP, P) pp_scalar (PP, "%p", P)
542 #define pp_identifier(PP, ID) pp_string (PP, (pp_translate_identifiers (PP) \
543 ? identifier_to_locale (ID) \
544 : (ID)))
547 extern void pp_set_line_maximum_length (pretty_printer *, int);
548 inline void pp_set_prefix (pretty_printer *pp, char *prefix)
550 pp->set_prefix (prefix);
552 extern void pp_clear_output_area (pretty_printer *);
553 extern const char *pp_formatted_text (pretty_printer *);
554 extern const char *pp_last_position_in_text (const pretty_printer *);
555 inline void pp_emit_prefix (pretty_printer *pp)
557 pp->emit_prefix ();
559 extern void pp_append_text (pretty_printer *, const char *, const char *);
560 extern void pp_newline_and_flush (pretty_printer *);
561 extern void pp_newline_and_indent (pretty_printer *, int);
562 extern void pp_separate_with (pretty_printer *, char);
564 /* If we haven't already defined a front-end-specific diagnostics
565 style, use the generic one. */
566 #ifdef GCC_DIAG_STYLE
567 #define GCC_PPDIAG_STYLE GCC_DIAG_STYLE
568 #else
569 #define GCC_PPDIAG_STYLE __gcc_diag__
570 #endif
572 /* This header may be included before diagnostics-core.h, hence the duplicate
573 definitions to allow for GCC-specific formats. */
574 #if GCC_VERSION >= 3005
575 #define ATTRIBUTE_GCC_PPDIAG(m, n) __attribute__ ((__format__ (GCC_PPDIAG_STYLE, m ,n))) ATTRIBUTE_NONNULL(m)
576 #else
577 #define ATTRIBUTE_GCC_PPDIAG(m, n) ATTRIBUTE_NONNULL(m)
578 #endif
579 extern void pp_printf (pretty_printer *, const char *, ...)
580 ATTRIBUTE_GCC_PPDIAG(2,3);
582 extern void pp_printf_n (pretty_printer *, unsigned HOST_WIDE_INT n,
583 const char *, const char *, ...)
584 ATTRIBUTE_GCC_PPDIAG(3,5)
585 ATTRIBUTE_GCC_PPDIAG(4,5);
587 extern void pp_verbatim (pretty_printer *, const char *, ...)
588 ATTRIBUTE_GCC_PPDIAG(2,3);
589 extern void pp_flush (pretty_printer *);
590 extern void pp_really_flush (pretty_printer *);
591 inline void pp_format (pretty_printer *pp, text_info *text)
593 gcc_assert (text);
594 pp->format (*text);
596 extern void pp_output_formatted_text (pretty_printer *,
597 const urlifier * = nullptr);
598 extern void pp_format_verbatim (pretty_printer *, text_info *);
600 extern void pp_indent (pretty_printer *);
601 extern void pp_newline (pretty_printer *);
602 extern void pp_character (pretty_printer *, int);
603 extern void pp_string (pretty_printer *, const char *);
604 extern void pp_string_n (pretty_printer *, const char *, size_t);
605 extern void pp_unicode_character (pretty_printer *, unsigned);
607 extern void pp_write_text_to_stream (pretty_printer *);
608 extern void pp_write_text_as_dot_label_to_stream (pretty_printer *, bool);
609 extern void pp_write_text_as_html_like_dot_to_stream (pretty_printer *pp);
611 inline void pp_maybe_space (pretty_printer *pp)
613 pp->maybe_space ();
616 extern void pp_begin_quote (pretty_printer *, bool);
617 extern void pp_end_quote (pretty_printer *, bool);
619 inline void
620 pp_begin_url (pretty_printer *pp, const char *url)
622 pp->begin_url (url);
625 inline void
626 pp_end_url (pretty_printer *pp)
628 pp->end_url ();
631 /* Switch into verbatim mode and return the old mode. */
632 inline pp_wrapping_mode_t
633 pp_set_verbatim_wrapping (pretty_printer *pp)
635 return pp->set_verbatim_wrapping ();
638 extern const char *identifier_to_locale (const char *);
639 extern void *(*identifier_to_locale_alloc) (size_t);
640 extern void (*identifier_to_locale_free) (void *);
642 /* Print I to PP in decimal. */
644 inline void
645 pp_wide_integer (pretty_printer *pp, HOST_WIDE_INT i)
647 pp_scalar (pp, HOST_WIDE_INT_PRINT_DEC, i);
650 inline void
651 pp_wide_int (pretty_printer *pp, const wide_int_ref &w, signop sgn)
653 unsigned int len;
654 print_dec_buf_size (w, sgn, &len);
655 if (UNLIKELY (len > sizeof (pp_buffer (pp)->m_digit_buffer)))
656 pp_wide_int_large (pp, w, sgn);
657 else
659 print_dec (w, pp_buffer (pp)->m_digit_buffer, sgn);
660 pp_string (pp, pp_buffer (pp)->m_digit_buffer);
664 template<unsigned int N, typename T>
665 void pp_wide_integer (pretty_printer *pp, const poly_int<N, T> &);
667 #endif /* GCC_PRETTY_PRINT_H */