]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / source / Plugins / ExpressionParser / Clang / ClangExpressionParser.cpp
1 //===-- ClangExpressionParser.cpp -------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // C Includes
11 // C++ Includes
12 // Other libraries and framework includes
13 #include "clang/AST/ASTContext.h"
14 #include "clang/AST/ASTDiagnostic.h"
15 #include "clang/AST/ExternalASTSource.h"
16 #include "clang/Basic/DiagnosticIDs.h"
17 #include "clang/Basic/FileManager.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Basic/TargetInfo.h"
20 #include "clang/Basic/Version.h"
21 #include "clang/CodeGen/CodeGenAction.h"
22 #include "clang/CodeGen/ModuleBuilder.h"
23 #include "clang/Edit/Commit.h"
24 #include "clang/Edit/EditedSource.h"
25 #include "clang/Edit/EditsReceiver.h"
26 #include "clang/Frontend/CompilerInstance.h"
27 #include "clang/Frontend/CompilerInvocation.h"
28 #include "clang/Frontend/FrontendActions.h"
29 #include "clang/Frontend/FrontendDiagnostic.h"
30 #include "clang/Frontend/FrontendPluginRegistry.h"
31 #include "clang/Frontend/TextDiagnosticBuffer.h"
32 #include "clang/Frontend/TextDiagnosticPrinter.h"
33 #include "clang/Lex/Preprocessor.h"
34 #include "clang/Parse/ParseAST.h"
35 #include "clang/Rewrite/Core/Rewriter.h"
36 #include "clang/Rewrite/Frontend/FrontendActions.h"
37 #include "clang/Sema/SemaConsumer.h"
38 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
39
40 #include "llvm/ADT/StringRef.h"
41 #include "llvm/ExecutionEngine/ExecutionEngine.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Support/FileSystem.h"
44 #include "llvm/Support/TargetSelect.h"
45
46 #pragma clang diagnostic push
47 #pragma clang diagnostic ignored "-Wglobal-constructors"
48 #include "llvm/ExecutionEngine/MCJIT.h"
49 #pragma clang diagnostic pop
50
51 #include "llvm/IR/LLVMContext.h"
52 #include "llvm/IR/Module.h"
53 #include "llvm/Support/DynamicLibrary.h"
54 #include "llvm/Support/ErrorHandling.h"
55 #include "llvm/Support/Host.h"
56 #include "llvm/Support/MemoryBuffer.h"
57 #include "llvm/Support/Signals.h"
58
59 // Project includes
60 #include "ClangDiagnostic.h"
61 #include "ClangExpressionParser.h"
62
63 #include "ClangASTSource.h"
64 #include "ClangExpressionDeclMap.h"
65 #include "ClangExpressionHelper.h"
66 #include "ClangModulesDeclVendor.h"
67 #include "ClangPersistentVariables.h"
68 #include "IRForTarget.h"
69
70 #include "lldb/Core/ArchSpec.h"
71 #include "lldb/Core/DataBufferHeap.h"
72 #include "lldb/Core/Debugger.h"
73 #include "lldb/Core/Disassembler.h"
74 #include "lldb/Core/Log.h"
75 #include "lldb/Core/Module.h"
76 #include "lldb/Core/Stream.h"
77 #include "lldb/Core/StreamFile.h"
78 #include "lldb/Core/StreamString.h"
79 #include "lldb/Core/StringList.h"
80 #include "lldb/Expression/IRDynamicChecks.h"
81 #include "lldb/Expression/IRExecutionUnit.h"
82 #include "lldb/Expression/IRInterpreter.h"
83 #include "lldb/Host/File.h"
84 #include "lldb/Host/HostInfo.h"
85 #include "lldb/Symbol/ClangASTContext.h"
86 #include "lldb/Symbol/SymbolVendor.h"
87 #include "lldb/Target/ExecutionContext.h"
88 #include "lldb/Target/Language.h"
89 #include "lldb/Target/ObjCLanguageRuntime.h"
90 #include "lldb/Target/Process.h"
91 #include "lldb/Target/Target.h"
92 #include "lldb/Target/ThreadPlanCallFunction.h"
93 #include "lldb/Utility/LLDBAssert.h"
94
95 using namespace clang;
96 using namespace llvm;
97 using namespace lldb_private;
98
99 //===----------------------------------------------------------------------===//
100 // Utility Methods for Clang
101 //===----------------------------------------------------------------------===//
102
103 class ClangExpressionParser::LLDBPreprocessorCallbacks : public PPCallbacks {
104   ClangModulesDeclVendor &m_decl_vendor;
105   ClangPersistentVariables &m_persistent_vars;
106   StreamString m_error_stream;
107   bool m_has_errors = false;
108
109 public:
110   LLDBPreprocessorCallbacks(ClangModulesDeclVendor &decl_vendor,
111                             ClangPersistentVariables &persistent_vars)
112       : m_decl_vendor(decl_vendor), m_persistent_vars(persistent_vars) {}
113
114   void moduleImport(SourceLocation import_location, clang::ModuleIdPath path,
115                     const clang::Module * /*null*/) override {
116     std::vector<ConstString> string_path;
117
118     for (const std::pair<IdentifierInfo *, SourceLocation> &component : path) {
119       string_path.push_back(ConstString(component.first->getName()));
120     }
121
122     StreamString error_stream;
123
124     ClangModulesDeclVendor::ModuleVector exported_modules;
125
126     if (!m_decl_vendor.AddModule(string_path, &exported_modules,
127                                  m_error_stream)) {
128       m_has_errors = true;
129     }
130
131     for (ClangModulesDeclVendor::ModuleID module : exported_modules) {
132       m_persistent_vars.AddHandLoadedClangModule(module);
133     }
134   }
135
136   bool hasErrors() { return m_has_errors; }
137
138   llvm::StringRef getErrorString() { return m_error_stream.GetString(); }
139 };
140
141 class ClangDiagnosticManagerAdapter : public clang::DiagnosticConsumer {
142 public:
143   ClangDiagnosticManagerAdapter()
144       : m_passthrough(new clang::TextDiagnosticBuffer) {}
145
146   ClangDiagnosticManagerAdapter(
147       const std::shared_ptr<clang::TextDiagnosticBuffer> &passthrough)
148       : m_passthrough(passthrough) {}
149
150   void ResetManager(DiagnosticManager *manager = nullptr) {
151     m_manager = manager;
152   }
153
154   void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
155                         const clang::Diagnostic &Info) {
156     if (m_manager) {
157       llvm::SmallVector<char, 32> diag_str;
158       Info.FormatDiagnostic(diag_str);
159       diag_str.push_back('\0');
160       const char *data = diag_str.data();
161
162       lldb_private::DiagnosticSeverity severity;
163       bool make_new_diagnostic = true;
164
165       switch (DiagLevel) {
166       case DiagnosticsEngine::Level::Fatal:
167       case DiagnosticsEngine::Level::Error:
168         severity = eDiagnosticSeverityError;
169         break;
170       case DiagnosticsEngine::Level::Warning:
171         severity = eDiagnosticSeverityWarning;
172         break;
173       case DiagnosticsEngine::Level::Remark:
174       case DiagnosticsEngine::Level::Ignored:
175         severity = eDiagnosticSeverityRemark;
176         break;
177       case DiagnosticsEngine::Level::Note:
178         m_manager->AppendMessageToDiagnostic(data);
179         make_new_diagnostic = false;
180       }
181       if (make_new_diagnostic) {
182         ClangDiagnostic *new_diagnostic =
183             new ClangDiagnostic(data, severity, Info.getID());
184         m_manager->AddDiagnostic(new_diagnostic);
185
186         // Don't store away warning fixits, since the compiler doesn't have
187         // enough
188         // context in an expression for the warning to be useful.
189         // FIXME: Should we try to filter out FixIts that apply to our generated
190         // code, and not the user's expression?
191         if (severity == eDiagnosticSeverityError) {
192           size_t num_fixit_hints = Info.getNumFixItHints();
193           for (size_t i = 0; i < num_fixit_hints; i++) {
194             const clang::FixItHint &fixit = Info.getFixItHint(i);
195             if (!fixit.isNull())
196               new_diagnostic->AddFixitHint(fixit);
197           }
198         }
199       }
200     }
201
202     m_passthrough->HandleDiagnostic(DiagLevel, Info);
203   }
204
205   void FlushDiagnostics(DiagnosticsEngine &Diags) {
206     m_passthrough->FlushDiagnostics(Diags);
207   }
208
209   DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
210     return new ClangDiagnosticManagerAdapter(m_passthrough);
211   }
212
213   clang::TextDiagnosticBuffer *GetPassthrough() { return m_passthrough.get(); }
214
215 private:
216   DiagnosticManager *m_manager = nullptr;
217   std::shared_ptr<clang::TextDiagnosticBuffer> m_passthrough;
218 };
219
220 //===----------------------------------------------------------------------===//
221 // Implementation of ClangExpressionParser
222 //===----------------------------------------------------------------------===//
223
224 ClangExpressionParser::ClangExpressionParser(ExecutionContextScope *exe_scope,
225                                              Expression &expr,
226                                              bool generate_debug_info)
227     : ExpressionParser(exe_scope, expr, generate_debug_info), m_compiler(),
228       m_code_generator(), m_pp_callbacks(nullptr) {
229   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
230
231   // We can't compile expressions without a target.  So if the exe_scope is null
232   // or doesn't have a target,
233   // then we just need to get out of here.  I'll lldb_assert and not make any of
234   // the compiler objects since
235   // I can't return errors directly from the constructor.  Further calls will
236   // check if the compiler was made and
237   // bag out if it wasn't.
238
239   if (!exe_scope) {
240     lldb_assert(exe_scope, "Can't make an expression parser with a null scope.",
241                 __FUNCTION__, __FILE__, __LINE__);
242     return;
243   }
244
245   lldb::TargetSP target_sp;
246   target_sp = exe_scope->CalculateTarget();
247   if (!target_sp) {
248     lldb_assert(target_sp.get(),
249                 "Can't make an expression parser with a null target.",
250                 __FUNCTION__, __FILE__, __LINE__);
251     return;
252   }
253
254   // 1. Create a new compiler instance.
255   m_compiler.reset(new CompilerInstance());
256   lldb::LanguageType frame_lang =
257       expr.Language(); // defaults to lldb::eLanguageTypeUnknown
258   bool overridden_target_opts = false;
259   lldb_private::LanguageRuntime *lang_rt = nullptr;
260
261   std::string abi;
262   ArchSpec target_arch;
263   target_arch = target_sp->GetArchitecture();
264
265   const auto target_machine = target_arch.GetMachine();
266
267   // If the expression is being evaluated in the context of an existing
268   // stack frame, we introspect to see if the language runtime is available.
269
270   lldb::StackFrameSP frame_sp = exe_scope->CalculateStackFrame();
271   lldb::ProcessSP process_sp = exe_scope->CalculateProcess();
272
273   // Make sure the user hasn't provided a preferred execution language
274   // with `expression --language X -- ...`
275   if (frame_sp && frame_lang == lldb::eLanguageTypeUnknown)
276     frame_lang = frame_sp->GetLanguage();
277
278   if (process_sp && frame_lang != lldb::eLanguageTypeUnknown) {
279     lang_rt = process_sp->GetLanguageRuntime(frame_lang);
280     if (log)
281       log->Printf("Frame has language of type %s",
282                   Language::GetNameForLanguageType(frame_lang));
283   }
284
285   // 2. Configure the compiler with a set of default options that are
286   // appropriate
287   // for most situations.
288   if (target_arch.IsValid()) {
289     std::string triple = target_arch.GetTriple().str();
290     m_compiler->getTargetOpts().Triple = triple;
291     if (log)
292       log->Printf("Using %s as the target triple",
293                   m_compiler->getTargetOpts().Triple.c_str());
294   } else {
295     // If we get here we don't have a valid target and just have to guess.
296     // Sometimes this will be ok to just use the host target triple (when we
297     // evaluate say "2+3", but other
298     // expressions like breakpoint conditions and other things that _are_ target
299     // specific really shouldn't just be
300     // using the host triple. In such a case the language runtime should expose
301     // an overridden options set (3),
302     // below.
303     m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
304     if (log)
305       log->Printf("Using default target triple of %s",
306                   m_compiler->getTargetOpts().Triple.c_str());
307   }
308   // Now add some special fixes for known architectures:
309   // Any arm32 iOS environment, but not on arm64
310   if (m_compiler->getTargetOpts().Triple.find("arm64") == std::string::npos &&
311       m_compiler->getTargetOpts().Triple.find("arm") != std::string::npos &&
312       m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos) {
313     m_compiler->getTargetOpts().ABI = "apcs-gnu";
314   }
315   // Supported subsets of x86
316   if (target_machine == llvm::Triple::x86 ||
317       target_machine == llvm::Triple::x86_64) {
318     m_compiler->getTargetOpts().Features.push_back("+sse");
319     m_compiler->getTargetOpts().Features.push_back("+sse2");
320   }
321
322   // Set the target CPU to generate code for.
323   // This will be empty for any CPU that doesn't really need to make a special
324   // CPU string.
325   m_compiler->getTargetOpts().CPU = target_arch.GetClangTargetCPU();
326
327   // Set the target ABI
328   abi = GetClangTargetABI(target_arch);
329   if (!abi.empty())
330     m_compiler->getTargetOpts().ABI = abi;
331
332   // 3. Now allow the runtime to provide custom configuration options for the
333   // target.
334   // In this case, a specialized language runtime is available and we can query
335   // it for extra options.
336   // For 99% of use cases, this will not be needed and should be provided when
337   // basic platform detection is not enough.
338   if (lang_rt)
339     overridden_target_opts =
340         lang_rt->GetOverrideExprOptions(m_compiler->getTargetOpts());
341
342   if (overridden_target_opts)
343     if (log) {
344       log->Debug(
345           "Using overridden target options for the expression evaluation");
346
347       auto opts = m_compiler->getTargetOpts();
348       log->Debug("Triple: '%s'", opts.Triple.c_str());
349       log->Debug("CPU: '%s'", opts.CPU.c_str());
350       log->Debug("FPMath: '%s'", opts.FPMath.c_str());
351       log->Debug("ABI: '%s'", opts.ABI.c_str());
352       log->Debug("LinkerVersion: '%s'", opts.LinkerVersion.c_str());
353       StringList::LogDump(log, opts.FeaturesAsWritten, "FeaturesAsWritten");
354       StringList::LogDump(log, opts.Features, "Features");
355       StringList::LogDump(log, opts.Reciprocals, "Reciprocals");
356     }
357
358   // 4. Create and install the target on the compiler.
359   m_compiler->createDiagnostics();
360   auto target_info = TargetInfo::CreateTargetInfo(
361       m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts);
362   if (log) {
363     log->Printf("Using SIMD alignment: %d", target_info->getSimdDefaultAlign());
364     log->Printf("Target datalayout string: '%s'",
365                 target_info->getDataLayout().getStringRepresentation().c_str());
366     log->Printf("Target ABI: '%s'", target_info->getABI().str().c_str());
367     log->Printf("Target vector alignment: %d",
368                 target_info->getMaxVectorAlign());
369   }
370   m_compiler->setTarget(target_info);
371
372   assert(m_compiler->hasTarget());
373
374   // 5. Set language options.
375   lldb::LanguageType language = expr.Language();
376
377   switch (language) {
378   case lldb::eLanguageTypeC:
379   case lldb::eLanguageTypeC89:
380   case lldb::eLanguageTypeC99:
381   case lldb::eLanguageTypeC11:
382     // FIXME: the following language option is a temporary workaround,
383     // to "ask for C, get C++."
384     // For now, the expression parser must use C++ anytime the
385     // language is a C family language, because the expression parser
386     // uses features of C++ to capture values.
387     m_compiler->getLangOpts().CPlusPlus = true;
388     break;
389   case lldb::eLanguageTypeObjC:
390     m_compiler->getLangOpts().ObjC1 = true;
391     m_compiler->getLangOpts().ObjC2 = true;
392     // FIXME: the following language option is a temporary workaround,
393     // to "ask for ObjC, get ObjC++" (see comment above).
394     m_compiler->getLangOpts().CPlusPlus = true;
395     break;
396   case lldb::eLanguageTypeC_plus_plus:
397   case lldb::eLanguageTypeC_plus_plus_11:
398   case lldb::eLanguageTypeC_plus_plus_14:
399     m_compiler->getLangOpts().CPlusPlus11 = true;
400     m_compiler->getHeaderSearchOpts().UseLibcxx = true;
401     LLVM_FALLTHROUGH;
402   case lldb::eLanguageTypeC_plus_plus_03:
403     m_compiler->getLangOpts().CPlusPlus = true;
404     // FIXME: the following language option is a temporary workaround,
405     // to "ask for C++, get ObjC++".  Apple hopes to remove this requirement
406     // on non-Apple platforms, but for now it is needed.
407     m_compiler->getLangOpts().ObjC1 = true;
408     break;
409   case lldb::eLanguageTypeObjC_plus_plus:
410   case lldb::eLanguageTypeUnknown:
411   default:
412     m_compiler->getLangOpts().ObjC1 = true;
413     m_compiler->getLangOpts().ObjC2 = true;
414     m_compiler->getLangOpts().CPlusPlus = true;
415     m_compiler->getLangOpts().CPlusPlus11 = true;
416     m_compiler->getHeaderSearchOpts().UseLibcxx = true;
417     break;
418   }
419
420   m_compiler->getLangOpts().Bool = true;
421   m_compiler->getLangOpts().WChar = true;
422   m_compiler->getLangOpts().Blocks = true;
423   m_compiler->getLangOpts().DebuggerSupport =
424       true; // Features specifically for debugger clients
425   if (expr.DesiredResultType() == Expression::eResultTypeId)
426     m_compiler->getLangOpts().DebuggerCastResultToId = true;
427
428   m_compiler->getLangOpts().CharIsSigned =
429       ArchSpec(m_compiler->getTargetOpts().Triple.c_str())
430           .CharIsSignedByDefault();
431
432   // Spell checking is a nice feature, but it ends up completing a
433   // lot of types that we didn't strictly speaking need to complete.
434   // As a result, we spend a long time parsing and importing debug
435   // information.
436   m_compiler->getLangOpts().SpellChecking = false;
437
438   if (process_sp && m_compiler->getLangOpts().ObjC1) {
439     if (process_sp->GetObjCLanguageRuntime()) {
440       if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() ==
441           ObjCLanguageRuntime::ObjCRuntimeVersions::eAppleObjC_V2)
442         m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX,
443                                                   VersionTuple(10, 7));
444       else
445         m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX,
446                                                   VersionTuple(10, 7));
447
448       if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
449         m_compiler->getLangOpts().DebuggerObjCLiteral = true;
450     }
451   }
452
453   m_compiler->getLangOpts().ThreadsafeStatics = false;
454   m_compiler->getLangOpts().AccessControl =
455       false; // Debuggers get universal access
456   m_compiler->getLangOpts().DollarIdents =
457       true; // $ indicates a persistent variable name
458
459   // Set CodeGen options
460   m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
461   m_compiler->getCodeGenOpts().InstrumentFunctions = false;
462   m_compiler->getCodeGenOpts().DisableFPElim = true;
463   m_compiler->getCodeGenOpts().OmitLeafFramePointer = false;
464   if (generate_debug_info)
465     m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::FullDebugInfo);
466   else
467     m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::NoDebugInfo);
468
469   // Disable some warnings.
470   m_compiler->getDiagnostics().setSeverityForGroup(
471       clang::diag::Flavor::WarningOrError, "unused-value",
472       clang::diag::Severity::Ignored, SourceLocation());
473   m_compiler->getDiagnostics().setSeverityForGroup(
474       clang::diag::Flavor::WarningOrError, "odr",
475       clang::diag::Severity::Ignored, SourceLocation());
476
477   // Inform the target of the language options
478   //
479   // FIXME: We shouldn't need to do this, the target should be immutable once
480   // created. This complexity should be lifted elsewhere.
481   m_compiler->getTarget().adjust(m_compiler->getLangOpts());
482
483   // 6. Set up the diagnostic buffer for reporting errors
484
485   m_compiler->getDiagnostics().setClient(new ClangDiagnosticManagerAdapter);
486
487   // 7. Set up the source management objects inside the compiler
488
489   clang::FileSystemOptions file_system_options;
490   m_file_manager.reset(new clang::FileManager(file_system_options));
491
492   if (!m_compiler->hasSourceManager())
493     m_compiler->createSourceManager(*m_file_manager.get());
494
495   m_compiler->createFileManager();
496   m_compiler->createPreprocessor(TU_Complete);
497
498   if (ClangModulesDeclVendor *decl_vendor =
499           target_sp->GetClangModulesDeclVendor()) {
500     ClangPersistentVariables *clang_persistent_vars =
501         llvm::cast<ClangPersistentVariables>(
502             target_sp->GetPersistentExpressionStateForLanguage(
503                 lldb::eLanguageTypeC));
504     std::unique_ptr<PPCallbacks> pp_callbacks(
505         new LLDBPreprocessorCallbacks(*decl_vendor, *clang_persistent_vars));
506     m_pp_callbacks =
507         static_cast<LLDBPreprocessorCallbacks *>(pp_callbacks.get());
508     m_compiler->getPreprocessor().addPPCallbacks(std::move(pp_callbacks));
509   }
510
511   // 8. Most of this we get from the CompilerInstance, but we
512   // also want to give the context an ExternalASTSource.
513   m_selector_table.reset(new SelectorTable());
514   m_builtin_context.reset(new Builtin::Context());
515
516   std::unique_ptr<clang::ASTContext> ast_context(
517       new ASTContext(m_compiler->getLangOpts(), m_compiler->getSourceManager(),
518                      m_compiler->getPreprocessor().getIdentifierTable(),
519                      *m_selector_table.get(), *m_builtin_context.get()));
520
521   ast_context->InitBuiltinTypes(m_compiler->getTarget());
522
523   ClangExpressionHelper *type_system_helper =
524       dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
525   ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap();
526
527   if (decl_map) {
528     llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source(
529         decl_map->CreateProxy());
530     decl_map->InstallASTContext(ast_context.get());
531     ast_context->setExternalSource(ast_source);
532   }
533
534   m_ast_context.reset(
535       new ClangASTContext(m_compiler->getTargetOpts().Triple.c_str()));
536   m_ast_context->setASTContext(ast_context.get());
537   m_compiler->setASTContext(ast_context.release());
538
539   std::string module_name("$__lldb_module");
540
541   m_llvm_context.reset(new LLVMContext());
542   m_code_generator.reset(CreateLLVMCodeGen(
543       m_compiler->getDiagnostics(), module_name,
544       m_compiler->getHeaderSearchOpts(), m_compiler->getPreprocessorOpts(),
545       m_compiler->getCodeGenOpts(), *m_llvm_context));
546 }
547
548 ClangExpressionParser::~ClangExpressionParser() {}
549
550 unsigned ClangExpressionParser::Parse(DiagnosticManager &diagnostic_manager) {
551   ClangDiagnosticManagerAdapter *adapter =
552       static_cast<ClangDiagnosticManagerAdapter *>(
553           m_compiler->getDiagnostics().getClient());
554   clang::TextDiagnosticBuffer *diag_buf = adapter->GetPassthrough();
555   diag_buf->FlushDiagnostics(m_compiler->getDiagnostics());
556
557   adapter->ResetManager(&diagnostic_manager);
558
559   const char *expr_text = m_expr.Text();
560
561   clang::SourceManager &source_mgr = m_compiler->getSourceManager();
562   bool created_main_file = false;
563   if (m_compiler->getCodeGenOpts().getDebugInfo() ==
564       codegenoptions::FullDebugInfo) {
565     int temp_fd = -1;
566     llvm::SmallString<PATH_MAX> result_path;
567     FileSpec tmpdir_file_spec;
568     if (HostInfo::GetLLDBPath(lldb::ePathTypeLLDBTempSystemDir,
569                               tmpdir_file_spec)) {
570       tmpdir_file_spec.AppendPathComponent("lldb-%%%%%%.expr");
571       std::string temp_source_path = tmpdir_file_spec.GetPath();
572       llvm::sys::fs::createUniqueFile(temp_source_path, temp_fd, result_path);
573     } else {
574       llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path);
575     }
576
577     if (temp_fd != -1) {
578       lldb_private::File file(temp_fd, true);
579       const size_t expr_text_len = strlen(expr_text);
580       size_t bytes_written = expr_text_len;
581       if (file.Write(expr_text, bytes_written).Success()) {
582         if (bytes_written == expr_text_len) {
583           file.Close();
584           source_mgr.setMainFileID(
585               source_mgr.createFileID(m_file_manager->getFile(result_path),
586                                       SourceLocation(), SrcMgr::C_User));
587           created_main_file = true;
588         }
589       }
590     }
591   }
592
593   if (!created_main_file) {
594     std::unique_ptr<MemoryBuffer> memory_buffer =
595         MemoryBuffer::getMemBufferCopy(expr_text, __FUNCTION__);
596     source_mgr.setMainFileID(source_mgr.createFileID(std::move(memory_buffer)));
597   }
598
599   diag_buf->BeginSourceFile(m_compiler->getLangOpts(),
600                             &m_compiler->getPreprocessor());
601
602   ClangExpressionHelper *type_system_helper =
603       dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
604
605   ASTConsumer *ast_transformer =
606       type_system_helper->ASTTransformer(m_code_generator.get());
607
608   if (ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap())
609     decl_map->InstallCodeGenerator(m_code_generator.get());
610
611   if (ast_transformer) {
612     ast_transformer->Initialize(m_compiler->getASTContext());
613     ParseAST(m_compiler->getPreprocessor(), ast_transformer,
614              m_compiler->getASTContext());
615   } else {
616     m_code_generator->Initialize(m_compiler->getASTContext());
617     ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(),
618              m_compiler->getASTContext());
619   }
620
621   diag_buf->EndSourceFile();
622
623   unsigned num_errors = diag_buf->getNumErrors();
624
625   if (m_pp_callbacks && m_pp_callbacks->hasErrors()) {
626     num_errors++;
627     diagnostic_manager.PutString(eDiagnosticSeverityError,
628                                  "while importing modules:");
629     diagnostic_manager.AppendMessageToDiagnostic(
630         m_pp_callbacks->getErrorString());
631   }
632
633   if (!num_errors) {
634     if (type_system_helper->DeclMap() &&
635         !type_system_helper->DeclMap()->ResolveUnknownTypes()) {
636       diagnostic_manager.Printf(eDiagnosticSeverityError,
637                                 "Couldn't infer the type of a variable");
638       num_errors++;
639     }
640   }
641
642   if (!num_errors) {
643     type_system_helper->CommitPersistentDecls();
644   }
645
646   adapter->ResetManager();
647
648   return num_errors;
649 }
650
651 std::string
652 ClangExpressionParser::GetClangTargetABI(const ArchSpec &target_arch) {
653   std::string abi;
654
655   if (target_arch.IsMIPS()) {
656     switch (target_arch.GetFlags() & ArchSpec::eMIPSABI_mask) {
657     case ArchSpec::eMIPSABI_N64:
658       abi = "n64";
659       break;
660     case ArchSpec::eMIPSABI_N32:
661       abi = "n32";
662       break;
663     case ArchSpec::eMIPSABI_O32:
664       abi = "o32";
665       break;
666     default:
667       break;
668     }
669   }
670   return abi;
671 }
672
673 bool ClangExpressionParser::RewriteExpression(
674     DiagnosticManager &diagnostic_manager) {
675   clang::SourceManager &source_manager = m_compiler->getSourceManager();
676   clang::edit::EditedSource editor(source_manager, m_compiler->getLangOpts(),
677                                    nullptr);
678   clang::edit::Commit commit(editor);
679   clang::Rewriter rewriter(source_manager, m_compiler->getLangOpts());
680
681   class RewritesReceiver : public edit::EditsReceiver {
682     Rewriter &rewrite;
683
684   public:
685     RewritesReceiver(Rewriter &in_rewrite) : rewrite(in_rewrite) {}
686
687     void insert(SourceLocation loc, StringRef text) override {
688       rewrite.InsertText(loc, text);
689     }
690     void replace(CharSourceRange range, StringRef text) override {
691       rewrite.ReplaceText(range.getBegin(), rewrite.getRangeSize(range), text);
692     }
693   };
694
695   RewritesReceiver rewrites_receiver(rewriter);
696
697   const DiagnosticList &diagnostics = diagnostic_manager.Diagnostics();
698   size_t num_diags = diagnostics.size();
699   if (num_diags == 0)
700     return false;
701
702   for (const Diagnostic *diag : diagnostic_manager.Diagnostics()) {
703     const ClangDiagnostic *diagnostic = llvm::dyn_cast<ClangDiagnostic>(diag);
704     if (diagnostic && diagnostic->HasFixIts()) {
705       for (const FixItHint &fixit : diagnostic->FixIts()) {
706         // This is cobbed from clang::Rewrite::FixItRewriter.
707         if (fixit.CodeToInsert.empty()) {
708           if (fixit.InsertFromRange.isValid()) {
709             commit.insertFromRange(fixit.RemoveRange.getBegin(),
710                                    fixit.InsertFromRange, /*afterToken=*/false,
711                                    fixit.BeforePreviousInsertions);
712           } else
713             commit.remove(fixit.RemoveRange);
714         } else {
715           if (fixit.RemoveRange.isTokenRange() ||
716               fixit.RemoveRange.getBegin() != fixit.RemoveRange.getEnd())
717             commit.replace(fixit.RemoveRange, fixit.CodeToInsert);
718           else
719             commit.insert(fixit.RemoveRange.getBegin(), fixit.CodeToInsert,
720                           /*afterToken=*/false, fixit.BeforePreviousInsertions);
721         }
722       }
723     }
724   }
725
726   // FIXME - do we want to try to propagate specific errors here?
727   if (!commit.isCommitable())
728     return false;
729   else if (!editor.commit(commit))
730     return false;
731
732   // Now play all the edits, and stash the result in the diagnostic manager.
733   editor.applyRewrites(rewrites_receiver);
734   RewriteBuffer &main_file_buffer =
735       rewriter.getEditBuffer(source_manager.getMainFileID());
736
737   std::string fixed_expression;
738   llvm::raw_string_ostream out_stream(fixed_expression);
739
740   main_file_buffer.write(out_stream);
741   out_stream.flush();
742   diagnostic_manager.SetFixedExpression(fixed_expression);
743
744   return true;
745 }
746
747 static bool FindFunctionInModule(ConstString &mangled_name,
748                                  llvm::Module *module, const char *orig_name) {
749   for (const auto &func : module->getFunctionList()) {
750     const StringRef &name = func.getName();
751     if (name.find(orig_name) != StringRef::npos) {
752       mangled_name.SetString(name);
753       return true;
754     }
755   }
756
757   return false;
758 }
759
760 lldb_private::Error ClangExpressionParser::PrepareForExecution(
761     lldb::addr_t &func_addr, lldb::addr_t &func_end,
762     lldb::IRExecutionUnitSP &execution_unit_sp, ExecutionContext &exe_ctx,
763     bool &can_interpret, ExecutionPolicy execution_policy) {
764   func_addr = LLDB_INVALID_ADDRESS;
765   func_end = LLDB_INVALID_ADDRESS;
766   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
767
768   lldb_private::Error err;
769
770   std::unique_ptr<llvm::Module> llvm_module_ap(
771       m_code_generator->ReleaseModule());
772
773   if (!llvm_module_ap.get()) {
774     err.SetErrorToGenericError();
775     err.SetErrorString("IR doesn't contain a module");
776     return err;
777   }
778
779   ConstString function_name;
780
781   if (execution_policy != eExecutionPolicyTopLevel) {
782     // Find the actual name of the function (it's often mangled somehow)
783
784     if (!FindFunctionInModule(function_name, llvm_module_ap.get(),
785                               m_expr.FunctionName())) {
786       err.SetErrorToGenericError();
787       err.SetErrorStringWithFormat("Couldn't find %s() in the module",
788                                    m_expr.FunctionName());
789       return err;
790     } else {
791       if (log)
792         log->Printf("Found function %s for %s", function_name.AsCString(),
793                     m_expr.FunctionName());
794     }
795   }
796
797   SymbolContext sc;
798
799   if (lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP()) {
800     sc = frame_sp->GetSymbolContext(lldb::eSymbolContextEverything);
801   } else if (lldb::TargetSP target_sp = exe_ctx.GetTargetSP()) {
802     sc.target_sp = target_sp;
803   }
804
805   LLVMUserExpression::IRPasses custom_passes;
806   {
807     auto lang = m_expr.Language();
808     if (log)
809       log->Printf("%s - Currrent expression language is %s\n", __FUNCTION__,
810                   Language::GetNameForLanguageType(lang));
811     lldb::ProcessSP process_sp = exe_ctx.GetProcessSP();
812     if (process_sp && lang != lldb::eLanguageTypeUnknown) {
813       auto runtime = process_sp->GetLanguageRuntime(lang);
814       if (runtime)
815         runtime->GetIRPasses(custom_passes);
816     }
817   }
818
819   if (custom_passes.EarlyPasses) {
820     if (log)
821       log->Printf("%s - Running Early IR Passes from LanguageRuntime on "
822                   "expression module '%s'",
823                   __FUNCTION__, m_expr.FunctionName());
824
825     custom_passes.EarlyPasses->run(*llvm_module_ap);
826   }
827
828   execution_unit_sp.reset(
829       new IRExecutionUnit(m_llvm_context, // handed off here
830                           llvm_module_ap, // handed off here
831                           function_name, exe_ctx.GetTargetSP(), sc,
832                           m_compiler->getTargetOpts().Features));
833
834   ClangExpressionHelper *type_system_helper =
835       dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
836   ClangExpressionDeclMap *decl_map =
837       type_system_helper->DeclMap(); // result can be NULL
838
839   if (decl_map) {
840     Stream *error_stream = NULL;
841     Target *target = exe_ctx.GetTargetPtr();
842     error_stream = target->GetDebugger().GetErrorFile().get();
843
844     IRForTarget ir_for_target(decl_map, m_expr.NeedsVariableResolution(),
845                               *execution_unit_sp, *error_stream,
846                               function_name.AsCString());
847
848     bool ir_can_run =
849         ir_for_target.runOnModule(*execution_unit_sp->GetModule());
850
851     if (!ir_can_run) {
852       err.SetErrorString(
853           "The expression could not be prepared to run in the target");
854       return err;
855     }
856
857     Process *process = exe_ctx.GetProcessPtr();
858
859     if (execution_policy != eExecutionPolicyAlways &&
860         execution_policy != eExecutionPolicyTopLevel) {
861       lldb_private::Error interpret_error;
862
863       bool interpret_function_calls =
864           !process ? false : process->CanInterpretFunctionCalls();
865       can_interpret = IRInterpreter::CanInterpret(
866           *execution_unit_sp->GetModule(), *execution_unit_sp->GetFunction(),
867           interpret_error, interpret_function_calls);
868
869       if (!can_interpret && execution_policy == eExecutionPolicyNever) {
870         err.SetErrorStringWithFormat("Can't run the expression locally: %s",
871                                      interpret_error.AsCString());
872         return err;
873       }
874     }
875
876     if (!process && execution_policy == eExecutionPolicyAlways) {
877       err.SetErrorString("Expression needed to run in the target, but the "
878                          "target can't be run");
879       return err;
880     }
881
882     if (!process && execution_policy == eExecutionPolicyTopLevel) {
883       err.SetErrorString("Top-level code needs to be inserted into a runnable "
884                          "target, but the target can't be run");
885       return err;
886     }
887
888     if (execution_policy == eExecutionPolicyAlways ||
889         (execution_policy != eExecutionPolicyTopLevel && !can_interpret)) {
890       if (m_expr.NeedsValidation() && process) {
891         if (!process->GetDynamicCheckers()) {
892           DynamicCheckerFunctions *dynamic_checkers =
893               new DynamicCheckerFunctions();
894
895           DiagnosticManager install_diagnostics;
896
897           if (!dynamic_checkers->Install(install_diagnostics, exe_ctx)) {
898             if (install_diagnostics.Diagnostics().size())
899               err.SetErrorString("couldn't install checkers, unknown error");
900             else
901               err.SetErrorString(install_diagnostics.GetString().c_str());
902
903             return err;
904           }
905
906           process->SetDynamicCheckers(dynamic_checkers);
907
908           if (log)
909             log->Printf("== [ClangUserExpression::Evaluate] Finished "
910                         "installing dynamic checkers ==");
911         }
912
913         IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(),
914                                           function_name.AsCString());
915
916         llvm::Module *module = execution_unit_sp->GetModule();
917         if (!module || !ir_dynamic_checks.runOnModule(*module)) {
918           err.SetErrorToGenericError();
919           err.SetErrorString("Couldn't add dynamic checks to the expression");
920           return err;
921         }
922
923         if (custom_passes.LatePasses) {
924           if (log)
925             log->Printf("%s - Running Late IR Passes from LanguageRuntime on "
926                         "expression module '%s'",
927                         __FUNCTION__, m_expr.FunctionName());
928
929           custom_passes.LatePasses->run(*module);
930         }
931       }
932     }
933
934     if (execution_policy == eExecutionPolicyAlways ||
935         execution_policy == eExecutionPolicyTopLevel || !can_interpret) {
936       execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
937     }
938   } else {
939     execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
940   }
941
942   return err;
943 }
944
945 lldb_private::Error ClangExpressionParser::RunStaticInitializers(
946     lldb::IRExecutionUnitSP &execution_unit_sp, ExecutionContext &exe_ctx) {
947   lldb_private::Error err;
948
949   lldbassert(execution_unit_sp.get());
950   lldbassert(exe_ctx.HasThreadScope());
951
952   if (!execution_unit_sp.get()) {
953     err.SetErrorString(
954         "can't run static initializers for a NULL execution unit");
955     return err;
956   }
957
958   if (!exe_ctx.HasThreadScope()) {
959     err.SetErrorString("can't run static initializers without a thread");
960     return err;
961   }
962
963   std::vector<lldb::addr_t> static_initializers;
964
965   execution_unit_sp->GetStaticInitializers(static_initializers);
966
967   for (lldb::addr_t static_initializer : static_initializers) {
968     EvaluateExpressionOptions options;
969
970     lldb::ThreadPlanSP call_static_initializer(new ThreadPlanCallFunction(
971         exe_ctx.GetThreadRef(), Address(static_initializer), CompilerType(),
972         llvm::ArrayRef<lldb::addr_t>(), options));
973
974     DiagnosticManager execution_errors;
975     lldb::ExpressionResults results =
976         exe_ctx.GetThreadRef().GetProcess()->RunThreadPlan(
977             exe_ctx, call_static_initializer, options, execution_errors);
978
979     if (results != lldb::eExpressionCompleted) {
980       err.SetErrorStringWithFormat("couldn't run static initializer: %s",
981                                    execution_errors.GetString().c_str());
982       return err;
983     }
984   }
985
986   return err;
987 }