]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
diagnostics: consolidate calls to colorizer::set_named_color
authorDavid Malcolm <dmalcolm@redhat.com>
Fri, 30 May 2025 11:18:44 +0000 (07:18 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Fri, 30 May 2025 11:18:44 +0000 (07:18 -0400)
I noticed whilst working on another issue that in diagnostic-show-locus
within the quoted source lines and the annotation underlines that when
we're showing highlight-{a,b} that we emit
  start-colorization-code, character, end-colorization-code
per *character*, rather than just when the colorization changes.

This was due to me failing to implement consolidation of such changes in
colorizer::set_named_color in r15-2015-g7d73c01ce6d1ab.

Fixed thusly, simplifying the output.

I manually inspected all of the changed testcases in a terminal with
color enabled and verified that the output is visually identical to
before.

gcc/ChangeLog:
* diagnostic-show-locus.cc (colorizer::m_current_named_color): New
field.
(colorizer::set_named_color): Use it to consolidate repeated calls
to the same color.

gcc/testsuite/ChangeLog:
* g++.dg/diagnostic/bad-binary-ops-highlight-colors.C: Update
expected multiline output for quoted source and underlines to
reflect emitting color codes when changes happen, rather than
per character.
* g++.dg/diagnostic/long-short-colorization.C: Likewise.
* g++.dg/plugin/show-template-tree-color-labels.C: Likewise.
* gcc.dg/bad-binary-ops-highlight-colors.c: Likewise.
* gcc.dg/format/colors.c: Likewise.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
gcc/diagnostic-show-locus.cc
gcc/testsuite/g++.dg/diagnostic/bad-binary-ops-highlight-colors.C
gcc/testsuite/g++.dg/diagnostic/long-short-colorization.C
gcc/testsuite/g++.dg/plugin/show-template-tree-color-labels.C
gcc/testsuite/gcc.dg/bad-binary-ops-highlight-colors.c
gcc/testsuite/gcc.dg/format/colors.c

index 397fffbb79e883ff4eb4e23f3aabf672be439600..c6a0cd80ce8cd866a5291a0d1f4e07730214b5e1 100644 (file)
@@ -148,6 +148,7 @@ class colorizer
   const char *m_fixit_insert;
   const char *m_fixit_delete;
   const char *m_stop_color;
+  std::string m_current_named_color;
 };
 
 /* In order to handle multibyte sources properly, all of this logic needs to be
@@ -932,9 +933,13 @@ colorizer::~colorizer ()
 void
 colorizer::set_named_color (const char *color)
 {
+  if (m_current_state == STATE_NAMED_COLOR
+      && color == m_current_named_color)
+    return;
   finish_state (m_current_state);
   m_current_state = STATE_NAMED_COLOR;
   pp_string (&m_pp, colorize_start (pp_show_color (&m_pp), color));
+  m_current_named_color = color;
 }
 
 /* Update state, printing color codes if necessary if there's a state
index 12603456b4842a995a843ab4b2670cf58d2905ea..687af3209809b4f74fd9cb3d05d344757790cc00 100644 (file)
@@ -23,8 +23,8 @@ int test_4 (void)
      { dg-end-multiline-output "" } */
 
   /* { dg-begin-multiline-output "" }
-   return \e[01;32m\e[Kc\e[m\e[K\e[01;32m\e[Ka\e[m\e[K\e[01;32m\e[Kl\e[m\e[K\e[01;32m\e[Kl\e[m\e[K\e[01;32m\e[Ke\e[m\e[K\e[01;32m\e[Ke\e[m\e[K\e[01;32m\e[K_\e[m\e[K\e[01;32m\e[K4\e[m\e[K\e[01;32m\e[Ka\e[m\e[K\e[01;32m\e[K \e[m\e[K\e[01;32m\e[K(\e[m\e[K\e[01;32m\e[K)\e[m\e[K \e[01;31m\e[K+\e[m\e[K \e[01;34m\e[Kc\e[m\e[K\e[01;34m\e[Ka\e[m\e[K\e[01;34m\e[Kl\e[m\e[K\e[01;34m\e[Kl\e[m\e[K\e[01;34m\e[Ke\e[m\e[K\e[01;34m\e[Ke\e[m\e[K\e[01;34m\e[K_\e[m\e[K\e[01;34m\e[K4\e[m\e[K\e[01;34m\e[Kb\e[m\e[K\e[01;34m\e[K \e[m\e[K\e[01;34m\e[K(\e[m\e[K\e[01;34m\e[K)\e[m\e[K;
-          \e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K \e[01;31m\e[K^\e[m\e[K \e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K
+   return \e[01;32m\e[Kcallee_4a ()\e[m\e[K \e[01;31m\e[K+\e[m\e[K \e[01;34m\e[Kcallee_4b ()\e[m\e[K;
+          \e[01;32m\e[K~~~~~~~~~~~~\e[m\e[K \e[01;31m\e[K^\e[m\e[K \e[01;34m\e[K~~~~~~~~~~~~\e[m\e[K
                     \e[01;32m\e[K|\e[m\e[K              \e[01;34m\e[K|\e[m\e[K
                     \e[01;32m\e[KS {aka s}\e[m\e[K      \e[01;34m\e[KT {aka t}\e[m\e[K
      { dg-end-multiline-output "" } */
