Skip to content

Commit 0d23f6f

Browse files
authored
Merge pull request #16483 from nkcsgexi/migrator-cherry-pick-05-09
[4.2] cherry pick migrator changes.
2 parents 6850a67 + bc95428 commit 0d23f6f

File tree

11 files changed

+119
-35
lines changed

11 files changed

+119
-35
lines changed

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,6 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
347347
Editor.replace(Range, RepText);
348348
return true;
349349
}
350-
if (updateStringRepresentableDeclRef(Item, Range)) {
351-
return true;
352-
}
353350
}
354351
return true;
355352
}
@@ -683,20 +680,30 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
683680
auto *RD = getReferencedDecl(Reference);
684681
if (!RD)
685682
return false;
683+
std::string Func;
684+
std::string Rename;
686685
for (auto *Item: getRelatedDiffItems(RD)) {
687-
if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
686+
if (isSimpleReplacement(Item, Rename)) {
687+
} else if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
688688
if (CI->isStringRepresentableChange() &&
689689
CI->NodeKind == SDKNodeKind::DeclVar) {
690690
SmallString<256> Buffer;
691-
auto Func = insertHelperFunction(CI->DiffKind, CI->RightComment,
692-
Buffer, FromString);
693-
Editor.insert(WrapperTarget->getStartLoc(), (Twine(Func) + "(").str());
694-
Editor.insertAfterToken(WrapperTarget->getEndLoc(), ")");
695-
return true;
691+
Func = insertHelperFunction(CI->DiffKind, CI->RightComment, Buffer,
692+
FromString);
696693
}
697694
}
698695
}
699-
return false;
696+
if (Func.empty())
697+
return false;
698+
699+
Editor.insert(WrapperTarget->getStartLoc(), (Twine(Func) + "(").str());
700+
Editor.insertAfterToken(WrapperTarget->getEndLoc(), ")");
701+
if (!Rename.empty()) {
702+
auto Range = CharSourceRange(SM, Reference->getStartLoc(),
703+
Lexer::getLocForEndOfToken(SM, Reference->getEndLoc()));
704+
Editor.replace(Range, Rename);
705+
}
706+
return true;
700707
}
701708

