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