Skip to content

Commit f408619

Browse files
committed
Address Hamish’s review comments
1 parent fc08a24 commit f408619

File tree

8 files changed

+38
-43
lines changed

8 files changed

+38
-43
lines changed

include/swift/IDE/IDEBridging.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,15 @@ class BridgedResolvedLocVector {
112112
/// Delete the heap-allocated memory owned by this object. Accessing
113113
/// `unbridged` is illegal after calling `destroy`.
114114
void destroy();
115+
116+
SWIFT_IMPORT_UNSAFE
117+
void *getOpaqueValue() const;
115118
};
116119

117120

118121
SWIFT_NAME("BridgedResolvedLocVector.empty()")
119122
BridgedResolvedLocVector BridgedResolvedLocVector_createEmpty();
120123

121-
/// Get an opaque pointer value that describes this
122-
/// `BridgedResolvedLocVector`. This opaque value can be returned by a
123-
/// `@_cdecl` function in Swift.
124-
///
125-
/// - Note: Cannot be defined as a member on BridgedResolvecLocVector because
126-
/// Swift 5.8 does not import methods that return pointers.
127-
void *BridgedResolvedLocVector_getOpaqueValue(const BridgedResolvedLocVector &vector);
128-
129124
typedef std::vector<BridgedSourceLoc> SourceLocVector;
130125

131126
/// Needed so that we can manually conform `SourceLocVectorIterator` to

include/swift/Refactoring/Refactoring.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ class RenameLocs {
9292
RenameLocs() {}
9393
};
9494

95-
/// Return the location to rename when renaming the identifier at \p startLoc
95+
/// Return the locations to rename when renaming the \p valueDecl
9696
/// in \p sourceFile.
9797
///
9898
/// - Parameters:
9999
/// - sourceFile: The source file in which to perform local rename
100-
/// - renameInfo: Information about the symbol to rename. See `getRenameInfo`
101-
RenameLocs localRenameLocs(SourceFile *sourceFile, RenameInfo renameInfo);
100+
/// - valueDecl: The declaration that should be renamed
101+
RenameLocs localRenameLocs(SourceFile *sourceFile, const ValueDecl *valueDecl);
102102

103103
/// Given a list of `RenameLoc`s, get the corresponding `ResolveLoc`s.
104104
///

lib/ASTGen/Sources/SwiftIDEUtilsBridging/NameMatcherBridging.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,5 @@ public func runNameMatcher(
107107
let positions: [AbsolutePosition] = locations.compactMap { sourceFile.position(of: $0) }
108108
let resolvedLocs = NameMatcher.resolve(baseNamePositions: positions, in: sourceFile.syntax)
109109
let bridged = BridgedResolvedLocVector(from: resolvedLocs, in: sourceFile)
110-
return BridgedResolvedLocVector_getOpaqueValue(bridged)
110+
return bridged.getOpaqueValue()
111111
}

