Skip to content

Commit c139c59

Browse files
author
David Ungar
committed
Pass defaultDiagnosticLoc to handleDiagnostic, not currentPrimaryInput.
1 parent 3b8b903 commit c139c59

23 files changed

+134
-132
lines changed

include/swift/AST/DiagnosticConsumer.h

+8-11
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class DiagnosticConsumer {
9898
DiagnosticKind Kind, StringRef FormatString,
9999
ArrayRef<DiagnosticArgument> FormatArgs,
100100
const DiagnosticInfo &Info,
101-
StringRef currentPrimaryInput) = 0;
101+
SourceLoc defaultDiagnosticLoc) = 0;
102102

103103
/// \returns true if an error occurred while finishing-up.
104104
virtual bool finishProcessing() { return false; }
@@ -120,7 +120,7 @@ class NullDiagnosticConsumer : public DiagnosticConsumer {
120120
StringRef FormatString,
121121
ArrayRef<DiagnosticArgument> FormatArgs,
122122
const DiagnosticInfo &Info,
123-
StringRef currentPrimaryInput) override;
123+
SourceLoc defaultDiagnosticLoc) override;
124124
};
125125

126126
/// DiagnosticConsumer that forwards diagnostics to the consumers of
@@ -133,7 +133,7 @@ class ForwardingDiagnosticConsumer : public DiagnosticConsumer {
133133
StringRef FormatString,
134134
ArrayRef<DiagnosticArgument> FormatArgs,
135135
const DiagnosticInfo &Info,
136-
StringRef currentPrimaryInput) override;
136+
SourceLoc defaultDiagnosticLoc) override;
137137
};
138138

139139
/// DiagnosticConsumer that funnels diagnostics in certain files to
@@ -195,12 +195,12 @@ class FileSpecificDiagnosticConsumer : public DiagnosticConsumer {
195195
StringRef FormatString,
196196
ArrayRef<DiagnosticArgument> FormatArgs,
197197
const DiagnosticInfo &Info,
198-
StringRef currentPrimaryInput) {
198+
const SourceLoc defaultDiagnosticLoc) {
199199
if (!getConsumer())
200200
return;
201201
hasAnErrorBeenConsumed |= Kind == DiagnosticKind::Error;
202202
getConsumer()->handleDiagnostic(SM, Loc, Kind, FormatString, FormatArgs,
203-
Info, currentPrimaryInput);
203+
Info, defaultDiagnosticLoc);
204204
}
205205

206206
void informDriverOfIncompleteBatchModeCompilation() {
@@ -291,7 +291,7 @@ class FileSpecificDiagnosticConsumer : public DiagnosticConsumer {
291291
StringRef FormatString,
292292
ArrayRef<DiagnosticArgument> FormatArgs,
293293
const DiagnosticInfo &Info,
294-
StringRef currentPrimaryInput) override;
294+
SourceLoc defaultDiagnosticLoc) override;
295295

296296
bool finishProcessing() override;
297297

@@ -312,14 +312,11 @@ class FileSpecificDiagnosticConsumer : public DiagnosticConsumer {
312312

313313
Optional<FileSpecificDiagnosticConsumer::Subconsumer *>
314314
findSubconsumerForAnyKind(SourceManager &SM, SourceLoc loc,
315-
DiagnosticKind Kind, StringRef currentPrimaryInput);
315+
DiagnosticKind Kind, SourceLoc defaultDiagnosticLoc);
316316

317317
Optional<FileSpecificDiagnosticConsumer::Subconsumer *>
318318
findSubconsumerForNonNote(SourceManager &SM, SourceLoc loc,
319-
StringRef currentPrimaryInput);
320-
321-
SourceLoc findLocationOfCurrentPrimaryFile(SourceManager &SM,
322-
StringRef currentPrimaryInput);
319+
SourceLoc defaultDiagnosticLoc);
323320
};
324321

325322
} // end namespace swift

