You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

494 lines
15KB

  1. /* A self-testing framework, for use by -fself-test.
  2. Copyright (C) 2015-2020 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef GCC_SELFTEST_H
  16. #define GCC_SELFTEST_H
  17. /* The selftest code should entirely disappear in a production
  18. configuration, hence we guard all of it with #if CHECKING_P. */
  19. #if CHECKING_P
  20. namespace selftest {
  21. /* A struct describing the source-location of a selftest, to make it
  22. easier to track down failing tests. */
  23. class location
  24. {
  25. public:
  26. location (const char *file, int line, const char *function)
  27. : m_file (file), m_line (line), m_function (function) {}
  28. const char *m_file;
  29. int m_line;
  30. const char *m_function;
  31. };
  32. /* A macro for use in selftests and by the ASSERT_ macros below,
  33. constructing a selftest::location for the current source location. */
  34. #define SELFTEST_LOCATION \
  35. (::selftest::location (__FILE__, __LINE__, __FUNCTION__))
  36. /* The entrypoint for running all tests. */
  37. extern void run_tests ();
  38. /* Record the successful outcome of some aspect of the test. */
  39. extern void pass (const location &loc, const char *msg);
  40. /* Report the failed outcome of some aspect of the test and abort. */
  41. extern void fail (const location &loc, const char *msg)
  42. ATTRIBUTE_NORETURN;
  43. /* As "fail", but using printf-style formatted output. */
  44. extern void fail_formatted (const location &loc, const char *fmt, ...)
  45. ATTRIBUTE_PRINTF_2 ATTRIBUTE_NORETURN;
  46. /* Implementation detail of ASSERT_STREQ. */
  47. extern void assert_streq (const location &loc,
  48. const char *desc_val1, const char *desc_val2,
  49. const char *val1, const char *val2);
  50. /* Implementation detail of ASSERT_STR_CONTAINS. */
  51. extern void assert_str_contains (const location &loc,
  52. const char *desc_haystack,
  53. const char *desc_needle,
  54. const char *val_haystack,
  55. const char *val_needle);
  56. /* Implementation detail of ASSERT_STR_STARTSWITH. */
  57. extern void assert_str_startswith (const location &loc,
  58. const char *desc_str,
  59. const char *desc_prefix,
  60. const char *val_str,
  61. const char *val_prefix);
  62. /* A named temporary file for use in selftests.
  63. Usable for writing out files, and as the base class for
  64. temp_source_file.
  65. The file is unlinked in the destructor. */
  66. class named_temp_file
  67. {
  68. public:
  69. named_temp_file (const char *suffix);
  70. ~named_temp_file ();
  71. const char *get_filename () const { return m_filename; }
  72. private:
  73. char *m_filename;
  74. };
  75. /* A class for writing out a temporary sourcefile for use in selftests
  76. of input handling. */
  77. class temp_source_file : public named_temp_file
  78. {
  79. public:
  80. temp_source_file (const location &loc, const char *suffix,
  81. const char *content);
  82. };
  83. /* RAII-style class for avoiding introducing locale-specific differences
  84. in strings containing localized quote marks, by temporarily overriding
  85. the "open_quote" and "close_quote" globals to something hardcoded.
  86. Specifically, the C locale's values are used:
  87. - open_quote becomes "`"
  88. - close_quote becomes "'"
  89. for the lifetime of the object. */
  90. class auto_fix_quotes
  91. {
  92. public:
  93. auto_fix_quotes ();
  94. ~auto_fix_quotes ();
  95. private:
  96. const char *m_saved_open_quote;
  97. const char *m_saved_close_quote;
  98. };
  99. /* Various selftests involving location-handling require constructing a
  100. line table and one or more line maps within it.
  101. For maximum test coverage we want to run these tests with a variety
  102. of situations:
  103. - line_table->default_range_bits: some frontends use a non-zero value
  104. and others use zero
  105. - the fallback modes within line-map.c: there are various threshold
  106. values for location_t beyond line-map.c changes
  107. behavior (disabling of the range-packing optimization, disabling
  108. of column-tracking). We can exercise these by starting the line_table
  109. at interesting values at or near these thresholds.
  110. The following struct describes a particular case within our test
  111. matrix. */
  112. class line_table_case;
  113. /* A class for overriding the global "line_table" within a selftest,
  114. restoring its value afterwards. At most one instance of this
  115. class can exist at once, due to the need to keep the old value
  116. of line_table as a GC root. */
  117. class line_table_test
  118. {
  119. public:
  120. /* Default constructor. Override "line_table", using sane defaults
  121. for the temporary line_table. */
  122. line_table_test ();
  123. /* Constructor. Override "line_table", using the case described by C. */
  124. line_table_test (const line_table_case &c);
  125. /* Destructor. Restore the saved line_table. */
  126. ~line_table_test ();
  127. };
  128. /* Helper function for selftests that need a function decl. */
  129. extern tree make_fndecl (tree return_type,
  130. const char *name,
  131. vec <tree> &param_types,
  132. bool is_variadic = false);
  133. /* Run TESTCASE multiple times, once for each case in our test matrix. */
  134. extern void
  135. for_each_line_table_case (void (*testcase) (const line_table_case &));
  136. /* Read the contents of PATH into memory, returning a 0-terminated buffer
  137. that must be freed by the caller.
  138. Fail (and abort) if there are any problems, with LOC as the reported
  139. location of the failure. */
  140. extern char *read_file (const location &loc, const char *path);
  141. /* A helper function for writing tests that interact with the
  142. garbage collector. */
  143. extern void forcibly_ggc_collect ();
  144. /* Convert a path relative to SRCDIR/gcc/testsuite/selftests
  145. to a real path (either absolute, or relative to pwd).
  146. The result should be freed by the caller. */
  147. extern char *locate_file (const char *path);
  148. /* The path of SRCDIR/testsuite/selftests. */
  149. extern const char *path_to_selftest_files;
  150. /* selftest::test_runner is an implementation detail of selftest::run_tests,
  151. exposed here to allow plugins to run their own suites of tests. */
  152. class test_runner
  153. {
  154. public:
  155. test_runner (const char *name);
  156. ~test_runner ();
  157. private:
  158. const char *m_name;
  159. long m_start_time;
  160. };
  161. /* Declarations for specific families of tests (by source file), in
  162. alphabetical order. */
  163. extern void attribute_c_tests ();
  164. extern void bitmap_c_tests ();
  165. extern void cgraph_c_tests ();
  166. extern void convert_c_tests ();
  167. extern void diagnostic_c_tests ();
  168. extern void diagnostic_format_json_cc_tests ();
  169. extern void diagnostic_show_locus_c_tests ();
  170. extern void digraph_cc_tests ();
  171. extern void dumpfile_c_tests ();
  172. extern void edit_context_c_tests ();
  173. extern void et_forest_c_tests ();
  174. extern void fibonacci_heap_c_tests ();
  175. extern void fold_const_c_tests ();
  176. extern void function_tests_c_tests ();
  177. extern void ggc_tests_c_tests ();
  178. extern void gimple_c_tests ();
  179. extern void hash_map_tests_c_tests ();
  180. extern void hash_set_tests_c_tests ();
  181. extern void input_c_tests ();
  182. extern void json_cc_tests ();
  183. extern void opt_problem_cc_tests ();
  184. extern void optinfo_emit_json_cc_tests ();
  185. extern void opts_c_tests ();
  186. extern void ordered_hash_map_tests_cc_tests ();
  187. extern void predict_c_tests ();
  188. extern void pretty_print_c_tests ();
  189. extern void range_tests ();
  190. extern void read_rtl_function_c_tests ();
  191. extern void rtl_tests_c_tests ();
  192. extern void sbitmap_c_tests ();
  193. extern void selftest_c_tests ();
  194. extern void simplify_rtx_c_tests ();
  195. extern void spellcheck_c_tests ();
  196. extern void spellcheck_tree_c_tests ();
  197. extern void sreal_c_tests ();
  198. extern void store_merging_c_tests ();
  199. extern void tree_c_tests ();
  200. extern void tree_cfg_c_tests ();
  201. extern void tree_diagnostic_path_cc_tests ();
  202. extern void tristate_cc_tests ();
  203. extern void typed_splay_tree_c_tests ();
  204. extern void unique_ptr_tests_cc_tests ();
  205. extern void vec_c_tests ();
  206. extern void vec_perm_indices_c_tests ();
  207. extern void wide_int_cc_tests ();
  208. extern void opt_proposer_c_tests ();
  209. extern void dbgcnt_c_tests ();
  210. extern int num_passes;
  211. } /* end of namespace selftest. */
  212. /* Macros for writing tests. */
  213. /* Evaluate EXPR and coerce to bool, calling
  214. ::selftest::pass if it is true,
  215. ::selftest::fail if it false. */
  216. #define ASSERT_TRUE(EXPR) \
  217. ASSERT_TRUE_AT (SELFTEST_LOCATION, (EXPR))
  218. /* Like ASSERT_TRUE, but treat LOC as the effective location of the
  219. selftest. */
  220. #define ASSERT_TRUE_AT(LOC, EXPR) \
  221. SELFTEST_BEGIN_STMT \
  222. const char *desc_ = "ASSERT_TRUE (" #EXPR ")"; \
  223. bool actual_ = (EXPR); \
  224. if (actual_) \
  225. ::selftest::pass ((LOC), desc_); \
  226. else \
  227. ::selftest::fail ((LOC), desc_); \
  228. SELFTEST_END_STMT
  229. /* Evaluate EXPR and coerce to bool, calling
  230. ::selftest::pass if it is false,
  231. ::selftest::fail if it true. */
  232. #define ASSERT_FALSE(EXPR) \
  233. ASSERT_FALSE_AT (SELFTEST_LOCATION, (EXPR))
  234. /* Like ASSERT_FALSE, but treat LOC as the effective location of the
  235. selftest. */
  236. #define ASSERT_FALSE_AT(LOC, EXPR) \
  237. SELFTEST_BEGIN_STMT \
  238. const char *desc_ = "ASSERT_FALSE (" #EXPR ")"; \
  239. bool actual_ = (EXPR); \
  240. if (actual_) \
  241. ::selftest::fail ((LOC), desc_); \
  242. else \
  243. ::selftest::pass ((LOC), desc_); \
  244. SELFTEST_END_STMT
  245. /* Evaluate VAL1 and VAL2 and compare them with ==, calling
  246. ::selftest::pass if they are equal,
  247. ::selftest::fail if they are non-equal. */
  248. #define ASSERT_EQ(VAL1, VAL2) \
  249. ASSERT_EQ_AT ((SELFTEST_LOCATION), (VAL1), (VAL2))
  250. /* Like ASSERT_EQ, but treat LOC as the effective location of the
  251. selftest. */
  252. #define ASSERT_EQ_AT(LOC, VAL1, VAL2) \
  253. SELFTEST_BEGIN_STMT \
  254. const char *desc_ = "ASSERT_EQ (" #VAL1 ", " #VAL2 ")"; \
  255. if ((VAL1) == (VAL2)) \
  256. ::selftest::pass ((LOC), desc_); \
  257. else \
  258. ::selftest::fail ((LOC), desc_); \
  259. SELFTEST_END_STMT
  260. /* Evaluate VAL1 and VAL2 and compare them with known_eq, calling
  261. ::selftest::pass if they are always equal,
  262. ::selftest::fail if they might be non-equal. */
  263. #define ASSERT_KNOWN_EQ(VAL1, VAL2) \
  264. ASSERT_KNOWN_EQ_AT ((SELFTEST_LOCATION), (VAL1), (VAL2))
  265. /* Like ASSERT_KNOWN_EQ, but treat LOC as the effective location of the
  266. selftest. */
  267. #define ASSERT_KNOWN_EQ_AT(LOC, VAL1, VAL2) \
  268. SELFTEST_BEGIN_STMT \
  269. const char *desc = "ASSERT_KNOWN_EQ (" #VAL1 ", " #VAL2 ")"; \
  270. if (known_eq (VAL1, VAL2)) \
  271. ::selftest::pass ((LOC), desc); \
  272. else \
  273. ::selftest::fail ((LOC), desc); \
  274. SELFTEST_END_STMT
  275. /* Evaluate VAL1 and VAL2 and compare them with !=, calling
  276. ::selftest::pass if they are non-equal,
  277. ::selftest::fail if they are equal. */
  278. #define ASSERT_NE(VAL1, VAL2) \
  279. SELFTEST_BEGIN_STMT \
  280. const char *desc_ = "ASSERT_NE (" #VAL1 ", " #VAL2 ")"; \
  281. if ((VAL1) != (VAL2)) \
  282. ::selftest::pass (SELFTEST_LOCATION, desc_); \
  283. else \
  284. ::selftest::fail (SELFTEST_LOCATION, desc_); \
  285. SELFTEST_END_STMT
  286. /* Evaluate VAL1 and VAL2 and compare them with maybe_ne, calling
  287. ::selftest::pass if they might be non-equal,
  288. ::selftest::fail if they are known to be equal. */
  289. #define ASSERT_MAYBE_NE(VAL1, VAL2) \
  290. ASSERT_MAYBE_NE_AT ((SELFTEST_LOCATION), (VAL1), (VAL2))
  291. /* Like ASSERT_MAYBE_NE, but treat LOC as the effective location of the
  292. selftest. */
  293. #define ASSERT_MAYBE_NE_AT(LOC, VAL1, VAL2) \
  294. SELFTEST_BEGIN_STMT \
  295. const char *desc = "ASSERT_MAYBE_NE (" #VAL1 ", " #VAL2 ")"; \
  296. if (maybe_ne (VAL1, VAL2)) \
  297. ::selftest::pass ((LOC), desc); \
  298. else \
  299. ::selftest::fail ((LOC), desc); \
  300. SELFTEST_END_STMT
  301. /* Evaluate LHS and RHS and compare them with >, calling
  302. ::selftest::pass if LHS > RHS,
  303. ::selftest::fail otherwise. */
  304. #define ASSERT_GT(LHS, RHS) \
  305. ASSERT_GT_AT ((SELFTEST_LOCATION), (LHS), (RHS))
  306. /* Like ASSERT_GT, but treat LOC as the effective location of the
  307. selftest. */
  308. #define ASSERT_GT_AT(LOC, LHS, RHS) \
  309. SELFTEST_BEGIN_STMT \
  310. const char *desc_ = "ASSERT_GT (" #LHS ", " #RHS ")"; \
  311. if ((LHS) > (RHS)) \
  312. ::selftest::pass ((LOC), desc_); \
  313. else \
  314. ::selftest::fail ((LOC), desc_); \
  315. SELFTEST_END_STMT
  316. /* Evaluate LHS and RHS and compare them with <, calling
  317. ::selftest::pass if LHS < RHS,
  318. ::selftest::fail otherwise. */
  319. #define ASSERT_LT(LHS, RHS) \
  320. ASSERT_LT_AT ((SELFTEST_LOCATION), (LHS), (RHS))
  321. /* Like ASSERT_LT, but treat LOC as the effective location of the
  322. selftest. */
  323. #define ASSERT_LT_AT(LOC, LHS, RHS) \
  324. SELFTEST_BEGIN_STMT \
  325. const char *desc_ = "ASSERT_LT (" #LHS ", " #RHS ")"; \
  326. if ((LHS) < (RHS)) \
  327. ::selftest::pass ((LOC), desc_); \
  328. else \
  329. ::selftest::fail ((LOC), desc_); \
  330. SELFTEST_END_STMT
  331. /* Evaluate VAL1 and VAL2 and compare them with strcmp, calling
  332. ::selftest::pass if they are equal (and both are non-NULL),
  333. ::selftest::fail if they are non-equal, or are both NULL. */
  334. #define ASSERT_STREQ(VAL1, VAL2) \
  335. SELFTEST_BEGIN_STMT \
  336. ::selftest::assert_streq (SELFTEST_LOCATION, #VAL1, #VAL2, \
  337. (VAL1), (VAL2)); \
  338. SELFTEST_END_STMT
  339. /* Like ASSERT_STREQ, but treat LOC as the effective location of the
  340. selftest. */
  341. #define ASSERT_STREQ_AT(LOC, VAL1, VAL2) \
  342. SELFTEST_BEGIN_STMT \
  343. ::selftest::assert_streq ((LOC), #VAL1, #VAL2, \
  344. (VAL1), (VAL2)); \
  345. SELFTEST_END_STMT
  346. /* Evaluate HAYSTACK and NEEDLE and use strstr to determine if NEEDLE
  347. is within HAYSTACK.
  348. ::selftest::pass if NEEDLE is found.
  349. ::selftest::fail if it is not found. */
  350. #define ASSERT_STR_CONTAINS(HAYSTACK, NEEDLE) \
  351. SELFTEST_BEGIN_STMT \
  352. ::selftest::assert_str_contains (SELFTEST_LOCATION, #HAYSTACK, #NEEDLE, \
  353. (HAYSTACK), (NEEDLE)); \
  354. SELFTEST_END_STMT
  355. /* Like ASSERT_STR_CONTAINS, but treat LOC as the effective location of the
  356. selftest. */
  357. #define ASSERT_STR_CONTAINS_AT(LOC, HAYSTACK, NEEDLE) \
  358. SELFTEST_BEGIN_STMT \
  359. ::selftest::assert_str_contains (LOC, #HAYSTACK, #NEEDLE, \
  360. (HAYSTACK), (NEEDLE)); \
  361. SELFTEST_END_STMT
  362. /* Evaluate STR and PREFIX and determine if STR starts with PREFIX.
  363. ::selftest::pass if STR does start with PREFIX.
  364. ::selftest::fail if does not, or either is NULL. */
  365. #define ASSERT_STR_STARTSWITH(STR, PREFIX) \
  366. SELFTEST_BEGIN_STMT \
  367. ::selftest::assert_str_startswith (SELFTEST_LOCATION, #STR, #PREFIX, \
  368. (STR), (PREFIX)); \
  369. SELFTEST_END_STMT
  370. /* Evaluate PRED1 (VAL1), calling ::selftest::pass if it is true,
  371. ::selftest::fail if it is false. */
  372. #define ASSERT_PRED1(PRED1, VAL1) \
  373. SELFTEST_BEGIN_STMT \
  374. const char *desc_ = "ASSERT_PRED1 (" #PRED1 ", " #VAL1 ")"; \
  375. bool actual_ = (PRED1) (VAL1); \
  376. if (actual_) \
  377. ::selftest::pass (SELFTEST_LOCATION, desc_); \
  378. else \
  379. ::selftest::fail (SELFTEST_LOCATION, desc_); \
  380. SELFTEST_END_STMT
  381. #define SELFTEST_BEGIN_STMT do {
  382. #define SELFTEST_END_STMT } while (0)
  383. #endif /* #if CHECKING_P */
  384. #endif /* GCC_SELFTEST_H */