Skip to content

Commit 0f4c8b8

Browse files
committed
[LLDB] Add type summaries for MSVC STL strings
1 parent 463ce01 commit 0f4c8b8

File tree

19 files changed

+570
-152
lines changed

19 files changed

+570
-152
lines changed

lldb/include/lldb/DataFormatters/StringPrinter.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,20 @@ 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+
}
167+
return 0;
168+
}
155169
};
156170

157171
} // namespace formatters

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,46 @@ 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 False, f"Don't know how to build with MSVC's STL on {platform}"
840+
841+
with tempfile.NamedTemporaryFile() as f:
842+
cmd = [configuration.compiler, "-xc++", "-o", f.name, "-E", "-"]
843+
p = subprocess.Popen(
844+
cmd,
845+
stdin=subprocess.PIPE,
846+
stdout=subprocess.PIPE,
847+
stderr=subprocess.PIPE,
848+
universal_newlines=True,
849+
)
850+
_, stderr = p.communicate(
851+
"""
852+
#include <yvals_core.h>
853+
#ifndef _MSVC_STL_VERSION
854+
#error _MSVC_STL_VERSION not defined
855+
#endif
856+
"""
857+
)
858+
if not p.returncode:
859+
return True, "Compiling with MSVC STL"
860+
return (False, f"Not compiling with MSVC STL: {stderr}")
861+
862+
863+
def checkMsvcStlSupport():
864+
result, reason = canRunMsvcStlTests()
865+
if result:
866+
return # msvcstl supported
867+
if "msvcstl" in configuration.categories_list:
868+
return # msvcstl category explicitly requested, let it run.
869+
if configuration.verbose:
870+
print(f"msvcstl tests will not be run because: {reason}")
871+
configuration.skip_categories.append("msvcstl")
872+
873+
834874
def canRunWatchpointTests():
835875
from lldbsuite.test import lldbplatformutil
836876

@@ -1044,6 +1084,7 @@ def run_suite():
10441084

10451085
checkLibcxxSupport()
10461086
checkLibstdcxxSupport()
1087+
checkMsvcStlSupport()
10471088
checkWatchpointSupport()
10481089
checkDebugInfoSupport()
10491090
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: 108 additions & 31 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,37 @@ static void LoadLibCxxFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
13721373
"${var.__y_} ${var.__m_} ${var.__wdl_}")));
13731374
}
13741375

1376+
static void RegisterStdStringSummaryProvider(
1377+
const lldb::TypeCategoryImplSP &category_sp, llvm::StringRef string_ty,
1378+
llvm::StringRef char_ty, lldb::TypeSummaryImplSP summary_sp) {
1379+
auto makeSpecifier = [](llvm::StringRef name) {
1380+
return std::make_shared<lldb_private::TypeNameSpecifierImpl>(
1381+
name, eFormatterMatchExact);
1382+
};
1383+
1384+
category_sp->AddTypeSummary(makeSpecifier(string_ty), summary_sp);
1385+
1386+
// std::basic_string<char>
1387+
category_sp->AddTypeSummary(
1388+
makeSpecifier(llvm::formatv("std::basic_string<{}>", char_ty).str()),
1389+
summary_sp);
1390+
// std::basic_string<char,std::char_traits<char>,std::allocator<char> >
1391+
category_sp->AddTypeSummary(
1392+
makeSpecifier(llvm::formatv("std::basic_string<{0},std::char_traits<{0}>,"
1393+
"std::allocator<{0}> >",
1394+
char_ty)
1395+
.str()),
1396+
summary_sp);
1397+
// std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1398+
category_sp->AddTypeSummary(
1399+
makeSpecifier(
1400+
llvm::formatv("std::basic_string<{0}, std::char_traits<{0}>, "
1401+
"std::allocator<{0}> >",
1402+
char_ty)
1403+
.str()),
1404+
summary_sp);
1405+
}
1406+
13751407
static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
13761408
if (!cpp_category_sp)
13771409
return;
@@ -1385,27 +1417,13 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
13851417
.SetShowMembersOneLiner(false)
13861418
.SetHideItemNames(false);
13871419

1388-
lldb::TypeSummaryImplSP std_string_summary_sp(
1389-
new StringSummaryFormat(stl_summary_flags, "${var._M_dataplus._M_p}"));
1390-
13911420
lldb::TypeSummaryImplSP cxx11_string_summary_sp(new CXXFunctionSummaryFormat(
13921421
stl_summary_flags, LibStdcppStringSummaryProvider,
13931422
"libstdc++ c++11 std::string summary provider"));
13941423
lldb::TypeSummaryImplSP cxx11_wstring_summary_sp(new CXXFunctionSummaryFormat(
13951424
stl_summary_flags, LibStdcppWStringSummaryProvider,
13961425
"libstdc++ c++11 std::wstring summary provider"));
13971426

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);
1408-
14091427
cpp_category_sp->AddTypeSummary("std::__cxx11::string", eFormatterMatchExact,
14101428
cxx11_string_summary_sp);
14111429
cpp_category_sp->AddTypeSummary(
@@ -1418,23 +1436,6 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
14181436
eFormatterMatchExact,
14191437
cxx11_string_summary_sp);
14201438

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);
1437-
14381439
cpp_category_sp->AddTypeSummary("std::__cxx11::wstring", eFormatterMatchExact,
14391440
cxx11_wstring_summary_sp);
14401441
cpp_category_sp->AddTypeSummary(
@@ -1629,6 +1630,80 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
16291630
"^std::optional<.+>(( )?&)?$", stl_summary_flags, true);
16301631
}
16311632

