[Lldb-commits] [lldb] [lldb] Fix `FindDirectNestedType` not working with class templates (PR #81666)

Vlad Serebrennikov via lldb-commits lldb-commits at lists.llvm.org
Tue Feb 13 13:33:33 PST 2024


https://github.com/Endilll updated https://github.com/llvm/llvm-project/pull/81666

>From bf92dc89858668518a5d842eac34bdf1b3eaade2 Mon Sep 17 00:00:00 2001
From: Vlad Serebrennikov <serebrennikov.vladislav at gmail.com>
Date: Wed, 14 Feb 2024 00:26:09 +0300
Subject: [PATCH 1/2] [lldb] Fix `FindDirectNestedType` not working with class
 templates

This patch attempts to fix lookup in class template specialization that have an integral non-type template parameter of non-int type. unsigned 3 might be printed as `3` or `3U` depending on PrintingPolicy. This patch bring it in line with what Clang does (which addes the suffix).
---
 .../TypeSystem/Clang/TypeSystemClang.cpp        |  9 +++++++--
 lldb/test/API/python_api/type/TestTypeList.py   | 17 +++++++++++++++++
 lldb/test/API/python_api/type/main.cpp          | 13 +++++++++++++
 3 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
index a41e2c690853d2..e6a9d3f4f02836 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -2165,6 +2165,7 @@ PrintingPolicy TypeSystemClang::GetTypePrintingPolicy() {
   // (and we then would have suppressed them from the type name) and also setups
   // where LLDB wasn't able to reconstruct the default arguments.
   printing_policy.SuppressDefaultTemplateArgs = false;
+  printing_policy.AlwaysIncludeTypeForTemplateArgument = true;
   return printing_policy;
 }
 
@@ -9265,8 +9266,12 @@ ConstString TypeSystemClang::DeclContextGetName(void *opaque_decl_ctx) {
   if (opaque_decl_ctx) {
     clang::NamedDecl *named_decl =
         llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
-    if (named_decl)
-      return ConstString(named_decl->getName());
+    if (named_decl) {
+      std::string name;
+      llvm::raw_string_ostream stream{name};
+      named_decl->getNameForDiagnostic(stream, GetTypePrintingPolicy(), /*qualified=*/ false);
+      return ConstString(name);
+    }
   }
   return ConstString();
 }
diff --git a/lldb/test/API/python_api/type/TestTypeList.py b/lldb/test/API/python_api/type/TestTypeList.py
index c267defb58edf9..f874e87771c624 100644
--- a/lldb/test/API/python_api/type/TestTypeList.py
+++ b/lldb/test/API/python_api/type/TestTypeList.py
@@ -150,6 +150,23 @@ def test(self):
         invalid_type = task_type.FindDirectNestedType(None)
         self.assertFalse(invalid_type)
 
+        # Check that FindDirectNestedType works with types from AST
+        pointer = frame0.FindVariable("pointer")
+        pointer_type = pointer.GetType()
+        self.assertTrue(pointer_type)
+        self.DebugSBType(pointer_type)
+        pointer_info_type = pointer_type.template_args[0]
+        self.assertTrue(pointer_info_type)
+        self.DebugSBType(pointer_info_type)
+
+        pointer_masks1_type = pointer_info_type.FindDirectNestedType("Masks1")
+        self.assertTrue(pointer_masks1_type)
+        self.DebugSBType(pointer_masks1_type)
+
+        pointer_masks2_type = pointer_info_type.FindDirectNestedType("Masks2")
+        self.assertTrue(pointer_masks2_type)
+        self.DebugSBType(pointer_masks2_type)
+
         # We'll now get the child member 'id' from 'task_head'.
         id = task_head.GetChildMemberWithName("id")
         self.DebugSBValue(id)
diff --git a/lldb/test/API/python_api/type/main.cpp b/lldb/test/API/python_api/type/main.cpp
index 98de9707d88654..b587acdd96c590 100644
--- a/lldb/test/API/python_api/type/main.cpp
+++ b/lldb/test/API/python_api/type/main.cpp
@@ -34,6 +34,15 @@ class Task {
     {}
 };
 
+template <unsigned Value>
+struct PointerInfo {
+  enum Masks1 { pointer_mask };
+  enum class Masks2 { pointer_mask };
+};
+
+template <unsigned Value, typename InfoType = PointerInfo<Value>>
+struct Pointer {};
+
 enum EnumType {};
 enum class ScopedEnumType {};
 enum class EnumUChar : unsigned char {};
@@ -71,5 +80,9 @@ int main (int argc, char const *argv[])
     ScopedEnumType scoped_enum_type;
     EnumUChar scoped_enum_type_uchar;
 
+    Pointer<3> pointer;
+    PointerInfo<3>::Masks1 mask1 = PointerInfo<3>::Masks1::pointer_mask;
+    PointerInfo<3>::Masks2 mask2 = PointerInfo<3>::Masks2::pointer_mask;
+
     return 0; // Break at this line
 }

>From 153c8ec6abda4315c5bce6d088513a6ff63f26f9 Mon Sep 17 00:00:00 2001
From: Vlad Serebrennikov <serebrennikov.vladislav at gmail.com>
Date: Wed, 14 Feb 2024 00:33:17 +0300
Subject: [PATCH 2/2] Run clang-format

---
 lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp | 3 ++-
 lldb/test/API/python_api/type/main.cpp                   | 3 +--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
index e6a9d3f4f02836..0652ac0e134f53 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -9269,7 +9269,8 @@ ConstString TypeSystemClang::DeclContextGetName(void *opaque_decl_ctx) {
     if (named_decl) {
       std::string name;
       llvm::raw_string_ostream stream{name};
-      named_decl->getNameForDiagnostic(stream, GetTypePrintingPolicy(), /*qualified=*/ false);
+      named_decl->getNameForDiagnostic(stream, GetTypePrintingPolicy(),
+                                       /*qualified=*/false);
       return ConstString(name);
     }
   }
diff --git a/lldb/test/API/python_api/type/main.cpp b/lldb/test/API/python_api/type/main.cpp
index b587acdd96c590..391f58e3e5871c 100644
--- a/lldb/test/API/python_api/type/main.cpp
+++ b/lldb/test/API/python_api/type/main.cpp
@@ -34,8 +34,7 @@ class Task {
     {}
 };
 
-template <unsigned Value>
-struct PointerInfo {
+template <unsigned Value> struct PointerInfo {
   enum Masks1 { pointer_mask };
   enum class Masks2 { pointer_mask };
 };



More information about the lldb-commits mailing list