Skip to content

Commit ba5dbf8

Browse files
committed
Merge remote-tracking branch 'origin/main' into rebranch
2 parents 5dbdcd9 + 1f67607 commit ba5dbf8

File tree

5 files changed

+80
-58
lines changed

5 files changed

+80
-58
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2651,44 +2651,31 @@ ArgumentAttrs ClangImporter::Implementation::inferDefaultArgument(
26512651
}
26522652
} else if (const clang::TypedefType *typedefType =
26532653
type->getAs<clang::TypedefType>()) {
2654-
// Get the AvailabilityAttr that would be set from CF/NS_OPTIONS
2655-
if (importer::isUnavailableInSwift(typedefType->getDecl(), nullptr, true)) {
2656-
// If we've taken this branch it means we have an enum type, and it is
2657-
// likely an integer or NSInteger that is being used by NS/CF_OPTIONS to
2658-
// behave like a C enum in the presence of C++.
2659-
auto enumName = typedefType->getDecl()->getName();
2660-
ArgumentAttrs argumentAttrs(DefaultArgumentKind::None, true, enumName);
2661-
auto camelCaseWords = camel_case::getWords(enumName);
2662-
for (auto it = camelCaseWords.rbegin(); it != camelCaseWords.rend();
2663-
++it) {
2664-
auto word = *it;
2665-
auto next = std::next(it);
2666-
if (camel_case::sameWordIgnoreFirstCase(word, "options")) {
2667-
argumentAttrs.argumentKind = DefaultArgumentKind::EmptyArray;
2668-
return argumentAttrs;
2654+
clang::TypedefNameDecl *typedefDecl = typedefType->getDecl();
2655+
// Find the next decl in the same context. If this typedef is a part of an
2656+
// NS/CF_OPTIONS declaration, the next decl will be an enum.
2657+
auto declsInContext = typedefDecl->getDeclContext()->decls();
2658+
auto declIter = llvm::find(declsInContext, typedefDecl);
2659+
if (declIter != declsInContext.end())
2660+
declIter++;
2661+
if (declIter != declsInContext.end()) {
2662+
if (auto enumDecl = dyn_cast<clang::EnumDecl>(*declIter)) {
2663+
if (auto cfOptionsTy =
2664+
nameImporter.getContext()
2665+
.getClangModuleLoader()
2666+
->getTypeDefForCXXCFOptionsDefinition(enumDecl)) {
2667+
if (cfOptionsTy->getDecl() == typedefDecl) {
2668+
auto enumName = typedefDecl->getName();
2669+
ArgumentAttrs argumentAttrs(DefaultArgumentKind::None, true,
2670+
enumName);
2671+
for (auto word : llvm::reverse(camel_case::getWords(enumName))) {
2672+
if (camel_case::sameWordIgnoreFirstCase(word, "options")) {
2673+
argumentAttrs.argumentKind = DefaultArgumentKind::EmptyArray;
2674+
}
2675+
}
2676+
return argumentAttrs;
2677+
}
26692678
}
2670-
if (camel_case::sameWordIgnoreFirstCase(word, "units"))
2671-
return argumentAttrs;
2672-
if (camel_case::sameWordIgnoreFirstCase(word, "domain"))
2673-
return argumentAttrs;
2674-
if (camel_case::sameWordIgnoreFirstCase(word, "action"))
2675-
return argumentAttrs;
2676-
if (camel_case::sameWordIgnoreFirstCase(word, "event"))
2677-
return argumentAttrs;
2678-
if (camel_case::sameWordIgnoreFirstCase(word, "events") &&
2679-
next != camelCaseWords.rend() &&
2680-
camel_case::sameWordIgnoreFirstCase(*next, "control"))
2681-
return argumentAttrs;
2682-
if (camel_case::sameWordIgnoreFirstCase(word, "state"))
2683-
return argumentAttrs;
2684-
if (camel_case::sameWordIgnoreFirstCase(word, "unit"))
2685-
return argumentAttrs;
2686-
if (camel_case::sameWordIgnoreFirstCase(word, "position") &&
2687-
next != camelCaseWords.rend() &&
2688-
camel_case::sameWordIgnoreFirstCase(*next, "scroll"))
2689-
return argumentAttrs;
2690-
if (camel_case::sameWordIgnoreFirstCase(word, "edge"))
2691-
return argumentAttrs;
26922679
}
26932680
}
26942681
}

test/Interop/Cxx/enum/Inputs/c-enums-withOptions-omit.h

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,57 @@
1-
// Enum usage that is bitwise-able and assignable in C++, aka how CF_OPTIONS
2-
// does things.
3-
typedef int __attribute__((availability(swift, unavailable))) NSEnumerationOptions;
4-
enum : NSEnumerationOptions { NSEnumerationConcurrent, NSEnumerationReverse };
1+
typedef unsigned NSUInteger;
2+
3+
#define __CF_OPTIONS_ATTRIBUTES __attribute__((flag_enum,enum_extensibility(open)))
4+
#if (__cplusplus)
5+
#define CF_OPTIONS(_type, _name) __attribute__((availability(swift,unavailable))) _type _name; enum __CF_OPTIONS_ATTRIBUTES : _name
6+
#else
7+
#define CF_OPTIONS(_type, _name) enum __CF_OPTIONS_ATTRIBUTES _name : _type _name; enum _name : _type
8+
#endif
9+
10+
typedef CF_OPTIONS(NSUInteger, NSEnumerationOptions) {
11+
NSEnumerationConcurrent = (1UL << 0),
12+
NSEnumerationReverse = (1UL << 1),
13+
};
514

615
@interface NSSet
716
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts ;
817
@end
918

10-
typedef int __attribute__((availability(swift, unavailable))) NSOrderedCollectionDifferenceCalculationOptions;
11-
enum : NSOrderedCollectionDifferenceCalculationOptions {
19+
typedef CF_OPTIONS(NSUInteger, NSOrderedCollectionDifferenceCalculationOptions) {
1220
NSOrderedCollectionDifferenceCalculationOptions1,
1321
NSOrderedCollectionDifferenceCalculationOptions2
1422
};
1523

16-
typedef int __attribute__((availability(swift, unavailable))) NSCalendarUnit;
17-
enum : NSCalendarUnit { NSCalendarUnit1, NSCalendarUnit2 };
24+
typedef CF_OPTIONS(NSUInteger, NSCalendarUnit) {
25+
NSCalendarUnit1,
26+
NSCalendarUnit2
27+
};
1828

19-
typedef int __attribute__((availability(swift, unavailable))) NSSearchPathDomainMask;
20-
enum : NSSearchPathDomainMask { NSSearchPathDomainMask1, NSSearchPathDomainMask2 };
29+
typedef CF_OPTIONS(NSUInteger, NSSearchPathDomainMask) {
30+
NSSearchPathDomainMask1,
31+
NSSearchPathDomainMask2
32+
};
2133

22-
typedef int __attribute__((availability(swift, unavailable))) NSControlCharacterAction;
23-
enum : NSControlCharacterAction { NSControlCharacterAction1, NSControlCharacterAction2 };
34+
typedef CF_OPTIONS(NSUInteger, NSControlCharacterAction) {
35+
NSControlCharacterAction1,
36+
NSControlCharacterAction2
37+
};
2438

25-
typedef int __attribute__((availability(swift, unavailable))) UIControlState;
26-
enum : UIControlState { UIControlState1, UIControlState2 };
39+
typedef CF_OPTIONS(NSUInteger, UIControlState) {
40+
UIControlState1,
41+
UIControlState2
42+
};
2743

28-
typedef int __attribute__((availability(swift, unavailable))) UITableViewCellStateMask;
29-
enum : UITableViewCellStateMask { UITableViewCellStateMask1, UITableViewCellStateMask2 };
44+
typedef CF_OPTIONS(NSUInteger, UITableViewCellStateMask) {
45+
UITableViewCellStateMask1,
46+
UITableViewCellStateMask2
47+
};
3048

31-
typedef int __attribute__((availability(swift, unavailable))) UIControlEvents;
32-
enum : UIControlEvents { UIControlEvents1, UIControlEvents2 };
49+
typedef CF_OPTIONS(NSUInteger, UIControlEvents) {
50+
UIControlEvents1,
51+
UIControlEvents2
52+
};
3353

34-
typedef int __attribute__((availability(swift, unavailable)))
35-
UITableViewScrollPosition;
36-
enum : UITableViewScrollPosition {
54+
typedef CF_OPTIONS(NSUInteger, UITableViewScrollPosition) {
3755
UITableViewScrollPosition1,
3856
UITableViewScrollPosition2
3957
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "CFOptions.h"
2+
3+
typedef CF_OPTIONS(unsigned, MyControlFlags) {
4+
MyControlFlagsNone = 0,
5+
MyControlFlagsFirst
6+
};

test/Interop/Cxx/objc-correctness/Inputs/module.modulemap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ module CxxClassWithNSStringInit {
99
requires cplusplus
1010
}
1111

12+
module CustomNSOptions {
13+
header "customNSOptions.h"
14+
}
15+
1216
module NSOptionsMangling {
1317
header "NSOptionsMangling.h"
1418
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %target-typecheck-verify-swift -verify-ignore-unknown -I %S/Inputs -enable-objc-interop -enable-experimental-cxx-interop
2+
// REQUIRES: objc_interop
3+
4+
import CustomNSOptions
5+
6+
let flags1: MyControlFlags = []
7+
let flags2: MyControlFlags = [.first]

0 commit comments

Comments
 (0)