[lldb][NFC] Fix all formatting errors in .cpp file headers
Summary:
A *.cpp file header in LLDB (and in LLDB) should like this:
```
//===-- TestUtilities.cpp -------------------------------------------------===//
```
However in LLDB most of our source files have arbitrary changes to this format and
these changes are spreading through LLDB as folks usually just use the existing
source files as templates for their new files (most notably the unnecessary
editor language indicator `-*- C++ -*-` is spreading and in every review
someone is pointing out that this is wrong, resulting in people pointing out that this
is done in the same way in other files).
This patch removes most of these inconsistencies including the editor language indicators,
all the different missing/additional '-' characters, files that center the file name, missing
trailing `===//` (mostly caused by clang-format breaking the line).
Reviewers: aprantl, espindola, jfb, shafik, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: dexonsmith, wuzish, emaste, sdardis, nemanjai, kbarton, MaskRay, atanasyan, arphaman, jfb, abidh, jsji, JDevlieghere, usaxena95, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D73258
2020-01-24 08:23:27 +01:00
|
|
|
//===-- DataEncoder.cpp ---------------------------------------------------===//
|
2011-09-01 18:10:09 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-09-01 18:10:09 +00:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2017-03-04 01:30:05 +00:00
|
|
|
#include "lldb/Utility/DataEncoder.h"
|
|
|
|
|
|
2021-12-07 09:44:22 -08:00
|
|
|
#include "lldb/Utility/DataBufferHeap.h"
|
2017-03-04 01:30:05 +00:00
|
|
|
#include "lldb/Utility/Endian.h"
|
|
|
|
|
|
2017-12-13 01:41:16 +00:00
|
|
|
#include "llvm/Support/Endian.h"
|
2018-11-11 23:16:43 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2011-09-01 18:10:09 +00:00
|
|
|
|
2016-03-02 01:09:03 +00:00
|
|
|
#include <cstddef>
|
2011-09-01 18:10:09 +00:00
|
|
|
|
2021-05-26 12:19:37 +02:00
|
|
|
#include <cstring>
|
2017-04-06 18:12:24 +00:00
|
|
|
|
2011-09-01 18:10:09 +00:00
|
|
|
using namespace lldb;
|
|
|
|
|
using namespace lldb_private;
|
2017-12-13 01:41:16 +00:00
|
|
|
using namespace llvm::support::endian;
|
2011-09-01 18:10:09 +00:00
|
|
|
|
|
|
|
|
DataEncoder::DataEncoder()
|
2021-12-07 09:44:22 -08:00
|
|
|
: m_data_sp(new DataBufferHeap()), m_byte_order(endian::InlHostByteOrder()),
|
|
|
|
|
m_addr_size(sizeof(void *)) {}
|
2011-09-01 18:10:09 +00:00
|
|
|
|
2021-12-07 09:44:22 -08:00
|
|
|
DataEncoder::DataEncoder(const void *data, uint32_t length, ByteOrder endian,
|
2011-09-01 18:10:09 +00:00
|
|
|
uint8_t addr_size)
|
2021-12-07 09:44:22 -08:00
|
|
|
: m_data_sp(new DataBufferHeap(data, length)), m_byte_order(endian),
|
|
|
|
|
m_addr_size(addr_size) {}
|
2011-09-01 18:10:09 +00:00
|
|
|
|
2021-12-07 09:44:22 -08:00
|
|
|
DataEncoder::DataEncoder(ByteOrder endian, uint8_t addr_size)
|
|
|
|
|
: m_data_sp(new DataBufferHeap()), m_byte_order(endian),
|
|
|
|
|
m_addr_size(addr_size) {}
|
2011-09-01 18:10:09 +00:00
|
|
|
|
2021-12-07 09:44:22 -08:00
|
|
|
DataEncoder::~DataEncoder() = default;
|
2011-09-01 18:10:09 +00:00
|
|
|
|
2021-12-07 09:44:22 -08:00
|
|
|
llvm::ArrayRef<uint8_t> DataEncoder::GetData() const {
|
|
|
|
|
return llvm::ArrayRef<uint8_t>(m_data_sp->GetBytes(), GetByteSize());
|
2011-09-01 18:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
2021-12-07 09:44:22 -08:00
|
|
|
size_t DataEncoder::GetByteSize() const { return m_data_sp->GetByteSize(); }
|
|
|
|
|
|
2018-04-30 16:49:04 +00:00
|
|
|
// Extract a single unsigned char from the binary data and update the offset
|
|
|
|
|
// pointed to by "offset_ptr".
|
2011-09-01 18:10:09 +00:00
|
|
|
//
|
|
|
|
|
// RETURNS the byte that was extracted, or zero on failure.
|
|
|
|
|
uint32_t DataEncoder::PutU8(uint32_t offset, uint8_t value) {
|
|
|
|
|
if (ValidOffset(offset)) {
|
2021-12-07 09:44:22 -08:00
|
|
|
m_data_sp->GetBytes()[offset] = value;
|
2011-09-01 18:10:09 +00:00
|
|
|
return offset + 1;
|
|
|
|
|
}
|
2016-03-12 03:33:36 +00:00
|
|
|
return UINT32_MAX;
|
2011-09-01 18:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t DataEncoder::PutU16(uint32_t offset, uint16_t value) {
|
|
|
|
|
if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
|
2015-11-07 04:40:13 +00:00
|
|
|
if (m_byte_order != endian::InlHostByteOrder())
|
2021-12-07 09:44:22 -08:00
|
|
|
write16be(m_data_sp->GetBytes() + offset, value);
|
2011-09-01 18:10:09 +00:00
|
|
|
else
|
2021-12-07 09:44:22 -08:00
|
|
|
write16le(m_data_sp->GetBytes() + offset, value);
|
2011-09-01 18:10:09 +00:00
|
|
|
|
|
|
|
|
return offset + sizeof(value);
|
|
|
|
|
}
|
2016-03-12 03:33:36 +00:00
|
|
|
return UINT32_MAX;
|
2011-09-01 18:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t DataEncoder::PutU32(uint32_t offset, uint32_t value) {
|
|
|
|
|
if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
|
2015-11-07 04:40:13 +00:00
|
|
|
if (m_byte_order != endian::InlHostByteOrder())
|
2021-12-07 09:44:22 -08:00
|
|
|
write32be(m_data_sp->GetBytes() + offset, value);
|
2011-09-01 18:10:09 +00:00
|
|
|
else
|
2021-12-07 09:44:22 -08:00
|
|
|
write32le(m_data_sp->GetBytes() + offset, value);
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2011-09-01 18:10:09 +00:00
|
|
|
return offset + sizeof(value);
|
|
|
|
|
}
|
2016-03-12 03:33:36 +00:00
|
|
|
return UINT32_MAX;
|
2011-09-01 18:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t DataEncoder::PutU64(uint32_t offset, uint64_t value) {
|
|
|
|
|
if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
|
2015-11-07 04:40:13 +00:00
|
|
|
if (m_byte_order != endian::InlHostByteOrder())
|
2021-12-07 09:44:22 -08:00
|
|
|
write64be(m_data_sp->GetBytes() + offset, value);
|
2011-09-01 18:10:09 +00:00
|
|
|
else
|
2021-12-07 09:44:22 -08:00
|
|
|
write64le(m_data_sp->GetBytes() + offset, value);
|
2016-09-06 20:57:50 +00:00
|
|
|
|
2011-09-01 18:10:09 +00:00
|
|
|
return offset + sizeof(value);
|
|
|
|
|
}
|
2016-03-12 03:33:36 +00:00
|
|
|
return UINT32_MAX;
|
2011-09-01 18:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
2019-11-13 15:38:43 -08:00
|
|
|
uint32_t DataEncoder::PutUnsigned(uint32_t offset, uint32_t byte_size,
|
|
|
|
|
uint64_t value) {
|
2011-09-01 18:10:09 +00:00
|
|
|
switch (byte_size) {
|
|
|
|
|
case 1:
|
|
|
|
|
return PutU8(offset, value);
|
|
|
|
|
case 2:
|
|
|
|
|
return PutU16(offset, value);
|
|
|
|
|
case 4:
|
|
|
|
|
return PutU32(offset, value);
|
|
|
|
|
case 8:
|
|
|
|
|
return PutU64(offset, value);
|
|
|
|
|
default:
|
2017-01-06 00:38:06 +00:00
|
|
|
llvm_unreachable("GetMax64 unhandled case!");
|
2011-09-01 18:10:09 +00:00
|
|
|
}
|
2016-03-12 03:33:36 +00:00
|
|
|
return UINT32_MAX;
|
2011-09-01 18:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t DataEncoder::PutData(uint32_t offset, const void *src,
|
|
|
|
|
uint32_t src_len) {
|
2016-03-02 01:09:03 +00:00
|
|
|
if (src == nullptr || src_len == 0)
|
2011-09-01 18:10:09 +00:00
|
|
|
return offset;
|
|
|
|
|
|
|
|
|
|
if (ValidOffsetForDataOfSize(offset, src_len)) {
|
2021-12-07 09:44:22 -08:00
|
|
|
memcpy(m_data_sp->GetBytes() + offset, src, src_len);
|
2011-09-01 18:10:09 +00:00
|
|
|
return offset + src_len;
|
|
|
|
|
}
|
2016-03-12 03:33:36 +00:00
|
|
|
return UINT32_MAX;
|
2011-09-01 18:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t DataEncoder::PutAddress(uint32_t offset, lldb::addr_t addr) {
|
2019-11-13 15:38:43 -08:00
|
|
|
return PutUnsigned(offset, m_addr_size, addr);
|
2011-09-01 18:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t DataEncoder::PutCString(uint32_t offset, const char *cstr) {
|
2016-03-02 01:09:03 +00:00
|
|
|
if (cstr != nullptr)
|
2011-09-01 18:13:54 +00:00
|
|
|
return PutData(offset, cstr, strlen(cstr) + 1);
|
2016-03-12 03:33:36 +00:00
|
|
|
return UINT32_MAX;
|
2011-09-01 18:10:09 +00:00
|
|
|
}
|
2021-12-07 09:44:22 -08:00
|
|
|
|
|
|
|
|
void DataEncoder::AppendU8(uint8_t value) {
|
|
|
|
|
m_data_sp->AppendData(&value, sizeof(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DataEncoder::AppendU16(uint16_t value) {
|
|
|
|
|
uint32_t offset = m_data_sp->GetByteSize();
|
|
|
|
|
m_data_sp->SetByteSize(m_data_sp->GetByteSize() + sizeof(value));
|
|
|
|
|
PutU16(offset, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DataEncoder::AppendU32(uint32_t value) {
|
|
|
|
|
uint32_t offset = m_data_sp->GetByteSize();
|
|
|
|
|
m_data_sp->SetByteSize(m_data_sp->GetByteSize() + sizeof(value));
|
|
|
|
|
PutU32(offset, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DataEncoder::AppendU64(uint64_t value) {
|
|
|
|
|
uint32_t offset = m_data_sp->GetByteSize();
|
|
|
|
|
m_data_sp->SetByteSize(m_data_sp->GetByteSize() + sizeof(value));
|
|
|
|
|
PutU64(offset, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DataEncoder::AppendAddress(lldb::addr_t addr) {
|
|
|
|
|
switch (m_addr_size) {
|
|
|
|
|
case 4:
|
|
|
|
|
AppendU32(addr);
|
|
|
|
|
break;
|
|
|
|
|
case 8:
|
|
|
|
|
AppendU64(addr);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
llvm_unreachable("AppendAddress unhandled case!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DataEncoder::AppendData(llvm::StringRef data) {
|
|
|
|
|
const char *bytes = data.data();
|
|
|
|
|
const size_t length = data.size();
|
|
|
|
|
if (bytes && length > 0)
|
|
|
|
|
m_data_sp->AppendData(bytes, length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DataEncoder::AppendCString(llvm::StringRef data) {
|
|
|
|
|
const char *bytes = data.data();
|
|
|
|
|
const size_t length = data.size();
|
|
|
|
|
if (bytes) {
|
|
|
|
|
if (length > 0)
|
|
|
|
|
m_data_sp->AppendData(bytes, length);
|
|
|
|
|
if (length == 0 || bytes[length - 1] != '\0')
|
|
|
|
|
AppendU8(0);
|
|
|
|
|
}
|
|
|
|
|
}
|