Skip to content

Commit 6b0ebbc

Browse files
committed
Revert "[cxx-interop] Check for NS_OPTIONS macro in findOptionSetEnum()"
This reverts commit 89f8855.
1 parent 20521ed commit 6b0ebbc

8 files changed

+83
-28
lines changed

lib/ClangImporter/ImportEnumInfo.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,6 @@ ImportedType importer::findOptionSetEnum(clang::QualType type,
255255
// then this definitely isn't used for {CF,NS}_OPTIONS.
256256
return ImportedType();
257257

258-
if (Impl.SwiftContext.LangOpts.EnableCXXInterop &&
259-
!isCFOptionsMacro(typedefType->getDecl(), Impl.getClangPreprocessor())) {
260-
return ImportedType();
261-
}
262-
263258
auto clangEnum = findAnonymousEnumForTypedef(Impl.SwiftContext, typedefType);
264259
if (!clangEnum)
265260
return ImportedType();

lib/ClangImporter/ImportEnumInfo.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
#include "swift/AST/ASTContext.h"
2121
#include "swift/AST/Decl.h"
22-
#include "clang/Lex/Preprocessor.h"
23-
#include "clang/Sema/Sema.h"
2422
#include "llvm/ADT/APSInt.h"
2523
#include "llvm/ADT/DenseMap.h"
2624

@@ -166,18 +164,14 @@ StringRef getCommonPluralPrefix(StringRef singular, StringRef plural);
166164
/// an elaborated type, an unwrapped type is returned.
167165
const clang::Type *getUnderlyingType(const clang::EnumDecl *decl);
168166

169-
inline bool isCFOptionsMacro(const clang::NamedDecl *decl,
170-
clang::Preprocessor &preprocessor) {
171-
auto loc = decl->getEndLoc();
172-
if (!loc.isMacroID())
173-
return false;
174-
return llvm::StringSwitch<bool>(preprocessor.getImmediateMacroName(loc))
167+
inline bool isCFOptionsMacro(StringRef macroName) {
168+
return llvm::StringSwitch<bool>(macroName)
175169
.Case("CF_OPTIONS", true)
176170
.Case("NS_OPTIONS", true)
177171
.Default(false);
178172
}
179173

180-
} // namespace importer
181-
} // namespace swift
174+
}
175+
}
182176

183177
#endif // SWIFT_CLANG_IMPORT_ENUM_H

lib/ClangImporter/ImportName.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "CFTypeInfo.h"
1919
#include "ClangClassTemplateNamePrinter.h"
2020
#include "ClangDiagnosticConsumer.h"
21-
#include "ImportEnumInfo.h"
2221
#include "ImporterImpl.h"
2322
#include "swift/AST/ASTContext.h"
2423
#include "swift/AST/ClangSwiftTypeCorrespondence.h"
@@ -1878,9 +1877,15 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
18781877
// imported into Swift to avoid having two types with the same name, which
18791878
// cause subtle name lookup issues.
18801879
if (swiftCtx.LangOpts.EnableCXXInterop &&
1881-
isUnavailableInSwift(D, nullptr, true) &&
1882-
isCFOptionsMacro(D, clangSema.getPreprocessor()))
1883-
return ImportedName();
1880+
isUnavailableInSwift(D, nullptr, true)) {
1881+
auto loc = D->getEndLoc();
1882+
if (loc.isMacroID()) {
1883+
StringRef macroName =
1884+
clangSema.getPreprocessor().getImmediateMacroName(loc);
1885+
if (isCFOptionsMacro(macroName))
1886+
return ImportedName();
1887+
}
1888+
}
18841889

18851890
/// Whether the result is a function name.
18861891
bool isFunction = false;

