Skip to content

Commit 1e43256

Browse files
committed
Clang importer: Swift 2 lowercased subsequent argument names coming from Objective-C selectors. Mimic this.
1 parent d9ce07f commit 1e43256

File tree

5 files changed

+34
-13
lines changed

5 files changed

+34
-13
lines changed

include/swift/Basic/StringExtras.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ namespace swift {
5151
/// Determine the part of speech for the given word.
5252
PartOfSpeech getPartOfSpeech(StringRef word);
5353

54+
/// Scratch space used for returning a set of StringRefs.
55+
class StringScratchSpace {
56+
llvm::BumpPtrAllocator Allocator;
57+
58+
public:
59+
StringRef copyString(StringRef string);
60+
};
61+
5462
namespace camel_case {
5563
class WordIterator;
5664

@@ -219,6 +227,16 @@ namespace swift {
219227
/// unchanged.
220228
StringRef toLowercaseWord(StringRef string, SmallVectorImpl<char> &scratch);
221229

230+
/// Lowercase the first word within the given camelCase string.
231+
///
232+
/// \param string The string to lowercase.
233+
/// \param scratch Scratch buffer used to form the resulting string.
234+
///
235+
/// \returns the string with the first word lowercased. When the
236+
/// first word is an acronym, the string will be returned
237+
/// unchanged.
238+
StringRef toLowercaseWord(StringRef string, StringScratchSpace &scratch);
239+
222240
/// Sentence-case the given camelCase string by turning the first
223241
/// letter into an uppercase letter.
224242
///
@@ -358,14 +376,6 @@ struct OmissionTypeName {
358376
/// would produce "ByAppendingString".
359377
StringRef matchLeadingTypeName(StringRef name, OmissionTypeName typeName);
360378

361-
/// Scratch space used for returning a set of StringRefs.
362-
class StringScratchSpace {
363-
llvm::BumpPtrAllocator Allocator;
364-
365-
public:
366-
StringRef copyString(StringRef string);
367-
};
368-
369379
/// Describes a set of names with an inheritance relationship.
370380
class InheritedNameSet {
371381
const InheritedNameSet *Parent;

lib/Basic/StringExtras.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,10 @@ bool InheritedNameSet::contains(StringRef name) const {
442442
}
443443

444444
/// Wrapper for camel_case::toLowercaseWord that uses string scratch space.
445-
static StringRef toLowercaseWord(StringRef string, StringScratchSpace &scratch){
445+
StringRef camel_case::toLowercaseWord(StringRef string,
446+
StringScratchSpace &scratch){
446447
llvm::SmallString<32> scratchStr;
447-
StringRef result = camel_case::toLowercaseWord(string, scratchStr);
448+
StringRef result = toLowercaseWord(string, scratchStr);
448449
if (string == result)
449450
return string;
450451

lib/ClangImporter/ClangImporter.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2150,6 +2150,7 @@ auto ClangImporter::Implementation::importFullName(
21502150
StringRef baseName;
21512151
SmallVector<StringRef, 4> argumentNames;
21522152
SmallString<16> selectorSplitScratch;
2153+
StringScratchSpace stringScratch;
21532154
ArrayRef<const clang::ParmVarDecl *> params;
21542155
switch (D->getDeclName().getNameKind()) {
21552156
case clang::DeclarationName::CXXConstructorName:
@@ -2205,7 +2206,15 @@ auto ClangImporter::Implementation::importFullName(
22052206
if (index == 0) {
22062207
argumentNames.push_back(StringRef());
22072208
} else {
2208-
argumentNames.push_back(selector.getNameForSlot(index));
2209+
StringRef argName = selector.getNameForSlot(index);
2210+
2211+
// Swift 2 lowercased all subsequent argument names.
2212+
// Swift 3 may handle this as part of omitting needless words, below,
2213+
// but don't preempt that here.
2214+
if (!OmitNeedlessWords)
2215+
argName = camel_case::toLowercaseWord(argName, stringScratch);
2216+
2217+
argumentNames.push_back(argName);
22092218
}
22102219
}
22112220

test/IDE/Inputs/swift_name_objc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ SWIFT_NAME(SomeClass)
1717
@interface SNSomeClass : NSObject
1818
- (instancetype)initWithFloat:(float)f;
1919
- (instancetype)initWithDefault;
20-
- (void)instanceMethodWithX:(float)x y:(float)y z:(float)z;
20+
- (void)instanceMethodWithX:(float)x Y:(float)y Z:(float)z;
2121
+ (instancetype)someClassWithDouble:(double)d;
2222
+ (instancetype)someClassWithTry:(BOOL)shouldTry;
2323
+ (NSObject *)buildWithObject:(NSObject *)object SWIFT_NAME(init(object:));

test/IDE/dump_swift_lookup_tables_objc.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
// CHECK-NEXT: init(withTry:):
8585
// CHECK-NEXT: SNSomeClass: +[SNSomeClass someClassWithTry:]
8686
// CHECK-NEXT: instanceMethodWithX(_:y:z:):
87-
// CHECK-NEXT: SNSomeClass: -[SNSomeClass instanceMethodWithX:y:z:]
87+
// CHECK-NEXT: SNSomeClass: -[SNSomeClass instanceMethodWithX:Y:Z:]
8888
// CHECK-NEXT: method():
8989
// CHECK-NEXT: NSErrorImports: -[NSErrorImports methodAndReturnError:]
9090
// CHECK-NEXT: methodWithFloat(_:):
@@ -95,6 +95,7 @@
9595
// CHECK-NEXT: NSAccessibility: -[NSAccessibility setAccessibilityFloat:]
9696

9797
// CHECK-OMIT-NEEDLESS-WORDS: Base -> full name mappings:
98+
// CHECK-OMIT-NEEDLESS-WORDS: instanceMethodWithX --> instanceMethodWithX(_:y:z:)
9899
// CHECK-OMIT-NEEDLESS-WORDS: methodWith --> methodWith(_:)
99100

100101
// CHECK-OMIT-NEEDLESS-WORDS: Full name -> entry mappings:

0 commit comments

Comments
 (0)