Skip to content

Commit e15c197

Browse files
committed
swift-api-digester: detect simple parameter/return type update from String to StringRepresentableStruct.
1 parent d9f7d14 commit e15c197

File tree

7 files changed

+80
-3
lines changed

7 files changed

+80
-3
lines changed

include/swift/IDE/DigesterEnums.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ NODE_ANNOTATION(DictionaryKeyUpdate)
7878
NODE_ANNOTATION(OptionalDictionaryKeyUpdate)
7979
NODE_ANNOTATION(ArrayMemberUpdate)
8080
NODE_ANNOTATION(OptionalArrayMemberUpdate)
81+
NODE_ANNOTATION(SimpleStringRepresentableUpdate)
82+
NODE_ANNOTATION(SimpleOptionalStringRepresentableUpdate)
8183

8284
DECL_ATTR(deprecated)
8385
DECL_ATTR(fixedLayout)

test/api-digester/Inputs/APINotesLeft/APINotesTest.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ extern int ANTGlobalValue;
2323
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingOptionalAttributes:(nullable NSDictionary<NSString*, id> *)attributes;
2424
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingAttributesArray:(nonnull NSArray<NSString*> *)attributes;
2525
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingOptionalAttributesArray:(nullable NSArray<NSString*> *)attributes;
26+
+ (nonnull AnimalStatusDescriptor *)animalStatusSingleOptionalAttribute:(nullable NSString *)attributes;
27+
+ (nonnull AnimalStatusDescriptor *)animalStatusSingleAttribute:(nonnull NSString *)attributes;
2628
@end

test/api-digester/Inputs/APINotesRight/APINotesTest.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ typedef NSString * AnimalAttributeName NS_STRING_ENUM;
2323
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingOptionalAttributes:(nullable NSDictionary<AnimalAttributeName, id> *)attributes;
2424
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingAttributesArray:(nonnull NSArray<AnimalAttributeName> *)attributes;
2525
- (nonnull AnimalStatusDescriptor *)animalStatusDescriptorByAddingOptionalAttributesArray:(nullable NSArray<AnimalAttributeName> *)attributes;
26-
26+
+ (nonnull AnimalStatusDescriptor *)animalStatusSingleOptionalAttribute:(nullable AnimalAttributeName)attributes;
27+
+ (nonnull AnimalStatusDescriptor *)animalStatusSingleAttribute:(nonnull AnimalAttributeName)attributes;
2728
@end

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingAttributes(_:) h
1313
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingAttributesArray(_:) has parameter 0 type change from [String] to [AnimalAttributeName]
1414
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingOptionalAttributes(_:) has parameter 0 type change from [String : Any]? to [AnimalAttributeName : Any]?
1515
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingOptionalAttributesArray(_:) has parameter 0 type change from [String]? to [AnimalAttributeName]?
16+
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.animalStatusSingleAttribute(_:) has parameter 0 type change from String to AnimalAttributeName
17+
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.animalStatusSingleOptionalAttribute(_:) has parameter 0 type change from String? to AnimalAttributeName?
1618

1719
/* Decl Attribute changes */

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingAttributes(_:) h
1717
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingAttributesArray(_:) has parameter 0 type change from [String] to [AnimalAttributeName]
1818
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingOptionalAttributes(_:) has parameter 0 type change from [String : Any]? to [AnimalAttributeName : Any]?
1919
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.addingOptionalAttributesArray(_:) has parameter 0 type change from [String]? to [AnimalAttributeName]?
20+
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.animalStatusSingleAttribute(_:) has parameter 0 type change from String to AnimalAttributeName
21+
APINotesTest(APINotesTest.h): Func AnimalStatusDescriptor.animalStatusSingleOptionalAttribute(_:) has parameter 0 type change from String? to AnimalAttributeName?
2022

