]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/input.c
PR fortran/95090 - ICE: identifier overflow
[thirdparty/gcc.git] / gcc / input.c
index d058b8aa81f1639156210f86e9722ed1cdcd940b..dd1d23df2f75d07edf396e21fe99705a86dba63d 100644 (file)
@@ -1,5 +1,5 @@
 /* Data and functions related to line maps and input files.
-   Copyright (C) 2004-2016 Free Software Foundation, Inc.
+   Copyright (C) 2004-2020 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -21,17 +21,24 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "coretypes.h"
 #include "intl.h"
+#include "diagnostic.h"
 #include "diagnostic-core.h"
 #include "selftest.h"
 #include "cpplib.h"
 
+#ifndef HAVE_ICONV
+#define HAVE_ICONV 0
+#endif
+
 /* This is a cache used by get_next_line to store the content of a
    file to be searched for file lines.  */
-struct fcache
+class fcache
 {
+public:
   /* These are information used to store a line boundary.  */
-  struct line_info
+  class line_info
   {
+  public:
     /* The line number.  It starts from 1.  */
     size_t line_num;
 
@@ -59,6 +66,10 @@ struct fcache
      array.  */
   unsigned use_count;
 
+  /* The file_path is the key for identifying a particular file in
+     the cache.
+     For libcpp-using code, the underlying buffer for this field is
+     owned by the corresponding _cpp_file within the cpp_reader.  */
   const char *file_path;
 
   FILE *fp;
@@ -91,6 +102,11 @@ struct fcache
      before the line map has seen the end of the file.  */
   size_t total_lines;
 
+  /* Could this file be missing a trailing newline on its final line?
+     Initially true (to cope with empty files), set to true/false
+     as each line is read.  */
+  bool missing_trailing_newline;
+
   /* This is a record of the beginning and end of the lines we've seen
      while reading the file.  This is useful to avoid walking the data
      from the beginning when we are asked to read a line that is
@@ -108,7 +124,14 @@ struct fcache
 
 location_t input_location = UNKNOWN_LOCATION;
 
-struct line_maps *line_table;
+class 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.  */
+
+class line_maps *saved_line_table;
 
 static fcache *fcache_tab;
 static const size_t fcache_tab_size = 16;
@@ -127,11 +150,14 @@ static const size_t fcache_line_record_size = 100;
    associated line/column) in the context of a macro expansion, the
    returned location is the first one (while unwinding the macro
    location towards its expansion point) that is in real source
-   code.  */
+   code.
+
+   ASPECT controls which part of the location to use.  */
 
 static expanded_location
-expand_location_1 (source_location loc,
-                  bool expansion_point_p)
+expand_location_1 (location_t loc,
+                  bool expansion_point_p,
+                  enum location_aspect aspect)
 {
   expanded_location xloc;
   const line_map_ordinary *map;
@@ -161,8 +187,36 @@ expand_location_1 (source_location loc,
                                                          loc, NULL);
          lrk = LRK_SPELLING_LOCATION;
        }
-      loc = linemap_resolve_location (line_table, loc,
-                                     lrk, &map);
+      loc = linemap_resolve_location (line_table, loc, lrk, &map);
+
+      /* loc is now either in an ordinary map, or is a reserved location.
+        If it is a compound location, the caret is in a spelling location,
+        but the start/finish might still be a virtual location.
+        Depending of what the caller asked for, we may need to recurse
+        one level in order to resolve any virtual locations in the
+        end-points.  */
+      switch (aspect)
+       {
+       default:
+         gcc_unreachable ();
+         /* Fall through.  */
+       case LOCATION_ASPECT_CARET:
+         break;
+       case LOCATION_ASPECT_START:
+         {
+           location_t start = get_start (loc);
+           if (start != loc)
+             return expand_location_1 (start, expansion_point_p, aspect);
+         }
+         break;
+       case LOCATION_ASPECT_FINISH:
+         {
+           location_t finish = get_finish (loc);
+           if (finish != loc)
+             return expand_location_1 (finish, expansion_point_p, aspect);
+         }
+         break;
+       }
       xloc = linemap_expand_location (line_table, map, loc);
     }
 
@@ -205,7 +259,7 @@ static size_t
 total_lines_num (const char *file_path)
 {
   size_t r = 0;
-  source_location l = 0;
+  location_t l = 0;
   if (linemap_get_file_highest_location (line_table, file_path, &l))
     {
       gcc_assert (l >= RESERVED_LOCATION_COUNT);
@@ -245,6 +299,33 @@ lookup_file_in_cache_tab (const char *file_path)
   return r;
 }
 
+/* Purge any mention of FILENAME from the cache of files used for
+   printing source code.  For use in selftests when working
+   with tempfiles.  */
+
+void
+diagnostics_file_cache_forcibly_evict_file (const char *file_path)
+{
+  gcc_assert (file_path);
+
+  fcache *r = lookup_file_in_cache_tab (file_path);
+  if (!r)
+    /* Not found.  */
+    return;
+
+  r->file_path = NULL;
+  if (r->fp)
+    fclose (r->fp);
+  r->fp = NULL;
+  r->nb_read = 0;
+  r->line_start_idx = 0;
+  r->line_num = 0;
+  r->line_record.truncate (0);
+  r->use_count = 0;
+  r->total_lines = 0;
+  r->missing_trailing_newline = true;
+}
+
 /* Return the file cache that has been less used, recently, or the
    first empty one.  If HIGHEST_USE_COUNT is non-null,
    *HIGHEST_USE_COUNT is set to the highest use count of the entries
@@ -311,6 +392,7 @@ add_file_to_cache_tab (const char *file_path)
      add_file_to_cache_tab is called.  */
   r->use_count = ++highest_use_count;
   r->total_lines = total_lines_num (file_path);
+  r->missing_trailing_newline = true;
 
   return r;
 }
@@ -335,7 +417,7 @@ lookup_or_add_file_to_cache_tab (const char *file_path)
 fcache::fcache ()
 : use_count (0), file_path (NULL), fp (NULL), data (0),
   size (0), nb_read (0), line_start_idx (0), line_num (0),
-  total_lines (0)
+  total_lines (0), missing_trailing_newline (true)
 {
   line_record.create (0);
 }
@@ -388,7 +470,7 @@ maybe_grow (fcache *c)
     return;
 
   size_t size = c->size == 0 ? fcache_buffer_size : c->size * 2;
-  c->data = XRESIZEVEC (char, c->data, size + 1);
+  c->data = XRESIZEVEC (char, c->data, size);
   c->size = size;
 }
 
@@ -428,14 +510,13 @@ maybe_read_data (fcache *c)
 
 /* Read a new line from file FP, using C as a cache for the data
    coming from the file.  Upon successful completion, *LINE is set to
-   the beginning of the line found.  Space for that line has been
-   allocated in the cache thus *LINE has the same life time as C.
+   the beginning of the line found.  *LINE points directly in the
+   line cache and is only valid until the next call of get_next_line.
    *LINE_LEN is set to the length of the line.  Note that the line
    does not contain any terminal delimiter.  This function returns
    true if some data was read or process from the cache, false
-   otherwise.  Note that subsequent calls to get_next_line return the
-   next lines of the file and might overwrite the content of
-   *LINE.  */
+   otherwise.  Note that subsequent calls to get_next_line might
+   make the content of *LINE invalid.  */
 
 static bool
 get_next_line (fcache *c, char **line, ssize_t *line_len)
@@ -470,19 +551,27 @@ get_next_line (fcache *c, char **line, ssize_t *line_len)
            }
        }
       if (line_end == NULL)
-       /* We've loadded all the file into the cache and still no
-          '\n'.  Let's say the line ends up at one byte passed the
-          end of the file.  This is to stay consistent with the case
-          of when the line ends up with a '\n' and line_end points to
-          that terminal '\n'.  That consistency is useful below in
-          the len calculation.  */
-       line_end = c->data + c->nb_read ;
+       {
+         /* We've loadded all the file into the cache and still no
+            '\n'.  Let's say the line ends up at one byte passed the
+            end of the file.  This is to stay consistent with the case
+            of when the line ends up with a '\n' and line_end points to
+            that terminal '\n'.  That consistency is useful below in
+            the len calculation.  */
+         line_end = c->data + c->nb_read ;
+         c->missing_trailing_newline = true;
+       }
+      else
+       c->missing_trailing_newline = false;
     }
   else
-    next_line_start = line_end + 1;
+    {
+      next_line_start = line_end + 1;
+      c->missing_trailing_newline = false;
+    }
 
   if (ferror (c->fp))
