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/IRExecutionUnit.h"
26 #include "lldb/Expression/IRDynamicChecks.h"
27 #include "lldb/Expression/IRInterpreter.h"
28 #include "lldb/Host/File.h"
29 #include "lldb/Host/HostInfo.h"
30 #include "lldb/Symbol/SymbolVendor.h"
31 #include "lldb/Target/ExecutionContext.h"
32 #include "lldb/Target/ObjCLanguageRuntime.h"
33 #include "lldb/Target/Process.h"
34 #include "lldb/Target/Target.h"
36 #include "clang/AST/ASTContext.h"
37 #include "clang/AST/ExternalASTSource.h"
38 #include "clang/Basic/FileManager.h"
39 #include "clang/Basic/TargetInfo.h"
40 #include "clang/Basic/Version.h"
41 #include "clang/CodeGen/CodeGenAction.h"
42 #include "clang/CodeGen/ModuleBuilder.h"
43 #include "clang/Frontend/CompilerInstance.h"
44 #include "clang/Frontend/CompilerInvocation.h"
45 #include "clang/Frontend/FrontendActions.h"
46 #include "clang/Frontend/FrontendDiagnostic.h"
47 #include "clang/Frontend/FrontendPluginRegistry.h"
48 #include "clang/Frontend/TextDiagnosticBuffer.h"
49 #include "clang/Frontend/TextDiagnosticPrinter.h"
50 #include "clang/Lex/Preprocessor.h"
51 #include "clang/Parse/ParseAST.h"
52 #include "clang/Rewrite/Frontend/FrontendActions.h"
53 #include "clang/Sema/SemaConsumer.h"
54 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
56 #include "llvm/ADT/StringRef.h"
57 #include "llvm/ExecutionEngine/ExecutionEngine.h"
58 #include "llvm/Support/Debug.h"
59 #include "llvm/Support/FileSystem.h"
60 #include "llvm/Support/TargetSelect.h"
62 #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,
95 //===----------------------------------------------------------------------===//
96 // Implementation of ClangExpressionParser
97 //===----------------------------------------------------------------------===//
99 ClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope,
100 ClangExpression &expr,
101 bool generate_debug_info) :
106 // 1. Create a new compiler instance.
107 m_compiler.reset(new CompilerInstance());
109 // 2. Install the target.
111 lldb::TargetSP target_sp;
113 target_sp = exe_scope->CalculateTarget();
115 // TODO: figure out what to really do when we don't have a valid target.
116 // Sometimes this will be ok to just use the host target triple (when we
117 // evaluate say "2+3", but other expressions like breakpoint conditions
118 // and other things that _are_ target specific really shouldn't just be
119 // using the host triple. This needs to be fixed in a better way.
120 if (target_sp && target_sp->GetArchitecture().IsValid())
122 std::string triple = target_sp->GetArchitecture().GetTriple().str();
123 m_compiler->getTargetOpts().Triple = triple;
127 m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
130 if (target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86 ||
131 target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
133 m_compiler->getTargetOpts().Features.push_back("+sse");
134 m_compiler->getTargetOpts().Features.push_back("+sse2");
137 // Any arm32 iOS environment, but not on arm64
138 if (m_compiler->getTargetOpts().Triple.find("arm64") == std::string::npos &&
139 m_compiler->getTargetOpts().Triple.find("arm") != std::string::npos &&
140 m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos)
142 m_compiler->getTargetOpts().ABI = "apcs-gnu";
145 m_compiler->createDiagnostics();
147 // Create the target instance.
148 m_compiler->setTarget(TargetInfo::CreateTargetInfo(
149 m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts));
151 assert (m_compiler->hasTarget());
155 lldb::LanguageType language = expr.Language();
159 case lldb::eLanguageTypeC:
161 case lldb::eLanguageTypeObjC:
162 m_compiler->getLangOpts().ObjC1 = true;
163 m_compiler->getLangOpts().ObjC2 = true;
165 case lldb::eLanguageTypeC_plus_plus:
166 m_compiler->getLangOpts().CPlusPlus = true;
167 m_compiler->getLangOpts().CPlusPlus11 = true;
169 case lldb::eLanguageTypeObjC_plus_plus:
171 m_compiler->getLangOpts().ObjC1 = true;
172 m_compiler->getLangOpts().ObjC2 = true;
173 m_compiler->getLangOpts().CPlusPlus = true;
174 m_compiler->getLangOpts().CPlusPlus11 = true;
178 m_compiler->getLangOpts().Bool = true;
179 m_compiler->getLangOpts().WChar = true;
180 m_compiler->getLangOpts().Blocks = true;
181 m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients
182 if (expr.DesiredResultType() == ClangExpression::eResultTypeId)
183 m_compiler->getLangOpts().DebuggerCastResultToId = true;
185 // Spell checking is a nice feature, but it ends up completing a
186 // lot of types that we didn't strictly speaking need to complete.
187 // As a result, we spend a long time parsing and importing debug
189 m_compiler->getLangOpts().SpellChecking = false;
191 lldb::ProcessSP process_sp;
193 process_sp = exe_scope->CalculateProcess();
195 if (process_sp && m_compiler->getLangOpts().ObjC1)
197 if (process_sp->GetObjCLanguageRuntime())
199 if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == eAppleObjC_V2)
200 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7));
202 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7));
204 if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
205 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
209 m_compiler->getLangOpts().ThreadsafeStatics = false;
210 m_compiler->getLangOpts().AccessControl = false; // Debuggers get universal access
211 m_compiler->getLangOpts().DollarIdents = true; // $ indicates a persistent variable name
213 // Set CodeGen options
214 m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
215 m_compiler->getCodeGenOpts().InstrumentFunctions = false;
216 m_compiler->getCodeGenOpts().DisableFPElim = true;
217 m_compiler->getCodeGenOpts().OmitLeafFramePointer = false;
218 if (generate_debug_info)
219 m_compiler->getCodeGenOpts().setDebugInfo(CodeGenOptions::FullDebugInfo);
221 m_compiler->getCodeGenOpts().setDebugInfo(CodeGenOptions::NoDebugInfo);
223 // Disable some warnings.
224 m_compiler->getDiagnostics().setSeverityForGroup(clang::diag::Flavor::WarningOrError,
225 "unused-value", clang::diag::Severity::Ignored, SourceLocation());
226 m_compiler->getDiagnostics().setSeverityForGroup(clang::diag::Flavor::WarningOrError,
227 "odr", clang::diag::Severity::Ignored, SourceLocation());
229 // Inform the target of the language options
231 // FIXME: We shouldn't need to do this, the target should be immutable once
232 // created. This complexity should be lifted elsewhere.
233 m_compiler->getTarget().adjust(m_compiler->getLangOpts());
235 // 4. Set up the diagnostic buffer for reporting errors
237 m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer);
239 // 5. Set up the source management objects inside the compiler
241 clang::FileSystemOptions file_system_options;
242 m_file_manager.reset(new clang::FileManager(file_system_options));
244 if (!m_compiler->hasSourceManager())
245 m_compiler->createSourceManager(*m_file_manager.get());
247 m_compiler->createFileManager();
248 m_compiler->createPreprocessor(TU_Complete);
250 // 6. Most of this we get from the CompilerInstance, but we
251 // also want to give the context an ExternalASTSource.
252 m_selector_table.reset(new SelectorTable());
253 m_builtin_context.reset(new Builtin::Context());
255 std::unique_ptr<clang::ASTContext> ast_context(new ASTContext(m_compiler->getLangOpts(),
256 m_compiler->getSourceManager(),
257 m_compiler->getPreprocessor().getIdentifierTable(),
258 *m_selector_table.get(),
259 *m_builtin_context.get()));
260 ast_context->InitBuiltinTypes(m_compiler->getTarget());
262 ClangExpressionDeclMap *decl_map = m_expr.DeclMap();
266 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy());
267 decl_map->InstallASTContext(ast_context.get());
268 ast_context->setExternalSource(ast_source);
271 m_compiler->setASTContext(ast_context.release());
273 std::string module_name("$__lldb_module");
275 m_llvm_context.reset(new LLVMContext());
276 m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(),
278 m_compiler->getCodeGenOpts(),
279 m_compiler->getTargetOpts(),
283 ClangExpressionParser::~ClangExpressionParser()
288 ClangExpressionParser::Parse (Stream &stream)
290 TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient());
292 diag_buf->FlushDiagnostics (m_compiler->getDiagnostics());
294 const char *expr_text = m_expr.Text();
296 clang::SourceManager &SourceMgr = m_compiler->getSourceManager();
297 bool created_main_file = false;
298 if (m_compiler->getCodeGenOpts().getDebugInfo() == CodeGenOptions::FullDebugInfo)
300 std::string temp_source_path;
302 FileSpec tmpdir_file_spec;
303 if (HostInfo::GetLLDBPath(lldb::ePathTypeLLDBTempSystemDir, tmpdir_file_spec))
305 tmpdir_file_spec.AppendPathComponent("expr.XXXXXX");
306 temp_source_path = std::move(tmpdir_file_spec.GetPath());
310 temp_source_path = "/tmp/expr.XXXXXX";
313 if (mktemp(&temp_source_path[0]))
315 lldb_private::File file (temp_source_path.c_str(),
316 File::eOpenOptionWrite | File::eOpenOptionCanCreateNewOnly,
317 lldb::eFilePermissionsFileDefault);
318 const size_t expr_text_len = strlen(expr_text);
319 size_t bytes_written = expr_text_len;
320 if (file.Write(expr_text, bytes_written).Success())
322 if (bytes_written == expr_text_len)
325 SourceMgr.setMainFileID(SourceMgr.createFileID(
326 m_file_manager->getFile(temp_source_path),
327 SourceLocation(), SrcMgr::C_User));
328 created_main_file = true;
334 if (!created_main_file)
336 MemoryBuffer *memory_buffer = MemoryBuffer::getMemBufferCopy(expr_text, __FUNCTION__);
337 SourceMgr.setMainFileID(SourceMgr.createFileID(memory_buffer));
340 diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor());
342 ASTConsumer *ast_transformer = m_expr.ASTTransformer(m_code_generator.get());
345 ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext());
347 ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext());
349 diag_buf->EndSourceFile();
351 TextDiagnosticBuffer::const_iterator diag_iterator;
355 for (diag_iterator = diag_buf->warn_begin();
356 diag_iterator != diag_buf->warn_end();
358 stream.Printf("warning: %s\n", (*diag_iterator).second.c_str());
362 for (diag_iterator = diag_buf->err_begin();
363 diag_iterator != diag_buf->err_end();
367 stream.Printf("error: %s\n", (*diag_iterator).second.c_str());
370 for (diag_iterator = diag_buf->note_begin();
371 diag_iterator != diag_buf->note_end();
373 stream.Printf("note: %s\n", (*diag_iterator).second.c_str());
377 if (m_expr.DeclMap() && !m_expr.DeclMap()->ResolveUnknownTypes())
379 stream.Printf("error: Couldn't infer the type of a variable\n");
387 static bool FindFunctionInModule (ConstString &mangled_name,
388 llvm::Module *module,
389 const char *orig_name)
391 for (llvm::Module::iterator fi = module->getFunctionList().begin(), fe = module->getFunctionList().end();
395 if (fi->getName().str().find(orig_name) != std::string::npos)
397 mangled_name.SetCString(fi->getName().str().c_str());
406 ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_addr,
407 lldb::addr_t &func_end,
408 std::shared_ptr<IRExecutionUnit> &execution_unit_sp,
409 ExecutionContext &exe_ctx,
411 ExecutionPolicy execution_policy)
413 func_addr = LLDB_INVALID_ADDRESS;
414 func_end = LLDB_INVALID_ADDRESS;
415 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
419 std::unique_ptr<llvm::Module> llvm_module_ap (m_code_generator->ReleaseModule());
421 if (!llvm_module_ap.get())
423 err.SetErrorToGenericError();
424 err.SetErrorString("IR doesn't contain a module");
428 // Find the actual name of the function (it's often mangled somehow)
430 ConstString function_name;
432 if (!FindFunctionInModule(function_name, llvm_module_ap.get(), m_expr.FunctionName()))
434 err.SetErrorToGenericError();
435 err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName());
441 log->Printf("Found function %s for %s", function_name.AsCString(), m_expr.FunctionName());
444 execution_unit_sp.reset(new IRExecutionUnit (m_llvm_context, // handed off here
445 llvm_module_ap, // handed off here
447 exe_ctx.GetTargetSP(),
448 m_compiler->getTargetOpts().Features));
450 ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); // result can be NULL
454 Stream *error_stream = NULL;
455 Target *target = exe_ctx.GetTargetPtr();
457 error_stream = target->GetDebugger().GetErrorFile().get();
459 IRForTarget ir_for_target(decl_map,
460 m_expr.NeedsVariableResolution(),
463 function_name.AsCString());
465 bool ir_can_run = ir_for_target.runOnModule(*execution_unit_sp->GetModule());
467 Error interpret_error;
469 can_interpret = IRInterpreter::CanInterpret(*execution_unit_sp->GetModule(), *execution_unit_sp->GetFunction(), interpret_error);
471 Process *process = exe_ctx.GetProcessPtr();
475 err.SetErrorString("The expression could not be prepared to run in the target");
479 if (!can_interpret && execution_policy == eExecutionPolicyNever)
481 err.SetErrorStringWithFormat("Can't run the expression locally: %s", interpret_error.AsCString());
485 if (!process && execution_policy == eExecutionPolicyAlways)
487 err.SetErrorString("Expression needed to run in the target, but the target can't be run");
491 if (execution_policy == eExecutionPolicyAlways || !can_interpret)
493 if (m_expr.NeedsValidation() && process)
495 if (!process->GetDynamicCheckers())
497 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
499 StreamString install_errors;
501 if (!dynamic_checkers->Install(install_errors, exe_ctx))
503 if (install_errors.GetString().empty())
504 err.SetErrorString ("couldn't install checkers, unknown error");
506 err.SetErrorString (install_errors.GetString().c_str());
511 process->SetDynamicCheckers(dynamic_checkers);
514 log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
517 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString());
519 if (!ir_dynamic_checks.runOnModule(*execution_unit_sp->GetModule()))
521 err.SetErrorToGenericError();
522 err.SetErrorString("Couldn't add dynamic checks to the expression");
527 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
532 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
539 ClangExpressionParser::GetGenerateDebugInfo () const
542 return m_compiler->getCodeGenOpts().getDebugInfo() == CodeGenOptions::FullDebugInfo;