Preparatory work for letting language plugins help the StringPrinter with formatting special characters

llvm-svn: 247189
This commit is contained in:
Enrico Granata
2015-09-09 20:59:49 +00:00
parent 22ee1a7466
commit ad650a189c
6 changed files with 130 additions and 125 deletions

View File

@@ -19,99 +19,16 @@
#include "llvm/Support/ConvertUTF.h"
#include <ctype.h>
#include <functional>
#include <locale>
using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::formatters;
// I can't use a std::unique_ptr for this because the Deleter is a template argument there
// and I want the same type to represent both pointers I want to free and pointers I don't need
// to free - which is what this class essentially is
// It's very specialized to the needs of this file, and not suggested for general use
template <typename T = uint8_t, typename U = char, typename S = size_t>
struct StringPrinterBufferPointer
{
public:
typedef std::function<void(const T*)> Deleter;
StringPrinterBufferPointer (std::nullptr_t ptr) :
m_data(nullptr),
m_size(0),
m_deleter()
{}
StringPrinterBufferPointer(const T* bytes, S size, Deleter deleter = nullptr) :
m_data(bytes),
m_size(size),
m_deleter(deleter)
{}
StringPrinterBufferPointer(const U* bytes, S size, Deleter deleter = nullptr) :
m_data((T*)bytes),
m_size(size),
m_deleter(deleter)
{}
StringPrinterBufferPointer(StringPrinterBufferPointer&& rhs) :
m_data(rhs.m_data),
m_size(rhs.m_size),
m_deleter(rhs.m_deleter)
{
rhs.m_data = nullptr;
}
StringPrinterBufferPointer(const StringPrinterBufferPointer& rhs) :
m_data(rhs.m_data),
m_size(rhs.m_size),
m_deleter(rhs.m_deleter)
{
rhs.m_data = nullptr; // this is why m_data has to be mutable
}
const T*
GetBytes () const
{
return m_data;
}
const S
GetSize () const
{
return m_size;
}
~StringPrinterBufferPointer ()
{
if (m_data && m_deleter)
m_deleter(m_data);
m_data = nullptr;
}
StringPrinterBufferPointer&
operator = (const StringPrinterBufferPointer& rhs)
{
if (m_data && m_deleter)
m_deleter(m_data);
m_data = rhs.m_data;
m_size = rhs.m_size;
m_deleter = rhs.m_deleter;
rhs.m_data = nullptr;
return *this;
}
private:
mutable const T* m_data;
size_t m_size;
Deleter m_deleter;
};
// 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
template <StringElementType type>
static StringPrinterBufferPointer<>
static StringPrinter::StringPrinterBufferPointer<>
GetPrintableImpl(uint8_t* buffer, uint8_t* buffer_end, uint8_t*& next);
// mimic isprint() for Unicode codepoints
@@ -142,10 +59,10 @@ isprint(char32_t codepoint)
}
template <>
StringPrinterBufferPointer<>
StringPrinter::StringPrinterBufferPointer<>
GetPrintableImpl<StringElementType::ASCII> (uint8_t* buffer, uint8_t* buffer_end, uint8_t*& next)
{
StringPrinterBufferPointer<> retval = {nullptr};
StringPrinter::StringPrinterBufferPointer<> retval = {nullptr};
switch (*buffer)
{
@@ -212,10 +129,10 @@ ConvertUTF8ToCodePoint (unsigned char c0, unsigned char c1, unsigned char c2, un
}
template <>
StringPrinterBufferPointer<>
StringPrinter::StringPrinterBufferPointer<>
GetPrintableImpl<StringElementType::UTF8> (uint8_t* buffer, uint8_t* buffer_end, uint8_t*& next)
{
StringPrinterBufferPointer<> retval {nullptr};
StringPrinter::StringPrinterBufferPointer<> retval {nullptr};
unsigned utf8_encoded_len = getNumBytesForUTF8(*buffer);
@@ -309,7 +226,7 @@ GetPrintableImpl<StringElementType::UTF8> (uint8_t* buffer, uint8_t* buffer_end,
// 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
static StringPrinterBufferPointer<>
static StringPrinter::StringPrinterBufferPointer<>
GetPrintable(StringElementType type, uint8_t* buffer, uint8_t* buffer_end, uint8_t*& next)
{
if (!buffer)
@@ -464,7 +381,7 @@ namespace formatters
template <>
bool
ReadStringAndDumpToStream<StringElementType::ASCII> (const ReadStringAndDumpToStreamOptions& options)
StringPrinter::ReadStringAndDumpToStream<StringElementType::ASCII> (const ReadStringAndDumpToStreamOptions& options)
{
assert(options.GetStream() && "need a Stream to print the string to");
Error my_error;
@@ -608,7 +525,7 @@ ReadUTFBufferAndDumpToStream (const ReadStringAndDumpToStreamOptions& options,
template <>
bool
ReadStringAndDumpToStream<StringElementType::UTF8> (const ReadStringAndDumpToStreamOptions& options)
StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF8> (const ReadStringAndDumpToStreamOptions& options)
{
return ReadUTFBufferAndDumpToStream<UTF8>(options,
nullptr);
@@ -616,7 +533,7 @@ ReadStringAndDumpToStream<StringElementType::UTF8> (const ReadStringAndDumpToStr
template <>
bool
ReadStringAndDumpToStream<StringElementType::UTF16> (const ReadStringAndDumpToStreamOptions& options)
StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF16> (const ReadStringAndDumpToStreamOptions& options)
{
return ReadUTFBufferAndDumpToStream<UTF16>(options,
ConvertUTF16toUTF8);
@@ -624,7 +541,7 @@ ReadStringAndDumpToStream<StringElementType::UTF16> (const ReadStringAndDumpToSt
template <>
bool
ReadStringAndDumpToStream<StringElementType::UTF32> (const ReadStringAndDumpToStreamOptions& options)
StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF32> (const ReadStringAndDumpToStreamOptions& options)
{
return ReadUTFBufferAndDumpToStream<UTF32>(options,
ConvertUTF32toUTF8);
@@ -632,7 +549,7 @@ ReadStringAndDumpToStream<StringElementType::UTF32> (const ReadStringAndDumpToSt
template <>
bool
ReadBufferAndDumpToStream<StringElementType::UTF8> (const ReadBufferAndDumpToStreamOptions& options)
StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF8> (const ReadBufferAndDumpToStreamOptions& options)
{
assert(options.GetStream() && "need a Stream to print the string to");
@@ -641,7 +558,7 @@ ReadBufferAndDumpToStream<StringElementType::UTF8> (const ReadBufferAndDumpToStr
template <>
bool
ReadBufferAndDumpToStream<StringElementType::ASCII> (const ReadBufferAndDumpToStreamOptions& options)
StringPrinter::ReadBufferAndDumpToStream<StringElementType::ASCII> (const ReadBufferAndDumpToStreamOptions& options)
{
// treat ASCII the same as UTF8
// FIXME: can we optimize ASCII some more?
@@ -650,7 +567,7 @@ ReadBufferAndDumpToStream<StringElementType::ASCII> (const ReadBufferAndDumpToSt
template <>
bool
ReadBufferAndDumpToStream<StringElementType::UTF16> (const ReadBufferAndDumpToStreamOptions& options)
StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF16> (const ReadBufferAndDumpToStreamOptions& options)
{
assert(options.GetStream() && "need a Stream to print the string to");
@@ -659,7 +576,7 @@ ReadBufferAndDumpToStream<StringElementType::UTF16> (const ReadBufferAndDumpToSt
template <>
bool
ReadBufferAndDumpToStream<StringElementType::UTF32> (const ReadBufferAndDumpToStreamOptions& options)
StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF32> (const ReadBufferAndDumpToStreamOptions& options)
{
assert(options.GetStream() && "need a Stream to print the string to");