-    return -1;
+    return false;
 
   /* At this point, we've found the end of the of line.  It either
      points to the '\n' or to one byte after the last byte of the
@@ -545,36 +634,6 @@ get_next_line (fcache *c, char **line, ssize_t *line_len)
   return true;
 }
 
-/* Reads the next line from FILE into *LINE.  If *LINE is too small
-   (or NULL) it is allocated (or extended) to have enough space to
-   containe the line.  *LINE_LENGTH must contain the size of the
-   initial*LINE buffer.  It's then updated by this function to the
-   actual length of the returned line.  Note that the returned line
-   can contain several zero bytes.  Also note that the returned string
-   is allocated in static storage that is going to be re-used by
-   subsequent invocations of read_line.  */
-
-static bool
-read_next_line (fcache *cache, char ** line, ssize_t *line_len)
-{
-  char *l = NULL;
-  ssize_t len = 0;
-
-  if (!get_next_line (cache, &l, &len))
-    return false;
-
-  if (*line == NULL)
-    *line = XNEWVEC (char, len);
-  else
-    if (*line_len < len)
-       *line = XRESIZEVEC (char, *line, len);
-
-  memcpy (*line, l, len);
-  *line_len = len;
-
-  return true;
-}
-
 /* Consume the next bytes coming from the cache (or from its
    underlying file if there are remaining unread bytes in the file)
    until we reach the next end-of-line (or end-of-file).  There is no
@@ -591,15 +650,15 @@ goto_next_line (fcache *cache)
 }
 
 /* Read an arbitrary line number LINE_NUM from the file cached in C.
-   The line is copied into *LINE.  *LINE_LEN must have been set to the
-   length of *LINE.  If *LINE is too small (or NULL) it's extended (or
-   allocated) and *LINE_LEN is adjusted accordingly.  *LINE ends up
-   with a terminal zero byte and can contain additional zero bytes.
+   If the line was read successfully, *LINE points to the beginning
+   of the line in the file cache and *LINE_LEN is the length of the
+   line.  *LINE is not nul-terminated, but may contain zero bytes.
+   *LINE is only valid until the next call of read_line_num.
    This function returns bool if a line was read.  */
 
 static bool
 read_line_num (fcache *c, size_t line_num,
-              char ** line, ssize_t *line_len)
+              char **line, ssize_t *line_len)
 {
   gcc_assert (line_num > 0);
 
@@ -651,14 +710,9 @@ read_line_num (fcache *c, size_t line_num,
 
          if (i && i->line_num == line_num)
            {
-             /* We have the start/end of the line.  Let's just copy
-                it again and we are done.  */
-             ssize_t len = i->end_pos - i->start_pos + 1;
-             if (*line_len < len)
-               *line = XRESIZEVEC (char, *line, len);
-             memmove (*line, c->data + i->start_pos, len);
-             (*line)[len - 1] = '\0';
-             *line_len = --len;
+             /* We have the start/end of the line.  */
+             *line = c->data + i->start_pos;
+             *line_len = i->end_pos - i->start_pos;
              return true;
            }
 
@@ -683,35 +737,48 @@ read_line_num (fcache *c, size_t line_num,
 
   /* The line we want is the next one.  Let's read and copy it back to
      the caller.  */
-  return read_next_line (c, line, line_len);
+  return get_next_line (c, line, line_len);
 }
 
-/* Return the physical source line that corresponds to FILE_PATH/LINE in a
-   buffer that is statically allocated.  The newline is replaced by
-   the null character.  Note that the line can contain several null
-   characters, so LINE_LEN, if non-null, points to the actual length
-   of the line.  */
+/* Return the physical source line that corresponds to FILE_PATH/LINE.
+   The line is not nul-terminated.  The returned pointer is only
+   valid until the next call of location_get_source_line.
+   Note that the line can contain several null characters,
+   so the returned value's length has the actual length of the line.
+   If the function fails, a NULL char_span is returned.  */
 
-const char *
-location_get_source_line (const char *file_path, int line,
-                         int *line_len)
+char_span
+location_get_source_line (const char *file_path, int line)
 {
-  static char *buffer;
-  static ssize_t len;
+  char *buffer = NULL;
+  ssize_t len;
 
   if (line == 0)
-    return NULL;
+    return char_span (NULL, 0);
 
   fcache *c = lookup_or_add_file_to_cache_tab (file_path);
   if (c == NULL)
-    return NULL;
+    return char_span (NULL, 0);
 
   bool read = read_line_num (c, line, &buffer, &len);
+  if (!read)
+    return char_span (NULL, 0);
+
+  return char_span (buffer, len);
+}
 
-  if (read && line_len)
-    *line_len = len;
+/* Determine if FILE_PATH missing a trailing newline on its final line.
+   Only valid to call once all of the file has been loaded, by
+   requesting a line number beyond the end of the file.  */
 
-  return read ? buffer : NULL;
+bool
+location_missing_trailing_newline (const char *file_path)
+{
+  fcache *c = lookup_or_add_file_to_cache_tab (file_path);
+  if (c == NULL)
+    return false;
+
+  return c->missing_trailing_newline;
 }
 
 /* Test if the location originates from the spelling location of a
@@ -722,7 +789,7 @@ location_get_source_line (const char *file_path, int line,
    function would return true if passed a token "4" that is the result
    of the expansion of the built-in __LINE__ macro.  */
 bool
-is_location_from_builtin_token (source_location loc)
+is_location_from_builtin_token (location_t loc)
 {
   const line_map_ordinary *map = NULL;
   loc = linemap_resolve_location (line_table, loc,
@@ -736,9 +803,10 @@ is_location_from_builtin_token (source_location loc)
    readable location is set to the string "<built-in>".  */
 
 expanded_location
-expand_location (source_location loc)
+expand_location (location_t loc)
 {
-  return expand_location_1 (loc, /*expansion_point_p=*/true);
+  return expand_location_1 (loc, /*expansion_point_p=*/true,
+                           LOCATION_ASPECT_CARET);
 }
 
 /* Expand the source location LOC into a human readable location.  If
@@ -748,24 +816,26 @@ expand_location (source_location loc)
    "<built-in>".  */
 
 expanded_location
-expand_location_to_spelling_point (source_location loc)
+expand_location_to_spelling_point (location_t loc,
+                                  enum location_aspect aspect)
 {
-  return expand_location_1 (loc, /*expansion_point_p=*/false);
+  return expand_location_1 (loc, /*expansion_point_p=*/false, aspect);
 }
 
 /* The rich_location class within libcpp requires a way to expand
-   source_location instances, and relies on the client code
+   location_t instances, and relies on the client code
    providing a symbol named
      linemap_client_expand_location_to_spelling_point
    to do this.
 
    This is the implementation for libcommon.a (all host binaries),
-   which simply calls into expand_location_to_spelling_point.  */
+   which simply calls into expand_location_1.  */
 
 expanded_location
-linemap_client_expand_location_to_spelling_point (source_location loc)
+linemap_client_expand_location_to_spelling_point (location_t loc,
+                                                 enum location_aspect aspect)
 {
-  return expand_location_to_spelling_point (loc);
+  return expand_location_1 (loc, /*expansion_point_p=*/false, aspect);
 }
 
 
@@ -781,8 +851,8 @@ linemap_client_expand_location_to_spelling_point (source_location loc)
    warning_at, the diagnostic would be suppressed (unless
    -Wsystem-headers).  */
 
-source_location
-expansion_point_location_if_in_system_header (source_location location)
+location_t
+expansion_point_location_if_in_system_header (location_t location)
 {
   if (in_system_header_at (location))
     location = linemap_resolve_location (line_table, location,
@@ -794,35 +864,13 @@ expansion_point_location_if_in_system_header (source_location location)
 /* If LOCATION is a virtual location for a token coming from the expansion
    of a macro, unwind to the location of the expansion point of the macro.  */
 
-source_location
-expansion_point_location (source_location location)
+location_t
+expansion_point_location (location_t location)
 {
   return linemap_resolve_location (line_table, location,
                                   LRK_MACRO_EXPANSION_POINT, NULL);
 }
 
-/* Given location LOC, strip away any packed range information
-   or ad-hoc information.  */
-
-location_t
-get_pure_location (location_t loc)
-{
-  if (IS_ADHOC_LOC (loc))
-    loc
-      = line_table->location_adhoc_data_map.data[loc & MAX_SOURCE_LOCATION].locus;
-
-  if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
-    return loc;
-
-  if (loc < RESERVED_LOCATION_COUNT)
-    return loc;
-
-  const line_map *map = linemap_lookup (line_table, loc);
-  const line_map_ordinary *ordmap = linemap_check_ordinary (map);
-
-  return loc & ~((1 << ordmap->m_range_bits) - 1);
-}
-
 /* Construct a location with caret at CARET, ranging from START to
    finish e.g.
 
@@ -842,8 +890,8 @@ make_location (location_t caret, location_t start, location_t finish)
 {
   location_t pure_loc = get_pure_location (caret);
   source_range src_range;
-  src_range.m_start = start;
-  src_range.m_finish = finish;
+  src_range.m_start = get_start (start);
+  src_range.m_finish = get_finish (finish);
   location_t combined_loc = COMBINE_LOCATION_DATA (line_table,
                                                   pure_loc,
                                                   src_range,
@@ -851,30 +899,30 @@ make_location (location_t caret, location_t start, location_t finish)
   return combined_loc;
 }
 
-#define ONE_K 1024
-#define ONE_M (ONE_K * ONE_K)
+/* Same as above, but taking a source range rather than two locations.  */
 
-/* Display a number as an integer multiple of either:
-   - 1024, if said integer is >= to 10 K (in base 2)
-   - 1024 * 1024, if said integer is >= 10 M in (base 2)
- */
-#define SCALE(x) ((unsigned long) ((x) < 10 * ONE_K \
-                 ? (x) \
-                 : ((x) < 10 * ONE_M \
-                    ? (x) / ONE_K \
-                    : (x) / ONE_M)))
-
-/* For a given integer, display either:
-   - the character 'k', if the number is higher than 10 K (in base 2)
-     but strictly lower than 10 M (in base 2)
-   - the character 'M' if the number is higher than 10 M (in base2)
-   - the charcter ' ' if the number is strictly lower  than 10 K  */
-#define STAT_LABEL(x) ((x) < 10 * ONE_K ? ' ' : ((x) < 10 * ONE_M ? 'k' : 'M'))
+location_t
+make_location (location_t caret, source_range src_range)
+{
+  location_t pure_loc = get_pure_location (caret);
+  return COMBINE_LOCATION_DATA (line_table, pure_loc, src_range, NULL);
+}
 
-/* Display an integer amount as multiple of 1K or 1M (in base 2).
-   Display the correct unit (either k, M, or ' ') after the amout, as
-   well.  */
-#define FORMAT_AMOUNT(size) SCALE (size), STAT_LABEL (size)
+/* An expanded_location stores the column in byte units.  This function
+   converts that column to display units.  That requires reading the associated
+   source line in order to calculate the display width.  If that cannot be done
+   for any reason, then returns the byte column as a fallback.  */
+int
+location_compute_display_column (expanded_location exploc)
+{
+  if (!(exploc.file && *exploc.file && exploc.line && exploc.column))
+    return exploc.column;
+  char_span line = location_get_source_line (exploc.file, exploc.line);
+  /* If line is NULL, this function returns exploc.column which is the
+     desired fallback.  */
+  return cpp_byte_column_to_display_column (line.get_buffer (), line.length (),
+                                           exploc.column);
+}
 
 /* Dump statistics to stderr about the memory usage of the line_table
    set of line maps.  This also displays some statistics about macro
@@ -910,57 +958,45 @@ dump_line_table_statistics (void)
              s.num_macro_tokens / s.num_expanded_macros);
   fprintf (stderr,
            "\nLine Table allocations during the "
-           "compilation process\n");
-  fprintf (stderr, "Number of ordinary maps used:        %5ld%c\n",
-           SCALE (s.num_ordinary_maps_used),
-           STAT_LABEL (s.num_ordinary_maps_used));
-  fprintf (stderr, "Ordinary map used size:              %5ld%c\n",
-           SCALE (s.ordinary_maps_used_size),
-           STAT_LABEL (s.ordinary_maps_used_size));
-  fprintf (stderr, "Number of ordinary maps allocated:   %5ld%c\n",
-           SCALE (s.num_ordinary_maps_allocated),
-           STAT_LABEL (s.num_ordinary_maps_allocated));
-  fprintf (stderr, "Ordinary maps allocated size:        %5ld%c\n",
-           SCALE (s.ordinary_maps_allocated_size),
-           STAT_LABEL (s.ordinary_maps_allocated_size));
-  fprintf (stderr, "Number of macro maps used:           %5ld%c\n",
-           SCALE (s.num_macro_maps_used),
-           STAT_LABEL (s.num_macro_maps_used));
-  fprintf (stderr, "Macro maps used size:                %5ld%c\n",
-           SCALE (s.macro_maps_used_size),
-           STAT_LABEL (s.macro_maps_used_size));
-  fprintf (stderr, "Macro maps locations size:           %5ld%c\n",
-           SCALE (s.macro_maps_locations_size),
-           STAT_LABEL (s.macro_maps_locations_size));
-  fprintf (stderr, "Macro maps size:                     %5ld%c\n",
-           SCALE (macro_maps_size),
-           STAT_LABEL (macro_maps_size));
-  fprintf (stderr, "Duplicated maps locations size:      %5ld%c\n",
-           SCALE (s.duplicated_macro_maps_locations_size),
-           STAT_LABEL (s.duplicated_macro_maps_locations_size));
-  fprintf (stderr, "Total allocated maps size:           %5ld%c\n",
-           SCALE (total_allocated_map_size),
-           STAT_LABEL (total_allocated_map_size));
-  fprintf (stderr, "Total used maps size:                %5ld%c\n",
-           SCALE (total_used_map_size),
-           STAT_LABEL (total_used_map_size));
-  fprintf (stderr, "Ad-hoc table size:                   %5ld%c\n",
-          SCALE (s.adhoc_table_size),
-          STAT_LABEL (s.adhoc_table_size));
-  fprintf (stderr, "Ad-hoc table entries used:           %5ld\n",
-          s.adhoc_table_entries_used);
-  fprintf (stderr, "optimized_ranges: %i\n",
-          line_table->num_optimized_ranges);
-  fprintf (stderr, "unoptimized_ranges: %i\n",
-          line_table->num_unoptimized_ranges);
+          "compilation process\n");
+  fprintf (stderr, "Number of ordinary maps used:        " PRsa (5) "\n",
+          SIZE_AMOUNT (s.num_ordinary_maps_used));
+  fprintf (stderr, "Ordinary map used size:              " PRsa (5) "\n",
+          SIZE_AMOUNT (s.ordinary_maps_used_size));
+  fprintf (stderr, "Number of ordinary maps allocated:   " PRsa (5) "\n",
+          SIZE_AMOUNT (s.num_ordinary_maps_allocated));
+  fprintf (stderr, "Ordinary maps allocated size:        " PRsa (5) "\n",
+          SIZE_AMOUNT (s.ordinary_maps_allocated_size));
+  fprintf (stderr, "Number of macro maps used:           " PRsa (5) "\n",
+          SIZE_AMOUNT (s.num_macro_maps_used));
+  fprintf (stderr, "Macro maps used size:                " PRsa (5) "\n",
+          SIZE_AMOUNT (s.macro_maps_used_size));
+  fprintf (stderr, "Macro maps locations size:           " PRsa (5) "\n",
+          SIZE_AMOUNT (s.macro_maps_locations_size));
+  fprintf (stderr, "Macro maps size:                     " PRsa (5) "\n",
+          SIZE_AMOUNT (macro_maps_size));
+  fprintf (stderr, "Duplicated maps locations size:      " PRsa (5) "\n",
+          SIZE_AMOUNT (s.duplicated_macro_maps_locations_size));
+  fprintf (stderr, "Total allocated maps size:           " PRsa (5) "\n",
+          SIZE_AMOUNT (total_allocated_map_size));
+  fprintf (stderr, "Total used maps size:                " PRsa (5) "\n",
+          SIZE_AMOUNT (total_used_map_size));
+  fprintf (stderr, "Ad-hoc table size:                   " PRsa (5) "\n",
+          SIZE_AMOUNT (s.adhoc_table_size));
+  fprintf (stderr, "Ad-hoc table entries used:           " PRsa (5) "\n",
+          SIZE_AMOUNT (s.adhoc_table_entries_used));
+  fprintf (stderr, "optimized_ranges:                    " PRsa (5) "\n",
+          SIZE_AMOUNT (line_table->num_optimized_ranges));
+  fprintf (stderr, "unoptimized_ranges:                  " PRsa (5) "\n",
+          SIZE_AMOUNT (line_table->num_unoptimized_ranges));
 
   fprintf (stderr, "\n");
 }
 
 /* Get location one beyond the final location in ordinary map IDX.  */
 
-static source_location
-get_end_location (struct line_maps *set, unsigned int idx)
+static location_t
+get_end_location (class line_maps *set, unsigned int idx)
 {
   if (idx == LINEMAPS_ORDINARY_USED (set) - 1)
     return set->highest_location;
@@ -984,37 +1020,37 @@ write_digit (FILE *stream, int digit)
 static void
 write_digit_row (FILE *stream, int indent,
                 const line_map_ordinary *map,
-                source_location loc, int max_col, int divisor)
+                location_t loc, int max_col, int divisor)
 {
   fprintf (stream, "%*c", indent, ' ');
   fprintf (stream, "|");
   for (int column = 1; column < max_col; column++)
     {
-      source_location column_loc = loc + (column << map->m_range_bits);
+      location_t column_loc = loc + (column << map->m_range_bits);
       write_digit (stream, column_loc / divisor);
     }
   fprintf (stream, "\n");
 }
 
 /* Write a half-closed (START) / half-open (END) interval of
-   source_location to STREAM.  */
+   location_t to STREAM.  */
 
 static void
 dump_location_range (FILE *stream,
-                    source_location start, source_location end)
+                    location_t start, location_t end)
 {
   fprintf (stream,
-          "  source_location interval: %u <= loc < %u\n",
+          "  location_t interval: %u <= loc < %u\n",
           start, end);
 }
 
 /* Write a labelled description of a half-closed (START) / half-open (END)
-   interval of source_location to STREAM.  */
+   interval of location_t to STREAM.  */
 
 static void
 dump_labelled_location_range (FILE *stream,
                              const char *name,
-                             source_location start, source_location end)
+                             location_t start, location_t end)
 {
   fprintf (stream, "%s\n", name);
   dump_location_range (stream, start, end);
@@ -1033,7 +1069,7 @@ dump_location_info (FILE *stream)
   /* Visualize the ordinary line_map instances, rendering the sources. */
   for (unsigned int idx = 0; idx < LINEMAPS_ORDINARY_USED (line_table); idx++)
     {
-      source_location end_location = get_end_location (line_table, idx);
+      location_t end_location = get_end_location (line_table, idx);
       /* half-closed: doesn't include this one. */
 
       const line_map_ordinary *map
@@ -1050,9 +1086,40 @@ dump_location_info (FILE *stream)
               map->m_column_and_range_bits - map->m_range_bits);
       fprintf (stream, "  range bits: %i\n",
               map->m_range_bits);
+      const char * reason;
+      switch (map->reason) {
+      case LC_ENTER:
+       reason = "LC_ENTER";
+       break;
+      case LC_LEAVE:
+       reason = "LC_LEAVE";
+       break;
+      case LC_RENAME:
+       reason = "LC_RENAME";
+       break;
+      case LC_RENAME_VERBATIM:
+       reason = "LC_RENAME_VERBATIM";
+       break;
+      case LC_ENTER_MACRO:
+       reason = "LC_RENAME_MACRO";
+       break;
+      default:
+       reason = "Unknown";
+      }
+      fprintf (stream, "  reason: %d (%s)\n", map->reason, reason);
+
+      const line_map_ordinary *includer_map
+       = linemap_included_from_linemap (line_table, map);
+      fprintf (stream, "  included from location: %d",
+              linemap_included_from (map));
+      if (includer_map) {
+       fprintf (stream, " (in ordinary map %d)",
+                int (includer_map - line_table->info_ordinary.maps));
+      }
+      fprintf (stream, "\n");
 
       /* Render the span of source lines that this "map" covers.  */
-      for (source_location loc = MAP_START_LOCATION (map);
+      for (location_t loc = MAP_START_LOCATION (map);
           loc < end_location;
           loc += (1 << map->m_range_bits) )
        {
@@ -1061,31 +1128,36 @@ dump_location_info (FILE *stream)
          expanded_location exploc
            = linemap_expand_location (line_table, map, loc);
 
-         if (0 == exploc.column)
+         if (exploc.column == 0)
            {
              /* Beginning of a new source line: draw the line.  */
 
-             int line_size;
-             const char *line_text = location_get_source_line (exploc.file,
-                                                               exploc.line,
-                                                               &line_size);
+             char_span line_text = location_get_source_line (exploc.file,
+                                                             exploc.line);
              if (!line_text)
                break;
              fprintf (stream,
                       "%s:%3i|loc:%5i|%.*s\n",
                       exploc.file, exploc.line,
                       loc,
-                      line_size, line_text);
+                      (int)line_text.length (), line_text.get_buffer ());
 
              /* "loc" is at column 0, which means "the whole line".
                 Render the locations *within* the line, by underlining
-                it, showing the source_location numeric values
+                it, showing the location_t numeric values
                 at each column.  */
-             int max_col = (1 << map->m_column_and_range_bits) - 1;
-             if (max_col > line_size)
-               max_col = line_size + 1;
+             size_t max_col = (1 << map->m_column_and_range_bits) - 1;
+             if (max_col > line_text.length ())
+               max_col = line_text.length () + 1;
 
-             int indent = 14 + strlen (exploc.file);
+             int len_lnum = num_digits (exploc.line);
+             if (len_lnum < 3)
+               len_lnum = 3;
+             int len_loc = num_digits (loc);
+             if (len_loc < 5)
+               len_loc = 5;
+
+             int indent = 6 + strlen (exploc.file) + len_lnum + len_loc;
 
              /* Thousands.  */
              if (end_location > 999)
@@ -1113,12 +1185,12 @@ dump_location_info (FILE *stream)
   /* Visualize the macro line_map instances, rendering the sources. */
   for (unsigned int i = 0; i < LINEMAPS_MACRO_USED (line_table); i++)
     {
-      /* Each macro map that is allocated owns source_location values
+      /* Each macro map that is allocated owns location_t values
         that are *lower* that the one before them.
         Hence it's meaningful to view them either in order of ascending
         source locations, or in order of ascending macro map index.  */
-      const bool ascending_source_locations = true;
-      unsigned int idx = (ascending_source_locations
+      const bool ascending_location_ts = true;
+      unsigned int idx = (ascending_location_ts
                          ? (LINEMAPS_MACRO_USED (line_table) - (i + 1))
                          : i);
       const line_map_macro *map = LINEMAPS_MACRO_MAP_AT (line_table, idx);
@@ -1139,8 +1211,8 @@ dump_location_info (FILE *stream)
       fprintf (stream, "  macro_locations:\n");
       for (unsigned int i = 0; i < MACRO_MAP_NUM_MACRO_TOKENS (map); i++)
        {
-         source_location x = MACRO_MAP_LOCATIONS (map)[2 * i];
-         source_location y = MACRO_MAP_LOCATIONS (map)[(2 * i) + 1];
+         location_t x = MACRO_MAP_LOCATIONS (map)[2 * i];
+         location_t y = MACRO_MAP_LOCATIONS (map)[(2 * i) + 1];
 
          /* linemap_add_macro_token encodes token numbers in an expansion
             by putting them after MAP_START_LOCATION. */
@@ -1151,7 +1223,7 @@ dump_location_info (FILE *stream)
             adding 2 extra args for padding tokens; presumably there may
             be a leading and/or trailing padding token injected,
             each for 2 more location slots.
-            This would explain there being up to 4 source_locations slots
+            This would explain there being up to 4 location_ts slots
             that may be uninitialized.  */
 
          fprintf (stream, "    %u: %u, %u\n",
@@ -1161,7 +1233,8 @@ dump_location_info (FILE *stream)
          if (x == y)
            {
              if (x < MAP_START_LOCATION (map))
-               inform (x, "token %u has x-location == y-location == %u", i, x);
+               inform (x, "token %u has %<x-location == y-location == %u%>",
+                       i, x);
              else
                fprintf (stream,
                         "x-location == y-location == %u encodes token # %u\n",
@@ -1169,24 +1242,24 @@ dump_location_info (FILE *stream)
                }
          else
            {
-             inform (x, "token %u has x-location == %u", i, x);
-             inform (x, "token %u has y-location == %u", i, y);
+             inform (x, "token %u has %<x-location == %u%>", i, x);
+             inform (x, "token %u has %<y-location == %u%>", i, y);
            }
        }
       fprintf (stream, "\n");
     }
 
-  /* It appears that MAX_SOURCE_LOCATION itself is never assigned to a
+  /* It appears that MAX_LOCATION_T itself is never assigned to a
      macro map, presumably due to an off-by-one error somewhere
      between the logic in linemap_enter_macro and
      LINEMAPS_MACRO_LOWEST_LOCATION.  */
-  dump_labelled_location_range (stream, "MAX_SOURCE_LOCATION",
-                               MAX_SOURCE_LOCATION,
-                               MAX_SOURCE_LOCATION + 1);
+  dump_labelled_location_range (stream, "MAX_LOCATION_T",
+                               MAX_LOCATION_T,
+                               MAX_LOCATION_T + 1);
 
   /* Visualize ad-hoc values.  */
   dump_labelled_location_range (stream, "AD-HOC LOCATIONS",
-                               MAX_SOURCE_LOCATION + 1, UINT_MAX);
+                               MAX_LOCATION_T + 1, UINT_MAX);
 }
 
 /* string_concat's constructor.  */
@@ -1223,7 +1296,7 @@ string_concat_db::record_string_concatenation (int num, location_t *locs)
   m_table->put (key_loc, concat);
 }
 
-/* Determine if LOC was the location of the the initial token of a
+/* Determine if LOC was the location of the initial token of a
    concatenation of string literal tokens.
    If so, *OUT_NUM is written to with the number of tokens, and
    *OUT_LOCS with the location of an array of locations of the
@@ -1309,6 +1382,25 @@ get_substring_ranges_for_loc (cpp_reader *pfile,
   if (strloc == UNKNOWN_LOCATION)
     return "unknown location";
 
+  /* Reparsing the strings requires accurate location information.
+     If -ftrack-macro-expansion has been overridden from its default
+     of 2, then we might have a location of a macro expansion point,
+     rather than the location of the literal itself.
+     Avoid this by requiring that we have full macro expansion tracking
+     for substring locations to be available.  */
+  if (cpp_get_options (pfile)->track_macro_expansion != 2)
+    return "track_macro_expansion != 2";
+
+  /* If #line or # 44 "file"-style directives are present, then there's
+     no guarantee that the line numbers we have can be used to locate
+     the strings.  For example, we might have a .i file with # directives
+     pointing back to lines within a .c file, but the .c file might
+     have been edited since the .i file was created.
+     In such a case, the safest course is to disable on-demand substring
+     locations.  */
+  if (line_table->seen_line_directive)
+    return "seen line directive";
+
   /* If string concatenation has occurred at STRLOC, get the locations
      of all of the literal tokens making up the compound string.
      Otherwise, just use STRLOC.  */
@@ -1326,24 +1418,32 @@ get_substring_ranges_for_loc (cpp_reader *pfile,
       source_range src_range = get_range_from_loc (line_table, strlocs[i]);
 
       if (src_range.m_start >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
-       /* If the string is within a macro expansion, we can't get at the
-          end location.  */
-       return "macro expansion";
-
-      if (src_range.m_start >= LINE_MAP_MAX_LOCATION_WITH_COLS)
-       /* If so, we can't reliably determine where the token started within
-          its line.  */
-       return "range starts after LINE_MAP_MAX_LOCATION_WITH_COLS";
-
-      if (src_range.m_finish >= LINE_MAP_MAX_LOCATION_WITH_COLS)
-       /* If so, we can't reliably determine where the token finished within
-          its line.  */
-       return "range ends after LINE_MAP_MAX_LOCATION_WITH_COLS";
+       {
+         /* If the string token was within a macro expansion, then we can
+            cope with it for the simple case where we have a single token.
+            Otherwise, bail out.  */
+         if (src_range.m_start != src_range.m_finish)
+           return "macro expansion";
+       }
+      else
+       {
+         if (src_range.m_start >= LINE_MAP_MAX_LOCATION_WITH_COLS)
+           /* If so, we can't reliably determine where the token started within
+              its line.  */
+           return "range starts after LINE_MAP_MAX_LOCATION_WITH_COLS";
+
+         if (src_range.m_finish >= LINE_MAP_MAX_LOCATION_WITH_COLS)
+           /* If so, we can't reliably determine where the token finished
+              within its line.  */
+           return "range ends after LINE_MAP_MAX_LOCATION_WITH_COLS";
+       }
 
       expanded_location start
-       = expand_location_to_spelling_point (src_range.m_start);
+       = expand_location_to_spelling_point (src_range.m_start,
+                                            LOCATION_ASPECT_START);
       expanded_location finish
-       = expand_location_to_spelling_point (src_range.m_finish);
+       = expand_location_to_spelling_point (src_range.m_finish,
+                                            LOCATION_ASPECT_FINISH);
       if (start.file != finish.file)
        return "range endpoints are in different files";
       if (start.line != finish.line)
@@ -1351,34 +1451,51 @@ get_substring_ranges_for_loc (cpp_reader *pfile,
       if (start.column > finish.column)
        return "range endpoints are reversed";
 
-      int line_width;
-      const char *line = location_get_source_line (start.file, start.line,
-                                                  &line_width);
-      if (line == NULL)
+      char_span line = location_get_source_line (start.file, start.line);
+      if (!line)
        return "unable to read source line";
 
       /* Determine the location of the literal (including quotes
         and leading prefix chars, such as the 'u' in a u""
         token).  */
-      const char *literal = line + start.column - 1;
-      int literal_length = finish.column - start.column + 1;
+      size_t literal_length = finish.column - start.column + 1;
+
+      /* Ensure that we don't crash if we got the wrong location.  */
+      if (line.length () < (start.column - 1 + literal_length))
+       return "line is not wide enough";
+
+      char_span literal = line.subspan (start.column - 1, literal_length);
 
-      gcc_assert (line_width >= (start.column - 1 + literal_length));
       cpp_string from;
       from.len = literal_length;
       /* Make a copy of the literal, to avoid having to rely on
         the lifetime of the copy of the line within the cache.
         This will be released by the auto_cpp_string_vec dtor.  */
-      from.text = XDUPVEC (unsigned char, literal, literal_length);
+      from.text = (unsigned char *)literal.xstrdup ();
       strs.safe_push (from);
 
       /* For very long lines, a new linemap could have started
         halfway through the token.
         Ensure that the loc_reader uses the linemap of the
         *end* of the token for its start location.  */
+      const line_map_ordinary *start_ord_map;
+      linemap_resolve_location (line_table, src_range.m_start,
+                               LRK_SPELLING_LOCATION, &start_ord_map);
       const line_map_ordinary *final_ord_map;
       linemap_resolve_location (line_table, src_range.m_finish,
-                               LRK_MACRO_EXPANSION_POINT, &final_ord_map);
+                               LRK_SPELLING_LOCATION, &final_ord_map);
+      if (start_ord_map == NULL || final_ord_map == NULL)
+       return "failed to get ordinary maps";
+      /* Bulletproofing.  We ought to only have different ordinary maps
+        for start vs finish due to line-length jumps.  */
+      if (start_ord_map != final_ord_map
+         && start_ord_map->to_file != final_ord_map->to_file)
+       return "start and finish are spelled in different ordinary maps";
+      /* The file from linemap_resolve_location ought to match that from
+        expand_location_to_spelling_point.  */
+      if (start_ord_map->to_file != start.file)
+       return "mismatching file after resolving linemap";
+
       location_t start_loc
        = linemap_position_for_line_and_column (line_table, final_ord_map,
                                                start.line, start.column);
@@ -1398,10 +1515,17 @@ get_substring_ranges_for_loc (cpp_reader *pfile,
   return NULL;
 }
 
-/* Attempt to populate *OUT_RANGE with source location information on the
-   range of given characters within the string literal found at STRLOC.
-   START_IDX and END_IDX refer to offsets within the execution character
-   set.
+/* Attempt to populate *OUT_LOC with source location information on the
+   given characters within the string literal found at STRLOC.
+   CARET_IDX, START_IDX, and END_IDX refer to offsets within the execution
+   character set.
+
+   For example, given CARET_IDX = 4, START_IDX = 3, END_IDX  = 7
+   and string literal "012345\n789"
+   *OUT_LOC is written to with:
+     "012345\n789"
+         ~^~~~~
+
    If CONCATS is non-NULL, then any string literals that the token at
    STRLOC was concatenated with are also considered.
 
@@ -1412,16 +1536,17 @@ get_substring_ranges_for_loc (cpp_reader *pfile,
    than for end-users.  */
 
 const char *
-get_source_range_for_substring (cpp_reader *pfile,
-                               string_concat_db *concats,
-                               location_t strloc,
-                               enum cpp_ttype type,
-                               int start_idx, int end_idx,
-                               source_range *out_range)
-{
+get_location_within_string (cpp_reader *pfile,
+                           string_concat_db *concats,
+                           location_t strloc,
+                           enum cpp_ttype type,
+                           int caret_idx, int start_idx, int end_idx,
+                           location_t *out_loc)
+{
+  gcc_checking_assert (caret_idx >= 0);
   gcc_checking_assert (start_idx >= 0);
   gcc_checking_assert (end_idx >= 0);
-  gcc_assert (out_range);
+  gcc_assert (out_loc);
 
   cpp_substring_ranges ranges;
   const char *err
@@ -1429,20 +1554,65 @@ get_source_range_for_substring (cpp_reader *pfile,
   if (err)
     return err;
 
+  if (caret_idx >= ranges.get_num_ranges ())
+    return "caret_idx out of range";
   if (start_idx >= ranges.get_num_ranges ())
     return "start_idx out of range";
   if (end_idx >= ranges.get_num_ranges ())
     return "end_idx out of range";
 
-  out_range->m_start = ranges.get_range (start_idx).m_start;
-  out_range->m_finish = ranges.get_range (end_idx).m_finish;
+  *out_loc = make_location (ranges.get_range (caret_idx).m_start,
+                           ranges.get_range (start_idx).m_start,
+                           ranges.get_range (end_idx).m_finish);
+  return NULL;
+}
+
+#if CHECKING_P
+
+namespace selftest {
+
+/* Selftests of location handling.  */
+
+/* Attempt to populate *OUT_RANGE with source location information on the
+   given character within the string literal found at STRLOC.
+   CHAR_IDX refers to an offset within the execution character set.
+   If CONCATS is non-NULL, then any string literals that the token at
+   STRLOC was concatenated with are also considered.
+
+   This is implemented by re-parsing the relevant source line(s).
+
+   Return NULL if successful, or an error message if any errors occurred.
+   Error messages are intended for GCC developers (to help debugging) rather
+   than for end-users.  */
+
+static const char *
+get_source_range_for_char (cpp_reader *pfile,
+                          string_concat_db *concats,
+                          location_t strloc,
+                          enum cpp_ttype type,
+                          int char_idx,
+                          source_range *out_range)
+{
+  gcc_checking_assert (char_idx >= 0);
+  gcc_assert (out_range);
+
+  cpp_substring_ranges ranges;
+  const char *err
+    = get_substring_ranges_for_loc (pfile, concats, strloc, type, ranges);
+  if (err)
+    return err;
+
+  if (char_idx >= ranges.get_num_ranges ())
+    return "char_idx out of range";
+
+  *out_range = ranges.get_range (char_idx);
   return NULL;
 }
 
-/* As get_source_range_for_substring, but write to *OUT the number
+/* As get_source_range_for_char, but write to *OUT the number
    of ranges that are available.  */
 
-const char *
+static const char *
 get_num_source_ranges_for_substring (cpp_reader *pfile,
                                     string_concat_db *concats,
                                     location_t strloc,
@@ -1462,52 +1632,21 @@ get_num_source_ranges_for_substring (cpp_reader *pfile,
   return NULL;
 }
 
-#if CHECKING_P
-
-namespace selftest {
-
 /* Selftests of location handling.  */
 
-/* A class for writing out a temporary sourcefile for use in selftests
-   of input handling.  */
-
-class temp_source_file
-{
- public:
-  temp_source_file (const location &loc, const char *suffix,
-                   const char *content);
-  ~temp_source_file ();
-
-  const char *get_filename () const { return m_filename; }
+/* Verify that compare() on linenum_type handles comparisons over the full
+   range of the type.  */
 
- private:
-  char *m_filename;
-};
-
-/* Constructor.  Create a tempfile using SUFFIX, and write CONTENT to
-   it.  Abort if anything goes wrong, using LOC as the effective
-   location in the problem report.  */
-
-temp_source_file::temp_source_file (const location &loc, const char *suffix,
-                                   const char *content)
+static void
+test_linenum_comparisons ()
 {
-  m_filename = make_temp_file (suffix);
-  ASSERT_NE (m_filename, NULL);
+  linenum_type min_line (0);
+  linenum_type max_line (0xffffffff);
+  ASSERT_EQ (0, compare (min_line, min_line));
+  ASSERT_EQ (0, compare (max_line, max_line));
 
-  FILE *out = fopen (m_filename, "w");
-  if (!out)
-    ::selftest::fail_formatted (loc, "unable to open tempfile: %s",
-                               m_filename);
-  fprintf (out, "%s", content);
-  fclose (out);
-}
-
-/* Destructor.  Delete the tempfile.  */
-
-temp_source_file::~temp_source_file ()
-{
-  unlink (m_filename);
-  free (m_filename);
+  ASSERT_GT (compare (max_line, min_line), 0);
+  ASSERT_LT (compare (min_line, max_line), 0);
 }
 
 /* Helper function for verifying location data: when location_t
@@ -1554,15 +1693,15 @@ assert_loceq (const char *exp_filename, int exp_linenum, int exp_colnum,
     ASSERT_EQ (exp_colnum, LOCATION_COLUMN (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:
    - 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
+   values for 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.
@@ -1570,8 +1709,9 @@ assert_loceq (const char *exp_filename, int exp_linenum, int exp_colnum,
    The following struct describes a particular case within our test
    matrix.  */
 
-struct line_table_case
+class line_table_case
 {
+public:
   line_table_case (int default_range_bits, int base_location)
   : m_default_range_bits (default_range_bits),
     m_base_location (base_location)
@@ -1581,29 +1721,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)
     {
@@ -1614,9 +1760,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.  */
@@ -1624,7 +1772,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 ltt (case_);
 
   /* Build a simple linemap describing some locations. */
   linemap_add (line_table, LC_ENTER, false, "foo.c", 0);
@@ -1641,6 +1789,33 @@ test_accessing_ordinary_linemaps (const line_table_case &case_)
   linemap_line_start (line_table, 3, 2000);
   location_t loc_e = linemap_position_for_column (line_table, 700);
 
+  /* Transitioning back to a short line.  */
+  linemap_line_start (line_table, 4, 0);
+  location_t loc_back_to_short = linemap_position_for_column (line_table, 100);
+
+  if (should_have_column_data_p (loc_back_to_short))
+    {
+      /* Verify that we switched to short lines in the linemap.  */
+      line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
+      ASSERT_EQ (7, map->m_column_and_range_bits - map->m_range_bits);
+    }
+
+  /* Example of a line that will eventually be seen to be longer
+     than LINE_MAP_MAX_COLUMN_NUMBER; the initially seen width is
+     below that.  */
+  linemap_line_start (line_table, 5, 2000);
+
+  location_t loc_start_of_very_long_line
+    = linemap_position_for_column (line_table, 2000);
+  location_t loc_too_wide
+    = linemap_position_for_column (line_table, 4097);
+  location_t loc_too_wide_2
+    = linemap_position_for_column (line_table, 4098);
+
+  /* ...and back to a sane line length.  */
+  linemap_line_start (line_table, 6, 100);
+  location_t loc_sane_again = linemap_position_for_column (line_table, 10);
+
   linemap_add (line_table, LC_LEAVE, false, NULL, 0);
 
   /* Multiple files.  */
@@ -1655,6 +1830,17 @@ test_accessing_ordinary_linemaps (const line_table_case &case_)
   assert_loceq ("foo.c", 2, 1, loc_c);
   assert_loceq ("foo.c", 2, 17, loc_d);
   assert_loceq ("foo.c", 3, 700, loc_e);
+  assert_loceq ("foo.c", 4, 100, loc_back_to_short);
+
+  /* In the very wide line, the initial location should be fully tracked.  */
+  assert_loceq ("foo.c", 5, 2000, loc_start_of_very_long_line);
+  /* ...but once we exceed LINE_MAP_MAX_COLUMN_NUMBER column-tracking should
+     be disabled.  */
+  assert_loceq ("foo.c", 5, 0, loc_too_wide);
+  assert_loceq ("foo.c", 5, 0, loc_too_wide_2);
+  /*...and column-tracking should be re-enabled for subsequent lines.  */
+  assert_loceq ("foo.c", 6, 10, loc_sane_again);
+
   assert_loceq ("bar.c", 1, 150, loc_f);
 
   ASSERT_FALSE (is_location_from_builtin_token (loc_a));
@@ -1689,6 +1875,67 @@ test_builtins ()
   ASSERT_PRED1 (is_location_from_builtin_token, BUILTINS_LOCATION);
 }
 
+/* Regression test for make_location.
+   Ensure that we use pure locations for the start/finish of the range,
+   rather than storing a packed or ad-hoc range as the start/finish.  */
+
+static void
+test_make_location_nonpure_range_endpoints (const line_table_case &case_)
+{
+  /* Issue seen with testsuite/c-c++-common/Wlogical-not-parentheses-2.c
+     with C++ frontend.
+     ....................0000000001111111111222.
+     ....................1234567890123456789012.  */
+  const char *content = "     r += !aaa == bbb;\n";
+  temp_source_file tmp (SELFTEST_LOCATION, ".C", content);
+  line_table_test ltt (case_);
+  linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 1);
+
+  const location_t c11 = linemap_position_for_column (line_table, 11);
+  const location_t c12 = linemap_position_for_column (line_table, 12);
+  const location_t c13 = linemap_position_for_column (line_table, 13);
+  const location_t c14 = linemap_position_for_column (line_table, 14);
+  const location_t c21 = linemap_position_for_column (line_table, 21);
+
+  if (c21 > LINE_MAP_MAX_LOCATION_WITH_COLS)
+    return;
+
+  /* Use column 13 for the caret location, arbitrarily, to verify that we
+     handle start != caret.  */
+  const location_t aaa = make_location (c13, c12, c14);
+  ASSERT_EQ (c13, get_pure_location (aaa));
+  ASSERT_EQ (c12, get_start (aaa));
+  ASSERT_FALSE (IS_ADHOC_LOC (get_start (aaa)));
+  ASSERT_EQ (c14, get_finish (aaa));
+  ASSERT_FALSE (IS_ADHOC_LOC (get_finish (aaa)));
+
+  /* Make a location using a location with a range as the start-point.  */
+  const location_t not_aaa = make_location (c11, aaa, c14);
+  ASSERT_EQ (c11, get_pure_location (not_aaa));
+  /* It should use the start location of the range, not store the range
+     itself.  */
+  ASSERT_EQ (c12, get_start (not_aaa));
+  ASSERT_FALSE (IS_ADHOC_LOC (get_start (not_aaa)));
+  ASSERT_EQ (c14, get_finish (not_aaa));
+  ASSERT_FALSE (IS_ADHOC_LOC (get_finish (not_aaa)));
+
+  /* Similarly, make a location with a range as the end-point.  */
+  const location_t aaa_eq_bbb = make_location (c12, c12, c21);
+  ASSERT_EQ (c12, get_pure_location (aaa_eq_bbb));
+  ASSERT_EQ (c12, get_start (aaa_eq_bbb));
+  ASSERT_FALSE (IS_ADHOC_LOC (get_start (aaa_eq_bbb)));
+  ASSERT_EQ (c21, get_finish (aaa_eq_bbb));
+  ASSERT_FALSE (IS_ADHOC_LOC (get_finish (aaa_eq_bbb)));
+  const location_t not_aaa_eq_bbb = make_location (c11, c12, aaa_eq_bbb);
+  /* It should use the finish location of the range, not store the range
+     itself.  */
+  ASSERT_EQ (c11, get_pure_location (not_aaa_eq_bbb));
+  ASSERT_EQ (c12, get_start (not_aaa_eq_bbb));
+  ASSERT_FALSE (IS_ADHOC_LOC (get_start (not_aaa_eq_bbb)));
+  ASSERT_EQ (c21, get_finish (not_aaa_eq_bbb));
+  ASSERT_FALSE (IS_ADHOC_LOC (get_finish (not_aaa_eq_bbb)));
+}
+
 /* Verify reading of input files (e.g. for caret-based diagnostics).  */
 
 static void
@@ -1698,22 +1945,26 @@ test_reading_source_line ()
   temp_source_file tmp (SELFTEST_LOCATION, ".txt",
                        "01234567890123456789\n"
                        "This is the test text\n"
-                       "This is the 3rd line\n");
+                       "This is the 3rd line");
 
   /* Read back a specific line from the tempfile.  */
-  int line_size;
-  const char *source_line = location_get_source_line (tmp.get_filename (),
-                                                     2, &line_size);
-  ASSERT_TRUE (source_line != NULL);
-  ASSERT_EQ (21, line_size);
-  if (!strncmp ("This is the test text",
-               source_line, line_size))
-    ::selftest::pass (SELFTEST_LOCATION,
-                     "source_line matched expected value");
-  else
-    ::selftest::fail (SELFTEST_LOCATION,
-                     "source_line did not match expected value");
+  char_span source_line = location_get_source_line (tmp.get_filename (), 3);
+  ASSERT_TRUE (source_line);
+  ASSERT_TRUE (source_line.get_buffer () != NULL);
+  ASSERT_EQ (20, source_line.length ());
+  ASSERT_TRUE (!strncmp ("This is the 3rd line",
+                        source_line.get_buffer (), source_line.length ()));
+
+  source_line = location_get_source_line (tmp.get_filename (), 2);
+  ASSERT_TRUE (source_line);
+  ASSERT_TRUE (source_line.get_buffer () != NULL);
+  ASSERT_EQ (21, source_line.length ());
+  ASSERT_TRUE (!strncmp ("This is the test text",
+                        source_line.get_buffer (), source_line.length ()));
 
+  source_line = location_get_source_line (tmp.get_filename (), 4);
+  ASSERT_FALSE (source_line);
+  ASSERT_TRUE (source_line.get_buffer () == NULL);
 }
 
 /* Tests of lexing.  */
@@ -1776,7 +2027,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 ltt (case_);
 
   cpp_reader *parser = cpp_create_reader (CLK_GNUC89, NULL, line_table);
 
@@ -1816,7 +2067,7 @@ test_lexer (const line_table_case &case_)
 
 /* Forward decls.  */
 
-struct lexer_test;
+class lexer_test;
 class lexer_test_options;
 
 /* A class for specifying options of a lexer_test.
@@ -1828,20 +2079,52 @@ class lexer_test_options
   virtual void apply (lexer_test &) = 0;
 };
 
+/* Wrapper around an cpp_reader *, which calls cpp_finish and cpp_destroy
+   in its dtor.
+
+   This is needed by struct lexer_test to ensure that the cleanup of the
+   cpp_reader happens *after* the cleanup of the temp_source_file.  */
+
+class cpp_reader_ptr
+{
+ public:
+  cpp_reader_ptr (cpp_reader *ptr) : m_ptr (ptr) {}
+
+  ~cpp_reader_ptr ()
+  {
+    cpp_finish (m_ptr, NULL);
+    cpp_destroy (m_ptr);
+  }
+
+  operator cpp_reader * () const { return m_ptr; }
+
+ private:
+  cpp_reader *m_ptr;
+};
+
 /* A struct for writing lexer tests.  */
 
-struct lexer_test
+class lexer_test
 {
+public:
   lexer_test (const line_table_case &case_, const char *content,
              lexer_test_options *options);
   ~lexer_test ();
 
   const cpp_token *get_token ();
 
+  /* The ordering of these fields matters.
+     The line_table_test must be first, since the cpp_reader_ptr
+     uses it.
+     The cpp_reader must be cleaned up *after* the temp_source_file
+     since the filenames in input.c's input cache are owned by the
+     cpp_reader; in particular, when ~temp_source_file evicts the
+     filename the filenames must still be alive.  */
+  line_table_test m_ltt;
+  cpp_reader_ptr m_parser;
   temp_source_file m_tempfile;
-  temp_line_table m_tmp_lt;
-  cpp_reader *m_parser;
   string_concat_db m_concats;
+  bool m_implicitly_expect_EOF;
 };
 
 /* Use an EBCDIC encoding for the execution charset, specifically
@@ -1871,20 +2154,25 @@ class ebcdic_execution_charset : public lexer_test_options
     cpp_opts->narrow_charset = "IBM1047";
 
     cpp_callbacks *callbacks = cpp_get_callbacks (test.m_parser);
-    callbacks->error = on_error;
+    callbacks->diagnostic = on_diagnostic;
   }
 
-  static bool on_error (cpp_reader *pfile ATTRIBUTE_UNUSED,
-                       int level ATTRIBUTE_UNUSED,
-                       int reason ATTRIBUTE_UNUSED,
-                       rich_location *richloc ATTRIBUTE_UNUSED,
-                       const char *msgid, va_list *ap ATTRIBUTE_UNUSED)
+  static bool on_diagnostic (cpp_reader *pfile ATTRIBUTE_UNUSED,
+                            enum cpp_diagnostic_level level ATTRIBUTE_UNUSED,
+                            enum cpp_warning_reason reason ATTRIBUTE_UNUSED,
+                            rich_location *richloc ATTRIBUTE_UNUSED,
+                            const char *msgid, va_list *ap ATTRIBUTE_UNUSED)
     ATTRIBUTE_FPTR_PRINTF(5,0)
   {
     gcc_assert (s_singleton);
+    /* Avoid exgettext from picking this up, it is translated in libcpp.  */
+    const char *msg = "conversion from %s to %s not supported by iconv";
+#ifdef ENABLE_NLS
+    msg = dgettext ("cpplib", msg);
+#endif
     /* Detect and record errors emitted by libcpp/charset.c:init_iconv_desc
        when the local iconv build doesn't support the conversion.  */
-    if (strstr (msgid, "not supported by iconv"))
+    if (strcmp (msgid, msg) == 0)
       {
        s_singleton->m_num_iconv_errors++;
        return true;
@@ -1903,17 +2191,66 @@ class ebcdic_execution_charset : public lexer_test_options
 
 ebcdic_execution_charset *ebcdic_execution_charset::s_singleton;
 
+/* A lexer_test_options subclass that records a list of diagnostic
+   messages emitted by the lexer.  */
+
+class lexer_diagnostic_sink : public lexer_test_options
+{
+ public:
+  lexer_diagnostic_sink ()
+  {
+    gcc_assert (s_singleton == NULL);
+    s_singleton = this;
+  }
+  ~lexer_diagnostic_sink ()
+  {
+    gcc_assert (s_singleton == this);
+    s_singleton = NULL;
+
+    int i;
+    char *str;
+    FOR_EACH_VEC_ELT (m_diagnostics, i, str)
+      free (str);
+  }
+
+  void apply (lexer_test &test) FINAL OVERRIDE
+  {
+    cpp_callbacks *callbacks = cpp_get_callbacks (test.m_parser);
+    callbacks->diagnostic = on_diagnostic;
+  }
+
+  static bool on_diagnostic (cpp_reader *pfile ATTRIBUTE_UNUSED,
+                            enum cpp_diagnostic_level level ATTRIBUTE_UNUSED,
+                            enum cpp_warning_reason reason ATTRIBUTE_UNUSED,
+                            rich_location *richloc ATTRIBUTE_UNUSED,
+                            const char *msgid, va_list *ap)
+    ATTRIBUTE_FPTR_PRINTF(5,0)
+  {
+    char *msg = xvasprintf (msgid, *ap);
+    s_singleton->m_diagnostics.safe_push (msg);
+    return true;
+  }
+
+  auto_vec<char *> m_diagnostics;
+
+ private:
+  static lexer_diagnostic_sink *s_singleton;
+};
+
+lexer_diagnostic_sink *lexer_diagnostic_sink::s_singleton;
+
 /* Constructor.  Override line_table with a new instance based on CASE_,
    and write CONTENT to a tempfile.  Create a cpp_reader, and use it to
    start parsing the tempfile.  */
 
 lexer_test::lexer_test (const line_table_case &case_, const char *content,
-                       lexer_test_options *options) :
+                       lexer_test_options *options)
+: m_ltt (case_),
+  m_parser (cpp_create_reader (CLK_GNUC99, NULL, line_table)),
   /* Create a tempfile and write the text to it.  */
   m_tempfile (SELFTEST_LOCATION, ".c", content),
-  m_tmp_lt (case_),
-  m_parser (cpp_create_reader (CLK_GNUC99, NULL, line_table)),
-  m_concats ()
+  m_concats (),
+  m_implicitly_expect_EOF (true)
 {
   if (options)
     options->apply (*this);
@@ -1926,19 +2263,19 @@ lexer_test::lexer_test (const line_table_case &case_, const char *content,
   ASSERT_NE (fname, NULL);
 }
 
-/* Destructor.  Verify that the next token in m_parser is EOF.  */
+/* Destructor.  By default, verify that the next token in m_parser is EOF.  */
 
 lexer_test::~lexer_test ()
 {
   location_t loc;
   const cpp_token *tok;
 
-  tok = cpp_get_token_with_location (m_parser, &loc);
-  ASSERT_NE (tok, NULL);
-  ASSERT_EQ (tok->type, CPP_EOF);
-
-  cpp_finish (m_parser, NULL);
-  cpp_destroy (m_parser);
+  if (m_implicitly_expect_EOF)
+    {
+      tok = cpp_get_token_with_location (m_parser, &loc);
+      ASSERT_NE (tok, NULL);
+      ASSERT_EQ (tok->type, CPP_EOF);
+    }
 }
 
 /* Get the next token from m_parser.  */
@@ -1975,10 +2312,10 @@ assert_char_at_range (const location &loc,
   cpp_reader *pfile = test.m_parser;
   string_concat_db *concats = &test.m_concats;
 
-  source_range actual_range;
+  source_range actual_range = source_range();
   const char *err
-    = get_source_range_for_substring (pfile, concats, strloc, type,
-                                     idx, idx, &actual_range);
+    = get_source_range_for_char (pfile, concats, strloc, type, idx,
+                                &actual_range);
   if (should_have_column_data_p (strloc))
     ASSERT_EQ_AT (loc, NULL, err);
   else
@@ -2030,7 +2367,7 @@ assert_num_substring_ranges (const location &loc,
   cpp_reader *pfile = test.m_parser;
   string_concat_db *concats = &test.m_concats;
 
-  int actual_num_ranges;
+  int actual_num_ranges = -1;
   const char *err
     = get_num_source_ranges_for_substring (pfile, concats, strloc, type,
                                           &actual_num_ranges);
@@ -2119,12 +2456,12 @@ test_lexer_string_locations_simple (const line_table_case &case_)
   free (const_cast <unsigned char *> (dst_string.text));
 
   /* Verify ranges of individual characters.  This no longer includes the
-     quotes.  */
-  for (int i = 0; i <= 9; i++)
+     opening quote, but does include the closing quote.  */
+  for (int i = 0; i <= 10; i++)
     ASSERT_CHAR_AT_RANGE (test, tok->src_loc, type, i, 1,
                          10 + i, 10 + i);
 
-  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, type, 10);
+  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, type, 11);
 }
 
 /* As test_lexer_string_locations_simple, but use an EBCDIC execution
@@ -2219,14 +2556,14 @@ test_lexer_string_locations_hex (const line_table_case &case_)
   free (const_cast <unsigned char *> (dst_string.text));
 
   /* Verify ranges of individual characters.  This no longer includes the
-     quotes.  */
+     opening quote, but does include the closing quote.  */
   for (int i = 0; i <= 4; i++)
     ASSERT_CHAR_AT_RANGE (test, tok->src_loc, type, i, 1, 10 + i, 10 + i);
   ASSERT_CHAR_AT_RANGE (test, tok->src_loc, type, 5, 1, 15, 18);
-  for (int i = 6; i <= 9; i++)
+  for (int i = 6; i <= 10; i++)
     ASSERT_CHAR_AT_RANGE (test, tok->src_loc, type, i, 1, 13 + i, 13 + i);
 
-  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, type, 10);
+  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, type, 11);
 }
 
 /* Lex a string literal containing an octal-escaped character.
@@ -2260,14 +2597,14 @@ test_lexer_string_locations_oct (const line_table_case &case_)
   free (const_cast <unsigned char *> (dst_string.text));
 
   /* Verify ranges of individual characters.  This no longer includes the
-     quotes.  */
+     opening quote, but does include the closing quote.  */
   for (int i = 0; i < 5; i++)
     ASSERT_CHAR_AT_RANGE (test, tok->src_loc, type, i, 1, 10 + i, 10 + i);
   ASSERT_CHAR_AT_RANGE (test, tok->src_loc, type, 5, 1, 15, 18);
-  for (int i = 6; i <= 9; i++)
+  for (int i = 6; i <= 10; i++)
     ASSERT_CHAR_AT_RANGE (test, tok->src_loc, type, i, 1, 13 + i, 13 + i);
 
-  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, type, 10);
+  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, type, 11);
 }
 
 /* Test of string literal containing letter escapes.  */
@@ -2300,12 +2637,12 @@ test_lexer_string_locations_letter_escape_1 (const line_table_case &case_)
   ASSERT_CHAR_AT_RANGE (test, tok->src_loc, CPP_STRING,
                        5, 1, 17, 18);
 
-  /* "bar".  */
-  for (int i = 6; i <= 8; i++)
+  /* "bar" and closing quote for nul-terminator.  */
+  for (int i = 6; i <= 9; i++)
     ASSERT_CHAR_AT_RANGE (test, tok->src_loc, CPP_STRING,
                          i, 1, 13 + i, 13 + i);
 
-  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, CPP_STRING, 9);
+  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, CPP_STRING, 10);
 }
 
 /* Another test of a string literal containing a letter escape.
@@ -2335,7 +2672,11 @@ test_lexer_string_locations_letter_escape_2 (const line_table_case &case_)
   ASSERT_CHAR_AT_RANGE (test, tok->src_loc, CPP_STRING,
                        3, 1, 13, 14);
 
-  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, CPP_STRING, 4);
+  /* Closing quote for nul-terminator.  */
+  ASSERT_CHAR_AT_RANGE (test, tok->src_loc, CPP_STRING,
+                       4, 1, 15, 15);
+
+  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, CPP_STRING, 5);
 }
 
 /* Lex a string literal containing UCN 4 characters.
@@ -2360,7 +2701,7 @@ test_lexer_string_locations_ucn4 (const line_table_case &case_)
 
   /* Verify that cpp_interpret_string works.
      The string should be encoded in the execution character
-     set.  Assuming that that is UTF-8, we should have the following:
+     set.  Assuming that is UTF-8, we should have the following:
      -----------  ----  -----  -------  ----------------
      Byte offset  Byte  Octal  Unicode  Source Column(s)
      -----------  ----  -----  -------  ----------------
@@ -2378,6 +2719,7 @@ test_lexer_string_locations_ucn4 (const line_table_case &case_)
      11           0x37         '7'      27
      12           0x38         '8'      28
      13           0x39         '9'      29
+     14           0x00                  30 (closing quote)
      -----------  ----  -----  -------  ---------------.  */
 
   cpp_string dst_string;
