]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
cgraph: add selftest::symbol_table_test
authorDavid Malcolm <dmalcolm@redhat.com>
Thu, 8 Nov 2018 14:55:54 +0000 (14:55 +0000)
committerDavid Malcolm <dmalcolm@gcc.gnu.org>
Thu, 8 Nov 2018 14:55:54 +0000 (14:55 +0000)
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

gcc/ChangeLog
gcc/cgraph.c
gcc/cgraph.h
gcc/selftest-run-tests.c
gcc/selftest.h

index 4af240a16a6fedb4c7afd5581b0284c217fa75e1..46efe5d1ef03b5a3b83ab1a3dbcfaadaf378a018 100644 (file)
@@ -1,3 +1,17 @@
+2018-11-08  David Malcolm  <dmalcolm@redhat.com>
+
+       * 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.
+
 2018-11-08  Richard Biener  <rguenther@suse.de>
 
        * tree-data-ref.h (lambda_int): New typedef.
index b432f7e650076aa4f3e78cd13186ded311ed15e6..b3dd4296ea0b54c4836c13e634cda2eb8faa2e90 100644 (file)
@@ -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"
index c13d79850fa86e45b40e85b5b32d0854cfbbb2f1..021552251c4fe0de87c674a2f50beba5dc79f672 100644 (file)
@@ -3353,4 +3353,27 @@ xstrdup_for_dump (const char *transient_str)
   return ggc_strdup (transient_str);
 }
 
+extern GTY(()) symbol_table *saved_symtab;
+
+#if CHECKING_P
+
+namespace selftest {
+
+/* An RAII-style class for use in selftests for temporarily using a different
+   symbol_table, so that such tests can be isolated from each other.  */
+
+class symbol_table_test
+{
+ public:
+  /* Constructor.  Override "symtab".  */
+  symbol_table_test ();
+
+  /* Destructor.  Restore the saved_symtab.  */
+  ~symbol_table_test ();
+};
+
+} // namespace selftest
+
+#endif /* CHECKING_P */
+
 #endif  /* GCC_CGRAPH_H  */
index 562ada74a5f50d6ec6b11904017957a0281cffac..6d65d2491d352ec01643e0791faecc4535f5d473 100644 (file)
@@ -73,6 +73,7 @@ selftest::run_tests ()
   unique_ptr_tests_cc_tests ();
   opt_proposer_c_tests ();
   json_cc_tests ();
+  cgraph_c_tests ();
   optinfo_emit_json_cc_tests ();
   opt_problem_cc_tests ();
 
index 8da7c4ae1c0f3f897ca2c97951957e9ac39744e9..4e4c755f9b092c073acc4a17c9f186759a1cd6ef 100644 (file)
@@ -215,6 +215,7 @@ class test_runner
    alphabetical order.  */
 extern void attribute_c_tests ();
 extern void bitmap_c_tests ();
+extern void cgraph_c_tests ();
 extern void diagnostic_c_tests ();
 extern void diagnostic_show_locus_c_tests ();
 extern void dumpfile_c_tests ();