Skip to content

Commit 2b2252b

Browse files
committed
Gracefully handle incorrect SwiftName arg count
…at least in the specific case of initializers. Normally, clang validates that a SwiftNameAttr’s specified name seems to have the right number of argument labels for the declaration it’s applied to; if it doesn’t, it diagnoses and drops the attribute. However, this checking isn’t perfect because clang doesn’t know all of Clang Importer’s throwing rules. The upshot is that it’s possible to get a mismatched SwiftNameAttr into Clang Importer if the last parameter looks like an out parameter. This trips an assertion in asserts compilers, but release compilers get past that and don’t seem to notice the mismatch at all. Add code to the compiler to tolerate this condition, at least in initializers, by modifying the Swift name to have the correct number of argument labels and deprecating the declaration with a message explaining the problem. Fixes rdar://141124373.
1 parent eb7706d commit 2b2252b

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6674,6 +6674,7 @@ Decl *SwiftDeclConverter::importGlobalAsInitializer(
66746674
ArrayRef<Identifier> argNames = name.getArgumentNames();
66756675

66766676
ParameterList *parameterList = nullptr;
6677+
DeclName nameBeforeAdjustment;
66776678
if (argNames.size() == 1 && decl->getNumParams() == 0) {
66786679
// Special case: We need to create an empty first parameter for our
66796680
// argument label
@@ -6690,6 +6691,25 @@ Decl *SwiftDeclConverter::importGlobalAsInitializer(
66906691
dc, decl, {decl->param_begin(), decl->param_end()}, decl->isVariadic(),
66916692
allowNSUIntegerAsInt, argNames, /*genericParams=*/{},
66926693
/*resultType=*/nullptr, /*hasBoundsAnnotatedParam=*/nullptr);
6694+
6695+
if (name && parameterList && argNames.size() != parameterList->size()) {
6696+
// Remember that the name has changed.
6697+
nameBeforeAdjustment = name;
6698+
6699+
// Add or remove argument labels as needed to match `parameterList`.
6700+
SmallVector<Identifier, 16> newArgNames;
6701+
llvm::append_range(newArgNames, argNames);
6702+
while (newArgNames.size() > parameterList->size())
6703+
newArgNames.pop_back();
6704+
while (newArgNames.size() < parameterList->size()) {
6705+
auto param = parameterList->get(newArgNames.size());
6706+
newArgNames.push_back(param->getArgumentName());
6707+
}
6708+
6709+
// Construct the new name.
6710+
name = DeclName(Impl.SwiftContext, name.getBaseName(), newArgNames);
6711+
argNames = name.getArgumentNames();
6712+
}
66936713
}
66946714
if (!parameterList)
66956715
return nullptr;
@@ -6727,6 +6747,21 @@ Decl *SwiftDeclConverter::importGlobalAsInitializer(
67276747
result->setIsObjC(false);
67286748
result->setIsDynamic(false);
67296749

6750+
if (nameBeforeAdjustment) {
6751+
SmallString<16> message;
6752+
llvm::raw_svector_ostream os(message);
6753+
os << "declared Swift name '" << nameBeforeAdjustment
6754+
<< "' was adjusted to '" << name
6755+
<< "' because it does not have the correct number of parameters ("
6756+
<< nameBeforeAdjustment.getArgumentNames().size() << " vs. "
6757+
<< name.getArgumentNames().size()
6758+
<< "); please report this to its maintainer";
6759+
6760+
auto attr = AvailableAttr::createUniversallyDeprecated(Impl.SwiftContext,
6761+
Impl.SwiftContext.AllocateCopy(message.str()), "");
6762+
result->getAttrs().add(attr);
6763+
}
6764+
67306765
finishFuncDecl(decl, result);
67316766
if (correctSwiftName)
67326767
markAsVariant(result, *correctSwiftName);

test/ClangImporter/Inputs/custom-modules/SwiftName.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ typedef int my_int_t SWIFT_NAME(MyInt);
3232
void spuriousAPINotedSwiftName(int);
3333
void poorlyNamedFunction(const char *);
3434

35+
PointType readPoint(const char *path, void **errorOut) SWIFT_NAME(Point.init(path:));
36+
3537
struct BoxForConstants {
3638
int dummy;
3739
};

test/ClangImporter/attr-swift_name_renaming.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ func test() {
2222
var p = Point()
2323
var p2 = PointType() // FIXME: should provide Fix-It expected-error{{cannot find 'PointType' in scope}} {{none}}
2424

25+
// Initializers with incorrect argument count (rdar://141124373)
26+
var p3 = Point(path: "/dev/zero", nil) // expected-warning {{'init(path:_:)' is deprecated: declared Swift name 'init(path:)' was adjusted to 'init(path:_:)' because it does not have the correct number of parameters (1 vs. 2); please report this to its maintainer}}
27+
2528
// Field name remapping
2629
p.x = 7
2730

0 commit comments

Comments
 (0)