Skip to content

Commit 938bc84

Browse files
committed
[LLDB] Add type summaries for MSVC STL strings
1 parent c2ea940 commit 938bc84

File tree

18 files changed

+706
-157
lines changed

18 files changed

+706
-157
lines changed

lldb/include/lldb/DataFormatters/StringPrinter.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,21 @@ class StringPrinter {
152152
template <StringElementType element_type>
153153
static bool
154154
ReadBufferAndDumpToStream(const ReadBufferAndDumpToStreamOptions &options);
155+
156+
template <StringElementType element_type>
157+
static constexpr uint64_t ElementByteSize() {
158+
switch (element_type) {
159+
case StringElementType::ASCII:
160+
case StringElementType::UTF8:
161+
return 1;
162+
case StringElementType::UTF16:
163+
return 2;
164+
case StringElementType::UTF32:
165+
return 3;
166+
default:
167+
return 0;
168+
}
169+
}
155170
};
156171

157172
} // namespace formatters

lldb/packages/Python/lldbsuite/test/dotest.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,26 @@ def checkLibstdcxxSupport():
831831
configuration.skip_categories.append("libstdcxx")
832832

833833

834+
def canRunMsvcStlTests():
835+
from lldbsuite.test import lldbplatformutil
836+
837+
platform = lldbplatformutil.getPlatform()
838+
if platform == "windows":
839+
return True, "MSVC STL is present on Windows"
840+
return False, f"Don't know how to build with MSVC's STL on {platform}"
841+
842+
843+
def checkMsvcStlSupport():
844+
result, reason = canRunMsvcStlTests()
845+
if result:
846+
return # msvcstl supported
847+
if "msvcstl" in configuration.categories_list:
848+
return # msvcstl category explicitly requested, let it run.
849+
if configuration.verbose:
850+
print(f"msvcstl tests will not be run because: {reason}")
851+
configuration.skip_categories.append("msvcstl")
852+
853+
834854
def canRunWatchpointTests():
835855
from lldbsuite.test import lldbplatformutil
836856

@@ -1044,6 +1064,7 @@ def run_suite():
10441064

10451065
checkLibcxxSupport()
10461066
checkLibstdcxxSupport()
1067+
checkMsvcStlSupport()
10471068
checkWatchpointSupport()
10481069
checkDebugInfoSupport()
10491070
checkDebugServerSupport()

lldb/packages/Python/lldbsuite/test/test_categories.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"lldb-server": "Tests related to lldb-server",
3434
"lldb-dap": "Tests for the Debug Adapter Protocol with lldb-dap",
3535
"llgs": "Tests for the gdb-server functionality of lldb-server",
36+
"msvcstl": "Test for MSVC STL data formatters",
3637
"pexpect": "Tests requiring the pexpect library to be available",
3738
"objc": "Tests related to the Objective-C programming language support",
3839
"pyapi": "Tests related to the Python API",

lldb/source/Plugins/Language/CPlusPlus/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ add_lldb_library(lldbPluginCPlusPlusLanguage PLUGIN
3232
LibStdcpp.cpp
3333
LibStdcppTuple.cpp
3434
LibStdcppUniquePointer.cpp
35+
MsvcStl.cpp
3536
MSVCUndecoratedNameParser.cpp
3637

3738
LINK_COMPONENTS

lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp

Lines changed: 85 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "LibCxxVariant.h"
4747
#include "LibStdcpp.h"
4848
#include "MSVCUndecoratedNameParser.h"
49+
#include "MsvcStl.h"
4950
#include "lldb/lldb-enumerations.h"
5051

5152
using namespace lldb;
@@ -1372,6 +1373,56 @@ static void LoadLibCxxFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
13721373
"${var.__y_} ${var.__m_} ${var.__wdl_}")));
13731374
}
13741375

