blob: dd6da06e251bda6a4c88642e68851b5d7bd3fb99 [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 Callanan1a8d4092010-08-27 01:01:44 +000025#include "lldb/Target/ExecutionContext.h"
Sean Callananc3a16002011-01-17 23:42:46 +000026#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000027#include "lldb/Target/Process.h"
28#include "lldb/Target/Target.h"
29
30#include "clang/AST/ASTContext.h"
31#include "clang/AST/ExternalASTSource.h"
32#include "clang/Basic/FileManager.h"
33#include "clang/Basic/TargetInfo.h"
34#include "clang/Basic/Version.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000035#include "clang/CodeGen/CodeGenAction.h"
36#include "clang/CodeGen/ModuleBuilder.h"
37#include "clang/Driver/CC1Options.h"
38#include "clang/Driver/OptTable.h"
39#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"
Sean Callanan880e6802011-10-07 23:18:13 +000055#include "llvm/Support/TargetSelect.h"
Greg Clayton70b57652011-05-15 01:25:55 +000056
Greg Clayton0734dfb2012-10-30 17:11:34 +000057#if defined(__FreeBSD__)
Peter Collingbourne1740be7c2011-06-03 20:40:12 +000058#define USE_STANDARD_JIT
59#endif
60
Greg Clayton70b57652011-05-15 01:25:55 +000061#if defined (USE_STANDARD_JIT)
Sean Callanan1a8d4092010-08-27 01:01:44 +000062#include "llvm/ExecutionEngine/JIT.h"
Greg Clayton70b57652011-05-15 01:25:55 +000063#else
64#include "llvm/ExecutionEngine/MCJIT.h"
65#endif
Chandler Carruth1e157582013-01-02 12:20:07 +000066#include "llvm/IR/LLVMContext.h"
67#include "llvm/IR/Module.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000068#include "llvm/Support/ErrorHandling.h"
69#include "llvm/Support/MemoryBuffer.h"
Greg Clayton38a61402010-12-02 23:20:03 +000070#include "llvm/Support/DynamicLibrary.h"
71#include "llvm/Support/Host.h"
72#include "llvm/Support/Signals.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000073
74using namespace clang;
75using namespace llvm;
76using namespace lldb_private;
77
78//===----------------------------------------------------------------------===//
79// Utility Methods for Clang
80//===----------------------------------------------------------------------===//
81
82std::string GetBuiltinIncludePath(const char *Argv0) {
83 llvm::sys::Path P =
84 llvm::sys::Path::GetMainExecutable(Argv0,
85 (void*)(intptr_t) GetBuiltinIncludePath);
86
87 if (!P.isEmpty()) {
88 P.eraseComponent(); // Remove /clang from foo/bin/clang
89 P.eraseComponent(); // Remove /bin from foo/bin
90
91 // Get foo/lib/clang/<version>/include
92 P.appendComponent("lib");
93 P.appendComponent("clang");
94 P.appendComponent(CLANG_VERSION_STRING);
95 P.appendComponent("include");
96 }
97
98 return P.str();
99}
100
101
102//===----------------------------------------------------------------------===//
103// Main driver for Clang
104//===----------------------------------------------------------------------===//
105
106static void LLVMErrorHandler(void *UserData, const std::string &Message) {
Sean Callanan880e6802011-10-07 23:18:13 +0000107 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000108
109 Diags.Report(diag::err_fe_error_backend) << Message;
110
111 // We cannot recover from llvm errors.
Sean Callanan880e6802011-10-07 23:18:13 +0000112 assert(0);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000113}
114
115static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
116 using namespace clang::frontend;
117
118 switch (CI.getFrontendOpts().ProgramAction) {
119 default:
120 llvm_unreachable("Invalid program action!");
121
122 case ASTDump: return new ASTDumpAction();
123 case ASTPrint: return new ASTPrintAction();
Sean Callananfb0b7582011-03-15 00:17:19 +0000124 case ASTDumpXML: return new ASTDumpXMLAction();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000125 case ASTView: return new ASTViewAction();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000126 case DumpRawTokens: return new DumpRawTokensAction();
127 case DumpTokens: return new DumpTokensAction();
128 case EmitAssembly: return new EmitAssemblyAction();
129 case EmitBC: return new EmitBCAction();
130 case EmitHTML: return new HTMLPrintAction();
131 case EmitLLVM: return new EmitLLVMAction();
132 case EmitLLVMOnly: return new EmitLLVMOnlyAction();
133 case EmitCodeGenOnly: return new EmitCodeGenOnlyAction();
134 case EmitObj: return new EmitObjAction();
135 case FixIt: return new FixItAction();
Sean Callanana5230ce2011-12-01 04:31:46 +0000136 case GeneratePCH: return new GeneratePCHAction();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000137 case GeneratePTH: return new GeneratePTHAction();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000138 case InitOnly: return new InitOnlyAction();
139 case ParseSyntaxOnly: return new SyntaxOnlyAction();
140
141 case PluginAction: {
142 for (FrontendPluginRegistry::iterator it =
143 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
144 it != ie; ++it) {
145 if (it->getName() == CI.getFrontendOpts().ActionName) {
146 llvm::OwningPtr<PluginASTAction> P(it->instantiate());
147 if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
148 return 0;
149 return P.take();
150 }
151 }
152
153 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
154 << CI.getFrontendOpts().ActionName;
155 return 0;
156 }
157
158 case PrintDeclContext: return new DeclContextPrintAction();
159 case PrintPreamble: return new PrintPreambleAction();
160 case PrintPreprocessedInput: return new PrintPreprocessedAction();
161 case RewriteMacros: return new RewriteMacrosAction();
162 case RewriteObjC: return new RewriteObjCAction();
163 case RewriteTest: return new RewriteTestAction();
Sean Callanan2c777c42011-01-18 23:32:05 +0000164 //case RunAnalysis: return new AnalysisAction();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000165 case RunPreprocessorOnly: return new PreprocessOnlyAction();
166 }
167}
168
169static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
170 // Create the underlying action.
171 FrontendAction *Act = CreateFrontendBaseAction(CI);
172 if (!Act)
173 return 0;
174
175 // If there are any AST files to merge, create a frontend action
176 // adaptor to perform the merge.
177 if (!CI.getFrontendOpts().ASTMergeFiles.empty())
Sean Callanan5b26f272012-02-04 08:49:35 +0000178 Act = new ASTMergeAction(Act, CI.getFrontendOpts().ASTMergeFiles);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000179
180 return Act;
181}
182
183//===----------------------------------------------------------------------===//
184// Implementation of ClangExpressionParser
185//===----------------------------------------------------------------------===//
186
Greg Clayton514487e2011-02-15 21:59:32 +0000187ClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope,
188 ClangExpression &expr) :
189 m_expr (expr),
Sean Callanan1a8d4092010-08-27 01:01:44 +0000190 m_compiler (),
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000191 m_code_generator (NULL)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000192{
193 // Initialize targets first, so that --version shows registered targets.
194 static struct InitializeLLVM {
195 InitializeLLVM() {
196 llvm::InitializeAllTargets();
197 llvm::InitializeAllAsmPrinters();
Sean Callanancc427fa2011-07-30 02:42:06 +0000198 llvm::InitializeAllTargetMCs();
Sean Callanana5230ce2011-12-01 04:31:46 +0000199 llvm::InitializeAllDisassemblers();
Sean Callanan4ed61b02012-09-06 01:39:02 +0000200
201 llvm::DisablePrettyStackTrace = true;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000202 }
203 } InitializeLLVM;
Sean Callanan3d654b32012-09-24 22:25:51 +0000204
Sean Callanan1a8d4092010-08-27 01:01:44 +0000205 // 1. Create a new compiler instance.
206 m_compiler.reset(new CompilerInstance());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000207
Sean Callanan3d654b32012-09-24 22:25:51 +0000208 // 2. Install the target.
209
210 lldb::TargetSP target_sp;
211 if (exe_scope)
212 target_sp = exe_scope->CalculateTarget();
213
214 // TODO: figure out what to really do when we don't have a valid target.
215 // Sometimes this will be ok to just use the host target triple (when we
216 // evaluate say "2+3", but other expressions like breakpoint conditions
217 // and other things that _are_ target specific really shouldn't just be
218 // using the host triple. This needs to be fixed in a better way.
219 if (target_sp && target_sp->GetArchitecture().IsValid())
220 {
221 std::string triple = target_sp->GetArchitecture().GetTriple().str();
222
223 int dash_count = 0;
224 for (size_t i = 0; i < triple.size(); ++i)
225 {
226 if (triple[i] == '-')
227 dash_count++;
228 if (dash_count == 3)
229 {
230 triple.resize(i);
231 break;
232 }
233 }
234
235 m_compiler->getTargetOpts().Triple = triple;
236 }
237 else
238 {
239 m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
240 }
241
Sean Callananb45a6f02013-02-21 01:04:23 +0000242 if (target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86 ||
243 target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
244 {
245 m_compiler->getTargetOpts().Features.push_back("+sse");
246 m_compiler->getTargetOpts().Features.push_back("+sse2");
247 }
248
Sean Callanan3d654b32012-09-24 22:25:51 +0000249 if (m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos)
250 m_compiler->getTargetOpts().ABI = "apcs-gnu";
251
Sean Callananb1de8dd2013-01-22 02:20:20 +0000252 m_compiler->createDiagnostics();
Sean Callanan3d654b32012-09-24 22:25:51 +0000253
254 // Create the target instance.
255 m_compiler->setTarget(TargetInfo::CreateTargetInfo(m_compiler->getDiagnostics(),
Greg Clayton38d880a2012-11-16 21:35:22 +0000256 &m_compiler->getTargetOpts()));
Sean Callanan3d654b32012-09-24 22:25:51 +0000257
258 assert (m_compiler->hasTarget());
259
260 // 3. Set options.
Sean Callanan1a8d4092010-08-27 01:01:44 +0000261
Sean Callananc7b65062011-11-07 23:35:40 +0000262 lldb::LanguageType language = expr.Language();
Greg Claytonf83f32d2011-01-15 01:32:14 +0000263
Sean Callananc7b65062011-11-07 23:35:40 +0000264 switch (language)
265 {
266 case lldb::eLanguageTypeC:
267 break;
268 case lldb::eLanguageTypeObjC:
269 m_compiler->getLangOpts().ObjC1 = true;
270 m_compiler->getLangOpts().ObjC2 = true;
271 break;
272 case lldb::eLanguageTypeC_plus_plus:
273 m_compiler->getLangOpts().CPlusPlus = true;
Chandler Carruth38336a12013-01-02 12:55:00 +0000274 m_compiler->getLangOpts().CPlusPlus11 = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000275 break;
276 case lldb::eLanguageTypeObjC_plus_plus:
277 default:
278 m_compiler->getLangOpts().ObjC1 = true;
279 m_compiler->getLangOpts().ObjC2 = true;
280 m_compiler->getLangOpts().CPlusPlus = true;
Chandler Carruth38336a12013-01-02 12:55:00 +0000281 m_compiler->getLangOpts().CPlusPlus11 = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000282 break;
283 }
Sean Callananc3a16002011-01-17 23:42:46 +0000284
Sean Callananaa0f9cb2012-10-17 22:09:59 +0000285 m_compiler->getLangOpts().Bool = true;
286 m_compiler->getLangOpts().WChar = true;
Sean Callanan226b70c2012-03-08 02:39:03 +0000287 m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients
288 if (expr.DesiredResultType() == ClangExpression::eResultTypeId)
289 m_compiler->getLangOpts().DebuggerCastResultToId = true;
290
Sean Callanan0765c822012-04-17 00:49:48 +0000291 // Spell checking is a nice feature, but it ends up completing a
292 // lot of types that we didn't strictly speaking need to complete.
293 // As a result, we spend a long time parsing and importing debug
294 // information.
295 m_compiler->getLangOpts().SpellChecking = false;
296
Greg Claytond9e416c2012-02-18 05:35:26 +0000297 lldb::ProcessSP process_sp;
Greg Clayton514487e2011-02-15 21:59:32 +0000298 if (exe_scope)
Greg Claytond9e416c2012-02-18 05:35:26 +0000299 process_sp = exe_scope->CalculateProcess();
Greg Clayton514487e2011-02-15 21:59:32 +0000300
Greg Claytond9e416c2012-02-18 05:35:26 +0000301 if (process_sp && m_compiler->getLangOpts().ObjC1)
Sean Callananc3a16002011-01-17 23:42:46 +0000302 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000303 if (process_sp->GetObjCLanguageRuntime())
Sean Callananc3a16002011-01-17 23:42:46 +0000304 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000305 if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == eAppleObjC_V2)
Sean Callanan3d654b32012-09-24 22:25:51 +0000306 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7));
307 else
308 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7));
Sean Callanan226b70c2012-03-08 02:39:03 +0000309
310 if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
Sean Callanan226b70c2012-03-08 02:39:03 +0000311 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
Sean Callananc3a16002011-01-17 23:42:46 +0000312 }
313 }
Greg Claytonf83f32d2011-01-15 01:32:14 +0000314
Sean Callanan1a8d4092010-08-27 01:01:44 +0000315 m_compiler->getLangOpts().ThreadsafeStatics = false;
316 m_compiler->getLangOpts().AccessControl = false; // Debuggers get universal access
317 m_compiler->getLangOpts().DollarIdents = true; // $ indicates a persistent variable name
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000318
Sean Callanan1a8d4092010-08-27 01:01:44 +0000319 // Set CodeGen options
320 m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
321 m_compiler->getCodeGenOpts().InstrumentFunctions = false;
322
323 // Disable some warnings.
324 m_compiler->getDiagnosticOpts().Warnings.push_back("no-unused-value");
Sean Callanan83b3da92013-03-08 23:38:53 +0000325 m_compiler->getDiagnosticOpts().Warnings.push_back("no-odr");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000326
Sean Callanan1a8d4092010-08-27 01:01:44 +0000327 // Inform the target of the language options
328 //
329 // FIXME: We shouldn't need to do this, the target should be immutable once
330 // created. This complexity should be lifted elsewhere.
331 m_compiler->getTarget().setForcedLangOptions(m_compiler->getLangOpts());
332
333 // 4. Set up the diagnostic buffer for reporting errors
334
Sean Callanane2ef6e32010-09-23 03:01:22 +0000335 m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000336
337 // 5. Set up the source management objects inside the compiler
338
Greg Clayton38a61402010-12-02 23:20:03 +0000339 clang::FileSystemOptions file_system_options;
340 m_file_manager.reset(new clang::FileManager(file_system_options));
Sean Callanan79439e82010-11-18 02:56:27 +0000341
Sean Callanan1a8d4092010-08-27 01:01:44 +0000342 if (!m_compiler->hasSourceManager())
Greg Clayton38a61402010-12-02 23:20:03 +0000343 m_compiler->createSourceManager(*m_file_manager.get());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000344
345 m_compiler->createFileManager();
346 m_compiler->createPreprocessor();
347
348 // 6. Most of this we get from the CompilerInstance, but we
349 // also want to give the context an ExternalASTSource.
Sean Callanan6abfabf2010-11-19 20:20:02 +0000350 m_selector_table.reset(new SelectorTable());
Sean Callanan880e6802011-10-07 23:18:13 +0000351 m_builtin_context.reset(new Builtin::Context());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000352
353 std::auto_ptr<clang::ASTContext> ast_context(new ASTContext(m_compiler->getLangOpts(),
354 m_compiler->getSourceManager(),
Sean Callanan880e6802011-10-07 23:18:13 +0000355 &m_compiler->getTarget(),
Sean Callanan1a8d4092010-08-27 01:01:44 +0000356 m_compiler->getPreprocessor().getIdentifierTable(),
Sean Callanan6abfabf2010-11-19 20:20:02 +0000357 *m_selector_table.get(),
Sean Callanan1a8d4092010-08-27 01:01:44 +0000358 *m_builtin_context.get(),
359 0));
360
361 ClangExpressionDeclMap *decl_map = m_expr.DeclMap();
362
363 if (decl_map)
364 {
Sean Callananeddeb3b2011-10-28 23:38:38 +0000365 llvm::OwningPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy());
366 decl_map->InstallASTContext(ast_context.get());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000367 ast_context->setExternalSource(ast_source);
368 }
369
370 m_compiler->setASTContext(ast_context.release());
371
Greg Clayton7b462cc2010-10-15 22:48:33 +0000372 std::string module_name("$__lldb_module");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000373
Sean Callananfb0b7582011-03-15 00:17:19 +0000374 m_llvm_context.reset(new LLVMContext());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000375 m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(),
376 module_name,
377 m_compiler->getCodeGenOpts(),
Filipe Cabecinhas55fcb102013-02-14 22:02:57 +0000378 m_compiler->getTargetOpts(),
Sean Callananfb0b7582011-03-15 00:17:19 +0000379 *m_llvm_context));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000380}
381
382ClangExpressionParser::~ClangExpressionParser()
383{
384}
385
386unsigned
387ClangExpressionParser::Parse (Stream &stream)
388{
Sean Callanane2ef6e32010-09-23 03:01:22 +0000389 TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient());
390
391 diag_buf->FlushDiagnostics (m_compiler->getDiagnostics());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000392
393 MemoryBuffer *memory_buffer = MemoryBuffer::getMemBufferCopy(m_expr.Text(), __FUNCTION__);
Sean Callanand5f33a82012-03-01 02:03:47 +0000394 m_compiler->getSourceManager().createMainFileIDForMemBuffer (memory_buffer);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000395
Sean Callanane2ef6e32010-09-23 03:01:22 +0000396 diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000397
398 ASTConsumer *ast_transformer = m_expr.ASTTransformer(m_code_generator.get());
399
400 if (ast_transformer)
401 ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext());
402 else
403 ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext());
404
Sean Callanane2ef6e32010-09-23 03:01:22 +0000405 diag_buf->EndSourceFile();
Sean Callanan77502262011-05-12 23:54:16 +0000406
Sean Callanan1a8d4092010-08-27 01:01:44 +0000407 TextDiagnosticBuffer::const_iterator diag_iterator;
408
409 int num_errors = 0;
Sean Callanan57bbc6e2010-11-01 20:28:09 +0000410
Sean Callanane2ef6e32010-09-23 03:01:22 +0000411 for (diag_iterator = diag_buf->warn_begin();
412 diag_iterator != diag_buf->warn_end();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000413 ++diag_iterator)
414 stream.Printf("warning: %s\n", (*diag_iterator).second.c_str());
415
416 num_errors = 0;
417
Sean Callanane2ef6e32010-09-23 03:01:22 +0000418 for (diag_iterator = diag_buf->err_begin();
419 diag_iterator != diag_buf->err_end();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000420 ++diag_iterator)
421 {
422 num_errors++;
423 stream.Printf("error: %s\n", (*diag_iterator).second.c_str());
424 }
425
Sean Callanan57bbc6e2010-11-01 20:28:09 +0000426 for (diag_iterator = diag_buf->note_begin();
427 diag_iterator != diag_buf->note_end();
428 ++diag_iterator)
429 stream.Printf("note: %s\n", (*diag_iterator).second.c_str());
430
Sean Callanan77502262011-05-12 23:54:16 +0000431 if (!num_errors)
432 {
433 if (m_expr.DeclMap() && !m_expr.DeclMap()->ResolveUnknownTypes())
434 {
435 stream.Printf("error: Couldn't infer the type of a variable\n");
436 num_errors++;
437 }
438 }
439
Sean Callanan1a8d4092010-08-27 01:01:44 +0000440 return num_errors;
441}
442
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000443static bool FindFunctionInModule (ConstString &mangled_name,
Sean Callananfc55f5d2010-09-21 00:44:12 +0000444 llvm::Module *module,
445 const char *orig_name)
446{
447 for (llvm::Module::iterator fi = module->getFunctionList().begin(), fe = module->getFunctionList().end();
448 fi != fe;
449 ++fi)
450 {
451 if (fi->getName().str().find(orig_name) != std::string::npos)
452 {
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000453 mangled_name.SetCString(fi->getName().str().c_str());
Sean Callananfc55f5d2010-09-21 00:44:12 +0000454 return true;
455 }
456 }
457
458 return false;
459}
460
Sean Callanan1a8d4092010-08-27 01:01:44 +0000461Error
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000462ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_addr,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000463 lldb::addr_t &func_end,
464 ExecutionContext &exe_ctx,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000465 bool &evaluated_statically,
466 lldb::ClangExpressionVariableSP &const_result,
467 ExecutionPolicy execution_policy)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000468{
Greg Clayton22a939a2011-01-19 23:00:49 +0000469 func_addr = LLDB_INVALID_ADDRESS;
470 func_end = LLDB_INVALID_ADDRESS;
Greg Clayton5160ce52013-03-27 23:08:40 +0000471 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfc55f5d2010-09-21 00:44:12 +0000472
Greg Claytonfb9c6b72012-10-24 17:37:53 +0000473 std::auto_ptr<llvm::ExecutionEngine> execution_engine_ap;
Sean Callanan175a0d02012-01-24 22:06:48 +0000474
Sean Callanan1a8d4092010-08-27 01:01:44 +0000475 Error err;
476
Greg Claytonfb9c6b72012-10-24 17:37:53 +0000477 std::auto_ptr<llvm::Module> module_ap (m_code_generator->ReleaseModule());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000478
Greg Claytonfb9c6b72012-10-24 17:37:53 +0000479 if (!module_ap.get())
Sean Callanan1a8d4092010-08-27 01:01:44 +0000480 {
481 err.SetErrorToGenericError();
482 err.SetErrorString("IR doesn't contain a module");
483 return err;
484 }
485
Sean Callananfc55f5d2010-09-21 00:44:12 +0000486 // Find the actual name of the function (it's often mangled somehow)
487
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000488 ConstString function_name;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000489
Greg Claytonfb9c6b72012-10-24 17:37:53 +0000490 if (!FindFunctionInModule(function_name, module_ap.get(), m_expr.FunctionName()))
Sean Callananfc55f5d2010-09-21 00:44:12 +0000491 {
492 err.SetErrorToGenericError();
493 err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName());
494 return err;
495 }
496 else
497 {
Enrico Granata20edcdb2011-07-19 18:03:25 +0000498 if (log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000499 log->Printf("Found function %s for %s", function_name.AsCString(), m_expr.FunctionName());
Sean Callananfc55f5d2010-09-21 00:44:12 +0000500 }
501
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000502 m_execution_unit.reset(new IRExecutionUnit(module_ap, // handed off here
503 function_name,
504 exe_ctx.GetProcessSP(),
505 m_compiler->getTargetOpts().Features));
506
Sean Callanan1a8d4092010-08-27 01:01:44 +0000507 ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); // result can be NULL
508
509 if (decl_map)
510 {
Sean Callanan3989fb92011-01-27 01:07:04 +0000511 Stream *error_stream = NULL;
Greg Claytonc14ee322011-09-22 04:58:26 +0000512 Target *target = exe_ctx.GetTargetPtr();
513 if (target)
514 error_stream = &target->GetDebugger().GetErrorStream();
Sean Callanan3989fb92011-01-27 01:07:04 +0000515
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000516 IRForTarget ir_for_target(decl_map,
Sean Callanan9e6ed532010-09-13 21:34:21 +0000517 m_expr.NeedsVariableResolution(),
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000518 execution_policy,
Sean Callanane4ec90e2010-12-16 03:17:46 +0000519 const_result,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000520 *m_execution_unit,
Sean Callanan3989fb92011-01-27 01:07:04 +0000521 error_stream,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000522 function_name.AsCString());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000523
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000524 bool ir_can_run = ir_for_target.runOnModule(*m_execution_unit->GetModule());
Sean Callanan6961e872010-09-01 00:58:00 +0000525
Sean Callanan175a0d02012-01-24 22:06:48 +0000526 Error &interpreter_error(ir_for_target.getInterpreterError());
527
528 if (execution_policy != eExecutionPolicyAlways && interpreter_error.Success())
Sean Callanan63697e52011-05-07 01:06:41 +0000529 {
Johnny Chenb49440f2012-01-06 00:35:38 +0000530 if (const_result)
531 const_result->TransferAddress();
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000532 evaluated_statically = true;
Sean Callanan63697e52011-05-07 01:06:41 +0000533 err.Clear();
534 return err;
535 }
536
Greg Claytonc14ee322011-09-22 04:58:26 +0000537 Process *process = exe_ctx.GetProcessPtr();
538
539 if (!process || execution_policy == eExecutionPolicyNever)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000540 {
541 err.SetErrorToGenericError();
Sean Callanan175a0d02012-01-24 22:06:48 +0000542 if (execution_policy == eExecutionPolicyAlways)
543 err.SetErrorString("Execution needed to run in the target, but the target can't be run");
544 else
545 err.SetErrorStringWithFormat("Interpreting the expression locally failed: %s", interpreter_error.AsCString());
Sean Callanan5b26f272012-02-04 08:49:35 +0000546
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000547 return err;
548 }
Sean Callanan3d654b32012-09-24 22:25:51 +0000549 else if (!ir_can_run)
550 {
551 err.SetErrorToGenericError();
552 err.SetErrorString("The expression could not be prepared to run in the target");
553
554 return err;
555 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000556
Sean Callanan90539452011-09-20 23:01:51 +0000557 if (execution_policy != eExecutionPolicyNever &&
558 m_expr.NeedsValidation() &&
Greg Claytonc14ee322011-09-22 04:58:26 +0000559 process)
Sean Callanan6961e872010-09-01 00:58:00 +0000560 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000561 if (!process->GetDynamicCheckers())
Sean Callanan90539452011-09-20 23:01:51 +0000562 {
563 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
564
565 StreamString install_errors;
566
567 if (!dynamic_checkers->Install(install_errors, exe_ctx))
568 {
569 if (install_errors.GetString().empty())
570 err.SetErrorString ("couldn't install checkers, unknown error");
571 else
572 err.SetErrorString (install_errors.GetString().c_str());
573
574 return err;
575 }
576
Greg Claytonc14ee322011-09-22 04:58:26 +0000577 process->SetDynamicCheckers(dynamic_checkers);
Sean Callanan90539452011-09-20 23:01:51 +0000578
579 if (log)
580 log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
581 }
582
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000583 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString());
Sean Callanan9e6ed532010-09-13 21:34:21 +0000584
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000585 if (!ir_dynamic_checks.runOnModule(*m_execution_unit->GetModule()))
Sean Callanan9e6ed532010-09-13 21:34:21 +0000586 {
587 err.SetErrorToGenericError();
588 err.SetErrorString("Couldn't add dynamic checks to the expression");
589 return err;
590 }
591 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000592 }
593
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000594 m_execution_unit->GetRunnableInfo(err, func_addr, func_end);
Sean Callananea685ae2011-11-01 17:33:54 +0000595
Sean Callanan1a8d4092010-08-27 01:01:44 +0000596 return err;
597}