[Lldb-commits] [lldb] [lldb] Make SBType::GetDirectNestedType (mostly) work with typedefs (PR #91189)

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Mon May 6 04:35:30 PDT 2024


https://github.com/labath updated https://github.com/llvm/llvm-project/pull/91189

>From 7de5cfcc3016a9e49e289e1009bcf4cc68aa445e Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Mon, 6 May 2024 11:30:59 +0000
Subject: [PATCH] [lldb] Make SBType::GetDirectNestedType (mostly) work with
 typedefs

The implementation is straight-forward, but comes with a big disclaimer.
See #91186 for details.
---
 .../Plugins/TypeSystem/Clang/TypeSystemClang.cpp |  2 ++
 lldb/test/API/python_api/type/TestTypeList.py    | 16 ++++++++++++++++
 lldb/test/API/python_api/type/main.cpp           |  5 +++++
 3 files changed, 23 insertions(+)

diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
index a771016039e851..d0033fcd9cdfcf 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -7106,6 +7106,8 @@ TypeSystemClang::GetDirectNestedTypeWithName(lldb::opaque_compiler_type_t type,
     for (NamedDecl *decl : record_decl->lookup(decl_name)) {
       if (auto *tag_decl = dyn_cast<clang::TagDecl>(decl))
         return GetType(getASTContext().getTagDeclType(tag_decl));
+      if (auto *typedef_decl = dyn_cast<clang::TypedefNameDecl>(decl))
+        return GetType(getASTContext().getTypedefType(typedef_decl));
     }
     break;
   }
diff --git a/lldb/test/API/python_api/type/TestTypeList.py b/lldb/test/API/python_api/type/TestTypeList.py
index 0498396903dcbd..b028929eea4423 100644
--- a/lldb/test/API/python_api/type/TestTypeList.py
+++ b/lldb/test/API/python_api/type/TestTypeList.py
@@ -273,6 +273,22 @@ def test(self):
             self.DebugSBType(int_enum_uchar)
             self.assertEqual(int_enum_uchar.GetName(), "unsigned char")
 
+    def test_nested_typedef(self):
+        """Exercise FindDirectNestedType for typedefs."""
+        self.build()
+        target = self.dbg.CreateTarget(self.getBuildArtifact())
+        self.assertTrue(target)
+
+        with_nested_typedef = target.FindFirstType("WithNestedTypedef")
+        self.assertTrue(with_nested_typedef)
+
+        # This is necessary to work around #91186
+        self.assertTrue(target.FindFirstGlobalVariable("typedefed_value").GetType())
+
+        the_typedef = with_nested_typedef.FindDirectNestedType("TheTypedef")
+        self.assertTrue(the_typedef)
+        self.assertEqual(the_typedef.GetTypedefedType().GetName(), "int")
+
     def test_GetByteAlign(self):
         """Exercise SBType::GetByteAlign"""
         self.build()
diff --git a/lldb/test/API/python_api/type/main.cpp b/lldb/test/API/python_api/type/main.cpp
index 986ed3009a15f6..6acde5bb666a6e 100644
--- a/lldb/test/API/python_api/type/main.cpp
+++ b/lldb/test/API/python_api/type/main.cpp
@@ -53,6 +53,11 @@ enum class EnumUChar : unsigned char {};
 struct alignas(128) OverAlignedStruct {};
 OverAlignedStruct over_aligned_struct;
 
+struct WithNestedTypedef {
+  typedef int TheTypedef;
+};
+WithNestedTypedef::TheTypedef typedefed_value;
+
 int main (int argc, char const *argv[])
 {
     Task *task_head = new Task(-1, NULL);



More information about the lldb-commits mailing list