lib/IDE/IDEBridging.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ const std::vector<ResolvedLoc> &BridgedResolvedLocVector::unbridged() {
5656

5757
void BridgedResolvedLocVector::destroy() { delete vector; }
5858

59-
void *BridgedResolvedLocVector_getOpaqueValue(const BridgedResolvedLocVector &vector) {
60-
return static_cast<void *>(vector.vector);
59+
void *BridgedResolvedLocVector::getOpaqueValue() const {
60+
return static_cast<void *>(this->vector);
6161
}

lib/Refactoring/LocalRename.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class RenameRangeCollector : public IndexDataConsumer {
193193

194194
/// Take the resuls from the collector.
195195
/// This invalidates the collector and must only be called once.
196-
RenameLocs takeResults() {
196+
RenameLocs takeResults() && {
197197
return RenameLocs(locations, std::move(stringStorage));
198198
}
199199

@@ -278,7 +278,7 @@ RenameRangeCollector::indexSymbolToRenameLoc(const index::IndexSymbol &symbol) {
278278
///
279279
/// This \c DeclContext contains all possible references to \c VD within the
280280
/// file.
281-
DeclContext *getRenameScope(ValueDecl *VD) {
281+
DeclContext *getRenameScope(const ValueDecl *VD) {
282282
auto *Scope = VD->getDeclContext();
283283
// There may be sibling decls that the renamed symbol is visible from.
284284
switch (Scope->getContextKind()) {
@@ -303,15 +303,15 @@ DeclContext *getRenameScope(ValueDecl *VD) {
303303
return Scope;
304304
}
305305

306-
/// Get the `RenameInfo` at `startLoc` and validate that we can perform local
306+
/// Get the declaration at `startLoc` and validate that we can perform local
307307
/// rename on it (e.g. checking that the original definition isn't a system
308308
/// symbol).
309309
///
310-
/// If the validation succeeds, return the `RenameInfo`, otherwise add an error
311-
/// to `diags` and return `None`.
312-
static llvm::Optional<RenameInfo>
313-
getRenameInfoForLocalRename(SourceFile *sourceFile, SourceLoc startLoc,
314-
DiagnosticEngine &diags) {
310+
/// If the validation succeeds, return the `ValueDecl`, otherwise add an error
311+
/// to `diags` and return `nullptr`.
312+
static ValueDecl *getDeclForLocalRename(SourceFile *sourceFile,
313+
SourceLoc startLoc,
314+
DiagnosticEngine &diags) {
315315
auto cursorInfo = evaluateOrDefault(
316316
sourceFile->getASTContext().evaluator,
317317
CursorInfoRequest{CursorInfoOwner(sourceFile, startLoc)},
@@ -320,55 +320,56 @@ getRenameInfoForLocalRename(SourceFile *sourceFile, SourceLoc startLoc,
320320
llvm::Optional<RenameInfo> info = getRenameInfo(cursorInfo);
321321
if (!info) {
322322
diags.diagnose(startLoc, diag::unresolved_location);
323-
return llvm::None;
323+
return nullptr;
324324
}
325325

326326
switch (info->Availability.AvailableKind) {
327327
case RefactorAvailableKind::Available:
328328
break;
329329
case RefactorAvailableKind::Unavailable_system_symbol:
330330
diags.diagnose(startLoc, diag::decl_is_system_symbol, info->VD->getName());
331-
return llvm::None;
331+
return nullptr;
332332
case RefactorAvailableKind::Unavailable_has_no_location:
333333
diags.diagnose(startLoc, diag::value_decl_no_loc, info->VD->getName());
334-
return llvm::None;
334+
return nullptr;
335335
case RefactorAvailableKind::Unavailable_has_no_name:
336336
diags.diagnose(startLoc, diag::decl_has_no_name);
337-
return llvm::None;
337+
return nullptr;
338338
case RefactorAvailableKind::Unavailable_has_no_accessibility:
339339
diags.diagnose(startLoc, diag::decl_no_accessibility);
340-
return llvm::None;
340+
return nullptr;
341341
case RefactorAvailableKind::Unavailable_decl_from_clang:
342342
diags.diagnose(startLoc, diag::decl_from_clang);
343-
return llvm::None;
343+
return nullptr;
344344
case RefactorAvailableKind::Unavailable_decl_in_macro:
345345
diags.diagnose(startLoc, diag::decl_in_macro);
346-
return llvm::None;
346+
return nullptr;
347347
}
348348

349-
return info;
349+
return info->VD;
350350
}
351351

352-
RenameLocs swift::ide::localRenameLocs(SourceFile *SF, RenameInfo renameInfo) {
352+
RenameLocs swift::ide::localRenameLocs(SourceFile *SF,
353+
const ValueDecl *valueDecl) {
353354
DeclContext *RenameScope = SF;
354-
if (!RenameScope) {
355+
if (RenameScope) {
355356
// If the value is declared in a DeclContext that's a child of the file in
356357
// which we are performing the rename, we can limit our analysis to this
357358
// decl context.
358359
//
359360
// Cases where the rename scope is not a child of the source file include
360361
// if we are getting related identifiers of a type A that is defined in
361362
// another file. In this case, we need to analyze the entire file.
362-
auto DeclarationScope = getRenameScope(renameInfo.VD);
363+
auto DeclarationScope = getRenameScope(valueDecl);
363364
if (DeclarationScope->isChildContextOf(SF)) {
364365
RenameScope = DeclarationScope;
365366
}
366367
}
367368

368-
RenameRangeCollector rangeCollector(renameInfo.VD);
369+
RenameRangeCollector rangeCollector(valueDecl);
369370
indexDeclContext(RenameScope, rangeCollector);
370371

371-
return rangeCollector.takeResults();
372+
return std::move(rangeCollector).takeResults();
372373
}
373374

374375
CancellableResult<std::vector<SyntacticRenameRangeDetails>>
@@ -385,13 +386,12 @@ swift::ide::findLocalRenameRanges(SourceFile *SF, RangeConfig Range) {
385386
Diags.addConsumer(DiagConsumer);
386387

387388
auto StartLoc = Lexer::getLocForStartOfToken(SM, Range.getStart(SM));
388-
llvm::Optional<RenameInfo> info =
389-
getRenameInfoForLocalRename(SF, StartLoc, Diags);
390-
if (!info || DiagConsumer.didErrorOccur()) {
389+
ValueDecl *declToRename = getDeclForLocalRename(SF, StartLoc, Diags);
390+
if (!declToRename || DiagConsumer.didErrorOccur()) {
391391
return ResultType::failure(ErrBuffer);
392392
}
393393

394-
RenameLocs RenameRanges = localRenameLocs(SF, *info);
394+
RenameLocs RenameRanges = localRenameLocs(SF, declToRename);
395395

396396
return findSyntacticRenameRanges(SF, RenameRanges.getLocations(),
397397
/*NewName=*/StringRef());

lib/Refactoring/SyntacticRenameRangeDetails.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class RenameRangeDetailCollector {
3636
RegionType addSyntacticRenameRanges(const ResolvedLoc &Resolved,
3737
const RenameLoc &Config);
3838

39-
std::vector<RenameRangeDetail> getResult() const { return Ranges; }
39+
std::vector<RenameRangeDetail> &&takeResult() { return std::move(Ranges); }
4040

4141
private:
4242
void addRenameRange(CharSourceRange Label, RefactoringRangeKind RangeKind,
@@ -458,5 +458,5 @@ SyntacticRenameRangeDetails swift::ide::getSyntacticRenameRangeDetails(
458458
const RenameLoc &Config) {
459459
RenameRangeDetailCollector RangeCollector(SM, OldName);
460460
RegionType Type = RangeCollector.addSyntacticRenameRanges(Resolved, Config);
461-
return {Type, RangeCollector.getResult()};
461+
return {Type, RangeCollector.takeResult()};
462462
}

test/SourceKit/RelatedIdents/pound_if.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
func foo(x: Int) {
33
#if true
44
print(x)
5-
#else {
5+
#else
66
print(x)
77
#endif
88
}

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2569,7 +2569,7 @@ void SwiftLangSupport::findRelatedIdentifiersInFile(
25692569
return;
25702570
}
25712571

2572-
RenameLocs Locs = localRenameLocs(SrcFile, *Info);
2572+
RenameLocs Locs = localRenameLocs(SrcFile, Info->VD);
25732573

25742574
// Ignore any errors produced by `resolveRenameLocations` since, if some
25752575
// symbol failed to resolve, we still want to return all the other

0 commit comments

Comments
 (0)