Skip to content

Commit 5aed050

Browse files
committed
[lldb] Avoid spinnning up a SwiftASTContext just to determine the mangling flavor.
1 parent 0a257a1 commit 5aed050

File tree

6 files changed

+57
-53
lines changed

6 files changed

+57
-53
lines changed

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,47 +2632,6 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
26322632
return swift_ast_sp;
26332633
}
26342634

2635-
bool SwiftASTContext::CheckFlagInCU(CompileUnit *cu, const char *flag) {
2636-
AutoBool interop_enabled =
2637-
ModuleList::GetGlobalModuleListProperties().GetSwiftEnableCxxInterop();
2638-
switch (interop_enabled) {
2639-
case AutoBool::True:
2640-
return true;
2641-
case AutoBool::False:
2642-
return false;
2643-
case AutoBool::Auto: {
2644-
if (!cu)
2645-
return false;
2646-
lldb::ModuleSP module = cu->CalculateSymbolContextModule();
2647-
if (!module)
2648-
return false;
2649-
auto *sym_file = module->GetSymbolFile();
2650-
if (!sym_file)
2651-
return false;
2652-
auto options = sym_file->GetCompileOptions();
2653-
for (auto &[unit, args] : options) {
2654-
if (unit.get() == cu) {
2655-
if (cu->GetLanguage() == eLanguageTypeSwift)
2656-
for (const char *arg : args.GetArgumentArrayRef())
2657-
if (strcmp(arg, flag) == 0)
2658-
return true;
2659-
return false;
2660-
}
2661-
}
2662-
}
2663-
}
2664-
return false;
2665-
}
2666-
2667-
/// Determine whether this CU was compiled with C++ interop enabled.
2668-
bool SwiftASTContext::ShouldEnableCXXInterop(CompileUnit *cu) {
2669-
return CheckFlagInCU(cu, "-enable-experimental-cxx-interop");
2670-
}
2671-
2672-
bool SwiftASTContext::ShouldEnableEmbeddedSwift(CompileUnit *cu) {
2673-
return CheckFlagInCU(cu, "-enable-embedded-swift");
2674-
}
2675-
26762635
static bool IsUnitTestExecutable(lldb_private::Module &module) {
26772636
static ConstString s_xctest("xctest");
26782637
static ConstString s_XCTRunner("XCTRunner");

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,6 @@ class SwiftASTContext : public TypeSystemSwift {
210210
TypeSystemSwiftTypeRef &typeref_typesystem,
211211
const char *extra_options = nullptr);
212212

213-
/// Returns true if the given flag is present in the given compile unit.
214-
static bool CheckFlagInCU(CompileUnit *cu, const char *flag);
215-
216-
static bool ShouldEnableCXXInterop(CompileUnit *cu);
217-
218-
static bool ShouldEnableEmbeddedSwift(CompileUnit *cu);
219-
220213
static void EnumerateSupportedLanguages(
221214
std::set<lldb::LanguageType> &languages_for_types,
222215
std::set<lldb::LanguageType> &languages_for_expressions);

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwift.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "Plugins/ExpressionParser/Swift/SwiftPersistentExpressionState.h"
1616
#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
1717
#include "lldb/Core/PluginManager.h"
18+
#include "lldb/Symbol/CompileUnit.h"
1819
#include <lldb/lldb-enumerations.h>
1920
#include <llvm/ADT/StringRef.h>
2021

@@ -71,6 +72,47 @@ void TypeSystemSwift::Terminate() {
7172

7273
/// \}
7374

75+
bool TypeSystemSwift::CheckFlagInCU(CompileUnit *cu, const char *flag) {
76+
AutoBool interop_enabled =
77+
ModuleList::GetGlobalModuleListProperties().GetSwiftEnableCxxInterop();
78+
switch (interop_enabled) {
79+
case AutoBool::True:
80+
return true;
81+
case AutoBool::False:
82+
return false;
83+
case AutoBool::Auto: {
84+
if (!cu)
85+
return false;
86+
lldb::ModuleSP module = cu->CalculateSymbolContextModule();
87+
if (!module)
88+
return false;
89+
auto *sym_file = module->GetSymbolFile();
90+
if (!sym_file)
91+
return false;
92+
auto options = sym_file->GetCompileOptions();
93+
for (auto &[unit, args] : options) {
94+
if (unit.get() == cu) {
95+
if (cu->GetLanguage() == eLanguageTypeSwift)
96+
for (const char *arg : args.GetArgumentArrayRef())
97+
if (strcmp(arg, flag) == 0)
98+
return true;
99+
return false;
100+
}
101+
}
102+
}
103+
}
104+
return false;
105+
}
106+
107+
/// Determine whether this CU was compiled with C++ interop enabled.
108+
bool TypeSystemSwift::ShouldEnableCXXInterop(CompileUnit *cu) {
109+
return CheckFlagInCU(cu, "-enable-experimental-cxx-interop");
110+
}
111+
112+
bool TypeSystemSwift::ShouldEnableEmbeddedSwift(CompileUnit *cu) {
113+
return CheckFlagInCU(cu, "-enable-embedded-swift");
114+
}
115+
74116
void TypeSystemSwift::Dump(llvm::raw_ostream &output) {
75117
// TODO: What to dump?
76118
}

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwift.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ class TypeSystemSwift : public TypeSystem {
127127

128128
const std::string &GetDescription() const { return m_description; }
129129
static LanguageSet GetSupportedLanguagesForTypes();
130+
/// Returns true if the given flag is present in the given compile unit.
131+
static bool CheckFlagInCU(CompileUnit *cu, const char *flag);
132+
static bool ShouldEnableCXXInterop(CompileUnit *cu);
133+
static bool ShouldEnableEmbeddedSwift(CompileUnit *cu);
134+
130135
virtual SwiftASTContextSP
131136
GetSwiftASTContext(const SymbolContext &sc) const = 0;
132137
virtual TypeSystemSwiftTypeRefSP GetTypeSystemSwiftTypeRef() = 0;

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5336,11 +5336,13 @@ TypeSystemSwiftTypeRef::GetDependentGenericParamListForType(
53365336
swift::Mangle::ManglingFlavor
53375337
TypeSystemSwiftTypeRef::GetManglingFlavor(ExecutionContext *exe_ctx) {
53385338
auto sc = GetSymbolContext(exe_ctx);
5339-
if (auto ast_ctx = GetSwiftASTContext(sc))
5340-
return ast_ctx->GetManglingFlavor();
5341-
LLDB_LOG(GetLog(LLDBLog::Types),
5342-
"GetManglingFlavor failed to acquire a SwiftASTContext");
5343-
return swift::Mangle::ManglingFlavor::Default;
5339+
auto *cu = sc.comp_unit;
5340+
// Cache the result for the last recently used CU.
5341+
if (cu != m_lru_is_embedded.first)
5342+
m_lru_is_embedded = {cu, ShouldEnableEmbeddedSwift(sc.comp_unit)
5343+
? swift::Mangle::ManglingFlavor::Embedded
5344+
: swift::Mangle::ManglingFlavor::Default};
5345+
return m_lru_is_embedded.second;
53445346
}
53455347

53465348
#ifndef NDEBUG

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,9 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
597597

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

602605
/// This one owns a SwiftASTContextForExpressions.

0 commit comments

Comments
 (0)