@@ -2390,7 +2732,7 @@ test_lexer_string_locations_ucn4 (const line_table_case &case_)
   free (const_cast <unsigned char *> (dst_string.text));
 
   /* Verify ranges of individual characters.  This no longer includes the
-     quotes.
+     opening quote, but does include the closing quote.
      '01234'.  */
   for (int i = 0; i <= 4; i++)
     ASSERT_CHAR_AT_RANGE (test, tok->src_loc, type, i, 1, 10 + i, 10 + i);
@@ -2400,11 +2742,11 @@ test_lexer_string_locations_ucn4 (const line_table_case &case_)
   /* U+2175.  */
   for (int i = 8; i <= 10; i++)
     ASSERT_CHAR_AT_RANGE (test, tok->src_loc, type, i, 1, 21, 26);
-  /* '789'.  */
-  for (int i = 11; i <= 13; i++)
+  /* '789' and nul terminator  */
+  for (int i = 11; i <= 14; i++)
     ASSERT_CHAR_AT_RANGE (test, tok->src_loc, type, i, 1, 16 + i, 16 + i);
 
-  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, type, 14);
+  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, type, 15);
 }
 
 /* Lex a string literal containing UCN 8 characters.
@@ -2441,7 +2783,7 @@ test_lexer_string_locations_ucn8 (const line_table_case &case_)
   free (const_cast <unsigned char *> (dst_string.text));
 
   /* Verify ranges of individual characters.  This no longer includes the
-     quotes.
+     opening quote, but does include the closing quote.
      '01234'.  */
   for (int i = 0; i <= 4; i++)
     ASSERT_CHAR_AT_RANGE (test, tok->src_loc, type, i, 1, 10 + i, 10 + i);
