blob: f372c23d8c52129b37abf7596d0b5ed72fc4526a [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"
Sean Callanan579e70c2016-03-19 00:03:59 +000014#include "clang/AST/ASTDiagnostic.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000015#include "clang/AST/ExternalASTSource.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000016#include "clang/Basic/DiagnosticIDs.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000017#include "clang/Basic/FileManager.h"
Jim Ingham151c0322015-09-15 21:13:50 +000018#include "clang/Basic/SourceLocation.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000019#include "clang/Basic/TargetInfo.h"
20#include "clang/Basic/Version.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000021#include "clang/CodeGen/CodeGenAction.h"
22#include "clang/CodeGen/ModuleBuilder.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000023#include "clang/Frontend/CompilerInstance.h"
24#include "clang/Frontend/CompilerInvocation.h"
25#include "clang/Frontend/FrontendActions.h"
26#include "clang/Frontend/FrontendDiagnostic.h"
27#include "clang/Frontend/FrontendPluginRegistry.h"
28#include "clang/Frontend/TextDiagnosticBuffer.h"
29#include "clang/Frontend/TextDiagnosticPrinter.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000030#include "clang/Lex/Preprocessor.h"
Sean Callanane2ef6e32010-09-23 03:01:22 +000031#include "clang/Parse/ParseAST.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000032#include "clang/Rewrite/Frontend/FrontendActions.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000033#include "clang/Sema/SemaConsumer.h"
Sean Callananfb0b7582011-03-15 00:17:19 +000034#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000035
36#include "llvm/ADT/StringRef.h"
37#include "llvm/ExecutionEngine/ExecutionEngine.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000038#include "llvm/Support/Debug.h"
Rafael Espindola4609ea82013-06-26 14:10:56 +000039#include "llvm/Support/FileSystem.h"
Sean Callanan880e6802011-10-07 23:18:13 +000040#include "llvm/Support/TargetSelect.h"
Greg Clayton70b57652011-05-15 01:25:55 +000041
Greg Claytonc9a078a2016-03-24 23:50:03 +000042#pragma clang diagnostic push
43#pragma clang diagnostic ignored "-Wglobal-constructors"
Ed Mastea09ed032014-03-18 18:55:06 +000044#include "llvm/ExecutionEngine/MCJIT.h"
Greg Claytonc9a078a2016-03-24 23:50:03 +000045#pragma clang diagnostic pop
46
Chandler Carruth1e157582013-01-02 12:20:07 +000047#include "llvm/IR/LLVMContext.h"
48#include "llvm/IR/Module.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000049#include "llvm/Support/ErrorHandling.h"
50#include "llvm/Support/MemoryBuffer.h"
Greg Clayton38a61402010-12-02 23:20:03 +000051#include "llvm/Support/DynamicLibrary.h"
52#include "llvm/Support/Host.h"
53#include "llvm/Support/Signals.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000054
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000055// Project includes
56#include "ClangExpressionParser.h"
57
58#include "ClangASTSource.h"
59#include "ClangExpressionHelper.h"
60#include "ClangExpressionDeclMap.h"
61#include "ClangModulesDeclVendor.h"
62#include "ClangPersistentVariables.h"
63#include "IRForTarget.h"
64
65#include "lldb/Core/ArchSpec.h"
66#include "lldb/Core/DataBufferHeap.h"
67#include "lldb/Core/Debugger.h"
68#include "lldb/Core/Disassembler.h"
69#include "lldb/Core/Log.h"
70#include "lldb/Core/Module.h"
71#include "lldb/Core/Stream.h"
72#include "lldb/Core/StreamFile.h"
73#include "lldb/Core/StreamString.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000074#include "lldb/Core/StringList.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000075#include "lldb/Expression/IRDynamicChecks.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000076#include "lldb/Expression/IRExecutionUnit.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000077#include "lldb/Expression/IRInterpreter.h"
78#include "lldb/Host/File.h"
79#include "lldb/Host/HostInfo.h"
80#include "lldb/Symbol/ClangASTContext.h"
81#include "lldb/Symbol/SymbolVendor.h"
82#include "lldb/Target/ExecutionContext.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000083#include "lldb/Target/Language.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000084#include "lldb/Target/ObjCLanguageRuntime.h"
85#include "lldb/Target/Process.h"
86#include "lldb/Target/Target.h"
Sean Callananbd4dc692016-03-21 22:23:38 +000087#include "lldb/Target/ThreadPlanCallFunction.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000088#include "lldb/Utility/LLDBAssert.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000089
Sean Callanan1a8d4092010-08-27 01:01:44 +000090using namespace clang;
91using namespace llvm;
92using namespace lldb_private;
93
94//===----------------------------------------------------------------------===//
95// Utility Methods for Clang
96//===----------------------------------------------------------------------===//
97
Sean Callanan1a8d4092010-08-27 01:01:44 +000098
Sean Callananc8278af2014-12-05 01:27:35 +000099class ClangExpressionParser::LLDBPreprocessorCallbacks : public PPCallbacks
100{
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000101 ClangModulesDeclVendor &m_decl_vendor;
102 ClangPersistentVariables &m_persistent_vars;
103 StreamString m_error_stream;
104 bool m_has_errors = false;
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +0000105
Sean Callananc8278af2014-12-05 01:27:35 +0000106public:
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000107 LLDBPreprocessorCallbacks(ClangModulesDeclVendor &decl_vendor,
108 ClangPersistentVariables &persistent_vars) :
109 m_decl_vendor(decl_vendor),
110 m_persistent_vars(persistent_vars)
Sean Callananc8278af2014-12-05 01:27:35 +0000111 {
112 }
113
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +0000114 void
115 moduleImport(SourceLocation import_location,
116 clang::ModuleIdPath path,
117 const clang::Module * /*null*/) override
Sean Callananc8278af2014-12-05 01:27:35 +0000118 {
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000119 std::vector<ConstString> string_path;
Sean Callananc8278af2014-12-05 01:27:35 +0000120
121 for (const std::pair<IdentifierInfo *, SourceLocation> &component : path)
122 {
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000123 string_path.push_back(ConstString(component.first->getName()));
Sean Callananc8278af2014-12-05 01:27:35 +0000124 }
125
126 StreamString error_stream;
127
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000128 ClangModulesDeclVendor::ModuleVector exported_modules;
129
130 if (!m_decl_vendor.AddModule(string_path, &exported_modules, m_error_stream))
Sean Callananc8278af2014-12-05 01:27:35 +0000131 {
132 m_has_errors = true;
133 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000134
135 for (ClangModulesDeclVendor::ModuleID module : exported_modules)
136 {
137 m_persistent_vars.AddHandLoadedClangModule(module);
138 }
Sean Callananc8278af2014-12-05 01:27:35 +0000139 }
140
141 bool hasErrors()
142 {
143 return m_has_errors;
144 }
145
146 const std::string &getErrorString()
147 {
148 return m_error_stream.GetString();
149 }
150};
151
Sean Callanan579e70c2016-03-19 00:03:59 +0000152class ClangDiagnosticManagerAdapter : public clang::DiagnosticConsumer
153{
154public:
155 ClangDiagnosticManagerAdapter() : m_passthrough(new clang::TextDiagnosticBuffer) {}
156
157 ClangDiagnosticManagerAdapter(const std::shared_ptr<clang::TextDiagnosticBuffer> &passthrough)
158 : m_passthrough(passthrough)
159 {
160 }
161
162 void
163 ResetManager(DiagnosticManager *manager = nullptr)
164 {
165 m_manager = manager;
166 }
167
168 void
169 HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info)
170 {
171 if (m_manager)
172 {
173 llvm::SmallVector<char, 32> diag_str;
174 Info.FormatDiagnostic(diag_str);
175 diag_str.push_back('\0');
176 const char *data = diag_str.data();
177
178 switch (DiagLevel)
179 {
180 case DiagnosticsEngine::Level::Fatal:
181 case DiagnosticsEngine::Level::Error:
182 m_manager->AddDiagnostic(data, eDiagnosticSeverityError, eDiagnosticOriginClang, Info.getID());
183 break;
184 case DiagnosticsEngine::Level::Warning:
185 m_manager->AddDiagnostic(data, eDiagnosticSeverityWarning, eDiagnosticOriginClang, Info.getID());
186 break;
187 case DiagnosticsEngine::Level::Remark:
188 case DiagnosticsEngine::Level::Ignored:
189 m_manager->AddDiagnostic(data, eDiagnosticSeverityRemark, eDiagnosticOriginClang, Info.getID());
190 break;
191 case DiagnosticsEngine::Level::Note:
192 m_manager->AppendMessageToDiagnostic(data);
193 }
194 }
195
196 m_passthrough->HandleDiagnostic(DiagLevel, Info);
197 }
198
199 void
200 FlushDiagnostics(DiagnosticsEngine &Diags)
201 {
202 m_passthrough->FlushDiagnostics(Diags);
203 }
204
205 DiagnosticConsumer *
206 clone(DiagnosticsEngine &Diags) const
207 {
208 return new ClangDiagnosticManagerAdapter(m_passthrough);
209 }
210
211 clang::TextDiagnosticBuffer *
212 GetPassthrough()
213 {
214 return m_passthrough.get();
215 }
216
217private:
218 DiagnosticManager *m_manager = nullptr;
219 std::shared_ptr<clang::TextDiagnosticBuffer> m_passthrough;
220};
221
Sean Callananbd4dc692016-03-21 22:23:38 +0000222class LoggingDiagnosticConsumer : public clang::DiagnosticConsumer
223{
224public:
225 LoggingDiagnosticConsumer ()
226 {
227 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
228 m_passthrough.reset(new clang::TextDiagnosticBuffer);
229 }
230
231 LoggingDiagnosticConsumer (const std::shared_ptr<clang::TextDiagnosticBuffer> &passthrough)
232 {
233 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
234 m_passthrough = passthrough;
235 }
236
237 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info)
238 {
239 if (m_log)
240 {
241 llvm::SmallVector<char, 32> diag_str;
242 Info.FormatDiagnostic(diag_str);
243 diag_str.push_back('\0');
244 const char *data = diag_str.data();
245 m_log->Printf("[clang] COMPILER DIAGNOSTIC: %s", data);
246
247 lldbassert(Info.getID() != clang::diag::err_unsupported_ast_node && "'log enable lldb expr' to investigate.");
248 }
249
250 m_passthrough->HandleDiagnostic(DiagLevel, Info);
251 }
252
253 void FlushDiagnostics (DiagnosticsEngine &Diags)
254 {
255 m_passthrough->FlushDiagnostics(Diags);
256 }
257
258 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
259 {
260 return new LoggingDiagnosticConsumer (m_passthrough);
261 }
262
263 clang::TextDiagnosticBuffer *GetPassthrough()
264 {
265 return m_passthrough.get();
266 }
267private:
268 Log * m_log;
269 std::shared_ptr<clang::TextDiagnosticBuffer> m_passthrough;
270};
271
Sean Callanan1a8d4092010-08-27 01:01:44 +0000272//===----------------------------------------------------------------------===//
273// Implementation of ClangExpressionParser
274//===----------------------------------------------------------------------===//
275
Greg Clayton514487e2011-02-15 21:59:32 +0000276ClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope,
Jim Ingham151c0322015-09-15 21:13:50 +0000277 Expression &expr,
Greg Clayton23f8c952014-03-24 23:10:19 +0000278 bool generate_debug_info) :
Jim Ingham151c0322015-09-15 21:13:50 +0000279 ExpressionParser (exe_scope, expr, generate_debug_info),
Sean Callanan1a8d4092010-08-27 01:01:44 +0000280 m_compiler (),
Zachary Turner22ac8712014-12-05 22:54:56 +0000281 m_code_generator (),
282 m_pp_callbacks(nullptr)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000283{
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000284 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
285
Sean Callanan1a8d4092010-08-27 01:01:44 +0000286 // 1. Create a new compiler instance.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000287 m_compiler.reset(new CompilerInstance());
Ewan Crawford7648dd32016-03-10 12:38:55 +0000288 lldb::LanguageType frame_lang = expr.Language(); // defaults to lldb::eLanguageTypeUnknown
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000289 bool overridden_target_opts = false;
290 lldb_private::LanguageRuntime *lang_rt = nullptr;
Sean Callanan3d654b32012-09-24 22:25:51 +0000291 lldb::TargetSP target_sp;
292 if (exe_scope)
293 target_sp = exe_scope->CalculateTarget();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000294
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000295 ArchSpec target_arch;
296 if (target_sp)
297 target_arch = target_sp->GetArchitecture();
298
299 const auto target_machine = target_arch.GetMachine();
300
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000301 // If the expression is being evaluated in the context of an existing
302 // stack frame, we introspect to see if the language runtime is available.
303 auto frame = exe_scope->CalculateStackFrame();
304
305 // Make sure the user hasn't provided a preferred execution language
306 // with `expression --language X -- ...`
Ewan Crawford7648dd32016-03-10 12:38:55 +0000307 if (frame && frame_lang == lldb::eLanguageTypeUnknown)
308 frame_lang = frame->GetLanguage();
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000309
Ewan Crawford7648dd32016-03-10 12:38:55 +0000310 if (frame_lang != lldb::eLanguageTypeUnknown)
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000311 {
Ewan Crawford7648dd32016-03-10 12:38:55 +0000312 lang_rt = exe_scope->CalculateProcess()->GetLanguageRuntime(frame_lang);
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000313 if (log)
Ewan Crawford7648dd32016-03-10 12:38:55 +0000314 log->Printf("Frame has language of type %s", Language::GetNameForLanguageType(frame_lang));
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000315 }
316
317 // 2. Configure the compiler with a set of default options that are appropriate
318 // for most situations.
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000319 if (target_sp && target_arch.IsValid())
Sean Callanan3d654b32012-09-24 22:25:51 +0000320 {
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000321 std::string triple = target_arch.GetTriple().str();
Sean Callanan3d654b32012-09-24 22:25:51 +0000322 m_compiler->getTargetOpts().Triple = triple;
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000323 if (log)
324 log->Printf("Using %s as the target triple", m_compiler->getTargetOpts().Triple.c_str());
Sean Callanan3d654b32012-09-24 22:25:51 +0000325 }
326 else
327 {
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000328 // If we get here we don't have a valid target and just have to guess.
329 // Sometimes this will be ok to just use the host target triple (when we evaluate say "2+3", but other
330 // expressions like breakpoint conditions and other things that _are_ target specific really shouldn't just be
331 // using the host triple. In such a case the language runtime should expose an overridden options set (3),
332 // below.
Sean Callanan3d654b32012-09-24 22:25:51 +0000333 m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000334 if (log)
335 log->Printf("Using default target triple of %s", m_compiler->getTargetOpts().Triple.c_str());
Sean Callanan3d654b32012-09-24 22:25:51 +0000336 }
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000337 // Now add some special fixes for known architectures:
Jason Molendaa3329782014-03-29 18:54:20 +0000338 // Any arm32 iOS environment, but not on arm64
Sean Callanan60400ec2014-06-23 21:00:25 +0000339 if (m_compiler->getTargetOpts().Triple.find("arm64") == std::string::npos &&
340 m_compiler->getTargetOpts().Triple.find("arm") != std::string::npos &&
341 m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos)
Jason Molendaa3329782014-03-29 18:54:20 +0000342 {
Sean Callanan3d654b32012-09-24 22:25:51 +0000343 m_compiler->getTargetOpts().ABI = "apcs-gnu";
Jason Molendaa3329782014-03-29 18:54:20 +0000344 }
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000345 // Supported subsets of x86
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000346 if (target_machine == llvm::Triple::x86 ||
347 target_machine == llvm::Triple::x86_64)
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000348 {
349 m_compiler->getTargetOpts().Features.push_back("+sse");
350 m_compiler->getTargetOpts().Features.push_back("+sse2");
351 }
Jason Molendaa3329782014-03-29 18:54:20 +0000352
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000353 // Set the target CPU to generate code for.
354 // This will be empty for any CPU that doesn't really need to make a special CPU string.
355 m_compiler->getTargetOpts().CPU = target_arch.GetClangTargetCPU();
356
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000357 // 3. Now allow the runtime to provide custom configuration options for the target.
358 // In this case, a specialized language runtime is available and we can query it for extra options.
359 // For 99% of use cases, this will not be needed and should be provided when basic platform detection is not enough.
360 if (lang_rt)
361 overridden_target_opts = lang_rt->GetOverrideExprOptions(m_compiler->getTargetOpts());
362
363 if (overridden_target_opts)
364 if (log)
365 {
366 log->Debug("Using overridden target options for the expression evaluation");
367
368 auto opts = m_compiler->getTargetOpts();
369 log->Debug("Triple: '%s'", opts.Triple.c_str());
370 log->Debug("CPU: '%s'", opts.CPU.c_str());
371 log->Debug("FPMath: '%s'", opts.FPMath.c_str());
372 log->Debug("ABI: '%s'", opts.ABI.c_str());
373 log->Debug("LinkerVersion: '%s'", opts.LinkerVersion.c_str());
374 StringList::LogDump(log, opts.FeaturesAsWritten, "FeaturesAsWritten");
375 StringList::LogDump(log, opts.Features, "Features");
376 StringList::LogDump(log, opts.Reciprocals, "Reciprocals");
377 }
378
379 // 4. Create and install the target on the compiler.
Sean Callananb1de8dd2013-01-22 02:20:20 +0000380 m_compiler->createDiagnostics();
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000381 auto target_info = TargetInfo::CreateTargetInfo(m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts);
382 if (log)
383 {
384 log->Printf("Using SIMD alignment: %d", target_info->getSimdDefaultAlign());
James Y Knightdc395b92016-03-04 19:30:53 +0000385 log->Printf("Target datalayout string: '%s'", target_info->getDataLayout().getStringRepresentation().c_str());
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000386 log->Printf("Target ABI: '%s'", target_info->getABI().str().c_str());
387 log->Printf("Target vector alignment: %d", target_info->getMaxVectorAlign());
388 }
389 m_compiler->setTarget(target_info);
Alp Toker5f838642014-07-06 05:36:57 +0000390
Sean Callanan3d654b32012-09-24 22:25:51 +0000391 assert (m_compiler->hasTarget());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000392
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000393 // 5. Set language options.
Ewan Crawford7648dd32016-03-10 12:38:55 +0000394 lldb::LanguageType language = expr.Language();
395
396 switch (language)
Sean Callananc7b65062011-11-07 23:35:40 +0000397 {
398 case lldb::eLanguageTypeC:
Dawn Perchik009d1102015-09-04 01:02:30 +0000399 case lldb::eLanguageTypeC89:
400 case lldb::eLanguageTypeC99:
401 case lldb::eLanguageTypeC11:
402 // FIXME: the following language option is a temporary workaround,
403 // to "ask for C, get C++."
404 // For now, the expression parser must use C++ anytime the
405 // language is a C family language, because the expression parser
406 // uses features of C++ to capture values.
407 m_compiler->getLangOpts().CPlusPlus = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000408 break;
409 case lldb::eLanguageTypeObjC:
410 m_compiler->getLangOpts().ObjC1 = true;
411 m_compiler->getLangOpts().ObjC2 = true;
Dawn Perchik009d1102015-09-04 01:02:30 +0000412 // FIXME: the following language option is a temporary workaround,
413 // to "ask for ObjC, get ObjC++" (see comment above).
414 m_compiler->getLangOpts().CPlusPlus = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000415 break;
416 case lldb::eLanguageTypeC_plus_plus:
Dawn Perchik009d1102015-09-04 01:02:30 +0000417 case lldb::eLanguageTypeC_plus_plus_11:
418 case lldb::eLanguageTypeC_plus_plus_14:
Chandler Carruth38336a12013-01-02 12:55:00 +0000419 m_compiler->getLangOpts().CPlusPlus11 = true;
Jim Ingham26c7bf92014-10-11 00:38:27 +0000420 m_compiler->getHeaderSearchOpts().UseLibcxx = true;
Jason Molenda62e06812016-02-16 04:14:33 +0000421 LLVM_FALLTHROUGH;
Dawn Perchik009d1102015-09-04 01:02:30 +0000422 case lldb::eLanguageTypeC_plus_plus_03:
423 m_compiler->getLangOpts().CPlusPlus = true;
424 // FIXME: the following language option is a temporary workaround,
425 // to "ask for C++, get ObjC++". Apple hopes to remove this requirement
426 // on non-Apple platforms, but for now it is needed.
427 m_compiler->getLangOpts().ObjC1 = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000428 break;
429 case lldb::eLanguageTypeObjC_plus_plus:
Dawn Perchik009d1102015-09-04 01:02:30 +0000430 case lldb::eLanguageTypeUnknown:
Sean Callananc7b65062011-11-07 23:35:40 +0000431 default:
432 m_compiler->getLangOpts().ObjC1 = true;
433 m_compiler->getLangOpts().ObjC2 = true;
434 m_compiler->getLangOpts().CPlusPlus = true;
Chandler Carruth38336a12013-01-02 12:55:00 +0000435 m_compiler->getLangOpts().CPlusPlus11 = true;
Jim Ingham26c7bf92014-10-11 00:38:27 +0000436 m_compiler->getHeaderSearchOpts().UseLibcxx = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000437 break;
438 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000439
Sean Callananaa0f9cb2012-10-17 22:09:59 +0000440 m_compiler->getLangOpts().Bool = true;
441 m_compiler->getLangOpts().WChar = true;
Sean Callananeeb43842013-04-01 22:12:37 +0000442 m_compiler->getLangOpts().Blocks = true;
Sean Callanan226b70c2012-03-08 02:39:03 +0000443 m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients
Jim Ingham151c0322015-09-15 21:13:50 +0000444 if (expr.DesiredResultType() == Expression::eResultTypeId)
Sean Callanan226b70c2012-03-08 02:39:03 +0000445 m_compiler->getLangOpts().DebuggerCastResultToId = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000446
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000447 m_compiler->getLangOpts().CharIsSigned =
448 ArchSpec(m_compiler->getTargetOpts().Triple.c_str()).CharIsSignedByDefault();
449
Sean Callanan0765c822012-04-17 00:49:48 +0000450 // Spell checking is a nice feature, but it ends up completing a
451 // lot of types that we didn't strictly speaking need to complete.
452 // As a result, we spend a long time parsing and importing debug
453 // information.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000454 m_compiler->getLangOpts().SpellChecking = false;
455
Greg Claytond9e416c2012-02-18 05:35:26 +0000456 lldb::ProcessSP process_sp;
Greg Clayton514487e2011-02-15 21:59:32 +0000457 if (exe_scope)
Greg Claytond9e416c2012-02-18 05:35:26 +0000458 process_sp = exe_scope->CalculateProcess();
Greg Clayton514487e2011-02-15 21:59:32 +0000459
Greg Claytond9e416c2012-02-18 05:35:26 +0000460 if (process_sp && m_compiler->getLangOpts().ObjC1)
Sean Callananc3a16002011-01-17 23:42:46 +0000461 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000462 if (process_sp->GetObjCLanguageRuntime())
Sean Callananc3a16002011-01-17 23:42:46 +0000463 {
Enrico Granata15a67f492015-09-23 20:12:19 +0000464 if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == ObjCLanguageRuntime::ObjCRuntimeVersions::eAppleObjC_V2)
Sean Callanan3d654b32012-09-24 22:25:51 +0000465 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7));
466 else
467 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000468
Sean Callanan226b70c2012-03-08 02:39:03 +0000469 if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
Sean Callanan226b70c2012-03-08 02:39:03 +0000470 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
Sean Callananc3a16002011-01-17 23:42:46 +0000471 }
472 }
Greg Claytonf83f32d2011-01-15 01:32:14 +0000473
Sean Callanan1a8d4092010-08-27 01:01:44 +0000474 m_compiler->getLangOpts().ThreadsafeStatics = false;
475 m_compiler->getLangOpts().AccessControl = false; // Debuggers get universal access
476 m_compiler->getLangOpts().DollarIdents = true; // $ indicates a persistent variable name
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000477
Sean Callanan1a8d4092010-08-27 01:01:44 +0000478 // Set CodeGen options
479 m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
480 m_compiler->getCodeGenOpts().InstrumentFunctions = false;
Greg Claytonbc3122e2013-11-04 19:50:49 +0000481 m_compiler->getCodeGenOpts().DisableFPElim = true;
482 m_compiler->getCodeGenOpts().OmitLeafFramePointer = false;
Greg Clayton23f8c952014-03-24 23:10:19 +0000483 if (generate_debug_info)
Pavel Labathe03bd652016-02-02 13:07:27 +0000484 m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::FullDebugInfo);
Greg Clayton23f8c952014-03-24 23:10:19 +0000485 else
Pavel Labathe03bd652016-02-02 13:07:27 +0000486 m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::NoDebugInfo);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000487
Sean Callanan1a8d4092010-08-27 01:01:44 +0000488 // Disable some warnings.
Hafiz Abid Qadeer101e4902014-08-07 12:54:20 +0000489 m_compiler->getDiagnostics().setSeverityForGroup(clang::diag::Flavor::WarningOrError,
Alp Toker10933d12014-06-12 11:14:32 +0000490 "unused-value", clang::diag::Severity::Ignored, SourceLocation());
Hafiz Abid Qadeer101e4902014-08-07 12:54:20 +0000491 m_compiler->getDiagnostics().setSeverityForGroup(clang::diag::Flavor::WarningOrError,
Alp Toker10933d12014-06-12 11:14:32 +0000492 "odr", clang::diag::Severity::Ignored, SourceLocation());
493
Sean Callanan1a8d4092010-08-27 01:01:44 +0000494 // Inform the target of the language options
495 //
496 // FIXME: We shouldn't need to do this, the target should be immutable once
497 // created. This complexity should be lifted elsewhere.
Sylvestre Ledru3cc9d632014-07-06 17:50:36 +0000498 m_compiler->getTarget().adjust(m_compiler->getLangOpts());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000499
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000500 // 6. Set up the diagnostic buffer for reporting errors
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000501
Sean Callanan579e70c2016-03-19 00:03:59 +0000502 m_compiler->getDiagnostics().setClient(new ClangDiagnosticManagerAdapter);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000503
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000504 // 7. Set up the source management objects inside the compiler
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000505
Greg Clayton38a61402010-12-02 23:20:03 +0000506 clang::FileSystemOptions file_system_options;
507 m_file_manager.reset(new clang::FileManager(file_system_options));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000508
Sean Callanan1a8d4092010-08-27 01:01:44 +0000509 if (!m_compiler->hasSourceManager())
Greg Clayton38a61402010-12-02 23:20:03 +0000510 m_compiler->createSourceManager(*m_file_manager.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000511
Sean Callanan1a8d4092010-08-27 01:01:44 +0000512 m_compiler->createFileManager();
Ben Langmuir9f0bac52014-03-07 08:31:36 +0000513 m_compiler->createPreprocessor(TU_Complete);
Sean Callananc8278af2014-12-05 01:27:35 +0000514
515 if (ClangModulesDeclVendor *decl_vendor = target_sp->GetClangModulesDeclVendor())
516 {
Sean Callananb92bd752015-10-01 16:28:02 +0000517 ClangPersistentVariables *clang_persistent_vars = llvm::cast<ClangPersistentVariables>(target_sp->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC));
Sean Callanan8f1f9a12015-09-30 19:57:57 +0000518 std::unique_ptr<PPCallbacks> pp_callbacks(new LLDBPreprocessorCallbacks(*decl_vendor, *clang_persistent_vars));
Sean Callananc8278af2014-12-05 01:27:35 +0000519 m_pp_callbacks = static_cast<LLDBPreprocessorCallbacks*>(pp_callbacks.get());
520 m_compiler->getPreprocessor().addPPCallbacks(std::move(pp_callbacks));
521 }
522
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000523 // 8. Most of this we get from the CompilerInstance, but we
Sean Callanan1a8d4092010-08-27 01:01:44 +0000524 // also want to give the context an ExternalASTSource.
Sean Callanan6abfabf2010-11-19 20:20:02 +0000525 m_selector_table.reset(new SelectorTable());
Sean Callanan880e6802011-10-07 23:18:13 +0000526 m_builtin_context.reset(new Builtin::Context());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000527
Greg Clayton7b0992d2013-04-18 22:45:39 +0000528 std::unique_ptr<clang::ASTContext> ast_context(new ASTContext(m_compiler->getLangOpts(),
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000529 m_compiler->getSourceManager(),
530 m_compiler->getPreprocessor().getIdentifierTable(),
531 *m_selector_table.get(),
532 *m_builtin_context.get()));
Sean Callananc8278af2014-12-05 01:27:35 +0000533
Alp Tokercf55e882014-05-03 15:05:45 +0000534 ast_context->InitBuiltinTypes(m_compiler->getTarget());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000535
Jim Ingham151c0322015-09-15 21:13:50 +0000536 ClangExpressionHelper *type_system_helper = dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
537 ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000538
Sean Callanan1a8d4092010-08-27 01:01:44 +0000539 if (decl_map)
540 {
Sylvestre Ledru7ba631f2014-02-27 10:46:57 +0000541 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy());
Sean Callananeddeb3b2011-10-28 23:38:38 +0000542 decl_map->InstallASTContext(ast_context.get());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000543 ast_context->setExternalSource(ast_source);
544 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000545
Greg Claytond8d4a572015-08-11 21:38:15 +0000546 m_ast_context.reset(new ClangASTContext(m_compiler->getTargetOpts().Triple.c_str()));
547 m_ast_context->setASTContext(ast_context.get());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000548 m_compiler->setASTContext(ast_context.release());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000549
Greg Clayton7b462cc2010-10-15 22:48:33 +0000550 std::string module_name("$__lldb_module");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000551
Sean Callananfb0b7582011-03-15 00:17:19 +0000552 m_llvm_context.reset(new LLVMContext());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000553 m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(),
554 module_name,
Tamas Berghammer0b736f12015-06-30 09:26:52 +0000555 m_compiler->getHeaderSearchOpts(),
556 m_compiler->getPreprocessorOpts(),
Sean Callanan1a8d4092010-08-27 01:01:44 +0000557 m_compiler->getCodeGenOpts(),
Sean Callananfb0b7582011-03-15 00:17:19 +0000558 *m_llvm_context));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000559}
560
561ClangExpressionParser::~ClangExpressionParser()
562{
563}
564
565unsigned
Sean Callanan579e70c2016-03-19 00:03:59 +0000566ClangExpressionParser::Parse(DiagnosticManager &diagnostic_manager)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000567{
Sean Callanan579e70c2016-03-19 00:03:59 +0000568 ClangDiagnosticManagerAdapter *adapter =
569 static_cast<ClangDiagnosticManagerAdapter *>(m_compiler->getDiagnostics().getClient());
570 clang::TextDiagnosticBuffer *diag_buf = adapter->GetPassthrough();
Ewan Crawford615a8072016-02-19 17:55:10 +0000571 diag_buf->FlushDiagnostics(m_compiler->getDiagnostics());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000572
Sean Callanan579e70c2016-03-19 00:03:59 +0000573 adapter->ResetManager(&diagnostic_manager);
574
Greg Clayton23f8c952014-03-24 23:10:19 +0000575 const char *expr_text = m_expr.Text();
Rafael Espindola9cb97b62014-05-21 15:08:27 +0000576
Ewan Crawford615a8072016-02-19 17:55:10 +0000577 clang::SourceManager &source_mgr = m_compiler->getSourceManager();
Greg Clayton23f8c952014-03-24 23:10:19 +0000578 bool created_main_file = false;
Pavel Labathe03bd652016-02-02 13:07:27 +0000579 if (m_compiler->getCodeGenOpts().getDebugInfo() == codegenoptions::FullDebugInfo)
Greg Clayton23f8c952014-03-24 23:10:19 +0000580 {
Zachary Turnercb5742b2014-10-20 17:46:56 +0000581 int temp_fd = -1;
582 llvm::SmallString<PATH_MAX> result_path;
Greg Clayton23f8c952014-03-24 23:10:19 +0000583 FileSpec tmpdir_file_spec;
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000584 if (HostInfo::GetLLDBPath(lldb::ePathTypeLLDBTempSystemDir, tmpdir_file_spec))
Greg Clayton23f8c952014-03-24 23:10:19 +0000585 {
Zachary Turnercb5742b2014-10-20 17:46:56 +0000586 tmpdir_file_spec.AppendPathComponent("lldb-%%%%%%.expr");
Ewan Crawford615a8072016-02-19 17:55:10 +0000587 std::string temp_source_path = tmpdir_file_spec.GetPath();
Zachary Turnercb5742b2014-10-20 17:46:56 +0000588 llvm::sys::fs::createUniqueFile(temp_source_path, temp_fd, result_path);
Greg Clayton23f8c952014-03-24 23:10:19 +0000589 }
590 else
591 {
Zachary Turnercb5742b2014-10-20 17:46:56 +0000592 llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path);
Greg Clayton23f8c952014-03-24 23:10:19 +0000593 }
Ewan Crawford615a8072016-02-19 17:55:10 +0000594
Jason Molendabc464ee2014-10-16 23:10:03 +0000595 if (temp_fd != -1)
Greg Clayton23f8c952014-03-24 23:10:19 +0000596 {
Ewan Crawford615a8072016-02-19 17:55:10 +0000597 lldb_private::File file(temp_fd, true);
Greg Clayton23f8c952014-03-24 23:10:19 +0000598 const size_t expr_text_len = strlen(expr_text);
599 size_t bytes_written = expr_text_len;
600 if (file.Write(expr_text, bytes_written).Success())
601 {
602 if (bytes_written == expr_text_len)
603 {
604 file.Close();
Ewan Crawford615a8072016-02-19 17:55:10 +0000605 source_mgr.setMainFileID(source_mgr.createFileID(m_file_manager->getFile(result_path),
606 SourceLocation(), SrcMgr::C_User));
Greg Clayton23f8c952014-03-24 23:10:19 +0000607 created_main_file = true;
608 }
609 }
610 }
611 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000612
Greg Clayton23f8c952014-03-24 23:10:19 +0000613 if (!created_main_file)
614 {
Rafael Espindola2443e702014-08-27 20:09:08 +0000615 std::unique_ptr<MemoryBuffer> memory_buffer = MemoryBuffer::getMemBufferCopy(expr_text, __FUNCTION__);
Ewan Crawford615a8072016-02-19 17:55:10 +0000616 source_mgr.setMainFileID(source_mgr.createFileID(std::move(memory_buffer)));
Greg Clayton23f8c952014-03-24 23:10:19 +0000617 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000618
Sean Callanane2ef6e32010-09-23 03:01:22 +0000619 diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000620
Jim Ingham151c0322015-09-15 21:13:50 +0000621 ClangExpressionHelper *type_system_helper = dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000622
Jim Ingham151c0322015-09-15 21:13:50 +0000623 ASTConsumer *ast_transformer = type_system_helper->ASTTransformer(m_code_generator.get());
624
625 if (ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap())
Sean Callanan80c97592015-05-01 00:47:29 +0000626 decl_map->InstallCodeGenerator(m_code_generator.get());
Chaoren Lin57998de2015-08-19 01:24:57 +0000627
Sean Callanan1a8d4092010-08-27 01:01:44 +0000628 if (ast_transformer)
Chaoren Lin57998de2015-08-19 01:24:57 +0000629 {
630 ast_transformer->Initialize(m_compiler->getASTContext());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000631 ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext());
Chaoren Lin57998de2015-08-19 01:24:57 +0000632 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000633 else
Chaoren Lin57998de2015-08-19 01:24:57 +0000634 {
635 m_code_generator->Initialize(m_compiler->getASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000636 ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext());
Chaoren Lin57998de2015-08-19 01:24:57 +0000637 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000638
Sean Callanane2ef6e32010-09-23 03:01:22 +0000639 diag_buf->EndSourceFile();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000640
Ewan Crawford615a8072016-02-19 17:55:10 +0000641 unsigned num_errors = diag_buf->getNumErrors();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000642
Zachary Turner22ac8712014-12-05 22:54:56 +0000643 if (m_pp_callbacks && m_pp_callbacks->hasErrors())
Sean Callananc8278af2014-12-05 01:27:35 +0000644 {
645 num_errors++;
Sean Callanan579e70c2016-03-19 00:03:59 +0000646 diagnostic_manager.PutCString(eDiagnosticSeverityError, "while importing modules:");
647 diagnostic_manager.AppendMessageToDiagnostic(m_pp_callbacks->getErrorString().c_str());
Sean Callananc8278af2014-12-05 01:27:35 +0000648 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000649
Sean Callanan77502262011-05-12 23:54:16 +0000650 if (!num_errors)
651 {
Jim Ingham151c0322015-09-15 21:13:50 +0000652 if (type_system_helper->DeclMap() && !type_system_helper->DeclMap()->ResolveUnknownTypes())
Sean Callanan77502262011-05-12 23:54:16 +0000653 {
Sean Callanan579e70c2016-03-19 00:03:59 +0000654 diagnostic_manager.Printf(eDiagnosticSeverityError, "Couldn't infer the type of a variable");
Sean Callanan77502262011-05-12 23:54:16 +0000655 num_errors++;
656 }
657 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000658
Sean Callanan00294b32016-03-22 21:05:51 +0000659 if (!num_errors)
660 {
661 type_system_helper->CommitPersistentDecls();
662 }
663
Sean Callanan579e70c2016-03-19 00:03:59 +0000664 adapter->ResetManager();
665
Sean Callanan1a8d4092010-08-27 01:01:44 +0000666 return num_errors;
667}
668
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000669static bool FindFunctionInModule (ConstString &mangled_name,
Sean Callananfc55f5d2010-09-21 00:44:12 +0000670 llvm::Module *module,
671 const char *orig_name)
672{
Aidan Doddsb1298472016-02-25 13:07:04 +0000673 for (const auto &func : module->getFunctionList())
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000674 {
Aidan Doddsb1298472016-02-25 13:07:04 +0000675 const StringRef &name = func.getName();
676 if (name.find(orig_name) != StringRef::npos)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000677 {
Aidan Doddsb1298472016-02-25 13:07:04 +0000678 mangled_name.SetString(name);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000679 return true;
680 }
681 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000682
Sean Callananfc55f5d2010-09-21 00:44:12 +0000683 return false;
684}
685
Sean Callanan1a8d4092010-08-27 01:01:44 +0000686Error
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000687ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_addr,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000688 lldb::addr_t &func_end,
Jim Inghamc29e6982015-11-05 01:50:42 +0000689 lldb::IRExecutionUnitSP &execution_unit_sp,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000690 ExecutionContext &exe_ctx,
Sean Callanan1582ee62013-04-18 22:06:33 +0000691 bool &can_interpret,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000692 ExecutionPolicy execution_policy)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000693{
Greg Clayton22a939a2011-01-19 23:00:49 +0000694 func_addr = LLDB_INVALID_ADDRESS;
695 func_end = LLDB_INVALID_ADDRESS;
Greg Clayton5160ce52013-03-27 23:08:40 +0000696 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfc55f5d2010-09-21 00:44:12 +0000697
Sean Callanan1a8d4092010-08-27 01:01:44 +0000698 Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000699
Greg Clayton23f8c952014-03-24 23:10:19 +0000700 std::unique_ptr<llvm::Module> llvm_module_ap (m_code_generator->ReleaseModule());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000701
Greg Clayton23f8c952014-03-24 23:10:19 +0000702 if (!llvm_module_ap.get())
Sean Callanan1a8d4092010-08-27 01:01:44 +0000703 {
704 err.SetErrorToGenericError();
705 err.SetErrorString("IR doesn't contain a module");
706 return err;
707 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000708
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000709 ConstString function_name;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000710
Sean Callanan00294b32016-03-22 21:05:51 +0000711 if (execution_policy != eExecutionPolicyTopLevel)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000712 {
Sean Callanan00294b32016-03-22 21:05:51 +0000713 // Find the actual name of the function (it's often mangled somehow)
714
715 if (!FindFunctionInModule(function_name, llvm_module_ap.get(), m_expr.FunctionName()))
716 {
717 err.SetErrorToGenericError();
718 err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName());
719 return err;
720 }
721 else
722 {
723 if (log)
724 log->Printf("Found function %s for %s", function_name.AsCString(), m_expr.FunctionName());
725 }
Sean Callananfc55f5d2010-09-21 00:44:12 +0000726 }
Sean Callanan00294b32016-03-22 21:05:51 +0000727
Sean Callananb2814802016-02-12 21:11:25 +0000728 SymbolContext sc;
Sean Callanan00294b32016-03-22 21:05:51 +0000729
Sean Callananb2814802016-02-12 21:11:25 +0000730 if (lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP())
731 {
732 sc = frame_sp->GetSymbolContext(lldb::eSymbolContextEverything);
733 }
734 else if (lldb::TargetSP target_sp = exe_ctx.GetTargetSP())
735 {
736 sc.target_sp = target_sp;
737 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000738
Greg Clayton23f8c952014-03-24 23:10:19 +0000739 execution_unit_sp.reset(new IRExecutionUnit (m_llvm_context, // handed off here
740 llvm_module_ap, // handed off here
741 function_name,
742 exe_ctx.GetTargetSP(),
Sean Callananb2814802016-02-12 21:11:25 +0000743 sc,
Greg Clayton23f8c952014-03-24 23:10:19 +0000744 m_compiler->getTargetOpts().Features));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000745
Jim Ingham151c0322015-09-15 21:13:50 +0000746 ClangExpressionHelper *type_system_helper = dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
747 ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap(); // result can be NULL
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000748
Sean Callanan1a8d4092010-08-27 01:01:44 +0000749 if (decl_map)
750 {
Sean Callanan3989fb92011-01-27 01:07:04 +0000751 Stream *error_stream = NULL;
Greg Claytonc14ee322011-09-22 04:58:26 +0000752 Target *target = exe_ctx.GetTargetPtr();
753 if (target)
Greg Clayton44d93782014-01-27 23:43:24 +0000754 error_stream = target->GetDebugger().GetErrorFile().get();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000755
Sean Callanan00294b32016-03-22 21:05:51 +0000756 IRForTarget ir_for_target(decl_map, m_expr.NeedsVariableResolution(), *execution_unit_sp, error_stream,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000757 function_name.AsCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000758
Greg Clayton23f8c952014-03-24 23:10:19 +0000759 bool ir_can_run = ir_for_target.runOnModule(*execution_unit_sp->GetModule());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000760
Greg Claytonc14ee322011-09-22 04:58:26 +0000761 Process *process = exe_ctx.GetProcessPtr();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000762
Sean Callanan00294b32016-03-22 21:05:51 +0000763 if (execution_policy != eExecutionPolicyAlways && execution_policy != eExecutionPolicyTopLevel)
764 {
765 Error interpret_error;
Ewan Crawford90ff7912015-07-14 10:56:58 +0000766
Sean Callanan00294b32016-03-22 21:05:51 +0000767 bool interpret_function_calls = !process ? false : process->CanInterpretFunctionCalls();
768 can_interpret =
769 IRInterpreter::CanInterpret(*execution_unit_sp->GetModule(), *execution_unit_sp->GetFunction(),
770 interpret_error, interpret_function_calls);
771
772 if (!can_interpret && execution_policy == eExecutionPolicyNever)
773 {
774 err.SetErrorStringWithFormat("Can't run the expression locally: %s", interpret_error.AsCString());
775 return err;
776 }
777 }
Ewan Crawford90ff7912015-07-14 10:56:58 +0000778
Sean Callanan1582ee62013-04-18 22:06:33 +0000779 if (!ir_can_run)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000780 {
Sean Callanan3d654b32012-09-24 22:25:51 +0000781 err.SetErrorString("The expression could not be prepared to run in the target");
Sean Callanan3d654b32012-09-24 22:25:51 +0000782 return err;
783 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000784
Sean Callanan1582ee62013-04-18 22:06:33 +0000785 if (!process && execution_policy == eExecutionPolicyAlways)
786 {
787 err.SetErrorString("Expression needed to run in the target, but the target can't be run");
788 return err;
789 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000790
Sean Callanan00294b32016-03-22 21:05:51 +0000791 if (!process && execution_policy == eExecutionPolicyTopLevel)
792 {
793 err.SetErrorString(
794 "Top-level code needs to be inserted into a runnable target, but the target can't be run");
795 return err;
796 }
797
798 if (execution_policy == eExecutionPolicyAlways ||
799 (execution_policy != eExecutionPolicyTopLevel && !can_interpret))
Sean Callanan1582ee62013-04-18 22:06:33 +0000800 {
Sean Callanane8cde682013-05-18 00:38:20 +0000801 if (m_expr.NeedsValidation() && process)
802 {
803 if (!process->GetDynamicCheckers())
Sean Callanan90539452011-09-20 23:01:51 +0000804 {
Sean Callanane8cde682013-05-18 00:38:20 +0000805 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000806
Sean Callanan579e70c2016-03-19 00:03:59 +0000807 DiagnosticManager install_diagnostics;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000808
Sean Callanan579e70c2016-03-19 00:03:59 +0000809 if (!dynamic_checkers->Install(install_diagnostics, exe_ctx))
Sean Callanane8cde682013-05-18 00:38:20 +0000810 {
Sean Callanan579e70c2016-03-19 00:03:59 +0000811 if (install_diagnostics.Diagnostics().size())
812 err.SetErrorString("couldn't install checkers, unknown error");
Sean Callanane8cde682013-05-18 00:38:20 +0000813 else
Sean Callanan579e70c2016-03-19 00:03:59 +0000814 err.SetErrorString(install_diagnostics.GetString().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000815
Sean Callanane8cde682013-05-18 00:38:20 +0000816 return err;
817 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000818
Sean Callanane8cde682013-05-18 00:38:20 +0000819 process->SetDynamicCheckers(dynamic_checkers);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000820
Sean Callanane8cde682013-05-18 00:38:20 +0000821 if (log)
822 log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
Sean Callanan90539452011-09-20 23:01:51 +0000823 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000824
Sean Callanan1582ee62013-04-18 22:06:33 +0000825 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000826
Greg Clayton23f8c952014-03-24 23:10:19 +0000827 if (!ir_dynamic_checks.runOnModule(*execution_unit_sp->GetModule()))
Sean Callanan1582ee62013-04-18 22:06:33 +0000828 {
829 err.SetErrorToGenericError();
830 err.SetErrorString("Couldn't add dynamic checks to the expression");
831 return err;
832 }
Sean Callanan90539452011-09-20 23:01:51 +0000833 }
Sean Callanan00294b32016-03-22 21:05:51 +0000834 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000835
Sean Callanan00294b32016-03-22 21:05:51 +0000836 if (execution_policy == eExecutionPolicyAlways || execution_policy == eExecutionPolicyTopLevel ||
837 !can_interpret)
838 {
Greg Clayton23f8c952014-03-24 23:10:19 +0000839 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
Sean Callanan9e6ed532010-09-13 21:34:21 +0000840 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000841 }
Sean Callanan1582ee62013-04-18 22:06:33 +0000842 else
843 {
Greg Clayton23f8c952014-03-24 23:10:19 +0000844 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
Sean Callanan1582ee62013-04-18 22:06:33 +0000845 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000846
Sean Callanan1a8d4092010-08-27 01:01:44 +0000847 return err;
848}
Sean Callananbd4dc692016-03-21 22:23:38 +0000849
850Error
851ClangExpressionParser::RunStaticInitializers (lldb::IRExecutionUnitSP &execution_unit_sp,
852 ExecutionContext &exe_ctx)
853{
854 Error err;
855
856 lldbassert(execution_unit_sp.get());
857 lldbassert(exe_ctx.HasThreadScope());
858
859 if (!execution_unit_sp.get())
860 {
861 err.SetErrorString ("can't run static initializers for a NULL execution unit");
862 return err;
863 }
864
865 if (!exe_ctx.HasThreadScope())
866 {
867 err.SetErrorString ("can't run static initializers without a thread");
868 return err;
869 }
870
871 std::vector<lldb::addr_t> static_initializers;
872
873 execution_unit_sp->GetStaticInitializers(static_initializers);
874
875 for (lldb::addr_t static_initializer : static_initializers)
876 {
877 EvaluateExpressionOptions options;
878
879 lldb::ThreadPlanSP call_static_initializer(new ThreadPlanCallFunction(exe_ctx.GetThreadRef(),
880 Address(static_initializer),
881 CompilerType(),
882 llvm::ArrayRef<lldb::addr_t>(),
883 options));
884
885 DiagnosticManager execution_errors;
886 lldb::ExpressionResults results = exe_ctx.GetThreadRef().GetProcess()->RunThreadPlan(exe_ctx, call_static_initializer, options, execution_errors);
887
888 if (results != lldb::eExpressionCompleted)
889 {
890 err.SetErrorStringWithFormat ("couldn't run static initializer: %s", execution_errors.GetString().c_str());
891 return err;
892 }
893 }
894
895 return err;
896}