Skip to content

Commit a3ce462

Browse files
authored
Merge pull request #32214 from akyrtzi/clang-include-inherited-convenience-inits-for-members
[ClangImporter] Make sure that inherited convenience constructors are included in members of `IterableDeclContext`
2 parents 4205e46 + 915e215 commit a3ce462

File tree

4 files changed

+371
-10
lines changed

4 files changed

+371
-10
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4306,12 +4306,12 @@ namespace {
43064306
// "raw" name will be imported as unavailable with a more helpful and
43074307
// specific message.
43084308
++NumFactoryMethodsAsInitializers;
4309-
bool redundant = false;
4309+
ConstructorDecl *existing = nullptr;
43104310
auto result =
43114311
importConstructor(decl, dc, false, importedName.getInitKind(),
43124312
/*required=*/false, selector, importedName,
43134313
{decl->param_begin(), decl->param_size()},
4314-
decl->isVariadic(), redundant);
4314+
decl->isVariadic(), existing);
43154315

43164316
if (!isActiveSwiftVersion() && result)
43174317
markAsVariant(result, *correctSwiftName);
@@ -4555,7 +4555,7 @@ namespace {
45554555
ImportedName importedName,
45564556
ArrayRef<const clang::ParmVarDecl*> args,
45574557
bool variadic,
4558-
bool &redundant);
4558+
ConstructorDecl *&existing);
45594559

45604560
void recordObjCOverride(SubscriptDecl *subscript);
45614561

@@ -6234,11 +6234,11 @@ ConstructorDecl *SwiftDeclConverter::importConstructor(
62346234
variadic = false;
62356235
}
62366236

6237-
bool redundant;
6237+
ConstructorDecl *existing;
62386238
auto result = importConstructor(objcMethod, dc, implicit,
62396239
kind.getValueOr(importedName.getInitKind()),
62406240
required, selector, importedName, params,
6241-
variadic, redundant);
6241+
variadic, existing);
62426242

62436243
// If this is a compatibility stub, mark it as such.
62446244
if (result && correctSwiftName)
@@ -6350,8 +6350,8 @@ ConstructorDecl *SwiftDeclConverter::importConstructor(
63506350
const clang::ObjCMethodDecl *objcMethod, const DeclContext *dc, bool implicit,
63516351
CtorInitializerKind kind, bool required, ObjCSelector selector,
63526352
ImportedName importedName, ArrayRef<const clang::ParmVarDecl *> args,
6353-
bool variadic, bool &redundant) {
6354-
redundant = false;
6353+
bool variadic, ConstructorDecl *&existing) {
6354+
existing = nullptr;
63556355

63566356
// Figure out the type of the container.
63576357
auto ownerNominal = dc->getSelfNominalTypeDecl();
@@ -6451,7 +6451,7 @@ ConstructorDecl *SwiftDeclConverter::importConstructor(
64516451

64526452
// Otherwise, we shouldn't create a new constructor, because
64536453
// it will be no better than the existing one.
6454-
redundant = true;
6454+
existing = ctor;
64556455
return nullptr;
64566456
}
64576457

@@ -7388,19 +7388,26 @@ void SwiftDeclConverter::importInheritedConstructors(
73887388
!correctSwiftName &&
73897389
"Import inherited initializers never references correctSwiftName");
73907390
importedName.setHasCustomName();
7391-
bool redundant;
7391+
ConstructorDecl *existing;
73927392
if (auto newCtor =
73937393
importConstructor(objcMethod, classDecl,
73947394
/*implicit=*/true, ctor->getInitKind(),
73957395
/*required=*/false, ctor->getObjCSelector(),
73967396
importedName, objcMethod->parameters(),
7397-
objcMethod->isVariadic(), redundant)) {
7397+
objcMethod->isVariadic(), existing)) {
73987398
// If this is a compatibility stub, mark it as such.
73997399
if (correctSwiftName)
74007400
markAsVariant(newCtor, *correctSwiftName);
74017401

74027402
Impl.importAttributes(objcMethod, newCtor, curObjCClass);
74037403
newMembers.push_back(newCtor);
7404+
} else if (existing && existing->getClangDecl()) {
7405+
// Check that the existing constructor the prevented new creation is
7406+
// really an inherited factory initializer and not a class member.
7407+
auto existingMD = cast<clang::ObjCMethodDecl>(existing->getClangDecl());
7408+
if (existingMD->getClassInterface() != curObjCClass) {
7409+
newMembers.push_back(existing);
7410+
}
74047411
}
74057412
continue;
74067413
}

test/api-digester/Inputs/Foo-new-version/foo.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,13 @@
1717
@interface ClangInterface: NSObject <ObjcProt>
1818
- (void)someFunction;
1919
@end
20+
21+
@interface PhotoSettings: NSObject
22+
+ (instancetype)photoSettingsWithFormat:(int)format;
23+
+ (instancetype)photoSettingsWithNumber:(int)number;
24+
@end
25+
26+
@interface PhotoBracketSettings : PhotoSettings
27+
+ (instancetype)photoBracketSettingsWithRawPixelFormatType:(int)rawPixelFormatType processedFormat:(int)processedFormat;
28+
+ (instancetype)photoBracketSettingsWithNumber:(int)number;
29+
@end

test/api-digester/Inputs/Foo/foo.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,13 @@
1111
@interface ClangInterface: NSObject <ObjcProt>
1212
- (void)someFunction;
1313
@end
14+
15+
@interface PhotoSettings: NSObject
16+
+ (instancetype)photoSettingsWithFormat:(int)format;
17+
+ (instancetype)photoSettingsWithNumber:(int)number;
18+
@end
19+
20+
@interface PhotoBracketSettings : PhotoSettings
21+
+ (instancetype)photoBracketSettingsWithRawPixelFormatType:(int)rawPixelFormatType processedFormat:(int)processedFormat;
22+
+ (instancetype)photoBracketSettingsWithNumber:(int)number;
23+
@end

0 commit comments

Comments
 (0)