include/swift/AST/DiagnosticEngine.h

+10-9
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,8 @@ namespace swift {
562562
unsigned TransactionCount = 0;
563563

564564
/// For batch mode, use this to know where to output a diagnostic
565-
/// from a non-primary file.
566-
std::string CurrentPrimaryInput;
565+
/// from a non-primary file. May be invalid.
566+
SourceLoc defaultDiagnosticLoc;
567567

568568
friend class InFlightDiagnostic;
569569
friend class DiagnosticTransaction;
@@ -804,21 +804,22 @@ namespace swift {
804804
public:
805805
static const char *diagnosticStringFor(const DiagID id);
806806

807-
StringRef getCurrentPrimaryInput() const { return CurrentPrimaryInput; }
808-
void setCurrentPrimaryInput(std::string s) { CurrentPrimaryInput = s; }
807+
SourceLoc getDefaultDiagnostLoc() const { return defaultDiagnosticLoc; }
808+
void setDefaultDiagnostLocToInput(StringRef defaultDiagnosticInputFile);
809+
void resetDefaultDiagnostLoc();
809810
};
810811

811-
class CurrentPrimaryInputRAII {
812+
class DefaultDiagnosticLocRAII {
812813
private:
813814
DiagnosticEngine &Diags;
814815

815816
public:
816-
CurrentPrimaryInputRAII(DiagnosticEngine &Diags,
817-
StringRef currentPrimaryInput)
817+
DefaultDiagnosticLocRAII(DiagnosticEngine &Diags,
818+
StringRef defaultDiagnosticInputFile)
818819
: Diags(Diags) {
819-
Diags.setCurrentPrimaryInput(currentPrimaryInput);
820+
Diags.setDefaultDiagnostLocToInput(defaultDiagnosticInputFile);
820821
}
821-
~CurrentPrimaryInputRAII() { Diags.setCurrentPrimaryInput(""); }
822+
~DefaultDiagnosticLocRAII() { Diags.resetDefaultDiagnostLoc(); }
822823
};
823824

824825
/// Represents a diagnostic transaction. While a transaction is

include/swift/Frontend/PrintingDiagnosticConsumer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class PrintingDiagnosticConsumer : public DiagnosticConsumer {
3939
DiagnosticKind Kind, StringRef FormatString,
4040
ArrayRef<DiagnosticArgument> FormatArgs,
4141
const DiagnosticInfo &Info,
42-
StringRef currentPrimaryInput) override;
42+
SourceLoc defaultDiagnosticLoc) override;
4343

4444
void forceColors() {
4545
ForceColors = true;

include/swift/Migrator/FixitApplyDiagnosticConsumer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class FixitApplyDiagnosticConsumer final
6666
StringRef FormatString,
6767
ArrayRef<DiagnosticArgument> FormatArgs,
6868
const DiagnosticInfo &Info,
69-
StringRef currentPrimaryInput) override;
69+
SourceLoc defaultDiagnosticLoc) override;
7070

7171
unsigned getNumFixitsApplied() const {
7272
return NumFixitsApplied;

lib/AST/DiagnosticConsumer.cpp

+12-24
Original file line numberDiff line numberDiff line change
@@ -165,32 +165,32 @@ FileSpecificDiagnosticConsumer::subconsumerForLocation(SourceManager &SM,
165165
void FileSpecificDiagnosticConsumer::handleDiagnostic(
166166
SourceManager &SM, SourceLoc Loc, DiagnosticKind Kind,
167167
StringRef FormatString, ArrayRef<DiagnosticArgument> FormatArgs,
168-
const DiagnosticInfo &Info, StringRef currentPrimaryInput) {
168+
const DiagnosticInfo &Info, const SourceLoc defaultDiagnosticLoc) {
169169

170170
HasAnErrorBeenConsumed |= Kind == DiagnosticKind::Error;
171171

172172
auto subconsumer =
173-
findSubconsumerForAnyKind(SM, Loc, Kind, currentPrimaryInput);
173+
findSubconsumerForAnyKind(SM, Loc, Kind, defaultDiagnosticLoc);
174174
if (subconsumer) {
175175
subconsumer.getValue()->handleDiagnostic(
176-
SM, Loc, Kind, FormatString, FormatArgs, Info, currentPrimaryInput);
176+
SM, Loc, Kind, FormatString, FormatArgs, Info, defaultDiagnosticLoc);
177177
return;
178178
}
179179
// Last resort: spray it everywhere
180180
for (auto &subconsumer : Subconsumers)
181181
subconsumer.handleDiagnostic(SM, Loc, Kind, FormatString, FormatArgs, Info,
182-
currentPrimaryInput);
182+
defaultDiagnosticLoc);
183183
}
184184

