[5/5] diagnostics: don't print annotation lines when there's no column info

Message ID 20231121222019.646253-6-dmalcolm@redhat.com
State Committed
Commit 5099525bff4f7c36f289537bb15c582a8b51e2b3
Headers
Series v2 of libdiagnostics |

Commit Message

David Malcolm Nov. 21, 2023, 10:20 p.m. UTC
  gcc/ChangeLog:
	* diagnostic-show-locus.cc (layout::maybe_add_location_range):
	Don't print annotation lines for ranges when there's no column
	info.
	(selftest::test_one_liner_no_column): New.
	(selftest::test_diagnostic_show_locus_one_liner): Call it.
---
 gcc/diagnostic-show-locus.cc | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
  

Comments

David Malcolm Nov. 28, 2023, 1:25 a.m. UTC | #1
On Tue, 2023-11-21 at 17:20 -0500, David Malcolm wrote:
> gcc/ChangeLog:
>         * diagnostic-show-locus.cc
> (layout::maybe_add_location_range):
>         Don't print annotation lines for ranges when there's no
> column
>         info.
>         (selftest::test_one_liner_no_column): New.
>         (selftest::test_diagnostic_show_locus_one_liner): Call it.

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

Not a bugfix, but low-risk and useful for libdiagnostics, so I've taken
the liberty of pushing this to gcc trunk as r14-5896-g5099525bff4f7c.

Dave
  

Patch

diff --git a/gcc/diagnostic-show-locus.cc b/gcc/diagnostic-show-locus.cc
index 563d2826f249..55e7166b9448 100644
--- a/gcc/diagnostic-show-locus.cc
+++ b/gcc/diagnostic-show-locus.cc
@@ -1295,6 +1295,15 @@  layout::maybe_add_location_range (const location_range *loc_range,
 	   sanely relative to the primary location.  */
 	return false;
 
+  /* If there's no column information, then don't try to print
+     annotation lines for this range.  */
+  enum range_display_kind range_display_kind
+    = loc_range->m_range_display_kind;
+  if (start.column == 0
+      || finish.column == 0
+      || caret.column == 0)
+    range_display_kind = SHOW_LINES_WITHOUT_RANGE;
+
   /* Everything is now known to be in the correct source file,
      but it may require further sanitization.  */
   layout_range ri (exploc_with_display_col (m_file_cache,
@@ -1303,7 +1312,7 @@  layout::maybe_add_location_range (const location_range *loc_range,
 		   exploc_with_display_col (m_file_cache,
 					    finish, m_policy,
 					    LOCATION_ASPECT_FINISH),
-		   loc_range->m_range_display_kind,
+		   range_display_kind,
 		   exploc_with_display_col (m_file_cache,
 					    caret, m_policy,
 					    LOCATION_ASPECT_CARET),
@@ -3297,6 +3306,20 @@  test_one_liner_simple_caret ()
 		pp_formatted_text (dc.printer));
 }
 
+/* No column information (column == 0).
+   No annotation line should be printed.  */
+
+static void
+test_one_liner_no_column ()
+{
+  test_diagnostic_context dc;
+  location_t caret = linemap_position_for_column (line_table, 0);
+  rich_location richloc (line_table, caret);
+  diagnostic_show_locus (&dc, &richloc, DK_ERROR);
+  ASSERT_STREQ (" foo = bar.field;\n",
+		pp_formatted_text (dc.printer));
+}
+
 /* Caret and range.  */
 
 static void
@@ -3848,6 +3871,7 @@  test_diagnostic_show_locus_one_liner (const line_table_case &case_)
   ASSERT_EQ (16, LOCATION_COLUMN (line_end));
 
   test_one_liner_simple_caret ();
+  test_one_liner_no_column ();
   test_one_liner_caret_and_range ();
   test_one_liner_multiple_carets_and_ranges ();
   test_one_liner_fixit_insert_before ();