7
7
// ===----------------------------------------------------------------------===//
8
8
9
9
#include " lldb/ValueObject/DILEval.h"
10
+ #include " lldb/Symbol/CompileUnit.h"
10
11
#include " lldb/Symbol/VariableList.h"
11
12
#include " lldb/Target/RegisterContext.h"
12
13
#include " lldb/ValueObject/DILAST.h"
18
19
19
20
namespace lldb_private ::dil {
20
21
21
- static lldb::ValueObjectSP LookupStaticIdentifier (
22
- VariableList &variable_list, std::shared_ptr<StackFrame> exe_scope,
23
- llvm::StringRef name_ref, llvm::StringRef unqualified_name) {
24
- // First look for an exact match to the (possibly) qualified name.
25
- for (const lldb::VariableSP &var_sp : variable_list) {
26
- lldb::ValueObjectSP valobj_sp (
27
- ValueObjectVariable::Create (exe_scope.get (), var_sp));
28
- if (valobj_sp && valobj_sp->GetVariable () &&
29
- (valobj_sp->GetVariable ()->NameMatches (ConstString (name_ref))))
30
- return valobj_sp;
31
- }
32
-
33
- // If the qualified name is the same as the unqualfied name, there's nothing
34
- // more to be done.
35
- if (name_ref == unqualified_name)
36
- return nullptr ;
37
-
38
- // We didn't match the qualified name; try to match the unqualified name.
39
- for (const lldb::VariableSP &var_sp : variable_list) {
40
- lldb::ValueObjectSP valobj_sp (
41
- ValueObjectVariable::Create (exe_scope.get (), var_sp));
42
- if (valobj_sp && valobj_sp->GetVariable () &&
43
- (valobj_sp->GetVariable ()->NameMatches (ConstString (unqualified_name))))
44
- return valobj_sp;
45
- }
46
-
47
- return nullptr ;
48
- }
49
-
50
22
static lldb::VariableSP DILFindVariable (ConstString name,
51
- lldb::VariableListSP variable_list) {
23
+ VariableList & variable_list) {
52
24
lldb::VariableSP exact_match;
53
25
std::vector<lldb::VariableSP> possible_matches;
54
26
55
- for (lldb::VariableSP var_sp : * variable_list) {
27
+ for (lldb::VariableSP var_sp : variable_list) {
56
28
llvm::StringRef str_ref_name = var_sp->GetName ().GetStringRef ();
57
- // Check for global vars, which might start with '::'.
58
- str_ref_name.consume_front (" ::" );
59
29
60
- if (str_ref_name == name.GetStringRef ())
61
- possible_matches.push_back (var_sp);
62
- else if (var_sp->NameMatches (name))
63
- possible_matches.push_back (var_sp);
64
- }
65
-
66
- // Look for exact matches (favors local vars over global vars)
67
- auto exact_match_it =
68
- llvm::find_if (possible_matches, [&](lldb::VariableSP var_sp) {
69
- return var_sp->GetName () == name;
70
- });
71
-
72
- if (exact_match_it != possible_matches.end ())
73
- return *exact_match_it;
74
-
75
- // Look for a global var exact match.
76
- for (auto var_sp : possible_matches) {
77
- llvm::StringRef str_ref_name = var_sp->GetName ().GetStringRef ();
78
30
str_ref_name.consume_front (" ::" );
31
+ // Check for the exact same match
79
32
if (str_ref_name == name.GetStringRef ())
80
33
return var_sp;
34
+
35
+ // Check for possible matches by base name
36
+ if (var_sp->NameMatches (name))
37
+ possible_matches.push_back (var_sp);
81
38
}
82
39
83
- // If there's a single non-exact match, take it.
84
- if (possible_matches.size () == 1 )
40
+ // If there's a non-exact match, take it.
41
+ if (possible_matches.size () > 0 )
85
42
return possible_matches[0 ];
86
43
87
44
return nullptr ;
88
45
}
89
46
90
47
lldb::ValueObjectSP LookupGlobalIdentifier (
91
48
llvm::StringRef name_ref, std::shared_ptr<StackFrame> stack_frame,
92
- lldb::TargetSP target_sp, lldb::DynamicValueType use_dynamic,
93
- CompilerType *scope_ptr) {
94
- // First look for match in "local" global variables.
95
- lldb::VariableListSP variable_list (stack_frame->GetInScopeVariableList (true ));
96
- name_ref.consume_front (" ::" );
49
+ lldb::TargetSP target_sp, lldb::DynamicValueType use_dynamic) {
50
+ // Get a global variables list without the locals from the current frame
51
+ SymbolContext symbol_context =
52
+ stack_frame->GetSymbolContext (lldb::eSymbolContextCompUnit);
53
+ lldb::VariableListSP variable_list =
54
+ symbol_context.comp_unit ->GetVariableList (true );
97
55
56
+ name_ref.consume_front (" ::" );
98
57
lldb::ValueObjectSP value_sp;
99
58
if (variable_list) {
100
59
lldb::VariableSP var_sp =
101
- DILFindVariable (ConstString (name_ref), variable_list);
60
+ DILFindVariable (ConstString (name_ref), * variable_list);
102
61
if (var_sp)
103
62
value_sp =
104
63
stack_frame->GetValueObjectForFrameVariable (var_sp, use_dynamic);
105
64
}
106
65
107
- if (value_sp)
108
- return value_sp;
109
-
110
- // Also check for static global vars.
111
- if (variable_list) {
112
- const char *type_name = " " ;
113
- if (scope_ptr)
114
- type_name = scope_ptr->GetCanonicalType ().GetTypeName ().AsCString ();
115
- std::string name_with_type_prefix =
116
- llvm::formatv (" {0}::{1}" , type_name, name_ref).str ();
117
- value_sp = LookupStaticIdentifier (*variable_list, stack_frame,
118
- name_with_type_prefix, name_ref);
119
- if (!value_sp)
120
- value_sp = LookupStaticIdentifier (*variable_list, stack_frame, name_ref,
121
- name_ref);
122
- }
123
-
124
66
if (value_sp)
125
67
return value_sp;
126
68
@@ -129,28 +71,22 @@ lldb::ValueObjectSP LookupGlobalIdentifier(
129
71
target_sp->GetImages ().FindGlobalVariables (
130
72
ConstString (name_ref), std::numeric_limits<uint32_t >::max (),
131
73
modules_var_list);
132
- if (modules_var_list.Empty ())
133
- return nullptr ;
134
74
135
- for ( const lldb::VariableSP &var_sp : modules_var_list) {
136
- std::string qualified_name = llvm::formatv ( " ::{0} " , name_ref). str ();
137
- if (var_sp-> NameMatches ( ConstString (name_ref)) ||
138
- var_sp-> NameMatches ( ConstString (qualified_name))) {
75
+ if (! modules_var_list. Empty () ) {
76
+ lldb::VariableSP var_sp =
77
+ DILFindVariable ( ConstString (name_ref), modules_var_list);
78
+ if (var_sp)
139
79
value_sp = ValueObjectVariable::Create (stack_frame.get (), var_sp);
140
- break ;
141
- }
142
- }
143
-
144
- if (value_sp)
145
- return value_sp;
146
80
81
+ if (value_sp)
82
+ return value_sp;
83
+ }
147
84
return nullptr ;
148
85
}
149
86
150
87
lldb::ValueObjectSP LookupIdentifier (llvm::StringRef name_ref,
151
88
std::shared_ptr<StackFrame> stack_frame,
152
- lldb::DynamicValueType use_dynamic,
153
- CompilerType *scope_ptr) {
89
+ lldb::DynamicValueType use_dynamic) {
154
90
// Support $rax as a special syntax for accessing registers.
155
91
// Will return an invalid value in case the requested register doesn't exist.
156
92
if (name_ref.consume_front (" $" )) {
@@ -164,38 +100,34 @@ lldb::ValueObjectSP LookupIdentifier(llvm::StringRef name_ref,
164
100
return nullptr ;
165
101
}
166
102
167
- lldb::VariableListSP variable_list (
168
- stack_frame->GetInScopeVariableList (false ));
169
-
170
103
if (!name_ref.contains (" ::" )) {
171
- if (!scope_ptr || !scope_ptr->IsValid ()) {
172
- // Lookup in the current frame.
173
- // Try looking for a local variable in current scope.
174
- lldb::ValueObjectSP value_sp;
175
- if (variable_list) {
176
- lldb::VariableSP var_sp =
177
- DILFindVariable (ConstString (name_ref), variable_list);
178
- if (var_sp)
179
- value_sp =
180
- stack_frame->GetValueObjectForFrameVariable (var_sp, use_dynamic);
181
- }
182
- if (!value_sp)
183
- value_sp = stack_frame->FindVariable (ConstString (name_ref));
184
-
185
- if (value_sp)
186
- return value_sp;
187
-
188
- // Try looking for an instance variable (class member).
189
- SymbolContext sc = stack_frame->GetSymbolContext (
190
- lldb::eSymbolContextFunction | lldb::eSymbolContextBlock);
191
- llvm::StringRef ivar_name = sc.GetInstanceVariableName ();
192
- value_sp = stack_frame->FindVariable (ConstString (ivar_name));
193
- if (value_sp)
194
- value_sp = value_sp->GetChildMemberWithName (name_ref);
195
-
196
- if (value_sp)
197
- return value_sp;
104
+ // Lookup in the current frame.
105
+ // Try looking for a local variable in current scope.
106
+ lldb::VariableListSP variable_list (
107
+ stack_frame->GetInScopeVariableList (false ));
108
+
109
+ lldb::ValueObjectSP value_sp;
110
+ if (variable_list) {
111
+ lldb::VariableSP var_sp =
112
+ variable_list->FindVariable (ConstString (name_ref));
113
+ if (var_sp)
114
+ value_sp =
115
+ stack_frame->GetValueObjectForFrameVariable (var_sp, use_dynamic);
198
116
}
117
+
118
+ if (value_sp)
119
+ return value_sp;
120
+
121
+ // Try looking for an instance variable (class member).
122
+ SymbolContext sc = stack_frame->GetSymbolContext (
123
+ lldb::eSymbolContextFunction | lldb::eSymbolContextBlock);
124
+ llvm::StringRef ivar_name = sc.GetInstanceVariableName ();
125
+ value_sp = stack_frame->FindVariable (ConstString (ivar_name));
126
+ if (value_sp)
127
+ value_sp = value_sp->GetChildMemberWithName (name_ref);
128
+
129
+ if (value_sp)
130
+ return value_sp;
199
131
}
200
132
return nullptr ;
201
133
}
0 commit comments