2123
/* Decl Attribute changes */

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,39 @@
175175
"RightComment": "AnimalAttributeName",
176176
"ModuleName": "APINotesTest"
177177
},
178+
{
179+
"DiffItemKind": "CommonDiffItem",
180+
"NodeKind": "Function",
181+
"NodeAnnotation": "TypeRewritten",
182+
"ChildIndex": "1:0",
183+
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(cm)animalStatusSingleOptionalAttribute:",
184+
"LeftComment": "String",
185+
"RightUsr": "",
186+
"RightComment": "AnimalAttributeName",
187+
"ModuleName": "APINotesTest"
188+
},
189+
{
190+
"DiffItemKind": "CommonDiffItem",
191+
"NodeKind": "Function",
192+
"NodeAnnotation": "SimpleOptionalStringRepresentableUpdate",
193+
"ChildIndex": "1",
194+
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(cm)animalStatusSingleOptionalAttribute:",
195+
"LeftComment": "",
196+
"RightUsr": "",
197+
"RightComment": "AnimalAttributeName",
198+
"ModuleName": "APINotesTest"
199+
},
200+
{
201+
"DiffItemKind": "CommonDiffItem",
202+
"NodeKind": "Function",
203+
"NodeAnnotation": "SimpleStringRepresentableUpdate",
204+
"ChildIndex": "1",
205+
"LeftUsr": "c:objc(cs)AnimalStatusDescriptor(cm)animalStatusSingleAttribute:",
206+
"LeftComment": "",
207+
"RightUsr": "",
208+
"RightComment": "AnimalAttributeName",
209+
"ModuleName": "APINotesTest"
210+
},
178211
{
179212
"DiffItemKind": "CommonDiffItem",
180213
"NodeKind": "Function",

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ class SDKNodeType : public SDKNode {
516516
// When the type node represents a function parameter, this function returns
517517
// whether the parameter has a default value.
518518
bool hasDefaultArgument() const { return HasDefaultArg; }
519+
bool isTopLevelType() const { return !isa<SDKNodeType>(getParent()); }
519520
static bool classof(const SDKNode *N);
520521
};
521522

@@ -2730,7 +2731,7 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
27302731
return false;
27312732

27322733
// We only care if this the top-level type node.
2733-
if (!isa<SDKNodeDecl>(L->getParent()) || !isa<SDKNodeDecl>(R->getParent()))
2734+
if (!L->isTopLevelType() || !R->isTopLevelType())
27342735
return false;
27352736
StringRef KeyChangedTo;
27362737
if (L->getTypeKind() == KnownTypeKind::Optional &&
@@ -2771,7 +2772,7 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
27712772
if (!IsVisitingLeft)
27722773
return false;
27732774
// We only care if this the top-level type node.
2774-
if (!isa<SDKNodeDecl>(L->getParent()) || !isa<SDKNodeDecl>(R->getParent()))
2775+
if (!L->isTopLevelType() || !R->isTopLevelType())
27752776
return false;
27762777
StringRef KeyChangedTo;
27772778
if (L->getTypeKind() == KnownTypeKind::Optional &&
@@ -2794,6 +2795,35 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
27942795
return false;
27952796
}
27962797

2798+
bool detectSimpleStringRepresentableUpdate(SDKNodeType *L, SDKNodeType *R) {
2799+
if (!IsVisitingLeft)
2800+
return false;
2801+
if (!L->isTopLevelType() || !R->isTopLevelType())
2802+
return false;
2803+
StringRef KeyChangedTo;
2804+
bool HasOptional = L->getTypeKind() == KnownTypeKind::Optional &&
2805+
R->getTypeKind() == KnownTypeKind::Optional;
2806+
if (HasOptional) {
2807+
// Detect String? changes to StringRepresentableStruct? change.
2808+
KeyChangedTo =
2809+
getStringRepresentableChange(L->getOnlyChild()->getAs<SDKNodeType>(),
2810+
R->getOnlyChild()->getAs<SDKNodeType>());
2811+
} else {
2812+
// Detect String changes to StringRepresentableStruct change.
2813+
KeyChangedTo = getStringRepresentableChange(L, R);
2814+
}
2815+
if (!KeyChangedTo.empty()) {
2816+
L->annotate(NodeAnnotation::TypeRewrittenRight, KeyChangedTo);
2817+
if (HasOptional) {
2818+
L->annotate(NodeAnnotation::SimpleOptionalStringRepresentableUpdate);
2819+
} else {
2820+
L->annotate(NodeAnnotation::SimpleStringRepresentableUpdate);
2821+
}
2822+
return true;
2823+
}
2824+
return false;
2825+
}
2826+
27972827
bool isUnhandledCase(SDKNodeType *Node) {
27982828
auto Counter = UpdateMap.findUpdateCounterpart(Node)->getAs<SDKNodeType>();
27992829
return Node->getTypeKind() == KnownTypeKind::Void ||
@@ -2826,6 +2856,7 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
28262856
detectUnmanagedUpdate(Node, Counter)||
28272857
detectDictionaryKeyChange(Node, Counter) ||
28282858
detectArrayMemberChange(Node, Counter) ||
2859+
detectSimpleStringRepresentableUpdate(Node, Counter) ||
28292860
detectTypeRewritten(Node, Counter);
28302861
(void) Result;
28312862
return;
@@ -2938,6 +2969,8 @@ class DiffItemEmitter : public SDKNodeVisitor {
29382969
case NodeAnnotation::OptionalArrayMemberUpdate:
29392970
case NodeAnnotation::DictionaryKeyUpdate:
29402971
case NodeAnnotation::OptionalDictionaryKeyUpdate:
2972+
case NodeAnnotation::SimpleStringRepresentableUpdate:
2973+
case NodeAnnotation::SimpleOptionalStringRepresentableUpdate:
29412974
case NodeAnnotation::TypeRewritten:
29422975
return Node->getAnnotateComment(NodeAnnotation::TypeRewrittenRight);
29432976
case NodeAnnotation::ModernizeEnum:
@@ -2999,6 +3032,8 @@ class DiffItemEmitter : public SDKNodeVisitor {
29993032
NodeAnnotation::OptionalDictionaryKeyUpdate,
30003033
NodeAnnotation::ArrayMemberUpdate,
30013034
NodeAnnotation::OptionalArrayMemberUpdate,
3035+
NodeAnnotation::SimpleStringRepresentableUpdate,
3036+
NodeAnnotation::SimpleOptionalStringRepresentableUpdate,
30023037
});
30033038
}
30043039

0 commit comments

Comments
 (0)