[Lldb-commits] [lldb] [LLDB] Add array subscription and integer parsing to DIL (PR #138551)

Ilia Kuklin via lldb-commits lldb-commits at lists.llvm.org
Tue May 20 08:16:26 PDT 2025


================
@@ -272,4 +272,67 @@ Interpreter::Visit(const UnaryOpNode *node) {
       m_expr, "invalid ast: unexpected binary operator", node->GetLocation());
 }
 
+llvm::Expected<lldb::ValueObjectSP>
+Interpreter::Visit(const ArraySubscriptNode *node) {
+  auto lhs_or_err = Evaluate(node->GetBase());
+  if (!lhs_or_err) {
+    return lhs_or_err;
+  }
+  lldb::ValueObjectSP base = *lhs_or_err;
+  const llvm::APInt *index = node->GetIndex();
+
+  Status error;
+  if (base->GetCompilerType().IsReferenceType()) {
+    base = base->Dereference(error);
+    if (error.Fail())
+      return error.ToError();
+  }
+
+  // Check to see if 'base' has a synthetic value; if so, try using that.
+  uint64_t child_idx = index->getZExtValue();
+  if (base->HasSyntheticValue()) {
+    lldb::ValueObjectSP synthetic = base->GetSyntheticValue();
+    if (synthetic && synthetic != base) {
+      uint32_t num_children = synthetic->GetNumChildrenIgnoringErrors();
----------------
kuilpd wrote:

> One thing I can add that I noticed: that code worked when I tried it from console lldb, but didn't work on the same executable and input string when called from Python tests.

I didn't realize that the console executable got linked to GNU libstdc++ and subsequently used its data formatters. So, I'm guessing [GNU formatter](https://github.com/llvm/llvm-project/blob/621a5a976e2364677e57f1b3ab37417f2ed38fba/lldb/examples/synthetic/gnu_libstdcpp.py#L406) doesn't let you get the child with index out of bounds, but [LLDB formatter](https://github.com/llvm/llvm-project/blob/46ab684bddc812771226e53010642f3c846efe92/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp#L114) does. Technically, in runtime doing `vector[index_out_of_bounds]` returns 0, so neither is incorrect.

> I mean, I definitely agree that should be the high level functionality. My questions was more of like "whose responsibility is to make sure this happens?"

I guess if we want a specific behavior for our system, we need to check the index ourselves and not rely on the formatter. Maybe there are other uses of the formatter where out of bounds is expected to return 0.

https://github.com/llvm/llvm-project/pull/138551


More information about the lldb-commits mailing list