Skip to content

Commit 1dec2f1

Browse files
committed
[Clang importer] After stripping a prefix, lowercase initialisms as well.
1 parent 1a2c0b0 commit 1dec2f1

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed

include/swift/Basic/StringExtras.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,16 @@ namespace swift {
241241
/// unchanged.
242242
StringRef toLowercaseWord(StringRef string, StringScratchSpace &scratch);
243243

244+
/// Lowercase the first word within the given camelCase string.
245+
///
246+
/// \param string The string to lowercase.
247+
/// \param scratch Scratch buffer used to form the resulting string.
248+
///
249+
/// \returns the string with the first word lowercased, including
250+
/// initialisms.
251+
StringRef toLowercaseInitialisms(StringRef string,
252+
StringScratchSpace &scratch);
253+
244254
/// Sentence-case the given camelCase string by turning the first
245255
/// letter into an uppercase letter.
246256
///

lib/Basic/StringExtras.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,8 @@ static bool isPluralSuffix(StringRef word) {
752752
return word == "s" || word == "es" || word == "ies";
753753
}
754754

755-
/// A form of toLowercaseWord that also lowercases acronyms.
756-
static StringRef toLowercaseWordAndAcronym(StringRef string,
757-
StringScratchSpace &scratch) {
755+
StringRef camel_case::toLowercaseInitialisms(StringRef string,
756+
StringScratchSpace &scratch) {
758757
if (string.empty())
759758
return string;
760759

@@ -765,7 +764,8 @@ static StringRef toLowercaseWordAndAcronym(StringRef string,
765764
// Lowercase until we hit the an uppercase letter followed by a
766765
// non-uppercase letter.
767766
llvm::SmallString<32> scratchStr;
768-
for (unsigned i = 0, n = string.size(); i != n; ++i) {
767+
scratchStr.push_back(clang::toLowercase(string[0]));
768+
for (unsigned i = 1, n = string.size(); i != n; ++i) {
769769
// If the next character is not uppercase, stop.
770770
if (i < n - 1 && !clang::isUppercase(string[i+1])) {
771771
// If the next non-uppercase character was not a letter, we seem
@@ -802,14 +802,14 @@ bool swift::omitNeedlessWords(StringRef &baseName,
802802
/// Local function that lowercases all of the base names and
803803
/// argument names before returning.
804804
auto lowercaseAcronymsForReturn = [&] {
805-
StringRef newBaseName = toLowercaseWordAndAcronym(baseName, scratch);
805+
StringRef newBaseName = toLowercaseInitialisms(baseName, scratch);
806806
if (baseName.data() != newBaseName.data()) {
807807
baseName = newBaseName;
808808
anyChanges = true;
809809
}
810810

811811
for (StringRef &argName : argNames) {
812-
StringRef newArgName = toLowercaseWordAndAcronym(argName, scratch);
812+
StringRef newArgName = toLowercaseInitialisms(argName, scratch);
813813
if (argName.data() != newArgName.data()) {
814814
argName = newArgName;
815815
anyChanges = true;

lib/ClangImporter/ClangImporter.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,9 +2571,11 @@ auto ClangImporter::Implementation::importFullName(
25712571
baseName = baseName.substr(prefixLen);
25722572

25732573
// If the result is a value, lowercase it.
2574-
if (isa<clang::ValueDecl>(D) && shouldLowercaseValueName(baseName))
2575-
baseName = camel_case::toLowercaseWord(baseName,
2576-
omitNeedlessWordsScratch);
2574+
if (isa<clang::ValueDecl>(D) && shouldLowercaseValueName(baseName)) {
2575+
baseName =
2576+
camel_case::toLowercaseInitialisms(baseName,
2577+
omitNeedlessWordsScratch);
2578+
}
25772579
}
25782580
}
25792581
}

test/IDE/print_omit_needless_words.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@
172172
// Don't leave just a '_'.
173173
// CHECK-FOUNDATION: func NS_()
174174

175+
// Lowercasing initialisms.
176+
// CHECK-FOUNDATION: let httpRequestKey: String
177+
175178
// Lowercasing initialisms with plurals.
176179
// CHECK-FOUNDATION: var urlsInText: [URL] { get }
177180

test/Inputs/clang-importer-sdk/usr/include/Foundation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,7 @@ extern void NS123(void);
996996
extern void NSYELLING(void);
997997
extern void NS_SCREAMING(void);
998998
extern void NS_(void);
999+
extern NSString *NSHTTPRequestKey;
9991000

10001001
@interface NSString (URLExtraction)
10011002
@property (nonnull,copy,readonly) NSArray<NSURL *> *URLsInText;

0 commit comments

Comments
 (0)