1633+
static void LoadCommonStlFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
1634+
if (!cpp_category_sp)
1635+
return;
1636+
1637+
TypeSummaryImpl::Flags stl_summary_flags;
1638+
stl_summary_flags.SetCascades(true)
1639+
.SetSkipPointers(false)
1640+
.SetSkipReferences(false)
1641+
.SetDontShowChildren(true)
1642+
.SetDontShowValue(false)
1643+
.SetShowMembersOneLiner(false)
1644+
.SetHideItemNames(false);
1645+
using StringElementType = StringPrinter::StringElementType;
1646+
1647+
RegisterStdStringSummaryProvider(
1648+
cpp_category_sp, "std::string", "char",
1649+
std::make_shared<CXXFunctionSummaryFormat>(
1650+
stl_summary_flags,
1651+
[](ValueObject &valobj, Stream &stream,
1652+
const TypeSummaryOptions &options) {
1653+
if (IsMsvcStlStringType(valobj))
1654+
return MsvcStlStringSummaryProvider<StringElementType::ASCII>(
1655+
valobj, stream, options);
1656+
return LibStdcppStringSummaryProvider(valobj, stream, options);
1657+
},
1658+
"MSVC STL/libstdc++ std::string summary provider"));
1659+
RegisterStdStringSummaryProvider(
1660+
cpp_category_sp, "std::wstring", "wchar_t",
1661+
std::make_shared<CXXFunctionSummaryFormat>(
1662+
stl_summary_flags,
1663+
[](ValueObject &valobj, Stream &stream,
1664+
const TypeSummaryOptions &options) {
1665+
if (IsMsvcStlStringType(valobj))
1666+
return MsvcStlWStringSummaryProvider(valobj, stream, options);
1667+
return LibStdcppWStringSummaryProvider(valobj, stream, options);
1668+
},
1669+
"MSVC STL/libstdc++ std::wstring summary provider"));
1670+
}
1671+
1672+
static void LoadMsvcStlFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
1673+
if (!cpp_category_sp)
1674+
return;
1675+
1676+
TypeSummaryImpl::Flags stl_summary_flags;
1677+
stl_summary_flags.SetCascades(true)
1678+
.SetSkipPointers(false)
1679+
.SetSkipReferences(false)
1680+
.SetDontShowChildren(true)
1681+
.SetDontShowValue(false)
1682+
.SetShowMembersOneLiner(false)
1683+
.SetHideItemNames(false);
1684+
1685+
using StringElementType = StringPrinter::StringElementType;
1686+
1687+
RegisterStdStringSummaryProvider(
1688+
cpp_category_sp, "std::u8string", "char8_t",
1689+
std::make_shared<CXXFunctionSummaryFormat>(
1690+
stl_summary_flags,
1691+
MsvcStlStringSummaryProvider<StringElementType::UTF8>,
1692+
"MSVC STL std::u8string summary provider"));
1693+
RegisterStdStringSummaryProvider(
1694+
cpp_category_sp, "std::u16string", "char16_t",
1695+
std::make_shared<CXXFunctionSummaryFormat>(
1696+
stl_summary_flags,
1697+
MsvcStlStringSummaryProvider<StringElementType::UTF16>,
1698+
"MSVC STL std::u16string summary provider"));
1699+
RegisterStdStringSummaryProvider(
1700+
cpp_category_sp, "std::u32string", "char32_t",
1701+
std::make_shared<CXXFunctionSummaryFormat>(
1702+
stl_summary_flags,
1703+
MsvcStlStringSummaryProvider<StringElementType::UTF32>,
1704+
"MSVC STL std::u32string summary provider"));
1705+
}
1706+
16321707
static void LoadSystemFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
16331708
if (!cpp_category_sp)
16341709
return;
@@ -1743,6 +1818,8 @@ lldb::TypeCategoryImplSP CPlusPlusLanguage::GetFormatters() {
17431818
// LLDB prioritizes the last loaded matching formatter.
17441819
LoadLibCxxFormatters(g_category);
17451820
LoadLibStdcppFormatters(g_category);
1821+
LoadMsvcStlFormatters(g_category);
1822+
LoadCommonStlFormatters(g_category);
17461823
LoadSystemFormatters(g_category);
17471824
}
17481825
});

0 commit comments

Comments
 (0)