blob: 32daaa92861d674d9e675d38ac0fcdbaf62908b6 [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 Callananeeb43842013-04-01 22:12:37 +0000287 m_compiler->getLangOpts().Blocks = true;
Sean Callanan226b70c2012-03-08 02:39:03 +0000288 m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients
289 if (expr.DesiredResultType() == ClangExpression::eResultTypeId)
290 m_compiler->getLangOpts().DebuggerCastResultToId = true;
291
Sean Callanan0765c822012-04-17 00:49:48 +0000292 // Spell checking is a nice feature, but it ends up completing a
293 // lot of types that we didn't strictly speaking need to complete.
294 // As a result, we spend a long time parsing and importing debug
295 // information.
296 m_compiler->getLangOpts().SpellChecking = false;
297
Greg Claytond9e416c2012-02-18 05:35:26 +0000298 lldb::ProcessSP process_sp;
Greg Clayton514487e2011-02-15 21:59:32 +0000299 if (exe_scope)
Greg Claytond9e416c2012-02-18 05:35:26 +0000300 process_sp = exe_scope->CalculateProcess();
Greg Clayton514487e2011-02-15 21:59:32 +0000301
Greg Claytond9e416c2012-02-18 05:35:26 +0000302 if (process_sp && m_compiler->getLangOpts().ObjC1)
Sean Callananc3a16002011-01-17 23:42:46 +0000303 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000304 if (process_sp->GetObjCLanguageRuntime())
Sean Callananc3a16002011-01-17 23:42:46 +0000305 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000306 if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == eAppleObjC_V2)
Sean Callanan3d654b32012-09-24 22:25:51 +0000307 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7));
308 else
309 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7));
Sean Callanan226b70c2012-03-08 02:39:03 +0000310
311 if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
Sean Callanan226b70c2012-03-08 02:39:03 +0000312 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
Sean Callananc3a16002011-01-17 23:42:46 +0000313 }
314 }
Greg Claytonf83f32d2011-01-15 01:32:14 +0000315
Sean Callanan1a8d4092010-08-27 01:01:44 +0000316 m_compiler->getLangOpts().ThreadsafeStatics = false;
317 m_compiler->getLangOpts().AccessControl = false; // Debuggers get universal access
318 m_compiler->getLangOpts().DollarIdents = true; // $ indicates a persistent variable name
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000319
Sean Callanan1a8d4092010-08-27 01:01:44 +0000320 // Set CodeGen options
321 m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
322 m_compiler->getCodeGenOpts().InstrumentFunctions = false;
323
324 // Disable some warnings.
Sean Callanan42f26b42013-03-30 01:26:06 +0000325 m_compiler->getDiagnostics().setDiagnosticGroupMapping("unused-value", clang::diag::MAP_IGNORE, SourceLocation());
326 m_compiler->getDiagnostics().setDiagnosticGroupMapping("odr", clang::diag::MAP_IGNORE, SourceLocation());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000327
Sean Callanan1a8d4092010-08-27 01:01:44 +0000328 // Inform the target of the language options
329 //
330 // FIXME: We shouldn't need to do this, the target should be immutable once
331 // created. This complexity should be lifted elsewhere.
332 m_compiler->getTarget().setForcedLangOptions(m_compiler->getLangOpts());
333
334 // 4. Set up the diagnostic buffer for reporting errors
335
Sean Callanane2ef6e32010-09-23 03:01:22 +0000336 m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000337
338 // 5. Set up the source management objects inside the compiler
339
Greg Clayton38a61402010-12-02 23:20:03 +0000340 clang::FileSystemOptions file_system_options;
341 m_file_manager.reset(new clang::FileManager(file_system_options));
Sean Callanan79439e82010-11-18 02:56:27 +0000342
Sean Callanan1a8d4092010-08-27 01:01:44 +0000343 if (!m_compiler->hasSourceManager())
Greg Clayton38a61402010-12-02 23:20:03 +0000344 m_compiler->createSourceManager(*m_file_manager.get());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000345
346 m_compiler->createFileManager();
347 m_compiler->createPreprocessor();
348
349 // 6. Most of this we get from the CompilerInstance, but we
350 // also want to give the context an ExternalASTSource.
Sean Callanan6abfabf2010-11-19 20:20:02 +0000351 m_selector_table.reset(new SelectorTable());
Sean Callanan880e6802011-10-07 23:18:13 +0000352 m_builtin_context.reset(new Builtin::Context());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000353
354 std::auto_ptr<clang::ASTContext> ast_context(new ASTContext(m_compiler->getLangOpts(),
355 m_compiler->getSourceManager(),
Sean Callanan880e6802011-10-07 23:18:13 +0000356 &m_compiler->getTarget(),
Sean Callanan1a8d4092010-08-27 01:01:44 +0000357 m_compiler->getPreprocessor().getIdentifierTable(),
Sean Callanan6abfabf2010-11-19 20:20:02 +0000358 *m_selector_table.get(),
Sean Callanan1a8d4092010-08-27 01:01:44 +0000359 *m_builtin_context.get(),
360 0));
361
362 ClangExpressionDeclMap *decl_map = m_expr.DeclMap();
363
364 if (decl_map)
365 {
Sean Callananeddeb3b2011-10-28 23:38:38 +0000366 llvm::OwningPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy());
367 decl_map->InstallASTContext(ast_context.get());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000368 ast_context->setExternalSource(ast_source);
369 }
370
371 m_compiler->setASTContext(ast_context.release());
372
Greg Clayton7b462cc2010-10-15 22:48:33 +0000373 std::string module_name("$__lldb_module");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000374
Sean Callananfb0b7582011-03-15 00:17:19 +0000375 m_llvm_context.reset(new LLVMContext());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000376 m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(),
377 module_name,
378 m_compiler->getCodeGenOpts(),
Filipe Cabecinhas55fcb102013-02-14 22:02:57 +0000379 m_compiler->getTargetOpts(),
Sean Callananfb0b7582011-03-15 00:17:19 +0000380 *m_llvm_context));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000381}
382
383ClangExpressionParser::~ClangExpressionParser()
384{
385}
386
387unsigned
388ClangExpressionParser::Parse (Stream &stream)
389{
Sean Callanane2ef6e32010-09-23 03:01:22 +0000390 TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient());
391
392 diag_buf->FlushDiagnostics (m_compiler->getDiagnostics());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000393
394 MemoryBuffer *memory_buffer = MemoryBuffer::getMemBufferCopy(m_expr.Text(), __FUNCTION__);
Sean Callanand5f33a82012-03-01 02:03:47 +0000395 m_compiler->getSourceManager().createMainFileIDForMemBuffer (memory_buffer);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000396
Sean Callanane2ef6e32010-09-23 03:01:22 +0000397 diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000398
399 ASTConsumer *ast_transformer = m_expr.ASTTransformer(m_code_generator.get());
400
401 if (ast_transformer)
402 ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext());
403 else
404 ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext());
405
Sean Callanane2ef6e32010-09-23 03:01:22 +0000406 diag_buf->EndSourceFile();
Sean Callanan77502262011-05-12 23:54:16 +0000407
Sean Callanan1a8d4092010-08-27 01:01:44 +0000408 TextDiagnosticBuffer::const_iterator diag_iterator;
409
410 int num_errors = 0;
Sean Callanan57bbc6e2010-11-01 20:28:09 +0000411
Sean Callanane2ef6e32010-09-23 03:01:22 +0000412 for (diag_iterator = diag_buf->warn_begin();
413 diag_iterator != diag_buf->warn_end();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000414 ++diag_iterator)
415 stream.Printf("warning: %s\n", (*diag_iterator).second.c_str());
416
417 num_errors = 0;
418
Sean Callanane2ef6e32010-09-23 03:01:22 +0000419 for (diag_iterator = diag_buf->err_begin();
420 diag_iterator != diag_buf->err_end();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000421 ++diag_iterator)
422 {
423 num_errors++;
424 stream.Printf("error: %s\n", (*diag_iterator).second.c_str());
425 }
426
Sean Callanan57bbc6e2010-11-01 20:28:09 +0000427 for (diag_iterator = diag_buf->note_begin();
428 diag_iterator != diag_buf->note_end();
429 ++diag_iterator)
430 stream.Printf("note: %s\n", (*diag_iterator).second.c_str());
431
Sean Callanan77502262011-05-12 23:54:16 +0000432 if (!num_errors)
433 {
434 if (m_expr.DeclMap() && !m_expr.DeclMap()->ResolveUnknownTypes())
435 {
436 stream.Printf("error: Couldn't infer the type of a variable\n");
437 num_errors++;
438 }
439 }
440
Sean Callanan1a8d4092010-08-27 01:01:44 +0000441 return num_errors;
442}
443
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000444static bool FindFunctionInModule (ConstString &mangled_name,
Sean Callananfc55f5d2010-09-21 00:44:12 +0000445 llvm::Module *module,
446 const char *orig_name)
447{
448 for (llvm::Module::iterator fi = module->getFunctionList().begin(), fe = module->getFunctionList().end();
449 fi != fe;
450 ++fi)
451 {
452 if (fi->getName().str().find(orig_name) != std::string::npos)
453 {
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000454 mangled_name.SetCString(fi->getName().str().c_str());
Sean Callananfc55f5d2010-09-21 00:44:12 +0000455 return true;
456 }
457 }
458
459 return false;
460}
461
Sean Callanan1a8d4092010-08-27 01:01:44 +0000462Error
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000463ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_addr,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000464 lldb::addr_t &func_end,
465 ExecutionContext &exe_ctx,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000466 bool &evaluated_statically,
467 lldb::ClangExpressionVariableSP &const_result,
468 ExecutionPolicy execution_policy)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000469{
Greg Clayton22a939a2011-01-19 23:00:49 +0000470 func_addr = LLDB_INVALID_ADDRESS;
471 func_end = LLDB_INVALID_ADDRESS;
Greg Clayton5160ce52013-03-27 23:08:40 +0000472 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfc55f5d2010-09-21 00:44:12 +0000473
Greg Claytonfb9c6b72012-10-24 17:37:53 +0000474 std::auto_ptr<llvm::ExecutionEngine> execution_engine_ap;
Sean Callanan175a0d02012-01-24 22:06:48 +0000475
Sean Callanan1a8d4092010-08-27 01:01:44 +0000476 Error err;
477
Greg Claytonfb9c6b72012-10-24 17:37:53 +0000478 std::auto_ptr<llvm::Module> module_ap (m_code_generator->ReleaseModule());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000479
Greg Claytonfb9c6b72012-10-24 17:37:53 +0000480 if (!module_ap.get())
Sean Callanan1a8d4092010-08-27 01:01:44 +0000481 {
482 err.SetErrorToGenericError();
483 err.SetErrorString("IR doesn't contain a module");
484 return err;
485 }
486
Sean Callananfc55f5d2010-09-21 00:44:12 +0000487 // Find the actual name of the function (it's often mangled somehow)
488
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000489 ConstString function_name;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000490
Greg Claytonfb9c6b72012-10-24 17:37:53 +0000491 if (!FindFunctionInModule(function_name, module_ap.get(), m_expr.FunctionName()))
Sean Callananfc55f5d2010-09-21 00:44:12 +0000492 {
493 err.SetErrorToGenericError();
494 err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName());
495 return err;
496 }
497 else
498 {
Enrico Granata20edcdb2011-07-19 18:03:25 +0000499 if (log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000500 log->Printf("Found function %s for %s", function_name.AsCString(), m_expr.FunctionName());
Sean Callananfc55f5d2010-09-21 00:44:12 +0000501 }
502
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000503 m_execution_unit.reset(new IRExecutionUnit(module_ap, // handed off here
504 function_name,
505 exe_ctx.GetProcessSP(),
506 m_compiler->getTargetOpts().Features));
507
Sean Callanan1a8d4092010-08-27 01:01:44 +0000508 ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); // result can be NULL
509
510 if (decl_map)
511 {
Sean Callanan3989fb92011-01-27 01:07:04 +0000512 Stream *error_stream = NULL;
Greg Claytonc14ee322011-09-22 04:58:26 +0000513 Target *target = exe_ctx.GetTargetPtr();
514 if (target)
515 error_stream = &target->GetDebugger().GetErrorStream();
Sean Callanan3989fb92011-01-27 01:07:04 +0000516
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000517 IRForTarget ir_for_target(decl_map,
Sean Callanan9e6ed532010-09-13 21:34:21 +0000518 m_expr.NeedsVariableResolution(),
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000519 execution_policy,
Sean Callanane4ec90e2010-12-16 03:17:46 +0000520 const_result,
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 Callanan175a0d02012-01-24 22:06:48 +0000527 Error &interpreter_error(ir_for_target.getInterpreterError());
528
529 if (execution_policy != eExecutionPolicyAlways && interpreter_error.Success())
Sean Callanan63697e52011-05-07 01:06:41 +0000530 {
Johnny Chenb49440f2012-01-06 00:35:38 +0000531 if (const_result)
532 const_result->TransferAddress();
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000533 evaluated_statically = true;
Sean Callanan63697e52011-05-07 01:06:41 +0000534 err.Clear();
535 return err;
536 }
537
Greg Claytonc14ee322011-09-22 04:58:26 +0000538 Process *process = exe_ctx.GetProcessPtr();
539
540 if (!process || execution_policy == eExecutionPolicyNever)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000541 {
542 err.SetErrorToGenericError();
Sean Callanan175a0d02012-01-24 22:06:48 +0000543 if (execution_policy == eExecutionPolicyAlways)
544 err.SetErrorString("Execution needed to run in the target, but the target can't be run");
545 else
546 err.SetErrorStringWithFormat("Interpreting the expression locally failed: %s", interpreter_error.AsCString());
Sean Callanan5b26f272012-02-04 08:49:35 +0000547
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000548 return err;
549 }
Sean Callanan3d654b32012-09-24 22:25:51 +0000550 else if (!ir_can_run)
551 {
552 err.SetErrorToGenericError();
553 err.SetErrorString("The expression could not be prepared to run in the target");
554
555 return err;
556 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000557
Sean Callanan90539452011-09-20 23:01:51 +0000558 if (execution_policy != eExecutionPolicyNever &&
559 m_expr.NeedsValidation() &&
Greg Claytonc14ee322011-09-22 04:58:26 +0000560 process)
Sean Callanan6961e872010-09-01 00:58:00 +0000561 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000562 if (!process->GetDynamicCheckers())
Sean Callanan90539452011-09-20 23:01:51 +0000563 {
564 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
565
566 StreamString install_errors;
567
568 if (!dynamic_checkers->Install(install_errors, exe_ctx))
569 {
570 if (install_errors.GetString().empty())
571 err.SetErrorString ("couldn't install checkers, unknown error");
572 else
573 err.SetErrorString (install_errors.GetString().c_str());
574
575 return err;
576 }
577
Greg Claytonc14ee322011-09-22 04:58:26 +0000578 process->SetDynamicCheckers(dynamic_checkers);
Sean Callanan90539452011-09-20 23:01:51 +0000579
580 if (log)
581 log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
582 }
583
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000584 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString());
Sean Callanan9e6ed532010-09-13 21:34:21 +0000585
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000586 if (!ir_dynamic_checks.runOnModule(*m_execution_unit->GetModule()))
Sean Callanan9e6ed532010-09-13 21:34:21 +0000587 {
588 err.SetErrorToGenericError();
589 err.SetErrorString("Couldn't add dynamic checks to the expression");
590 return err;
591 }
592 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000593 }
594
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000595 m_execution_unit->GetRunnableInfo(err, func_addr, func_end);
Sean Callananea685ae2011-11-01 17:33:54 +0000596
Sean Callanan1a8d4092010-08-27 01:01:44 +0000597 return err;
598}