[Lldb-commits] [lldb] [LLDB] Add DIL code for handling plain variable names. (PR #120971)
Pavel Labath via lldb-commits
lldb-commits at lists.llvm.org
Mon Feb 24 01:49:53 PST 2025
================
@@ -0,0 +1,244 @@
+//===-- DILEval.cpp -------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/ValueObject/DILEval.h"
+#include "lldb/Symbol/VariableList.h"
+#include "lldb/Target/RegisterContext.h"
+#include "lldb/ValueObject/DILAST.h"
+#include "lldb/ValueObject/ValueObject.h"
+#include "lldb/ValueObject/ValueObjectRegister.h"
+#include "lldb/ValueObject/ValueObjectVariable.h"
+#include "llvm/Support/FormatAdapters.h"
+#include <memory>
+
+namespace lldb_private::dil {
+
+static lldb::ValueObjectSP
+LookupStaticIdentifier(VariableList &variable_list,
+ std::shared_ptr<ExecutionContextScope> exe_scope,
+ llvm::StringRef name_ref,
+ llvm::StringRef unqualified_name) {
+ // First look for an exact match to the (possibly) qualified name.
+ for (const lldb::VariableSP &var_sp : variable_list) {
+ lldb::ValueObjectSP valobj_sp(
+ ValueObjectVariable::Create(exe_scope.get(), var_sp));
+ if (valobj_sp && valobj_sp->GetVariable() &&
+ (valobj_sp->GetVariable()->NameMatches(ConstString(name_ref))))
+ return valobj_sp;
+ }
+
+ // If the qualified name is the same as the unqualfied name, there's nothing
+ // more to be done.
+ if (name_ref == unqualified_name)
+ return nullptr;
+
+ // We didn't match the qualified name; try to match the unqualified name.
+ for (const lldb::VariableSP &var_sp : variable_list) {
+ lldb::ValueObjectSP valobj_sp(
+ ValueObjectVariable::Create(exe_scope.get(), var_sp));
+ if (valobj_sp && valobj_sp->GetVariable() &&
+ (valobj_sp->GetVariable()->NameMatches(ConstString(unqualified_name))))
+ return valobj_sp;
+ }
+
+ return nullptr;
+}
+
+static lldb::VariableSP DILFindVariable(ConstString name,
+ VariableList *variable_list) {
+ lldb::VariableSP exact_match;
+ std::vector<lldb::VariableSP> possible_matches;
+
+ typedef std::vector<lldb::VariableSP> collection;
+ typedef collection::iterator iterator;
+
+ iterator pos, end = variable_list->end();
+ for (pos = variable_list->begin(); pos != end; ++pos) {
+ llvm::StringRef str_ref_name = pos->get()->GetName().GetStringRef();
+ // Check for global vars, which might start with '::'.
+ str_ref_name.consume_front("::");
+
+ if (str_ref_name == name.GetStringRef())
+ possible_matches.push_back(*pos);
+ else if (pos->get()->NameMatches(name))
+ possible_matches.push_back(*pos);
+ }
+
+ // Look for exact matches (favors local vars over global vars)
+ auto exact_match_it =
+ llvm::find_if(possible_matches, [&](lldb::VariableSP var_sp) {
+ return var_sp->GetName() == name;
+ });
+
+ if (exact_match_it != llvm::adl_end(possible_matches))
+ exact_match = *exact_match_it;
+
+ if (!exact_match)
+ // Look for a global var exact match.
+ for (auto var_sp : possible_matches) {
+ llvm::StringRef str_ref_name = var_sp->GetName().GetStringRef();
+ str_ref_name.consume_front("::");
+ if (str_ref_name == name.GetStringRef()) {
+ exact_match = var_sp;
+ break;
+ }
+ }
+
+ if (!exact_match && possible_matches.size() == 1)
+ exact_match = possible_matches[0];
+
+ return exact_match;
+}
+
+std::unique_ptr<IdentifierInfo>
+LookupIdentifier(llvm::StringRef name_ref,
+ std::shared_ptr<ExecutionContextScope> ctx_scope,
+ lldb::TargetSP target_sp, lldb::DynamicValueType use_dynamic,
+ CompilerType *scope_ptr) {
+ // Support $rax as a special syntax for accessing registers.
+ // Will return an invalid value in case the requested register doesn't exist.
+ if (name_ref.consume_front("$")) {
+ lldb::ValueObjectSP value_sp;
+ Process *process = ctx_scope->CalculateProcess().get();
+ if (!target_sp || !process)
+ return nullptr;
+
+ StackFrame *stack_frame = (StackFrame *)ctx_scope.get();
----------------
labath wrote:
This is sort of better, but not exactly what I wanted. There are two options here: one is to call `ctx_scope->CalculateStackFrame`, the other is to actually plumb the StackFrame object (with the right type) from `StackFrame::DILGetValueForVariableExpressionPath`. I think the choice depends on whether this will ever be called from a context which does not have a stack frame. Since this is going to be replacing `target variable` eventually, the first choice is probably better.
https://github.com/llvm/llvm-project/pull/120971
More information about the lldb-commits
mailing list