Skip to content

Commit d5f9247

Browse files
ibuclawkinke
authored andcommitted
dmd.error: expose ErrorSink to C++ compiler interface (#23773)
1 parent b119000 commit d5f9247

5 files changed

Lines changed: 94 additions & 23 deletions

File tree

compiler/include/dmd/errors.h

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +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-
tip = 3,
24-
message = 4,
25-
};
14+
#include "errorsink.h"
15+
#include "globals.h"
2616

2717
#if defined(__GNUC__)
2818
#define D_ATTRIBUTE_FORMAT(m, n) __attribute__((format(printf, m, n))) __attribute__((nonnull (m)))
@@ -53,3 +43,12 @@ D_ATTRIBUTE_FORMAT(1, 2) void tip(const char *format, ...);
5343
// Called after printing out fatal error messages.
5444
D_ATTRIBUTE_NORETURN void fatal();
5545
D_ATTRIBUTE_NORETURN void halt();
46+
47+
class ErrorSinkCompiler : public ErrorSink
48+
{
49+
public:
50+
uint32_t errorLimit;
51+
Diagnostic useWarnings;
52+
Diagnostic useDeprecated;
53+
d_bool showGaggedErrors;
54+
};

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
@@ -28,10 +28,9 @@ enum ErrorKind
2828
/***************************************
2929
* Where error/warning/deprecation messages go.
3030
*/
31-
abstract class ErrorSink
31+
extern (C++) abstract class ErrorSink
3232
{
3333
nothrow:
34-
extern (C++):
3534

3635
void verror(Loc loc, const(char)* format, va_list ap);
3736
void verrorSupplemental(Loc loc, const(char)* format, va_list ap);
@@ -110,10 +109,9 @@ abstract class ErrorSink
110109
/*****************************************
111110
* Just ignores the messages.
112111
*/
113-
class ErrorSinkNull : ErrorSink
112+
extern (C++) class ErrorSinkNull : ErrorSink
114113
{
115114
nothrow:
116-
extern (C++):
117115
override:
118116

119117
void verror(Loc loc, const(char)* format, va_list ap) { }
@@ -134,10 +132,9 @@ class ErrorSinkNull : ErrorSink
134132
/*****************************************
135133
* Ignores the messages, but sets `sawErrors` for any calls to `error()`
136134
*/
137-
class ErrorSinkLatch : ErrorSinkNull
135+
extern (C++) class ErrorSinkLatch : ErrorSinkNull
138136
{
139137
nothrow:
140-
extern (C++):
141138
override:
142139

143140
bool sawErrors;
@@ -149,13 +146,12 @@ class ErrorSinkLatch : ErrorSinkNull
149146
* Simplest implementation, just sends messages to stderr.
150147
* See also: ErrorSinkCompiler.
151148
*/
152-
class ErrorSinkStderr : ErrorSink
149+
extern (C++) class ErrorSinkStderr : ErrorSink
153150
{
154151
import core.stdc.stdio;
155152
import core.stdc.stdarg;
156153

157154
nothrow:
158-
extern (C++):
159155
override:
160156

161157
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
@@ -16783,7 +16783,7 @@ MATCH matchType(FuncExp funcExp, Type to, Scope* sc, FuncExp* presult, ErrorSink
1678316783
(*presult).fd.modifyReturns(sc, tof.next);
1678416784
}
1678516785
}
16786-
else if (!cast(ErrorSinkNull)eSink)
16786+
else
1678716787
{
1678816788
auto ts = toAutoQualChars(tx, to);
1678916789
eSink.error(loc, "cannot implicitly convert expression `%s` of type `%s` to `%s`",

0 commit comments

Comments
 (0)