annotate gcc/diagnostic-color.c @ 131:84e7813d76e9

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