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