702709
bool handleAssignDestMigration(Expr *E) {
@@ -827,7 +834,9 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
827834
bool walkToExprPre(Expr *E) override {
828835
if (handleQualifiedReplacement(E))
829836
return false;
830-
if (handleAssignDestMigration(E) || handleAttributeReference(E))
837+
if (handleAssignDestMigration(E))
838+
return false;
839+
if (handleAttributeReference(E))
831840
return false;
832841
if (auto *CE = dyn_cast<CallExpr>(E)) {
833842
auto Fn = CE->getFn();

test/Migrator/Inputs/Cities.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ public class ToplevelType {
5959
public init() {}
6060
public init(recordName: String) {}
6161
}
62+
63+
public var GlobalAttribute: String = ""

test/Migrator/Inputs/string-representable.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,23 @@
197197
"RightComment": "SimpleAttribute",
198198
"ModuleName": "Cities"
199199
},
200+
{
201+
"DiffItemKind": "CommonDiffItem",
202+
"NodeKind": "Var",
203+
"NodeAnnotation": "SimpleStringRepresentableUpdate",
204+
"ChildIndex": "0",
205+
"LeftUsr": "s:6Cities15GlobalAttributeSSvp",
206+
"LeftComment": "",
207+
"RightUsr": "",
208+
"RightComment": "NewAttribute",
209+
"ModuleName": "Cities"
210+
},
211+
{
212+
"DiffItemKind": "TypeMemberDiffItem",
213+
"Usr": "s:6Cities15GlobalAttributeSSvp",
214+
"OldPrintedName": "GlobalAttribute",
215+
"OldTypeName": "",
216+
"NewPrintedName": "NewAttribute",
217+
"NewTypeName": "AttributeWrapper"
218+
},
200219
]

test/Migrator/string-representable.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ func foo(_ c: Container) -> String {
3434
c.adding(attributes: c.attrDict)
3535
_ = Container(optionalAttrArray: c.attrArr)
3636
c.adding(optionalAttributes: c.optionalAttrDict)
37+
_ = GlobalAttribute
3738
return c.Value
3839
}

test/Migrator/string-representable.swift.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func foo(_ c: Container) -> String {
3434
c.adding(attributes: convertToSimpleAttributeDictionary(convertFromSimpleAttributeDictionary(c.attrDict)))
3535
_ = Container(optionalAttrArray: convertToOptionalSimpleAttributeArray(convertFromSimpleAttributeArray(c.attrArr)))
3636
c.adding(optionalAttributes: convertToOptionalSimpleAttributeDictionary(convertFromOptionalSimpleAttributeDictionary(c.optionalAttrDict)))
37+
_ = convertFromNewAttribute(AttributeWrapper.NewAttribute)
3738
return convertFromNewAttribute(c.Value)
3839
}
3940

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ extern int ANTGlobalValue;
2626
+ (nonnull AnimalStatusDescriptor *)animalStatusSingleOptionalAttribute:(nullable NSString *)attributes;
2727
+ (nonnull AnimalStatusDescriptor *)animalStatusSingleAttribute:(nonnull NSString *)attributes;
2828
@end
29+
30+
extern NSString *globalAttributeName;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ typedef NSString * AnimalAttributeName NS_STRING_ENUM;
2626
+ (nonnull AnimalStatusDescriptor *)animalStatusSingleOptionalAttribute:(nullable AnimalAttributeName)attributes;
2727
+ (nonnull AnimalStatusDescriptor *)animalStatusSingleAttribute:(nonnull AnimalAttributeName)attributes;
2828
@end
29+
30+
extern AnimalAttributeName globalAttributeName;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ APINotesTest(APINotesTest.h): TypeAlias AnimalAttributeName(NSString) is now Str
77
/* Moved Decls */
88

99
/* Renamed Decls */
10+
APINotesTest(APINotesTest.h): Var globalAttributeName has been renamed to Var AnimalAttributeName.globalAttributeName
1011

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ APINotesTest(APINotesTest.h): Func SwiftTypeWithMethodLeft.getPropertyA() has be
1111
/* Renamed Decls */
1212
APINotesTest(APINotesTest.h): Protocol SwiftTypeWithMethodLeft has been renamed to Protocol SwiftTypeWithMethodRight
1313
APINotesTest(APINotesTest.h): Var OldType.oldMember has been renamed to Var NewType.newMember
14+
APINotesTest(APINotesTest.h): Var globalAttributeName has been renamed to Var AnimalAttributeName.globalAttributeName
1415

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

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,25 @@
230230
"RightComment": "SwiftTypeWithMethodRight",
231231
"ModuleName": "APINotesTest"
232232
},
233+
{
234+
"DiffItemKind": "CommonDiffItem",
235+
"NodeKind": "Var",
236+
"NodeAnnotation": "SimpleStringRepresentableUpdate",
237+
"ChildIndex": "0",
238+
"LeftUsr": "c:@globalAttributeName",
239+
"LeftComment": "",
240+
"RightUsr": "",
241+
"RightComment": "AnimalAttributeName",
242+
"ModuleName": "APINotesTest"
243+
},
244+
{
245+
"DiffItemKind": "TypeMemberDiffItem",
246+
"Usr": "c:@globalAttributeName",
247+
"OldPrintedName": "globalAttributeName",
248+
"OldTypeName": "",
249+
"NewPrintedName": "globalAttributeName",
250+
"NewTypeName": "AnimalAttributeName"
251+
},
233252
{
234253
"DiffItemKind": "TypeMemberDiffItem",
235254
"Usr": "c:@ANTGlobalValue",

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

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,9 +2628,8 @@ class SearchVisitor : public SDKNodeVisitor {
26282628
}
26292629
};
26302630

2631-
class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
2631+
class InterfaceTypeChangeDetector {
26322632
bool IsVisitingLeft;
2633-
UpdatedNodesMap &UpdateMap;
26342633

26352634
#define ANNOTATE(Node, Counter, X, Y) \
26362635
auto ToAnnotate = IsVisitingLeft ? Node : Counter; \
@@ -2828,41 +2827,57 @@ class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
28282827
return false;
28292828
}
28302829

