1 //===-- ClangExpressionParser.cpp -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "lldb/Expression/ClangExpressionParser.h"
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/StreamString.h"
20 #include "lldb/Expression/ClangASTSource.h"
21 #include "lldb/Expression/ClangExpression.h"
22 #include "lldb/Expression/ClangExpressionDeclMap.h"
23 #include "lldb/Expression/IRExecutionUnit.h"
24 #include "lldb/Expression/IRDynamicChecks.h"
25 #include "lldb/Expression/IRInterpreter.h"
26 #include "lldb/Target/ExecutionContext.h"
27 #include "lldb/Target/ObjCLanguageRuntime.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/Target.h"
31 #include "clang/AST/ASTContext.h"
32 #include "clang/AST/ExternalASTSource.h"
33 #include "clang/Basic/FileManager.h"
34 #include "clang/Basic/TargetInfo.h"
35 #include "clang/Basic/Version.h"
36 #include "clang/CodeGen/CodeGenAction.h"
37 #include "clang/CodeGen/ModuleBuilder.h"
38 #include "clang/Driver/CC1Options.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"
52 #include "llvm/ADT/StringRef.h"
53 #include "llvm/ExecutionEngine/ExecutionEngine.h"
54 #include "llvm/Support/Debug.h"
55 #include "llvm/Support/PathV1.h"
56 #include "llvm/Support/TargetSelect.h"
58 #if defined(__FreeBSD__)
59 #define USE_STANDARD_JIT
62 #if defined (USE_STANDARD_JIT)
63 #include "llvm/ExecutionEngine/JIT.h"
65 #include "llvm/ExecutionEngine/MCJIT.h"
67 #include "llvm/IR/LLVMContext.h"
68 #include "llvm/IR/Module.h"
69 #include "llvm/Support/ErrorHandling.h"
70 #include "llvm/Support/MemoryBuffer.h"
71 #include "llvm/Support/DynamicLibrary.h"
72 #include "llvm/Support/Host.h"
73 #include "llvm/Support/Signals.h"
75 using namespace clang;
77 using namespace lldb_private;
79 //===----------------------------------------------------------------------===//
80 // Utility Methods for Clang
81 //===----------------------------------------------------------------------===//
83 std::string GetBuiltinIncludePath(const char *Argv0) {
85 llvm::sys::Path::GetMainExecutable(Argv0,
86 (void*)(intptr_t) GetBuiltinIncludePath);
89 P.eraseComponent(); // Remove /clang from foo/bin/clang
90 P.eraseComponent(); // Remove /bin from foo/bin
92 // Get foo/lib/clang/<version>/include
93 P.appendComponent("lib");
94 P.appendComponent("clang");
95 P.appendComponent(CLANG_VERSION_STRING);
96 P.appendComponent("include");
103 //===----------------------------------------------------------------------===//
104 // Main driver for Clang
105 //===----------------------------------------------------------------------===//
107 static void LLVMErrorHandler(void *UserData, const std::string &Message) {
108 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
110 Diags.Report(diag::err_fe_error_backend) << Message;
112 // We cannot recover from llvm errors.
116 static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
117 using namespace clang::frontend;
119 switch (CI.getFrontendOpts().ProgramAction) {
121 llvm_unreachable("Invalid program action!");
123 case ASTDump: return new ASTDumpAction();
124 case ASTPrint: return new ASTPrintAction();
125 case ASTDumpXML: return new ASTDumpXMLAction();
126 case ASTView: return new ASTViewAction();
127 case DumpRawTokens: return new DumpRawTokensAction();
128 case DumpTokens: return new DumpTokensAction();
129 case EmitAssembly: return new EmitAssemblyAction();
130 case EmitBC: return new EmitBCAction();
131 case EmitHTML: return new HTMLPrintAction();
132 case EmitLLVM: return new EmitLLVMAction();
133 case EmitLLVMOnly: return new EmitLLVMOnlyAction();
134 case EmitCodeGenOnly: return new EmitCodeGenOnlyAction();
135 case EmitObj: return new EmitObjAction();
136 case FixIt: return new FixItAction();
137 case GeneratePCH: return new GeneratePCHAction();
138 case GeneratePTH: return new GeneratePTHAction();
139 case InitOnly: return new InitOnlyAction();
140 case ParseSyntaxOnly: return new SyntaxOnlyAction();
143 for (FrontendPluginRegistry::iterator it =
144 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
146 if (it->getName() == CI.getFrontendOpts().ActionName) {
147 llvm::OwningPtr<PluginASTAction> P(it->instantiate());
148 if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
154 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
155 << CI.getFrontendOpts().ActionName;
159 case PrintDeclContext: return new DeclContextPrintAction();
160 case PrintPreamble: return new PrintPreambleAction();
161 case PrintPreprocessedInput: return new PrintPreprocessedAction();
162 case RewriteMacros: return new RewriteMacrosAction();
163 case RewriteObjC: return new RewriteObjCAction();
164 case RewriteTest: return new RewriteTestAction();
165 //case RunAnalysis: return new AnalysisAction();
166 case RunPreprocessorOnly: return new PreprocessOnlyAction();
170 static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
171 // Create the underlying action.
172 FrontendAction *Act = CreateFrontendBaseAction(CI);
176 // If there are any AST files to merge, create a frontend action
177 // adaptor to perform the merge.
178 if (!CI.getFrontendOpts().ASTMergeFiles.empty())
179 Act = new ASTMergeAction(Act, CI.getFrontendOpts().ASTMergeFiles);
184 //===----------------------------------------------------------------------===//
185 // Implementation of ClangExpressionParser
186 //===----------------------------------------------------------------------===//
188 ClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope,
189 ClangExpression &expr) :
194 // Initialize targets first, so that --version shows registered targets.
195 static struct InitializeLLVM {
197 llvm::InitializeAllTargets();
198 llvm::InitializeAllAsmPrinters();
199 llvm::InitializeAllTargetMCs();
200 llvm::InitializeAllDisassemblers();
202 llvm::DisablePrettyStackTrace = true;
206 // 1. Create a new compiler instance.
207 m_compiler.reset(new CompilerInstance());
209 // 2. Install the target.
211 lldb::TargetSP target_sp;
213 target_sp = exe_scope->CalculateTarget();
215 // TODO: figure out what to really do when we don't have a valid target.
216 // Sometimes this will be ok to just use the host target triple (when we
217 // evaluate say "2+3", but other expressions like breakpoint conditions
218 // and other things that _are_ target specific really shouldn't just be
219 // using the host triple. This needs to be fixed in a better way.
220 if (target_sp && target_sp->GetArchitecture().IsValid())
222 std::string triple = target_sp->GetArchitecture().GetTriple().str();
225 for (size_t i = 0; i < triple.size(); ++i)
227 if (triple[i] == '-')
236 m_compiler->getTargetOpts().Triple = triple;
240 m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
243 if (target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86 ||
244 target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
246 m_compiler->getTargetOpts().Features.push_back("+sse");
247 m_compiler->getTargetOpts().Features.push_back("+sse2");
250 if (m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos)
251 m_compiler->getTargetOpts().ABI = "apcs-gnu";
253 m_compiler->createDiagnostics();
255 // Create the target instance.
256 m_compiler->setTarget(TargetInfo::CreateTargetInfo(m_compiler->getDiagnostics(),
257 &m_compiler->getTargetOpts()));
259 assert (m_compiler->hasTarget());
263 lldb::LanguageType language = expr.Language();
267 case lldb::eLanguageTypeC:
269 case lldb::eLanguageTypeObjC:
270 m_compiler->getLangOpts().ObjC1 = true;
271 m_compiler->getLangOpts().ObjC2 = true;
273 case lldb::eLanguageTypeC_plus_plus:
274 m_compiler->getLangOpts().CPlusPlus = true;
275 m_compiler->getLangOpts().CPlusPlus11 = true;
277 case lldb::eLanguageTypeObjC_plus_plus:
279 m_compiler->getLangOpts().ObjC1 = true;
280 m_compiler->getLangOpts().ObjC2 = true;
281 m_compiler->getLangOpts().CPlusPlus = true;
282 m_compiler->getLangOpts().CPlusPlus11 = true;
286 m_compiler->getLangOpts().Bool = true;
287 m_compiler->getLangOpts().WChar = true;
288 m_compiler->getLangOpts().Blocks = true;
289 m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients
290 if (expr.DesiredResultType() == ClangExpression::eResultTypeId)
291 m_compiler->getLangOpts().DebuggerCastResultToId = true;
293 // Spell checking is a nice feature, but it ends up completing a
294 // lot of types that we didn't strictly speaking need to complete.
295 // As a result, we spend a long time parsing and importing debug
297 m_compiler->getLangOpts().SpellChecking = false;
299 lldb::ProcessSP process_sp;
301 process_sp = exe_scope->CalculateProcess();
303 if (process_sp && m_compiler->getLangOpts().ObjC1)
305 if (process_sp->GetObjCLanguageRuntime())
307 if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == eAppleObjC_V2)
308 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7));
310 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7));
312 if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
313 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
317 m_compiler->getLangOpts().ThreadsafeStatics = false;
318 m_compiler->getLangOpts().AccessControl = false; // Debuggers get universal access
319 m_compiler->getLangOpts().DollarIdents = true; // $ indicates a persistent variable name
321 // Set CodeGen options
322 m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
323 m_compiler->getCodeGenOpts().InstrumentFunctions = false;
325 // Disable some warnings.
326 m_compiler->getDiagnostics().setDiagnosticGroupMapping("unused-value", clang::diag::MAP_IGNORE, SourceLocation());
327 m_compiler->getDiagnostics().setDiagnosticGroupMapping("odr", clang::diag::MAP_IGNORE, SourceLocation());
329 // Inform the target of the language options
331 // FIXME: We shouldn't need to do this, the target should be immutable once
332 // created. This complexity should be lifted elsewhere.
333 m_compiler->getTarget().setForcedLangOptions(m_compiler->getLangOpts());
335 // 4. Set up the diagnostic buffer for reporting errors
337 m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer);
339 // 5. Set up the source management objects inside the compiler
341 clang::FileSystemOptions file_system_options;
342 m_file_manager.reset(new clang::FileManager(file_system_options));
344 if (!m_compiler->hasSourceManager())
345 m_compiler->createSourceManager(*m_file_manager.get());
347 m_compiler->createFileManager();
348 m_compiler->createPreprocessor();
350 // 6. Most of this we get from the CompilerInstance, but we
351 // also want to give the context an ExternalASTSource.
352 m_selector_table.reset(new SelectorTable());
353 m_builtin_context.reset(new Builtin::Context());
355 std::unique_ptr<clang::ASTContext> ast_context(new ASTContext(m_compiler->getLangOpts(),
356 m_compiler->getSourceManager(),
357 &m_compiler->getTarget(),
358 m_compiler->getPreprocessor().getIdentifierTable(),
359 *m_selector_table.get(),
360 *m_builtin_context.get(),
363 ClangExpressionDeclMap *decl_map = m_expr.DeclMap();
367 llvm::OwningPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy());
368 decl_map->InstallASTContext(ast_context.get());
369 ast_context->setExternalSource(ast_source);
372 m_compiler->setASTContext(ast_context.release());
374 std::string module_name("$__lldb_module");
376 m_llvm_context.reset(new LLVMContext());
377 m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(),
379 m_compiler->getCodeGenOpts(),
380 m_compiler->getTargetOpts(),
384 ClangExpressionParser::~ClangExpressionParser()
389 ClangExpressionParser::Parse (Stream &stream)
391 TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient());
393 diag_buf->FlushDiagnostics (m_compiler->getDiagnostics());
395 MemoryBuffer *memory_buffer = MemoryBuffer::getMemBufferCopy(m_expr.Text(), __FUNCTION__);
396 m_compiler->getSourceManager().createMainFileIDForMemBuffer (memory_buffer);
398 diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor());
400 ASTConsumer *ast_transformer = m_expr.ASTTransformer(m_code_generator.get());
403 ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext());
405 ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext());
407 diag_buf->EndSourceFile();
409 TextDiagnosticBuffer::const_iterator diag_iterator;
413 for (diag_iterator = diag_buf->warn_begin();
414 diag_iterator != diag_buf->warn_end();
416 stream.Printf("warning: %s\n", (*diag_iterator).second.c_str());
420 for (diag_iterator = diag_buf->err_begin();
421 diag_iterator != diag_buf->err_end();
425 stream.Printf("error: %s\n", (*diag_iterator).second.c_str());
428 for (diag_iterator = diag_buf->note_begin();
429 diag_iterator != diag_buf->note_end();
431 stream.Printf("note: %s\n", (*diag_iterator).second.c_str());
435 if (m_expr.DeclMap() && !m_expr.DeclMap()->ResolveUnknownTypes())
437 stream.Printf("error: Couldn't infer the type of a variable\n");
445 static bool FindFunctionInModule (ConstString &mangled_name,
446 llvm::Module *module,
447 const char *orig_name)
449 for (llvm::Module::iterator fi = module->getFunctionList().begin(), fe = module->getFunctionList().end();
453 if (fi->getName().str().find(orig_name) != std::string::npos)
455 mangled_name.SetCString(fi->getName().str().c_str());
464 ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_addr,
465 lldb::addr_t &func_end,
466 std::unique_ptr<IRExecutionUnit> &execution_unit_ap,
467 ExecutionContext &exe_ctx,
469 ExecutionPolicy execution_policy)
471 func_addr = LLDB_INVALID_ADDRESS;
472 func_end = LLDB_INVALID_ADDRESS;
473 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
475 std::unique_ptr<llvm::ExecutionEngine> execution_engine_ap;
479 std::unique_ptr<llvm::Module> module_ap (m_code_generator->ReleaseModule());
481 if (!module_ap.get())
483 err.SetErrorToGenericError();
484 err.SetErrorString("IR doesn't contain a module");
488 // Find the actual name of the function (it's often mangled somehow)
490 ConstString function_name;
492 if (!FindFunctionInModule(function_name, module_ap.get(), m_expr.FunctionName()))
494 err.SetErrorToGenericError();
495 err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName());
501 log->Printf("Found function %s for %s", function_name.AsCString(), m_expr.FunctionName());
504 m_execution_unit.reset(new IRExecutionUnit(m_llvm_context, // handed off here
505 module_ap, // handed off here
507 exe_ctx.GetTargetSP(),
508 m_compiler->getTargetOpts().Features));
510 ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); // result can be NULL
514 Stream *error_stream = NULL;
515 Target *target = exe_ctx.GetTargetPtr();
517 error_stream = &target->GetDebugger().GetErrorStream();
519 IRForTarget ir_for_target(decl_map,
520 m_expr.NeedsVariableResolution(),
523 function_name.AsCString());
525 bool ir_can_run = ir_for_target.runOnModule(*m_execution_unit->GetModule());
527 Error interpret_error;
529 can_interpret = IRInterpreter::CanInterpret(*m_execution_unit->GetModule(), *m_execution_unit->GetFunction(), interpret_error);
531 Process *process = exe_ctx.GetProcessPtr();
535 err.SetErrorString("The expression could not be prepared to run in the target");
539 if (!can_interpret && execution_policy == eExecutionPolicyNever)
541 err.SetErrorStringWithFormat("Can't run the expression locally: %s", interpret_error.AsCString());
545 if (!process && execution_policy == eExecutionPolicyAlways)
547 err.SetErrorString("Expression needed to run in the target, but the target can't be run");
551 if (execution_policy == eExecutionPolicyAlways || !can_interpret)
553 if (m_expr.NeedsValidation() && process)
555 if (!process->GetDynamicCheckers())
557 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
559 StreamString install_errors;
561 if (!dynamic_checkers->Install(install_errors, exe_ctx))
563 if (install_errors.GetString().empty())
564 err.SetErrorString ("couldn't install checkers, unknown error");
566 err.SetErrorString (install_errors.GetString().c_str());
571 process->SetDynamicCheckers(dynamic_checkers);
574 log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
577 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString());
579 if (!ir_dynamic_checks.runOnModule(*m_execution_unit->GetModule()))
581 err.SetErrorToGenericError();
582 err.SetErrorString("Couldn't add dynamic checks to the expression");
587 m_execution_unit->GetRunnableInfo(err, func_addr, func_end);
592 m_execution_unit->GetRunnableInfo(err, func_addr, func_end);
595 execution_unit_ap.reset (m_execution_unit.release());