r321167 - Remove llvm::MemoryBuffer const_casts
Pavel Labath via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 20 03:34:39 PST 2017
Author: labath
Date: Wed Dec 20 03:34:38 2017
New Revision: 321167
URL: http://llvm.org/viewvc/llvm-project?rev=321167&view=rev
Log:
Remove llvm::MemoryBuffer const_casts
Summary:
llvm has grown a WritableMemoryBuffer class, which is convertible
(inherits from) a MemoryBuffer. We can use it to avoid conts_casting the
buffer contents when we want to write to it.
Reviewers: dblaikie, rsmith
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D41387
Modified:
cfe/trunk/lib/Basic/SourceManager.cpp
cfe/trunk/lib/Lex/Preprocessor.cpp
Modified: cfe/trunk/lib/Basic/SourceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/SourceManager.cpp?rev=321167&r1=321166&r2=321167&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/SourceManager.cpp (original)
+++ cfe/trunk/lib/Basic/SourceManager.cpp Wed Dec 20 03:34:38 2017
@@ -125,11 +125,12 @@ llvm::MemoryBuffer *ContentCache::getBuf
// possible.
if (!BufferOrError) {
StringRef FillStr("<<<MISSING SOURCE FILE>>>\n");
- Buffer.setPointer(MemoryBuffer::getNewUninitMemBuffer(
- ContentsEntry->getSize(), "<invalid>").release());
- char *Ptr = const_cast<char*>(Buffer.getPointer()->getBufferStart());
+ auto BackupBuffer = llvm::WritableMemoryBuffer::getNewUninitMemBuffer(
+ ContentsEntry->getSize(), "<invalid>");
+ char *Ptr = BackupBuffer->getBufferStart();
for (unsigned i = 0, e = ContentsEntry->getSize(); i != e; ++i)
Ptr[i] = FillStr[i % FillStr.size()];
+ Buffer.setPointer(BackupBuffer.release());
if (Diag.isDiagnosticInFlight())
Diag.SetDelayedDiagnostic(diag::err_cannot_open_file,
Modified: cfe/trunk/lib/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Preprocessor.cpp?rev=321167&r1=321166&r2=321167&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Preprocessor.cpp (original)
+++ cfe/trunk/lib/Lex/Preprocessor.cpp Wed Dec 20 03:34:38 2017
@@ -420,10 +420,9 @@ bool Preprocessor::SetCodeCompletionPoin
CodeCompletionFile = File;
CodeCompletionOffset = Position - Buffer->getBufferStart();
- std::unique_ptr<MemoryBuffer> NewBuffer =
- MemoryBuffer::getNewUninitMemBuffer(Buffer->getBufferSize() + 1,
- Buffer->getBufferIdentifier());
- char *NewBuf = const_cast<char*>(NewBuffer->getBufferStart());
+ auto NewBuffer = llvm::WritableMemoryBuffer::getNewUninitMemBuffer(
+ Buffer->getBufferSize() + 1, Buffer->getBufferIdentifier());
+ char *NewBuf = NewBuffer->getBufferStart();
char *NewPos = std::copy(Buffer->getBufferStart(), Position, NewBuf);
*NewPos = '\0';
std::copy(Position, Buffer->getBufferEnd(), NewPos+1);
More information about the cfe-commits
mailing list