Skip to content

Commit 54c1bca

Browse files
committed
clang/Basic: Stop using SourceManager::getBuffer, NFC
Update clang/lib/Basic to stop relying on a `MemoryBuffer*`, using the `MemoryBufferRef` from `getBufferOrNone` or `getBufferOrFake` instead of `getBuffer`. Differential Revision: https://reviews.llvm.org/D89394
1 parent 32a4ad3 commit 54c1bca

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

clang/include/clang/Basic/SourceManager.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,17 @@ class SourceManager : public RefCountedBase<SourceManager> {
980980
Diag, getFileManager(), Loc);
981981
}
982982

983+
/// Return the buffer for the specified FileID.
984+
///
985+
/// If there is an error opening this buffer the first time, this
986+
/// manufactures a temporary buffer and returns it.
987+
llvm::MemoryBufferRef
988+
getBufferOrFake(FileID FID, SourceLocation Loc = SourceLocation()) const {
989+
if (auto B = getBufferOrNone(FID, Loc))
990+
return *B;
991+
return getFakeBufferForRecovery()->getMemBufferRef();
992+
}
993+
983994
/// Return the buffer for the specified FileID.
984995
///
985996
/// If there is an error opening this buffer the first time, this

clang/lib/Basic/Diagnostic.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ void DiagnosticsEngine::DiagStateMap::dump(SourceManager &SrcMgr,
265265
PrintedOuterHeading = true;
266266

267267
llvm::errs() << "File " << &File << " <FileID " << ID.getHashValue()
268-
<< ">: " << SrcMgr.getBuffer(ID)->getBufferIdentifier();
268+
<< ">: " << SrcMgr.getBufferOrFake(ID).getBufferIdentifier();
269+
269270
if (F.second.Parent) {
270271
std::pair<FileID, unsigned> Decomp =
271272
SrcMgr.getDecomposedIncludedLoc(ID);

clang/lib/Basic/SourceLocation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
245245

246246
StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
247247
assert(isValid());
248-
return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid)->getBuffer();
248+
return SrcMgr->getBufferData(SrcMgr->getFileID(*this), Invalid);
249249
}
250250

251251
std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {

clang/lib/Basic/SourceManager.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,12 +1228,11 @@ const char *SourceManager::getCharacterData(SourceLocation SL,
12281228
/// this is significantly cheaper to compute than the line number.
12291229
unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos,
12301230
bool *Invalid) const {
1231-
bool MyInvalid = false;
1232-
const llvm::MemoryBuffer *MemBuf = getBuffer(FID, &MyInvalid);
1231+
llvm::Optional<llvm::MemoryBufferRef> MemBuf = getBufferOrNone(FID);
12331232
if (Invalid)
1234-
*Invalid = MyInvalid;
1233+
*Invalid = !MemBuf;
12351234

1236-
if (MyInvalid)
1235+
if (!MemBuf)
12371236
return 1;
12381237

12391238
// It is okay to request a position just past the end of the buffer.
@@ -1509,7 +1508,10 @@ StringRef SourceManager::getBufferName(SourceLocation Loc,
15091508
bool *Invalid) const {
15101509
if (isInvalid(Loc, Invalid)) return "<invalid loc>";
15111510

1512-
return getBuffer(getFileID(Loc), Invalid)->getBufferIdentifier();
1511+
auto B = getBufferOrNone(getFileID(Loc));
1512+
if (Invalid)
1513+
*Invalid = !B;
1514+
return B ? B->getBufferIdentifier() : "<invalid buffer>";
15131515
}
15141516

15151517
/// getPresumedLoc - This method returns the "presumed" location of a
@@ -2047,8 +2049,8 @@ bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS,
20472049
// If we arrived here, the location is either in a built-ins buffer or
20482050
// associated with global inline asm. PR5662 and PR22576 are examples.
20492051

2050-
StringRef LB = getBuffer(LOffs.first)->getBufferIdentifier();
2051-
StringRef RB = getBuffer(ROffs.first)->getBufferIdentifier();
2052+
StringRef LB = getBufferOrFake(LOffs.first).getBufferIdentifier();
2053+
StringRef RB = getBufferOrFake(ROffs.first).getBufferIdentifier();
20522054
bool LIsBuiltins = LB == "<built-in>";
20532055
bool RIsBuiltins = RB == "<built-in>";
20542056
// Sort built-in before non-built-in.

0 commit comments

Comments
 (0)