From 817865c4501ee3fe4cfaa62b8ff440452e20cd4f Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 7 Jul 2016 04:43:58 -0400
Subject: [PATCH 11/25] FIXME: cleanups

---
 libcpp/charset.c        | 198 +-----------------------------------------------
 libcpp/expr.c           |   4 -
 libcpp/include/cpplib.h | 147 +----------------------------------
 libcpp/internal.h       |  15 ++++
 libcpp/lex.c            |  10 ---
 5 files changed, 19 insertions(+), 355 deletions(-)

diff --git a/libcpp/charset.c b/libcpp/charset.c
index 9dbf4d9..afc5abe 100644
--- a/libcpp/charset.c
+++ b/libcpp/charset.c
@@ -2237,203 +2237,9 @@ _cpp_default_encoding (void)
   return current_encoding;
 }
 
-/* Implementation of class cpp_string_location and
-   class cpp_string_location_reader.
-   We put them in this source file in the hope that they can be
-   inlined into heavy users such as cpp_interpret_string without
-   requiring the compiler itself to be built with LTO.  */
+/* Implementation of class cpp_string_location_reader.  */
 
-/* Initialize cpp_string_location.  This can't be a constructor since
-   cpp_string_location occurs inside a union.  Specifically, there
-   is a cpp_string_location within struct cpp_string, and the latter
-   is within union cpp_token_u within struct cpp_token.  */
-
-#if 0
-void
-cpp_string_location::init ()
-{
-  m_fragloc_array = NULL;
-  m_num_fraglocs = 0;
-  m_alloc_fraglocs = 0;
-}
-
-/* Another initializer for cpp_string_location; see the note above
-   for why this can't be a constructor.  */
-
-void
-cpp_string_location::init_raw (source_location loc, int len, int cols_per_item,
-			       int bytes_per_item, line_maps *line_table)
-{
-  if (loc > LINE_MAP_MAX_LOCATION_WITH_COLS)
-    cols_per_item = 0;
-
-  line_map_realloc reallocator = (line_table->reallocator
-				  ? line_table->reallocator
-				  : (line_map_realloc) xrealloc);
-  m_fragloc_array = (cpp_string_fragment_location *)reallocator
-    (NULL,
-     sizeof (cpp_string_fragment_location));
-  m_fragloc_array[0].m_len = len;
-
-  /* LOC might be a macro location.  It only makes sense to do
-     column-by-column calculations on ordinary maps, so get the
-     corresponding location in an ordinary map.  */
-  source_location ordinary_loc
-    = linemap_resolve_location (line_table, loc,
-				LRK_SPELLING_LOCATION, NULL);
-  m_fragloc_array[0].m_loc = ordinary_loc;
-  m_fragloc_array[0].m_cols_per_item = cols_per_item;
-  m_fragloc_array[0].m_bits_per_col
-    = LINEMAPS_LAST_ORDINARY_MAP (line_table)->m_range_bits;
-  m_fragloc_array[0].m_bytes_per_item = bytes_per_item;
-  m_num_fraglocs = 1;
-  m_alloc_fraglocs = 1;
-}
-
-
-/* FIXME: comment.  */
-/* FIXME: what if bytes_per_item is not 1?  */
-void
-cpp_string_location::add_char_at (source_range range, int bytes_per_item,
-				  line_maps *line_table)
-{
-  if (m_fragloc_array)
-    {
-      /* Is this a simple run-on character in the next column
-	 within the current fragment?  */
-      cpp_string_fragment_location *current_fragment
-	= get_current_fragment ();
-      source_range next_range = current_fragment->get_next_range ();
-      if (range.m_start == next_range.m_start
-	  && range.m_finish == next_range.m_finish
-	  && bytes_per_item == current_fragment->m_bytes_per_item)
-	/* If so, we can simply increase the length of the current
-	   fragment.  */
-	{
-	  current_fragment->m_len++;
-	  return;
-	}
-
-      /* We need to start a new fragment.  This may require growing
-	 the underlying array.  */
-      if (++m_num_fraglocs > m_alloc_fraglocs)
-	{
-	  m_alloc_fraglocs *= 2;
-	  line_map_realloc reallocator = (line_table->reallocator
-					  ? line_table->reallocator
-					  : (line_map_realloc) xrealloc);
-	  m_fragloc_array = (cpp_string_fragment_location *)reallocator
-	    (m_fragloc_array,
-	     sizeof (cpp_string_fragment_location) * m_alloc_fraglocs);
-	}
-    }
-  else
-    {
-      /* Begin new fragment array.  */
-      line_map_realloc reallocator = (line_table->reallocator
-				      ? line_table->reallocator
-				      : (line_map_realloc) xrealloc);
-      m_fragloc_array = (cpp_string_fragment_location *)reallocator
-	(NULL, sizeof (cpp_string_fragment_location));
-      m_num_fraglocs = 1;
-      m_alloc_fraglocs = 1;
-    }
-
-  cpp_string_fragment_location *current_fragment = get_current_fragment ();
-  current_fragment->m_len = 1;
-  current_fragment->m_loc = range.m_start;
-  current_fragment->m_bits_per_col
-    = LINEMAPS_LAST_ORDINARY_MAP (line_table)->m_range_bits;
-  /* FIXME: does this need reworking?  */
-  current_fragment->m_cols_per_item
-    = (((range.m_finish - range.m_start)
-	>> current_fragment->m_bits_per_col) + 1);
-  current_fragment->m_bytes_per_item = bytes_per_item;
-}
-
-/* FIXME.  */
-void
-cpp_string_location::add_n_chars_at (int flen, int bytes_per_item,
-				     cpp_string_location_reader *loc_reader)
-{
-  if (loc_reader)
-    while (flen--)
-      add_char_at (loc_reader->get_next (), bytes_per_item,
-		   loc_reader->get_line_maps ());
-}
-
-/* FIXME.  */
-source_location
-cpp_string_location::get_loc_at_index (unsigned int char_idx) const
-{
-  unsigned int fragment_idx = 0;
-  while (fragment_idx < m_num_fraglocs)
-    {
-      const cpp_string_fragment_location *fragment
-	= &m_fragloc_array[fragment_idx];
-      for (unsigned int item_idx = 0; item_idx < fragment->m_len; item_idx++)
-	{
-	  if (char_idx < fragment->m_bytes_per_item)
-	    return fragment->get_item_range (item_idx).m_start;
-	  char_idx -= fragment->m_bytes_per_item;
-	}
-      fragment_idx++;
-    }
-
-  /* Error: accessing beyond the end of the array.  */
-  return 0;
-}
-
-/* FIXME.  */
-source_range
-cpp_string_location::get_range_at_index (unsigned int char_idx) const
-{
-  unsigned int fragment_idx = 0;
-  while (fragment_idx < m_num_fraglocs)
-    {
-      const cpp_string_fragment_location *fragment
-	= &m_fragloc_array[fragment_idx];
-      for (unsigned int item_idx = 0; item_idx < fragment->m_len; item_idx++)
-	{
-	  if (char_idx < fragment->m_bytes_per_item)
-	    return fragment->get_item_range (item_idx);
-	  char_idx -= fragment->m_bytes_per_item;
-	}
-      fragment_idx++;
-    }
-
-  /* Error: accessing beyond the end of the array.  */
-  source_range err;
-  err.m_start = 0;
-  err.m_finish = 0;
-  return err;
-}
-
-/* FIXME. FINISH_IDX is within the range.  */
-source_range
-cpp_string_location::get_range_between_indices (unsigned int start_idx,
-						unsigned int finish_idx) const
-{
-  /* This could be optimized if necessary.  */
-  source_range result;
-  result.m_start = get_loc_at_index (start_idx);
-  result.m_finish = get_range_at_index (finish_idx).m_finish;
-  return result;
-}
-
-/* FIXME.  */
-bool
-cpp_string_location::trivial_p () const
-{
-  if (m_num_fraglocs == 1)
-    if (m_fragloc_array[0].m_cols_per_item == 1)
-      /* FIXME: do we also want to check bytes per item?  */
-      return true;
-  return false;
-}
-#endif
-
-/* Constructor for iterating through an arbitrary buffer.  */
+/* Constructor for cpp_string_location_reader.  */
 
 cpp_string_location_reader::
 cpp_string_location_reader (source_location src_loc,
diff --git a/libcpp/expr.c b/libcpp/expr.c
index deaa3c5..5cdca6f 100644
--- a/libcpp/expr.c
+++ b/libcpp/expr.c
@@ -1228,10 +1228,6 @@ _cpp_parse_expr (cpp_reader *pfile, bool is_if)
 			      "missing binary operator before token \"%s\"",
 			      cpp_token_as_text (pfile, op.token));
 	  want_value = false;
