ClangExpressionParser.cpp revision 269024
1//===-- ClangExpressionParser.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/lldb-python.h" 11 12#include "lldb/Expression/ClangExpressionParser.h" 13 14#include "lldb/Core/ArchSpec.h" 15#include "lldb/Core/DataBufferHeap.h" 16#include "lldb/Core/Debugger.h" 17#include "lldb/Core/Disassembler.h" 18#include "lldb/Core/Stream.h" 19#include "lldb/Core/StreamFile.h" 20#include "lldb/Core/StreamString.h" 21#include "lldb/Expression/ClangASTSource.h" 22#include "lldb/Expression/ClangExpression.h" 23#include "lldb/Expression/ClangExpressionDeclMap.h" 24#include "lldb/Expression/IRExecutionUnit.h" 25#include "lldb/Expression/IRDynamicChecks.h" 26#include "lldb/Expression/IRInterpreter.h" 27#include "lldb/Target/ExecutionContext.h" 28#include "lldb/Target/ObjCLanguageRuntime.h" 29#include "lldb/Target/Process.h" 30#include "lldb/Target/Target.h" 31 32#include "clang/AST/ASTContext.h" 33#include "clang/AST/ExternalASTSource.h" 34#include "clang/Basic/FileManager.h" 35#include "clang/Basic/TargetInfo.h" 36#include "clang/Basic/Version.h" 37#include "clang/CodeGen/CodeGenAction.h" 38#include "clang/CodeGen/ModuleBuilder.h" 39#include "clang/Frontend/CompilerInstance.h" 40#include "clang/Frontend/CompilerInvocation.h" 41#include "clang/Frontend/FrontendActions.h" 42#include "clang/Frontend/FrontendDiagnostic.h" 43#include "clang/Frontend/FrontendPluginRegistry.h" 44#include "clang/Frontend/TextDiagnosticBuffer.h" 45#include "clang/Frontend/TextDiagnosticPrinter.h" 46#include "clang/Lex/Preprocessor.h" 47#include "clang/Parse/ParseAST.h" 48#include "clang/Rewrite/Frontend/FrontendActions.h" 49#include "clang/Sema/SemaConsumer.h" 50#include "clang/StaticAnalyzer/Frontend/FrontendActions.h" 51 52#include "llvm/ADT/StringRef.h" 53#include "llvm/ExecutionEngine/ExecutionEngine.h" 54#include "llvm/Support/Debug.h" 55#include "llvm/Support/FileSystem.h" 56#include "llvm/Support/TargetSelect.h" 57 58#if defined (USE_STANDARD_JIT) 59#include "llvm/ExecutionEngine/JIT.h" 60#else 61#include "llvm/ExecutionEngine/MCJIT.h" 62#endif 63#include "llvm/IR/LLVMContext.h" 64#include "llvm/IR/Module.h" 65#include "llvm/Support/ErrorHandling.h" 66#include "llvm/Support/MemoryBuffer.h" 67#include "llvm/Support/DynamicLibrary.h" 68#include "llvm/Support/Host.h" 69#include "llvm/Support/Signals.h" 70 71using namespace clang; 72using namespace llvm; 73using namespace lldb_private; 74 75//===----------------------------------------------------------------------===// 76// Utility Methods for Clang 77//===----------------------------------------------------------------------===// 78 79std::string GetBuiltinIncludePath(const char *Argv0) { 80 SmallString<128> P(llvm::sys::fs::getMainExecutable( 81 Argv0, (void *)(intptr_t) GetBuiltinIncludePath)); 82 83 if (!P.empty()) { 84 llvm::sys::path::remove_filename(P); // Remove /clang from foo/bin/clang 85 llvm::sys::path::remove_filename(P); // Remove /bin from foo/bin 86 87 // Get foo/lib/clang/<version>/include 88 llvm::sys::path::append(P, "lib", "clang", CLANG_VERSION_STRING, 89 "include"); 90 } 91 92 return P.str(); 93} 94 95 96//===----------------------------------------------------------------------===// 97// Main driver for Clang 98//===----------------------------------------------------------------------===// 99 100static void LLVMErrorHandler(void *UserData, const std::string &Message) { 101 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData); 102 103 Diags.Report(diag::err_fe_error_backend) << Message; 104 105 // We cannot recover from llvm errors. 106 assert(0); 107} 108 109static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) { 110 using namespace clang::frontend; 111 112 switch (CI.getFrontendOpts().ProgramAction) { 113 default: 114 llvm_unreachable("Invalid program action!"); 115 116 case ASTDump: return new ASTDumpAction(); 117 case ASTPrint: return new ASTPrintAction(); 118 case ASTView: return new ASTViewAction(); 119 case DumpRawTokens: return new DumpRawTokensAction(); 120 case DumpTokens: return new DumpTokensAction(); 121 case EmitAssembly: return new EmitAssemblyAction(); 122 case EmitBC: return new EmitBCAction(); 123 case EmitHTML: return new HTMLPrintAction(); 124 case EmitLLVM: return new EmitLLVMAction(); 125 case EmitLLVMOnly: return new EmitLLVMOnlyAction(); 126 case EmitCodeGenOnly: return new EmitCodeGenOnlyAction(); 127 case EmitObj: return new EmitObjAction(); 128 case FixIt: return new FixItAction(); 129 case GeneratePCH: return new GeneratePCHAction(); 130 case GeneratePTH: return new GeneratePTHAction(); 131 case InitOnly: return new InitOnlyAction(); 132 case ParseSyntaxOnly: return new SyntaxOnlyAction(); 133 134 case PluginAction: { 135 for (FrontendPluginRegistry::iterator it = 136 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end(); 137 it != ie; ++it) { 138 if (it->getName() == CI.getFrontendOpts().ActionName) { 139 llvm::OwningPtr<PluginASTAction> P(it->instantiate()); 140 if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs)) 141 return 0; 142 return P.take(); 143 } 144 } 145 146 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) 147 << CI.getFrontendOpts().ActionName; 148 return 0; 149 } 150 151 case PrintDeclContext: return new DeclContextPrintAction(); 152 case PrintPreamble: return new PrintPreambleAction(); 153 case PrintPreprocessedInput: return new PrintPreprocessedAction(); 154 case RewriteMacros: return new RewriteMacrosAction(); 155 case RewriteObjC: return new RewriteObjCAction(); 156 case RewriteTest: return new RewriteTestAction(); 157 //case RunAnalysis: return new AnalysisAction(); 158 case RunPreprocessorOnly: return new PreprocessOnlyAction(); 159 } 160} 161 162static FrontendAction *CreateFrontendAction(CompilerInstance &CI) { 163 // Create the underlying action. 164 FrontendAction *Act = CreateFrontendBaseAction(CI); 165 if (!Act) 166 return 0; 167 168 // If there are any AST files to merge, create a frontend action 169 // adaptor to perform the merge. 170 if (!CI.getFrontendOpts().ASTMergeFiles.empty()) 171 Act = new ASTMergeAction(Act, CI.getFrontendOpts().ASTMergeFiles); 172 173 return Act; 174} 175 176//===----------------------------------------------------------------------===// 177// Implementation of ClangExpressionParser 178//===----------------------------------------------------------------------===// 179 180ClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope, 181 ClangExpression &expr) : 182 m_expr (expr), 183 m_compiler (), 184 m_code_generator () 185{ 186 // Initialize targets first, so that --version shows registered targets. 187 static struct InitializeLLVM { 188 InitializeLLVM() { 189 llvm::InitializeAllTargets(); 190 llvm::InitializeAllAsmPrinters(); 191 llvm::InitializeAllTargetMCs(); 192 llvm::InitializeAllDisassemblers(); 193 } 194 } InitializeLLVM; 195 196 // 1. Create a new compiler instance. 197 m_compiler.reset(new CompilerInstance()); 198 199 // 2. Install the target. 200 201 lldb::TargetSP target_sp; 202 if (exe_scope) 203 target_sp = exe_scope->CalculateTarget(); 204 205 // TODO: figure out what to really do when we don't have a valid target. 206 // Sometimes this will be ok to just use the host target triple (when we 207 // evaluate say "2+3", but other expressions like breakpoint conditions 208 // and other things that _are_ target specific really shouldn't just be 209 // using the host triple. This needs to be fixed in a better way. 210 if (target_sp && target_sp->GetArchitecture().IsValid()) 211 { 212 std::string triple = target_sp->GetArchitecture().GetTriple().str(); 213 214 int dash_count = 0; 215 for (size_t i = 0; i < triple.size(); ++i) 216 { 217 if (triple[i] == '-') 218 dash_count++; 219 if (dash_count == 3) 220 { 221 triple.resize(i); 222 break; 223 } 224 } 225 226 m_compiler->getTargetOpts().Triple = triple; 227 } 228 else 229 { 230 m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple(); 231 } 232 233 if (target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86 || 234 target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86_64) 235 { 236 m_compiler->getTargetOpts().Features.push_back("+sse"); 237 m_compiler->getTargetOpts().Features.push_back("+sse2"); 238 } 239 240 if (m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos) 241 m_compiler->getTargetOpts().ABI = "apcs-gnu"; 242 243 m_compiler->createDiagnostics(); 244 245 // Create the target instance. 246 m_compiler->setTarget(TargetInfo::CreateTargetInfo(m_compiler->getDiagnostics(), 247 &m_compiler->getTargetOpts())); 248 249 assert (m_compiler->hasTarget()); 250 251 // 3. Set options. 252 253 lldb::LanguageType language = expr.Language(); 254 255 switch (language) 256 { 257 case lldb::eLanguageTypeC: 258 break; 259 case lldb::eLanguageTypeObjC: 260 m_compiler->getLangOpts().ObjC1 = true; 261 m_compiler->getLangOpts().ObjC2 = true; 262 break; 263 case lldb::eLanguageTypeC_plus_plus: 264 m_compiler->getLangOpts().CPlusPlus = true; 265 m_compiler->getLangOpts().CPlusPlus11 = true; 266 break; 267 case lldb::eLanguageTypeObjC_plus_plus: 268 default: 269 m_compiler->getLangOpts().ObjC1 = true; 270 m_compiler->getLangOpts().ObjC2 = true; 271 m_compiler->getLangOpts().CPlusPlus = true; 272 m_compiler->getLangOpts().CPlusPlus11 = true; 273 break; 274 } 275 276 m_compiler->getLangOpts().Bool = true; 277 m_compiler->getLangOpts().WChar = true; 278 m_compiler->getLangOpts().Blocks = true; 279 m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients 280 if (expr.DesiredResultType() == ClangExpression::eResultTypeId) 281 m_compiler->getLangOpts().DebuggerCastResultToId = true; 282 283 // Spell checking is a nice feature, but it ends up completing a 284 // lot of types that we didn't strictly speaking need to complete. 285 // As a result, we spend a long time parsing and importing debug 286 // information. 287 m_compiler->getLangOpts().SpellChecking = false; 288 289 lldb::ProcessSP process_sp; 290 if (exe_scope) 291 process_sp = exe_scope->CalculateProcess(); 292 293 if (process_sp && m_compiler->getLangOpts().ObjC1) 294 { 295 if (process_sp->GetObjCLanguageRuntime()) 296 { 297 if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == eAppleObjC_V2) 298 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7)); 299 else 300 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7)); 301 302 if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing()) 303 m_compiler->getLangOpts().DebuggerObjCLiteral = true; 304 } 305 } 306 307 m_compiler->getLangOpts().ThreadsafeStatics = false; 308 m_compiler->getLangOpts().AccessControl = false; // Debuggers get universal access 309 m_compiler->getLangOpts().DollarIdents = true; // $ indicates a persistent variable name 310 311 // Set CodeGen options 312 m_compiler->getCodeGenOpts().EmitDeclMetadata = true; 313 m_compiler->getCodeGenOpts().InstrumentFunctions = false; 314 m_compiler->getCodeGenOpts().DisableFPElim = true; 315 m_compiler->getCodeGenOpts().OmitLeafFramePointer = false; 316 317 // Disable some warnings. 318 m_compiler->getDiagnostics().setDiagnosticGroupMapping("unused-value", clang::diag::MAP_IGNORE, SourceLocation()); 319 m_compiler->getDiagnostics().setDiagnosticGroupMapping("odr", clang::diag::MAP_IGNORE, SourceLocation()); 320 321 // Inform the target of the language options 322 // 323 // FIXME: We shouldn't need to do this, the target should be immutable once 324 // created. This complexity should be lifted elsewhere. 325 m_compiler->getTarget().setForcedLangOptions(m_compiler->getLangOpts()); 326 327 // 4. Set up the diagnostic buffer for reporting errors 328 329 m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer); 330 331 // 5. Set up the source management objects inside the compiler 332 333 clang::FileSystemOptions file_system_options; 334 m_file_manager.reset(new clang::FileManager(file_system_options)); 335 336 if (!m_compiler->hasSourceManager()) 337 m_compiler->createSourceManager(*m_file_manager.get()); 338 339 m_compiler->createFileManager(); 340 m_compiler->createPreprocessor(); 341 342 // 6. Most of this we get from the CompilerInstance, but we 343 // also want to give the context an ExternalASTSource. 344 m_selector_table.reset(new SelectorTable()); 345 m_builtin_context.reset(new Builtin::Context()); 346 347 std::unique_ptr<clang::ASTContext> ast_context(new ASTContext(m_compiler->getLangOpts(), 348 m_compiler->getSourceManager(), 349 &m_compiler->getTarget(), 350 m_compiler->getPreprocessor().getIdentifierTable(), 351 *m_selector_table.get(), 352 *m_builtin_context.get(), 353 0)); 354 355 ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); 356 357 if (decl_map) 358 { 359 llvm::OwningPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy()); 360 decl_map->InstallASTContext(ast_context.get()); 361 ast_context->setExternalSource(ast_source); 362 } 363 364 m_compiler->setASTContext(ast_context.release()); 365 366 std::string module_name("$__lldb_module"); 367 368 m_llvm_context.reset(new LLVMContext()); 369 m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(), 370 module_name, 371 m_compiler->getCodeGenOpts(), 372 m_compiler->getTargetOpts(), 373 *m_llvm_context)); 374} 375 376ClangExpressionParser::~ClangExpressionParser() 377{ 378} 379 380unsigned 381ClangExpressionParser::Parse (Stream &stream) 382{ 383 TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient()); 384 385 diag_buf->FlushDiagnostics (m_compiler->getDiagnostics()); 386 387 MemoryBuffer *memory_buffer = MemoryBuffer::getMemBufferCopy(m_expr.Text(), __FUNCTION__); 388 m_compiler->getSourceManager().createMainFileIDForMemBuffer (memory_buffer); 389 390 diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor()); 391 392 ASTConsumer *ast_transformer = m_expr.ASTTransformer(m_code_generator.get()); 393 394 if (ast_transformer) 395 ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext()); 396 else 397 ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext()); 398 399 diag_buf->EndSourceFile(); 400 401 TextDiagnosticBuffer::const_iterator diag_iterator; 402 403 int num_errors = 0; 404 405 for (diag_iterator = diag_buf->warn_begin(); 406 diag_iterator != diag_buf->warn_end(); 407 ++diag_iterator) 408 stream.Printf("warning: %s\n", (*diag_iterator).second.c_str()); 409 410 num_errors = 0; 411 412 for (diag_iterator = diag_buf->err_begin(); 413 diag_iterator != diag_buf->err_end(); 414 ++diag_iterator) 415 { 416 num_errors++; 417 stream.Printf("error: %s\n", (*diag_iterator).second.c_str()); 418 } 419 420 for (diag_iterator = diag_buf->note_begin(); 421 diag_iterator != diag_buf->note_end(); 422 ++diag_iterator) 423 stream.Printf("note: %s\n", (*diag_iterator).second.c_str()); 424 425 if (!num_errors) 426 { 427 if (m_expr.DeclMap() && !m_expr.DeclMap()->ResolveUnknownTypes()) 428 { 429 stream.Printf("error: Couldn't infer the type of a variable\n"); 430 num_errors++; 431 } 432 } 433 434 return num_errors; 435} 436 437static bool FindFunctionInModule (ConstString &mangled_name, 438 llvm::Module *module, 439 const char *orig_name) 440{ 441 for (llvm::Module::iterator fi = module->getFunctionList().begin(), fe = module->getFunctionList().end(); 442 fi != fe; 443 ++fi) 444 { 445 if (fi->getName().str().find(orig_name) != std::string::npos) 446 { 447 mangled_name.SetCString(fi->getName().str().c_str()); 448 return true; 449 } 450 } 451 452 return false; 453} 454 455Error 456ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_addr, 457 lldb::addr_t &func_end, 458 std::unique_ptr<IRExecutionUnit> &execution_unit_ap, 459 ExecutionContext &exe_ctx, 460 bool &can_interpret, 461 ExecutionPolicy execution_policy) 462{ 463 func_addr = LLDB_INVALID_ADDRESS; 464 func_end = LLDB_INVALID_ADDRESS; 465 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 466 467 std::unique_ptr<llvm::ExecutionEngine> execution_engine_ap; 468 469 Error err; 470 471 std::unique_ptr<llvm::Module> module_ap (m_code_generator->ReleaseModule()); 472 473 if (!module_ap.get()) 474 { 475 err.SetErrorToGenericError(); 476 err.SetErrorString("IR doesn't contain a module"); 477 return err; 478 } 479 480 // Find the actual name of the function (it's often mangled somehow) 481 482 ConstString function_name; 483 484 if (!FindFunctionInModule(function_name, module_ap.get(), m_expr.FunctionName())) 485 { 486 err.SetErrorToGenericError(); 487 err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName()); 488 return err; 489 } 490 else 491 { 492 if (log) 493 log->Printf("Found function %s for %s", function_name.AsCString(), m_expr.FunctionName()); 494 } 495 496 m_execution_unit.reset(new IRExecutionUnit(m_llvm_context, // handed off here 497 module_ap, // handed off here 498 function_name, 499 exe_ctx.GetTargetSP(), 500 m_compiler->getTargetOpts().Features)); 501 502 ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); // result can be NULL 503 504 if (decl_map) 505 { 506 Stream *error_stream = NULL; 507 Target *target = exe_ctx.GetTargetPtr(); 508 if (target) 509 error_stream = target->GetDebugger().GetErrorFile().get(); 510 511 IRForTarget ir_for_target(decl_map, 512 m_expr.NeedsVariableResolution(), 513 *m_execution_unit, 514 error_stream, 515 function_name.AsCString()); 516 517 bool ir_can_run = ir_for_target.runOnModule(*m_execution_unit->GetModule()); 518 519 Error interpret_error; 520 521 can_interpret = IRInterpreter::CanInterpret(*m_execution_unit->GetModule(), *m_execution_unit->GetFunction(), interpret_error); 522 523 Process *process = exe_ctx.GetProcessPtr(); 524 525 if (!ir_can_run) 526 { 527 err.SetErrorString("The expression could not be prepared to run in the target"); 528 return err; 529 } 530 531 if (!can_interpret && execution_policy == eExecutionPolicyNever) 532 { 533 err.SetErrorStringWithFormat("Can't run the expression locally: %s", interpret_error.AsCString()); 534 return err; 535 } 536 537 if (!process && execution_policy == eExecutionPolicyAlways) 538 { 539 err.SetErrorString("Expression needed to run in the target, but the target can't be run"); 540 return err; 541 } 542 543 if (execution_policy == eExecutionPolicyAlways || !can_interpret) 544 { 545 if (m_expr.NeedsValidation() && process) 546 { 547 if (!process->GetDynamicCheckers()) 548 { 549 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions(); 550 551 StreamString install_errors; 552 553 if (!dynamic_checkers->Install(install_errors, exe_ctx)) 554 { 555 if (install_errors.GetString().empty()) 556 err.SetErrorString ("couldn't install checkers, unknown error"); 557 else 558 err.SetErrorString (install_errors.GetString().c_str()); 559 560 return err; 561 } 562 563 process->SetDynamicCheckers(dynamic_checkers); 564 565 if (log) 566 log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers =="); 567 } 568 569 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString()); 570 571 if (!ir_dynamic_checks.runOnModule(*m_execution_unit->GetModule())) 572 { 573 err.SetErrorToGenericError(); 574 err.SetErrorString("Couldn't add dynamic checks to the expression"); 575 return err; 576 } 577 } 578 579 m_execution_unit->GetRunnableInfo(err, func_addr, func_end); 580 } 581 } 582 else 583 { 584 m_execution_unit->GetRunnableInfo(err, func_addr, func_end); 585 } 586 587 execution_unit_ap.reset (m_execution_unit.release()); 588 589 return err; 590} 591