Skip to content

Commit d5087ee

Browse files
committed
[NFC] DiagnosticVerifier: Cache computed column numbers for actual fix-its
1 parent 011ce73 commit d5087ee

File tree

2 files changed

+69
-37
lines changed

2 files changed

+69
-37
lines changed

include/swift/Frontend/DiagnosticVerifier.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,39 @@ struct LineColumnRange {
4545
LineColumnRange() : StartCol(NoValue), EndCol(NoValue) {}
4646
};
4747

48+
class CapturedFixItInfo final {
49+
DiagnosticInfo::FixIt FixIt;
50+
mutable LineColumnRange LineColRange;
51+
52+
public:
53+
CapturedFixItInfo(DiagnosticInfo::FixIt FixIt) : FixIt(FixIt) {}
54+
55+
CharSourceRange &getSourceRange() { return FixIt.getRange(); }
56+
const CharSourceRange &getSourceRange() const { return FixIt.getRange(); }
57+
58+
StringRef getText() const { return FixIt.getText(); }
59+
60+
/// Obtain the line-column range corresponding to the fix-it's
61+
/// replacement range.
62+
const LineColumnRange &getLineColumnRange(const SourceManager &SM,
63+
unsigned BufferID) const;
64+
};
65+
4866
struct CapturedDiagnosticInfo {
4967
llvm::SmallString<128> Message;
5068
llvm::SmallString<32> FileName;
5169
DiagnosticKind Classification;
5270
SourceLoc Loc;
5371
unsigned Line;
5472
unsigned Column;
55-
SmallVector<DiagnosticInfo::FixIt, 2> FixIts;
73+
SmallVector<CapturedFixItInfo, 2> FixIts;
5674
SmallVector<std::string, 1> EducationalNotes;
5775

5876
CapturedDiagnosticInfo(llvm::SmallString<128> Message,
5977
llvm::SmallString<32> FileName,
6078
DiagnosticKind Classification, SourceLoc Loc,
6179
unsigned Line, unsigned Column,
62-
SmallVector<DiagnosticInfo::FixIt, 2> FixIts,
80+
SmallVector<CapturedFixItInfo, 2> FixIts,
6381
SmallVector<std::string, 1> EducationalNotes)
6482
: Message(Message), FileName(FileName), Classification(Classification),
6583
Loc(Loc), Line(Line), Column(Column), FixIts(FixIts),
@@ -113,7 +131,7 @@ class DiagnosticVerifier : public DiagnosticConsumer {
113131
const CapturedDiagnosticInfo &D, unsigned BufferID) const;
114132

115133
// Render the verifier syntax for a given set of fix-its.
116-
std::string renderFixits(ArrayRef<DiagnosticInfo::FixIt> fixits,
134+
std::string renderFixits(ArrayRef<CapturedFixItInfo> ActualFixIts,
117135
unsigned BufferID) const;
118136

119137
void printRemainingDiagnostics() const;

lib/Frontend/DiagnosticVerifier.cpp

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ struct ExpectedFixIt {
3535
};
3636
} // end namespace swift
3737

38+
const LineColumnRange &
39+
CapturedFixItInfo::getLineColumnRange(const SourceManager &SM,
40+
unsigned BufferID) const {
41+
if (LineColRange.StartCol == LineColumnRange::NoValue) {
42+
LineColRange.StartCol =
43+
SM.getColumnInBuffer(getSourceRange().getStart(), BufferID);
44+
}
45+
46+
if (LineColRange.EndCol == LineColumnRange::NoValue) {
47+
LineColRange.EndCol =
48+
SM.getColumnInBuffer(getSourceRange().getEnd(), BufferID);
49+
}
50+
51+
return LineColRange;
52+
}
53+
3854
namespace {
3955

4056
static constexpr StringLiteral fixitExpectationNoneString("none");
@@ -246,11 +262,10 @@ bool DiagnosticVerifier::checkForFixIt(const ExpectedFixIt &Expected,
246262
if (ActualFixIt.getText() != Expected.Text)
247263
continue;
248264

249-
CharSourceRange Range = ActualFixIt.getRange();
250-
if (SM.getColumnInBuffer(Range.getStart(), BufferID) !=
251-
Expected.Range.StartCol)
265+
LineColumnRange ActualRange = ActualFixIt.getLineColumnRange(SM, BufferID);
266+
if (ActualRange.StartCol != Expected.Range.StartCol)
252267
continue;
253-
if (SM.getColumnInBuffer(Range.getEnd(), BufferID) != Expected.Range.EndCol)
268+
if (ActualRange.EndCol != Expected.Range.EndCol)
254269
continue;
255270

256271
return true;
@@ -260,31 +275,29 @@ bool DiagnosticVerifier::checkForFixIt(const ExpectedFixIt &Expected,
260275
}
261276

262277
std::string
263-
DiagnosticVerifier::renderFixits(ArrayRef<DiagnosticInfo::FixIt> fixits,
278+
DiagnosticVerifier::renderFixits(ArrayRef<CapturedFixItInfo> ActualFixIts,
264279
unsigned BufferID) const {
265280
std::string Result;
266281
llvm::raw_string_ostream OS(Result);
267-
interleave(fixits,
268-
[&](const DiagnosticInfo::FixIt &ActualFixIt) {
269-
CharSourceRange Range = ActualFixIt.getRange();
270-
271-
OS << "{{"
272-
<< SM.getColumnInBuffer(Range.getStart(), BufferID)
273-
<< '-'
274-
<< SM.getColumnInBuffer(Range.getEnd(), BufferID)
275-
<< '=';
276-
277-
for (auto C : ActualFixIt.getText()) {
278-
if (C == '\n')
279-
OS << "\\n";
280-
else if (C == '}' || C == '\\')
281-
OS << '\\' << C;
282-
else
283-
OS << C;
284-
}
285-
OS << "}}";
286-
},
287-
[&] { OS << ' '; });
282+
interleave(
283+
ActualFixIts,
284+
[&](const CapturedFixItInfo &ActualFixIt) {
285+
LineColumnRange ActualRange =
286+
ActualFixIt.getLineColumnRange(SM, BufferID);
287+
288+
OS << "{{" << ActualRange.StartCol << '-' << ActualRange.EndCol << '=';
289+
290+
for (auto C : ActualFixIt.getText()) {
291+
if (C == '\n')
292+
OS << "\\n";
293+
else if (C == '}' || C == '\\')
294+
OS << '\\' << C;
295+
else
296+
OS << C;
297+
}
298+
OS << "}}";
299+
},
300+
[&] { OS << ' '; });
288301
return OS.str();
289302
}
290303

@@ -628,8 +641,7 @@ DiagnosticVerifier::Result DiagnosticVerifier::verifyFile(unsigned BufferID) {
628641
};
629642

630643
auto makeActualFixitsPhrase =
631-
[&](ArrayRef<DiagnosticInfo::FixIt> actualFixits)
632-
-> ActualFixitsPhrase {
644+
[&](ArrayRef<CapturedFixItInfo> actualFixits) -> ActualFixitsPhrase {
633645
std::string actualFixitsStr = renderFixits(actualFixits, BufferID);
634646

635647
return ActualFixitsPhrase{(Twine("actual fix-it") +
@@ -933,8 +945,10 @@ void DiagnosticVerifier::printRemainingDiagnostics() const {
933945
/// file.
934946
void DiagnosticVerifier::handleDiagnostic(SourceManager &SM,
935947
const DiagnosticInfo &Info) {
936-
SmallVector<DiagnosticInfo::FixIt, 2> fixIts;
937-
std::copy(Info.FixIts.begin(), Info.FixIts.end(), std::back_inserter(fixIts));
948+
SmallVector<CapturedFixItInfo, 2> fixIts;
949+
for (const auto &fixIt : Info.FixIts) {
950+
fixIts.emplace_back(fixIt);
951+
}
938952

939953
llvm::SmallVector<std::string, 1> eduNotes;
940954
for (auto &notePath : Info.EducationalNotePaths) {
@@ -968,11 +982,11 @@ void DiagnosticVerifier::handleDiagnostic(SourceManager &SM,
968982

969983
capturedDiag.Loc = correctSM.getLocForForeignLoc(capturedDiag.Loc, SM);
970984
for (auto &fixIt : capturedDiag.FixIts) {
971-
auto newStart = correctSM.getLocForForeignLoc(fixIt.getRange().getStart(),
972-
SM);
973-
auto &mutableRange = fixIt.getRange();
985+
auto newStart =
986+
correctSM.getLocForForeignLoc(fixIt.getSourceRange().getStart(), SM);
987+
auto &mutableRange = fixIt.getSourceRange();
974988
mutableRange =
975-
CharSourceRange(newStart, fixIt.getRange().getByteLength());
989+
CharSourceRange(newStart, fixIt.getSourceRange().getByteLength());
976990
}
977991
}
978992
}

0 commit comments

Comments
 (0)