Skip to content

Commit 3d8fcf2

Browse files
committed
ABIChecker: include import decls in the intermediate JSON file
Removing an import statement can be potentially source-breaking. We should prepare for diagnosing such case.
1 parent a3e72f5 commit 3d8fcf2

File tree

10 files changed

+109
-0
lines changed

10 files changed

+109
-0
lines changed

include/swift/APIDigester/ModuleAnalyzerNodes.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,12 @@ class SDKNodeDeclAccessor: public SDKNodeDeclAbstractFunc {
717717
void jsonize(json::Output &Out) override;
718718
};
719719

720+
class SDKNodeDeclImport: public SDKNodeDecl {
721+
public:
722+
SDKNodeDeclImport(SDKNodeInitInfo Info);
723+
static bool classof(const SDKNode *N);
724+
};
725+
720726
// The additional information we need for a type node in the digest.
721727
// We use type node to represent entities more than types, e.g. parameters, so
722728
// this struct is necessary to pass down to create a type node.

include/swift/IDE/DigesterEnums.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ NODE_KIND(DeclOperator, OperatorDecl)
7272
NODE_KIND(DeclType, TypeDecl)
7373
NODE_KIND(DeclVar, Var)
7474
NODE_KIND(DeclTypeAlias, TypeAlias)
75+
NODE_KIND(DeclImport, Import)
7576
NODE_KIND(DeclAssociatedType, AssociatedType)
7677
NODE_KIND_RANGE(Decl, DeclFunction, DeclAssociatedType)
7778

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct swift::ide::api::SDKNodeInitInfo {
4646
SDKNodeInitInfo(SDKContext &Ctx, Decl *D);
4747
SDKNodeInitInfo(SDKContext &Ctx, ValueDecl *VD);
4848
SDKNodeInitInfo(SDKContext &Ctx, OperatorDecl *D);
49+
SDKNodeInitInfo(SDKContext &Ctx, ImportDecl *ID);
4950
SDKNodeInitInfo(SDKContext &Ctx, ProtocolConformance *Conform);
5051
SDKNodeInitInfo(SDKContext &Ctx, Type Ty, TypeInitInfo Info = TypeInitInfo());
5152
SDKNode* createSDKNode(SDKNodeKind Kind);
@@ -80,6 +81,9 @@ void SDKNodeRoot::registerDescendant(SDKNode *D) {
8081
// Operator doesn't have usr
8182
if (isa<SDKNodeDeclOperator>(D))
8283
return;
84+
// Import doesn't have usr
85+
if (isa<SDKNodeDeclImport>(D))
86+
return;
8387
if (auto DD = dyn_cast<SDKNodeDecl>(D)) {
8488
assert(!DD->getUsr().empty());
8589
DescendantDeclTable[DD->getUsr()].insert(DD);
@@ -166,6 +170,9 @@ SDKNodeDeclAccessor::SDKNodeDeclAccessor(SDKNodeInitInfo Info):
166170
SDKNodeDeclAbstractFunc(Info, SDKNodeKind::DeclAccessor),
167171
AccKind(Info.AccKind) {}
168172

173+
SDKNodeDeclImport::SDKNodeDeclImport(SDKNodeInitInfo Info):
174+
SDKNodeDecl(Info, SDKNodeKind::DeclImport) {}
175+
169176
SDKNodeDeclAssociatedType::SDKNodeDeclAssociatedType(SDKNodeInitInfo Info):
170177
SDKNodeDecl(Info, SDKNodeKind::DeclAssociatedType) {};
171178

@@ -375,6 +382,7 @@ StringRef SDKNodeType::getTypeRoleDescription() const {
375382
case SDKNodeKind::DeclType:
376383
case SDKNodeKind::DeclOperator:
377384
case SDKNodeKind::Conformance:
385+
case SDKNodeKind::DeclImport:
378386
llvm_unreachable("Type Parent is wrong");
379387
case SDKNodeKind::DeclFunction:
380388
case SDKNodeKind::DeclConstructor:
@@ -940,6 +948,7 @@ static bool isSDKNodeEqual(SDKContext &Ctx, const SDKNode &L, const SDKNode &R)
940948
}
941949
case SDKNodeKind::Conformance:
942950
case SDKNodeKind::TypeWitness:
951+
case SDKNodeKind::DeclImport:
943952
case SDKNodeKind::Root: {
944953
return L.getPrintedName() == R.getPrintedName() &&
945954
L.hasSameChildren(R);
@@ -1358,6 +1367,13 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, OperatorDecl *OD):
13581367
PrintedName = OD->getName().str();
13591368
}
13601369

1370+
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ImportDecl *ID):
1371+
SDKNodeInitInfo(Ctx, cast<Decl>(ID)) {
1372+
std::string content;
1373+
llvm::raw_string_ostream OS(content);
1374+
ID->getModulePath().print(OS);
1375+
Name = PrintedName = Ctx.buffer(content);
1376+
}
13611377

13621378
SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, ProtocolConformance *Conform):
13631379
SDKNodeInitInfo(Ctx, Conform->getProtocol()) {
@@ -1888,6 +1904,10 @@ void SwiftDeclCollector::processDecl(Decl *D) {
18881904
if (auto *OD = dyn_cast<OperatorDecl>(D)) {
18891905
RootNode->addChild(constructOperatorDeclNode(OD));
18901906
}
1907+
if (auto *IM = dyn_cast<ImportDecl>(D)) {
1908+
RootNode->addChild(SDKNodeInitInfo(Ctx, IM)
1909+
.createSDKNode(SDKNodeKind::DeclImport));
1910+
}
18911911
}
18921912

18931913
void SwiftDeclCollector::processValueDecl(ValueDecl *VD) {

lib/DriverTool/swift_api_digester_main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,7 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
11051105
case SDKNodeKind::DeclAccessor:
11061106
case SDKNodeKind::DeclConstructor:
11071107
case SDKNodeKind::DeclTypeAlias:
1108+
case SDKNodeKind::DeclImport:
11081109
case SDKNodeKind::TypeFunc:
11091110
case SDKNodeKind::TypeNominal:
11101111
case SDKNodeKind::TypeAlias: {

test/api-digester/Outputs/apinotes-diags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ APINotesTest(APINotesTest.h): TypeAlias CatAttributeName has been removed
1515
APINotesTest(APINotesTest.h): Protocol SwiftTypeWithMethodLeft has been renamed to Protocol SwiftTypeWithMethodRight
1616
APINotesTest(APINotesTest.h): Var OldType.oldMember has been renamed to Var NewType.newMember
1717
APINotesTest(APINotesTest.h): Var globalAttributeName has been renamed to Var AnimalAttributeName.globalAttributeName
18+
APINotesTest: Import Foundation has been renamed to Import objc_generics
1819

1920
/* Type Changes */
2021
APINotesTest(APINotesTest.h): Constructor Cat.init(name:) has return type change from APINotesTest.Cat to APINotesTest.Cat?

test/api-digester/Outputs/apinotes-migrator-gen-revert.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
[
2+
{
3+
"DiffItemKind": "CommonDiffItem",
4+
"NodeKind": "Import",
5+
"NodeAnnotation": "Rename",
6+
"ChildIndex": "0",
7+
"LeftUsr": "",
8+
"LeftComment": "objc_generics",
9+
"RightUsr": "",
10+
"RightComment": "Foundation",
11+
"ModuleName": "APINotesTest"
12+
},
213
{
314
"DiffItemKind": "CommonDiffItem",
415
"NodeKind": "Var",

test/api-digester/Outputs/apinotes-migrator-gen.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
[
2+
{
3+
"DiffItemKind": "CommonDiffItem",
4+
"NodeKind": "Import",
5+
"NodeAnnotation": "Rename",
6+
"ChildIndex": "0",
7+
"LeftUsr": "",
8+
"LeftComment": "Foundation",
9+
"RightUsr": "",
10+
"RightComment": "objc_generics",
11+
"ModuleName": "APINotesTest"
12+
},
213
{
314
"DiffItemKind": "CommonDiffItem",
415
"NodeKind": "Var",

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,30 @@
33
"name": "TopLevel",
44
"printedName": "TopLevel",
55
"children": [
6+
{
7+
"kind": "Import",
8+
"name": "SwiftOnoneSupport",
9+
"printedName": "SwiftOnoneSupport",
10+
"declKind": "Import",
11+
"moduleName": "cake"
12+
},
13+
{
14+
"kind": "Import",
15+
"name": "_Concurrency",
16+
"printedName": "_Concurrency",
17+
"declKind": "Import",
18+
"moduleName": "cake"
19+
},
20+
{
21+
"kind": "Import",
22+
"name": "cake",
23+
"printedName": "cake",
24+
"declKind": "Import",
25+
"moduleName": "cake",
26+
"declAttributes": [
27+
"Exported"
28+
]
29+
},
630
{
731
"kind": "TypeDecl",
832
"name": "P1",

test/api-digester/Outputs/cake.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,30 @@
33
"name": "TopLevel",
44
"printedName": "TopLevel",
55
"children": [
6+
{
7+
"kind": "Import",
8+
"name": "SwiftOnoneSupport",
9+
"printedName": "SwiftOnoneSupport",
10+
"declKind": "Import",
11+
"moduleName": "cake"
12+
},
13+
{
14+
"kind": "Import",
15+
"name": "_Concurrency",
16+
"printedName": "_Concurrency",
17+
"declKind": "Import",
18+
"moduleName": "cake"
19+
},
20+
{
21+
"kind": "Import",
22+
"name": "cake",
23+
"printedName": "cake",
24+
"declKind": "Import",
25+
"moduleName": "cake",
26+
"declAttributes": [
27+
"Exported"
28+
]
29+
},
630
{
731
"kind": "TypeDecl",
832
"name": "P1",

test/api-digester/Outputs/clang-module-dump.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
"name": "TopLevel",
44
"printedName": "TopLevel",
55
"children": [
6+
{
7+
"kind": "Import",
8+
"name": "ObjectiveC",
9+
"printedName": "ObjectiveC",
10+
"declKind": "Import",
11+
"moduleName": "Foo",
12+
"declAttributes": [
13+
"Exported"
14+
]
15+
},
616
{
717
"kind": "TypeDecl",
818
"name": "AnotherObjcProt",

0 commit comments

Comments
 (0)