Skip to content

Commit aeea062

Browse files
authored
[lldb][DataFormatter] Unwrap reference type when formatting std::unordered_map (#145872)
Desugar any potential references/typedefs before checking `isStdTemplate`. Previously, the typename might've been: ``` const std::unordered_map<...> & ``` for references. This patch gets the pointee type before grabbing the canonical type. `GetNonReferenceType` will unwrap typedefs too, so we should always end up with a non-reference before we get to `GetCanonicalType`. #145847
1 parent e0b83ca commit aeea062

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ CompilerType lldb_private::formatters::LibcxxStdUnorderedMapSyntheticFrontEnd::
113113
// wraps a std::pair. Peel away the internal wrapper type - whose structure is
114114
// of no value to users, to expose the std::pair. This matches the structure
115115
// returned by the std::map synthetic provider.
116-
if (isUnorderedMap(
117-
m_backend.GetCompilerType().GetCanonicalType().GetTypeName())) {
116+
if (isUnorderedMap(m_backend.GetCompilerType()
117+
.GetNonReferenceType()
118+
.GetCanonicalType()
119+
.GetTypeName())) {
118120
std::string name;
119121
CompilerType field_type =
120122
element_type.GetFieldAtIndex(0, name, nullptr, nullptr, nullptr);

lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/unordered_map/TestDataFormatterLibccUnorderedMap.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@
99

1010

1111
class LibcxxUnorderedMapDataFormatterTestCase(TestBase):
12+
def check_reference(self, var_name: str, expected_type: str):
13+
self.expect_var_path(
14+
var_name,
15+
summary="size=1",
16+
type=expected_type,
17+
children=[
18+
ValueCheck(
19+
name="[0]",
20+
children=[
21+
ValueCheck(name="first", summary='"Hello"'),
22+
ValueCheck(name="second", summary='"World"'),
23+
],
24+
),
25+
],
26+
)
27+
1228
@add_test_categories(["libc++"])
1329
def test_iterator_formatters(self):
1430
"""Test that std::unordered_map related structures are formatted correctly when printed.
@@ -64,3 +80,20 @@ def test_iterator_formatters(self):
6480
ValueCheck(name="second", summary='"Qux"'),
6581
],
6682
)
83+
84+
lldbutil.continue_to_breakpoint(process, bkpt)
85+
86+
# Test references to std::unordered_map
87+
self.check_reference("ref1", "const StringMapT &")
88+
self.check_reference("ref2", "StringMapT &")
89+
self.check_reference("ref3", "StringMapTRef")
90+
self.check_reference("ref4", "const StringMapT &")
91+
self.check_reference("ref5", "const StringMapT &&")
92+
self.check_reference("ref6", "StringMapT &&")
93+
94+
# FIXME: we're getting this wrong.
95+
self.expect_var_path(
96+
"ref7",
97+
summary="size=0",
98+
type="const StringMapT *const &",
99+
)

lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/unordered_map/main.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
#include <unordered_map>
44

55
using StringMapT = std::unordered_map<std::string, std::string>;
6+
using StringMapTRef = const StringMapT &;
7+
8+
static void check_references(const StringMapT &ref1, StringMapT &ref2,
9+
StringMapTRef ref3, StringMapTRef &ref4,
10+
const StringMapT &&ref5, StringMapT &&ref6,
11+
const StringMapT *const &ref7) {
12+
std::printf("Break here");
13+
}
614

715
int main() {
816
StringMapT string_map;
@@ -21,7 +29,12 @@ int main() {
2129
StringMapT::const_iterator const_baz = string_map.find("Baz");
2230
auto bucket_it = string_map.begin(string_map.bucket("Baz"));
2331
auto const_bucket_it = string_map.cbegin(string_map.bucket("Baz"));
32+
2433
std::printf("Break here");
34+
35+
StringMapT tmp{{"Hello", "World"}};
36+
check_references(tmp, tmp, tmp, tmp, StringMapT{tmp}, StringMapT{tmp},
37+
&tmp);
2538
}
2639

2740
return 0;

0 commit comments

Comments
 (0)