Skip to content

Commit 50eea9c

Browse files
committed
Clang importer: handle omit-needless-words for methods in importFullName.
1 parent 2d7044c commit 50eea9c

File tree

4 files changed

+71
-88
lines changed

4 files changed

+71
-88
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,7 @@ auto ClangImporter::Implementation::importFullName(
21462146
StringRef baseName;
21472147
SmallVector<StringRef, 4> argumentNames;
21482148
SmallString<16> selectorSplitScratch;
2149+
ArrayRef<const clang::ParmVarDecl *> params;
21492150
switch (D->getDeclName().getNameKind()) {
21502151
case clang::DeclarationName::CXXConstructorName:
21512152
case clang::DeclarationName::CXXConversionFunctionName:
@@ -2185,10 +2186,7 @@ auto ClangImporter::Implementation::importFullName(
21852186
baseName = selector.getNameForSlot(0);
21862187

21872188
// Get the parameters.
2188-
ArrayRef<const clang::ParmVarDecl *> params{
2189-
objcMethod->param_begin(),
2190-
objcMethod->param_end()
2191-
};
2189+
params = { objcMethod->param_begin(), objcMethod->param_end() };
21922190

21932191
// If we have a variadic method for which we need to drop the last
21942192
// selector piece, do so now.
@@ -2378,6 +2376,23 @@ auto ClangImporter::Implementation::importFullName(
23782376
omitNeedlessWordsScratch);
23792377
}
23802378
}
2379+
2380+
// Objective-C methods.
2381+
if (auto method = dyn_cast<clang::ObjCMethodDecl>(D)) {
2382+
(void)omitNeedlessWordsInFunctionName(
2383+
baseName,
2384+
argumentNames,
2385+
params,
2386+
method->getReturnType(),
2387+
method->getDeclContext(),
2388+
getNonNullArgs(method, params),
2389+
getKnownObjCMethod(method),
2390+
result.ErrorInfo ? Optional<unsigned>(result.ErrorInfo->ParamIndex)
2391+
: None,
2392+
method->hasRelatedResultType(),
2393+
method->isInstanceMethod(),
2394+
omitNeedlessWordsScratch);
2395+
}
23812396
}
23822397

23832398
// If this declaration has the swift_private attribute, prepend "__" to the

lib/ClangImporter/ImportType.cpp

Lines changed: 23 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "clang/AST/Decl.h"
3535
#include "clang/AST/DeclObjC.h"
3636
#include "clang/AST/TypeVisitor.h"
37-
#include "llvm/ADT/SmallBitVector.h"
3837
#include "llvm/ADT/SmallString.h"
3938
#include "llvm/ADT/StringExtras.h"
4039

