This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[committed] fix-it hints can't contain newlines (yet)


I hope to implement newline support within fix-it hints at some point,
but currently it's not supported, and leads to misleading diagnostic
output, so for now, fail gracefully.

Successfully bootstrapped&regrtested on x86_64-pc-linux-gnu.

Committed to trunk as r240169.

gcc/ChangeLog:
	* diagnostic-show-locus.c
	(selftest::test_fixit_insert_containing_newline): New function.
	(selftest::test_fixit_replace_containing_newline): New function.
	(selftest::diagnostic_show_locus_c_tests): Call the above.

libcpp/ChangeLog:
	* include/line-map.h (class rich_location): Note that newlines
	aren't supported in fix-it text.
	* line-map.c (rich_location::add_fixit_insert_before): Reject
	attempts to add fix-its containing newlines.
	(rich_location::add_fixit_replace): Likewise.
---
 gcc/diagnostic-show-locus.c | 83 +++++++++++++++++++++++++++++++++++++++++++++
 libcpp/include/line-map.h   |  2 ++
 libcpp/line-map.c           | 13 +++++++
 3 files changed, 98 insertions(+)

diff --git a/gcc/diagnostic-show-locus.c b/gcc/diagnostic-show-locus.c
index 331eb92..f6bfcc9 100644
--- a/gcc/diagnostic-show-locus.c
+++ b/gcc/diagnostic-show-locus.c
@@ -2160,6 +2160,87 @@ test_fixit_consolidation (const line_table_case &case_)
   }
 }
 
+/* Insertion fix-it hint: adding a "break;" on a line by itself.
+   This will fail, as newlines aren't yet supported.  */
+
+static void
+test_fixit_insert_containing_newline (const line_table_case &case_)
+{
+  /* Create a tempfile and write some text to it.
+     .........................0000000001111111.
+     .........................1234567890123456.  */
+  const char *old_content = ("    case 'a':\n" /* line 1. */
+			     "      x = a;\n"  /* line 2. */
+			     "    case 'b':\n" /* line 3. */
+			     "      x = b;\n");/* line 4. */
+
+  temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
+  line_table_test ltt (case_);
+  linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 3);
+
+  /* Add a "break;" on a line by itself before line 3 i.e. before
+     column 1 of line 3. */
+  location_t case_start = linemap_position_for_column (line_table, 5);
+  location_t case_finish = linemap_position_for_column (line_table, 13);
+  location_t case_loc = make_location (case_start, case_start, case_finish);
+  rich_location richloc (line_table, case_loc);
+  location_t line_start = linemap_position_for_column (line_table, 1);
+  richloc.add_fixit_insert_before (line_start, "      break;\n");
+
+  /* Newlines are not yet supported within fix-it hints, so
+     the fix-it should not be displayed.  */
+  ASSERT_TRUE (richloc.seen_impossible_fixit_p ());
+
+  if (case_finish > LINE_MAP_MAX_LOCATION_WITH_COLS)
+    return;
+
+  test_diagnostic_context dc;
+  diagnostic_show_locus (&dc, &richloc, DK_ERROR);
+  ASSERT_STREQ ("\n"
+		"     case 'b':\n"
+		"     ^~~~~~~~~\n",
+		pp_formatted_text (dc.printer));
+}
+
+/* Replacement fix-it hint containing a newline.
+   This will fail, as newlines aren't yet supported.  */
+
+static void
+test_fixit_replace_containing_newline (const line_table_case &case_)
+{
+  /* Create a tempfile and write some text to it.
+    .........................0000000001111.
+    .........................1234567890123.  */
+  const char *old_content = "foo = bar ();\n";
+
+  temp_source_file tmp (SELFTEST_LOCATION, ".c", old_content);
+  line_table_test ltt (case_);
+  linemap_add (line_table, LC_ENTER, false, tmp.get_filename (), 1);
+
+  /* Replace the " = " with "\n  = ", as if we were reformatting an
+     overly long line.  */
+  location_t start = linemap_position_for_column (line_table, 4);
+  location_t finish = linemap_position_for_column (line_table, 6);
+  location_t loc = linemap_position_for_column (line_table, 13);
+  rich_location richloc (line_table, loc);
+  source_range range = source_range::from_locations (start, finish);
+  richloc.add_fixit_replace (range, "\n =");
+
+  /* Newlines are not yet supported within fix-it hints, so
+     the fix-it should not be displayed.  */
+  ASSERT_TRUE (richloc.seen_impossible_fixit_p ());
+
+  if (finish > LINE_MAP_MAX_LOCATION_WITH_COLS)
+    return;
+
+  test_diagnostic_context dc;
+  diagnostic_show_locus (&dc, &richloc, DK_ERROR);
+  ASSERT_STREQ ("\n"
+		" foo = bar ();\n"
+		"             ^\n",
+		pp_formatted_text (dc.printer));
+}
+
 /* Run all of the selftests within this file.  */
 
 void
@@ -2176,6 +2257,8 @@ diagnostic_show_locus_c_tests ()
   for_each_line_table_case (test_diagnostic_show_locus_one_liner);
   for_each_line_table_case (test_diagnostic_show_locus_fixit_lines);
   for_each_line_table_case (test_fixit_consolidation);
+  for_each_line_table_case (test_fixit_insert_containing_newline);
+  for_each_line_table_case (test_fixit_replace_containing_newline);
 }
 
 } // namespace selftest
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index 939bfcc..747609d 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -1551,6 +1551,8 @@ class fixit_hint;
 
    Attempts to add a fix-it hint within a macro expansion will fail.
 
+   We do not yet support newlines in fix-it text; attempts to do so will fail.
+
    The rich_location API handles these failures gracefully, so that
    diagnostics can attempt to add fix-it hints without each needing
    extensive checking.
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 742af0a..07e3acb 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -2128,6 +2128,12 @@ rich_location::add_fixit_insert_before (source_location where,
 
   if (reject_impossible_fixit (start))
     return;
+  /* We do not yet support newlines within fix-it hints.  */
+  if (strchr (new_content, '\n'))
+    {
+      stop_supporting_fixits ();
+      return;
+    }
   add_fixit (new fixit_insert (start, new_content));
 }
 
@@ -2271,6 +2277,13 @@ rich_location::add_fixit_replace (source_range src_range,
   if (reject_impossible_fixit (src_range.m_finish))
     return;
 
+  /* We do not yet support newlines within fix-it hints.  */
+  if (strchr (new_content, '\n'))
+    {
+      stop_supporting_fixits ();
+      return;
+    }
+
   /* Consolidate neighboring fixits.  */
   fixit_hint *prev = get_last_fixit_hint ();
   if (prev)
-- 
1.8.5.3


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]