185185
Optional<FileSpecificDiagnosticConsumer::Subconsumer *>
186186
FileSpecificDiagnosticConsumer::findSubconsumerForAnyKind(
187187
SourceManager &SM, SourceLoc loc, DiagnosticKind Kind,
188-
StringRef currentPrimaryInput) {
188+
SourceLoc defaultDiagnosticLoc) {
189189
switch (Kind) {
190190
case DiagnosticKind::Error:
191191
case DiagnosticKind::Warning:
192192
case DiagnosticKind::Remark: {
193-
auto subconsumer = findSubconsumerForNonNote(SM, loc, currentPrimaryInput);
193+
auto subconsumer = findSubconsumerForNonNote(SM, loc, defaultDiagnosticLoc);
194194
SubconsumerForSubsequentNotes = subconsumer;
195195
return subconsumer;
196196
}
@@ -201,33 +201,21 @@ FileSpecificDiagnosticConsumer::findSubconsumerForAnyKind(
201201

202202
Optional<FileSpecificDiagnosticConsumer::Subconsumer *>
203203
FileSpecificDiagnosticConsumer::findSubconsumerForNonNote(
204-
SourceManager &SM, const SourceLoc loc, StringRef currentPrimaryInput) {
204+
SourceManager &SM, const SourceLoc loc, const SourceLoc defaultDiagnosticLoc) {
205205
const auto subconsumer = subconsumerForLocation(SM, loc);
206206
if (!subconsumer)
207207
return None; // No place to put it
208208
if ((*subconsumer)->getConsumer())
209209
return subconsumer; // A primary file with a .dia file
210-
const auto locInPrimary =
211-
findLocationOfCurrentPrimaryFile(SM, currentPrimaryInput);
212-
if (locInPrimary.isInvalid())
210+
if (defaultDiagnosticLoc.isInvalid())
213211
return None;
214212
const auto currentPrimarySubconsumer =
215-
subconsumerForLocation(SM, locInPrimary);
213+
subconsumerForLocation(SM, defaultDiagnosticLoc);
216214
assert(currentPrimarySubconsumer && (*subconsumer)->getConsumer() &&
217215
"current primary must have a .dia file");
218216
return currentPrimarySubconsumer;
219217
}
220218

221-
SourceLoc FileSpecificDiagnosticConsumer::findLocationOfCurrentPrimaryFile(
222-
SourceManager &SM, StringRef currentPrimaryInput) {
223-
if (currentPrimaryInput.empty())
224-
return SourceLoc();
225-
auto id = SM.getIDForBufferIdentifier(currentPrimaryInput);
226-
if (!id)
227-
return SourceLoc();
228-
return SM.getLocForBufferStart(*id);
229-
}
230-
231219
bool FileSpecificDiagnosticConsumer::finishProcessing() {
232220
tellSubconsumersToInformDriverOfIncompleteBatchModeCompilation();
233221

@@ -252,7 +240,7 @@ void FileSpecificDiagnosticConsumer::
252240
void NullDiagnosticConsumer::handleDiagnostic(
253241
SourceManager &SM, SourceLoc Loc, DiagnosticKind Kind,
254242
StringRef FormatString, ArrayRef<DiagnosticArgument> FormatArgs,
255-
const DiagnosticInfo &Info, StringRef) {
243+
const DiagnosticInfo &Info, const SourceLoc) {
256244
LLVM_DEBUG({
257245
llvm::dbgs() << "NullDiagnosticConsumer received diagnostic: ";
258246
DiagnosticEngine::formatDiagnosticText(llvm::dbgs(), FormatString,
@@ -267,7 +255,7 @@ ForwardingDiagnosticConsumer::ForwardingDiagnosticConsumer(DiagnosticEngine &Tar
267255
void ForwardingDiagnosticConsumer::handleDiagnostic(
268256
SourceManager &SM, SourceLoc Loc, DiagnosticKind Kind,
269257
StringRef FormatString, ArrayRef<DiagnosticArgument> FormatArgs,
270-
const DiagnosticInfo &Info, StringRef currentPrimaryInput) {
258+
const DiagnosticInfo &Info, const SourceLoc defaultDiagnosticLoc) {
271259
LLVM_DEBUG({
272260
llvm::dbgs() << "ForwardingDiagnosticConsumer received diagnostic: ";
273261
DiagnosticEngine::formatDiagnosticText(llvm::dbgs(), FormatString,
@@ -276,6 +264,6 @@ void ForwardingDiagnosticConsumer::handleDiagnostic(
276264
});
277265
for (auto *C : TargetEngine.getConsumers()) {
278266
C->handleDiagnostic(SM, Loc, Kind, FormatString, FormatArgs, Info,
279-
currentPrimaryInput);
267+
defaultDiagnosticLoc);
280268
}
281269
}

lib/AST/DiagnosticEngine.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -839,14 +839,30 @@ void DiagnosticEngine::emitDiagnostic(const Diagnostic &diagnostic) {
839839
Consumer->handleDiagnostic(SourceMgr, loc, toDiagnosticKind(behavior),
840840
diagnosticStringFor(Info.ID),
841841
diagnostic.getArgs(), Info,
842-
getCurrentPrimaryInput());
842+
getDefaultDiagnostLoc());
843843
}
844844
}
845845

846846
const char *DiagnosticEngine::diagnosticStringFor(const DiagID id) {
847847
return diagnosticStrings[(unsigned)id];
848848
}
849849

850+
void DiagnosticEngine::setDefaultDiagnostLocToInput(StringRef input) {
851+
if (input.empty()) {
852+
resetDefaultDiagnostLoc();
853+
return;
854+
}
855+
auto id = SourceMgr.getIDForBufferIdentifier(input);
856+
if (!id) {
857+
resetDefaultDiagnostLoc();
858+
return;
859+
}
860+
defaultDiagnosticLoc = SourceMgr.getLocForBufferStart(*id);
861+
}
862+
void DiagnosticEngine::resetDefaultDiagnostLoc() {
863+
defaultDiagnosticLoc = SourceLoc();
864+
}
865+
850866
DiagnosticSuppression::DiagnosticSuppression(DiagnosticEngine &diags)
851867
: diags(diags)
852868
{

lib/Frontend/PrintingDiagnosticConsumer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ namespace {
6666
void PrintingDiagnosticConsumer::handleDiagnostic(
6767
SourceManager &SM, SourceLoc Loc, DiagnosticKind Kind,
6868
StringRef FormatString, ArrayRef<DiagnosticArgument> FormatArgs,
69-
const DiagnosticInfo &Info, StringRef currentPrimaryInput) {
69+
const DiagnosticInfo &Info, const SourceLoc defaultDiagnosticLoc) {
7070
// Determine what kind of diagnostic we're emitting.
7171
llvm::SourceMgr::DiagKind SMKind;
7272
switch (Kind) {

lib/Frontend/SerializedDiagnosticConsumer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class SerializedDiagnosticConsumer : public DiagnosticConsumer {
195195
StringRef FormatString,
196196
ArrayRef<DiagnosticArgument> FormatArgs,
197197
const DiagnosticInfo &Info,
198-
StringRef currentPrimaryInput) override;
198+
SourceLoc defaultDiagnosticLoc) override;
199199

200200
/// The version of the diagnostics file.
201201
enum { Version = 1 };
@@ -545,7 +545,7 @@ emitDiagnosticMessage(SourceManager &SM,
545545
void SerializedDiagnosticConsumer::handleDiagnostic(
546546
SourceManager &SM, SourceLoc Loc, DiagnosticKind Kind,
547547
StringRef FormatString, ArrayRef<DiagnosticArgument> FormatArgs,
548-
const DiagnosticInfo &Info, StringRef currentPrimaryInput) {
548+
const DiagnosticInfo &Info, const SourceLoc defaultDiagnosticLoc) {
549549

550550
// Enter the block for a non-note diagnostic immediately, rather
551551
// than waiting for beginDiagnostic, in case associated notes

lib/FrontendTool/FrontendTool.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ class JSONFixitWriter
410410
StringRef FormatString,
411411
ArrayRef<DiagnosticArgument> FormatArgs,
412412
const DiagnosticInfo &Info,
413-
StringRef currentPrimaryInput) override {
413+
const SourceLoc defaultDiagnosticLoc) override {
414414
if (!(FixitAll || shouldTakeFixit(Kind, Info)))
415415
return;
416416
for (const auto &Fix : Info.FixIts) {
@@ -1218,7 +1218,7 @@ static bool performCompileStepsPostSILGen(
12181218
SILOptions &SILOpts = Invocation.getSILOptions();
12191219
IRGenOptions &IRGenOpts = Invocation.getIRGenOptions();
12201220

1221-
CurrentPrimaryInputRAII cpi(Context.Diags,
1221+
DefaultDiagnosticLocRAII cpi(Context.Diags,
12221222
PSPs.MainInputFilenameForDebugInfo);
12231223

12241224
if (Stats)
@@ -1632,9 +1632,9 @@ int swift::performFrontend(ArrayRef<const char *> Args,
16321632
"fatal error encountered during compilation; please "
16331633
"file a bug report with your project and the crash "
16341634
"log",
1635-
{}, DiagnosticInfo(), "");
1635+
{}, DiagnosticInfo(), SourceLoc());
16361636
PDC.handleDiagnostic(dummyMgr, SourceLoc(), DiagnosticKind::Note, reason,
1637-
{}, DiagnosticInfo(), "");
1637+
{}, DiagnosticInfo(), SourceLoc());
16381638
if (shouldCrash)
16391639
abort();
16401640
};

lib/Migrator/FixitApplyDiagnosticConsumer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void FixitApplyDiagnosticConsumer::printResult(llvm::raw_ostream &OS) const {
3434
void FixitApplyDiagnosticConsumer::handleDiagnostic(
3535
SourceManager &SM, SourceLoc Loc, DiagnosticKind Kind,
3636
StringRef FormatString, ArrayRef<DiagnosticArgument> FormatArgs,
37-
const DiagnosticInfo &Info, StringRef currentPrimaryInput) {
37+
const DiagnosticInfo &Info, const SourceLoc defaultDiagnosticLoc) {
3838
if (Loc.isInvalid()) {
3939
return;
4040
}

lib/Sema/InstrumenterSupport.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ErrorGatherer : public DiagnosticConsumer {
3737
StringRef FormatString,
3838
ArrayRef<DiagnosticArgument> FormatArgs,
3939
const DiagnosticInfo &Info,
40-
StringRef currentPrimaryInput) override {
40+
const SourceLoc defaultDiagnosticLoc) override {
4141
if (Kind == swift::DiagnosticKind::Error) {
4242
error = true;
4343
}

lib/Sema/TypeChecker.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ void swift::performTypeChecking(SourceFile &SF, TopLevelContext &TLC,
460460
return;
461461

462462
auto &Ctx = SF.getASTContext();
463-
CurrentPrimaryInputRAII cpr(SF.getASTContext().Diags, SF.getFilename());
463+
DefaultDiagnosticLocRAII cpr(SF.getASTContext().Diags, SF.getFilename());
464464

465465
// Make sure we have a type checker.
466466
TypeChecker &TC = createTypeChecker(Ctx);

tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class StreamDiagConsumer : public DiagnosticConsumer {
5050
StringRef FormatString,
5151
ArrayRef<DiagnosticArgument> FormatArgs,
5252
const DiagnosticInfo &Info,
53-
StringRef currentPrimaryInput) override {
53+
const SourceLoc defaultDiagnosticLoc) override {
5454
// FIXME: Print location info if available.
5555
switch (Kind) {
5656
case DiagnosticKind::Error: OS << "error: "; break;

tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1183,9 +1183,9 @@ accept(SourceManager &SM, RegionType RegionType,
11831183
void RequestRefactoringEditConsumer::handleDiagnostic(
11841184
SourceManager &SM, SourceLoc Loc, DiagnosticKind Kind,
11851185
StringRef FormatString, ArrayRef<DiagnosticArgument> FormatArgs,
1186-
const DiagnosticInfo &Info, StringRef currentPrimaryInput) {
1186+
const DiagnosticInfo &Info, const SourceLoc defaultDiagnosticLoc) {
11871187
Impl.DiagConsumer.handleDiagnostic(SM, Loc, Kind, FormatString, FormatArgs,
1188-
Info, currentPrimaryInput);
1188+
Info, defaultDiagnosticLoc);
11891189
}
11901190

11911191
class RequestRenameRangeConsumer::Implementation {
@@ -1242,9 +1242,9 @@ void RequestRenameRangeConsumer::accept(
12421242
void RequestRenameRangeConsumer::handleDiagnostic(
12431243
SourceManager &SM, SourceLoc Loc, DiagnosticKind Kind,
12441244
StringRef FormatString, ArrayRef<DiagnosticArgument> FormatArgs,
1245-
const DiagnosticInfo &Info, StringRef currentPrimaryInput) {
1245+
const DiagnosticInfo &Info, const SourceLoc defaultDiagnosticLoc) {
12461246
Impl.DiagConsumer.handleDiagnostic(SM, Loc, Kind, FormatString, FormatArgs,
1247-
Info, currentPrimaryInput);
1247+
Info, defaultDiagnosticLoc);
12481248
}
12491249

12501250
static NameUsage getNameUsage(RenameType Type) {

tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void EditorDiagConsumer::getAllDiagnostics(
7777
void EditorDiagConsumer::handleDiagnostic(
7878
SourceManager &SM, SourceLoc Loc, DiagnosticKind Kind,
7979
StringRef FormatString, ArrayRef<DiagnosticArgument> FormatArgs,
80-
const DiagnosticInfo &Info, StringRef currentPrimaryInput) {
80+
const DiagnosticInfo &Info, const SourceLoc defaultDiagnosticLoc) {
8181

8282
if (Kind == DiagnosticKind::Error) {
8383
HadAnyError = true;

tools/SourceKit/lib/SwiftLang/SwiftEditorDiagConsumer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class EditorDiagConsumer : public swift::DiagnosticConsumer {
7070
swift::DiagnosticKind Kind, StringRef FormatString,
7171
ArrayRef<swift::DiagnosticArgument> FormatArgs,
7272
const swift::DiagnosticInfo &Info,
73-
StringRef currentPrimaryInput) override;
73+
SourceLoc defaultDiagnosticLoc) override;
7474
};
7575

7676
} // namespace SourceKit

0 commit comments

Comments
 (0)