Skip to content

Commit 65eaccb

Browse files
committed
migrator: teach the tool to handle ArrayMemberUpdate and OptionalArrayMemberUpdate attributes emitted from swift-api-digester.
This bridges the function call arguments that used to be of type [String] and now become [StringRepresentableStruct].
1 parent 0f32529 commit 65eaccb

File tree

5 files changed

+56
-12
lines changed

5 files changed

+56
-12
lines changed

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
714714
case NodeAnnotation::OptionalArrayMemberUpdate:
715715
Segs = {"Optional", "Array", "[String]?"};
716716
Segs.push_back((Twine("[") + NewType +"]?").str());
717-
Segs.push_back("// Not implemented");
717+
Segs.push_back(Twine("\tguard let input = input else { return nil }\n"
718+
"\treturn input.map { key in " + NewType +"(key) }").str());
718719
break;
719720
case NodeAnnotation::OptionalDictionaryKeyUpdate:
720721
Segs = {"Optional", "Dictionary", "[String: Any]?"};
@@ -726,7 +727,7 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
726727
case NodeAnnotation::ArrayMemberUpdate:
727728
Segs = {"", "Array", "[String]"};
728729
Segs.push_back((Twine("[") + NewType +"]").str());
729-
Segs.push_back("// Not implemented");
730+
Segs.push_back(Twine("\treturn input.map { key in " + NewType +"(key) }").str());
730731
break;
731732
case NodeAnnotation::DictionaryKeyUpdate:
732733
Segs = {"", "Dictionary", "[String: Any]"};
@@ -778,17 +779,21 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
778779
}
779780
}
780781
}
781-
if (NewAttributeType.empty() || !ArgIdx)
782-
return;
783-
ArgIdx --;
784-
auto AllArgs = getCallArgInfo(SM, Arg, LabelRangeEndAt::LabelNameOnly);
785-
if (AllArgs.size() <= ArgIdx)
782+
if (NewAttributeType.empty())
786783
return;
787-
SmallString<256> Buffer;
788-
auto FuncName = insertHelperFunction(Kind, NewAttributeType, Buffer);
789-
auto Exp = AllArgs[ArgIdx].ArgExp;
790-
Editor.insert(Exp->getStartLoc(), (Twine(FuncName) + "(").str());
791-
Editor.insertAfterToken(Exp->getEndLoc(), ")");
784+
if (ArgIdx) {
785+
ArgIdx --;
786+
auto AllArgs = getCallArgInfo(SM, Arg, LabelRangeEndAt::LabelNameOnly);
787+
if (AllArgs.size() <= ArgIdx)
788+
return;
789+
SmallString<256> Buffer;
790+
auto FuncName = insertHelperFunction(Kind, NewAttributeType, Buffer);
791+
auto Exp = AllArgs[ArgIdx].ArgExp;
792+
Editor.insert(Exp->getStartLoc(), (Twine(FuncName) + "(").str());
793+
Editor.insertAfterToken(Exp->getEndLoc(), ")");
794+
} else {
795+
// FIXME: return value migration.
796+
}
792797
}
793798

794799
bool walkToExprPre(Expr *E) override {

test/Migrator/Inputs/Cities.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ public class Container {
4040
public func adding(attributes: [String: Any]) {}
4141
public func adding(optionalAttributes: [String: Any]?) {}
4242
public init(optionalAttributes: [String: Any]?) {}
43+
public func adding(attrArray: [String]) {}
44+
public init(optionalAttrArray: [String]?) {}
4345
}

test/Migrator/Inputs/string-representable.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,26 @@
5454
"RightComment": "SimpleAttribute",
5555
"ModuleName": "Cities"
5656
},
57+
{
58+
"DiffItemKind": "CommonDiffItem",
59+
"NodeKind": "Function",
60+
"NodeAnnotation": "ArrayMemberUpdate",
61+
"ChildIndex": "1",
62+
"LeftUsr": "s:6Cities9ContainerC6adding9attrArrayySaySSG_tF",
63+
"LeftComment": "",
64+
"RightUsr": "",
65+
"RightComment": "SimpleAttribute",
66+
"ModuleName": "Cities"
67+
},
68+
{
69+
"DiffItemKind": "CommonDiffItem",
70+
"NodeKind": "Function",
71+
"NodeAnnotation": "OptionalArrayMemberUpdate",
72+
"ChildIndex": "1",
73+
"LeftUsr": "s:6Cities9ContainerC17optionalAttrArrayACSaySSGSg_tcfc",
74+
"LeftComment": "",
75+
"RightUsr": "",
76+
"RightComment": "SimpleAttribute",
77+
"ModuleName": "Cities"
78+
},
5779
]

test/Migrator/string-representable.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ func foo(_ c: Container) -> String {
1313
c.adding(attributes: ["a": 1, "a": 2, "a": 3])
1414
c.adding(optionalAttributes: ["a": 1, "a": 2, "a": 3])
1515
_ = Container(optionalAttributes: nil)
16+
_ = Container(optionalAttrArray: nil)
17+
c.adding(attrArray: ["key1", "key2"])
1618
return c.Value
1719
}

test/Migrator/string-representable.swift.expected

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ func foo(_ c: Container) -> String {
1313
c.adding(attributes: converToSimpleAttributeDictionary(["a": 1, "a": 2, "a": 3]))
1414
c.adding(optionalAttributes: converToOptionalSimpleAttributeDictionary(["a": 1, "a": 2, "a": 3]))
1515
_ = Container(optionalAttributes: converToOptionalSimpleAttributeDictionary(nil))
16+
_ = Container(optionalAttrArray: converToOptionalSimpleAttributeArray(nil))
17+
c.adding(attrArray: converToSimpleAttributeArray(["key1", "key2"]))
1618
return c.Value.rawValue
1719
}
1820

@@ -31,3 +33,14 @@ fileprivate func converToOptionalSimpleAttributeDictionary(_ input: [String: Any
3133
guard let input = input else { return nil }
3234
return Dictionary(uniqueKeysWithValues: input.map { key, value in (SimpleAttribute(rawValue: key), value)})
3335
}
36+
37+
// Helper function inserted by Swift 4.2 migrator.
38+
fileprivate func converToOptionalSimpleAttributeArray(_ input: [String]?) -> [SimpleAttribute]? {
39+
guard let input = input else { return nil }
40+
return input.map { key in SimpleAttribute(key) }
41+
}
42+
43+
// Helper function inserted by Swift 4.2 migrator.
44+
fileprivate func converToSimpleAttributeArray(_ input: [String]) -> [SimpleAttribute] {
45+
return input.map { key in SimpleAttribute(key) }
46+
}

0 commit comments

Comments
 (0)