@@ -1246,9 +1245,9 @@ Type ClangImporter::Implementation::importPropertyType(
12461245

12471246
/// Get a bit vector indicating which arguments are non-null for a
12481247
/// given function or method.
1249-
static llvm::SmallBitVector getNonNullArgs(
1250-
const clang::Decl *decl,
1251-
ArrayRef<const clang::ParmVarDecl *> params) {
1248+
llvm::SmallBitVector ClangImporter::Implementation::getNonNullArgs(
1249+
const clang::Decl *decl,
1250+
ArrayRef<const clang::ParmVarDecl *> params) {
12521251
llvm::SmallBitVector result;
12531252
if (!decl)
12541253
return result;
@@ -1705,27 +1704,18 @@ OmissionTypeName ClangImporter::Implementation::getClangTypeNameForOmission(
17051704
}
17061705

17071706
/// Attempt to omit needless words from the given function name.
1708-
DeclName ClangImporter::Implementation::omitNeedlessWordsInFunctionName(
1709-
DeclName name,
1710-
ArrayRef<const clang::ParmVarDecl *> params,
1711-
clang::QualType resultType,
1712-
const clang::DeclContext *dc,
1713-
const llvm::SmallBitVector &nonNullArgs,
1714-
const Optional<api_notes::ObjCMethodInfo> &knownMethod,
1715-
Optional<unsigned> errorParamIndex,
1716-
bool returnsSelf,
1717-
bool isInstanceMethod) {
1718-
ASTContext &ctx = SwiftContext;
1719-
1720-
// Collect the argument names.
1721-
SmallVector<StringRef, 4> argNames;
1722-
for (auto arg : name.getArgumentNames()) {
1723-
if (arg.empty())
1724-
argNames.push_back("");
1725-
else
1726-
argNames.push_back(arg.str());
1727-
}
1728-
1707+
bool ClangImporter::Implementation::omitNeedlessWordsInFunctionName(
1708+
StringRef &baseName,
1709+
SmallVectorImpl<StringRef> &argumentNames,
1710+
ArrayRef<const clang::ParmVarDecl *> params,
1711+
clang::QualType resultType,
1712+
const clang::DeclContext *dc,
1713+
const llvm::SmallBitVector &nonNullArgs,
1714+
const Optional<api_notes::ObjCMethodInfo> &knownMethod,
1715+
Optional<unsigned> errorParamIndex,
1716+
bool returnsSelf,
1717+
bool isInstanceMethod,
1718+
StringScratchSpace &scratch) {
17291719
// Collect the parameter type names.
17301720
StringRef firstParamName;
17311721
SmallVector<OmissionTypeName, 4> paramTypes;
@@ -1752,21 +1742,17 @@ DeclName ClangImporter::Implementation::omitNeedlessWordsInFunctionName(
17521742
param->getType(),
17531743
getParamOptionality(param,
17541744
!nonNullArgs.empty() && nonNullArgs[i],
1755-
knownMethod
1745+
knownMethod && knownMethod->NullabilityAudited
17561746
? Optional<clang::NullabilityKind>(
17571747
knownMethod->getParamTypeInfo(i))
17581748
: None),
1759-
name.getBaseName(), numParams,
1749+
SwiftContext.getIdentifier(baseName), numParams,
17601750
isLastParameter);
17611751

17621752
paramTypes.push_back(getClangTypeNameForOmission(param->getType())
17631753
.withDefaultArgument(hasDefaultArg));
17641754
}
17651755

1766-
// Omit needless words.
1767-
StringRef baseName = name.getBaseName().str();
1768-
StringScratchSpace scratch;
1769-
17701756
// Find the property names.
17711757
const InheritedNameSet *allPropertyNames = nullptr;
17721758
auto contextType = getClangDeclContextType(dc);
@@ -1777,35 +1763,12 @@ DeclName ClangImporter::Implementation::omitNeedlessWordsInFunctionName(
17771763
isInstanceMethod);
17781764
}
17791765

1780-
if (!omitNeedlessWords(baseName, argNames, firstParamName,
1781-
getClangTypeNameForOmission(resultType),
1782-
getClangTypeNameForOmission(contextType),
1783-
paramTypes, returnsSelf, /*isProperty=*/false,
1784-
allPropertyNames, scratch))
1785-
return name;
1786-
1787-
/// Retrieve a replacement identifier.
1788-
auto getReplacementIdentifier = [&](StringRef name,
1789-
Identifier old) -> Identifier{
1790-
if (name.empty())
1791-
return Identifier();
1792-
1793-
if (!old.empty() && name == old.str())
1794-
return old;
1795-
1796-
return ctx.getIdentifier(name);
1797-
};
1798-
1799-
Identifier newBaseName = getReplacementIdentifier(baseName,
1800-
name.getBaseName());
1801-
SmallVector<Identifier, 4> newArgNames;
1802-
auto oldArgNames = name.getArgumentNames();
1803-
for (unsigned i = 0, n = argNames.size(); i != n; ++i) {
1804-
newArgNames.push_back(getReplacementIdentifier(argNames[i],
1805-
oldArgNames[i]));
1806-
}
1807-
1808-
return DeclName(ctx, newBaseName, newArgNames);
1766+
// Omit needless words.
1767+
return omitNeedlessWords(baseName, argumentNames, firstParamName,
1768+
getClangTypeNameForOmission(resultType),
1769+
getClangTypeNameForOmission(contextType),
1770+
paramTypes, returnsSelf, /*isProperty=*/false,
1771+
allPropertyNames, scratch);
18091772
}
18101773

18111774
/// Retrieve the instance type of the given Clang declaration context.
@@ -2035,20 +1998,6 @@ Type ClangImporter::Implementation::importMethodType(
20351998

20361999
llvm::SmallBitVector nonNullArgs = getNonNullArgs(clangDecl, params);
20372000

2038-
// If we should omit needless words and don't have a custom name, do so.
2039-
if (OmitNeedlessWords && !importedName.HasCustomName && clangDecl &&
2040-
(kind == SpecialMethodKind::Regular ||
2041-
kind == SpecialMethodKind::Constructor)) {
2042-
methodName = omitNeedlessWordsInFunctionName(
2043-
methodName, params, resultType,
2044-
clangDecl->getDeclContext(),
2045-
nonNullArgs,
2046-
knownMethod,
2047-
errorInfo ? Optional<unsigned>(errorInfo->ParamIndex) : None,
2048-
clangDecl->hasRelatedResultType(),
2049-
clangDecl->isInstanceMethod());
2050-
}
2051-
20522001
// Import the parameters.
20532002
SmallVector<TupleTypeElt, 4> swiftArgParams;
20542003
SmallVector<TupleTypeElt, 4> swiftBodyParams;

lib/ClangImporter/ImporterImpl.h

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "llvm/ADT/APSInt.h"
3333
#include "llvm/ADT/DenseMap.h"
3434
#include "llvm/ADT/IntrusiveRefCntPtr.h"
35+
#include "llvm/ADT/SmallBitVector.h"
3536
#include "llvm/ADT/SmallSet.h"
3637
#include "llvm/ADT/TinyPtrVector.h"
3738
#include <set>
@@ -730,16 +731,18 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
730731
OmissionTypeName getClangTypeNameForOmission(clang::QualType type);
731732

732733
/// Omit needless words in a function name.
733-
DeclName omitNeedlessWordsInFunctionName(
734-
DeclName name,
735-
ArrayRef<const clang::ParmVarDecl *> params,
736-
clang::QualType resultType,
737-
const clang::DeclContext *dc,
738-
const llvm::SmallBitVector &nonNullArgs,
739-
const Optional<api_notes::ObjCMethodInfo> &knownMethod,
740-
Optional<unsigned> errorParamIndex,
741-
bool returnsSelf,
742-
bool isInstanceMethod);
734+
bool omitNeedlessWordsInFunctionName(
735+
StringRef &baseName,
736+
SmallVectorImpl<StringRef> &argumentNames,
737+
ArrayRef<const clang::ParmVarDecl *> params,
738+
clang::QualType resultType,
739+
const clang::DeclContext *dc,
740+
const llvm::SmallBitVector &nonNullArgs,
741+
const Optional<api_notes::ObjCMethodInfo> &knownMethod,
742+
Optional<unsigned> errorParamIndex,
743+
bool returnsSelf,
744+
bool isInstanceMethod,
745+
StringScratchSpace &scratch);
743746

744747
/// \brief Converts the given Swift identifier for Clang.
745748
clang::DeclarationName exportName(Identifier name);
@@ -1108,6 +1111,12 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
11081111
unsigned numParams,
11091112
bool isLastParameter);
11101113

1114+
/// Retrieve a bit vector containing the non-null argument
1115+
/// annotations for the given declaration.
1116+
llvm::SmallBitVector getNonNullArgs(
1117+
const clang::Decl *decl,
1118+
ArrayRef<const clang::ParmVarDecl *> params);
1119+
11111120
/// \brief Import the type of an Objective-C method.
11121121
///
11131122
/// This routine should be preferred when importing function types for

test/IDE/dump_swift_lookup_tables_objc.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// RUN: %target-swift-ide-test -dump-importer-lookup-table -source-filename %s -import-objc-header %S/Inputs/swift_name_objc.h > %t.log 2>&1
22
// RUN: FileCheck %s < %t.log
33

4+
// RUN: %target-swift-ide-test -dump-importer-lookup-table -source-filename %s -import-objc-header %S/Inputs/swift_name_objc.h -enable-omit-needless-words > %t-omit-needless-words.log 2>&1
5+
// RUN: FileCheck -check-prefix=CHECK-OMIT-NEEDLESS-WORDS %s < %t-omit-needless-words.log
6+
47
// REQUIRES: objc_interop
58

69
// CHECK: Base -> full name mappings:
@@ -78,3 +81,10 @@
7881
// CHECK-NEXT: SNSomeProtocol: -[SNSomeProtocol protoInstanceMethodWithX:y:]
7982
// CHECK-NEXT: setAccessibilityFloat(_:):
8083
// CHECK-NEXT: NSAccessibility: -[NSAccessibility setAccessibilityFloat:]
84+
85+
// CHECK-OMIT-NEEDLESS-WORDS: Base -> full name mappings:
86+
// CHECK-OMIT-NEEDLESS-WORDS: methodWith --> methodWith(_:)
87+
88+
// CHECK-OMIT-NEEDLESS-WORDS: Full name -> entry mappings:
89+
// CHECK-OMIT-NEEDLESS-WORDS: methodWith(_:):
90+
// CHECK-OMIT-NEEDLESS-WORDS: NSErrorImports: -[NSErrorImports methodWithFloat:error:]

0 commit comments

Comments
 (0)