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