blob: d91fb7a443be9f39c44db1c3a252d785d5f76f22 [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
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
Sean Callanan1a8d4092010-08-27 01:01:44 +000013#include "clang/AST/ASTContext.h"
14#include "clang/AST/ExternalASTSource.h"
15#include "clang/Basic/FileManager.h"
Jim Ingham151c0322015-09-15 21:13:50 +000016#include "clang/Basic/SourceLocation.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000017#include "clang/Basic/TargetInfo.h"
18#include "clang/Basic/Version.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000019#include "clang/CodeGen/CodeGenAction.h"
20#include "clang/CodeGen/ModuleBuilder.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000021#include "clang/Frontend/CompilerInstance.h"
22#include "clang/Frontend/CompilerInvocation.h"
23#include "clang/Frontend/FrontendActions.h"
24#include "clang/Frontend/FrontendDiagnostic.h"
25#include "clang/Frontend/FrontendPluginRegistry.h"
26#include "clang/Frontend/TextDiagnosticBuffer.h"
27#include "clang/Frontend/TextDiagnosticPrinter.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000028#include "clang/Lex/Preprocessor.h"
Sean Callanane2ef6e32010-09-23 03:01:22 +000029#include "clang/Parse/ParseAST.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000030#include "clang/Rewrite/Frontend/FrontendActions.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000031#include "clang/Sema/SemaConsumer.h"
Sean Callananfb0b7582011-03-15 00:17:19 +000032#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000033
34#include "llvm/ADT/StringRef.h"
35#include "llvm/ExecutionEngine/ExecutionEngine.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000036#include "llvm/Support/Debug.h"
Rafael Espindola4609ea82013-06-26 14:10:56 +000037#include "llvm/Support/FileSystem.h"
Sean Callanan880e6802011-10-07 23:18:13 +000038#include "llvm/Support/TargetSelect.h"
Greg Clayton70b57652011-05-15 01:25:55 +000039
Ed Mastea09ed032014-03-18 18:55:06 +000040#include "llvm/ExecutionEngine/MCJIT.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000041#include "llvm/IR/LLVMContext.h"
42#include "llvm/IR/Module.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000043#include "llvm/Support/ErrorHandling.h"
44#include "llvm/Support/MemoryBuffer.h"
Greg Clayton38a61402010-12-02 23:20:03 +000045#include "llvm/Support/DynamicLibrary.h"
46#include "llvm/Support/Host.h"
47#include "llvm/Support/Signals.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000048
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000049// Project includes
50#include "ClangExpressionParser.h"
51
52#include "ClangASTSource.h"
53#include "ClangExpressionHelper.h"
54#include "ClangExpressionDeclMap.h"
55#include "ClangModulesDeclVendor.h"
56#include "ClangPersistentVariables.h"
57#include "IRForTarget.h"
58
59#include "lldb/Core/ArchSpec.h"
60#include "lldb/Core/DataBufferHeap.h"
61#include "lldb/Core/Debugger.h"
62#include "lldb/Core/Disassembler.h"
63#include "lldb/Core/Log.h"
64#include "lldb/Core/Module.h"
65#include "lldb/Core/Stream.h"
66#include "lldb/Core/StreamFile.h"
Aidan Dodds1b6785a2016-02-03 12:33:05 +000067#include "lldb/Core/StringList.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000068#include "lldb/Core/StreamString.h"
69#include "lldb/Expression/IRExecutionUnit.h"
70#include "lldb/Expression/IRDynamicChecks.h"
71#include "lldb/Expression/IRInterpreter.h"
72#include "lldb/Host/File.h"
73#include "lldb/Host/HostInfo.h"
74#include "lldb/Symbol/ClangASTContext.h"
75#include "lldb/Symbol/SymbolVendor.h"
76#include "lldb/Target/ExecutionContext.h"
77#include "lldb/Target/ObjCLanguageRuntime.h"
78#include "lldb/Target/Process.h"
79#include "lldb/Target/Target.h"
Aidan Dodds1b6785a2016-02-03 12:33:05 +000080#include "lldb/Target/Language.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000081
Sean Callanan1a8d4092010-08-27 01:01:44 +000082using namespace clang;
83using namespace llvm;
84using namespace lldb_private;
85
86//===----------------------------------------------------------------------===//
87// Utility Methods for Clang
88//===----------------------------------------------------------------------===//
89
Sean Callanan1a8d4092010-08-27 01:01:44 +000090
Sean Callananc8278af2014-12-05 01:27:35 +000091class ClangExpressionParser::LLDBPreprocessorCallbacks : public PPCallbacks
92{
Sean Callananf0c5aeb2015-04-20 16:31:29 +000093 ClangModulesDeclVendor &m_decl_vendor;
94 ClangPersistentVariables &m_persistent_vars;
95 StreamString m_error_stream;
96 bool m_has_errors = false;
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000097
Sean Callananc8278af2014-12-05 01:27:35 +000098public:
Sean Callananf0c5aeb2015-04-20 16:31:29 +000099 LLDBPreprocessorCallbacks(ClangModulesDeclVendor &decl_vendor,
100 ClangPersistentVariables &persistent_vars) :
101 m_decl_vendor(decl_vendor),
102 m_persistent_vars(persistent_vars)
Sean Callananc8278af2014-12-05 01:27:35 +0000103 {
104 }
105
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +0000106 void
107 moduleImport(SourceLocation import_location,
108 clang::ModuleIdPath path,
109 const clang::Module * /*null*/) override
Sean Callananc8278af2014-12-05 01:27:35 +0000110 {
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000111 std::vector<ConstString> string_path;
Sean Callananc8278af2014-12-05 01:27:35 +0000112
113 for (const std::pair<IdentifierInfo *, SourceLocation> &component : path)
114 {
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000115 string_path.push_back(ConstString(component.first->getName()));
Sean Callananc8278af2014-12-05 01:27:35 +0000116 }
117
118 StreamString error_stream;
119
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000120 ClangModulesDeclVendor::ModuleVector exported_modules;
121
122 if (!m_decl_vendor.AddModule(string_path, &exported_modules, m_error_stream))
Sean Callananc8278af2014-12-05 01:27:35 +0000123 {
124 m_has_errors = true;
125 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000126
127 for (ClangModulesDeclVendor::ModuleID module : exported_modules)
128 {
129 m_persistent_vars.AddHandLoadedClangModule(module);
130 }
Sean Callananc8278af2014-12-05 01:27:35 +0000131 }
132
133 bool hasErrors()
134 {
135 return m_has_errors;
136 }
137
138 const std::string &getErrorString()
139 {
140 return m_error_stream.GetString();
141 }
142};
143
Sean Callanan1a8d4092010-08-27 01:01:44 +0000144//===----------------------------------------------------------------------===//
145// Implementation of ClangExpressionParser
146//===----------------------------------------------------------------------===//
147
Greg Clayton514487e2011-02-15 21:59:32 +0000148ClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope,
Jim Ingham151c0322015-09-15 21:13:50 +0000149 Expression &expr,
Greg Clayton23f8c952014-03-24 23:10:19 +0000150 bool generate_debug_info) :
Jim Ingham151c0322015-09-15 21:13:50 +0000151 ExpressionParser (exe_scope, expr, generate_debug_info),
Sean Callanan1a8d4092010-08-27 01:01:44 +0000152 m_compiler (),
Zachary Turner22ac8712014-12-05 22:54:56 +0000153 m_code_generator (),
154 m_pp_callbacks(nullptr)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000155{
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000156 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
157
Sean Callanan1a8d4092010-08-27 01:01:44 +0000158 // 1. Create a new compiler instance.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000159 m_compiler.reset(new CompilerInstance());
Ewan Crawford7648dd32016-03-10 12:38:55 +0000160 lldb::LanguageType frame_lang = expr.Language(); // defaults to lldb::eLanguageTypeUnknown
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000161 bool overridden_target_opts = false;
162 lldb_private::LanguageRuntime *lang_rt = nullptr;
Sean Callanan3d654b32012-09-24 22:25:51 +0000163 lldb::TargetSP target_sp;
164 if (exe_scope)
165 target_sp = exe_scope->CalculateTarget();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000166
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000167 ArchSpec target_arch;
168 if (target_sp)
169 target_arch = target_sp->GetArchitecture();
170
171 const auto target_machine = target_arch.GetMachine();
172
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000173 // If the expression is being evaluated in the context of an existing
174 // stack frame, we introspect to see if the language runtime is available.
175 auto frame = exe_scope->CalculateStackFrame();
176
177 // Make sure the user hasn't provided a preferred execution language
178 // with `expression --language X -- ...`
Ewan Crawford7648dd32016-03-10 12:38:55 +0000179 if (frame && frame_lang == lldb::eLanguageTypeUnknown)
180 frame_lang = frame->GetLanguage();
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000181
Ewan Crawford7648dd32016-03-10 12:38:55 +0000182 if (frame_lang != lldb::eLanguageTypeUnknown)
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000183 {
Ewan Crawford7648dd32016-03-10 12:38:55 +0000184 lang_rt = exe_scope->CalculateProcess()->GetLanguageRuntime(frame_lang);
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000185 if (log)
Ewan Crawford7648dd32016-03-10 12:38:55 +0000186 log->Printf("Frame has language of type %s", Language::GetNameForLanguageType(frame_lang));
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000187 }
188
189 // 2. Configure the compiler with a set of default options that are appropriate
190 // for most situations.
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000191 if (target_sp && target_arch.IsValid())
Sean Callanan3d654b32012-09-24 22:25:51 +0000192 {
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000193 std::string triple = target_arch.GetTriple().str();
Sean Callanan3d654b32012-09-24 22:25:51 +0000194 m_compiler->getTargetOpts().Triple = triple;
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000195 if (log)
196 log->Printf("Using %s as the target triple", m_compiler->getTargetOpts().Triple.c_str());
Sean Callanan3d654b32012-09-24 22:25:51 +0000197 }
198 else
199 {
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000200 // If we get here we don't have a valid target and just have to guess.
201 // Sometimes this will be ok to just use the host target triple (when we evaluate say "2+3", but other
202 // expressions like breakpoint conditions and other things that _are_ target specific really shouldn't just be
203 // using the host triple. In such a case the language runtime should expose an overridden options set (3),
204 // below.
Sean Callanan3d654b32012-09-24 22:25:51 +0000205 m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000206 if (log)
207 log->Printf("Using default target triple of %s", m_compiler->getTargetOpts().Triple.c_str());
Sean Callanan3d654b32012-09-24 22:25:51 +0000208 }
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000209 // Now add some special fixes for known architectures:
Jason Molendaa3329782014-03-29 18:54:20 +0000210 // Any arm32 iOS environment, but not on arm64
Sean Callanan60400ec2014-06-23 21:00:25 +0000211 if (m_compiler->getTargetOpts().Triple.find("arm64") == std::string::npos &&
212 m_compiler->getTargetOpts().Triple.find("arm") != std::string::npos &&
213 m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos)
Jason Molendaa3329782014-03-29 18:54:20 +0000214 {
Sean Callanan3d654b32012-09-24 22:25:51 +0000215 m_compiler->getTargetOpts().ABI = "apcs-gnu";
Jason Molendaa3329782014-03-29 18:54:20 +0000216 }
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000217 // Supported subsets of x86
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000218 if (target_machine == llvm::Triple::x86 ||
219 target_machine == llvm::Triple::x86_64)
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000220 {
221 m_compiler->getTargetOpts().Features.push_back("+sse");
222 m_compiler->getTargetOpts().Features.push_back("+sse2");
223 }
Jason Molendaa3329782014-03-29 18:54:20 +0000224
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000225 // Set the target CPU to generate code for.
226 // This will be empty for any CPU that doesn't really need to make a special CPU string.
227 m_compiler->getTargetOpts().CPU = target_arch.GetClangTargetCPU();
228
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000229 // 3. Now allow the runtime to provide custom configuration options for the target.
230 // In this case, a specialized language runtime is available and we can query it for extra options.
231 // For 99% of use cases, this will not be needed and should be provided when basic platform detection is not enough.
232 if (lang_rt)
233 overridden_target_opts = lang_rt->GetOverrideExprOptions(m_compiler->getTargetOpts());
234
235 if (overridden_target_opts)
236 if (log)
237 {
238 log->Debug("Using overridden target options for the expression evaluation");
239
240 auto opts = m_compiler->getTargetOpts();
241 log->Debug("Triple: '%s'", opts.Triple.c_str());
242 log->Debug("CPU: '%s'", opts.CPU.c_str());
243 log->Debug("FPMath: '%s'", opts.FPMath.c_str());
244 log->Debug("ABI: '%s'", opts.ABI.c_str());
245 log->Debug("LinkerVersion: '%s'", opts.LinkerVersion.c_str());
246 StringList::LogDump(log, opts.FeaturesAsWritten, "FeaturesAsWritten");
247 StringList::LogDump(log, opts.Features, "Features");
248 StringList::LogDump(log, opts.Reciprocals, "Reciprocals");
249 }
250
251 // 4. Create and install the target on the compiler.
Sean Callananb1de8dd2013-01-22 02:20:20 +0000252 m_compiler->createDiagnostics();
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000253 auto target_info = TargetInfo::CreateTargetInfo(m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts);
254 if (log)
255 {
256 log->Printf("Using SIMD alignment: %d", target_info->getSimdDefaultAlign());
James Y Knightdc395b92016-03-04 19:30:53 +0000257 log->Printf("Target datalayout string: '%s'", target_info->getDataLayout().getStringRepresentation().c_str());
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000258 log->Printf("Target ABI: '%s'", target_info->getABI().str().c_str());
259 log->Printf("Target vector alignment: %d", target_info->getMaxVectorAlign());
260 }
261 m_compiler->setTarget(target_info);
Alp Toker5f838642014-07-06 05:36:57 +0000262
Sean Callanan3d654b32012-09-24 22:25:51 +0000263 assert (m_compiler->hasTarget());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000264
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000265 // 5. Set language options.
Ewan Crawford7648dd32016-03-10 12:38:55 +0000266 lldb::LanguageType language = expr.Language();
267
268 switch (language)
Sean Callananc7b65062011-11-07 23:35:40 +0000269 {
270 case lldb::eLanguageTypeC:
Dawn Perchik009d1102015-09-04 01:02:30 +0000271 case lldb::eLanguageTypeC89:
272 case lldb::eLanguageTypeC99:
273 case lldb::eLanguageTypeC11:
274 // FIXME: the following language option is a temporary workaround,
275 // to "ask for C, get C++."
276 // For now, the expression parser must use C++ anytime the
277 // language is a C family language, because the expression parser
278 // uses features of C++ to capture values.
279 m_compiler->getLangOpts().CPlusPlus = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000280 break;
281 case lldb::eLanguageTypeObjC:
282 m_compiler->getLangOpts().ObjC1 = true;
283 m_compiler->getLangOpts().ObjC2 = true;
Dawn Perchik009d1102015-09-04 01:02:30 +0000284 // FIXME: the following language option is a temporary workaround,
285 // to "ask for ObjC, get ObjC++" (see comment above).
286 m_compiler->getLangOpts().CPlusPlus = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000287 break;
288 case lldb::eLanguageTypeC_plus_plus:
Dawn Perchik009d1102015-09-04 01:02:30 +0000289 case lldb::eLanguageTypeC_plus_plus_11:
290 case lldb::eLanguageTypeC_plus_plus_14:
Chandler Carruth38336a12013-01-02 12:55:00 +0000291 m_compiler->getLangOpts().CPlusPlus11 = true;
Jim Ingham26c7bf92014-10-11 00:38:27 +0000292 m_compiler->getHeaderSearchOpts().UseLibcxx = true;
Jason Molenda62e06812016-02-16 04:14:33 +0000293 LLVM_FALLTHROUGH;
Dawn Perchik009d1102015-09-04 01:02:30 +0000294 case lldb::eLanguageTypeC_plus_plus_03:
295 m_compiler->getLangOpts().CPlusPlus = true;
296 // FIXME: the following language option is a temporary workaround,
297 // to "ask for C++, get ObjC++". Apple hopes to remove this requirement
298 // on non-Apple platforms, but for now it is needed.
299 m_compiler->getLangOpts().ObjC1 = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000300 break;
301 case lldb::eLanguageTypeObjC_plus_plus:
Dawn Perchik009d1102015-09-04 01:02:30 +0000302 case lldb::eLanguageTypeUnknown:
Sean Callananc7b65062011-11-07 23:35:40 +0000303 default:
304 m_compiler->getLangOpts().ObjC1 = true;
305 m_compiler->getLangOpts().ObjC2 = true;
306 m_compiler->getLangOpts().CPlusPlus = true;
Chandler Carruth38336a12013-01-02 12:55:00 +0000307 m_compiler->getLangOpts().CPlusPlus11 = true;
Jim Ingham26c7bf92014-10-11 00:38:27 +0000308 m_compiler->getHeaderSearchOpts().UseLibcxx = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000309 break;
310 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000311
Sean Callananaa0f9cb2012-10-17 22:09:59 +0000312 m_compiler->getLangOpts().Bool = true;
313 m_compiler->getLangOpts().WChar = true;
Sean Callananeeb43842013-04-01 22:12:37 +0000314 m_compiler->getLangOpts().Blocks = true;
Sean Callanan226b70c2012-03-08 02:39:03 +0000315 m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients
Jim Ingham151c0322015-09-15 21:13:50 +0000316 if (expr.DesiredResultType() == Expression::eResultTypeId)
Sean Callanan226b70c2012-03-08 02:39:03 +0000317 m_compiler->getLangOpts().DebuggerCastResultToId = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000318
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000319 m_compiler->getLangOpts().CharIsSigned =
320 ArchSpec(m_compiler->getTargetOpts().Triple.c_str()).CharIsSignedByDefault();
321
Sean Callanan0765c822012-04-17 00:49:48 +0000322 // Spell checking is a nice feature, but it ends up completing a
323 // lot of types that we didn't strictly speaking need to complete.
324 // As a result, we spend a long time parsing and importing debug
325 // information.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000326 m_compiler->getLangOpts().SpellChecking = false;
327
Greg Claytond9e416c2012-02-18 05:35:26 +0000328 lldb::ProcessSP process_sp;
Greg Clayton514487e2011-02-15 21:59:32 +0000329 if (exe_scope)
Greg Claytond9e416c2012-02-18 05:35:26 +0000330 process_sp = exe_scope->CalculateProcess();
Greg Clayton514487e2011-02-15 21:59:32 +0000331
Greg Claytond9e416c2012-02-18 05:35:26 +0000332 if (process_sp && m_compiler->getLangOpts().ObjC1)
Sean Callananc3a16002011-01-17 23:42:46 +0000333 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000334 if (process_sp->GetObjCLanguageRuntime())
Sean Callananc3a16002011-01-17 23:42:46 +0000335 {
Enrico Granata15a67f492015-09-23 20:12:19 +0000336 if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == ObjCLanguageRuntime::ObjCRuntimeVersions::eAppleObjC_V2)
Sean Callanan3d654b32012-09-24 22:25:51 +0000337 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7));
338 else
339 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000340
Sean Callanan226b70c2012-03-08 02:39:03 +0000341 if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
Sean Callanan226b70c2012-03-08 02:39:03 +0000342 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
Sean Callananc3a16002011-01-17 23:42:46 +0000343 }
344 }
Greg Claytonf83f32d2011-01-15 01:32:14 +0000345
Sean Callanan1a8d4092010-08-27 01:01:44 +0000346 m_compiler->getLangOpts().ThreadsafeStatics = false;
347 m_compiler->getLangOpts().AccessControl = false; // Debuggers get universal access
348 m_compiler->getLangOpts().DollarIdents = true; // $ indicates a persistent variable name
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000349
Sean Callanan1a8d4092010-08-27 01:01:44 +0000350 // Set CodeGen options
351 m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
352 m_compiler->getCodeGenOpts().InstrumentFunctions = false;
Greg Claytonbc3122e2013-11-04 19:50:49 +0000353 m_compiler->getCodeGenOpts().DisableFPElim = true;
354 m_compiler->getCodeGenOpts().OmitLeafFramePointer = false;
Greg Clayton23f8c952014-03-24 23:10:19 +0000355 if (generate_debug_info)
Pavel Labathe03bd652016-02-02 13:07:27 +0000356 m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::FullDebugInfo);
Greg Clayton23f8c952014-03-24 23:10:19 +0000357 else
Pavel Labathe03bd652016-02-02 13:07:27 +0000358 m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::NoDebugInfo);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000359
Sean Callanan1a8d4092010-08-27 01:01:44 +0000360 // Disable some warnings.
Hafiz Abid Qadeer101e4902014-08-07 12:54:20 +0000361 m_compiler->getDiagnostics().setSeverityForGroup(clang::diag::Flavor::WarningOrError,
Alp Toker10933d12014-06-12 11:14:32 +0000362 "unused-value", clang::diag::Severity::Ignored, SourceLocation());
Hafiz Abid Qadeer101e4902014-08-07 12:54:20 +0000363 m_compiler->getDiagnostics().setSeverityForGroup(clang::diag::Flavor::WarningOrError,
Alp Toker10933d12014-06-12 11:14:32 +0000364 "odr", clang::diag::Severity::Ignored, SourceLocation());
365
Sean Callanan1a8d4092010-08-27 01:01:44 +0000366 // Inform the target of the language options
367 //
368 // FIXME: We shouldn't need to do this, the target should be immutable once
369 // created. This complexity should be lifted elsewhere.
Sylvestre Ledru3cc9d632014-07-06 17:50:36 +0000370 m_compiler->getTarget().adjust(m_compiler->getLangOpts());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000371
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000372 // 6. Set up the diagnostic buffer for reporting errors
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000373
Sean Callanane2ef6e32010-09-23 03:01:22 +0000374 m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000375
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000376 // 7. Set up the source management objects inside the compiler
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000377
Greg Clayton38a61402010-12-02 23:20:03 +0000378 clang::FileSystemOptions file_system_options;
379 m_file_manager.reset(new clang::FileManager(file_system_options));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000380
Sean Callanan1a8d4092010-08-27 01:01:44 +0000381 if (!m_compiler->hasSourceManager())
Greg Clayton38a61402010-12-02 23:20:03 +0000382 m_compiler->createSourceManager(*m_file_manager.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000383
Sean Callanan1a8d4092010-08-27 01:01:44 +0000384 m_compiler->createFileManager();
Ben Langmuir9f0bac52014-03-07 08:31:36 +0000385 m_compiler->createPreprocessor(TU_Complete);
Sean Callananc8278af2014-12-05 01:27:35 +0000386
387 if (ClangModulesDeclVendor *decl_vendor = target_sp->GetClangModulesDeclVendor())
388 {
Sean Callananb92bd752015-10-01 16:28:02 +0000389 ClangPersistentVariables *clang_persistent_vars = llvm::cast<ClangPersistentVariables>(target_sp->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC));
Sean Callanan8f1f9a12015-09-30 19:57:57 +0000390 std::unique_ptr<PPCallbacks> pp_callbacks(new LLDBPreprocessorCallbacks(*decl_vendor, *clang_persistent_vars));
Sean Callananc8278af2014-12-05 01:27:35 +0000391 m_pp_callbacks = static_cast<LLDBPreprocessorCallbacks*>(pp_callbacks.get());
392 m_compiler->getPreprocessor().addPPCallbacks(std::move(pp_callbacks));
393 }
394
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000395 // 8. Most of this we get from the CompilerInstance, but we
Sean Callanan1a8d4092010-08-27 01:01:44 +0000396 // also want to give the context an ExternalASTSource.
Sean Callanan6abfabf2010-11-19 20:20:02 +0000397 m_selector_table.reset(new SelectorTable());
Sean Callanan880e6802011-10-07 23:18:13 +0000398 m_builtin_context.reset(new Builtin::Context());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000399
Greg Clayton7b0992d2013-04-18 22:45:39 +0000400 std::unique_ptr<clang::ASTContext> ast_context(new ASTContext(m_compiler->getLangOpts(),
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000401 m_compiler->getSourceManager(),
402 m_compiler->getPreprocessor().getIdentifierTable(),
403 *m_selector_table.get(),
404 *m_builtin_context.get()));
Sean Callananc8278af2014-12-05 01:27:35 +0000405
Alp Tokercf55e882014-05-03 15:05:45 +0000406 ast_context->InitBuiltinTypes(m_compiler->getTarget());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000407
Jim Ingham151c0322015-09-15 21:13:50 +0000408 ClangExpressionHelper *type_system_helper = dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
409 ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000410
Sean Callanan1a8d4092010-08-27 01:01:44 +0000411 if (decl_map)
412 {
Sylvestre Ledru7ba631f2014-02-27 10:46:57 +0000413 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy());
Sean Callananeddeb3b2011-10-28 23:38:38 +0000414 decl_map->InstallASTContext(ast_context.get());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000415 ast_context->setExternalSource(ast_source);
416 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000417
Greg Claytond8d4a572015-08-11 21:38:15 +0000418 m_ast_context.reset(new ClangASTContext(m_compiler->getTargetOpts().Triple.c_str()));
419 m_ast_context->setASTContext(ast_context.get());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000420 m_compiler->setASTContext(ast_context.release());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000421
Greg Clayton7b462cc2010-10-15 22:48:33 +0000422 std::string module_name("$__lldb_module");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000423
Sean Callananfb0b7582011-03-15 00:17:19 +0000424 m_llvm_context.reset(new LLVMContext());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000425 m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(),
426 module_name,
Tamas Berghammer0b736f12015-06-30 09:26:52 +0000427 m_compiler->getHeaderSearchOpts(),
428 m_compiler->getPreprocessorOpts(),
Sean Callanan1a8d4092010-08-27 01:01:44 +0000429 m_compiler->getCodeGenOpts(),
Sean Callananfb0b7582011-03-15 00:17:19 +0000430 *m_llvm_context));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000431}
432
433ClangExpressionParser::~ClangExpressionParser()
434{
435}
436
437unsigned
Ewan Crawford615a8072016-02-19 17:55:10 +0000438ClangExpressionParser::Parse(Stream &stream)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000439{
Ewan Crawford615a8072016-02-19 17:55:10 +0000440 TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer *>(m_compiler->getDiagnostics().getClient());
441 diag_buf->FlushDiagnostics(m_compiler->getDiagnostics());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000442
Greg Clayton23f8c952014-03-24 23:10:19 +0000443 const char *expr_text = m_expr.Text();
Rafael Espindola9cb97b62014-05-21 15:08:27 +0000444
Ewan Crawford615a8072016-02-19 17:55:10 +0000445 clang::SourceManager &source_mgr = m_compiler->getSourceManager();
Greg Clayton23f8c952014-03-24 23:10:19 +0000446 bool created_main_file = false;
Pavel Labathe03bd652016-02-02 13:07:27 +0000447 if (m_compiler->getCodeGenOpts().getDebugInfo() == codegenoptions::FullDebugInfo)
Greg Clayton23f8c952014-03-24 23:10:19 +0000448 {
Zachary Turnercb5742b2014-10-20 17:46:56 +0000449 int temp_fd = -1;
450 llvm::SmallString<PATH_MAX> result_path;
Greg Clayton23f8c952014-03-24 23:10:19 +0000451 FileSpec tmpdir_file_spec;
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000452 if (HostInfo::GetLLDBPath(lldb::ePathTypeLLDBTempSystemDir, tmpdir_file_spec))
Greg Clayton23f8c952014-03-24 23:10:19 +0000453 {
Zachary Turnercb5742b2014-10-20 17:46:56 +0000454 tmpdir_file_spec.AppendPathComponent("lldb-%%%%%%.expr");
Ewan Crawford615a8072016-02-19 17:55:10 +0000455 std::string temp_source_path = tmpdir_file_spec.GetPath();
Zachary Turnercb5742b2014-10-20 17:46:56 +0000456 llvm::sys::fs::createUniqueFile(temp_source_path, temp_fd, result_path);
Greg Clayton23f8c952014-03-24 23:10:19 +0000457 }
458 else
459 {
Zachary Turnercb5742b2014-10-20 17:46:56 +0000460 llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path);
Greg Clayton23f8c952014-03-24 23:10:19 +0000461 }
Ewan Crawford615a8072016-02-19 17:55:10 +0000462
Jason Molendabc464ee2014-10-16 23:10:03 +0000463 if (temp_fd != -1)
Greg Clayton23f8c952014-03-24 23:10:19 +0000464 {
Ewan Crawford615a8072016-02-19 17:55:10 +0000465 lldb_private::File file(temp_fd, true);
Greg Clayton23f8c952014-03-24 23:10:19 +0000466 const size_t expr_text_len = strlen(expr_text);
467 size_t bytes_written = expr_text_len;
468 if (file.Write(expr_text, bytes_written).Success())
469 {
470 if (bytes_written == expr_text_len)
471 {
472 file.Close();
Ewan Crawford615a8072016-02-19 17:55:10 +0000473 source_mgr.setMainFileID(source_mgr.createFileID(m_file_manager->getFile(result_path),
474 SourceLocation(), SrcMgr::C_User));
Greg Clayton23f8c952014-03-24 23:10:19 +0000475 created_main_file = true;
476 }
477 }
478 }
479 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000480
Greg Clayton23f8c952014-03-24 23:10:19 +0000481 if (!created_main_file)
482 {
Rafael Espindola2443e702014-08-27 20:09:08 +0000483 std::unique_ptr<MemoryBuffer> memory_buffer = MemoryBuffer::getMemBufferCopy(expr_text, __FUNCTION__);
Ewan Crawford615a8072016-02-19 17:55:10 +0000484 source_mgr.setMainFileID(source_mgr.createFileID(std::move(memory_buffer)));
Greg Clayton23f8c952014-03-24 23:10:19 +0000485 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000486
Sean Callanane2ef6e32010-09-23 03:01:22 +0000487 diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000488
Jim Ingham151c0322015-09-15 21:13:50 +0000489 ClangExpressionHelper *type_system_helper = dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000490
Jim Ingham151c0322015-09-15 21:13:50 +0000491 ASTConsumer *ast_transformer = type_system_helper->ASTTransformer(m_code_generator.get());
492
493 if (ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap())
Sean Callanan80c97592015-05-01 00:47:29 +0000494 decl_map->InstallCodeGenerator(m_code_generator.get());
Chaoren Lin57998de2015-08-19 01:24:57 +0000495
Sean Callanan1a8d4092010-08-27 01:01:44 +0000496 if (ast_transformer)
Chaoren Lin57998de2015-08-19 01:24:57 +0000497 {
498 ast_transformer->Initialize(m_compiler->getASTContext());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000499 ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext());
Chaoren Lin57998de2015-08-19 01:24:57 +0000500 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000501 else
Chaoren Lin57998de2015-08-19 01:24:57 +0000502 {
503 m_code_generator->Initialize(m_compiler->getASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000504 ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext());
Chaoren Lin57998de2015-08-19 01:24:57 +0000505 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000506
Sean Callanane2ef6e32010-09-23 03:01:22 +0000507 diag_buf->EndSourceFile();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000508
Ewan Crawford615a8072016-02-19 17:55:10 +0000509 unsigned num_errors = diag_buf->getNumErrors();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000510
Zachary Turner22ac8712014-12-05 22:54:56 +0000511 if (m_pp_callbacks && m_pp_callbacks->hasErrors())
Sean Callananc8278af2014-12-05 01:27:35 +0000512 {
513 num_errors++;
Sean Callananc8278af2014-12-05 01:27:35 +0000514 stream.PutCString(m_pp_callbacks->getErrorString().c_str());
515 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000516
Ewan Crawford615a8072016-02-19 17:55:10 +0000517 for (TextDiagnosticBuffer::const_iterator warn = diag_buf->warn_begin(), warn_end = diag_buf->warn_end();
518 warn != warn_end; ++warn)
519 stream.Printf("warning: %s\n", warn->second.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000520
Ewan Crawford615a8072016-02-19 17:55:10 +0000521 for (TextDiagnosticBuffer::const_iterator err = diag_buf->err_begin(), err_end = diag_buf->err_end();
522 err != err_end; ++err)
523 stream.Printf("error: %s\n", err->second.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000524
Ewan Crawford615a8072016-02-19 17:55:10 +0000525 for (TextDiagnosticBuffer::const_iterator note = diag_buf->note_begin(), note_end = diag_buf->note_end();
526 note != note_end; ++note)
527 stream.Printf("note: %s\n", note->second.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000528
Sean Callanan77502262011-05-12 23:54:16 +0000529 if (!num_errors)
530 {
Jim Ingham151c0322015-09-15 21:13:50 +0000531 if (type_system_helper->DeclMap() && !type_system_helper->DeclMap()->ResolveUnknownTypes())
Sean Callanan77502262011-05-12 23:54:16 +0000532 {
533 stream.Printf("error: Couldn't infer the type of a variable\n");
534 num_errors++;
535 }
536 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000537
Sean Callanan1a8d4092010-08-27 01:01:44 +0000538 return num_errors;
539}
540
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000541static bool FindFunctionInModule (ConstString &mangled_name,
Sean Callananfc55f5d2010-09-21 00:44:12 +0000542 llvm::Module *module,
543 const char *orig_name)
544{
Aidan Doddsb1298472016-02-25 13:07:04 +0000545 for (const auto &func : module->getFunctionList())
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000546 {
Aidan Doddsb1298472016-02-25 13:07:04 +0000547 const StringRef &name = func.getName();
548 if (name.find(orig_name) != StringRef::npos)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000549 {
Aidan Doddsb1298472016-02-25 13:07:04 +0000550 mangled_name.SetString(name);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000551 return true;
552 }
553 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000554
Sean Callananfc55f5d2010-09-21 00:44:12 +0000555 return false;
556}
557
Sean Callanan1a8d4092010-08-27 01:01:44 +0000558Error
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000559ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_addr,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000560 lldb::addr_t &func_end,
Jim Inghamc29e6982015-11-05 01:50:42 +0000561 lldb::IRExecutionUnitSP &execution_unit_sp,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000562 ExecutionContext &exe_ctx,
Sean Callanan1582ee62013-04-18 22:06:33 +0000563 bool &can_interpret,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000564 ExecutionPolicy execution_policy)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000565{
Greg Clayton22a939a2011-01-19 23:00:49 +0000566 func_addr = LLDB_INVALID_ADDRESS;
567 func_end = LLDB_INVALID_ADDRESS;
Greg Clayton5160ce52013-03-27 23:08:40 +0000568 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfc55f5d2010-09-21 00:44:12 +0000569
Sean Callanan1a8d4092010-08-27 01:01:44 +0000570 Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000571
Greg Clayton23f8c952014-03-24 23:10:19 +0000572 std::unique_ptr<llvm::Module> llvm_module_ap (m_code_generator->ReleaseModule());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000573
Greg Clayton23f8c952014-03-24 23:10:19 +0000574 if (!llvm_module_ap.get())
Sean Callanan1a8d4092010-08-27 01:01:44 +0000575 {
576 err.SetErrorToGenericError();
577 err.SetErrorString("IR doesn't contain a module");
578 return err;
579 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000580
Sean Callananfc55f5d2010-09-21 00:44:12 +0000581 // Find the actual name of the function (it's often mangled somehow)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000582
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000583 ConstString function_name;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000584
Greg Clayton23f8c952014-03-24 23:10:19 +0000585 if (!FindFunctionInModule(function_name, llvm_module_ap.get(), m_expr.FunctionName()))
Sean Callananfc55f5d2010-09-21 00:44:12 +0000586 {
587 err.SetErrorToGenericError();
588 err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName());
589 return err;
590 }
591 else
592 {
Enrico Granata20edcdb2011-07-19 18:03:25 +0000593 if (log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000594 log->Printf("Found function %s for %s", function_name.AsCString(), m_expr.FunctionName());
Sean Callananfc55f5d2010-09-21 00:44:12 +0000595 }
Sean Callananb2814802016-02-12 21:11:25 +0000596
597 SymbolContext sc;
598
599 if (lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP())
600 {
601 sc = frame_sp->GetSymbolContext(lldb::eSymbolContextEverything);
602 }
603 else if (lldb::TargetSP target_sp = exe_ctx.GetTargetSP())
604 {
605 sc.target_sp = target_sp;
606 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000607
Greg Clayton23f8c952014-03-24 23:10:19 +0000608 execution_unit_sp.reset(new IRExecutionUnit (m_llvm_context, // handed off here
609 llvm_module_ap, // handed off here
610 function_name,
611 exe_ctx.GetTargetSP(),
Sean Callananb2814802016-02-12 21:11:25 +0000612 sc,
Greg Clayton23f8c952014-03-24 23:10:19 +0000613 m_compiler->getTargetOpts().Features));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000614
Jim Ingham151c0322015-09-15 21:13:50 +0000615 ClangExpressionHelper *type_system_helper = dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
616 ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap(); // result can be NULL
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000617
Sean Callanan1a8d4092010-08-27 01:01:44 +0000618 if (decl_map)
619 {
Sean Callanan3989fb92011-01-27 01:07:04 +0000620 Stream *error_stream = NULL;
Greg Claytonc14ee322011-09-22 04:58:26 +0000621 Target *target = exe_ctx.GetTargetPtr();
622 if (target)
Greg Clayton44d93782014-01-27 23:43:24 +0000623 error_stream = target->GetDebugger().GetErrorFile().get();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000624
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000625 IRForTarget ir_for_target(decl_map,
Sean Callanan9e6ed532010-09-13 21:34:21 +0000626 m_expr.NeedsVariableResolution(),
Greg Clayton23f8c952014-03-24 23:10:19 +0000627 *execution_unit_sp,
Sean Callanan3989fb92011-01-27 01:07:04 +0000628 error_stream,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000629 function_name.AsCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000630
Greg Clayton23f8c952014-03-24 23:10:19 +0000631 bool ir_can_run = ir_for_target.runOnModule(*execution_unit_sp->GetModule());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000632
Sean Callanan1582ee62013-04-18 22:06:33 +0000633 Error interpret_error;
Greg Claytonc14ee322011-09-22 04:58:26 +0000634 Process *process = exe_ctx.GetProcessPtr();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000635
Ewan Crawford90ff7912015-07-14 10:56:58 +0000636 bool interpret_function_calls = !process ? false : process->CanInterpretFunctionCalls();
637 can_interpret = IRInterpreter::CanInterpret(*execution_unit_sp->GetModule(), *execution_unit_sp->GetFunction(), interpret_error, interpret_function_calls);
638
639
Sean Callanan1582ee62013-04-18 22:06:33 +0000640 if (!ir_can_run)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000641 {
Sean Callanan3d654b32012-09-24 22:25:51 +0000642 err.SetErrorString("The expression could not be prepared to run in the target");
Sean Callanan3d654b32012-09-24 22:25:51 +0000643 return err;
644 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000645
Sean Callanan1582ee62013-04-18 22:06:33 +0000646 if (!can_interpret && execution_policy == eExecutionPolicyNever)
Sean Callanan6961e872010-09-01 00:58:00 +0000647 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000648 err.SetErrorStringWithFormat("Can't run the expression locally: %s", interpret_error.AsCString());
649 return err;
650 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000651
Sean Callanan1582ee62013-04-18 22:06:33 +0000652 if (!process && execution_policy == eExecutionPolicyAlways)
653 {
654 err.SetErrorString("Expression needed to run in the target, but the target can't be run");
655 return err;
656 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000657
Sean Callanan1582ee62013-04-18 22:06:33 +0000658 if (execution_policy == eExecutionPolicyAlways || !can_interpret)
659 {
Sean Callanane8cde682013-05-18 00:38:20 +0000660 if (m_expr.NeedsValidation() && process)
661 {
662 if (!process->GetDynamicCheckers())
Sean Callanan90539452011-09-20 23:01:51 +0000663 {
Sean Callanane8cde682013-05-18 00:38:20 +0000664 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000665
Sean Callanane8cde682013-05-18 00:38:20 +0000666 StreamString install_errors;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000667
Sean Callanane8cde682013-05-18 00:38:20 +0000668 if (!dynamic_checkers->Install(install_errors, exe_ctx))
669 {
670 if (install_errors.GetString().empty())
671 err.SetErrorString ("couldn't install checkers, unknown error");
672 else
673 err.SetErrorString (install_errors.GetString().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000674
Sean Callanane8cde682013-05-18 00:38:20 +0000675 return err;
676 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000677
Sean Callanane8cde682013-05-18 00:38:20 +0000678 process->SetDynamicCheckers(dynamic_checkers);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000679
Sean Callanane8cde682013-05-18 00:38:20 +0000680 if (log)
681 log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
Sean Callanan90539452011-09-20 23:01:51 +0000682 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000683
Sean Callanan1582ee62013-04-18 22:06:33 +0000684 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000685
Greg Clayton23f8c952014-03-24 23:10:19 +0000686 if (!ir_dynamic_checks.runOnModule(*execution_unit_sp->GetModule()))
Sean Callanan1582ee62013-04-18 22:06:33 +0000687 {
688 err.SetErrorToGenericError();
689 err.SetErrorString("Couldn't add dynamic checks to the expression");
690 return err;
691 }
Sean Callanan90539452011-09-20 23:01:51 +0000692 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000693
Greg Clayton23f8c952014-03-24 23:10:19 +0000694 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
Sean Callanan9e6ed532010-09-13 21:34:21 +0000695 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000696 }
Sean Callanan1582ee62013-04-18 22:06:33 +0000697 else
698 {
Greg Clayton23f8c952014-03-24 23:10:19 +0000699 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
Sean Callanan1582ee62013-04-18 22:06:33 +0000700 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000701
Sean Callanan1a8d4092010-08-27 01:01:44 +0000702 return err;
703}