Skip to content

[lldb] Avoid spinnning up a SwiftASTContext just to determine the man… #10638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2632,47 +2632,6 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
return swift_ast_sp;
}

bool SwiftASTContext::CheckFlagInCU(CompileUnit *cu, const char *flag) {
AutoBool interop_enabled =
ModuleList::GetGlobalModuleListProperties().GetSwiftEnableCxxInterop();
switch (interop_enabled) {
case AutoBool::True:
return true;
case AutoBool::False:
return false;
case AutoBool::Auto: {
if (!cu)
return false;
lldb::ModuleSP module = cu->CalculateSymbolContextModule();
if (!module)
return false;
auto *sym_file = module->GetSymbolFile();
if (!sym_file)
return false;
auto options = sym_file->GetCompileOptions();
for (auto &[unit, args] : options) {
if (unit.get() == cu) {
if (cu->GetLanguage() == eLanguageTypeSwift)
for (const char *arg : args.GetArgumentArrayRef())
if (strcmp(arg, flag) == 0)
return true;
return false;
}
}
}
}
return false;
}

/// Determine whether this CU was compiled with C++ interop enabled.
bool SwiftASTContext::ShouldEnableCXXInterop(CompileUnit *cu) {
return CheckFlagInCU(cu, "-enable-experimental-cxx-interop");
}

bool SwiftASTContext::ShouldEnableEmbeddedSwift(CompileUnit *cu) {
return CheckFlagInCU(cu, "-enable-embedded-swift");
}

static bool IsUnitTestExecutable(lldb_private::Module &module) {
static ConstString s_xctest("xctest");
static ConstString s_XCTRunner("XCTRunner");
Expand Down
7 changes: 0 additions & 7 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,6 @@ class SwiftASTContext : public TypeSystemSwift {
TypeSystemSwiftTypeRef &typeref_typesystem,
const char *extra_options = nullptr);

/// Returns true if the given flag is present in the given compile unit.
static bool CheckFlagInCU(CompileUnit *cu, const char *flag);

static bool ShouldEnableCXXInterop(CompileUnit *cu);

static bool ShouldEnableEmbeddedSwift(CompileUnit *cu);

static void EnumerateSupportedLanguages(
std::set<lldb::LanguageType> &languages_for_types,
std::set<lldb::LanguageType> &languages_for_expressions);
Expand Down
42 changes: 42 additions & 0 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "Plugins/ExpressionParser/Swift/SwiftPersistentExpressionState.h"
#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Symbol/CompileUnit.h"
#include <lldb/lldb-enumerations.h>
#include <llvm/ADT/StringRef.h>

Expand Down Expand Up @@ -71,6 +72,47 @@ void TypeSystemSwift::Terminate() {

/// \}

bool TypeSystemSwift::CheckFlagInCU(CompileUnit *cu, const char *flag) {
AutoBool interop_enabled =
ModuleList::GetGlobalModuleListProperties().GetSwiftEnableCxxInterop();
switch (interop_enabled) {
case AutoBool::True:
return true;
case AutoBool::False:
return false;
case AutoBool::Auto: {
if (!cu)
return false;
lldb::ModuleSP module = cu->CalculateSymbolContextModule();
if (!module)
return false;
auto *sym_file = module->GetSymbolFile();
if (!sym_file)
return false;
auto options = sym_file->GetCompileOptions();
for (auto &[unit, args] : options) {
if (unit.get() == cu) {
if (cu->GetLanguage() == eLanguageTypeSwift)
for (const char *arg : args.GetArgumentArrayRef())
if (strcmp(arg, flag) == 0)
return true;
return false;
}
}
}
}
return false;
}

/// Determine whether this CU was compiled with C++ interop enabled.
bool TypeSystemSwift::ShouldEnableCXXInterop(CompileUnit *cu) {
return CheckFlagInCU(cu, "-enable-experimental-cxx-interop");
}

bool TypeSystemSwift::ShouldEnableEmbeddedSwift(CompileUnit *cu) {
return CheckFlagInCU(cu, "-enable-embedded-swift");
}

void TypeSystemSwift::Dump(llvm::raw_ostream &output) {
// TODO: What to dump?
}
Expand Down
5 changes: 5 additions & 0 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwift.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ class TypeSystemSwift : public TypeSystem {

const std::string &GetDescription() const { return m_description; }
static LanguageSet GetSupportedLanguagesForTypes();
/// Returns true if the given flag is present in the given compile unit.
static bool CheckFlagInCU(CompileUnit *cu, const char *flag);
static bool ShouldEnableCXXInterop(CompileUnit *cu);
static bool ShouldEnableEmbeddedSwift(CompileUnit *cu);

virtual SwiftASTContextSP
GetSwiftASTContext(const SymbolContext &sc) const = 0;
virtual TypeSystemSwiftTypeRefSP GetTypeSystemSwiftTypeRef() = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5336,11 +5336,13 @@ TypeSystemSwiftTypeRef::GetDependentGenericParamListForType(
swift::Mangle::ManglingFlavor
TypeSystemSwiftTypeRef::GetManglingFlavor(ExecutionContext *exe_ctx) {
auto sc = GetSymbolContext(exe_ctx);
if (auto ast_ctx = GetSwiftASTContext(sc))
return ast_ctx->GetManglingFlavor();
LLDB_LOG(GetLog(LLDBLog::Types),
"GetManglingFlavor failed to acquire a SwiftASTContext");
return swift::Mangle::ManglingFlavor::Default;
auto *cu = sc.comp_unit;
// Cache the result for the last recently used CU.
if (cu != m_lru_is_embedded.first)
m_lru_is_embedded = {cu, ShouldEnableEmbeddedSwift(sc.comp_unit)
? swift::Mangle::ManglingFlavor::Embedded
: swift::Mangle::ManglingFlavor::Default};
return m_lru_is_embedded.second;
}

#ifndef NDEBUG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,9 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {

/// All lldb::Type pointers produced by DWARFASTParser Swift go here.
ThreadSafeDenseMap<const char *, lldb::TypeSP> m_swift_type_map;
/// An LRU cache for \ref GetManglingFlavor().
std::pair<CompileUnit *, swift::Mangle::ManglingFlavor> m_lru_is_embedded = {
nullptr, swift::Mangle::ManglingFlavor::Default};
};

/// This one owns a SwiftASTContextForExpressions.
Expand Down