Skip to content

Commit d806942

Browse files
committed
[LLDB] Add type summaries for MSVC STL strings
1 parent 1f10c6a commit d806942

File tree

15 files changed

+569
-145
lines changed

15 files changed

+569
-145
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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,45 @@ 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+
def checkMsvcStlSupport():
863+
result, reason = canRunMsvcStlTests()
864+
if result:
865+
return # msvcstl supported
866+
if "msvcstl" in configuration.categories_list:
867+
return # msvcstl category explicitly requested, let it run.
868+
if configuration.verbose:
869+
print(f"msvcstl tests will not be run because: {reason}")
870+
configuration.skip_categories.append("msvcstl")
871+
872+
834873
def canRunWatchpointTests():
835874
from lldbsuite.test import lldbplatformutil
836875

@@ -1044,6 +1083,7 @@ def run_suite():
10441083

10451084
checkLibcxxSupport()
10461085
checkLibstdcxxSupport()
1086+
checkMsvcStlSupport()
10471087
checkWatchpointSupport()
10481088
checkDebugInfoSupport()
10491089
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: 107 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,36 @@ 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::Twine("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::Twine("std::basic_string<") + char_ty +
1393+
",std::char_traits<" + char_ty + ">,std::allocator<" +
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((llvm::Twine("std::basic_string<") + char_ty +
1400+
", std::char_traits<" + char_ty + ">, std::allocator<" +
1401+
char_ty + "> >")
1402+
.str()),
1403+
summary_sp);
1404+
}
1405+
13751406
static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
13761407
if (!cpp_category_sp)
13771408
return;
@@ -1385,27 +1416,13 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
13851416
.SetShowMembersOneLiner(false)
13861417
.SetHideItemNames(false);
13871418

1388-
lldb::TypeSummaryImplSP std_string_summary_sp(
1389-
new StringSummaryFormat(stl_summary_flags, "${var._M_dataplus._M_p}"));
1390-
13911419
lldb::TypeSummaryImplSP cxx11_string_summary_sp(new CXXFunctionSummaryFormat(
13921420
stl_summary_flags, LibStdcppStringSummaryProvider,
13931421
"libstdc++ c++11 std::string summary provider"));
13941422
lldb::TypeSummaryImplSP cxx11_wstring_summary_sp(new CXXFunctionSummaryFormat(
13951423
stl_summary_flags, LibStdcppWStringSummaryProvider,
13961424
"libstdc++ c++11 std::wstring summary provider"));
13971425

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-
14091426
cpp_category_sp->AddTypeSummary("std::__cxx11::string", eFormatterMatchExact,
14101427
cxx11_string_summary_sp);
14111428
cpp_category_sp->AddTypeSummary(
@@ -1418,23 +1435,6 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
14181435
eFormatterMatchExact,
14191436
cxx11_string_summary_sp);
14201437

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-
14381438
cpp_category_sp->AddTypeSummary("std::__cxx11::wstring", eFormatterMatchExact,
14391439
cxx11_wstring_summary_sp);
14401440
cpp_category_sp->AddTypeSummary(
@@ -1629,6 +1629,80 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
16291629
"^std::optional<.+>(( )?&)?$", stl_summary_flags, true);
16301630
}
16311631

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

0 commit comments

Comments
 (0)