Skip to content

swift-api-digester: teach the tool to detect [String:Any] changes to [StringRepresentable:Any]. #15524

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 2 commits into from
Mar 27, 2018
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
10 changes: 10 additions & 0 deletions include/swift/IDE/DigesterEnums.def
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#define KNOWN_TYPE(NAME)
#endif

#ifndef KNOWN_PROTOCOL
#define KNOWN_PROTOCOL(NAME)
#endif

#ifndef DIFF_ITEM_KIND
#define DIFF_ITEM_KIND(NAME)
#endif
Expand Down Expand Up @@ -70,6 +74,7 @@ NODE_ANNOTATION(NowThrowing)
NODE_ANNOTATION(NowMutating)
NODE_ANNOTATION(StaticChange)
NODE_ANNOTATION(OwnershipChange)
NODE_ANNOTATION(DictionaryKeyUpdate)

DECL_ATTR(deprecated)
DECL_ATTR(fixedLayout)
Expand Down Expand Up @@ -100,6 +105,10 @@ KNOWN_TYPE(ImplicitlyUnwrappedOptional)
KNOWN_TYPE(Void)
KNOWN_TYPE(Unmanaged)
KNOWN_TYPE(Function)
KNOWN_TYPE(Dictionary)
KNOWN_TYPE(String)

KNOWN_PROTOCOL(RawRepresentable)

DIFF_ITEM_KIND(CommonDiffItem)
DIFF_ITEM_KIND(TypeMemberDiffItem)
Expand Down Expand Up @@ -149,6 +158,7 @@ SPECIAL_CASE_ID(ToUIntMax)
#undef DIFF_ITEM_KEY_KIND
#undef DIFF_ITEM_KIND
#undef KNOWN_TYPE
#undef KNOWN_PROTOCOL
#undef KEY
#undef DECL_ATTR
#undef NODE_ANNOTATION
Expand Down
22 changes: 22 additions & 0 deletions test/api-digester/Outputs/apinotes-migrator-gen.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
"NodeAnnotation": "DictionaryKeyUpdate",
"ChildIndex": "1",
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingAttributes:",
"LeftComment": "",
"RightUsr": "",
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
Expand All @@ -21,6 +32,17 @@
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
"NodeAnnotation": "DictionaryKeyUpdate",
"ChildIndex": "1",
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingAttributes:",
"LeftComment": "",
"RightUsr": "",
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
Expand Down
93 changes: 68 additions & 25 deletions tools/swift-api-digester/swift-api-digester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ enum class KnownTypeKind: uint8_t {
Unknown,
};

enum class KnownProtocolKind: uint8_t {
#define KNOWN_PROTOCOL(NAME) NAME,
#include "swift/IDE/DigesterEnums.def"
};

