Fix ICE in substring-handling building 502.gcc_r (PR 87562)
authorDavid Malcolm <dmalcolm@redhat.com>
Thu, 18 Oct 2018 15:44:39 +0000 (15:44 +0000)
committerDavid Malcolm <dmalcolm@gcc.gnu.org>
Thu, 18 Oct 2018 15:44:39 +0000 (15:44 +0000)
In r264887 I broke the build of 502.gcc_r due to an ICE.
The ICE occurs when generating a location for an sprintf warning within
a string literal, where the sprintf call is in a macro.

The root cause is a bug in the original commit of substring locations
(r239175).  get_substring_ranges_for_loc has code to handle the case
where the string literal is in a very long source line that exceeds the
length that the current linemap can represent: the start of the token
is in one line map, but then another line map is started, and the end
of the token is in the new linemap.  get_substring_ranges_for_loc handles
this by using the linemap of the end-point when building location_t
values within the string.  When extracting the linemap for the endpoint
in r239175 I erroneously used LRK_MACRO_EXPANSION_POINT, which should
have instead been LRK_SPELLING_LOCATION.

I believe this bug was dormant due to rejecting macro locations earlier
in the function, but in r264887 I allowed some macro locations in order
to deal with locations coming from the C++ lexer, and this uncovered
the bug: if a string literal was defined in a macro, locations within
the string literal would be looked up using the linemap of the expansion
point of the macro, rather than of the spelling point.  This would lead
to garbage location_t values, and, depending on the precise line numbers
of the two locations, an assertion failure (which was causing the build
failure in 502.gcc_r).

This patch fixes the bug by using LRK_SPELLING_LOCATION, and adds some
bulletproofing to the "two linemaps" case.

Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu
(g++.sum gained 5 PASS results; gcc.sum gained 3 PASS results).
I also verified that this fixes the build of 502.gcc_r.

gcc/ChangeLog:
PR tree-optimization/87562
* input.c (get_substring_ranges_for_loc): Use
LRK_SPELLING_LOCATION rather than LRK_MACRO_EXPANSION_POINT when
getting the linemap for the endpoint.  Verify that it's either
in the same linemap as the start point's spelling location, or
at least in the same file.

gcc/testsuite/ChangeLog:
PR tree-optimization/87562
* c-c++-common/substring-location-PR-87562-1-a.h: New file.
* c-c++-common/substring-location-PR-87562-1-b.h: New file.
* c-c++-common/substring-location-PR-87562-1.c: New test.
* gcc.dg/plugin/diagnostic-test-string-literals-1.c: Add test for
PR 87562.
* gcc.dg/plugin/pr87562-a.h: New file.
* gcc.dg/plugin/pr87562-b.h: New file.

From-SVN: r265271

gcc/ChangeLog
gcc/input.c
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/substring-location-PR-87562-1-a.h [new file with mode: 0644]
gcc/testsuite/c-c++-common/substring-location-PR-87562-1-b.h [new file with mode: 0644]
gcc/testsuite/c-c++-common/substring-location-PR-87562-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/plugin/diagnostic-test-string-literals-1.c
gcc/testsuite/gcc.dg/plugin/pr87562-a.h [new file with mode: 0644]
gcc/testsuite/gcc.dg/plugin/pr87562-b.h [new file with mode: 0644]

index 1c2e61292188b691b9380235bb3b95cd731e3481..b97a8a2461b6a316ba80abac9e99d64463d4b7a3 100644 (file)
@@ -1,3 +1,12 @@
+2018-10-18  David Malcolm  <dmalcolm@redhat.com>
+
+       PR tree-optimization/87562
+       * input.c (get_substring_ranges_for_loc): Use
+       LRK_SPELLING_LOCATION rather than LRK_MACRO_EXPANSION_POINT when
+       getting the linemap for the endpoint.  Verify that it's either
+       in the same linemap as the start point's spelling location, or
+       at least in the same file.
+
 2018-10-18  Richard Biener  <rguenther@suse.de>
 
        * config/i386/i386.c (ix86_builtin_vectorization_cost): Do not
index eeeb11ecc0ab262f480c3dd54e85eb0105a190b3..57a1a3c42d3166b4214d8dcd273dcf2a6e98c2ea 100644 (file)
@@ -1457,9 +1457,17 @@ get_substring_ranges_for_loc (cpp_reader *pfile,
         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);
