]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/diagnostic-color.c
Update copyright years.
[thirdparty/gcc.git] / gcc / diagnostic-color.c
CommitLineData
f3065bdb 1/* Output colorization.
8d9254fc 2 Copyright (C) 2011-2020 Free Software Foundation, Inc.
f3065bdb
JJ
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
17 02110-1301, USA. */
18
19#include "config.h"
20#include "system.h"
21#include "diagnostic-color.h"
d2608235 22#include "diagnostic-url.h"
f3065bdb 23
db0d1bae
LH
24#ifdef __MINGW32__
25# include <windows.h>
26#endif
27
28f4a4a8 28#include "color-macros.h"
f3065bdb
JJ
29
30/* The context and logic for choosing default --color screen attributes
31 (foreground and background colors, etc.) are the following.
32 -- There are eight basic colors available, each with its own
33 nominal luminosity to the human eye and foreground/background
34 codes (black [0 %, 30/40], blue [11 %, 34/44], red [30 %, 31/41],
35 magenta [41 %, 35/45], green [59 %, 32/42], cyan [70 %, 36/46],
36 yellow [89 %, 33/43], and white [100 %, 37/47]).
37 -- Sometimes, white as a background is actually implemented using
38 a shade of light gray, so that a foreground white can be visible
39 on top of it (but most often not).
40 -- Sometimes, black as a foreground is actually implemented using
41 a shade of dark gray, so that it can be visible on top of a
42 background black (but most often not).
43 -- Sometimes, more colors are available, as extensions.
44 -- Other attributes can be selected/deselected (bold [1/22],
45 underline [4/24], standout/inverse [7/27], blink [5/25], and
46 invisible/hidden [8/28]). They are sometimes implemented by
47 using colors instead of what their names imply; e.g., bold is
48 often achieved by using brighter colors. In practice, only bold
49 is really available to us, underline sometimes being mapped by
50 the terminal to some strange color choice, and standout best
51 being left for use by downstream programs such as less(1).
52 -- We cannot assume that any of the extensions or special features
53 are available for the purpose of choosing defaults for everyone.
54 -- The most prevalent default terminal backgrounds are pure black
55 and pure white, and are not necessarily the same shades of
56 those as if they were selected explicitly with SGR sequences.
57 Some terminals use dark or light pictures as default background,
58 but those are covered over by an explicit selection of background
59 color with an SGR sequence; their users will appreciate their
60 background pictures not be covered like this, if possible.
61 -- Some uses of colors attributes is to make some output items
62 more understated (e.g., context lines); this cannot be achieved
63 by changing the background color.
64 -- For these reasons, the GCC color defaults should strive not
65 to change the background color from its default, unless it's
66 for a short item that should be highlighted, not understated.
67 -- The GCC foreground color defaults (without an explicitly set
68 background) should provide enough contrast to be readable on any
69 terminal with either a black (dark) or white (light) background.
70 This only leaves red, magenta, green, and cyan (and their bold
71 counterparts) and possibly bold blue. */
72/* Default colors. The user can overwrite them using environment
73 variable GCC_COLORS. */
74struct color_cap
75{
76 const char *name;
77 const char *val;
78 unsigned char name_len;
79 bool free_val;
80};
81
82/* For GCC_COLORS. */
83static struct color_cap color_dict[] =
84{
85 { "error", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_RED), 5, false },
86 { "warning", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_MAGENTA),
87 7, false },
88 { "note", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_CYAN), 4, false },
8a645150
DM
89 { "range1", SGR_SEQ (COLOR_FG_GREEN), 6, false },
90 { "range2", SGR_SEQ (COLOR_FG_BLUE), 6, false },
f3065bdb
JJ
91 { "locus", SGR_SEQ (COLOR_BOLD), 5, false },
92 { "quote", SGR_SEQ (COLOR_BOLD), 5, false },
d41e76cf
DM
93 { "fixit-insert", SGR_SEQ (COLOR_FG_GREEN), 12, false },
94 { "fixit-delete", SGR_SEQ (COLOR_FG_RED), 12, false },
c65236d6
DM
95 { "diff-filename", SGR_SEQ (COLOR_BOLD), 13, false },
96 { "diff-hunk", SGR_SEQ (COLOR_FG_CYAN), 9, false },
97 { "diff-delete", SGR_SEQ (COLOR_FG_RED), 11, false },
98 { "diff-insert", SGR_SEQ (COLOR_FG_GREEN), 11, false },
f012c8ef 99 { "type-diff", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN), 9, false },
f3065bdb
JJ
100 { NULL, NULL, 0, false }
101};
102
103const char *
104colorize_start (bool show_color, const char *name, size_t name_len)
105{
106 struct color_cap const *cap;
107
108 if (!show_color)
109 return "";
110
111 for (cap = color_dict; cap->name; cap++)
112 if (cap->name_len == name_len
113 && memcmp (cap->name, name, name_len) == 0)
114 break;
115 if (cap->name == NULL)
116 return "";
117
118 return cap->val;
119}
120
121const char *
122colorize_stop (bool show_color)
123{
124 return show_color ? SGR_RESET : "";
125}
126
127/* Parse GCC_COLORS. The default would look like:
d41e76cf
DM
128 GCC_COLORS='error=01;31:warning=01;35:note=01;36:\
129 range1=32:range2=34:locus=01:quote=01:\
f012c8ef
DM
130 fixit-insert=32:fixit-delete=31:'\
131 diff-filename=01:diff-hunk=32:diff-delete=31:diff-insert=32:\
132 type-diff=01;32'
f3065bdb
JJ
133 No character escaping is needed or supported. */
134static bool
135parse_gcc_colors (void)
136{
137 const char *p, *q, *name, *val;
138 char *b;
139 size_t name_len = 0, val_len = 0;
140
141 p = getenv ("GCC_COLORS"); /* Plural! */
142 if (p == NULL)
143 return true;
144 if (*p == '\0')
145 return false;
146
147 name = q = p;
148 val = NULL;
149 /* From now on, be well-formed or you're gone. */
150 for (;;)
151 if (*q == ':' || *q == '\0')
152 {
153 struct color_cap *cap;
154
155 if (val)
156 val_len = q - val;
157 else
158 name_len = q - name;
159 /* Empty name without val (empty cap)
160 won't match and will be ignored. */
161 for (cap = color_dict; cap->name; cap++)
162 if (cap->name_len == name_len
163 && memcmp (cap->name, name, name_len) == 0)
164 break;
165 /* If name unknown, go on for forward compatibility. */
166 if (cap->val && val)
167 {
168 if (cap->free_val)
169 free (CONST_CAST (char *, cap->val));
170 b = XNEWVEC (char, val_len + sizeof (SGR_SEQ ("")));
171 memcpy (b, SGR_START, strlen (SGR_START));
172 memcpy (b + strlen (SGR_START), val, val_len);
173 memcpy (b + strlen (SGR_START) + val_len, SGR_END,
174 sizeof (SGR_END));
175 cap->val = (const char *) b;
176 cap->free_val = true;
177 }
178 if (*q == '\0')
179 return true;
180 name = ++q;
181 val = NULL;
182 }
183 else if (*q == '=')
184 {
185 if (q == name || val)
186 return true;
187
188 name_len = q - name;
189 val = ++q; /* Can be the empty string. */
190 }
191 else if (val == NULL)
192 q++; /* Accumulate name. */
193 else if (*q == ';' || (*q >= '0' && *q <= '9'))
194 q++; /* Accumulate val. Protect the terminal from being sent
195 garbage. */
196 else
197 return true;
198}
199
f3065bdb
JJ
200/* Return true if we should use color when in auto mode, false otherwise. */
201static bool
202should_colorize (void)
203{
db0d1bae
LH
204#ifdef __MINGW32__
205 /* For consistency reasons, one should check the handle returned by
206 _get_osfhandle(_fileno(stderr)) because the function
207 pp_write_text_to_stream() in pretty-print.c calls fputs() on
208 that stream. However, the code below for non-Windows doesn't seem
209 to care about it either... */
210 HANDLE h;
211 DWORD m;
212
213 h = GetStdHandle (STD_ERROR_HANDLE);
214 return (h != INVALID_HANDLE_VALUE) && (h != NULL)
215 && GetConsoleMode (h, &m);
216#else
f3065bdb
JJ
217 char const *t = getenv ("TERM");
218 return t && strcmp (t, "dumb") != 0 && isatty (STDERR_FILENO);
db0d1bae 219#endif
f3065bdb
JJ
220}
221
f3065bdb
JJ
222bool
223colorize_init (diagnostic_color_rule_t rule)
224{
225 switch (rule)
226 {
227 case DIAGNOSTICS_COLOR_NO:
228 return false;
229 case DIAGNOSTICS_COLOR_YES:
230 return parse_gcc_colors ();
231 case DIAGNOSTICS_COLOR_AUTO:
232 if (should_colorize ())
233 return parse_gcc_colors ();
234 else
235 return false;
236 default:
237 gcc_unreachable ();
238 }
239}
d2608235
DM
240
241/* Determine if URLs should be enabled, based on RULE.
242 This reuses the logic for colorization. */
243
244bool
245diagnostic_urls_enabled_p (diagnostic_url_rule_t rule)
246{
247 switch (rule)
248 {
249 case DIAGNOSTICS_URL_NO:
250 return false;
251 case DIAGNOSTICS_URL_YES:
252 return true;
253 case DIAGNOSTICS_URL_AUTO:
254 return should_colorize ();
255 default:
256 gcc_unreachable ();
257 }
258}