blob: 45812032cac40329f97aad01746eae74ba1df6f9 [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
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000010#include <cctype>
Sean Callanan1a8d4092010-08-27 01:01:44 +000011#include "clang/AST/ASTContext.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000012#include "clang/AST/ASTDiagnostic.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000013#include "clang/AST/ExternalASTSource.h"
Raphael Isemann9dd34c82018-09-17 12:06:07 +000014#include "clang/AST/PrettyPrinter.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000015#include "clang/Basic/DiagnosticIDs.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000016#include "clang/Basic/FileManager.h"
Jim Ingham151c0322015-09-15 21:13:50 +000017#include "clang/Basic/SourceLocation.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000018#include "clang/Basic/TargetInfo.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000019#include "clang/Basic/Version.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000020#include "clang/CodeGen/CodeGenAction.h"
21#include "clang/CodeGen/ModuleBuilder.h"
Jim Inghama1e541b2016-03-25 01:57:14 +000022#include "clang/Edit/Commit.h"
Jim Inghama1e541b2016-03-25 01:57:14 +000023#include "clang/Edit/EditedSource.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000024#include "clang/Edit/EditsReceiver.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000025#include "clang/Frontend/CompilerInstance.h"
26#include "clang/Frontend/CompilerInvocation.h"
27#include "clang/Frontend/FrontendActions.h"
28#include "clang/Frontend/FrontendDiagnostic.h"
29#include "clang/Frontend/FrontendPluginRegistry.h"
30#include "clang/Frontend/TextDiagnosticBuffer.h"
31#include "clang/Frontend/TextDiagnosticPrinter.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000032#include "clang/Lex/Preprocessor.h"
Sean Callanane2ef6e32010-09-23 03:01:22 +000033#include "clang/Parse/ParseAST.h"
Jim Inghama1e541b2016-03-25 01:57:14 +000034#include "clang/Rewrite/Core/Rewriter.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000035#include "clang/Rewrite/Frontend/FrontendActions.h"
Raphael Isemann74829732018-08-30 17:29:37 +000036#include "clang/Sema/CodeCompleteConsumer.h"
37#include "clang/Sema/Sema.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000038#include "clang/Sema/SemaConsumer.h"
39
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
Jim Inghama1e541b2016-03-25 01:57:14 +000059#include "ClangDiagnostic.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000060#include "ClangExpressionParser.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000061
62#include "ClangASTSource.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000063#include "ClangExpressionDeclMap.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000064#include "ClangExpressionHelper.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000065#include "ClangModulesDeclVendor.h"
66#include "ClangPersistentVariables.h"
67#include "IRForTarget.h"
68
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000069#include "lldb/Core/Debugger.h"
70#include "lldb/Core/Disassembler.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000071#include "lldb/Core/Module.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000072#include "lldb/Core/StreamFile.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000073#include "lldb/Expression/IRDynamicChecks.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000074#include "lldb/Expression/IRExecutionUnit.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000075#include "lldb/Expression/IRInterpreter.h"
76#include "lldb/Host/File.h"
77#include "lldb/Host/HostInfo.h"
78#include "lldb/Symbol/ClangASTContext.h"
79#include "lldb/Symbol/SymbolVendor.h"
80#include "lldb/Target/ExecutionContext.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000081#include "lldb/Target/Language.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000082#include "lldb/Target/ObjCLanguageRuntime.h"
83#include "lldb/Target/Process.h"
84#include "lldb/Target/Target.h"
Sean Callananbd4dc692016-03-21 22:23:38 +000085#include "lldb/Target/ThreadPlanCallFunction.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000086#include "lldb/Utility/DataBufferHeap.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000087#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000088#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000089#include "lldb/Utility/Stream.h"
90#include "lldb/Utility/StreamString.h"
Zachary Turner573ab902017-03-21 18:25:04 +000091#include "lldb/Utility/StringList.h"
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +000092
Sean Callanan1a8d4092010-08-27 01:01:44 +000093using namespace clang;
94using namespace llvm;
95using namespace lldb_private;
96
97//===----------------------------------------------------------------------===//
98// Utility Methods for Clang
99//===----------------------------------------------------------------------===//
100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101class ClangExpressionParser::LLDBPreprocessorCallbacks : public PPCallbacks {
102 ClangModulesDeclVendor &m_decl_vendor;
103 ClangPersistentVariables &m_persistent_vars;
104 StreamString m_error_stream;
105 bool m_has_errors = false;
Eugene Zelenko4c3f2b92015-10-21 01:03:30 +0000106
Sean Callananc8278af2014-12-05 01:27:35 +0000107public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 LLDBPreprocessorCallbacks(ClangModulesDeclVendor &decl_vendor,
109 ClangPersistentVariables &persistent_vars)
110 : m_decl_vendor(decl_vendor), m_persistent_vars(persistent_vars) {}
111
112 void moduleImport(SourceLocation import_location, clang::ModuleIdPath path,
113 const clang::Module * /*null*/) override {
114 std::vector<ConstString> string_path;
115
116 for (const std::pair<IdentifierInfo *, SourceLocation> &component : path) {
117 string_path.push_back(ConstString(component.first->getName()));
Sean Callananc8278af2014-12-05 01:27:35 +0000118 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119
120 StreamString error_stream;
121
122 ClangModulesDeclVendor::ModuleVector exported_modules;
123
124 if (!m_decl_vendor.AddModule(string_path, &exported_modules,
125 m_error_stream)) {
126 m_has_errors = true;
Sean Callananc8278af2014-12-05 01:27:35 +0000127 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128
129 for (ClangModulesDeclVendor::ModuleID module : exported_modules) {
130 m_persistent_vars.AddHandLoadedClangModule(module);
Sean Callananc8278af2014-12-05 01:27:35 +0000131 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 }
133
134 bool hasErrors() { return m_has_errors; }
135
Zachary Turnerc1564272016-11-16 21:15:24 +0000136 llvm::StringRef getErrorString() { return m_error_stream.GetString(); }
Sean Callananc8278af2014-12-05 01:27:35 +0000137};
138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139class ClangDiagnosticManagerAdapter : public clang::DiagnosticConsumer {
Sean Callanan579e70c2016-03-19 00:03:59 +0000140public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 ClangDiagnosticManagerAdapter()
142 : m_passthrough(new clang::TextDiagnosticBuffer) {}
Sean Callanan579e70c2016-03-19 00:03:59 +0000143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 ClangDiagnosticManagerAdapter(
145 const std::shared_ptr<clang::TextDiagnosticBuffer> &passthrough)
146 : m_passthrough(passthrough) {}
Sean Callanan579e70c2016-03-19 00:03:59 +0000147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 void ResetManager(DiagnosticManager *manager = nullptr) {
149 m_manager = manager;
150 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000151
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
153 const clang::Diagnostic &Info) {
154 if (m_manager) {
155 llvm::SmallVector<char, 32> diag_str;
156 Info.FormatDiagnostic(diag_str);
157 diag_str.push_back('\0');
158 const char *data = diag_str.data();
Sean Callanan579e70c2016-03-19 00:03:59 +0000159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 lldb_private::DiagnosticSeverity severity;
161 bool make_new_diagnostic = true;
162
163 switch (DiagLevel) {
164 case DiagnosticsEngine::Level::Fatal:
165 case DiagnosticsEngine::Level::Error:
166 severity = eDiagnosticSeverityError;
167 break;
168 case DiagnosticsEngine::Level::Warning:
169 severity = eDiagnosticSeverityWarning;
170 break;
171 case DiagnosticsEngine::Level::Remark:
172 case DiagnosticsEngine::Level::Ignored:
173 severity = eDiagnosticSeverityRemark;
174 break;
175 case DiagnosticsEngine::Level::Note:
176 m_manager->AppendMessageToDiagnostic(data);
177 make_new_diagnostic = false;
178 }
179 if (make_new_diagnostic) {
180 ClangDiagnostic *new_diagnostic =
181 new ClangDiagnostic(data, severity, Info.getID());
182 m_manager->AddDiagnostic(new_diagnostic);
183
184 // Don't store away warning fixits, since the compiler doesn't have
Adrian Prantl05097242018-04-30 16:49:04 +0000185 // enough context in an expression for the warning to be useful.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 // FIXME: Should we try to filter out FixIts that apply to our generated
187 // code, and not the user's expression?
188 if (severity == eDiagnosticSeverityError) {
189 size_t num_fixit_hints = Info.getNumFixItHints();
190 for (size_t i = 0; i < num_fixit_hints; i++) {
191 const clang::FixItHint &fixit = Info.getFixItHint(i);
192 if (!fixit.isNull())
193 new_diagnostic->AddFixitHint(fixit);
194 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000195 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000197 }
198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 m_passthrough->HandleDiagnostic(DiagLevel, Info);
200 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 void FlushDiagnostics(DiagnosticsEngine &Diags) {
203 m_passthrough->FlushDiagnostics(Diags);
204 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000205
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
207 return new ClangDiagnosticManagerAdapter(m_passthrough);
208 }
209
210 clang::TextDiagnosticBuffer *GetPassthrough() { return m_passthrough.get(); }
Sean Callanan579e70c2016-03-19 00:03:59 +0000211
212private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 DiagnosticManager *m_manager = nullptr;
214 std::shared_ptr<clang::TextDiagnosticBuffer> m_passthrough;
Sean Callanan579e70c2016-03-19 00:03:59 +0000215};
216
Sean Callanan1a8d4092010-08-27 01:01:44 +0000217//===----------------------------------------------------------------------===//
218// Implementation of ClangExpressionParser
219//===----------------------------------------------------------------------===//
220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221ClangExpressionParser::ClangExpressionParser(ExecutionContextScope *exe_scope,
222 Expression &expr,
223 bool generate_debug_info)
224 : ExpressionParser(exe_scope, expr, generate_debug_info), m_compiler(),
Adrian Prantl2305c042018-08-30 15:39:08 +0000225 m_pp_callbacks(nullptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000227
Adrian Prantl05097242018-04-30 16:49:04 +0000228 // We can't compile expressions without a target. So if the exe_scope is
229 // null or doesn't have a target, then we just need to get out of here. I'll
230 // lldb_assert and not make any of the compiler objects since
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 // I can't return errors directly from the constructor. Further calls will
232 // check if the compiler was made and
233 // bag out if it wasn't.
Sagar Thakuradc1abe2016-05-13 11:04:47 +0000234
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235 if (!exe_scope) {
236 lldb_assert(exe_scope, "Can't make an expression parser with a null scope.",
237 __FUNCTION__, __FILE__, __LINE__);
238 return;
239 }
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 lldb::TargetSP target_sp;
242 target_sp = exe_scope->CalculateTarget();
243 if (!target_sp) {
244 lldb_assert(target_sp.get(),
245 "Can't make an expression parser with a null target.",
246 __FUNCTION__, __FILE__, __LINE__);
247 return;
248 }
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000249
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 // 1. Create a new compiler instance.
251 m_compiler.reset(new CompilerInstance());
252 lldb::LanguageType frame_lang =
253 expr.Language(); // defaults to lldb::eLanguageTypeUnknown
254 bool overridden_target_opts = false;
255 lldb_private::LanguageRuntime *lang_rt = nullptr;
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000256
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257 std::string abi;
258 ArchSpec target_arch;
259 target_arch = target_sp->GetArchitecture();
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 const auto target_machine = target_arch.GetMachine();
Jason Molendaa3329782014-03-29 18:54:20 +0000262
Adrian Prantl05097242018-04-30 16:49:04 +0000263 // If the expression is being evaluated in the context of an existing stack
264 // frame, we introspect to see if the language runtime is available.
Bhushan D. Attarde3592a6e2016-02-18 11:53:28 +0000265
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266 lldb::StackFrameSP frame_sp = exe_scope->CalculateStackFrame();
267 lldb::ProcessSP process_sp = exe_scope->CalculateProcess();
Sagar Thakuradc1abe2016-05-13 11:04:47 +0000268
Adrian Prantl05097242018-04-30 16:49:04 +0000269 // Make sure the user hasn't provided a preferred execution language with
270 // `expression --language X -- ...`
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 if (frame_sp && frame_lang == lldb::eLanguageTypeUnknown)
272 frame_lang = frame_sp->GetLanguage();
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000273
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 if (process_sp && frame_lang != lldb::eLanguageTypeUnknown) {
275 lang_rt = process_sp->GetLanguageRuntime(frame_lang);
Aidan Dodds1b6785a2016-02-03 12:33:05 +0000276 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 log->Printf("Frame has language of type %s",
278 Language::GetNameForLanguageType(frame_lang));
279 }
Alp Toker5f838642014-07-06 05:36:57 +0000280
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 // 2. Configure the compiler with a set of default options that are
Adrian Prantl05097242018-04-30 16:49:04 +0000282 // appropriate for most situations.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283 if (target_arch.IsValid()) {
284 std::string triple = target_arch.GetTriple().str();
285 m_compiler->getTargetOpts().Triple = triple;
286 if (log)
287 log->Printf("Using %s as the target triple",
288 m_compiler->getTargetOpts().Triple.c_str());
289 } else {
290 // If we get here we don't have a valid target and just have to guess.
291 // Sometimes this will be ok to just use the host target triple (when we
Adrian Prantl05097242018-04-30 16:49:04 +0000292 // evaluate say "2+3", but other expressions like breakpoint conditions and
293 // other things that _are_ target specific really shouldn't just be using
294 // the host triple. In such a case the language runtime should expose an
295 // overridden options set (3), below.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000296 m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
297 if (log)
298 log->Printf("Using default target triple of %s",
299 m_compiler->getTargetOpts().Triple.c_str());
300 }
Adrian Prantl05097242018-04-30 16:49:04 +0000301 // Now add some special fixes for known architectures: Any arm32 iOS
302 // environment, but not on arm64
Kate Stoneb9c1b512016-09-06 20:57:50 +0000303 if (m_compiler->getTargetOpts().Triple.find("arm64") == std::string::npos &&
304 m_compiler->getTargetOpts().Triple.find("arm") != std::string::npos &&
305 m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos) {
306 m_compiler->getTargetOpts().ABI = "apcs-gnu";
307 }
308 // Supported subsets of x86
309 if (target_machine == llvm::Triple::x86 ||
310 target_machine == llvm::Triple::x86_64) {
311 m_compiler->getTargetOpts().Features.push_back("+sse");
312 m_compiler->getTargetOpts().Features.push_back("+sse2");
313 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000314
Adrian Prantl05097242018-04-30 16:49:04 +0000315 // Set the target CPU to generate code for. This will be empty for any CPU
316 // that doesn't really need to make a special
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317 // CPU string.
318 m_compiler->getTargetOpts().CPU = target_arch.GetClangTargetCPU();
Ewan Crawford7648dd32016-03-10 12:38:55 +0000319
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320 // Set the target ABI
321 abi = GetClangTargetABI(target_arch);
322 if (!abi.empty())
323 m_compiler->getTargetOpts().ABI = abi;
324
325 // 3. Now allow the runtime to provide custom configuration options for the
Adrian Prantl05097242018-04-30 16:49:04 +0000326 // target. In this case, a specialized language runtime is available and we
327 // can query it for extra options. For 99% of use cases, this will not be
328 // needed and should be provided when basic platform detection is not enough.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329 if (lang_rt)
330 overridden_target_opts =
331 lang_rt->GetOverrideExprOptions(m_compiler->getTargetOpts());
332
333 if (overridden_target_opts)
Pavel Labath6302bf62017-02-13 11:03:17 +0000334 if (log && log->GetVerbose()) {
335 LLDB_LOGV(
336 log, "Using overridden target options for the expression evaluation");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000337
338 auto opts = m_compiler->getTargetOpts();
Pavel Labath6302bf62017-02-13 11:03:17 +0000339 LLDB_LOGV(log, "Triple: '{0}'", opts.Triple);
340 LLDB_LOGV(log, "CPU: '{0}'", opts.CPU);
341 LLDB_LOGV(log, "FPMath: '{0}'", opts.FPMath);
342 LLDB_LOGV(log, "ABI: '{0}'", opts.ABI);
343 LLDB_LOGV(log, "LinkerVersion: '{0}'", opts.LinkerVersion);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 StringList::LogDump(log, opts.FeaturesAsWritten, "FeaturesAsWritten");
345 StringList::LogDump(log, opts.Features, "Features");
Sean Callananc7b65062011-11-07 23:35:40 +0000346 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000347
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 // 4. Create and install the target on the compiler.
349 m_compiler->createDiagnostics();
350 auto target_info = TargetInfo::CreateTargetInfo(
351 m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts);
352 if (log) {
353 log->Printf("Using SIMD alignment: %d", target_info->getSimdDefaultAlign());
354 log->Printf("Target datalayout string: '%s'",
355 target_info->getDataLayout().getStringRepresentation().c_str());
356 log->Printf("Target ABI: '%s'", target_info->getABI().str().c_str());
357 log->Printf("Target vector alignment: %d",
358 target_info->getMaxVectorAlign());
359 }
360 m_compiler->setTarget(target_info);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000361
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362 assert(m_compiler->hasTarget());
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000363
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364 // 5. Set language options.
365 lldb::LanguageType language = expr.Language();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 switch (language) {
368 case lldb::eLanguageTypeC:
369 case lldb::eLanguageTypeC89:
370 case lldb::eLanguageTypeC99:
371 case lldb::eLanguageTypeC11:
372 // FIXME: the following language option is a temporary workaround,
373 // to "ask for C, get C++."
Adrian Prantl05097242018-04-30 16:49:04 +0000374 // For now, the expression parser must use C++ anytime the language is a C
375 // family language, because the expression parser uses features of C++ to
376 // capture values.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 m_compiler->getLangOpts().CPlusPlus = true;
378 break;
379 case lldb::eLanguageTypeObjC:
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000380 m_compiler->getLangOpts().ObjC = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381 // FIXME: the following language option is a temporary workaround,
382 // to "ask for ObjC, get ObjC++" (see comment above).
383 m_compiler->getLangOpts().CPlusPlus = true;
Davide Italiano89419f82017-12-15 00:50:43 +0000384
385 // Clang now sets as default C++14 as the default standard (with
386 // GNU extensions), so we do the same here to avoid mismatches that
Adrian Prantl05097242018-04-30 16:49:04 +0000387 // cause compiler error when evaluating expressions (e.g. nullptr not found
388 // as it's a C++11 feature). Currently lldb evaluates C++14 as C++11 (see
389 // two lines below) so we decide to be consistent with that, but this could
390 // be re-evaluated in the future.
Davide Italiano89419f82017-12-15 00:50:43 +0000391 m_compiler->getLangOpts().CPlusPlus11 = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000392 break;
393 case lldb::eLanguageTypeC_plus_plus:
394 case lldb::eLanguageTypeC_plus_plus_11:
395 case lldb::eLanguageTypeC_plus_plus_14:
396 m_compiler->getLangOpts().CPlusPlus11 = true;
397 m_compiler->getHeaderSearchOpts().UseLibcxx = true;
398 LLVM_FALLTHROUGH;
399 case lldb::eLanguageTypeC_plus_plus_03:
400 m_compiler->getLangOpts().CPlusPlus = true;
401 // FIXME: the following language option is a temporary workaround,
Adrian Prantl05097242018-04-30 16:49:04 +0000402 // to "ask for C++, get ObjC++". Apple hopes to remove this requirement on
403 // non-Apple platforms, but for now it is needed.
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000404 m_compiler->getLangOpts().ObjC = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 break;
406 case lldb::eLanguageTypeObjC_plus_plus:
407 case lldb::eLanguageTypeUnknown:
408 default:
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000409 m_compiler->getLangOpts().ObjC = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000410 m_compiler->getLangOpts().CPlusPlus = true;
411 m_compiler->getLangOpts().CPlusPlus11 = true;
412 m_compiler->getHeaderSearchOpts().UseLibcxx = true;
413 break;
414 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000415
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416 m_compiler->getLangOpts().Bool = true;
417 m_compiler->getLangOpts().WChar = true;
418 m_compiler->getLangOpts().Blocks = true;
419 m_compiler->getLangOpts().DebuggerSupport =
420 true; // Features specifically for debugger clients
421 if (expr.DesiredResultType() == Expression::eResultTypeId)
422 m_compiler->getLangOpts().DebuggerCastResultToId = true;
423
424 m_compiler->getLangOpts().CharIsSigned =
425 ArchSpec(m_compiler->getTargetOpts().Triple.c_str())
426 .CharIsSignedByDefault();
427
Adrian Prantl05097242018-04-30 16:49:04 +0000428 // Spell checking is a nice feature, but it ends up completing a lot of types
429 // that we didn't strictly speaking need to complete. As a result, we spend a
430 // long time parsing and importing debug information.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 m_compiler->getLangOpts().SpellChecking = false;
432
Erik Pilkingtonfa983902018-10-30 20:31:30 +0000433 if (process_sp && m_compiler->getLangOpts().ObjC) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 if (process_sp->GetObjCLanguageRuntime()) {
435 if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() ==
436 ObjCLanguageRuntime::ObjCRuntimeVersions::eAppleObjC_V2)
437 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX,
438 VersionTuple(10, 7));
439 else
440 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX,
441 VersionTuple(10, 7));
442
443 if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
444 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
Sean Callananc3a16002011-01-17 23:42:46 +0000445 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 }
Greg Claytonf83f32d2011-01-15 01:32:14 +0000447
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448 m_compiler->getLangOpts().ThreadsafeStatics = false;
449 m_compiler->getLangOpts().AccessControl =
450 false; // Debuggers get universal access
451 m_compiler->getLangOpts().DollarIdents =
452 true; // $ indicates a persistent variable name
Raphael Isemannd424c2a2018-08-30 20:56:58 +0000453 // We enable all builtin functions beside the builtins from libc/libm (e.g.
454 // 'fopen'). Those libc functions are already correctly handled by LLDB, and
455 // additionally enabling them as expandable builtins is breaking Clang.
456 m_compiler->getLangOpts().NoBuiltin = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000457
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458 // Set CodeGen options
459 m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
460 m_compiler->getCodeGenOpts().InstrumentFunctions = false;
461 m_compiler->getCodeGenOpts().DisableFPElim = true;
462 m_compiler->getCodeGenOpts().OmitLeafFramePointer = false;
463 if (generate_debug_info)
464 m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::FullDebugInfo);
465 else
466 m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::NoDebugInfo);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000467
Kate Stoneb9c1b512016-09-06 20:57:50 +0000468 // Disable some warnings.
469 m_compiler->getDiagnostics().setSeverityForGroup(
470 clang::diag::Flavor::WarningOrError, "unused-value",
471 clang::diag::Severity::Ignored, SourceLocation());
472 m_compiler->getDiagnostics().setSeverityForGroup(
473 clang::diag::Flavor::WarningOrError, "odr",
474 clang::diag::Severity::Ignored, SourceLocation());
Alp Toker10933d12014-06-12 11:14:32 +0000475
Kate Stoneb9c1b512016-09-06 20:57:50 +0000476 // Inform the target of the language options
477 //
478 // FIXME: We shouldn't need to do this, the target should be immutable once
479 // created. This complexity should be lifted elsewhere.
480 m_compiler->getTarget().adjust(m_compiler->getLangOpts());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000481
Kate Stoneb9c1b512016-09-06 20:57:50 +0000482 // 6. Set up the diagnostic buffer for reporting errors
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000483
Kate Stoneb9c1b512016-09-06 20:57:50 +0000484 m_compiler->getDiagnostics().setClient(new ClangDiagnosticManagerAdapter);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000485
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486 // 7. Set up the source management objects inside the compiler
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000487
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488 clang::FileSystemOptions file_system_options;
489 m_file_manager.reset(new clang::FileManager(file_system_options));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000490
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491 if (!m_compiler->hasSourceManager())
492 m_compiler->createSourceManager(*m_file_manager.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000493
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494 m_compiler->createFileManager();
495 m_compiler->createPreprocessor(TU_Complete);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000496
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497 if (ClangModulesDeclVendor *decl_vendor =
498 target_sp->GetClangModulesDeclVendor()) {
499 ClangPersistentVariables *clang_persistent_vars =
500 llvm::cast<ClangPersistentVariables>(
501 target_sp->GetPersistentExpressionStateForLanguage(
502 lldb::eLanguageTypeC));
503 std::unique_ptr<PPCallbacks> pp_callbacks(
504 new LLDBPreprocessorCallbacks(*decl_vendor, *clang_persistent_vars));
505 m_pp_callbacks =
506 static_cast<LLDBPreprocessorCallbacks *>(pp_callbacks.get());
507 m_compiler->getPreprocessor().addPPCallbacks(std::move(pp_callbacks));
508 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000509
Adrian Prantl05097242018-04-30 16:49:04 +0000510 // 8. Most of this we get from the CompilerInstance, but we also want to give
511 // the context an ExternalASTSource.
Raphael Isemann778b3082018-08-23 20:40:45 +0000512
513 auto &PP = m_compiler->getPreprocessor();
514 auto &builtin_context = PP.getBuiltinInfo();
515 builtin_context.initializeBuiltins(PP.getIdentifierTable(),
516 m_compiler->getLangOpts());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000517
Raphael Isemann596709d2018-08-27 15:18:33 +0000518 m_compiler->createASTContext();
519 clang::ASTContext &ast_context = m_compiler->getASTContext();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000520
Kate Stoneb9c1b512016-09-06 20:57:50 +0000521 ClangExpressionHelper *type_system_helper =
522 dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
523 ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000524
Kate Stoneb9c1b512016-09-06 20:57:50 +0000525 if (decl_map) {
526 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source(
527 decl_map->CreateProxy());
Raphael Isemann596709d2018-08-27 15:18:33 +0000528 decl_map->InstallASTContext(ast_context, m_compiler->getFileManager());
529 ast_context.setExternalSource(ast_source);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000530 }
531
532 m_ast_context.reset(
533 new ClangASTContext(m_compiler->getTargetOpts().Triple.c_str()));
Raphael Isemann596709d2018-08-27 15:18:33 +0000534 m_ast_context->setASTContext(&ast_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535
536 std::string module_name("$__lldb_module");
537
538 m_llvm_context.reset(new LLVMContext());
539 m_code_generator.reset(CreateLLVMCodeGen(
540 m_compiler->getDiagnostics(), module_name,
541 m_compiler->getHeaderSearchOpts(), m_compiler->getPreprocessorOpts(),
542 m_compiler->getCodeGenOpts(), *m_llvm_context));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000543}
544
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545ClangExpressionParser::~ClangExpressionParser() {}
Sean Callanan1a8d4092010-08-27 01:01:44 +0000546
Raphael Isemann74829732018-08-30 17:29:37 +0000547namespace {
548
549//----------------------------------------------------------------------
550/// @class CodeComplete
551///
552/// A code completion consumer for the clang Sema that is responsible for
553/// creating the completion suggestions when a user requests completion
554/// of an incomplete `expr` invocation.
555//----------------------------------------------------------------------
556class CodeComplete : public CodeCompleteConsumer {
Raphael Isemann39ec6e72018-08-30 19:47:53 +0000557 CodeCompletionTUInfo m_info;
Raphael Isemann74829732018-08-30 17:29:37 +0000558
Raphael Isemann39ec6e72018-08-30 19:47:53 +0000559 std::string m_expr;
560 unsigned m_position = 0;
Raphael Isemannc11a7802018-08-30 21:26:32 +0000561 CompletionRequest &m_request;
Raphael Isemann9dd34c82018-09-17 12:06:07 +0000562 /// The printing policy we use when printing declarations for our completion
563 /// descriptions.
564 clang::PrintingPolicy m_desc_policy;
Raphael Isemann74829732018-08-30 17:29:37 +0000565
566 /// Returns true if the given character can be used in an identifier.
567 /// This also returns true for numbers because for completion we usually
568 /// just iterate backwards over iterators.
569 ///
570 /// Note: lldb uses '$' in its internal identifiers, so we also allow this.
571 static bool IsIdChar(char c) {
572 return c == '_' || std::isalnum(c) || c == '$';
573 }
574
575 /// Returns true if the given character is used to separate arguments
576 /// in the command line of lldb.
577 static bool IsTokenSeparator(char c) { return c == ' ' || c == '\t'; }
578
579 /// Drops all tokens in front of the expression that are unrelated for
580 /// the completion of the cmd line. 'unrelated' means here that the token
581 /// is not interested for the lldb completion API result.
582 StringRef dropUnrelatedFrontTokens(StringRef cmd) {
583 if (cmd.empty())
584 return cmd;
585
586 // If we are at the start of a word, then all tokens are unrelated to
587 // the current completion logic.
588 if (IsTokenSeparator(cmd.back()))
589 return StringRef();
590
591 // Remove all previous tokens from the string as they are unrelated
592 // to completing the current token.
593 StringRef to_remove = cmd;
594 while (!to_remove.empty() && !IsTokenSeparator(to_remove.back())) {
595 to_remove = to_remove.drop_back();
596 }
597 cmd = cmd.drop_front(to_remove.size());
598
599 return cmd;
600 }
601
602 /// Removes the last identifier token from the given cmd line.
603 StringRef removeLastToken(StringRef cmd) {
604 while (!cmd.empty() && IsIdChar(cmd.back())) {
605 cmd = cmd.drop_back();
606 }
607 return cmd;
608 }
609
610 /// Attemps to merge the given completion from the given position into the
611 /// existing command. Returns the completion string that can be returned to
612 /// the lldb completion API.
613 std::string mergeCompletion(StringRef existing, unsigned pos,
614 StringRef completion) {
615 StringRef existing_command = existing.substr(0, pos);
616 // We rewrite the last token with the completion, so let's drop that
617 // token from the command.
618 existing_command = removeLastToken(existing_command);
619 // We also should remove all previous tokens from the command as they
620 // would otherwise be added to the completion that already has the
621 // completion.
622 existing_command = dropUnrelatedFrontTokens(existing_command);
623 return existing_command.str() + completion.str();
624 }
625
626public:
627 /// Constructs a CodeComplete consumer that can be attached to a Sema.
628 /// @param[out] matches
629 /// The list of matches that the lldb completion API expects as a result.
630 /// This may already contain matches, so it's only allowed to append
631 /// to this variable.
632 /// @param[out] expr
633 /// The whole expression string that we are currently parsing. This
634 /// string needs to be equal to the input the user typed, and NOT the
635 /// final code that Clang is parsing.
636 /// @param[out] position
637 /// The character position of the user cursor in the `expr` parameter.
638 ///
Raphael Isemann9dd34c82018-09-17 12:06:07 +0000639 CodeComplete(CompletionRequest &request, clang::LangOptions ops,
640 std::string expr, unsigned position)
Raphael Isemann74829732018-08-30 17:29:37 +0000641 : CodeCompleteConsumer(CodeCompleteOptions(), false),
Raphael Isemann39ec6e72018-08-30 19:47:53 +0000642 m_info(std::make_shared<GlobalCodeCompletionAllocator>()), m_expr(expr),
Raphael Isemann9dd34c82018-09-17 12:06:07 +0000643 m_position(position), m_request(request), m_desc_policy(ops) {
644
645 // Ensure that the printing policy is producing a description that is as
646 // short as possible.
647 m_desc_policy.SuppressScope = true;
648 m_desc_policy.SuppressTagKeyword = true;
649 m_desc_policy.FullyQualifiedName = false;
650 m_desc_policy.TerseOutput = true;
651 m_desc_policy.IncludeNewlines = false;
652 m_desc_policy.UseVoidForZeroParams = false;
653 m_desc_policy.Bool = true;
654 }
Raphael Isemann74829732018-08-30 17:29:37 +0000655
656 /// Deregisters and destroys this code-completion consumer.
657 virtual ~CodeComplete() {}
658
659 /// \name Code-completion filtering
660 /// Check if the result should be filtered out.
661 bool isResultFilteredOut(StringRef Filter,
662 CodeCompletionResult Result) override {
663 // This code is mostly copied from CodeCompleteConsumer.
664 switch (Result.Kind) {
665 case CodeCompletionResult::RK_Declaration:
666 return !(
667 Result.Declaration->getIdentifier() &&
668 Result.Declaration->getIdentifier()->getName().startswith(Filter));
669 case CodeCompletionResult::RK_Keyword:
670 return !StringRef(Result.Keyword).startswith(Filter);
671 case CodeCompletionResult::RK_Macro:
672 return !Result.Macro->getName().startswith(Filter);
673 case CodeCompletionResult::RK_Pattern:
674 return !StringRef(Result.Pattern->getAsString()).startswith(Filter);
675 }
676 // If we trigger this assert or the above switch yields a warning, then
677 // CodeCompletionResult has been enhanced with more kinds of completion
678 // results. Expand the switch above in this case.
679 assert(false && "Unknown completion result type?");
680 // If we reach this, then we should just ignore whatever kind of unknown
681 // result we got back. We probably can't turn it into any kind of useful
682 // completion suggestion with the existing code.
683 return true;
684 }
685
686 /// \name Code-completion callbacks
687 /// Process the finalized code-completion results.
688 void ProcessCodeCompleteResults(Sema &SemaRef, CodeCompletionContext Context,
689 CodeCompletionResult *Results,
690 unsigned NumResults) override {
691
692 // The Sema put the incomplete token we try to complete in here during
693 // lexing, so we need to retrieve it here to know what we are completing.
694 StringRef Filter = SemaRef.getPreprocessor().getCodeCompletionFilter();
695
696 // Iterate over all the results. Filter out results we don't want and
697 // process the rest.
698 for (unsigned I = 0; I != NumResults; ++I) {
699 // Filter the results with the information from the Sema.
700 if (!Filter.empty() && isResultFilteredOut(Filter, Results[I]))
701 continue;
702
703 CodeCompletionResult &R = Results[I];
704 std::string ToInsert;
Raphael Isemann9dd34c82018-09-17 12:06:07 +0000705 std::string Description;
Raphael Isemann74829732018-08-30 17:29:37 +0000706 // Handle the different completion kinds that come from the Sema.
707 switch (R.Kind) {
708 case CodeCompletionResult::RK_Declaration: {
709 const NamedDecl *D = R.Declaration;
710 ToInsert = R.Declaration->getNameAsString();
711 // If we have a function decl that has no arguments we want to
712 // complete the empty parantheses for the user. If the function has
713 // arguments, we at least complete the opening bracket.
714 if (const FunctionDecl *F = dyn_cast<FunctionDecl>(D)) {
715 if (F->getNumParams() == 0)
716 ToInsert += "()";
717 else
718 ToInsert += "(";
Raphael Isemann9dd34c82018-09-17 12:06:07 +0000719 raw_string_ostream OS(Description);
720 F->print(OS, m_desc_policy, false);
721 OS.flush();
722 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
723 Description = V->getType().getAsString(m_desc_policy);
724 } else if (const FieldDecl *F = dyn_cast<FieldDecl>(D)) {
725 Description = F->getType().getAsString(m_desc_policy);
726 } else if (const NamespaceDecl *N = dyn_cast<NamespaceDecl>(D)) {
727 // If we try to complete a namespace, then we can directly append
728 // the '::'.
Raphael Isemann74829732018-08-30 17:29:37 +0000729 if (!N->isAnonymousNamespace())
730 ToInsert += "::";
731 }
732 break;
733 }
734 case CodeCompletionResult::RK_Keyword:
735 ToInsert = R.Keyword;
736 break;
737 case CodeCompletionResult::RK_Macro:
Raphael Isemann74829732018-08-30 17:29:37 +0000738 ToInsert = R.Macro->getName().str();
739 break;
740 case CodeCompletionResult::RK_Pattern:
741 ToInsert = R.Pattern->getTypedText();
742 break;
743 }
744 // At this point all information is in the ToInsert string.
745
746 // We also filter some internal lldb identifiers here. The user
747 // shouldn't see these.
748 if (StringRef(ToInsert).startswith("$__lldb_"))
749 continue;
750 if (!ToInsert.empty()) {
751 // Merge the suggested Token into the existing command line to comply
752 // with the kind of result the lldb API expects.
753 std::string CompletionSuggestion =
Raphael Isemann39ec6e72018-08-30 19:47:53 +0000754 mergeCompletion(m_expr, m_position, ToInsert);
Raphael Isemann9dd34c82018-09-17 12:06:07 +0000755 m_request.AddCompletion(CompletionSuggestion, Description);
Raphael Isemann74829732018-08-30 17:29:37 +0000756 }
757 }
758 }
759
760 /// \param S the semantic-analyzer object for which code-completion is being
761 /// done.
762 ///
763 /// \param CurrentArg the index of the current argument.
764 ///
765 /// \param Candidates an array of overload candidates.
766 ///
767 /// \param NumCandidates the number of overload candidates
768 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
769 OverloadCandidate *Candidates,
770 unsigned NumCandidates,
771 SourceLocation OpenParLoc) override {
772 // At the moment we don't filter out any overloaded candidates.
773 }
774
775 CodeCompletionAllocator &getAllocator() override {
Raphael Isemann39ec6e72018-08-30 19:47:53 +0000776 return m_info.getAllocator();
Raphael Isemann74829732018-08-30 17:29:37 +0000777 }
778
Raphael Isemann39ec6e72018-08-30 19:47:53 +0000779 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return m_info; }
Raphael Isemann74829732018-08-30 17:29:37 +0000780};
781} // namespace
782
Raphael Isemannc11a7802018-08-30 21:26:32 +0000783bool ClangExpressionParser::Complete(CompletionRequest &request, unsigned line,
Raphael Isemann74829732018-08-30 17:29:37 +0000784 unsigned pos, unsigned typed_pos) {
785 DiagnosticManager mgr;
786 // We need the raw user expression here because that's what the CodeComplete
787 // class uses to provide completion suggestions.
788 // However, the `Text` method only gives us the transformed expression here.
789 // To actually get the raw user input here, we have to cast our expression to
790 // the LLVMUserExpression which exposes the right API. This should never fail
791 // as we always have a ClangUserExpression whenever we call this.
792 LLVMUserExpression &llvm_expr = *static_cast<LLVMUserExpression *>(&m_expr);
Raphael Isemann9dd34c82018-09-17 12:06:07 +0000793 CodeComplete CC(request, m_compiler->getLangOpts(), llvm_expr.GetUserText(),
794 typed_pos);
Raphael Isemann74829732018-08-30 17:29:37 +0000795 // We don't need a code generator for parsing.
796 m_code_generator.reset();
797 // Start parsing the expression with our custom code completion consumer.
798 ParseInternal(mgr, &CC, line, pos);
799 return true;
800}
801
Kate Stoneb9c1b512016-09-06 20:57:50 +0000802unsigned ClangExpressionParser::Parse(DiagnosticManager &diagnostic_manager) {
Raphael Isemann74829732018-08-30 17:29:37 +0000803 return ParseInternal(diagnostic_manager);
804}
805
806unsigned
807ClangExpressionParser::ParseInternal(DiagnosticManager &diagnostic_manager,
808 CodeCompleteConsumer *completion_consumer,
809 unsigned completion_line,
810 unsigned completion_column) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000811 ClangDiagnosticManagerAdapter *adapter =
812 static_cast<ClangDiagnosticManagerAdapter *>(
813 m_compiler->getDiagnostics().getClient());
814 clang::TextDiagnosticBuffer *diag_buf = adapter->GetPassthrough();
815 diag_buf->FlushDiagnostics(m_compiler->getDiagnostics());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000816
Kate Stoneb9c1b512016-09-06 20:57:50 +0000817 adapter->ResetManager(&diagnostic_manager);
Sean Callanan579e70c2016-03-19 00:03:59 +0000818
Kate Stoneb9c1b512016-09-06 20:57:50 +0000819 const char *expr_text = m_expr.Text();
Rafael Espindola9cb97b62014-05-21 15:08:27 +0000820
Kate Stoneb9c1b512016-09-06 20:57:50 +0000821 clang::SourceManager &source_mgr = m_compiler->getSourceManager();
822 bool created_main_file = false;
Raphael Isemann74829732018-08-30 17:29:37 +0000823
824 // Clang wants to do completion on a real file known by Clang's file manager,
825 // so we have to create one to make this work.
826 // TODO: We probably could also simulate to Clang's file manager that there
827 // is a real file that contains our code.
828 bool should_create_file = completion_consumer != nullptr;
829
830 // We also want a real file on disk if we generate full debug info.
831 should_create_file |= m_compiler->getCodeGenOpts().getDebugInfo() ==
832 codegenoptions::FullDebugInfo;
833
834 if (should_create_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000835 int temp_fd = -1;
836 llvm::SmallString<PATH_MAX> result_path;
Pavel Labath60f028f2018-06-19 15:09:07 +0000837 if (FileSpec tmpdir_file_spec = HostInfo::GetProcessTempDir()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838 tmpdir_file_spec.AppendPathComponent("lldb-%%%%%%.expr");
839 std::string temp_source_path = tmpdir_file_spec.GetPath();
840 llvm::sys::fs::createUniqueFile(temp_source_path, temp_fd, result_path);
841 } else {
842 llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path);
843 }
844
845 if (temp_fd != -1) {
846 lldb_private::File file(temp_fd, true);
847 const size_t expr_text_len = strlen(expr_text);
848 size_t bytes_written = expr_text_len;
849 if (file.Write(expr_text, bytes_written).Success()) {
850 if (bytes_written == expr_text_len) {
851 file.Close();
852 source_mgr.setMainFileID(
853 source_mgr.createFileID(m_file_manager->getFile(result_path),
854 SourceLocation(), SrcMgr::C_User));
855 created_main_file = true;
Greg Clayton23f8c952014-03-24 23:10:19 +0000856 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000857 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000858 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000859 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000860
Kate Stoneb9c1b512016-09-06 20:57:50 +0000861 if (!created_main_file) {
862 std::unique_ptr<MemoryBuffer> memory_buffer =
863 MemoryBuffer::getMemBufferCopy(expr_text, __FUNCTION__);
864 source_mgr.setMainFileID(source_mgr.createFileID(std::move(memory_buffer)));
865 }
866
867 diag_buf->BeginSourceFile(m_compiler->getLangOpts(),
868 &m_compiler->getPreprocessor());
869
870 ClangExpressionHelper *type_system_helper =
871 dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
872
873 ASTConsumer *ast_transformer =
874 type_system_helper->ASTTransformer(m_code_generator.get());
875
876 if (ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap())
877 decl_map->InstallCodeGenerator(m_code_generator.get());
878
Raphael Isemann74829732018-08-30 17:29:37 +0000879 // If we want to parse for code completion, we need to attach our code
880 // completion consumer to the Sema and specify a completion position.
881 // While parsing the Sema will call this consumer with the provided
882 // completion suggestions.
883 if (completion_consumer) {
884 auto main_file = source_mgr.getFileEntryForID(source_mgr.getMainFileID());
885 auto &PP = m_compiler->getPreprocessor();
886 // Lines and columns start at 1 in Clang, but code completion positions are
887 // indexed from 0, so we need to add 1 to the line and column here.
888 ++completion_line;
889 ++completion_column;
890 PP.SetCodeCompletionPoint(main_file, completion_line, completion_column);
891 }
892
Kate Stoneb9c1b512016-09-06 20:57:50 +0000893 if (ast_transformer) {
894 ast_transformer->Initialize(m_compiler->getASTContext());
895 ParseAST(m_compiler->getPreprocessor(), ast_transformer,
Raphael Isemann74829732018-08-30 17:29:37 +0000896 m_compiler->getASTContext(), false, TU_Complete,
897 completion_consumer);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000898 } else {
899 m_code_generator->Initialize(m_compiler->getASTContext());
900 ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(),
Raphael Isemann74829732018-08-30 17:29:37 +0000901 m_compiler->getASTContext(), false, TU_Complete,
902 completion_consumer);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000903 }
904
905 diag_buf->EndSourceFile();
906
907 unsigned num_errors = diag_buf->getNumErrors();
908
909 if (m_pp_callbacks && m_pp_callbacks->hasErrors()) {
910 num_errors++;
Zachary Turnere2411fa2016-11-12 19:12:56 +0000911 diagnostic_manager.PutString(eDiagnosticSeverityError,
912 "while importing modules:");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000913 diagnostic_manager.AppendMessageToDiagnostic(
Zachary Turnerc1564272016-11-16 21:15:24 +0000914 m_pp_callbacks->getErrorString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000915 }
916
917 if (!num_errors) {
918 if (type_system_helper->DeclMap() &&
919 !type_system_helper->DeclMap()->ResolveUnknownTypes()) {
920 diagnostic_manager.Printf(eDiagnosticSeverityError,
921 "Couldn't infer the type of a variable");
922 num_errors++;
Greg Clayton23f8c952014-03-24 23:10:19 +0000923 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000924 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000925
Kate Stoneb9c1b512016-09-06 20:57:50 +0000926 if (!num_errors) {
927 type_system_helper->CommitPersistentDecls();
928 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000929
Kate Stoneb9c1b512016-09-06 20:57:50 +0000930 adapter->ResetManager();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000931
Kate Stoneb9c1b512016-09-06 20:57:50 +0000932 return num_errors;
Sean Callanan1a8d4092010-08-27 01:01:44 +0000933}
934
Sagar Thakuradc1abe2016-05-13 11:04:47 +0000935std::string
Kate Stoneb9c1b512016-09-06 20:57:50 +0000936ClangExpressionParser::GetClangTargetABI(const ArchSpec &target_arch) {
937 std::string abi;
938
939 if (target_arch.IsMIPS()) {
940 switch (target_arch.GetFlags() & ArchSpec::eMIPSABI_mask) {
941 case ArchSpec::eMIPSABI_N64:
942 abi = "n64";
943 break;
944 case ArchSpec::eMIPSABI_N32:
945 abi = "n32";
946 break;
947 case ArchSpec::eMIPSABI_O32:
948 abi = "o32";
949 break;
950 default:
951 break;
Sagar Thakuradc1abe2016-05-13 11:04:47 +0000952 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000953 }
954 return abi;
Sagar Thakuradc1abe2016-05-13 11:04:47 +0000955}
956
Kate Stoneb9c1b512016-09-06 20:57:50 +0000957bool ClangExpressionParser::RewriteExpression(
958 DiagnosticManager &diagnostic_manager) {
959 clang::SourceManager &source_manager = m_compiler->getSourceManager();
960 clang::edit::EditedSource editor(source_manager, m_compiler->getLangOpts(),
961 nullptr);
962 clang::edit::Commit commit(editor);
963 clang::Rewriter rewriter(source_manager, m_compiler->getLangOpts());
Jim Inghama1e541b2016-03-25 01:57:14 +0000964
Kate Stoneb9c1b512016-09-06 20:57:50 +0000965 class RewritesReceiver : public edit::EditsReceiver {
966 Rewriter &rewrite;
Jim Inghama1e541b2016-03-25 01:57:14 +0000967
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968 public:
969 RewritesReceiver(Rewriter &in_rewrite) : rewrite(in_rewrite) {}
970
971 void insert(SourceLocation loc, StringRef text) override {
972 rewrite.InsertText(loc, text);
Jim Inghama1e541b2016-03-25 01:57:14 +0000973 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974 void replace(CharSourceRange range, StringRef text) override {
975 rewrite.ReplaceText(range.getBegin(), rewrite.getRangeSize(range), text);
Sean Callananfc55f5d2010-09-21 00:44:12 +0000976 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000977 };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000978
Kate Stoneb9c1b512016-09-06 20:57:50 +0000979 RewritesReceiver rewrites_receiver(rewriter);
980
981 const DiagnosticList &diagnostics = diagnostic_manager.Diagnostics();
982 size_t num_diags = diagnostics.size();
983 if (num_diags == 0)
Sean Callananfc55f5d2010-09-21 00:44:12 +0000984 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000985
986 for (const Diagnostic *diag : diagnostic_manager.Diagnostics()) {
987 const ClangDiagnostic *diagnostic = llvm::dyn_cast<ClangDiagnostic>(diag);
988 if (diagnostic && diagnostic->HasFixIts()) {
989 for (const FixItHint &fixit : diagnostic->FixIts()) {
990 // This is cobbed from clang::Rewrite::FixItRewriter.
991 if (fixit.CodeToInsert.empty()) {
992 if (fixit.InsertFromRange.isValid()) {
993 commit.insertFromRange(fixit.RemoveRange.getBegin(),
994 fixit.InsertFromRange, /*afterToken=*/false,
995 fixit.BeforePreviousInsertions);
996 } else
997 commit.remove(fixit.RemoveRange);
998 } else {
999 if (fixit.RemoveRange.isTokenRange() ||
1000 fixit.RemoveRange.getBegin() != fixit.RemoveRange.getEnd())
1001 commit.replace(fixit.RemoveRange, fixit.CodeToInsert);
1002 else
1003 commit.insert(fixit.RemoveRange.getBegin(), fixit.CodeToInsert,
1004 /*afterToken=*/false, fixit.BeforePreviousInsertions);
1005 }
1006 }
1007 }
1008 }
1009
1010 // FIXME - do we want to try to propagate specific errors here?
1011 if (!commit.isCommitable())
1012 return false;
1013 else if (!editor.commit(commit))
1014 return false;
1015
1016 // Now play all the edits, and stash the result in the diagnostic manager.
1017 editor.applyRewrites(rewrites_receiver);
1018 RewriteBuffer &main_file_buffer =
1019 rewriter.getEditBuffer(source_manager.getMainFileID());
1020
1021 std::string fixed_expression;
1022 llvm::raw_string_ostream out_stream(fixed_expression);
1023
1024 main_file_buffer.write(out_stream);
1025 out_stream.flush();
1026 diagnostic_manager.SetFixedExpression(fixed_expression);
1027
1028 return true;
Sean Callananfc55f5d2010-09-21 00:44:12 +00001029}
1030
Kate Stoneb9c1b512016-09-06 20:57:50 +00001031static bool FindFunctionInModule(ConstString &mangled_name,
1032 llvm::Module *module, const char *orig_name) {
1033 for (const auto &func : module->getFunctionList()) {
1034 const StringRef &name = func.getName();
1035 if (name.find(orig_name) != StringRef::npos) {
1036 mangled_name.SetString(name);
1037 return true;
Sean Callanan1a8d4092010-08-27 01:01:44 +00001038 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001039 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +00001040
Kate Stoneb9c1b512016-09-06 20:57:50 +00001041 return false;
Sean Callanan1a8d4092010-08-27 01:01:44 +00001042}
Sean Callananbd4dc692016-03-21 22:23:38 +00001043
Zachary Turner97206d52017-05-12 04:51:55 +00001044lldb_private::Status ClangExpressionParser::PrepareForExecution(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001045 lldb::addr_t &func_addr, lldb::addr_t &func_end,
1046 lldb::IRExecutionUnitSP &execution_unit_sp, ExecutionContext &exe_ctx,
1047 bool &can_interpret, ExecutionPolicy execution_policy) {
1048 func_addr = LLDB_INVALID_ADDRESS;
1049 func_end = LLDB_INVALID_ADDRESS;
1050 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1051
Zachary Turner97206d52017-05-12 04:51:55 +00001052 lldb_private::Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001053
1054 std::unique_ptr<llvm::Module> llvm_module_ap(
1055 m_code_generator->ReleaseModule());
1056
1057 if (!llvm_module_ap.get()) {
1058 err.SetErrorToGenericError();
1059 err.SetErrorString("IR doesn't contain a module");
Sean Callananbd4dc692016-03-21 22:23:38 +00001060 return err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001061 }
1062
1063 ConstString function_name;
1064
1065 if (execution_policy != eExecutionPolicyTopLevel) {
1066 // Find the actual name of the function (it's often mangled somehow)
1067
1068 if (!FindFunctionInModule(function_name, llvm_module_ap.get(),
1069 m_expr.FunctionName())) {
1070 err.SetErrorToGenericError();
1071 err.SetErrorStringWithFormat("Couldn't find %s() in the module",
1072 m_expr.FunctionName());
1073 return err;
1074 } else {
1075 if (log)
1076 log->Printf("Found function %s for %s", function_name.AsCString(),
1077 m_expr.FunctionName());
1078 }
1079 }
1080
1081 SymbolContext sc;
1082
1083 if (lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP()) {
1084 sc = frame_sp->GetSymbolContext(lldb::eSymbolContextEverything);
1085 } else if (lldb::TargetSP target_sp = exe_ctx.GetTargetSP()) {
1086 sc.target_sp = target_sp;
1087 }
1088
1089 LLVMUserExpression::IRPasses custom_passes;
1090 {
1091 auto lang = m_expr.Language();
1092 if (log)
Bruce Mitchener4ebdee02018-05-29 09:10:46 +00001093 log->Printf("%s - Current expression language is %s\n", __FUNCTION__,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001094 Language::GetNameForLanguageType(lang));
Jim Ingham22562152016-11-03 23:42:09 +00001095 lldb::ProcessSP process_sp = exe_ctx.GetProcessSP();
1096 if (process_sp && lang != lldb::eLanguageTypeUnknown) {
1097 auto runtime = process_sp->GetLanguageRuntime(lang);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001098 if (runtime)
1099 runtime->GetIRPasses(custom_passes);
1100 }
1101 }
1102
1103 if (custom_passes.EarlyPasses) {
1104 if (log)
1105 log->Printf("%s - Running Early IR Passes from LanguageRuntime on "
1106 "expression module '%s'",
1107 __FUNCTION__, m_expr.FunctionName());
1108
1109 custom_passes.EarlyPasses->run(*llvm_module_ap);
1110 }
1111
1112 execution_unit_sp.reset(
1113 new IRExecutionUnit(m_llvm_context, // handed off here
1114 llvm_module_ap, // handed off here
1115 function_name, exe_ctx.GetTargetSP(), sc,
1116 m_compiler->getTargetOpts().Features));
1117
1118 ClangExpressionHelper *type_system_helper =
1119 dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
1120 ClangExpressionDeclMap *decl_map =
1121 type_system_helper->DeclMap(); // result can be NULL
1122
1123 if (decl_map) {
1124 Stream *error_stream = NULL;
1125 Target *target = exe_ctx.GetTargetPtr();
1126 error_stream = target->GetDebugger().GetErrorFile().get();
1127
1128 IRForTarget ir_for_target(decl_map, m_expr.NeedsVariableResolution(),
1129 *execution_unit_sp, *error_stream,
1130 function_name.AsCString());
1131
1132 bool ir_can_run =
1133 ir_for_target.runOnModule(*execution_unit_sp->GetModule());
1134
1135 if (!ir_can_run) {
1136 err.SetErrorString(
1137 "The expression could not be prepared to run in the target");
1138 return err;
1139 }
1140
1141 Process *process = exe_ctx.GetProcessPtr();
1142
1143 if (execution_policy != eExecutionPolicyAlways &&
1144 execution_policy != eExecutionPolicyTopLevel) {
Zachary Turner97206d52017-05-12 04:51:55 +00001145 lldb_private::Status interpret_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001146
1147 bool interpret_function_calls =
1148 !process ? false : process->CanInterpretFunctionCalls();
1149 can_interpret = IRInterpreter::CanInterpret(
1150 *execution_unit_sp->GetModule(), *execution_unit_sp->GetFunction(),
1151 interpret_error, interpret_function_calls);
1152
1153 if (!can_interpret && execution_policy == eExecutionPolicyNever) {
1154 err.SetErrorStringWithFormat("Can't run the expression locally: %s",
1155 interpret_error.AsCString());
1156 return err;
1157 }
1158 }
1159
1160 if (!process && execution_policy == eExecutionPolicyAlways) {
1161 err.SetErrorString("Expression needed to run in the target, but the "
1162 "target can't be run");
1163 return err;
1164 }
1165
1166 if (!process && execution_policy == eExecutionPolicyTopLevel) {
1167 err.SetErrorString("Top-level code needs to be inserted into a runnable "
1168 "target, but the target can't be run");
1169 return err;
1170 }
1171
1172 if (execution_policy == eExecutionPolicyAlways ||
1173 (execution_policy != eExecutionPolicyTopLevel && !can_interpret)) {
1174 if (m_expr.NeedsValidation() && process) {
1175 if (!process->GetDynamicCheckers()) {
1176 DynamicCheckerFunctions *dynamic_checkers =
1177 new DynamicCheckerFunctions();
1178
1179 DiagnosticManager install_diagnostics;
1180
1181 if (!dynamic_checkers->Install(install_diagnostics, exe_ctx)) {
1182 if (install_diagnostics.Diagnostics().size())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001183 err.SetErrorString(install_diagnostics.GetString().c_str());
Raphael Isemann262dd8c2018-09-11 13:59:47 +00001184 else
1185 err.SetErrorString("couldn't install checkers, unknown error");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001186
1187 return err;
1188 }
1189
1190 process->SetDynamicCheckers(dynamic_checkers);
1191
1192 if (log)
1193 log->Printf("== [ClangUserExpression::Evaluate] Finished "
1194 "installing dynamic checkers ==");
1195 }
1196
1197 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(),
1198 function_name.AsCString());
1199
1200 llvm::Module *module = execution_unit_sp->GetModule();
1201 if (!module || !ir_dynamic_checks.runOnModule(*module)) {
1202 err.SetErrorToGenericError();
1203 err.SetErrorString("Couldn't add dynamic checks to the expression");
1204 return err;
1205 }
1206
1207 if (custom_passes.LatePasses) {
1208 if (log)
1209 log->Printf("%s - Running Late IR Passes from LanguageRuntime on "
1210 "expression module '%s'",
1211 __FUNCTION__, m_expr.FunctionName());
1212
1213 custom_passes.LatePasses->run(*module);
1214 }
1215 }
1216 }
1217
1218 if (execution_policy == eExecutionPolicyAlways ||
1219 execution_policy == eExecutionPolicyTopLevel || !can_interpret) {
1220 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
1221 }
1222 } else {
1223 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
1224 }
1225
1226 return err;
1227}
1228
Zachary Turner97206d52017-05-12 04:51:55 +00001229lldb_private::Status ClangExpressionParser::RunStaticInitializers(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001230 lldb::IRExecutionUnitSP &execution_unit_sp, ExecutionContext &exe_ctx) {
Zachary Turner97206d52017-05-12 04:51:55 +00001231 lldb_private::Status err;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001232
1233 lldbassert(execution_unit_sp.get());
1234 lldbassert(exe_ctx.HasThreadScope());
1235
1236 if (!execution_unit_sp.get()) {
1237 err.SetErrorString(
1238 "can't run static initializers for a NULL execution unit");
1239 return err;
1240 }
1241
1242 if (!exe_ctx.HasThreadScope()) {
1243 err.SetErrorString("can't run static initializers without a thread");
1244 return err;
1245 }
1246
1247 std::vector<lldb::addr_t> static_initializers;
1248
1249 execution_unit_sp->GetStaticInitializers(static_initializers);
1250
1251 for (lldb::addr_t static_initializer : static_initializers) {
1252 EvaluateExpressionOptions options;
1253
1254 lldb::ThreadPlanSP call_static_initializer(new ThreadPlanCallFunction(
1255 exe_ctx.GetThreadRef(), Address(static_initializer), CompilerType(),
1256 llvm::ArrayRef<lldb::addr_t>(), options));
1257
1258 DiagnosticManager execution_errors;
1259 lldb::ExpressionResults results =
1260 exe_ctx.GetThreadRef().GetProcess()->RunThreadPlan(
1261 exe_ctx, call_static_initializer, options, execution_errors);
1262
1263 if (results != lldb::eExpressionCompleted) {
1264 err.SetErrorStringWithFormat("couldn't run static initializer: %s",
1265 execution_errors.GetString().c_str());
1266 return err;
1267 }
1268 }
1269
1270 return err;
Sean Callananbd4dc692016-03-21 22:23:38 +00001271}