Skip to content

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

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
Apr 7, 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
3 changes: 3 additions & 0 deletions include/swift/IDE/DigesterEnums.def
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ NODE_ANNOTATION(StaticChange)
NODE_ANNOTATION(OwnershipChange)
NODE_ANNOTATION(DictionaryKeyUpdate)
NODE_ANNOTATION(OptionalDictionaryKeyUpdate)
NODE_ANNOTATION(ArrayMemberUpdate)
NODE_ANNOTATION(OptionalArrayMemberUpdate)

DECL_ATTR(deprecated)
DECL_ATTR(fixedLayout)
Expand Down Expand Up @@ -108,6 +110,7 @@ KNOWN_TYPE(Unmanaged)
KNOWN_TYPE(Function)
KNOWN_TYPE(Dictionary)
KNOWN_TYPE(String)
KNOWN_TYPE(Array)

KNOWN_PROTOCOL(RawRepresentable)

Expand Down
2 changes: 2 additions & 0 deletions test/api-digester/Inputs/APINotesLeft/APINotesTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ extern int ANTGlobalValue;
@interface AnimalStatusDescriptor
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingAttributes:(nonnull NSDictionary<NSString*, id> *)attributes;
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingOptionalAttributes:(nullable NSDictionary<NSString*, id> *)attributes;
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingAttributesArray:(nonnull NSArray<NSString*> *)attributes;
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingOptionalAttributesArray:(nullable NSArray<NSString*> *)attributes;
@end
3 changes: 3 additions & 0 deletions test/api-digester/Inputs/APINotesRight/APINotesTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ typedef NSString * AnimalAttributeName NS_STRING_ENUM;
@interface AnimalStatusDescriptor
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingAttributes:(nonnull NSDictionary<AnimalAttributeName, id> *)attributes;
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingOptionalAttributes:(nullable NSDictionary<AnimalAttributeName, id> *)attributes;
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingAttributesArray:(nonnull NSArray<AnimalAttributeName> *)attributes;
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingOptionalAttributesArray:(nullable NSArray<AnimalAttributeName> *)attributes;

@end
2 changes: 2 additions & 0 deletions test/api-digester/Outputs/apinotes-diags-3-4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ APINotesTest(APINotesTest.h): TypeAlias AnimalAttributeName(NSString) is now Str

/* Type Changes */
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingAttributes(_:) has parameter 0 type change from [String : Any] to [AnimalAttributeName : Any]
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingAttributesArray(_:) has parameter 0 type change from [String] to [AnimalAttributeName]
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingOptionalAttributes(_:) has parameter 0 type change from [String : Any]? to [AnimalAttributeName : Any]?
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingOptionalAttributesArray(_:) has parameter 0 type change from [String]? to [AnimalAttributeName]?

/* Decl Attribute changes */
2 changes: 2 additions & 0 deletions test/api-digester/Outputs/apinotes-diags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ APINotesTest(APINotesTest.h): Var OldType.oldMember has been renamed to Var NewT

/* Type Changes */
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingAttributes(_:) has parameter 0 type change from [String : Any] to [AnimalAttributeName : Any]
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingAttributesArray(_:) has parameter 0 type change from [String] to [AnimalAttributeName]
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingOptionalAttributes(_:) has parameter 0 type change from [String : Any]? to [AnimalAttributeName : Any]?
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingOptionalAttributesArray(_:) has parameter 0 type change from [String]? to [AnimalAttributeName]?

/* Decl Attribute changes */
88 changes: 88 additions & 0 deletions test/api-digester/Outputs/apinotes-migrator-gen.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,94 @@
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
"NodeAnnotation": "TypeRewritten",
"ChildIndex": "1:0",
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingAttributesArray:",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
"NodeAnnotation": "ArrayMemberUpdate",
"ChildIndex": "1",
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingAttributesArray:",
"LeftComment": "",
"RightUsr": "",
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
"NodeAnnotation": "TypeRewritten",
"ChildIndex": "1:0",
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingAttributesArray:",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
"NodeAnnotation": "ArrayMemberUpdate",
"ChildIndex": "1",
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingAttributesArray:",
"LeftComment": "",
"RightUsr": "",
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
"NodeAnnotation": "TypeRewritten",
"ChildIndex": "1:0:0",
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingOptionalAttributesArray:",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
"NodeAnnotation": "OptionalArrayMemberUpdate",
"ChildIndex": "1",
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingOptionalAttributesArray:",
"LeftComment": "",
"RightUsr": "",
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
"NodeAnnotation": "TypeRewritten",
"ChildIndex": "1:0:0",
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingOptionalAttributesArray:",
"LeftComment": "String",
"RightUsr": "",
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
"NodeAnnotation": "OptionalArrayMemberUpdate",
"ChildIndex": "1",
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(im)animalStatusDescriptorByAddingOptionalAttributesArray:",
"LeftComment": "",
"RightUsr": "",
"RightComment": "AnimalAttributeName",
"ModuleName": "APINotesTest"
},
{
"DiffItemKind": "CommonDiffItem",
"NodeKind": "Function",
Expand Down
75 changes: 63 additions & 12 deletions tools/swift-api-digester/swift-api-digester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2693,18 +2693,9 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
return false;
}

