1 //===-- Variable.cpp --------------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Symbol/Variable.h"
11
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/ValueObject.h"
14 #include "lldb/Core/ValueObjectVariable.h"
15 #include "lldb/Symbol/Block.h"
16 #include "lldb/Symbol/CompileUnit.h"
17 #include "lldb/Symbol/CompilerDecl.h"
18 #include "lldb/Symbol/CompilerDeclContext.h"
19 #include "lldb/Symbol/Function.h"
20 #include "lldb/Symbol/SymbolContext.h"
21 #include "lldb/Symbol/SymbolFile.h"
22 #include "lldb/Symbol/Type.h"
23 #include "lldb/Symbol/TypeSystem.h"
24 #include "lldb/Symbol/VariableList.h"
25 #include "lldb/Target/ABI.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/RegisterContext.h"
28 #include "lldb/Target/StackFrame.h"
29 #include "lldb/Target/Target.h"
30 #include "lldb/Target/Thread.h"
31 #include "lldb/Utility/RegularExpression.h"
32 #include "lldb/Utility/Stream.h"
33
34 #include "llvm/ADT/Twine.h"
35
36 using namespace lldb;
37 using namespace lldb_private;
38
39 //----------------------------------------------------------------------
40 // Variable constructor
41 //----------------------------------------------------------------------
Variable(lldb::user_id_t uid,const char * name,const char * mangled,const lldb::SymbolFileTypeSP & symfile_type_sp,ValueType scope,SymbolContextScope * context,const RangeList & scope_range,Declaration * decl_ptr,const DWARFExpression & location,bool external,bool artificial,bool static_member)42 Variable::Variable(
43 lldb::user_id_t uid, const char *name,
44 const char *mangled, // The mangled or fully qualified name of the variable.
45 const lldb::SymbolFileTypeSP &symfile_type_sp, ValueType scope,
46 SymbolContextScope *context, const RangeList &scope_range,
47 Declaration *decl_ptr, const DWARFExpression &location, bool external,
48 bool artificial, bool static_member)
49 : UserID(uid), m_name(name), m_mangled(ConstString(mangled)),
50 m_symfile_type_sp(symfile_type_sp), m_scope(scope),
51 m_owner_scope(context), m_scope_range(scope_range),
52 m_declaration(decl_ptr), m_location(location), m_external(external),
53 m_artificial(artificial), m_loc_is_const_data(false),
54 m_static_member(static_member) {}
55
56 //----------------------------------------------------------------------
57 // Destructor
58 //----------------------------------------------------------------------
~Variable()59 Variable::~Variable() {}
60
GetLanguage() const61 lldb::LanguageType Variable::GetLanguage() const {
62 SymbolContext variable_sc;
63 m_owner_scope->CalculateSymbolContext(&variable_sc);
64 if (variable_sc.comp_unit)
65 return variable_sc.comp_unit->GetLanguage();
66 return lldb::eLanguageTypeUnknown;
67 }
68
GetName() const69 ConstString Variable::GetName() const {
70 ConstString name = m_mangled.GetName(GetLanguage());
71 if (name)
72 return name;
73 return m_name;
74 }
75
GetUnqualifiedName() const76 ConstString Variable::GetUnqualifiedName() const { return m_name; }
77
NameMatches(const ConstString & name) const78 bool Variable::NameMatches(const ConstString &name) const {
79 if (m_name == name)
80 return true;
81 SymbolContext variable_sc;
82 m_owner_scope->CalculateSymbolContext(&variable_sc);
83
84 LanguageType language = eLanguageTypeUnknown;
85 if (variable_sc.comp_unit)
86 language = variable_sc.comp_unit->GetLanguage();
87 return m_mangled.NameMatches(name, language);
88 }
NameMatches(const RegularExpression & regex) const89 bool Variable::NameMatches(const RegularExpression ®ex) const {
90 if (regex.Execute(m_name.AsCString()))
91 return true;
92 if (m_mangled)
93 return m_mangled.NameMatches(regex, GetLanguage());
94 return false;
95 }
96
GetType()97 Type *Variable::GetType() {
98 if (m_symfile_type_sp)
99 return m_symfile_type_sp->GetType();
100 return nullptr;
101 }
102
Dump(Stream * s,bool show_context) const103 void Variable::Dump(Stream *s, bool show_context) const {
104 s->Printf("%p: ", static_cast<const void *>(this));
105 s->Indent();
106 *s << "Variable" << (const UserID &)*this;
107
108 if (m_name)
109 *s << ", name = \"" << m_name << "\"";
110
111 if (m_symfile_type_sp) {
112 Type *type = m_symfile_type_sp->GetType();
113 if (type) {
114 *s << ", type = {" << type->GetID() << "} " << (void *)type << " (";
115 type->DumpTypeName(s);
116 s->PutChar(')');
117 }
118 }
119
120 if (m_scope != eValueTypeInvalid) {
121 s->PutCString(", scope = ");
122 switch (m_scope) {
123 case eValueTypeVariableGlobal:
124 s->PutCString(m_external ? "global" : "static");
125 break;
126 case eValueTypeVariableArgument:
127 s->PutCString("parameter");
128 break;
129 case eValueTypeVariableLocal:
130 s->PutCString("local");
131 break;
132 case eValueTypeVariableThreadLocal:
133 s->PutCString("thread local");
134 break;
135 default:
136 *s << "??? (" << m_scope << ')';
137 }
138 }
139
140 if (show_context && m_owner_scope != nullptr) {
141 s->PutCString(", context = ( ");
142 m_owner_scope->DumpSymbolContext(s);
143 s->PutCString(" )");
144 }
145
146 bool show_fullpaths = false;
147 m_declaration.Dump(s, show_fullpaths);
148
149 if (m_location.IsValid()) {
150 s->PutCString(", location = ");
151 lldb::addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
152 if (m_location.IsLocationList()) {
153 SymbolContext variable_sc;
154 m_owner_scope->CalculateSymbolContext(&variable_sc);
155 if (variable_sc.function)
156 loclist_base_addr = variable_sc.function->GetAddressRange()
157 .GetBaseAddress()
158 .GetFileAddress();
159 }
160 ABISP abi;
161 if (m_owner_scope) {
162 ModuleSP module_sp(m_owner_scope->CalculateSymbolContextModule());
163 if (module_sp)
164 abi = ABI::FindPlugin(ProcessSP(), module_sp->GetArchitecture());
165 }
166 m_location.GetDescription(s, lldb::eDescriptionLevelBrief,
167 loclist_base_addr, abi.get());
168 }
169
170 if (m_external)
171 s->PutCString(", external");
172
173 if (m_artificial)
174 s->PutCString(", artificial");
175
176 s->EOL();
177 }
178
DumpDeclaration(Stream * s,bool show_fullpaths,bool show_module)179 bool Variable::DumpDeclaration(Stream *s, bool show_fullpaths,
180 bool show_module) {
181 bool dumped_declaration_info = false;
182 if (m_owner_scope) {
183 SymbolContext sc;
184 m_owner_scope->CalculateSymbolContext(&sc);
185 sc.block = nullptr;
186 sc.line_entry.Clear();
187 bool show_inlined_frames = false;
188 const bool show_function_arguments = true;
189 const bool show_function_name = true;
190
191 dumped_declaration_info = sc.DumpStopContext(
192 s, nullptr, Address(), show_fullpaths, show_module, show_inlined_frames,
193 show_function_arguments, show_function_name);
194
195 if (sc.function)
196 s->PutChar(':');
197 }
198 if (m_declaration.DumpStopContext(s, false))
199 dumped_declaration_info = true;
200 return dumped_declaration_info;
201 }
202
MemorySize() const203 size_t Variable::MemorySize() const { return sizeof(Variable); }
204
GetDeclContext()205 CompilerDeclContext Variable::GetDeclContext() {
206 Type *type = GetType();
207 if (type)
208 return type->GetSymbolFile()->GetDeclContextContainingUID(GetID());
209 return CompilerDeclContext();
210 }
211
GetDecl()212 CompilerDecl Variable::GetDecl() {
213 Type *type = GetType();
214 return type ? type->GetSymbolFile()->GetDeclForUID(GetID()) : CompilerDecl();
215 }
216
CalculateSymbolContext(SymbolContext * sc)217 void Variable::CalculateSymbolContext(SymbolContext *sc) {
218 if (m_owner_scope) {
219 m_owner_scope->CalculateSymbolContext(sc);
220 sc->variable = this;
221 } else
222 sc->Clear(false);
223 }
224
LocationIsValidForFrame(StackFrame * frame)225 bool Variable::LocationIsValidForFrame(StackFrame *frame) {
226 // Is the variable is described by a single location?
227 if (!m_location.IsLocationList()) {
228 // Yes it is, the location is valid.
229 return true;
230 }
231
232 if (frame) {
233 Function *function =
234 frame->GetSymbolContext(eSymbolContextFunction).function;
235 if (function) {
236 TargetSP target_sp(frame->CalculateTarget());
237
238 addr_t loclist_base_load_addr =
239 function->GetAddressRange().GetBaseAddress().GetLoadAddress(
240 target_sp.get());
241 if (loclist_base_load_addr == LLDB_INVALID_ADDRESS)
242 return false;
243 // It is a location list. We just need to tell if the location list
244 // contains the current address when converted to a load address
245 return m_location.LocationListContainsAddress(
246 loclist_base_load_addr,
247 frame->GetFrameCodeAddress().GetLoadAddress(target_sp.get()));
248 }
249 }
250 return false;
251 }
252
LocationIsValidForAddress(const Address & address)253 bool Variable::LocationIsValidForAddress(const Address &address) {
254 // Be sure to resolve the address to section offset prior to calling this
255 // function.
256 if (address.IsSectionOffset()) {
257 SymbolContext sc;
258 CalculateSymbolContext(&sc);
259 if (sc.module_sp == address.GetModule()) {
260 // Is the variable is described by a single location?
261 if (!m_location.IsLocationList()) {
262 // Yes it is, the location is valid.
263 return true;
264 }
265
266 if (sc.function) {
267 addr_t loclist_base_file_addr =
268 sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
269 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS)
270 return false;
271 // It is a location list. We just need to tell if the location list
272 // contains the current address when converted to a load address
273 return m_location.LocationListContainsAddress(loclist_base_file_addr,
274 address.GetFileAddress());
275 }
276 }
277 }
278 return false;
279 }
280
IsInScope(StackFrame * frame)281 bool Variable::IsInScope(StackFrame *frame) {
282 switch (m_scope) {
283 case eValueTypeRegister:
284 case eValueTypeRegisterSet:
285 return frame != nullptr;
286
287 case eValueTypeConstResult:
288 case eValueTypeVariableGlobal:
289 case eValueTypeVariableStatic:
290 case eValueTypeVariableThreadLocal:
291 return true;
292
293 case eValueTypeVariableArgument:
294 case eValueTypeVariableLocal:
295 if (frame) {
296 // We don't have a location list, we just need to see if the block that
297 // this variable was defined in is currently
298 Block *deepest_frame_block =
299 frame->GetSymbolContext(eSymbolContextBlock).block;
300 if (deepest_frame_block) {
301 SymbolContext variable_sc;
302 CalculateSymbolContext(&variable_sc);
303
304 // Check for static or global variable defined at the compile unit
305 // level that wasn't defined in a block
306 if (variable_sc.block == nullptr)
307 return true;
308
309 // Check if the variable is valid in the current block
310 if (variable_sc.block != deepest_frame_block &&
311 !variable_sc.block->Contains(deepest_frame_block))
312 return false;
313
314 // If no scope range is specified then it means that the scope is the
315 // same as the scope of the enclosing lexical block.
316 if (m_scope_range.IsEmpty())
317 return true;
318
319 addr_t file_address = frame->GetFrameCodeAddress().GetFileAddress();
320 return m_scope_range.FindEntryThatContains(file_address) != nullptr;
321 }
322 }
323 break;
324
325 default:
326 break;
327 }
328 return false;
329 }
330
GetValuesForVariableExpressionPath(llvm::StringRef variable_expr_path,ExecutionContextScope * scope,GetVariableCallback callback,void * baton,VariableList & variable_list,ValueObjectList & valobj_list)331 Status Variable::GetValuesForVariableExpressionPath(
332 llvm::StringRef variable_expr_path, ExecutionContextScope *scope,
333 GetVariableCallback callback, void *baton, VariableList &variable_list,
334 ValueObjectList &valobj_list) {
335 Status error;
336 if (!callback || variable_expr_path.empty()) {
337 error.SetErrorString("unknown error");
338 return error;
339 }
340
341 switch (variable_expr_path.front()) {
342 case '*':
343 error = Variable::GetValuesForVariableExpressionPath(
344 variable_expr_path.drop_front(), scope, callback, baton, variable_list,
345 valobj_list);
346 if (error.Fail()) {
347 error.SetErrorString("unknown error");
348 return error;
349 }
350 for (uint32_t i = 0; i < valobj_list.GetSize();) {
351 Status tmp_error;
352 ValueObjectSP valobj_sp(
353 valobj_list.GetValueObjectAtIndex(i)->Dereference(tmp_error));
354 if (tmp_error.Fail()) {
355 variable_list.RemoveVariableAtIndex(i);
356 valobj_list.RemoveValueObjectAtIndex(i);
357 } else {
358 valobj_list.SetValueObjectAtIndex(i, valobj_sp);
359 ++i;
360 }
361 }
362 return error;
363 case '&': {
364 error = Variable::GetValuesForVariableExpressionPath(
365 variable_expr_path.drop_front(), scope, callback, baton, variable_list,
366 valobj_list);
367 if (error.Success()) {
368 for (uint32_t i = 0; i < valobj_list.GetSize();) {
369 Status tmp_error;
370 ValueObjectSP valobj_sp(
371 valobj_list.GetValueObjectAtIndex(i)->AddressOf(tmp_error));
372 if (tmp_error.Fail()) {
373 variable_list.RemoveVariableAtIndex(i);
374 valobj_list.RemoveValueObjectAtIndex(i);
375 } else {
376 valobj_list.SetValueObjectAtIndex(i, valobj_sp);
377 ++i;
378 }
379 }
380 } else {
381 error.SetErrorString("unknown error");
382 }
383 return error;
384 } break;
385
386 default: {
387 static RegularExpression g_regex(
388 llvm::StringRef("^([A-Za-z_:][A-Za-z_0-9:]*)(.*)"));
389 RegularExpression::Match regex_match(1);
390 std::string variable_name;
391 variable_list.Clear();
392 if (!g_regex.Execute(variable_expr_path, ®ex_match)) {
393 error.SetErrorStringWithFormat(
394 "unable to extract a variable name from '%s'",
395 variable_expr_path.str().c_str());
396 return error;
397 }
398 if (!regex_match.GetMatchAtIndex(variable_expr_path, 1, variable_name)) {
399 error.SetErrorStringWithFormat(
400 "unable to extract a variable name from '%s'",
401 variable_expr_path.str().c_str());
402 return error;
403 }
404 if (!callback(baton, variable_name.c_str(), variable_list)) {
405 error.SetErrorString("unknown error");
406 return error;
407 }
408 uint32_t i = 0;
409 while (i < variable_list.GetSize()) {
410 VariableSP var_sp(variable_list.GetVariableAtIndex(i));
411 ValueObjectSP valobj_sp;
412 if (!var_sp) {
413 variable_list.RemoveVariableAtIndex(i);
414 continue;
415 }
416 ValueObjectSP variable_valobj_sp(
417 ValueObjectVariable::Create(scope, var_sp));
418 if (!variable_valobj_sp) {
419 variable_list.RemoveVariableAtIndex(i);
420 continue;
421 }
422
423 llvm::StringRef variable_sub_expr_path =
424 variable_expr_path.drop_front(variable_name.size());
425 if (!variable_sub_expr_path.empty()) {
426 valobj_sp = variable_valobj_sp->GetValueForExpressionPath(
427 variable_sub_expr_path);
428 if (!valobj_sp) {
429 error.SetErrorStringWithFormat(
430 "invalid expression path '%s' for variable '%s'",
431 variable_sub_expr_path.str().c_str(),
432 var_sp->GetName().GetCString());
433 variable_list.RemoveVariableAtIndex(i);
434 continue;
435 }
436 } else {
437 // Just the name of a variable with no extras
438 valobj_sp = variable_valobj_sp;
439 }
440
441 valobj_list.Append(valobj_sp);
442 ++i;
443 }
444
445 if (variable_list.GetSize() > 0) {
446 error.Clear();
447 return error;
448 }
449 } break;
450 }
451 error.SetErrorString("unknown error");
452 return error;
453 }
454
DumpLocationForAddress(Stream * s,const Address & address)455 bool Variable::DumpLocationForAddress(Stream *s, const Address &address) {
456 // Be sure to resolve the address to section offset prior to calling this
457 // function.
458 if (address.IsSectionOffset()) {
459 SymbolContext sc;
460 CalculateSymbolContext(&sc);
461 if (sc.module_sp == address.GetModule()) {
462 ABISP abi;
463 if (m_owner_scope) {
464 ModuleSP module_sp(m_owner_scope->CalculateSymbolContextModule());
465 if (module_sp)
466 abi = ABI::FindPlugin(ProcessSP(), module_sp->GetArchitecture());
467 }
468
469 const addr_t file_addr = address.GetFileAddress();
470 if (sc.function) {
471 if (sc.function->GetAddressRange().ContainsFileAddress(address)) {
472 addr_t loclist_base_file_addr =
473 sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
474 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS)
475 return false;
476 return m_location.DumpLocationForAddress(s, eDescriptionLevelBrief,
477 loclist_base_file_addr,
478 file_addr, abi.get());
479 }
480 }
481 return m_location.DumpLocationForAddress(s, eDescriptionLevelBrief,
482 LLDB_INVALID_ADDRESS, file_addr,
483 abi.get());
484 }
485 }
486 return false;
487 }
488
489 static void PrivateAutoComplete(
490 StackFrame *frame, llvm::StringRef partial_path,
491 const llvm::Twine
492 &prefix_path, // Anything that has been resolved already will be in here
493 const CompilerType &compiler_type,
494 StringList &matches, bool &word_complete);
495
496 static void PrivateAutoCompleteMembers(
497 StackFrame *frame, const std::string &partial_member_name,
498 llvm::StringRef partial_path,
499 const llvm::Twine
500 &prefix_path, // Anything that has been resolved already will be in here
501 const CompilerType &compiler_type,
502 StringList &matches, bool &word_complete);
503
PrivateAutoCompleteMembers(StackFrame * frame,const std::string & partial_member_name,llvm::StringRef partial_path,const llvm::Twine & prefix_path,const CompilerType & compiler_type,StringList & matches,bool & word_complete)504 static void PrivateAutoCompleteMembers(
505 StackFrame *frame, const std::string &partial_member_name,
506 llvm::StringRef partial_path,
507 const llvm::Twine
508 &prefix_path, // Anything that has been resolved already will be in here
509 const CompilerType &compiler_type,
510 StringList &matches, bool &word_complete) {
511
512 // We are in a type parsing child members
513 const uint32_t num_bases = compiler_type.GetNumDirectBaseClasses();
514
515 if (num_bases > 0) {
516 for (uint32_t i = 0; i < num_bases; ++i) {
517 CompilerType base_class_type =
518 compiler_type.GetDirectBaseClassAtIndex(i, nullptr);
519
520 PrivateAutoCompleteMembers(
521 frame, partial_member_name, partial_path, prefix_path,
522 base_class_type.GetCanonicalType(), matches, word_complete);
523 }
524 }
525
526 const uint32_t num_vbases = compiler_type.GetNumVirtualBaseClasses();
527
528 if (num_vbases > 0) {
529 for (uint32_t i = 0; i < num_vbases; ++i) {
530 CompilerType vbase_class_type =
531 compiler_type.GetVirtualBaseClassAtIndex(i, nullptr);
532
533 PrivateAutoCompleteMembers(
534 frame, partial_member_name, partial_path, prefix_path,
535 vbase_class_type.GetCanonicalType(), matches, word_complete);
536 }
537 }
538
539 // We are in a type parsing child members
540 const uint32_t num_fields = compiler_type.GetNumFields();
541
542 if (num_fields > 0) {
543 for (uint32_t i = 0; i < num_fields; ++i) {
544 std::string member_name;
545
546 CompilerType member_compiler_type = compiler_type.GetFieldAtIndex(
547 i, member_name, nullptr, nullptr, nullptr);
548
549 if (partial_member_name.empty() ||
550 member_name.find(partial_member_name) == 0) {
551 if (member_name == partial_member_name) {
552 PrivateAutoComplete(
553 frame, partial_path,
554 prefix_path + member_name, // Anything that has been resolved
555 // already will be in here
556 member_compiler_type.GetCanonicalType(), matches, word_complete);
557 } else {
558 matches.AppendString((prefix_path + member_name).str());
559 }
560 }
561 }
562 }
563 }
564
PrivateAutoComplete(StackFrame * frame,llvm::StringRef partial_path,const llvm::Twine & prefix_path,const CompilerType & compiler_type,StringList & matches,bool & word_complete)565 static void PrivateAutoComplete(
566 StackFrame *frame, llvm::StringRef partial_path,
567 const llvm::Twine
568 &prefix_path, // Anything that has been resolved already will be in here
569 const CompilerType &compiler_type,
570 StringList &matches, bool &word_complete) {
571 // printf ("\nPrivateAutoComplete()\n\tprefix_path = '%s'\n\tpartial_path =
572 // '%s'\n", prefix_path.c_str(), partial_path.c_str());
573 std::string remaining_partial_path;
574
575 const lldb::TypeClass type_class = compiler_type.GetTypeClass();
576 if (partial_path.empty()) {
577 if (compiler_type.IsValid()) {
578 switch (type_class) {
579 default:
580 case eTypeClassArray:
581 case eTypeClassBlockPointer:
582 case eTypeClassBuiltin:
583 case eTypeClassComplexFloat:
584 case eTypeClassComplexInteger:
585 case eTypeClassEnumeration:
586 case eTypeClassFunction:
587 case eTypeClassMemberPointer:
588 case eTypeClassReference:
589 case eTypeClassTypedef:
590 case eTypeClassVector: {
591 matches.AppendString(prefix_path.str());
592 word_complete = matches.GetSize() == 1;
593 } break;
594
595 case eTypeClassClass:
596 case eTypeClassStruct:
597 case eTypeClassUnion:
598 if (prefix_path.str().back() != '.')
599 matches.AppendString((prefix_path + ".").str());
600 break;
601
602 case eTypeClassObjCObject:
603 case eTypeClassObjCInterface:
604 break;
605 case eTypeClassObjCObjectPointer:
606 case eTypeClassPointer: {
607 bool omit_empty_base_classes = true;
608 if (compiler_type.GetNumChildren(omit_empty_base_classes, nullptr) > 0)
609 matches.AppendString((prefix_path + "->").str());
610 else {
611 matches.AppendString(prefix_path.str());
612 word_complete = true;
613 }
614 } break;
615 }
616 } else {
617 if (frame) {
618 const bool get_file_globals = true;
619
620 VariableList *variable_list = frame->GetVariableList(get_file_globals);
621
622 if (variable_list) {
623 const size_t num_variables = variable_list->GetSize();
624 for (size_t i = 0; i < num_variables; ++i) {
625 Variable *variable = variable_list->GetVariableAtIndex(i).get();
626 matches.AppendString(variable->GetName().AsCString());
627 }
628 }
629 }
630 }
631 } else {
632 const char ch = partial_path[0];
633 switch (ch) {
634 case '*':
635 if (prefix_path.str().empty()) {
636 PrivateAutoComplete(frame, partial_path.substr(1), "*", compiler_type,
637 matches, word_complete);
638 }
639 break;
640
641 case '&':
642 if (prefix_path.isTriviallyEmpty()) {
643 PrivateAutoComplete(frame, partial_path.substr(1), std::string("&"),
644 compiler_type, matches, word_complete);
645 }
646 break;
647
648 case '-':
649 if (partial_path.size() > 1 && partial_path[1] == '>' &&
650 !prefix_path.str().empty()) {
651 switch (type_class) {
652 case lldb::eTypeClassPointer: {
653 CompilerType pointee_type(compiler_type.GetPointeeType());
654 if (partial_path.size() > 2 && partial_path[2]) {
655 // If there is more after the "->", then search deeper
656 PrivateAutoComplete(
657 frame, partial_path.substr(2), prefix_path + "->",
658 pointee_type.GetCanonicalType(), matches, word_complete);
659 } else {
660 // Nothing after the "->", so list all members
661 PrivateAutoCompleteMembers(
662 frame, std::string(), std::string(), prefix_path + "->",
663 pointee_type.GetCanonicalType(), matches, word_complete);
664 }
665 } break;
666 default:
667 break;
668 }
669 }
670 break;
671
672 case '.':
673 if (compiler_type.IsValid()) {
674 switch (type_class) {
675 case lldb::eTypeClassUnion:
676 case lldb::eTypeClassStruct:
677 case lldb::eTypeClassClass:
678 if (partial_path.size() > 1 && partial_path[1]) {
679 // If there is more after the ".", then search deeper
680 PrivateAutoComplete(frame, partial_path.substr(1),
681 prefix_path + ".", compiler_type, matches,
682 word_complete);
683
684 } else {
685 // Nothing after the ".", so list all members
686 PrivateAutoCompleteMembers(frame, std::string(), partial_path,
687 prefix_path + ".", compiler_type,
688 matches, word_complete);
689 }
690 break;
691 default:
692 break;
693 }
694 }
695 break;
696 default:
697 if (isalpha(ch) || ch == '_' || ch == '$') {
698 const size_t partial_path_len = partial_path.size();
699 size_t pos = 1;
700 while (pos < partial_path_len) {
701 const char curr_ch = partial_path[pos];
702 if (isalnum(curr_ch) || curr_ch == '_' || curr_ch == '$') {
703 ++pos;
704 continue;
705 }
706 break;
707 }
708
709 std::string token(partial_path, 0, pos);
710 remaining_partial_path = partial_path.substr(pos);
711
712 if (compiler_type.IsValid()) {
713 PrivateAutoCompleteMembers(frame, token, remaining_partial_path,
714 prefix_path, compiler_type, matches,
715 word_complete);
716 } else if (frame) {
717 // We haven't found our variable yet
718 const bool get_file_globals = true;
719
720 VariableList *variable_list =
721 frame->GetVariableList(get_file_globals);
722
723 if (!variable_list)
724 break;
725
726 const size_t num_variables = variable_list->GetSize();
727 for (size_t i = 0; i < num_variables; ++i) {
728 Variable *variable = variable_list->GetVariableAtIndex(i).get();
729
730 if (!variable)
731 continue;
732
733 const char *variable_name = variable->GetName().AsCString();
734 if (strstr(variable_name, token.c_str()) == variable_name) {
735 if (strcmp(variable_name, token.c_str()) == 0) {
736 Type *variable_type = variable->GetType();
737 if (variable_type) {
738 CompilerType variable_compiler_type(
739 variable_type->GetForwardCompilerType());
740 PrivateAutoComplete(
741 frame, remaining_partial_path,
742 prefix_path + token, // Anything that has been resolved
743 // already will be in here
744 variable_compiler_type.GetCanonicalType(), matches,
745 word_complete);
746 } else {
747 matches.AppendString((prefix_path + variable_name).str());
748 }
749 } else if (remaining_partial_path.empty()) {
750 matches.AppendString((prefix_path + variable_name).str());
751 }
752 }
753 }
754 }
755 }
756 break;
757 }
758 }
759 }
760
AutoComplete(const ExecutionContext & exe_ctx,CompletionRequest & request)761 size_t Variable::AutoComplete(const ExecutionContext &exe_ctx,
762 CompletionRequest &request) {
763 CompilerType compiler_type;
764
765 bool word_complete = false;
766 StringList matches;
767 PrivateAutoComplete(exe_ctx.GetFramePtr(), request.GetCursorArgumentPrefix(),
768 "", compiler_type, matches, word_complete);
769 request.SetWordComplete(word_complete);
770 request.AddCompletions(matches);
771
772 return request.GetNumberOfMatches();
773 }
774