1 //===-- ClangExpressionParser.cpp -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
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"
46 #pragma clang diagnostic push
47 #pragma clang diagnostic ignored "-Wglobal-constructors"
48 #include "llvm/ExecutionEngine/MCJIT.h"
49 #pragma clang diagnostic pop
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"
60 #include "ClangDiagnostic.h"
61 #include "ClangExpressionParser.h"
63 #include "ClangASTSource.h"
64 #include "ClangExpressionDeclMap.h"
65 #include "ClangExpressionHelper.h"
66 #include "ClangModulesDeclVendor.h"
67 #include "ClangPersistentVariables.h"
68 #include "IRForTarget.h"
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"
95 using namespace clang;
97 using namespace lldb_private;
99 //===----------------------------------------------------------------------===//
100 // Utility Methods for Clang
101 //===----------------------------------------------------------------------===//
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;
110 LLDBPreprocessorCallbacks(ClangModulesDeclVendor &decl_vendor,
111 ClangPersistentVariables &persistent_vars)
112 : m_decl_vendor(decl_vendor), m_persistent_vars(persistent_vars) {}
114 void moduleImport(SourceLocation import_location, clang::ModuleIdPath path,
115 const clang::Module * /*null*/) override {
116 std::vector<ConstString> string_path;
118 for (const std::pair<IdentifierInfo *, SourceLocation> &component : path) {
119 string_path.push_back(ConstString(component.first->getName()));
122 StreamString error_stream;
124 ClangModulesDeclVendor::ModuleVector exported_modules;
126 if (!m_decl_vendor.AddModule(string_path, &exported_modules,
131 for (ClangModulesDeclVendor::ModuleID module : exported_modules) {
132 m_persistent_vars.AddHandLoadedClangModule(module);
136 bool hasErrors() { return m_has_errors; }
138 llvm::StringRef getErrorString() { return m_error_stream.GetString(); }
141 class ClangDiagnosticManagerAdapter : public clang::DiagnosticConsumer {
143 ClangDiagnosticManagerAdapter()
144 : m_passthrough(new clang::TextDiagnosticBuffer) {}
146 ClangDiagnosticManagerAdapter(
147 const std::shared_ptr<clang::TextDiagnosticBuffer> &passthrough)
148 : m_passthrough(passthrough) {}
150 void ResetManager(DiagnosticManager *manager = nullptr) {
154 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
155 const clang::Diagnostic &Info) {
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();
162 lldb_private::DiagnosticSeverity severity;
163 bool make_new_diagnostic = true;
166 case DiagnosticsEngine::Level::Fatal:
167 case DiagnosticsEngine::Level::Error:
168 severity = eDiagnosticSeverityError;
170 case DiagnosticsEngine::Level::Warning:
171 severity = eDiagnosticSeverityWarning;
173 case DiagnosticsEngine::Level::Remark:
174 case DiagnosticsEngine::Level::Ignored:
175 severity = eDiagnosticSeverityRemark;
177 case DiagnosticsEngine::Level::Note:
178 m_manager->AppendMessageToDiagnostic(data);
179 make_new_diagnostic = false;
181 if (make_new_diagnostic) {
182 ClangDiagnostic *new_diagnostic =
183 new ClangDiagnostic(data, severity, Info.getID());
184 m_manager->AddDiagnostic(new_diagnostic);
186 // Don't store away warning fixits, since the compiler doesn't have
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);
196 new_diagnostic->AddFixitHint(fixit);
202 m_passthrough->HandleDiagnostic(DiagLevel, Info);
205 void FlushDiagnostics(DiagnosticsEngine &Diags) {
206 m_passthrough->FlushDiagnostics(Diags);
209 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
210 return new ClangDiagnosticManagerAdapter(m_passthrough);
213 clang::TextDiagnosticBuffer *GetPassthrough() { return m_passthrough.get(); }
216 DiagnosticManager *m_manager = nullptr;
217 std::shared_ptr<clang::TextDiagnosticBuffer> m_passthrough;
220 //===----------------------------------------------------------------------===//
221 // Implementation of ClangExpressionParser
222 //===----------------------------------------------------------------------===//
224 ClangExpressionParser::ClangExpressionParser(ExecutionContextScope *exe_scope,
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));
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.
240 lldb_assert(exe_scope, "Can't make an expression parser with a null scope.",
241 __FUNCTION__, __FILE__, __LINE__);
245 lldb::TargetSP target_sp;
246 target_sp = exe_scope->CalculateTarget();
248 lldb_assert(target_sp.get(),
249 "Can't make an expression parser with a null target.",
250 __FUNCTION__, __FILE__, __LINE__);
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;
262 ArchSpec target_arch;
263 target_arch = target_sp->GetArchitecture();
265 const auto target_machine = target_arch.GetMachine();
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.
270 lldb::StackFrameSP frame_sp = exe_scope->CalculateStackFrame();
271 lldb::ProcessSP process_sp = exe_scope->CalculateProcess();
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();
278 if (process_sp && frame_lang != lldb::eLanguageTypeUnknown) {
279 lang_rt = process_sp->GetLanguageRuntime(frame_lang);
281 log->Printf("Frame has language of type %s",
282 Language::GetNameForLanguageType(frame_lang));
285 // 2. Configure the compiler with a set of default options that are
287 // for most situations.
288 if (target_arch.IsValid()) {
289 std::string triple = target_arch.GetTriple().str();
290 m_compiler->getTargetOpts().Triple = triple;
292 log->Printf("Using %s as the target triple",
293 m_compiler->getTargetOpts().Triple.c_str());
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),
303 m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
305 log->Printf("Using default target triple of %s",
306 m_compiler->getTargetOpts().Triple.c_str());
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";
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");
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
325 m_compiler->getTargetOpts().CPU = target_arch.GetClangTargetCPU();
327 // Set the target ABI
328 abi = GetClangTargetABI(target_arch);
330 m_compiler->getTargetOpts().ABI = abi;
332 // 3. Now allow the runtime to provide custom configuration options for the
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.
339 overridden_target_opts =
340 lang_rt->GetOverrideExprOptions(m_compiler->getTargetOpts());
342 if (overridden_target_opts)
345 "Using overridden target options for the expression evaluation");
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");
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);
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());
370 m_compiler->setTarget(target_info);
372 assert(m_compiler->hasTarget());
374 // 5. Set language options.
375 lldb::LanguageType language = expr.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;
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;
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;
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;
409 case lldb::eLanguageTypeObjC_plus_plus:
410 case lldb::eLanguageTypeUnknown:
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;
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;
428 m_compiler->getLangOpts().CharIsSigned =
429 ArchSpec(m_compiler->getTargetOpts().Triple.c_str())
430 .CharIsSignedByDefault();
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
436 m_compiler->getLangOpts().SpellChecking = false;
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));
445 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX,
446 VersionTuple(10, 7));
448 if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
449 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
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
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);
467 m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::NoDebugInfo);
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());
477 // Inform the target of the language options
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());
483 // 6. Set up the diagnostic buffer for reporting errors
485 m_compiler->getDiagnostics().setClient(new ClangDiagnosticManagerAdapter);
487 // 7. Set up the source management objects inside the compiler
489 clang::FileSystemOptions file_system_options;
490 m_file_manager.reset(new clang::FileManager(file_system_options));
492 if (!m_compiler->hasSourceManager())
493 m_compiler->createSourceManager(*m_file_manager.get());
495 m_compiler->createFileManager();
496 m_compiler->createPreprocessor(TU_Complete);
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));
507 static_cast<LLDBPreprocessorCallbacks *>(pp_callbacks.get());
508 m_compiler->getPreprocessor().addPPCallbacks(std::move(pp_callbacks));
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());
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()));
521 ast_context->InitBuiltinTypes(m_compiler->getTarget());
523 ClangExpressionHelper *type_system_helper =
524 dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
525 ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap();
528 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source(
529 decl_map->CreateProxy());
530 decl_map->InstallASTContext(ast_context.get());
531 ast_context->setExternalSource(ast_source);
535 new ClangASTContext(m_compiler->getTargetOpts().Triple.c_str()));
536 m_ast_context->setASTContext(ast_context.get());
537 m_compiler->setASTContext(ast_context.release());
539 std::string module_name("$__lldb_module");
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));
548 ClangExpressionParser::~ClangExpressionParser() {}
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());
557 adapter->ResetManager(&diagnostic_manager);
559 const char *expr_text = m_expr.Text();
561 clang::SourceManager &source_mgr = m_compiler->getSourceManager();
562 bool created_main_file = false;
563 if (m_compiler->getCodeGenOpts().getDebugInfo() ==
564 codegenoptions::FullDebugInfo) {
566 llvm::SmallString<PATH_MAX> result_path;
567 FileSpec tmpdir_file_spec;
568 if (HostInfo::GetLLDBPath(lldb::ePathTypeLLDBTempSystemDir,
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);
574 llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path);
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) {
584 source_mgr.setMainFileID(
585 source_mgr.createFileID(m_file_manager->getFile(result_path),
586 SourceLocation(), SrcMgr::C_User));
587 created_main_file = true;
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)));
599 diag_buf->BeginSourceFile(m_compiler->getLangOpts(),
600 &m_compiler->getPreprocessor());
602 ClangExpressionHelper *type_system_helper =
603 dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
605 ASTConsumer *ast_transformer =
606 type_system_helper->ASTTransformer(m_code_generator.get());
608 if (ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap())
609 decl_map->InstallCodeGenerator(m_code_generator.get());
611 if (ast_transformer) {
612 ast_transformer->Initialize(m_compiler->getASTContext());
613 ParseAST(m_compiler->getPreprocessor(), ast_transformer,
614 m_compiler->getASTContext());
616 m_code_generator->Initialize(m_compiler->getASTContext());
617 ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(),
618 m_compiler->getASTContext());
621 diag_buf->EndSourceFile();
623 unsigned num_errors = diag_buf->getNumErrors();
625 if (m_pp_callbacks && m_pp_callbacks->hasErrors()) {
627 diagnostic_manager.PutString(eDiagnosticSeverityError,
628 "while importing modules:");
629 diagnostic_manager.AppendMessageToDiagnostic(
630 m_pp_callbacks->getErrorString());
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");
643 type_system_helper->CommitPersistentDecls();
646 adapter->ResetManager();
652 ClangExpressionParser::GetClangTargetABI(const ArchSpec &target_arch) {
655 if (target_arch.IsMIPS()) {
656 switch (target_arch.GetFlags() & ArchSpec::eMIPSABI_mask) {
657 case ArchSpec::eMIPSABI_N64:
660 case ArchSpec::eMIPSABI_N32:
663 case ArchSpec::eMIPSABI_O32:
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(),
678 clang::edit::Commit commit(editor);
679 clang::Rewriter rewriter(source_manager, m_compiler->getLangOpts());
681 class RewritesReceiver : public edit::EditsReceiver {
685 RewritesReceiver(Rewriter &in_rewrite) : rewrite(in_rewrite) {}
687 void insert(SourceLocation loc, StringRef text) override {
688 rewrite.InsertText(loc, text);
690 void replace(CharSourceRange range, StringRef text) override {
691 rewrite.ReplaceText(range.getBegin(), rewrite.getRangeSize(range), text);
695 RewritesReceiver rewrites_receiver(rewriter);
697 const DiagnosticList &diagnostics = diagnostic_manager.Diagnostics();
698 size_t num_diags = diagnostics.size();
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);
713 commit.remove(fixit.RemoveRange);
715 if (fixit.RemoveRange.isTokenRange() ||
716 fixit.RemoveRange.getBegin() != fixit.RemoveRange.getEnd())
717 commit.replace(fixit.RemoveRange, fixit.CodeToInsert);
719 commit.insert(fixit.RemoveRange.getBegin(), fixit.CodeToInsert,
720 /*afterToken=*/false, fixit.BeforePreviousInsertions);
726 // FIXME - do we want to try to propagate specific errors here?
727 if (!commit.isCommitable())
729 else if (!editor.commit(commit))
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());
737 std::string fixed_expression;
738 llvm::raw_string_ostream out_stream(fixed_expression);
740 main_file_buffer.write(out_stream);
742 diagnostic_manager.SetFixedExpression(fixed_expression);
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);
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));
768 lldb_private::Error err;
770 std::unique_ptr<llvm::Module> llvm_module_ap(
771 m_code_generator->ReleaseModule());
773 if (!llvm_module_ap.get()) {
774 err.SetErrorToGenericError();
775 err.SetErrorString("IR doesn't contain a module");
779 ConstString function_name;
781 if (execution_policy != eExecutionPolicyTopLevel) {
782 // Find the actual name of the function (it's often mangled somehow)
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());
792 log->Printf("Found function %s for %s", function_name.AsCString(),
793 m_expr.FunctionName());
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;
805 LLVMUserExpression::IRPasses custom_passes;
807 auto lang = m_expr.Language();
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);
815 runtime->GetIRPasses(custom_passes);
819 if (custom_passes.EarlyPasses) {
821 log->Printf("%s - Running Early IR Passes from LanguageRuntime on "
822 "expression module '%s'",
823 __FUNCTION__, m_expr.FunctionName());
825 custom_passes.EarlyPasses->run(*llvm_module_ap);
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));
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
840 Stream *error_stream = NULL;
841 Target *target = exe_ctx.GetTargetPtr();
842 error_stream = target->GetDebugger().GetErrorFile().get();
844 IRForTarget ir_for_target(decl_map, m_expr.NeedsVariableResolution(),
845 *execution_unit_sp, *error_stream,
846 function_name.AsCString());
849 ir_for_target.runOnModule(*execution_unit_sp->GetModule());
853 "The expression could not be prepared to run in the target");
857 Process *process = exe_ctx.GetProcessPtr();
859 if (execution_policy != eExecutionPolicyAlways &&
860 execution_policy != eExecutionPolicyTopLevel) {
861 lldb_private::Error interpret_error;
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);
869 if (!can_interpret && execution_policy == eExecutionPolicyNever) {
870 err.SetErrorStringWithFormat("Can't run the expression locally: %s",
871 interpret_error.AsCString());
876 if (!process && execution_policy == eExecutionPolicyAlways) {
877 err.SetErrorString("Expression needed to run in the target, but the "
878 "target can't be run");
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");
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();
895 DiagnosticManager install_diagnostics;
897 if (!dynamic_checkers->Install(install_diagnostics, exe_ctx)) {
898 if (install_diagnostics.Diagnostics().size())
899 err.SetErrorString("couldn't install checkers, unknown error");
901 err.SetErrorString(install_diagnostics.GetString().c_str());
906 process->SetDynamicCheckers(dynamic_checkers);
909 log->Printf("== [ClangUserExpression::Evaluate] Finished "
910 "installing dynamic checkers ==");
913 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(),
914 function_name.AsCString());
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");
923 if (custom_passes.LatePasses) {
925 log->Printf("%s - Running Late IR Passes from LanguageRuntime on "
926 "expression module '%s'",
927 __FUNCTION__, m_expr.FunctionName());
929 custom_passes.LatePasses->run(*module);
934 if (execution_policy == eExecutionPolicyAlways ||
935 execution_policy == eExecutionPolicyTopLevel || !can_interpret) {
936 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
939 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
945 lldb_private::Error ClangExpressionParser::RunStaticInitializers(
946 lldb::IRExecutionUnitSP &execution_unit_sp, ExecutionContext &exe_ctx) {
947 lldb_private::Error err;
949 lldbassert(execution_unit_sp.get());
950 lldbassert(exe_ctx.HasThreadScope());
952 if (!execution_unit_sp.get()) {
954 "can't run static initializers for a NULL execution unit");
958 if (!exe_ctx.HasThreadScope()) {
959 err.SetErrorString("can't run static initializers without a thread");
963 std::vector<lldb::addr_t> static_initializers;
965 execution_unit_sp->GetStaticInitializers(static_initializers);
967 for (lldb::addr_t static_initializer : static_initializers) {
968 EvaluateExpressionOptions options;
970 lldb::ThreadPlanSP call_static_initializer(new ThreadPlanCallFunction(
971 exe_ctx.GetThreadRef(), Address(static_initializer), CompilerType(),
972 llvm::ArrayRef<lldb::addr_t>(), options));
974 DiagnosticManager execution_errors;
975 lldb::ExpressionResults results =
976 exe_ctx.GetThreadRef().GetProcess()->RunThreadPlan(
977 exe_ctx, call_static_initializer, options, execution_errors);
979 if (results != lldb::eExpressionCompleted) {
980 err.SetErrorStringWithFormat("couldn't run static initializer: %s",
981 execution_errors.GetString().c_str());