blob: f32ca3ae216ce837728980bb92e78f6ae9b828fd [file] [log] [blame]
Sean Callanan1a8d4092010-08-27 01:01:44 +00001//===-- 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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Sean Callanan1a8d4092010-08-27 01:01:44 +000012#include "lldb/Expression/ClangExpressionParser.h"
13
14#include "lldb/Core/ArchSpec.h"
15#include "lldb/Core/DataBufferHeap.h"
Sean Callanan3989fb92011-01-27 01:07:04 +000016#include "lldb/Core/Debugger.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000017#include "lldb/Core/Disassembler.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000018#include "lldb/Core/Module.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000019#include "lldb/Core/Stream.h"
Greg Clayton44d93782014-01-27 23:43:24 +000020#include "lldb/Core/StreamFile.h"
Sean Callanan6961e872010-09-01 00:58:00 +000021#include "lldb/Core/StreamString.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000022#include "lldb/Expression/ClangASTSource.h"
23#include "lldb/Expression/ClangExpression.h"
Sean Callanan77502262011-05-12 23:54:16 +000024#include "lldb/Expression/ClangExpressionDeclMap.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000025#include "lldb/Expression/IRExecutionUnit.h"
Sean Callanan6961e872010-09-01 00:58:00 +000026#include "lldb/Expression/IRDynamicChecks.h"
Sean Callanan1582ee62013-04-18 22:06:33 +000027#include "lldb/Expression/IRInterpreter.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000028#include "lldb/Host/File.h"
Zachary Turner42ff0ad2014-08-21 17:29:12 +000029#include "lldb/Host/HostInfo.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000030#include "lldb/Symbol/SymbolVendor.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000031#include "lldb/Target/ExecutionContext.h"
Sean Callananc3a16002011-01-17 23:42:46 +000032#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000033#include "lldb/Target/Process.h"
34#include "lldb/Target/Target.h"
35
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"
Sean Callanan1a8d4092010-08-27 01:01:44 +000041#include "clang/CodeGen/CodeGenAction.h"
42#include "clang/CodeGen/ModuleBuilder.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000043#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"
Sean Callanan1a8d4092010-08-27 01:01:44 +000050#include "clang/Lex/Preprocessor.h"
Sean Callanane2ef6e32010-09-23 03:01:22 +000051#include "clang/Parse/ParseAST.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000052#include "clang/Rewrite/Frontend/FrontendActions.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000053#include "clang/Sema/SemaConsumer.h"
Sean Callananfb0b7582011-03-15 00:17:19 +000054#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000055
56#include "llvm/ADT/StringRef.h"
57#include "llvm/ExecutionEngine/ExecutionEngine.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000058#include "llvm/Support/Debug.h"
Rafael Espindola4609ea82013-06-26 14:10:56 +000059#include "llvm/Support/FileSystem.h"
Sean Callanan880e6802011-10-07 23:18:13 +000060#include "llvm/Support/TargetSelect.h"
Greg Clayton70b57652011-05-15 01:25:55 +000061
Ed Mastea09ed032014-03-18 18:55:06 +000062#include "llvm/ExecutionEngine/MCJIT.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000063#include "llvm/IR/LLVMContext.h"
64#include "llvm/IR/Module.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000065#include "llvm/Support/ErrorHandling.h"
66#include "llvm/Support/MemoryBuffer.h"
Greg Clayton38a61402010-12-02 23:20:03 +000067#include "llvm/Support/DynamicLibrary.h"
68#include "llvm/Support/Host.h"
69#include "llvm/Support/Signals.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000070
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) {
Rafael Espindola4609ea82013-06-26 14:10:56 +000080 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
Sean Callanan1a8d4092010-08-27 01:01:44 +000087 // Get foo/lib/clang/<version>/include
Rafael Espindola4609ea82013-06-26 14:10:56 +000088 llvm::sys::path::append(P, "lib", "clang", CLANG_VERSION_STRING,
89 "include");
Sean Callanan1a8d4092010-08-27 01:01:44 +000090 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000091
Sean Callanan1a8d4092010-08-27 01:01:44 +000092 return P.str();
93}
94
Sean Callanan1a8d4092010-08-27 01:01:44 +000095//===----------------------------------------------------------------------===//
96// Implementation of ClangExpressionParser
97//===----------------------------------------------------------------------===//
98
Greg Clayton514487e2011-02-15 21:59:32 +000099ClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope,
Greg Clayton23f8c952014-03-24 23:10:19 +0000100 ClangExpression &expr,
101 bool generate_debug_info) :
Greg Clayton514487e2011-02-15 21:59:32 +0000102 m_expr (expr),
Sean Callanan1a8d4092010-08-27 01:01:44 +0000103 m_compiler (),
Greg Claytone01e07b2013-04-18 18:10:51 +0000104 m_code_generator ()
Sean Callanan1a8d4092010-08-27 01:01:44 +0000105{
Sean Callanan1a8d4092010-08-27 01:01:44 +0000106 // 1. Create a new compiler instance.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000107 m_compiler.reset(new CompilerInstance());
108
Sean Callanan3d654b32012-09-24 22:25:51 +0000109 // 2. Install the target.
110
111 lldb::TargetSP target_sp;
112 if (exe_scope)
113 target_sp = exe_scope->CalculateTarget();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000114
Sean Callanan3d654b32012-09-24 22:25:51 +0000115 // 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())
121 {
122 std::string triple = target_sp->GetArchitecture().GetTriple().str();
Sean Callanan3d654b32012-09-24 22:25:51 +0000123 m_compiler->getTargetOpts().Triple = triple;
124 }
125 else
126 {
127 m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
128 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000129
Sean Callananb45a6f02013-02-21 01:04:23 +0000130 if (target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86 ||
131 target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
132 {
133 m_compiler->getTargetOpts().Features.push_back("+sse");
134 m_compiler->getTargetOpts().Features.push_back("+sse2");
135 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000136
Jason Molendaa3329782014-03-29 18:54:20 +0000137 // Any arm32 iOS environment, but not on arm64
Sean Callanan60400ec2014-06-23 21:00:25 +0000138 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)
Jason Molendaa3329782014-03-29 18:54:20 +0000141 {
Sean Callanan3d654b32012-09-24 22:25:51 +0000142 m_compiler->getTargetOpts().ABI = "apcs-gnu";
Jason Molendaa3329782014-03-29 18:54:20 +0000143 }
144
Sean Callananb1de8dd2013-01-22 02:20:20 +0000145 m_compiler->createDiagnostics();
Jason Molendaa3329782014-03-29 18:54:20 +0000146
Sean Callanan3d654b32012-09-24 22:25:51 +0000147 // Create the target instance.
Alp Toker5f838642014-07-06 05:36:57 +0000148 m_compiler->setTarget(TargetInfo::CreateTargetInfo(
149 m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts));
150
Sean Callanan3d654b32012-09-24 22:25:51 +0000151 assert (m_compiler->hasTarget());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000152
Sean Callanan3d654b32012-09-24 22:25:51 +0000153 // 3. Set options.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000154
Sean Callananc7b65062011-11-07 23:35:40 +0000155 lldb::LanguageType language = expr.Language();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000156
Sean Callananc7b65062011-11-07 23:35:40 +0000157 switch (language)
158 {
159 case lldb::eLanguageTypeC:
160 break;
161 case lldb::eLanguageTypeObjC:
162 m_compiler->getLangOpts().ObjC1 = true;
163 m_compiler->getLangOpts().ObjC2 = true;
164 break;
165 case lldb::eLanguageTypeC_plus_plus:
166 m_compiler->getLangOpts().CPlusPlus = true;
Chandler Carruth38336a12013-01-02 12:55:00 +0000167 m_compiler->getLangOpts().CPlusPlus11 = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000168 break;
169 case lldb::eLanguageTypeObjC_plus_plus:
170 default:
171 m_compiler->getLangOpts().ObjC1 = true;
172 m_compiler->getLangOpts().ObjC2 = true;
173 m_compiler->getLangOpts().CPlusPlus = true;
Chandler Carruth38336a12013-01-02 12:55:00 +0000174 m_compiler->getLangOpts().CPlusPlus11 = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000175 break;
176 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000177
Sean Callananaa0f9cb2012-10-17 22:09:59 +0000178 m_compiler->getLangOpts().Bool = true;
179 m_compiler->getLangOpts().WChar = true;
Sean Callananeeb43842013-04-01 22:12:37 +0000180 m_compiler->getLangOpts().Blocks = true;
Sean Callanan226b70c2012-03-08 02:39:03 +0000181 m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients
182 if (expr.DesiredResultType() == ClangExpression::eResultTypeId)
183 m_compiler->getLangOpts().DebuggerCastResultToId = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000184
Sean Callanan0765c822012-04-17 00:49:48 +0000185 // 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
188 // information.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000189 m_compiler->getLangOpts().SpellChecking = false;
190
Greg Claytond9e416c2012-02-18 05:35:26 +0000191 lldb::ProcessSP process_sp;
Greg Clayton514487e2011-02-15 21:59:32 +0000192 if (exe_scope)
Greg Claytond9e416c2012-02-18 05:35:26 +0000193 process_sp = exe_scope->CalculateProcess();
Greg Clayton514487e2011-02-15 21:59:32 +0000194
Greg Claytond9e416c2012-02-18 05:35:26 +0000195 if (process_sp && m_compiler->getLangOpts().ObjC1)
Sean Callananc3a16002011-01-17 23:42:46 +0000196 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000197 if (process_sp->GetObjCLanguageRuntime())
Sean Callananc3a16002011-01-17 23:42:46 +0000198 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000199 if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == eAppleObjC_V2)
Sean Callanan3d654b32012-09-24 22:25:51 +0000200 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7));
201 else
202 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000203
Sean Callanan226b70c2012-03-08 02:39:03 +0000204 if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
Sean Callanan226b70c2012-03-08 02:39:03 +0000205 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
Sean Callananc3a16002011-01-17 23:42:46 +0000206 }
207 }
Greg Claytonf83f32d2011-01-15 01:32:14 +0000208
Sean Callanan1a8d4092010-08-27 01:01:44 +0000209 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
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000212
Sean Callanan1a8d4092010-08-27 01:01:44 +0000213 // Set CodeGen options
214 m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
215 m_compiler->getCodeGenOpts().InstrumentFunctions = false;
Greg Claytonbc3122e2013-11-04 19:50:49 +0000216 m_compiler->getCodeGenOpts().DisableFPElim = true;
217 m_compiler->getCodeGenOpts().OmitLeafFramePointer = false;
Greg Clayton23f8c952014-03-24 23:10:19 +0000218 if (generate_debug_info)
219 m_compiler->getCodeGenOpts().setDebugInfo(CodeGenOptions::FullDebugInfo);
220 else
221 m_compiler->getCodeGenOpts().setDebugInfo(CodeGenOptions::NoDebugInfo);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000222
Sean Callanan1a8d4092010-08-27 01:01:44 +0000223 // Disable some warnings.
Hafiz Abid Qadeer101e4902014-08-07 12:54:20 +0000224 m_compiler->getDiagnostics().setSeverityForGroup(clang::diag::Flavor::WarningOrError,
Alp Toker10933d12014-06-12 11:14:32 +0000225 "unused-value", clang::diag::Severity::Ignored, SourceLocation());
Hafiz Abid Qadeer101e4902014-08-07 12:54:20 +0000226 m_compiler->getDiagnostics().setSeverityForGroup(clang::diag::Flavor::WarningOrError,
Alp Toker10933d12014-06-12 11:14:32 +0000227 "odr", clang::diag::Severity::Ignored, SourceLocation());
228
Sean Callanan1a8d4092010-08-27 01:01:44 +0000229 // Inform the target of the language options
230 //
231 // FIXME: We shouldn't need to do this, the target should be immutable once
232 // created. This complexity should be lifted elsewhere.
Sylvestre Ledru3cc9d632014-07-06 17:50:36 +0000233 m_compiler->getTarget().adjust(m_compiler->getLangOpts());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000234
Sean Callanan1a8d4092010-08-27 01:01:44 +0000235 // 4. Set up the diagnostic buffer for reporting errors
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000236
Sean Callanane2ef6e32010-09-23 03:01:22 +0000237 m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000238
Sean Callanan1a8d4092010-08-27 01:01:44 +0000239 // 5. Set up the source management objects inside the compiler
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000240
Greg Clayton38a61402010-12-02 23:20:03 +0000241 clang::FileSystemOptions file_system_options;
242 m_file_manager.reset(new clang::FileManager(file_system_options));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000243
Sean Callanan1a8d4092010-08-27 01:01:44 +0000244 if (!m_compiler->hasSourceManager())
Greg Clayton38a61402010-12-02 23:20:03 +0000245 m_compiler->createSourceManager(*m_file_manager.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000246
Sean Callanan1a8d4092010-08-27 01:01:44 +0000247 m_compiler->createFileManager();
Ben Langmuir9f0bac52014-03-07 08:31:36 +0000248 m_compiler->createPreprocessor(TU_Complete);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000249
250 // 6. Most of this we get from the CompilerInstance, but we
Sean Callanan1a8d4092010-08-27 01:01:44 +0000251 // also want to give the context an ExternalASTSource.
Sean Callanan6abfabf2010-11-19 20:20:02 +0000252 m_selector_table.reset(new SelectorTable());
Sean Callanan880e6802011-10-07 23:18:13 +0000253 m_builtin_context.reset(new Builtin::Context());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000254
Greg Clayton7b0992d2013-04-18 22:45:39 +0000255 std::unique_ptr<clang::ASTContext> ast_context(new ASTContext(m_compiler->getLangOpts(),
Greg Claytone01e07b2013-04-18 18:10:51 +0000256 m_compiler->getSourceManager(),
Greg Claytone01e07b2013-04-18 18:10:51 +0000257 m_compiler->getPreprocessor().getIdentifierTable(),
258 *m_selector_table.get(),
Alp Tokercf55e882014-05-03 15:05:45 +0000259 *m_builtin_context.get()));
260 ast_context->InitBuiltinTypes(m_compiler->getTarget());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000261
Sean Callanan1a8d4092010-08-27 01:01:44 +0000262 ClangExpressionDeclMap *decl_map = m_expr.DeclMap();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000263
Sean Callanan1a8d4092010-08-27 01:01:44 +0000264 if (decl_map)
265 {
Sylvestre Ledru7ba631f2014-02-27 10:46:57 +0000266 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy());
Sean Callananeddeb3b2011-10-28 23:38:38 +0000267 decl_map->InstallASTContext(ast_context.get());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000268 ast_context->setExternalSource(ast_source);
269 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000270
Sean Callanan1a8d4092010-08-27 01:01:44 +0000271 m_compiler->setASTContext(ast_context.release());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000272
Greg Clayton7b462cc2010-10-15 22:48:33 +0000273 std::string module_name("$__lldb_module");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000274
Sean Callananfb0b7582011-03-15 00:17:19 +0000275 m_llvm_context.reset(new LLVMContext());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000276 m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(),
277 module_name,
278 m_compiler->getCodeGenOpts(),
Filipe Cabecinhas55fcb102013-02-14 22:02:57 +0000279 m_compiler->getTargetOpts(),
Sean Callananfb0b7582011-03-15 00:17:19 +0000280 *m_llvm_context));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000281}
282
283ClangExpressionParser::~ClangExpressionParser()
284{
285}
286
287unsigned
288ClangExpressionParser::Parse (Stream &stream)
289{
Sean Callanane2ef6e32010-09-23 03:01:22 +0000290 TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000291
Sean Callanane2ef6e32010-09-23 03:01:22 +0000292 diag_buf->FlushDiagnostics (m_compiler->getDiagnostics());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000293
Greg Clayton23f8c952014-03-24 23:10:19 +0000294 const char *expr_text = m_expr.Text();
Rafael Espindola9cb97b62014-05-21 15:08:27 +0000295
296 clang::SourceManager &SourceMgr = m_compiler->getSourceManager();
Greg Clayton23f8c952014-03-24 23:10:19 +0000297 bool created_main_file = false;
298 if (m_compiler->getCodeGenOpts().getDebugInfo() == CodeGenOptions::FullDebugInfo)
299 {
300 std::string temp_source_path;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000301
Greg Clayton23f8c952014-03-24 23:10:19 +0000302 FileSpec tmpdir_file_spec;
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000303 if (HostInfo::GetLLDBPath(lldb::ePathTypeLLDBTempSystemDir, tmpdir_file_spec))
Greg Clayton23f8c952014-03-24 23:10:19 +0000304 {
Zachary Turnerc25146b2014-08-21 23:56:55 +0000305 tmpdir_file_spec.AppendPathComponent("expr.XXXXXX");
Greg Clayton23f8c952014-03-24 23:10:19 +0000306 temp_source_path = std::move(tmpdir_file_spec.GetPath());
307 }
308 else
309 {
310 temp_source_path = "/tmp/expr.XXXXXX";
311 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000312
Greg Clayton23f8c952014-03-24 23:10:19 +0000313 if (mktemp(&temp_source_path[0]))
314 {
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())
321 {
322 if (bytes_written == expr_text_len)
323 {
324 file.Close();
Rafael Espindola9cb97b62014-05-21 15:08:27 +0000325 SourceMgr.setMainFileID(SourceMgr.createFileID(
326 m_file_manager->getFile(temp_source_path),
327 SourceLocation(), SrcMgr::C_User));
Greg Clayton23f8c952014-03-24 23:10:19 +0000328 created_main_file = true;
329 }
330 }
331 }
332 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000333
Greg Clayton23f8c952014-03-24 23:10:19 +0000334 if (!created_main_file)
335 {
Rafael Espindola2443e702014-08-27 20:09:08 +0000336 std::unique_ptr<MemoryBuffer> memory_buffer = MemoryBuffer::getMemBufferCopy(expr_text, __FUNCTION__);
Sean Callanan302be672014-08-30 02:24:56 +0000337 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(memory_buffer)));
Greg Clayton23f8c952014-03-24 23:10:19 +0000338 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000339
Sean Callanane2ef6e32010-09-23 03:01:22 +0000340 diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000341
Sean Callanan1a8d4092010-08-27 01:01:44 +0000342 ASTConsumer *ast_transformer = m_expr.ASTTransformer(m_code_generator.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000343
Sean Callanan1a8d4092010-08-27 01:01:44 +0000344 if (ast_transformer)
345 ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000346 else
347 ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext());
348
Sean Callanane2ef6e32010-09-23 03:01:22 +0000349 diag_buf->EndSourceFile();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000350
Sean Callanan1a8d4092010-08-27 01:01:44 +0000351 TextDiagnosticBuffer::const_iterator diag_iterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000352
Sean Callanan1a8d4092010-08-27 01:01:44 +0000353 int num_errors = 0;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000354
Sean Callanane2ef6e32010-09-23 03:01:22 +0000355 for (diag_iterator = diag_buf->warn_begin();
356 diag_iterator != diag_buf->warn_end();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000357 ++diag_iterator)
358 stream.Printf("warning: %s\n", (*diag_iterator).second.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000359
Sean Callanan1a8d4092010-08-27 01:01:44 +0000360 num_errors = 0;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000361
Sean Callanane2ef6e32010-09-23 03:01:22 +0000362 for (diag_iterator = diag_buf->err_begin();
363 diag_iterator != diag_buf->err_end();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000364 ++diag_iterator)
365 {
366 num_errors++;
367 stream.Printf("error: %s\n", (*diag_iterator).second.c_str());
368 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000369
Sean Callanan57bbc6e2010-11-01 20:28:09 +0000370 for (diag_iterator = diag_buf->note_begin();
371 diag_iterator != diag_buf->note_end();
372 ++diag_iterator)
373 stream.Printf("note: %s\n", (*diag_iterator).second.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000374
Sean Callanan77502262011-05-12 23:54:16 +0000375 if (!num_errors)
376 {
377 if (m_expr.DeclMap() && !m_expr.DeclMap()->ResolveUnknownTypes())
378 {
379 stream.Printf("error: Couldn't infer the type of a variable\n");
380 num_errors++;
381 }
382 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000383
Sean Callanan1a8d4092010-08-27 01:01:44 +0000384 return num_errors;
385}
386
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000387static bool FindFunctionInModule (ConstString &mangled_name,
Sean Callananfc55f5d2010-09-21 00:44:12 +0000388 llvm::Module *module,
389 const char *orig_name)
390{
391 for (llvm::Module::iterator fi = module->getFunctionList().begin(), fe = module->getFunctionList().end();
392 fi != fe;
393 ++fi)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000394 {
Sean Callananfc55f5d2010-09-21 00:44:12 +0000395 if (fi->getName().str().find(orig_name) != std::string::npos)
396 {
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000397 mangled_name.SetCString(fi->getName().str().c_str());
Sean Callananfc55f5d2010-09-21 00:44:12 +0000398 return true;
399 }
400 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000401
Sean Callananfc55f5d2010-09-21 00:44:12 +0000402 return false;
403}
404
Sean Callanan1a8d4092010-08-27 01:01:44 +0000405Error
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000406ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_addr,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000407 lldb::addr_t &func_end,
Greg Clayton23f8c952014-03-24 23:10:19 +0000408 std::shared_ptr<IRExecutionUnit> &execution_unit_sp,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000409 ExecutionContext &exe_ctx,
Sean Callanan1582ee62013-04-18 22:06:33 +0000410 bool &can_interpret,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000411 ExecutionPolicy execution_policy)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000412{
Greg Clayton22a939a2011-01-19 23:00:49 +0000413 func_addr = LLDB_INVALID_ADDRESS;
414 func_end = LLDB_INVALID_ADDRESS;
Greg Clayton5160ce52013-03-27 23:08:40 +0000415 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfc55f5d2010-09-21 00:44:12 +0000416
Sean Callanan1a8d4092010-08-27 01:01:44 +0000417 Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000418
Greg Clayton23f8c952014-03-24 23:10:19 +0000419 std::unique_ptr<llvm::Module> llvm_module_ap (m_code_generator->ReleaseModule());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000420
Greg Clayton23f8c952014-03-24 23:10:19 +0000421 if (!llvm_module_ap.get())
Sean Callanan1a8d4092010-08-27 01:01:44 +0000422 {
423 err.SetErrorToGenericError();
424 err.SetErrorString("IR doesn't contain a module");
425 return err;
426 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000427
Sean Callananfc55f5d2010-09-21 00:44:12 +0000428 // Find the actual name of the function (it's often mangled somehow)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000429
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000430 ConstString function_name;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000431
Greg Clayton23f8c952014-03-24 23:10:19 +0000432 if (!FindFunctionInModule(function_name, llvm_module_ap.get(), m_expr.FunctionName()))
Sean Callananfc55f5d2010-09-21 00:44:12 +0000433 {
434 err.SetErrorToGenericError();
435 err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName());
436 return err;
437 }
438 else
439 {
Enrico Granata20edcdb2011-07-19 18:03:25 +0000440 if (log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000441 log->Printf("Found function %s for %s", function_name.AsCString(), m_expr.FunctionName());
Sean Callananfc55f5d2010-09-21 00:44:12 +0000442 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000443
Greg Clayton23f8c952014-03-24 23:10:19 +0000444 execution_unit_sp.reset(new IRExecutionUnit (m_llvm_context, // handed off here
445 llvm_module_ap, // handed off here
446 function_name,
447 exe_ctx.GetTargetSP(),
448 m_compiler->getTargetOpts().Features));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000449
Sean Callanan1a8d4092010-08-27 01:01:44 +0000450 ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); // result can be NULL
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000451
Sean Callanan1a8d4092010-08-27 01:01:44 +0000452 if (decl_map)
453 {
Sean Callanan3989fb92011-01-27 01:07:04 +0000454 Stream *error_stream = NULL;
Greg Claytonc14ee322011-09-22 04:58:26 +0000455 Target *target = exe_ctx.GetTargetPtr();
456 if (target)
Greg Clayton44d93782014-01-27 23:43:24 +0000457 error_stream = target->GetDebugger().GetErrorFile().get();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000458
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000459 IRForTarget ir_for_target(decl_map,
Sean Callanan9e6ed532010-09-13 21:34:21 +0000460 m_expr.NeedsVariableResolution(),
Greg Clayton23f8c952014-03-24 23:10:19 +0000461 *execution_unit_sp,
Sean Callanan3989fb92011-01-27 01:07:04 +0000462 error_stream,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000463 function_name.AsCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000464
Greg Clayton23f8c952014-03-24 23:10:19 +0000465 bool ir_can_run = ir_for_target.runOnModule(*execution_unit_sp->GetModule());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000466
Sean Callanan1582ee62013-04-18 22:06:33 +0000467 Error interpret_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000468
Greg Clayton23f8c952014-03-24 23:10:19 +0000469 can_interpret = IRInterpreter::CanInterpret(*execution_unit_sp->GetModule(), *execution_unit_sp->GetFunction(), interpret_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000470
Greg Claytonc14ee322011-09-22 04:58:26 +0000471 Process *process = exe_ctx.GetProcessPtr();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000472
Sean Callanan1582ee62013-04-18 22:06:33 +0000473 if (!ir_can_run)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000474 {
Sean Callanan3d654b32012-09-24 22:25:51 +0000475 err.SetErrorString("The expression could not be prepared to run in the target");
Sean Callanan3d654b32012-09-24 22:25:51 +0000476 return err;
477 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000478
Sean Callanan1582ee62013-04-18 22:06:33 +0000479 if (!can_interpret && execution_policy == eExecutionPolicyNever)
Sean Callanan6961e872010-09-01 00:58:00 +0000480 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000481 err.SetErrorStringWithFormat("Can't run the expression locally: %s", interpret_error.AsCString());
482 return err;
483 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000484
Sean Callanan1582ee62013-04-18 22:06:33 +0000485 if (!process && execution_policy == eExecutionPolicyAlways)
486 {
487 err.SetErrorString("Expression needed to run in the target, but the target can't be run");
488 return err;
489 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000490
Sean Callanan1582ee62013-04-18 22:06:33 +0000491 if (execution_policy == eExecutionPolicyAlways || !can_interpret)
492 {
Sean Callanane8cde682013-05-18 00:38:20 +0000493 if (m_expr.NeedsValidation() && process)
494 {
495 if (!process->GetDynamicCheckers())
Sean Callanan90539452011-09-20 23:01:51 +0000496 {
Sean Callanane8cde682013-05-18 00:38:20 +0000497 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000498
Sean Callanane8cde682013-05-18 00:38:20 +0000499 StreamString install_errors;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000500
Sean Callanane8cde682013-05-18 00:38:20 +0000501 if (!dynamic_checkers->Install(install_errors, exe_ctx))
502 {
503 if (install_errors.GetString().empty())
504 err.SetErrorString ("couldn't install checkers, unknown error");
505 else
506 err.SetErrorString (install_errors.GetString().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000507
Sean Callanane8cde682013-05-18 00:38:20 +0000508 return err;
509 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000510
Sean Callanane8cde682013-05-18 00:38:20 +0000511 process->SetDynamicCheckers(dynamic_checkers);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000512
Sean Callanane8cde682013-05-18 00:38:20 +0000513 if (log)
514 log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
Sean Callanan90539452011-09-20 23:01:51 +0000515 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000516
Sean Callanan1582ee62013-04-18 22:06:33 +0000517 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000518
Greg Clayton23f8c952014-03-24 23:10:19 +0000519 if (!ir_dynamic_checks.runOnModule(*execution_unit_sp->GetModule()))
Sean Callanan1582ee62013-04-18 22:06:33 +0000520 {
521 err.SetErrorToGenericError();
522 err.SetErrorString("Couldn't add dynamic checks to the expression");
523 return err;
524 }
Sean Callanan90539452011-09-20 23:01:51 +0000525 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000526
Greg Clayton23f8c952014-03-24 23:10:19 +0000527 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
Sean Callanan9e6ed532010-09-13 21:34:21 +0000528 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000529 }
Sean Callanan1582ee62013-04-18 22:06:33 +0000530 else
531 {
Greg Clayton23f8c952014-03-24 23:10:19 +0000532 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
Sean Callanan1582ee62013-04-18 22:06:33 +0000533 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000534
Sean Callanan1a8d4092010-08-27 01:01:44 +0000535 return err;
536}
Greg Clayton23f8c952014-03-24 23:10:19 +0000537
538bool
539ClangExpressionParser::GetGenerateDebugInfo () const
540{
541 if (m_compiler)
542 return m_compiler->getCodeGenOpts().getDebugInfo() == CodeGenOptions::FullDebugInfo;
543 return false;
544}