您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

285 行
8.6KB

  1. /* Support code for handling the various dump_* calls in dumpfile.h
  2. Copyright (C) 2018-2020 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. class stashed_item
  127. {
  128. public:
  129. stashed_item (const char **buffer_ptr_, optinfo_item *item_)
  130. : buffer_ptr (buffer_ptr_), item (item_) {}
  131. const char **buffer_ptr;
  132. optinfo_item *item;
  133. };
  134. static bool format_decoder_cb (pretty_printer *pp, text_info *text,
  135. const char *spec, int /*precision*/,
  136. bool /*wide*/, bool /*set_locus*/,
  137. bool /*verbose*/, bool */*quoted*/,
  138. const char **buffer_ptr);
  139. bool decode_format (text_info *text, const char *spec,
  140. const char **buffer_ptr);
  141. void stash_item (const char **buffer_ptr, optinfo_item *item);
  142. void emit_any_pending_textual_chunks (optinfo *dest);
  143. void emit_item (optinfo_item *item, optinfo *dest);
  144. dump_context *m_context;
  145. dump_flags_t m_dump_kind;
  146. auto_vec<stashed_item> m_stashed_items;
  147. };
  148. #if CHECKING_P
  149. namespace selftest {
  150. /* An RAII-style class for use in selftests for temporarily using a different
  151. dump_context. */
  152. class temp_dump_context
  153. {
  154. public:
  155. temp_dump_context (bool forcibly_enable_optinfo,
  156. bool forcibly_enable_dumping,
  157. dump_flags_t test_pp_flags);
  158. ~temp_dump_context ();
  159. /* Support for selftests. */
  160. optinfo *get_pending_optinfo () const { return m_context.m_pending; }
  161. const char *get_dumped_text ();
  162. private:
  163. pretty_printer m_pp;
  164. dump_context m_context;
  165. dump_context *m_saved;
  166. };
  167. /* Implementation detail of ASSERT_DUMPED_TEXT_EQ. */
  168. extern void verify_dumped_text (const location &loc,
  169. temp_dump_context *context,
  170. const char *expected_text);
  171. /* Verify that the text dumped so far in CONTEXT equals
  172. EXPECTED_TEXT.
  173. As a side-effect, the internal buffer is 0-terminated. */
  174. #define ASSERT_DUMPED_TEXT_EQ(CONTEXT, EXPECTED_TEXT) \
  175. SELFTEST_BEGIN_STMT \
  176. verify_dumped_text (SELFTEST_LOCATION, &(CONTEXT), (EXPECTED_TEXT)); \
  177. SELFTEST_END_STMT
  178. /* Verify that ITEM has the expected values. */
  179. void
  180. verify_item (const location &loc,
  181. const optinfo_item *item,
  182. enum optinfo_item_kind expected_kind,
  183. location_t expected_location,
  184. const char *expected_text);
  185. /* Verify that ITEM is a text item, with EXPECTED_TEXT. */
  186. #define ASSERT_IS_TEXT(ITEM, EXPECTED_TEXT) \
  187. SELFTEST_BEGIN_STMT \
  188. verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TEXT, \
  189. UNKNOWN_LOCATION, (EXPECTED_TEXT)); \
  190. SELFTEST_END_STMT
  191. /* Verify that ITEM is a tree item, with the expected values. */
  192. #define ASSERT_IS_TREE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
  193. SELFTEST_BEGIN_STMT \
  194. verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_TREE, \
  195. (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
  196. SELFTEST_END_STMT
  197. /* Verify that ITEM is a gimple item, with the expected values. */
  198. #define ASSERT_IS_GIMPLE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
  199. SELFTEST_BEGIN_STMT \
  200. verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_GIMPLE, \
  201. (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
  202. SELFTEST_END_STMT
  203. /* Verify that ITEM is a symtab node, with the expected values. */
  204. #define ASSERT_IS_SYMTAB_NODE(ITEM, EXPECTED_LOCATION, EXPECTED_TEXT) \
  205. SELFTEST_BEGIN_STMT \
  206. verify_item (SELFTEST_LOCATION, (ITEM), OPTINFO_ITEM_KIND_SYMTAB_NODE, \
  207. (EXPECTED_LOCATION), (EXPECTED_TEXT)); \
  208. SELFTEST_END_STMT
  209. } // namespace selftest
  210. #endif /* CHECKING_P */
  211. #endif /* GCC_DUMP_CONTEXT_H */