Skip to content

Commit c7ddbee

Browse files
authored
Merge pull request #79822 from swiftlang/egorzhdan/NSUnderlineStyle
[cxx-interop] Allow AppKit & UIKit to be rebuilt with C++ interop enabled
2 parents fd92e27 + 3791ccb commit c7ddbee

File tree

6 files changed

+29
-16
lines changed

6 files changed

+29
-16
lines changed

lib/ClangImporter/ImportName.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,25 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
18611861
return result;
18621862
}
18631863

1864+
// In C++ language mode, CF_OPTIONS/NS_OPTIONS macro has a different
1865+
// expansion: instead of a forward-declared enum, it expands into a typedef
1866+
// that is marked as `__attribute__((availability(swift,unavailable)))`, and
1867+
// an anonymous enum that inherits from the typedef. The logic above imports
1868+
// the anonymous enum with the desired name based on the typedef's name. In
1869+
// addition to that, we should make sure the unavailable typedef isn't
1870+
// imported into Swift to avoid having two types with the same name, which
1871+
// cause subtle name lookup issues.
1872+
if (swiftCtx.LangOpts.EnableCXXInterop &&
1873+
isUnavailableInSwift(D, nullptr, true)) {
1874+
auto loc = D->getEndLoc();
1875+
if (loc.isMacroID()) {
1876+
StringRef macroName =
1877+
clangSema.getPreprocessor().getImmediateMacroName(loc);
1878+
if (macroName == "CF_OPTIONS" || macroName == "NS_OPTIONS")
1879+
return ImportedName();
1880+
}
1881+
}
1882+
18641883
/// Whether the result is a function name.
18651884
bool isFunction = false;
18661885
bool isInitializer = false;

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,10 +2106,7 @@ InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
21062106
// interop enabled by the Swift CI because it uses an old host SDK.
21072107
// FIXME: Hack for CoreGraphics.swiftmodule, which cannot be rebuilt because
21082108
// of a CF_OPTIONS bug (rdar://142762174).
2109-
// FIXME: Hack for AppKit.swiftmodule / UIKit.swiftmodule, which cannot be
2110-
// rebuilt because of an NS_OPTIONS bug (rdar://143033209)
2111-
if (moduleName == "Darwin" || moduleName == "CoreGraphics"
2112-
|| moduleName == "AppKit" || moduleName == "UIKit") {
2109+
if (moduleName == "Darwin" || moduleName == "CoreGraphics") {
21132110
subInvocation.getLangOptions().EnableCXXInterop = false;
21142111
subInvocation.getLangOptions().cxxInteropCompatVersion = {};
21152112
BuildArgs.erase(llvm::remove_if(BuildArgs,

test/Interop/Cxx/enum/Inputs/CFAvailability.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@
1010
#endif
1111

1212
#if (__cplusplus)
13-
#define CF_OPTIONS(_type, _name) \
14-
_type __attribute__((availability(swift, unavailable))) _name; \
15-
enum __CF_OPTIONS_ATTRIBUTES : _name
13+
#define CF_OPTIONS(_type, _name) __attribute__((availability(swift,unavailable))) _type _name; enum __CF_OPTIONS_ATTRIBUTES : _name
1614
#else
17-
#define CF_OPTIONS(_type, _name) \
18-
enum __CF_OPTIONS_ATTRIBUTES _name : _type _name; \
19-
enum _name : _type
15+
#define CF_OPTIONS(_type, _name) enum __CF_OPTIONS_ATTRIBUTES _name : _type _name; enum _name : _type
2016
#endif
2117

2218
#define NS_OPTIONS(_type, _name) CF_OPTIONS(_type, _name)

test/Interop/Cxx/enum/anonymous-with-swift-name-module-interface.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
// CHECK: static var All: SOColorMask { get }
2828
// CHECK: }
2929

30-
// CHECK: @available(*, unavailable, message: "Not available in Swift")
31-
// CHECK: typealias CFColorMask = UInt32
30+
// CHECK-NOT: typealias CFColorMask = UInt32
3231

3332
// CHECK: struct CFColorMask : OptionSet {
3433
// CHECK: init(rawValue: UInt32)

test/Interop/Cxx/enum/c-enums-NS_OPTIONS-NS_REFINED_FOR_SWIFT.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
import CenumsNSOptions
55

6-
// CHECK: typealias NSAttributedStringFormattingOptions = UInt
7-
// CHECK-NEXT: struct __NSAttributedStringFormattingOptions : OptionSet, @unchecked Sendable {
6+
// CHECK-NOT: typealias NSAttributedStringFormattingOptions = UInt
7+
8+
// CHECK: struct __NSAttributedStringFormattingOptions : OptionSet, @unchecked Sendable {
89
// CHECK-NEXT: init(rawValue: UInt)
910
// CHECK-NEXT: let rawValue: UInt
1011
// CHECK-NEXT: typealias RawValue = UInt

test/Interop/Cxx/enum/c-enums-NS_OPTIONS.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
import CenumsNSOptions
55

6-
// CHECK: typealias NSBinarySearchingOptions = UInt
7-
// CHECK-NEXT: struct NSBinarySearchingOptions : OptionSet, @unchecked Sendable {
6+
// CHECK-NOT: typealias NSBinarySearchingOptions = UInt
7+
8+
// CHECK: struct NSBinarySearchingOptions : OptionSet, @unchecked Sendable {
89
// CHECK-NEXT: init(rawValue: UInt)
910
// CHECK-NEXT: let rawValue: UInt
1011
// CHECK-NEXT: typealias RawValue = UInt

0 commit comments

Comments
 (0)