annotate gcc/diagnostic-color.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Output colorization.
kono
parents:
diff changeset
2 Copyright (C) 2011-2017 Free Software Foundation, Inc.
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"
kono
parents:
diff changeset
22
kono
parents:
diff changeset
23 #ifdef __MINGW32__
kono
parents:
diff changeset
24 # include <windows.h>
kono
parents:
diff changeset
25 #endif
kono
parents:
diff changeset
26
kono
parents:
diff changeset
27 /* Select Graphic Rendition (SGR, "\33[...m") strings. */
kono
parents:
diff changeset
28 /* Also Erase in Line (EL) to Right ("\33[K") by default. */
kono
parents:
diff changeset
29 /* Why have EL to Right after SGR?
kono
parents:
diff changeset
30 -- The behavior of line-wrapping when at the bottom of the
kono
parents:
diff changeset
31 terminal screen and at the end of the current line is often
kono
parents:
diff changeset
32 such that a new line is introduced, entirely cleared with
kono
parents:
diff changeset
33 the current background color which may be different from the
kono
parents:
diff changeset
34 default one (see the boolean back_color_erase terminfo(5)
kono
parents:
diff changeset
35 capability), thus scrolling the display by one line.
kono
parents:
diff changeset
36 The end of this new line will stay in this background color
kono
parents:
diff changeset
37 even after reverting to the default background color with
kono
parents:
diff changeset
38 "\33[m', unless it is explicitly cleared again with "\33[K"
kono
parents:
diff changeset
39 (which is the behavior the user would instinctively expect
kono
parents:
diff changeset
40 from the whole thing). There may be some unavoidable
kono
parents:
diff changeset
41 background-color flicker at the end of this new line because
kono
parents:
diff changeset
42 of this (when timing with the monitor's redraw is just right).
kono
parents:
diff changeset
43 -- The behavior of HT (tab, "\t") is usually the same as that of
kono
parents:
diff changeset
44 Cursor Forward Tabulation (CHT) with a default parameter
kono
parents:
diff changeset
45 of 1 ("\33[I"), i.e., it performs pure movement to the next
kono
parents:
diff changeset
46 tab stop, without any clearing of either content or screen
kono
parents:
diff changeset
47 attributes (including background color); try
kono
parents:
diff changeset
48 printf 'asdfqwerzxcv\rASDF\tZXCV\n'
kono
parents:
diff changeset
49 in a bash(1) shell to demonstrate this. This is not what the
kono
parents:
diff changeset
50 user would instinctively expect of HT (but is ok for CHT).
kono
parents:
diff changeset
51 The instinctive behavior would include clearing the terminal
kono
parents:
diff changeset
52 cells that are skipped over by HT with blank cells in the
kono
parents:
diff changeset
53 current screen attributes, including background color;
kono
parents:
diff changeset
54 the boolean dest_tabs_magic_smso terminfo(5) capability
kono
parents:
diff changeset
55 indicates this saner behavior for HT, but only some rare
kono
parents:
diff changeset
56 terminals have it (although it also indicates a special
kono
parents:
diff changeset
57 glitch with standout mode in the Teleray terminal for which
kono
parents:
diff changeset
58 it was initially introduced). The remedy is to add "\33K"
kono
parents:
diff changeset
59 after each SGR sequence, be it START (to fix the behavior
kono
parents:
diff changeset
60 of any HT after that before another SGR) or END (to fix the
kono
parents:
diff changeset
61 behavior of an HT in default background color that would
kono
parents:
diff changeset
62 follow a line-wrapping at the bottom of the screen in another
kono
parents:
diff changeset
63 background color, and to complement doing it after START).
kono
parents:
diff changeset
64 Piping GCC's output through a pager such as less(1) avoids
kono
parents:
diff changeset
65 any HT problems since the pager performs tab expansion.
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 Generic disadvantages of this remedy are:
kono
parents:
diff changeset
68 -- Some very rare terminals might support SGR but not EL (nobody
kono
parents:
diff changeset
69 will use "gcc -fdiagnostics-color" on a terminal that does not
kono
parents:
diff changeset
70 support SGR in the first place).
kono
parents:
diff changeset
71 -- Having these extra control sequences might somewhat complicate
kono
parents:
diff changeset
72 the task of any program trying to parse "gcc -fdiagnostics-color"
kono
parents:
diff changeset
73 output in order to extract structuring information from it.
kono
parents:
diff changeset
74 A specific disadvantage to doing it after SGR START is:
kono
parents:
diff changeset
75 -- Even more possible background color flicker (when timing
kono
parents:
diff changeset
76 with the monitor's redraw is just right), even when not at the
kono
parents:
diff changeset
77 bottom of the screen.
kono
parents:
diff changeset
78 There are no additional disadvantages specific to doing it after
kono
parents:
diff changeset
79 SGR END.
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 It would be impractical for GCC to become a full-fledged
kono
parents:
diff changeset
82 terminal program linked against ncurses or the like, so it will
kono
parents:
diff changeset
83 not detect terminfo(5) capabilities. */
kono
parents:
diff changeset
84 #define COLOR_SEPARATOR ";"
kono
parents:
diff changeset
85 #define COLOR_NONE "00"
kono
parents:
diff changeset
86 #define COLOR_BOLD "01"
kono
parents:
diff changeset
87 #define COLOR_UNDERSCORE "04"
kono
parents:
diff changeset
88 #define COLOR_BLINK "05"
kono
parents:
diff changeset
89 #define COLOR_REVERSE "07"
kono
parents:
diff changeset
90 #define COLOR_FG_BLACK "30"
kono
parents:
diff changeset
91 #define COLOR_FG_RED "31"
kono
parents:
diff changeset
92 #define COLOR_FG_GREEN "32"
kono
parents:
diff changeset
93 #define COLOR_FG_YELLOW "33"
kono
parents:
diff changeset
94 #define COLOR_FG_BLUE "34"
kono
parents:
diff changeset
95 #define COLOR_FG_MAGENTA "35"
kono
parents:
diff changeset
96 #define COLOR_FG_CYAN "36"
kono
parents:
diff changeset
97 #define COLOR_FG_WHITE "37"
kono
parents:
diff changeset
98 #define COLOR_BG_BLACK "40"
kono
parents:
diff changeset
99 #define COLOR_BG_RED "41"
kono
parents:
diff changeset
100 #define COLOR_BG_GREEN "42"
kono
parents:
diff changeset
101 #define COLOR_BG_YELLOW "43"
kono
parents:
diff changeset
102 #define COLOR_BG_BLUE "44"
kono
parents:
diff changeset
103 #define COLOR_BG_MAGENTA "45"
kono
parents:
diff changeset
104 #define COLOR_BG_CYAN "46"
kono
parents:
diff changeset
105 #define COLOR_BG_WHITE "47"
kono
parents:
diff changeset
106 #define SGR_START "\33["
kono
parents:
diff changeset
107 #define SGR_END "m\33[K"
kono
parents:
diff changeset
108 #define SGR_SEQ(str) SGR_START str SGR_END
kono
parents:
diff changeset
109 #define SGR_RESET SGR_SEQ("")
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 /* The context and logic for choosing default --color screen attributes
kono
parents:
diff changeset
113 (foreground and background colors, etc.) are the following.
kono
parents:
diff changeset
114 -- There are eight basic colors available, each with its own
kono
parents:
diff changeset
115 nominal luminosity to the human eye and foreground/background
kono
parents:
diff changeset
116 codes (black [0 %, 30/40], blue [11 %, 34/44], red [30 %, 31/41],
kono
parents:
diff changeset
117 magenta [41 %, 35/45], green [59 %, 32/42], cyan [70 %, 36/46],
kono
parents:
diff changeset
118 yellow [89 %, 33/43], and white [100 %, 37/47]).
kono
parents:
diff changeset
119 -- Sometimes, white as a background is actually implemented using
kono
parents:
diff changeset
120 a shade of light gray, so that a foreground white can be visible
kono
parents:
diff changeset
121 on top of it (but most often not).
kono
parents:
diff changeset
122 -- Sometimes, black as a foreground is actually implemented using
kono
parents:
diff changeset
123 a shade of dark gray, so that it can be visible on top of a
kono
parents:
diff changeset
124 background black (but most often not).
kono
parents:
diff changeset
125 -- Sometimes, more colors are available, as extensions.
kono
parents:
diff changeset
126 -- Other attributes can be selected/deselected (bold [1/22],
kono
parents:
diff changeset
127 underline [4/24], standout/inverse [7/27], blink [5/25], and
kono
parents:
diff changeset
128 invisible/hidden [8/28]). They are sometimes implemented by
kono
parents:
diff changeset
129 using colors instead of what their names imply; e.g., bold is
kono
parents:
diff changeset
130 often achieved by using brighter colors. In practice, only bold
kono
parents:
diff changeset
131 is really available to us, underline sometimes being mapped by
kono
parents:
diff changeset
132 the terminal to some strange color choice, and standout best
kono
parents:
diff changeset
133 being left for use by downstream programs such as less(1).
kono
parents:
diff changeset
134 -- We cannot assume that any of the extensions or special features
kono
parents:
diff changeset
135 are available for the purpose of choosing defaults for everyone.
kono
parents:
diff changeset
136 -- The most prevalent default terminal backgrounds are pure black
kono
parents:
diff changeset
137 and pure white, and are not necessarily the same shades of
kono
parents:
diff changeset
138 those as if they were selected explicitly with SGR sequences.
kono
parents:
diff changeset
139 Some terminals use dark or light pictures as default background,
kono
parents:
diff changeset
140 but those are covered over by an explicit selection of background
kono
parents:
diff changeset
141 color with an SGR sequence; their users will appreciate their
kono
parents:
diff changeset
142 background pictures not be covered like this, if possible.
kono
parents:
diff changeset
143 -- Some uses of colors attributes is to make some output items
kono
parents:
diff changeset
144 more understated (e.g., context lines); this cannot be achieved
kono
parents:
diff changeset
145 by changing the background color.
kono
parents:
diff changeset
146 -- For these reasons, the GCC color defaults should strive not
kono
parents:
diff changeset
147 to change the background color from its default, unless it's
kono
parents:
diff changeset
148 for a short item that should be highlighted, not understated.
kono
parents:
diff changeset
149 -- The GCC foreground color defaults (without an explicitly set
kono
parents:
diff changeset
150 background) should provide enough contrast to be readable on any
kono
parents:
diff changeset
151 terminal with either a black (dark) or white (light) background.
kono
parents:
diff changeset
152 This only leaves red, magenta, green, and cyan (and their bold
kono
parents:
diff changeset
153 counterparts) and possibly bold blue. */
kono
parents:
diff changeset
154 /* Default colors. The user can overwrite them using environment
kono
parents:
diff changeset
155 variable GCC_COLORS. */
kono
parents:
diff changeset
156 struct color_cap
kono
parents:
diff changeset
157 {
kono
parents:
diff changeset
158 const char *name;
kono
parents:
diff changeset
159 const char *val;
kono
parents:
diff changeset
160 unsigned char name_len;
kono
parents:
diff changeset
161 bool free_val;
kono
parents:
diff changeset
162 };
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 /* For GCC_COLORS. */
kono
parents:
diff changeset
165 static struct color_cap color_dict[] =
kono
parents:
diff changeset
166 {
kono
parents:
diff changeset
167 { "error", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_RED), 5, false },
kono
parents:
diff changeset
168 { "warning", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_MAGENTA),
kono
parents:
diff changeset
169 7, false },
kono
parents:
diff changeset
170 { "note", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_CYAN), 4, false },
kono
parents:
diff changeset
171 { "range1", SGR_SEQ (COLOR_FG_GREEN), 6, false },
kono
parents:
diff changeset
172 { "range2", SGR_SEQ (COLOR_FG_BLUE), 6, false },
kono
parents:
diff changeset
173 { "locus", SGR_SEQ (COLOR_BOLD), 5, false },
kono
parents:
diff changeset
174 { "quote", SGR_SEQ (COLOR_BOLD), 5, false },
kono
parents:
diff changeset
175 { "fixit-insert", SGR_SEQ (COLOR_FG_GREEN), 12, false },
kono
parents:
diff changeset
176 { "fixit-delete", SGR_SEQ (COLOR_FG_RED), 12, false },
kono
parents:
diff changeset
177 { "diff-filename", SGR_SEQ (COLOR_BOLD), 13, false },
kono
parents:
diff changeset
178 { "diff-hunk", SGR_SEQ (COLOR_FG_CYAN), 9, false },
kono
parents:
diff changeset
179 { "diff-delete", SGR_SEQ (COLOR_FG_RED), 11, false },
kono
parents:
diff changeset
180 { "diff-insert", SGR_SEQ (COLOR_FG_GREEN), 11, false },
kono
parents:
diff changeset
181 { "type-diff", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN), 9, false },
kono
parents:
diff changeset
182 { NULL, NULL, 0, false }
kono
parents:
diff changeset
183 };
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 const char *
kono
parents:
diff changeset
186 colorize_start (bool show_color, const char *name, size_t name_len)
kono
parents:
diff changeset
187 {
kono
parents:
diff changeset
188 struct color_cap const *cap;
kono
parents:
diff changeset
189
kono
parents:
diff changeset
190 if (!show_color)
kono
parents:
diff changeset
191 return "";
kono
parents:
diff changeset
192
kono
parents:
diff changeset
193 for (cap = color_dict; cap->name; cap++)
kono
parents:
diff changeset
194 if (cap->name_len == name_len
kono
parents:
diff changeset
195 && memcmp (cap->name, name, name_len) == 0)
kono
parents:
diff changeset
196 break;
kono
parents:
diff changeset
197 if (cap->name == NULL)
kono
parents:
diff changeset
198 return "";
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 return cap->val;
kono
parents:
diff changeset
201 }
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 const char *
kono
parents:
diff changeset
204 colorize_stop (bool show_color)
kono
parents:
diff changeset
205 {
kono
parents:
diff changeset
206 return show_color ? SGR_RESET : "";
kono
parents:
diff changeset
207 }
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 /* Parse GCC_COLORS. The default would look like:
kono
parents:
diff changeset
210 GCC_COLORS='error=01;31:warning=01;35:note=01;36:\
kono
parents:
diff changeset
211 range1=32:range2=34:locus=01:quote=01:\
kono
parents:
diff changeset
212 fixit-insert=32:fixit-delete=31:'\
kono
parents:
diff changeset
213 diff-filename=01:diff-hunk=32:diff-delete=31:diff-insert=32:\
kono
parents:
diff changeset
214 type-diff=01;32'
kono
parents:
diff changeset
215 No character escaping is needed or supported. */
kono
parents:
diff changeset
216 static bool
kono
parents:
diff changeset
217 parse_gcc_colors (void)
kono
parents:
diff changeset
218 {
kono
parents:
diff changeset
219 const char *p, *q, *name, *val;
kono
parents:
diff changeset
220 char *b;
kono
parents:
diff changeset
221 size_t name_len = 0, val_len = 0;
kono
parents:
diff changeset
222
kono
parents:
diff changeset
223 p = getenv ("GCC_COLORS"); /* Plural! */
kono
parents:
diff changeset
224 if (p == NULL)
kono
parents:
diff changeset
225 return true;
kono
parents:
diff changeset
226 if (*p == '\0')
kono
parents:
diff changeset
227 return false;
kono
parents:
diff changeset
228
kono
parents:
diff changeset
229 name = q = p;
kono
parents:
diff changeset
230 val = NULL;
kono
parents:
diff changeset
231 /* From now on, be well-formed or you're gone. */
kono
parents:
diff changeset
232 for (;;)
kono
parents:
diff changeset
233 if (*q == ':' || *q == '\0')
kono
parents:
diff changeset
234 {
kono
parents:
diff changeset
235 struct color_cap *cap;
kono
parents:
diff changeset
236
kono
parents:
diff changeset
237 if (val)
kono
parents:
diff changeset
238 val_len = q - val;
kono
parents:
diff changeset
239 else
kono
parents:
diff changeset
240 name_len = q - name;
kono
parents:
diff changeset
241 /* Empty name without val (empty cap)
kono
parents:
diff changeset
242 won't match and will be ignored. */
kono
parents:
diff changeset
243 for (cap = color_dict; cap->name; cap++)
kono
parents:
diff changeset
244 if (cap->name_len == name_len
kono
parents:
diff changeset
245 && memcmp (cap->name, name, name_len) == 0)
kono
parents:
diff changeset
246 break;
kono
parents:
diff changeset
247 /* If name unknown, go on for forward compatibility. */
kono
parents:
diff changeset
248 if (cap->val && val)
kono
parents:
diff changeset
249 {
kono
parents:
diff changeset
250 if (cap->free_val)
kono
parents:
diff changeset
251 free (CONST_CAST (char *, cap->val));
kono
parents:
diff changeset
252 b = XNEWVEC (char, val_len + sizeof (SGR_SEQ ("")));
kono
parents:
diff changeset
253 memcpy (b, SGR_START, strlen (SGR_START));
kono
parents:
diff changeset
254 memcpy (b + strlen (SGR_START), val, val_len);
kono
parents:
diff changeset
255 memcpy (b + strlen (SGR_START) + val_len, SGR_END,
kono
parents:
diff changeset
256 sizeof (SGR_END));
kono
parents:
diff changeset
257 cap->val = (const char *) b;
kono
parents:
diff changeset
258 cap->free_val = true;
kono
parents:
diff changeset
259 }
kono
parents:
diff changeset
260 if (*q == '\0')
kono
parents:
diff changeset
261 return true;
kono
parents:
diff changeset
262 name = ++q;
kono
parents:
diff changeset
263 val = NULL;
kono
parents:
diff changeset
264 }
kono
parents:
diff changeset
265 else if (*q == '=')
kono
parents:
diff changeset
266 {
kono
parents:
diff changeset
267 if (q == name || val)
kono
parents:
diff changeset
268 return true;
kono
parents:
diff changeset
269
kono
parents:
diff changeset
270 name_len = q - name;
kono
parents:
diff changeset
271 val = ++q; /* Can be the empty string. */
kono
parents:
diff changeset
272 }
kono
parents:
diff changeset
273 else if (val == NULL)
kono
parents:
diff changeset
274 q++; /* Accumulate name. */
kono
parents:
diff changeset
275 else if (*q == ';' || (*q >= '0' && *q <= '9'))
kono
parents:
diff changeset
276 q++; /* Accumulate val. Protect the terminal from being sent
kono
parents:
diff changeset
277 garbage. */
kono
parents:
diff changeset
278 else
kono
parents:
diff changeset
279 return true;
kono
parents:
diff changeset
280 }
kono
parents:
diff changeset
281
kono
parents:
diff changeset
282 /* Return true if we should use color when in auto mode, false otherwise. */
kono
parents:
diff changeset
283 static bool
kono
parents:
diff changeset
284 should_colorize (void)
kono
parents:
diff changeset
285 {
kono
parents:
diff changeset
286 #ifdef __MINGW32__
kono
parents:
diff changeset
287 /* For consistency reasons, one should check the handle returned by
kono
parents:
diff changeset
288 _get_osfhandle(_fileno(stderr)) because the function
kono
parents:
diff changeset
289 pp_write_text_to_stream() in pretty-print.c calls fputs() on
kono
parents:
diff changeset
290 that stream. However, the code below for non-Windows doesn't seem
kono
parents:
diff changeset
291 to care about it either... */
kono
parents:
diff changeset
292 HANDLE h;
kono
parents:
diff changeset
293 DWORD m;
kono
parents:
diff changeset
294
kono
parents:
diff changeset
295 h = GetStdHandle (STD_ERROR_HANDLE);
kono
parents:
diff changeset
296 return (h != INVALID_HANDLE_VALUE) && (h != NULL)
kono
parents:
diff changeset
297 && GetConsoleMode (h, &m);
kono
parents:
diff changeset
298 #else
kono
parents:
diff changeset
299 char const *t = getenv ("TERM");
kono
parents:
diff changeset
300 return t && strcmp (t, "dumb") != 0 && isatty (STDERR_FILENO);
kono
parents:
diff changeset
301 #endif
kono
parents:
diff changeset
302 }
kono
parents:
diff changeset
303
kono
parents:
diff changeset
304 bool
kono
parents:
diff changeset
305 colorize_init (diagnostic_color_rule_t rule)
kono
parents:
diff changeset
306 {
kono
parents:
diff changeset
307 switch (rule)
kono
parents:
diff changeset
308 {
kono
parents:
diff changeset
309 case DIAGNOSTICS_COLOR_NO:
kono
parents:
diff changeset
310 return false;
kono
parents:
diff changeset
311 case DIAGNOSTICS_COLOR_YES:
kono
parents:
diff changeset
312 return parse_gcc_colors ();
kono
parents:
diff changeset
313 case DIAGNOSTICS_COLOR_AUTO:
kono
parents:
diff changeset
314 if (should_colorize ())
kono
parents:
diff changeset
315 return parse_gcc_colors ();
kono
parents:
diff changeset
316 else
kono
parents:
diff changeset
317 return false;
kono
parents:
diff changeset
318 default:
kono
parents:
diff changeset
319 gcc_unreachable ();
kono
parents:
diff changeset
320 }
kono
parents:
diff changeset
321 }