Skip to content

Commit ca641d5

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 3542af9 commit ca641d5

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

lib/ClangImporter/ImportDecl.cpp

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

65406540
ParameterList *parameterList = nullptr;
6541+
DeclName nameBeforeAdjustment;
65416542
if (argNames.size() == 1 && decl->getNumParams() == 0) {
65426543
// Special case: We need to create an empty first parameter for our
65436544
// argument label
@@ -6554,6 +6555,25 @@ Decl *SwiftDeclConverter::importGlobalAsInitializer(
65546555
dc, decl, {decl->param_begin(), decl->param_end()}, decl->isVariadic(),
65556556
allowNSUIntegerAsInt, argNames, /*genericParams=*/{},
65566557
/*resultType=*/nullptr, /*hasBoundsAnnotatedParam=*/nullptr);
6558+
6559+
if (name && parameterList && argNames.size() != parameterList->size()) {
6560+
// Remember that the name has changed.
6561+
nameBeforeAdjustment = name;
6562+
6563+
// Add or remove argument labels as needed to match `parameterList`.
6564+
SmallVector<Identifier, 16> newArgNames;
6565+
llvm::append_range(newArgNames, argNames);
6566+
while (newArgNames.size() > parameterList->size())
6567+
newArgNames.pop_back();
6568+
while (newArgNames.size() < parameterList->size()) {
6569+
auto param = parameterList->get(newArgNames.size());
6570+
newArgNames.push_back(param->getArgumentName());
6571+
}
6572+
6573+
// Construct the new name.
6574+
name = DeclName(Impl.SwiftContext, name.getBaseName(), newArgNames);
6575+
argNames = name.getArgumentNames();
6576+
}
65576577
}
65586578
if (!parameterList)
65596579
return nullptr;
@@ -6591,6 +6611,22 @@ Decl *SwiftDeclConverter::importGlobalAsInitializer(
65916611
result->setIsObjC(false);
65926612
result->setIsDynamic(false);
65936613

6614+
if (nameBeforeAdjustment) {
6615+
SmallString<16> message;
6616+
llvm::raw_svector_ostream os(message);
6617+
os << "declared Swift name '" << nameBeforeAdjustment
6618+
<< "' was adjusted to '" << name
6619+
<< "' because it does not have the correct number of parameters ("
6620+
<< nameBeforeAdjustment.getArgumentNames().size() << " vs. "
6621+
<< name.getArgumentNames().size()
6622+
<< "); please report this to its maintainer";
6623+
6624+
auto attr = AvailableAttr::createPlatformAgnostic(Impl.SwiftContext,
6625+
Impl.SwiftContext.AllocateCopy(message.str()), "",
6626+
PlatformAgnosticAvailabilityKind::Deprecated);
6627+
result->getAttrs().add(attr);
6628+
}
6629+
65946630
finishFuncDecl(decl, result);
65956631
if (correctSwiftName)
65966632
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)