@@ -2454,8 +2796,10 @@ test_lexer_string_locations_ucn8 (const line_table_case &case_)
   /* '789' at columns 35-37  */
   for (int i = 11; i <= 13; i++)
     ASSERT_CHAR_AT_RANGE (test, tok->src_loc, type, i, 1, 24 + i, 24 + i);
+  /* Closing quote/nul-terminator at column 38.  */
+  ASSERT_CHAR_AT_RANGE (test, tok->src_loc, type, 14, 1, 38, 38);
 
-  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, type, 14);
+  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, type, 15);
 }
 
 /* Fetch a big-endian 32-bit value and convert to host endianness.  */
@@ -2631,8 +2975,8 @@ test_lexer_string_locations_u8 (const line_table_case &case_)
   free (const_cast <unsigned char *> (dst_string.text));
 
   /* Verify ranges of individual characters.  This no longer includes the
-     quotes.  */
-  for (int i = 0; i <= 9; i++)
+     opening quote, but does include the closing quote.  */
+  for (int i = 0; i <= 10; i++)
     ASSERT_CHAR_AT_RANGE (test, tok->src_loc, type, i, 1, 10 + i, 10 + i);
 }
 
@@ -2709,13 +3053,15 @@ test_lexer_string_locations_utf8_source (const line_table_case &case_)
   free (const_cast <unsigned char *> (dst_string.text));
 
   /* Verify ranges of individual characters.  This no longer includes the
-     quotes.
+     opening quote, but does include the closing quote.
      Assuming that both source and execution encodings are UTF-8, we have
-     a run of 25 octets in each.  */
+     a run of 25 octets in each, plus the NUL terminator.  */
   for (int i = 0; i < 25; i++)
     ASSERT_CHAR_AT_RANGE (test, tok->src_loc, type, i, 1, 10 + i, 10 + i);
