Skip to content

[LLDB] Simplify libstdc++ string summaries #146562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1344,38 +1344,31 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
.SetShowMembersOneLiner(false)
.SetHideItemNames(false);

lldb::TypeSummaryImplSP std_string_summary_sp(
new StringSummaryFormat(stl_summary_flags, "${var._M_dataplus._M_p}"));

lldb::TypeSummaryImplSP cxx11_string_summary_sp(new CXXFunctionSummaryFormat(
lldb::TypeSummaryImplSP string_summary_sp(new CXXFunctionSummaryFormat(
stl_summary_flags, LibStdcppStringSummaryProvider,
"libstdc++ c++11 std::string summary provider"));
lldb::TypeSummaryImplSP cxx11_wstring_summary_sp(new CXXFunctionSummaryFormat(
stl_summary_flags, LibStdcppWStringSummaryProvider,
"libstdc++ c++11 std::wstring summary provider"));
"libstdc++ std::(w)string summary provider"));

cpp_category_sp->AddTypeSummary("std::string", eFormatterMatchExact,
std_string_summary_sp);
string_summary_sp);
cpp_category_sp->AddTypeSummary("std::basic_string<char>",
eFormatterMatchExact, std_string_summary_sp);
eFormatterMatchExact, string_summary_sp);
cpp_category_sp->AddTypeSummary(
"std::basic_string<char,std::char_traits<char>,std::allocator<char> >",
eFormatterMatchExact, std_string_summary_sp);
eFormatterMatchExact, string_summary_sp);
cpp_category_sp->AddTypeSummary(
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >",
eFormatterMatchExact, std_string_summary_sp);
eFormatterMatchExact, string_summary_sp);

cpp_category_sp->AddTypeSummary("std::__cxx11::string", eFormatterMatchExact,
cxx11_string_summary_sp);
string_summary_sp);
cpp_category_sp->AddTypeSummary(
"std::__cxx11::basic_string<char, std::char_traits<char>, "
"std::allocator<char> >",
eFormatterMatchExact, cxx11_string_summary_sp);
eFormatterMatchExact, string_summary_sp);
cpp_category_sp->AddTypeSummary("std::__cxx11::basic_string<unsigned char, "
"std::char_traits<unsigned char>, "
"std::allocator<unsigned char> >",
eFormatterMatchExact,
cxx11_string_summary_sp);
eFormatterMatchExact, string_summary_sp);

// making sure we force-pick the summary for printing wstring (_M_p is a
// wchar_t*)
Expand All @@ -1395,11 +1388,11 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
eFormatterMatchExact, std_wstring_summary_sp);

cpp_category_sp->AddTypeSummary("std::__cxx11::wstring", eFormatterMatchExact,
cxx11_wstring_summary_sp);
string_summary_sp);
cpp_category_sp->AddTypeSummary(
"std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, "
"std::allocator<wchar_t> >",
eFormatterMatchExact, cxx11_wstring_summary_sp);
eFormatterMatchExact, string_summary_sp);

