dump-context.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* Support code for handling the various dump_* calls in dumpfile.h
  2. Copyright (C) 2018-2019 Free Software Foundation, Inc.
  3. Contributed by David Malcolm <dmalcolm@redhat.com>.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License 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_DUMP_CONTEXT_H
  17. #define GCC_DUMP_CONTEXT_H 1
  18. #include "dumpfile.h"
  19. #include "pretty-print.h"
  20. #include "selftest.h"
  21. #include "optinfo.h"
  22. class optrecord_json_writer;
  23. namespace selftest { class temp_dump_context; }
  24. /* A class for handling the various dump_* calls.
  25. In particular, this class has responsibility for consolidating
  26. the "dump_*" calls into optinfo instances (delimited by "dump_*_loc"
  27. calls), and emitting them.
  28. Putting this in a class (rather than as global state) allows
  29. for selftesting of this code. */
  30. class dump_context
  31. {
  32. friend class selftest::temp_dump_context;
  33. public:
  34. static dump_context &get () { return *s_current; }
  35. ~dump_context ();
  36. void refresh_dumps_are_enabled ();
  37. void dump_loc (const dump_metadata_t &metadata,
  38. const dump_user_location_t &loc);
  39. void dump_loc_immediate (dump_flags_t dump_kind,
  40. const dump_user_location_t &loc);
  41. void dump_gimple_stmt (const dump_metadata_t &metadata,
  42. dump_flags_t extra_dump_flags,
  43. gimple *gs, int spc);
  44. void dump_gimple_stmt_loc (const dump_metadata_t &metadata,
  45. const dump_user_location_t &loc,
  46. dump_flags_t extra_dump_flags,
  47. gimple *gs, int spc);
  48. void dump_gimple_expr (const dump_metadata_t &metadata,
  49. dump_flags_t extra_dump_flags,
  50. gimple *gs, int spc);
  51. void dump_gimple_expr_loc (const dump_metadata_t &metadata,
  52. const dump_user_location_t &loc,
  53. dump_flags_t extra_dump_flags,
  54. gimple *gs,
  55. int spc);
  56. void dump_generic_expr (const dump_metadata_t &metadata,
  57. dump_flags_t extra_dump_flags,
  58. tree t);
  59. void dump_generic_expr_loc (const dump_metadata_t &metadata,
  60. const dump_user_location_t &loc,
  61. dump_flags_t extra_dump_flags,
  62. tree t);
  63. void dump_printf_va (const dump_metadata_t &metadata, const char *format,
  64. va_list *ap) ATTRIBUTE_GCC_DUMP_PRINTF (3, 0);
  65. void dump_printf_loc_va (const dump_metadata_t &metadata,
  66. const dump_user_location_t &loc,
  67. const char *format, va_list *ap)
  68. ATTRIBUTE_GCC_DUMP_PRINTF (4, 0);
  69. template<unsigned int N, typename C>
  70. void dump_dec (const dump_metadata_t &metadata, const poly_int<N, C> &value);
  71. void dump_symtab_node (const dump_metadata_t &metadata, symtab_node *node);
  72. /* Managing nested scopes. */
  73. unsigned int get_scope_depth () const;
  74. void begin_scope (const char *name,
  75. const dump_user_location_t &user_location,
  76. const dump_impl_location_t &impl_location);
  77. void end_scope ();
  78. /* Should optinfo instances be created?
  79. All creation of optinfos should be guarded by this predicate.
  80. Return true if any optinfo destinations are active. */
  81. bool optinfo_enabled_p () const;
  82. bool optimization_records_enabled_p () const
  83. {
  84. return m_json_writer != NULL;
  85. }
  86. void set_json_writer (optrecord_json_writer *writer);
  87. void finish_any_json_writer ();
  88. void end_any_optinfo ();
  89. void emit_optinfo (const optinfo *info);
  90. void emit_item (optinfo_item *item, dump_flags_t dump_kind);
  91. bool apply_dump_filter_p (dump_flags_t dump_kind, dump_flags_t filter) const;
  92. private:
  93. optinfo &ensure_pending_optinfo (const dump_metadata_t &metadata);
  94. optinfo &begin_next_optinfo (const dump_metadata_t &metadata,
  95. const dump_user_location_t &loc);
  96. /* The current nesting depth of dump scopes, for showing nesting
  97. via indentation). */
  98. unsigned int m_scope_depth;
  99. /* The optinfo currently being accumulated since the last dump_*_loc call,
  100. if any. */
  101. optinfo *m_pending;
  102. /* If -fsave-optimization-record is enabled, the heap-allocated JSON writer
  103. instance, otherwise NULL. */
  104. optrecord_json_writer *m_json_writer;
  105. /* For use in selftests: if non-NULL, then items are to be printed
  106. to this, using the given flags. */
  107. pretty_printer *m_test_pp;
  108. dump_flags_t m_test_pp_flags;
  109. /* The currently active dump_context, for use by the dump_* API calls. */
  110. static dump_context *s_current;
  111. /* The default active context. */
  112. static dump_context s_default;
  113. };
  114. /* A subclass of pretty_printer for implementing dump_context::dump_printf_va.
  115. In particular, the formatted chunks are captured as optinfo_item instances,
  116. thus retaining metadata about the entities being dumped (e.g. source
  117. locations), rather than just as plain text. */
  118. class dump_pretty_printer : public pretty_printer
  119. {
  120. public:
  121. dump_pretty_printer (dump_context *context, dump_flags_t dump_kind);
  122. void emit_items (optinfo *dest);
  123. private:
  124. /* Information on an optinfo_item that was generated during phase 2 of
  125. formatting. */
  126. struct stashed_item
  127. {
  128. stashed_item (const char **buffer_ptr_, optinfo_item *item_)
  129. : buffer_ptr (buffer_ptr_), item (item_) {}
  130. const char **buffer_ptr;
  131. optinfo_item *item;
  132. };
  133. static bool format_decoder_cb (pretty_printer *pp, text_info *text,
  134. const char *spec, int /*precision*/,
  135. bool /*wide*/, bool /*set_locus*/,
  136. bool /*verbose*/, bool */*quoted*/,
  137. const char **buffer_ptr);
  138. bool decode_format (text_info *text, const char *spec,
  139. const char **buffer_ptr);
  140. void stash_item (const char **buffer_ptr, optinfo_item *item);
  141. void emit_any_pending_textual_chunks (optinfo *dest);
  142. void emit_item (optinfo_item *item, optinfo *dest);
  143. dump_context *m_context;
  144. dump_flags_t m_dump_kind;
  145. auto_vec<stashed_item> m_stashed_items;
  146. };
  147. #if CHECKING_P
  148. namespace selftest {
  149. /* An RAII-style class for use in selftests for temporarily using a different
  150. dump_context. */
  151. class temp_dump_context
  152. {
  153. public:
  154. temp_dump_context (bool forcibly_enable_optinfo,
  155. bool forcibly_enable_dumping,
  156. dump_flags_t test_pp_flags);
  157. ~temp_dump_context ();
  158. /* Support for selftests. */
  159. optinfo *get_pending_optinfo () const { return m_context.m_pending; }
  160. const char *get_dumped_text ();
  161. private:
  162. pretty_printer m_pp;
  163. dump_context m_context;
  164. dump_context *m_saved;
  165. };
  166. /* Implementation detail of ASSERT_DUMPED_TEXT_EQ. */
  167. extern void verify_dumped_text (const location &loc,
  168. temp_dump_context *context,
  169. const char *expected_text);
  170. /* Verify that the text dumped so far in CONTEXT equals
  171. EXPECTED_TEXT.
  172. As a side-effect, the internal buffer is 0-terminated. */
  173. #define ASSERT_DUMPED_TEXT_EQ(CONTEXT, EXPECTED_TEXT) \
  174. SELFTEST_BEGIN_STMT \
  175. verify_dumped_text (SELFTEST_LOCATION, &(CONTEXT), (EXPECTED_TEXT)); \
  176. SELFTEST_END_STMT
  177. /* Verify that ITEM has the expected values. */
  178. void
  179. verify_item (const location &loc,
  180. const optinfo_item *item,
  181. enum optinfo_item_kind expected_kind,
  182. location_t expected_location,
  183. const char *expected_text);
  184. /* Verify that ITEM is a text item, with EXPECTED_TEXT. */
  185. #define ASSERT_IS_TEXT(ITEM, EXPECTED_TEXT) \
  186. SELFTEST_BEGIN_STMT \
  187. verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TEXT, \
  188. UNKNOWN_LOCATION, (EXPECTED_TEXT)); \
  189. SELFTEST_END_STMT
  190. /* Verify that ITEM is a tree item, with the expected values. */
  191. #define ASSERT_IS_TREE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
  192. SELFTEST_BEGIN_STMT \
  193. verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TREE, \
  194. (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
  195. SELFTEST_END_STMT
  196. /* Verify that ITEM is a gimple item, with the expected values. */
  197. #define ASSERT_IS_GIMPLE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
  198. SELFTEST_BEGIN_STMT \
  199. verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_GIMPLE, \
  200. (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
  201. SELFTEST_END_STMT
  202. /* Verify that ITEM is a symtab node, with the expected values. */
  203. #define ASSERT_IS_SYMTAB_NODE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
  204. SELFTEST_BEGIN_STMT \
  205. verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_SYMTAB_NODE, \
  206. (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
  207. SELFTEST_END_STMT
  208. } // namespace selftest
  209. #endif /* CHECKING_P */
  210. #endif /* GCC_DUMP_CONTEXT_H */