-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Share implementation of local rename and related identifiers + implement NameMatcher
in Swift
#70008
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
ahoppen
merged 25 commits into
swiftlang:main
from
ahoppen:ahoppen/name-matcher-in-swift
Nov 30, 2023
Merged
Share implementation of local rename and related identifiers + implement NameMatcher
in Swift
#70008
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
698397c
[Refactoring] Expose `findLocalRenameLocs` in Refactoring.h
ahoppen ada3076
[SourceKit] Share implemention of local rename and related idents
ahoppen bc3c8fc
[SourceKit] In related identifiers, directly construct an array of ra…
ahoppen d095d0a
[SourceKit] Remove `newName` from `RenameLoc`
ahoppen d9eef8c
[SourceKit] Remove `IsNonProtocolType` parameter from rename locations
ahoppen b3ab9bf
[SourceKit] Remove `UnresolvedLoc.ResolveArgLocs`
ahoppen ba06b5c
[SourceKit] Replace `UnresolvedLoc` by `SourceLoc`
ahoppen 8208c23
[SourceKit] Remove `ResolvedLoc.Node`
ahoppen 5d7200a
[SourceKit] Remove the local rename refactoring action
ahoppen bfad46d
[SourceKit] Remove the `-syntactic-rename` option from `swift-refactor`
ahoppen a0313e3
[SourceKit] Merge `Renamer` and `RenameRangeDetailCollector`
ahoppen ec267e2
[SourceKit] Hide `RenameRangeDetailCollector` as an implementation de…
ahoppen 8993680
[SourceKit] Return results from rename using return values instead of…
ahoppen 33e9c30
[SourceKit] Unify boolean values in `ResolvedLoc` into a `ResolvedLoc…
ahoppen 1047328
[SourceKit] Lowercase members of `ResolvedLoc`
ahoppen 313055d
[build] Add CMake logic to generate a C++ briding header from `add_pu…
ahoppen 8fd0256
[SourceKit] Use `NameMatcher` that is rewritten in Swift for syntacti…
ahoppen 80c399c
build: disable additional pieces for the build tools phase
compnerd d224549
[SourceKit] Don’t use C++ to Swift interop to run `NameMatcher`
ahoppen 330ed60
[SourceKit] Don't use C++ interop to append elements to a `ResolvedLo…
ahoppen fc08a24
[build] Set `CRT_USE_BUILTIN_OFFSETOF` when building with C++ interop…
ahoppen f408619
Address Hamish’s review comments
ahoppen b313982
Don’t include `SourceLoc.h` when `USED_IN_CPP_SOURCE` is not set
ahoppen 14bc814
Don’t include `vector` in `BasicBridging.h
ahoppen bc5cc43
Don’t include `<vector>` in IDEBridging.h
ahoppen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2022 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef SWIFT_IDE_IDEBRIDGING | ||
#define SWIFT_IDE_IDEBRIDGING | ||
|
||
#include "swift/Basic/BasicBridging.h" | ||
|
||
#ifdef USED_IN_CPP_SOURCE | ||
#include "swift/Basic/SourceLoc.h" | ||
#include "llvm/ADT/Optional.h" | ||
#include "llvm/CAS/CASReference.h" | ||
#include <vector> | ||
#endif | ||
|
||
enum class LabelRangeType { | ||
None, | ||
|
||
/// `foo([a: ]2) or .foo([a: ]String)` | ||
CallArg, | ||
|
||
/// `func([a b]: Int)` | ||
Param, | ||
|
||
/// `subscript([a a]: Int)` | ||
NoncollapsibleParam, | ||
|
||
/// `#selector(foo.func([a]:))` | ||
Selector, | ||
}; | ||
|
||
enum class ResolvedLocContext { Default, Selector, Comment, StringLiteral }; | ||
|
||
#ifdef USED_IN_CPP_SOURCE | ||
struct ResolvedLoc { | ||
/// The range of the call's base name. | ||
swift::CharSourceRange range; | ||
|
||
// FIXME: (NameMatcher) We should agree on whether `labelRanges` contains the | ||
// colon or not | ||
/// The range of the labels. | ||
/// | ||
/// What the label range contains depends on the `labelRangeType`: | ||
/// - Labels of calls span from the label name (excluding trivia) to the end | ||
/// of the colon's trivia. | ||
/// - Declaration labels contain the first name and the second name, excluding | ||
/// the trivia on their sides | ||
/// - For function arguments that don't have a label, this is an empty range | ||
/// that points to the start of the argument (exculding trivia). | ||
std::vector<swift::CharSourceRange> labelRanges; | ||
|
||
/// The in index in `labelRanges` that belongs to the first trailing closure | ||
/// or `llvm::None` if there is no trailing closure. | ||
llvm::Optional<unsigned> firstTrailingLabel; | ||
|
||
LabelRangeType labelType; | ||
|
||
/// Whether the location is in an active `#if` region or not. | ||
bool isActive; | ||
|
||
ResolvedLocContext context; | ||
|
||
ResolvedLoc(swift::CharSourceRange range, | ||
std::vector<swift::CharSourceRange> labelRanges, | ||
llvm::Optional<unsigned> firstTrailingLabel, | ||
LabelRangeType labelType, bool isActive, | ||
ResolvedLocContext context); | ||
|
||
ResolvedLoc(); | ||
}; | ||
|
||
#endif // USED_IN_CPP_SOURCE | ||
|
||
/// An opaque, heap-allocated `ResolvedLoc`. | ||
/// | ||
/// This type is manually memory managed. The creator of the object needs to | ||
/// ensure that `takeUnbridged` is called to free the memory. | ||
struct BridgedResolvedLoc { | ||
/// Opaque pointer to `ResolvedLoc`. | ||
void *resolvedLoc; | ||
|
||
/// This consumes `labelRanges` by calling `takeUnbridged` on it. | ||
SWIFT_NAME( | ||
"init(range:labelRanges:firstTrailingLabel:labelType:isActive:context:)") | ||
BridgedResolvedLoc(BridgedCharSourceRange range, | ||
BridgedCharSourceRangeVector labelRanges, | ||
unsigned firstTrailingLabel, LabelRangeType labelType, | ||
bool isActive, ResolvedLocContext context); | ||
|
||
#ifdef USED_IN_CPP_SOURCE | ||
ResolvedLoc takeUnbridged() { | ||
ResolvedLoc *resolvedLocPtr = static_cast<ResolvedLoc *>(resolvedLoc); | ||
ResolvedLoc unbridged = *resolvedLocPtr; | ||
delete resolvedLocPtr; | ||
return unbridged; | ||
} | ||
#endif | ||
}; | ||
|
||
/// A heap-allocated `std::vector<ResoledLoc>` that can be represented by an | ||
/// opaque pointer value. | ||
/// | ||
/// This type is manually memory managed. The creator of the object needs to | ||
/// ensure that `takeUnbridged` is called to free the memory. | ||
class BridgedResolvedLocVector { | ||
/// Opaque pointer to `std::vector<ResolvedLoc>` | ||
void *vector; | ||
|
||
public: | ||
BridgedResolvedLocVector(); | ||
|
||
/// Create a `BridgedResolvedLocVector` from an opaque value obtained from | ||
/// `getOpaqueValue`. | ||
BridgedResolvedLocVector(void *opaqueValue); | ||
|
||
/// This consumes `Loc`, calling `takeUnbridged` on it. | ||
SWIFT_NAME("append(_:)") | ||
void push_back(BridgedResolvedLoc Loc); | ||
|
||
#ifdef USED_IN_CPP_SOURCE | ||
std::vector<ResolvedLoc> takeUnbridged() { | ||
std::vector<ResolvedLoc> *vectorPtr = | ||
static_cast<std::vector<ResolvedLoc> *>(vector); | ||
std::vector<ResolvedLoc> unbridged = *vectorPtr; | ||
delete vectorPtr; | ||
return unbridged; | ||
} | ||
#endif | ||
|
||
SWIFT_IMPORT_UNSAFE | ||
void *getOpaqueValue() const; | ||
}; | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/// Entry point to run the NameMatcher written in swift-syntax. | ||
/// | ||
/// - Parameters: | ||
/// - sourceFilePtr: A pointer to an `ExportedSourceFile`, used to access the | ||
/// syntax tree | ||
/// - locations: Pointer to a buffer of `BridgedSourceLoc` that should be | ||
/// resolved by the name matcher. | ||
/// - locationsCount: Number of elements in `locations`. | ||
/// - Returns: The opaque value of a `BridgedResolvedLocVector`. | ||
void *swift_SwiftIDEUtilsBridging_runNameMatcher(const void *sourceFilePtr, | ||
BridgedSourceLoc *locations, | ||
size_t locationsCount); | ||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 thanks!