From 25c52f2c1ed70ecd40a4362e12ae5f07df670bed Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Mon, 22 Jan 2018 10:22:35 -0500
Subject: [PATCH 2/9] FIXME: input.c: introduce class source_manager

---
 gcc/input.c | 83 +++++++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 59 insertions(+), 24 deletions(-)

diff --git a/gcc/input.c b/gcc/input.c
index 57a1a3c..0f7f1c7 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -130,8 +130,28 @@ struct line_maps *line_table;
 
 struct line_maps *saved_line_table;
 
-static fcache *fcache_tab;
-static const size_t fcache_tab_size = 16;
+/* FIXME.  */
+
+class source_manager
+{
+ public:
+  fcache *lookup_or_add_file (const char *file_path);
+  void forcibly_evict_file (const char *file_path);
+
+ private:
+  fcache *evicted_cache_tab_entry (unsigned *highest_use_count);
+  fcache *add_file (const char *file_path);
+  fcache *lookup_file (const char *file_path);
+
+ private:
+  static const size_t fcache_tab_size = 16;
+  fcache fcache_tab[fcache_tab_size];
+};
+
+/* FIXME.  */
+
+static source_manager *g_source_manager;
+
 static const size_t fcache_buffer_size = 4 * 1024;
 static const size_t fcache_line_record_size = 100;
 
@@ -230,8 +250,8 @@ expand_location_1 (source_location loc,
 static void
 diagnostic_file_cache_init (void)
 {
-  if (fcache_tab == NULL)
-    fcache_tab = new fcache[fcache_tab_size];
+  if (g_source_manager == NULL)
+    g_source_manager = new source_manager ();
 }
 
 /* Free the resources used by the set of cache used for files accessed
@@ -240,10 +260,10 @@ diagnostic_file_cache_init (void)
 void
 diagnostic_file_cache_fini (void)
 {
-  if (fcache_tab)
+  if (g_source_manager)
     {
-      delete [] (fcache_tab);
-      fcache_tab = NULL;
+      delete g_source_manager;
+      g_source_manager = NULL;
     }
 }
 
@@ -270,13 +290,10 @@ total_lines_num (const char *file_path)
    caret diagnostic.  Return the found cached file, or NULL if no
    cached file was found.  */
 
-static fcache*
-lookup_file_in_cache_tab (const char *file_path)
+fcache *
+source_manager::lookup_file (const char *file_path)
 {
-  if (file_path == NULL)
-    return NULL;
-
-  diagnostic_file_cache_init ();
+  gcc_assert (file_path);
 
   /* This will contain the found cached file.  */
   fcache *r = NULL;
@@ -305,7 +322,18 @@ diagnostics_file_cache_forcibly_evict_file (const char *file_path)
 {
   gcc_assert (file_path);
 
-  fcache *r = lookup_file_in_cache_tab (file_path);
+  if (!g_source_manager)
+    return;
+
+  g_source_manager->forcibly_evict_file (file_path);
+}
+
+void
+source_manager::forcibly_evict_file (const char *file_path)
+{
+  gcc_assert (file_path);
+
+  fcache *r = lookup_file (file_path);
   if (!r)
     /* Not found.  */
     return;
@@ -328,8 +356,8 @@ diagnostics_file_cache_forcibly_evict_file (const char *file_path)
    *HIGHEST_USE_COUNT is set to the highest use count of the entries
    in the cache table.  */
 
-static fcache*
-evicted_cache_tab_entry (unsigned *highest_use_count)
+fcache*
+source_manager::evicted_cache_tab_entry (unsigned *highest_use_count)
 {
   diagnostic_file_cache_init ();
 
@@ -367,8 +395,8 @@ evicted_cache_tab_entry (unsigned *highest_use_count)
    function returns the created cache.  Note that only the last
    fcache_tab_size files are cached.  */
 
-static fcache*
-add_file_to_cache_tab (const char *file_path)
+fcache*
+source_manager::add_file (const char *file_path)
 {
 
   FILE *fp = fopen (file_path, "r");
@@ -399,12 +427,12 @@ add_file_to_cache_tab (const char *file_path)
    for this file, add it to the array of cached file and return
    it.  */
 
-static fcache*
-lookup_or_add_file_to_cache_tab (const char *file_path)
+fcache*
+source_manager::lookup_or_add_file (const char *file_path)
 {
-  fcache *r = lookup_file_in_cache_tab (file_path);
+  fcache *r = lookup_file (file_path);
   if (r == NULL)
-    r = add_file_to_cache_tab (file_path);
+    r = add_file (file_path);
   return r;
 }
 
@@ -753,7 +781,12 @@ location_get_source_line (const char *file_path, int line)
   if (line == 0)
     return char_span (NULL, 0);
 
-  fcache *c = lookup_or_add_file_to_cache_tab (file_path);
+  if (file_path == NULL)
+    return char_span (NULL, 0);
+
+  diagnostic_file_cache_init ();
+
+  fcache *c = g_source_manager->lookup_or_add_file (file_path);
   if (c == NULL)
     return char_span (NULL, 0);
 
@@ -771,7 +804,9 @@ location_get_source_line (const char *file_path, int line)
 bool
 location_missing_trailing_newline (const char *file_path)
 {
-  fcache *c = lookup_or_add_file_to_cache_tab (file_path);
+  diagnostic_file_cache_init ();
+
+  fcache *c = g_source_manager->lookup_or_add_file (file_path);
   if (c == NULL)
     return false;
 
-- 
1.8.5.3

