1 //===-- ClangUserExpression.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 <stdio.h> 11 #if HAVE_SYS_TYPES_H 12 #include <sys/types.h> 13 #endif 14 15 #include <cstdlib> 16 #include <map> 17 #include <string> 18 19 #include "ClangUserExpression.h" 20 21 #include "ASTResultSynthesizer.h" 22 #include "ClangDiagnostic.h" 23 #include "ClangExpressionDeclMap.h" 24 #include "ClangExpressionParser.h" 25 #include "ClangModulesDeclVendor.h" 26 #include "ClangPersistentVariables.h" 27 28 #include "lldb/Core/Debugger.h" 29 #include "lldb/Core/Module.h" 30 #include "lldb/Core/StreamFile.h" 31 #include "lldb/Core/ValueObjectConstResult.h" 32 #include "lldb/Expression/ExpressionSourceCode.h" 33 #include "lldb/Expression/IRExecutionUnit.h" 34 #include "lldb/Expression/IRInterpreter.h" 35 #include "lldb/Expression/Materializer.h" 36 #include "lldb/Host/HostInfo.h" 37 #include "lldb/Symbol/Block.h" 38 #include "lldb/Symbol/ClangASTContext.h" 39 #include "lldb/Symbol/ClangExternalASTSourceCommon.h" 40 #include "lldb/Symbol/Function.h" 41 #include "lldb/Symbol/ObjectFile.h" 42 #include "lldb/Symbol/SymbolVendor.h" 43 #include "lldb/Symbol/Type.h" 44 #include "lldb/Symbol/VariableList.h" 45 #include "lldb/Target/ExecutionContext.h" 46 #include "lldb/Target/Process.h" 47 #include "lldb/Target/StackFrame.h" 48 #include "lldb/Target/Target.h" 49 #include "lldb/Target/ThreadPlan.h" 50 #include "lldb/Target/ThreadPlanCallUserExpression.h" 51 #include "lldb/Utility/ConstString.h" 52 #include "lldb/Utility/Log.h" 53 #include "lldb/Utility/StreamString.h" 54 55 #include "clang/AST/DeclCXX.h" 56 #include "clang/AST/DeclObjC.h" 57 58 using namespace lldb_private; 59 60 ClangUserExpression::ClangUserExpression( 61 ExecutionContextScope &exe_scope, llvm::StringRef expr, 62 llvm::StringRef prefix, lldb::LanguageType language, 63 ResultType desired_type, const EvaluateExpressionOptions &options) 64 : LLVMUserExpression(exe_scope, expr, prefix, language, desired_type, 65 options), 66 m_type_system_helper(*m_target_wp.lock().get(), 67 options.GetExecutionPolicy() == 68 eExecutionPolicyTopLevel), 69 m_result_delegate(exe_scope.CalculateTarget()) { 70 switch (m_language) { 71 case lldb::eLanguageTypeC_plus_plus: 72 m_allow_cxx = true; 73 break; 74 case lldb::eLanguageTypeObjC: 75 m_allow_objc = true; 76 break; 77 case lldb::eLanguageTypeObjC_plus_plus: 78 default: 79 m_allow_cxx = true; 80 m_allow_objc = true; 81 break; 82 } 83 } 84 85 ClangUserExpression::~ClangUserExpression() {} 86 87 void ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Status &err) { 88 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 89 90 if (log) 91 log->Printf("ClangUserExpression::ScanContext()"); 92 93 m_target = exe_ctx.GetTargetPtr(); 94 95 if (!(m_allow_cxx || m_allow_objc)) { 96 if (log) 97 log->Printf(" [CUE::SC] Settings inhibit C++ and Objective-C"); 98 return; 99 } 100 101 StackFrame *frame = exe_ctx.GetFramePtr(); 102 if (frame == NULL) { 103 if (log) 104 log->Printf(" [CUE::SC] Null stack frame"); 105 return; 106 } 107 108 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | 109 lldb::eSymbolContextBlock); 110 111 if (!sym_ctx.function) { 112 if (log) 113 log->Printf(" [CUE::SC] Null function"); 114 return; 115 } 116 117 // Find the block that defines the function represented by "sym_ctx" 118 Block *function_block = sym_ctx.GetFunctionBlock(); 119 120 if (!function_block) { 121 if (log) 122 log->Printf(" [CUE::SC] Null function block"); 123 return; 124 } 125 126 CompilerDeclContext decl_context = function_block->GetDeclContext(); 127 128 if (!decl_context) { 129 if (log) 130 log->Printf(" [CUE::SC] Null decl context"); 131 return; 132 } 133 134 if (clang::CXXMethodDecl *method_decl = 135 ClangASTContext::DeclContextGetAsCXXMethodDecl(decl_context)) { 136 if (m_allow_cxx && method_decl->isInstance()) { 137 if (m_enforce_valid_object) { 138 lldb::VariableListSP variable_list_sp( 139 function_block->GetBlockVariableList(true)); 140 141 const char *thisErrorString = "Stopped in a C++ method, but 'this' " 142 "isn't available; pretending we are in a " 143 "generic context"; 144 145 if (!variable_list_sp) { 146 err.SetErrorString(thisErrorString); 147 return; 148 } 149 150 lldb::VariableSP this_var_sp( 151 variable_list_sp->FindVariable(ConstString("this"))); 152 153 if (!this_var_sp || !this_var_sp->IsInScope(frame) || 154 !this_var_sp->LocationIsValidForFrame(frame)) { 155 err.SetErrorString(thisErrorString); 156 return; 157 } 158 } 159 160 m_in_cplusplus_method = true; 161 m_needs_object_ptr = true; 162 } 163 } else if (clang::ObjCMethodDecl *method_decl = 164 ClangASTContext::DeclContextGetAsObjCMethodDecl( 165 decl_context)) { 166 if (m_allow_objc) { 167 if (m_enforce_valid_object) { 168 lldb::VariableListSP variable_list_sp( 169 function_block->GetBlockVariableList(true)); 170 171 const char *selfErrorString = "Stopped in an Objective-C method, but " 172 "'self' isn't available; pretending we " 173 "are in a generic context"; 174 175 if (!variable_list_sp) { 176 err.SetErrorString(selfErrorString); 177 return; 178 } 179 180 lldb::VariableSP self_variable_sp = 181 variable_list_sp->FindVariable(ConstString("self")); 182 183 if (!self_variable_sp || !self_variable_sp->IsInScope(frame) || 184 !self_variable_sp->LocationIsValidForFrame(frame)) { 185 err.SetErrorString(selfErrorString); 186 return; 187 } 188 } 189 190 m_in_objectivec_method = true; 191 m_needs_object_ptr = true; 192 193 if (!method_decl->isInstanceMethod()) 194 m_in_static_method = true; 195 } 196 } else if (clang::FunctionDecl *function_decl = 197 ClangASTContext::DeclContextGetAsFunctionDecl(decl_context)) { 198 // We might also have a function that said in the debug information that it 199 // captured an object pointer. The best way to deal with getting to the 200 // ivars at present is by pretending that this is a method of a class in 201 // whatever runtime the debug info says the object pointer belongs to. Do 202 // that here. 203 204 ClangASTMetadata *metadata = 205 ClangASTContext::DeclContextGetMetaData(decl_context, function_decl); 206 if (metadata && metadata->HasObjectPtr()) { 207 lldb::LanguageType language = metadata->GetObjectPtrLanguage(); 208 if (language == lldb::eLanguageTypeC_plus_plus) { 209 if (m_enforce_valid_object) { 210 lldb::VariableListSP variable_list_sp( 211 function_block->GetBlockVariableList(true)); 212 213 const char *thisErrorString = "Stopped in a context claiming to " 214 "capture a C++ object pointer, but " 215 "'this' isn't available; pretending we " 216 "are in a generic context"; 217 218 if (!variable_list_sp) { 219 err.SetErrorString(thisErrorString); 220 return; 221 } 222 223 lldb::VariableSP this_var_sp( 224 variable_list_sp->FindVariable(ConstString("this"))); 225 226 if (!this_var_sp || !this_var_sp->IsInScope(frame) || 227 !this_var_sp->LocationIsValidForFrame(frame)) { 228 err.SetErrorString(thisErrorString); 229 return; 230 } 231 } 232 233 m_in_cplusplus_method = true; 234 m_needs_object_ptr = true; 235 } else if (language == lldb::eLanguageTypeObjC) { 236 if (m_enforce_valid_object) { 237 lldb::VariableListSP variable_list_sp( 238 function_block->GetBlockVariableList(true)); 239 240 const char *selfErrorString = 241 "Stopped in a context claiming to capture an Objective-C object " 242 "pointer, but 'self' isn't available; pretending we are in a " 243 "generic context"; 244 245 if (!variable_list_sp) { 246 err.SetErrorString(selfErrorString); 247 return; 248 } 249 250 lldb::VariableSP self_variable_sp = 251 variable_list_sp->FindVariable(ConstString("self")); 252 253 if (!self_variable_sp || !self_variable_sp->IsInScope(frame) || 254 !self_variable_sp->LocationIsValidForFrame(frame)) { 255 err.SetErrorString(selfErrorString); 256 return; 257 } 258 259 Type *self_type = self_variable_sp->GetType(); 260 261 if (!self_type) { 262 err.SetErrorString(selfErrorString); 263 return; 264 } 265 266 CompilerType self_clang_type = self_type->GetForwardCompilerType(); 267 268 if (!self_clang_type) { 269 err.SetErrorString(selfErrorString); 270 return; 271 } 272 273 if (ClangASTContext::IsObjCClassType(self_clang_type)) { 274 return; 275 } else if (ClangASTContext::IsObjCObjectPointerType( 276 self_clang_type)) { 277 m_in_objectivec_method = true; 278 m_needs_object_ptr = true; 279 } else { 280 err.SetErrorString(selfErrorString); 281 return; 282 } 283 } else { 284 m_in_objectivec_method = true; 285 m_needs_object_ptr = true; 286 } 287 } 288 } 289 } 290 } 291 292 // This is a really nasty hack, meant to fix Objective-C expressions of the 293 // form (int)[myArray count]. Right now, because the type information for 294 // count is not available, [myArray count] returns id, which can't be directly 295 // cast to int without causing a clang error. 296 static void ApplyObjcCastHack(std::string &expr) { 297 #define OBJC_CAST_HACK_FROM "(int)[" 298 #define OBJC_CAST_HACK_TO "(int)(long long)[" 299 300 size_t from_offset; 301 302 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos) 303 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, 304 OBJC_CAST_HACK_TO); 305 306 #undef OBJC_CAST_HACK_TO 307 #undef OBJC_CAST_HACK_FROM 308 } 309 310 namespace { 311 // Utility guard that calls a callback when going out of scope. 312 class OnExit { 313 public: 314 typedef std::function<void(void)> Callback; 315 316 OnExit(Callback const &callback) : m_callback(callback) {} 317 318 ~OnExit() { m_callback(); } 319 320 private: 321 Callback m_callback; 322 }; 323 } // namespace 324 325 bool ClangUserExpression::SetupPersistentState(DiagnosticManager &diagnostic_manager, 326 ExecutionContext &exe_ctx) { 327 if (Target *target = exe_ctx.GetTargetPtr()) { 328 if (PersistentExpressionState *persistent_state = 329 target->GetPersistentExpressionStateForLanguage( 330 lldb::eLanguageTypeC)) { 331 m_result_delegate.RegisterPersistentState(persistent_state); 332 } else { 333 diagnostic_manager.PutString( 334 eDiagnosticSeverityError, 335 "couldn't start parsing (no persistent data)"); 336 return false; 337 } 338 } else { 339 diagnostic_manager.PutString(eDiagnosticSeverityError, 340 "error: couldn't start parsing (no target)"); 341 return false; 342 } 343 return true; 344 } 345 346 static void SetupDeclVendor(ExecutionContext &exe_ctx, Target *target) { 347 if (ClangModulesDeclVendor *decl_vendor = 348 target->GetClangModulesDeclVendor()) { 349 const ClangModulesDeclVendor::ModuleVector &hand_imported_modules = 350 llvm::cast<ClangPersistentVariables>( 351 target->GetPersistentExpressionStateForLanguage( 352 lldb::eLanguageTypeC)) 353 ->GetHandLoadedClangModules(); 354 ClangModulesDeclVendor::ModuleVector modules_for_macros; 355 356 for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) { 357 modules_for_macros.push_back(module); 358 } 359 360 if (target->GetEnableAutoImportClangModules()) { 361 if (StackFrame *frame = exe_ctx.GetFramePtr()) { 362 if (Block *block = frame->GetFrameBlock()) { 363 SymbolContext sc; 364 365 block->CalculateSymbolContext(&sc); 366 367 if (sc.comp_unit) { 368 StreamString error_stream; 369 370 decl_vendor->AddModulesForCompileUnit( 371 *sc.comp_unit, modules_for_macros, error_stream); 372 } 373 } 374 } 375 } 376 } 377 } 378 379 llvm::Optional<lldb::LanguageType> ClangUserExpression::GetLanguageForExpr( 380 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx) { 381 lldb::LanguageType lang_type = lldb::LanguageType::eLanguageTypeUnknown; 382 383 std::string prefix = m_expr_prefix; 384 385 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) { 386 m_transformed_text = m_expr_text; 387 } else { 388 std::unique_ptr<ExpressionSourceCode> source_code( 389 ExpressionSourceCode::CreateWrapped(prefix.c_str(), 390 m_expr_text.c_str())); 391 392 if (m_in_cplusplus_method) 393 lang_type = lldb::eLanguageTypeC_plus_plus; 394 else if (m_in_objectivec_method) 395 lang_type = lldb::eLanguageTypeObjC; 396 else 397 lang_type = lldb::eLanguageTypeC; 398 399 if (!source_code->GetText(m_transformed_text, lang_type, m_in_static_method, 400 exe_ctx)) { 401 diagnostic_manager.PutString(eDiagnosticSeverityError, 402 "couldn't construct expression body"); 403 return llvm::Optional<lldb::LanguageType>(); 404 } 405 406 // Find and store the start position of the original code inside the 407 // transformed code. We need this later for the code completion. 408 std::size_t original_start; 409 std::size_t original_end; 410 bool found_bounds = source_code->GetOriginalBodyBounds( 411 m_transformed_text, lang_type, original_start, original_end); 412 if (found_bounds) { 413 m_user_expression_start_pos = original_start; 414 } 415 } 416 return lang_type; 417 } 418 419 bool ClangUserExpression::PrepareForParsing( 420 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx) { 421 InstallContext(exe_ctx); 422 423 if (!SetupPersistentState(diagnostic_manager, exe_ctx)) 424 return false; 425 426 Status err; 427 ScanContext(exe_ctx, err); 428 429 if (!err.Success()) { 430 diagnostic_manager.PutString(eDiagnosticSeverityWarning, err.AsCString()); 431 } 432 433 //////////////////////////////////// 434 // Generate the expression 435 // 436 437 ApplyObjcCastHack(m_expr_text); 438 439 SetupDeclVendor(exe_ctx, m_target); 440 return true; 441 } 442 443 bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager, 444 ExecutionContext &exe_ctx, 445 lldb_private::ExecutionPolicy execution_policy, 446 bool keep_result_in_memory, 447 bool generate_debug_info) { 448 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 449 450 if (!PrepareForParsing(diagnostic_manager, exe_ctx)) 451 return false; 452 453 lldb::LanguageType lang_type = lldb::LanguageType::eLanguageTypeUnknown; 454 if (auto new_lang = GetLanguageForExpr(diagnostic_manager, exe_ctx)) { 455 lang_type = new_lang.getValue(); 456 } 457 458 if (log) 459 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str()); 460 461 //////////////////////////////////// 462 // Set up the target and compiler 463 // 464 465 Target *target = exe_ctx.GetTargetPtr(); 466 467 if (!target) { 468 diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid target"); 469 return false; 470 } 471 472 ////////////////////////// 473 // Parse the expression 474 // 475 476 m_materializer_ap.reset(new Materializer()); 477 478 ResetDeclMap(exe_ctx, m_result_delegate, keep_result_in_memory); 479 480 OnExit on_exit([this]() { ResetDeclMap(); }); 481 482 if (!DeclMap()->WillParse(exe_ctx, m_materializer_ap.get())) { 483 diagnostic_manager.PutString( 484 eDiagnosticSeverityError, 485 "current process state is unsuitable for expression parsing"); 486 return false; 487 } 488 489 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) { 490 DeclMap()->SetLookupsEnabled(true); 491 } 492 493 Process *process = exe_ctx.GetProcessPtr(); 494 ExecutionContextScope *exe_scope = process; 495 496 if (!exe_scope) 497 exe_scope = exe_ctx.GetTargetPtr(); 498 499 // We use a shared pointer here so we can use the original parser - if it 500 // succeeds or the rewrite parser we might make if it fails. But the 501 // parser_sp will never be empty. 502 503 ClangExpressionParser parser(exe_scope, *this, generate_debug_info); 504 505 unsigned num_errors = parser.Parse(diagnostic_manager); 506 507 // Check here for FixItHints. If there are any try to apply the fixits and 508 // set the fixed text in m_fixed_text before returning an error. 509 if (num_errors) { 510 if (diagnostic_manager.HasFixIts()) { 511 if (parser.RewriteExpression(diagnostic_manager)) { 512 size_t fixed_start; 513 size_t fixed_end; 514 const std::string &fixed_expression = 515 diagnostic_manager.GetFixedExpression(); 516 if (ExpressionSourceCode::GetOriginalBodyBounds( 517 fixed_expression, lang_type, fixed_start, fixed_end)) 518 m_fixed_text = 519 fixed_expression.substr(fixed_start, fixed_end - fixed_start); 520 } 521 } 522 return false; 523 } 524 525 ////////////////////////////////////////////////////////////////////////////////////////// 526 // Prepare the output of the parser for execution, evaluating it statically 527 // if possible 528 // 529 530 { 531 Status jit_error = parser.PrepareForExecution( 532 m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx, 533 m_can_interpret, execution_policy); 534 535 if (!jit_error.Success()) { 536 const char *error_cstr = jit_error.AsCString(); 537 if (error_cstr && error_cstr[0]) 538 diagnostic_manager.PutString(eDiagnosticSeverityError, error_cstr); 539 else 540 diagnostic_manager.PutString(eDiagnosticSeverityError, 541 "expression can't be interpreted or run"); 542 return false; 543 } 544 } 545 546 if (exe_ctx.GetProcessPtr() && execution_policy == eExecutionPolicyTopLevel) { 547 Status static_init_error = 548 parser.RunStaticInitializers(m_execution_unit_sp, exe_ctx); 549 550 if (!static_init_error.Success()) { 551 const char *error_cstr = static_init_error.AsCString(); 552 if (error_cstr && error_cstr[0]) 553 diagnostic_manager.Printf(eDiagnosticSeverityError, 554 "couldn't run static initializers: %s\n", 555 error_cstr); 556 else 557 diagnostic_manager.PutString(eDiagnosticSeverityError, 558 "couldn't run static initializers\n"); 559 return false; 560 } 561 } 562 563 if (m_execution_unit_sp) { 564 bool register_execution_unit = false; 565 566 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) { 567 register_execution_unit = true; 568 } 569 570 // If there is more than one external function in the execution unit, it 571 // needs to keep living even if it's not top level, because the result 572 // could refer to that function. 573 574 if (m_execution_unit_sp->GetJittedFunctions().size() > 1) { 575 register_execution_unit = true; 576 } 577 578 if (register_execution_unit) { 579 llvm::cast<PersistentExpressionState>( 580 exe_ctx.GetTargetPtr()->GetPersistentExpressionStateForLanguage( 581 m_language)) 582 ->RegisterExecutionUnit(m_execution_unit_sp); 583 } 584 } 585 586 if (generate_debug_info) { 587 lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule()); 588 589 if (jit_module_sp) { 590 ConstString const_func_name(FunctionName()); 591 FileSpec jit_file; 592 jit_file.GetFilename() = const_func_name; 593 jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString()); 594 m_jit_module_wp = jit_module_sp; 595 target->GetImages().Append(jit_module_sp); 596 } 597 } 598 599 if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS) 600 m_jit_process_wp = lldb::ProcessWP(process->shared_from_this()); 601 return true; 602 } 603 604 //------------------------------------------------------------------ 605 /// Converts an absolute position inside a given code string into 606 /// a column/line pair. 607 /// 608 /// @param[in] abs_pos 609 /// A absolute position in the code string that we want to convert 610 /// to a column/line pair. 611 /// 612 /// @param[in] code 613 /// A multi-line string usually representing source code. 614 /// 615 /// @param[out] line 616 /// The line in the code that contains the given absolute position. 617 /// The first line in the string is indexed as 1. 618 /// 619 /// @param[out] column 620 /// The column in the line that contains the absolute position. 621 /// The first character in a line is indexed as 0. 622 //------------------------------------------------------------------ 623 static void AbsPosToLineColumnPos(size_t abs_pos, llvm::StringRef code, 624 unsigned &line, unsigned &column) { 625 // Reset to code position to beginning of the file. 626 line = 0; 627 column = 0; 628 629 assert(abs_pos <= code.size() && "Absolute position outside code string?"); 630 631 // We have to walk up to the position and count lines/columns. 632 for (std::size_t i = 0; i < abs_pos; ++i) { 633 // If we hit a line break, we go back to column 0 and enter a new line. 634 // We only handle \n because that's what we internally use to make new 635 // lines for our temporary code strings. 636 if (code[i] == '\n') { 637 ++line; 638 column = 0; 639 continue; 640 } 641 ++column; 642 } 643 } 644 645 bool ClangUserExpression::Complete(ExecutionContext &exe_ctx, 646 CompletionRequest &request, 647 unsigned complete_pos) { 648 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 649 650 // We don't want any visible feedback when completing an expression. Mostly 651 // because the results we get from an incomplete invocation are probably not 652 // correct. 653 DiagnosticManager diagnostic_manager; 654 655 if (!PrepareForParsing(diagnostic_manager, exe_ctx)) 656 return false; 657 658 GetLanguageForExpr(diagnostic_manager, exe_ctx); 659 660 if (log) 661 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str()); 662 663 ////////////////////////// 664 // Parse the expression 665 // 666 667 m_materializer_ap.reset(new Materializer()); 668 669 ResetDeclMap(exe_ctx, m_result_delegate, /*keep result in memory*/ true); 670 671 OnExit on_exit([this]() { ResetDeclMap(); }); 672 673 if (!DeclMap()->WillParse(exe_ctx, m_materializer_ap.get())) { 674 diagnostic_manager.PutString( 675 eDiagnosticSeverityError, 676 "current process state is unsuitable for expression parsing"); 677 678 return false; 679 } 680 681 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) { 682 DeclMap()->SetLookupsEnabled(true); 683 } 684 685 Process *process = exe_ctx.GetProcessPtr(); 686 ExecutionContextScope *exe_scope = process; 687 688 if (!exe_scope) 689 exe_scope = exe_ctx.GetTargetPtr(); 690 691 ClangExpressionParser parser(exe_scope, *this, false); 692 693 // We have to find the source code location where the user text is inside 694 // the transformed expression code. When creating the transformed text, we 695 // already stored the absolute position in the m_transformed_text string. The 696 // only thing left to do is to transform it into the line:column format that 697 // Clang expects. 698 699 // The line and column of the user expression inside the transformed source 700 // code. 701 unsigned user_expr_line, user_expr_column; 702 if (m_user_expression_start_pos.hasValue()) 703 AbsPosToLineColumnPos(*m_user_expression_start_pos, m_transformed_text, 704 user_expr_line, user_expr_column); 705 else 706 return false; 707 708 // The actual column where we have to complete is the start column of the 709 // user expression + the offset inside the user code that we were given. 710 const unsigned completion_column = user_expr_column + complete_pos; 711 parser.Complete(request, user_expr_line, completion_column, complete_pos); 712 713 return true; 714 } 715 716 bool ClangUserExpression::AddArguments(ExecutionContext &exe_ctx, 717 std::vector<lldb::addr_t> &args, 718 lldb::addr_t struct_address, 719 DiagnosticManager &diagnostic_manager) { 720 lldb::addr_t object_ptr = LLDB_INVALID_ADDRESS; 721 lldb::addr_t cmd_ptr = LLDB_INVALID_ADDRESS; 722 723 if (m_needs_object_ptr) { 724 lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP(); 725 if (!frame_sp) 726 return true; 727 728 ConstString object_name; 729 730 if (m_in_cplusplus_method) { 731 object_name.SetCString("this"); 732 } else if (m_in_objectivec_method) { 733 object_name.SetCString("self"); 734 } else { 735 diagnostic_manager.PutString( 736 eDiagnosticSeverityError, 737 "need object pointer but don't know the language"); 738 return false; 739 } 740 741 Status object_ptr_error; 742 743 object_ptr = GetObjectPointer(frame_sp, object_name, object_ptr_error); 744 745 if (!object_ptr_error.Success()) { 746 exe_ctx.GetTargetRef().GetDebugger().GetAsyncOutputStream()->Printf( 747 "warning: `%s' is not accessible (substituting 0)\n", 748 object_name.AsCString()); 749 object_ptr = 0; 750 } 751 752 if (m_in_objectivec_method) { 753 ConstString cmd_name("_cmd"); 754 755 cmd_ptr = GetObjectPointer(frame_sp, cmd_name, object_ptr_error); 756 757 if (!object_ptr_error.Success()) { 758 diagnostic_manager.Printf( 759 eDiagnosticSeverityWarning, 760 "couldn't get cmd pointer (substituting NULL): %s", 761 object_ptr_error.AsCString()); 762 cmd_ptr = 0; 763 } 764 } 765 766 args.push_back(object_ptr); 767 768 if (m_in_objectivec_method) 769 args.push_back(cmd_ptr); 770 771 args.push_back(struct_address); 772 } else { 773 args.push_back(struct_address); 774 } 775 return true; 776 } 777 778 lldb::ExpressionVariableSP ClangUserExpression::GetResultAfterDematerialization( 779 ExecutionContextScope *exe_scope) { 780 return m_result_delegate.GetVariable(); 781 } 782 783 void ClangUserExpression::ClangUserExpressionHelper::ResetDeclMap( 784 ExecutionContext &exe_ctx, 785 Materializer::PersistentVariableDelegate &delegate, 786 bool keep_result_in_memory) { 787 m_expr_decl_map_up.reset( 788 new ClangExpressionDeclMap(keep_result_in_memory, &delegate, exe_ctx)); 789 } 790 791 clang::ASTConsumer * 792 ClangUserExpression::ClangUserExpressionHelper::ASTTransformer( 793 clang::ASTConsumer *passthrough) { 794 m_result_synthesizer_up.reset( 795 new ASTResultSynthesizer(passthrough, m_top_level, m_target)); 796 797 return m_result_synthesizer_up.get(); 798 } 799 800 void ClangUserExpression::ClangUserExpressionHelper::CommitPersistentDecls() { 801 if (m_result_synthesizer_up.get()) { 802 m_result_synthesizer_up->CommitPersistentDecls(); 803 } 804 } 805 806 ConstString ClangUserExpression::ResultDelegate::GetName() { 807 auto prefix = m_persistent_state->GetPersistentVariablePrefix(); 808 return m_persistent_state->GetNextPersistentVariableName(*m_target_sp, 809 prefix); 810 } 811 812 void ClangUserExpression::ResultDelegate::DidDematerialize( 813 lldb::ExpressionVariableSP &variable) { 814 m_variable = variable; 815 } 816 817 void ClangUserExpression::ResultDelegate::RegisterPersistentState( 818 PersistentExpressionState *persistent_state) { 819 m_persistent_state = persistent_state; 820 } 821 822 lldb::ExpressionVariableSP &ClangUserExpression::ResultDelegate::GetVariable() { 823 return m_variable; 824 } 825