[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 15:23:27 +08:00
|
|
|
//===-- DataEncoder.cpp ---------------------------------------------------===//
|
2011-09-02 02:10:09 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08: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-02 02:10:09 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-03-04 09:30:05 +08:00
|
|
|
#include "lldb/Utility/DataEncoder.h"
|
|
|
|
|
|
|
|
#include "lldb/Utility/DataBuffer.h"
|
|
|
|
#include "lldb/Utility/Endian.h"
|
|
|
|
|
2017-12-13 09:41:16 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
2018-11-12 07:16:43 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2011-09-02 02:10:09 +08:00
|
|
|
|
2016-03-02 09:09:03 +08:00
|
|
|
#include <cstddef>
|
2011-09-02 02:10:09 +08:00
|
|
|
|
2021-05-26 18:19:37 +08:00
|
|
|
#include <cstring>
|
2017-04-07 02:12:24 +08:00
|
|
|
|
2011-09-02 02:10:09 +08:00
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
2017-12-13 09:41:16 +08:00
|
|
|
using namespace llvm::support::endian;
|
2011-09-02 02:10:09 +08:00
|
|
|
|
|
|
|
// Default constructor.
|
|
|
|
DataEncoder::DataEncoder()
|
2015-11-07 12:40:13 +08:00
|
|
|
: m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)),
|
2016-03-02 09:09:03 +08:00
|
|
|
m_data_sp() {}
|
2011-09-02 02:10:09 +08:00
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// This constructor allows us to use data that is owned by someone else. The
|
|
|
|
// data must stay around as long as this object is valid.
|
2011-09-02 02:10:09 +08:00
|
|
|
DataEncoder::DataEncoder(void *data, uint32_t length, ByteOrder endian,
|
|
|
|
uint8_t addr_size)
|
2019-05-23 13:12:11 +08:00
|
|
|
: m_start(static_cast<uint8_t *>(data)),
|
|
|
|
m_end(static_cast<uint8_t *>(data) + length), m_byte_order(endian),
|
|
|
|
m_addr_size(addr_size), m_data_sp() {}
|
2011-09-02 02:10:09 +08:00
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Make a shared pointer reference to the shared data in "data_sp" and set the
|
|
|
|
// endian swapping setting to "swap", and the address size to "addr_size". The
|
|
|
|
// shared data reference will ensure the data lives as long as any DataEncoder
|
|
|
|
// objects exist that have a reference to this data.
|
2011-09-02 02:10:09 +08:00
|
|
|
DataEncoder::DataEncoder(const DataBufferSP &data_sp, ByteOrder endian,
|
|
|
|
uint8_t addr_size)
|
|
|
|
: m_start(nullptr), m_end(nullptr), m_byte_order(endian),
|
2016-03-02 09:09:03 +08:00
|
|
|
m_addr_size(addr_size), m_data_sp() {
|
2011-09-02 02:10:09 +08:00
|
|
|
SetData(data_sp);
|
|
|
|
}
|
|
|
|
|
2016-03-02 09:09:03 +08:00
|
|
|
DataEncoder::~DataEncoder() = default;
|
2011-09-02 02:10:09 +08:00
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Clears the object contents back to a default invalid state, and release any
|
|
|
|
// references to shared data that this object may contain.
|
2011-09-02 02:10:09 +08:00
|
|
|
void DataEncoder::Clear() {
|
2016-03-02 09:09:03 +08:00
|
|
|
m_start = nullptr;
|
|
|
|
m_end = nullptr;
|
2015-11-07 12:40:13 +08:00
|
|
|
m_byte_order = endian::InlHostByteOrder();
|
2011-09-02 02:10:09 +08:00
|
|
|
m_addr_size = sizeof(void *);
|
|
|
|
m_data_sp.reset();
|
|
|
|
}
|
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Assign the data for this object to be a subrange of the shared data in
|
|
|
|
// "data_sp" starting "data_offset" bytes into "data_sp" and ending
|
|
|
|
// "data_length" bytes later. If "data_offset" is not a valid offset into
|
|
|
|
// "data_sp", then this object will contain no bytes. If "data_offset" is
|
|
|
|
// within "data_sp" yet "data_length" is too large, the length will be capped
|
|
|
|
// at the number of bytes remaining in "data_sp". A ref counted pointer to the
|
|
|
|
// data in "data_sp" will be made in this object IF the number of bytes this
|
|
|
|
// object refers to in greater than zero (if at least one byte was available
|
|
|
|
// starting at "data_offset") to ensure the data stays around as long as it is
|
|
|
|
// needed. The address size and endian swap settings will remain unchanged from
|
|
|
|
// their current settings.
|
2011-09-02 02:10:09 +08:00
|
|
|
uint32_t DataEncoder::SetData(const DataBufferSP &data_sp, uint32_t data_offset,
|
|
|
|
uint32_t data_length) {
|
2016-03-02 09:09:03 +08:00
|
|
|
m_start = m_end = nullptr;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2011-09-02 02:10:09 +08:00
|
|
|
if (data_length > 0) {
|
|
|
|
m_data_sp = data_sp;
|
2016-03-02 09:09:03 +08:00
|
|
|
if (data_sp) {
|
2011-09-02 02:10:09 +08:00
|
|
|
const size_t data_size = data_sp->GetByteSize();
|
|
|
|
if (data_offset < data_size) {
|
|
|
|
m_start = data_sp->GetBytes() + data_offset;
|
|
|
|
const size_t bytes_left = data_size - data_offset;
|
|
|
|
// Cap the length of we asked for too many
|
|
|
|
if (data_length <= bytes_left)
|
|
|
|
m_end = m_start + data_length; // We got all the bytes we wanted
|
|
|
|
else
|
|
|
|
m_end = m_start + bytes_left; // Not all the bytes requested were
|
|
|
|
// available in the shared data
|
|
|
|
}
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2011-09-02 02:10:09 +08:00
|
|
|
|
|
|
|
uint32_t new_size = GetByteSize();
|
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Don't hold a shared pointer to the data buffer if we don't share any valid
|
|
|
|
// bytes in the shared buffer.
|
2011-09-02 02:10:09 +08:00
|
|
|
if (new_size == 0)
|
|
|
|
m_data_sp.reset();
|
|
|
|
|
|
|
|
return new_size;
|
|
|
|
}
|
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Extract a single unsigned char from the binary data and update the offset
|
|
|
|
// pointed to by "offset_ptr".
|
2011-09-02 02:10:09 +08:00
|
|
|
//
|
|
|
|
// RETURNS the byte that was extracted, or zero on failure.
|
|
|
|
uint32_t DataEncoder::PutU8(uint32_t offset, uint8_t value) {
|
|
|
|
if (ValidOffset(offset)) {
|
|
|
|
m_start[offset] = value;
|
|
|
|
return offset + 1;
|
|
|
|
}
|
2016-03-12 11:33:36 +08:00
|
|
|
return UINT32_MAX;
|
2011-09-02 02:10:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t DataEncoder::PutU16(uint32_t offset, uint16_t value) {
|
|
|
|
if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
|
2015-11-07 12:40:13 +08:00
|
|
|
if (m_byte_order != endian::InlHostByteOrder())
|
2017-12-13 09:41:16 +08:00
|
|
|
write16be(m_start + offset, value);
|
2011-09-02 02:10:09 +08:00
|
|
|
else
|
2017-12-13 09:41:16 +08:00
|
|
|
write16le(m_start + offset, value);
|
2011-09-02 02:10:09 +08:00
|
|
|
|
|
|
|
return offset + sizeof(value);
|
|
|
|
}
|
2016-03-12 11:33:36 +08:00
|
|
|
return UINT32_MAX;
|
2011-09-02 02:10:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t DataEncoder::PutU32(uint32_t offset, uint32_t value) {
|
|
|
|
if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
|
2015-11-07 12:40:13 +08:00
|
|
|
if (m_byte_order != endian::InlHostByteOrder())
|
2017-12-13 09:41:16 +08:00
|
|
|
write32be(m_start + offset, value);
|
2011-09-02 02:10:09 +08:00
|
|
|
else
|
2017-12-13 09:41:16 +08:00
|
|
|
write32le(m_start + offset, value);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2011-09-02 02:10:09 +08:00
|
|
|
return offset + sizeof(value);
|
|
|
|
}
|
2016-03-12 11:33:36 +08:00
|
|
|
return UINT32_MAX;
|
2011-09-02 02:10:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t DataEncoder::PutU64(uint32_t offset, uint64_t value) {
|
|
|
|
if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
|
2015-11-07 12:40:13 +08:00
|
|
|
if (m_byte_order != endian::InlHostByteOrder())
|
2017-12-13 09:41:16 +08:00
|
|
|
write64be(m_start + offset, value);
|
2011-09-02 02:10:09 +08:00
|
|
|
else
|
2017-12-13 09:41:16 +08:00
|
|
|
write64le(m_start + offset, value);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2011-09-02 02:10:09 +08:00
|
|
|
return offset + sizeof(value);
|
|
|
|
}
|
2016-03-12 11:33:36 +08:00
|
|
|
return UINT32_MAX;
|
2011-09-02 02:10:09 +08:00
|
|
|
}
|
|
|
|
|
2019-11-14 07:38:43 +08:00
|
|
|
uint32_t DataEncoder::PutUnsigned(uint32_t offset, uint32_t byte_size,
|
|
|
|
uint64_t value) {
|
2011-09-02 02:10:09 +08: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 08:38:06 +08:00
|
|
|
llvm_unreachable("GetMax64 unhandled case!");
|
2011-09-02 02:10:09 +08:00
|
|
|
}
|
2016-03-12 11:33:36 +08:00
|
|
|
return UINT32_MAX;
|
2011-09-02 02:10:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t DataEncoder::PutData(uint32_t offset, const void *src,
|
|
|
|
uint32_t src_len) {
|
2016-03-02 09:09:03 +08:00
|
|
|
if (src == nullptr || src_len == 0)
|
2011-09-02 02:10:09 +08:00
|
|
|
return offset;
|
|
|
|
|
|
|
|
if (ValidOffsetForDataOfSize(offset, src_len)) {
|
|
|
|
memcpy(m_start + offset, src, src_len);
|
|
|
|
return offset + src_len;
|
|
|
|
}
|
2016-03-12 11:33:36 +08:00
|
|
|
return UINT32_MAX;
|
2011-09-02 02:10:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t DataEncoder::PutAddress(uint32_t offset, lldb::addr_t addr) {
|
2019-11-14 07:38:43 +08:00
|
|
|
return PutUnsigned(offset, m_addr_size, addr);
|
2011-09-02 02:10:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t DataEncoder::PutCString(uint32_t offset, const char *cstr) {
|
2016-03-02 09:09:03 +08:00
|
|
|
if (cstr != nullptr)
|
2011-09-02 02:13:54 +08:00
|
|
|
return PutData(offset, cstr, strlen(cstr) + 1);
|
2016-03-12 11:33:36 +08:00
|
|
|
return UINT32_MAX;
|
2011-09-02 02:10:09 +08:00
|
|
|
}
|