From 20d3b4aec7d79c66affbbd196de4caae4e1110f4 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 13 Jul 2016 11:54:20 -0400
Subject: [PATCH 06/35] FIXME: input.c: introduce class sources_cache

---
 gcc/input.c | 79 +++++++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 56 insertions(+), 23 deletions(-)

diff --git a/gcc/input.c b/gcc/input.c
index d7b2818..1f6e183 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -110,8 +110,28 @@ location_t input_location = UNKNOWN_LOCATION;
 
 struct line_maps *line_table;
 
-static fcache *fcache_tab;
-static const size_t fcache_tab_size = 16;
+/* FIXME.  */
+
+class sources_cache
+{
+ 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 sources_cache *g_sources_cache;
+
 static const size_t fcache_buffer_size = 4 * 1024;
 static const size_t fcache_line_record_size = 100;
 
@@ -179,8 +199,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_sources_cache == NULL)
+    g_sources_cache = new sources_cache ();
 }
 
 /* Free the resources used by the set of cache used for files accessed
@@ -189,10 +209,10 @@ diagnostic_file_cache_init (void)
 void
 diagnostic_file_cache_fini (void)
 {
-  if (fcache_tab)
+  if (g_sources_cache)
     {
-      delete [] (fcache_tab);
-      fcache_tab = NULL;
+      delete g_sources_cache;
+      g_sources_cache = NULL;
     }
 }
 
@@ -219,13 +239,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 *
+sources_cache::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;
@@ -254,7 +271,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_sources_cache)
+    return;
+
+  g_sources_cache->forcibly_evict_file (file_path);
+}
+
+void
+sources_cache::forcibly_evict_file (const char *file_path)
+{
+  gcc_assert (file_path);
+
+  fcache *r = lookup_file (file_path);
   if (!r)
     /* Not found.  */
     return;
@@ -276,8 +304,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*
+sources_cache::evicted_cache_tab_entry (unsigned *highest_use_count)
 {
   diagnostic_file_cache_init ();
 
@@ -315,8 +343,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*
+sources_cache::add_file (const char *file_path)
 {
 
   FILE *fp = fopen (file_path, "r");
@@ -346,12 +374,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*
+sources_cache::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;
 }
 
@@ -728,7 +756,12 @@ location_get_source_line (const char *file_path, int line,
   if (line == 0)
     return NULL;
 
-  fcache *c = lookup_or_add_file_to_cache_tab (file_path);
+  if (file_path == NULL)
+    return NULL;
+
+  diagnostic_file_cache_init ();
+
+  fcache *c = g_sources_cache->lookup_or_add_file (file_path);
   if (c == NULL)
     return NULL;
 
-- 
1.8.5.3

