[Lldb-commits] [lldb] [lldb][Formatters] Do not recursively dereference pointer type when creating formatter candicates list. (PR #124048)

Zequan Wu via lldb-commits lldb-commits at lists.llvm.org
Thu Jan 23 10:42:43 PST 2025


https://github.com/ZequanWu updated https://github.com/llvm/llvm-project/pull/124048

>From 1948805894e006d84fbb78299574b3c7618959d8 Mon Sep 17 00:00:00 2001
From: Zequan Wu <zequanwu at google.com>
Date: Wed, 22 Jan 2025 18:32:11 -0800
Subject: [PATCH 1/2] [lldb][Formatters] Do not recursively dereference pointer
 type when creating formatter candicates list.

---
 lldb/include/lldb/DataFormatters/FormatManager.h     |  3 ++-
 lldb/source/DataFormatters/FormatManager.cpp         | 10 ++++++----
 .../ptr_ref_typedef/TestPtrRef2Typedef.py            | 12 ++++++++++++
 .../data-formatter/ptr_ref_typedef/main.cpp          |  3 +++
 4 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/lldb/include/lldb/DataFormatters/FormatManager.h b/lldb/include/lldb/DataFormatters/FormatManager.h
index db2fe99c44cafc..9329ee930125a6 100644
--- a/lldb/include/lldb/DataFormatters/FormatManager.h
+++ b/lldb/include/lldb/DataFormatters/FormatManager.h
@@ -163,7 +163,7 @@ class FormatManager : public IFormatChangeListener {
   GetPossibleMatches(ValueObject &valobj, lldb::DynamicValueType use_dynamic) {
     FormattersMatchVector matches;
     GetPossibleMatches(valobj, valobj.GetCompilerType(), use_dynamic, matches,
-                       FormattersMatchCandidate::Flags(), true);
+                       FormattersMatchCandidate::Flags(), true, true);
     return matches;
   }
 
@@ -180,6 +180,7 @@ class FormatManager : public IFormatChangeListener {
                                  lldb::DynamicValueType use_dynamic,
                                  FormattersMatchVector &entries,
                                  FormattersMatchCandidate::Flags current_flags,
+                                 bool dereference_ptr = true,
                                  bool root_level = false);
 
   std::atomic<uint32_t> m_last_revision;
diff --git a/lldb/source/DataFormatters/FormatManager.cpp b/lldb/source/DataFormatters/FormatManager.cpp
index 3b891cecb1c8b9..d6d6935f3e002c 100644
--- a/lldb/source/DataFormatters/FormatManager.cpp
+++ b/lldb/source/DataFormatters/FormatManager.cpp
@@ -174,7 +174,8 @@ void FormatManager::DisableAllCategories() {
 void FormatManager::GetPossibleMatches(
     ValueObject &valobj, CompilerType compiler_type,
     lldb::DynamicValueType use_dynamic, FormattersMatchVector &entries,
-    FormattersMatchCandidate::Flags current_flags, bool root_level) {
+    FormattersMatchCandidate::Flags current_flags, bool dereference_ptr,
+    bool root_level) {
   compiler_type = compiler_type.GetTypeForFormatters();
   ConstString type_name(compiler_type.GetTypeName());
   // A ValueObject that couldn't be made correctly won't necessarily have a
@@ -222,14 +223,15 @@ void FormatManager::GetPossibleMatches(
 
   if (compiler_type.IsPointerType()) {
     CompilerType non_ptr_type = compiler_type.GetPointeeType();
-    GetPossibleMatches(valobj, non_ptr_type, use_dynamic, entries,
-                       current_flags.WithStrippedPointer());
+    if (dereference_ptr)
+      GetPossibleMatches(valobj, non_ptr_type, use_dynamic, entries,
+                        current_flags.WithStrippedPointer(), false);
     if (non_ptr_type.IsTypedefType()) {
       CompilerType deffed_pointed_type =
           non_ptr_type.GetTypedefedType().GetPointerType();
       // this is not exactly the usual meaning of stripping typedefs
       GetPossibleMatches(valobj, deffed_pointed_type, use_dynamic, entries,
-                         current_flags.WithStrippedTypedef());
+                         current_flags.WithStrippedTypedef(), dereference_ptr);
     }
   }
 
diff --git a/lldb/test/API/functionalities/data-formatter/ptr_ref_typedef/TestPtrRef2Typedef.py b/lldb/test/API/functionalities/data-formatter/ptr_ref_typedef/TestPtrRef2Typedef.py
index f70162bf285839..fea40252a2a75c 100644
--- a/lldb/test/API/functionalities/data-formatter/ptr_ref_typedef/TestPtrRef2Typedef.py
+++ b/lldb/test/API/functionalities/data-formatter/ptr_ref_typedef/TestPtrRef2Typedef.py
@@ -53,3 +53,15 @@ def cleanup():
         # the match.
         self.expect("frame variable y", substrs=["(Foo &", ") y = 0x", "IntLRef"])
         self.expect("frame variable z", substrs=["(Foo &&", ") z = 0x", "IntRRef"])
+
+        # Test lldb doesn't dereference pointer more than once.
+        # xp has type Foo**, so it can only uses summary for Foo* or int*, not 
+        # summary for Foo or int.
+        self.expect("frame variable xp", substrs=["(Foo **) xp = 0x", "IntPointer"])
+
+        self.runCmd('type summary delete "int *"')
+        self.runCmd('type summary add --cascade true -s "MyInt" "int"')
+        self.expect("frame variable xp", substrs=["(Foo **) xp = 0x"])
+
+        self.runCmd('type summary add --cascade true -s "FooPointer" "Foo *"')
+        self.expect("frame variable xp", substrs=["(Foo **) xp = 0x", "FooPointer"])
diff --git a/lldb/test/API/functionalities/data-formatter/ptr_ref_typedef/main.cpp b/lldb/test/API/functionalities/data-formatter/ptr_ref_typedef/main.cpp
index 13102106551086..41b1d282344b91 100644
--- a/lldb/test/API/functionalities/data-formatter/ptr_ref_typedef/main.cpp
+++ b/lldb/test/API/functionalities/data-formatter/ptr_ref_typedef/main.cpp
@@ -5,6 +5,9 @@ int main() {
 	Foo* x = &lval;
 	Foo& y = lval;
 	Foo&& z = 1;
+
+	// Test lldb doesn't dereference pointer more than once.
+	Foo** xp = &x;
 	return 0; // Set breakpoint here
 }
 

>From 83197bd326521352d145e4ef64edee7e5eaf41b5 Mon Sep 17 00:00:00 2001
From: Zequan Wu <zequanwu at google.com>
Date: Thu, 23 Jan 2025 10:41:41 -0800
Subject: [PATCH 2/2] remove unnecessary flag and format.

---
 lldb/include/lldb/DataFormatters/FormatManager.h  |  3 +--
 lldb/source/DataFormatters/FormatManager.cpp      |  9 ++++-----
 .../data-formatter/ptr_ref_typedef/main.cpp       | 15 +++++++--------
 3 files changed, 12 insertions(+), 15 deletions(-)

diff --git a/lldb/include/lldb/DataFormatters/FormatManager.h b/lldb/include/lldb/DataFormatters/FormatManager.h
index 9329ee930125a6..db2fe99c44cafc 100644
--- a/lldb/include/lldb/DataFormatters/FormatManager.h
+++ b/lldb/include/lldb/DataFormatters/FormatManager.h
@@ -163,7 +163,7 @@ class FormatManager : public IFormatChangeListener {
   GetPossibleMatches(ValueObject &valobj, lldb::DynamicValueType use_dynamic) {
     FormattersMatchVector matches;
     GetPossibleMatches(valobj, valobj.GetCompilerType(), use_dynamic, matches,
-                       FormattersMatchCandidate::Flags(), true, true);
+                       FormattersMatchCandidate::Flags(), true);
     return matches;
   }
 
@@ -180,7 +180,6 @@ class FormatManager : public IFormatChangeListener {
                                  lldb::DynamicValueType use_dynamic,
                                  FormattersMatchVector &entries,
                                  FormattersMatchCandidate::Flags current_flags,
-                                 bool dereference_ptr = true,
                                  bool root_level = false);
 
   std::atomic<uint32_t> m_last_revision;
diff --git a/lldb/source/DataFormatters/FormatManager.cpp b/lldb/source/DataFormatters/FormatManager.cpp
index d6d6935f3e002c..e53685ac511b6d 100644
--- a/lldb/source/DataFormatters/FormatManager.cpp
+++ b/lldb/source/DataFormatters/FormatManager.cpp
@@ -174,8 +174,7 @@ void FormatManager::DisableAllCategories() {
 void FormatManager::GetPossibleMatches(
     ValueObject &valobj, CompilerType compiler_type,
     lldb::DynamicValueType use_dynamic, FormattersMatchVector &entries,
-    FormattersMatchCandidate::Flags current_flags, bool dereference_ptr,
-    bool root_level) {
+    FormattersMatchCandidate::Flags current_flags, bool root_level) {
   compiler_type = compiler_type.GetTypeForFormatters();
   ConstString type_name(compiler_type.GetTypeName());
   // A ValueObject that couldn't be made correctly won't necessarily have a
@@ -223,15 +222,15 @@ void FormatManager::GetPossibleMatches(
 
   if (compiler_type.IsPointerType()) {
     CompilerType non_ptr_type = compiler_type.GetPointeeType();
-    if (dereference_ptr)
+    if (!current_flags.stripped_pointer)
       GetPossibleMatches(valobj, non_ptr_type, use_dynamic, entries,
-                        current_flags.WithStrippedPointer(), false);
+                         current_flags.WithStrippedPointer(), false);
     if (non_ptr_type.IsTypedefType()) {
       CompilerType deffed_pointed_type =
           non_ptr_type.GetTypedefedType().GetPointerType();
       // this is not exactly the usual meaning of stripping typedefs
       GetPossibleMatches(valobj, deffed_pointed_type, use_dynamic, entries,
-                         current_flags.WithStrippedTypedef(), dereference_ptr);
+                         current_flags.WithStrippedTypedef());
     }
   }
 
diff --git a/lldb/test/API/functionalities/data-formatter/ptr_ref_typedef/main.cpp b/lldb/test/API/functionalities/data-formatter/ptr_ref_typedef/main.cpp
index 41b1d282344b91..a2f235a48dc443 100644
--- a/lldb/test/API/functionalities/data-formatter/ptr_ref_typedef/main.cpp
+++ b/lldb/test/API/functionalities/data-formatter/ptr_ref_typedef/main.cpp
@@ -1,13 +1,12 @@
 typedef int Foo;
 
 int main() {
-	int lval = 1;
-	Foo* x = &lval;
-	Foo& y = lval;
-	Foo&& z = 1;
+  int lval = 1;
+  Foo *x = &lval;
+  Foo &y = lval;
+  Foo &&z = 1;
 
-	// Test lldb doesn't dereference pointer more than once.
-	Foo** xp = &x;
-	return 0; // Set breakpoint here
+  // Test lldb doesn't dereference pointer more than once.
+  Foo **xp = &x;
+  return 0; // Set breakpoint here
 }
-



More information about the lldb-commits mailing list