Skip to content

Commit 8e4ea9b

Browse files
committed
[NFC] DiagnosticVerifier: Move 'getColumnNumber' into SourceManager
1 parent b6a4923 commit 8e4ea9b

File tree

4 files changed

+30
-25
lines changed

4 files changed

+30
-25
lines changed

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ class DiagnosticVerifier : public DiagnosticConsumer {
101101
Result verifyFile(unsigned BufferID);
102102

103103
bool checkForFixIt(const ExpectedFixIt &Expected,
104-
const CapturedDiagnosticInfo &D, StringRef buffer);
104+
const CapturedDiagnosticInfo &D, unsigned BufferID) const;
105105

106106
// Render the verifier syntax for a given set of fix-its.
107107
std::string renderFixits(ArrayRef<DiagnosticInfo::FixIt> fixits,
108-
StringRef InputFile);
108+
unsigned BufferID) const;
109109

110110
void printRemainingDiagnostics() const;
111111
};

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
}

lib/Frontend/DiagnosticVerifier.cpp

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -237,34 +237,19 @@ verifyUnknown(SourceManager &SM,
237237
}
238238
} // end anonymous namespace
239239

240-
static unsigned getColumnNumber(StringRef buffer, llvm::SMLoc loc) {
241-
assert(loc.getPointer() >= buffer.data());
242-
assert((size_t)(loc.getPointer() - buffer.data()) <= buffer.size());
243-
244-
StringRef UpToLoc = buffer.slice(0, loc.getPointer() - buffer.data());
245-
246-
size_t ColumnNo = UpToLoc.size();
247-
size_t NewlinePos = UpToLoc.find_last_of("\r\n");
248-
if (NewlinePos != StringRef::npos)
249-
ColumnNo -= NewlinePos;
250-
251-
return static_cast<unsigned>(ColumnNo);
252-
}
253-
254240
/// Return true if the given \p ExpectedFixIt is in the fix-its emitted by
255241
/// diagnostic \p D.
256242
bool DiagnosticVerifier::checkForFixIt(const ExpectedFixIt &Expected,
257243
const CapturedDiagnosticInfo &D,
258-
StringRef buffer) {
244+
unsigned BufferID) const {
259245
for (auto &ActualFixIt : D.FixIts) {
260246
if (ActualFixIt.getText() != Expected.Text)
261247
continue;
262248

263249
CharSourceRange Range = ActualFixIt.getRange();
264-
if (getColumnNumber(buffer, getRawLoc(Range.getStart())) !=
265-
Expected.StartCol)
250+
if (SM.getColumnInBuffer(Range.getStart(), BufferID) != Expected.StartCol)
266251
continue;
267-
if (getColumnNumber(buffer, getRawLoc(Range.getEnd())) != Expected.EndCol)
252+
if (SM.getColumnInBuffer(Range.getEnd(), BufferID) != Expected.EndCol)
268253
continue;
269254

270255
return true;
@@ -275,17 +260,17 @@ bool DiagnosticVerifier::checkForFixIt(const ExpectedFixIt &Expected,
275260

276261
std::string
277262
DiagnosticVerifier::renderFixits(ArrayRef<DiagnosticInfo::FixIt> fixits,
278-
StringRef InputFile) {
263+
unsigned BufferID) const {
279264
std::string Result;
280265
llvm::raw_string_ostream OS(Result);
281266
interleave(fixits,
282267
[&](const DiagnosticInfo::FixIt &ActualFixIt) {
283268
CharSourceRange Range = ActualFixIt.getRange();
284269

285270
OS << "{{"
286-
<< getColumnNumber(InputFile, getRawLoc(Range.getStart()))
271+
<< SM.getColumnInBuffer(Range.getStart(), BufferID)
287272
<< '-'
288-
<< getColumnNumber(InputFile, getRawLoc(Range.getEnd()))
273+
<< SM.getColumnInBuffer(Range.getEnd(), BufferID)
289274
<< '=';
290275

291276
for (auto C : ActualFixIt.getText()) {
@@ -627,7 +612,7 @@ DiagnosticVerifier::Result DiagnosticVerifier::verifyFile(unsigned BufferID) {
627612
// Verify that any expected fix-its are present in the diagnostic.
628613
for (auto fixit : expected.Fixits) {
629614
// If we found it, we're ok.
630-
if (!checkForFixIt(fixit, FoundDiagnostic, InputFile)) {
615+
if (!checkForFixIt(fixit, FoundDiagnostic, BufferID)) {
631616
missedFixitLoc = fixit.StartLoc;
632617
break;
633618
}
@@ -644,7 +629,7 @@ DiagnosticVerifier::Result DiagnosticVerifier::verifyFile(unsigned BufferID) {
644629
auto makeActualFixitsPhrase =
645630
[&](ArrayRef<DiagnosticInfo::FixIt> actualFixits)
646631
-> ActualFixitsPhrase {
647-
std::string actualFixitsStr = renderFixits(actualFixits, InputFile);
632+
std::string actualFixitsStr = renderFixits(actualFixits, BufferID);
648633

649634
return ActualFixitsPhrase{(Twine("actual fix-it") +
650635
(actualFixits.size() >= 2 ? "s" : "") +

0 commit comments

Comments
 (0)