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 //---------------------------------------------------------------------- 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_static_member(static_member) {} 54 55 //---------------------------------------------------------------------- 56 // Destructor 57 //---------------------------------------------------------------------- 58 Variable::~Variable() {} 59 60 lldb::LanguageType Variable::GetLanguage() const { 61 SymbolContext variable_sc; 62 m_owner_scope->CalculateSymbolContext(&variable_sc); 63 if (variable_sc.comp_unit) 64 return variable_sc.comp_unit->GetLanguage(); 65 return lldb::eLanguageTypeUnknown; 66 } 67 68 ConstString Variable::GetName() const { 69 ConstString name = m_mangled.GetName(GetLanguage()); 70 if (name) 71 return name; 72 return m_name; 73 } 74 75 ConstString Variable::GetUnqualifiedName() const { return m_name; } 76 77 bool Variable::NameMatches(const ConstString &name) const { 78 if (m_name == name) 79 return true; 80 SymbolContext variable_sc; 81 m_owner_scope->CalculateSymbolContext(&variable_sc); 82 83 LanguageType language = eLanguageTypeUnknown; 84 if (variable_sc.comp_unit) 85 language = variable_sc.comp_unit->GetLanguage(); 86 return m_mangled.NameMatches(name, language); 87 } 88 bool Variable::NameMatches(const RegularExpression ®ex) const { 89 if (regex.Execute(m_name.AsCString())) 90 return true; 91 if (m_mangled) 92 return m_mangled.NameMatches(regex, GetLanguage()); 93 return false; 94 } 95 96 Type *Variable::GetType() { 97 if (m_symfile_type_sp) 98 return m_symfile_type_sp->GetType(); 99 return nullptr; 100 } 101 102 void Variable::Dump(Stream *s, bool show_context) const { 103 s->Printf("%p: ", static_cast<const void *>(this)); 104 s->Indent(); 105 *s << "Variable" << (const UserID &)*this; 106 107 if (m_name) 108 *s << ", name = \"" << m_name << "\""; 109 110 if (m_symfile_type_sp) { 111 Type *type = m_symfile_type_sp->GetType(); 112 if (type) { 113 *s << ", type = {" << type->GetID() << "} " << (void *)type << " ("; 114 type->DumpTypeName(s); 115 s->PutChar(')'); 116 } 117 } 118 119 if (m_scope != eValueTypeInvalid) { 120 s->PutCString(", scope = "); 121 switch (m_scope) { 122 case eValueTypeVariableGlobal: 123 s->PutCString(m_external ? "global" : "static"); 124 break; 125 case eValueTypeVariableArgument: 126 s->PutCString("parameter"); 127 break; 128 case eValueTypeVariableLocal: 129 s->PutCString("local"); 130 break; 131 case eValueTypeVariableThreadLocal: 132 s->PutCString("thread local"); 133 break; 134 default: 135 *s << "??? (" << m_scope << ')'; 136 } 137 } 138 139 if (show_context && m_owner_scope != nullptr) { 140 s->PutCString(", context = ( "); 141 m_owner_scope->DumpSymbolContext(s); 142 s->PutCString(" )"); 143 } 144 145 bool show_fullpaths = false; 146 m_declaration.Dump(s, show_fullpaths); 147 148 if (m_location.IsValid()) { 149 s->PutCString(", location = "); 150 lldb::addr_t loclist_base_addr = LLDB_INVALID_ADDRESS; 151 if (m_location.IsLocationList()) { 152 SymbolContext variable_sc; 153 m_owner_scope->CalculateSymbolContext(&variable_sc); 154 if (variable_sc.function) 155 loclist_base_addr = variable_sc.function->GetAddressRange() 156 .GetBaseAddress() 157 .GetFileAddress(); 158 } 159 ABI *abi = nullptr; 160 if (m_owner_scope) { 161 ModuleSP module_sp(m_owner_scope->CalculateSymbolContextModule()); 162 if (module_sp) 163 abi = ABI::FindPlugin(ProcessSP(), module_sp->GetArchitecture()).get(); 164 } 165 m_location.GetDescription(s, lldb::eDescriptionLevelBrief, 166 loclist_base_addr, abi); 167 } 168 169 if (m_external) 170 s->PutCString(", external"); 171 172 if (m_artificial) 173 s->PutCString(", artificial"); 174 175 s->EOL(); 176 } 177 178 bool Variable::DumpDeclaration(Stream *s, bool show_fullpaths, 179 bool show_module) { 180 bool dumped_declaration_info = false; 181 if (m_owner_scope) { 182 SymbolContext sc; 183 m_owner_scope->CalculateSymbolContext(&sc); 184 sc.block = nullptr; 185 sc.line_entry.Clear(); 186 bool show_inlined_frames = false; 187 const bool show_function_arguments = true; 188 const bool show_function_name = true; 189 190 dumped_declaration_info = sc.DumpStopContext( 191 s, nullptr, Address(), show_fullpaths, show_module, show_inlined_frames, 192 show_function_arguments, show_function_name); 193 194 if (sc.function) 195 s->PutChar(':'); 196 } 197 if (m_declaration.DumpStopContext(s, false)) 198 dumped_declaration_info = true; 199 return dumped_declaration_info; 200 } 201 202 size_t Variable::MemorySize() const { return sizeof(Variable); } 203 204 CompilerDeclContext Variable::GetDeclContext() { 205 Type *type = GetType(); 206 if (type) 207 return type->GetSymbolFile()->GetDeclContextContainingUID(GetID()); 208 return CompilerDeclContext(); 209 } 210 211 CompilerDecl Variable::GetDecl() { 212 Type *type = GetType(); 213 return type ? type->GetSymbolFile()->GetDeclForUID(GetID()) : CompilerDecl(); 214 } 215 216 void Variable::CalculateSymbolContext(SymbolContext *sc) { 217 if (m_owner_scope) { 218 m_owner_scope->CalculateSymbolContext(sc); 219 sc->variable = this; 220 } else 221 sc->Clear(false); 222 } 223 224 bool Variable::LocationIsValidForFrame(StackFrame *frame) { 225 // Is the variable is described by a single location? 226 if (!m_location.IsLocationList()) { 227 // Yes it is, the location is valid. 228 return true; 229 } 230 231 if (frame) { 232 Function *function = 233 frame->GetSymbolContext(eSymbolContextFunction).function; 234 if (function) { 235 TargetSP target_sp(frame->CalculateTarget()); 236 237 addr_t loclist_base_load_addr = 238 function->GetAddressRange().GetBaseAddress().GetLoadAddress( 239 target_sp.get()); 240 if (loclist_base_load_addr == LLDB_INVALID_ADDRESS) 241 return false; 242 // It is a location list. We just need to tell if the location 243 // list contains the current address when converted to a load 244 // 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 253 bool Variable::LocationIsValidForAddress(const Address &address) { 254 // Be sure to resolve the address to section offset prior to 255 // calling this 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 272 // list contains the current address when converted to a load 273 // address 274 return m_location.LocationListContainsAddress(loclist_base_file_addr, 275 address.GetFileAddress()); 276 } 277 } 278 } 279 return false; 280 } 281 282 bool Variable::IsInScope(StackFrame *frame) { 283 switch (m_scope) { 284 case eValueTypeRegister: 285 case eValueTypeRegisterSet: 286 return frame != nullptr; 287 288 case eValueTypeConstResult: 289 case eValueTypeVariableGlobal: 290 case eValueTypeVariableStatic: 291 case eValueTypeVariableThreadLocal: 292 return true; 293 294 case eValueTypeVariableArgument: 295 case eValueTypeVariableLocal: 296 if (frame) { 297 // We don't have a location list, we just need to see if the block 298 // that this variable was defined in is currently 299 Block *deepest_frame_block = 300 frame->GetSymbolContext(eSymbolContextBlock).block; 301 if (deepest_frame_block) { 302 SymbolContext variable_sc; 303 CalculateSymbolContext(&variable_sc); 304 305 // Check for static or global variable defined at the compile unit 306 // level that wasn't defined in a block 307 if (variable_sc.block == nullptr) 308 return true; 309 310 // Check if the variable is valid in the current block 311 if (variable_sc.block != deepest_frame_block && 312 !variable_sc.block->Contains(deepest_frame_block)) 313 return false; 314 315 // If no scope range is specified then it means that the scope is the 316 // same as the 317 // scope of the enclosing lexical block. 318 if (m_scope_range.IsEmpty()) 319 return true; 320 321 addr_t file_address = frame->GetFrameCodeAddress().GetFileAddress(); 322 return m_scope_range.FindEntryThatContains(file_address) != nullptr; 323 } 324 } 325 break; 326 327 default: 328 break; 329 } 330 return false; 331 } 332 333 Status Variable::GetValuesForVariableExpressionPath( 334 llvm::StringRef variable_expr_path, ExecutionContextScope *scope, 335 GetVariableCallback callback, void *baton, VariableList &variable_list, 336 ValueObjectList &valobj_list) { 337 Status error; 338 if (!callback || variable_expr_path.empty()) { 339 error.SetErrorString("unknown error"); 340 return error; 341 } 342 343 switch (variable_expr_path.front()) { 344 case '*': 345 error = Variable::GetValuesForVariableExpressionPath( 346 variable_expr_path.drop_front(), scope, callback, baton, variable_list, 347 valobj_list); 348 if (error.Fail()) { 349 error.SetErrorString("unknown error"); 350 return error; 351 } 352 for (uint32_t i = 0; i < valobj_list.GetSize();) { 353 Status tmp_error; 354 ValueObjectSP valobj_sp( 355 valobj_list.GetValueObjectAtIndex(i)->Dereference(tmp_error)); 356 if (tmp_error.Fail()) { 357 variable_list.RemoveVariableAtIndex(i); 358 valobj_list.RemoveValueObjectAtIndex(i); 359 } else { 360 valobj_list.SetValueObjectAtIndex(i, valobj_sp); 361 ++i; 362 } 363 } 364 return error; 365 case '&': { 366 error = Variable::GetValuesForVariableExpressionPath( 367 variable_expr_path.drop_front(), scope, callback, baton, variable_list, 368 valobj_list); 369 if (error.Success()) { 370 for (uint32_t i = 0; i < valobj_list.GetSize();) { 371 Status tmp_error; 372 ValueObjectSP valobj_sp( 373 valobj_list.GetValueObjectAtIndex(i)->AddressOf(tmp_error)); 374 if (tmp_error.Fail()) { 375 variable_list.RemoveVariableAtIndex(i); 376 valobj_list.RemoveValueObjectAtIndex(i); 377 } else { 378 valobj_list.SetValueObjectAtIndex(i, valobj_sp); 379 ++i; 380 } 381 } 382 } else { 383 error.SetErrorString("unknown error"); 384 } 385 return error; 386 } break; 387 388 default: { 389 static RegularExpression g_regex( 390 llvm::StringRef("^([A-Za-z_:][A-Za-z_0-9:]*)(.*)")); 391 RegularExpression::Match regex_match(1); 392 std::string variable_name; 393 variable_list.Clear(); 394 if (!g_regex.Execute(variable_expr_path, ®ex_match)) { 395 error.SetErrorStringWithFormat( 396 "unable to extract a variable name from '%s'", 397 variable_expr_path.str().c_str()); 398 return error; 399 } 400 if (!regex_match.GetMatchAtIndex(variable_expr_path, 1, variable_name)) { 401 error.SetErrorStringWithFormat( 402 "unable to extract a variable name from '%s'", 403 variable_expr_path.str().c_str()); 404 return error; 405 } 406 if (!callback(baton, variable_name.c_str(), variable_list)) { 407 error.SetErrorString("unknown error"); 408 return error; 409 } 410 uint32_t i = 0; 411 while (i < variable_list.GetSize()) { 412 VariableSP var_sp(variable_list.GetVariableAtIndex(i)); 413 ValueObjectSP valobj_sp; 414 if (!var_sp) { 415 variable_list.RemoveVariableAtIndex(i); 416 continue; 417 } 418 ValueObjectSP variable_valobj_sp( 419 ValueObjectVariable::Create(scope, var_sp)); 420 if (!variable_valobj_sp) { 421 variable_list.RemoveVariableAtIndex(i); 422 continue; 423 } 424 425 llvm::StringRef variable_sub_expr_path = 426 variable_expr_path.drop_front(variable_name.size()); 427 if (!variable_sub_expr_path.empty()) { 428 valobj_sp = variable_valobj_sp->GetValueForExpressionPath( 429 variable_sub_expr_path); 430 if (!valobj_sp) { 431 error.SetErrorStringWithFormat( 432 "invalid expression path '%s' for variable '%s'", 433 variable_sub_expr_path.str().c_str(), 434 var_sp->GetName().GetCString()); 435 variable_list.RemoveVariableAtIndex(i); 436 continue; 437 } 438 } else { 439 // Just the name of a variable with no extras 440 valobj_sp = variable_valobj_sp; 441 } 442 443 valobj_list.Append(valobj_sp); 444 ++i; 445 } 446 447 if (variable_list.GetSize() > 0) { 448 error.Clear(); 449 return error; 450 } 451 } break; 452 } 453 error.SetErrorString("unknown error"); 454 return error; 455 } 456 457 bool Variable::DumpLocationForAddress(Stream *s, const Address &address) { 458 // Be sure to resolve the address to section offset prior to 459 // calling this function. 460 if (address.IsSectionOffset()) { 461 SymbolContext sc; 462 CalculateSymbolContext(&sc); 463 if (sc.module_sp == address.GetModule()) { 464 ABI *abi = nullptr; 465 if (m_owner_scope) { 466 ModuleSP module_sp(m_owner_scope->CalculateSymbolContextModule()); 467 if (module_sp) 468 abi = ABI::FindPlugin(ProcessSP(), module_sp->GetArchitecture()).get(); 469 } 470 471 const addr_t file_addr = address.GetFileAddress(); 472 if (sc.function) { 473 if (sc.function->GetAddressRange().ContainsFileAddress(address)) { 474 addr_t loclist_base_file_addr = 475 sc.function->GetAddressRange().GetBaseAddress().GetFileAddress(); 476 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS) 477 return false; 478 return m_location.DumpLocationForAddress(s, eDescriptionLevelBrief, 479 loclist_base_file_addr, 480 file_addr, abi); 481 } 482 } 483 return m_location.DumpLocationForAddress( 484 s, eDescriptionLevelBrief, LLDB_INVALID_ADDRESS, file_addr, abi); 485 } 486 } 487 return false; 488 } 489 490 static void PrivateAutoComplete( 491 StackFrame *frame, llvm::StringRef partial_path, 492 const llvm::Twine 493 &prefix_path, // Anything that has been resolved already will be in here 494 const CompilerType &compiler_type, 495 StringList &matches, bool &word_complete); 496 497 static void PrivateAutoCompleteMembers( 498 StackFrame *frame, const std::string &partial_member_name, 499 llvm::StringRef partial_path, 500 const llvm::Twine 501 &prefix_path, // Anything that has been resolved already will be in here 502 const CompilerType &compiler_type, 503 StringList &matches, bool &word_complete); 504 505 static void PrivateAutoCompleteMembers( 506 StackFrame *frame, const std::string &partial_member_name, 507 llvm::StringRef partial_path, 508 const llvm::Twine 509 &prefix_path, // Anything that has been resolved already will be in here 510 const CompilerType &compiler_type, 511 StringList &matches, bool &word_complete) { 512 513 // We are in a type parsing child members 514 const uint32_t num_bases = compiler_type.GetNumDirectBaseClasses(); 515 516 if (num_bases > 0) { 517 for (uint32_t i = 0; i < num_bases; ++i) { 518 CompilerType base_class_type = 519 compiler_type.GetDirectBaseClassAtIndex(i, nullptr); 520 521 PrivateAutoCompleteMembers( 522 frame, partial_member_name, partial_path, prefix_path, 523 base_class_type.GetCanonicalType(), matches, word_complete); 524 } 525 } 526 527 const uint32_t num_vbases = compiler_type.GetNumVirtualBaseClasses(); 528 529 if (num_vbases > 0) { 530 for (uint32_t i = 0; i < num_vbases; ++i) { 531 CompilerType vbase_class_type = 532 compiler_type.GetVirtualBaseClassAtIndex(i, nullptr); 533 534 PrivateAutoCompleteMembers( 535 frame, partial_member_name, partial_path, prefix_path, 536 vbase_class_type.GetCanonicalType(), matches, word_complete); 537 } 538 } 539 540 // We are in a type parsing child members 541 const uint32_t num_fields = compiler_type.GetNumFields(); 542 543 if (num_fields > 0) { 544 for (uint32_t i = 0; i < num_fields; ++i) { 545 std::string member_name; 546 547 CompilerType member_compiler_type = compiler_type.GetFieldAtIndex( 548 i, member_name, nullptr, nullptr, nullptr); 549 550 if (partial_member_name.empty() || 551 member_name.find(partial_member_name) == 0) { 552 if (member_name == partial_member_name) { 553 PrivateAutoComplete( 554 frame, partial_path, 555 prefix_path + member_name, // Anything that has been resolved 556 // already will be in here 557 member_compiler_type.GetCanonicalType(), matches, word_complete); 558 } else { 559 matches.AppendString((prefix_path + member_name).str()); 560 } 561 } 562 } 563 } 564 } 565 566 static void PrivateAutoComplete( 567 StackFrame *frame, llvm::StringRef partial_path, 568 const llvm::Twine 569 &prefix_path, // Anything that has been resolved already will be in here 570 const CompilerType &compiler_type, 571 StringList &matches, bool &word_complete) { 572 // printf ("\nPrivateAutoComplete()\n\tprefix_path = '%s'\n\tpartial_path = 573 // '%s'\n", prefix_path.c_str(), partial_path.c_str()); 574 std::string remaining_partial_path; 575 576 const lldb::TypeClass type_class = compiler_type.GetTypeClass(); 577 if (partial_path.empty()) { 578 if (compiler_type.IsValid()) { 579 switch (type_class) { 580 default: 581 case eTypeClassArray: 582 case eTypeClassBlockPointer: 583 case eTypeClassBuiltin: 584 case eTypeClassComplexFloat: 585 case eTypeClassComplexInteger: 586 case eTypeClassEnumeration: 587 case eTypeClassFunction: 588 case eTypeClassMemberPointer: 589 case eTypeClassReference: 590 case eTypeClassTypedef: 591 case eTypeClassVector: { 592 matches.AppendString(prefix_path.str()); 593 word_complete = matches.GetSize() == 1; 594 } break; 595 596 case eTypeClassClass: 597 case eTypeClassStruct: 598 case eTypeClassUnion: 599 if (prefix_path.str().back() != '.') 600 matches.AppendString((prefix_path + ".").str()); 601 break; 602 603 case eTypeClassObjCObject: 604 case eTypeClassObjCInterface: 605 break; 606 case eTypeClassObjCObjectPointer: 607 case eTypeClassPointer: { 608 bool omit_empty_base_classes = true; 609 if (compiler_type.GetNumChildren(omit_empty_base_classes) > 0) 610 matches.AppendString((prefix_path + "->").str()); 611 else { 612 matches.AppendString(prefix_path.str()); 613 word_complete = true; 614 } 615 } break; 616 } 617 } else { 618 if (frame) { 619 const bool get_file_globals = true; 620 621 VariableList *variable_list = frame->GetVariableList(get_file_globals); 622 623 if (variable_list) { 624 const size_t num_variables = variable_list->GetSize(); 625 for (size_t i = 0; i < num_variables; ++i) { 626 Variable *variable = variable_list->GetVariableAtIndex(i).get(); 627 matches.AppendString(variable->GetName().AsCString()); 628 } 629 } 630 } 631 } 632 } else { 633 const char ch = partial_path[0]; 634 switch (ch) { 635 case '*': 636 if (prefix_path.str().empty()) { 637 PrivateAutoComplete(frame, partial_path.substr(1), "*", compiler_type, 638 matches, word_complete); 639 } 640 break; 641 642 case '&': 643 if (prefix_path.isTriviallyEmpty()) { 644 PrivateAutoComplete(frame, partial_path.substr(1), std::string("&"), 645 compiler_type, matches, word_complete); 646 } 647 break; 648 649 case '-': 650 if (partial_path[1] == '>' && !prefix_path.str().empty()) { 651 switch (type_class) { 652 case lldb::eTypeClassPointer: { 653 CompilerType pointee_type(compiler_type.GetPointeeType()); 654 if (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[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 761 size_t Variable::AutoComplete(const ExecutionContext &exe_ctx, 762 llvm::StringRef partial_path, StringList &matches, 763 bool &word_complete) { 764 word_complete = false; 765 CompilerType compiler_type; 766 767 PrivateAutoComplete(exe_ctx.GetFramePtr(), partial_path, "", compiler_type, 768 matches, word_complete); 769 770 return matches.GetSize(); 771 } 772