2 * Functions for raising errors.
4 * Copyright: Copyright (C) 1999-2023 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/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/src/dmd/errors.d
14 import core
.stdc
.stdarg
;
21 /// Constants used to discriminate kinds of error messages.
31 /***************************
32 * Error message sink for D compiler.
34 class ErrorSinkCompiler
: ErrorSink
40 void error(const ref Loc loc
, const(char)* format
, ...)
44 verrorReport(loc
, format
, ap
, ErrorKind
.error
);
48 void errorSupplemental(const ref Loc loc
, const(char)* format
, ...)
52 verrorReportSupplemental(loc
, format
, ap
, ErrorKind
.error
);
56 void warning(const ref Loc loc
, const(char)* format
, ...)
60 verrorReport(loc
, format
, ap
, ErrorKind
.warning
);
64 void warningSupplemental(const ref Loc loc
, const(char)* format
, ...)
68 verrorReportSupplemental(loc
, format
, ap
, ErrorKind
.warning
);
72 void deprecation(const ref Loc loc
, const(char)* format
, ...)
76 verrorReport(loc
, format
, ap
, ErrorKind
.deprecation
);
80 void deprecationSupplemental(const ref Loc loc
, const(char)* format
, ...)
84 verrorReportSupplemental(loc
, format
, ap
, ErrorKind
.deprecation
);
88 void message(const ref Loc loc
, const(char)* format
, ...)
92 verrorReport(loc
, format
, ap
, ErrorKind
.message
);
99 * Color highlighting to classify messages
101 enum Classification
: Color
103 error
= Color
.brightRed
, /// for errors
104 gagged
= Color
.brightBlue
, /// for gagged errors
105 warning
= Color
.brightYellow
, /// for warnings
106 deprecation
= Color
.brightCyan
, /// for deprecations
107 tip
= Color
.brightGreen
, /// for tip messages
116 yellow
= red | green
,
117 magenta
= red | blue
,
119 lightGray
= red | green | blue
,
121 darkGray
= bright | black
,
122 brightRed
= bright | red
,
123 brightGreen
= bright | green
,
124 brightBlue
= bright | blue
,
125 brightYellow
= bright | yellow
,
126 brightMagenta
= bright | magenta
,
127 brightCyan
= bright | cyan
,
128 white
= bright | lightGray
,
132 static if (__VERSION__
< 2092)
133 private extern (C
++) void noop(const ref Loc loc
, const(char)* format
, ...) {}
135 pragma(printf
) private extern (C
++) void noop(const ref Loc loc
, const(char)* format
, ...) {}
138 package auto previewErrorFunc(bool isDeprecated
, FeatureState featureState
) @safe @nogc pure nothrow
140 with (FeatureState
) final switch (featureState
)
149 return isDeprecated ?
&noop
: &deprecation
;
153 package auto previewSupplementalFunc(bool isDeprecated
, FeatureState featureState
) @safe @nogc pure nothrow
155 with (FeatureState
) final switch (featureState
)
158 return &errorSupplemental
;
164 return isDeprecated ?
&noop
: &deprecationSupplemental
;
170 * Print an error message, increasing the global error count.
172 * loc = location of error
173 * format = printf-style format specification
174 * ... = printf-style variadic arguments
176 static if (__VERSION__
< 2092)
177 extern (C
++) void error(const ref Loc loc
, const(char)* format
, ...)
180 va_start(ap
, format
);
181 verrorReport(loc
, format
, ap
, ErrorKind
.error
);
185 pragma(printf
) extern (C
++) void error(const ref Loc loc
, const(char)* format
, ...)
188 va_start(ap
, format
);
189 verrorReport(loc
, format
, ap
, ErrorKind
.error
);
194 * Same as above, but takes a filename and line information arguments as separate parameters.
196 * filename = source file of error
197 * linnum = line in the source file
198 * charnum = column number on the line
199 * format = printf-style format specification
200 * ... = printf-style variadic arguments
202 static if (__VERSION__
< 2092)
203 extern (C
++) void error(const(char)* filename
, uint linnum
, uint charnum
, const(char)* format
, ...)
205 const loc
= Loc(filename
, linnum
, charnum
);
207 va_start(ap
, format
);
208 verrorReport(loc
, format
, ap
, ErrorKind
.error
);
212 pragma(printf
) extern (C
++) void error(const(char)* filename
, uint linnum
, uint charnum
, const(char)* format
, ...)
214 const loc
= Loc(filename
, linnum
, charnum
);
216 va_start(ap
, format
);
217 verrorReport(loc
, format
, ap
, ErrorKind
.error
);
222 * Print additional details about an error message.
223 * Doesn't increase the error count or print an additional error prefix.
225 * loc = location of error
226 * format = printf-style format specification
227 * ... = printf-style variadic arguments
229 static if (__VERSION__
< 2092)
230 extern (C
++) void errorSupplemental(const ref Loc loc
, const(char)* format
, ...)
233 va_start(ap
, format
);
234 verrorReportSupplemental(loc
, format
, ap
, ErrorKind
.error
);
238 pragma(printf
) extern (C
++) void errorSupplemental(const ref Loc loc
, const(char)* format
, ...)
241 va_start(ap
, format
);
242 verrorReportSupplemental(loc
, format
, ap
, ErrorKind
.error
);
247 * Print a warning message, increasing the global warning count.
249 * loc = location of warning
250 * format = printf-style format specification
251 * ... = printf-style variadic arguments
253 static if (__VERSION__
< 2092)
254 extern (C
++) void warning(const ref Loc loc
, const(char)* format
, ...)
257 va_start(ap
, format
);
258 verrorReport(loc
, format
, ap
, ErrorKind
.warning
);
262 pragma(printf
) extern (C
++) void warning(const ref Loc loc
, const(char)* format
, ...)
265 va_start(ap
, format
);
266 verrorReport(loc
, format
, ap
, ErrorKind
.warning
);
271 * Print additional details about a warning message.
272 * Doesn't increase the warning count or print an additional warning prefix.
274 * loc = location of warning
275 * format = printf-style format specification
276 * ... = printf-style variadic arguments
278 static if (__VERSION__
< 2092)
279 extern (C
++) void warningSupplemental(const ref Loc loc
, const(char)* format
, ...)
282 va_start(ap
, format
);
283 verrorReportSupplemental(loc
, format
, ap
, ErrorKind
.warning
);
287 pragma(printf
) extern (C
++) void warningSupplemental(const ref Loc loc
, const(char)* format
, ...)
290 va_start(ap
, format
);
291 verrorReportSupplemental(loc
, format
, ap
, ErrorKind
.warning
);
296 * Print a deprecation message, may increase the global warning or error count
297 * depending on whether deprecations are ignored.
299 * loc = location of deprecation
300 * format = printf-style format specification
301 * ... = printf-style variadic arguments
303 static if (__VERSION__
< 2092)
304 extern (C
++) void deprecation(const ref Loc loc
, const(char)* format
, ...)
307 va_start(ap
, format
);
308 verrorReport(loc
, format
, ap
, ErrorKind
.deprecation
);
312 pragma(printf
) extern (C
++) void deprecation(const ref Loc loc
, const(char)* format
, ...)
315 va_start(ap
, format
);
316 verrorReport(loc
, format
, ap
, ErrorKind
.deprecation
);
321 * Print additional details about a deprecation message.
322 * Doesn't increase the error count, or print an additional deprecation prefix.
324 * loc = location of deprecation
325 * format = printf-style format specification
326 * ... = printf-style variadic arguments
328 static if (__VERSION__
< 2092)
329 extern (C
++) void deprecationSupplemental(const ref Loc loc
, const(char)* format
, ...)
332 va_start(ap
, format
);
333 verrorReportSupplemental(loc
, format
, ap
, ErrorKind
.deprecation
);
337 pragma(printf
) extern (C
++) void deprecationSupplemental(const ref Loc loc
, const(char)* format
, ...)
340 va_start(ap
, format
);
341 verrorReportSupplemental(loc
, format
, ap
, ErrorKind
.deprecation
);
346 * Print a verbose message.
347 * Doesn't prefix or highlight messages.
349 * loc = location of message
350 * format = printf-style format specification
351 * ... = printf-style variadic arguments
353 static if (__VERSION__
< 2092)
354 extern (C
++) void message(const ref Loc loc
, const(char)* format
, ...)
357 va_start(ap
, format
);
358 verrorReport(loc
, format
, ap
, ErrorKind
.message
);
362 pragma(printf
) extern (C
++) void message(const ref Loc loc
, const(char)* format
, ...)
365 va_start(ap
, format
);
366 verrorReport(loc
, format
, ap
, ErrorKind
.message
);
371 * Same as above, but doesn't take a location argument.
373 * format = printf-style format specification
374 * ... = printf-style variadic arguments
376 static if (__VERSION__
< 2092)
377 extern (C
++) void message(const(char)* format
, ...)
380 va_start(ap
, format
);
381 verrorReport(Loc
.initial
, format
, ap
, ErrorKind
.message
);
385 pragma(printf
) extern (C
++) void message(const(char)* format
, ...)
388 va_start(ap
, format
);
389 verrorReport(Loc
.initial
, format
, ap
, ErrorKind
.message
);
394 * The type of the diagnostic handler
395 * see verrorReport for arguments
396 * Returns: true if error handling is done, false to continue printing to stderr
398 alias DiagnosticHandler
= bool delegate(const ref Loc location
, Color headerColor
, const(char)* header
, const(char)* messageFormat
, va_list args
, const(char)* prefix1
, const(char)* prefix2
);
401 * The diagnostic handler.
402 * If non-null it will be called for every diagnostic message issued by the compiler.
403 * If it returns false, the message will be printed to stderr as usual.
405 __gshared DiagnosticHandler diagnosticHandler
;
408 * Print a tip message with the prefix and highlighting.
410 * format = printf-style format specification
411 * ... = printf-style variadic arguments
413 static if (__VERSION__
< 2092)
414 extern (C
++) void tip(const(char)* format
, ...)
417 va_start(ap
, format
);
418 verrorReport(Loc
.initial
, format
, ap
, ErrorKind
.tip
);
422 pragma(printf
) extern (C
++) void tip(const(char)* format
, ...)
425 va_start(ap
, format
);
426 verrorReport(Loc
.initial
, format
, ap
, ErrorKind
.tip
);
432 * Implements $(D error), $(D warning), $(D deprecation), $(D message), and
433 * $(D tip). Report a diagnostic error, taking a va_list parameter, and
434 * optionally additional message prefixes. Whether the message gets printed
435 * depends on runtime values of DiagnosticReporting and global gagging.
437 * loc = location of error
438 * format = printf-style format specification
439 * ap = printf-style variadic arguments
440 * kind = kind of error being printed
441 * p1 = additional message prefix
442 * p2 = additional message prefix
444 extern (C
++) void verrorReport(const ref Loc loc
, const(char)* format
, va_list ap
, ErrorKind kind
, const(char)* p1
= null, const(char)* p2
= null);
447 * Implements $(D errorSupplemental), $(D warningSupplemental), and
448 * $(D deprecationSupplemental). Report an addition diagnostic error, taking a
449 * va_list parameter. Whether the message gets printed depends on runtime
450 * values of DiagnosticReporting and global gagging.
452 * loc = location of error
453 * format = printf-style format specification
454 * ap = printf-style variadic arguments
455 * kind = kind of error being printed
457 extern (C
++) void verrorReportSupplemental(const ref Loc loc
, const(char)* format
, va_list ap
, ErrorKind kind
);
460 * The type of the fatal error handler
461 * Returns: true if error handling is done, false to do exit(EXIT_FAILURE)
463 alias FatalErrorHandler
= bool delegate();
466 * The fatal error handler.
467 * If non-null it will be called for every fatal() call issued by the compiler.
469 __gshared FatalErrorHandler fatalErrorHandler
;
472 * Call this after printing out fatal error messages to clean up and exit the
473 * compiler. You can also set a fatalErrorHandler to override this behaviour.
475 extern (C
++) void fatal();
478 * Try to stop forgetting to remove the breakpoints from
481 extern (C
++) void halt() @safe;