Skip to content

Commit ff1bdd4

Browse files
authored
dmd.error: expose ErrorSink to C++ compiler interface (#23773)
1 parent 52c8c25 commit ff1bdd4

5 files changed

Lines changed: 94 additions & 22 deletions

File tree

compiler/include/dmd/errors.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,8 @@
1111
#pragma once
1212

1313
#include "root/dsystem.h"
14-
15-
struct Loc;
16-
17-
// Constants used to discriminate kinds of error messages.
18-
enum class ErrorKind
19-
{
20-
warning = 0,
21-
deprecation = 1,
22-
error = 2,
23-
message = 3,
24-
};
14+
#include "errorsink.h"
15+
#include "globals.h"
2516

2617
#if defined(__GNUC__)
2718
#define D_ATTRIBUTE_FORMAT(m, n) __attribute__((format(printf, m, n))) __attribute__((nonnull (m)))
@@ -51,3 +42,12 @@ D_ATTRIBUTE_FORMAT(2, 3) void message(Loc loc, const char *format, ...);
5142
// Called after printing out fatal error messages.
5243
D_ATTRIBUTE_NORETURN void fatal();
5344
D_ATTRIBUTE_NORETURN void halt();
45+
46+
class ErrorSinkCompiler : public ErrorSink
47+
{
48+
public:
49+
uint32_t errorLimit;
50+
Diagnostic useWarnings;
51+
Diagnostic useDeprecated;
52+
d_bool showGaggedErrors;
53+
};

compiler/include/dmd/errorsink.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
/* Compiler implementation of the D programming language
3+
* Copyright (C) 1999-2026 by The D Language Foundation, All Rights Reserved
4+
* written by Walter Bright
5+
* https://www.digitalmars.com
6+
* Distributed under the Boost Software License, Version 1.0.
7+
* https://www.boost.org/LICENSE_1_0.txt
8+
* https://github.com/dlang/dmd/blob/master/src/dmd/errorsink.h
9+
*/
10+
11+
#pragma once
12+
13+
#include "root/dsystem.h"
14+
15+
struct Loc;
16+
17+
// Constants used to discriminate kinds of error messages.
18+
enum class ErrorKind
19+
{
20+
warning = 0,
21+
deprecation = 1,
22+
error = 2,
23+
message = 3,
24+
};
25+
26+
class ErrorSink
27+
{
28+
public:
29+
virtual void verror(Loc loc, const char *format, va_list ap) = 0;
30+
virtual void verrorSupplemental(Loc loc, const char *format, va_list ap) = 0;
31+
virtual void vwarning(Loc loc, const char *format, va_list ap) = 0;
32+
virtual void vwarningSupplemental(Loc loc, const char *format, va_list ap) = 0;
33+
virtual void vmessage(Loc loc, const char *format, va_list ap) = 0;
34+
virtual void vdeprecation(Loc loc, const char *format, va_list ap) = 0;
35+
virtual void vdeprecationSupplemental(Loc loc, const char *format, va_list ap) = 0;
36+
37+
virtual void error(Loc loc, const char *format, ...);
38+
virtual void errorSupplemental(Loc loc, const char *format, ...);
39+
virtual void warning(Loc loc, const char *format, ...);
40+
virtual void warningSupplemental(Loc loc, const char *format, ...);
41+
virtual void message(Loc loc, const char *format, ...);
42+
virtual void deprecation(Loc loc, const char *format, ...);
43+
virtual void deprecationSupplemental(Loc loc, const char *format, ...);
44+
virtual void plugSink();
45+
};
46+
47+
class ErrorSinkNull : public ErrorSink
48+
{
49+
public:
50+
void verror(Loc loc, const char *format, va_list ap) override;
51+
void verrorSupplemental(Loc loc, const char *format, va_list ap) override;
52+
void vwarning(Loc loc, const char *format, va_list ap) override;
53+
void vwarningSupplemental(Loc loc, const char *format, va_list ap) override;
54+
void vmessage(Loc loc, const char *format, va_list ap) override;
55+
void vdeprecation(Loc loc, const char *format, va_list ap) override;
56+
void vdeprecationSupplemental(Loc loc, const char *format, va_list ap) override;
57+
};
58+
59+
class ErrorSinkLatch : public ErrorSinkNull
60+
{
61+
public:
62+
bool sawErrors;
63+
64+
void verror(Loc loc, const char *format, va_list ap) override;
65+
};
66+
67+
class ErrorSinkStderr : public ErrorSink
68+
{
69+
public:
70+
void verror(Loc loc, const char *format, va_list ap) override;
71+
void verrorSupplemental(Loc loc, const char *format, va_list ap) override;
72+
void vwarning(Loc loc, const char *format, va_list ap) override;
73+
void vwarningSupplemental(Loc loc, const char *format, va_list ap) override;
74+
void vdeprecation(Loc loc, const char *format, va_list ap) override;
75+
void vmessage(Loc loc, const char *format, va_list ap) override;
76+
void vdeprecationSupplemental(Loc loc, const char *format, va_list ap) override;
77+
};

compiler/src/dmd/errors.d

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ nothrow:
3636
* and delegates the actual output to the virtual $(D emit) method, which
3737
* subclasses such as $(D dmd.sarif.ErrorSinkSarif) override to change format.
3838
*/
39-
class ErrorSinkCompiler : ErrorSink
39+
extern (C++) class ErrorSinkCompiler : ErrorSink
4040
{
4141
/// Maximum number of errors/deprecations to display before calling $(D fatal).
4242
/// 0 means unlimited.
@@ -59,7 +59,6 @@ class ErrorSinkCompiler : ErrorSink
5959
}
6060

6161
nothrow:
62-
extern (C++):
6362

6463
// Overrides of the abstract ErrorSink methods convert Loc to SourceLoc
6564
// and dispatch to the SourceLoc-taking overload that holds the gating body.

compiler/src/dmd/errorsink.d

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ enum ErrorKind
2727
/***************************************
2828
* Where error/warning/deprecation messages go.
2929
*/
30-
abstract class ErrorSink
30+
extern (C++) abstract class ErrorSink
3131
{
3232
nothrow:
33-
extern (C++):
3433

3534
void verror(Loc loc, const(char)* format, va_list ap);
3635
void verrorSupplemental(Loc loc, const(char)* format, va_list ap);
@@ -177,10 +176,9 @@ else
177176
/*****************************************
178177
* Just ignores the messages.
179178
*/
180-
class ErrorSinkNull : ErrorSink
179+
extern (C++) class ErrorSinkNull : ErrorSink
181180
{
182181
nothrow:
183-
extern (C++):
184182
override:
185183

186184
void verror(Loc loc, const(char)* format, va_list ap) { }
@@ -201,10 +199,9 @@ class ErrorSinkNull : ErrorSink
201199
/*****************************************
202200
* Ignores the messages, but sets `sawErrors` for any calls to `error()`
203201
*/
204-
class ErrorSinkLatch : ErrorSinkNull
202+
extern (C++) class ErrorSinkLatch : ErrorSinkNull
205203
{
206204
nothrow:
207-
extern (C++):
208205
override:
209206

210207
bool sawErrors;
@@ -216,13 +213,12 @@ class ErrorSinkLatch : ErrorSinkNull
216213
* Simplest implementation, just sends messages to stderr.
217214
* See also: ErrorSinkCompiler.
218215
*/
219-
class ErrorSinkStderr : ErrorSink
216+
extern (C++) class ErrorSinkStderr : ErrorSink
220217
{
221218
import core.stdc.stdio;
222219
import core.stdc.stdarg;
223220

224221
nothrow:
225-
extern (C++):
226222
override:
227223

228224
void verror(Loc loc, const(char)* format, va_list ap)

compiler/src/dmd/expressionsem.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16887,7 +16887,7 @@ MATCH matchType(FuncExp funcExp, Type to, Scope* sc, FuncExp* presult, ErrorSink
1688716887
(*presult).fd.modifyReturns(sc, tof.next);
1688816888
}
1688916889
}
16890-
else if (!cast(ErrorSinkNull)eSink)
16890+
else
1689116891
{
1689216892
auto ts = toAutoQualChars(tx, to);
1689316893
eSink.error(loc, "cannot implicitly convert expression `%s` of type `%s` to `%s`",

0 commit comments

Comments
 (0)