diff options
author | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2020-10-14 18:57:04 -0400 |
---|---|---|
committer | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2020-10-20 21:03:53 -0400 |
commit | 296314516d103f8eeb987a08b509c1381cfeef89 (patch) | |
tree | d6c8d4f4e5a34f1a48b73a53c87d9ab65c6ea56d /clang/lib/Basic/SourceManager.cpp | |
parent | 134ffa8138c31444685013e10f592cd7c88d675b (diff) | |
download | llvm-296314516d103f8eeb987a08b509c1381cfeef89.zip llvm-296314516d103f8eeb987a08b509c1381cfeef89.tar.gz llvm-296314516d103f8eeb987a08b509c1381cfeef89.tar.bz2 |
ContentCache: Simplify by always owning the MemoryBuffer
This changes `ContentCache::Buffer` to use
`std::unique_ptr<MemoryBuffer>` instead of the `PointerIntPair`. It
drops the (mostly unused) `DoNotFree` bit, instead creating a (new)
non-owning `MemoryBuffer` instance when passed a `MemoryBufferRef`.
Differential Revision: https://reviews.llvm.org/D67030
Diffstat (limited to 'clang/lib/Basic/SourceManager.cpp')
-rw-r--r-- | clang/lib/Basic/SourceManager.cpp | 60 |
1 files changed, 20 insertions, 40 deletions
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index c831d26..84315bf 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -49,28 +49,22 @@ using llvm::MemoryBuffer; // SourceManager Helper Classes //===----------------------------------------------------------------------===// -ContentCache::~ContentCache() { - if (shouldFreeBuffer()) - delete Buffer.getPointer(); -} - /// getSizeBytesMapped - Returns the number of bytes actually mapped for this /// ContentCache. This can be 0 if the MemBuffer was not actually expanded. unsigned ContentCache::getSizeBytesMapped() const { - return Buffer.getPointer() ? Buffer.getPointer()->getBufferSize() : 0; + return Buffer ? Buffer->getBufferSize() : 0; } /// Returns the kind of memory used to back the memory buffer for /// this content cache. This is used for performance analysis. llvm::MemoryBuffer::BufferKind ContentCache::getMemoryBufferKind() const { - assert(Buffer.getPointer()); + assert(Buffer); // Should be unreachable, but keep for sanity. - if (!Buffer.getPointer()) + if (!Buffer) return llvm::MemoryBuffer::MemoryBuffer_Malloc; - const llvm::MemoryBuffer *buf = Buffer.getPointer(); - return buf->getBufferKind(); + return Buffer->getBufferKind(); } /// getSize - Returns the size of the content encapsulated by this ContentCache. @@ -78,21 +72,8 @@ llvm::MemoryBuffer::BufferKind ContentCache::getMemoryBufferKind() const { /// scratch buffer. If the ContentCache encapsulates a source file, that /// file is not lazily brought in from disk to satisfy this query. unsigned ContentCache::getSize() const { - return Buffer.getPointer() ? (unsigned) Buffer.getPointer()->getBufferSize() - : (unsigned) ContentsEntry->getSize(); -} - -void ContentCache::replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree) { - if (B && B == Buffer.getPointer()) { - assert(0 && "Replacing with the same buffer"); - Buffer.setInt(DoNotFree? DoNotFreeFlag : 0); - return; - } - - if (shouldFreeBuffer()) - delete Buffer.getPointer(); - Buffer.setPointer(B); - Buffer.setInt((B && DoNotFree) ? DoNotFreeFlag : 0); + return Buffer ? (unsigned)Buffer->getBufferSize() + : (unsigned)ContentsEntry->getSize(); } const char *ContentCache::getInvalidBOM(StringRef BufStr) { @@ -125,8 +106,8 @@ ContentCache::getBufferOrNone(DiagnosticsEngine &Diag, FileManager &FM, // computed it, just return what we have. if (IsBufferInvalid) return None; - if (auto *B = Buffer.getPointer()) - return B->getMemBufferRef(); + if (Buffer) + return Buffer->getMemBufferRef(); if (!ContentsEntry) return None; @@ -168,7 +149,7 @@ ContentCache::getBufferOrNone(DiagnosticsEngine &Diag, FileManager &FM, return None; } - Buffer.setPointer(BufferOrError->release()); + Buffer = std::move(*BufferOrError); // Check that the file's size is the same as in the file entry (which may // have come from a stat cache). @@ -187,7 +168,7 @@ ContentCache::getBufferOrNone(DiagnosticsEngine &Diag, FileManager &FM, // If the buffer is valid, check to see if it has a UTF Byte Order Mark // (BOM). We only support UTF-8 with and without a BOM right now. See // http://en.wikipedia.org/wiki/Byte_order_mark for more information. - StringRef BufStr = Buffer.getPointer()->getBuffer(); + StringRef BufStr = Buffer->getBuffer(); const char *InvalidBOM = getInvalidBOM(BufStr); if (InvalidBOM) { @@ -197,7 +178,7 @@ ContentCache::getBufferOrNone(DiagnosticsEngine &Diag, FileManager &FM, return None; } - return Buffer.getPointer()->getMemBufferRef(); + return Buffer->getMemBufferRef(); } unsigned LineTableInfo::getLineTableFilenameID(StringRef Name) { @@ -380,7 +361,7 @@ void SourceManager::initializeForReplay(const SourceManager &Old) { Clone->BufferOverridden = Cache->BufferOverridden; Clone->IsFileVolatile = Cache->IsFileVolatile; Clone->IsTransient = Cache->IsTransient; - Clone->replaceBuffer(Cache->getRawBuffer(), /*DoNotFree*/true); + Clone->setUnownedBuffer(Cache->getRawBuffer()); return Clone; }; @@ -441,7 +422,7 @@ const ContentCache *SourceManager::createMemBufferContentCache( ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(); new (Entry) ContentCache(); MemBufferInfos.push_back(Entry); - Entry->replaceBuffer(Buffer.release(), /*DoNotFree=*/false); + Entry->setBuffer(std::move(Buffer)); return Entry; } @@ -493,8 +474,7 @@ const SrcMgr::ContentCache * SourceManager::getFakeContentCacheForRecovery() const { if (!FakeContentCacheForRecovery) { FakeContentCacheForRecovery = std::make_unique<SrcMgr::ContentCache>(); - FakeContentCacheForRecovery->replaceBuffer(getFakeBufferForRecovery(), - /*DoNotFree=*/true); + FakeContentCacheForRecovery->setUnownedBuffer(getFakeBufferForRecovery()); } return FakeContentCacheForRecovery.get(); } @@ -700,14 +680,14 @@ SourceManager::getMemoryBufferForFileOrNone(const FileEntry *File) { return IR->getBufferOrNone(Diag, getFileManager(), SourceLocation()); } -void SourceManager::overrideFileContents(const FileEntry *SourceFile, - llvm::MemoryBuffer *Buffer, - bool DoNotFree) { - const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile); +void SourceManager::overrideFileContents( + const FileEntry *SourceFile, std::unique_ptr<llvm::MemoryBuffer> Buffer) { + auto *IR = + const_cast<SrcMgr::ContentCache *>(getOrCreateContentCache(SourceFile)); assert(IR && "getOrCreateContentCache() cannot return NULL"); - const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(Buffer, DoNotFree); - const_cast<SrcMgr::ContentCache *>(IR)->BufferOverridden = true; + IR->setBuffer(std::move(Buffer)); + IR->BufferOverridden = true; getOverriddenFilesInfo().OverriddenFilesWithBuffer.insert(SourceFile); } |