libcpp: fix ICE comparing macro locations without column info [PR96391]
authorDavid Malcolm <dmalcolm@redhat.com>
Wed, 10 Feb 2021 19:33:10 +0000 (14:33 -0500)
committerDavid Malcolm <dmalcolm@redhat.com>
Wed, 10 Feb 2021 19:33:10 +0000 (14:33 -0500)
PR preprocessor/96391 describes an ICE in the C++ frontend on:

  #define CONST const
  #define VOID void
  typedef CONST VOID *PCVOID;

where the typedef line occurs after enough code has been compiled
that location_t values are beyond LINE_MAP_MAX_LOCATION_WITH_COLS,
and hence no column numbers are available.

The issue occurs in linemap_compare_locations when comparing the
locations of the "const" and "void" tokens.
Upon resolving the LRK_MACRO_EXPANSION_POINT, both have the same
location_t, the line of the "typedef" (with no column), and so
the l0 == l1 clause is triggered, but they are not from the
same macro expansion, leading first_map_in_common to return NULL
and triggering the "abort" condition.

This patch fixes the issue by checking when the two macro expansion
point location_t values are equal that the value
<= LINE_MAP_MAX_LOCATION_WITH_COLS and thus has column information,
fixing the issue.

gcc/testsuite/ChangeLog:
PR preprocessor/96391
* g++.dg/plugin/location-overflow-test-pr96391.c: New test.
* g++.dg/plugin/plugin.exp (plugin_test_list): Add it,
using the location_overflow_plugin.c from gcc.dg/plugin.

libcpp/ChangeLog:
PR preprocessor/96391
* line-map.c (linemap_compare_locations): Require that
the location be <= LINE_MAP_MAX_LOCATION_WITH_COLS when
treating locations as coming from the same macro expansion.

gcc/testsuite/g++.dg/plugin/location-overflow-test-pr96391.c [new file with mode: 0644]
gcc/testsuite/g++.dg/plugin/plugin.exp
libcpp/line-map.c

diff --git a/gcc/testsuite/g++.dg/plugin/location-overflow-test-pr96391.c b/gcc/testsuite/g++.dg/plugin/location-overflow-test-pr96391.c
new file mode 100644 (file)
index 0000000..1816961
--- /dev/null
@@ -0,0 +1,12 @@
+/* { dg-options "-fplugin-arg-location_overflow_plugin-value=0x60000001" } */
+
+/* We use location_overflow_plugin.c, which injects the case that location_t
+   values have exceeded LINE_MAP_MAX_LOCATION_WITH_COLS, and hence no column
+   numbers are available.  */
+
+/* Verify that we're in column-less mode.  */
+extern unknown_type test; /* { dg-error "-:'unknown_type' does not name a type" } */
+
+#define CONST const
+#define VOID void
+typedef CONST VOID *PCVOID;
index 80c5355fb5eead096355c8e25146d01f99b108b5..5cd4b4bff902e33f104737abe1f06b479bb12ba7 100644 (file)
@@ -72,6 +72,8 @@ set plugin_test_list [list \
          ../../gcc.dg/plugin/diagnostic-test-string-literals-2.c \
          ../../gcc.dg/plugin/diagnostic-test-string-literals-3.c \
          ../../gcc.dg/plugin/diagnostic-test-string-literals-4.c } \
+    { ../../gcc.dg/plugin/location_overflow_plugin.c \
+         location-overflow-test-pr96391.c } \
     { show_template_tree_color_plugin.c \
          show-template-tree-color.C \
          show-template-tree-color-labels.C \
index 2432cd50d2607e2402fd6db271853c0b883b99a2..cccacf2fe5b2989567366befbb907ab6a0c6ebf3 100644 (file)
@@ -1417,7 +1417,8 @@ linemap_compare_locations (line_maps *set,
 
   if (l0 == l1
       && pre_virtual_p
-      && post_virtual_p)
+      && post_virtual_p
+      && l0 <= LINE_MAP_MAX_LOCATION_WITH_COLS)
     {
       /* So pre and post represent two tokens that are present in a
         same macro expansion.  Let's see if the token for pre was