1376+
template <StringPrinter::StringElementType element_type>
1377+
static bool
1378+
LibstdcppOrMsvcStringSummaryProvider(ValueObject &valobj, Stream &stream,
1379+
const TypeSummaryOptions &options) {
1380+
ValueObjectSP libstdcpp = valobj.GetChildMemberWithName("_M_dataplus");
1381+
if (libstdcpp) {
1382+
ValueObjectSP ptr = libstdcpp->GetChildMemberWithName("_M_p");
1383+
if (!ptr)
1384+
return false;
1385+
return CharTStringSummaryProvider<element_type>(*ptr, stream);
1386+
}
1387+
return MsvcStlStringSummaryProvider<element_type>(valobj, stream, options);
1388+
}
1389+
1390+
static bool
1391+
LibstdcppOrMsvcWStringSummaryProvider(ValueObject &valobj, Stream &stream,
1392+
const TypeSummaryOptions &options) {
1393+
ValueObjectSP libstdcpp = valobj.GetChildMemberWithName("_M_dataplus");
1394+
if (libstdcpp) {
1395+
ValueObjectSP ptr = libstdcpp->GetChildMemberWithName("_M_p");
1396+
if (!ptr)
1397+
return false;
1398+
return WCharStringSummaryProvider(*ptr, stream, options);
1399+
}
1400+
return MsvcStlWStringSummaryProvider(valobj, stream, options);
1401+
}
1402+
1403+
static void RegisterLibstdcppOrMsvcStringSummaryProvider(
1404+
const lldb::TypeCategoryImplSP &category_sp, llvm::StringRef string_ty,
1405+
llvm::StringRef char_ty, const lldb::TypeSummaryImplSP &summary_sp) {
1406+
category_sp->AddTypeSummary(string_ty, eFormatterMatchExact, summary_sp);
1407+
1408+
// std::basic_string<char>
1409+
category_sp->AddTypeSummary(
1410+
(llvm::Twine("std::basic_string<") + char_ty + ">").str(),
1411+
eFormatterMatchExact, summary_sp);
1412+
// std::basic_string<char,std::char_traits<char>,std::allocator<char> >
1413+
category_sp->AddTypeSummary((llvm::Twine("std::basic_string<") + char_ty +
1414+
",std::char_traits<" + char_ty +
1415+
">,std::allocator<" + char_ty + "> >")
1416+
.str(),
1417+
eFormatterMatchExact, summary_sp);
1418+
// std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1419+
category_sp->AddTypeSummary((llvm::Twine("std::basic_string<") + char_ty +
1420+
", std::char_traits<" + char_ty +
1421+
">, std::allocator<" + char_ty + "> >")
1422+
.str(),
1423+
eFormatterMatchExact, summary_sp);
1424+
}
1425+
13751426
static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
13761427
if (!cpp_category_sp)
13771428
return;
@@ -1385,26 +1436,20 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
13851436
.SetShowMembersOneLiner(false)
13861437
.SetHideItemNames(false);
13871438

1388-
lldb::TypeSummaryImplSP std_string_summary_sp(
1389-
new StringSummaryFormat(stl_summary_flags, "${var._M_dataplus._M_p}"));
1390-
13911439
lldb::TypeSummaryImplSP cxx11_string_summary_sp(new CXXFunctionSummaryFormat(
13921440
stl_summary_flags, LibStdcppStringSummaryProvider,
13931441
"libstdc++ c++11 std::string summary provider"));
13941442
lldb::TypeSummaryImplSP cxx11_wstring_summary_sp(new CXXFunctionSummaryFormat(
13951443
stl_summary_flags, LibStdcppWStringSummaryProvider,
13961444
"libstdc++ c++11 std::wstring summary provider"));
13971445

1398-
cpp_category_sp->AddTypeSummary("std::string", eFormatterMatchExact,
1399-
std_string_summary_sp);
1400-
cpp_category_sp->AddTypeSummary("std::basic_string<char>",
1401-
eFormatterMatchExact, std_string_summary_sp);
1402-
cpp_category_sp->AddTypeSummary(
1403-
"std::basic_string<char,std::char_traits<char>,std::allocator<char> >",
1404-
eFormatterMatchExact, std_string_summary_sp);
1405-
cpp_category_sp->AddTypeSummary(
1406-
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >",
1407-
eFormatterMatchExact, std_string_summary_sp);
1446+
RegisterLibstdcppOrMsvcStringSummaryProvider(
1447+
cpp_category_sp, "std::string", "char",
1448+
lldb::TypeSummaryImplSP(new CXXFunctionSummaryFormat(
1449+
stl_summary_flags,
1450+
LibstdcppOrMsvcStringSummaryProvider<
1451+
StringPrinter::StringElementType::ASCII>,
1452+
"libstdc++/MSVC STL std::string summary provider")));
14081453

