Skip to content

[cherry-pick][lldb] Check if C++ interop and embedded Swift are enabled on CU instead of whole module #10303

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
26 changes: 23 additions & 3 deletions lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1165,9 +1165,6 @@ SwiftLanguage::GetHardcodedSynthetics() {

Log *log(GetLog(LLDBLog::DataFormatters));

if (!valobj.GetTargetSP()->IsSwiftCxxInteropEnabled())
return nullptr;

CompilerType type(valobj.GetCompilerType());
auto swift_type_system =
type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
Expand Down Expand Up @@ -1197,6 +1194,29 @@ SwiftLanguage::GetHardcodedSynthetics() {
return nullptr;
}

// Find the compile unit and module using the frame, because the value
// object may not have a module in the case of an expression that
// evaluates to a type.
if (!valobj.GetFrameSP())
return nullptr;

auto sc = valobj.GetFrameSP()->GetSymbolContext(
lldb::SymbolContextItem::eSymbolContextCompUnit |
lldb::SymbolContextItem::eSymbolContextModule);

// If there is a compile unit, use that to check if C++ interop should be
// enabled. If there is no compiler unit, use the module. If neither
// exist, assume that C++ interop is disabled.
if (auto *cu = sc.comp_unit) {
if (!SwiftASTContext::ShouldEnableCXXInterop(cu))
return nullptr;
} else if (sc.module_sp) {
if (!sc.module_sp->IsSwiftCxxInteropEnabled())
return nullptr;
} else {
return nullptr;
}

casted->SetName(ConstString("Clang_Type"));

SyntheticChildrenSP synth_sp =
Expand Down
22 changes: 14 additions & 8 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2625,8 +2625,7 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
return swift_ast_sp;
}

/// Determine whether this CU was compiled with C++ interop enabled.
static bool ShouldEnableCXXInterop(CompileUnit *cu) {
bool SwiftASTContext::CheckFlagInCU(CompileUnit *cu, const char *flag) {
AutoBool interop_enabled =
ModuleList::GetGlobalModuleListProperties().GetSwiftEnableCxxInterop();
switch (interop_enabled) {
Expand All @@ -2640,8 +2639,6 @@ static bool ShouldEnableCXXInterop(CompileUnit *cu) {
lldb::ModuleSP module = cu->CalculateSymbolContextModule();
if (!module)
return false;
// Look for the "-enable-experimental-cxx-interop" compile flag in the
// args of the compile units this module is composed of.
auto *sym_file = module->GetSymbolFile();
if (!sym_file)
return false;
Expand All @@ -2650,7 +2647,7 @@ static bool ShouldEnableCXXInterop(CompileUnit *cu) {
if (unit.get() == cu) {
if (cu->GetLanguage() == eLanguageTypeSwift)
for (const char *arg : args.GetArgumentArrayRef())
if (strcmp(arg, "-enable-experimental-cxx-interop") == 0)
if (strcmp(arg, flag) == 0)
return true;
return false;
}
Expand All @@ -2660,6 +2657,15 @@ static bool ShouldEnableCXXInterop(CompileUnit *cu) {
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 Expand Up @@ -2773,7 +2779,7 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
swift_ast_sp->m_is_scratch_context = true;
auto &lang_opts = swift_ast_sp->GetLanguageOptions();
lang_opts.EnableCXXInterop = ShouldEnableCXXInterop(cu);
if (target_sp->IsEmbeddedSwift())
if (ShouldEnableEmbeddedSwift(cu))
lang_opts.enableFeature(swift::Feature::Embedded);
} else {
// Typesystem fallback context.
Expand All @@ -2789,8 +2795,8 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
swift_ast_sp->m_module = module_sp.get();
auto &lang_opts = swift_ast_sp->GetLanguageOptions();
lang_opts.EnableAccessControl = false;
lang_opts.EnableCXXInterop = module_sp->IsSwiftCxxInteropEnabled();
if (module_sp->IsEmbeddedSwift())
lang_opts.EnableCXXInterop = ShouldEnableCXXInterop(cu);
if (ShouldEnableEmbeddedSwift(cu))
lang_opts.enableFeature(swift::Feature::Embedded);
}
auto defer_log = llvm::make_scope_exit(
Expand Down
7 changes: 7 additions & 0 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ 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