From fa47399200e08d0ffdfe42b6dc779c4cc506411a Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Mon, 18 Jul 2016 13:23:56 -0400
Subject: [PATCH 11/35] FIXME: rename temp_line_table to line_table_test and
 expose it in selftest.h

FIXME: fixes to FIXME: rename temp_line_table to line_table_test and expose it in selftest.h
---
 gcc/input.c    | 80 +++++++++++++++++++++++++++++++++++++---------------------
 gcc/input.h    |  1 +
 gcc/selftest.h | 42 ++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+), 29 deletions(-)

diff --git a/gcc/input.c b/gcc/input.c
index 1f6e183..dad640e 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -110,6 +110,13 @@ location_t input_location = UNKNOWN_LOCATION;
 
 struct line_maps *line_table;
 
+/* A stashed copy of "line_table" for use by selftest::line_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 line_table_test.  */
+
+struct line_maps *saved_line_table;
+
 /* FIXME.  */
 
 class sources_cache
@@ -1300,8 +1307,8 @@ assert_location_eq (const location &report_loc,
     ASSERT_EQ_AT (report_loc, exp_colnum, LOCATION_COLUMN (actual_loc));
 }
 
-/* Various selftests in this file involve constructing a line table
-   and one or more line maps within it.
+/* Various selftests involve constructing a line table and one or more
+   line maps within it.
 
    For maximum test coverage we want to run these tests with a variety
    of situations:
@@ -1327,29 +1334,35 @@ struct line_table_case
   int m_base_location;
 };
 
-/* A class for overriding the global "line_table" within a selftest,
-   restoring its value afterwards.  */
+/* Constructor.  Store the old value of line_table, and create a new
+   one, using sane defaults.  */
 
-class temp_line_table
+line_table_test::line_table_test ()
 {
- public:
-  temp_line_table (const line_table_case &);
-  ~temp_line_table ();
-
- private:
-  line_maps *m_old_line_table;
-};
+  gcc_assert (saved_line_table == NULL);
+  saved_line_table = line_table;
+  line_table = ggc_alloc<line_maps> ();
+  linemap_init (line_table, BUILTINS_LOCATION);
+  gcc_assert (saved_line_table->reallocator);
+  line_table->reallocator = saved_line_table->reallocator;
+  gcc_assert (saved_line_table->round_alloc_size);
+  line_table->round_alloc_size = saved_line_table->round_alloc_size;
+  line_table->default_range_bits = 0;
+}
 
 /* Constructor.  Store the old value of line_table, and create a new
    one, using the sitation described in CASE_.  */
 