+  /* NUL-terminator should use the closing quote at column 35.  */
+  ASSERT_CHAR_AT_RANGE (test, tok->src_loc, type, 25, 1, 35, 35);
 
-  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, type, 25);
+  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, type, 26);
 }
 
 /* Test of string literal concatenation.  */
@@ -2761,12 +3107,14 @@ test_lexer_string_locations_concatenation_1 (const line_table_case &case_)
 
   location_t initial_loc = input_locs[0];
 
+  /* "01234" on line 1.  */
   for (int i = 0; i <= 4; i++)
     ASSERT_CHAR_AT_RANGE (test, initial_loc, type, i, 1, 10 + i, 10 + i);
-  for (int i = 5; i <= 9; i++)
+  /* "56789" in line 2, plus its closing quote for the nul terminator.  */
+  for (int i = 5; i <= 10; i++)
     ASSERT_CHAR_AT_RANGE (test, initial_loc, type, i, 2, 5 + i, 5 + i);
 
-  ASSERT_NUM_SUBSTRING_RANGES (test, initial_loc, type, 10);
+  ASSERT_NUM_SUBSTRING_RANGES (test, initial_loc, type, 11);
 }
 
 /* Another test of string literal concatenation.  */
@@ -2827,9 +3175,8 @@ test_lexer_string_locations_concatenation_2 (const line_table_case &case_)
         this case.  */
       source_range actual_range;
       const char *err