lib/ClangImporter/ImportType.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "CFTypeInfo.h"
18-
#include "ImportEnumInfo.h"
1918
#include "ImporterImpl.h"
2019
#include "SwiftDeclSynthesizer.h"
2120
#include "swift/ABI/MetadataValues.h"
@@ -2910,8 +2909,13 @@ ArgumentAttrs ClangImporter::Implementation::inferDefaultArgument(
29102909
return argumentAttrs;
29112910
}
29122911
}
2913-
if (isCFOptionsMacro(typedefDecl, nameImporter.getClangPreprocessor()))
2914-
return argumentAttrs;
2912+
auto loc = typedefDecl->getEndLoc();
2913+
if (loc.isMacroID()) {
2914+
StringRef macroName =
2915+
nameImporter.getClangPreprocessor().getImmediateMacroName(loc);
2916+
if (isCFOptionsMacro(macroName))
2917+
return argumentAttrs;
2918+
}
29152919
}
29162920
}
29172921

test/Interop/Cxx/enum/Inputs/anonymous-with-swift-name.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
#ifndef TEST_INTEROP_CXX_ENUM_INPUTS_ANONYMOUS_WITH_SWIFT_NAME_H
22
#define TEST_INTEROP_CXX_ENUM_INPUTS_ANONYMOUS_WITH_SWIFT_NAME_H
33

4+
#define SOME_OPTIONS(_type, _name) __attribute__((availability(swift, unavailable))) _type _name; enum __attribute__((flag_enum,enum_extensibility(open))) : _name
45
#define CF_OPTIONS(_type, _name) __attribute__((availability(swift, unavailable))) _type _name; enum : _name
56

7+
typedef SOME_OPTIONS(unsigned, SOColorMask) {
8+
kSOColorMaskRed = (1 << 1),
9+
kSOColorMaskGreen = (1 << 2),
10+
kSOColorMaskBlue = (1 << 3),
11+
kSOColorMaskAll = ~0U
12+
};
13+
14+
615
typedef CF_OPTIONS(unsigned, CFColorMask) {
716
kCFColorMaskRed = (1 << 1),
817
kCFColorMaskGreen = (1 << 2),
918
kCFColorMaskBlue = (1 << 3),
1019
kCFColorMaskAll = ~0U
1120
};
1221

22+
inline SOColorMask useSOColorMask(SOColorMask mask) { return mask; }
1323
inline CFColorMask useCFColorMask(CFColorMask mask) { return mask; }
1424

1525
struct ParentStruct { };
1626

1727
inline CFColorMask renameCFColorMask(ParentStruct parent)
1828
__attribute__((swift_name("ParentStruct.childFn(self:)")))
19-
{ return kCFColorMaskRed; }
29+
{ return kSOColorMaskRed; }
2030

2131
inline CFColorMask getCFColorMask(ParentStruct parent)
2232
__attribute__((swift_name("getter:ParentStruct.colorProp(self:)")))
23-
{ return kCFColorMaskRed; }
33+
{ return kSOColorMaskRed; }
2434

25-
inline void setCFColorMask(ParentStruct parent, CFColorMask newValue)
35+
inline void getCFColorMask(ParentStruct parent, CFColorMask newValue)
2636
__attribute__((swift_name("setter:ParentStruct.colorProp(self:newValue:)")))
2737
{ }
2838

