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.

Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Pushed to trunk as r16-1002-g333628a1e29641.

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 <dmalc...@redhat.com>
---
 gcc/diagnostic-show-locus.cc                              | 5 +++++
 .../g++.dg/diagnostic/bad-binary-ops-highlight-colors.C   | 4 ++--
 gcc/testsuite/g++.dg/diagnostic/long-short-colorization.C | 8 ++++----
 .../g++.dg/plugin/show-template-tree-color-labels.C       | 4 ++--
 gcc/testsuite/gcc.dg/bad-binary-ops-highlight-colors.c    | 4 ++--
 gcc/testsuite/gcc.dg/format/colors.c                      | 4 ++--
 6 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/gcc/diagnostic-show-locus.cc b/gcc/diagnostic-show-locus.cc
index 397fffbb79e8..c6a0cd80ce8c 100644
--- a/gcc/diagnostic-show-locus.cc
+++ b/gcc/diagnostic-show-locus.cc
@@ -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
diff --git a/gcc/testsuite/g++.dg/diagnostic/bad-binary-ops-highlight-colors.C 
b/gcc/testsuite/g++.dg/diagnostic/bad-binary-ops-highlight-colors.C
index 12603456b484..687af3209809 100644
--- a/gcc/testsuite/g++.dg/diagnostic/bad-binary-ops-highlight-colors.C
+++ b/gcc/testsuite/g++.dg/diagnostic/bad-binary-ops-highlight-colors.C
@@ -23,8 +23,8 @@ int test_4 (void)
      { dg-end-multiline-output "" } */
 
   /* { dg-begin-multiline-output "" }
-   return 
callee_4a
 () + 
callee_4b
 ();
-          
~~~~~~~~~~~~
 ^ 
~~~~~~~~~~~~
+   return callee_4a () + 
callee_4b ();
+          ~~~~~~~~~~~~ ^ 
~~~~~~~~~~~~
                     |              |
                     S {aka s}      T {aka t}
      { dg-end-multiline-output "" } */
diff --git a/gcc/testsuite/g++.dg/diagnostic/long-short-colorization.C 
b/gcc/testsuite/g++.dg/diagnostic/long-short-colorization.C
index d2111fee1d21..1c29ceae011d 100644
--- a/gcc/testsuite/g++.dg/diagnostic/long-short-colorization.C
+++ b/gcc/testsuite/g++.dg/diagnostic/long-short-colorization.C
@@ -6,14 +6,14 @@
 long short int a;
 /* { dg-begin-multiline-output "" }
 'long' and 
'short' specified together
- long 
short
 int a;
- ^~~~ 
~~~~~
+ long short int a;
+ ^~~~ ~~~~~
    { dg-end-multiline-output "" } */
 short long int b;
 /* { dg-begin-multiline-output "" }
 'long' and 
'short' specified together
- 
short
 long int b;
- 
~~~~~
 ^~~~
+ short long int b;
+ ~~~~~ ^~~~
    { dg-end-multiline-output "" } */
 
 // Discard the remaining colorized output that confuses dejagnu.
diff --git a/gcc/testsuite/g++.dg/plugin/show-template-tree-color-labels.C 
b/gcc/testsuite/g++.dg/plugin/show-template-tree-color-labels.C
index 75488cb6e0f1..3e9312665c79 100644
--- a/gcc/testsuite/g++.dg/plugin/show-template-tree-color-labels.C
+++ b/gcc/testsuite/g++.dg/plugin/show-template-tree-color-labels.C
@@ -16,8 +16,8 @@ void test_1 (vector<double> vec)
   fn_1 (vec);
   /* { dg-begin-multiline-output "" }
 could not convert 'vec' from 
'vector<double>' to 
'vector<int>'
-   fn_1 (vec);
-         ^~~
+   fn_1 (vec);
+         ^~~
          |
          vector<double>
      { dg-end-multiline-output "" } */
diff --git a/gcc/testsuite/gcc.dg/bad-binary-ops-highlight-colors.c 
b/gcc/testsuite/gcc.dg/bad-binary-ops-highlight-colors.c
index efbcdf39c502..da98f1a4140e 100644
--- a/gcc/testsuite/gcc.dg/bad-binary-ops-highlight-colors.c
+++ b/gcc/testsuite/gcc.dg/bad-binary-ops-highlight-colors.c
@@ -23,8 +23,8 @@ int test_4 (void)
      { dg-end-multiline-output "" } */
 
   /* { dg-begin-multiline-output "" }
-   return 
callee_4a
 () + 
callee_4b
 ();
-          
~~~~~~~~~~~~
 ^ 
~~~~~~~~~~~~
+   return callee_4a () + 
callee_4b ();
+          ~~~~~~~~~~~~ ^ 
~~~~~~~~~~~~
           |              |
           |              T {aka struct t}
           S {aka struct s}
diff --git a/gcc/testsuite/gcc.dg/format/colors.c 
b/gcc/testsuite/gcc.dg/format/colors.c
index 43484b70a2a1..42cfd5043d42 100644
--- a/gcc/testsuite/gcc.dg/format/colors.c
+++ b/gcc/testsuite/gcc.dg/format/colors.c
@@ -15,8 +15,8 @@ void test_mismatching_types (const char *msg)
 warning: format '%i' expects argument of 
type 'int', but argument 2 has type 
'const char *' [-Wformat=]
    { dg-end-multiline-output "" } */
 /* { dg-begin-multiline-output "" }
-   printf("hello %i", 
msg);
-                 ~^   
~~~
+   printf("hello %i", msg);
+                 ~^   ~~~
                   |   |
                   int const char *
                  %s
-- 
2.26.3

Reply via email to