Debian Codegraph @

gcc/d/d-diagnostic.cc

raw
1 /* d-diagnostics.cc -- D frontend interface to gcc diagnostics.
2 Copyright (C) 2017-2025 Free Software Foundation, Inc.
3
4 GCC 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 GCC 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 GCC; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. */
17
18 #include "config.h"
19 #include "system.h"
20 #include "coretypes.h"
21
22 #include "dmd/globals.h"
23 #include "dmd/errors.h"
24
25 #include "tree.h"
26 #include "options.h"
27 #include "diagnostic.h"
28
29 #include "d-tree.h"
30
31
32 /* Rewrite the format string FORMAT to deal with any format extensions not
33 supported by pp_format().
34
35 The following format specifiers are handled:
36 `...`: text within backticks gets quoted as '%<...%>'.
37 %-10s: left-justify format flag is removed leaving '%s' remaining.
38 %02x: zero-padding format flag is removed leaving '%x' remaining.
39 %X: uppercase unsigned hexadecimals are rewritten as '%x'. */
40
41 static char *
42 expand_d_format (const char *format)
43 {
44 obstack buf;
45 bool inbacktick = false;
46
47 gcc_obstack_init (&buf);
48
49 for (const char *p = format; *p;)
50 {
51 while (*p != '\0' && *p != '\\' && *p != '%' && *p != '`')
52 {
53 obstack_1grow (&buf, *p);
54 p++;
55 }
56
57 if (*p == '\0')
58 break;
59
60 if (*p == '\\')
61 {
62 if (p[1] == '`')
63 {
64 /* Escaped backtick, don't expand it as a quoted string. */
65 obstack_1grow (&buf, '`');
66 p++;;
67 }
68 else
69 obstack_1grow (&buf, *p);
70
71 p++;
72 continue;
73 }
74
75 if (*p == '`')
76 {
77 /* Text enclosed by `...` are translated as a quoted string. */
78 if (inbacktick)
79 {
80 obstack_grow (&buf, "%>", 2);
81 inbacktick = false;
82 }
83 else
84 {
85 obstack_grow (&buf, "%<", 2);
86 inbacktick = true;
87 }
88 p++;
89 continue;
90 }
91
92 /* Check the conversion specification for unhandled flags. */
93 obstack_1grow (&buf, *p);
94 p++;
95
96 Lagain:
97 switch (*p)
98 {
99 case '\0':
100 /* Malformed format string. */
101 gcc_unreachable ();
102
103 case '-':
104 /* Remove whitespace formatting. */
105 p++;
106 while (ISDIGIT (*p))
107 p++;
108 goto Lagain;
109
110 case '0':
111 /* Remove zero padding from format string. */
112 while (ISDIGIT (*p))
113 p++;
114 goto Lagain;
115
116 case 'X':
117 /* Hex format only supports lower-case. */
118 obstack_1grow (&buf, 'x');
119 p++;
120 break;
121
122 default:
123 break;
124 }
125 }
126
127 gcc_assert (!inbacktick);
128 obstack_1grow (&buf, '\0');
129 return (char *) obstack_finish (&buf);
130 }
131
132 /* Rewrite the format string FORMAT to deal with any characters that require
133 escaping before expand_d_format expands it. */
134
135 static char *
136 escape_d_format (const char *format)
137 {
138 bool quoted = false;
139 size_t format_len = 0;
140 obstack buf;
141
142 gcc_obstack_init (&buf);
143
144 /* If the format string is enclosed by two '`' characters, then don't escape
145 the first and last characters. */
146 if (*format == '`')
147 {
148 format_len = strlen (format) - 1;
149 if (format_len && format[format_len] == '`')
150 quoted = true;
151 }
152
153 for (const char *p = format; *p; p++)
154 {
155 switch (*p)
156 {
157 case '%':
158 /* Escape `%' characters so that pp_format does not confuse them
159 for actual format specifiers. */
160 obstack_1grow (&buf, '%');
161 break;
162
163 case '`':
164 /* Escape '`' characters so that expand_d_format does not confuse them
165 for a quoted string. */
166 if (!quoted || (p != format && p != (format + format_len)))
167 obstack_1grow (&buf, '\\');
168 break;
169
170 default:
171 break;
172 }
173
174 obstack_1grow (&buf, *p);
175 }
176
177 obstack_1grow (&buf, '\0');
178 return (char *) obstack_finish (&buf);
179 }
180
181 /* Helper routine for all error routines. Reports a diagnostic specified by
182 KIND at the explicit location LOC. The message FORMAT comes from the dmd
183 front-end, which does not get translated by the gcc diagnostic routines. */
184
185 static void ATTRIBUTE_GCC_DIAG(3,0)
186 d_diagnostic_report_diagnostic (const SourceLoc &loc, int opt,
187 const char *format, va_list ap,
188 diagnostic_t kind, bool verbatim)
189 {
190 va_list argp;
191 va_copy (argp, ap);
192
193 if (loc.filename.length != 0 || !verbatim)
194 {
195 rich_location rich_loc (line_table, make_location_t (loc));
196 diagnostic_info diagnostic;
197 char *xformat = expand_d_format (format);
198
199 diagnostic_set_info_translated (&diagnostic, xformat, &argp,
200 &rich_loc, kind);
201 if (opt != 0)
202 diagnostic.option_id = opt;
203
204 diagnostic_report_diagnostic (global_dc, &diagnostic);
205 }
206 else
207 {
208 /* Write verbatim messages with no location direct to stream. */
209 text_info text (expand_d_format (format), &argp, errno, nullptr);
210
211 pretty_printer *const pp = global_dc->get_reference_printer ();
212 pp_format_verbatim (pp, &text);
213 pp_newline_and_flush (pp);
214 }
215
216 va_end (argp);
217 }
218
219 /* Print a diagnostic message of type KIND with explicit location LOC with an
220 optional message prefix PREFIX1 and PREFIX2, increasing the global or gagged
221 error count depending on how KIND is treated. */
222
223 void D_ATTRIBUTE_FORMAT(2,0) ATTRIBUTE_GCC_DIAG(2,0)
224 verrorReport (const SourceLoc loc, const char *format, va_list ap,
225 ErrorKind kind, const char *prefix1, const char *prefix2)
226 {
227 diagnostic_t diag_kind = DK_UNSPECIFIED;
228 int opt = 0;
229 bool verbatim = false;
230 char *xformat;
231
232 if (kind == ErrorKind::error)
233 {
234 global.errors++;
235 if (global.gag)
236 global.gaggedErrors++;
237
238 if (global.gag && !global.params.v.showGaggedErrors)
239 return;
240
241 diag_kind = global.gag ? DK_ANACHRONISM : DK_ERROR;
242 }
243 else if (kind == ErrorKind::warning)
244 {
245 if (global.gag || global.params.useWarnings == DIAGNOSTICoff)
246 {
247 if (global.gag)
248 global.gaggedWarnings++;
249
250 return;
251 }
252
253 /* Warnings don't count if not treated as errors. */
254 if (global.params.useWarnings == DIAGNOSTICerror)
255 global.warnings++;
256
257 diag_kind = DK_WARNING;
258 }
259 else if (kind == ErrorKind::deprecation)
260 {
261 if (global.params.useDeprecated == DIAGNOSTICerror)
262 return verrorReport (loc, format, ap, ErrorKind::error, prefix1,
263 prefix2);
264 else if (global.gag || global.params.useDeprecated != DIAGNOSTICinform)
265 {
266 if (global.gag)
267 global.gaggedWarnings++;
268
269 return;
270 }
271
272 opt = OPT_Wdeprecated;
273 diag_kind = DK_WARNING;
274 }
275 else if (kind == ErrorKind::message)
276 {
277 diag_kind = DK_NOTE;
278 verbatim = true;
279 }
280 else if (kind == ErrorKind::tip)
281 {
282 if (global.gag)
283 return;
284
285 diag_kind = DK_DEBUG;
286 verbatim = true;
287 }
288 else
289 gcc_unreachable ();
290
291 /* Build string and emit. */
292 if (prefix2 != NULL)
293 xformat = xasprintf ("%s %s %s", escape_d_format (prefix1),
294 escape_d_format (prefix2), format);
295 else if (prefix1 != NULL)
296 xformat = xasprintf ("%s %s", escape_d_format (prefix1), format);
297 else
298 xformat = xasprintf ("%s", format);
299
300 d_diagnostic_report_diagnostic (loc, opt, xformat, ap, diag_kind, verbatim);
301 free (xformat);
302 }
303
304 /* Print supplementary message about the last diagnostic of type KIND, with
305 explicit location LOC. This doesn't increase the global error count. */
306
307 void D_ATTRIBUTE_FORMAT(2,0) ATTRIBUTE_GCC_DIAG(2,0)
308 verrorReportSupplemental (const SourceLoc loc, const char* format, va_list ap,
309 ErrorKind kind)
310 {
311 if (kind == ErrorKind::error)
312 {
313 if (global.gag && !global.params.v.showGaggedErrors)
314 return;
315 }
316 else if (kind == ErrorKind::warning)
317 {
318 if (global.params.useWarnings == DIAGNOSTICoff || global.gag)
319 return;
320 }
321 else if (kind == ErrorKind::deprecation)
322 {
323 if (global.params.useDeprecated == DIAGNOSTICerror)
324 return verrorReportSupplemental (loc, format, ap, ErrorKind::error);
325 else if (global.params.useDeprecated != DIAGNOSTICinform || global.gag)
326 return;
327 }
328 else
329 gcc_unreachable ();
330
331 d_diagnostic_report_diagnostic (loc, 0, format, ap, DK_NOTE, false);
332 }
333
334 /* Call this after printing out fatal error messages to clean up and
335 exit the compiler. */
336
337 void
338 fatal (void)
339 {
340 exit (FATAL_EXIT_CODE);
341 }