Skip to content

swift-api-digester: reference ownership changes considered source-breaking. #5560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions test/api-digester/Inputs/cake1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public struct S1 {
public class C1 {
public class func foo1() {}
public func foo2(_ : Int) {}
public weak var CIIns1 : C1?
public var CIIns2 : C1?
}
2 changes: 2 additions & 0 deletions test/api-digester/Inputs/cake2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public struct S1 {
public class C1 {
public func foo1() {}
public func foo2(_ : ()->()) {}
public var CIIns1 : C1?
public weak var CIIns2 : C1?
}
2 changes: 2 additions & 0 deletions test/api-digester/Outputs/Cake.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Constructor S1.init(_:) has 1st parameter type change from Int to Double
Func C1.foo2(_:) has 1st parameter type change from Int to () -> ()

==================================================== Decl Attribute changes ====================================================
Var C1.CIIns1 changes from weak to strong
Var C1.CIIns2 changes from strong to weak
Func C1.foo1() is now not static
Func S1.foo3() is now static
Func S1.foo1() is now mutating
1 change: 1 addition & 0 deletions tools/swift-api-digester/DigesterEnums.def
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ NODE_ANNOTATION(RenameNewName)
NODE_ANNOTATION(NowThrowing)
NODE_ANNOTATION(NowMutating)
NODE_ANNOTATION(StaticChange)
NODE_ANNOTATION(OwnershipChange)

DECL_ATTR(deprecated)

Expand Down
60 changes: 40 additions & 20 deletions tools/swift-api-digester/swift-api-digester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,8 @@ bool SDKNode::operator==(const SDKNode &Other) const {
auto Right = (&Other)->getAs<SDKNodeDecl>();
if (Left->isStatic() ^ Right->isStatic())
return false;
if (Left->getOwnership() != Right->getOwnership())
return false;
SWIFT_FALLTHROUGH;
}
case SDKNodeKind::Root:
Expand Down Expand Up @@ -1944,20 +1946,13 @@ class UpdatedNodesMap : public MatchedNodeListener {
}
};

static void detectThrowing(NodePtr L, NodePtr R) {
static void detectFuncDeclChange(NodePtr L, NodePtr R) {
assert(L->getKind() == R->getKind());
if (auto LF = dyn_cast<SDKNodeAbstractFunc>(L)) {
auto RF = R->getAs<SDKNodeAbstractFunc>();
if (!LF->isThrowing() && RF->isThrowing()) {
LF->annotate(NodeAnnotation::NowThrowing);
}
}
}

static void detectMutating(NodePtr L, NodePtr R) {
assert(L->getKind() == R->getKind());
if (auto LF = dyn_cast<SDKNodeAbstractFunc>(L)) {
auto RF = R->getAs<SDKNodeAbstractFunc>();
if (!LF->isMutating() && RF->isMutating()) {
LF->annotate(NodeAnnotation::NowMutating);
}
Expand All @@ -1975,12 +1970,15 @@ static void detectRename(NodePtr L, NodePtr R) {
}
}

static void detectStaticUpdate(NodePtr L, NodePtr R) {
static void detectDeclChange(NodePtr L, NodePtr R) {
assert(L->getKind() == R->getKind());
if (auto LD = dyn_cast<SDKNodeDecl>(L)) {
if (LD->isStatic() ^ R->getAs<SDKNodeDecl>()->isStatic()) {
auto *RD = R->getAs<SDKNodeDecl>();
if (LD->isStatic() ^ RD->isStatic())
L->annotate(NodeAnnotation::StaticChange);
}
if (LD->getOwnership() != RD->getOwnership())
L->annotate(NodeAnnotation::OwnershipChange);
detectRename(L, R);
}
}

Expand Down Expand Up @@ -2040,10 +2038,8 @@ class PrunePass : public MatchedNodeListener, public SDKTreeDiffPass {
Right->getKind() != SDKNodeKind::Nil);
assert(Kind == SDKNodeKind::Root || *Left != *Right);

detectRename(Left, Right);
detectThrowing(Left, Right);
detectMutating(Left, Right);
detectStaticUpdate(Left, Right);
detectDeclChange(Left, Right);
detectFuncDeclChange(Left, Right);

switch(Kind) {
case SDKNodeKind::Root:
Expand Down Expand Up @@ -2755,9 +2751,14 @@ class DiagnosisEmitter : public SDKNodeVisitor {
struct DeclAttrDiag {
DeclKind Kind;
StringRef DeclName;
StringRef AttrName;
DeclAttrDiag(DeclKind Kind, StringRef DeclName, StringRef AttrName) :
Kind(Kind), DeclName(DeclName), AttrName(AttrName) {}
StringRef AttrBefore;
StringRef AttrAfter;
DeclAttrDiag(DeclKind Kind, StringRef DeclName, StringRef AttrBefore,
StringRef AttrAfter) : Kind(Kind), DeclName(DeclName),
AttrBefore(AttrBefore), AttrAfter(AttrAfter) {}
DeclAttrDiag(DeclKind Kind, StringRef DeclName, StringRef AttrAfter) :
DeclAttrDiag(Kind, DeclName, StringRef(), AttrAfter) {}

bool operator<(DeclAttrDiag Other) const;
void output() const;
static void theme(raw_ostream &OS) { OS << "Decl Attribute changes"; };
Expand Down Expand Up @@ -2887,8 +2888,12 @@ bool DiagnosisEmitter::DeclAttrDiag::operator<(DeclAttrDiag Other) const {
}

void DiagnosisEmitter::DeclAttrDiag::output() const {
llvm::outs() << Kind << " " << printName(DeclName) << " is now " <<
printDiagKeyword(AttrName) << "\n";
if (AttrBefore.empty())
llvm::outs() << Kind << " " << printName(DeclName) << " is now " <<
printDiagKeyword(AttrAfter)<< "\n";
else
llvm::outs() << Kind << " " << printName(DeclName) << " changes from " <<
printDiagKeyword(AttrBefore) << " to "<< printDiagKeyword(AttrAfter)<< "\n";
}

void DiagnosisEmitter::diagnosis(NodePtr LeftRoot, NodePtr RightRoot,
Expand Down Expand Up @@ -2935,6 +2940,21 @@ void DiagnosisEmitter::visitDecl(SDKNodeDecl *Node) {
Node->getFullyQualifiedName(),
InsertToBuffer(Node->isStatic() ? "not static" : "static"));
}
if (Node->isAnnotatedAs(NodeAnnotation::OwnershipChange)) {
auto getOwnershipDescription = [](swift::Ownership O) {
switch (O) {
case Ownership::Strong: return InsertToBuffer("strong");
case Ownership::Weak: return InsertToBuffer("weak");
case Ownership::Unowned: return InsertToBuffer("unowned");
case Ownership::Unmanaged: return InsertToBuffer("unowned(unsafe)");
}
};
auto *Count = UpdateMap.findUpdateCounterpart(Node)->getAs<SDKNodeDecl>();
AttrChangedDecls.Diags.emplace_back(Node->getDeclKind(),
Node->getFullyQualifiedName(),
getOwnershipDescription(Node->getOwnership()),
getOwnershipDescription(Count->getOwnership()));
}
}
void DiagnosisEmitter::visitType(SDKNodeType *Node) {
auto *Parent = Node->getParent()->getAs<SDKNodeDecl>();
Expand Down