MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p.
[official-gcc.git] / gcc / d / dmd / errors.d
blob542b97b1ada835467a6e1cdf0f4b43079d4c18b8
1 /**
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
12 module dmd.errors;
14 import core.stdc.stdarg;
15 import dmd.errorsink;
16 import dmd.globals;
17 import dmd.location;
19 nothrow:
21 /// Constants used to discriminate kinds of error messages.
22 enum ErrorKind
24 warning,
25 deprecation,
26 error,
27 tip,
28 message,
31 /***************************
32 * Error message sink for D compiler.
34 class ErrorSinkCompiler : ErrorSink
36 nothrow:
37 extern (C++):
38 override:
40 void error(const ref Loc loc, const(char)* format, ...)
42 va_list ap;
43 va_start(ap, format);
44 verrorReport(loc, format, ap, ErrorKind.error);
45 va_end(ap);
48 void errorSupplemental(const ref Loc loc, const(char)* format, ...)
50 va_list ap;
51 va_start(ap, format);
52 verrorReportSupplemental(loc, format, ap, ErrorKind.error);
53 va_end(ap);
56 void warning(const ref Loc loc, const(char)* format, ...)
58 va_list ap;
59 va_start(ap, format);
60 verrorReport(loc, format, ap, ErrorKind.warning);
61 va_end(ap);
64 void warningSupplemental(const ref Loc loc, const(char)* format, ...)
66 va_list ap;
67 va_start(ap, format);
68 verrorReportSupplemental(loc, format, ap, ErrorKind.warning);
69 va_end(ap);
72 void deprecation(const ref Loc loc, const(char)* format, ...)
74 va_list ap;
75 va_start(ap, format);
76 verrorReport(loc, format, ap, ErrorKind.deprecation);
77 va_end(ap);
80 void deprecationSupplemental(const ref Loc loc, const(char)* format, ...)
82 va_list ap;
83 va_start(ap, format);
84 verrorReportSupplemental(loc, format, ap, ErrorKind.deprecation);
85 va_end(ap);
88 void message(const ref Loc loc, const(char)* format, ...)
90 va_list ap;
91 va_start(ap, format);
92 verrorReport(loc, format, ap, ErrorKind.message);
93 va_end(ap);
98 /**
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
110 enum Color : int
112 black = 0,
113 red = 1,
114 green = 2,
115 blue = 4,
116 yellow = red | green,
117 magenta = red | blue,
118 cyan = green | blue,
119 lightGray = red | green | blue,
120 bright = 8,
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, ...) {}
134 else
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)
142 case enabled:
143 return &error;
145 case disabled:
146 return &noop;
148 case default_:
149 return isDeprecated ? &noop : &deprecation;
153 package auto previewSupplementalFunc(bool isDeprecated, FeatureState featureState) @safe @nogc pure nothrow
155 with (FeatureState) final switch (featureState)
157 case enabled:
158 return &errorSupplemental;
160 case disabled:
161 return &noop;
163 case default_:
164 return isDeprecated ? &noop : &deprecationSupplemental;
170 * Print an error message, increasing the global error count.
171 * Params:
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, ...)
179 va_list ap;
180 va_start(ap, format);
181 verrorReport(loc, format, ap, ErrorKind.error);
182 va_end(ap);
184 else
185 pragma(printf) extern (C++) void error(const ref Loc loc, const(char)* format, ...)
187 va_list ap;
188 va_start(ap, format);
189 verrorReport(loc, format, ap, ErrorKind.error);
190 va_end(ap);
194 * Same as above, but takes a filename and line information arguments as separate parameters.
195 * Params:
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);
206 va_list ap;
207 va_start(ap, format);
208 verrorReport(loc, format, ap, ErrorKind.error);
209 va_end(ap);
211 else
212 pragma(printf) extern (C++) void error(const(char)* filename, uint linnum, uint charnum, const(char)* format, ...)
214 const loc = Loc(filename, linnum, charnum);
215 va_list ap;
216 va_start(ap, format);
217 verrorReport(loc, format, ap, ErrorKind.error);
218 va_end(ap);
222 * Print additional details about an error message.
223 * Doesn't increase the error count or print an additional error prefix.
224 * Params:
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, ...)
232 va_list ap;
233 va_start(ap, format);
234 verrorReportSupplemental(loc, format, ap, ErrorKind.error);
235 va_end(ap);
237 else
238 pragma(printf) extern (C++) void errorSupplemental(const ref Loc loc, const(char)* format, ...)
240 va_list ap;
241 va_start(ap, format);
242 verrorReportSupplemental(loc, format, ap, ErrorKind.error);
243 va_end(ap);
247 * Print a warning message, increasing the global warning count.
248 * Params:
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, ...)
256 va_list ap;
257 va_start(ap, format);
258 verrorReport(loc, format, ap, ErrorKind.warning);
259 va_end(ap);
261 else
262 pragma(printf) extern (C++) void warning(const ref Loc loc, const(char)* format, ...)
264 va_list ap;
265 va_start(ap, format);
266 verrorReport(loc, format, ap, ErrorKind.warning);
267 va_end(ap);
271 * Print additional details about a warning message.
272 * Doesn't increase the warning count or print an additional warning prefix.
273 * Params:
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, ...)
281 va_list ap;
282 va_start(ap, format);
283 verrorReportSupplemental(loc, format, ap, ErrorKind.warning);
284 va_end(ap);
286 else
287 pragma(printf) extern (C++) void warningSupplemental(const ref Loc loc, const(char)* format, ...)
289 va_list ap;
290 va_start(ap, format);
291 verrorReportSupplemental(loc, format, ap, ErrorKind.warning);
292 va_end(ap);
296 * Print a deprecation message, may increase the global warning or error count
297 * depending on whether deprecations are ignored.
298 * Params:
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, ...)
306 va_list ap;
307 va_start(ap, format);
308 verrorReport(loc, format, ap, ErrorKind.deprecation);
309 va_end(ap);
311 else
312 pragma(printf) extern (C++) void deprecation(const ref Loc loc, const(char)* format, ...)
314 va_list ap;
315 va_start(ap, format);
316 verrorReport(loc, format, ap, ErrorKind.deprecation);
317 va_end(ap);
321 * Print additional details about a deprecation message.
322 * Doesn't increase the error count, or print an additional deprecation prefix.
323 * Params:
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, ...)
331 va_list ap;
332 va_start(ap, format);
333 verrorReportSupplemental(loc, format, ap, ErrorKind.deprecation);
334 va_end(ap);
336 else
337 pragma(printf) extern (C++) void deprecationSupplemental(const ref Loc loc, const(char)* format, ...)
339 va_list ap;
340 va_start(ap, format);
341 verrorReportSupplemental(loc, format, ap, ErrorKind.deprecation);
342 va_end(ap);
346 * Print a verbose message.
347 * Doesn't prefix or highlight messages.
348 * Params:
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, ...)
356 va_list ap;
357 va_start(ap, format);
358 verrorReport(loc, format, ap, ErrorKind.message);
359 va_end(ap);
361 else
362 pragma(printf) extern (C++) void message(const ref Loc loc, const(char)* format, ...)
364 va_list ap;
365 va_start(ap, format);
366 verrorReport(loc, format, ap, ErrorKind.message);
367 va_end(ap);
371 * Same as above, but doesn't take a location argument.
372 * Params:
373 * format = printf-style format specification
374 * ... = printf-style variadic arguments
376 static if (__VERSION__ < 2092)
377 extern (C++) void message(const(char)* format, ...)
379 va_list ap;
380 va_start(ap, format);
381 verrorReport(Loc.initial, format, ap, ErrorKind.message);
382 va_end(ap);
384 else
385 pragma(printf) extern (C++) void message(const(char)* format, ...)
387 va_list ap;
388 va_start(ap, format);
389 verrorReport(Loc.initial, format, ap, ErrorKind.message);
390 va_end(ap);
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.
409 * Params:
410 * format = printf-style format specification
411 * ... = printf-style variadic arguments
413 static if (__VERSION__ < 2092)
414 extern (C++) void tip(const(char)* format, ...)
416 va_list ap;
417 va_start(ap, format);
418 verrorReport(Loc.initial, format, ap, ErrorKind.tip);
419 va_end(ap);
421 else
422 pragma(printf) extern (C++) void tip(const(char)* format, ...)
424 va_list ap;
425 va_start(ap, format);
426 verrorReport(Loc.initial, format, ap, ErrorKind.tip);
427 va_end(ap);
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.
436 * Params:
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.
451 * Params:
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
479 * release builds.
481 extern (C++) void halt() @safe;