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