2831-
bool isUnhandledCase(SDKNodeType *Node) {
2832-
auto Counter = UpdateMap.findUpdateCounterpart(Node)->getAs<SDKNodeType>();
2830+
bool isUnhandledCase(SDKNodeType *Node, SDKNodeType *Counter) {
28332831
return Node->getTypeKind() == KnownTypeKind::Void ||
28342832
Counter->getTypeKind() == KnownTypeKind::Void;
28352833
}
28362834

28372835
public:
2838-
ChangeRefinementPass(UpdatedNodesMap &UpdateMap) : UpdateMap(UpdateMap) {}
2836+
InterfaceTypeChangeDetector(bool IsVisitingLeft):
2837+
IsVisitingLeft(IsVisitingLeft) {}
2838+
void detect(SDKNode *Left, SDKNode *Right) {
2839+
auto *Node = dyn_cast<SDKNodeType>(Left);
2840+
auto *Counter = dyn_cast<SDKNodeType>(Right);
2841+
if (!Node || !Counter || isUnhandledCase(Node, Counter))
2842+
return;
2843+
bool Result = detectWrapOptional(Node, Counter)||
2844+
detectOptionalUpdate(Node, Counter)||
2845+
detectWrapImplicitOptional(Node, Counter)||
2846+
detectUnmanagedUpdate(Node, Counter)||
2847+
detectDictionaryKeyChange(Node, Counter) ||
2848+
detectArrayMemberChange(Node, Counter) ||
2849+
detectSimpleStringRepresentableUpdate(Node, Counter) ||
2850+
detectTypeRewritten(Node, Counter);
2851+
(void) Result;
2852+
return;
2853+
}
2854+
};
2855+
2856+
class ChangeRefinementPass : public SDKTreeDiffPass, public SDKNodeVisitor {
2857+
UpdatedNodesMap &UpdateMap;
2858+
InterfaceTypeChangeDetector LeftDetector;
2859+
InterfaceTypeChangeDetector RightDetector;
2860+
InterfaceTypeChangeDetector *Detector;
2861+
2862+
public:
2863+
ChangeRefinementPass(UpdatedNodesMap &UpdateMap) : UpdateMap(UpdateMap),
2864+
LeftDetector(true), RightDetector(false), Detector(nullptr) {}
28392865

28402866
void pass(NodePtr Left, NodePtr Right) override {
28412867

28422868
// Post-order visit is necessary since we propagate annotations bottom-up
2843-
IsVisitingLeft = true;
2869+
Detector = &LeftDetector;
28442870
SDKNode::postorderVisit(Left, *this);
2845-
IsVisitingLeft = false;
2871+
Detector = &RightDetector;
28462872
SDKNode::postorderVisit(Right, *this);
28472873
}
28482874

2849-
void visit(NodePtr N) override {
2850-
auto Node = dyn_cast<SDKNodeType>(N);
2851-
if (!Node || !Node->isAnnotatedAs(NodeAnnotation::Updated) ||
2852-
isUnhandledCase(Node))
2875+
void visit(NodePtr Node) override {
2876+
assert(Detector);
2877+
if (!Node || !Node->isAnnotatedAs(NodeAnnotation::Updated))
28532878
return;
2854-
auto Counter = const_cast<SDKNodeType*>(UpdateMap.
2855-
findUpdateCounterpart(Node)->getAs<SDKNodeType>());
2856-
2857-
bool Result = detectWrapOptional(Node, Counter)||
2858-
detectOptionalUpdate(Node, Counter)||
2859-
detectWrapImplicitOptional(Node, Counter)||
2860-
detectUnmanagedUpdate(Node, Counter)||
2861-
detectDictionaryKeyChange(Node, Counter) ||
2862-
detectArrayMemberChange(Node, Counter) ||
2863-
detectSimpleStringRepresentableUpdate(Node, Counter) ||
2864-
detectTypeRewritten(Node, Counter);
2865-
(void) Result;
2879+
auto *Counter = UpdateMap.findUpdateCounterpart(Node);
2880+
Detector->detect(Node, Counter);
28662881
return;
28672882
}
28682883
};
@@ -3585,9 +3600,15 @@ class OverloadMemberFunctionEmitter : public SDKNodeVisitor {
35853600
namespace fs = llvm::sys::fs;
35863601
namespace path = llvm::sys::path;
35873602

3588-
struct RenameDetectorForMemberDiff : public MatchedNodeListener {
3603+
class RenameDetectorForMemberDiff : public MatchedNodeListener {
3604+
InterfaceTypeChangeDetector LeftDetector;
3605+
InterfaceTypeChangeDetector RightDetector;
3606+
public:
3607+
RenameDetectorForMemberDiff(): LeftDetector(true), RightDetector(false) {}
35893608
void foundMatch(NodePtr Left, NodePtr Right) override {
35903609
detectRename(Left, Right);
3610+
LeftDetector.detect(Left, Right);
3611+
RightDetector.detect(Right, Left);
35913612
}
35923613
void workOn(NodePtr Left, NodePtr Right) {
35933614
if (Left->getKind() == Right->getKind() &&
@@ -3596,6 +3617,12 @@ struct RenameDetectorForMemberDiff : public MatchedNodeListener {
35963617
*this);
35973618
SNMatcher.match();
35983619
}
3620+
if (Left->getKind() == Right->getKind() &&
3621+
Left->getKind() == SDKNodeKind::DeclVar) {
3622+
SequentialNodeMatcher Matcher(Left->getChildren(),
3623+
Right->getChildren(), *this);
3624+
Matcher.match();
3625+
}
35993626
}
36003627
};
36013628

0 commit comments

Comments
 (0)