Skip to content

Commit 1b70ba1

Browse files
Fix CachingDiagnosticsProcessor when replay by FileSpecificDiagConsumer
When using a FileSpecificDiagConsumer, the source file that doesn't have any diagnostics need to exist in the source manager for the consumer to be successfully created.
1 parent 4713770 commit 1b70ba1

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

lib/Frontend/CachedDiagnostics.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,33 @@ struct DiagnosticSerializer {
108108
ReplayFunc Fn = nullptr);
109109
llvm::Error serializeEmittedDiagnostics(llvm::raw_ostream &os);
110110

111-
static llvm::Error emitDiagnosticsFromCached(llvm::StringRef Buffer,
112-
SourceManager &SrcMgr,
113-
DiagnosticEngine &Diags) {
111+
static llvm::Error
112+
emitDiagnosticsFromCached(llvm::StringRef Buffer, SourceManager &SrcMgr,
113+
DiagnosticEngine &Diags,
114+
const FrontendInputsAndOutputs &InAndOut) {
114115
// Create a new DiagnosticSerializer since this cannot be shared with a
115116
// serialization instance.
116117
DiagnosticSerializer DS(SrcMgr.getFileSystem());
118+
DS.addInputsToSourceMgr(InAndOut);
117119
return DS.doEmitFromCached(Buffer, Diags);
118120
}
119121

120122
SourceManager &getSourceMgr() { return SrcMgr; }
121123

124+
void addInputsToSourceMgr(const FrontendInputsAndOutputs &InAndOut) {
125+
// Extract all the input file names so they can be added to the source
126+
// manager when replaying the diagnostics. All input files are needed even
127+
// they don't contain diagnostics because FileSpecificDiagConsumer need
128+
// has references to input files to find subconsumer.
129+
auto addInputToSourceMgr = [&](const InputFile &Input) {
130+
if (Input.getFileName() != "-")
131+
SrcMgr.getExternalSourceBufferID(Input.getFileName());
132+
return false;
133+
};
134+
InAndOut.forEachInputProducingSupplementaryOutput(addInputToSourceMgr);
135+
InAndOut.forEachNonPrimaryInput(addInputToSourceMgr);
136+
}
137+
122138
private:
123139
// Serialization helper
124140
unsigned getFileIDFromBufferID(SourceManager &SM, unsigned Idx);
@@ -632,7 +648,7 @@ class CachingDiagnosticsProcessor::Implementation
632648

633649
llvm::Error replayCachedDiagnostics(llvm::StringRef Buffer) {
634650
return DiagnosticSerializer::emitDiagnosticsFromCached(
635-
Buffer, getDiagnosticSourceMgr(), Diags);
651+
Buffer, getDiagnosticSourceMgr(), Diags, InAndOut);
636652
}
637653

638654
void handleDiagnostic(SourceManager &SM,
@@ -683,18 +699,7 @@ class CachingDiagnosticsProcessor::Implementation
683699
if (!Serializer) {
684700
Serializer.reset(
685701
new DiagnosticSerializer(InstanceSourceMgr.getFileSystem()));
686-
auto &SM = Serializer->getSourceMgr();
687-
// Extract all the input file names so they can be added to the source
688-
// manager when replaying the diagnostics. All input files are needed even
689-
// they don't contain diagnostics because FileSpecificDiagConsumer need
690-
// has references to input files to find subconsumer.
691-
auto addInputToSourceMgr = [&](const InputFile &Input) {
692-
if (Input.getFileName() != "-")
693-
SM.getExternalSourceBufferID(Input.getFileName());
694-
return false;
695-
};
696-
InAndOut.forEachInputProducingSupplementaryOutput(addInputToSourceMgr);
697-
InAndOut.forEachNonPrimaryInput(addInputToSourceMgr);
702+
Serializer->addInputsToSourceMgr(InAndOut);
698703
}
699704

700705
return *Serializer;

0 commit comments

Comments
 (0)