Skip to content

Commit 4b02dd4

Browse files
committed
analyzer: respect GCC_COLORS in out-of-bounds diagrams [PR114588]
gcc/analyzer/ChangeLog: PR analyzer/114588 * access-diagram.cc (access_diagram_impl::access_diagram_impl): Replace hardcoded colors for valid_style and invalid_style with calls to text_art::get_style_from_color_cap_name. gcc/ChangeLog: PR analyzer/114588 * diagnostic-color.cc (color_dict): Add "valid" and "invalid" as color capability names. * doc/invoke.texi: Document them in description of GCC_COLORS. * text-art/style.cc: Include "diagnostic-color.h". (text_art::get_style_from_color_cap_name): New. * text-art/types.h (get_style_from_color_cap_name): New decl. Signed-off-by: David Malcolm <[email protected]>
1 parent 75b49c0 commit 4b02dd4

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

gcc/analyzer/access-diagram.cc

+2-6
Original file line numberDiff line numberDiff line change
@@ -2059,14 +2059,10 @@ class access_diagram_impl : public vbox_widget
20592059

20602060
/* Register painting styles. */
20612061
{
2062-
style valid_style;
2063-
valid_style.m_fg_color = style::named_color::GREEN;
2064-
valid_style.m_bold = true;
2062+
style valid_style (get_style_from_color_cap_name ("valid"));
20652063
m_valid_style_id = m_sm.get_or_create_id (valid_style);
20662064

2067-
style invalid_style;
2068-
invalid_style.m_fg_color = style::named_color::RED;
2069-
invalid_style.m_bold = true;
2065+
style invalid_style (get_style_from_color_cap_name ("invalid"));
20702066
m_invalid_style_id = m_sm.get_or_create_id (invalid_style);
20712067
}
20722068

gcc/diagnostic-color.cc

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ static struct color_cap color_dict[] =
101101
{ "diff-delete", SGR_SEQ (COLOR_FG_RED), 11, false },
102102
{ "diff-insert", SGR_SEQ (COLOR_FG_GREEN), 11, false },
103103
{ "type-diff", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN), 9, false },
104+
{ "valid", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN), 5, false },
105+
{ "invalid", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_RED), 7, false },
104106
{ NULL, NULL, 0, false }
105107
};
106108

gcc/doc/invoke.texi

+9-1
Original file line numberDiff line numberDiff line change
@@ -5244,7 +5244,7 @@ The default @env{GCC_COLORS} is
52445244
error=01;31:warning=01;35:note=01;36:range1=32:range2=34:locus=01:\
52455245
quote=01:path=01;36:fixit-insert=32:fixit-delete=31:\
52465246
diff-filename=01:diff-hunk=32:diff-delete=31:diff-insert=32:\
5247-
type-diff=01;32:fnname=01;32:targs=35
5247+
type-diff=01;32:fnname=01;32:targs=35:valid=01;31:invalid=01;32
52485248
@end smallexample
52495249
@noindent
52505250
where @samp{01;31} is bold red, @samp{01;35} is bold magenta,
@@ -5327,6 +5327,14 @@ SGR substring for inserted lines within generated patches.
53275327
@item type-diff=
53285328
SGR substring for highlighting mismatching types within template
53295329
arguments in the C++ frontend.
5330+
5331+
@vindex valid GCC_COLORS @r{capability}
5332+
@item valid=
5333+
SGR substring for highlighting valid elements within text art diagrams.
5334+
5335+
@vindex invalid GCC_COLORS @r{capability}
5336+
@item invalid=
5337+
SGR substring for highlighting invalid elements within text art diagrams.
53305338
@end table
53315339

53325340
@opindex fdiagnostics-urls

gcc/text-art/style.cc

+18
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ along with GCC; see the file COPYING3. If not see
3131
#include "text-art/selftests.h"
3232
#include "text-art/types.h"
3333
#include "color-macros.h"
34+
#include "diagnostic-color.h"
3435

3536
using namespace text_art;
3637

@@ -256,6 +257,23 @@ style::print_changes (pretty_printer *pp,
256257
}
257258
}
258259

260+
/* Look up the current SGR codes for a color capability NAME
261+
(from GCC_COLORS or the defaults), and convert them to
262+
a text_art::style. */
263+
264+
style
265+
text_art::get_style_from_color_cap_name (const char *name)
266+
{
267+
const char *sgr_codes = colorize_start (true, name);
268+
gcc_assert (sgr_codes);
269+
270+
/* Parse the sgr codes. We expect the resulting styled_string to be
271+
empty; we're interested in the final style created during parsing. */
272+
style_manager sm;
273+
styled_string styled_str (sm, sgr_codes);
274+
return sm.get_style (sm.get_num_styles () - 1);
275+
}
276+
259277
/* class text_art::style_manager. */
260278

261279
style_manager::style_manager ()

gcc/text-art/types.h

+2
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ struct style
332332
std::vector<cppchar_t> m_url; // empty = no URL
333333
};
334334

335+
extern style get_style_from_color_cap_name (const char *name);
336+
335337
/* A class to keep track of all the styles in use in a drawing, so that
336338
we can refer to them via the compact style::id_t type, rather than
337339
via e.g. pointers. */

0 commit comments

Comments
 (0)