blob: a75e60ff63c1c2c68afe0a1a89971acca861a403 [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"
Kate Stoneb9c1b512016-09-06 20:57:50 +000020#include "clang/Basic/Version.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000021#include "clang/CodeGen/CodeGenAction.h"
22#include "clang/CodeGen/ModuleBuilder.h"
Jim Inghama1e541b2016-03-25 01:57:14 +000023#include "clang/Edit/Commit.h"
Jim Inghama1e541b2016-03-25 01:57:14 +000024#include "clang/Edit/EditedSource.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000025#include "clang/Edit/EditsReceiver.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000026#include "clang/Frontend/CompilerInstance.h"
27#include "clang/Frontend/CompilerInvocation.h"
28#include "clang/Frontend/FrontendActions.h"
29#include "clang/Frontend/FrontendDiagnostic.h"
30#include "clang/Frontend/FrontendPluginRegistry.h"
31#include "clang/Frontend/TextDiagnosticBuffer.h"
32#include "clang/Frontend/TextDiagnosticPrinter.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000033#include "clang/Lex/Preprocessor.h"
Sean Callanane2ef6e32010-09-23 03:01:22 +000034#include "clang/Parse/ParseAST.h"
Jim Inghama1e541b2016-03-25 01:57:14 +000035#include "clang/Rewrite/Core/Rewriter.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000036#include "clang/Rewrite/Frontend/FrontendActions.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000037#include "clang/Sema/SemaConsumer.h"
Sean Callananfb0b7582011-03-15 00:17:19 +000038#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000039
40#include "llvm/ADT/StringRef.h"
41#include "llvm/ExecutionEngine/ExecutionEngine.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000042#include "llvm/Support/Debug.h"
Rafael Espindola4609ea82013-06-26 14:10:56 +000043#include "llvm/Support/FileSystem.h"
Sean Callanan880e6802011-10-07 23:18:13 +000044#include "llvm/Support/TargetSelect.h"
Greg Clayton70b57652011-05-15 01:25:55 +000045
Greg Claytonc9a078a2016-03-24 23:50:03 +000046#pragma clang diagnostic push
47#pragma clang diagnostic ignored "-Wglobal-constructors"
Ed Mastea09ed032014-03-18 18:55:06 +000048#include "llvm/ExecutionEngine/MCJIT.h"
Greg Claytonc9a078a2016-03-24 23:50:03 +000049#pragma clang diagnostic pop
50
Chandler Carruth1e157582013-01-02 12:20:07 +000051#include "llvm/IR/LLVMContext.h"
52#include "llvm/IR/Module.h"
Greg Clayton38a61402010-12-02 23:20:03 +000053#include "llvm/Support/DynamicLibrary.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000054#include "llvm/Support/ErrorHandling.h"
Greg Clayton38a61402010-12-02 23:20:03 +000055#include "llvm/Support/Host.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000056#include "llvm/Support/MemoryBuffer.h"
Greg Clayton38a61402010-12-02 23:20:03 +000057#include "llvm/Support/Signals.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000058
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000059// Project includes
Jim Inghama1e541b2016-03-25 01:57:14 +000060#include "ClangDiagnostic.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000061#include "ClangExpressionParser.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000062
63#include "ClangASTSource.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000064#include "ClangExpressionDeclMap.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000065#include "ClangExpressionHelper.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000066#include "ClangModulesDeclVendor.h"
67#include "ClangPersistentVariables.h"
68#include "IRForTarget.h"
69
70#include "lldb/Core/ArchSpec.h"
71#include "lldb/Core/DataBufferHeap.h"
72#include "lldb/Core/Debugger.h"
73#include "lldb/Core/Disassembler.h"
74#include "lldb/Core/Log.h"
75#include "lldb/Core/Module.h"
76#include "lldb/Core/Stream.h"
77#include "lldb/Core/StreamFile.h"
78#include "lldb/Core/StreamString.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000079#include "lldb/Core/StringList.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000080#include "lldb/Expression/IRDynamicChecks.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000081#include "lldb/Expression/IRExecutionUnit.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000082#include "lldb/Expression/IRInterpreter.h"
83#include "lldb/Host/File.h"
84#include "lldb/Host/HostInfo.h"
85#include "lldb/Symbol/ClangASTContext.h"
86#include "lldb/Symbol/SymbolVendor.h"
87#include "lldb/Target/ExecutionContext.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000088#include "lldb/Target/Language.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000089#include "lldb/Target/ObjCLanguageRuntime.h"
90#include "lldb/Target/Process.h"
91#include "lldb/Target/Target.h"
Sean Callananbd4dc692016-03-21 22:23:38 +000092#include "lldb/Target/ThreadPlanCallFunction.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000093#include "lldb/Utility/LLDBAssert.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000094
Sean Callanan1a8d4092010-08-27 01:01:44 +000095using namespace clang;
96using namespace llvm;
97using namespace lldb_private;
98
99//===----------------------------------------------------------------------===//
100// Utility Methods for Clang
101//===----------------------------------------------------------------------===//
102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103class ClangExpressionParser::LLDBPreprocessorCallbacks : public PPCallbacks {
104 ClangModulesDeclVendor &m_decl_vendor;
105 ClangPersistentVariables &m_persistent_vars;
106 StreamString m_error_stream;
107 bool m_has_errors = false;
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +0000108
Sean Callananc8278af2014-12-05 01:27:35 +0000109public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 LLDBPreprocessorCallbacks(ClangModulesDeclVendor &decl_vendor,
111 ClangPersistentVariables &persistent_vars)
112 : m_decl_vendor(decl_vendor), m_persistent_vars(persistent_vars) {}
113
114 void moduleImport(SourceLocation import_location, clang::ModuleIdPath path,
115 const clang::Module * /*null*/) override {
116 std::vector<ConstString> string_path;
117
118 for (const std::pair<IdentifierInfo *, SourceLocation> &component : path) {
119 string_path.push_back(ConstString(component.first->getName()));
Sean Callananc8278af2014-12-05 01:27:35 +0000120 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121
122 StreamString error_stream;
123
124 ClangModulesDeclVendor::ModuleVector exported_modules;
125
126 if (!m_decl_vendor.AddModule(string_path, &exported_modules,
127 m_error_stream)) {
128 m_has_errors = true;
Sean Callananc8278af2014-12-05 01:27:35 +0000129 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130
131 for (ClangModulesDeclVendor::ModuleID module : exported_modules) {
132 m_persistent_vars.AddHandLoadedClangModule(module);
Sean Callananc8278af2014-12-05 01:27:35 +0000133 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 }
135
136 bool hasErrors() { return m_has_errors; }
137
Zachary Turnerc1564272016-11-16 21:15:24 +0000138 llvm::StringRef getErrorString() { return m_error_stream.GetString(); }
Sean Callananc8278af2014-12-05 01:27:35 +0000139};
140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141class ClangDiagnosticManagerAdapter : public clang::DiagnosticConsumer {
Sean Callanan579e70c2016-03-19 00:03:59 +0000142public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 ClangDiagnosticManagerAdapter()
144 : m_passthrough(new clang::TextDiagnosticBuffer) {}
Sean Callanan579e70c2016-03-19 00:03:59 +0000145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 ClangDiagnosticManagerAdapter(
147 const std::shared_ptr<clang::TextDiagnosticBuffer> &passthrough)
148 : m_passthrough(passthrough) {}
Sean Callanan579e70c2016-03-19 00:03:59 +0000149
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 void ResetManager(DiagnosticManager *manager = nullptr) {
151 m_manager = manager;
152 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
155 const clang::Diagnostic &Info) {
156 if (m_manager) {
157 llvm::SmallVector<char, 32> diag_str;
158 Info.FormatDiagnostic(diag_str);
159 diag_str.push_back('\0');
160 const char *data = diag_str.data();
Sean Callanan579e70c2016-03-19 00:03:59 +0000161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 lldb_private::DiagnosticSeverity severity;
163 bool make_new_diagnostic = true;
164
165 switch (DiagLevel) {
166 case DiagnosticsEngine::Level::Fatal:
167 case DiagnosticsEngine::Level::Error:
168 severity = eDiagnosticSeverityError;
169 break;
170 case DiagnosticsEngine::Level::Warning:
171 severity = eDiagnosticSeverityWarning;
172 break;
173 case DiagnosticsEngine::Level::Remark:
174 case DiagnosticsEngine::Level::Ignored:
175 severity = eDiagnosticSeverityRemark;
176 break;
177 case DiagnosticsEngine::Level::Note:
178 m_manager->AppendMessageToDiagnostic(data);
179 make_new_diagnostic = false;
180 }
181 if (make_new_diagnostic) {
182 ClangDiagnostic *new_diagnostic =
183 new ClangDiagnostic(data, severity, Info.getID());
184 m_manager->AddDiagnostic(new_diagnostic);
185
186 // Don't store away warning fixits, since the compiler doesn't have
187 // enough
188 // context in an expression for the warning to be useful.
189 // FIXME: Should we try to filter out FixIts that apply to our generated
190 // code, and not the user's expression?
191 if (severity == eDiagnosticSeverityError) {
192 size_t num_fixit_hints = Info.getNumFixItHints();
193 for (size_t i = 0; i < num_fixit_hints; i++) {
194 const clang::FixItHint &fixit = Info.getFixItHint(i);
195 if (!fixit.isNull())
196 new_diagnostic->AddFixitHint(fixit);
197 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000198 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000200 }
201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 m_passthrough->HandleDiagnostic(DiagLevel, Info);
203 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 void FlushDiagnostics(DiagnosticsEngine &Diags) {
206 m_passthrough->FlushDiagnostics(Diags);
207 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000208
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
210 return new ClangDiagnosticManagerAdapter(m_passthrough);
211 }
212
213 clang::TextDiagnosticBuffer *GetPassthrough() { return m_passthrough.get(); }
Sean Callanan579e70c2016-03-19 00:03:59 +0000214
215private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 DiagnosticManager *m_manager = nullptr;
217 std::shared_ptr<clang::TextDiagnosticBuffer> m_passthrough;
Sean Callanan579e70c2016-03-19 00:03:59 +0000218};
219
Sean Callanan1a8d4092010-08-27 01:01:44 +0000220//===----------------------------------------------------------------------===//
221// Implementation of ClangExpressionParser
222//===----------------------------------------------------------------------===//
223
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224ClangExpressionParser::ClangExpressionParser(ExecutionContextScope *exe_scope,
225 Expression &expr,
226 bool generate_debug_info)
227 : ExpressionParser(exe_scope, expr, generate_debug_info), m_compiler(),
228 m_code_generator(), m_pp_callbacks(nullptr) {
229 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 // We can't compile expressions without a target. So if the exe_scope is null
232 // or doesn't have a target,
233 // then we just need to get out of here. I'll lldb_assert and not make any of
234 // the compiler objects since
235 // I can't return errors directly from the constructor. Further calls will
236 // check if the compiler was made and
237 // bag out if it wasn't.
Sagar Thakuradc1abe2016-05-13 11:04:47 +0000238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239 if (!exe_scope) {
240 lldb_assert(exe_scope, "Can't make an expression parser with a null scope.",
241 __FUNCTION__, __FILE__, __LINE__);
242 return;
243 }
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000244
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245 lldb::TargetSP target_sp;
246 target_sp = exe_scope->CalculateTarget();
247 if (!target_sp) {
248 lldb_assert(target_sp.get(),
249 "Can't make an expression parser with a null target.",
250 __FUNCTION__, __FILE__, __LINE__);
251 return;
252 }
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 // 1. Create a new compiler instance.
255 m_compiler.reset(new CompilerInstance());
256 lldb::LanguageType frame_lang =
257 expr.Language(); // defaults to lldb::eLanguageTypeUnknown
258 bool overridden_target_opts = false;
259 lldb_private::LanguageRuntime *lang_rt = nullptr;
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 std::string abi;
262 ArchSpec target_arch;
263 target_arch = target_sp->GetArchitecture();
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 const auto target_machine = target_arch.GetMachine();
Jason Molendaa3329782014-03-29 18:54:20 +0000266
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 // If the expression is being evaluated in the context of an existing
268 // stack frame, we introspect to see if the language runtime is available.
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270 lldb::StackFrameSP frame_sp = exe_scope->CalculateStackFrame();
271 lldb::ProcessSP process_sp = exe_scope->CalculateProcess();
Sagar Thakuradc1abe2016-05-13 11:04:47 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 // Make sure the user hasn't provided a preferred execution language
274 // with `expression --language X -- ...`
275 if (frame_sp && frame_lang == lldb::eLanguageTypeUnknown)
276 frame_lang = frame_sp->GetLanguage();
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 if (process_sp && frame_lang != lldb::eLanguageTypeUnknown) {
279 lang_rt = process_sp->GetLanguageRuntime(frame_lang);
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000280 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 log->Printf("Frame has language of type %s",
282 Language::GetNameForLanguageType(frame_lang));
283 }
Alp Toker5f838642014-07-06 05:36:57 +0000284
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 // 2. Configure the compiler with a set of default options that are
286 // appropriate
287 // for most situations.
288 if (target_arch.IsValid()) {
289 std::string triple = target_arch.GetTriple().str();
290 m_compiler->getTargetOpts().Triple = triple;
291 if (log)
292 log->Printf("Using %s as the target triple",
293 m_compiler->getTargetOpts().Triple.c_str());
294 } else {
295 // If we get here we don't have a valid target and just have to guess.
296 // Sometimes this will be ok to just use the host target triple (when we
297 // evaluate say "2+3", but other
298 // expressions like breakpoint conditions and other things that _are_ target
299 // specific really shouldn't just be
300 // using the host triple. In such a case the language runtime should expose
301 // an overridden options set (3),
302 // below.
303 m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
304 if (log)
305 log->Printf("Using default target triple of %s",
306 m_compiler->getTargetOpts().Triple.c_str());
307 }
308 // Now add some special fixes for known architectures:
309 // Any arm32 iOS environment, but not on arm64
310 if (m_compiler->getTargetOpts().Triple.find("arm64") == std::string::npos &&
311 m_compiler->getTargetOpts().Triple.find("arm") != std::string::npos &&
312 m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos) {
313 m_compiler->getTargetOpts().ABI = "apcs-gnu";
314 }
315 // Supported subsets of x86
316 if (target_machine == llvm::Triple::x86 ||
317 target_machine == llvm::Triple::x86_64) {
318 m_compiler->getTargetOpts().Features.push_back("+sse");
319 m_compiler->getTargetOpts().Features.push_back("+sse2");
320 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000321
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322 // Set the target CPU to generate code for.
323 // This will be empty for any CPU that doesn't really need to make a special
324 // CPU string.
325 m_compiler->getTargetOpts().CPU = target_arch.GetClangTargetCPU();
Ewan Crawford7648dd32016-03-10 12:38:55 +0000326
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327 // Set the target ABI
328 abi = GetClangTargetABI(target_arch);
329 if (!abi.empty())
330 m_compiler->getTargetOpts().ABI = abi;
331
332 // 3. Now allow the runtime to provide custom configuration options for the
333 // target.
334 // In this case, a specialized language runtime is available and we can query
335 // it for extra options.
336 // For 99% of use cases, this will not be needed and should be provided when
337 // basic platform detection is not enough.
338 if (lang_rt)
339 overridden_target_opts =
340 lang_rt->GetOverrideExprOptions(m_compiler->getTargetOpts());
341
342 if (overridden_target_opts)
343 if (log) {
344 log->Debug(
345 "Using overridden target options for the expression evaluation");
346
347 auto opts = m_compiler->getTargetOpts();
348 log->Debug("Triple: '%s'", opts.Triple.c_str());
349 log->Debug("CPU: '%s'", opts.CPU.c_str());
350 log->Debug("FPMath: '%s'", opts.FPMath.c_str());
351 log->Debug("ABI: '%s'", opts.ABI.c_str());
352 log->Debug("LinkerVersion: '%s'", opts.LinkerVersion.c_str());
353 StringList::LogDump(log, opts.FeaturesAsWritten, "FeaturesAsWritten");
354 StringList::LogDump(log, opts.Features, "Features");
355 StringList::LogDump(log, opts.Reciprocals, "Reciprocals");
Sean Callananc7b65062011-11-07 23:35:40 +0000356 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000357
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358 // 4. Create and install the target on the compiler.
359 m_compiler->createDiagnostics();
360 auto target_info = TargetInfo::CreateTargetInfo(
361 m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts);
362 if (log) {
363 log->Printf("Using SIMD alignment: %d", target_info->getSimdDefaultAlign());
364 log->Printf("Target datalayout string: '%s'",
365 target_info->getDataLayout().getStringRepresentation().c_str());
366 log->Printf("Target ABI: '%s'", target_info->getABI().str().c_str());
367 log->Printf("Target vector alignment: %d",
368 target_info->getMaxVectorAlign());
369 }
370 m_compiler->setTarget(target_info);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000371
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372 assert(m_compiler->hasTarget());
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000373
Kate Stoneb9c1b512016-09-06 20:57:50 +0000374 // 5. Set language options.
375 lldb::LanguageType language = expr.Language();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000376
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 switch (language) {
378 case lldb::eLanguageTypeC:
379 case lldb::eLanguageTypeC89:
380 case lldb::eLanguageTypeC99:
381 case lldb::eLanguageTypeC11:
382 // FIXME: the following language option is a temporary workaround,
383 // to "ask for C, get C++."
384 // For now, the expression parser must use C++ anytime the
385 // language is a C family language, because the expression parser
386 // uses features of C++ to capture values.
387 m_compiler->getLangOpts().CPlusPlus = true;
388 break;
389 case lldb::eLanguageTypeObjC:
390 m_compiler->getLangOpts().ObjC1 = true;
391 m_compiler->getLangOpts().ObjC2 = true;
392 // FIXME: the following language option is a temporary workaround,
393 // to "ask for ObjC, get ObjC++" (see comment above).
394 m_compiler->getLangOpts().CPlusPlus = true;
395 break;
396 case lldb::eLanguageTypeC_plus_plus:
397 case lldb::eLanguageTypeC_plus_plus_11:
398 case lldb::eLanguageTypeC_plus_plus_14:
399 m_compiler->getLangOpts().CPlusPlus11 = true;
400 m_compiler->getHeaderSearchOpts().UseLibcxx = true;
401 LLVM_FALLTHROUGH;
402 case lldb::eLanguageTypeC_plus_plus_03:
403 m_compiler->getLangOpts().CPlusPlus = true;
404 // FIXME: the following language option is a temporary workaround,
405 // to "ask for C++, get ObjC++". Apple hopes to remove this requirement
406 // on non-Apple platforms, but for now it is needed.
407 m_compiler->getLangOpts().ObjC1 = true;
408 break;
409 case lldb::eLanguageTypeObjC_plus_plus:
410 case lldb::eLanguageTypeUnknown:
411 default:
412 m_compiler->getLangOpts().ObjC1 = true;
413 m_compiler->getLangOpts().ObjC2 = true;
414 m_compiler->getLangOpts().CPlusPlus = true;
415 m_compiler->getLangOpts().CPlusPlus11 = true;
416 m_compiler->getHeaderSearchOpts().UseLibcxx = true;
417 break;
418 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 m_compiler->getLangOpts().Bool = true;
421 m_compiler->getLangOpts().WChar = true;
422 m_compiler->getLangOpts().Blocks = true;
423 m_compiler->getLangOpts().DebuggerSupport =
424 true; // Features specifically for debugger clients
425 if (expr.DesiredResultType() == Expression::eResultTypeId)
426 m_compiler->getLangOpts().DebuggerCastResultToId = true;
427
428 m_compiler->getLangOpts().CharIsSigned =
429 ArchSpec(m_compiler->getTargetOpts().Triple.c_str())
430 .CharIsSignedByDefault();
431
432 // Spell checking is a nice feature, but it ends up completing a
433 // lot of types that we didn't strictly speaking need to complete.
434 // As a result, we spend a long time parsing and importing debug
435 // information.
436 m_compiler->getLangOpts().SpellChecking = false;
437
438 if (process_sp && m_compiler->getLangOpts().ObjC1) {
439 if (process_sp->GetObjCLanguageRuntime()) {
440 if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() ==
441 ObjCLanguageRuntime::ObjCRuntimeVersions::eAppleObjC_V2)
442 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX,
443 VersionTuple(10, 7));
444 else
445 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX,
446 VersionTuple(10, 7));
447
448 if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
449 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
Sean Callananc3a16002011-01-17 23:42:46 +0000450 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 }
Greg Claytonf83f32d2011-01-15 01:32:14 +0000452
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453 m_compiler->getLangOpts().ThreadsafeStatics = false;
454 m_compiler->getLangOpts().AccessControl =
455 false; // Debuggers get universal access
456 m_compiler->getLangOpts().DollarIdents =
457 true; // $ indicates a persistent variable name
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000458
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 // Set CodeGen options
460 m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
461 m_compiler->getCodeGenOpts().InstrumentFunctions = false;
462 m_compiler->getCodeGenOpts().DisableFPElim = true;
463 m_compiler->getCodeGenOpts().OmitLeafFramePointer = false;
464 if (generate_debug_info)
465 m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::FullDebugInfo);
466 else
467 m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::NoDebugInfo);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000468
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469 // Disable some warnings.
470 m_compiler->getDiagnostics().setSeverityForGroup(
471 clang::diag::Flavor::WarningOrError, "unused-value",
472 clang::diag::Severity::Ignored, SourceLocation());
473 m_compiler->getDiagnostics().setSeverityForGroup(
474 clang::diag::Flavor::WarningOrError, "odr",
475 clang::diag::Severity::Ignored, SourceLocation());
Alp Toker10933d12014-06-12 11:14:32 +0000476
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477 // Inform the target of the language options
478 //
479 // FIXME: We shouldn't need to do this, the target should be immutable once
480 // created. This complexity should be lifted elsewhere.
481 m_compiler->getTarget().adjust(m_compiler->getLangOpts());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000482
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483 // 6. Set up the diagnostic buffer for reporting errors
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000484
Kate Stoneb9c1b512016-09-06 20:57:50 +0000485 m_compiler->getDiagnostics().setClient(new ClangDiagnosticManagerAdapter);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000486
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487 // 7. Set up the source management objects inside the compiler
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000488
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489 clang::FileSystemOptions file_system_options;
490 m_file_manager.reset(new clang::FileManager(file_system_options));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000491
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492 if (!m_compiler->hasSourceManager())
493 m_compiler->createSourceManager(*m_file_manager.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000494
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495 m_compiler->createFileManager();
496 m_compiler->createPreprocessor(TU_Complete);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000497
Kate Stoneb9c1b512016-09-06 20:57:50 +0000498 if (ClangModulesDeclVendor *decl_vendor =
499 target_sp->GetClangModulesDeclVendor()) {
500 ClangPersistentVariables *clang_persistent_vars =
501 llvm::cast<ClangPersistentVariables>(
502 target_sp->GetPersistentExpressionStateForLanguage(
503 lldb::eLanguageTypeC));
504 std::unique_ptr<PPCallbacks> pp_callbacks(
505 new LLDBPreprocessorCallbacks(*decl_vendor, *clang_persistent_vars));
506 m_pp_callbacks =
507 static_cast<LLDBPreprocessorCallbacks *>(pp_callbacks.get());
508 m_compiler->getPreprocessor().addPPCallbacks(std::move(pp_callbacks));
509 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000510
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511 // 8. Most of this we get from the CompilerInstance, but we
512 // also want to give the context an ExternalASTSource.
513 m_selector_table.reset(new SelectorTable());
514 m_builtin_context.reset(new Builtin::Context());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000515
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 std::unique_ptr<clang::ASTContext> ast_context(
517 new ASTContext(m_compiler->getLangOpts(), m_compiler->getSourceManager(),
518 m_compiler->getPreprocessor().getIdentifierTable(),
519 *m_selector_table.get(), *m_builtin_context.get()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000520
Kate Stoneb9c1b512016-09-06 20:57:50 +0000521 ast_context->InitBuiltinTypes(m_compiler->getTarget());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000522
Kate Stoneb9c1b512016-09-06 20:57:50 +0000523 ClangExpressionHelper *type_system_helper =
524 dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
525 ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000526
Kate Stoneb9c1b512016-09-06 20:57:50 +0000527 if (decl_map) {
528 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source(
529 decl_map->CreateProxy());
530 decl_map->InstallASTContext(ast_context.get());
531 ast_context->setExternalSource(ast_source);
532 }
533
534 m_ast_context.reset(
535 new ClangASTContext(m_compiler->getTargetOpts().Triple.c_str()));
536 m_ast_context->setASTContext(ast_context.get());
537 m_compiler->setASTContext(ast_context.release());
538
539 std::string module_name("$__lldb_module");
540
541 m_llvm_context.reset(new LLVMContext());
542 m_code_generator.reset(CreateLLVMCodeGen(
543 m_compiler->getDiagnostics(), module_name,
544 m_compiler->getHeaderSearchOpts(), m_compiler->getPreprocessorOpts(),
545 m_compiler->getCodeGenOpts(), *m_llvm_context));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000546}
547
Kate Stoneb9c1b512016-09-06 20:57:50 +0000548ClangExpressionParser::~ClangExpressionParser() {}
Sean Callanan1a8d4092010-08-27 01:01:44 +0000549
Kate Stoneb9c1b512016-09-06 20:57:50 +0000550unsigned ClangExpressionParser::Parse(DiagnosticManager &diagnostic_manager) {
551 ClangDiagnosticManagerAdapter *adapter =
552 static_cast<ClangDiagnosticManagerAdapter *>(
553 m_compiler->getDiagnostics().getClient());
554 clang::TextDiagnosticBuffer *diag_buf = adapter->GetPassthrough();
555 diag_buf->FlushDiagnostics(m_compiler->getDiagnostics());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000556
Kate Stoneb9c1b512016-09-06 20:57:50 +0000557 adapter->ResetManager(&diagnostic_manager);
Sean Callanan579e70c2016-03-19 00:03:59 +0000558
Kate Stoneb9c1b512016-09-06 20:57:50 +0000559 const char *expr_text = m_expr.Text();
Rafael Espindola9cb97b62014-05-21 15:08:27 +0000560
Kate Stoneb9c1b512016-09-06 20:57:50 +0000561 clang::SourceManager &source_mgr = m_compiler->getSourceManager();
562 bool created_main_file = false;
563 if (m_compiler->getCodeGenOpts().getDebugInfo() ==
564 codegenoptions::FullDebugInfo) {
565 int temp_fd = -1;
566 llvm::SmallString<PATH_MAX> result_path;
567 FileSpec tmpdir_file_spec;
568 if (HostInfo::GetLLDBPath(lldb::ePathTypeLLDBTempSystemDir,
569 tmpdir_file_spec)) {
570 tmpdir_file_spec.AppendPathComponent("lldb-%%%%%%.expr");
571 std::string temp_source_path = tmpdir_file_spec.GetPath();
572 llvm::sys::fs::createUniqueFile(temp_source_path, temp_fd, result_path);
573 } else {
574 llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path);
575 }
576
577 if (temp_fd != -1) {
578 lldb_private::File file(temp_fd, true);
579 const size_t expr_text_len = strlen(expr_text);
580 size_t bytes_written = expr_text_len;
581 if (file.Write(expr_text, bytes_written).Success()) {
582 if (bytes_written == expr_text_len) {
583 file.Close();
584 source_mgr.setMainFileID(
585 source_mgr.createFileID(m_file_manager->getFile(result_path),
586 SourceLocation(), SrcMgr::C_User));
587 created_main_file = true;
Greg Clayton23f8c952014-03-24 23:10:19 +0000588 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000590 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000592
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593 if (!created_main_file) {
594 std::unique_ptr<MemoryBuffer> memory_buffer =
595 MemoryBuffer::getMemBufferCopy(expr_text, __FUNCTION__);
596 source_mgr.setMainFileID(source_mgr.createFileID(std::move(memory_buffer)));
597 }
598
599 diag_buf->BeginSourceFile(m_compiler->getLangOpts(),
600 &m_compiler->getPreprocessor());
601
602 ClangExpressionHelper *type_system_helper =
603 dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
604
605 ASTConsumer *ast_transformer =
606 type_system_helper->ASTTransformer(m_code_generator.get());
607
608 if (ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap())
609 decl_map->InstallCodeGenerator(m_code_generator.get());
610
611 if (ast_transformer) {
612 ast_transformer->Initialize(m_compiler->getASTContext());
613 ParseAST(m_compiler->getPreprocessor(), ast_transformer,
614 m_compiler->getASTContext());
615 } else {
616 m_code_generator->Initialize(m_compiler->getASTContext());
617 ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(),
618 m_compiler->getASTContext());
619 }
620
621 diag_buf->EndSourceFile();
622
623 unsigned num_errors = diag_buf->getNumErrors();
624
625 if (m_pp_callbacks && m_pp_callbacks->hasErrors()) {
626 num_errors++;
Zachary Turnere2411fa2016-11-12 19:12:56 +0000627 diagnostic_manager.PutString(eDiagnosticSeverityError,
628 "while importing modules:");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000629 diagnostic_manager.AppendMessageToDiagnostic(
Zachary Turnerc1564272016-11-16 21:15:24 +0000630 m_pp_callbacks->getErrorString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 }
632
633 if (!num_errors) {
634 if (type_system_helper->DeclMap() &&
635 !type_system_helper->DeclMap()->ResolveUnknownTypes()) {
636 diagnostic_manager.Printf(eDiagnosticSeverityError,
637 "Couldn't infer the type of a variable");
638 num_errors++;
Greg Clayton23f8c952014-03-24 23:10:19 +0000639 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000641
Kate Stoneb9c1b512016-09-06 20:57:50 +0000642 if (!num_errors) {
643 type_system_helper->CommitPersistentDecls();
644 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000645
Kate Stoneb9c1b512016-09-06 20:57:50 +0000646 adapter->ResetManager();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000647
Kate Stoneb9c1b512016-09-06 20:57:50 +0000648 return num_errors;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000649}
650
Sagar Thakuradc1abe2016-05-13 11:04:47 +0000651std::string
Kate Stoneb9c1b512016-09-06 20:57:50 +0000652ClangExpressionParser::GetClangTargetABI(const ArchSpec &target_arch) {
653 std::string abi;
654
655 if (target_arch.IsMIPS()) {
656 switch (target_arch.GetFlags() & ArchSpec::eMIPSABI_mask) {
657 case ArchSpec::eMIPSABI_N64:
658 abi = "n64";
659 break;
660 case ArchSpec::eMIPSABI_N32:
661 abi = "n32";
662 break;
663 case ArchSpec::eMIPSABI_O32:
664 abi = "o32";
665 break;
666 default:
667 break;
Sagar Thakuradc1abe2016-05-13 11:04:47 +0000668 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669 }
670 return abi;
Sagar Thakuradc1abe2016-05-13 11:04:47 +0000671}
672
Kate Stoneb9c1b512016-09-06 20:57:50 +0000673bool ClangExpressionParser::RewriteExpression(
674 DiagnosticManager &diagnostic_manager) {
675 clang::SourceManager &source_manager = m_compiler->getSourceManager();
676 clang::edit::EditedSource editor(source_manager, m_compiler->getLangOpts(),
677 nullptr);
678 clang::edit::Commit commit(editor);
679 clang::Rewriter rewriter(source_manager, m_compiler->getLangOpts());
Jim Inghama1e541b2016-03-25 01:57:14 +0000680
Kate Stoneb9c1b512016-09-06 20:57:50 +0000681 class RewritesReceiver : public edit::EditsReceiver {
682 Rewriter &rewrite;
Jim Inghama1e541b2016-03-25 01:57:14 +0000683
Kate Stoneb9c1b512016-09-06 20:57:50 +0000684 public:
685 RewritesReceiver(Rewriter &in_rewrite) : rewrite(in_rewrite) {}
686
687 void insert(SourceLocation loc, StringRef text) override {
688 rewrite.InsertText(loc, text);
Jim Inghama1e541b2016-03-25 01:57:14 +0000689 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000690 void replace(CharSourceRange range, StringRef text) override {
691 rewrite.ReplaceText(range.getBegin(), rewrite.getRangeSize(range), text);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000692 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000693 };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000694
Kate Stoneb9c1b512016-09-06 20:57:50 +0000695 RewritesReceiver rewrites_receiver(rewriter);
696
697 const DiagnosticList &diagnostics = diagnostic_manager.Diagnostics();
698 size_t num_diags = diagnostics.size();
699 if (num_diags == 0)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000700 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000701
702 for (const Diagnostic *diag : diagnostic_manager.Diagnostics()) {
703 const ClangDiagnostic *diagnostic = llvm::dyn_cast<ClangDiagnostic>(diag);
704 if (diagnostic && diagnostic->HasFixIts()) {
705 for (const FixItHint &fixit : diagnostic->FixIts()) {
706 // This is cobbed from clang::Rewrite::FixItRewriter.
707 if (fixit.CodeToInsert.empty()) {
708 if (fixit.InsertFromRange.isValid()) {
709 commit.insertFromRange(fixit.RemoveRange.getBegin(),
710 fixit.InsertFromRange, /*afterToken=*/false,
711 fixit.BeforePreviousInsertions);
712 } else
713 commit.remove(fixit.RemoveRange);
714 } else {
715 if (fixit.RemoveRange.isTokenRange() ||
716 fixit.RemoveRange.getBegin() != fixit.RemoveRange.getEnd())
717 commit.replace(fixit.RemoveRange, fixit.CodeToInsert);
718 else
719 commit.insert(fixit.RemoveRange.getBegin(), fixit.CodeToInsert,
720 /*afterToken=*/false, fixit.BeforePreviousInsertions);
721 }
722 }
723 }
724 }
725
726 // FIXME - do we want to try to propagate specific errors here?
727 if (!commit.isCommitable())
728 return false;
729 else if (!editor.commit(commit))
730 return false;
731
732 // Now play all the edits, and stash the result in the diagnostic manager.
733 editor.applyRewrites(rewrites_receiver);
734 RewriteBuffer &main_file_buffer =
735 rewriter.getEditBuffer(source_manager.getMainFileID());
736
737 std::string fixed_expression;
738 llvm::raw_string_ostream out_stream(fixed_expression);
739
740 main_file_buffer.write(out_stream);
741 out_stream.flush();
742 diagnostic_manager.SetFixedExpression(fixed_expression);
743
744 return true;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000745}
746
Kate Stoneb9c1b512016-09-06 20:57:50 +0000747static bool FindFunctionInModule(ConstString &mangled_name,
748 llvm::Module *module, const char *orig_name) {
749 for (const auto &func : module->getFunctionList()) {
750 const StringRef &name = func.getName();
751 if (name.find(orig_name) != StringRef::npos) {
752 mangled_name.SetString(name);
753 return true;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000754 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000755 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000756
Kate Stoneb9c1b512016-09-06 20:57:50 +0000757 return false;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000758}
Sean Callananbd4dc692016-03-21 22:23:38 +0000759
Kate Stoneb9c1b512016-09-06 20:57:50 +0000760lldb_private::Error ClangExpressionParser::PrepareForExecution(
761 lldb::addr_t &func_addr, lldb::addr_t &func_end,
762 lldb::IRExecutionUnitSP &execution_unit_sp, ExecutionContext &exe_ctx,
763 bool &can_interpret, ExecutionPolicy execution_policy) {
764 func_addr = LLDB_INVALID_ADDRESS;
765 func_end = LLDB_INVALID_ADDRESS;
766 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
767
768 lldb_private::Error err;
769
770 std::unique_ptr<llvm::Module> llvm_module_ap(
771 m_code_generator->ReleaseModule());
772
773 if (!llvm_module_ap.get()) {
774 err.SetErrorToGenericError();
775 err.SetErrorString("IR doesn't contain a module");
Sean Callananbd4dc692016-03-21 22:23:38 +0000776 return err;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000777 }
778
779 ConstString function_name;
780
781 if (execution_policy != eExecutionPolicyTopLevel) {
782 // Find the actual name of the function (it's often mangled somehow)
783
784 if (!FindFunctionInModule(function_name, llvm_module_ap.get(),
785 m_expr.FunctionName())) {
786 err.SetErrorToGenericError();
787 err.SetErrorStringWithFormat("Couldn't find %s() in the module",
788 m_expr.FunctionName());
789 return err;
790 } else {
791 if (log)
792 log->Printf("Found function %s for %s", function_name.AsCString(),
793 m_expr.FunctionName());
794 }
795 }
796
797 SymbolContext sc;
798
799 if (lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP()) {
800 sc = frame_sp->GetSymbolContext(lldb::eSymbolContextEverything);
801 } else if (lldb::TargetSP target_sp = exe_ctx.GetTargetSP()) {
802 sc.target_sp = target_sp;
803 }
804
805 LLVMUserExpression::IRPasses custom_passes;
806 {
807 auto lang = m_expr.Language();
808 if (log)
809 log->Printf("%s - Currrent expression language is %s\n", __FUNCTION__,
810 Language::GetNameForLanguageType(lang));
Jim Ingham22562152016-11-03 23:42:09 +0000811 lldb::ProcessSP process_sp = exe_ctx.GetProcessSP();
812 if (process_sp && lang != lldb::eLanguageTypeUnknown) {
813 auto runtime = process_sp->GetLanguageRuntime(lang);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000814 if (runtime)
815 runtime->GetIRPasses(custom_passes);
816 }
817 }
818
819 if (custom_passes.EarlyPasses) {
820 if (log)
821 log->Printf("%s - Running Early IR Passes from LanguageRuntime on "
822 "expression module '%s'",
823 __FUNCTION__, m_expr.FunctionName());
824
825 custom_passes.EarlyPasses->run(*llvm_module_ap);
826 }
827
828 execution_unit_sp.reset(
829 new IRExecutionUnit(m_llvm_context, // handed off here
830 llvm_module_ap, // handed off here
831 function_name, exe_ctx.GetTargetSP(), sc,
832 m_compiler->getTargetOpts().Features));
833
834 ClangExpressionHelper *type_system_helper =
835 dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
836 ClangExpressionDeclMap *decl_map =
837 type_system_helper->DeclMap(); // result can be NULL
838
839 if (decl_map) {
840 Stream *error_stream = NULL;
841 Target *target = exe_ctx.GetTargetPtr();
842 error_stream = target->GetDebugger().GetErrorFile().get();
843
844 IRForTarget ir_for_target(decl_map, m_expr.NeedsVariableResolution(),
845 *execution_unit_sp, *error_stream,
846 function_name.AsCString());
847
848 bool ir_can_run =
849 ir_for_target.runOnModule(*execution_unit_sp->GetModule());
850
851 if (!ir_can_run) {
852 err.SetErrorString(
853 "The expression could not be prepared to run in the target");
854 return err;
855 }
856
857 Process *process = exe_ctx.GetProcessPtr();
858
859 if (execution_policy != eExecutionPolicyAlways &&
860 execution_policy != eExecutionPolicyTopLevel) {
861 lldb_private::Error interpret_error;
862
863 bool interpret_function_calls =
864 !process ? false : process->CanInterpretFunctionCalls();
865 can_interpret = IRInterpreter::CanInterpret(
866 *execution_unit_sp->GetModule(), *execution_unit_sp->GetFunction(),
867 interpret_error, interpret_function_calls);
868
869 if (!can_interpret && execution_policy == eExecutionPolicyNever) {
870 err.SetErrorStringWithFormat("Can't run the expression locally: %s",
871 interpret_error.AsCString());
872 return err;
873 }
874 }
875
876 if (!process && execution_policy == eExecutionPolicyAlways) {
877 err.SetErrorString("Expression needed to run in the target, but the "
878 "target can't be run");
879 return err;
880 }
881
882 if (!process && execution_policy == eExecutionPolicyTopLevel) {
883 err.SetErrorString("Top-level code needs to be inserted into a runnable "
884 "target, but the target can't be run");
885 return err;
886 }
887
888 if (execution_policy == eExecutionPolicyAlways ||
889 (execution_policy != eExecutionPolicyTopLevel && !can_interpret)) {
890 if (m_expr.NeedsValidation() && process) {
891 if (!process->GetDynamicCheckers()) {
892 DynamicCheckerFunctions *dynamic_checkers =
893 new DynamicCheckerFunctions();
894
895 DiagnosticManager install_diagnostics;
896
897 if (!dynamic_checkers->Install(install_diagnostics, exe_ctx)) {
898 if (install_diagnostics.Diagnostics().size())
899 err.SetErrorString("couldn't install checkers, unknown error");
900 else
901 err.SetErrorString(install_diagnostics.GetString().c_str());
902
903 return err;
904 }
905
906 process->SetDynamicCheckers(dynamic_checkers);
907
908 if (log)
909 log->Printf("== [ClangUserExpression::Evaluate] Finished "
910 "installing dynamic checkers ==");
911 }
912
913 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(),
914 function_name.AsCString());
915
916 llvm::Module *module = execution_unit_sp->GetModule();
917 if (!module || !ir_dynamic_checks.runOnModule(*module)) {
918 err.SetErrorToGenericError();
919 err.SetErrorString("Couldn't add dynamic checks to the expression");
920 return err;
921 }
922
923 if (custom_passes.LatePasses) {
924 if (log)
925 log->Printf("%s - Running Late IR Passes from LanguageRuntime on "
926 "expression module '%s'",
927 __FUNCTION__, m_expr.FunctionName());
928
929 custom_passes.LatePasses->run(*module);
930 }
931 }
932 }
933
934 if (execution_policy == eExecutionPolicyAlways ||
935 execution_policy == eExecutionPolicyTopLevel || !can_interpret) {
936 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
937 }
938 } else {
939 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
940 }
941
942 return err;
943}
944
945lldb_private::Error ClangExpressionParser::RunStaticInitializers(
946 lldb::IRExecutionUnitSP &execution_unit_sp, ExecutionContext &exe_ctx) {
947 lldb_private::Error err;
948
949 lldbassert(execution_unit_sp.get());
950 lldbassert(exe_ctx.HasThreadScope());
951
952 if (!execution_unit_sp.get()) {
953 err.SetErrorString(
954 "can't run static initializers for a NULL execution unit");
955 return err;
956 }
957
958 if (!exe_ctx.HasThreadScope()) {
959 err.SetErrorString("can't run static initializers without a thread");
960 return err;
961 }
962
963 std::vector<lldb::addr_t> static_initializers;
964
965 execution_unit_sp->GetStaticInitializers(static_initializers);
966
967 for (lldb::addr_t static_initializer : static_initializers) {
968 EvaluateExpressionOptions options;
969
970 lldb::ThreadPlanSP call_static_initializer(new ThreadPlanCallFunction(
971 exe_ctx.GetThreadRef(), Address(static_initializer), CompilerType(),
972 llvm::ArrayRef<lldb::addr_t>(), options));
973
974 DiagnosticManager execution_errors;
975 lldb::ExpressionResults results =
976 exe_ctx.GetThreadRef().GetProcess()->RunThreadPlan(
977 exe_ctx, call_static_initializer, options, execution_errors);
978
979 if (results != lldb::eExpressionCompleted) {
980 err.SetErrorStringWithFormat("couldn't run static initializer: %s",
981 execution_errors.GetString().c_str());
982 return err;
983 }
984 }
985
986 return err;
Sean Callananbd4dc692016-03-21 22:23:38 +0000987}