@@ -42,8 +52,9 @@ enum __attribute__((flag_enum,enum_extensibility(open))) : GlobalOldName {
4252

4353
#if __OBJC__
4454
@interface ColorMaker
55+
- (void)makeColorWithOptions:(SOColorMask)opts;
4556
- (void)makeOtherColorWithInt:(int) x withOptions:(CFColorMask)opts;
4657
@end
4758
#endif // SWIFT_OBJC_INTEROP
4859

49-
#endif // TEST_INTEROP_CXX_ENUM_INPUTS_ANONYMOUS_WITH_SWIFT_NAME_H
60+
#endif // TEST_INTEROP_CXX_ENUM_INPUTS_ANONYMOUS_WITH_SWIFT_NAME_H

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
// RUN: %target-swift-ide-test -print-module -module-to-print=AnonymousWithSwiftName -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
22

3+
// CHECK: @available(*, unavailable, message: "Not available in Swift")
4+
// CHECK: typealias SOColorMask = UInt32
5+
6+
// CHECK: struct SOColorMask : OptionSet, @unchecked Sendable {
7+
// CHECK: init(rawValue: UInt32)
8+
// CHECK: let rawValue: UInt32
9+
// CHECK: typealias RawValue = UInt32
10+
// CHECK: typealias Element = SOColorMask
11+
// CHECK: typealias ArrayLiteralElement = SOColorMask
12+
13+
// CHECK: static var red: SOColorMask { get }
14+
// CHECK: @available(swift, obsoleted: 3, renamed: "red")
15+
// CHECK: static var Red: SOColorMask { get }
16+
17+
// CHECK: static var green: SOColorMask { get }
18+
// CHECK: @available(swift, obsoleted: 3, renamed: "green")
19+
// CHECK: static var Green: SOColorMask { get }
20+
21+
// CHECK: static var blue: SOColorMask { get }
22+
// CHECK: @available(swift, obsoleted: 3, renamed: "blue")
23+
// CHECK: static var Blue: SOColorMask { get }
24+
25+
// CHECK: static var all: SOColorMask { get }
26+
// CHECK: @available(swift, obsoleted: 3, renamed: "all")
27+
// CHECK: static var All: SOColorMask { get }
28+
// CHECK: }
29+
330
// CHECK-NOT: typealias CFColorMask = UInt32
431

532
// CHECK: struct CFColorMask : OptionSet {
@@ -26,6 +53,7 @@
2653
// CHECK: static var All: CFColorMask { get }
2754
// CHECK: }
2855

56+
// CHECK: func useSOColorMask(_ mask: SOColorMask) -> SOColorMask
2957
// CHECK: func useCFColorMask(_ mask: CFColorMask) -> CFColorMask
3058

3159
// Test rename with "swift_name" attr:

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
// REQUIRES: objc_interop
44

55
// CHECK: class ColorMaker {
6+
// CHECK: class func makeColor(withOptions opts: SOColorMask)
7+
// CHECK: func makeColor(withOptions opts: SOColorMask)
8+
// CHECK: @available(swift, obsoleted: 3, renamed: "makeColor(withOptions:)")
9+
// CHECK: class func makeColorWithOptions(_ opts: SOColorMask)
10+
// CHECK: @available(swift, obsoleted: 3, renamed: "makeColor(withOptions:)")
11+
// CHECK: func makeColorWithOptions(_ opts: SOColorMask)
612
// CHECK: class func makeOtherColor(with x: Int32, withOptions opts: CFColorMask)
713
// CHECK: func makeOtherColor(with x: Int32, withOptions opts: CFColorMask)
814
// CHECK: @available(swift, obsoleted: 3, renamed: "makeOtherColor(with:withOptions:)")

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ import StdlibUnittest
77

88
var AnonymousEnumsTestSuite = TestSuite("Anonymous Enums With Swift Name")
99

10+
AnonymousEnumsTestSuite.test("SOME_OPTIONS") {
11+
let red: SOColorMask = .red
12+
let green = SOColorMask.green
13+
let blue = .blue as SOColorMask
14+
let all: SOColorMask = .all
15+
16+
expectEqual(red.rawValue, 2)
17+
expectEqual(green.rawValue, 4)
18+
expectEqual(blue.rawValue, 8)
19+
expectEqual(all.rawValue, ~CUnsignedInt(0))
20+
}
21+
1022
AnonymousEnumsTestSuite.test("CF_OPTIONS") {
1123
let red: CFColorMask = .red
1224
let green = CFColorMask.green
@@ -23,8 +35,8 @@ AnonymousEnumsTestSuite.test("Parameter types") {
2335
let red: CFColorMask = .red
2436
let green = CFColorMask.green
2537

26-
let blue = useCFColorMask(.blue)
27-
let all = useCFColorMask(.all)
38+
let blue = useSOColorMask(.blue)
39+
let all = useSOColorMask(.all)
2840

2941
expectEqual(red, useCFColorMask(.red))
3042
expectEqual(green, useCFColorMask(.green))

0 commit comments

Comments
 (0)