@@ -27,7 +27,7 @@ struct swift::ide::api::SDKNodeInitInfo {
27
27
SDKContext &Ctx;
28
28
DeclKind DKind;
29
29
AccessorKind AccKind;
30
-
30
+ SourceLoc Loc;
31
31
#define KEY_STRING (X, Y ) StringRef X;
32
32
#include " swift/IDE/DigesterEnums.def"
33
33
#define KEY_BOOL (X, Y ) bool X = false ;
@@ -52,6 +52,29 @@ struct swift::ide::api::SDKNodeInitInfo {
52
52
53
53
SDKContext::SDKContext (CheckerOptions Opts): Diags(SourceMgr), Opts(Opts) {}
54
54
55
+ DiagnosticEngine &SDKContext::getDiags (SourceLoc Loc) {
56
+ // If the location is invalid, we just use the locally created DiagEngine.
57
+ if (Loc.isInvalid ())
58
+ return Diags;
59
+ // If the Loc is valid, it may belong to any of the SourceManagers owned by
60
+ // the ASTContexts we created, thus we should go through the ASTContxts to find
61
+ // the right DiagnosticEngine to use.
62
+ for (auto &CI: CIs) {
63
+ if (CI->getSourceMgr ().isOwning (Loc))
64
+ return CI->getDiags ();
65
+ }
66
+ llvm_unreachable (" cannot find diagnostic engine to use" );
67
+ }
68
+
69
+ void SDKContext::addDiagConsumer (DiagnosticConsumer &Consumer) {
70
+ // we may emit diagnostics via any of the diagnostic engine, so add the consumer
71
+ // to all of them.
72
+ Diags.addConsumer (Consumer);
73
+ for (auto &CI: CIs) {
74
+ CI->getDiags ().addConsumer (Consumer);
75
+ }
76
+ }
77
+
55
78
void SDKNodeRoot::registerDescendant (SDKNode *D) {
56
79
// Operator doesn't have usr
57
80
if (isa<SDKNodeDeclOperator>(D))
@@ -70,7 +93,7 @@ SDKNodeRoot::SDKNodeRoot(SDKNodeInitInfo Info): SDKNode(Info, SDKNodeKind::Root)
70
93
JsonFormatVer(Info.JsonFormatVer.hasValue() ? *Info.JsonFormatVer : DIGESTER_JSON_DEFAULT_VERSION) {}
71
94
72
95
SDKNodeDecl::SDKNodeDecl (SDKNodeInitInfo Info, SDKNodeKind Kind)
73
- : SDKNode(Info, Kind), DKind(Info.DKind), Usr(Info.Usr),
96
+ : SDKNode(Info, Kind), DKind(Info.DKind), Usr(Info.Usr), Loc(Info.Loc),
74
97
Location(Info.Location), ModuleName(Info.ModuleName),
75
98
DeclAttributes(Info.DeclAttrs), IsImplicit(Info.IsImplicit),
76
99
IsStatic(Info.IsStatic), IsDeprecated(Info.IsDeprecated),
@@ -1297,7 +1320,7 @@ StringRef SDKContext::getInitKind(Decl *D) {
1297
1320
}
1298
1321
1299
1322
SDKNodeInitInfo::SDKNodeInitInfo (SDKContext &Ctx, Decl *D):
1300
- Ctx(Ctx), DKind(D->getKind ()),
1323
+ Ctx(Ctx), DKind(D->getKind ()), Loc(D-> getLoc ()),
1301
1324
Location(calculateLocation(Ctx, D)),
1302
1325
ModuleName(D->getModuleContext ()->getName().str()),
1303
1326
GenericSig(printGenericSignature(Ctx, D, /* Canonical*/ Ctx.checkingABI())),
@@ -2143,6 +2166,9 @@ swift::ide::api::getSDKNodeRoot(SDKContext &SDKCtx,
2143
2166
if (llvm::errs ().has_colors ())
2144
2167
PrintDiags.forceColors ();
2145
2168
CI.addDiagnosticConsumer (&PrintDiags);
2169
+ // The PrintDiags is only responsible compiler errors, we should remove the
2170
+ // consumer immediately after importing is done.
2171
+ SWIFT_DEFER { CI.getDiags ().removeConsumer (PrintDiags); };
2146
2172
if (CI.setup (Invocation)) {
2147
2173
llvm::errs () << " Failed to setup the compiler instance\n " ;
2148
2174
return nullptr ;
@@ -2211,7 +2237,7 @@ int swift::ide::api::deserializeSDKDump(StringRef dumpPath, StringRef OutputPath
2211
2237
}
2212
2238
PrintingDiagnosticConsumer PDC;
2213
2239
SDKContext Ctx (Opts);
2214
- Ctx.getDiags (). addConsumer (PDC);
2240
+ Ctx.addDiagConsumer (PDC);
2215
2241
2216
2242
SwiftDeclCollector Collector (Ctx);
2217
2243
Collector.deSerialize (dumpPath);
@@ -2227,7 +2253,7 @@ int swift::ide::api::findDeclUsr(StringRef dumpPath, CheckerOptions Opts) {
2227
2253
}
2228
2254
PrintingDiagnosticConsumer PDC;
2229
2255
SDKContext Ctx (Opts);
2230
- Ctx.getDiags (). addConsumer (PDC);
2256
+ Ctx.addDiagConsumer (PDC);
2231
2257
2232
2258
SwiftDeclCollector Collector (Ctx);
2233
2259
Collector.deSerialize (dumpPath);
0 commit comments