Skip to content

Commit 6cc5855

Browse files
committed
swift-module-digester: when we have multiple extensions to a single external type, we should synthesize only one type decl node.
1 parent 08c8cf1 commit 6cc5855

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

test/api-digester/Inputs/cake.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,5 @@ public struct fixedLayoutStruct {
4848
private var b = 2
4949
var c = 3
5050
}
51+
52+
extension Int: P1 { public func bar() {} }

test/api-digester/Outputs/cake-abi.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,7 @@
983983
"Strideable",
984984
"ExpressibleByIntegerLiteral",
985985
"FixedWidthInteger",
986+
"P1",
986987
"Encodable",
987988
"Decodable",
988989
"Hashable",
@@ -1012,6 +1013,22 @@
10121013
"printedName": "()"
10131014
}
10141015
]
1016+
},
1017+
{
1018+
"kind": "Function",
1019+
"name": "bar",
1020+
"printedName": "bar()",
1021+
"declKind": "Func",
1022+
"usr": "s:Si4cakeE3baryyF",
1023+
"location": "",
1024+
"moduleName": "cake",
1025+
"children": [
1026+
{
1027+
"kind": "TypeNominal",
1028+
"name": "Void",
1029+
"printedName": "()"
1030+
}
1031+
]
10151032
}
10161033
]
10171034
}

test/api-digester/Outputs/cake.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,7 @@
859859
"Strideable",
860860
"ExpressibleByIntegerLiteral",
861861
"FixedWidthInteger",
862+
"P1",
862863
"Encodable",
863864
"Decodable",
864865
"Hashable",
@@ -888,6 +889,22 @@
888889
"printedName": "()"
889890
}
890891
]
892+
},
893+
{
894+
"kind": "Function",
895+
"name": "bar",
896+
"printedName": "bar()",
897+
"declKind": "Func",
898+
"usr": "s:Si4cakeE3baryyF",
899+
"location": "",
900+
"moduleName": "cake",
901+
"children": [
902+
{
903+
"kind": "TypeNominal",
904+
"name": "Void",
905+
"printedName": "()"
906+
}
907+
]
891908
}
892909
]
893910
}

tools/swift-api-digester/ModuleAnalyzerNodes.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,13 +1216,16 @@ static SDKNode *constructTypeDeclNode(SDKContext &Ctx, NominalTypeDecl *NTD,
12161216
/// synthesize this type node to include those extension members, since these
12171217
/// extension members are legit members of the module.
12181218
static SDKNode *constructExternalExtensionNode(SDKContext &Ctx, SDKNode *Root,
1219-
ExtensionDecl *Ext,
1219+
NominalTypeDecl *NTD,
1220+
ArrayRef<ExtensionDecl*> AllExts,
12201221
std::set<ExtensionDecl*> &HandledExts) {
1221-
auto *TypeNode = SDKNodeInitInfo(Ctx, Ext->getSelfNominalTypeDecl())
1222-
.createSDKNode(SDKNodeKind::DeclType);
1222+
auto *TypeNode = SDKNodeInitInfo(Ctx, NTD).createSDKNode(SDKNodeKind::DeclType);
12231223

1224-
// The members of the extension are the only members of this synthesized type.
1225-
addMembersToRoot(Ctx, TypeNode, Ext, HandledExts);
1224+
// The members of the extensions are the only members of this synthesized type.
1225+
for (auto *Ext: AllExts) {
1226+
HandledExts.insert(Ext);
1227+
addMembersToRoot(Ctx, TypeNode, Ext, HandledExts);
1228+
}
12261229
return TypeNode;
12271230
}
12281231

@@ -1302,16 +1305,20 @@ void SwiftDeclCollector::lookupVisibleDecls(ArrayRef<ModuleDecl *> Modules) {
13021305
for (auto *VD : ClangMacros)
13031306
processDecl(VD);
13041307

1305-
// For all known decls, collect those unhandled extensions and handle them
1306-
// separately.
1308+
// Collect extensions to types from other modules and synthesize type nodes
1309+
// for them.
1310+
llvm::MapVector<NominalTypeDecl*, llvm::SmallVector<ExtensionDecl*, 4>> ExtensionMap;
13071311
for (auto *D: KnownDecls) {
13081312
if (auto *Ext = dyn_cast<ExtensionDecl>(D)) {
13091313
if (HandledExtensions.find(Ext) == HandledExtensions.end()) {
1310-
RootNode->addChild(constructExternalExtensionNode(Ctx, RootNode, Ext,
1311-
HandledExtensions));
1314+
ExtensionMap[Ext->getExtendedNominal()].push_back(Ext);
13121315
}
13131316
}
13141317
}
1318+
for (auto Pair: ExtensionMap) {
1319+
RootNode->addChild(constructExternalExtensionNode(Ctx, RootNode,
1320+
Pair.first, Pair.second, HandledExtensions));
1321+
}
13151322
}
13161323

13171324
void SwiftDeclCollector::processDecl(ValueDecl *VD) {

tools/swift-api-digester/ModuleAnalyzerNodes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ class SwiftDeclCollector: public VisibleDeclConsumer {
495495
SDKContext &Ctx;
496496
std::vector<std::unique_ptr<llvm::MemoryBuffer>> OwnedBuffers;
497497
SDKNode *RootNode;
498-
llvm::DenseSet<Decl*> KnownDecls;
498+
llvm::SetVector<Decl*> KnownDecls;
499499
// Collected and sorted after we get all of them.
500500
std::vector<ValueDecl *> ClangMacros;
501501
std::set<ExtensionDecl*> HandledExtensions;

0 commit comments

Comments
 (0)