Skip to content

Commit d9d0be2

Browse files
committed
Fix NS_OPTION parameter label transformation for multi-word suffixes
When Objective-c methods are imported into Swift, the labels are transforms to remove suffixes based on the type of the parameter. Due to the structure of NS_OPTION when importing in C++ mode, there is some special handling for this case introduced in PR swiftlang#59421. This patch fixes some of this hanlding for when a multi-word suffix needs removing, specifically 'ControlEvents' and 'ScrollPosition'.
1 parent 930d74c commit d9d0be2

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2588,7 +2588,10 @@ ArgumentAttrs ClangImporter::Implementation::inferDefaultArgument(
25882588
// behave like a C enum in the presence of C++.
25892589
auto enumName = typedefType->getDecl()->getName();
25902590
ArgumentAttrs argumentAttrs(DefaultArgumentKind::None, true, enumName);
2591-
for (auto word : llvm::reverse(camel_case::getWords(enumName))) {
2591+
auto camelCaseWords = camel_case::getWords(enumName);
2592+
for (auto it = camelCaseWords.rbegin(); it != camelCaseWords.rend(); ++it) {
2593+
auto word = *it;
2594+
auto next = std::next(it);
25922595
if (camel_case::sameWordIgnoreFirstCase(word, "options")) {
25932596
argumentAttrs.argumentKind = DefaultArgumentKind::EmptyArray;
25942597
return argumentAttrs;
@@ -2599,13 +2602,13 @@ ArgumentAttrs ClangImporter::Implementation::inferDefaultArgument(
25992602
return argumentAttrs;
26002603
if (camel_case::sameWordIgnoreFirstCase(word, "action"))
26012604
return argumentAttrs;
2602-
if (camel_case::sameWordIgnoreFirstCase(word, "controlevents"))
2605+
if (camel_case::sameWordIgnoreFirstCase(word, "events") && next != camelCaseWords.rend() && camel_case::sameWordIgnoreFirstCase(*next, "control"))
26032606
return argumentAttrs;
26042607
if (camel_case::sameWordIgnoreFirstCase(word, "state"))
26052608
return argumentAttrs;
26062609
if (camel_case::sameWordIgnoreFirstCase(word, "unit"))
26072610
return argumentAttrs;
2608-
if (camel_case::sameWordIgnoreFirstCase(word, "scrollposition"))
2611+
if (camel_case::sameWordIgnoreFirstCase(word, "position") && next != camelCaseWords.rend() && camel_case::sameWordIgnoreFirstCase(*next, "scroll"))
26092612
return argumentAttrs;
26102613
if (camel_case::sameWordIgnoreFirstCase(word, "edge"))
26112614
return argumentAttrs;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,23 @@ enum : UIControlState { UIControlState1, UIControlState2 };
2828
typedef int __attribute__((availability(swift, unavailable))) UITableViewCellStateMask;
2929
enum : UITableViewCellStateMask { UITableViewCellStateMask1, UITableViewCellStateMask2 };
3030

31+
typedef int __attribute__((availability(swift, unavailable))) UIControlEvents;
32+
enum : UIControlEvents { UIControlEvents1, UIControlEvents2 };
33+
34+
typedef int __attribute__((availability(swift, unavailable))) UITableViewScrollPosition;
35+
enum : UITableViewScrollPosition { UITableViewScrollPosition1, UITableViewScrollPosition2 };
36+
37+
@interface NSIndexPath
38+
@end
39+
typedef signed char BOOL;
40+
3141
@interface TestsForEnhancedOmitNeedlessWords
3242
- (void)differenceFromArray:(int)other withOptions:(NSOrderedCollectionDifferenceCalculationOptions)options ;
3343
- (unsigned)minimumRangeOfUnit:(NSCalendarUnit)unit;
3444
- (unsigned)URLForDirectory:(unsigned)directory inDomain:(NSSearchPathDomainMask)domain ;
3545
- (unsigned)layoutManager:(unsigned)layoutManager shouldUseAction:(NSControlCharacterAction)action ;
3646
- (void)setBackButtonBackgroundImage:(unsigned)backgroundImage forState:(UIControlState)state ;
3747
- (void)willTransitionToState:(UITableViewCellStateMask)state ;
48+
- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
49+
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
3850
@end

test/Interop/Cxx/enum/c-enums-withOptions-omit.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,20 @@ import CenumsWithOptionsOmit
5656
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "willTransition(to:)")
5757
// CHECK-NEXT: class func willTransitionToState(_ state: UITableViewCellStateMask)
5858
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "willTransition(to:)")
59-
// CHECK-NEXT: func willTransitionToState(_ state: UITableViewCellStateMask)
59+
// CHECK-NEXT: func willTransitionToState(_ state: UITableViewCellStateMask)
60+
61+
// Tests for forControlEvents -> 'for controlEvents'
62+
// CHECK-NEXT: class func addTarget(_ target: Any?, action: OpaquePointer!, for controlEvents: UIControlEvents)
63+
// CHECK-NEXT: func addTarget(_ target: Any?, action: OpaquePointer!, for controlEvents: UIControlEvents)
64+
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "addTarget(_:action:for:)")
65+
// CHECK-NEXT: class func addTarget(_ target: Any?, action: OpaquePointer!, forControlEvents controlEvents: UIControlEvents)
66+
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "addTarget(_:action:for:)")
67+
// CHECK-NEXT: func addTarget(_ target: Any?, action: OpaquePointer!, forControlEvents controlEvents: UIControlEvents)
68+
69+
// Tests for atScrollPosition -> 'at atScrollPosition'
70+
// CHECK-NEXT: class func scrollToRow(at indexPath: NSIndexPath!, at scrollPosition: UITableViewScrollPosition, animated: BOOL)
71+
// CHECK-NEXT: func scrollToRow(at indexPath: NSIndexPath!, at scrollPosition: UITableViewScrollPosition, animated: BOOL)
72+
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "scrollToRow(at:at:animated:)")
73+
// CHECK-NEXT: class func scrollToRowAtIndexPath(_ indexPath: NSIndexPath!, atScrollPosition scrollPosition: UITableViewScrollPosition, animated: BOOL)
74+
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "scrollToRow(at:at:animated:)")
75+
// CHECK-NEXT: func scrollToRowAtIndexPath(_ indexPath: NSIndexPath!, atScrollPosition scrollPosition: UITableViewScrollPosition, animated: BOOL)

0 commit comments

Comments
 (0)