index d2111fee1d21d5a0f826b7e44b14a29cf57f382a..1c29ceae011de57f4ea1503ab00683d33ed65f9e 100644 (file)
@@ -6,14 +6,14 @@
 long short int a;
 /* { dg-begin-multiline-output "" }
 \e[m\e[K'\e[01m\e[K\e[01;32m\e[Klong\e[m\e[K\e[m\e[K' and '\e[01m\e[K\e[01;34m\e[Kshort\e[m\e[K\e[m\e[K' specified together
\e[01;32m\e[Kl\e[m\e[K\e[01;32m\e[Ko\e[m\e[K\e[01;32m\e[Kn\e[m\e[K\e[01;32m\e[Kg\e[m\e[K \e[01;34m\e[Ks\e[m\e[K\e[01;34m\e[Kh\e[m\e[K\e[01;34m\e[Ko\e[m\e[K\e[01;34m\e[Kr\e[m\e[K\e[01;34m\e[Kt\e[m\e[K int a;
\e[01;32m\e[K^\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K \e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K
\e[01;32m\e[Klong\e[m\e[K \e[01;34m\e[Kshort\e[m\e[K int a;
\e[01;32m\e[K^~~~\e[m\e[K \e[01;34m\e[K~~~~~\e[m\e[K
    { dg-end-multiline-output "" } */
 short long int b;
 /* { dg-begin-multiline-output "" }
 \e[m\e[K'\e[01m\e[K\e[01;32m\e[Klong\e[m\e[K\e[m\e[K' and '\e[01m\e[K\e[01;34m\e[Kshort\e[m\e[K\e[m\e[K' specified together
\e[01;34m\e[Ks\e[m\e[K\e[01;34m\e[Kh\e[m\e[K\e[01;34m\e[Ko\e[m\e[K\e[01;34m\e[Kr\e[m\e[K\e[01;34m\e[Kt\e[m\e[K \e[01;32m\e[Kl\e[m\e[K\e[01;32m\e[Ko\e[m\e[K\e[01;32m\e[Kn\e[m\e[K\e[01;32m\e[Kg\e[m\e[K int b;
\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K \e[01;32m\e[K^\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K
\e[01;34m\e[Kshort\e[m\e[K \e[01;32m\e[Klong\e[m\e[K int b;
\e[01;34m\e[K~~~~~\e[m\e[K \e[01;32m\e[K^~~~\e[m\e[K
    { dg-end-multiline-output "" } */
 
 // Discard the remaining colorized output that confuses dejagnu.
index 75488cb6e0f19ab7134e276f43cb9036c018cf34..3e9312665c7908f944ac9320c64f5109f8fe164e 100644 (file)
@@ -16,8 +16,8 @@ void test_1 (vector<double> vec)
   fn_1 (vec);
   /* { dg-begin-multiline-output "" }
 could not convert '\e[01m\e[Kvec\e[m\e[K' from '\e[01m\e[Kvector<\e[01;32m\e[Kdouble\e[m\e[K>\e[m\e[K' to '\e[01m\e[Kvector<\e[01;34m\e[Kint\e[m\e[K>\e[m\e[K'
-   fn_1 (\e[01;32m\e[Kv\e[m\e[K\e[01;32m\e[Ke\e[m\e[K\e[01;32m\e[Kc\e[m\e[K);
-         \e[01;32m\e[K^\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K
+   fn_1 (\e[01;32m\e[Kvec\e[m\e[K);
+         \e[01;32m\e[K^~~\e[m\e[K
          \e[01;32m\e[K|\e[m\e[K
          \e[01;32m\e[Kvector<double>\e[m\e[K
      { dg-end-multiline-output "" } */
