[clang-tools-extra] 156e8b3 - clang/Basic: Remove ContentCache::getRawBuffer, NFC
Duncan P. N. Exon Smith via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 22 11:01:11 PDT 2020
Author: Duncan P. N. Exon Smith
Date: 2020-10-22T14:00:44-04:00
New Revision: 156e8b37024abd8630666e0ae8a251b80299ed1d
URL: https://github.com/llvm/llvm-project/commit/156e8b37024abd8630666e0ae8a251b80299ed1d
DIFF: https://github.com/llvm/llvm-project/commit/156e8b37024abd8630666e0ae8a251b80299ed1d.diff
LOG: clang/Basic: Remove ContentCache::getRawBuffer, NFC
Replace `ContentCache::getRawBuffer` with `getBufferDataIfLoaded` and
`getBufferIfLoaded`, excising another accessor for the underlying
`MemoryBuffer*` in favour of `StringRef` and `MemoryBufferRef`.
Differential Revision: https://reviews.llvm.org/D89445
Added:
Modified:
clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp
clang/include/clang/Basic/SourceManager.h
clang/lib/Basic/SourceManager.cpp
clang/lib/Serialization/ASTReader.cpp
clang/lib/Serialization/ASTWriter.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp b/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp
index f20182bd25e6..4299baedd79f 100644
--- a/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp
+++ b/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp
@@ -30,12 +30,12 @@ class ExpandModularHeadersPPCallbacks::FileRecorder {
return;
// FIXME: Why is this happening? We might be losing contents here.
- if (!ContentCache.getRawBuffer())
+ llvm::Optional<StringRef> Data = ContentCache.getBufferDataIfLoaded();
+ if (!Data)
return;
InMemoryFs.addFile(File->getName(), /*ModificationTime=*/0,
- llvm::MemoryBuffer::getMemBufferCopy(
- ContentCache.getRawBuffer()->getBuffer()));
+ llvm::MemoryBuffer::getMemBufferCopy(*Data));
// Remove the file from the set of necessary files.
FilesToRecord.erase(File);
}
diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h
index 65c3de5a1e41..4b84ae132c3e 100644
--- a/clang/include/clang/Basic/SourceManager.h
+++ b/clang/include/clang/Basic/SourceManager.h
@@ -194,9 +194,23 @@ namespace SrcMgr {
/// this content cache. This is used for performance analysis.
llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
- /// Get the underlying buffer, returning NULL if the buffer is not
- /// yet available.
- const llvm::MemoryBuffer *getRawBuffer() const { return Buffer.get(); }
+ /// Return the buffer, only if it has been loaded.
+ /// specified FileID, returning None if it's not yet loaded.
+ ///
+ /// \param FID The file ID whose contents will be returned.
+ llvm::Optional<llvm::MemoryBufferRef> getBufferIfLoaded() const {
+ if (Buffer)
+ return Buffer->getMemBufferRef();
+ return None;
+ }
+
+ /// Return a StringRef to the source buffer data, only if it has already
+ /// been loaded.
+ llvm::Optional<StringRef> getBufferDataIfLoaded() const {
+ if (Buffer)
+ return Buffer->getBuffer();
+ return None;
+ }
/// Set the buffer.
void setBuffer(std::unique_ptr<llvm::MemoryBuffer> B) {
@@ -207,10 +221,10 @@ namespace SrcMgr {
/// Set the buffer to one that's not owned (or to nullptr).
///
/// \pre Buffer cannot already be set.
- void setUnownedBuffer(const llvm::MemoryBuffer *B) {
+ void setUnownedBuffer(llvm::Optional<llvm::MemoryBufferRef> B) {
assert(!Buffer && "Expected to be called right after construction");
if (B)
- setBuffer(llvm::MemoryBuffer::getMemBuffer(B->getMemBufferRef()));
+ setBuffer(llvm::MemoryBuffer::getMemBuffer(*B));
}
// If BufStr has an invalid BOM, returns the BOM name; otherwise, returns
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index f61b4e2a2281..9592b92f59bc 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -155,7 +155,7 @@ ContentCache::getBufferOrNone(DiagnosticsEngine &Diag, FileManager &FM,
// Check that the file's size is the same as in the file entry (which may
// have come from a stat cache).
- if (getRawBuffer()->getBufferSize() != (size_t)ContentsEntry->getSize()) {
+ if (Buffer->getBufferSize() != (size_t)ContentsEntry->getSize()) {
if (Diag.isDiagnosticInFlight())
Diag.SetDelayedDiagnostic(diag::err_file_modified,
ContentsEntry->getName());
@@ -363,7 +363,7 @@ void SourceManager::initializeForReplay(const SourceManager &Old) {
Clone->BufferOverridden = Cache->BufferOverridden;
Clone->IsFileVolatile = Cache->IsFileVolatile;
Clone->IsTransient = Cache->IsTransient;
- Clone->setUnownedBuffer(Cache->getRawBuffer());
+ Clone->setUnownedBuffer(Cache->getBufferIfLoaded());
return Clone;
};
@@ -476,7 +476,8 @@ const SrcMgr::ContentCache *
SourceManager::getFakeContentCacheForRecovery() const {
if (!FakeContentCacheForRecovery) {
FakeContentCacheForRecovery = std::make_unique<SrcMgr::ContentCache>();
- FakeContentCacheForRecovery->setUnownedBuffer(getFakeBufferForRecovery());
+ FakeContentCacheForRecovery->setUnownedBuffer(
+ getFakeBufferForRecovery()->getMemBufferRef());
}
return FakeContentCacheForRecovery.get();
}
@@ -751,10 +752,7 @@ SourceManager::getBufferDataIfLoaded(FileID FID) const {
if (!SLoc.isFile() || MyInvalid)
return None;
- if (const llvm::MemoryBuffer *Buf =
- SLoc.getFile().getContentCache()->getRawBuffer())
- return Buf->getBuffer();
- return None;
+ return SLoc.getFile().getContentCache()->getBufferDataIfLoaded();
}
llvm::Optional<StringRef> SourceManager::getBufferDataOrNone(FileID FID) const {
diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp
index 4cd76c0bba94..bad4342d380d 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -1542,7 +1542,7 @@ bool ASTReader::ReadSLocEntry(int ID) {
= SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
if (OverriddenBuffer && !ContentCache->BufferOverridden &&
ContentCache->ContentsEntry == ContentCache->OrigEntry &&
- !ContentCache->getRawBuffer()) {
+ !ContentCache->getBufferIfLoaded()) {
auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
if (!Buffer)
return true;
diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp
index b23e04279d29..760278c20288 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -1468,7 +1468,7 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
if (PP->getHeaderSearchInfo()
.getHeaderSearchOpts()
.ValidateASTInputFilesContent) {
- auto *MemBuff = Cache->getRawBuffer();
+ auto MemBuff = Cache->getBufferIfLoaded();
if (MemBuff)
ContentHash = hash_value(MemBuff->getBuffer());
else
More information about the cfe-commits
mailing list