Skip to content

Commit 2d75d7f

Browse files
Merge pull request #39572 from AnthonyLatsis/fix-it-line
Verifier: Support line numbers in fix-it verification
2 parents b0fd4df + 4ebd9c0 commit 2d75d7f

File tree

7 files changed

+244
-78
lines changed

7 files changed

+244
-78
lines changed

include/swift/AST/DiagnosticConsumer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ struct DiagnosticInfo {
6969
public:
7070
FixIt(CharSourceRange R, StringRef Str, ArrayRef<DiagnosticArgument> Args);
7171

72-
CharSourceRange getRange() const { return Range; }
72+
CharSourceRange &getRange() { return Range; }
73+
const CharSourceRange &getRange() const { return Range; }
74+
7375
StringRef getText() const { return Text; }
7476
};
7577

include/swift/Basic/SourceManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ class SourceManager {
247247
return LLVMSourceMgr.getLineAndColumn(Loc.Value, BufferID);
248248
}
249249

250+
/// Returns the column for the given source location in the given buffer.
251+
unsigned getColumnInBuffer(SourceLoc Loc, unsigned BufferID) const;
252+
250253
StringRef getEntireTextForBuffer(unsigned BufferID) const;
251254

252255
StringRef extractText(CharSourceRange Range,

include/swift/Frontend/DiagnosticVerifier.h

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,53 @@ bool verifyDependencies(SourceManager &SM, ArrayRef<SourceFile *> SFs);
3636
// MARK: - DiagnosticVerifier
3737
struct ExpectedFixIt;
3838

39+
/// A range expressed in terms of line-and-column pairs.
40+
struct LineColumnRange {
41+
static constexpr unsigned NoValue = ~0u;
42+
43+
unsigned StartLine, StartCol;
44+
unsigned EndLine, EndCol;
45+
46+
LineColumnRange()
47+
: StartLine(NoValue), StartCol(NoValue), EndLine(NoValue),
48+
EndCol(NoValue) {}
49+
};
50+
51+
class CapturedFixItInfo final {
52+
DiagnosticInfo::FixIt FixIt;
53+
mutable LineColumnRange LineColRange;
54+
55+
public:
56+
CapturedFixItInfo(DiagnosticInfo::FixIt FixIt) : FixIt(FixIt) {}
57+
58+
CharSourceRange &getSourceRange() { return FixIt.getRange(); }
59+
const CharSourceRange &getSourceRange() const { return FixIt.getRange(); }
60+
61+
StringRef getText() const { return FixIt.getText(); }
62+
63+
/// Obtain the line-column range corresponding to the fix-it's
64+
/// replacement range.
65+
const LineColumnRange &getLineColumnRange(const SourceManager &SM,
66+
unsigned BufferID,
67+
bool ComputeStartLocLine,
68+
bool ComputeEndLocLine) const;
69+
};
70+
3971
struct CapturedDiagnosticInfo {
4072
llvm::SmallString<128> Message;
4173
llvm::SmallString<32> FileName;
4274
DiagnosticKind Classification;
4375
SourceLoc Loc;
4476
unsigned Line;
4577
unsigned Column;
46-
SmallVector<DiagnosticInfo::FixIt, 2> FixIts;
78+
SmallVector<CapturedFixItInfo, 2> FixIts;
4779
SmallVector<std::string, 1> EducationalNotes;
4880

4981
CapturedDiagnosticInfo(llvm::SmallString<128> Message,
5082
llvm::SmallString<32> FileName,
5183
DiagnosticKind Classification, SourceLoc Loc,
5284
unsigned Line, unsigned Column,
53-
SmallVector<DiagnosticInfo::FixIt, 2> FixIts,
85+
SmallVector<CapturedFixItInfo, 2> FixIts,
5486
SmallVector<std::string, 1> EducationalNotes)
5587
: Message(Message), FileName(FileName), Classification(Classification),
5688
Loc(Loc), Line(Line), Column(Column), FixIts(FixIts),
@@ -101,11 +133,11 @@ class DiagnosticVerifier : public DiagnosticConsumer {
101133
Result verifyFile(unsigned BufferID);
102134

103135
bool checkForFixIt(const ExpectedFixIt &Expected,
104-
const CapturedDiagnosticInfo &D, StringRef buffer);
136+
const CapturedDiagnosticInfo &D, unsigned BufferID) const;
105137

106138
// Render the verifier syntax for a given set of fix-its.
107-
std::string renderFixits(ArrayRef<DiagnosticInfo::FixIt> fixits,
108-
StringRef InputFile);
139+
std::string renderFixits(ArrayRef<CapturedFixItInfo> ActualFixIts,
140+
unsigned BufferID, unsigned DiagnosticLineNo) const;
109141

110142
void printRemainingDiagnostics() const;
111143
};

lib/Basic/SourceLoc.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,23 @@ unsigned SourceManager::getByteDistance(SourceLoc Start, SourceLoc End) const {
220220
return End.Value.getPointer() - Start.Value.getPointer();
221221
}
222222

223+
unsigned SourceManager::getColumnInBuffer(SourceLoc Loc,
224+
unsigned BufferID) const {
225+
assert(Loc.isValid());
226+
227+
const StringRef Buffer = getEntireTextForBuffer(BufferID);
228+
const char *Ptr = static_cast<const char *>(Loc.getOpaquePointerValue());
229+
230+
StringRef UpToLoc = Buffer.slice(0, Ptr - Buffer.data());
231+
232+
size_t ColumnNo = UpToLoc.size();
233+
size_t NewlinePos = UpToLoc.find_last_of("\r\n");
234+
if (NewlinePos != StringRef::npos)
235+
ColumnNo -= NewlinePos;
236+
237+
return static_cast<unsigned>(ColumnNo);
238+
}
239+
223240
StringRef SourceManager::getEntireTextForBuffer(unsigned BufferID) const {
224241
return LLVMSourceMgr.getMemoryBuffer(BufferID)->getBuffer();
225242
}

0 commit comments

Comments
 (0)