Index: lldb/source/Plugins/Language/CPlusPlus/CxxStringTypes.cpp =================================================================== --- lldb/source/Plugins/Language/CPlusPlus/CxxStringTypes.cpp +++ lldb/source/Plugins/Language/CPlusPlus/CxxStringTypes.cpp @@ -32,8 +32,25 @@ using namespace lldb_private; using namespace lldb_private::formatters; -bool lldb_private::formatters::Char8StringSummaryProvider( - ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) { +using StringElementType = StringPrinter::StringElementType; + +static constexpr std::pair getElementTraits(StringElementType ElemType) +{ + switch(ElemType) + { + case StringElementType::UTF8: + return std::make_pair("u8", lldb::eFormatUnicode8); + case StringElementType::UTF16: + return std::make_pair("u", lldb::eFormatUnicode16); + case StringElementType::UTF32: + return std::make_pair("U", lldb::eFormatUnicode32); + default: + return std::make_pair(nullptr, lldb::eFormatInvalid); + } +} + +template +static bool CharStringSummaryProvider(ValueObject &valobj, Stream &stream) { ProcessSP process_sp = valobj.GetProcessSP(); if (!process_sp) return false; @@ -46,65 +63,55 @@ options.SetLocation(valobj_addr); options.SetProcessSP(process_sp); options.SetStream(&stream); - options.SetPrefixToken("u8"); + options.SetPrefixToken(getElementTraits(ElemType).first); - if (!StringPrinter::ReadStringAndDumpToStream< - StringPrinter::StringElementType::UTF8>(options)) { + if (!StringPrinter::ReadStringAndDumpToStream(options)) stream.Printf("Summary Unavailable"); - return true; - } return true; } -bool lldb_private::formatters::Char16StringSummaryProvider( - ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) { - ProcessSP process_sp = valobj.GetProcessSP(); - if (!process_sp) - return false; +template +static bool CharSummaryProvider(ValueObject &valobj, Stream &stream) { + DataExtractor data; + Status error; + valobj.GetData(data, error); - lldb::addr_t valobj_addr = GetArrayAddressOrPointerValue(valobj); - if (valobj_addr == 0 || valobj_addr == LLDB_INVALID_ADDRESS) + if (error.Fail()) return false; - StringPrinter::ReadStringAndDumpToStreamOptions options(valobj); - options.SetLocation(valobj_addr); - options.SetProcessSP(process_sp); - options.SetStream(&stream); - options.SetPrefixToken("u"); + std::string value; + StringPrinter::ReadBufferAndDumpToStreamOptions options(valobj); - if (!StringPrinter::ReadStringAndDumpToStream< - StringPrinter::StringElementType::UTF16>(options)) { - stream.Printf("Summary Unavailable"); - return true; - } + constexpr auto ElemTraits = getElementTraits(ElemType); + valobj.GetValueAsCString(ElemTraits.second, value); - return true; -} + if (!value.empty()) + stream.Printf("%s ", value.c_str()); -bool lldb_private::formatters::Char32StringSummaryProvider( - ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) { - ProcessSP process_sp = valobj.GetProcessSP(); - if (!process_sp) - return false; + options.SetData(std::move(data)); + options.SetStream(&stream); + options.SetPrefixToken(ElemTraits.first); + options.SetQuote('\''); + options.SetSourceSize(1); + options.SetBinaryZeroIsTerminator(false); - lldb::addr_t valobj_addr = GetArrayAddressOrPointerValue(valobj); - if (valobj_addr == 0 || valobj_addr == LLDB_INVALID_ADDRESS) - return false; + return StringPrinter::ReadBufferAndDumpToStream(options); +} - StringPrinter::ReadStringAndDumpToStreamOptions options(valobj); - options.SetLocation(valobj_addr); - options.SetProcessSP(process_sp); - options.SetStream(&stream); - options.SetPrefixToken("U"); +bool lldb_private::formatters::Char8StringSummaryProvider( + ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) { + return CharStringSummaryProvider(valobj, stream); +} - if (!StringPrinter::ReadStringAndDumpToStream< - StringPrinter::StringElementType::UTF32>(options)) { - stream.Printf("Summary Unavailable"); - return true; - } +bool lldb_private::formatters::Char16StringSummaryProvider( + ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) { + return CharStringSummaryProvider(valobj, stream); +} - return true; +bool lldb_private::formatters::Char32StringSummaryProvider( + ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) { + return CharStringSummaryProvider(valobj, stream); } bool lldb_private::formatters::WCharStringSummaryProvider( @@ -139,13 +146,13 @@ switch (wchar_size) { case 8: return StringPrinter::ReadStringAndDumpToStream< - StringPrinter::StringElementType::UTF8>(options); + StringElementType::UTF8>(options); case 16: return StringPrinter::ReadStringAndDumpToStream< - StringPrinter::StringElementType::UTF16>(options); + StringElementType::UTF16>(options); case 32: return StringPrinter::ReadStringAndDumpToStream< - StringPrinter::StringElementType::UTF32>(options); + StringElementType::UTF32>(options); default: stream.Printf("size for wchar_t is not valid"); return true; @@ -155,80 +162,17 @@ bool lldb_private::formatters::Char8SummaryProvider( ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) { - DataExtractor data; - Status error; - valobj.GetData(data, error); - - if (error.Fail()) - return false; - - std::string value; - valobj.GetValueAsCString(lldb::eFormatUnicode8, value); - if (!value.empty()) - stream.Printf("%s ", value.c_str()); - - StringPrinter::ReadBufferAndDumpToStreamOptions options(valobj); - options.SetData(std::move(data)); - options.SetStream(&stream); - options.SetPrefixToken("u8"); - options.SetQuote('\''); - options.SetSourceSize(1); - options.SetBinaryZeroIsTerminator(false); - - return StringPrinter::ReadBufferAndDumpToStream< - StringPrinter::StringElementType::UTF8>(options); + return CharSummaryProvider(valobj, stream); } bool lldb_private::formatters::Char16SummaryProvider( ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) { - DataExtractor data; - Status error; - valobj.GetData(data, error); - - if (error.Fail()) - return false; - - std::string value; - valobj.GetValueAsCString(lldb::eFormatUnicode16, value); - if (!value.empty()) - stream.Printf("%s ", value.c_str()); - - StringPrinter::ReadBufferAndDumpToStreamOptions options(valobj); - options.SetData(std::move(data)); - options.SetStream(&stream); - options.SetPrefixToken("u"); - options.SetQuote('\''); - options.SetSourceSize(1); - options.SetBinaryZeroIsTerminator(false); - - return StringPrinter::ReadBufferAndDumpToStream< - StringPrinter::StringElementType::UTF16>(options); + return CharSummaryProvider(valobj, stream); } bool lldb_private::formatters::Char32SummaryProvider( ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) { - DataExtractor data; - Status error; - valobj.GetData(data, error); - - if (error.Fail()) - return false; - - std::string value; - valobj.GetValueAsCString(lldb::eFormatUnicode32, value); - if (!value.empty()) - stream.Printf("%s ", value.c_str()); - - StringPrinter::ReadBufferAndDumpToStreamOptions options(valobj); - options.SetData(std::move(data)); - options.SetStream(&stream); - options.SetPrefixToken("U"); - options.SetQuote('\''); - options.SetSourceSize(1); - options.SetBinaryZeroIsTerminator(false); - - return StringPrinter::ReadBufferAndDumpToStream< - StringPrinter::StringElementType::UTF32>(options); + return CharSummaryProvider(valobj, stream); } bool lldb_private::formatters::WCharSummaryProvider( @@ -264,13 +208,13 @@ switch (wchar_size) { case 8: return StringPrinter::ReadBufferAndDumpToStream< - StringPrinter::StringElementType::UTF8>(options); + StringElementType::UTF8>(options); case 16: return StringPrinter::ReadBufferAndDumpToStream< - StringPrinter::StringElementType::UTF16>(options); + StringElementType::UTF16>(options); case 32: return StringPrinter::ReadBufferAndDumpToStream< - StringPrinter::StringElementType::UTF32>(options); + StringElementType::UTF32>(options); default: stream.Printf("size for wchar_t is not valid"); return true;