labath updated this revision to Diff 127500. labath added a comment. It took a while, but we've finally landed an llvm::WritableMemoryBuffer class. This patch now becomes simple, as all I need to do is use that instead of the MemoryBuffer class.
Well.. almost. The new class does not have the null-termination capability, so I also update our buffer uses to not require null termination. https://reviews.llvm.org/D40079 Files: include/lldb/Interpreter/OptionValueFileSpec.h include/lldb/Target/Target.h include/lldb/Utility/DataBufferLLVM.h source/Interpreter/OptionValueFileSpec.cpp source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp source/Plugins/Platform/MacOSX/PlatformDarwin.cpp source/Target/Target.cpp source/Utility/DataBufferLLVM.cpp
Index: source/Utility/DataBufferLLVM.cpp =================================================================== --- source/Utility/DataBufferLLVM.cpp +++ source/Utility/DataBufferLLVM.cpp @@ -18,7 +18,8 @@ using namespace lldb_private; -DataBufferLLVM::DataBufferLLVM(std::unique_ptr<llvm::MemoryBuffer> MemBuffer) +DataBufferLLVM::DataBufferLLVM( + std::unique_ptr<llvm::WritableMemoryBuffer> MemBuffer) : Buffer(std::move(MemBuffer)) { assert(Buffer != nullptr && "Cannot construct a DataBufferLLVM with a null buffer"); @@ -28,43 +29,40 @@ std::shared_ptr<DataBufferLLVM> DataBufferLLVM::CreateSliceFromPath(const llvm::Twine &Path, uint64_t Size, - uint64_t Offset, bool Private) { + uint64_t Offset) { // If the file resides non-locally, pass the volatile flag so that we don't // mmap it. - if (!Private) - Private = !llvm::sys::fs::is_local(Path); + bool IsVolatile = !llvm::sys::fs::is_local(Path); - auto Buffer = llvm::MemoryBuffer::getFileSlice(Path, Size, Offset, Private); + auto Buffer = + llvm::WritableMemoryBuffer::getFileSlice(Path, Size, Offset, IsVolatile); if (!Buffer) return nullptr; return std::shared_ptr<DataBufferLLVM>( new DataBufferLLVM(std::move(*Buffer))); } std::shared_ptr<DataBufferLLVM> -DataBufferLLVM::CreateFromPath(const llvm::Twine &Path, bool NullTerminate, bool Private) { +DataBufferLLVM::CreateFromPath(const llvm::Twine &Path) { // If the file resides non-locally, pass the volatile flag so that we don't // mmap it. - if (!Private) - Private = !llvm::sys::fs::is_local(Path); + bool IsVolatile = !llvm::sys::fs::is_local(Path); - auto Buffer = llvm::MemoryBuffer::getFile(Path, -1, NullTerminate, Private); + auto Buffer = llvm::WritableMemoryBuffer::getFile(Path, -1, IsVolatile); if (!Buffer) return nullptr; return std::shared_ptr<DataBufferLLVM>( new DataBufferLLVM(std::move(*Buffer))); } uint8_t *DataBufferLLVM::GetBytes() { - return const_cast<uint8_t *>(GetBuffer()); + return reinterpret_cast<uint8_t *>(Buffer->getBufferStart()); } -const uint8_t *DataBufferLLVM::GetBytes() const { return GetBuffer(); } +const uint8_t *DataBufferLLVM::GetBytes() const { + return reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()); +} lldb::offset_t DataBufferLLVM::GetByteSize() const { return Buffer->getBufferSize(); } - -const uint8_t *DataBufferLLVM::GetBuffer() const { - return reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()); -} Index: source/Target/Target.cpp =================================================================== --- source/Target/Target.cpp +++ source/Target/Target.cpp @@ -2313,7 +2313,7 @@ result_valobj_sp = persistent_var_sp->GetValueObject(); execution_results = eExpressionCompleted; } else { - const char *prefix = GetExpressionPrefixContentsAsCString(); + llvm::StringRef prefix = GetExpressionPrefixContents(); Status error; execution_results = UserExpression::Evaluate(exe_ctx, options, expr, prefix, result_valobj_sp, error, @@ -4033,18 +4033,19 @@ return LanguageType(); } -const char *TargetProperties::GetExpressionPrefixContentsAsCString() { +llvm::StringRef TargetProperties::GetExpressionPrefixContents() { const uint32_t idx = ePropertyExprPrefix; OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec(nullptr, false, idx); if (file) { - const bool null_terminate = true; - DataBufferSP data_sp(file->GetFileContents(null_terminate)); + DataBufferSP data_sp(file->GetFileContents()); if (data_sp) - return (const char *)data_sp->GetBytes(); + return llvm::StringRef( + reinterpret_cast<const char *>(data_sp->GetBytes()), + data_sp->GetByteSize()); } - return nullptr; + return ""; } bool TargetProperties::GetBreakpointsConsultPlatformAvoidList() { Index: source/Plugins/Platform/MacOSX/PlatformDarwin.cpp =================================================================== --- source/Plugins/Platform/MacOSX/PlatformDarwin.cpp +++ source/Plugins/Platform/MacOSX/PlatformDarwin.cpp @@ -1173,7 +1173,7 @@ xcode_dir_path.append("/usr/share/xcode-select/xcode_dir_path"); temp_file_spec.SetFile(xcode_dir_path, false); auto dir_buffer = - DataBufferLLVM::CreateFromPath(temp_file_spec.GetPath(), true); + DataBufferLLVM::CreateFromPath(temp_file_spec.GetPath()); if (dir_buffer && dir_buffer->GetByteSize() > 0) { llvm::StringRef path_ref(dir_buffer->GetChars()); // Trim tailing newlines and make sure there is enough room for a null Index: source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp =================================================================== --- source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp +++ source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp @@ -437,7 +437,7 @@ // A bit of a hack, but we intend to write to this buffer, so we can't // mmap it. auto buffer_sp = - DataBufferLLVM::CreateSliceFromPath(m_file.GetPath(), size, offset, true); + DataBufferLLVM::CreateSliceFromPath(m_file.GetPath(), size, offset); return DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()); } ProcessSP process_sp(m_process_wp.lock()); Index: source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp =================================================================== --- source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp +++ source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp @@ -406,8 +406,8 @@ lldb::offset_t file_offset, lldb::offset_t length) { if (!data_sp) { - data_sp = - DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset, true); + data_sp = DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, + file_offset); if (!data_sp) return nullptr; data_offset = 0; @@ -424,8 +424,8 @@ // Update the data to contain the entire file if it doesn't already if (data_sp->GetByteSize() < length) { - data_sp = - DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset, true); + data_sp = DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, + file_offset); if (!data_sp) return nullptr; data_offset = 0; Index: source/Interpreter/OptionValueFileSpec.cpp =================================================================== --- source/Interpreter/OptionValueFileSpec.cpp +++ source/Interpreter/OptionValueFileSpec.cpp @@ -113,14 +113,12 @@ return matches.GetSize(); } -const lldb::DataBufferSP & -OptionValueFileSpec::GetFileContents(bool null_terminate) { +const lldb::DataBufferSP &OptionValueFileSpec::GetFileContents() { if (m_current_value) { const auto file_mod_time = FileSystem::GetModificationTime(m_current_value); if (m_data_sp && m_data_mod_time == file_mod_time) return m_data_sp; - m_data_sp = DataBufferLLVM::CreateFromPath(m_current_value.GetPath(), - null_terminate); + m_data_sp = DataBufferLLVM::CreateFromPath(m_current_value.GetPath()); m_data_mod_time = file_mod_time; } return m_data_sp; Index: include/lldb/Utility/DataBufferLLVM.h =================================================================== --- include/lldb/Utility/DataBufferLLVM.h +++ include/lldb/Utility/DataBufferLLVM.h @@ -17,7 +17,7 @@ #include <stdint.h> // for uint8_t, uint64_t namespace llvm { -class MemoryBuffer; +class WritableMemoryBuffer; class Twine; } @@ -28,10 +28,10 @@ ~DataBufferLLVM(); static std::shared_ptr<DataBufferLLVM> - CreateSliceFromPath(const llvm::Twine &Path, uint64_t Size, uint64_t Offset, bool Private = false); + CreateSliceFromPath(const llvm::Twine &Path, uint64_t Size, uint64_t Offset); static std::shared_ptr<DataBufferLLVM> - CreateFromPath(const llvm::Twine &Path, bool NullTerminate = false, bool Private = false); + CreateFromPath(const llvm::Twine &Path); uint8_t *GetBytes() override; const uint8_t *GetBytes() const override; @@ -42,10 +42,9 @@ private: /// \brief Construct a DataBufferLLVM from \p Buffer. \p Buffer must be a /// valid pointer. - explicit DataBufferLLVM(std::unique_ptr<llvm::MemoryBuffer> Buffer); - const uint8_t *GetBuffer() const; + explicit DataBufferLLVM(std::unique_ptr<llvm::WritableMemoryBuffer> Buffer); - std::unique_ptr<llvm::MemoryBuffer> Buffer; + std::unique_ptr<llvm::WritableMemoryBuffer> Buffer; }; } Index: include/lldb/Target/Target.h =================================================================== --- include/lldb/Target/Target.h +++ include/lldb/Target/Target.h @@ -160,7 +160,7 @@ lldb::LanguageType GetLanguage() const; - const char *GetExpressionPrefixContentsAsCString(); + llvm::StringRef GetExpressionPrefixContents(); bool GetUseHexImmediates() const; Index: include/lldb/Interpreter/OptionValueFileSpec.h =================================================================== --- include/lldb/Interpreter/OptionValueFileSpec.h +++ include/lldb/Interpreter/OptionValueFileSpec.h @@ -77,7 +77,7 @@ void SetDefaultValue(const FileSpec &value) { m_default_value = value; } - const lldb::DataBufferSP &GetFileContents(bool null_terminate); + const lldb::DataBufferSP &GetFileContents(); void SetCompletionMask(uint32_t mask) { m_completion_mask = mask; }
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits