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/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"
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"
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"
58 #if defined (USE_STANDARD_JIT)
59 #include "llvm/ExecutionEngine/JIT.h"
61 #include "llvm/ExecutionEngine/MCJIT.h"
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"
71 using namespace clang;
73 using namespace lldb_private;
75 //===----------------------------------------------------------------------===//
76 // Utility Methods for Clang
77 //===----------------------------------------------------------------------===//
79 std::string GetBuiltinIncludePath(const char *Argv0) {
80 SmallString<128> P(llvm::sys::fs::getMainExecutable(
81 Argv0, (void *)(intptr_t) GetBuiltinIncludePath));
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
87 // Get foo/lib/clang/<version>/include
88 llvm::sys::path::append(P, "lib", "clang", CLANG_VERSION_STRING,
96 //===----------------------------------------------------------------------===//
97 // Main driver for Clang
98 //===----------------------------------------------------------------------===//
100 static void LLVMErrorHandler(void *UserData, const std::string &Message) {
101 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
103 Diags.Report(diag::err_fe_error_backend) << Message;
105 // We cannot recover from llvm errors.
109 static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
110 using namespace clang::frontend;
112 switch (CI.getFrontendOpts().ProgramAction) {
114 llvm_unreachable("Invalid program action!");
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();
135 for (FrontendPluginRegistry::iterator it =
136 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
138 if (it->getName() == CI.getFrontendOpts().ActionName) {
139 llvm::OwningPtr<PluginASTAction> P(it->instantiate());
140 if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
146 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
147 << CI.getFrontendOpts().ActionName;
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();
162 static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
163 // Create the underlying action.
164 FrontendAction *Act = CreateFrontendBaseAction(CI);
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);
176 //===----------------------------------------------------------------------===//
177 // Implementation of ClangExpressionParser
178 //===----------------------------------------------------------------------===//
180 ClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope,
181 ClangExpression &expr) :
186 // Initialize targets first, so that --version shows registered targets.
187 static struct InitializeLLVM {
189 llvm::InitializeAllTargets();
190 llvm::InitializeAllAsmPrinters();
191 llvm::InitializeAllTargetMCs();
192 llvm::InitializeAllDisassemblers();
196 // 1. Create a new compiler instance.
197 m_compiler.reset(new CompilerInstance());
199 // 2. Install the target.
201 lldb::TargetSP target_sp;
203 target_sp = exe_scope->CalculateTarget();
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())
212 std::string triple = target_sp->GetArchitecture().GetTriple().str();
215 for (size_t i = 0; i < triple.size(); ++i)
217 if (triple[i] == '-')
226 m_compiler->getTargetOpts().Triple = triple;
230 m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
233 if (target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86 ||
234 target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
236 m_compiler->getTargetOpts().Features.push_back("+sse");
237 m_compiler->getTargetOpts().Features.push_back("+sse2");
240 if (m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos)
241 m_compiler->getTargetOpts().ABI = "apcs-gnu";
243 m_compiler->createDiagnostics();
245 // Create the target instance.
246 m_compiler->setTarget(TargetInfo::CreateTargetInfo(m_compiler->getDiagnostics(),
247 &m_compiler->getTargetOpts()));
249 assert (m_compiler->hasTarget());
253 lldb::LanguageType language = expr.Language();
257 case lldb::eLanguageTypeC:
259 case lldb::eLanguageTypeObjC:
260 m_compiler->getLangOpts().ObjC1 = true;
261 m_compiler->getLangOpts().ObjC2 = true;
263 case lldb::eLanguageTypeC_plus_plus:
264 m_compiler->getLangOpts().CPlusPlus = true;
265 m_compiler->getLangOpts().CPlusPlus11 = true;
267 case lldb::eLanguageTypeObjC_plus_plus:
269 m_compiler->getLangOpts().ObjC1 = true;
270 m_compiler->getLangOpts().ObjC2 = true;
271 m_compiler->getLangOpts().CPlusPlus = true;
272 m_compiler->getLangOpts().CPlusPlus11 = true;
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;
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
287 m_compiler->getLangOpts().SpellChecking = false;
289 lldb::ProcessSP process_sp;
291 process_sp = exe_scope->CalculateProcess();
293 if (process_sp && m_compiler->getLangOpts().ObjC1)
295 if (process_sp->GetObjCLanguageRuntime())
297 if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == eAppleObjC_V2)
298 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7));
300 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7));
302 if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
303 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
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
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;
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());
321 // Inform the target of the language options
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());
327 // 4. Set up the diagnostic buffer for reporting errors
329 m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer);
331 // 5. Set up the source management objects inside the compiler
333 clang::FileSystemOptions file_system_options;
334 m_file_manager.reset(new clang::FileManager(file_system_options));
336 if (!m_compiler->hasSourceManager())
337 m_compiler->createSourceManager(*m_file_manager.get());
339 m_compiler->createFileManager();
340 m_compiler->createPreprocessor();
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());
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(),
355 ClangExpressionDeclMap *decl_map = m_expr.DeclMap();
359 llvm::OwningPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy());
360 decl_map->InstallASTContext(ast_context.get());
361 ast_context->setExternalSource(ast_source);
364 m_compiler->setASTContext(ast_context.release());
366 std::string module_name("$__lldb_module");
368 m_llvm_context.reset(new LLVMContext());
369 m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(),
371 m_compiler->getCodeGenOpts(),
372 m_compiler->getTargetOpts(),
376 ClangExpressionParser::~ClangExpressionParser()
381 ClangExpressionParser::Parse (Stream &stream)
383 TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient());
385 diag_buf->FlushDiagnostics (m_compiler->getDiagnostics());
387 MemoryBuffer *memory_buffer = MemoryBuffer::getMemBufferCopy(m_expr.Text(), __FUNCTION__);
388 m_compiler->getSourceManager().createMainFileIDForMemBuffer (memory_buffer);
390 diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor());
392 ASTConsumer *ast_transformer = m_expr.ASTTransformer(m_code_generator.get());
395 ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext());
397 ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext());
399 diag_buf->EndSourceFile();
401 TextDiagnosticBuffer::const_iterator diag_iterator;
405 for (diag_iterator = diag_buf->warn_begin();
406 diag_iterator != diag_buf->warn_end();
408 stream.Printf("warning: %s\n", (*diag_iterator).second.c_str());
412 for (diag_iterator = diag_buf->err_begin();
413 diag_iterator != diag_buf->err_end();
417 stream.Printf("error: %s\n", (*diag_iterator).second.c_str());
420 for (diag_iterator = diag_buf->note_begin();
421 diag_iterator != diag_buf->note_end();
423 stream.Printf("note: %s\n", (*diag_iterator).second.c_str());
427 if (m_expr.DeclMap() && !m_expr.DeclMap()->ResolveUnknownTypes())
429 stream.Printf("error: Couldn't infer the type of a variable\n");
437 static bool FindFunctionInModule (ConstString &mangled_name,
438 llvm::Module *module,
439 const char *orig_name)
441 for (llvm::Module::iterator fi = module->getFunctionList().begin(), fe = module->getFunctionList().end();
445 if (fi->getName().str().find(orig_name) != std::string::npos)
447 mangled_name.SetCString(fi->getName().str().c_str());
456 ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_addr,
457 lldb::addr_t &func_end,
458 std::unique_ptr<IRExecutionUnit> &execution_unit_ap,
459 ExecutionContext &exe_ctx,
461 ExecutionPolicy execution_policy)
463 func_addr = LLDB_INVALID_ADDRESS;
464 func_end = LLDB_INVALID_ADDRESS;
465 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
467 std::unique_ptr<llvm::ExecutionEngine> execution_engine_ap;
471 std::unique_ptr<llvm::Module> module_ap (m_code_generator->ReleaseModule());
473 if (!module_ap.get())
475 err.SetErrorToGenericError();
476 err.SetErrorString("IR doesn't contain a module");
480 // Find the actual name of the function (it's often mangled somehow)
482 ConstString function_name;
484 if (!FindFunctionInModule(function_name, module_ap.get(), m_expr.FunctionName()))
486 err.SetErrorToGenericError();
487 err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName());
493 log->Printf("Found function %s for %s", function_name.AsCString(), m_expr.FunctionName());
496 m_execution_unit.reset(new IRExecutionUnit(m_llvm_context, // handed off here
497 module_ap, // handed off here
499 exe_ctx.GetTargetSP(),
500 m_compiler->getTargetOpts().Features));
502 ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); // result can be NULL
506 Stream *error_stream = NULL;
507 Target *target = exe_ctx.GetTargetPtr();
509 error_stream = target->GetDebugger().GetErrorFile().get();
511 IRForTarget ir_for_target(decl_map,
512 m_expr.NeedsVariableResolution(),
515 function_name.AsCString());
517 bool ir_can_run = ir_for_target.runOnModule(*m_execution_unit->GetModule());
519 Error interpret_error;
521 can_interpret = IRInterpreter::CanInterpret(*m_execution_unit->GetModule(), *m_execution_unit->GetFunction(), interpret_error);
523 Process *process = exe_ctx.GetProcessPtr();
527 err.SetErrorString("The expression could not be prepared to run in the target");
531 if (!can_interpret && execution_policy == eExecutionPolicyNever)
533 err.SetErrorStringWithFormat("Can't run the expression locally: %s", interpret_error.AsCString());
537 if (!process && execution_policy == eExecutionPolicyAlways)
539 err.SetErrorString("Expression needed to run in the target, but the target can't be run");
543 if (execution_policy == eExecutionPolicyAlways || !can_interpret)
545 if (m_expr.NeedsValidation() && process)
547 if (!process->GetDynamicCheckers())
549 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
551 StreamString install_errors;
553 if (!dynamic_checkers->Install(install_errors, exe_ctx))
555 if (install_errors.GetString().empty())
556 err.SetErrorString ("couldn't install checkers, unknown error");
558 err.SetErrorString (install_errors.GetString().c_str());
563 process->SetDynamicCheckers(dynamic_checkers);
566 log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
569 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString());
571 if (!ir_dynamic_checks.runOnModule(*m_execution_unit->GetModule()))
573 err.SetErrorToGenericError();
574 err.SetErrorString("Couldn't add dynamic checks to the expression");
579 m_execution_unit->GetRunnableInfo(err, func_addr, func_end);
584 m_execution_unit->GetRunnableInfo(err, func_addr, func_end);
587 execution_unit_ap.reset (m_execution_unit.release());