Debian Codegraph @

gcc/d/dmd/errors.d

raw
1 /**
2 * Functions for raising errors.
3 *
4 * Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved
5 * Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
6 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
7 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/errors.d, _errors.d)
8 * Documentation: https://dlang.org/phobos/dmd_errors.html
9 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/errors.d
10 */
11
12 module dmd.errors;
13
14 public import core.stdc.stdarg;
15 public import dmd.root.string: fTuple;
16 import dmd.errorsink;
17 import dmd.globals;
18 import dmd.location;
19 import dmd.root.string;
20
21 nothrow:
22
23 /// Constants used to discriminate kinds of error messages.
24 enum ErrorKind
25 {
26 warning,
27 deprecation,
28 error,
29 tip,
30 message,
31 }
32
33 /***************************
34 * Error message sink for D compiler.
35 */
36 class ErrorSinkCompiler : ErrorSink
37 {
38 nothrow:
39 extern (C++):
40 override:
41
42 void verror(Loc loc, const(char)* format, va_list ap)
43 {
44 verrorReport(loc, format, ap, ErrorKind.error);
45 }
46
47 void verrorSupplemental(Loc loc, const(char)* format, va_list ap)
48 {
49 verrorReportSupplemental(loc, format, ap, ErrorKind.error);
50 }
51
52 void vwarning(Loc loc, const(char)* format, va_list ap)
53 {
54 verrorReport(loc, format, ap, ErrorKind.warning);
55 }
56
57 void vwarningSupplemental(Loc loc, const(char)* format, va_list ap)
58 {
59 verrorReportSupplemental(loc, format, ap, ErrorKind.warning);
60 }
61
62 void vdeprecation(Loc loc, const(char)* format, va_list ap)
63 {
64 verrorReport(loc, format, ap, ErrorKind.deprecation);
65 }
66
67 void vdeprecationSupplemental(Loc loc, const(char)* format, va_list ap)
68 {
69 verrorReportSupplemental(loc, format, ap, ErrorKind.deprecation);
70 }
71
72 void vmessage(Loc loc, const(char)* format, va_list ap)
73 {
74 verrorReport(loc, format, ap, ErrorKind.message);
75 }
76 }
77
78
79 /**
80 * Color highlighting to classify messages
81 */
82 enum Classification : Color
83 {
84 error = Color.brightRed, /// for errors
85 gagged = Color.brightBlue, /// for gagged errors
86 warning = Color.brightYellow, /// for warnings
87 deprecation = Color.brightCyan, /// for deprecations
88 tip = Color.brightGreen, /// for tip messages
89 }
90
91 enum Color : int
92 {
93 black = 0,
94 red = 1,
95 green = 2,
96 blue = 4,
97 yellow = red | green,
98 magenta = red | blue,
99 cyan = green | blue,
100 lightGray = red | green | blue,
101 bright = 8,
102 darkGray = bright | black,
103 brightRed = bright | red,
104 brightGreen = bright | green,
105 brightBlue = bright | blue,
106 brightYellow = bright | yellow,
107 brightMagenta = bright | magenta,
108 brightCyan = bright | cyan,
109 white = bright | lightGray,
110 }
111
112
113 static if (__VERSION__ < 2092)
114 private extern (C++) void noop(Loc loc, const(char)* format, ...) {}
115 else
116 pragma(printf) private extern (C++) void noop(Loc loc, const(char)* format, ...) {}
117
118
119 package auto previewErrorFunc(bool isDeprecated, FeatureState featureState) @safe @nogc pure nothrow
120 {
121 with (FeatureState) final switch (featureState)
122 {
123 case enabled:
124 return &error;
125
126 case disabled:
127 return &noop;
128
129 case default_:
130 return isDeprecated ? &noop : &deprecation;
131 }
132 }
133
134 package auto previewSupplementalFunc(bool isDeprecated, FeatureState featureState) @safe @nogc pure nothrow
135 {
136 with (FeatureState) final switch (featureState)
137 {
138 case enabled:
139 return &errorSupplemental;
140
141 case disabled:
142 return &noop;
143
144 case default_:
145 return isDeprecated ? &noop : &deprecationSupplemental;
146 }
147 }
148
149
150 /**
151 * Print an error message, increasing the global error count.
152 * Params:
153 * loc = location of error
154 * format = printf-style format specification
155 * ... = printf-style variadic arguments
156 */
157 static if (__VERSION__ < 2092)
158 extern (C++) void error(Loc loc, const(char)* format, ...)
159 {
160 va_list ap;
161 va_start(ap, format);
162 verrorReport(loc, format, ap, ErrorKind.error);
163 va_end(ap);
164 }
165 else
166 pragma(printf) extern (C++) void error(Loc loc, const(char)* format, ...)
167 {
168 va_list ap;
169 va_start(ap, format);
170 verrorReport(loc, format, ap, ErrorKind.error);
171 va_end(ap);
172 }
173
174 /**
175 * Same as above, but takes a filename and line information arguments as separate parameters.
176 * Params:
177 * filename = source file of error
178 * linnum = line in the source file
179 * charnum = column number on the line
180 * format = printf-style format specification
181 * ... = printf-style variadic arguments
182 */
183 static if (__VERSION__ < 2092)
184 extern (C++) void error(const(char)* filename, uint linnum, uint charnum, const(char)* format, ...)
185 {
186 const loc = SourceLoc(filename.toDString, linnum, charnum);
187 va_list ap;
188 va_start(ap, format);
189 verrorReport(loc, format, ap, ErrorKind.error);
190 va_end(ap);
191 }
192 else
193 pragma(printf) extern (C++) void error(const(char)* filename, uint linnum, uint charnum, const(char)* format, ...)
194 {
195 const loc = SourceLoc(filename.toDString, linnum, charnum);
196 va_list ap;
197 va_start(ap, format);
198 verrorReport(loc, format, ap, ErrorKind.error);
199 va_end(ap);
200 }
201
202 /// Callback for when the backend wants to report an error
203 extern(C++) void errorBackend(const(char)* filename, uint linnum, uint charnum, const(char)* format, ...)
204 {
205 const loc = SourceLoc(filename.toDString, linnum, charnum);
206 va_list ap;
207 va_start(ap, format);
208 verrorReport(loc, format, ap, ErrorKind.error);
209 va_end(ap);
210 }
211
212 /**
213 * Print additional details about an error message.
214 * Doesn't increase the error count or print an additional error prefix.
215 * Params:
216 * loc = location of error
217 * format = printf-style format specification
218 * ... = printf-style variadic arguments
219 */
220 static if (__VERSION__ < 2092)
221 extern (C++) void errorSupplemental(Loc loc, const(char)* format, ...)
222 {
223 va_list ap;
224 va_start(ap, format);
225 verrorReportSupplemental(loc, format, ap, ErrorKind.error);
226 va_end(ap);
227 }
228 else
229 pragma(printf) extern (C++) void errorSupplemental(Loc loc, const(char)* format, ...)
230 {
231 va_list ap;
232 va_start(ap, format);
233 verrorReportSupplemental(loc, format, ap, ErrorKind.error);
234 va_end(ap);
235 }
236
237 /**
238 * Print a warning message, increasing the global warning count.
239 * Params:
240 * loc = location of warning
241 * format = printf-style format specification
242 * ... = printf-style variadic arguments
243 */
244 static if (__VERSION__ < 2092)
245 extern (C++) void warning(Loc loc, const(char)* format, ...)
246 {
247 va_list ap;
248 va_start(ap, format);
249 verrorReport(loc, format, ap, ErrorKind.warning);
250 va_end(ap);
251 }
252 else
253 pragma(printf) extern (C++) void warning(Loc loc, const(char)* format, ...)
254 {
255 va_list ap;
256 va_start(ap, format);
257 verrorReport(loc, format, ap, ErrorKind.warning);
258 va_end(ap);
259 }
260
261 /**
262 * Print additional details about a warning message.
263 * Doesn't increase the warning count or print an additional warning prefix.
264 * Params:
265 * loc = location of warning
266 * format = printf-style format specification
267 * ... = printf-style variadic arguments
268 */
269 static if (__VERSION__ < 2092)
270 extern (C++) void warningSupplemental(Loc loc, const(char)* format, ...)
271 {
272 va_list ap;
273 va_start(ap, format);
274 verrorReportSupplemental(loc, format, ap, ErrorKind.warning);
275 va_end(ap);
276 }
277 else
278 pragma(printf) extern (C++) void warningSupplemental(Loc loc, const(char)* format, ...)
279 {
280 va_list ap;
281 va_start(ap, format);
282 verrorReportSupplemental(loc, format, ap, ErrorKind.warning);
283 va_end(ap);
284 }
285
286 /**
287 * Print a deprecation message, may increase the global warning or error count
288 * depending on whether deprecations are ignored.
289 * Params:
290 * loc = location of deprecation
291 * format = printf-style format specification
292 * ... = printf-style variadic arguments
293 */
294 static if (__VERSION__ < 2092)
295 extern (C++) void deprecation(Loc loc, const(char)* format, ...)
296 {
297 va_list ap;
298 va_start(ap, format);
299 verrorReport(loc, format, ap, ErrorKind.deprecation);
300 va_end(ap);
301 }
302 else
303 pragma(printf) extern (C++) void deprecation(Loc loc, const(char)* format, ...)
304 {
305 va_list ap;
306 va_start(ap, format);
307 verrorReport(loc, format, ap, ErrorKind.deprecation);
308 va_end(ap);
309 }
310
311 /**
312 * Print additional details about a deprecation message.
313 * Doesn't increase the error count, or print an additional deprecation prefix.
314 * Params:
315 * loc = location of deprecation
316 * format = printf-style format specification
317 * ... = printf-style variadic arguments
318 */
319 static if (__VERSION__ < 2092)
320 extern (C++) void deprecationSupplemental(Loc loc, const(char)* format, ...)
321 {
322 va_list ap;
323 va_start(ap, format);
324 verrorReportSupplemental(loc, format, ap, ErrorKind.deprecation);
325 va_end(ap);
326 }
327 else
328 pragma(printf) extern (C++) void deprecationSupplemental(Loc loc, const(char)* format, ...)
329 {
330 va_list ap;
331 va_start(ap, format);
332 verrorReportSupplemental(loc, format, ap, ErrorKind.deprecation);
333 va_end(ap);
334 }
335
336 /**
337 * Print a verbose message.
338 * Doesn't prefix or highlight messages.
339 * Params:
340 * loc = location of message
341 * format = printf-style format specification
342 * ... = printf-style variadic arguments
343 */
344 static if (__VERSION__ < 2092)
345 extern (C++) void message(Loc loc, const(char)* format, ...)
346 {
347 va_list ap;
348 va_start(ap, format);
349 verrorReport(loc, format, ap, ErrorKind.message);
350 va_end(ap);
351 }
352 else
353 pragma(printf) extern (C++) void message(Loc loc, const(char)* format, ...)
354 {
355 va_list ap;
356 va_start(ap, format);
357 verrorReport(loc, format, ap, ErrorKind.message);
358 va_end(ap);
359 }
360
361 /**
362 * Same as above, but doesn't take a location argument.
363 * Params:
364 * format = printf-style format specification
365 * ... = printf-style variadic arguments
366 */
367 static if (__VERSION__ < 2092)
368 extern (C++) void message(const(char)* format, ...)
369 {
370 va_list ap;
371 va_start(ap, format);
372 verrorReport(Loc.initial, format, ap, ErrorKind.message);
373 va_end(ap);
374 }
375 else
376 pragma(printf) extern (C++) void message(const(char)* format, ...)
377 {
378 va_list ap;
379 va_start(ap, format);
380 verrorReport(Loc.initial, format, ap, ErrorKind.message);
381 va_end(ap);
382 }
383
384 /**
385 * The type of the diagnostic handler
386 * see verrorReport for arguments
387 * Returns: true if error handling is done, false to continue printing to stderr
388 */
389 alias DiagnosticHandler = bool delegate(const ref SourceLoc location, Color headerColor, const(char)* header, const(char)* messageFormat, va_list args, const(char)* prefix1, const(char)* prefix2);
390
391 /**
392 * The diagnostic handler.
393 * If non-null it will be called for every diagnostic message issued by the compiler.
394 * If it returns false, the message will be printed to stderr as usual.
395 */
396 __gshared DiagnosticHandler diagnosticHandler;
397
398 /**
399 * Print a tip message with the prefix and highlighting.
400 * Params:
401 * format = printf-style format specification
402 * ... = printf-style variadic arguments
403 */
404 static if (__VERSION__ < 2092)
405 extern (C++) void tip(const(char)* format, ...)
406 {
407 va_list ap;
408 va_start(ap, format);
409 verrorReport(Loc.initial, format, ap, ErrorKind.tip);
410 va_end(ap);
411 }
412 else
413 pragma(printf) extern (C++) void tip(const(char)* format, ...)
414 {
415 va_list ap;
416 va_start(ap, format);
417 verrorReport(Loc.initial, format, ap, ErrorKind.tip);
418 va_end(ap);
419 }
420
421
422 /**
423 * Implements $(D error), $(D warning), $(D deprecation), $(D message), and
424 * $(D tip). Report a diagnostic error, taking a va_list parameter, and
425 * optionally additional message prefixes. Whether the message gets printed
426 * depends on runtime values of DiagnosticReporting and global gagging.
427 * Params:
428 * loc = location of error
429 * format = printf-style format specification
430 * ap = printf-style variadic arguments
431 * kind = kind of error being printed
432 * p1 = additional message prefix
433 * p2 = additional message prefix
434 */
435 private extern(C++) void verrorReport(Loc loc, const(char)* format, va_list ap, ErrorKind kind, const(char)* p1 = null, const(char)* p2 = null)
436 {
437 return verrorReport(loc.SourceLoc, format, ap, kind, p1, p2);
438 }
439
440 /// ditto
441 private extern(C++) void verrorReport(const SourceLoc loc, const(char)* format, va_list ap, ErrorKind kind, const(char)* p1 = null, const(char)* p2 = null);
442
443 /**
444 * Implements $(D errorSupplemental), $(D warningSupplemental), and
445 * $(D deprecationSupplemental). Report an addition diagnostic error, taking a
446 * va_list parameter. Whether the message gets printed depends on runtime
447 * values of DiagnosticReporting and global gagging.
448 * Params:
449 * loc = location of error
450 * format = printf-style format specification
451 * ap = printf-style variadic arguments
452 * kind = kind of error being printed
453 */
454 private extern(C++) void verrorReportSupplemental(Loc loc, const(char)* format, va_list ap, ErrorKind kind)
455 {
456 return verrorReportSupplemental(loc.SourceLoc, format, ap, kind);
457 }
458
459 /// ditto
460 private extern(C++) void verrorReportSupplemental(const SourceLoc loc, const(char)* format, va_list ap, ErrorKind kind);
461
462 /**
463 * The type of the fatal error handler
464 * Returns: true if error handling is done, false to do exit(EXIT_FAILURE)
465 */
466 alias FatalErrorHandler = bool delegate();
467
468 /**
469 * The fatal error handler.
470 * If non-null it will be called for every fatal() call issued by the compiler.
471 */
472 __gshared FatalErrorHandler fatalErrorHandler;
473
474 /**
475 * Call this after printing out fatal error messages to clean up and exit the
476 * compiler. You can also set a fatalErrorHandler to override this behaviour.
477 */
478 extern (C++) void fatal();
479
480 /**
481 * Try to stop forgetting to remove the breakpoints from
482 * release builds.
483 */
484 extern (C++) void halt() @safe;