SyntheticChildren::Flags stl_synth_flags;
stl_synth_flags.SetCascades(true).SetSkipPointers(false).SetSkipReferences(
Expand Down
120 changes: 5 additions & 115 deletions lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,122 +239,12 @@ VectorIteratorSyntheticFrontEnd::GetIndexOfChildWithName(ConstString name) {

bool lldb_private::formatters::LibStdcppStringSummaryProvider(
ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
const bool scalar_is_load_addr = true;
auto [addr_of_string, addr_type] =
valobj.IsPointerOrReferenceType()
? valobj.GetPointerValue()
: valobj.GetAddressOf(scalar_is_load_addr);
if (addr_of_string != LLDB_INVALID_ADDRESS) {
switch (addr_type) {
case eAddressTypeLoad: {
ProcessSP process_sp(valobj.GetProcessSP());
if (!process_sp)
return false;

StringPrinter::ReadStringAndDumpToStreamOptions options(valobj);
Status error;
lldb::addr_t addr_of_data =
process_sp->ReadPointerFromMemory(addr_of_string, error);
if (error.Fail() || addr_of_data == 0 ||
addr_of_data == LLDB_INVALID_ADDRESS)
return false;
options.SetLocation(addr_of_data);
options.SetTargetSP(valobj.GetTargetSP());
options.SetStream(&stream);
options.SetNeedsZeroTermination(false);
options.SetBinaryZeroIsTerminator(true);
lldb::addr_t size_of_data = process_sp->ReadPointerFromMemory(
addr_of_string + process_sp->GetAddressByteSize(), error);
if (error.Fail())
return false;
options.SetSourceSize(size_of_data);
options.SetHasSourceSize(true);

if (!StringPrinter::ReadStringAndDumpToStream<
StringPrinter::StringElementType::UTF8>(options)) {
stream.Printf("Summary Unavailable");
return true;
} else
return true;
} break;
case eAddressTypeHost:
break;
case eAddressTypeInvalid:
case eAddressTypeFile:
break;
}
}
return false;
}
ValueObjectSP ptr = valobj.GetChildAtNamePath({"_M_dataplus", "_M_p"});
if (!ptr)
return false;

bool lldb_private::formatters::LibStdcppWStringSummaryProvider(
ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
const bool scalar_is_load_addr = true;
auto [addr_of_string, addr_type] = valobj.GetAddressOf(scalar_is_load_addr);
if (addr_of_string != LLDB_INVALID_ADDRESS) {
switch (addr_type) {
case eAddressTypeLoad: {
ProcessSP process_sp(valobj.GetProcessSP());
if (!process_sp)
return false;

CompilerType wchar_compiler_type =
valobj.GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeWChar);

if (!wchar_compiler_type)
return false;

// Safe to pass nullptr for exe_scope here.
std::optional<uint64_t> size =
llvm::expectedToOptional(wchar_compiler_type.GetBitSize(nullptr));
if (!size)
return false;
const uint32_t wchar_size = *size;

StringPrinter::ReadStringAndDumpToStreamOptions options(valobj);
Status error;
lldb::addr_t addr_of_data =
process_sp->ReadPointerFromMemory(addr_of_string, error);
if (error.Fail() || addr_of_data == 0 ||
addr_of_data == LLDB_INVALID_ADDRESS)
return false;
options.SetLocation(addr_of_data);
options.SetTargetSP(valobj.GetTargetSP());
options.SetStream(&stream);
options.SetNeedsZeroTermination(false);
options.SetBinaryZeroIsTerminator(false);
lldb::addr_t size_of_data = process_sp->ReadPointerFromMemory(
addr_of_string + process_sp->GetAddressByteSize(), error);
if (error.Fail())
return false;
options.SetSourceSize(size_of_data);
options.SetHasSourceSize(true);
options.SetPrefixToken("L");

switch (wchar_size) {
case 8:
return StringPrinter::ReadStringAndDumpToStream<
StringPrinter::StringElementType::UTF8>(options);
case 16:
return StringPrinter::ReadStringAndDumpToStream<
StringPrinter::StringElementType::UTF16>(options);
case 32:
return StringPrinter::ReadStringAndDumpToStream<
StringPrinter::StringElementType::UTF32>(options);
default:
stream.Printf("size for wchar_t is not valid");
return true;
}
return true;
} break;
case eAddressTypeHost:
break;
case eAddressTypeInvalid:
case eAddressTypeFile:
break;
}
}
return false;
stream << ptr->GetSummaryAsCString();
return true;
}

LibStdcppSharedPtrSyntheticFrontEnd::LibStdcppSharedPtrSyntheticFrontEnd(
Expand Down
6 changes: 1 addition & 5 deletions lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ namespace lldb_private {
namespace formatters {
bool LibStdcppStringSummaryProvider(
ValueObject &valobj, Stream &stream,
const TypeSummaryOptions &options); // libcstdc++ c++11 std::string

bool LibStdcppWStringSummaryProvider(
ValueObject &valobj, Stream &stream,
const TypeSummaryOptions &options); // libcstdc++ c++11 std::wstring
const TypeSummaryOptions &options); // libstdc++ std::string

bool LibStdcppSmartPointerSummaryProvider(
ValueObject &valobj, Stream &stream,
Expand Down
Loading