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