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/Module.h"
19 #include "lldb/Core/Stream.h"
20 #include "lldb/Core/StreamFile.h"
21 #include "lldb/Core/StreamString.h"
22 #include "lldb/Expression/ClangASTSource.h"
23 #include "lldb/Expression/ClangExpression.h"
24 #include "lldb/Expression/ClangExpressionDeclMap.h"
25 #include "lldb/Expression/ClangModulesDeclVendor.h"
26 #include "lldb/Expression/IRExecutionUnit.h"
27 #include "lldb/Expression/IRDynamicChecks.h"
28 #include "lldb/Expression/IRInterpreter.h"
29 #include "lldb/Host/File.h"
30 #include "lldb/Host/HostInfo.h"
31 #include "lldb/Symbol/SymbolVendor.h"
32 #include "lldb/Target/ExecutionContext.h"
33 #include "lldb/Target/ObjCLanguageRuntime.h"
34 #include "lldb/Target/Process.h"
35 #include "lldb/Target/Target.h"
37 #include "clang/AST/ASTContext.h"
38 #include "clang/AST/ExternalASTSource.h"
39 #include "clang/Basic/FileManager.h"
40 #include "clang/Basic/TargetInfo.h"
41 #include "clang/Basic/Version.h"
42 #include "clang/CodeGen/CodeGenAction.h"
43 #include "clang/CodeGen/ModuleBuilder.h"
44 #include "clang/Frontend/CompilerInstance.h"
45 #include "clang/Frontend/CompilerInvocation.h"
46 #include "clang/Frontend/FrontendActions.h"
47 #include "clang/Frontend/FrontendDiagnostic.h"
48 #include "clang/Frontend/FrontendPluginRegistry.h"
49 #include "clang/Frontend/TextDiagnosticBuffer.h"
50 #include "clang/Frontend/TextDiagnosticPrinter.h"
51 #include "clang/Lex/Preprocessor.h"
52 #include "clang/Parse/ParseAST.h"
53 #include "clang/Rewrite/Frontend/FrontendActions.h"
54 #include "clang/Sema/SemaConsumer.h"
55 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
57 #include "llvm/ADT/StringRef.h"
58 #include "llvm/ExecutionEngine/ExecutionEngine.h"
59 #include "llvm/Support/Debug.h"
60 #include "llvm/Support/FileSystem.h"
61 #include "llvm/Support/TargetSelect.h"
63 #include "llvm/ExecutionEngine/MCJIT.h"
64 #include "llvm/IR/LLVMContext.h"
65 #include "llvm/IR/Module.h"
66 #include "llvm/Support/ErrorHandling.h"
67 #include "llvm/Support/MemoryBuffer.h"
68 #include "llvm/Support/DynamicLibrary.h"
69 #include "llvm/Support/Host.h"
70 #include "llvm/Support/Signals.h"
72 using namespace clang;
74 using namespace lldb_private;
76 //===----------------------------------------------------------------------===//
77 // Utility Methods for Clang
78 //===----------------------------------------------------------------------===//
80 std::string GetBuiltinIncludePath(const char *Argv0) {
81 SmallString<128> P(llvm::sys::fs::getMainExecutable(
82 Argv0, (void *)(intptr_t) GetBuiltinIncludePath));
85 llvm::sys::path::remove_filename(P); // Remove /clang from foo/bin/clang
86 llvm::sys::path::remove_filename(P); // Remove /bin from foo/bin
88 // Get foo/lib/clang/<version>/include
89 llvm::sys::path::append(P, "lib", "clang", CLANG_VERSION_STRING,
96 class ClangExpressionParser::LLDBPreprocessorCallbacks : public PPCallbacks
98 ClangModulesDeclVendor &m_decl_vendor;
99 StreamString m_error_stream;
100 bool m_has_errors = false;
102 LLDBPreprocessorCallbacks(ClangModulesDeclVendor &decl_vendor) :
103 m_decl_vendor(decl_vendor)
107 virtual void moduleImport(SourceLocation import_location,
109 const clang::Module * /*null*/)
111 std::vector<llvm::StringRef> string_path;
113 for (const std::pair<IdentifierInfo *, SourceLocation> &component : path)
115 string_path.push_back(component.first->getName());
118 StreamString error_stream;
120 if (!m_decl_vendor.AddModule(string_path, m_error_stream))
131 const std::string &getErrorString()
133 return m_error_stream.GetString();
137 //===----------------------------------------------------------------------===//
138 // Implementation of ClangExpressionParser
139 //===----------------------------------------------------------------------===//
141 ClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope,
142 ClangExpression &expr,
143 bool generate_debug_info) :
147 m_pp_callbacks(nullptr)
149 // 1. Create a new compiler instance.
150 m_compiler.reset(new CompilerInstance());
152 // 2. Install the target.
154 lldb::TargetSP target_sp;
156 target_sp = exe_scope->CalculateTarget();
158 // TODO: figure out what to really do when we don't have a valid target.
159 // Sometimes this will be ok to just use the host target triple (when we
160 // evaluate say "2+3", but other expressions like breakpoint conditions
161 // and other things that _are_ target specific really shouldn't just be
162 // using the host triple. This needs to be fixed in a better way.
163 if (target_sp && target_sp->GetArchitecture().IsValid())
165 std::string triple = target_sp->GetArchitecture().GetTriple().str();
166 m_compiler->getTargetOpts().Triple = triple;
170 m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
173 if (target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86 ||
174 target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
176 m_compiler->getTargetOpts().Features.push_back("+sse");
177 m_compiler->getTargetOpts().Features.push_back("+sse2");
180 // Any arm32 iOS environment, but not on arm64
181 if (m_compiler->getTargetOpts().Triple.find("arm64") == std::string::npos &&
182 m_compiler->getTargetOpts().Triple.find("arm") != std::string::npos &&
183 m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos)
185 m_compiler->getTargetOpts().ABI = "apcs-gnu";
188 m_compiler->createDiagnostics();
190 // Create the target instance.
191 m_compiler->setTarget(TargetInfo::CreateTargetInfo(
192 m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts));
194 assert (m_compiler->hasTarget());
198 lldb::LanguageType language = expr.Language();
202 case lldb::eLanguageTypeC:
204 case lldb::eLanguageTypeObjC:
205 m_compiler->getLangOpts().ObjC1 = true;
206 m_compiler->getLangOpts().ObjC2 = true;
208 case lldb::eLanguageTypeC_plus_plus:
209 m_compiler->getLangOpts().CPlusPlus = true;
210 m_compiler->getLangOpts().CPlusPlus11 = true;
211 m_compiler->getHeaderSearchOpts().UseLibcxx = true;
213 case lldb::eLanguageTypeObjC_plus_plus:
215 m_compiler->getLangOpts().ObjC1 = true;
216 m_compiler->getLangOpts().ObjC2 = true;
217 m_compiler->getLangOpts().CPlusPlus = true;
218 m_compiler->getLangOpts().CPlusPlus11 = true;
219 m_compiler->getHeaderSearchOpts().UseLibcxx = true;
223 m_compiler->getLangOpts().Bool = true;
224 m_compiler->getLangOpts().WChar = true;
225 m_compiler->getLangOpts().Blocks = true;
226 m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients
227 if (expr.DesiredResultType() == ClangExpression::eResultTypeId)
228 m_compiler->getLangOpts().DebuggerCastResultToId = true;
230 // Spell checking is a nice feature, but it ends up completing a
231 // lot of types that we didn't strictly speaking need to complete.
232 // As a result, we spend a long time parsing and importing debug
234 m_compiler->getLangOpts().SpellChecking = false;
236 lldb::ProcessSP process_sp;
238 process_sp = exe_scope->CalculateProcess();
240 if (process_sp && m_compiler->getLangOpts().ObjC1)
242 if (process_sp->GetObjCLanguageRuntime())
244 if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == eAppleObjC_V2)
245 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7));
247 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7));
249 if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
250 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
254 m_compiler->getLangOpts().ThreadsafeStatics = false;
255 m_compiler->getLangOpts().AccessControl = false; // Debuggers get universal access
256 m_compiler->getLangOpts().DollarIdents = true; // $ indicates a persistent variable name
258 // Set CodeGen options
259 m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
260 m_compiler->getCodeGenOpts().InstrumentFunctions = false;
261 m_compiler->getCodeGenOpts().DisableFPElim = true;
262 m_compiler->getCodeGenOpts().OmitLeafFramePointer = false;
263 if (generate_debug_info)
264 m_compiler->getCodeGenOpts().setDebugInfo(CodeGenOptions::FullDebugInfo);
266 m_compiler->getCodeGenOpts().setDebugInfo(CodeGenOptions::NoDebugInfo);
268 // Disable some warnings.
269 m_compiler->getDiagnostics().setSeverityForGroup(clang::diag::Flavor::WarningOrError,
270 "unused-value", clang::diag::Severity::Ignored, SourceLocation());
271 m_compiler->getDiagnostics().setSeverityForGroup(clang::diag::Flavor::WarningOrError,
272 "odr", clang::diag::Severity::Ignored, SourceLocation());
274 // Inform the target of the language options
276 // FIXME: We shouldn't need to do this, the target should be immutable once
277 // created. This complexity should be lifted elsewhere.
278 m_compiler->getTarget().adjust(m_compiler->getLangOpts());
280 // 4. Set up the diagnostic buffer for reporting errors
282 m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer);
284 // 5. Set up the source management objects inside the compiler
286 clang::FileSystemOptions file_system_options;
287 m_file_manager.reset(new clang::FileManager(file_system_options));
289 if (!m_compiler->hasSourceManager())
290 m_compiler->createSourceManager(*m_file_manager.get());
292 m_compiler->createFileManager();
293 m_compiler->createPreprocessor(TU_Complete);
295 if (ClangModulesDeclVendor *decl_vendor = target_sp->GetClangModulesDeclVendor())
297 std::unique_ptr<PPCallbacks> pp_callbacks(new LLDBPreprocessorCallbacks(*decl_vendor));
298 m_pp_callbacks = static_cast<LLDBPreprocessorCallbacks*>(pp_callbacks.get());
299 m_compiler->getPreprocessor().addPPCallbacks(std::move(pp_callbacks));
302 // 6. Most of this we get from the CompilerInstance, but we
303 // also want to give the context an ExternalASTSource.
304 m_selector_table.reset(new SelectorTable());
305 m_builtin_context.reset(new Builtin::Context());
307 std::unique_ptr<clang::ASTContext> ast_context(new ASTContext(m_compiler->getLangOpts(),
308 m_compiler->getSourceManager(),
309 m_compiler->getPreprocessor().getIdentifierTable(),
310 *m_selector_table.get(),
311 *m_builtin_context.get()));
313 ast_context->InitBuiltinTypes(m_compiler->getTarget());
315 ClangExpressionDeclMap *decl_map = m_expr.DeclMap();
319 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy());
320 decl_map->InstallASTContext(ast_context.get());
321 ast_context->setExternalSource(ast_source);
324 m_compiler->setASTContext(ast_context.release());
326 std::string module_name("$__lldb_module");
328 m_llvm_context.reset(new LLVMContext());
329 m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(),
331 m_compiler->getCodeGenOpts(),
332 m_compiler->getTargetOpts(),
336 ClangExpressionParser::~ClangExpressionParser()
341 ClangExpressionParser::Parse (Stream &stream)
343 TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient());
345 diag_buf->FlushDiagnostics (m_compiler->getDiagnostics());
347 const char *expr_text = m_expr.Text();
349 clang::SourceManager &SourceMgr = m_compiler->getSourceManager();
350 bool created_main_file = false;
351 if (m_compiler->getCodeGenOpts().getDebugInfo() == CodeGenOptions::FullDebugInfo)
353 std::string temp_source_path;
356 llvm::SmallString<PATH_MAX> result_path;
357 FileSpec tmpdir_file_spec;
358 if (HostInfo::GetLLDBPath(lldb::ePathTypeLLDBTempSystemDir, tmpdir_file_spec))
360 tmpdir_file_spec.AppendPathComponent("lldb-%%%%%%.expr");
361 temp_source_path = std::move(tmpdir_file_spec.GetPath());
362 llvm::sys::fs::createUniqueFile(temp_source_path, temp_fd, result_path);
366 llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path);
371 lldb_private::File file (temp_fd, true);
372 const size_t expr_text_len = strlen(expr_text);
373 size_t bytes_written = expr_text_len;
374 if (file.Write(expr_text, bytes_written).Success())
376 if (bytes_written == expr_text_len)
379 SourceMgr.setMainFileID(SourceMgr.createFileID(
380 m_file_manager->getFile(result_path),
381 SourceLocation(), SrcMgr::C_User));
382 created_main_file = true;
388 if (!created_main_file)
390 std::unique_ptr<MemoryBuffer> memory_buffer = MemoryBuffer::getMemBufferCopy(expr_text, __FUNCTION__);
391 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(memory_buffer)));
394 diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor());
396 ASTConsumer *ast_transformer = m_expr.ASTTransformer(m_code_generator.get());
399 ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext());
401 ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext());
403 diag_buf->EndSourceFile();
405 TextDiagnosticBuffer::const_iterator diag_iterator;
409 if (m_pp_callbacks && m_pp_callbacks->hasErrors())
413 stream.PutCString(m_pp_callbacks->getErrorString().c_str());
416 for (diag_iterator = diag_buf->warn_begin();
417 diag_iterator != diag_buf->warn_end();
419 stream.Printf("warning: %s\n", (*diag_iterator).second.c_str());
421 for (diag_iterator = diag_buf->err_begin();
422 diag_iterator != diag_buf->err_end();
426 stream.Printf("error: %s\n", (*diag_iterator).second.c_str());
429 for (diag_iterator = diag_buf->note_begin();
430 diag_iterator != diag_buf->note_end();
432 stream.Printf("note: %s\n", (*diag_iterator).second.c_str());
436 if (m_expr.DeclMap() && !m_expr.DeclMap()->ResolveUnknownTypes())
438 stream.Printf("error: Couldn't infer the type of a variable\n");
446 static bool FindFunctionInModule (ConstString &mangled_name,
447 llvm::Module *module,
448 const char *orig_name)
450 for (llvm::Module::iterator fi = module->getFunctionList().begin(), fe = module->getFunctionList().end();
454 if (fi->getName().str().find(orig_name) != std::string::npos)
456 mangled_name.SetCString(fi->getName().str().c_str());
465 ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_addr,
466 lldb::addr_t &func_end,
467 std::shared_ptr<IRExecutionUnit> &execution_unit_sp,
468 ExecutionContext &exe_ctx,
470 ExecutionPolicy execution_policy)
472 func_addr = LLDB_INVALID_ADDRESS;
473 func_end = LLDB_INVALID_ADDRESS;
474 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
478 std::unique_ptr<llvm::Module> llvm_module_ap (m_code_generator->ReleaseModule());
480 if (!llvm_module_ap.get())
482 err.SetErrorToGenericError();
483 err.SetErrorString("IR doesn't contain a module");
487 // Find the actual name of the function (it's often mangled somehow)
489 ConstString function_name;
491 if (!FindFunctionInModule(function_name, llvm_module_ap.get(), m_expr.FunctionName()))
493 err.SetErrorToGenericError();
494 err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName());
500 log->Printf("Found function %s for %s", function_name.AsCString(), m_expr.FunctionName());
503 execution_unit_sp.reset(new IRExecutionUnit (m_llvm_context, // handed off here
504 llvm_module_ap, // handed off here
506 exe_ctx.GetTargetSP(),
507 m_compiler->getTargetOpts().Features));
509 ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); // result can be NULL
513 Stream *error_stream = NULL;
514 Target *target = exe_ctx.GetTargetPtr();
516 error_stream = target->GetDebugger().GetErrorFile().get();
518 IRForTarget ir_for_target(decl_map,
519 m_expr.NeedsVariableResolution(),
522 function_name.AsCString());
524 bool ir_can_run = ir_for_target.runOnModule(*execution_unit_sp->GetModule());
526 Error interpret_error;
528 can_interpret = IRInterpreter::CanInterpret(*execution_unit_sp->GetModule(), *execution_unit_sp->GetFunction(), interpret_error);
530 Process *process = exe_ctx.GetProcessPtr();
534 err.SetErrorString("The expression could not be prepared to run in the target");
538 if (!can_interpret && execution_policy == eExecutionPolicyNever)
540 err.SetErrorStringWithFormat("Can't run the expression locally: %s", interpret_error.AsCString());
544 if (!process && execution_policy == eExecutionPolicyAlways)
546 err.SetErrorString("Expression needed to run in the target, but the target can't be run");
550 if (execution_policy == eExecutionPolicyAlways || !can_interpret)
552 if (m_expr.NeedsValidation() && process)
554 if (!process->GetDynamicCheckers())
556 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
558 StreamString install_errors;
560 if (!dynamic_checkers->Install(install_errors, exe_ctx))
562 if (install_errors.GetString().empty())
563 err.SetErrorString ("couldn't install checkers, unknown error");
565 err.SetErrorString (install_errors.GetString().c_str());
570 process->SetDynamicCheckers(dynamic_checkers);
573 log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
576 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString());
578 if (!ir_dynamic_checks.runOnModule(*execution_unit_sp->GetModule()))
580 err.SetErrorToGenericError();
581 err.SetErrorString("Couldn't add dynamic checks to the expression");
586 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
591 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
598 ClangExpressionParser::GetGenerateDebugInfo () const
601 return m_compiler->getCodeGenOpts().getDebugInfo() == CodeGenOptions::FullDebugInfo;