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