Fix issues with unrepresentable column numbers (PR c++/77949)
authorDavid Malcolm <dmalcolm@redhat.com>
Tue, 10 Jan 2017 21:54:09 +0000 (21:54 +0000)
committerDavid Malcolm <dmalcolm@gcc.gnu.org>
Tue, 10 Jan 2017 21:54:09 +0000 (21:54 +0000)
PR c++/77949 identifies an ICE when the C++ frontend attempts to emit a
fix-it hint inserting a missing semicolon at column 4097 of a source file.

This column value exceeds LINE_MAP_MAX_COLUMN_NUMBER and hence isn't
representable using a location_t.

Attempting to do so leads to these problems, which this patch fixes:

(a) when encountering a column number > LINE_MAP_MAX_COLUMN_NUMBER we
create a new linemap with m_column_and_range_bits == 0, but
linemap_position_for_column doesn't check for this, and hence can emit
a bogus location_t value that's calculated relative to the previous
linemap start, but which will be decoded relative to the new linemap,
leading to very large incorrect line values.

(b) when encountering a column number that can't be represented, and
for which the linemap was pre-existing, the code would hit this assertion:
  if (linemap_assert_fails (column < (1u << map->m_column_and_range_bits)))
around a bail-out condition.  The patch replaces this assertion with a
simple conditional, to stop the ICE when this occurs, and fixes the
bit count (effective column bits, vs column+range bits)

(c) the C++ frontend wasn't checking for failure of
linemap_position_for_loc_and_offset when considering emitting the fix-it
hint.  The patch adds a conditional, so that no fix-it hint is emitted
if the location is bogus.

gcc/cp/ChangeLog:
PR c++/77949
* parser.c (cp_parser_class_specifier_1): Only suggest inserting
a missing semicolon if we have a valid insertion location for
the fix-it hint.

gcc/ChangeLog:
PR c++/77949
* input.c (selftest::test_accessing_ordinary_linemaps): Verify
that we correctly handle column numbers greater than
LINE_MAP_MAX_COLUMN_NUMBER.

gcc/testsuite/ChangeLog:
PR c++/77949
* g++.dg/diagnostic/pr77949.C: New test case.

libcpp/ChangeLog:
PR c++/77949
* line-map.c (linemap_position_for_column): When calling
linemap_start_line, detect if a new linemap was created with
0 column bits, and bail out early if this is the case.
(linemap_position_for_loc_and_offset): Replace overzealous
linemap_assert_fails with a simple conditional; use correct
bit count.

From-SVN: r244292

gcc/ChangeLog
gcc/cp/ChangeLog
gcc/cp/parser.c
gcc/input.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/diagnostic/pr77949.C [new file with mode: 0644]
libcpp/ChangeLog
libcpp/line-map.c

index d0c53d066eae29b9867607f02bd53235cbd6210b..ff8dac4edddb1f7408e03957a2563faa2bc17126 100644 (file)
@@ -1,3 +1,10 @@
+2017-01-10  David Malcolm  <dmalcolm@redhat.com>
+
+       PR c++/77949
+       * input.c (selftest::test_accessing_ordinary_linemaps): Verify
+       that we correctly handle column numbers greater than
+       LINE_MAP_MAX_COLUMN_NUMBER.
+
 2017-01-10  Martin Sebor  <msebor@redhat.com>
 
        PR tree-optimization/78775
index 25f5a85db92e68847128b8816cf0752fcf17406b..892651a7eeb98b9ab56844be6e3e71ccd7e8cecb 100644 (file)
@@ -1,3 +1,10 @@
+2017-01-10  David Malcolm  <dmalcolm@redhat.com>
+
+       PR c++/77949
+       * parser.c (cp_parser_class_specifier_1): Only suggest inserting
+       a missing semicolon if we have a valid insertion location for
+       the fix-it hint.
+
 2017-01-10  Jason Merrill  <jason@redhat.com>
 
        FI 20, decomposition declaration with parenthesized initializer.
index 45173131ee93045ecbdeaf41f16329b63dae0a79..7b3ee30345ef843077fb83bfa1e265c992be00f1 100644 (file)
@@ -22164,7 +22164,10 @@ cp_parser_class_specifier_1 (cp_parser* parser)
          next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
 
        rich_location richloc (line_table, next_loc);