+      /* 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";
       location_t start_loc
        = linemap_position_for_line_and_column (line_table, final_ord_map,
                                                start.line, start.column);
index 6f7d3b9e8838fe3725d5852dfbe555907f0e9f31..4929e368f417e201ff600b07628b5e29d018fe8d 100644 (file)
@@ -1,3 +1,14 @@
+2018-10-18  David Malcolm  <dmalcolm@redhat.com>
+
+       PR tree-optimization/87562
+       * c-c++-common/substring-location-PR-87562-1-a.h: New file.
+       * c-c++-common/substring-location-PR-87562-1-b.h: New file.
+       * c-c++-common/substring-location-PR-87562-1.c: New test.
+       * gcc.dg/plugin/diagnostic-test-string-literals-1.c: Add test for
+       PR 87562.
+       * gcc.dg/plugin/pr87562-a.h: New file.
+       * gcc.dg/plugin/pr87562-b.h: New file.
+
 2018-10-18  Paul Thomas  <pault@gcc.gnu.org>
 
        PR fortran/58618
diff --git a/gcc/testsuite/c-c++-common/substring-location-PR-87562-1-a.h b/gcc/testsuite/c-c++-common/substring-location-PR-87562-1-a.h
new file mode 100644 (file)
index 0000000..369c9d0
--- /dev/null
@@ -0,0 +1,7 @@
+#undef  ASM_GENERATE_INTERNAL_LABEL
+#define ASM_GENERATE_INTERNAL_LABEL(LABEL, PREFIX, NUM)                \
+  do                                                           \
+    {                                                          \
+      __builtin_sprintf (LABEL, "*.%s%u", PREFIX, (unsigned) (NUM));   \
+    }                                                          \
+  while (0)
diff --git a/gcc/testsuite/c-c++-common/substring-location-PR-87562-1-b.h b/gcc/testsuite/c-c++-common/substring-location-PR-87562-1-b.h
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/gcc/testsuite/c-c++-common/substring-location-PR-87562-1.c b/gcc/testsuite/c-c++-common/substring-location-PR-87562-1.c
new file mode 100644 (file)
index 0000000..6afd22c
--- /dev/null
@@ -0,0 +1,15 @@
+/* Reproducing this ICE is dependent on line numbering, hence the blank
+   lines below.  */
+#include "substring-location-PR-87562-1-a.h"
+
+
+
+
+#include "substring-location-PR-87562-1-b.h"
+
+void
+dbxout_stab_value_internal_label (const char *stem, int counter)
+{
+  char label[100];
+  ASM_GENERATE_INTERNAL_LABEL (label, stem, counter);
+}
index 73770aa1bee1b5fbff97eab500008f2f8117c910..b7350955e99168919d1bd99997902e194dfd7551 100644 (file)
@@ -296,3 +296,26 @@ test_backslash_continued_logical_lines (void)
  ~~~~~~                          
    { dg-end-multiline-output "" } */
 }
+
+/* Reproducer for PR 87652; this is whitespace-sensitive.  */
+
+#include "pr87562-a.h"
+
+
+
+
+#include "pr87562-b.h"
+
+void
+pr87652 (const char *stem, int counter)
+{
+  char label[100];
+  ASM_GENERATE_INTERNAL_LABEL (label, stem, counter);
+
+  /* This warning is actually in "pr87562-a.h".  */
+  /* { dg-warning "39: range" "" { target *-*-* } 5 } */
+  /* { dg-begin-multiline-output "" }
+       __emit_string_literal_range ("*.%s%u", 2, 2, 3); \
+                                       ^~
+     { dg-end-multiline-output "" } */
+}
diff --git a/gcc/testsuite/gcc.dg/plugin/pr87562-a.h b/gcc/testsuite/gcc.dg/plugin/pr87562-a.h
new file mode 100644 (file)
index 0000000..0e5a9f1
--- /dev/null
@@ -0,0 +1,7 @@
+#undef  ASM_GENERATE_INTERNAL_LABEL
+#define ASM_GENERATE_INTERNAL_LABEL(LABEL, PREFIX, NUM)                \
+  do                                                           \
+    {                                                          \
+      __emit_string_literal_range ("*.%s%u", 2, 2, 3); \
+    }                                                          \
+  while (0)
diff --git a/gcc/testsuite/gcc.dg/plugin/pr87562-b.h b/gcc/testsuite/gcc.dg/plugin/pr87562-b.h
new file mode 100644 (file)
index 0000000..e69de29