Skip to content

Commit 4625b5a

Browse files
authored
Merge pull request #17305 from nkcsgexi/cherry-pick-06-18
[4.2-06-11] Cherry-pick migrator changes
2 parents 9792d6e + b0a5188 commit 4625b5a

File tree

8 files changed

+156
-11
lines changed

8 files changed

+156
-11
lines changed

include/swift/IDE/DigesterEnums.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ NODE_ANNOTATION_CHANGE_KIND(OptionalArrayMemberUpdate)
8080
NODE_ANNOTATION_CHANGE_KIND(SimpleStringRepresentableUpdate)
8181
NODE_ANNOTATION_CHANGE_KIND(SimpleOptionalStringRepresentableUpdate)
8282
NODE_ANNOTATION_CHANGE_KIND(TypeAliasDeclToRawRepresentable)
83+
NODE_ANNOTATION_CHANGE_KIND(RevertTypeAliasDeclToRawRepresentable)
8384

8485
NODE_ANNOTATION_CHANGE_KIND(RevertDictionaryKeyUpdate)
8586
NODE_ANNOTATION_CHANGE_KIND(RevertOptionalDictionaryKeyUpdate)

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
275275

276276
std::vector<APIDiffItem*> getRelatedDiffItems(ValueDecl *VD) {
277277
std::vector<APIDiffItem*> results;
278+
if (!VD)
279+
return results;
278280
auto addDiffItems = [&](ValueDecl *VD) {
279281
llvm::SmallString<64> Buffer;
280282
llvm::raw_svector_ostream OS(Buffer);
@@ -921,9 +923,66 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
921923
insertHelperFunction(Kind, RawType, NewAttributeType, FromString, WrapTarget);
922924
}
923925

926+
bool hasRevertRawRepresentableChange(ValueDecl *VD) {
927+
for (auto Item: getRelatedDiffItems(VD)) {
928+
if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
929+
if (CI->DiffKind ==
930+
NodeAnnotation::RevertTypeAliasDeclToRawRepresentable)
931+
return true;
932+
}
933+
}
934+
return false;
935+
}
936+
937+
bool handleRevertRawRepresentable(Expr *E) {
938+
// Change attribute.rawValue to attribute
939+
if (auto *MRE = dyn_cast<MemberRefExpr>(E)) {
940+
auto Found = false;
941+
if (auto *Base = MRE->getBase()) {
942+
if (hasRevertRawRepresentableChange(Base->getType()->getAnyNominal())) {
943+
Found = true;
944+
}
945+
}
946+
if (!Found)
947+
return false;
948+
auto NL = MRE->getNameLoc().getStartLoc();
949+
auto DL = MRE->getDotLoc();
950+
if (NL.isInvalid() || DL.isInvalid())
951+
return false;
952+
CharSourceRange Range = Lexer::getCharSourceRangeFromSourceRange(SM, {DL, NL});
953+
if (Range.str() == ".rawValue") {
954+
Editor.remove(Range);
955+
return true;
956+
}
957+
}
958+
959+
// Change attribute(rawValue: "value") to "value"
960+
if (auto *CE = dyn_cast<CallExpr>(E)) {
961+
auto Found = false;
962+
if (auto *CRC = dyn_cast<ConstructorRefCallExpr>(CE->getFn())) {
963+
if (auto *TE = dyn_cast<TypeExpr>(CRC->getBase())) {
964+
if (hasRevertRawRepresentableChange(TE->getInstanceType()->getAnyNominal()))
965+
Found = true;
966+
}
967+
}
968+
if (!Found)
969+
return false;
970+
std::vector<CallArgInfo> AllArgs =
971+
getCallArgInfo(SM, CE->getArg(), LabelRangeEndAt::LabelNameOnly);
972+
if (AllArgs.size() == 1 && AllArgs.front().LabelRange.str() == "rawValue") {
973+
Editor.replace(CE->getSourceRange(), Lexer::getCharSourceRangeFromSourceRange(SM,
974+
AllArgs.front().ArgExp->getSourceRange()).str());
975+
return true;
976+
}
977+
}
978+
return false;
979+
}
980+
924981
bool walkToExprPre(Expr *E) override {
925982
if (E->getSourceRange().isInvalid())
926983
return false;
984+
if (handleRevertRawRepresentable(E))
985+
return false;
927986
if (handleQualifiedReplacement(E))
928987
return false;
929988
if (handleAssignDestMigration(E))

test/Migrator/Inputs/Cities.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,17 @@ public enum FontWeight: Int {
7474
case Regular
7575
case Bold
7676
}
77+
78+
public struct AwesomeCityAttribute: RawRepresentable {
79+
public init?(rawValue: String) { self.rawValue = rawValue }
80+
public var rawValue: String
81+
public typealias RawValue = String
82+
}
83+
84+
public class Wrapper {
85+
public struct Attribute: RawRepresentable {
86+
public init?(rawValue: String) { self.rawValue = rawValue }
87+
public var rawValue: String
88+
public typealias RawValue = String
89+
}
90+
}

test/Migrator/Inputs/string-representable.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,26 @@
227227
"RightComment": "AwesomeIntWrapper",
228228
"ModuleName": "bar"
229229
},
230+
{
231+
"DiffItemKind": "CommonDiffItem",
232+
"NodeKind": "TypeDecl",
233+
"NodeAnnotation": "RevertTypeAliasDeclToRawRepresentable",
234+
"ChildIndex": "0",
235+
"LeftUsr": "s:6Cities20AwesomeCityAttributeV",
236+
"LeftComment": "String",
237+
"RightUsr": "",
238+
"RightComment": "String",
239+
"ModuleName": "Cities"
240+
},
241+
{
242+
"DiffItemKind": "CommonDiffItem",
243+
"NodeKind": "TypeDecl",
244+
"NodeAnnotation": "RevertTypeAliasDeclToRawRepresentable",
245+
"ChildIndex": "0",
246+
"LeftUsr": "s:6Cities7WrapperC9AttributeV",
247+
"LeftComment": "String",
248+
"RightUsr": "",
249+
"RightComment": "String",
250+
"ModuleName": "Cities"
251+
},
230252
]

