Index: lldb/source/Plugins/Language/CPlusPlus/CxxStringTypes.cpp =================================================================== --- lldb/source/Plugins/Language/CPlusPlus/CxxStringTypes.cpp +++ lldb/source/Plugins/Language/CPlusPlus/CxxStringTypes.cpp @@ -32,79 +32,86 @@ using namespace lldb_private; using namespace lldb_private::formatters; -bool lldb_private::formatters::Char8StringSummaryProvider( - ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) { - ProcessSP process_sp = valobj.GetProcessSP(); - if (!process_sp) - return false; +namespace { + + template + static bool CharStringSummaryProvider(ValueObject &valobj, Stream &stream) { + ProcessSP process_sp = valobj.GetProcessSP(); + if (!process_sp) + return false; + + lldb::addr_t valobj_addr = GetArrayAddressOrPointerValue(valobj); + if (valobj_addr == 0 || valobj_addr == LLDB_INVALID_ADDRESS) + return false; + + StringPrinter::ReadStringAndDumpToStreamOptions options(valobj); + options.SetLocation(valobj_addr); + options.SetProcessSP(process_sp); + options.SetStream(&stream); + if (ElemType == StringPrinter::StringElementType::UTF8) + options.SetPrefixToken("u8"); + else if (ElemType == StringPrinter::StringElementType::UTF16) + options.SetPrefixToken("u"); + else if (ElemType == StringPrinter::StringElementType::UTF32) + options.SetPrefixToken("U"); + + if (!StringPrinter::ReadStringAndDumpToStream(options)) + stream.Printf("Summary Unavailable"); - lldb::addr_t valobj_addr = GetArrayAddressOrPointerValue(valobj); - if (valobj_addr == 0 || valobj_addr == LLDB_INVALID_ADDRESS) - return false; - - StringPrinter::ReadStringAndDumpToStreamOptions options(valobj); - options.SetLocation(valobj_addr); - options.SetProcessSP(process_sp); - options.SetStream(&stream); - options.SetPrefixToken("u8"); - - if (!StringPrinter::ReadStringAndDumpToStream< - StringPrinter::StringElementType::UTF8>(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); + + if (error.Fail()) + return false; + + std::string value; + StringPrinter::ReadBufferAndDumpToStreamOptions options(valobj); + + if (ElemType == StringPrinter::StringElementType::UTF8) { + options.SetPrefixToken("u8"); + valobj.GetValueAsCString(lldb::eFormatUnicode8, value); + } else if (ElemType == StringPrinter::StringElementType::UTF16) { + options.SetPrefixToken("u"); + valobj.GetValueAsCString(lldb::eFormatUnicode16, value); + } else if (ElemType == StringPrinter::StringElementType::UTF32) { + options.SetPrefixToken("U"); + valobj.GetValueAsCString(lldb::eFormatUnicode32, value); + } + + if (!value.empty()) + stream.Printf("%s ", value.c_str()); + + options.SetData(std::move(data)); + options.SetStream(&stream); + options.SetQuote('\''); + options.SetSourceSize(1); + options.SetBinaryZeroIsTerminator(false); + + return StringPrinter::ReadBufferAndDumpToStream(options); + } - lldb::addr_t valobj_addr = GetArrayAddressOrPointerValue(valobj); - if (valobj_addr == 0 || valobj_addr == LLDB_INVALID_ADDRESS) - return false; +} // namespace - StringPrinter::ReadStringAndDumpToStreamOptions options(valobj); - options.SetLocation(valobj_addr); - options.SetProcessSP(process_sp); - options.SetStream(&stream); - options.SetPrefixToken("u"); - if (!StringPrinter::ReadStringAndDumpToStream< - StringPrinter::StringElementType::UTF16>(options)) { - stream.Printf("Summary Unavailable"); - return true; - } +bool lldb_private::formatters::Char8StringSummaryProvider( + ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) { + return CharStringSummaryProvider(valobj, stream); +} - return true; +bool lldb_private::formatters::Char16StringSummaryProvider( + ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) { + return CharStringSummaryProvider(valobj, stream); } bool lldb_private::formatters::Char32StringSummaryProvider( ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) { - ProcessSP process_sp = valobj.GetProcessSP(); - if (!process_sp) - return false; - - lldb::addr_t valobj_addr = GetArrayAddressOrPointerValue(valobj); - if (valobj_addr == 0 || valobj_addr == LLDB_INVALID_ADDRESS) - return false; - - StringPrinter::ReadStringAndDumpToStreamOptions options(valobj); - options.SetLocation(valobj_addr); - options.SetProcessSP(process_sp); - options.SetStream(&stream); - options.SetPrefixToken("U"); - - if (!StringPrinter::ReadStringAndDumpToStream< - StringPrinter::StringElementType::UTF32>(options)) { - stream.Printf("Summary Unavailable"); - return true; - } - - return true; + return CharStringSummaryProvider(valobj, stream); } bool lldb_private::formatters::WCharStringSummaryProvider( @@ -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(