-#if 0
-	  ((cpp_token *)op.token)->val.str.loc.init_raw (op.loc, 1, 1, 1, /* FIXME */
-							 pfile->line_table);
-#endif
 	  top->value = eval_token (pfile, op.token, op.loc);
 	  continue;
 
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index 596be39..27f5bbf 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -173,151 +173,6 @@ enum c_lang {CLK_GNUC89 = 0, CLK_GNUC99, CLK_GNUC11,
 	     CLK_GNUCXX, CLK_CXX98, CLK_GNUCXX11, CLK_CXX11,
 	     CLK_GNUCXX14, CLK_CXX14, CLK_GNUCXX1Z, CLK_CXX1Z, CLK_ASM};
 
-#if 1
-//extern source_location cpp_get_substring_location (source_location 
-#else
-/* Location of the individual chars in a cpp_string.
-   Specifically, this stores a run of characters of len, starting at loc,
-   with a consistent number of columns per char.
-   See the description below for cpp_string_location.  */
-struct GTY(()) cpp_string_fragment_location {
-  source_location m_loc;
-  unsigned int m_len : 16;
-  unsigned int m_cols_per_item : 8;
-  unsigned int m_bits_per_col : 4;
-  unsigned int m_bytes_per_item : 4;
-
-  source_location get_item_start (int item_idx) const
-  {
-    return m_loc + ((item_idx * m_cols_per_item) << m_bits_per_col);
-  }
-
-  source_location get_item_finish (int item_idx) const
-  {
-    if (m_cols_per_item > 0)
-      return get_item_start (item_idx) + ((m_cols_per_item - 1) << m_bits_per_col);
-    else
-      return m_loc;
-  }
-
-  source_range get_item_range (int item_idx) const
-  {
-    source_range result;
-    result.m_start = get_item_start (item_idx);
-    result.m_finish = get_item_finish (item_idx);
-    return result;
-  }
-  source_range get_next_range () const
-  {
-    return get_item_range (m_len);
-  }
-  source_range get_covered_range () const
-  {
-    source_range result;
-    result.m_start = m_loc;
-    result.m_finish = get_item_finish (m_len - 1);
-    return result;
-  }
-  void debug (const char *msg) const;
-};
-
-class cpp_string_location_reader;
-
-/* Location of the individual chars in a cpp_string.
-   This is stored as a dynamically-allocated array of fragments.
-   For example, consider this call to printf:
-
-     printf ("foo \x25\151 bar"  "baz\n",
-             "not an int");
-
-   The string constant for the first parameter is composed of
-   the concatenation of two string literals, with hexadecimal
-   encoding of a '%' and octal encoding of a 'i', giving a
-   resulting STRING_CST of:
-
-     "foo %i barbaz\n"
-
-   We want to efficiently record the range of locations in the
-   source file of each character so that we can emit warnings about
-   the type mismatch between format specifier "%i" and the non-int
-   second argument.
-
-   We record the locations as a series of fragments, where within
-   each fragment we have a contiguous run of input characters with
-   a consistent number of columns per character.  In the example
-   above the fragments are:
-
-    printf ("foo \x25\151 bar"  "baz\n",
-    .........^^^^.....................: fragment 0: 4 chars at 1 col per char
-    .............^^^^^^^^.............: fragment 1: 2 chars at 4 cols per char
-    .....................^^^^.........: fragment 2: 4 chars at 1 col per char
-    .............................^^^..: fragment 3: 3 chars at 1 col per char
-    ................................^^: fragment 4: 1 char at 2 cols per char
-
-   Note that the hex and octal chars both happen to be 4 cols per char
-   and are contiguous, hence both end up being in fragment 1, whereas the
-   "bar" and "baz" aren't contiguous and hence have to be in separate
-   fragments.
-
-   Note also that having a constant cols-per-char within each fragment
-   means that given an index into the fragment we can directly compute
-   the corresponding source_range.  */
-
-struct GTY(()) cpp_string_location {
-
-  void init ();
-  void init_raw (source_location loc, int len, int cols_per_char,
-		 int bytes_per_item, line_maps *line_table);
-
-  void add_char_at (source_range range, int bytes_per_item,
-		    line_maps *line_table);
-  void add_n_chars_at (int flen, int bytes_per_item,
-		       cpp_string_location_reader *loc_reader);
-
-  source_location get_loc_at_index (unsigned int idx) const;
-  source_range get_range_at_index (unsigned int idx) const;
-  source_range get_range_between_indices (unsigned int start_idx,
-					  unsigned int finish_idx) const;
-
-  void debug () const;
-
-  bool trivial_p () const;
-
- private:
-  cpp_string_fragment_location *get_current_fragment () const
-  {
-    return &m_fragloc_array[m_num_fraglocs - 1];
-  }
-
-  /* Fields.
-     Ideally we would make these fields private, but this isn't easily
-     doable since gengtype generates functions in gtype-desc.c that
-     access them.  */
- public:
-
-  /* We seemingly can't use vec<> from libcpp, so do it "by hand"
-     here.  */
-  cpp_string_fragment_location *m_fragloc_array;
-  unsigned int m_num_fraglocs;
-  unsigned int m_alloc_fraglocs;
-};
-#endif
-
-/* A class for iterating through the source-locations within a
-   string.  */
-class cpp_string_location_reader {
- public:
-  cpp_string_location_reader (source_location src_loc,
-			      line_maps *line_table);
-
-  source_range get_next ();
-
- private:
-  source_location m_loc;
-  int m_offset_per_column;
-  line_maps *m_line_table;
-};
-
 /* Payload of a NUMBER, STRING, CHAR or COMMENT token.  */
 struct GTY(()) cpp_string {
   unsigned int len;
@@ -892,6 +747,8 @@ struct GTY(()) cpp_hashnode {
 // FIXME: GTY marker?
 // FIXME: probably not; it's intended to be temporary.
 
+class cpp_string_location_reader;
+
 class cpp_substring_ranges
 {
  public:
diff --git a/libcpp/internal.h b/libcpp/internal.h
index d669415..06cec86 100644
--- a/libcpp/internal.h
+++ b/libcpp/internal.h
@@ -895,6 +895,21 @@ int linemap_get_expansion_line (struct line_maps *,
 const char* linemap_get_expansion_filename (struct line_maps *,
 					    source_location);
 
+/* A class for iterating through the source-locations within a
+   string.  */
+class cpp_string_location_reader {
+ public:
+  cpp_string_location_reader (source_location src_loc,
+			      line_maps *line_table);
+
+  source_range get_next ();
+
+ private:
+  source_location m_loc;
+  int m_offset_per_column;
+  line_maps *m_line_table;
+};
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/libcpp/lex.c b/libcpp/lex.c
index 0e0fdfd..4e71965 100644
--- a/libcpp/lex.c
+++ b/libcpp/lex.c
@@ -1407,15 +1407,6 @@ lex_number (cpp_reader *pfile, cpp_string *number,
   const uchar *base;
   uchar *dest;
 
-  /* FIXME: should it really use a new "cpp_number", rather than cpp_string?
-     We need to init this, or we get a crash accessing uninited data
-     during GC, since,
-       struct GTY(()) cpp_token
-     has union cpp_token_u with
-       desc ("cpp_token_val_index (&%1)")))
-     and this gives CPP_TOKEN_FLD_STR for numbers (and strings).  */
-  //number->loc.init ();
-
   base = pfile->buffer->cur - 1;
   do
     {
@@ -1455,7 +1446,6 @@ create_literal (cpp_reader *pfile, cpp_token *token, const uchar *base,
   token->type = type;
   token->val.str.len = len;
   token->val.str.text = dest;
-  //token->val.str.loc.init_raw (token->src_loc, len, 1, 1, pfile->line_table);
 }
 
 /* Subroutine of lex_raw_string: Append LEN chars from BASE to the buffer
-- 
1.8.5.3

