From 54c1bcab90102481fe43b73f8547d47446ba2163 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Duncan=20P=2E=20N=2E=20Exon=C2=A0Smith?= Date: Wed, 14 Oct 2020 10:32:00 -0400 Subject: [PATCH] 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 --- clang/include/clang/Basic/SourceManager.h | 11 +++++++++++ clang/lib/Basic/Diagnostic.cpp | 3 ++- clang/lib/Basic/SourceLocation.cpp | 2 +- clang/lib/Basic/SourceManager.cpp | 16 +++++++++------- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h index e34fe45..2156a01 100644 --- a/clang/include/clang/Basic/SourceManager.h +++ b/clang/include/clang/Basic/SourceManager.h @@ -983,6 +983,17 @@ public: /// Return the buffer for the specified FileID. /// /// If there is an error opening this buffer the first time, this + /// manufactures a temporary buffer and returns it. + llvm::MemoryBufferRef + getBufferOrFake(FileID FID, SourceLocation Loc = SourceLocation()) const { + if (auto B = getBufferOrNone(FID, Loc)) + return *B; + return getFakeBufferForRecovery()->getMemBufferRef(); + } + + /// Return the buffer for the specified FileID. + /// + /// If there is an error opening this buffer the first time, this /// manufactures a temporary buffer and returns a non-empty error string. /// /// TODO: Update users of Invalid to call getBufferOrNone and change return diff --git a/clang/lib/Basic/Diagnostic.cpp b/clang/lib/Basic/Diagnostic.cpp index 661eabf..ba8c2fb 100644 --- a/clang/lib/Basic/Diagnostic.cpp +++ b/clang/lib/Basic/Diagnostic.cpp @@ -265,7 +265,8 @@ void DiagnosticsEngine::DiagStateMap::dump(SourceManager &SrcMgr, PrintedOuterHeading = true; llvm::errs() << "File " << &File << " : " << SrcMgr.getBuffer(ID)->getBufferIdentifier(); + << ">: " << SrcMgr.getBufferOrFake(ID).getBufferIdentifier(); + if (F.second.Parent) { std::pair Decomp = SrcMgr.getDecomposedIncludedLoc(ID); diff --git a/clang/lib/Basic/SourceLocation.cpp b/clang/lib/Basic/SourceLocation.cpp index c1fa406..8cb0899 100644 --- a/clang/lib/Basic/SourceLocation.cpp +++ b/clang/lib/Basic/SourceLocation.cpp @@ -245,7 +245,7 @@ const char *FullSourceLoc::getCharacterData(bool *Invalid) const { StringRef FullSourceLoc::getBufferData(bool *Invalid) const { assert(isValid()); - return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid)->getBuffer(); + return SrcMgr->getBufferData(SrcMgr->getFileID(*this), Invalid); } std::pair FullSourceLoc::getDecomposedLoc() const { diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index 1e191519..61e186e 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -1228,12 +1228,11 @@ const char *SourceManager::getCharacterData(SourceLocation SL, /// this is significantly cheaper to compute than the line number. unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos, bool *Invalid) const { - bool MyInvalid = false; - const llvm::MemoryBuffer *MemBuf = getBuffer(FID, &MyInvalid); + llvm::Optional MemBuf = getBufferOrNone(FID); if (Invalid) - *Invalid = MyInvalid; + *Invalid = !MemBuf; - if (MyInvalid) + if (!MemBuf) return 1; // It is okay to request a position just past the end of the buffer. @@ -1509,7 +1508,10 @@ StringRef SourceManager::getBufferName(SourceLocation Loc, bool *Invalid) const { if (isInvalid(Loc, Invalid)) return ""; - return getBuffer(getFileID(Loc), Invalid)->getBufferIdentifier(); + auto B = getBufferOrNone(getFileID(Loc)); + if (Invalid) + *Invalid = !B; + return B ? B->getBufferIdentifier() : ""; } /// getPresumedLoc - This method returns the "presumed" location of a @@ -2047,8 +2049,8 @@ bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS, // If we arrived here, the location is either in a built-ins buffer or // associated with global inline asm. PR5662 and PR22576 are examples. - StringRef LB = getBuffer(LOffs.first)->getBufferIdentifier(); - StringRef RB = getBuffer(ROffs.first)->getBufferIdentifier(); + StringRef LB = getBufferOrFake(LOffs.first).getBufferIdentifier(); + StringRef RB = getBufferOrFake(ROffs.first).getBufferIdentifier(); bool LIsBuiltins = LB == ""; bool RIsBuiltins = RB == ""; // Sort built-in before non-built-in. -- 2.7.4