Skip to content

Re-apply "[migrator] migrator: avoid inserting base name while renaming if users' member access doesn't specify the base name. rdar://40373279" #17330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions lib/Migrator/APIDiffMigratorPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,21 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
SF->getASTContext().SourceMgr, Range).str() == "nil";
}

bool isDotMember(CharSourceRange Range) {
auto S = Range.str();
return S.startswith(".") && S.substr(1).find(".") == StringRef::npos;
}

bool isDotMember(SourceRange Range) {
return isDotMember(Lexer::getCharSourceRangeFromSourceRange(
SF->getASTContext().SourceMgr, Range));
}

bool isDotMember(Expr *E) {
auto Range = E->getSourceRange();
return Range.isValid() && isDotMember(Range);
}

std::vector<APIDiffItem*> getRelatedDiffItems(ValueDecl *VD) {
std::vector<APIDiffItem*> results;
if (!VD)
Expand Down Expand Up @@ -323,11 +338,13 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
}


bool isSimpleReplacement(APIDiffItem *Item, std::string &Text) {
bool isSimpleReplacement(APIDiffItem *Item, bool isDotMember, std::string &Text) {
if (auto *MD = dyn_cast<TypeMemberDiffItem>(Item)) {
if (MD->Subkind == TypeMemberDiffItemSubKind::SimpleReplacement) {
Text = (llvm::Twine(MD->newTypeName) + "." + MD->getNewName().base()).
str();
bool NeedNoTypeName = isDotMember &&
MD->oldPrintedName == MD->newPrintedName;
Text = (llvm::Twine(NeedNoTypeName ? "" : MD->newTypeName) + "." +
MD->getNewName().base()).str();
return true;
}
}
Expand Down Expand Up @@ -423,7 +440,7 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
Type T, ReferenceMetaData Data) override {
for (auto *Item: getRelatedDiffItems(CtorTyRef ? CtorTyRef: D)) {
std::string RepText;
if (isSimpleReplacement(Item, RepText)) {
if (isSimpleReplacement(Item, isDotMember(Range), RepText)) {
Editor.replace(Range, RepText);
return true;
}
Expand Down Expand Up @@ -498,8 +515,11 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
for (auto *I: getRelatedDiffItems(VD)) {
if (auto *Item = dyn_cast<TypeMemberDiffItem>(I)) {
if (Item->Subkind == TypeMemberDiffItemSubKind::QualifiedReplacement) {
Editor.replace(ToReplace, (llvm::Twine(Item->newTypeName) + "." +
Item->getNewName().base()).str());
bool NeedNoTypeName = isDotMember(ToReplace) &&
Item->oldPrintedName == Item->newPrintedName;
Editor.replace(ToReplace,
(llvm::Twine(NeedNoTypeName ? "" : Item->newTypeName) + "." +
Item->getNewName().base()).str());
return true;
}
}
Expand Down Expand Up @@ -773,7 +793,7 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
StringRef LeftComment;
StringRef RightComment;
for (auto *Item: getRelatedDiffItems(RD)) {
if (isSimpleReplacement(Item, Rename)) {
if (isSimpleReplacement(Item, isDotMember(Reference), Rename)) {
} else if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
if (CI->isStringRepresentableChange() &&
CI->NodeKind == SDKNodeKind::DeclVar) {
Expand Down
10 changes: 9 additions & 1 deletion test/Migrator/Inputs/qualified.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,13 @@
"OldTypeName": "",
"NewPrintedName": "internalType",
"NewTypeName": "ToplevelWrapper"
}
},
{
"DiffItemKind": "TypeMemberDiffItem",
"Usr": "c:@E@FooComparisonResult@FooOrderedMemberSame",
"OldPrintedName": "orderedMemberSame",
"OldTypeName": "FooComparisonResult",
"NewPrintedName": "orderedMemberSame",
"NewTypeName": "NewFooComparisonResult.Nested"
},
]
3 changes: 2 additions & 1 deletion test/Migrator/mock-sdk/Bar.framework/Headers/Bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ typedef SomeItemSet SomeEnvironment;
typedef NS_ENUM(long, FooComparisonResult) {
FooOrderedAscending = -1L,
FooOrderedSame,
FooOrderedDescending
FooOrderedDescending,
FooOrderedMemberSame,
};

@interface BarBase
Expand Down
5 changes: 5 additions & 0 deletions test/Migrator/qualified-replacement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ func foo() {
_ = Cities.CityKind.Town
_ = ToplevelType()
_ = ToplevelType(recordName: "")
bar(.orderedSame)
bar(.orderedMemberSame)
bar(FooComparisonResult.orderedSame)
bar(FooComparisonResult.orderedMemberSame)
}

func foo(_: ToplevelType) {}
func bar(_ : FooComparisonResult) {}
5 changes: 5 additions & 0 deletions test/Migrator/qualified-replacement.swift.expected
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ func foo() {
_ = NewCityKind.NewTown
_ = ToplevelWrapper.internalType()
_ = ToplevelWrapper.internalType(recordName: "")
bar(NewFooComparisonResult.NewFooOrderedSame)
bar(.orderedMemberSame)
bar(NewFooComparisonResult.NewFooOrderedSame)
bar(NewFooComparisonResult.Nested.orderedMemberSame)
}

func foo(_: ToplevelWrapper.internalType) {}
func bar(_ : FooComparisonResult) {}