diff mbox series

[pushed] diagnostics: handle SGR codes in line_label::m_display_width

Message ID 20240516013000.2967650-1-dmalcolm@redhat.com
State New
Headers show
Series [pushed] diagnostics: handle SGR codes in line_label::m_display_width | expand

Commit Message

David Malcolm May 16, 2024, 1:29 a.m. UTC
Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Successful run of analyzer integration tests on x86_64-pc-linux-gnu.
Pushed to trunk as r15-532-ga7be993806a90a.

gcc/ChangeLog:
	* diagnostic-show-locus.cc: Define INCLUDE_VECTOR and include
	"text-art/types.h".
	(line_label::line_label): Drop "policy" argument.  Use
	styled_string::calc_canvas_width when computing m_display_width,
	as this skips SGR codes.
	(layout::print_any_labels): Update for line_label ctor change.
	(selftest::test_one_liner_labels_utf8): Update expected text to
	reflect that the labels can fit on one line if we don't get
	confused by SGR colorization codes.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
---
 gcc/diagnostic-show-locus.cc | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)
diff mbox series

Patch

diff --git a/gcc/diagnostic-show-locus.cc b/gcc/diagnostic-show-locus.cc
index ceccc0b793d1..f42006cfe2a1 100644
--- a/gcc/diagnostic-show-locus.cc
+++ b/gcc/diagnostic-show-locus.cc
@@ -19,6 +19,7 @@  along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
 #include "config.h"
+#define INCLUDE_VECTOR
 #include "system.h"
 #include "coretypes.h"
 #include "version.h"
@@ -31,6 +32,7 @@  along with GCC; see the file COPYING3.  If not see
 #include "selftest.h"
 #include "selftest-diagnostic.h"
 #include "cpplib.h"
+#include "text-art/types.h"
 
 #ifdef HAVE_TERMIOS_H
 # include <termios.h>
@@ -1923,14 +1925,18 @@  struct pod_label_text
 class line_label
 {
 public:
-  line_label (const cpp_char_column_policy &policy,
-	      int state_idx, int column,
+  line_label (int state_idx, int column,
 	      label_text text)
   : m_state_idx (state_idx), m_column (column),
     m_text (std::move (text)), m_label_line (0), m_has_vbar (true)
   {
-    const int bytes = strlen (m_text.m_buffer);
-    m_display_width = cpp_display_width (m_text.m_buffer, bytes, policy);
+    /* Using styled_string rather than cpp_display_width here
+       lets us skip SGR formatting characters for color and URLs.
+       It doesn't handle tabs and unicode escaping, but we don't
+       expect to see either of those in labels.  */
+    text_art::style_manager sm;
+    text_art::styled_string str (sm, m_text.m_buffer);
+    m_display_width = str.calc_canvas_width ();
   }
 
   /* Sorting is primarily by column, then by state index.  */
@@ -1990,7 +1996,7 @@  layout::print_any_labels (linenum_type row)
 	if (text.get () == NULL)
 	  continue;
 
-	labels.safe_push (line_label (m_policy, i, disp_col, std::move (text)));
+	labels.safe_push (line_label (i, disp_col, std::move (text)));
       }
   }
 
@@ -4382,9 +4388,9 @@  test_one_liner_labels_utf8 ()
       ASSERT_STREQ (" <U+1F602>_foo = <U+03C0>_bar.<U+1F602>_field<U+03C0>;\n"
 		    " ^~~~~~~~~~~~~   ~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~\n"
 		    " |               |            |\n"
-		    " |               |            label 2\xcf\x80\n"
-		    " |               label 1\xcf\x80\n"
-		    " label 0\xf0\x9f\x98\x82\n",
+		    " label 0\xf0\x9f\x98\x82"
+		    /* ... */ "       label 1\xcf\x80"
+		    /* ...................*/ "     label 2\xcf\x80\n",
 		    pp_formatted_text (dc.printer));
     }
     {
@@ -4395,9 +4401,9 @@  test_one_liner_labels_utf8 ()
 	(" <f0><9f><98><82>_foo = <cf><80>_bar.<f0><9f><98><82>_field<cf><80>;\n"
 	 " ^~~~~~~~~~~~~~~~~~~~   ~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
 	 " |                      |            |\n"
-	 " |                      |            label 2\xcf\x80\n"
-	 " |                      label 1\xcf\x80\n"
-	 " label 0\xf0\x9f\x98\x82\n",
+	 " label 0\xf0\x9f\x98\x82"
+	 /* ... */ "              label 1\xcf\x80"
+	 /* ..........................*/ "     label 2\xcf\x80\n",
 	 pp_formatted_text (dc.printer));
     }
   }