-       = get_source_range_for_substring (test.m_parser, &test.m_concats,
-                                         initial_loc, type, 0, 0,
-                                         &actual_range);
+       = get_source_range_for_char (test.m_parser, &test.m_concats,
+                                    initial_loc, type, 0, &actual_range);
       ASSERT_STREQ ("range starts after LINE_MAP_MAX_LOCATION_WITH_COLS", err);
       return;
     }
@@ -2839,7 +3186,10 @@ test_lexer_string_locations_concatenation_2 (const line_table_case &case_)
       ASSERT_CHAR_AT_RANGE (test, initial_loc, type, (i * 2) + j,
                            i + 1, 10 + j, 10 + j);
 
-  ASSERT_NUM_SUBSTRING_RANGES (test, initial_loc, type, 10);
+  /* NUL-terminator should use the final closing quote at line 5 column 12.  */
+  ASSERT_CHAR_AT_RANGE (test, initial_loc, type, 10, 5, 12, 12);
+
+  ASSERT_NUM_SUBSTRING_RANGES (test, initial_loc, type, 11);
 }
 
 /* Another test of string literal concatenation, this time combined with
@@ -2890,7 +3240,10 @@ test_lexer_string_locations_concatenation_3 (const line_table_case &case_)
   for (int i = 7; i <= 9; i++)
     ASSERT_CHAR_AT_RANGE (test, initial_loc, type, i, 1, 28 + i, 28 + i);
 
-  ASSERT_NUM_SUBSTRING_RANGES (test, initial_loc, type, 10);
+  /* NUL-terminator should use the location of the final closing quote.  */
+  ASSERT_CHAR_AT_RANGE (test, initial_loc, type, 10, 1, 38, 38);
+
+  ASSERT_NUM_SUBSTRING_RANGES (test, initial_loc, type, 11);
 }
 
 /* Test of string literal in a macro.  */