14091454
cpp_category_sp->AddTypeSummary("std::__cxx11::string", eFormatterMatchExact,
14101455
cxx11_string_summary_sp);
@@ -1418,22 +1463,11 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
14181463
eFormatterMatchExact,
14191464
cxx11_string_summary_sp);
14201465

1421-
// making sure we force-pick the summary for printing wstring (_M_p is a
1422-
// wchar_t*)
1423-
lldb::TypeSummaryImplSP std_wstring_summary_sp(
1424-
new StringSummaryFormat(stl_summary_flags, "${var._M_dataplus._M_p%S}"));
1425-
1426-
cpp_category_sp->AddTypeSummary("std::wstring", eFormatterMatchExact,
1427-
std_wstring_summary_sp);
1428-
cpp_category_sp->AddTypeSummary("std::basic_string<wchar_t>",
1429-
eFormatterMatchExact, std_wstring_summary_sp);
1430-
cpp_category_sp->AddTypeSummary("std::basic_string<wchar_t,std::char_traits<"
1431-
"wchar_t>,std::allocator<wchar_t> >",
1432-
eFormatterMatchExact, std_wstring_summary_sp);
1433-
cpp_category_sp->AddTypeSummary(
1434-
"std::basic_string<wchar_t, std::char_traits<wchar_t>, "
1435-
"std::allocator<wchar_t> >",
1436-
eFormatterMatchExact, std_wstring_summary_sp);
1466+
RegisterLibstdcppOrMsvcStringSummaryProvider(
1467+
cpp_category_sp, "std::wstring", "wchar_t",
1468+
lldb::TypeSummaryImplSP(new CXXFunctionSummaryFormat(
1469+
stl_summary_flags, LibstdcppOrMsvcWStringSummaryProvider,
1470+
"libstdc++/MSVC STL std::wstring summary provider")));
14371471

14381472
cpp_category_sp->AddTypeSummary("std::__cxx11::wstring", eFormatterMatchExact,
14391473
cxx11_wstring_summary_sp);
@@ -1442,6 +1476,28 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
14421476
"std::allocator<wchar_t> >",
14431477
eFormatterMatchExact, cxx11_wstring_summary_sp);
14441478

1479+
RegisterLibstdcppOrMsvcStringSummaryProvider(
1480+
cpp_category_sp, "std::u8string", "char8_t",
1481+
lldb::TypeSummaryImplSP(new CXXFunctionSummaryFormat(
1482+
stl_summary_flags,
1483+
LibstdcppOrMsvcStringSummaryProvider<
1484+
StringPrinter::StringElementType::UTF8>,
1485+
"libstdc++/MSVC STL std::u8string summary provider")));
1486+
RegisterLibstdcppOrMsvcStringSummaryProvider(
1487+
cpp_category_sp, "std::u16string", "char16_t",
1488+
lldb::TypeSummaryImplSP(new CXXFunctionSummaryFormat(
1489+
stl_summary_flags,
1490+
LibstdcppOrMsvcStringSummaryProvider<
1491+
StringPrinter::StringElementType::UTF16>,
1492+
"libstdc++/MSVC STL std::u16string summary provider")));
1493+
RegisterLibstdcppOrMsvcStringSummaryProvider(
1494+
cpp_category_sp, "std::u32string", "char32_t",
1495+
lldb::TypeSummaryImplSP(new CXXFunctionSummaryFormat(
1496+
stl_summary_flags,
1497+
LibstdcppOrMsvcStringSummaryProvider<
1498+
StringPrinter::StringElementType::UTF32>,
1499+
"libstdc++/MSVC STL std::u32string summary provider")));
1500+
14451501
SyntheticChildren::Flags stl_synth_flags;
14461502
stl_synth_flags.SetCascades(true).SetSkipPointers(false).SetSkipReferences(
14471503
false);

0 commit comments

Comments
 (0)