[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
|
|
|
//===-- StringPrinter.cpp -------------------------------------------------===//
|
2014-10-30 09:45:39 +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
|
2014-10-30 09:45:39 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lldb/DataFormatters/StringPrinter.h"
|
|
|
|
|
2014-11-06 05:20:48 +08:00
|
|
|
#include "lldb/Core/Debugger.h"
|
|
|
|
#include "lldb/Core/ValueObject.h"
|
2015-09-10 06:30:24 +08:00
|
|
|
#include "lldb/Target/Language.h"
|
2014-10-30 09:45:39 +08:00
|
|
|
#include "lldb/Target/Process.h"
|
|
|
|
#include "lldb/Target/Target.h"
|
2017-05-12 12:51:55 +08:00
|
|
|
#include "lldb/Utility/Status.h"
|
2014-10-30 09:45:39 +08:00
|
|
|
|
2020-06-04 02:52:29 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2014-10-30 09:45:39 +08:00
|
|
|
#include "llvm/Support/ConvertUTF.h"
|
|
|
|
|
2021-05-26 18:19:37 +08:00
|
|
|
#include <cctype>
|
2014-10-30 09:45:39 +08:00
|
|
|
#include <locale>
|
2019-02-12 07:13:08 +08:00
|
|
|
#include <memory>
|
2014-10-30 09:45:39 +08:00
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
using namespace lldb_private::formatters;
|
2020-04-10 07:53:58 +08:00
|
|
|
using GetPrintableElementType = StringPrinter::GetPrintableElementType;
|
|
|
|
using StringElementType = StringPrinter::StringElementType;
|
|
|
|
|
|
|
|
/// DecodedCharBuffer stores the decoded contents of a single character. It
|
|
|
|
/// avoids managing memory on the heap by copying decoded bytes into an in-line
|
|
|
|
/// buffer.
|
2020-06-04 02:51:22 +08:00
|
|
|
class DecodedCharBuffer {
|
2020-04-10 07:53:58 +08:00
|
|
|
public:
|
|
|
|
DecodedCharBuffer(std::nullptr_t) {}
|
|
|
|
|
|
|
|
DecodedCharBuffer(const uint8_t *bytes, size_t size) : m_size(size) {
|
|
|
|
if (size > MaxLength)
|
|
|
|
llvm_unreachable("unsupported length");
|
|
|
|
memcpy(m_data, bytes, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
DecodedCharBuffer(const char *bytes, size_t size)
|
|
|
|
: DecodedCharBuffer(reinterpret_cast<const uint8_t *>(bytes), size) {}
|
|
|
|
|
|
|
|
const uint8_t *GetBytes() const { return m_data; }
|
|
|
|
|
|
|
|
size_t GetSize() const { return m_size; }
|
|
|
|
|
|
|
|
private:
|
2020-06-04 02:51:22 +08:00
|
|
|
static constexpr unsigned MaxLength = 16;
|
|
|
|
|
2020-04-10 07:53:58 +08:00
|
|
|
size_t m_size = 0;
|
|
|
|
uint8_t m_data[MaxLength] = {0};
|
|
|
|
};
|
|
|
|
|
|
|
|
using EscapingHelper =
|
|
|
|
std::function<DecodedCharBuffer(uint8_t *, uint8_t *, uint8_t *&)>;
|
2014-10-30 09:45:39 +08:00
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// we define this for all values of type but only implement it for those we
|
|
|
|
// care about that's good because we get linker errors for any unsupported type
|
2020-04-10 07:53:58 +08:00
|
|
|
template <StringElementType type>
|
|
|
|
static DecodedCharBuffer
|
|
|
|
GetPrintableImpl(uint8_t *buffer, uint8_t *buffer_end, uint8_t *&next,
|
|
|
|
StringPrinter::EscapeStyle escape_style);
|
2014-10-30 09:45:39 +08:00
|
|
|
|
2020-04-10 07:53:58 +08:00
|
|
|
// Mimic isprint() for Unicode codepoints.
|
|
|
|
static bool isprint32(char32_t codepoint) {
|
2014-10-30 09:45:39 +08:00
|
|
|
if (codepoint <= 0x1F || codepoint == 0x7F) // C0
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (codepoint >= 0x80 && codepoint <= 0x9F) // C1
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (codepoint == 0x2028 || codepoint == 0x2029) // line/paragraph separators
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (codepoint == 0x200E || codepoint == 0x200F ||
|
|
|
|
(codepoint >= 0x202A &&
|
|
|
|
codepoint <= 0x202E)) // bidirectional text control
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (codepoint >= 0xFFF9 &&
|
|
|
|
codepoint <= 0xFFFF) // interlinears and generally specials
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-04 02:52:29 +08:00
|
|
|
DecodedCharBuffer attemptASCIIEscape(llvm::UTF32 c,
|
2020-04-10 07:53:58 +08:00
|
|
|
StringPrinter::EscapeStyle escape_style) {
|
|
|
|
const bool is_swift_escape_style =
|
|
|
|
escape_style == StringPrinter::EscapeStyle::Swift;
|
|
|
|
switch (c) {
|
2014-12-19 03:43:29 +08:00
|
|
|
case 0:
|
2020-04-10 07:53:58 +08:00
|
|
|
return {"\\0", 2};
|
2014-10-30 09:45:39 +08:00
|
|
|
case '\a':
|
2020-04-10 07:53:58 +08:00
|
|
|
return {"\\a", 2};
|
2014-10-30 09:45:39 +08:00
|
|
|
case '\b':
|
2020-04-10 07:53:58 +08:00
|
|
|
if (is_swift_escape_style)
|
|
|
|
return nullptr;
|
|
|
|
return {"\\b", 2};
|
2014-10-30 09:45:39 +08:00
|
|
|
case '\f':
|
2020-04-10 07:53:58 +08:00
|
|
|
if (is_swift_escape_style)
|
|
|
|
return nullptr;
|
|
|
|
return {"\\f", 2};
|
2014-10-30 09:45:39 +08:00
|
|
|
case '\n':
|
2020-04-10 07:53:58 +08:00
|
|
|
return {"\\n", 2};
|
2014-10-30 09:45:39 +08:00
|
|
|
case '\r':
|
2020-04-10 07:53:58 +08:00
|
|
|
return {"\\r", 2};
|
2014-10-30 09:45:39 +08:00
|
|
|
case '\t':
|
2020-04-10 07:53:58 +08:00
|
|
|
return {"\\t", 2};
|
2014-10-30 09:45:39 +08:00
|
|
|
case '\v':
|
2020-04-10 07:53:58 +08:00
|
|
|
if (is_swift_escape_style)
|
|
|
|
return nullptr;
|
|
|
|
return {"\\v", 2};
|
2014-10-30 09:45:39 +08:00
|
|
|
case '\"':
|
2020-04-10 07:53:58 +08:00
|
|
|
return {"\\\"", 2};
|
|
|
|
case '\'':
|
|
|
|
if (is_swift_escape_style)
|
|
|
|
return {"\\'", 2};
|
|
|
|
return nullptr;
|
2014-10-30 09:45:39 +08:00
|
|
|
case '\\':
|
2020-04-10 07:53:58 +08:00
|
|
|
return {"\\\\", 2};
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2020-04-10 07:53:58 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2020-04-10 07:53:58 +08:00
|
|
|
template <>
|
|
|
|
DecodedCharBuffer GetPrintableImpl<StringElementType::ASCII>(
|
|
|
|
uint8_t *buffer, uint8_t *buffer_end, uint8_t *&next,
|
|
|
|
StringPrinter::EscapeStyle escape_style) {
|
|
|
|
// The ASCII helper always advances 1 byte at a time.
|
2014-10-30 09:45:39 +08:00
|
|
|
next = buffer + 1;
|
2020-04-10 07:53:58 +08:00
|
|
|
|
|
|
|
DecodedCharBuffer retval = attemptASCIIEscape(*buffer, escape_style);
|
|
|
|
if (retval.GetSize())
|
|
|
|
return retval;
|
2020-06-04 02:52:29 +08:00
|
|
|
|
|
|
|
// Use llvm's locale-independent isPrint(char), instead of the libc
|
|
|
|
// implementation which may give different results on different platforms.
|
|
|
|
if (llvm::isPrint(*buffer))
|
2020-04-10 07:53:58 +08:00
|
|
|
return {buffer, 1};
|
|
|
|
|
|
|
|
unsigned escaped_len;
|
|
|
|
constexpr unsigned max_buffer_size = 7;
|
|
|
|
uint8_t data[max_buffer_size];
|
|
|
|
switch (escape_style) {
|
|
|
|
case StringPrinter::EscapeStyle::CXX:
|
|
|
|
// Prints 4 characters, then a \0 terminator.
|
|
|
|
escaped_len = sprintf((char *)data, "\\x%02x", *buffer);
|
|
|
|
break;
|
|
|
|
case StringPrinter::EscapeStyle::Swift:
|
|
|
|
// Prints up to 6 characters, then a \0 terminator.
|
|
|
|
escaped_len = sprintf((char *)data, "\\u{%x}", *buffer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lldbassert(escaped_len > 0 && "unknown string escape style");
|
|
|
|
return {data, escaped_len};
|
2014-10-30 09:45:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2020-04-10 07:53:58 +08:00
|
|
|
DecodedCharBuffer GetPrintableImpl<StringElementType::UTF8>(
|
|
|
|
uint8_t *buffer, uint8_t *buffer_end, uint8_t *&next,
|
|
|
|
StringPrinter::EscapeStyle escape_style) {
|
2020-06-04 02:52:29 +08:00
|
|
|
// If the utf8 encoded length is invalid (i.e., not in the closed interval
|
|
|
|
// [1;4]), or if there aren't enough bytes to print, or if the subsequence
|
|
|
|
// isn't valid utf8, fall back to printing an ASCII-escaped subsequence.
|
|
|
|
if (!llvm::isLegalUTF8Sequence(buffer, buffer_end))
|
2020-04-10 07:53:58 +08:00
|
|
|
return GetPrintableImpl<StringElementType::ASCII>(buffer, buffer_end, next,
|
|
|
|
escape_style);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2020-06-04 02:52:29 +08:00
|
|
|
// Convert the valid utf8 sequence to a utf32 codepoint. This cannot fail.
|
|
|
|
llvm::UTF32 codepoint = 0;
|
|
|
|
const llvm::UTF8 *buffer_for_conversion = buffer;
|
|
|
|
llvm::ConversionResult result = llvm::convertUTF8Sequence(
|
|
|
|
&buffer_for_conversion, buffer_end, &codepoint, llvm::strictConversion);
|
|
|
|
assert(result == llvm::conversionOK &&
|
|
|
|
"Failed to convert legal utf8 sequence");
|
|
|
|
(void)result;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2020-04-10 07:53:58 +08:00
|
|
|
// The UTF8 helper always advances by the utf8 encoded length.
|
2020-06-04 02:52:29 +08:00
|
|
|
const unsigned utf8_encoded_len = buffer_for_conversion - buffer;
|
2020-04-10 07:53:58 +08:00
|
|
|
next = buffer + utf8_encoded_len;
|
2020-06-04 02:52:29 +08:00
|
|
|
|
2020-04-10 07:53:58 +08:00
|
|
|
DecodedCharBuffer retval = attemptASCIIEscape(codepoint, escape_style);
|
|
|
|
if (retval.GetSize())
|
2014-10-30 09:45:39 +08:00
|
|
|
return retval;
|
2020-04-10 07:53:58 +08:00
|
|
|
if (isprint32(codepoint))
|
|
|
|
return {buffer, utf8_encoded_len};
|
|
|
|
|
|
|
|
unsigned escaped_len;
|
|
|
|
constexpr unsigned max_buffer_size = 13;
|
|
|
|
uint8_t data[max_buffer_size];
|
|
|
|
switch (escape_style) {
|
|
|
|
case StringPrinter::EscapeStyle::CXX:
|
|
|
|
// Prints 10 characters, then a \0 terminator.
|
2020-06-04 02:52:29 +08:00
|
|
|
escaped_len = sprintf((char *)data, "\\U%08x", codepoint);
|
2020-04-10 07:53:58 +08:00
|
|
|
break;
|
|
|
|
case StringPrinter::EscapeStyle::Swift:
|
|
|
|
// Prints up to 12 characters, then a \0 terminator.
|
2020-06-04 02:52:29 +08:00
|
|
|
escaped_len = sprintf((char *)data, "\\u{%x}", codepoint);
|
2020-04-10 07:53:58 +08:00
|
|
|
break;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2020-04-10 07:53:58 +08:00
|
|
|
lldbassert(escaped_len > 0 && "unknown string escape style");
|
|
|
|
return {data, escaped_len};
|
2014-10-30 09:45:39 +08:00
|
|
|
}
|
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Given a sequence of bytes, this function returns: a sequence of bytes to
|
|
|
|
// actually print out + a length the following unscanned position of the buffer
|
|
|
|
// is in next
|
2020-04-10 07:53:58 +08:00
|
|
|
static DecodedCharBuffer GetPrintable(StringElementType type, uint8_t *buffer,
|
|
|
|
uint8_t *buffer_end, uint8_t *&next,
|
|
|
|
StringPrinter::EscapeStyle escape_style) {
|
2020-02-01 15:18:17 +08:00
|
|
|
if (!buffer || buffer >= buffer_end)
|
2014-10-30 09:45:39 +08:00
|
|
|
return {nullptr};
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-30 09:45:39 +08:00
|
|
|
switch (type) {
|
2020-04-10 07:53:58 +08:00
|
|
|
case StringElementType::ASCII:
|
|
|
|
return GetPrintableImpl<StringElementType::ASCII>(buffer, buffer_end, next,
|
|
|
|
escape_style);
|
|
|
|
case StringElementType::UTF8:
|
|
|
|
return GetPrintableImpl<StringElementType::UTF8>(buffer, buffer_end, next,
|
|
|
|
escape_style);
|
2014-10-30 09:45:39 +08:00
|
|
|
default:
|
|
|
|
return {nullptr};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 07:53:58 +08:00
|
|
|
static EscapingHelper
|
|
|
|
GetDefaultEscapingHelper(GetPrintableElementType elem_type,
|
|
|
|
StringPrinter::EscapeStyle escape_style) {
|
2015-09-10 06:30:24 +08:00
|
|
|
switch (elem_type) {
|
|
|
|
case GetPrintableElementType::UTF8:
|
|
|
|
case GetPrintableElementType::ASCII:
|
2020-04-10 07:53:58 +08:00
|
|
|
return [escape_style, elem_type](uint8_t *buffer, uint8_t *buffer_end,
|
|
|
|
uint8_t *&next) -> DecodedCharBuffer {
|
|
|
|
return GetPrintable(elem_type == GetPrintableElementType::UTF8
|
|
|
|
? StringElementType::UTF8
|
|
|
|
: StringElementType::ASCII,
|
|
|
|
buffer, buffer_end, next, escape_style);
|
2015-09-10 06:30:24 +08:00
|
|
|
};
|
|
|
|
}
|
2015-10-19 04:51:18 +08:00
|
|
|
llvm_unreachable("bad element type");
|
2015-09-10 06:30:24 +08:00
|
|
|
}
|
|
|
|
|
2020-04-10 07:53:58 +08:00
|
|
|
/// Read a string encoded in accordance with \tparam SourceDataType from a
|
|
|
|
/// host-side LLDB buffer, then pretty-print it to a stream using \p style.
|
2014-10-30 09:45:39 +08:00
|
|
|
template <typename SourceDataType>
|
2020-04-10 07:53:58 +08:00
|
|
|
static bool DumpEncodedBufferToStream(
|
|
|
|
GetPrintableElementType style,
|
2016-09-30 08:38:45 +08:00
|
|
|
llvm::ConversionResult (*ConvertFunction)(const SourceDataType **,
|
|
|
|
const SourceDataType *,
|
|
|
|
llvm::UTF8 **, llvm::UTF8 *,
|
|
|
|
llvm::ConversionFlags),
|
2015-09-10 06:30:24 +08:00
|
|
|
const StringPrinter::ReadBufferAndDumpToStreamOptions &dump_options) {
|
2020-04-10 07:53:58 +08:00
|
|
|
assert(dump_options.GetStream() && "need a Stream to print the string to");
|
2015-07-17 09:03:59 +08:00
|
|
|
Stream &stream(*dump_options.GetStream());
|
[lldb] NFC modernize codebase with modernize-use-nullptr
Summary:
NFC = [[ https://llvm.org/docs/Lexicon.html#nfc | Non functional change ]]
This commit is the result of modernizing the LLDB codebase by using
`nullptr` instread of `0` or `NULL`. See
https://clang.llvm.org/extra/clang-tidy/checks/modernize-use-nullptr.html
for more information.
This is the command I ran and I to fix and format the code base:
```
run-clang-tidy.py \
-header-filter='.*' \
-checks='-*,modernize-use-nullptr' \
-fix ~/dev/llvm-project/lldb/.* \
-format \
-style LLVM \
-p ~/llvm-builds/debug-ninja-gcc
```
NOTE: There were also changes to `llvm/utils/unittest` but I did not
include them because I felt that maybe this library shall be updated in
isolation somehow.
NOTE: I know this is a rather large commit but it is a nobrainer in most
parts.
Reviewers: martong, espindola, shafik, #lldb, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: arsenm, jvesely, nhaehnle, hiraditya, JDevlieghere, teemperor, rnkovacs, emaste, kubamracek, nemanjai, ki.stfu, javed.absar, arichardson, kbarton, jrtc27, MaskRay, atanasyan, dexonsmith, arphaman, jfb, jsji, jdoerfert, lldb-commits, llvm-commits
Tags: #lldb, #llvm
Differential Revision: https://reviews.llvm.org/D61847
llvm-svn: 361484
2019-05-23 19:14:47 +08:00
|
|
|
if (dump_options.GetPrefixToken() != nullptr)
|
2015-10-07 10:06:48 +08:00
|
|
|
stream.Printf("%s", dump_options.GetPrefixToken());
|
2015-07-17 09:03:59 +08:00
|
|
|
if (dump_options.GetQuote() != 0)
|
|
|
|
stream.Printf("%c", dump_options.GetQuote());
|
|
|
|
auto data(dump_options.GetData());
|
2015-05-13 08:25:54 +08:00
|
|
|
auto source_size(dump_options.GetSourceSize());
|
|
|
|
if (data.GetByteSize() && data.GetDataStart() && data.GetDataEnd()) {
|
2015-07-17 09:03:59 +08:00
|
|
|
const int bufferSPSize = data.GetByteSize();
|
|
|
|
if (dump_options.GetSourceSize() == 0) {
|
|
|
|
const int origin_encoding = 8 * sizeof(SourceDataType);
|
|
|
|
source_size = bufferSPSize / (origin_encoding / 4);
|
2014-10-30 09:45:39 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-07-17 09:03:59 +08:00
|
|
|
const SourceDataType *data_ptr =
|
|
|
|
(const SourceDataType *)data.GetDataStart();
|
2014-10-30 09:45:39 +08:00
|
|
|
const SourceDataType *data_end_ptr = data_ptr + source_size;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-07-17 09:03:59 +08:00
|
|
|
const bool zero_is_terminator = dump_options.GetBinaryZeroIsTerminator();
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-10 06:30:24 +08:00
|
|
|
if (zero_is_terminator) {
|
|
|
|
while (data_ptr < data_end_ptr) {
|
|
|
|
if (!*data_ptr) {
|
|
|
|
data_end_ptr = data_ptr;
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
2015-09-10 06:30:24 +08:00
|
|
|
}
|
2014-10-30 09:45:39 +08:00
|
|
|
data_ptr++;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2014-10-30 09:45:39 +08:00
|
|
|
data_ptr = (const SourceDataType *)data.GetDataStart();
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2014-10-30 09:45:39 +08:00
|
|
|
lldb::DataBufferSP utf8_data_buffer_sp;
|
2016-09-30 08:38:45 +08:00
|
|
|
llvm::UTF8 *utf8_data_ptr = nullptr;
|
|
|
|
llvm::UTF8 *utf8_data_end_ptr = nullptr;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-30 09:45:39 +08:00
|
|
|
if (ConvertFunction) {
|
2019-02-12 07:13:08 +08:00
|
|
|
utf8_data_buffer_sp =
|
|
|
|
std::make_shared<DataBufferHeap>(4 * bufferSPSize, 0);
|
2016-09-30 08:38:45 +08:00
|
|
|
utf8_data_ptr = (llvm::UTF8 *)utf8_data_buffer_sp->GetBytes();
|
2015-09-10 06:30:24 +08:00
|
|
|
utf8_data_end_ptr = utf8_data_ptr + utf8_data_buffer_sp->GetByteSize();
|
|
|
|
ConvertFunction(&data_ptr, data_end_ptr, &utf8_data_ptr,
|
2016-09-30 08:38:45 +08:00
|
|
|
utf8_data_end_ptr, llvm::lenientConversion);
|
2018-12-15 08:15:33 +08:00
|
|
|
if (!zero_is_terminator)
|
2014-10-30 09:45:39 +08:00
|
|
|
utf8_data_end_ptr = utf8_data_ptr;
|
2016-09-30 08:38:45 +08:00
|
|
|
// needed because the ConvertFunction will change the value of the
|
|
|
|
// data_ptr.
|
2014-10-30 09:45:39 +08:00
|
|
|
utf8_data_ptr =
|
2016-09-30 08:38:45 +08:00
|
|
|
(llvm::UTF8 *)utf8_data_buffer_sp->GetBytes();
|
2016-09-07 04:57:50 +08:00
|
|
|
} else {
|
2014-10-30 09:45:39 +08:00
|
|
|
// just copy the pointers - the cast is necessary to make the compiler
|
2018-05-01 00:49:04 +08:00
|
|
|
// happy but this should only happen if we are reading UTF8 data
|
2016-09-30 08:38:45 +08:00
|
|
|
utf8_data_ptr = const_cast<llvm::UTF8 *>(
|
|
|
|
reinterpret_cast<const llvm::UTF8 *>(data_ptr));
|
|
|
|
utf8_data_end_ptr = const_cast<llvm::UTF8 *>(
|
|
|
|
reinterpret_cast<const llvm::UTF8 *>(data_end_ptr));
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2015-09-10 06:30:24 +08:00
|
|
|
const bool escape_non_printables = dump_options.GetEscapeNonPrintables();
|
2020-04-10 07:53:58 +08:00
|
|
|
EscapingHelper escaping_callback;
|
|
|
|
if (escape_non_printables)
|
|
|
|
escaping_callback =
|
|
|
|
GetDefaultEscapingHelper(style, dump_options.GetEscapeStyle());
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-30 09:45:39 +08:00
|
|
|
// since we tend to accept partial data (and even partially malformed data)
|
2018-05-01 00:49:04 +08:00
|
|
|
// we might end up with no NULL terminator before the end_ptr hence we need
|
|
|
|
// to take a slower route and ensure we stay within boundaries
|
2014-10-30 09:45:39 +08:00
|
|
|
for (; utf8_data_ptr < utf8_data_end_ptr;) {
|
2015-07-17 09:03:59 +08:00
|
|
|
if (zero_is_terminator && !*utf8_data_ptr)
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
|
|
|
|
2014-10-30 09:45:39 +08:00
|
|
|
if (escape_non_printables) {
|
|
|
|
uint8_t *next_data = nullptr;
|
|
|
|
auto printable =
|
|
|
|
escaping_callback(utf8_data_ptr, utf8_data_end_ptr, next_data);
|
|
|
|
auto printable_bytes = printable.GetBytes();
|
|
|
|
auto printable_size = printable.GetSize();
|
2020-02-01 15:18:17 +08:00
|
|
|
|
|
|
|
// We failed to figure out how to print this string.
|
|
|
|
if (!printable_bytes || !next_data)
|
|
|
|
return false;
|
|
|
|
|
2014-12-29 21:03:19 +08:00
|
|
|
for (unsigned c = 0; c < printable_size; c++)
|
2014-10-30 09:45:39 +08:00
|
|
|
stream.Printf("%c", *(printable_bytes + c));
|
|
|
|
utf8_data_ptr = (uint8_t *)next_data;
|
2016-09-07 04:57:50 +08:00
|
|
|
} else {
|
2014-10-30 09:45:39 +08:00
|
|
|
stream.Printf("%c", *utf8_data_ptr);
|
|
|
|
utf8_data_ptr++;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-10-30 09:45:39 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2015-07-17 09:03:59 +08:00
|
|
|
if (dump_options.GetQuote() != 0)
|
|
|
|
stream.Printf("%c", dump_options.GetQuote());
|
[lldb] NFC modernize codebase with modernize-use-nullptr
Summary:
NFC = [[ https://llvm.org/docs/Lexicon.html#nfc | Non functional change ]]
This commit is the result of modernizing the LLDB codebase by using
`nullptr` instread of `0` or `NULL`. See
https://clang.llvm.org/extra/clang-tidy/checks/modernize-use-nullptr.html
for more information.
This is the command I ran and I to fix and format the code base:
```
run-clang-tidy.py \
-header-filter='.*' \
-checks='-*,modernize-use-nullptr' \
-fix ~/dev/llvm-project/lldb/.* \
-format \
-style LLVM \
-p ~/llvm-builds/debug-ninja-gcc
```
NOTE: There were also changes to `llvm/utils/unittest` but I did not
include them because I felt that maybe this library shall be updated in
isolation somehow.
NOTE: I know this is a rather large commit but it is a nobrainer in most
parts.
Reviewers: martong, espindola, shafik, #lldb, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: arsenm, jvesely, nhaehnle, hiraditya, JDevlieghere, teemperor, rnkovacs, emaste, kubamracek, nemanjai, ki.stfu, javed.absar, arichardson, kbarton, jrtc27, MaskRay, atanasyan, dexonsmith, arphaman, jfb, jsji, jdoerfert, lldb-commits, llvm-commits
Tags: #lldb, #llvm
Differential Revision: https://reviews.llvm.org/D61847
llvm-svn: 361484
2019-05-23 19:14:47 +08:00
|
|
|
if (dump_options.GetSuffixToken() != nullptr)
|
2015-10-07 10:06:48 +08:00
|
|
|
stream.Printf("%s", dump_options.GetSuffixToken());
|
2015-11-04 08:02:08 +08:00
|
|
|
if (dump_options.GetIsTruncated())
|
|
|
|
stream.Printf("...");
|
2014-10-30 09:45:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-10 06:30:24 +08:00
|
|
|
lldb_private::formatters::StringPrinter::ReadStringAndDumpToStreamOptions::
|
|
|
|
ReadStringAndDumpToStreamOptions(ValueObject &valobj)
|
2014-11-06 05:20:48 +08:00
|
|
|
: ReadStringAndDumpToStreamOptions() {
|
|
|
|
SetEscapeNonPrintables(
|
|
|
|
valobj.GetTargetSP()->GetDebugger().GetEscapeNonPrintables());
|
|
|
|
}
|
|
|
|
|
2015-09-10 06:30:24 +08:00
|
|
|
lldb_private::formatters::StringPrinter::ReadBufferAndDumpToStreamOptions::
|
|
|
|
ReadBufferAndDumpToStreamOptions(ValueObject &valobj)
|
2014-11-06 05:20:48 +08:00
|
|
|
: ReadBufferAndDumpToStreamOptions() {
|
|
|
|
SetEscapeNonPrintables(
|
|
|
|
valobj.GetTargetSP()->GetDebugger().GetEscapeNonPrintables());
|
|
|
|
}
|
|
|
|
|
2015-09-10 06:30:24 +08:00
|
|
|
lldb_private::formatters::StringPrinter::ReadBufferAndDumpToStreamOptions::
|
|
|
|
ReadBufferAndDumpToStreamOptions(
|
|
|
|
const ReadStringAndDumpToStreamOptions &options)
|
2015-07-17 09:03:59 +08:00
|
|
|
: ReadBufferAndDumpToStreamOptions() {
|
|
|
|
SetStream(options.GetStream());
|
|
|
|
SetPrefixToken(options.GetPrefixToken());
|
2015-10-07 10:06:48 +08:00
|
|
|
SetSuffixToken(options.GetSuffixToken());
|
2015-07-17 09:03:59 +08:00
|
|
|
SetQuote(options.GetQuote());
|
|
|
|
SetEscapeNonPrintables(options.GetEscapeNonPrintables());
|
|
|
|
SetBinaryZeroIsTerminator(options.GetBinaryZeroIsTerminator());
|
2020-04-10 07:53:58 +08:00
|
|
|
SetEscapeStyle(options.GetEscapeStyle());
|
2015-07-17 09:03:59 +08:00
|
|
|
}
|
|
|
|
|
2014-11-05 06:43:34 +08:00
|
|
|
namespace lldb_private {
|
2014-11-06 05:20:48 +08:00
|
|
|
|
2014-11-05 06:43:34 +08:00
|
|
|
namespace formatters {
|
|
|
|
|
2014-10-30 09:45:39 +08:00
|
|
|
template <typename SourceDataType>
|
2020-04-10 07:53:58 +08:00
|
|
|
static bool ReadEncodedBufferAndDumpToStream(
|
|
|
|
StringElementType elem_type,
|
2015-09-10 06:30:24 +08:00
|
|
|
const StringPrinter::ReadStringAndDumpToStreamOptions &options,
|
2016-09-30 08:38:45 +08:00
|
|
|
llvm::ConversionResult (*ConvertFunction)(const SourceDataType **,
|
|
|
|
const SourceDataType *,
|
|
|
|
llvm::UTF8 **, llvm::UTF8 *,
|
|
|
|
llvm::ConversionFlags)) {
|
2014-10-30 09:45:39 +08:00
|
|
|
assert(options.GetStream() && "need a Stream to print the string to");
|
2020-04-10 07:53:58 +08:00
|
|
|
if (!options.GetStream())
|
|
|
|
return false;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-30 09:45:39 +08:00
|
|
|
if (options.GetLocation() == 0 ||
|
|
|
|
options.GetLocation() == LLDB_INVALID_ADDRESS)
|
|
|
|
return false;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-30 09:45:39 +08:00
|
|
|
lldb::ProcessSP process_sp(options.GetProcessSP());
|
|
|
|
if (!process_sp)
|
|
|
|
return false;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2020-04-10 07:53:58 +08:00
|
|
|
constexpr int type_width = sizeof(SourceDataType);
|
|
|
|
constexpr int origin_encoding = 8 * type_width;
|
2014-10-30 09:45:39 +08:00
|
|
|
if (origin_encoding != 8 && origin_encoding != 16 && origin_encoding != 32)
|
|
|
|
return false;
|
2020-04-10 07:53:58 +08:00
|
|
|
// If not UTF8 or ASCII, conversion to UTF8 is necessary.
|
2014-10-30 09:45:39 +08:00
|
|
|
if (origin_encoding != 8 && !ConvertFunction)
|
|
|
|
return false;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-30 09:45:39 +08:00
|
|
|
bool needs_zero_terminator = options.GetNeedsZeroTermination();
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-11-04 08:02:08 +08:00
|
|
|
bool is_truncated = false;
|
|
|
|
const auto max_size = process_sp->GetTarget().GetMaximumSizeOfStringSummary();
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2020-04-10 07:53:58 +08:00
|
|
|
uint32_t sourceSize;
|
|
|
|
if (elem_type == StringElementType::ASCII && !options.GetSourceSize()) {
|
|
|
|
// FIXME: The NSString formatter sets HasSourceSize(true) when the size is
|
|
|
|
// actually unknown, as well as SetBinaryZeroIsTerminator(false). IIUC the
|
|
|
|
// C++ formatter also sets SetBinaryZeroIsTerminator(false) when it doesn't
|
|
|
|
// mean to. I don't see how this makes sense: we should fix the formatters.
|
|
|
|
//
|
|
|
|
// Until then, the behavior that's expected for ASCII strings with unknown
|
|
|
|
// lengths is to read up to the max size and then null-terminate. Do that.
|
|
|
|
sourceSize = max_size;
|
|
|
|
needs_zero_terminator = true;
|
|
|
|
} else if (options.HasSourceSize()) {
|
2020-03-19 19:20:18 +08:00
|
|
|
sourceSize = options.GetSourceSize();
|
|
|
|
if (!options.GetIgnoreMaxLength()) {
|
|
|
|
if (sourceSize > max_size) {
|
|
|
|
sourceSize = max_size;
|
|
|
|
is_truncated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-11-04 08:02:08 +08:00
|
|
|
sourceSize = max_size;
|
2014-10-30 09:45:39 +08:00
|
|
|
needs_zero_terminator = true;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-11-05 06:43:34 +08:00
|
|
|
|
2014-10-30 09:45:39 +08:00
|
|
|
const int bufferSPSize = sourceSize * type_width;
|
|
|
|
lldb::DataBufferSP buffer_sp(new DataBufferHeap(bufferSPSize, 0));
|
2014-11-05 06:43:34 +08:00
|
|
|
|
2020-03-19 19:20:18 +08:00
|
|
|
// Check if we got bytes. We never get any bytes if we have an empty
|
|
|
|
// string, but we still continue so that we end up actually printing
|
|
|
|
// an empty string ("").
|
|
|
|
if (sourceSize != 0 && !buffer_sp->GetBytes())
|
2014-10-30 09:45:39 +08:00
|
|
|
return false;
|
2014-11-05 06:43:34 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error;
|
2014-10-30 09:45:39 +08:00
|
|
|
char *buffer = reinterpret_cast<char *>(buffer_sp->GetBytes());
|
2014-11-05 06:43:34 +08:00
|
|
|
|
2020-04-10 07:53:58 +08:00
|
|
|
if (elem_type == StringElementType::ASCII)
|
|
|
|
process_sp->ReadCStringFromMemory(options.GetLocation(), buffer,
|
|
|
|
bufferSPSize, error);
|
|
|
|
else if (needs_zero_terminator)
|
2015-05-13 08:25:54 +08:00
|
|
|
process_sp->ReadStringFromMemory(options.GetLocation(), buffer,
|
|
|
|
bufferSPSize, error, type_width);
|
2014-10-30 09:45:39 +08:00
|
|
|
else
|
2020-04-10 07:53:58 +08:00
|
|
|
process_sp->ReadMemoryFromInferior(options.GetLocation(), buffer,
|
2015-05-13 08:25:54 +08:00
|
|
|
bufferSPSize, error);
|
2014-11-18 07:14:11 +08:00
|
|
|
if (error.Fail()) {
|
2014-10-30 09:45:39 +08:00
|
|
|
options.GetStream()->Printf("unable to read data");
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-10 06:30:24 +08:00
|
|
|
StringPrinter::ReadBufferAndDumpToStreamOptions dump_options(options);
|
2021-08-05 02:34:21 +08:00
|
|
|
dump_options.SetData(DataExtractor(buffer_sp, process_sp->GetByteOrder(),
|
|
|
|
process_sp->GetAddressByteSize()));
|
2015-07-17 09:03:59 +08:00
|
|
|
dump_options.SetSourceSize(sourceSize);
|
2015-11-04 08:02:08 +08:00
|
|
|
dump_options.SetIsTruncated(is_truncated);
|
2020-04-10 07:53:58 +08:00
|
|
|
dump_options.SetNeedsZeroTermination(needs_zero_terminator);
|
|
|
|
if (needs_zero_terminator)
|
|
|
|
dump_options.SetBinaryZeroIsTerminator(true);
|
2014-11-05 06:43:34 +08:00
|
|
|
|
2020-04-10 07:53:58 +08:00
|
|
|
GetPrintableElementType print_style = (elem_type == StringElementType::ASCII)
|
|
|
|
? GetPrintableElementType::ASCII
|
|
|
|
: GetPrintableElementType::UTF8;
|
|
|
|
return DumpEncodedBufferToStream(print_style, ConvertFunction, dump_options);
|
2014-10-30 09:45:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2020-04-10 07:53:58 +08:00
|
|
|
bool StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF8>(
|
2015-09-10 06:30:24 +08:00
|
|
|
const ReadStringAndDumpToStreamOptions &options) {
|
2020-04-10 07:53:58 +08:00
|
|
|
return ReadEncodedBufferAndDumpToStream<llvm::UTF8>(StringElementType::UTF8,
|
|
|
|
options, nullptr);
|
2014-10-30 09:45:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2020-04-10 07:53:58 +08:00
|
|
|
bool StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF16>(
|
2015-09-10 06:30:24 +08:00
|
|
|
const ReadStringAndDumpToStreamOptions &options) {
|
2020-04-10 07:53:58 +08:00
|
|
|
return ReadEncodedBufferAndDumpToStream<llvm::UTF16>(
|
|
|
|
StringElementType::UTF16, options, llvm::ConvertUTF16toUTF8);
|
2014-10-30 09:45:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2020-04-10 07:53:58 +08:00
|
|
|
bool StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF32>(
|
2015-09-10 06:30:24 +08:00
|
|
|
const ReadStringAndDumpToStreamOptions &options) {
|
2020-04-10 07:53:58 +08:00
|
|
|
return ReadEncodedBufferAndDumpToStream<llvm::UTF32>(
|
|
|
|
StringElementType::UTF32, options, llvm::ConvertUTF32toUTF8);
|
2014-10-30 09:45:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2020-04-10 07:53:58 +08:00
|
|
|
bool StringPrinter::ReadStringAndDumpToStream<StringElementType::ASCII>(
|
|
|
|
const ReadStringAndDumpToStreamOptions &options) {
|
|
|
|
return ReadEncodedBufferAndDumpToStream<char>(StringElementType::ASCII,
|
|
|
|
options, nullptr);
|
2014-10-30 09:45:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2020-04-10 07:53:58 +08:00
|
|
|
bool StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF8>(
|
2015-09-10 06:30:24 +08:00
|
|
|
const ReadBufferAndDumpToStreamOptions &options) {
|
2020-04-10 07:53:58 +08:00
|
|
|
return DumpEncodedBufferToStream<llvm::UTF8>(GetPrintableElementType::UTF8,
|
|
|
|
nullptr, options);
|
2014-10-30 09:45:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2020-04-10 07:53:58 +08:00
|
|
|
bool StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF16>(
|
2015-09-10 06:30:24 +08:00
|
|
|
const ReadBufferAndDumpToStreamOptions &options) {
|
2020-04-10 07:53:58 +08:00
|
|
|
return DumpEncodedBufferToStream(GetPrintableElementType::UTF8,
|
|
|
|
llvm::ConvertUTF16toUTF8, options);
|
2014-10-30 09:45:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2020-04-10 07:53:58 +08:00
|
|
|
bool StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF32>(
|
2015-09-10 06:30:24 +08:00
|
|
|
const ReadBufferAndDumpToStreamOptions &options) {
|
2020-04-10 07:53:58 +08:00
|
|
|
return DumpEncodedBufferToStream(GetPrintableElementType::UTF8,
|
|
|
|
llvm::ConvertUTF32toUTF8, options);
|
|
|
|
}
|
2014-11-05 06:43:34 +08:00
|
|
|
|
2020-04-10 07:53:58 +08:00
|
|
|
template <>
|
|
|
|
bool StringPrinter::ReadBufferAndDumpToStream<StringElementType::ASCII>(
|
|
|
|
const ReadBufferAndDumpToStreamOptions &options) {
|
|
|
|
// Treat ASCII the same as UTF8.
|
|
|
|
//
|
|
|
|
// FIXME: This is probably not the right thing to do (well, it's debatable).
|
|
|
|
// If an ASCII-encoded string happens to contain a sequence of invalid bytes
|
|
|
|
// that forms a valid UTF8 character, we'll print out that character. This is
|
|
|
|
// good if you're playing fast and loose with encodings (probably good for
|
|
|
|
// std::string users), but maybe not so good if you care about your string
|
|
|
|
// formatter respecting the semantics of your selected string encoding. In
|
|
|
|
// the latter case you'd want to see the character byte sequence ('\x..'), not
|
|
|
|
// the UTF8 character itself.
|
|
|
|
return ReadBufferAndDumpToStream<StringElementType::UTF8>(options);
|
2014-10-30 09:45:39 +08:00
|
|
|
}
|
2014-11-05 06:43:34 +08:00
|
|
|
|
|
|
|
} // namespace formatters
|
|
|
|
|
|
|
|
} // namespace lldb_private
|