@@ -2915,11 +3268,11 @@ test_lexer_string_locations_macro (const line_table_case &case_)
 
   /* Verify ranges of individual characters.  We ought to
      see columns within the macro definition.  */
-  for (int i = 0; i <= 9; i++)
+  for (int i = 0; i <= 10; i++)
     ASSERT_CHAR_AT_RANGE (test, tok->src_loc, CPP_STRING,
                          i, 1, 20 + i, 20 + i);
 
-  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, CPP_STRING, 10);
+  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, CPP_STRING, 11);
 
   tok = test.get_token ();
   ASSERT_EQ (tok->type, CPP_PADDING);
@@ -3013,12 +3366,109 @@ test_lexer_string_locations_long_line (const line_table_case &case_)
     return;
 
   /* Verify ranges of individual characters.  */
-  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, CPP_STRING, 130);
-  for (int i = 0; i < 130; i++)
+  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, CPP_STRING, 131);
+  for (int i = 0; i < 131; i++)
     ASSERT_CHAR_AT_RANGE (test, tok->src_loc, CPP_STRING,
                          i, 2, 7 + i, 7 + i);
 }
 
+/* Test of locations within a raw string that doesn't contain a newline.  */
+
+static void
+test_lexer_string_locations_raw_string_one_line (const line_table_case &case_)
+{
+  /* .....................00.0000000111111111122.
+     .....................12.3456789012345678901.  */
+  const char *content = ("R\"foo(0123456789)foo\"\n");
+  lexer_test test (case_, content, NULL);
+
+  /* Verify that we get the expected token back.  */
+  const cpp_token *tok = test.get_token ();
+  ASSERT_EQ (tok->type, CPP_STRING);
+
+  /* Verify that cpp_interpret_string works.  */
+  cpp_string dst_string;
+  const enum cpp_ttype type = CPP_STRING;
+  bool result = cpp_interpret_string (test.m_parser, &tok->val.str, 1,
+                                     &dst_string, type);
+  ASSERT_TRUE (result);
+  ASSERT_STREQ ("0123456789", (const char *)dst_string.text);
+  free (const_cast <unsigned char *> (dst_string.text));
+
+  if (!should_have_column_data_p (line_table->highest_location))
+    return;
+
+  /* 0-9, plus the nil terminator.  */
+  ASSERT_NUM_SUBSTRING_RANGES (test, tok->src_loc, CPP_STRING, 11);
+  for (int i = 0; i < 11; i++)
+    ASSERT_CHAR_AT_RANGE (test, tok->src_loc, CPP_STRING,
+                         i, 1, 7 + i, 7 + i);
+}
+
+/* Test of locations within a raw string that contains a newline.  */
+
+static void
+test_lexer_string_locations_raw_string_multiline (const line_table_case &case_)
+{
+  /* .....................00.0000.
+     .....................12.3456.  */
+  const char *content = ("R\"foo(\n"
+  /* .....................00000.
+     .....................12345.  */
+                        "hello\n"
+                        "world\n"
+  /* .....................00000.
+     .....................12345.  */
+                        ")foo\"\n");
+  lexer_test test (case_, content, NULL);
+
+  /* Verify that we get the expected token back.  */
+  const cpp_token *tok = test.get_token ();
+  ASSERT_EQ (tok->type, CPP_STRING);
+
+  /* Verify that cpp_interpret_string works.  */
+  cpp_string dst_string;
+  const enum cpp_ttype type = CPP_STRING;
+  bool result = cpp_interpret_string (test.m_parser, &tok->val.str, 1,
+                                     &dst_string, type);
+  ASSERT_TRUE (result);
+  ASSERT_STREQ ("\nhello\nworld\n", (const char *)dst_string.text);
+  free (const_cast <unsigned char *> (dst_string.text));
+
+  if (!should_have_column_data_p (line_table->highest_location))
+    return;
+
+  /* Currently we don't support locations within raw strings that
+     contain newlines.  */
+  ASSERT_HAS_NO_SUBSTRING_RANGES (test, tok->src_loc, tok->type,
+                                 "range endpoints are on different lines");
+}
+
+/* Test of parsing an unterminated raw string.  */
+
+static void
+test_lexer_string_locations_raw_string_unterminated (const line_table_case &case_)
+{
+  const char *content = "R\"ouch()ouCh\" /* etc */";
+
+  lexer_diagnostic_sink diagnostics;
+  lexer_test test (case_, content, &diagnostics);
+  test.m_implicitly_expect_EOF = false;
+
+  /* Attempt to parse the raw string.  */
+  const cpp_token *tok = test.get_token ();
+  ASSERT_EQ (tok->type, CPP_EOF);
+
+  ASSERT_EQ (1, diagnostics.m_diagnostics.length ());
+  /* We expect the message "unterminated raw string"
+     in the "cpplib" translation domain.
+     It's not clear that dgettext is available on all supported hosts,
+     so this assertion is commented-out for now.
+       ASSERT_STREQ (dgettext ("cpplib", "unterminated raw string"),
+                     diagnostics.m_diagnostics[0]);
+  */
+}
+
 /* Test of lexing char constants.  */
 
 static void
