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