[Lldb-commits] [lldb] [lldb] Avoid expression evaluation in the std::deque formatter (PR #127071)

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Thu Feb 13 07:13:25 PST 2025


https://github.com/labath created https://github.com/llvm/llvm-project/pull/127071

It's slower and it can fail in contexts where expression evaluation doesn't work.

>From 0bed8bfb406208b1934a5dc2c538e40891d86e1d Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Thu, 13 Feb 2025 16:09:36 +0100
Subject: [PATCH] [lldb] Avoid expression evaluation in the std::deque
 formatter

It's slower and it can fail in contexts where expression evaluation
doesn't work.
---
 lldb/examples/synthetic/libcxx.py | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/lldb/examples/synthetic/libcxx.py b/lldb/examples/synthetic/libcxx.py
index 7a71556e779df..5abeb3061f4f5 100644
--- a/lldb/examples/synthetic/libcxx.py
+++ b/lldb/examples/synthetic/libcxx.py
@@ -694,6 +694,13 @@ def get_child_index(self, name):
         except:
             return -1
 
+    @staticmethod
+    def _subscript(ptr: lldb.SBValue, idx: int, name: str) -> lldb.SBValue:
+        """Access a pointer value as if it was an array. Returns ptr[idx]."""
+        deref_t = ptr.GetType().GetPointeeType()
+        offset = idx * deref_t.GetByteSize()
+        return ptr.CreateChildAtOffset(name, offset, deref_t)
+
     def get_child_at_index(self, index):
         logger = lldb.formatters.Logger.Logger()
         logger.write("Fetching child " + str(index))
@@ -703,11 +710,8 @@ def get_child_at_index(self, index):
             return None
         try:
             i, j = divmod(self.start + index, self.block_size)
-
-            return self.first.CreateValueFromExpression(
-                "[" + str(index) + "]",
-                "*(*(%s + %d) + %d)" % (self.map_begin.get_expr_path(), i, j),
-            )
+            val = stddeque_SynthProvider._subscript(self.map_begin, i, "")
+            return stddeque_SynthProvider._subscript(val, j, f"[{index}]")
         except:
             return None
 



More information about the lldb-commits mailing list