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