index efbcdf39c50270e39dbb7c6bf65ceeae25c2fe24..da98f1a4140eda60dd7d3d9829802457a616af56 100644 (file)
@@ -23,8 +23,8 @@ int test_4 (void)
      { dg-end-multiline-output "" } */
 
   /* { dg-begin-multiline-output "" }
-   return \e[01;32m\e[Kc\e[m\e[K\e[01;32m\e[Ka\e[m\e[K\e[01;32m\e[Kl\e[m\e[K\e[01;32m\e[Kl\e[m\e[K\e[01;32m\e[Ke\e[m\e[K\e[01;32m\e[Ke\e[m\e[K\e[01;32m\e[K_\e[m\e[K\e[01;32m\e[K4\e[m\e[K\e[01;32m\e[Ka\e[m\e[K\e[01;32m\e[K \e[m\e[K\e[01;32m\e[K(\e[m\e[K\e[01;32m\e[K)\e[m\e[K \e[01;31m\e[K+\e[m\e[K \e[01;34m\e[Kc\e[m\e[K\e[01;34m\e[Ka\e[m\e[K\e[01;34m\e[Kl\e[m\e[K\e[01;34m\e[Kl\e[m\e[K\e[01;34m\e[Ke\e[m\e[K\e[01;34m\e[Ke\e[m\e[K\e[01;34m\e[K_\e[m\e[K\e[01;34m\e[K4\e[m\e[K\e[01;34m\e[Kb\e[m\e[K\e[01;34m\e[K \e[m\e[K\e[01;34m\e[K(\e[m\e[K\e[01;34m\e[K)\e[m\e[K;
-          \e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K~\e[m\e[K \e[01;31m\e[K^\e[m\e[K \e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K
+   return \e[01;32m\e[Kcallee_4a ()\e[m\e[K \e[01;31m\e[K+\e[m\e[K \e[01;34m\e[Kcallee_4b ()\e[m\e[K;
+          \e[01;32m\e[K~~~~~~~~~~~~\e[m\e[K \e[01;31m\e[K^\e[m\e[K \e[01;34m\e[K~~~~~~~~~~~~\e[m\e[K
           \e[01;32m\e[K|\e[m\e[K              \e[01;34m\e[K|\e[m\e[K
           \e[01;32m\e[K|\e[m\e[K              \e[01;34m\e[KT {aka struct t}\e[m\e[K
           \e[01;32m\e[KS {aka struct s}\e[m\e[K
index 43484b70a2a1bf7ec24df0c5c603658dabbabdf0..42cfd5043d42ca24c67a3cd8ad6358291a585ce9 100644 (file)
@@ -15,8 +15,8 @@ void test_mismatching_types (const char *msg)
 warning: \e[m\e[Kformat '\e[01m\e[K\e[01;32m\e[K%i\e[m\e[K\e[m\e[K' expects argument of type '\e[01m\e[K\e[01;32m\e[Kint\e[m\e[K\e[m\e[K', but argument 2 has type '\e[01m\e[K\e[01;34m\e[Kconst char *\e[m\e[K\e[m\e[K' [\e[01;35m\e[K-Wformat=\e[m\e[K]
    { dg-end-multiline-output "" } */
 /* { dg-begin-multiline-output "" }
-   printf("hello \e[01;32m\e[K%\e[m\e[K\e[01;32m\e[Ki\e[m\e[K", \e[01;34m\e[Km\e[m\e[K\e[01;34m\e[Ks\e[m\e[K\e[01;34m\e[Kg\e[m\e[K);
-                 \e[01;32m\e[K~\e[m\e[K\e[01;32m\e[K^\e[m\e[K   \e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K\e[01;34m\e[K~\e[m\e[K
+   printf("hello \e[01;32m\e[K%i\e[m\e[K", \e[01;34m\e[Kmsg\e[m\e[K);
+                 \e[01;32m\e[K~^\e[m\e[K   \e[01;34m\e[K~~~\e[m\e[K
                   \e[01;32m\e[K|\e[m\e[K   \e[01;34m\e[K|\e[m\e[K
                   \e[01;32m\e[Kint\e[m\e[K \e[01;34m\e[Kconst char *\e[m\e[K
                  \e[32m\e[K%s\e[m\e[K