pretty-print.h 14 KB

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