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