diff options
author | David Malcolm <dmalcolm@redhat.com> | 2018-11-08 14:55:54 +0000 |
---|---|---|
committer | David Malcolm <dmalcolm@gcc.gnu.org> | 2018-11-08 14:55:54 +0000 |
commit | 212755ff9137bcd3975e02f4936c96bdb7eaf709 (patch) | |
tree | 3c5fb53164804c22395d16b8ad4db4ae78afe627 /gcc/cgraph.c | |
parent | 1c8badf66bec4e0ff73ae24bf4f8cabbef8c137a (diff) | |
download | gcc-212755ff9137bcd3975e02f4936c96bdb7eaf709.tar.gz |
cgraph: add selftest::symbol_table_test
This patch adds a selftest fixture for overriding the "symtab" global,
so that selftests involving symtab nodes can be isolated from each
other: each selftest can have its own symbol_table instance.
In particular, this ensures that nodes can have a predictable "order"
and thus predictable dump names within selftests.
gcc/ChangeLog:
* cgraph.c: Include "selftest.h".
(saved_symtab): New variable.
(selftest::symbol_table_test::symbol_table_test): New ctor.
(selftest::symbol_table_test::~symbol_table_test): New dtor.
(selftest::test_symbol_table_test): New test.
(selftest::cgraph_c_tests): New.
* cgraph.h (saved_symtab): New decl.
(selftest::symbol_table_test): New class.
* selftest-run-tests.c (selftest::run_tests): Call
selftest::cgraph_c_tests.
* selftest.h (selftest::cgraph_c_tests): New decl.
From-SVN: r265915
Diffstat (limited to 'gcc/cgraph.c')
-rw-r--r-- | gcc/cgraph.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/gcc/cgraph.c b/gcc/cgraph.c index b432f7e6500..b3dd4296ea0 100644 --- a/gcc/cgraph.c +++ b/gcc/cgraph.c @@ -62,6 +62,7 @@ along with GCC; see the file COPYING3. If not see #include "gimplify.h" #include "stringpool.h" #include "attribs.h" +#include "selftest.h" /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */ #include "tree-pass.h" @@ -3765,4 +3766,70 @@ cgraph_edge::sreal_frequency () : caller->count); } +/* A stashed copy of "symtab" for use by selftest::symbol_table_test. + This needs to be a global so that it can be a GC root, and thus + prevent the stashed copy from being garbage-collected if the GC runs + during a symbol_table_test. */ + +symbol_table *saved_symtab; + +#if CHECKING_P + +namespace selftest { + +/* class selftest::symbol_table_test. */ + +/* Constructor. Store the old value of symtab, and create a new one. */ + +symbol_table_test::symbol_table_test () +{ + gcc_assert (saved_symtab == NULL); + saved_symtab = symtab; + symtab = new (ggc_cleared_alloc <symbol_table> ()) symbol_table (); +} + +/* Destructor. Restore the old value of symtab. */ + +symbol_table_test::~symbol_table_test () +{ + gcc_assert (saved_symtab != NULL); + symtab = saved_symtab; + saved_symtab = NULL; +} + +/* Verify that symbol_table_test works. */ + +static void +test_symbol_table_test () +{ + /* Simulate running two selftests involving symbol tables. */ + for (int i = 0; i < 2; i++) + { + symbol_table_test stt; + tree test_decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, + get_identifier ("test_decl"), + build_function_type_list (void_type_node, + NULL_TREE)); + cgraph_node *node = cgraph_node::get_create (test_decl); + gcc_assert (node); + + /* Verify that the node has order 0 on both iterations, + and thus that nodes have predictable dump names in selftests. */ + ASSERT_EQ (node->order, 0); + ASSERT_STREQ (node->dump_name (), "test_decl/0"); + } +} + +/* Run all of the selftests within this file. */ + +void +cgraph_c_tests () +{ + test_symbol_table_test (); +} + +} // namespace selftest + +#endif /* CHECKING_P */ + #include "gt-cgraph.h" |