Skip to content

[lldb] Avoid expression evaluation in the std::deque formatter #127071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2025

Conversation

labath
Copy link
Collaborator

@labath labath commented Feb 13, 2025

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

It's slower and it can fail in contexts where expression evaluation
doesn't work.
@labath labath requested a review from jimingham February 13, 2025 15:13
@labath labath requested a review from JDevlieghere as a code owner February 13, 2025 15:13
@llvmbot llvmbot added the lldb label Feb 13, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 13, 2025

@llvm/pr-subscribers-lldb

Author: Pavel Labath (labath)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/127071.diff

1 Files Affected:

  • (modified) lldb/examples/synthetic/libcxx.py (+9-5)
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
 

Copy link
Collaborator

@jimingham jimingham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - you really really should avoid expressions in data formatters if you can. So even in examples we shouldn't be showing that.

@labath labath merged commit 0949330 into llvm:main Feb 14, 2025
9 checks passed
@labath labath deleted the deque branch February 14, 2025 07:51
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
…127071)

It's slower and it can fail in contexts where expression evaluation
doesn't work.
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
…127071)

It's slower and it can fail in contexts where expression evaluation
doesn't work.
Michael137 pushed a commit to swiftlang/llvm-project that referenced this pull request Jun 18, 2025
…127071)

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

(cherry picked from commit 0949330)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants