diff options
author | nerix <nerixdev@outlook.de> | 2025-07-08 10:55:18 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-08 09:55:18 +0100 |
commit | 45689b26eb4bfa619127013dccea8efd8de4d21a (patch) | |
tree | 232bae8e440a57b8b1ec54583ae72047e7bdb97b /lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp | |
parent | 29487759e3cc8fc056a599a38b3a6d2d8849a20e (diff) | |
download | llvm-45689b26eb4bfa619127013dccea8efd8de4d21a.zip llvm-45689b26eb4bfa619127013dccea8efd8de4d21a.tar.gz llvm-45689b26eb4bfa619127013dccea8efd8de4d21a.tar.bz2 |
[LLDB] Add type summaries for MSVC STL strings (#143177)
This PR adds type summaries for
`std::{string,wstring,u8string,u16string,u32string}` from the MSVC STL.
See https://github.com/llvm/llvm-project/issues/24834 for the MSVC STL
issue.
The following changes were made:
- `dotest.py` now detects if the MSVC STL is available. It does so by
looking at the target triple, which is an additional argument passed
from Lit. It specifically checks for `windows-msvc` to not match on
`windows-gnu` (i.e. MinGW/Cygwin).
- (The main part): Added support for summarizing `std::(w)string` from
MSVC's STL. Because the type names from the libstdc++ (pre C++ 11)
string types are the same as on MSVC's STL, `CXXCompositeSummaryFormat`
is used with two entries, one for MSVC's STL and one for libstdc++.
With MSVC's STL, `std::u{8,16,32}string` is also handled. These aren't
handled for libstdc++, so I put them in `LoadMsvcStlFormatters`.
Diffstat (limited to 'lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp')
-rw-r--r-- | lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp | 138 |
1 files changed, 109 insertions, 29 deletions
diff --git a/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp b/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp index e64b3eb..17963c0 100644 --- a/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp +++ b/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp @@ -46,6 +46,7 @@ #include "LibCxxVariant.h" #include "LibStdcpp.h" #include "MSVCUndecoratedNameParser.h" +#include "MsvcStl.h" #include "lldb/lldb-enumerations.h" using namespace lldb; @@ -1331,6 +1332,37 @@ static void LoadLibCxxFormatters(lldb::TypeCategoryImplSP cpp_category_sp) { "${var.__y_} ${var.__m_} ${var.__wdl_}"))); } +static void RegisterStdStringSummaryProvider( + const lldb::TypeCategoryImplSP &category_sp, llvm::StringRef string_ty, + llvm::StringRef char_ty, lldb::TypeSummaryImplSP summary_sp) { + auto makeSpecifier = [](llvm::StringRef name) { + return std::make_shared<lldb_private::TypeNameSpecifierImpl>( + name, eFormatterMatchExact); + }; + + category_sp->AddTypeSummary(makeSpecifier(string_ty), summary_sp); + + // std::basic_string<char> + category_sp->AddTypeSummary( + makeSpecifier(llvm::formatv("std::basic_string<{}>", char_ty).str()), + summary_sp); + // std::basic_string<char,std::char_traits<char>,std::allocator<char> > + category_sp->AddTypeSummary( + makeSpecifier(llvm::formatv("std::basic_string<{0},std::char_traits<{0}>," + "std::allocator<{0}> >", + char_ty) + .str()), + summary_sp); + // std::basic_string<char, std::char_traits<char>, std::allocator<char> > + category_sp->AddTypeSummary( + makeSpecifier( + llvm::formatv("std::basic_string<{0}, std::char_traits<{0}>, " + "std::allocator<{0}> >", + char_ty) + .str()), + summary_sp); +} + static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) { if (!cpp_category_sp) return; @@ -1347,18 +1379,6 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) { lldb::TypeSummaryImplSP string_summary_sp(new CXXFunctionSummaryFormat( stl_summary_flags, LibStdcppStringSummaryProvider, "libstdc++ std::(w)string summary provider")); - - cpp_category_sp->AddTypeSummary("std::string", eFormatterMatchExact, - string_summary_sp); - cpp_category_sp->AddTypeSummary("std::basic_string<char>", - eFormatterMatchExact, string_summary_sp); - cpp_category_sp->AddTypeSummary( - "std::basic_string<char,std::char_traits<char>,std::allocator<char> >", - eFormatterMatchExact, string_summary_sp); - cpp_category_sp->AddTypeSummary( - "std::basic_string<char, std::char_traits<char>, std::allocator<char> >", - eFormatterMatchExact, string_summary_sp); - cpp_category_sp->AddTypeSummary("std::__cxx11::string", eFormatterMatchExact, string_summary_sp); cpp_category_sp->AddTypeSummary( @@ -1370,23 +1390,6 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) { "std::allocator<unsigned char> >", eFormatterMatchExact, string_summary_sp); - // making sure we force-pick the summary for printing wstring (_M_p is a - // wchar_t*) - lldb::TypeSummaryImplSP std_wstring_summary_sp( - new StringSummaryFormat(stl_summary_flags, "${var._M_dataplus._M_p%S}")); - - cpp_category_sp->AddTypeSummary("std::wstring", eFormatterMatchExact, - std_wstring_summary_sp); - cpp_category_sp->AddTypeSummary("std::basic_string<wchar_t>", - eFormatterMatchExact, std_wstring_summary_sp); - cpp_category_sp->AddTypeSummary("std::basic_string<wchar_t,std::char_traits<" - "wchar_t>,std::allocator<wchar_t> >", - eFormatterMatchExact, std_wstring_summary_sp); - cpp_category_sp->AddTypeSummary( - "std::basic_string<wchar_t, std::char_traits<wchar_t>, " - "std::allocator<wchar_t> >", - eFormatterMatchExact, std_wstring_summary_sp); - cpp_category_sp->AddTypeSummary("std::__cxx11::wstring", eFormatterMatchExact, string_summary_sp); cpp_category_sp->AddTypeSummary( @@ -1595,6 +1598,81 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) { "^std::optional<.+>(( )?&)?$", stl_summary_flags, true); } +/// Load formatters that are formatting types from more than one STL +static void LoadCommonStlFormatters(lldb::TypeCategoryImplSP cpp_category_sp) { + if (!cpp_category_sp) + return; + + TypeSummaryImpl::Flags stl_summary_flags; + stl_summary_flags.SetCascades(true) + .SetSkipPointers(false) + .SetSkipReferences(false) + .SetDontShowChildren(true) + .SetDontShowValue(false) + .SetShowMembersOneLiner(false) + .SetHideItemNames(false); + using StringElementType = StringPrinter::StringElementType; + + RegisterStdStringSummaryProvider( + cpp_category_sp, "std::string", "char", + std::make_shared<CXXFunctionSummaryFormat>( + stl_summary_flags, + [](ValueObject &valobj, Stream &stream, + const TypeSummaryOptions &options) { + if (IsMsvcStlStringType(valobj)) + return MsvcStlStringSummaryProvider<StringElementType::ASCII>( + valobj, stream, options); + return LibStdcppStringSummaryProvider(valobj, stream, options); + }, + "MSVC STL/libstdc++ std::string summary provider")); + RegisterStdStringSummaryProvider( + cpp_category_sp, "std::wstring", "wchar_t", + std::make_shared<CXXFunctionSummaryFormat>( + stl_summary_flags, + [](ValueObject &valobj, Stream &stream, + const TypeSummaryOptions &options) { + if (IsMsvcStlStringType(valobj)) + return MsvcStlWStringSummaryProvider(valobj, stream, options); + return LibStdcppStringSummaryProvider(valobj, stream, options); + }, + "MSVC STL/libstdc++ std::wstring summary provider")); +} + +static void LoadMsvcStlFormatters(lldb::TypeCategoryImplSP cpp_category_sp) { + if (!cpp_category_sp) + return; + + TypeSummaryImpl::Flags stl_summary_flags; + stl_summary_flags.SetCascades(true) + .SetSkipPointers(false) + .SetSkipReferences(false) + .SetDontShowChildren(true) + .SetDontShowValue(false) + .SetShowMembersOneLiner(false) + .SetHideItemNames(false); + + using StringElementType = StringPrinter::StringElementType; + + RegisterStdStringSummaryProvider( + cpp_category_sp, "std::u8string", "char8_t", + std::make_shared<CXXFunctionSummaryFormat>( + stl_summary_flags, + MsvcStlStringSummaryProvider<StringElementType::UTF8>, + "MSVC STL std::u8string summary provider")); + RegisterStdStringSummaryProvider( + cpp_category_sp, "std::u16string", "char16_t", + std::make_shared<CXXFunctionSummaryFormat>( + stl_summary_flags, + MsvcStlStringSummaryProvider<StringElementType::UTF16>, + "MSVC STL std::u16string summary provider")); + RegisterStdStringSummaryProvider( + cpp_category_sp, "std::u32string", "char32_t", + std::make_shared<CXXFunctionSummaryFormat>( + stl_summary_flags, + MsvcStlStringSummaryProvider<StringElementType::UTF32>, + "MSVC STL std::u32string summary provider")); +} + static void LoadSystemFormatters(lldb::TypeCategoryImplSP cpp_category_sp) { if (!cpp_category_sp) return; @@ -1709,6 +1787,8 @@ lldb::TypeCategoryImplSP CPlusPlusLanguage::GetFormatters() { // LLDB prioritizes the last loaded matching formatter. LoadLibCxxFormatters(g_category); LoadLibStdcppFormatters(g_category); + LoadMsvcStlFormatters(g_category); + LoadCommonStlFormatters(g_category); LoadSystemFormatters(g_category); } }); |