Skip to content

Commit 6c0c9ac

Browse files
authored
Merge pull request #16605 from nkcsgexi/cherry-migrator-changes-05-14-04-30
[4.2-04-30] Cherry-pick migrator changes
2 parents d23e583 + 9f56068 commit 6c0c9ac

File tree

10 files changed

+78
-30
lines changed

10 files changed

+78
-30
lines changed

include/swift/IDE/APIDigesterData.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ enum class TypeMemberDiffItemSubKind {
247247
HoistSelfOnly,
248248
HoistSelfAndRemoveParam,
249249
HoistSelfAndUseProperty,
250+
FuncRename,
250251
};
251252

252253
struct TypeMemberDiffItem: public APIDiffItem {

lib/IDE/APIDigesterData.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,10 @@ swift::ide::api::TypeMemberDiffItem::getSubKind() const {
148148
assert(OldName.argSize() == 0);
149149
assert(!removedIndex);
150150
return TypeMemberDiffItemSubKind::GlobalFuncToStaticProperty;
151-
} else if (oldTypeName.empty()){
151+
} else if (oldTypeName.empty()) {
152+
// we can handle this as a simple function rename.
152153
assert(NewName.argSize() == OldName.argSize());
153-
return TypeMemberDiffItemSubKind::SimpleReplacement;
154+
return TypeMemberDiffItemSubKind::FuncRename;
154155
} else {
155156
assert(NewName.argSize() == OldName.argSize());
156157
return TypeMemberDiffItemSubKind::QualifiedReplacement;

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
247247

248248
APIDiffItemStore DiffStore;
249249

250+
bool isNilExpr(Expr *E) {
251+
auto Range = E->getSourceRange();
252+
return Range.isValid() && Lexer::getCharSourceRangeFromSourceRange(
253+
SF->getASTContext().SourceMgr, Range).str() == "nil";
254+
}
255+
250256
std::vector<APIDiffItem*> getRelatedDiffItems(ValueDecl *VD) {
251257
std::vector<APIDiffItem*> results;
252258
auto addDiffItems = [&](ValueDecl *VD) {
@@ -266,7 +272,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
266272
return results;
267273
}
268274

269-
DeclNameViewer getFuncRename(ValueDecl *VD, bool &IgnoreBase) {
275+
DeclNameViewer getFuncRename(ValueDecl *VD, llvm::SmallString<32> &Buffer,
276+
bool &IgnoreBase) {
270277
for (auto *Item: getRelatedDiffItems(VD)) {
271278
if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
272279
if (CI->isRename()) {
@@ -282,6 +289,13 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
282289
}
283290
}
284291
}
292+
if (auto *MI = dyn_cast<TypeMemberDiffItem>(Item)) {
293+
if (MI->Subkind == TypeMemberDiffItemSubKind::FuncRename) {
294+
llvm::raw_svector_ostream OS(Buffer);
295+
OS << MI->newTypeName << "." << MI->newPrintedName;
296+
return DeclNameViewer(OS.str());
297+
}
298+
}
285299
}
286300
return DeclNameViewer();
287301
}
@@ -413,7 +427,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
413427

414428
void handleFuncRename(ValueDecl *FD, Expr* FuncRefContainer, Expr *Arg) {
415429
bool IgnoreBase = false;
416-
if (auto View = getFuncRename(FD, IgnoreBase)) {
430+
llvm::SmallString<32> Buffer;
431+
if (auto View = getFuncRename(FD, Buffer, IgnoreBase)) {
417432
if (!IgnoreBase) {
418433
ReferenceCollector Walker(FD);
419434
Walker.walk(FuncRefContainer);
@@ -575,7 +590,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
575590
if (!Item)
576591
return false;
577592
if (Item->Subkind == TypeMemberDiffItemSubKind::SimpleReplacement ||
578-
Item->Subkind == TypeMemberDiffItemSubKind::QualifiedReplacement)
593+
Item->Subkind == TypeMemberDiffItemSubKind::QualifiedReplacement ||
594+
Item->Subkind == TypeMemberDiffItemSubKind::FuncRename)
579595
return false;
580596

581597
if (Item->Subkind == TypeMemberDiffItemSubKind::GlobalFuncToStaticProperty) {
@@ -624,6 +640,7 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
624640
}
625641

626642
switch (Item->Subkind) {
643+
case TypeMemberDiffItemSubKind::FuncRename:
627644
case TypeMemberDiffItemSubKind::GlobalFuncToStaticProperty:
628645
case TypeMemberDiffItemSubKind::SimpleReplacement:
629646
case TypeMemberDiffItemSubKind::QualifiedReplacement:
@@ -696,24 +713,27 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
696713
auto *RD = getReferencedDecl(Reference);
697714
if (!RD)
698715
return false;
699-
std::string Func;
700716
std::string Rename;
717+
Optional<NodeAnnotation> Kind;
718+
StringRef RightComment;
701719
for (auto *Item: getRelatedDiffItems(RD)) {
702720
if (isSimpleReplacement(Item, Rename)) {
703721
} else if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
704722
if (CI->isStringRepresentableChange() &&
705723
CI->NodeKind == SDKNodeKind::DeclVar) {
706-
SmallString<256> Buffer;
707-
Func = insertHelperFunction(CI->DiffKind, CI->RightComment, Buffer,
708-
FromString);
724+
Kind = CI->DiffKind;
725+
RightComment = CI->RightComment;
709726
}
710727
}
711728
}
712-
if (Func.empty())
729+
if (!Kind.hasValue())
713730
return false;
714-
715-
Editor.insert(WrapperTarget->getStartLoc(), (Twine(Func) + "(").str());
716-
Editor.insertAfterToken(WrapperTarget->getEndLoc(), ")");
731+
if (Kind && !isNilExpr(WrapperTarget)) {
732+
SmallString<256> Buffer;
733+
auto Func = insertHelperFunction(*Kind, RightComment, Buffer, FromString);
734+
Editor.insert(WrapperTarget->getStartLoc(), (Twine(Func) + "(").str());
735+
Editor.insertAfterToken(WrapperTarget->getEndLoc(), ")");
736+
}
717737
if (!Rename.empty()) {
718738
replaceExpr(Reference, Rename);
719739
}
@@ -838,21 +858,25 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
838858
}
839859
if (NewAttributeType.empty())
840860
return;
841-
SmallString<256> Buffer;
842-
auto FuncName = insertHelperFunction(Kind, NewAttributeType, Buffer,
843-
/*FromString*/ArgIdx);
861+
Expr *WrapTarget = Call;
862+
bool FromString = false;
844863
if (ArgIdx) {
845864
ArgIdx --;
865+
FromString = true;
846866
auto AllArgs = getCallArgInfo(SM, Arg, LabelRangeEndAt::LabelNameOnly);
847867
if (AllArgs.size() <= ArgIdx)
848868
return;
849-
auto Exp = AllArgs[ArgIdx].ArgExp;
850-
Editor.insert(Exp->getStartLoc(), (Twine(FuncName) + "(").str());
851-
Editor.insertAfterToken(Exp->getEndLoc(), ")");
852-
} else {
853-
Editor.insert(Call->getStartLoc(), (Twine(FuncName) + "(").str());
854-
Editor.insertAfterToken(Call->getEndLoc(), ")");
869+
WrapTarget = AllArgs[ArgIdx].ArgExp;
870+
// Avoid wrapping nil literal.
871+
if (isNilExpr(WrapTarget))
872+
return;
855873
}
874+
assert(WrapTarget);
875+
SmallString<256> Buffer;
876+
auto FuncName = insertHelperFunction(Kind, NewAttributeType, Buffer,
877+
FromString);
878+
Editor.insert(WrapTarget->getStartLoc(), (Twine(FuncName) + "(").str());
879+
Editor.insertAfterToken(WrapTarget->getEndLoc(), ")");
856880
}
857881

858882
bool walkToExprPre(Expr *E) override {
@@ -904,7 +928,8 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
904928
void handleFuncDeclRename(AbstractFunctionDecl *AFD,
905929
CharSourceRange NameRange) {
906930
bool IgnoreBase = false;
907-
if (auto View = getFuncRename(AFD, IgnoreBase)) {
931+
llvm::SmallString<32> Buffer;
932+
if (auto View = getFuncRename(AFD, Buffer, IgnoreBase)) {
908933
if (!IgnoreBase)
909934
Editor.replace(NameRange, View.base());
910935
unsigned Index = 0;

test/Migrator/Inputs/API.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,5 +509,13 @@
509509
"RightUsr": "",
510510
"RightComment": "BarBase.Nested",
511511
"ModuleName": "bar"
512-
}
512+
},
513+
{
514+
"DiffItemKind": "TypeMemberDiffItem",
515+
"Usr": "c:@F@barGlobalHoistedFuncOldName",
516+
"OldPrintedName": "barGlobalHoistedFuncOldName(_:_:_:)",
517+
"OldTypeName": "",
518+
"NewPrintedName": "newName(is:at:for:)",
519+
"NewTypeName": "AwesomeWrapper"
520+
},
513521
]

test/Migrator/mock-sdk/Bar.framework/Headers/Bar.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ int barGlobalVariableOldEnumElement = 1;
1010

1111
int barGlobalFuncOldName(int a);
1212

13+
int barGlobalHoistedFuncOldName(int a, int b, int c);
14+
1315
@interface BarForwardDeclaredClass
1416
- (id _Nonnull)initWithOldLabel0:(int)frame;
1517
- (void) barInstanceFunc0;

test/Migrator/rename.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func foo(_ b: BarForwardDeclaredClass) {
1818
_ = barGlobalVariableOldEnumElement
1919
_ = PropertyUserInterface.methodPlus()
2020
let _: BarBaseNested
21+
_ = barGlobalHoistedFuncOldName(0, 1, 2)
2122
}
2223

2324
func foo1(_ b: BarForwardDeclaredClass) {

test/Migrator/rename.swift.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func foo(_ b: BarForwardDeclaredClass) {
1818
_ = NewEnum.enumElement
1919
_ = PropertyUserInterface.newMethodPlus()
2020
let _: BarBase.Nested
21+
_ = AwesomeWrapper.newName(is: 0, at: 1, for: 2)
2122
}
2223

2324
func foo1(_ b: BarForwardDeclaredClass) {

test/Migrator/string-representable.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ func foo(_ c: Container) -> String {
1212
c.addingAttributes(["a": "b", "a": "b", "a": "b"])
1313
c.adding(attributes: ["a": 1, "a": 2, "a": 3])
1414
c.adding(optionalAttributes: ["a": 1, "a": 2, "a": 3])
15+
_ = Container(optionalAttributes: [:])
16+
_ = Container(optionalAttrArray: [])
1517
_ = Container(optionalAttributes: nil)
1618
_ = Container(optionalAttrArray: nil)
1719
c.adding(attrArray: ["key1", "key2"])
1820
c.add(single: "")
21+
c.add(singleOptional: "")
1922
c.add(singleOptional: nil)
2023
_ = c.getAttrDictionary()
2124
_ = c.getOptionalAttrDictionary()
@@ -36,5 +39,6 @@ func foo(_ c: Container) -> String {
3639
c.adding(optionalAttributes: c.optionalAttrDict)
3740
_ = GlobalAttribute
3841
c.Value = GlobalAttribute
42+
c.optionalAttrDict = nil
3943
return c.Value
4044
}

test/Migrator/string-representable.swift.expected

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ func foo(_ c: Container) -> String {
1212
c.addingAttributes(convertToCitiesContainerAttributeDictionary(["a": "b", "a": "b", "a": "b"]))
1313
c.adding(attributes: convertToSimpleAttributeDictionary(["a": 1, "a": 2, "a": 3]))
1414
c.adding(optionalAttributes: convertToOptionalSimpleAttributeDictionary(["a": 1, "a": 2, "a": 3]))
15-
_ = Container(optionalAttributes: convertToOptionalSimpleAttributeDictionary(nil))
16-
_ = Container(optionalAttrArray: convertToOptionalSimpleAttributeArray(nil))
15+
_ = Container(optionalAttributes: convertToOptionalSimpleAttributeDictionary([:]))
16+
_ = Container(optionalAttrArray: convertToOptionalSimpleAttributeArray([]))
17+
_ = Container(optionalAttributes: nil)
18+
_ = Container(optionalAttrArray: nil)
1719
c.adding(attrArray: convertToSimpleAttributeArray(["key1", "key2"]))
1820
c.add(single: convertToSimpleAttribute(""))
19-
c.add(singleOptional: convertToOptionalSimpleAttribute(nil))
21+
c.add(singleOptional: convertToOptionalSimpleAttribute(""))
22+
c.add(singleOptional: nil)
2023
_ = convertFromSimpleAttributeDictionary(c.getAttrDictionary())
2124
_ = convertFromOptionalSimpleAttributeDictionary(c.getOptionalAttrDictionary())
2225
_ = convertFromSimpleAttribute(c.getSingleAttr())
@@ -36,6 +39,7 @@ func foo(_ c: Container) -> String {
3639
c.adding(optionalAttributes: convertToOptionalSimpleAttributeDictionary(convertFromOptionalSimpleAttributeDictionary(c.optionalAttrDict)))
3740
_ = convertFromNewAttribute(AttributeWrapper.NewAttribute)
3841
c.Value = convertToNewAttribute(convertFromNewAttribute(AttributeWrapper.NewAttribute))
42+
c.optionalAttrDict = nil
3943
return convertFromNewAttribute(c.Value)
4044
}
4145

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,8 @@ void SDKNode::removeChild(NodePtr C) {
622622
}
623623

624624
void SDKNode::annotate(NodeAnnotation Anno, StringRef Comment) {
625-
assert(!isAnnotatedAs(Anno) && "already annotated");
625+
assert(!Comment.empty());
626+
assert(!isAnnotatedAs(Anno) || AnnotateComments[Anno] == Comment);
626627
annotate(Anno);
627628
AnnotateComments[Anno] = Comment;
628629
}
@@ -2357,8 +2358,8 @@ static void detectFuncDeclChange(NodePtr L, NodePtr R) {
23572358
}
23582359

23592360
static void detectRename(NodePtr L, NodePtr R) {
2360-
assert(L->getKind() == R->getKind());
2361-
if (isa<SDKNodeDecl>(L) && L->getPrintedName() != R->getPrintedName()) {
2361+
if (L->getKind() == R->getKind() && isa<SDKNodeDecl>(L) &&
2362+
L->getPrintedName() != R->getPrintedName()) {
23622363
L->annotate(NodeAnnotation::Rename);
23632364
L->annotate(NodeAnnotation::RenameOldName, L->getPrintedName());
23642365
L->annotate(NodeAnnotation::RenameNewName, R->getPrintedName());

0 commit comments

Comments
 (0)