pretty-print.h 15 KB

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