Skip to content

Commit a5530ce

Browse files
committed
[SourceKit] Factor out some more bridging code to run the NameMatcher from swift-syntax
1 parent 32e366a commit a5530ce

File tree

6 files changed

+48
-18
lines changed

6 files changed

+48
-18
lines changed

include/swift/IDE/IDEBridging.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,10 @@ class BridgedResolvedLocVector {
164164
extern "C" {
165165
#endif
166166

167-
/// Entry point to run the NameMatcher written in swift-syntax.
168-
///
167+
/// Low-level entry point to run the NameMatcher written in swift-syntax.
168+
///
169+
/// - Warning: The only caller of this should be `swift::ide::runNameMatcher`.
170+
///
169171
/// - Parameters:
170172
/// - sourceFilePtr: A pointer to an `ExportedSourceFile`, used to access the
171173
/// syntax tree

include/swift/IDE/Utils.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,19 @@ bool isDynamicRef(Expr *Base, ValueDecl *D, llvm::function_ref<Type(Expr *)> get
715715
void getReceiverType(Expr *Base,
716716
SmallVectorImpl<NominalTypeDecl *> &Types);
717717

718+
#if SWIFT_BUILD_SWIFT_SYNTAX
719+
/// Entry point to run the NameMatcher written in swift-syntax.
720+
///
721+
/// - Parameters:
722+
/// - sourceFile: The source file from which to load the SwiftSyntax tree
723+
/// - locations: The locations to resolve
724+
/// - Returns: A list of `ResolvedLoc` that have been resolved. This list might
725+
/// be shorteder than `locations` if some locations could not be resolved and
726+
/// the resolved locations might be in a different order than `locations`.
727+
std::vector<ResolvedLoc> runNameMatcher(const SourceFile &sourceFile,
728+
ArrayRef<SourceLoc> locations);
729+
#endif
730+
718731
} // namespace ide
719732
} // namespace swift
720733

lib/IDE/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,14 @@ target_link_libraries(swiftIDE PRIVATE
4646
swiftParse
4747
swiftSema)
4848

49+
if (SWIFT_BUILD_SWIFT_SYNTAX)
50+
target_compile_definitions(swiftIDE
51+
PRIVATE
52+
SWIFT_BUILD_SWIFT_SYNTAX
53+
)
54+
target_link_libraries(swiftIDE PRIVATE
55+
swiftIDEUtilsBridging
56+
)
57+
endif()
58+
4959
set_swift_llvm_is_available(swiftIDE)

lib/IDE/Utils.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,3 +1094,21 @@ void swift::ide::getReceiverType(Expr *Base,
10941094
Types.push_back(TyD);
10951095
}
10961096
}
1097+
1098+
#if SWIFT_BUILD_SWIFT_SYNTAX
1099+
std::vector<ResolvedLoc>
1100+
swift::ide::runNameMatcher(const SourceFile &sourceFile,
1101+
ArrayRef<SourceLoc> locations) {
1102+
std::vector<BridgedSourceLoc> bridgedUnresolvedLocs;
1103+
bridgedUnresolvedLocs.reserve(locations.size());
1104+
for (SourceLoc loc : locations) {
1105+
bridgedUnresolvedLocs.push_back(BridgedSourceLoc(loc));
1106+
}
1107+
1108+
BridgedResolvedLocVector bridgedResolvedLocs =
1109+
swift_SwiftIDEUtilsBridging_runNameMatcher(
1110+
sourceFile.getExportedSourceFile(), bridgedUnresolvedLocs.data(),
1111+
bridgedUnresolvedLocs.size());
1112+
return bridgedResolvedLocs.takeUnbridged();
1113+
}
1114+
#endif // SWIFT_BUILD_SWIFT_SYNTAX

lib/Refactoring/LocalRename.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,8 @@ isReferenceWithoutArguments(const llvm::Optional<RenameRefInfo> &refInfo) {
5757
if (refInfo->IsArgLabel) {
5858
return false;
5959
}
60-
BridgedSourceLoc Loc(refInfo->Loc);
61-
BridgedResolvedLocVector bridgedResolvedLocs =
62-
swift_SwiftIDEUtilsBridging_runNameMatcher(
63-
refInfo->SF->getExportedSourceFile(), &Loc, 1);
64-
std::vector<ResolvedLoc> resolvedLocs = bridgedResolvedLocs.takeUnbridged();
60+
std::vector<ResolvedLoc> resolvedLocs =
61+
runNameMatcher(*refInfo->SF, refInfo->Loc);
6562
if (!resolvedLocs.empty()) {
6663
ResolvedLoc resolvedLoc = resolvedLocs.front();
6764
return resolvedLoc.labelRanges.empty();

lib/Refactoring/SyntacticRename.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,8 @@ swift::ide::resolveRenameLocations(ArrayRef<RenameLoc> RenameLocs,
7474

7575
assert(UnresolvedLocs.size() == RenameLocs.size());
7676

77-
std::vector<BridgedSourceLoc> BridgedUnresolvedLocs;
78-
BridgedUnresolvedLocs.reserve(UnresolvedLocs.size());
79-
for (SourceLoc Loc : UnresolvedLocs) {
80-
BridgedUnresolvedLocs.push_back(BridgedSourceLoc(Loc));
81-
}
82-
83-
BridgedResolvedLocVector bridgedResolvedLocs =
84-
swift_SwiftIDEUtilsBridging_runNameMatcher(SF.getExportedSourceFile(),
85-
BridgedUnresolvedLocs.data(),
86-
BridgedUnresolvedLocs.size());
8777
std::vector<ResolvedLoc> resolvedLocsInSourceOrder =
88-
bridgedResolvedLocs.takeUnbridged();
78+
runNameMatcher(SF, UnresolvedLocs);
8979

9080
// Callers need to corrolate the `ResolvedLoc` with the `RenameLoc` that they
9181
// originated from. Match them.

0 commit comments

Comments
 (0)