Skip to content

Commit 9219b0c

Browse files
committed
[SourceKit] Address FIXMEs for the NameMatcher
rdar://118996561
1 parent bf102d2 commit 9219b0c

File tree

4 files changed

+47
-40
lines changed

4 files changed

+47
-40
lines changed

Diff for: include/swift/IDE/IDEBridging.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ struct ResolvedLoc {
6464
/// The range of the call's base name.
6565
swift::CharSourceRange range;
6666

67-
// FIXME: (NameMatcher) We should agree on whether `labelRanges` contains the
68-
// colon or not
6967
/// The range of the labels.
7068
///
7169
/// What the label range contains depends on the `labelRangeType`:
@@ -75,6 +73,9 @@ struct ResolvedLoc {
7573
/// the trivia on their sides
7674
/// - For function arguments that don't have a label, this is an empty range
7775
/// that points to the start of the argument (exculding trivia).
76+
///
77+
/// See documentation on `DeclNameLocation.Argument` in swift-syntax for more
78+
/// background.
7879
std::vector<swift::CharSourceRange> labelRanges;
7980

8081
/// The in index in `labelRanges` that belongs to the first trailing closure

Diff for: include/swift/Refactoring/Refactoring.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ class RenameLocs {
8787
RenameLocs localRenameLocs(SourceFile *sourceFile, const ValueDecl *valueDecl);
8888

8989
#if SWIFT_BUILD_SWIFT_SYNTAX
90+
/// A `RenameLoc` together with the `ResolvedLoc` that it resolved to.
91+
struct ResolvedAndRenameLoc {
92+
RenameLoc renameLoc;
93+
ResolvedLoc resolved;
94+
};
95+
9096
/// Given a list of `RenameLoc`s, get the corresponding `ResolveLoc`s.
9197
///
9298
/// These resolve locations contain more structured information, such as the
@@ -95,10 +101,9 @@ RenameLocs localRenameLocs(SourceFile *sourceFile, const ValueDecl *valueDecl);
95101
/// If a \p newName is passed, it is used to verify that all \p renameLocs can
96102
/// be renamed to this name. If any the names cannot be renamed, an empty vector
97103
/// is returned and the issue is diagnosed via \p diags.
98-
std::vector<ResolvedLoc> resolveRenameLocations(ArrayRef<RenameLoc> renameLocs,
99-
StringRef newName,
100-
SourceFile &sourceFile,
101-
DiagnosticEngine &diags);
104+
std::vector<ResolvedAndRenameLoc>
105+
resolveRenameLocations(ArrayRef<RenameLoc> renameLocs, StringRef newName,
106+
SourceFile &sourceFile, DiagnosticEngine &diags);
102107
#endif
103108

104109
struct RangeConfig {

Diff for: lib/Refactoring/SyntacticRename.cpp

+27-25
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using namespace swift;
2222
using namespace swift::ide;
2323

2424
#if SWIFT_BUILD_SWIFT_SYNTAX
25-
std::vector<ResolvedLoc>
25+
std::vector<ResolvedAndRenameLoc>
2626
swift::ide::resolveRenameLocations(ArrayRef<RenameLoc> RenameLocs,
2727
StringRef NewName, SourceFile &SF,
2828
DiagnosticEngine &Diags) {
@@ -72,6 +72,8 @@ swift::ide::resolveRenameLocations(ArrayRef<RenameLoc> RenameLocs,
7272
UnresolvedLocs.push_back({Location});
7373
}
7474

75+
assert(UnresolvedLocs.size() == RenameLocs.size());
76+
7577
std::vector<BridgedSourceLoc> BridgedUnresolvedLocs;
7678
BridgedUnresolvedLocs.reserve(UnresolvedLocs.size());
7779
for (SourceLoc Loc : UnresolvedLocs) {
@@ -85,26 +87,28 @@ swift::ide::resolveRenameLocations(ArrayRef<RenameLoc> RenameLocs,
8587
const std::vector<ResolvedLoc> &resolvedLocsInSourceOrder =
8688
bridgedResolvedLocs.takeUnbridged();
8789

88-
// Callers expect the resolved locs in the same order as the unresolved locs.
89-
// Sort them.
90-
// FIXME: (NameMatcher) Can we change the callers to not rely on this?
91-
std::vector<ResolvedLoc> resolvedLocsInRequestedOrder;
92-
for (SourceLoc unresolvedLoc : UnresolvedLocs) {
93-
auto found =
94-
llvm::find_if(resolvedLocsInSourceOrder,
95-
[unresolvedLoc](const ResolvedLoc &resolved) {
96-
return resolved.range.getStart() == unresolvedLoc;
97-
});
90+
// Callers need to corrolate the `ResolvedLoc` with the `RenameLoc` that they
91+
// originated from. Match them.
92+
std::vector<ResolvedAndRenameLoc> resolvedAndRenameLocs;
93+
for (auto [unresolvedLoc, renameLoc] :
94+
llvm::zip_equal(UnresolvedLocs, RenameLocs)) {
95+
auto found = llvm::find_if(
96+
resolvedLocsInSourceOrder,
97+
[unresolvedLoc = unresolvedLoc](const ResolvedLoc &resolved) {
98+
return resolved.range.getStart() == unresolvedLoc;
99+
});
100+
ResolvedLoc resolvedLoc;
98101
if (found == resolvedLocsInSourceOrder.end()) {
99-
resolvedLocsInRequestedOrder.push_back(
102+
resolvedLoc =
100103
ResolvedLoc(CharSourceRange(),
101104
/*LabelRanges=*/{}, llvm::None, LabelRangeType::None,
102-
/*IsActive=*/true, ResolvedLocContext::Comment));
105+
/*IsActive=*/true, ResolvedLocContext::Comment);
103106
} else {
104-
resolvedLocsInRequestedOrder.push_back(*found);
107+
resolvedLoc = *found;
105108
}
109+
resolvedAndRenameLocs.push_back({renameLoc, resolvedLoc});
106110
}
107-
return resolvedLocsInRequestedOrder;
111+
return resolvedAndRenameLocs;
108112
}
109113
#endif
110114

@@ -126,21 +130,19 @@ swift::ide::findSyntacticRenameRanges(SourceFile *SF,
126130
swift::PrintingDiagnosticConsumer DiagConsumer(DiagOS);
127131
DiagEngine.addConsumer(DiagConsumer);
128132

129-
auto ResolvedLocs =
133+
auto ResolvedAndRenameLocs =
130134
resolveRenameLocations(RenameLocs, NewName, *SF, DiagEngine);
131-
if (ResolvedLocs.size() != RenameLocs.size() || DiagConsumer.didErrorOccur())
135+
if (ResolvedAndRenameLocs.size() != RenameLocs.size() ||
136+
DiagConsumer.didErrorOccur())
132137
return ResultType::failure(ErrBuffer);
133138

134139
std::vector<SyntacticRenameRangeDetails> Result;
135-
size_t index = 0;
136-
for (const RenameLoc &Rename : RenameLocs) {
137-
ResolvedLoc &Resolved = ResolvedLocs[index++];
138-
139-
SyntacticRenameRangeDetails Details =
140-
getSyntacticRenameRangeDetails(SM, Rename.OldName, Resolved, Rename);
140+
for (const ResolvedAndRenameLoc &Loc : ResolvedAndRenameLocs) {
141+
SyntacticRenameRangeDetails Details = getSyntacticRenameRangeDetails(
142+
SM, Loc.renameLoc.OldName, Loc.resolved, Loc.renameLoc);
141143
if (Details.Type == RegionType::Mismatch) {
142-
DiagEngine.diagnose(Resolved.range.getStart(), diag::mismatched_rename,
143-
NewName);
144+
DiagEngine.diagnose(Loc.resolved.range.getStart(),
145+
diag::mismatched_rename, NewName);
144146
Result.emplace_back(SyntacticRenameRangeDetails{Details.Type, {}});
145147
} else {
146148
Result.push_back(Details);

Diff for: tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -2565,20 +2565,19 @@ void SwiftLangSupport::findRelatedIdentifiersInFile(
25652565
// symbols. This makes related idents more fault-tolerant.
25662566
DiagnosticEngine Diags(SrcMgr);
25672567

2568-
std::vector<ResolvedLoc> ResolvedLocs = resolveRenameLocations(
2569-
Locs.getLocations(), /*NewName=*/StringRef(), *SrcFile, Diags);
2568+
std::vector<ResolvedAndRenameLoc> ResolvedAndRenameLocs =
2569+
resolveRenameLocations(Locs.getLocations(), /*NewName=*/StringRef(),
2570+
*SrcFile, Diags);
25702571

25712572
SmallVector<RelatedIdentInfo, 8> Ranges;
2572-
assert(ResolvedLocs.size() == Locs.getLocations().size());
2573-
for (auto [RenameLoc, ResolvedLoc] :
2574-
llvm::zip_equal(Locs.getLocations(), ResolvedLocs)) {
2575-
if (ResolvedLoc.range.isInvalid()) {
2573+
for (auto Loc : ResolvedAndRenameLocs) {
2574+
if (Loc.resolved.range.isInvalid()) {
25762575
continue;
25772576
}
2578-
unsigned Offset =
2579-
SrcMgr.getLocOffsetInBuffer(ResolvedLoc.range.getStart(), BufferID);
2577+
unsigned Offset = SrcMgr.getLocOffsetInBuffer(
2578+
Loc.resolved.range.getStart(), BufferID);
25802579
Ranges.push_back(
2581-
{Offset, ResolvedLoc.range.getByteLength(), RenameLoc.Usage});
2580+
{Offset, Loc.resolved.range.getByteLength(), Loc.renameLoc.Usage});
25822581
}
25832582

25842583
return RelatedIdentsResult(Ranges, OldName);

0 commit comments

Comments
 (0)