Skip to content

Precise compiler invocations: Set C++ interoperability per SymbolContext #8592

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
merged 1 commit into from
Apr 18, 2024
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
4 changes: 3 additions & 1 deletion lldb/source/Core/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,9 @@ bool Module::IsSwiftCxxInteropEnabled() {
auto *sym_file = GetSymbolFile();
if (sym_file) {
auto options = sym_file->GetCompileOptions();
for (auto &[_, args] : options) {
for (auto &[cu, args] : options) {
if (cu->GetLanguage() != eLanguageTypeSwift)
continue;
for (const char *arg : args.GetArgumentArrayRef()) {
if (strcmp(arg, "-enable-experimental-cxx-interop") == 0) {
m_is_swift_cxx_interop_enabled = eLazyBoolYes;
Expand Down
37 changes: 36 additions & 1 deletion lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2172,6 +2172,41 @@ 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) {
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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't you call Module::IsSwiftCxxInteropEnabled? The code looks pretty much the same.

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;
auto options = sym_file->GetCompileOptions();
for (auto &[unit, args] : options) {
if (unit.get() == cu) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of this check.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (cu->GetLanguage() == eLanguageTypeSwift)
for (const char *arg : args.GetArgumentArrayRef())
if (strcmp(arg, "-enable-experimental-cxx-interop") == 0)
return true;
return false;
}
}
}
}
return false;
}

static bool IsUnitTestExecutable(lldb_private::Module &module) {
static ConstString s_xctest("xctest");
static ConstString s_XCTRunner("XCTRunner");
Expand Down Expand Up @@ -2728,7 +2763,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
swift_ast_sp->m_is_scratch_context = true;

swift_ast_sp->GetLanguageOptions().EnableCXXInterop =
target.IsSwiftCxxInteropEnabled();
ShouldEnableCXXInterop(cu);

if (target.IsEmbeddedSwift())
swift_ast_sp->GetLanguageOptions().enableFeature(swift::Feature::Embedded);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public func f() {
print("break here")
}
21 changes: 21 additions & 0 deletions lldb/test/API/lang/swift/cxx_interop/forward/langopt/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
SWIFT_SOURCES := main.swift
SWIFT_CXX_INTEROP := 1
SWIFTFLAGS_EXTRAS = -I.
LD_EXTRAS = -L$(BUILDDIR) -lDylib

all: libDylib.dylib a.out

include Makefile.rules

libDylib.dylib: Dylib.swift
$(MAKE) MAKE_DSYM=$(MAKE_DSYM) CC=$(CC) SWIFTC=$(SWIFTC) \
ARCH=$(ARCH) DSYMUTIL=$(DSYMUTIL) \
VPATH=$(SRCDIR) -I $(SRCDIR) \
-f $(THIS_FILE_DIR)/Makefile.rules \
DYLIB_SWIFT_SOURCES=Dylib.swift \
DYLIB_NAME=Dylib \
DYLIB_ONLY=YES \
SWIFT_SOURCES= \
SWIFT_CXX_INTEROP=0 \
SWIFT_BRIDGING_HEADER= \
all
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

"""
Test that a C++ class is visible in Swift.
"""
from lldbsuite.test.lldbtest import *
from lldbsuite.test.decorators import *


class TestSwiftForwardInteropCxxLangOpt(TestBase):

@swiftTest
@expectedFailureAll(setting=('symbols.swift-precise-compiler-invocation', 'false'))
def test_class(self):
"""
Test that C++ interoperability is enabled on a per-CU basis.
"""
self.build()
log = self.getBuildArtifact("types.log")
self.runCmd('log enable lldb types -f "%s"' % log)
target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
self, 'break here', lldb.SBFileSpec('main.swift'),
extra_images=['Dylib'])
dylib_bkpt = target.BreakpointCreateBySourceRegex(
'break here', lldb.SBFileSpec('Dylib.swift'))
self.assertGreater(dylib_bkpt.GetNumLocations(), 0, VALID_BREAKPOINT)
self.expect('expr 0')
lldbutil.continue_to_breakpoint(process, dylib_bkpt)
self.expect('expr 1')
self.filecheck('platform shell cat ""%s"' % log, __file__)
# CHECK: SwiftASTContextForExpressions(module: "a", cu: "main.swift")::LogConfiguration(){{.*}}Swift/C++ interop : on
# CHECK: SwiftASTContextForExpressions(module: "Dylib", cu: "Dylib.swift")::LogConfiguration(){{.*}}Swift/C++ interop : off

10 changes: 10 additions & 0 deletions lldb/test/API/lang/swift/cxx_interop/forward/langopt/bridging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

struct CxxClass {
long long a1 = 10;
long long a2 = 20;
long long a3 = 30;
};

struct InheritedCxxClass: CxxClass {
long long a4 = 40;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Dylib

print("break here")
f()