enum class SDKDeclAttrKind: uint8_t {
#define DECL_ATTR(Name) DAK_##Name,
#include "swift/IDE/DigesterEnums.def"
Expand Down Expand Up @@ -378,11 +383,11 @@ class SDKNode {
void removeChild(ChildIt CI) { Children.erase(CI); }
ChildIt getChildBegin() { return Children.begin(); }
void annotate(NodeAnnotation Anno) { Annotations.insert(Anno); }
void annotate(NodeAnnotation Anno, StringRef Comment);
NodePtr getParent() const { return Parent; };
unsigned getChildrenCount() const { return Children.size(); }
NodePtr childAt(unsigned I) const;
void removeChild(NodePtr C);
void addAnnotateComment(NodeAnnotation Anno, StringRef Comment);
StringRef getAnnotateComment(NodeAnnotation Anno) const;
bool isAnnotatedAs(NodeAnnotation Anno) const;
void addChild(SDKNode *Child);
Expand Down Expand Up @@ -598,8 +603,9 @@ void SDKNode::removeChild(NodePtr C) {
Children.erase(std::find(Children.begin(), Children.end(), C));
}

void SDKNode::addAnnotateComment(NodeAnnotation Anno, StringRef Comment) {
assert(isAnnotatedAs(Anno) && "Cannot find annotation");
void SDKNode::annotate(NodeAnnotation Anno, StringRef Comment) {
assert(!isAnnotatedAs(Anno) && "already annotated");
annotate(Anno);
AnnotateComments[Anno] = Comment;
}

Expand Down Expand Up @@ -831,6 +837,18 @@ class SDKNodeTypeDecl : public SDKNodeDecl {
}
return None;
}

bool isConformingTo(KnownProtocolKind Kind) const {
StringRef Usr;
switch (Kind) {
#define KNOWN_PROTOCOL(NAME) \
case KnownProtocolKind::NAME: \
return std::find(ConformingProtocols.begin(), \
ConformingProtocols.end(), \
#NAME) != ConformingProtocols.end();
#include "swift/IDE/DigesterEnums.def"
}
}
};

class SDKNodeTypeAlias : public SDKNodeDecl {
Expand Down Expand Up @@ -1957,8 +1975,7 @@ class RemovedAddedNodeMatcher : public NodeMatcher, public MatchedNodeListener {
} else {
return false;
}
R->annotate(NodeAnnotation::PropertyName);
R->addAnnotateComment(NodeAnnotation::PropertyName, A->getPrintedName());
R->annotate(NodeAnnotation::PropertyName, A->getPrintedName());
foundMatch(R, A);
return true;
}
Expand Down Expand Up @@ -1995,11 +2012,10 @@ class RemovedAddedNodeMatcher : public NodeMatcher, public MatchedNodeListener {
if (auto VC = dyn_cast<SDKNodeVar>(Child)) {
auto LastPartOfA = getLastPartOfUsr(VC);
if (LastPartOfA && LastPartOfR.getValue() == LastPartOfA.getValue()) {
R->annotate(NodeAnnotation::ModernizeEnum);
std::string FullName = (llvm::Twine(A->getName()) + "." +
Child->getName()).str();
R->addAnnotateComment(NodeAnnotation::ModernizeEnum,
R->getSDKContext().buffer(FullName));
R->annotate(NodeAnnotation::ModernizeEnum,
R->getSDKContext().buffer(FullName));
foundMatch(R, A);
return true;
}
Expand Down Expand Up @@ -2304,10 +2320,8 @@ static void detectRename(NodePtr L, NodePtr R) {
assert(L->getKind() == R->getKind());
if (isa<SDKNodeDecl>(L) && L->getPrintedName() != R->getPrintedName()) {
L->annotate(NodeAnnotation::Rename);
L->annotate(NodeAnnotation::RenameOldName);
L->addAnnotateComment(NodeAnnotation::RenameOldName, L->getPrintedName());
L->annotate(NodeAnnotation::RenameNewName);
L->addAnnotateComment(NodeAnnotation::RenameNewName, R->getPrintedName());
L->annotate(NodeAnnotation::RenameOldName, L->getPrintedName());
L->annotate(NodeAnnotation::RenameNewName, R->getPrintedName());
}
}

Expand Down Expand Up @@ -2477,12 +2491,10 @@ class TypeMemberDiffFinder : public SDKNodeVisitor {
diffNode->getKind() == SDKNodeKind::Function &&
node->isNameValid()) {
diffNode->annotate(NodeAnnotation::Rename);
diffNode->annotate(NodeAnnotation::RenameOldName);
diffNode->addAnnotateComment(NodeAnnotation::RenameOldName,
diffNode->getPrintedName());
diffNode->annotate(NodeAnnotation::RenameNewName);
diffNode->addAnnotateComment(NodeAnnotation::RenameNewName,
node->getParent()->getPrintedName());
diffNode->annotate(NodeAnnotation::RenameOldName,
diffNode->getPrintedName());
diffNode->annotate(NodeAnnotation::RenameNewName,
node->getParent()->getPrintedName());
}
}

Expand Down Expand Up @@ -2587,17 +2599,45 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
(Node->getName() != Counter->getName()||
Node->getChildrenCount() != Counter->getChildrenCount())) {
Node->annotate(NodeAnnotation::TypeRewritten);
Node->annotate(NodeAnnotation::TypeRewrittenLeft);
Node->annotate(NodeAnnotation::TypeRewrittenRight);
Node->addAnnotateComment(NodeAnnotation::TypeRewrittenLeft,
Node->getPrintedName());
Node->addAnnotateComment(NodeAnnotation::TypeRewrittenRight,
Counter->getPrintedName());
Node->annotate(NodeAnnotation::TypeRewrittenLeft, Node->getPrintedName());
Node->annotate(NodeAnnotation::TypeRewrittenRight,
Counter->getPrintedName());
return true;
}
return false;
}

bool detectDictionaryKeyChange(SDKNodeType *L, SDKNodeType *R) {
if (!IsVisitingLeft)
return false;
if (L->getTypeKind() != KnownTypeKind::Dictionary ||
R->getTypeKind() != KnownTypeKind::Dictionary)
return false;
auto *Left = dyn_cast<SDKNodeTypeNominal>(L);
auto *Right = dyn_cast<SDKNodeTypeNominal>(R);
assert(Left && Right);
assert(Left->getChildrenCount() == 2);
assert(Right->getChildrenCount() == 2);
auto* LKey = dyn_cast<SDKNodeTypeNominal>(*Left->getChildBegin());
auto* RKey = dyn_cast<SDKNodeTypeNominal>(*Right->getChildBegin());
if (!LKey || !RKey)
return false;
if (LKey->getTypeKind() != KnownTypeKind::String)
return false;
auto Results = RKey->getRootNode()->getDescendantsByUsr(RKey->getUsr());
if (Results.empty())
return false;
if (auto DT = dyn_cast<SDKNodeTypeDecl>(Results.front())) {
if (DT->isConformingTo(KnownProtocolKind::RawRepresentable)) {
L->annotate(NodeAnnotation::DictionaryKeyUpdate);
L->annotate(NodeAnnotation::TypeRewrittenRight,
DT->getFullyQualifiedName());
return true;
}
}
return false;
}

bool isUnhandledCase(SDKNodeType *Node) {
auto Counter = UpdateMap.findUpdateCounterpart(Node)->getAs<SDKNodeType>();
return Node->getTypeKind() == KnownTypeKind::Void ||
Expand Down Expand Up @@ -2628,6 +2668,7 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
detectOptionalUpdate(Node, Counter)||
detectWrapImplicitOptional(Node, Counter)||
detectUnmanagedUpdate(Node, Counter)||
detectDictionaryKeyChange(Node, Counter) ||
detectTypeRewritten(Node, Counter);
(void) Result;
return;
Expand Down Expand Up @@ -2739,6 +2780,7 @@ class DiffItemEmitter : public SDKNodeVisitor {

static StringRef getRightComment(NodePtr Node, NodeAnnotation Anno) {
switch (Anno) {
case NodeAnnotation::DictionaryKeyUpdate:
case NodeAnnotation::TypeRewritten:
return Node->getAnnotateComment(NodeAnnotation::TypeRewrittenRight);
case NodeAnnotation::ModernizeEnum:
Expand Down Expand Up @@ -2795,7 +2837,8 @@ class DiffItemEmitter : public SDKNodeVisitor {
NodeAnnotation::GetterToProperty,
NodeAnnotation::ModernizeEnum,
NodeAnnotation::Rename,
NodeAnnotation::NowThrowing
NodeAnnotation::NowThrowing,
NodeAnnotation::DictionaryKeyUpdate,
});
}

Expand Down