Skip to content

Commit 136c86b

Browse files
authored
Merge pull request #5560 from nkcsgexi/digester-ownership
2 parents cbc9d89 + 725aef7 commit 136c86b

File tree

5 files changed

+47
-20
lines changed

5 files changed

+47
-20
lines changed

test/api-digester/Inputs/cake1.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ public struct S1 {
88
public class C1 {
99
public class func foo1() {}
1010
public func foo2(_ : Int) {}
11+
public weak var CIIns1 : C1?
12+
public var CIIns2 : C1?
1113
}

test/api-digester/Inputs/cake2.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ public struct S1 {
88
public class C1 {
99
public func foo1() {}
1010
public func foo2(_ : ()->()) {}
11+
public var CIIns1 : C1?
12+
public weak var CIIns2 : C1?
1113
}

test/api-digester/Outputs/Cake.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Constructor S1.init(_:) has 1st parameter type change from Int to Double
1010
Func C1.foo2(_:) has 1st parameter type change from Int to () -> ()
1111

1212
==================================================== Decl Attribute changes ====================================================
13+
Var C1.CIIns1 changes from weak to strong
14+
Var C1.CIIns2 changes from strong to weak
1315
Func C1.foo1() is now not static
1416
Func S1.foo3() is now static
1517
Func S1.foo1() is now mutating

tools/swift-api-digester/DigesterEnums.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ NODE_ANNOTATION(RenameNewName)
5656
NODE_ANNOTATION(NowThrowing)
5757
NODE_ANNOTATION(NowMutating)
5858
NODE_ANNOTATION(StaticChange)
59+
NODE_ANNOTATION(OwnershipChange)
5960

6061
DECL_ATTR(deprecated)
6162

tools/swift-api-digester/swift-api-digester.cpp

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,8 @@ bool SDKNode::operator==(const SDKNode &Other) const {
889889
auto Right = (&Other)->getAs<SDKNodeDecl>();
890890
if (Left->isStatic() ^ Right->isStatic())
891891
return false;
892+
if (Left->getOwnership() != Right->getOwnership())
893+
return false;
892894
SWIFT_FALLTHROUGH;
893895
}
894896
case SDKNodeKind::Root:
@@ -1945,20 +1947,13 @@ class UpdatedNodesMap : public MatchedNodeListener {
19451947
}
19461948
};
19471949

1948-
static void detectThrowing(NodePtr L, NodePtr R) {
1950+
static void detectFuncDeclChange(NodePtr L, NodePtr R) {
19491951
assert(L->getKind() == R->getKind());
19501952
if (auto LF = dyn_cast<SDKNodeAbstractFunc>(L)) {
19511953
auto RF = R->getAs<SDKNodeAbstractFunc>();
19521954
if (!LF->isThrowing() && RF->isThrowing()) {
19531955
LF->annotate(NodeAnnotation::NowThrowing);
19541956
}
1955-
}
1956-
}
1957-
1958-
static void detectMutating(NodePtr L, NodePtr R) {
1959-
assert(L->getKind() == R->getKind());
1960-
if (auto LF = dyn_cast<SDKNodeAbstractFunc>(L)) {
1961-
auto RF = R->getAs<SDKNodeAbstractFunc>();
19621957
if (!LF->isMutating() && RF->isMutating()) {
19631958
LF->annotate(NodeAnnotation::NowMutating);
19641959
}
@@ -1976,12 +1971,15 @@ static void detectRename(NodePtr L, NodePtr R) {
19761971
}
19771972
}
19781973

1979-
static void detectStaticUpdate(NodePtr L, NodePtr R) {
1974+
static void detectDeclChange(NodePtr L, NodePtr R) {
19801975
assert(L->getKind() == R->getKind());
19811976
if (auto LD = dyn_cast<SDKNodeDecl>(L)) {
1982-
if (LD->isStatic() ^ R->getAs<SDKNodeDecl>()->isStatic()) {
1977+
auto *RD = R->getAs<SDKNodeDecl>();
1978+
if (LD->isStatic() ^ RD->isStatic())
19831979
L->annotate(NodeAnnotation::StaticChange);
1984-
}
1980+
if (LD->getOwnership() != RD->getOwnership())
1981+
L->annotate(NodeAnnotation::OwnershipChange);
1982+
detectRename(L, R);
19851983
}
19861984
}
19871985

@@ -2041,10 +2039,8 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
20412039
Right->getKind() != SDKNodeKind::Nil);
20422040
assert(Kind == SDKNodeKind::Root || *Left != *Right);
20432041

2044-
detectRename(Left, Right);
2045-
detectThrowing(Left, Right);
2046-
detectMutating(Left, Right);
2047-
detectStaticUpdate(Left, Right);
2042+
detectDeclChange(Left, Right);
2043+
detectFuncDeclChange(Left, Right);
20482044

20492045
switch(Kind) {
20502046
case SDKNodeKind::Root:
@@ -2756,9 +2752,14 @@ class DiagnosisEmitter : public SDKNodeVisitor {
27562752
struct DeclAttrDiag {
27572753
DeclKind Kind;
27582754
StringRef DeclName;
2759-
StringRef AttrName;
2760-
DeclAttrDiag(DeclKind Kind, StringRef DeclName, StringRef AttrName) :
2761-
Kind(Kind), DeclName(DeclName), AttrName(AttrName) {}
2755+
StringRef AttrBefore;
2756+
StringRef AttrAfter;
2757+
DeclAttrDiag(DeclKind Kind, StringRef DeclName, StringRef AttrBefore,
2758+
StringRef AttrAfter) : Kind(Kind), DeclName(DeclName),
2759+
AttrBefore(AttrBefore), AttrAfter(AttrAfter) {}
2760+
DeclAttrDiag(DeclKind Kind, StringRef DeclName, StringRef AttrAfter) :
2761+
DeclAttrDiag(Kind, DeclName, StringRef(), AttrAfter) {}
2762+
27622763
bool operator<(DeclAttrDiag Other) const;
27632764
void output() const;
27642765
static void theme(raw_ostream &OS) { OS << "Decl Attribute changes"; };
@@ -2888,8 +2889,12 @@ bool DiagnosisEmitter::DeclAttrDiag::operator<(DeclAttrDiag Other) const {
28882889
}
28892890

28902891
void DiagnosisEmitter::DeclAttrDiag::output() const {
2891-
llvm::outs() << Kind << " " << printName(DeclName) << " is now " <<
2892-
printDiagKeyword(AttrName) << "\n";
2892+
if (AttrBefore.empty())
2893+
llvm::outs() << Kind << " " << printName(DeclName) << " is now " <<
2894+
printDiagKeyword(AttrAfter)<< "\n";
2895+
else
2896+
llvm::outs() << Kind << " " << printName(DeclName) << " changes from " <<
2897+
printDiagKeyword(AttrBefore) << " to "<< printDiagKeyword(AttrAfter)<< "\n";
28932898
}
28942899

28952900
void DiagnosisEmitter::diagnosis(NodePtr LeftRoot, NodePtr RightRoot,
@@ -2936,6 +2941,21 @@ void DiagnosisEmitter::visitDecl(SDKNodeDecl *Node) {
29362941
Node->getFullyQualifiedName(),
29372942
InsertToBuffer(Node->isStatic() ? "not static" : "static"));
29382943
}
2944+
if (Node->isAnnotatedAs(NodeAnnotation::OwnershipChange)) {
2945+
auto getOwnershipDescription = [](swift::Ownership O) {
2946+
switch (O) {
2947+
case Ownership::Strong: return InsertToBuffer("strong");
2948+
case Ownership::Weak: return InsertToBuffer("weak");
2949+
case Ownership::Unowned: return InsertToBuffer("unowned");
2950+
case Ownership::Unmanaged: return InsertToBuffer("unowned(unsafe)");
2951+
}
2952+
};
2953+
auto *Count = UpdateMap.findUpdateCounterpart(Node)->getAs<SDKNodeDecl>();
2954+
AttrChangedDecls.Diags.emplace_back(Node->getDeclKind(),
2955+
Node->getFullyQualifiedName(),
2956+
getOwnershipDescription(Node->getOwnership()),
2957+
getOwnershipDescription(Count->getOwnership()));
2958+
}
29392959
}
29402960
void DiagnosisEmitter::visitType(SDKNodeType *Node) {
29412961
auto *Parent = Node->getParent()->getAs<SDKNodeDecl>();

0 commit comments

Comments
 (0)