1 //===-- ClangExpressionParser.cpp -----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/AST/ASTContext.h"
10 #include "clang/AST/ASTDiagnostic.h"
11 #include "clang/AST/ExternalASTSource.h"
12 #include "clang/AST/PrettyPrinter.h"
13 #include "clang/Basic/Builtins.h"
14 #include "clang/Basic/DiagnosticIDs.h"
15 #include "clang/Basic/SourceLocation.h"
16 #include "clang/Basic/TargetInfo.h"
17 #include "clang/Basic/Version.h"
18 #include "clang/CodeGen/CodeGenAction.h"
19 #include "clang/CodeGen/ModuleBuilder.h"
20 #include "clang/Edit/Commit.h"
21 #include "clang/Edit/EditedSource.h"
22 #include "clang/Edit/EditsReceiver.h"
23 #include "clang/Frontend/CompilerInstance.h"
24 #include "clang/Frontend/CompilerInvocation.h"
25 #include "clang/Frontend/FrontendActions.h"
26 #include "clang/Frontend/FrontendDiagnostic.h"
27 #include "clang/Frontend/FrontendPluginRegistry.h"
28 #include "clang/Frontend/TextDiagnosticBuffer.h"
29 #include "clang/Frontend/TextDiagnosticPrinter.h"
30 #include "clang/Lex/Preprocessor.h"
31 #include "clang/Parse/ParseAST.h"
32 #include "clang/Rewrite/Core/Rewriter.h"
33 #include "clang/Rewrite/Frontend/FrontendActions.h"
34 #include "clang/Sema/CodeCompleteConsumer.h"
35 #include "clang/Sema/Sema.h"
36 #include "clang/Sema/SemaConsumer.h"
37 
38 #include "llvm/ADT/StringRef.h"
39 #include "llvm/ExecutionEngine/ExecutionEngine.h"
40 #include "llvm/Support/CrashRecoveryContext.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/FileSystem.h"
43 #include "llvm/Support/TargetSelect.h"
44 
45 #include "llvm/IR/LLVMContext.h"
46 #include "llvm/IR/Module.h"
47 #include "llvm/Support/DynamicLibrary.h"
48 #include "llvm/Support/ErrorHandling.h"
49 #include "llvm/Support/Host.h"
50 #include "llvm/Support/MemoryBuffer.h"
51 #include "llvm/Support/Signals.h"
52 
53 #include "ClangDiagnostic.h"
54 #include "ClangExpressionParser.h"
55 #include "ClangUserExpression.h"
56 
57 #include "ASTUtils.h"
58 #include "ClangASTSource.h"
59 #include "ClangDiagnostic.h"
60 #include "ClangExpressionDeclMap.h"
61 #include "ClangExpressionHelper.h"
62 #include "ClangExpressionParser.h"
63 #include "ClangHost.h"
64 #include "ClangModulesDeclVendor.h"
65 #include "ClangPersistentVariables.h"
66 #include "IRDynamicChecks.h"
67 #include "IRForTarget.h"
68 #include "ModuleDependencyCollector.h"
69 
70 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
71 #include "lldb/Core/Debugger.h"
72 #include "lldb/Core/Disassembler.h"
73 #include "lldb/Core/Module.h"
74 #include "lldb/Core/StreamFile.h"
75 #include "lldb/Expression/IRExecutionUnit.h"
76 #include "lldb/Expression/IRInterpreter.h"
77 #include "lldb/Host/File.h"
78 #include "lldb/Host/HostInfo.h"
79 #include "lldb/Symbol/SymbolVendor.h"
80 #include "lldb/Target/ExecutionContext.h"
81 #include "lldb/Target/Language.h"
82 #include "lldb/Target/Process.h"
83 #include "lldb/Target/Target.h"
84 #include "lldb/Target/ThreadPlanCallFunction.h"
85 #include "lldb/Utility/DataBufferHeap.h"
86 #include "lldb/Utility/LLDBAssert.h"
87 #include "lldb/Utility/LLDBLog.h"
88 #include "lldb/Utility/Log.h"
89 #include "lldb/Utility/Stream.h"
90 #include "lldb/Utility/StreamString.h"
91 #include "lldb/Utility/StringList.h"
92 
93 #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
94 #include "Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.h"
95 
96 #include <cctype>
97 #include <memory>
98 #include <optional>
99 
100 using namespace clang;
101 using namespace llvm;
102 using namespace lldb_private;
103 
104 //===----------------------------------------------------------------------===//
105 // Utility Methods for Clang
106 //===----------------------------------------------------------------------===//
107 
108 class ClangExpressionParser::LLDBPreprocessorCallbacks : public PPCallbacks {
109   ClangModulesDeclVendor &m_decl_vendor;
110   ClangPersistentVariables &m_persistent_vars;
111   clang::SourceManager &m_source_mgr;
112   StreamString m_error_stream;
113   bool m_has_errors = false;
114 
115 public:
116   LLDBPreprocessorCallbacks(ClangModulesDeclVendor &decl_vendor,
117                             ClangPersistentVariables &persistent_vars,
118                             clang::SourceManager &source_mgr)
119       : m_decl_vendor(decl_vendor), m_persistent_vars(persistent_vars),
120         m_source_mgr(source_mgr) {}
121 
122   void moduleImport(SourceLocation import_location, clang::ModuleIdPath path,
123                     const clang::Module * /*null*/) override {
124     // Ignore modules that are imported in the wrapper code as these are not
125     // loaded by the user.
126     llvm::StringRef filename =
127         m_source_mgr.getPresumedLoc(import_location).getFilename();
128     if (filename == ClangExpressionSourceCode::g_prefix_file_name)
129       return;
130 
131     SourceModule module;
132 
133     for (const std::pair<IdentifierInfo *, SourceLocation> &component : path)
134       module.path.push_back(ConstString(component.first->getName()));
135 
136     StreamString error_stream;
137 
138     ClangModulesDeclVendor::ModuleVector exported_modules;
139     if (!m_decl_vendor.AddModule(module, &exported_modules, m_error_stream))
140       m_has_errors = true;
141 
142     for (ClangModulesDeclVendor::ModuleID module : exported_modules)
143       m_persistent_vars.AddHandLoadedClangModule(module);
144   }
145 
146   bool hasErrors() { return m_has_errors; }
147 
148   llvm::StringRef getErrorString() { return m_error_stream.GetString(); }
149 };
150 
151 static void AddAllFixIts(ClangDiagnostic *diag, const clang::Diagnostic &Info) {
152   for (auto &fix_it : Info.getFixItHints()) {
153     if (fix_it.isNull())
154       continue;
155     diag->AddFixitHint(fix_it);
156   }
157 }
158 
159 class ClangDiagnosticManagerAdapter : public clang::DiagnosticConsumer {
160 public:
161   ClangDiagnosticManagerAdapter(DiagnosticOptions &opts) {
162     DiagnosticOptions *options = new DiagnosticOptions(opts);
163     options->ShowPresumedLoc = true;
164     options->ShowLevel = false;
165     m_os = std::make_shared<llvm::raw_string_ostream>(m_output);
166     m_passthrough =
167         std::make_shared<clang::TextDiagnosticPrinter>(*m_os, options);
168   }
169 
170   void ResetManager(DiagnosticManager *manager = nullptr) {
171     m_manager = manager;
172   }
173 
174   /// Returns the last ClangDiagnostic message that the DiagnosticManager
175   /// received or a nullptr if the DiagnosticMangager hasn't seen any
176   /// Clang diagnostics yet.
177   ClangDiagnostic *MaybeGetLastClangDiag() const {
178     if (m_manager->Diagnostics().empty())
179       return nullptr;
180     lldb_private::Diagnostic *diag = m_manager->Diagnostics().back().get();
181     ClangDiagnostic *clang_diag = dyn_cast<ClangDiagnostic>(diag);
182     return clang_diag;
183   }
184 
185   void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
186                         const clang::Diagnostic &Info) override {
187     if (!m_manager) {
188       // We have no DiagnosticManager before/after parsing but we still could
189       // receive diagnostics (e.g., by the ASTImporter failing to copy decls
190       // when we move the expression result ot the ScratchASTContext). Let's at
191       // least log these diagnostics until we find a way to properly render
192       // them and display them to the user.
193       Log *log = GetLog(LLDBLog::Expressions);
194       if (log) {
195         llvm::SmallVector<char, 32> diag_str;
196         Info.FormatDiagnostic(diag_str);
197         diag_str.push_back('\0');
198         const char *plain_diag = diag_str.data();
199         LLDB_LOG(log, "Received diagnostic outside parsing: {0}", plain_diag);
200       }
201       return;
202     }
203 
204     // Update error/warning counters.
205     DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
206 
207     // Render diagnostic message to m_output.
208     m_output.clear();
209     m_passthrough->HandleDiagnostic(DiagLevel, Info);
210     m_os->flush();
211 
212     lldb_private::DiagnosticSeverity severity;
213     bool make_new_diagnostic = true;
214 
215     switch (DiagLevel) {
216     case DiagnosticsEngine::Level::Fatal:
217     case DiagnosticsEngine::Level::Error:
218       severity = eDiagnosticSeverityError;
219       break;
220     case DiagnosticsEngine::Level::Warning:
221       severity = eDiagnosticSeverityWarning;
222       break;
223     case DiagnosticsEngine::Level::Remark:
224     case DiagnosticsEngine::Level::Ignored:
225       severity = eDiagnosticSeverityRemark;
226       break;
227     case DiagnosticsEngine::Level::Note:
228       m_manager->AppendMessageToDiagnostic(m_output);
229       make_new_diagnostic = false;
230 
231       // 'note:' diagnostics for errors and warnings can also contain Fix-Its.
232       // We add these Fix-Its to the last error diagnostic to make sure
233       // that we later have all Fix-Its related to an 'error' diagnostic when
234       // we apply them to the user expression.
235       auto *clang_diag = MaybeGetLastClangDiag();
236       // If we don't have a previous diagnostic there is nothing to do.
237       // If the previous diagnostic already has its own Fix-Its, assume that
238       // the 'note:' Fix-It is just an alternative way to solve the issue and
239       // ignore these Fix-Its.
240       if (!clang_diag || clang_diag->HasFixIts())
241         break;
242       // Ignore all Fix-Its that are not associated with an error.
243       if (clang_diag->GetSeverity() != eDiagnosticSeverityError)
244         break;
245       AddAllFixIts(clang_diag, Info);
246       break;
247     }
248     if (make_new_diagnostic) {
249       // ClangDiagnostic messages are expected to have no whitespace/newlines
250       // around them.
251       std::string stripped_output =
252           std::string(llvm::StringRef(m_output).trim());
253 
254       auto new_diagnostic = std::make_unique<ClangDiagnostic>(
255           stripped_output, severity, Info.getID());
256 
257       // Don't store away warning fixits, since the compiler doesn't have
258       // enough context in an expression for the warning to be useful.
259       // FIXME: Should we try to filter out FixIts that apply to our generated
260       // code, and not the user's expression?
261       if (severity == eDiagnosticSeverityError)
262         AddAllFixIts(new_diagnostic.get(), Info);
263 
264       m_manager->AddDiagnostic(std::move(new_diagnostic));
265     }
266   }
267 
268   void BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) override {
269     m_passthrough->BeginSourceFile(LO, PP);
270   }
271 
272   void EndSourceFile() override { m_passthrough->EndSourceFile(); }
273 
274 private:
275   DiagnosticManager *m_manager = nullptr;
276   std::shared_ptr<clang::TextDiagnosticPrinter> m_passthrough;
277   /// Output stream of m_passthrough.
278   std::shared_ptr<llvm::raw_string_ostream> m_os;
279   /// Output string filled by m_os.
280   std::string m_output;
281 };
282 
283 static void SetupModuleHeaderPaths(CompilerInstance *compiler,
284                                    std::vector<std::string> include_directories,
285                                    lldb::TargetSP target_sp) {
286   Log *log = GetLog(LLDBLog::Expressions);
287 
288   HeaderSearchOptions &search_opts = compiler->getHeaderSearchOpts();
289 
290   for (const std::string &dir : include_directories) {
291     search_opts.AddPath(dir, frontend::System, false, true);
292     LLDB_LOG(log, "Added user include dir: {0}", dir);
293   }
294 
295   llvm::SmallString<128> module_cache;
296   const auto &props = ModuleList::GetGlobalModuleListProperties();
297   props.GetClangModulesCachePath().GetPath(module_cache);
298   search_opts.ModuleCachePath = std::string(module_cache.str());
299   LLDB_LOG(log, "Using module cache path: {0}", module_cache.c_str());
300 
301   search_opts.ResourceDir = GetClangResourceDir().GetPath();
302 
303   search_opts.ImplicitModuleMaps = true;
304 }
305 
306 /// Iff the given identifier is a C++ keyword, remove it from the
307 /// identifier table (i.e., make the token a normal identifier).
308 static void RemoveCppKeyword(IdentifierTable &idents, llvm::StringRef token) {
309   // FIXME: 'using' is used by LLDB for local variables, so we can't remove
310   // this keyword without breaking this functionality.
311   if (token == "using")
312     return;
313   // GCC's '__null' is used by LLDB to define NULL/Nil/nil.
314   if (token == "__null")
315     return;
316 
317   LangOptions cpp_lang_opts;
318   cpp_lang_opts.CPlusPlus = true;
319   cpp_lang_opts.CPlusPlus11 = true;
320   cpp_lang_opts.CPlusPlus20 = true;
321 
322   clang::IdentifierInfo &ii = idents.get(token);
323   // The identifier has to be a C++-exclusive keyword. if not, then there is
324   // nothing to do.
325   if (!ii.isCPlusPlusKeyword(cpp_lang_opts))
326     return;
327   // If the token is already an identifier, then there is nothing to do.
328   if (ii.getTokenID() == clang::tok::identifier)
329     return;
330   // Otherwise the token is a C++ keyword, so turn it back into a normal
331   // identifier.
332   ii.revertTokenIDToIdentifier();
333 }
334 
335 /// Remove all C++ keywords from the given identifier table.
336 static void RemoveAllCppKeywords(IdentifierTable &idents) {
337 #define KEYWORD(NAME, FLAGS) RemoveCppKeyword(idents, llvm::StringRef(#NAME));
338 #include "clang/Basic/TokenKinds.def"
339 }
340 
341 /// Configures Clang diagnostics for the expression parser.
342 static void SetupDefaultClangDiagnostics(CompilerInstance &compiler) {
343   // List of Clang warning groups that are not useful when parsing expressions.
344   const std::vector<const char *> groupsToIgnore = {
345       "unused-value",
346       "odr",
347       "unused-getter-return-value",
348   };
349   for (const char *group : groupsToIgnore) {
350     compiler.getDiagnostics().setSeverityForGroup(
351         clang::diag::Flavor::WarningOrError, group,
352         clang::diag::Severity::Ignored, SourceLocation());
353   }
354 }
355 
356 //===----------------------------------------------------------------------===//
357 // Implementation of ClangExpressionParser
358 //===----------------------------------------------------------------------===//
359 
360 ClangExpressionParser::ClangExpressionParser(
361     ExecutionContextScope *exe_scope, Expression &expr,
362     bool generate_debug_info, std::vector<std::string> include_directories,
363     std::string filename)
364     : ExpressionParser(exe_scope, expr, generate_debug_info), m_compiler(),
365       m_pp_callbacks(nullptr),
366       m_include_directories(std::move(include_directories)),
367       m_filename(std::move(filename)) {
368   Log *log = GetLog(LLDBLog::Expressions);
369 
370   // We can't compile expressions without a target.  So if the exe_scope is
371   // null or doesn't have a target, then we just need to get out of here.  I'll
372   // lldbassert and not make any of the compiler objects since
373   // I can't return errors directly from the constructor.  Further calls will
374   // check if the compiler was made and
375   // bag out if it wasn't.
376 
377   if (!exe_scope) {
378     lldbassert(exe_scope &&
379                "Can't make an expression parser with a null scope.");
380     return;
381   }
382 
383   lldb::TargetSP target_sp;
384   target_sp = exe_scope->CalculateTarget();
385   if (!target_sp) {
386     lldbassert(target_sp.get() &&
387                "Can't make an expression parser with a null target.");
388     return;
389   }
390 
391   // 1. Create a new compiler instance.
392   m_compiler = std::make_unique<CompilerInstance>();
393 
394   // Make sure clang uses the same VFS as LLDB.
395   m_compiler->createFileManager(FileSystem::Instance().GetVirtualFileSystem());
396 
397   lldb::LanguageType frame_lang =
398       expr.Language(); // defaults to lldb::eLanguageTypeUnknown
399   bool overridden_target_opts = false;
400   lldb_private::LanguageRuntime *lang_rt = nullptr;
401 
402   std::string abi;
403   ArchSpec target_arch;
404   target_arch = target_sp->GetArchitecture();
405 
406   const auto target_machine = target_arch.GetMachine();
407 
408   // If the expression is being evaluated in the context of an existing stack
409   // frame, we introspect to see if the language runtime is available.
410 
411   lldb::StackFrameSP frame_sp = exe_scope->CalculateStackFrame();
412   lldb::ProcessSP process_sp = exe_scope->CalculateProcess();
413 
414   // Make sure the user hasn't provided a preferred execution language with
415   // `expression --language X -- ...`
416   if (frame_sp && frame_lang == lldb::eLanguageTypeUnknown)
417     frame_lang = frame_sp->GetLanguage();
418 
419   if (process_sp && frame_lang != lldb::eLanguageTypeUnknown) {
420     lang_rt = process_sp->GetLanguageRuntime(frame_lang);
421     LLDB_LOGF(log, "Frame has language of type %s",
422               Language::GetNameForLanguageType(frame_lang));
423   }
424 
425   // 2. Configure the compiler with a set of default options that are
426   // appropriate for most situations.
427   if (target_arch.IsValid()) {
428     std::string triple = target_arch.GetTriple().str();
429     m_compiler->getTargetOpts().Triple = triple;
430     LLDB_LOGF(log, "Using %s as the target triple",
431               m_compiler->getTargetOpts().Triple.c_str());
432   } else {
433     // If we get here we don't have a valid target and just have to guess.
434     // Sometimes this will be ok to just use the host target triple (when we
435     // evaluate say "2+3", but other expressions like breakpoint conditions and
436     // other things that _are_ target specific really shouldn't just be using
437     // the host triple. In such a case the language runtime should expose an
438     // overridden options set (3), below.
439     m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
440     LLDB_LOGF(log, "Using default target triple of %s",
441               m_compiler->getTargetOpts().Triple.c_str());
442   }
443   // Now add some special fixes for known architectures: Any arm32 iOS
444   // environment, but not on arm64
445   if (m_compiler->getTargetOpts().Triple.find("arm64") == std::string::npos &&
446       m_compiler->getTargetOpts().Triple.find("arm") != std::string::npos &&
447       m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos) {
448     m_compiler->getTargetOpts().ABI = "apcs-gnu";
449   }
450   // Supported subsets of x86
451   if (target_machine == llvm::Triple::x86 ||
452       target_machine == llvm::Triple::x86_64) {
453     m_compiler->getTargetOpts().Features.push_back("+sse");
454     m_compiler->getTargetOpts().Features.push_back("+sse2");
455   }
456 
457   // Set the target CPU to generate code for. This will be empty for any CPU
458   // that doesn't really need to make a special
459   // CPU string.
460   m_compiler->getTargetOpts().CPU = target_arch.GetClangTargetCPU();
461 
462   // Set the target ABI
463   abi = GetClangTargetABI(target_arch);
464   if (!abi.empty())
465     m_compiler->getTargetOpts().ABI = abi;
466 
467   // 3. Now allow the runtime to provide custom configuration options for the
468   // target. In this case, a specialized language runtime is available and we
469   // can query it for extra options. For 99% of use cases, this will not be
470   // needed and should be provided when basic platform detection is not enough.
471   // FIXME: Generalize this. Only RenderScriptRuntime currently supports this
472   // currently. Hardcoding this isn't ideal but it's better than LanguageRuntime
473   // having knowledge of clang::TargetOpts.
474   if (auto *renderscript_rt =
475           llvm::dyn_cast_or_null<RenderScriptRuntime>(lang_rt))
476     overridden_target_opts =
477         renderscript_rt->GetOverrideExprOptions(m_compiler->getTargetOpts());
478 
479   if (overridden_target_opts)
480     if (log && log->GetVerbose()) {
481       LLDB_LOGV(
482           log, "Using overridden target options for the expression evaluation");
483 
484       auto opts = m_compiler->getTargetOpts();
485       LLDB_LOGV(log, "Triple: '{0}'", opts.Triple);
486       LLDB_LOGV(log, "CPU: '{0}'", opts.CPU);
487       LLDB_LOGV(log, "FPMath: '{0}'", opts.FPMath);
488       LLDB_LOGV(log, "ABI: '{0}'", opts.ABI);
489       LLDB_LOGV(log, "LinkerVersion: '{0}'", opts.LinkerVersion);
490       StringList::LogDump(log, opts.FeaturesAsWritten, "FeaturesAsWritten");
491       StringList::LogDump(log, opts.Features, "Features");
492     }
493 
494   // 4. Create and install the target on the compiler.
495   m_compiler->createDiagnostics();
496   // Limit the number of error diagnostics we emit.
497   // A value of 0 means no limit for both LLDB and Clang.
498   m_compiler->getDiagnostics().setErrorLimit(target_sp->GetExprErrorLimit());
499 
500   auto target_info = TargetInfo::CreateTargetInfo(
501       m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts);
502   if (log) {
503     LLDB_LOGF(log, "Using SIMD alignment: %d",
504               target_info->getSimdDefaultAlign());
505     LLDB_LOGF(log, "Target datalayout string: '%s'",
506               target_info->getDataLayoutString());
507     LLDB_LOGF(log, "Target ABI: '%s'", target_info->getABI().str().c_str());
508     LLDB_LOGF(log, "Target vector alignment: %d",
509               target_info->getMaxVectorAlign());
510   }
511   m_compiler->setTarget(target_info);
512 
513   assert(m_compiler->hasTarget());
514 
515   // 5. Set language options.
516   lldb::LanguageType language = expr.Language();
517   LangOptions &lang_opts = m_compiler->getLangOpts();
518 
519   switch (language) {
520   case lldb::eLanguageTypeC:
521   case lldb::eLanguageTypeC89:
522   case lldb::eLanguageTypeC99:
523   case lldb::eLanguageTypeC11:
524     // FIXME: the following language option is a temporary workaround,
525     // to "ask for C, get C++."
526     // For now, the expression parser must use C++ anytime the language is a C
527     // family language, because the expression parser uses features of C++ to
528     // capture values.
529     lang_opts.CPlusPlus = true;
530     break;
531   case lldb::eLanguageTypeObjC:
532     lang_opts.ObjC = true;
533     // FIXME: the following language option is a temporary workaround,
534     // to "ask for ObjC, get ObjC++" (see comment above).
535     lang_opts.CPlusPlus = true;
536 
537     // Clang now sets as default C++14 as the default standard (with
538     // GNU extensions), so we do the same here to avoid mismatches that
539     // cause compiler error when evaluating expressions (e.g. nullptr not found
540     // as it's a C++11 feature). Currently lldb evaluates C++14 as C++11 (see
541     // two lines below) so we decide to be consistent with that, but this could
542     // be re-evaluated in the future.
543     lang_opts.CPlusPlus11 = true;
544     break;
545   case lldb::eLanguageTypeC_plus_plus:
546   case lldb::eLanguageTypeC_plus_plus_11:
547   case lldb::eLanguageTypeC_plus_plus_14:
548     lang_opts.CPlusPlus11 = true;
549     m_compiler->getHeaderSearchOpts().UseLibcxx = true;
550     [[fallthrough]];
551   case lldb::eLanguageTypeC_plus_plus_03:
552     lang_opts.CPlusPlus = true;
553     if (process_sp)
554       lang_opts.ObjC =
555           process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC) != nullptr;
556     break;
557   case lldb::eLanguageTypeObjC_plus_plus:
558   case lldb::eLanguageTypeUnknown:
559   default:
560     lang_opts.ObjC = true;
561     lang_opts.CPlusPlus = true;
562     lang_opts.CPlusPlus11 = true;
563     m_compiler->getHeaderSearchOpts().UseLibcxx = true;
564     break;
565   }
566 
567   lang_opts.Bool = true;
568   lang_opts.WChar = true;
569   lang_opts.Blocks = true;
570   lang_opts.DebuggerSupport =
571       true; // Features specifically for debugger clients
572   if (expr.DesiredResultType() == Expression::eResultTypeId)
573     lang_opts.DebuggerCastResultToId = true;
574 
575   lang_opts.CharIsSigned = ArchSpec(m_compiler->getTargetOpts().Triple.c_str())
576                                .CharIsSignedByDefault();
577 
578   // Spell checking is a nice feature, but it ends up completing a lot of types
579   // that we didn't strictly speaking need to complete. As a result, we spend a
580   // long time parsing and importing debug information.
581   lang_opts.SpellChecking = false;
582 
583   auto *clang_expr = dyn_cast<ClangUserExpression>(&m_expr);
584   if (clang_expr && clang_expr->DidImportCxxModules()) {
585     LLDB_LOG(log, "Adding lang options for importing C++ modules");
586 
587     lang_opts.Modules = true;
588     // We want to implicitly build modules.
589     lang_opts.ImplicitModules = true;
590     // To automatically import all submodules when we import 'std'.
591     lang_opts.ModulesLocalVisibility = false;
592 
593     // We use the @import statements, so we need this:
594     // FIXME: We could use the modules-ts, but that currently doesn't work.
595     lang_opts.ObjC = true;
596 
597     // Options we need to parse libc++ code successfully.
598     // FIXME: We should ask the driver for the appropriate default flags.
599     lang_opts.GNUMode = true;
600     lang_opts.GNUKeywords = true;
601     lang_opts.DoubleSquareBracketAttributes = true;
602     lang_opts.CPlusPlus11 = true;
603 
604     // The Darwin libc expects this macro to be set.
605     lang_opts.GNUCVersion = 40201;
606 
607     SetupModuleHeaderPaths(m_compiler.get(), m_include_directories,
608                            target_sp);
609   }
610 
611   if (process_sp && lang_opts.ObjC) {
612     if (auto *runtime = ObjCLanguageRuntime::Get(*process_sp)) {
613       if (runtime->GetRuntimeVersion() ==
614           ObjCLanguageRuntime::ObjCRuntimeVersions::eAppleObjC_V2)
615         lang_opts.ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7));
616       else
617         lang_opts.ObjCRuntime.set(ObjCRuntime::FragileMacOSX,
618                                   VersionTuple(10, 7));
619 
620       if (runtime->HasNewLiteralsAndIndexing())
621         lang_opts.DebuggerObjCLiteral = true;
622     }
623   }
624 
625   lang_opts.ThreadsafeStatics = false;
626   lang_opts.AccessControl = false; // Debuggers get universal access
627   lang_opts.DollarIdents = true;   // $ indicates a persistent variable name
628   // We enable all builtin functions beside the builtins from libc/libm (e.g.
629   // 'fopen'). Those libc functions are already correctly handled by LLDB, and
630   // additionally enabling them as expandable builtins is breaking Clang.
631   lang_opts.NoBuiltin = true;
632 
633   // Set CodeGen options
634   m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
635   m_compiler->getCodeGenOpts().InstrumentFunctions = false;
636   m_compiler->getCodeGenOpts().setFramePointer(
637                                     CodeGenOptions::FramePointerKind::All);
638   if (generate_debug_info)
639     m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::FullDebugInfo);
640   else
641     m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::NoDebugInfo);
642 
643   // Disable some warnings.
644   SetupDefaultClangDiagnostics(*m_compiler);
645 
646   // Inform the target of the language options
647   //
648   // FIXME: We shouldn't need to do this, the target should be immutable once
649   // created. This complexity should be lifted elsewhere.
650   m_compiler->getTarget().adjust(m_compiler->getDiagnostics(),
651 		                 m_compiler->getLangOpts());
652 
653   // 6. Set up the diagnostic buffer for reporting errors
654 
655   auto diag_mgr = new ClangDiagnosticManagerAdapter(
656       m_compiler->getDiagnostics().getDiagnosticOptions());
657   m_compiler->getDiagnostics().setClient(diag_mgr);
658 
659   // 7. Set up the source management objects inside the compiler
660   m_compiler->createFileManager();
661   if (!m_compiler->hasSourceManager())
662     m_compiler->createSourceManager(m_compiler->getFileManager());
663   m_compiler->createPreprocessor(TU_Complete);
664 
665   switch (language) {
666   case lldb::eLanguageTypeC:
667   case lldb::eLanguageTypeC89:
668   case lldb::eLanguageTypeC99:
669   case lldb::eLanguageTypeC11:
670   case lldb::eLanguageTypeObjC:
671     // This is not a C++ expression but we enabled C++ as explained above.
672     // Remove all C++ keywords from the PP so that the user can still use
673     // variables that have C++ keywords as names (e.g. 'int template;').
674     RemoveAllCppKeywords(m_compiler->getPreprocessor().getIdentifierTable());
675     break;
676   default:
677     break;
678   }
679 
680   if (auto *clang_persistent_vars = llvm::cast<ClangPersistentVariables>(
681           target_sp->GetPersistentExpressionStateForLanguage(
682               lldb::eLanguageTypeC))) {
683     if (std::shared_ptr<ClangModulesDeclVendor> decl_vendor =
684             clang_persistent_vars->GetClangModulesDeclVendor()) {
685       std::unique_ptr<PPCallbacks> pp_callbacks(
686           new LLDBPreprocessorCallbacks(*decl_vendor, *clang_persistent_vars,
687                                         m_compiler->getSourceManager()));
688       m_pp_callbacks =
689           static_cast<LLDBPreprocessorCallbacks *>(pp_callbacks.get());
690       m_compiler->getPreprocessor().addPPCallbacks(std::move(pp_callbacks));
691     }
692   }
693 
694   // 8. Most of this we get from the CompilerInstance, but we also want to give
695   // the context an ExternalASTSource.
696 
697   auto &PP = m_compiler->getPreprocessor();
698   auto &builtin_context = PP.getBuiltinInfo();
699   builtin_context.initializeBuiltins(PP.getIdentifierTable(),
700                                      m_compiler->getLangOpts());
701 
702   m_compiler->createASTContext();
703   clang::ASTContext &ast_context = m_compiler->getASTContext();
704 
705   m_ast_context = std::make_shared<TypeSystemClang>(
706       "Expression ASTContext for '" + m_filename + "'", ast_context);
707 
708   std::string module_name("$__lldb_module");
709 
710   m_llvm_context = std::make_unique<LLVMContext>();
711   m_code_generator.reset(CreateLLVMCodeGen(
712       m_compiler->getDiagnostics(), module_name,
713       &m_compiler->getVirtualFileSystem(), m_compiler->getHeaderSearchOpts(),
714       m_compiler->getPreprocessorOpts(), m_compiler->getCodeGenOpts(),
715       *m_llvm_context));
716 }
717 
718 ClangExpressionParser::~ClangExpressionParser() = default;
719 
720 namespace {
721 
722 /// \class CodeComplete
723 ///
724 /// A code completion consumer for the clang Sema that is responsible for
725 /// creating the completion suggestions when a user requests completion
726 /// of an incomplete `expr` invocation.
727 class CodeComplete : public CodeCompleteConsumer {
728   CodeCompletionTUInfo m_info;
729 
730   std::string m_expr;
731   unsigned m_position = 0;
732   /// The printing policy we use when printing declarations for our completion
733   /// descriptions.
734   clang::PrintingPolicy m_desc_policy;
735 
736   struct CompletionWithPriority {
737     CompletionResult::Completion completion;
738     /// See CodeCompletionResult::Priority;
739     unsigned Priority;
740 
741     /// Establishes a deterministic order in a list of CompletionWithPriority.
742     /// The order returned here is the order in which the completions are
743     /// displayed to the user.
744     bool operator<(const CompletionWithPriority &o) const {
745       // High priority results should come first.
746       if (Priority != o.Priority)
747         return Priority > o.Priority;
748 
749       // Identical priority, so just make sure it's a deterministic order.
750       return completion.GetUniqueKey() < o.completion.GetUniqueKey();
751     }
752   };
753 
754   /// The stored completions.
755   /// Warning: These are in a non-deterministic order until they are sorted
756   /// and returned back to the caller.
757   std::vector<CompletionWithPriority> m_completions;
758 
759   /// Returns true if the given character can be used in an identifier.
760   /// This also returns true for numbers because for completion we usually
761   /// just iterate backwards over iterators.
762   ///
763   /// Note: lldb uses '$' in its internal identifiers, so we also allow this.
764   static bool IsIdChar(char c) {
765     return c == '_' || std::isalnum(c) || c == '$';
766   }
767 
768   /// Returns true if the given character is used to separate arguments
769   /// in the command line of lldb.
770   static bool IsTokenSeparator(char c) { return c == ' ' || c == '\t'; }
771 
772   /// Drops all tokens in front of the expression that are unrelated for
773   /// the completion of the cmd line. 'unrelated' means here that the token
774   /// is not interested for the lldb completion API result.
775   StringRef dropUnrelatedFrontTokens(StringRef cmd) const {
776     if (cmd.empty())
777       return cmd;
778 
779     // If we are at the start of a word, then all tokens are unrelated to
780     // the current completion logic.
781     if (IsTokenSeparator(cmd.back()))
782       return StringRef();
783 
784     // Remove all previous tokens from the string as they are unrelated
785     // to completing the current token.
786     StringRef to_remove = cmd;
787     while (!to_remove.empty() && !IsTokenSeparator(to_remove.back())) {
788       to_remove = to_remove.drop_back();
789     }
790     cmd = cmd.drop_front(to_remove.size());
791 
792     return cmd;
793   }
794 
795   /// Removes the last identifier token from the given cmd line.
796   StringRef removeLastToken(StringRef cmd) const {
797     while (!cmd.empty() && IsIdChar(cmd.back())) {
798       cmd = cmd.drop_back();
799     }
800     return cmd;
801   }
802 
803   /// Attempts to merge the given completion from the given position into the
804   /// existing command. Returns the completion string that can be returned to
805   /// the lldb completion API.
806   std::string mergeCompletion(StringRef existing, unsigned pos,
807                               StringRef completion) const {
808     StringRef existing_command = existing.substr(0, pos);
809     // We rewrite the last token with the completion, so let's drop that
810     // token from the command.
811     existing_command = removeLastToken(existing_command);
812     // We also should remove all previous tokens from the command as they
813     // would otherwise be added to the completion that already has the
814     // completion.
815     existing_command = dropUnrelatedFrontTokens(existing_command);
816     return existing_command.str() + completion.str();
817   }
818 
819 public:
820   /// Constructs a CodeComplete consumer that can be attached to a Sema.
821   ///
822   /// \param[out] expr
823   ///    The whole expression string that we are currently parsing. This
824   ///    string needs to be equal to the input the user typed, and NOT the
825   ///    final code that Clang is parsing.
826   /// \param[out] position
827   ///    The character position of the user cursor in the `expr` parameter.
828   ///
829   CodeComplete(clang::LangOptions ops, std::string expr, unsigned position)
830       : CodeCompleteConsumer(CodeCompleteOptions()),
831         m_info(std::make_shared<GlobalCodeCompletionAllocator>()), m_expr(expr),
832         m_position(position), m_desc_policy(ops) {
833 
834     // Ensure that the printing policy is producing a description that is as
835     // short as possible.
836     m_desc_policy.SuppressScope = true;
837     m_desc_policy.SuppressTagKeyword = true;
838     m_desc_policy.FullyQualifiedName = false;
839     m_desc_policy.TerseOutput = true;
840     m_desc_policy.IncludeNewlines = false;
841     m_desc_policy.UseVoidForZeroParams = false;
842     m_desc_policy.Bool = true;
843   }
844 
845   /// \name Code-completion filtering
846   /// Check if the result should be filtered out.
847   bool isResultFilteredOut(StringRef Filter,
848                            CodeCompletionResult Result) override {
849     // This code is mostly copied from CodeCompleteConsumer.
850     switch (Result.Kind) {
851     case CodeCompletionResult::RK_Declaration:
852       return !(
853           Result.Declaration->getIdentifier() &&
854           Result.Declaration->getIdentifier()->getName().startswith(Filter));
855     case CodeCompletionResult::RK_Keyword:
856       return !StringRef(Result.Keyword).startswith(Filter);
857     case CodeCompletionResult::RK_Macro:
858       return !Result.Macro->getName().startswith(Filter);
859     case CodeCompletionResult::RK_Pattern:
860       return !StringRef(Result.Pattern->getAsString()).startswith(Filter);
861     }
862     // If we trigger this assert or the above switch yields a warning, then
863     // CodeCompletionResult has been enhanced with more kinds of completion
864     // results. Expand the switch above in this case.
865     assert(false && "Unknown completion result type?");
866     // If we reach this, then we should just ignore whatever kind of unknown
867     // result we got back. We probably can't turn it into any kind of useful
868     // completion suggestion with the existing code.
869     return true;
870   }
871 
872 private:
873   /// Generate the completion strings for the given CodeCompletionResult.
874   /// Note that this function has to process results that could come in
875   /// non-deterministic order, so this function should have no side effects.
876   /// To make this easier to enforce, this function and all its parameters
877   /// should always be const-qualified.
878   /// \return Returns std::nullopt if no completion should be provided for the
879   ///         given CodeCompletionResult.
880   std::optional<CompletionWithPriority>
881   getCompletionForResult(const CodeCompletionResult &R) const {
882     std::string ToInsert;
883     std::string Description;
884     // Handle the different completion kinds that come from the Sema.
885     switch (R.Kind) {
886     case CodeCompletionResult::RK_Declaration: {
887       const NamedDecl *D = R.Declaration;
888       ToInsert = R.Declaration->getNameAsString();
889       // If we have a function decl that has no arguments we want to
890       // complete the empty parantheses for the user. If the function has
891       // arguments, we at least complete the opening bracket.
892       if (const FunctionDecl *F = dyn_cast<FunctionDecl>(D)) {
893         if (F->getNumParams() == 0)
894           ToInsert += "()";
895         else
896           ToInsert += "(";
897         raw_string_ostream OS(Description);
898         F->print(OS, m_desc_policy, false);
899         OS.flush();
900       } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
901         Description = V->getType().getAsString(m_desc_policy);
902       } else if (const FieldDecl *F = dyn_cast<FieldDecl>(D)) {
903         Description = F->getType().getAsString(m_desc_policy);
904       } else if (const NamespaceDecl *N = dyn_cast<NamespaceDecl>(D)) {
905         // If we try to complete a namespace, then we can directly append
906         // the '::'.
907         if (!N->isAnonymousNamespace())
908           ToInsert += "::";
909       }
910       break;
911     }
912     case CodeCompletionResult::RK_Keyword:
913       ToInsert = R.Keyword;
914       break;
915     case CodeCompletionResult::RK_Macro:
916       ToInsert = R.Macro->getName().str();
917       break;
918     case CodeCompletionResult::RK_Pattern:
919       ToInsert = R.Pattern->getTypedText();
920       break;
921     }
922     // We also filter some internal lldb identifiers here. The user
923     // shouldn't see these.
924     if (llvm::StringRef(ToInsert).startswith("$__lldb_"))
925       return std::nullopt;
926     if (ToInsert.empty())
927       return std::nullopt;
928     // Merge the suggested Token into the existing command line to comply
929     // with the kind of result the lldb API expects.
930     std::string CompletionSuggestion =
931         mergeCompletion(m_expr, m_position, ToInsert);
932 
933     CompletionResult::Completion completion(CompletionSuggestion, Description,
934                                             CompletionMode::Normal);
935     return {{completion, R.Priority}};
936   }
937 
938 public:
939   /// Adds the completions to the given CompletionRequest.
940   void GetCompletions(CompletionRequest &request) {
941     // Bring m_completions into a deterministic order and pass it on to the
942     // CompletionRequest.
943     llvm::sort(m_completions);
944 
945     for (const CompletionWithPriority &C : m_completions)
946       request.AddCompletion(C.completion.GetCompletion(),
947                             C.completion.GetDescription(),
948                             C.completion.GetMode());
949   }
950 
951   /// \name Code-completion callbacks
952   /// Process the finalized code-completion results.
953   void ProcessCodeCompleteResults(Sema &SemaRef, CodeCompletionContext Context,
954                                   CodeCompletionResult *Results,
955                                   unsigned NumResults) override {
956 
957     // The Sema put the incomplete token we try to complete in here during
958     // lexing, so we need to retrieve it here to know what we are completing.
959     StringRef Filter = SemaRef.getPreprocessor().getCodeCompletionFilter();
960 
961     // Iterate over all the results. Filter out results we don't want and
962     // process the rest.
963     for (unsigned I = 0; I != NumResults; ++I) {
964       // Filter the results with the information from the Sema.
965       if (!Filter.empty() && isResultFilteredOut(Filter, Results[I]))
966         continue;
967 
968       CodeCompletionResult &R = Results[I];
969       std::optional<CompletionWithPriority> CompletionAndPriority =
970           getCompletionForResult(R);
971       if (!CompletionAndPriority)
972         continue;
973       m_completions.push_back(*CompletionAndPriority);
974     }
975   }
976 
977   /// \param S the semantic-analyzer object for which code-completion is being
978   /// done.
979   ///
980   /// \param CurrentArg the index of the current argument.
981   ///
982   /// \param Candidates an array of overload candidates.
983   ///
984   /// \param NumCandidates the number of overload candidates
985   void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
986                                  OverloadCandidate *Candidates,
987                                  unsigned NumCandidates,
988                                  SourceLocation OpenParLoc,
989                                  bool Braced) override {
990     // At the moment we don't filter out any overloaded candidates.
991   }
992 
993   CodeCompletionAllocator &getAllocator() override {
994     return m_info.getAllocator();
995   }
996 
997   CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return m_info; }
998 };
999 } // namespace
1000 
1001 bool ClangExpressionParser::Complete(CompletionRequest &request, unsigned line,
1002                                      unsigned pos, unsigned typed_pos) {
1003   DiagnosticManager mgr;
1004   // We need the raw user expression here because that's what the CodeComplete
1005   // class uses to provide completion suggestions.
1006   // However, the `Text` method only gives us the transformed expression here.
1007   // To actually get the raw user input here, we have to cast our expression to
1008   // the LLVMUserExpression which exposes the right API. This should never fail
1009   // as we always have a ClangUserExpression whenever we call this.
1010   ClangUserExpression *llvm_expr = cast<ClangUserExpression>(&m_expr);
1011   CodeComplete CC(m_compiler->getLangOpts(), llvm_expr->GetUserText(),
1012                   typed_pos);
1013   // We don't need a code generator for parsing.
1014   m_code_generator.reset();
1015   // Start parsing the expression with our custom code completion consumer.
1016   ParseInternal(mgr, &CC, line, pos);
1017   CC.GetCompletions(request);
1018   return true;
1019 }
1020 
1021 unsigned ClangExpressionParser::Parse(DiagnosticManager &diagnostic_manager) {
1022   return ParseInternal(diagnostic_manager);
1023 }
1024 
1025 unsigned
1026 ClangExpressionParser::ParseInternal(DiagnosticManager &diagnostic_manager,
1027                                      CodeCompleteConsumer *completion_consumer,
1028                                      unsigned completion_line,
1029                                      unsigned completion_column) {
1030   ClangDiagnosticManagerAdapter *adapter =
1031       static_cast<ClangDiagnosticManagerAdapter *>(
1032           m_compiler->getDiagnostics().getClient());
1033 
1034   adapter->ResetManager(&diagnostic_manager);
1035 
1036   const char *expr_text = m_expr.Text();
1037 
1038   clang::SourceManager &source_mgr = m_compiler->getSourceManager();
1039   bool created_main_file = false;
1040 
1041   // Clang wants to do completion on a real file known by Clang's file manager,
1042   // so we have to create one to make this work.
1043   // TODO: We probably could also simulate to Clang's file manager that there
1044   // is a real file that contains our code.
1045   bool should_create_file = completion_consumer != nullptr;
1046 
1047   // We also want a real file on disk if we generate full debug info.
1048   should_create_file |= m_compiler->getCodeGenOpts().getDebugInfo() ==
1049                         codegenoptions::FullDebugInfo;
1050 
1051   if (should_create_file) {
1052     int temp_fd = -1;
1053     llvm::SmallString<128> result_path;
1054     if (FileSpec tmpdir_file_spec = HostInfo::GetProcessTempDir()) {
1055       tmpdir_file_spec.AppendPathComponent("lldb-%%%%%%.expr");
1056       std::string temp_source_path = tmpdir_file_spec.GetPath();
1057       llvm::sys::fs::createUniqueFile(temp_source_path, temp_fd, result_path);
1058     } else {
1059       llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path);
1060     }
1061 
1062     if (temp_fd != -1) {
1063       lldb_private::NativeFile file(temp_fd, File::eOpenOptionWriteOnly, true);
1064       const size_t expr_text_len = strlen(expr_text);
1065       size_t bytes_written = expr_text_len;
1066       if (file.Write(expr_text, bytes_written).Success()) {
1067         if (bytes_written == expr_text_len) {
1068           file.Close();
1069           if (auto fileEntry = m_compiler->getFileManager().getOptionalFileRef(
1070                   result_path)) {
1071             source_mgr.setMainFileID(source_mgr.createFileID(
1072                 *fileEntry,
1073                 SourceLocation(), SrcMgr::C_User));
1074             created_main_file = true;
1075           }
1076         }
1077       }
1078     }
1079   }
1080 
1081   if (!created_main_file) {
1082     std::unique_ptr<MemoryBuffer> memory_buffer =
1083         MemoryBuffer::getMemBufferCopy(expr_text, m_filename);
1084     source_mgr.setMainFileID(source_mgr.createFileID(std::move(memory_buffer)));
1085   }
1086 
1087   adapter->BeginSourceFile(m_compiler->getLangOpts(),
1088                            &m_compiler->getPreprocessor());
1089 
1090   ClangExpressionHelper *type_system_helper =
1091       dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
1092 
1093   // If we want to parse for code completion, we need to attach our code
1094   // completion consumer to the Sema and specify a completion position.
1095   // While parsing the Sema will call this consumer with the provided
1096   // completion suggestions.
1097   if (completion_consumer) {
1098     auto main_file = source_mgr.getFileEntryForID(source_mgr.getMainFileID());
1099     auto &PP = m_compiler->getPreprocessor();
1100     // Lines and columns start at 1 in Clang, but code completion positions are
1101     // indexed from 0, so we need to add 1 to the line and column here.
1102     ++completion_line;
1103     ++completion_column;
1104     PP.SetCodeCompletionPoint(main_file, completion_line, completion_column);
1105   }
1106 
1107   ASTConsumer *ast_transformer =
1108       type_system_helper->ASTTransformer(m_code_generator.get());
1109 
1110   std::unique_ptr<clang::ASTConsumer> Consumer;
1111   if (ast_transformer) {
1112     Consumer = std::make_unique<ASTConsumerForwarder>(ast_transformer);
1113   } else if (m_code_generator) {
1114     Consumer = std::make_unique<ASTConsumerForwarder>(m_code_generator.get());
1115   } else {
1116     Consumer = std::make_unique<ASTConsumer>();
1117   }
1118 
1119   clang::ASTContext &ast_context = m_compiler->getASTContext();
1120 
1121   m_compiler->setSema(new Sema(m_compiler->getPreprocessor(), ast_context,
1122                                *Consumer, TU_Complete, completion_consumer));
1123   m_compiler->setASTConsumer(std::move(Consumer));
1124 
1125   if (ast_context.getLangOpts().Modules) {
1126     m_compiler->createASTReader();
1127     m_ast_context->setSema(&m_compiler->getSema());
1128   }
1129 
1130   ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap();
1131   if (decl_map) {
1132     decl_map->InstallCodeGenerator(&m_compiler->getASTConsumer());
1133     decl_map->InstallDiagnosticManager(diagnostic_manager);
1134 
1135     clang::ExternalASTSource *ast_source = decl_map->CreateProxy();
1136 
1137     if (ast_context.getExternalSource()) {
1138       auto module_wrapper =
1139           new ExternalASTSourceWrapper(ast_context.getExternalSource());
1140 
1141       auto ast_source_wrapper = new ExternalASTSourceWrapper(ast_source);
1142 
1143       auto multiplexer =
1144           new SemaSourceWithPriorities(*module_wrapper, *ast_source_wrapper);
1145       IntrusiveRefCntPtr<ExternalASTSource> Source(multiplexer);
1146       ast_context.setExternalSource(Source);
1147     } else {
1148       ast_context.setExternalSource(ast_source);
1149     }
1150     decl_map->InstallASTContext(*m_ast_context);
1151   }
1152 
1153   // Check that the ASTReader is properly attached to ASTContext and Sema.
1154   if (ast_context.getLangOpts().Modules) {
1155     assert(m_compiler->getASTContext().getExternalSource() &&
1156            "ASTContext doesn't know about the ASTReader?");
1157     assert(m_compiler->getSema().getExternalSource() &&
1158            "Sema doesn't know about the ASTReader?");
1159   }
1160 
1161   {
1162     llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(
1163         &m_compiler->getSema());
1164     ParseAST(m_compiler->getSema(), false, false);
1165   }
1166 
1167   // Make sure we have no pointer to the Sema we are about to destroy.
1168   if (ast_context.getLangOpts().Modules)
1169     m_ast_context->setSema(nullptr);
1170   // Destroy the Sema. This is necessary because we want to emulate the
1171   // original behavior of ParseAST (which also destroys the Sema after parsing).
1172   m_compiler->setSema(nullptr);
1173 
1174   adapter->EndSourceFile();
1175 
1176   unsigned num_errors = adapter->getNumErrors();
1177 
1178   if (m_pp_callbacks && m_pp_callbacks->hasErrors()) {
1179     num_errors++;
1180     diagnostic_manager.PutString(eDiagnosticSeverityError,
1181                                  "while importing modules:");
1182     diagnostic_manager.AppendMessageToDiagnostic(
1183         m_pp_callbacks->getErrorString());
1184   }
1185 
1186   if (!num_errors) {
1187     type_system_helper->CommitPersistentDecls();
1188   }
1189 
1190   adapter->ResetManager();
1191 
1192   return num_errors;
1193 }
1194 
1195 std::string
1196 ClangExpressionParser::GetClangTargetABI(const ArchSpec &target_arch) {
1197   std::string abi;
1198 
1199   if (target_arch.IsMIPS()) {
1200     switch (target_arch.GetFlags() & ArchSpec::eMIPSABI_mask) {
1201     case ArchSpec::eMIPSABI_N64:
1202       abi = "n64";
1203       break;
1204     case ArchSpec::eMIPSABI_N32:
1205       abi = "n32";
1206       break;
1207     case ArchSpec::eMIPSABI_O32:
1208       abi = "o32";
1209       break;
1210     default:
1211       break;
1212     }
1213   }
1214   return abi;
1215 }
1216 
1217 /// Applies the given Fix-It hint to the given commit.
1218 static void ApplyFixIt(const FixItHint &fixit, clang::edit::Commit &commit) {
1219   // This is cobbed from clang::Rewrite::FixItRewriter.
1220   if (fixit.CodeToInsert.empty()) {
1221     if (fixit.InsertFromRange.isValid()) {
1222       commit.insertFromRange(fixit.RemoveRange.getBegin(),
1223                              fixit.InsertFromRange, /*afterToken=*/false,
1224                              fixit.BeforePreviousInsertions);
1225       return;
1226     }
1227     commit.remove(fixit.RemoveRange);
1228     return;
1229   }
1230   if (fixit.RemoveRange.isTokenRange() ||
1231       fixit.RemoveRange.getBegin() != fixit.RemoveRange.getEnd()) {
1232     commit.replace(fixit.RemoveRange, fixit.CodeToInsert);
1233     return;
1234   }
1235   commit.insert(fixit.RemoveRange.getBegin(), fixit.CodeToInsert,
1236                 /*afterToken=*/false, fixit.BeforePreviousInsertions);
1237 }
1238 
1239 bool ClangExpressionParser::RewriteExpression(
1240     DiagnosticManager &diagnostic_manager) {
1241   clang::SourceManager &source_manager = m_compiler->getSourceManager();
1242   clang::edit::EditedSource editor(source_manager, m_compiler->getLangOpts(),
1243                                    nullptr);
1244   clang::edit::Commit commit(editor);
1245   clang::Rewriter rewriter(source_manager, m_compiler->getLangOpts());
1246 
1247   class RewritesReceiver : public edit::EditsReceiver {
1248     Rewriter &rewrite;
1249 
1250   public:
1251     RewritesReceiver(Rewriter &in_rewrite) : rewrite(in_rewrite) {}
1252 
1253     void insert(SourceLocation loc, StringRef text) override {
1254       rewrite.InsertText(loc, text);
1255     }
1256     void replace(CharSourceRange range, StringRef text) override {
1257       rewrite.ReplaceText(range.getBegin(), rewrite.getRangeSize(range), text);
1258     }
1259   };
1260 
1261   RewritesReceiver rewrites_receiver(rewriter);
1262 
1263   const DiagnosticList &diagnostics = diagnostic_manager.Diagnostics();
1264   size_t num_diags = diagnostics.size();
1265   if (num_diags == 0)
1266     return false;
1267 
1268   for (const auto &diag : diagnostic_manager.Diagnostics()) {
1269     const auto *diagnostic = llvm::dyn_cast<ClangDiagnostic>(diag.get());
1270     if (!diagnostic)
1271       continue;
1272     if (!diagnostic->HasFixIts())
1273       continue;
1274     for (const FixItHint &fixit : diagnostic->FixIts())
1275       ApplyFixIt(fixit, commit);
1276   }
1277 
1278   // FIXME - do we want to try to propagate specific errors here?
1279   if (!commit.isCommitable())
1280     return false;
1281   else if (!editor.commit(commit))
1282     return false;
1283 
1284   // Now play all the edits, and stash the result in the diagnostic manager.
1285   editor.applyRewrites(rewrites_receiver);
1286   RewriteBuffer &main_file_buffer =
1287       rewriter.getEditBuffer(source_manager.getMainFileID());
1288 
1289   std::string fixed_expression;
1290   llvm::raw_string_ostream out_stream(fixed_expression);
1291 
1292   main_file_buffer.write(out_stream);
1293   out_stream.flush();
1294   diagnostic_manager.SetFixedExpression(fixed_expression);
1295 
1296   return true;
1297 }
1298 
1299 static bool FindFunctionInModule(ConstString &mangled_name,
1300                                  llvm::Module *module, const char *orig_name) {
1301   for (const auto &func : module->getFunctionList()) {
1302     const StringRef &name = func.getName();
1303     if (name.contains(orig_name)) {
1304       mangled_name.SetString(name);
1305       return true;
1306     }
1307   }
1308 
1309   return false;
1310 }
1311 
1312 lldb_private::Status ClangExpressionParser::PrepareForExecution(
1313     lldb::addr_t &func_addr, lldb::addr_t &func_end,
1314     lldb::IRExecutionUnitSP &execution_unit_sp, ExecutionContext &exe_ctx,
1315     bool &can_interpret, ExecutionPolicy execution_policy) {
1316   func_addr = LLDB_INVALID_ADDRESS;
1317   func_end = LLDB_INVALID_ADDRESS;
1318   Log *log = GetLog(LLDBLog::Expressions);
1319 
1320   lldb_private::Status err;
1321 
1322   std::unique_ptr<llvm::Module> llvm_module_up(
1323       m_code_generator->ReleaseModule());
1324 
1325   if (!llvm_module_up) {
1326     err.SetErrorToGenericError();
1327     err.SetErrorString("IR doesn't contain a module");
1328     return err;
1329   }
1330 
1331   ConstString function_name;
1332 
1333   if (execution_policy != eExecutionPolicyTopLevel) {
1334     // Find the actual name of the function (it's often mangled somehow)
1335 
1336     if (!FindFunctionInModule(function_name, llvm_module_up.get(),
1337                               m_expr.FunctionName())) {
1338       err.SetErrorToGenericError();
1339       err.SetErrorStringWithFormat("Couldn't find %s() in the module",
1340                                    m_expr.FunctionName());
1341       return err;
1342     } else {
1343       LLDB_LOGF(log, "Found function %s for %s", function_name.AsCString(),
1344                 m_expr.FunctionName());
1345     }
1346   }
1347 
1348   SymbolContext sc;
1349 
1350   if (lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP()) {
1351     sc = frame_sp->GetSymbolContext(lldb::eSymbolContextEverything);
1352   } else if (lldb::TargetSP target_sp = exe_ctx.GetTargetSP()) {
1353     sc.target_sp = target_sp;
1354   }
1355 
1356   LLVMUserExpression::IRPasses custom_passes;
1357   {
1358     auto lang = m_expr.Language();
1359     LLDB_LOGF(log, "%s - Current expression language is %s\n", __FUNCTION__,
1360               Language::GetNameForLanguageType(lang));
1361     lldb::ProcessSP process_sp = exe_ctx.GetProcessSP();
1362     if (process_sp && lang != lldb::eLanguageTypeUnknown) {
1363       auto runtime = process_sp->GetLanguageRuntime(lang);
1364       if (runtime)
1365         runtime->GetIRPasses(custom_passes);
1366     }
1367   }
1368 
1369   if (custom_passes.EarlyPasses) {
1370     LLDB_LOGF(log,
1371               "%s - Running Early IR Passes from LanguageRuntime on "
1372               "expression module '%s'",
1373               __FUNCTION__, m_expr.FunctionName());
1374 
1375     custom_passes.EarlyPasses->run(*llvm_module_up);
1376   }
1377 
1378   execution_unit_sp = std::make_shared<IRExecutionUnit>(
1379       m_llvm_context, // handed off here
1380       llvm_module_up, // handed off here
1381       function_name, exe_ctx.GetTargetSP(), sc,
1382       m_compiler->getTargetOpts().Features);
1383 
1384   ClangExpressionHelper *type_system_helper =
1385       dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
1386   ClangExpressionDeclMap *decl_map =
1387       type_system_helper->DeclMap(); // result can be NULL
1388 
1389   if (decl_map) {
1390     StreamString error_stream;
1391     IRForTarget ir_for_target(decl_map, m_expr.NeedsVariableResolution(),
1392                               *execution_unit_sp, error_stream,
1393                               function_name.AsCString());
1394 
1395     if (!ir_for_target.runOnModule(*execution_unit_sp->GetModule())) {
1396       err.SetErrorString(error_stream.GetString());
1397       return err;
1398     }
1399 
1400     Process *process = exe_ctx.GetProcessPtr();
1401 
1402     if (execution_policy != eExecutionPolicyAlways &&
1403         execution_policy != eExecutionPolicyTopLevel) {
1404       lldb_private::Status interpret_error;
1405 
1406       bool interpret_function_calls =
1407           !process ? false : process->CanInterpretFunctionCalls();
1408       can_interpret = IRInterpreter::CanInterpret(
1409           *execution_unit_sp->GetModule(), *execution_unit_sp->GetFunction(),
1410           interpret_error, interpret_function_calls);
1411 
1412       if (!can_interpret && execution_policy == eExecutionPolicyNever) {
1413         err.SetErrorStringWithFormat(
1414             "Can't evaluate the expression without a running target due to: %s",
1415             interpret_error.AsCString());
1416         return err;
1417       }
1418     }
1419 
1420     if (!process && execution_policy == eExecutionPolicyAlways) {
1421       err.SetErrorString("Expression needed to run in the target, but the "
1422                          "target can't be run");
1423       return err;
1424     }
1425 
1426     if (!process && execution_policy == eExecutionPolicyTopLevel) {
1427       err.SetErrorString("Top-level code needs to be inserted into a runnable "
1428                          "target, but the target can't be run");
1429       return err;
1430     }
1431 
1432     if (execution_policy == eExecutionPolicyAlways ||
1433         (execution_policy != eExecutionPolicyTopLevel && !can_interpret)) {
1434       if (m_expr.NeedsValidation() && process) {
1435         if (!process->GetDynamicCheckers()) {
1436           ClangDynamicCheckerFunctions *dynamic_checkers =
1437               new ClangDynamicCheckerFunctions();
1438 
1439           DiagnosticManager install_diagnostics;
1440 
1441           if (!dynamic_checkers->Install(install_diagnostics, exe_ctx)) {
1442             if (install_diagnostics.Diagnostics().size())
1443               err.SetErrorString(install_diagnostics.GetString().c_str());
1444             else
1445               err.SetErrorString("couldn't install checkers, unknown error");
1446 
1447             return err;
1448           }
1449 
1450           process->SetDynamicCheckers(dynamic_checkers);
1451 
1452           LLDB_LOGF(log, "== [ClangExpressionParser::PrepareForExecution] "
1453                          "Finished installing dynamic checkers ==");
1454         }
1455 
1456         if (auto *checker_funcs = llvm::dyn_cast<ClangDynamicCheckerFunctions>(
1457                 process->GetDynamicCheckers())) {
1458           IRDynamicChecks ir_dynamic_checks(*checker_funcs,
1459                                             function_name.AsCString());
1460 
1461           llvm::Module *module = execution_unit_sp->GetModule();
1462           if (!module || !ir_dynamic_checks.runOnModule(*module)) {
1463             err.SetErrorToGenericError();
1464             err.SetErrorString("Couldn't add dynamic checks to the expression");
1465             return err;
1466           }
1467 
1468           if (custom_passes.LatePasses) {
1469             LLDB_LOGF(log,
1470                       "%s - Running Late IR Passes from LanguageRuntime on "
1471                       "expression module '%s'",
1472                       __FUNCTION__, m_expr.FunctionName());
1473 
1474             custom_passes.LatePasses->run(*module);
1475           }
1476         }
1477       }
1478     }
1479 
1480     if (execution_policy == eExecutionPolicyAlways ||
1481         execution_policy == eExecutionPolicyTopLevel || !can_interpret) {
1482       execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
1483     }
1484   } else {
1485     execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
1486   }
1487 
1488   return err;
1489 }
1490 
1491 lldb_private::Status ClangExpressionParser::RunStaticInitializers(
1492     lldb::IRExecutionUnitSP &execution_unit_sp, ExecutionContext &exe_ctx) {
1493   lldb_private::Status err;
1494 
1495   lldbassert(execution_unit_sp.get());
1496   lldbassert(exe_ctx.HasThreadScope());
1497 
1498   if (!execution_unit_sp.get()) {
1499     err.SetErrorString(
1500         "can't run static initializers for a NULL execution unit");
1501     return err;
1502   }
1503 
1504   if (!exe_ctx.HasThreadScope()) {
1505     err.SetErrorString("can't run static initializers without a thread");
1506     return err;
1507   }
1508 
1509   std::vector<lldb::addr_t> static_initializers;
1510 
1511   execution_unit_sp->GetStaticInitializers(static_initializers);
1512 
1513   for (lldb::addr_t static_initializer : static_initializers) {
1514     EvaluateExpressionOptions options;
1515 
1516     lldb::ThreadPlanSP call_static_initializer(new ThreadPlanCallFunction(
1517         exe_ctx.GetThreadRef(), Address(static_initializer), CompilerType(),
1518         llvm::ArrayRef<lldb::addr_t>(), options));
1519 
1520     DiagnosticManager execution_errors;
1521     lldb::ExpressionResults results =
1522         exe_ctx.GetThreadRef().GetProcess()->RunThreadPlan(
1523             exe_ctx, call_static_initializer, options, execution_errors);
1524 
1525     if (results != lldb::eExpressionCompleted) {
1526       err.SetErrorStringWithFormat("couldn't run static initializer: %s",
1527                                    execution_errors.GetString().c_str());
1528       return err;
1529     }
1530   }
1531 
1532   return err;
1533 }
1534