Skip to content

Commit a4d9082

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 #59421. This patch fixes some of this hanlding for when a multi-word suffix needs removing, specifically 'ControlEvents' and 'ScrollPosition'.
1 parent fb14414 commit a4d9082

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2589,7 +2589,11 @@ ArgumentAttrs ClangImporter::Implementation::inferDefaultArgument(
25892589
// behave like a C enum in the presence of C++.
25902590
auto enumName = typedefType->getDecl()->getName();
25912591
ArgumentAttrs argumentAttrs(DefaultArgumentKind::None, true, enumName);
2592-
for (auto word : llvm::reverse(camel_case::getWords(enumName))) {
2592+
auto camelCaseWords = camel_case::getWords(enumName);
2593+
for (auto it = camelCaseWords.rbegin(); it != camelCaseWords.rend();
2594+
++it) {
2595+
auto word = *it;
2596+
auto next = std::next(it);
25932597
if (camel_case::sameWordIgnoreFirstCase(word, "options")) {
25942598
argumentAttrs.argumentKind = DefaultArgumentKind::EmptyArray;
25952599
return argumentAttrs;
@@ -2600,13 +2604,17 @@ ArgumentAttrs ClangImporter::Implementation::inferDefaultArgument(
26002604
return argumentAttrs;
26012605
if (camel_case::sameWordIgnoreFirstCase(word, "action"))
26022606
return argumentAttrs;
2603-
if (camel_case::sameWordIgnoreFirstCase(word, "controlevents"))
2607+
if (camel_case::sameWordIgnoreFirstCase(word, "events") &&
2608+
next != camelCaseWords.rend() &&
2609+
camel_case::sameWordIgnoreFirstCase(*next, "control"))
26042610
return argumentAttrs;
26052611
if (camel_case::sameWordIgnoreFirstCase(word, "state"))
26062612
return argumentAttrs;
26072613
if (camel_case::sameWordIgnoreFirstCase(word, "unit"))
26082614
return argumentAttrs;
2609-
if (camel_case::sameWordIgnoreFirstCase(word, "scrollposition"))
2615+
if (camel_case::sameWordIgnoreFirstCase(word, "position") &&
2616+
next != camelCaseWords.rend() &&
2617+
camel_case::sameWordIgnoreFirstCase(*next, "scroll"))
26102618
return argumentAttrs;
26112619
if (camel_case::sameWordIgnoreFirstCase(word, "edge"))
26122620
return argumentAttrs;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,29 @@ 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)))
35+
UITableViewScrollPosition;
36+
enum : UITableViewScrollPosition {
37+
UITableViewScrollPosition1,
38+
UITableViewScrollPosition2
39+
};
40+
41+
@interface NSIndexPath
42+
@end
43+
3144
@interface TestsForEnhancedOmitNeedlessWords
3245
- (void)differenceFromArray:(int)other withOptions:(NSOrderedCollectionDifferenceCalculationOptions)options ;
3346
- (unsigned)minimumRangeOfUnit:(NSCalendarUnit)unit;
3447
- (unsigned)URLForDirectory:(unsigned)directory inDomain:(NSSearchPathDomainMask)domain ;
3548
- (unsigned)layoutManager:(unsigned)layoutManager shouldUseAction:(NSControlCharacterAction)action ;
3649
- (void)setBackButtonBackgroundImage:(unsigned)backgroundImage forState:(UIControlState)state ;
3750
- (void)willTransitionToState:(UITableViewCellStateMask)state ;
51+
- (void)addTarget:(nullable id)target
52+
action:(SEL)action
53+
forControlEvents:(UIControlEvents)controlEvents;
54+
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath
55+
atScrollPosition:(UITableViewScrollPosition)scrollPosition;
3856
@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)
71+
// CHECK-NEXT: func scrollToRow(at indexPath: NSIndexPath!, at scrollPosition: UITableViewScrollPosition)
72+
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "scrollToRow(at:at:)")
73+
// CHECK-NEXT: class func scrollToRowAtIndexPath(_ indexPath: NSIndexPath!, atScrollPosition scrollPosition: UITableViewScrollPosition)
74+
// CHECK-NEXT: @available(swift, obsoleted: 3, renamed: "scrollToRow(at:at:)")
75+
// CHECK-NEXT: func scrollToRowAtIndexPath(_ indexPath: NSIndexPath!, atScrollPosition scrollPosition: UITableViewScrollPosition)

0 commit comments

Comments
 (0)