blob: ccff5b56338d4f396c12b3f18ff13ebf28d6645d [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"
18#include "lldb/Core/Stream.h"
Greg Clayton44d93782014-01-27 23:43:24 +000019#include "lldb/Core/StreamFile.h"
Sean Callanan6961e872010-09-01 00:58:00 +000020#include "lldb/Core/StreamString.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000021#include "lldb/Expression/ClangASTSource.h"
22#include "lldb/Expression/ClangExpression.h"
Sean Callanan77502262011-05-12 23:54:16 +000023#include "lldb/Expression/ClangExpressionDeclMap.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000024#include "lldb/Expression/IRExecutionUnit.h"
Sean Callanan6961e872010-09-01 00:58:00 +000025#include "lldb/Expression/IRDynamicChecks.h"
Sean Callanan1582ee62013-04-18 22:06:33 +000026#include "lldb/Expression/IRInterpreter.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000027#include "lldb/Target/ExecutionContext.h"
Sean Callananc3a16002011-01-17 23:42:46 +000028#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000029#include "lldb/Target/Process.h"
30#include "lldb/Target/Target.h"
31
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"
Sean Callanan1a8d4092010-08-27 01:01:44 +000037#include "clang/CodeGen/CodeGenAction.h"
38#include "clang/CodeGen/ModuleBuilder.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000039#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"
Sean Callanan1a8d4092010-08-27 01:01:44 +000046#include "clang/Lex/Preprocessor.h"
Sean Callanane2ef6e32010-09-23 03:01:22 +000047#include "clang/Parse/ParseAST.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000048#include "clang/Rewrite/Frontend/FrontendActions.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000049#include "clang/Sema/SemaConsumer.h"
Sean Callananfb0b7582011-03-15 00:17:19 +000050#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000051
52#include "llvm/ADT/StringRef.h"
53#include "llvm/ExecutionEngine/ExecutionEngine.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000054#include "llvm/Support/Debug.h"
Rafael Espindola4609ea82013-06-26 14:10:56 +000055#include "llvm/Support/FileSystem.h"
Sean Callanan880e6802011-10-07 23:18:13 +000056#include "llvm/Support/TargetSelect.h"
Greg Clayton70b57652011-05-15 01:25:55 +000057
Greg Clayton70b57652011-05-15 01:25:55 +000058#if defined (USE_STANDARD_JIT)
Sean Callanan1a8d4092010-08-27 01:01:44 +000059#include "llvm/ExecutionEngine/JIT.h"
Greg Clayton70b57652011-05-15 01:25:55 +000060#else
61#include "llvm/ExecutionEngine/MCJIT.h"
62#endif
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 }
91
92 return P.str();
93}
94
95
96//===----------------------------------------------------------------------===//
97// Main driver for Clang
98//===----------------------------------------------------------------------===//
99
100static void LLVMErrorHandler(void *UserData, const std::string &Message) {
Sean Callanan880e6802011-10-07 23:18:13 +0000101 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000102
103 Diags.Report(diag::err_fe_error_backend) << Message;
104
105 // We cannot recover from llvm errors.
Sean Callanan880e6802011-10-07 23:18:13 +0000106 assert(0);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000107}
108
109static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
110 using namespace clang::frontend;
111
112 switch (CI.getFrontendOpts().ProgramAction) {
113 default:
114 llvm_unreachable("Invalid program action!");
115
116 case ASTDump: return new ASTDumpAction();
117 case ASTPrint: return new ASTPrintAction();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000118 case ASTView: return new ASTViewAction();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000119 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();
Sean Callanana5230ce2011-12-01 04:31:46 +0000129 case GeneratePCH: return new GeneratePCHAction();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000130 case GeneratePTH: return new GeneratePTHAction();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000131 case InitOnly: return new InitOnlyAction();
132 case ParseSyntaxOnly: return new SyntaxOnlyAction();
133
134 case PluginAction: {
135 for (FrontendPluginRegistry::iterator it =
136 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
137 it != ie; ++it) {
138 if (it->getName() == CI.getFrontendOpts().ActionName) {
Ahmed Charles8f926ad2014-03-07 04:45:22 +0000139 std::unique_ptr<PluginASTAction> P(it->instantiate());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000140 if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
141 return 0;
Ahmed Charles8f926ad2014-03-07 04:45:22 +0000142 return P.release();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000143 }
144 }
145
146 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
147 << CI.getFrontendOpts().ActionName;
148 return 0;
149 }
150
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();
Sean Callanan2c777c42011-01-18 23:32:05 +0000157 //case RunAnalysis: return new AnalysisAction();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000158 case RunPreprocessorOnly: return new PreprocessOnlyAction();
159 }
160}
161
162static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
163 // Create the underlying action.
164 FrontendAction *Act = CreateFrontendBaseAction(CI);
165 if (!Act)
166 return 0;
167
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())
Sean Callanan5b26f272012-02-04 08:49:35 +0000171 Act = new ASTMergeAction(Act, CI.getFrontendOpts().ASTMergeFiles);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000172
173 return Act;
174}
175
176//===----------------------------------------------------------------------===//
177// Implementation of ClangExpressionParser
178//===----------------------------------------------------------------------===//
179
Greg Clayton514487e2011-02-15 21:59:32 +0000180ClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope,
181 ClangExpression &expr) :
182 m_expr (expr),
Sean Callanan1a8d4092010-08-27 01:01:44 +0000183 m_compiler (),
Greg Claytone01e07b2013-04-18 18:10:51 +0000184 m_code_generator ()
Sean Callanan1a8d4092010-08-27 01:01:44 +0000185{
186 // Initialize targets first, so that --version shows registered targets.
187 static struct InitializeLLVM {
188 InitializeLLVM() {
189 llvm::InitializeAllTargets();
190 llvm::InitializeAllAsmPrinters();
Sean Callanancc427fa2011-07-30 02:42:06 +0000191 llvm::InitializeAllTargetMCs();
Sean Callanana5230ce2011-12-01 04:31:46 +0000192 llvm::InitializeAllDisassemblers();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000193 }
194 } InitializeLLVM;
Sean Callanan3d654b32012-09-24 22:25:51 +0000195
Sean Callanan1a8d4092010-08-27 01:01:44 +0000196 // 1. Create a new compiler instance.
197 m_compiler.reset(new CompilerInstance());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000198
Sean Callanan3d654b32012-09-24 22:25:51 +0000199 // 2. Install the target.
200
201 lldb::TargetSP target_sp;
202 if (exe_scope)
203 target_sp = exe_scope->CalculateTarget();
204
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())
211 {
212 std::string triple = target_sp->GetArchitecture().GetTriple().str();
213
214 int dash_count = 0;
215 for (size_t i = 0; i < triple.size(); ++i)
216 {
217 if (triple[i] == '-')
218 dash_count++;
219 if (dash_count == 3)
220 {
221 triple.resize(i);
222 break;
223 }
224 }
225
226 m_compiler->getTargetOpts().Triple = triple;
227 }
228 else
229 {
230 m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
231 }
232
Sean Callananb45a6f02013-02-21 01:04:23 +0000233 if (target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86 ||
234 target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
235 {
236 m_compiler->getTargetOpts().Features.push_back("+sse");
237 m_compiler->getTargetOpts().Features.push_back("+sse2");
238 }
239
Sean Callanan3d654b32012-09-24 22:25:51 +0000240 if (m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos)
241 m_compiler->getTargetOpts().ABI = "apcs-gnu";
242
Sean Callananb1de8dd2013-01-22 02:20:20 +0000243 m_compiler->createDiagnostics();
Sean Callanan3d654b32012-09-24 22:25:51 +0000244
245 // Create the target instance.
246 m_compiler->setTarget(TargetInfo::CreateTargetInfo(m_compiler->getDiagnostics(),
Greg Clayton38d880a2012-11-16 21:35:22 +0000247 &m_compiler->getTargetOpts()));
Sean Callanan3d654b32012-09-24 22:25:51 +0000248
249 assert (m_compiler->hasTarget());
250
251 // 3. Set options.
Sean Callanan1a8d4092010-08-27 01:01:44 +0000252
Sean Callananc7b65062011-11-07 23:35:40 +0000253 lldb::LanguageType language = expr.Language();
Greg Claytonf83f32d2011-01-15 01:32:14 +0000254
Sean Callananc7b65062011-11-07 23:35:40 +0000255 switch (language)
256 {
257 case lldb::eLanguageTypeC:
258 break;
259 case lldb::eLanguageTypeObjC:
260 m_compiler->getLangOpts().ObjC1 = true;
261 m_compiler->getLangOpts().ObjC2 = true;
262 break;
263 case lldb::eLanguageTypeC_plus_plus:
264 m_compiler->getLangOpts().CPlusPlus = true;
Chandler Carruth38336a12013-01-02 12:55:00 +0000265 m_compiler->getLangOpts().CPlusPlus11 = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000266 break;
267 case lldb::eLanguageTypeObjC_plus_plus:
268 default:
269 m_compiler->getLangOpts().ObjC1 = true;
270 m_compiler->getLangOpts().ObjC2 = true;
271 m_compiler->getLangOpts().CPlusPlus = true;
Chandler Carruth38336a12013-01-02 12:55:00 +0000272 m_compiler->getLangOpts().CPlusPlus11 = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000273 break;
274 }
Sean Callananc3a16002011-01-17 23:42:46 +0000275
Sean Callananaa0f9cb2012-10-17 22:09:59 +0000276 m_compiler->getLangOpts().Bool = true;
277 m_compiler->getLangOpts().WChar = true;
Sean Callananeeb43842013-04-01 22:12:37 +0000278 m_compiler->getLangOpts().Blocks = true;
Sean Callanan226b70c2012-03-08 02:39:03 +0000279 m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients
280 if (expr.DesiredResultType() == ClangExpression::eResultTypeId)
281 m_compiler->getLangOpts().DebuggerCastResultToId = true;
282
Sean Callanan0765c822012-04-17 00:49:48 +0000283 // 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
286 // information.
287 m_compiler->getLangOpts().SpellChecking = false;
288
Greg Claytond9e416c2012-02-18 05:35:26 +0000289 lldb::ProcessSP process_sp;
Greg Clayton514487e2011-02-15 21:59:32 +0000290 if (exe_scope)
Greg Claytond9e416c2012-02-18 05:35:26 +0000291 process_sp = exe_scope->CalculateProcess();
Greg Clayton514487e2011-02-15 21:59:32 +0000292
Greg Claytond9e416c2012-02-18 05:35:26 +0000293 if (process_sp && m_compiler->getLangOpts().ObjC1)
Sean Callananc3a16002011-01-17 23:42:46 +0000294 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000295 if (process_sp->GetObjCLanguageRuntime())
Sean Callananc3a16002011-01-17 23:42:46 +0000296 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000297 if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == eAppleObjC_V2)
Sean Callanan3d654b32012-09-24 22:25:51 +0000298 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7));
299 else
300 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7));
Sean Callanan226b70c2012-03-08 02:39:03 +0000301
302 if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
Sean Callanan226b70c2012-03-08 02:39:03 +0000303 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
Sean Callananc3a16002011-01-17 23:42:46 +0000304 }
305 }
Greg Claytonf83f32d2011-01-15 01:32:14 +0000306
Sean Callanan1a8d4092010-08-27 01:01:44 +0000307 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
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000310
Sean Callanan1a8d4092010-08-27 01:01:44 +0000311 // Set CodeGen options
312 m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
313 m_compiler->getCodeGenOpts().InstrumentFunctions = false;
Greg Claytonbc3122e2013-11-04 19:50:49 +0000314 m_compiler->getCodeGenOpts().DisableFPElim = true;
315 m_compiler->getCodeGenOpts().OmitLeafFramePointer = false;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000316
317 // Disable some warnings.
Sean Callanan42f26b42013-03-30 01:26:06 +0000318 m_compiler->getDiagnostics().setDiagnosticGroupMapping("unused-value", clang::diag::MAP_IGNORE, SourceLocation());
319 m_compiler->getDiagnostics().setDiagnosticGroupMapping("odr", clang::diag::MAP_IGNORE, SourceLocation());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000320
Sean Callanan1a8d4092010-08-27 01:01:44 +0000321 // Inform the target of the language options
322 //
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());
326
327 // 4. Set up the diagnostic buffer for reporting errors
328
Sean Callanane2ef6e32010-09-23 03:01:22 +0000329 m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000330
331 // 5. Set up the source management objects inside the compiler
332
Greg Clayton38a61402010-12-02 23:20:03 +0000333 clang::FileSystemOptions file_system_options;
334 m_file_manager.reset(new clang::FileManager(file_system_options));
Sean Callanan79439e82010-11-18 02:56:27 +0000335
Sean Callanan1a8d4092010-08-27 01:01:44 +0000336 if (!m_compiler->hasSourceManager())
Greg Clayton38a61402010-12-02 23:20:03 +0000337 m_compiler->createSourceManager(*m_file_manager.get());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000338
339 m_compiler->createFileManager();
Ben Langmuir9f0bac52014-03-07 08:31:36 +0000340 m_compiler->createPreprocessor(TU_Complete);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000341
342 // 6. Most of this we get from the CompilerInstance, but we
343 // also want to give the context an ExternalASTSource.
Sean Callanan6abfabf2010-11-19 20:20:02 +0000344 m_selector_table.reset(new SelectorTable());
Sean Callanan880e6802011-10-07 23:18:13 +0000345 m_builtin_context.reset(new Builtin::Context());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000346
Greg Clayton7b0992d2013-04-18 22:45:39 +0000347 std::unique_ptr<clang::ASTContext> ast_context(new ASTContext(m_compiler->getLangOpts(),
Greg Claytone01e07b2013-04-18 18:10:51 +0000348 m_compiler->getSourceManager(),
349 &m_compiler->getTarget(),
350 m_compiler->getPreprocessor().getIdentifierTable(),
351 *m_selector_table.get(),
352 *m_builtin_context.get(),
353 0));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000354
355 ClangExpressionDeclMap *decl_map = m_expr.DeclMap();
356
357 if (decl_map)
358 {
Sylvestre Ledru7ba631f2014-02-27 10:46:57 +0000359 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy());
Sean Callananeddeb3b2011-10-28 23:38:38 +0000360 decl_map->InstallASTContext(ast_context.get());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000361 ast_context->setExternalSource(ast_source);
362 }
363
364 m_compiler->setASTContext(ast_context.release());
365
Greg Clayton7b462cc2010-10-15 22:48:33 +0000366 std::string module_name("$__lldb_module");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000367
Sean Callananfb0b7582011-03-15 00:17:19 +0000368 m_llvm_context.reset(new LLVMContext());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000369 m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(),
370 module_name,
371 m_compiler->getCodeGenOpts(),
Filipe Cabecinhas55fcb102013-02-14 22:02:57 +0000372 m_compiler->getTargetOpts(),
Sean Callananfb0b7582011-03-15 00:17:19 +0000373 *m_llvm_context));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000374}
375
376ClangExpressionParser::~ClangExpressionParser()
377{
378}
379
380unsigned
381ClangExpressionParser::Parse (Stream &stream)
382{
Sean Callanane2ef6e32010-09-23 03:01:22 +0000383 TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient());
384
385 diag_buf->FlushDiagnostics (m_compiler->getDiagnostics());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000386
387 MemoryBuffer *memory_buffer = MemoryBuffer::getMemBufferCopy(m_expr.Text(), __FUNCTION__);
Sean Callanand5f33a82012-03-01 02:03:47 +0000388 m_compiler->getSourceManager().createMainFileIDForMemBuffer (memory_buffer);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000389
Sean Callanane2ef6e32010-09-23 03:01:22 +0000390 diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000391
392 ASTConsumer *ast_transformer = m_expr.ASTTransformer(m_code_generator.get());
393
394 if (ast_transformer)
395 ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext());
396 else
397 ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext());
398
Sean Callanane2ef6e32010-09-23 03:01:22 +0000399 diag_buf->EndSourceFile();
Sean Callanan77502262011-05-12 23:54:16 +0000400
Sean Callanan1a8d4092010-08-27 01:01:44 +0000401 TextDiagnosticBuffer::const_iterator diag_iterator;
402
403 int num_errors = 0;
Sean Callanan57bbc6e2010-11-01 20:28:09 +0000404
Sean Callanane2ef6e32010-09-23 03:01:22 +0000405 for (diag_iterator = diag_buf->warn_begin();
406 diag_iterator != diag_buf->warn_end();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000407 ++diag_iterator)
408 stream.Printf("warning: %s\n", (*diag_iterator).second.c_str());
409
410 num_errors = 0;
411
Sean Callanane2ef6e32010-09-23 03:01:22 +0000412 for (diag_iterator = diag_buf->err_begin();
413 diag_iterator != diag_buf->err_end();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000414 ++diag_iterator)
415 {
416 num_errors++;
417 stream.Printf("error: %s\n", (*diag_iterator).second.c_str());
418 }
419
Sean Callanan57bbc6e2010-11-01 20:28:09 +0000420 for (diag_iterator = diag_buf->note_begin();
421 diag_iterator != diag_buf->note_end();
422 ++diag_iterator)
423 stream.Printf("note: %s\n", (*diag_iterator).second.c_str());
424
Sean Callanan77502262011-05-12 23:54:16 +0000425 if (!num_errors)
426 {
427 if (m_expr.DeclMap() && !m_expr.DeclMap()->ResolveUnknownTypes())
428 {
429 stream.Printf("error: Couldn't infer the type of a variable\n");
430 num_errors++;
431 }
432 }
433
Sean Callanan1a8d4092010-08-27 01:01:44 +0000434 return num_errors;
435}
436
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000437static bool FindFunctionInModule (ConstString &mangled_name,
Sean Callananfc55f5d2010-09-21 00:44:12 +0000438 llvm::Module *module,
439 const char *orig_name)
440{
441 for (llvm::Module::iterator fi = module->getFunctionList().begin(), fe = module->getFunctionList().end();
442 fi != fe;
443 ++fi)
444 {
445 if (fi->getName().str().find(orig_name) != std::string::npos)
446 {
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000447 mangled_name.SetCString(fi->getName().str().c_str());
Sean Callananfc55f5d2010-09-21 00:44:12 +0000448 return true;
449 }
450 }
451
452 return false;
453}
454
Sean Callanan1a8d4092010-08-27 01:01:44 +0000455Error
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000456ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_addr,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000457 lldb::addr_t &func_end,
Greg Clayton7b0992d2013-04-18 22:45:39 +0000458 std::unique_ptr<IRExecutionUnit> &execution_unit_ap,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000459 ExecutionContext &exe_ctx,
Sean Callanan1582ee62013-04-18 22:06:33 +0000460 bool &can_interpret,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000461 ExecutionPolicy execution_policy)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000462{
Greg Clayton22a939a2011-01-19 23:00:49 +0000463 func_addr = LLDB_INVALID_ADDRESS;
464 func_end = LLDB_INVALID_ADDRESS;
Greg Clayton5160ce52013-03-27 23:08:40 +0000465 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfc55f5d2010-09-21 00:44:12 +0000466
Greg Clayton7b0992d2013-04-18 22:45:39 +0000467 std::unique_ptr<llvm::ExecutionEngine> execution_engine_ap;
Sean Callanan175a0d02012-01-24 22:06:48 +0000468
Sean Callanan1a8d4092010-08-27 01:01:44 +0000469 Error err;
470
Greg Clayton7b0992d2013-04-18 22:45:39 +0000471 std::unique_ptr<llvm::Module> module_ap (m_code_generator->ReleaseModule());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000472
Greg Claytonfb9c6b72012-10-24 17:37:53 +0000473 if (!module_ap.get())
Sean Callanan1a8d4092010-08-27 01:01:44 +0000474 {
475 err.SetErrorToGenericError();
476 err.SetErrorString("IR doesn't contain a module");
477 return err;
478 }
479
Sean Callananfc55f5d2010-09-21 00:44:12 +0000480 // Find the actual name of the function (it's often mangled somehow)
481
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000482 ConstString function_name;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000483
Greg Claytonfb9c6b72012-10-24 17:37:53 +0000484 if (!FindFunctionInModule(function_name, module_ap.get(), m_expr.FunctionName()))
Sean Callananfc55f5d2010-09-21 00:44:12 +0000485 {
486 err.SetErrorToGenericError();
487 err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName());
488 return err;
489 }
490 else
491 {
Enrico Granata20edcdb2011-07-19 18:03:25 +0000492 if (log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000493 log->Printf("Found function %s for %s", function_name.AsCString(), m_expr.FunctionName());
Sean Callananfc55f5d2010-09-21 00:44:12 +0000494 }
495
Sean Callanan14b1bae2013-04-16 23:25:35 +0000496 m_execution_unit.reset(new IRExecutionUnit(m_llvm_context, // handed off here
497 module_ap, // handed off here
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000498 function_name,
Sean Callananb024d872013-04-15 17:12:47 +0000499 exe_ctx.GetTargetSP(),
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000500 m_compiler->getTargetOpts().Features));
501
Sean Callanan1a8d4092010-08-27 01:01:44 +0000502 ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); // result can be NULL
503
504 if (decl_map)
505 {
Sean Callanan3989fb92011-01-27 01:07:04 +0000506 Stream *error_stream = NULL;
Greg Claytonc14ee322011-09-22 04:58:26 +0000507 Target *target = exe_ctx.GetTargetPtr();
508 if (target)
Greg Clayton44d93782014-01-27 23:43:24 +0000509 error_stream = target->GetDebugger().GetErrorFile().get();
Sean Callanan3989fb92011-01-27 01:07:04 +0000510
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000511 IRForTarget ir_for_target(decl_map,
Sean Callanan9e6ed532010-09-13 21:34:21 +0000512 m_expr.NeedsVariableResolution(),
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000513 *m_execution_unit,
Sean Callanan3989fb92011-01-27 01:07:04 +0000514 error_stream,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000515 function_name.AsCString());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000516
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000517 bool ir_can_run = ir_for_target.runOnModule(*m_execution_unit->GetModule());
Sean Callanan6961e872010-09-01 00:58:00 +0000518
Sean Callanan1582ee62013-04-18 22:06:33 +0000519 Error interpret_error;
Sean Callanan175a0d02012-01-24 22:06:48 +0000520
Sean Callanan1582ee62013-04-18 22:06:33 +0000521 can_interpret = IRInterpreter::CanInterpret(*m_execution_unit->GetModule(), *m_execution_unit->GetFunction(), interpret_error);
Sean Callanan63697e52011-05-07 01:06:41 +0000522
Greg Claytonc14ee322011-09-22 04:58:26 +0000523 Process *process = exe_ctx.GetProcessPtr();
Sean Callanan1582ee62013-04-18 22:06:33 +0000524
525 if (!ir_can_run)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000526 {
Sean Callanan3d654b32012-09-24 22:25:51 +0000527 err.SetErrorString("The expression could not be prepared to run in the target");
Sean Callanan3d654b32012-09-24 22:25:51 +0000528 return err;
529 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000530
Sean Callanan1582ee62013-04-18 22:06:33 +0000531 if (!can_interpret && execution_policy == eExecutionPolicyNever)
Sean Callanan6961e872010-09-01 00:58:00 +0000532 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000533 err.SetErrorStringWithFormat("Can't run the expression locally: %s", interpret_error.AsCString());
534 return err;
535 }
536
537 if (!process && execution_policy == eExecutionPolicyAlways)
538 {
539 err.SetErrorString("Expression needed to run in the target, but the target can't be run");
540 return err;
541 }
542
543 if (execution_policy == eExecutionPolicyAlways || !can_interpret)
544 {
Sean Callanane8cde682013-05-18 00:38:20 +0000545 if (m_expr.NeedsValidation() && process)
546 {
547 if (!process->GetDynamicCheckers())
Sean Callanan90539452011-09-20 23:01:51 +0000548 {
Sean Callanane8cde682013-05-18 00:38:20 +0000549 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
Sean Callanan90539452011-09-20 23:01:51 +0000550
Sean Callanane8cde682013-05-18 00:38:20 +0000551 StreamString install_errors;
552
553 if (!dynamic_checkers->Install(install_errors, exe_ctx))
554 {
555 if (install_errors.GetString().empty())
556 err.SetErrorString ("couldn't install checkers, unknown error");
557 else
558 err.SetErrorString (install_errors.GetString().c_str());
559
560 return err;
561 }
562
563 process->SetDynamicCheckers(dynamic_checkers);
564
565 if (log)
566 log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
Sean Callanan90539452011-09-20 23:01:51 +0000567 }
568
Sean Callanan1582ee62013-04-18 22:06:33 +0000569 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString());
570
571 if (!ir_dynamic_checks.runOnModule(*m_execution_unit->GetModule()))
572 {
573 err.SetErrorToGenericError();
574 err.SetErrorString("Couldn't add dynamic checks to the expression");
575 return err;
576 }
Sean Callanan90539452011-09-20 23:01:51 +0000577 }
578
Sean Callanan1582ee62013-04-18 22:06:33 +0000579 m_execution_unit->GetRunnableInfo(err, func_addr, func_end);
Sean Callanan9e6ed532010-09-13 21:34:21 +0000580 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000581 }
Sean Callanan1582ee62013-04-18 22:06:33 +0000582 else
583 {
584 m_execution_unit->GetRunnableInfo(err, func_addr, func_end);
585 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000586
Greg Claytone01e07b2013-04-18 18:10:51 +0000587 execution_unit_ap.reset (m_execution_unit.release());
Sean Callananea685ae2011-11-01 17:33:54 +0000588
Sean Callanan1a8d4092010-08-27 01:01:44 +0000589 return err;
590}