-temp_line_table::temp_line_table (const line_table_case &case_)
-  : m_old_line_table (line_table)
+line_table_test::line_table_test (const line_table_case &case_)
 {
+  gcc_assert (saved_line_table == NULL);
+  saved_line_table = line_table;
   line_table = ggc_alloc<line_maps> ();
   linemap_init (line_table, BUILTINS_LOCATION);
-  line_table->reallocator = m_old_line_table->reallocator;
-  line_table->round_alloc_size = m_old_line_table->round_alloc_size;
+  gcc_assert (saved_line_table->reallocator);
+  line_table->reallocator = saved_line_table->reallocator;
+  gcc_assert (saved_line_table->round_alloc_size);
+  line_table->round_alloc_size = saved_line_table->round_alloc_size;
   line_table->default_range_bits = case_.m_default_range_bits;
   if (case_.m_base_location)
     {
@@ -1360,9 +1373,11 @@ temp_line_table::temp_line_table (const line_table_case &case_)
 
 /* Destructor.  Restore the old value of line_table.  */
 
-temp_line_table::~temp_line_table ()
+line_table_test::~line_table_test ()
 {
-  line_table = m_old_line_table;
+  gcc_assert (saved_line_table != NULL);
+  line_table = saved_line_table;
+  saved_line_table = NULL;
 }
 
 /* Verify basic operation of ordinary linemaps.  */
@@ -1370,7 +1385,7 @@ temp_line_table::~temp_line_table ()
 static void
 test_accessing_ordinary_linemaps (const line_table_case &case_)
 {
-  temp_line_table tmp_lt (case_);
+  line_table_test tmp_lt (case_);
 
   /* Build a simple linemap describing some locations. */
   linemap_add (line_table, LC_ENTER, false, "foo.c", 0);
@@ -1522,7 +1537,7 @@ test_lexer (const line_table_case &case_)
      "   42\n");
   temp_source_file tmp (SELFTEST_LOCATION, ".txt", content);
 
-  temp_line_table tmp_lt (case_);
+  line_table_test tmp_lt (case_);
 
   cpp_reader *parser = cpp_create_reader (CLK_GNUC89, NULL, line_table);
 
@@ -1586,15 +1601,11 @@ static const location_t boundary_locations[] = {
   LINE_MAP_MAX_LOCATION_WITH_COLS + 0x100,
 };
 
-/* Run all of the selftests within this file.  */
+/* Run TESTCASE multiple times, once for each case in our test matrix.  */
 
 void
-input_c_tests ()
+for_each_line_table_case (void (*testcase) (const line_table_case &))
 {
-  test_should_have_column_data_p ();
-  test_unknown_location ();
-  test_builtins ();
-
   /* As noted above in the description of struct line_table_case,
      we want to explore a test matrix of interesting line_table
      situations, running various selftests for each case within the
@@ -1615,9 +1626,7 @@ input_c_tests ()
 	{
 	  line_table_case c (default_range_bits, boundary_locations[loc_idx]);
 
-	  /* Run all tests for the given case within the test matrix.  */
-	  test_accessing_ordinary_linemaps (c);
-	  test_lexer (c);
+	  testcase (c);
 
 	  num_cases_tested++;
 	}
@@ -1625,6 +1634,19 @@ input_c_tests ()
 
   /* Verify that we fully covered the test matrix.  */
   ASSERT_EQ (num_cases_tested, 2 * 12);
+}
+
+/* Run all of the selftests within this file.  */
+
+void
+input_c_tests ()
+{
+  test_should_have_column_data_p ();
+  test_unknown_location ();
+  test_builtins ();
+
+  for_each_line_table_case (test_accessing_ordinary_linemaps);
+  for_each_line_table_case (test_lexer);
 
   test_reading_source_line ();
 }
diff --git a/gcc/input.h b/gcc/input.h
index e6f44c0..4de0d78 100644
--- a/gcc/input.h
+++ b/gcc/input.h
@@ -24,6 +24,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "line-map.h"
 
 extern GTY(()) struct line_maps *line_table;
+extern GTY(()) struct line_maps *saved_line_table;
 
 /* A value which will never be used to represent a real location.  */
 #define UNKNOWN_LOCATION ((source_location) 0)
diff --git a/gcc/selftest.h b/gcc/selftest.h
index 5aa7568..f2e34b7 100644
--- a/gcc/selftest.h
+++ b/gcc/selftest.h
@@ -91,6 +91,48 @@ class temp_source_file
   char *m_filename;
 };
 
+/* Various selftests involving location-handling require constructing a
+   line table and one or more line maps within it.
+
+   For maximum test coverage we want to run these tests with a variety
+   of situations:
+   - line_table->default_range_bits: some frontends use a non-zero value
+   and others use zero
+   - the fallback modes within line-map.c: there are various threshold
+   values for source_location/location_t beyond line-map.c changes
+   behavior (disabling of the range-packing optimization, disabling
+   of column-tracking).  We can exercise these by starting the line_table
+   at interesting values at or near these thresholds.
+
+   The following struct describes a particular case within our test
+   matrix.  */
+
+struct line_table_case;
+
+/* A class for overriding the global "line_table" within a selftest,
+   restoring its value afterwards.  At most one instance of this
+   class can exist at once, due to the need to keep the old value
+   of line_table as a GC root.  */
+
+class line_table_test
+{
+ public:
+  /* Default constructor.  Override "line_table", using sane defaults
+     for the temporary line_table.  */
+  line_table_test ();
+
+  /* Constructor.  Override "line_table", using the case described by C.  */
+  line_table_test (const line_table_case &c);
+
+  /* Destructor.  Restore the saved line_table.  */
+  ~line_table_test ();
+};
+
+/* Run TESTCASE multiple times, once for each case in our test matrix.  */
+
+extern void
+for_each_line_table_case (void (*testcase) (const line_table_case &));
+
 /* A helper function for writing tests that interact with the
    garbage collector.  */
 
-- 
1.8.5.3