-       richloc.add_fixit_insert_before (next_loc, ";");
+
+       /* If we successfully offset the location, suggest the fix-it.  */
+       if (next_loc != loc)
+         richloc.add_fixit_insert_before (next_loc, ";");
 
        if (CLASSTYPE_DECLARED_CLASS (type))
          error_at_rich_loc (&richloc,
index bbb6abb8878b3588c3bd48fc03acf44d30dc85fd..3e67314932a1faf08a7db2fb59fc119bb2a913f2 100644 (file)
@@ -1699,6 +1699,22 @@ test_accessing_ordinary_linemaps (const line_table_case &case_)
       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.  */
@@ -1714,6 +1730,16 @@ test_accessing_ordinary_linemaps (const line_table_case &case_)
   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));
index f8cba4221ab1a8d1b68faea590a19178a86c916f..59a2fcfbef4242476df2f3cfccb42ec374a05c04 100644 (file)
@@ -1,3 +1,8 @@
+2017-01-10  David Malcolm  <dmalcolm@redhat.com>
+
+       PR c++/77949
+       * g++.dg/diagnostic/pr77949.C: New test case.
+
 2017-01-10  Martin Sebor  <msebor@redhat.com>
 
        PR tree-optimization/78775
diff --git a/gcc/testsuite/g++.dg/diagnostic/pr77949.C b/gcc/testsuite/g++.dg/diagnostic/pr77949.C
new file mode 100644 (file)
index 0000000..0d8b333
--- /dev/null
@@ -0,0 +1,7 @@
+// Ensure that no fix-it hints are emitted
+// { dg-options "-fdiagnostics-parseable-fixits" }
+
+/* Very long line, where a missing semicolon would be suggested for
+   insertion at column 4097.  */
+class test {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   }   
+// { dg-error "0: expected .;. after class definition" "" { target *-*-* } .-1 }
index 082291d56489b67595a9bc23b48ab6508d6cb26b..208e1d52e6f5a4a711631eb0ece9ceff5a753114 100644 (file)
@@ -1,3 +1,13 @@
+2017-01-10  David Malcolm  <dmalcolm@redhat.com>
+
+       PR c++/77949
+       * line-map.c (linemap_position_for_column): When calling
+       linemap_start_line, detect if a new linemap was created with
+       0 column bits, and bail out early if this is the case.
+       (linemap_position_for_loc_and_offset): Replace overzealous
+       linemap_assert_fails with a simple conditional; use correct
+       bit count.
+
 2017-01-07  David Malcolm  <dmalcolm@redhat.com>
 
        PR c++/72803
index b410c00e36724523bf89794a186aa272547f3f5c..949489eb1a1b645e6b7b58f307a5d27b9a23a0f7 100644 (file)
@@ -816,8 +816,22 @@ linemap_position_for_column (struct line_maps *set, unsigned int to_column)
        }
       else
        {
+         /* Otherwise, attempt to start a new line that can hold TO_COLUMN,
+            with some space to spare.  This may or may not lead to a new
+            linemap being created.  */
          line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
          r = linemap_line_start (set, SOURCE_LINE (map, r), to_column + 50);
+         map = LINEMAPS_LAST_ORDINARY_MAP (set);
+         if (map->m_column_and_range_bits == 0)
+           {
+             /* ...then the linemap has column-tracking disabled,
+                presumably due to exceeding either
+                LINE_MAP_MAX_LOCATION_WITH_COLS (overall) or
+                LINE_MAP_MAX_COLUMN_NUMBER (within this line).
+                Return the start of the linemap, which encodes column 0, for
+                the whole line.  */
+             return r;
+           }
        }
     }
   line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (set);
@@ -905,7 +919,10 @@ linemap_position_for_loc_and_offset (struct line_maps *set,
     }
 
   column += column_offset;
-  if (linemap_assert_fails (column < (1u << map->m_column_and_range_bits)))
+
+  /* Bail out if the column is not representable within the existing
+     linemap.  */
+  if (column >= (1u << (map->m_column_and_range_bits - map->m_range_bits)))
     return loc;
 
   source_location r =