Skip to content

Commit 4214f25

Browse files
committed
Re-land "[lldb] Fix FindDirectNestedType not working with class templates (#81666)"
This patch attempts to fix lookup in class template specialization. The first fixed problem is that during type lookup `DeclContextGetName` have been dropping template arguments. So when such a name was compared against a name in `DW_AT_name`, which contains template arguments, false mismatches have been occurring. The second fixed problem is that LLDB's printing policy hasn't been matching Clang's printing policy when it comes to integral non-type template arguments. This again caused some false mismatches during type lookup, because Clang puts e.g. `3U` in debug info for class specializations, but LLDB has been expecting just `3`. This patch brings printing policy in line with what Clang does.
1 parent 6841395 commit 4214f25

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9265,8 +9265,14 @@ ConstString TypeSystemClang::DeclContextGetName(void *opaque_decl_ctx) {
92659265
if (opaque_decl_ctx) {
92669266
clang::NamedDecl *named_decl =
92679267
llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9268-
if (named_decl)
9269-
return ConstString(named_decl->getName());
9268+
if (named_decl) {
9269+
std::string name;
9270+
llvm::raw_string_ostream stream{name};
9271+
auto policy = GetTypePrintingPolicy();
9272+
policy.AlwaysIncludeTypeForTemplateArgument = true;
9273+
named_decl->getNameForDiagnostic(stream, policy, /*qualified=*/false);
9274+
return ConstString(name);
9275+
}
92709276
}
92719277
return ConstString();
92729278
}

lldb/test/API/python_api/type/TestTypeList.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,23 @@ def test(self):
150150
invalid_type = task_type.FindDirectNestedType(None)
151151
self.assertFalse(invalid_type)
152152

153+
# Check that FindDirectNestedType works with types from AST
154+
pointer = frame0.FindVariable("pointer")
155+
pointer_type = pointer.GetType()
156+
self.assertTrue(pointer_type)
157+
self.DebugSBType(pointer_type)
158+
pointer_info_type = pointer_type.template_args[1]
159+
self.assertTrue(pointer_info_type)
160+
self.DebugSBType(pointer_info_type)
161+
162+
pointer_masks1_type = pointer_info_type.FindDirectNestedType("Masks1")
163+
self.assertTrue(pointer_masks1_type)
164+
self.DebugSBType(pointer_masks1_type)
165+
166+
pointer_masks2_type = pointer_info_type.FindDirectNestedType("Masks2")
167+
self.assertTrue(pointer_masks2_type)
168+
self.DebugSBType(pointer_masks2_type)
169+
153170
# We'll now get the child member 'id' from 'task_head'.
154171
id = task_head.GetChildMemberWithName("id")
155172
self.DebugSBValue(id)

lldb/test/API/python_api/type/main.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ class Task {
3434
{}
3535
};
3636

37+
template <unsigned Value> struct PointerInfo {
38+
enum Masks1 { pointer_mask };
39+
enum class Masks2 { pointer_mask };
40+
};
41+
42+
template <unsigned Value, typename InfoType = PointerInfo<Value>>
43+
struct Pointer {};
44+
3745
enum EnumType {};
3846
enum class ScopedEnumType {};
3947
enum class EnumUChar : unsigned char {};
@@ -71,5 +79,9 @@ int main (int argc, char const *argv[])
7179
ScopedEnumType scoped_enum_type;
7280
EnumUChar scoped_enum_type_uchar;
7381

82+
Pointer<3> pointer;
83+
PointerInfo<3>::Masks1 mask1 = PointerInfo<3>::Masks1::pointer_mask;
84+
PointerInfo<3>::Masks2 mask2 = PointerInfo<3>::Masks2::pointer_mask;
85+
7486
return 0; // Break at this line
7587
}

0 commit comments

Comments
 (0)