annotate gcc/diagnostic-color.c @ 145:1830386684a0

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