gcc/gcc/diagnostic-color.c

312 lines
11 KiB
C
Raw Normal View History

opts.c: Include diagnostic-color.h. * opts.c: Include diagnostic-color.h. (common_handle_option): Handle OPT_fdiagnostics_color_. * Makefile.in (OBJS-libcommon): Add diagnostic-color.o. (diagnostic.o, opts.o, pretty-print.o): Depend on diagnostic-color.h. (diagnostic-color.o): New. * common.opt (fdiagnostics-color, fdiagnostics-color=): New options. (diagnostic_color_rule): New enum. * dwarf2out.c (gen_producer_string): Don't print -fdiagnostics-color*. * langhooks.c (lhd_print_error_function): Add %r "locus" and %R around the location string. * diagnostic.def: Add 3rd argument to DEFINE_DIAGNOSTIC_KIND macros, either NULL, or color kind. * diagnostic-color.c: New file. * diagnostic-color.h: New file. * diagnostic-core.h (DEFINE_DIAGNOSTIC_KIND): Adjust macro for 3 arguments. * doc/invoke.texi (-fdiagnostics-color): Document. * pretty-print.h (pp_show_color): Define. (struct pretty_print_info): Add show_color field. * diagnostic.c: Include diagnostic-color.h. (diagnostic_build_prefix): Adjust for 3 argument DEFINE_DIAGNOSTIC_KIND macros. Colorize error:, warning: etc. strings and also the location string. (diagnostic_show_locus): Colorize the caret line. * pretty-print.c: Include diagnostic-color.h. (pp_base_format): Handle %r and %R format specifiers. Colorize strings inside of %< %> quotes or quoted through q format modifier. c-family/ * c-format.c (gcc_diag_char_table, gcc_tdiag_char_table, gcc_cdiag_char_table, gcc_cxxdiag_char_table): Add %r and %R format specifiers. cp/ * error.c (cp_print_error_function, print_instantiation_partial_context_line, maybe_print_constexpr_context): Colorize locus strings. From-SVN: r197842
2013-04-12 08:20:36 +02:00
/* Output colorization.
Copyright (C) 2011-2017 Free Software Foundation, Inc.
opts.c: Include diagnostic-color.h. * opts.c: Include diagnostic-color.h. (common_handle_option): Handle OPT_fdiagnostics_color_. * Makefile.in (OBJS-libcommon): Add diagnostic-color.o. (diagnostic.o, opts.o, pretty-print.o): Depend on diagnostic-color.h. (diagnostic-color.o): New. * common.opt (fdiagnostics-color, fdiagnostics-color=): New options. (diagnostic_color_rule): New enum. * dwarf2out.c (gen_producer_string): Don't print -fdiagnostics-color*. * langhooks.c (lhd_print_error_function): Add %r "locus" and %R around the location string. * diagnostic.def: Add 3rd argument to DEFINE_DIAGNOSTIC_KIND macros, either NULL, or color kind. * diagnostic-color.c: New file. * diagnostic-color.h: New file. * diagnostic-core.h (DEFINE_DIAGNOSTIC_KIND): Adjust macro for 3 arguments. * doc/invoke.texi (-fdiagnostics-color): Document. * pretty-print.h (pp_show_color): Define. (struct pretty_print_info): Add show_color field. * diagnostic.c: Include diagnostic-color.h. (diagnostic_build_prefix): Adjust for 3 argument DEFINE_DIAGNOSTIC_KIND macros. Colorize error:, warning: etc. strings and also the location string. (diagnostic_show_locus): Colorize the caret line. * pretty-print.c: Include diagnostic-color.h. (pp_base_format): Handle %r and %R format specifiers. Colorize strings inside of %< %> quotes or quoted through q format modifier. c-family/ * c-format.c (gcc_diag_char_table, gcc_tdiag_char_table, gcc_cdiag_char_table, gcc_cxxdiag_char_table): Add %r and %R format specifiers. cp/ * error.c (cp_print_error_function, print_instantiation_partial_context_line, maybe_print_constexpr_context): Colorize locus strings. From-SVN: r197842
2013-04-12 08:20:36 +02:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
02110-1301, USA. */
#include "config.h"
#include "system.h"
#include "diagnostic-color.h"
/* Select Graphic Rendition (SGR, "\33[...m") strings. */
/* Also Erase in Line (EL) to Right ("\33[K") by default. */
/* Why have EL to Right after SGR?
-- The behavior of line-wrapping when at the bottom of the
terminal screen and at the end of the current line is often
such that a new line is introduced, entirely cleared with
the current background color which may be different from the
default one (see the boolean back_color_erase terminfo(5)
capability), thus scrolling the display by one line.
The end of this new line will stay in this background color
even after reverting to the default background color with
"\33[m', unless it is explicitly cleared again with "\33[K"
(which is the behavior the user would instinctively expect
from the whole thing). There may be some unavoidable
background-color flicker at the end of this new line because
of this (when timing with the monitor's redraw is just right).
-- The behavior of HT (tab, "\t") is usually the same as that of
Cursor Forward Tabulation (CHT) with a default parameter
of 1 ("\33[I"), i.e., it performs pure movement to the next
tab stop, without any clearing of either content or screen
attributes (including background color); try
printf 'asdfqwerzxcv\rASDF\tZXCV\n'
in a bash(1) shell to demonstrate this. This is not what the
user would instinctively expect of HT (but is ok for CHT).
The instinctive behavior would include clearing the terminal
cells that are skipped over by HT with blank cells in the
current screen attributes, including background color;
the boolean dest_tabs_magic_smso terminfo(5) capability
indicates this saner behavior for HT, but only some rare
terminals have it (although it also indicates a special
glitch with standout mode in the Teleray terminal for which
it was initially introduced). The remedy is to add "\33K"
after each SGR sequence, be it START (to fix the behavior
of any HT after that before another SGR) or END (to fix the
behavior of an HT in default background color that would
follow a line-wrapping at the bottom of the screen in another
background color, and to complement doing it after START).
Piping GCC's output through a pager such as less(1) avoids
any HT problems since the pager performs tab expansion.
Generic disadvantages of this remedy are:
-- Some very rare terminals might support SGR but not EL (nobody
will use "gcc -fdiagnostics-color" on a terminal that does not
support SGR in the first place).
-- Having these extra control sequences might somewhat complicate
the task of any program trying to parse "gcc -fdiagnostics-color"
output in order to extract structuring information from it.
A specific disadvantage to doing it after SGR START is:
-- Even more possible background color flicker (when timing
with the monitor's redraw is just right), even when not at the
bottom of the screen.
There are no additional disadvantages specific to doing it after
SGR END.
It would be impractical for GCC to become a full-fledged
terminal program linked against ncurses or the like, so it will
not detect terminfo(5) capabilities. */
#define COLOR_SEPARATOR ";"
#define COLOR_NONE "00"
#define COLOR_BOLD "01"
#define COLOR_UNDERSCORE "04"
#define COLOR_BLINK "05"
#define COLOR_REVERSE "07"
#define COLOR_FG_BLACK "30"
#define COLOR_FG_RED "31"
#define COLOR_FG_GREEN "32"
#define COLOR_FG_YELLOW "33"
#define COLOR_FG_BLUE "34"
#define COLOR_FG_MAGENTA "35"
#define COLOR_FG_CYAN "36"
#define COLOR_FG_WHITE "37"
#define COLOR_BG_BLACK "40"
#define COLOR_BG_RED "41"
#define COLOR_BG_GREEN "42"
#define COLOR_BG_YELLOW "43"
#define COLOR_BG_BLUE "44"
#define COLOR_BG_MAGENTA "45"
#define COLOR_BG_CYAN "46"
#define COLOR_BG_WHITE "47"
#define SGR_START "\33["
#define SGR_END "m\33[K"
#define SGR_SEQ(str) SGR_START str SGR_END
#define SGR_RESET SGR_SEQ("")
/* The context and logic for choosing default --color screen attributes
(foreground and background colors, etc.) are the following.
-- There are eight basic colors available, each with its own
nominal luminosity to the human eye and foreground/background
codes (black [0 %, 30/40], blue [11 %, 34/44], red [30 %, 31/41],
magenta [41 %, 35/45], green [59 %, 32/42], cyan [70 %, 36/46],
yellow [89 %, 33/43], and white [100 %, 37/47]).
-- Sometimes, white as a background is actually implemented using
a shade of light gray, so that a foreground white can be visible
on top of it (but most often not).
-- Sometimes, black as a foreground is actually implemented using
a shade of dark gray, so that it can be visible on top of a
background black (but most often not).
-- Sometimes, more colors are available, as extensions.
-- Other attributes can be selected/deselected (bold [1/22],
underline [4/24], standout/inverse [7/27], blink [5/25], and
invisible/hidden [8/28]). They are sometimes implemented by
using colors instead of what their names imply; e.g., bold is
often achieved by using brighter colors. In practice, only bold
is really available to us, underline sometimes being mapped by
the terminal to some strange color choice, and standout best
being left for use by downstream programs such as less(1).
-- We cannot assume that any of the extensions or special features
are available for the purpose of choosing defaults for everyone.
-- The most prevalent default terminal backgrounds are pure black
and pure white, and are not necessarily the same shades of
those as if they were selected explicitly with SGR sequences.
Some terminals use dark or light pictures as default background,
but those are covered over by an explicit selection of background
color with an SGR sequence; their users will appreciate their
background pictures not be covered like this, if possible.
-- Some uses of colors attributes is to make some output items
more understated (e.g., context lines); this cannot be achieved
by changing the background color.
-- For these reasons, the GCC color defaults should strive not
to change the background color from its default, unless it's
for a short item that should be highlighted, not understated.
-- The GCC foreground color defaults (without an explicitly set
background) should provide enough contrast to be readable on any
terminal with either a black (dark) or white (light) background.
This only leaves red, magenta, green, and cyan (and their bold
counterparts) and possibly bold blue. */
/* Default colors. The user can overwrite them using environment
variable GCC_COLORS. */
struct color_cap
{
const char *name;
const char *val;
unsigned char name_len;
bool free_val;
};
/* For GCC_COLORS. */
static struct color_cap color_dict[] =
{
{ "error", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_RED), 5, false },
{ "warning", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_MAGENTA),
7, false },
{ "note", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_CYAN), 4, false },
Reimplement diagnostic_show_locus, introducing rich_location classes gcc/ChangeLog: * diagnostic-color.c (color_dict): Eliminate "caret"; add "range1" and "range2". (parse_gcc_colors): Update comment to describe default GCC_COLORS. * diagnostic-core.h (warning_at_rich_loc): New declaration. (error_at_rich_loc): New declaration. (permerror_at_rich_loc): New declaration. (inform_at_rich_loc): New declaration. * diagnostic-show-locus.c (adjust_line): Delete. (struct point_state): New struct. (class colorizer): New class. (class layout_point): New class. (class layout_range): New class. (struct line_bounds): New. (class layout): New class. (colorizer::colorizer): New ctor. (colorizer::~colorizer): New dtor. (layout::layout): New ctor. (layout::print_source_line): New method. (layout::print_annotation_line): New method. (layout::get_state_at_point): New method. (layout::get_x_bound_for_row): New method. (diagnostic_show_locus): Reimplement in terms of class layout. (diagnostic_print_caret_line): Delete. * diagnostic.c (diagnostic_initialize): Replace MAX_LOCATIONS_PER_MESSAGE with rich_location::MAX_RANGES. (diagnostic_set_info_translated): Convert param from location_t to rich_location *. Eliminate calls to set_location on the message in favor of storing the rich_location ptr there. (diagnostic_set_info): Convert param from location_t to rich_location *. (diagnostic_build_prefix): Break out array into... (diagnostic_kind_color): New variable. (diagnostic_get_color_for_kind): New function. (diagnostic_report_diagnostic): Colorize the option_text using the color for the severity. (diagnostic_append_note): Update for change in signature of diagnostic_set_info. (diagnostic_append_note_at_rich_loc): New function. (emit_diagnostic): Update for change in signature of diagnostic_set_info. (inform): Likewise. (inform_at_rich_loc): New function. (inform_n): Update for change in signature of diagnostic_set_info. (warning): Likewise. (warning_at): Likewise. (warning_at_rich_loc): New function. (warning_n): Update for change in signature of diagnostic_set_info. (pedwarn): Likewise. (permerror): Likewise. (permerror_at_rich_loc): New function. (error): Update for change in signature of diagnostic_set_info. (error_n): Likewise. (error_at): Likewise. (error_at_rich_loc): New function. (sorry): Update for change in signature of diagnostic_set_info. (fatal_error): Likewise. (internal_error): Likewise. (internal_error_no_backtrace): Likewise. (source_range::debug): New function. * diagnostic.h (struct diagnostic_info): Eliminate field "override_column". Add field "richloc". (struct diagnostic_context): Add field "colorize_source_p". (diagnostic_override_column): Delete. (diagnostic_set_info): Convert param from location_t to rich_location *. (diagnostic_set_info_translated): Likewise. (diagnostic_append_note_at_rich_loc): New function. (diagnostic_num_locations): New function. (diagnostic_expand_location): Get the location from the rich_location. (diagnostic_print_caret_line): Delete. (diagnostic_get_color_for_kind): New declaration. * genmatch.c (linemap_client_expand_location_to_spelling_point): New. (error_cb): Update for change in signature of "error" callback. (fatal_at): Likewise. (warning_at): Likewise. * input.c (linemap_client_expand_location_to_spelling_point): New. * pretty-print.c (text_info::set_range): New method. (text_info::get_location): New method. * pretty-print.h (MAX_LOCATIONS_PER_MESSAGE): Eliminate this macro. (struct text_info): Eliminate "locations" array in favor of "m_richloc", a rich_location *. (textinfo::set_location): Add a "caret_p" param, and reimplement in terms of a call to set_range. (textinfo::get_location): Eliminate inline implementation in favor of an out-of-line reimplementation. (textinfo::set_range): New method. * rtl-error.c (diagnostic_for_asm): Update for change in signature of diagnostic_set_info. * tree-diagnostic.c (default_tree_printer): Update for new "caret_p" param for textinfo::set_location. * tree-pretty-print.c (percent_K_format): Likewise. gcc/c-family/ChangeLog: * c-common.c (c_cpp_error): Convert parameter from location_t to rich_location *. Eliminate the "column_override" parameter and the call to diagnostic_override_column. Update the "done_lexing" clause to set range 0 on the rich_location, rather than overwriting a location_t. * c-common.h (c_cpp_error): Convert parameter from location_t to rich_location *. Eliminate the "column_override" parameter. gcc/c/ChangeLog: * c-decl.c (warn_defaults_to): Update for change in signature of diagnostic_set_info. * c-errors.c (pedwarn_c99): Likewise. (pedwarn_c90): Likewise. * c-objc-common.c (c_tree_printer): Update for new "caret_p" param for textinfo::set_location. gcc/cp/ChangeLog: * error.c (cp_printer): Update for new "caret_p" param for textinfo::set_location. (pedwarn_cxx98): Update for change in signature of diagnostic_set_info. gcc/fortran/ChangeLog: * cpp.c (cb_cpp_error): Convert parameter from location_t to rich_location *. Eliminate the "column_override" parameter. * error.c (gfc_warning): Update for change in signature of diagnostic_set_info. (gfc_format_decoder): Update handling of %C/%L for changes to struct text_info. (gfc_diagnostic_starter): Use richloc when determining whether to print one locus or two. When handling a location that will involve a call to diagnostic_show_locus, only attempt to print the locus for the primary location, and don't call into diagnostic_print_caret_line. (gfc_warning_now_at): Update for change in signature of diagnostic_set_info. (gfc_warning_now): Likewise. (gfc_error_now): Likewise. (gfc_fatal_error): Likewise. (gfc_error): Likewise. (gfc_internal_error): Likewise. gcc/testsuite/ChangeLog: * gcc.dg/plugin/diagnostic-test-show-locus-bw.c: New file. * gcc.dg/plugin/diagnostic-test-show-locus-color.c: New file. * gcc.dg/plugin/diagnostic_plugin_test_show_locus.c: New file. * gcc.dg/plugin/plugin.exp (plugin_test_list): Add the above. * lib/gcc-dg.exp: Load multiline.exp. libcpp/ChangeLog: * errors.c (cpp_diagnostic): Update for change in signature of "error" callback. (cpp_diagnostic_with_line): Likewise, calling override_column on the rich_location. * include/cpplib.h (struct cpp_callbacks): Within "error" callback, convert param from source_location to rich_location *, and drop column_override param. * include/line-map.h (struct source_range): New struct. (struct location_range): New struct. (class rich_location): New class. (linemap_client_expand_location_to_spelling_point): New declaration. * line-map.c (rich_location::rich_location): New ctors. (rich_location::lazily_expand_location): New method. (rich_location::override_column): New method. (rich_location::add_range): New methods. (rich_location::set_range): New method. From-SVN: r229884
2015-11-06 20:50:50 +01:00
{ "range1", SGR_SEQ (COLOR_FG_GREEN), 6, false },
{ "range2", SGR_SEQ (COLOR_FG_BLUE), 6, false },
opts.c: Include diagnostic-color.h. * opts.c: Include diagnostic-color.h. (common_handle_option): Handle OPT_fdiagnostics_color_. * Makefile.in (OBJS-libcommon): Add diagnostic-color.o. (diagnostic.o, opts.o, pretty-print.o): Depend on diagnostic-color.h. (diagnostic-color.o): New. * common.opt (fdiagnostics-color, fdiagnostics-color=): New options. (diagnostic_color_rule): New enum. * dwarf2out.c (gen_producer_string): Don't print -fdiagnostics-color*. * langhooks.c (lhd_print_error_function): Add %r "locus" and %R around the location string. * diagnostic.def: Add 3rd argument to DEFINE_DIAGNOSTIC_KIND macros, either NULL, or color kind. * diagnostic-color.c: New file. * diagnostic-color.h: New file. * diagnostic-core.h (DEFINE_DIAGNOSTIC_KIND): Adjust macro for 3 arguments. * doc/invoke.texi (-fdiagnostics-color): Document. * pretty-print.h (pp_show_color): Define. (struct pretty_print_info): Add show_color field. * diagnostic.c: Include diagnostic-color.h. (diagnostic_build_prefix): Adjust for 3 argument DEFINE_DIAGNOSTIC_KIND macros. Colorize error:, warning: etc. strings and also the location string. (diagnostic_show_locus): Colorize the caret line. * pretty-print.c: Include diagnostic-color.h. (pp_base_format): Handle %r and %R format specifiers. Colorize strings inside of %< %> quotes or quoted through q format modifier. c-family/ * c-format.c (gcc_diag_char_table, gcc_tdiag_char_table, gcc_cdiag_char_table, gcc_cxxdiag_char_table): Add %r and %R format specifiers. cp/ * error.c (cp_print_error_function, print_instantiation_partial_context_line, maybe_print_constexpr_context): Colorize locus strings. From-SVN: r197842
2013-04-12 08:20:36 +02:00
{ "locus", SGR_SEQ (COLOR_BOLD), 5, false },
{ "quote", SGR_SEQ (COLOR_BOLD), 5, false },
Tweak to colors of fix-it hints Previous, fix-it hints were printed using the color of the severity of the diagnostic (magenta for warnings, red for errors, cyan for notes). This patch updates fix-it hints so that replacement text is printed in green, to better distinguish the suggested improvement from the current code. For example: spellcheck-fields.cc:52:13: error: 'struct s' has no member named 'colour'; did you mean 'color'? return ptr->colour; <<< RED ^~~~~~ <<< RED color <<< GREEN It makes sense for the underlinings that indicate deletions to be printed in red, so the patch changes that also. For example: diagnostic-test-show-locus-color.c:179:9: warning: example of a removal hint int a;; <<< MAGENTA ^ <<< MAGENTA - <<< RED gcc/ChangeLog: * diagnostic-color.c (color_dict): Add "fixit-insert" and "fixit-delete". (parse_gcc_colors): Update description of default GCC_COLORS. * diagnostic-show-locus.c (colorizer::set_fixit_hint): Delete. (colorizer::set_fixit_insert): New method. (colorizer::set_fixit_delete): New method. (colorizer::get_color_by_name): New method. (colorizer::STATE_FIXIT_INSERT): New constant. (colorizer::STATE_FIXIT_DELETE): New constant. (class colorizer): Drop "_cs" suffix from fields. Delete "_ce" fields in favor of new field "m_stop_color". Add fields "m_fixit_insert" and "m_fixit_delete". (colorizer::colorizer): Update for above changes. Replace colorize_start calls with calls to get_color_by_name. (colorizer::begin_state): Handle STATE_FIXIT_INSERT and STATE_FIXIT_DELETE. Update for field renamings. (colorizer::finish_state): Simplify by using m_stop_color, rather than multiple identical "*_ce" fields. (colorizer::get_color_by_name): New method. (layout::print_any_fixits): Print insertions and replacements using the "fixit-insert" color, and deletions using the "fixit-delete" color. * doc/invoke.texi (-fdiagnostics-color): Update description of default GCC_COLORS, and of the supported capabilities. gcc/testsuite/ChangeLog: * gcc.dg/plugin/diagnostic-test-show-locus-color.c (test_fixit_insert): Update expected output. (test_fixit_remove): Likewise. (test_fixit_replace): Likewise. From-SVN: r239787
2016-08-26 19:59:08 +02:00
{ "fixit-insert", SGR_SEQ (COLOR_FG_GREEN), 12, false },
{ "fixit-delete", SGR_SEQ (COLOR_FG_RED), 12, false },
{ "diff-filename", SGR_SEQ (COLOR_BOLD), 13, false },
{ "diff-hunk", SGR_SEQ (COLOR_FG_CYAN), 9, false },
{ "diff-delete", SGR_SEQ (COLOR_FG_RED), 11, false },
{ "diff-insert", SGR_SEQ (COLOR_FG_GREEN), 11, false },
opts.c: Include diagnostic-color.h. * opts.c: Include diagnostic-color.h. (common_handle_option): Handle OPT_fdiagnostics_color_. * Makefile.in (OBJS-libcommon): Add diagnostic-color.o. (diagnostic.o, opts.o, pretty-print.o): Depend on diagnostic-color.h. (diagnostic-color.o): New. * common.opt (fdiagnostics-color, fdiagnostics-color=): New options. (diagnostic_color_rule): New enum. * dwarf2out.c (gen_producer_string): Don't print -fdiagnostics-color*. * langhooks.c (lhd_print_error_function): Add %r "locus" and %R around the location string. * diagnostic.def: Add 3rd argument to DEFINE_DIAGNOSTIC_KIND macros, either NULL, or color kind. * diagnostic-color.c: New file. * diagnostic-color.h: New file. * diagnostic-core.h (DEFINE_DIAGNOSTIC_KIND): Adjust macro for 3 arguments. * doc/invoke.texi (-fdiagnostics-color): Document. * pretty-print.h (pp_show_color): Define. (struct pretty_print_info): Add show_color field. * diagnostic.c: Include diagnostic-color.h. (diagnostic_build_prefix): Adjust for 3 argument DEFINE_DIAGNOSTIC_KIND macros. Colorize error:, warning: etc. strings and also the location string. (diagnostic_show_locus): Colorize the caret line. * pretty-print.c: Include diagnostic-color.h. (pp_base_format): Handle %r and %R format specifiers. Colorize strings inside of %< %> quotes or quoted through q format modifier. c-family/ * c-format.c (gcc_diag_char_table, gcc_tdiag_char_table, gcc_cdiag_char_table, gcc_cxxdiag_char_table): Add %r and %R format specifiers. cp/ * error.c (cp_print_error_function, print_instantiation_partial_context_line, maybe_print_constexpr_context): Colorize locus strings. From-SVN: r197842
2013-04-12 08:20:36 +02:00
{ NULL, NULL, 0, false }
};
const char *
colorize_start (bool show_color, const char *name, size_t name_len)
{
struct color_cap const *cap;
if (!show_color)
return "";
for (cap = color_dict; cap->name; cap++)
if (cap->name_len == name_len
&& memcmp (cap->name, name, name_len) == 0)
break;
if (cap->name == NULL)
return "";
return cap->val;
}
const char *
colorize_stop (bool show_color)
{
return show_color ? SGR_RESET : "";
}
/* Parse GCC_COLORS. The default would look like:
Tweak to colors of fix-it hints Previous, fix-it hints were printed using the color of the severity of the diagnostic (magenta for warnings, red for errors, cyan for notes). This patch updates fix-it hints so that replacement text is printed in green, to better distinguish the suggested improvement from the current code. For example: spellcheck-fields.cc:52:13: error: 'struct s' has no member named 'colour'; did you mean 'color'? return ptr->colour; <<< RED ^~~~~~ <<< RED color <<< GREEN It makes sense for the underlinings that indicate deletions to be printed in red, so the patch changes that also. For example: diagnostic-test-show-locus-color.c:179:9: warning: example of a removal hint int a;; <<< MAGENTA ^ <<< MAGENTA - <<< RED gcc/ChangeLog: * diagnostic-color.c (color_dict): Add "fixit-insert" and "fixit-delete". (parse_gcc_colors): Update description of default GCC_COLORS. * diagnostic-show-locus.c (colorizer::set_fixit_hint): Delete. (colorizer::set_fixit_insert): New method. (colorizer::set_fixit_delete): New method. (colorizer::get_color_by_name): New method. (colorizer::STATE_FIXIT_INSERT): New constant. (colorizer::STATE_FIXIT_DELETE): New constant. (class colorizer): Drop "_cs" suffix from fields. Delete "_ce" fields in favor of new field "m_stop_color". Add fields "m_fixit_insert" and "m_fixit_delete". (colorizer::colorizer): Update for above changes. Replace colorize_start calls with calls to get_color_by_name. (colorizer::begin_state): Handle STATE_FIXIT_INSERT and STATE_FIXIT_DELETE. Update for field renamings. (colorizer::finish_state): Simplify by using m_stop_color, rather than multiple identical "*_ce" fields. (colorizer::get_color_by_name): New method. (layout::print_any_fixits): Print insertions and replacements using the "fixit-insert" color, and deletions using the "fixit-delete" color. * doc/invoke.texi (-fdiagnostics-color): Update description of default GCC_COLORS, and of the supported capabilities. gcc/testsuite/ChangeLog: * gcc.dg/plugin/diagnostic-test-show-locus-color.c (test_fixit_insert): Update expected output. (test_fixit_remove): Likewise. (test_fixit_replace): Likewise. From-SVN: r239787
2016-08-26 19:59:08 +02:00
GCC_COLORS='error=01;31:warning=01;35:note=01;36:\
range1=32:range2=34:locus=01:quote=01:\
fixit-insert=32:fixit-delete=31'\
diff-filename=01:diff-hunk=32:diff-delete=31:diff-insert=32'
opts.c: Include diagnostic-color.h. * opts.c: Include diagnostic-color.h. (common_handle_option): Handle OPT_fdiagnostics_color_. * Makefile.in (OBJS-libcommon): Add diagnostic-color.o. (diagnostic.o, opts.o, pretty-print.o): Depend on diagnostic-color.h. (diagnostic-color.o): New. * common.opt (fdiagnostics-color, fdiagnostics-color=): New options. (diagnostic_color_rule): New enum. * dwarf2out.c (gen_producer_string): Don't print -fdiagnostics-color*. * langhooks.c (lhd_print_error_function): Add %r "locus" and %R around the location string. * diagnostic.def: Add 3rd argument to DEFINE_DIAGNOSTIC_KIND macros, either NULL, or color kind. * diagnostic-color.c: New file. * diagnostic-color.h: New file. * diagnostic-core.h (DEFINE_DIAGNOSTIC_KIND): Adjust macro for 3 arguments. * doc/invoke.texi (-fdiagnostics-color): Document. * pretty-print.h (pp_show_color): Define. (struct pretty_print_info): Add show_color field. * diagnostic.c: Include diagnostic-color.h. (diagnostic_build_prefix): Adjust for 3 argument DEFINE_DIAGNOSTIC_KIND macros. Colorize error:, warning: etc. strings and also the location string. (diagnostic_show_locus): Colorize the caret line. * pretty-print.c: Include diagnostic-color.h. (pp_base_format): Handle %r and %R format specifiers. Colorize strings inside of %< %> quotes or quoted through q format modifier. c-family/ * c-format.c (gcc_diag_char_table, gcc_tdiag_char_table, gcc_cdiag_char_table, gcc_cxxdiag_char_table): Add %r and %R format specifiers. cp/ * error.c (cp_print_error_function, print_instantiation_partial_context_line, maybe_print_constexpr_context): Colorize locus strings. From-SVN: r197842
2013-04-12 08:20:36 +02:00
No character escaping is needed or supported. */
static bool
parse_gcc_colors (void)
{
const char *p, *q, *name, *val;
char *b;
size_t name_len = 0, val_len = 0;
p = getenv ("GCC_COLORS"); /* Plural! */
if (p == NULL)
return true;
if (*p == '\0')
return false;
name = q = p;
val = NULL;
/* From now on, be well-formed or you're gone. */
for (;;)
if (*q == ':' || *q == '\0')
{
struct color_cap *cap;
if (val)
val_len = q - val;
else
name_len = q - name;
/* Empty name without val (empty cap)
won't match and will be ignored. */
for (cap = color_dict; cap->name; cap++)
if (cap->name_len == name_len
&& memcmp (cap->name, name, name_len) == 0)
break;
/* If name unknown, go on for forward compatibility. */
if (cap->val && val)
{
if (cap->free_val)
free (CONST_CAST (char *, cap->val));
b = XNEWVEC (char, val_len + sizeof (SGR_SEQ ("")));
memcpy (b, SGR_START, strlen (SGR_START));
memcpy (b + strlen (SGR_START), val, val_len);
memcpy (b + strlen (SGR_START) + val_len, SGR_END,
sizeof (SGR_END));
cap->val = (const char *) b;
cap->free_val = true;
}
if (*q == '\0')
return true;
name = ++q;
val = NULL;
}
else if (*q == '=')
{
if (q == name || val)
return true;
name_len = q - name;
val = ++q; /* Can be the empty string. */
}
else if (val == NULL)
q++; /* Accumulate name. */
else if (*q == ';' || (*q >= '0' && *q <= '9'))
q++; /* Accumulate val. Protect the terminal from being sent
garbage. */
else
return true;
}
#if defined(_WIN32)
bool
colorize_init (diagnostic_color_rule_t)
opts.c: Include diagnostic-color.h. * opts.c: Include diagnostic-color.h. (common_handle_option): Handle OPT_fdiagnostics_color_. * Makefile.in (OBJS-libcommon): Add diagnostic-color.o. (diagnostic.o, opts.o, pretty-print.o): Depend on diagnostic-color.h. (diagnostic-color.o): New. * common.opt (fdiagnostics-color, fdiagnostics-color=): New options. (diagnostic_color_rule): New enum. * dwarf2out.c (gen_producer_string): Don't print -fdiagnostics-color*. * langhooks.c (lhd_print_error_function): Add %r "locus" and %R around the location string. * diagnostic.def: Add 3rd argument to DEFINE_DIAGNOSTIC_KIND macros, either NULL, or color kind. * diagnostic-color.c: New file. * diagnostic-color.h: New file. * diagnostic-core.h (DEFINE_DIAGNOSTIC_KIND): Adjust macro for 3 arguments. * doc/invoke.texi (-fdiagnostics-color): Document. * pretty-print.h (pp_show_color): Define. (struct pretty_print_info): Add show_color field. * diagnostic.c: Include diagnostic-color.h. (diagnostic_build_prefix): Adjust for 3 argument DEFINE_DIAGNOSTIC_KIND macros. Colorize error:, warning: etc. strings and also the location string. (diagnostic_show_locus): Colorize the caret line. * pretty-print.c: Include diagnostic-color.h. (pp_base_format): Handle %r and %R format specifiers. Colorize strings inside of %< %> quotes or quoted through q format modifier. c-family/ * c-format.c (gcc_diag_char_table, gcc_tdiag_char_table, gcc_cdiag_char_table, gcc_cxxdiag_char_table): Add %r and %R format specifiers. cp/ * error.c (cp_print_error_function, print_instantiation_partial_context_line, maybe_print_constexpr_context): Colorize locus strings. From-SVN: r197842
2013-04-12 08:20:36 +02:00
{
return false;
}
#else
/* Return true if we should use color when in auto mode, false otherwise. */
static bool
should_colorize (void)
{
char const *t = getenv ("TERM");
return t && strcmp (t, "dumb") != 0 && isatty (STDERR_FILENO);
}
bool
colorize_init (diagnostic_color_rule_t rule)
{
switch (rule)
{
case DIAGNOSTICS_COLOR_NO:
return false;
case DIAGNOSTICS_COLOR_YES:
return parse_gcc_colors ();
case DIAGNOSTICS_COLOR_AUTO:
if (should_colorize ())
return parse_gcc_colors ();
else
return false;
default:
gcc_unreachable ();
}
}
#endif