test/Migrator/string-representable.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,12 @@ func foo(_ c: Container) -> String {
4545
}
4646

4747
class C: BarForwardDeclaredClass {}
48+
49+
func revert(_ a: AwesomeCityAttribute, b: Wrapper.Attribute) {
50+
_ = AwesomeCityAttribute(rawValue: "somevalue")
51+
_ = AwesomeCityAttribute.init(rawValue: "somevalue")
52+
_ = a.rawValue
53+
_ = Wrapper.Attribute(rawValue: "somevalue")
54+
_ = Wrapper.Attribute.init(rawValue: "somevalue")
55+
_ = b.rawValue
56+
}

test/Migrator/string-representable.swift.expected

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ func foo(_ c: Container) -> String {
4646

4747
class C: BarForwardDeclaredClass {}
4848

49+
func revert(_ a: AwesomeCityAttribute, b: Wrapper.Attribute) {
50+
_ = "somevalue"
51+
_ = "somevalue"
52+
_ = a
53+
_ = "somevalue"
54+
_ = "somevalue"
55+
_ = b
56+
}
57+
4958
// Helper function inserted by Swift 4.2 migrator.
5059
fileprivate func convertToNewAttribute(_ input: String) -> NewAttribute {
5160
return NewAttribute(rawValue: input)

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
[
2+
{
3+
"DiffItemKind": "CommonDiffItem",
4+
"NodeKind": "TypeDecl",
5+
"NodeAnnotation": "RevertTypeAliasDeclToRawRepresentable",
6+
"ChildIndex": "0",
7+
"LeftUsr": "c:APINotesTest.h@T@CatAttributeName",
8+
"LeftComment": "String",
9+
"RightUsr": "",
10+
"RightComment": "NSString",
11+
"ModuleName": "APINotesTest"
12+
},
213
{
314
"DiffItemKind": "CommonDiffItem",
415
"NodeKind": "Function",

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ class SDKContext {
252252
llvm::BumpPtrAllocator Allocator;
253253
UpdatedNodesMap UpdateMap;
254254
NodeMap TypeAliasUpdateMap;
255+
NodeMap RevertTypeAliasUpdateMap;
255256
TypeMemberDiffVector TypeMemberDiffs;
256257
public:
257258
llvm::BumpPtrAllocator &allocator() {
@@ -266,6 +267,9 @@ class SDKContext {
266267
NodeMap &getTypeAliasUpdateMap() {
267268
return TypeAliasUpdateMap;
268269
}
270+
NodeMap &getRevertTypeAliasUpdateMap() {
271+
return RevertTypeAliasUpdateMap;
272+
}
269273
TypeMemberDiffVector &getTypeMemberDiffs() {
270274
return TypeMemberDiffs;
271275
}
@@ -3793,6 +3797,27 @@ static int diagnoseModuleChange(StringRef LeftPath, StringRef RightPath) {
37933797
return 0;
37943798
}
37953799

3800+
static void populateAliasChanges(NodeMap &AliasMap, DiffVector &AllItems,
3801+
const bool isRevert) {
3802+
for (auto Pair: AliasMap) {
3803+
auto UnderlyingType = Pair.first->getAs<SDKNodeDeclTypeAlias>()->
3804+
getUnderlyingType()->getPrintedName();
3805+
auto RawType = AliasMap[(SDKNode*)Pair.first]->getAs<SDKNodeDeclType>()->
3806+
getRawValueType()->getPrintedName();
3807+
if (isRevert) {
3808+
auto *D = Pair.second->getAs<SDKNodeDecl>();
3809+
AllItems.emplace_back(SDKNodeKind::DeclType,
3810+
NodeAnnotation::RevertTypeAliasDeclToRawRepresentable, "0",
3811+
D->getUsr(), "", RawType, UnderlyingType, D->getModuleName());
3812+
} else {
3813+
auto *D = Pair.first->getAs<SDKNodeDecl>();
3814+
AllItems.emplace_back(SDKNodeKind::DeclTypeAlias,
3815+
NodeAnnotation::TypeAliasDeclToRawRepresentable, "0",
3816+
D->getUsr(), "", UnderlyingType, RawType, D->getModuleName());
3817+
}
3818+
}
3819+
}
3820+
37963821
static int compareSDKs(StringRef LeftPath, StringRef RightPath,
37973822
StringRef DiffPath,
37983823
llvm::StringSet<> &IgnoredRemoveUsrs) {
@@ -3827,20 +3852,15 @@ static int compareSDKs(StringRef LeftPath, StringRef RightPath,
38273852
DiffVector AllItems;
38283853
DiffItemEmitter::collectDiffItems(LeftModule, AllItems);
38293854

3830-
auto &AliasMap = Ctx.getTypeAliasUpdateMap();
38313855
// Find type alias change first.
3856+
auto &AliasMap = Ctx.getTypeAliasUpdateMap();
38323857
TypeAliasDiffFinder(LeftModule, RightModule, AliasMap).search();
3858+
populateAliasChanges(AliasMap, AllItems, /*IsRevert*/false);
38333859

3834-
for (auto Pair: AliasMap) {
3835-
auto Left = Pair.first->getAs<SDKNodeDeclTypeAlias>()->getUnderlyingType()->
3836-
getPrintedName();
3837-
auto Right = AliasMap[(SDKNode*)Pair.first]->getAs<SDKNodeDeclType>()->
3838-
getRawValueType()->getPrintedName();
3839-
auto *D = Pair.first->getAs<SDKNodeDecl>();
3840-
AllItems.emplace_back(SDKNodeKind::DeclTypeAlias,
3841-
NodeAnnotation::TypeAliasDeclToRawRepresentable, "0",
3842-
D->getUsr(), "", Left, Right, D->getModuleName());
3843-
}
3860+
// Find type alias revert change.
3861+
auto &RevertAliasMap = Ctx.getRevertTypeAliasUpdateMap();
3862+
TypeAliasDiffFinder(RightModule, LeftModule, RevertAliasMap).search();
3863+
populateAliasChanges(RevertAliasMap, AllItems, /*IsRevert*/true);
38443864

38453865
AllItems.erase(std::remove_if(AllItems.begin(), AllItems.end(),
38463866
[&](CommonDiffItem &Item) {

0 commit comments

Comments
 (0)