@@ -3093,15 +3543,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
@@ -3122,30 +3568,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);
-         test_lexer_string_locations_simple (c);
-         test_lexer_string_locations_ebcdic (c);
-         test_lexer_string_locations_hex (c);
-         test_lexer_string_locations_oct (c);
-         test_lexer_string_locations_letter_escape_1 (c);
-         test_lexer_string_locations_letter_escape_2 (c);
-         test_lexer_string_locations_ucn4 (c);
-         test_lexer_string_locations_ucn8 (c);
-         test_lexer_string_locations_wide_string (c);
-         test_lexer_string_locations_string16 (c);
-         test_lexer_string_locations_string32 (c);
-         test_lexer_string_locations_u8 (c);
-         test_lexer_string_locations_utf8_source (c);
-         test_lexer_string_locations_concatenation_1 (c);
-         test_lexer_string_locations_concatenation_2 (c);
-         test_lexer_string_locations_concatenation_3 (c);
-         test_lexer_string_locations_macro (c);
-         test_lexer_string_locations_stringified_macro_argument (c);
-         test_lexer_string_locations_non_string (c);
-         test_lexer_string_locations_long_line (c);
-         test_lexer_char_constants (c);
+         testcase (c);
 
          num_cases_tested++;
        }
@@ -3153,8 +3576,166 @@ input_c_tests ()
 
   /* Verify that we fully covered the test matrix.  */
   ASSERT_EQ (num_cases_tested, 2 * 12);
+}
+
+/* Verify that when presented with a consecutive pair of locations with
+   a very large line offset, we don't attempt to consolidate them into
+   a single ordinary linemap where the line offsets within the line map
+   would lead to overflow (PR lto/88147).  */
+
+static void
+test_line_offset_overflow ()
+{
+  line_table_test ltt (line_table_case (5, 0));
+
+  linemap_add (line_table, LC_ENTER, false, "foo.c", 0);
+  linemap_line_start (line_table, 1, 100);
+  location_t loc_a = linemap_line_start (line_table, 2578, 255);
+  assert_loceq ("foo.c", 2578, 0, loc_a);
+
+  const line_map_ordinary *ordmap_a = LINEMAPS_LAST_ORDINARY_MAP (line_table);
+  ASSERT_EQ (ordmap_a->m_column_and_range_bits, 13);
+  ASSERT_EQ (ordmap_a->m_range_bits, 5);
+
+  location_t loc_b = linemap_line_start (line_table, 404198, 512);
+  assert_loceq ("foo.c", 404198, 0, loc_b);
+
+  /* We should have started a new linemap, rather than attempting to store
+     a very large line offset.  */
+  const line_map_ordinary *ordmap_b = LINEMAPS_LAST_ORDINARY_MAP (line_table);
+  ASSERT_NE (ordmap_a, ordmap_b);
+}
+
+void test_cpp_utf8 ()
+{
+  /* Verify that wcwidth of invalid UTF-8 or control bytes is 1.  */
+  {
+    int w_bad = cpp_display_width ("\xf0!\x9f!\x98!\x82!", 8);
+    ASSERT_EQ (8, w_bad);
+    int w_ctrl = cpp_display_width ("\r\t\n\v\0\1", 6);
+    ASSERT_EQ (6, w_ctrl);
+  }
+
+  /* Verify that wcwidth of valid UTF-8 is as expected.  */
+  {
+    const int w_pi = cpp_display_width ("\xcf\x80", 2);
+    ASSERT_EQ (1, w_pi);
+    const int w_emoji = cpp_display_width ("\xf0\x9f\x98\x82", 4);
+    ASSERT_EQ (2, w_emoji);
+    const int w_umlaut_precomposed = cpp_display_width ("\xc3\xbf", 2);
+    ASSERT_EQ (1, w_umlaut_precomposed);
+    const int w_umlaut_combining = cpp_display_width ("y\xcc\x88", 3);
+    ASSERT_EQ (1, w_umlaut_combining);
+    const int w_han = cpp_display_width ("\xe4\xb8\xba", 3);
+    ASSERT_EQ (2, w_han);
+    const int w_ascii = cpp_display_width ("GCC", 3);
+    ASSERT_EQ (3, w_ascii);
+    const int w_mixed = cpp_display_width ("\xcf\x80 = 3.14 \xf0\x9f\x98\x82"
+                                          "\x9f! \xe4\xb8\xba y\xcc\x88", 24);
+    ASSERT_EQ (18, w_mixed);
+  }
+
+  /* Verify that cpp_byte_column_to_display_column can go past the end,
+     and similar edge cases.  */
+  {
+    const char *str
+      /* Display columns.
+         111111112345  */
+      = "\xcf\x80 abc";
+      /* 111122223456
+        Byte columns.  */
+
+    ASSERT_EQ (5, cpp_display_width (str, 6));
+    ASSERT_EQ (105, cpp_byte_column_to_display_column (str, 6, 106));
+    ASSERT_EQ (10000, cpp_byte_column_to_display_column (NULL, 0, 10000));
+    ASSERT_EQ (0, cpp_byte_column_to_display_column (NULL, 10000, 0));
+  }
+
+  /* Verify that cpp_display_column_to_byte_column can go past the end,
+     and similar edge cases, and check invertibility.  */
+  {
+    const char *str
+      /* Display columns.
+        000000000000000000000000000000000000011
+        111111112222222234444444455555555678901  */
+      = "\xf0\x9f\x98\x82 \xf0\x9f\x98\x82 hello";
+      /* 000000000000000000000000000000000111111
+        111122223333444456666777788889999012345
+        Byte columns.  */
+    ASSERT_EQ (4, cpp_display_column_to_byte_column (str, 15, 2));
+    ASSERT_EQ (15, cpp_display_column_to_byte_column (str, 15, 11));
+    ASSERT_EQ (115, cpp_display_column_to_byte_column (str, 15, 111));
+    ASSERT_EQ (10000, cpp_display_column_to_byte_column (NULL, 0, 10000));
+    ASSERT_EQ (0, cpp_display_column_to_byte_column (NULL, 10000, 0));
+
+    /* Verify that we do not interrupt a UTF-8 sequence.  */
+    ASSERT_EQ (4, cpp_display_column_to_byte_column (str, 15, 1));
+
+    for (int byte_col = 1; byte_col <= 15; ++byte_col)
+      {
+       const int disp_col = cpp_byte_column_to_display_column (str, 15,
+                                                               byte_col);
+       const int byte_col2 = cpp_display_column_to_byte_column (str, 15,
+                                                                disp_col);
+
+       /* If we ask for the display column in the middle of a UTF-8
+          sequence, it will return the length of the partial sequence,
+          matching the behavior of GCC before display column support.
+          Otherwise check the round trip was successful.  */
+       if (byte_col < 4)
+         ASSERT_EQ (byte_col, disp_col);
+       else if (byte_col >= 6 && byte_col < 9)
+         ASSERT_EQ (3 + (byte_col - 5), disp_col);
+       else
+         ASSERT_EQ (byte_col2, byte_col);
+      }
+  }
+
+}
+
+/* Run all of the selftests within this file.  */
+
+void
+input_c_tests ()
+{
+  test_linenum_comparisons ();
+  test_should_have_column_data_p ();
+  test_unknown_location ();
+  test_builtins ();
+  for_each_line_table_case (test_make_location_nonpure_range_endpoints);
+
+  for_each_line_table_case (test_accessing_ordinary_linemaps);
+  for_each_line_table_case (test_lexer);
+  for_each_line_table_case (test_lexer_string_locations_simple);
+  for_each_line_table_case (test_lexer_string_locations_ebcdic);
+  for_each_line_table_case (test_lexer_string_locations_hex);
+  for_each_line_table_case (test_lexer_string_locations_oct);
+  for_each_line_table_case (test_lexer_string_locations_letter_escape_1);
+  for_each_line_table_case (test_lexer_string_locations_letter_escape_2);
+  for_each_line_table_case (test_lexer_string_locations_ucn4);
+  for_each_line_table_case (test_lexer_string_locations_ucn8);
+  for_each_line_table_case (test_lexer_string_locations_wide_string);
+  for_each_line_table_case (test_lexer_string_locations_string16);
+  for_each_line_table_case (test_lexer_string_locations_string32);
+  for_each_line_table_case (test_lexer_string_locations_u8);
+  for_each_line_table_case (test_lexer_string_locations_utf8_source);
+  for_each_line_table_case (test_lexer_string_locations_concatenation_1);
+  for_each_line_table_case (test_lexer_string_locations_concatenation_2);
+  for_each_line_table_case (test_lexer_string_locations_concatenation_3);
+  for_each_line_table_case (test_lexer_string_locations_macro);
+  for_each_line_table_case (test_lexer_string_locations_stringified_macro_argument);
+  for_each_line_table_case (test_lexer_string_locations_non_string);
+  for_each_line_table_case (test_lexer_string_locations_long_line);
+  for_each_line_table_case (test_lexer_string_locations_raw_string_one_line);
+  for_each_line_table_case (test_lexer_string_locations_raw_string_multiline);
+  for_each_line_table_case (test_lexer_string_locations_raw_string_unterminated);
+  for_each_line_table_case (test_lexer_char_constants);
 
   test_reading_source_line ();
+
+  test_line_offset_overflow ();
+
+  test_cpp_utf8 ();
 }
 
 } // namespace selftest