Skip to content

Commit 524d88e

Browse files
author
David Ungar
committed
Pass SM in finishProcessing
1 parent 8baef6a commit 524d88e

9 files changed

+58
-47
lines changed

include/swift/AST/DiagnosticConsumer.h

+8-5
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class DiagnosticConsumer {
100100
const DiagnosticInfo &Info) = 0;
101101

102102
/// \returns true if an error occurred while finishing-up.
103-
virtual bool finishProcessing() { return false; }
103+
virtual bool finishProcessing(SourceManager &) { return false; }
104104
};
105105

106106
/// \brief DiagnosticConsumer that discards all diagnostics.
@@ -140,11 +140,13 @@ class FileSpecificDiagnosticConsumer : public DiagnosticConsumer {
140140
/// All consumers owned by this FileSpecificDiagnosticConsumer.
141141
const SmallVector<ConsumerPair, 4> SubConsumers;
142142

143+
// The commented-out consts are there because the data does not change
144+
// but the swap method gets called on this structure.
143145
struct ConsumerSpecificInformation {
144-
const CharSourceRange range;
146+
/*const*/ CharSourceRange range;
145147
/// The DiagnosticConsumer may be empty if those diagnostics are not to be
146148
/// emitted.
147-
DiagnosticConsumer *const consumer;
149+
DiagnosticConsumer * /*const*/ consumer;
148150
bool hasAnErrorBeenEmitted = false;
149151

150152
ConsumerSpecificInformation(const CharSourceRange range,
@@ -188,10 +190,11 @@ class FileSpecificDiagnosticConsumer : public DiagnosticConsumer {
188190
ArrayRef<DiagnosticArgument> FormatArgs,
189191
const DiagnosticInfo &Info) override;
190192

191-
bool finishProcessing() override;
193+
bool finishProcessing(SourceManager &) override;
192194

193195
private:
194-
void addNonSpecificErrors();
196+
void addNonSpecificErrors(SourceManager &SM);
197+
195198
void computeConsumersOrderedByRange(SourceManager &SM);
196199

197200
/// Returns nullptr if diagnostic is to be suppressed,

include/swift/AST/DiagnosticEngine.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,8 @@ namespace swift {
759759

760760
/// \returns true if any diagnostic consumer gave an error while invoking
761761
//// \c finishProcessing.
762-
bool finishProcessing();
763-
762+
bool finishProcessing(SourceManager &);
763+
764764
/// \brief Format the given diagnostic text and place the result in the given
765765
/// buffer.
766766
static void formatDiagnosticText(
@@ -781,6 +781,9 @@ namespace swift {
781781
/// \brief Send all tentative diagnostics to all diagnostic consumers and
782782
/// delete them.
783783
void emitTentativeDiagnostics();
784+
785+
public:
786+
static const char *diagnosticStringFor(const DiagID id);
784787
};
785788

786789
/// \brief Represents a diagnostic transaction. While a transaction is

include/swift/AST/DiagnosticsFrontend.def

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ WARNING(cannot_assign_value_to_conditional_compilation_flag,none,
235235
ERROR(error_optimization_remark_pattern, none, "%0 in '%1'",
236236
(StringRef, StringRef))
237237

238-
ERROR(error_some_error_occured_in_a_file_that_was_used_in_this_one,none, "some error occured in a file that was used by this one", ())
238+
ERROR(error_some_error_occured_in_a_file_that_was_used_by_this_one,none, "some error occured in a file that was used by this one", ())
239239

240240
#ifndef DIAG_NO_UNDEF
241241
# if defined(DIAG)

lib/AST/DiagnosticConsumer.cpp

+17-14
Original file line numberDiff line numberDiff line change
@@ -189,38 +189,41 @@ void FileSpecificDiagnosticConsumer::handleDiagnostic(
189189
true; // Suppress non-primary diagnostic in batch mode.
190190
}
191191

192-
bool FileSpecificDiagnosticConsumer::finishProcessing() {
192+
bool FileSpecificDiagnosticConsumer::finishProcessing(SourceManager &SM) {
193+
addNonSpecificErrors(SM);
194+
193195
// Deliberately don't use std::any_of here because we don't want early-exit
194196
// behavior.
195197

196-
addNonSpecificErrors();
197-
198198
bool hadError = false;
199199
for (auto &subConsumer : SubConsumers)
200-
hadError |= subConsumer.second && subConsumer.second->finishProcessing();
200+
hadError |= subConsumer.second && subConsumer.second->finishProcessing(SM);
201201
return hadError;
202202
}
203203

204-
void FileSpecificDiagnosticConsumer::addNonSpecificErrors() {
205-
if (!WasAnErrorSuppressed)
206-
return;
207-
208-
SourceManager s;
209-
204+
static void produceNonSpecificError(DiagnosticConsumer *consumer,
205+
SourceManager &SM) {
210206
Diagnostic diagnostic(
211-
diag::error_some_error_occured_in_a_file_that_was_used_in_this_one);
207+
diag::error_some_error_occured_in_a_file_that_was_used_by_this_one);
212208

213209
// Stolen from DiagnosticEngine::emitDiagnostic
214210
DiagnosticInfo Info;
215211
Info.ID = diagnostic.getID();
216212
Info.Ranges = diagnostic.getRanges();
217213
Info.FixIts = diagnostic.getFixIts();
218214

215+
consumer->handleDiagnostic(
216+
SM, SourceLoc(), DiagnosticKind::Error,
217+
DiagnosticEngine::diagnosticStringFor(diagnostic.getID()), {}, Info);
218+
}
219+
220+
void FileSpecificDiagnosticConsumer::addNonSpecificErrors(SourceManager &SM) {
221+
if (!WasAnErrorSuppressed)
222+
return;
223+
219224
for (auto &info : ConsumersOrderedByRange) {
220225
if (!info.hasAnErrorBeenEmitted && info.consumer) {
221-
info.consumer->handleDiagnostic(s, SourceLoc(), DiagnosticKind::Error,
222-
diagnosticStrings[(unsigned)Info.ID],
223-
diagnostic.getArgs(), Info);
226+
produceNonSpecificError(info.consumer, SM);
224227
info.hasAnErrorBeenEmitted = true;
225228
}
226229
}

lib/AST/DiagnosticEngine.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,10 @@ bool DiagnosticEngine::isDiagnosticPointsToFirstBadToken(DiagID ID) const {
251251
return storedDiagnosticInfos[(unsigned) ID].pointsToFirstBadToken;
252252
}
253253

254-
bool DiagnosticEngine::finishProcessing() {
254+
bool DiagnosticEngine::finishProcessing(SourceManager &SM) {
255255
bool hadError = false;
256256
for (auto &Consumer : Consumers) {
257-
hadError |= Consumer->finishProcessing();
257+
hadError |= Consumer->finishProcessing(SM);
258258
}
259259
return hadError;
260260
}
@@ -822,9 +822,11 @@ void DiagnosticEngine::emitDiagnostic(const Diagnostic &diagnostic) {
822822
Info.FixIts = diagnostic.getFixIts();
823823
for (auto &Consumer : Consumers) {
824824
Consumer->handleDiagnostic(SourceMgr, loc, toDiagnosticKind(behavior),
825-
diagnosticStrings[(unsigned)Info.ID],
826-
diagnostic.getArgs(),
827-
Info);
825+
diagnosticStringFor(Info.ID),
826+
diagnostic.getArgs(), Info);
828827
}
829828
}
830829

830+
const char *DiagnosticEngine::diagnosticStringFor(const DiagID id) {
831+
return diagnosticStrings[(unsigned)id];
832+
}

lib/Frontend/SerializedDiagnosticConsumer.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class SerializedDiagnosticConsumer : public DiagnosticConsumer {
140140
assert(CalledFinishProcessing && "did not call finishProcessing()");
141141
}
142142

143-
bool finishProcessing() override {
143+
bool finishProcessing(SourceManager &SM) override {
144144
assert(!CalledFinishProcessing &&
145145
"called finishProcessing() multiple times");
146146
CalledFinishProcessing = true;
@@ -160,8 +160,7 @@ class SerializedDiagnosticConsumer : public DiagnosticConsumer {
160160
llvm::sys::fs::F_None));
161161
if (EC) {
162162
// Create a temporary diagnostics engine to print the error to stderr.
163-
SourceManager dummyMgr;
164-
DiagnosticEngine DE(dummyMgr);
163+
DiagnosticEngine DE(SM);
165164
PrintingDiagnosticConsumer PDC;
166165
DE.addConsumer(PDC);
167166
DE.diagnose(SourceLoc(), diag::cannot_open_serialized_file,

lib/FrontendTool/FrontendTool.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ class JSONFixitWriter
404404
}
405405
}
406406

407-
bool finishProcessing() override {
407+
bool finishProcessing(SourceManager &) override {
408408
std::error_code EC;
409409
std::unique_ptr<llvm::raw_fd_ostream> OS;
410410
OS.reset(new llvm::raw_fd_ostream(FixitsOutputPath,
@@ -1651,7 +1651,7 @@ int swift::performFrontend(ArrayRef<const char *> Args,
16511651

16521652
auto finishDiagProcessing = [&](int retValue) -> int {
16531653
FinishDiagProcessingCheckRAII.CalledFinishDiagProcessing = true;
1654-
bool err = Instance->getDiags().finishProcessing();
1654+
bool err = Instance->getDiags().finishProcessing(Instance->getSourceMgr());
16551655
return retValue ? retValue : err;
16561656
};
16571657

test/Misc/serialized-diagnostics-batch-mode-compilation-failed.swift test/Misc/serialized-diagnostics-batch-mode-nonspecific-error.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// To avoid redundant diagnostics showing up in Xcode, batch-mode must suppress diagnostics in
22
// non-primary files.
3-
// But if the only fatal errors are suppressed ones, a "compilation failed" error must be emitted
3+
// But if the only errors are suppressed ones, a nonspecific error must be emitted
44
// for the primary files so Xcode knows something happened.
55
//
66
// RUN: rm -f %t.*
@@ -16,9 +16,10 @@
1616

1717
// Ensure the error is in the serialized diagnostics:
1818

19-
// RUN: %FileCheck -check-prefix=COMPILATION-FAILED %s <%t.main.txt
20-
// RUN: %FileCheck -check-prefix=COMPILATION-FAILED %s <%t.empty.txt
21-
// COMPILATION-FAILED: compilation failed
19+
// RUN: %FileCheck -check-prefix=NONSPECIFIC-ERROR %s <%t.main.txt
20+
// RUN: %FileCheck -check-prefix=NONSPECIFIC-ERROR %s <%t.empty.txt
21+
// NONSPECIFIC-ERROR: error: some error occured in a file that was used by this one
2222

2323
func test(x: SomeType) {
24+
nonexistent()
2425
}

unittests/AST/DiagnosticConsumerTests.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace {
4343
expected.erase(expected.begin());
4444
}
4545

46-
bool finishProcessing() override {
46+
bool finishProcessing(SourceManager &) override {
4747
EXPECT_FALSE(hasFinished);
4848
if (previous)
4949
EXPECT_TRUE(previous->hasFinished);
@@ -68,7 +68,7 @@ TEST(FileSpecificDiagnosticConsumer, SubConsumersFinishInOrder) {
6868
consumers.emplace_back("", std::move(consumerUnaffiliated));
6969

7070
FileSpecificDiagnosticConsumer topConsumer(consumers);
71-
topConsumer.finishProcessing();
71+
topConsumer.finishProcessing(sourceMgr);
7272
}
7373

7474
TEST(FileSpecificDiagnosticConsumer, InvalidLocDiagsGoToEveryConsumer) {
@@ -89,7 +89,7 @@ TEST(FileSpecificDiagnosticConsumer, InvalidLocDiagsGoToEveryConsumer) {
8989
FileSpecificDiagnosticConsumer topConsumer(consumers);
9090
topConsumer.handleDiagnostic(sourceMgr, SourceLoc(), DiagnosticKind::Error,
9191
"dummy", {}, DiagnosticInfo());
92-
topConsumer.finishProcessing();
92+
topConsumer.finishProcessing(sourceMgr);
9393
}
9494

9595
TEST(FileSpecificDiagnosticConsumer, ErrorsWithLocationsGoToExpectedConsumers) {
@@ -139,7 +139,7 @@ TEST(FileSpecificDiagnosticConsumer, ErrorsWithLocationsGoToExpectedConsumers) {
139139
"back", {}, DiagnosticInfo());
140140
topConsumer.handleDiagnostic(sourceMgr, backOfB, DiagnosticKind::Error,
141141
"back", {}, DiagnosticInfo());
142-
topConsumer.finishProcessing();
142+
topConsumer.finishProcessing(sourceMgr);
143143
}
144144

145145
TEST(FileSpecificDiagnosticConsumer,
@@ -193,7 +193,7 @@ TEST(FileSpecificDiagnosticConsumer,
193193
"back", {}, DiagnosticInfo());
194194
topConsumer.handleDiagnostic(sourceMgr, backOfB, DiagnosticKind::Error,
195195
"back", {}, DiagnosticInfo());
196-
topConsumer.finishProcessing();
196+
topConsumer.finishProcessing(sourceMgr);
197197
}
198198

199199
TEST(FileSpecificDiagnosticConsumer, WarningsAndRemarksAreTreatedLikeErrors) {
@@ -234,7 +234,7 @@ TEST(FileSpecificDiagnosticConsumer, WarningsAndRemarksAreTreatedLikeErrors) {
234234
"remark", {}, DiagnosticInfo());
235235
topConsumer.handleDiagnostic(sourceMgr, frontOfB, DiagnosticKind::Remark,
236236
"remark", {}, DiagnosticInfo());
237-
topConsumer.finishProcessing();
237+
topConsumer.finishProcessing(sourceMgr);
238238
}
239239

240240
TEST(FileSpecificDiagnosticConsumer, NotesAreAttachedToErrors) {
@@ -296,7 +296,7 @@ TEST(FileSpecificDiagnosticConsumer, NotesAreAttachedToErrors) {
296296
"note", {}, DiagnosticInfo());
297297
topConsumer.handleDiagnostic(sourceMgr, backOfA, DiagnosticKind::Note,
298298
"note", {}, DiagnosticInfo());
299-
topConsumer.finishProcessing();
299+
topConsumer.finishProcessing(sourceMgr);
300300
}
301301

302302
TEST(FileSpecificDiagnosticConsumer, NotesAreAttachedToWarningsAndRemarks) {
@@ -358,7 +358,7 @@ TEST(FileSpecificDiagnosticConsumer, NotesAreAttachedToWarningsAndRemarks) {
358358
"note", {}, DiagnosticInfo());
359359
topConsumer.handleDiagnostic(sourceMgr, backOfA, DiagnosticKind::Note,
360360
"note", {}, DiagnosticInfo());
361-
topConsumer.finishProcessing();
361+
topConsumer.finishProcessing(sourceMgr);
362362
}
363363

364364
TEST(FileSpecificDiagnosticConsumer, NotesAreAttachedToErrorsEvenAcrossFiles) {
@@ -417,7 +417,7 @@ TEST(FileSpecificDiagnosticConsumer, NotesAreAttachedToErrorsEvenAcrossFiles) {
417417
"note", {}, DiagnosticInfo());
418418
topConsumer.handleDiagnostic(sourceMgr, backOfA, DiagnosticKind::Note,
419419
"note", {}, DiagnosticInfo());
420-
topConsumer.finishProcessing();
420+
topConsumer.finishProcessing(sourceMgr);
421421
}
422422

423423
TEST(FileSpecificDiagnosticConsumer,
@@ -480,7 +480,7 @@ TEST(FileSpecificDiagnosticConsumer,
480480
"note", {}, DiagnosticInfo());
481481
topConsumer.handleDiagnostic(sourceMgr, backOfA, DiagnosticKind::Note,
482482
"note", {}, DiagnosticInfo());
483-
topConsumer.finishProcessing();
483+
topConsumer.finishProcessing(sourceMgr);
484484
}
485485

486486

@@ -529,5 +529,5 @@ TEST(FileSpecificDiagnosticConsumer,
529529
"error", {}, DiagnosticInfo());
530530
topConsumer.handleDiagnostic(sourceMgr, SourceLoc(), DiagnosticKind::Note,
531531
"note", {}, DiagnosticInfo());
532-
topConsumer.finishProcessing();
532+
topConsumer.finishProcessing(sourceMgr);
533533
}

0 commit comments

Comments
 (0)