static StringRef detectDictionaryKeyChangeInternal(SDKNodeType *L,
SDKNodeType *R) {
if (L->getTypeKind() != KnownTypeKind::Dictionary ||
R->getTypeKind() != KnownTypeKind::Dictionary)
return StringRef();
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());
static StringRef getStringRepresentableChange(SDKNode *L, SDKNode *R) {
auto* LKey = dyn_cast<SDKNodeTypeNominal>(L);
auto* RKey = dyn_cast<SDKNodeTypeNominal>(R);
if (!LKey || !RKey)
return StringRef();
if (LKey->getTypeKind() != KnownTypeKind::String)
Expand All @@ -2720,6 +2711,20 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
return StringRef();
}

static StringRef detectDictionaryKeyChangeInternal(SDKNodeType *L,
SDKNodeType *R) {
if (L->getTypeKind() != KnownTypeKind::Dictionary ||
R->getTypeKind() != KnownTypeKind::Dictionary)
return StringRef();
auto *Left = dyn_cast<SDKNodeTypeNominal>(L);
auto *Right = dyn_cast<SDKNodeTypeNominal>(R);
assert(Left && Right);
assert(Left->getChildrenCount() == 2);
assert(Right->getChildrenCount() == 2);
return getStringRepresentableChange(*Left->getChildBegin(),
*Right->getChildBegin());
}

bool detectDictionaryKeyChange(SDKNodeType *L, SDKNodeType *R) {
if (!IsVisitingLeft)
return false;
Expand Down Expand Up @@ -2748,6 +2753,47 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
return false;
}

static StringRef detectArrayMemberChangeInternal(SDKNodeType *L,
SDKNodeType *R) {
if (L->getTypeKind() != KnownTypeKind::Array ||
R->getTypeKind() != KnownTypeKind::Array)
return StringRef();
auto *Left = dyn_cast<SDKNodeTypeNominal>(L);
auto *Right = dyn_cast<SDKNodeTypeNominal>(R);
assert(Left && Right);
assert(Left->getChildrenCount() == 1);
assert(Right->getChildrenCount() == 1);
return getStringRepresentableChange(Left->getOnlyChild(),
Right->getOnlyChild());
}

bool detectArrayMemberChange(SDKNodeType* L, SDKNodeType *R) {
if (!IsVisitingLeft)
return false;
// We only care if this the top-level type node.
if (!isa<SDKNodeDecl>(L->getParent()) || !isa<SDKNodeDecl>(R->getParent()))
return false;
StringRef KeyChangedTo;
if (L->getTypeKind() == KnownTypeKind::Optional &&
R->getTypeKind() == KnownTypeKind::Optional) {
// Detect [String]? to [StringRepresentableStruct]? Chnage
KeyChangedTo =
detectArrayMemberChangeInternal(L->getOnlyChild()->getAs<SDKNodeType>(),
R->getOnlyChild()->getAs<SDKNodeType>());
} else {
// Detect [String] to [StringRepresentableStruct] Chnage
KeyChangedTo = detectArrayMemberChangeInternal(L, R);
}
if (!KeyChangedTo.empty()) {
L->annotate(L->getTypeKind() == KnownTypeKind::Optional ?
NodeAnnotation::OptionalArrayMemberUpdate :
NodeAnnotation::ArrayMemberUpdate);
L->annotate(NodeAnnotation::TypeRewrittenRight, KeyChangedTo);
return true;
}
return false;
}

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

static StringRef getRightComment(NodePtr Node, NodeAnnotation Anno) {
switch (Anno) {
case NodeAnnotation::ArrayMemberUpdate:
case NodeAnnotation::OptionalArrayMemberUpdate:
case NodeAnnotation::DictionaryKeyUpdate:
case NodeAnnotation::OptionalDictionaryKeyUpdate:
case NodeAnnotation::TypeRewritten:
Expand Down Expand Up @@ -2948,6 +2997,8 @@ class DiffItemEmitter : public SDKNodeVisitor {
NodeAnnotation::NowThrowing,
NodeAnnotation::DictionaryKeyUpdate,
NodeAnnotation::OptionalDictionaryKeyUpdate,
NodeAnnotation::ArrayMemberUpdate,
NodeAnnotation::OptionalArrayMemberUpdate,
});
}

Expand Down