blob: 5e4b934822b8fbadfd9aa59a34adb0912641680f [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"
Sean Callanan6961e872010-09-01 00:58:00 +000019#include "lldb/Core/StreamString.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000020#include "lldb/Expression/ClangASTSource.h"
21#include "lldb/Expression/ClangExpression.h"
Sean Callanan77502262011-05-12 23:54:16 +000022#include "lldb/Expression/ClangExpressionDeclMap.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000023#include "lldb/Expression/IRExecutionUnit.h"
Sean Callanan6961e872010-09-01 00:58:00 +000024#include "lldb/Expression/IRDynamicChecks.h"
Sean Callanan1582ee62013-04-18 22:06:33 +000025#include "lldb/Expression/IRInterpreter.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000026#include "lldb/Target/ExecutionContext.h"
Sean Callananc3a16002011-01-17 23:42:46 +000027#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000028#include "lldb/Target/Process.h"
29#include "lldb/Target/Target.h"
30
31#include "clang/AST/ASTContext.h"
32#include "clang/AST/ExternalASTSource.h"
33#include "clang/Basic/FileManager.h"
34#include "clang/Basic/TargetInfo.h"
35#include "clang/Basic/Version.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000036#include "clang/CodeGen/CodeGenAction.h"
37#include "clang/CodeGen/ModuleBuilder.h"
38#include "clang/Driver/CC1Options.h"
39#include "clang/Driver/OptTable.h"
40#include "clang/Frontend/CompilerInstance.h"
41#include "clang/Frontend/CompilerInvocation.h"
42#include "clang/Frontend/FrontendActions.h"
43#include "clang/Frontend/FrontendDiagnostic.h"
44#include "clang/Frontend/FrontendPluginRegistry.h"
45#include "clang/Frontend/TextDiagnosticBuffer.h"
46#include "clang/Frontend/TextDiagnosticPrinter.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000047#include "clang/Lex/Preprocessor.h"
Sean Callanane2ef6e32010-09-23 03:01:22 +000048#include "clang/Parse/ParseAST.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000049#include "clang/Rewrite/Frontend/FrontendActions.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000050#include "clang/Sema/SemaConsumer.h"
Sean Callananfb0b7582011-03-15 00:17:19 +000051#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000052
53#include "llvm/ADT/StringRef.h"
54#include "llvm/ExecutionEngine/ExecutionEngine.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000055#include "llvm/Support/Debug.h"
Sean Callanan880e6802011-10-07 23:18:13 +000056#include "llvm/Support/TargetSelect.h"
Greg Clayton70b57652011-05-15 01:25:55 +000057
Greg Clayton0734dfb2012-10-30 17:11:34 +000058#if defined(__FreeBSD__)
Peter Collingbourne1740be7c2011-06-03 20:40:12 +000059#define USE_STANDARD_JIT
60#endif
61
Greg Clayton70b57652011-05-15 01:25:55 +000062#if defined (USE_STANDARD_JIT)
Sean Callanan1a8d4092010-08-27 01:01:44 +000063#include "llvm/ExecutionEngine/JIT.h"
Greg Clayton70b57652011-05-15 01:25:55 +000064#else
65#include "llvm/ExecutionEngine/MCJIT.h"
66#endif
Chandler Carruth1e157582013-01-02 12:20:07 +000067#include "llvm/IR/LLVMContext.h"
68#include "llvm/IR/Module.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000069#include "llvm/Support/ErrorHandling.h"
70#include "llvm/Support/MemoryBuffer.h"
Greg Clayton38a61402010-12-02 23:20:03 +000071#include "llvm/Support/DynamicLibrary.h"
72#include "llvm/Support/Host.h"
73#include "llvm/Support/Signals.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000074
75using namespace clang;
76using namespace llvm;
77using namespace lldb_private;
78
79//===----------------------------------------------------------------------===//
80// Utility Methods for Clang
81//===----------------------------------------------------------------------===//
82
83std::string GetBuiltinIncludePath(const char *Argv0) {
84 llvm::sys::Path P =
85 llvm::sys::Path::GetMainExecutable(Argv0,
86 (void*)(intptr_t) GetBuiltinIncludePath);
87
88 if (!P.isEmpty()) {
89 P.eraseComponent(); // Remove /clang from foo/bin/clang
90 P.eraseComponent(); // Remove /bin from foo/bin
91
92 // Get foo/lib/clang/<version>/include
93 P.appendComponent("lib");
94 P.appendComponent("clang");
95 P.appendComponent(CLANG_VERSION_STRING);
96 P.appendComponent("include");
97 }
98
99 return P.str();
100}
101
102
103//===----------------------------------------------------------------------===//
104// Main driver for Clang
105//===----------------------------------------------------------------------===//
106
107static void LLVMErrorHandler(void *UserData, const std::string &Message) {
Sean Callanan880e6802011-10-07 23:18:13 +0000108 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000109
110 Diags.Report(diag::err_fe_error_backend) << Message;
111
112 // We cannot recover from llvm errors.
Sean Callanan880e6802011-10-07 23:18:13 +0000113 assert(0);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000114}
115
116static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
117 using namespace clang::frontend;
118
119 switch (CI.getFrontendOpts().ProgramAction) {
120 default:
121 llvm_unreachable("Invalid program action!");
122
123 case ASTDump: return new ASTDumpAction();
124 case ASTPrint: return new ASTPrintAction();
Sean Callananfb0b7582011-03-15 00:17:19 +0000125 case ASTDumpXML: return new ASTDumpXMLAction();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000126 case ASTView: return new ASTViewAction();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000127 case DumpRawTokens: return new DumpRawTokensAction();
128 case DumpTokens: return new DumpTokensAction();
129 case EmitAssembly: return new EmitAssemblyAction();
130 case EmitBC: return new EmitBCAction();
131 case EmitHTML: return new HTMLPrintAction();
132 case EmitLLVM: return new EmitLLVMAction();
133 case EmitLLVMOnly: return new EmitLLVMOnlyAction();
134 case EmitCodeGenOnly: return new EmitCodeGenOnlyAction();
135 case EmitObj: return new EmitObjAction();
136 case FixIt: return new FixItAction();
Sean Callanana5230ce2011-12-01 04:31:46 +0000137 case GeneratePCH: return new GeneratePCHAction();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000138 case GeneratePTH: return new GeneratePTHAction();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000139 case InitOnly: return new InitOnlyAction();
140 case ParseSyntaxOnly: return new SyntaxOnlyAction();
141
142 case PluginAction: {
143 for (FrontendPluginRegistry::iterator it =
144 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
145 it != ie; ++it) {
146 if (it->getName() == CI.getFrontendOpts().ActionName) {
147 llvm::OwningPtr<PluginASTAction> P(it->instantiate());
148 if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
149 return 0;
150 return P.take();
151 }
152 }
153
154 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
155 << CI.getFrontendOpts().ActionName;
156 return 0;
157 }
158
159 case PrintDeclContext: return new DeclContextPrintAction();
160 case PrintPreamble: return new PrintPreambleAction();
161 case PrintPreprocessedInput: return new PrintPreprocessedAction();
162 case RewriteMacros: return new RewriteMacrosAction();
163 case RewriteObjC: return new RewriteObjCAction();
164 case RewriteTest: return new RewriteTestAction();
Sean Callanan2c777c42011-01-18 23:32:05 +0000165 //case RunAnalysis: return new AnalysisAction();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000166 case RunPreprocessorOnly: return new PreprocessOnlyAction();
167 }
168}
169
170static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
171 // Create the underlying action.
172 FrontendAction *Act = CreateFrontendBaseAction(CI);
173 if (!Act)
174 return 0;
175
176 // If there are any AST files to merge, create a frontend action
177 // adaptor to perform the merge.
178 if (!CI.getFrontendOpts().ASTMergeFiles.empty())
Sean Callanan5b26f272012-02-04 08:49:35 +0000179 Act = new ASTMergeAction(Act, CI.getFrontendOpts().ASTMergeFiles);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000180
181 return Act;
182}
183
184//===----------------------------------------------------------------------===//
185// Implementation of ClangExpressionParser
186//===----------------------------------------------------------------------===//
187
Greg Clayton514487e2011-02-15 21:59:32 +0000188ClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope,
189 ClangExpression &expr) :
190 m_expr (expr),
Sean Callanan1a8d4092010-08-27 01:01:44 +0000191 m_compiler (),
Greg Claytone01e07b2013-04-18 18:10:51 +0000192 m_code_generator ()
Sean Callanan1a8d4092010-08-27 01:01:44 +0000193{
194 // Initialize targets first, so that --version shows registered targets.
195 static struct InitializeLLVM {
196 InitializeLLVM() {
197 llvm::InitializeAllTargets();
198 llvm::InitializeAllAsmPrinters();
Sean Callanancc427fa2011-07-30 02:42:06 +0000199 llvm::InitializeAllTargetMCs();
Sean Callanana5230ce2011-12-01 04:31:46 +0000200 llvm::InitializeAllDisassemblers();
Sean Callanan4ed61b02012-09-06 01:39:02 +0000201
202 llvm::DisablePrettyStackTrace = true;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000203 }
204 } InitializeLLVM;
Sean Callanan3d654b32012-09-24 22:25:51 +0000205
Sean Callanan1a8d4092010-08-27 01:01:44 +0000206 // 1. Create a new compiler instance.
207 m_compiler.reset(new CompilerInstance());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000208
Sean Callanan3d654b32012-09-24 22:25:51 +0000209 // 2. Install the target.
210
211 lldb::TargetSP target_sp;
212 if (exe_scope)
213 target_sp = exe_scope->CalculateTarget();
214
215 // TODO: figure out what to really do when we don't have a valid target.
216 // Sometimes this will be ok to just use the host target triple (when we
217 // evaluate say "2+3", but other expressions like breakpoint conditions
218 // and other things that _are_ target specific really shouldn't just be
219 // using the host triple. This needs to be fixed in a better way.
220 if (target_sp && target_sp->GetArchitecture().IsValid())
221 {
222 std::string triple = target_sp->GetArchitecture().GetTriple().str();
223
224 int dash_count = 0;
225 for (size_t i = 0; i < triple.size(); ++i)
226 {
227 if (triple[i] == '-')
228 dash_count++;
229 if (dash_count == 3)
230 {
231 triple.resize(i);
232 break;
233 }
234 }
235
236 m_compiler->getTargetOpts().Triple = triple;
237 }
238 else
239 {
240 m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
241 }
242
Sean Callananb45a6f02013-02-21 01:04:23 +0000243 if (target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86 ||
244 target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
245 {
246 m_compiler->getTargetOpts().Features.push_back("+sse");
247 m_compiler->getTargetOpts().Features.push_back("+sse2");
248 }
249
Sean Callanan3d654b32012-09-24 22:25:51 +0000250 if (m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos)
251 m_compiler->getTargetOpts().ABI = "apcs-gnu";
252
Sean Callananb1de8dd2013-01-22 02:20:20 +0000253 m_compiler->createDiagnostics();
Sean Callanan3d654b32012-09-24 22:25:51 +0000254
255 // Create the target instance.
256 m_compiler->setTarget(TargetInfo::CreateTargetInfo(m_compiler->getDiagnostics(),
Greg Clayton38d880a2012-11-16 21:35:22 +0000257 &m_compiler->getTargetOpts()));
Sean Callanan3d654b32012-09-24 22:25:51 +0000258
259 assert (m_compiler->hasTarget());
260
261 // 3. Set options.
Sean Callanan1a8d4092010-08-27 01:01:44 +0000262
Sean Callananc7b65062011-11-07 23:35:40 +0000263 lldb::LanguageType language = expr.Language();
Greg Claytonf83f32d2011-01-15 01:32:14 +0000264
Sean Callananc7b65062011-11-07 23:35:40 +0000265 switch (language)
266 {
267 case lldb::eLanguageTypeC:
268 break;
269 case lldb::eLanguageTypeObjC:
270 m_compiler->getLangOpts().ObjC1 = true;
271 m_compiler->getLangOpts().ObjC2 = true;
272 break;
273 case lldb::eLanguageTypeC_plus_plus:
274 m_compiler->getLangOpts().CPlusPlus = true;
Chandler Carruth38336a12013-01-02 12:55:00 +0000275 m_compiler->getLangOpts().CPlusPlus11 = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000276 break;
277 case lldb::eLanguageTypeObjC_plus_plus:
278 default:
279 m_compiler->getLangOpts().ObjC1 = true;
280 m_compiler->getLangOpts().ObjC2 = true;
281 m_compiler->getLangOpts().CPlusPlus = true;
Chandler Carruth38336a12013-01-02 12:55:00 +0000282 m_compiler->getLangOpts().CPlusPlus11 = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000283 break;
284 }
Sean Callananc3a16002011-01-17 23:42:46 +0000285
Sean Callananaa0f9cb2012-10-17 22:09:59 +0000286 m_compiler->getLangOpts().Bool = true;
287 m_compiler->getLangOpts().WChar = true;
Sean Callananeeb43842013-04-01 22:12:37 +0000288 m_compiler->getLangOpts().Blocks = true;
Sean Callanan226b70c2012-03-08 02:39:03 +0000289 m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients
290 if (expr.DesiredResultType() == ClangExpression::eResultTypeId)
291 m_compiler->getLangOpts().DebuggerCastResultToId = true;
292
Sean Callanan0765c822012-04-17 00:49:48 +0000293 // Spell checking is a nice feature, but it ends up completing a
294 // lot of types that we didn't strictly speaking need to complete.
295 // As a result, we spend a long time parsing and importing debug
296 // information.
297 m_compiler->getLangOpts().SpellChecking = false;
298
Greg Claytond9e416c2012-02-18 05:35:26 +0000299 lldb::ProcessSP process_sp;
Greg Clayton514487e2011-02-15 21:59:32 +0000300 if (exe_scope)
Greg Claytond9e416c2012-02-18 05:35:26 +0000301 process_sp = exe_scope->CalculateProcess();
Greg Clayton514487e2011-02-15 21:59:32 +0000302
Greg Claytond9e416c2012-02-18 05:35:26 +0000303 if (process_sp && m_compiler->getLangOpts().ObjC1)
Sean Callananc3a16002011-01-17 23:42:46 +0000304 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000305 if (process_sp->GetObjCLanguageRuntime())
Sean Callananc3a16002011-01-17 23:42:46 +0000306 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000307 if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == eAppleObjC_V2)
Sean Callanan3d654b32012-09-24 22:25:51 +0000308 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7));
309 else
310 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7));
Sean Callanan226b70c2012-03-08 02:39:03 +0000311
312 if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
Sean Callanan226b70c2012-03-08 02:39:03 +0000313 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
Sean Callananc3a16002011-01-17 23:42:46 +0000314 }
315 }
Greg Claytonf83f32d2011-01-15 01:32:14 +0000316
Sean Callanan1a8d4092010-08-27 01:01:44 +0000317 m_compiler->getLangOpts().ThreadsafeStatics = false;
318 m_compiler->getLangOpts().AccessControl = false; // Debuggers get universal access
319 m_compiler->getLangOpts().DollarIdents = true; // $ indicates a persistent variable name
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000320
Sean Callanan1a8d4092010-08-27 01:01:44 +0000321 // Set CodeGen options
322 m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
323 m_compiler->getCodeGenOpts().InstrumentFunctions = false;
324
325 // Disable some warnings.
Sean Callanan42f26b42013-03-30 01:26:06 +0000326 m_compiler->getDiagnostics().setDiagnosticGroupMapping("unused-value", clang::diag::MAP_IGNORE, SourceLocation());
327 m_compiler->getDiagnostics().setDiagnosticGroupMapping("odr", clang::diag::MAP_IGNORE, SourceLocation());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000328
Sean Callanan1a8d4092010-08-27 01:01:44 +0000329 // Inform the target of the language options
330 //
331 // FIXME: We shouldn't need to do this, the target should be immutable once
332 // created. This complexity should be lifted elsewhere.
333 m_compiler->getTarget().setForcedLangOptions(m_compiler->getLangOpts());
334
335 // 4. Set up the diagnostic buffer for reporting errors
336
Sean Callanane2ef6e32010-09-23 03:01:22 +0000337 m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000338
339 // 5. Set up the source management objects inside the compiler
340
Greg Clayton38a61402010-12-02 23:20:03 +0000341 clang::FileSystemOptions file_system_options;
342 m_file_manager.reset(new clang::FileManager(file_system_options));
Sean Callanan79439e82010-11-18 02:56:27 +0000343
Sean Callanan1a8d4092010-08-27 01:01:44 +0000344 if (!m_compiler->hasSourceManager())
Greg Clayton38a61402010-12-02 23:20:03 +0000345 m_compiler->createSourceManager(*m_file_manager.get());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000346
347 m_compiler->createFileManager();
348 m_compiler->createPreprocessor();
349
350 // 6. Most of this we get from the CompilerInstance, but we
351 // also want to give the context an ExternalASTSource.
Sean Callanan6abfabf2010-11-19 20:20:02 +0000352 m_selector_table.reset(new SelectorTable());
Sean Callanan880e6802011-10-07 23:18:13 +0000353 m_builtin_context.reset(new Builtin::Context());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000354
Greg Claytone01e07b2013-04-18 18:10:51 +0000355 STD_UNIQUE_PTR(clang::ASTContext) ast_context(new ASTContext(m_compiler->getLangOpts(),
356 m_compiler->getSourceManager(),
357 &m_compiler->getTarget(),
358 m_compiler->getPreprocessor().getIdentifierTable(),
359 *m_selector_table.get(),
360 *m_builtin_context.get(),
361 0));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000362
363 ClangExpressionDeclMap *decl_map = m_expr.DeclMap();
364
365 if (decl_map)
366 {
Sean Callananeddeb3b2011-10-28 23:38:38 +0000367 llvm::OwningPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy());
368 decl_map->InstallASTContext(ast_context.get());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000369 ast_context->setExternalSource(ast_source);
370 }
371
372 m_compiler->setASTContext(ast_context.release());
373
Greg Clayton7b462cc2010-10-15 22:48:33 +0000374 std::string module_name("$__lldb_module");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000375
Sean Callananfb0b7582011-03-15 00:17:19 +0000376 m_llvm_context.reset(new LLVMContext());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000377 m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(),
378 module_name,
379 m_compiler->getCodeGenOpts(),
Filipe Cabecinhas55fcb102013-02-14 22:02:57 +0000380 m_compiler->getTargetOpts(),
Sean Callananfb0b7582011-03-15 00:17:19 +0000381 *m_llvm_context));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000382}
383
384ClangExpressionParser::~ClangExpressionParser()
385{
386}
387
388unsigned
389ClangExpressionParser::Parse (Stream &stream)
390{
Sean Callanane2ef6e32010-09-23 03:01:22 +0000391 TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient());
392
393 diag_buf->FlushDiagnostics (m_compiler->getDiagnostics());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000394
395 MemoryBuffer *memory_buffer = MemoryBuffer::getMemBufferCopy(m_expr.Text(), __FUNCTION__);
Sean Callanand5f33a82012-03-01 02:03:47 +0000396 m_compiler->getSourceManager().createMainFileIDForMemBuffer (memory_buffer);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000397
Sean Callanane2ef6e32010-09-23 03:01:22 +0000398 diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000399
400 ASTConsumer *ast_transformer = m_expr.ASTTransformer(m_code_generator.get());
401
402 if (ast_transformer)
403 ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext());
404 else
405 ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext());
406
Sean Callanane2ef6e32010-09-23 03:01:22 +0000407 diag_buf->EndSourceFile();
Sean Callanan77502262011-05-12 23:54:16 +0000408
Sean Callanan1a8d4092010-08-27 01:01:44 +0000409 TextDiagnosticBuffer::const_iterator diag_iterator;
410
411 int num_errors = 0;
Sean Callanan57bbc6e2010-11-01 20:28:09 +0000412
Sean Callanane2ef6e32010-09-23 03:01:22 +0000413 for (diag_iterator = diag_buf->warn_begin();
414 diag_iterator != diag_buf->warn_end();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000415 ++diag_iterator)
416 stream.Printf("warning: %s\n", (*diag_iterator).second.c_str());
417
418 num_errors = 0;
419
Sean Callanane2ef6e32010-09-23 03:01:22 +0000420 for (diag_iterator = diag_buf->err_begin();
421 diag_iterator != diag_buf->err_end();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000422 ++diag_iterator)
423 {
424 num_errors++;
425 stream.Printf("error: %s\n", (*diag_iterator).second.c_str());
426 }
427
Sean Callanan57bbc6e2010-11-01 20:28:09 +0000428 for (diag_iterator = diag_buf->note_begin();
429 diag_iterator != diag_buf->note_end();
430 ++diag_iterator)
431 stream.Printf("note: %s\n", (*diag_iterator).second.c_str());
432
Sean Callanan77502262011-05-12 23:54:16 +0000433 if (!num_errors)
434 {
435 if (m_expr.DeclMap() && !m_expr.DeclMap()->ResolveUnknownTypes())
436 {
437 stream.Printf("error: Couldn't infer the type of a variable\n");
438 num_errors++;
439 }
440 }
441
Sean Callanan1a8d4092010-08-27 01:01:44 +0000442 return num_errors;
443}
444
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000445static bool FindFunctionInModule (ConstString &mangled_name,
Sean Callananfc55f5d2010-09-21 00:44:12 +0000446 llvm::Module *module,
447 const char *orig_name)
448{
449 for (llvm::Module::iterator fi = module->getFunctionList().begin(), fe = module->getFunctionList().end();
450 fi != fe;
451 ++fi)
452 {
453 if (fi->getName().str().find(orig_name) != std::string::npos)
454 {
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000455 mangled_name.SetCString(fi->getName().str().c_str());
Sean Callananfc55f5d2010-09-21 00:44:12 +0000456 return true;
457 }
458 }
459
460 return false;
461}
462
Sean Callanan1a8d4092010-08-27 01:01:44 +0000463Error
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000464ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_addr,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000465 lldb::addr_t &func_end,
Greg Claytone01e07b2013-04-18 18:10:51 +0000466 STD_UNIQUE_PTR(IRExecutionUnit) &execution_unit_ap,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000467 ExecutionContext &exe_ctx,
Sean Callanan1582ee62013-04-18 22:06:33 +0000468 bool &can_interpret,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000469 ExecutionPolicy execution_policy)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000470{
Greg Clayton22a939a2011-01-19 23:00:49 +0000471 func_addr = LLDB_INVALID_ADDRESS;
472 func_end = LLDB_INVALID_ADDRESS;
Greg Clayton5160ce52013-03-27 23:08:40 +0000473 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfc55f5d2010-09-21 00:44:12 +0000474
Greg Claytone01e07b2013-04-18 18:10:51 +0000475 STD_UNIQUE_PTR(llvm::ExecutionEngine) execution_engine_ap;
Sean Callanan175a0d02012-01-24 22:06:48 +0000476
Sean Callanan1a8d4092010-08-27 01:01:44 +0000477 Error err;
478
Greg Claytone01e07b2013-04-18 18:10:51 +0000479 STD_UNIQUE_PTR(llvm::Module) module_ap (m_code_generator->ReleaseModule());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000480
Greg Claytonfb9c6b72012-10-24 17:37:53 +0000481 if (!module_ap.get())
Sean Callanan1a8d4092010-08-27 01:01:44 +0000482 {
483 err.SetErrorToGenericError();
484 err.SetErrorString("IR doesn't contain a module");
485 return err;
486 }
487
Sean Callananfc55f5d2010-09-21 00:44:12 +0000488 // Find the actual name of the function (it's often mangled somehow)
489
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000490 ConstString function_name;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000491
Greg Claytonfb9c6b72012-10-24 17:37:53 +0000492 if (!FindFunctionInModule(function_name, module_ap.get(), m_expr.FunctionName()))
Sean Callananfc55f5d2010-09-21 00:44:12 +0000493 {
494 err.SetErrorToGenericError();
495 err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName());
496 return err;
497 }
498 else
499 {
Enrico Granata20edcdb2011-07-19 18:03:25 +0000500 if (log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000501 log->Printf("Found function %s for %s", function_name.AsCString(), m_expr.FunctionName());
Sean Callananfc55f5d2010-09-21 00:44:12 +0000502 }
503
Sean Callanan14b1bae2013-04-16 23:25:35 +0000504 m_execution_unit.reset(new IRExecutionUnit(m_llvm_context, // handed off here
505 module_ap, // handed off here
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000506 function_name,
Sean Callananb024d872013-04-15 17:12:47 +0000507 exe_ctx.GetTargetSP(),
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000508 m_compiler->getTargetOpts().Features));
509
Sean Callanan1a8d4092010-08-27 01:01:44 +0000510 ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); // result can be NULL
511
512 if (decl_map)
513 {
Sean Callanan3989fb92011-01-27 01:07:04 +0000514 Stream *error_stream = NULL;
Greg Claytonc14ee322011-09-22 04:58:26 +0000515 Target *target = exe_ctx.GetTargetPtr();
516 if (target)
517 error_stream = &target->GetDebugger().GetErrorStream();
Sean Callanan3989fb92011-01-27 01:07:04 +0000518
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000519 IRForTarget ir_for_target(decl_map,
Sean Callanan9e6ed532010-09-13 21:34:21 +0000520 m_expr.NeedsVariableResolution(),
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000521 *m_execution_unit,
Sean Callanan3989fb92011-01-27 01:07:04 +0000522 error_stream,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000523 function_name.AsCString());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000524
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000525 bool ir_can_run = ir_for_target.runOnModule(*m_execution_unit->GetModule());
Sean Callanan6961e872010-09-01 00:58:00 +0000526
Sean Callanan1582ee62013-04-18 22:06:33 +0000527 Error interpret_error;
Sean Callanan175a0d02012-01-24 22:06:48 +0000528
Sean Callanan1582ee62013-04-18 22:06:33 +0000529 can_interpret = IRInterpreter::CanInterpret(*m_execution_unit->GetModule(), *m_execution_unit->GetFunction(), interpret_error);
Sean Callanan63697e52011-05-07 01:06:41 +0000530
Greg Claytonc14ee322011-09-22 04:58:26 +0000531 Process *process = exe_ctx.GetProcessPtr();
Sean Callanan1582ee62013-04-18 22:06:33 +0000532
533 if (!ir_can_run)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000534 {
Sean Callanan3d654b32012-09-24 22:25:51 +0000535 err.SetErrorString("The expression could not be prepared to run in the target");
Sean Callanan3d654b32012-09-24 22:25:51 +0000536 return err;
537 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000538
Sean Callanan1582ee62013-04-18 22:06:33 +0000539 if (!can_interpret && execution_policy == eExecutionPolicyNever)
Sean Callanan6961e872010-09-01 00:58:00 +0000540 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000541 err.SetErrorStringWithFormat("Can't run the expression locally: %s", interpret_error.AsCString());
542 return err;
543 }
544
545 if (!process && execution_policy == eExecutionPolicyAlways)
546 {
547 err.SetErrorString("Expression needed to run in the target, but the target can't be run");
548 return err;
549 }
550
551 if (execution_policy == eExecutionPolicyAlways || !can_interpret)
552 {
553 if (!process->GetDynamicCheckers() && m_expr.NeedsValidation())
Sean Callanan90539452011-09-20 23:01:51 +0000554 {
555 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
556
557 StreamString install_errors;
558
559 if (!dynamic_checkers->Install(install_errors, exe_ctx))
560 {
561 if (install_errors.GetString().empty())
562 err.SetErrorString ("couldn't install checkers, unknown error");
563 else
564 err.SetErrorString (install_errors.GetString().c_str());
565
566 return err;
567 }
568
Greg Claytonc14ee322011-09-22 04:58:26 +0000569 process->SetDynamicCheckers(dynamic_checkers);
Sean Callanan90539452011-09-20 23:01:51 +0000570
571 if (log)
572 log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
Sean Callanan1582ee62013-04-18 22:06:33 +0000573
574 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString());
575
576 if (!ir_dynamic_checks.runOnModule(*m_execution_unit->GetModule()))
577 {
578 err.SetErrorToGenericError();
579 err.SetErrorString("Couldn't add dynamic checks to the expression");
580 return err;
581 }
Sean Callanan90539452011-09-20 23:01:51 +0000582 }
583
Sean Callanan1582ee62013-04-18 22:06:33 +0000584 m_execution_unit->GetRunnableInfo(err, func_addr, func_end);
Sean Callanan9e6ed532010-09-13 21:34:21 +0000585 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000586 }
Sean Callanan1582ee62013-04-18 22:06:33 +0000587 else
588 {
589 m_execution_unit->GetRunnableInfo(err, func_addr, func_end);
590 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000591
Greg Claytone01e07b2013-04-18 18:10:51 +0000592 execution_unit_ap.reset (m_execution_unit.release());
Sean Callananea685ae2011-11-01 17:33:54 +0000593
Sean Callanan1a8d4092010-08-27 01:01:44 +0000594 return err;
595}