Skip to content

Commit d224549

Browse files
committed
[SourceKit] Don’t use C++ to Swift interop to run NameMatcher
1 parent 80c399c commit d224549

File tree

8 files changed

+94
-75
lines changed

8 files changed

+94
-75
lines changed

Diff for: cmake/modules/AddPureSwift.cmake

+1-34
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ endfunction()
106106
# EMIT_MODULE
107107
# Emit '.swiftmodule' to
108108
#
109-
# GENERATE_CXX_BRIDGING_HEADER
110-
# Emit a briding header with all public declarations for this library that can
111-
# be imported from C++ to include/ASTGen/<target-name>-Swift.h
112-
#
113109
# DEPENDENCIES
114110
# Target names to pass target_link_library
115111
#
@@ -129,8 +125,7 @@ function(add_pure_swift_host_library name)
129125
set(options
130126
SHARED
131127
STATIC
132-
EMIT_MODULE
133-
GENERATE_CXX_BRIDGING_HEADER)
128+
EMIT_MODULE)
134129
set(single_parameter_options)
135130
set(multiple_parameter_options
136131
DEPENDENCIES
@@ -236,34 +231,6 @@ function(add_pure_swift_host_library name)
236231
>)
237232
endif()
238233

239-
if(APSHL_GENERATE_CXX_BRIDGING_HEADER)
240-
set(bridging_header_dir "${CMAKE_BINARY_DIR}/include/swift/ASTGen")
241-
set(bridging_header_path "${bridging_header_dir}/${name}-Swift.h")
242-
file(MAKE_DIRECTORY ${bridging_header_dir})
243-
target_compile_options(${name} PRIVATE
244-
"SHELL: -Xfrontend -emit-clang-header-path -Xfrontend ${bridging_header_path}"
245-
)
246-
if (CMAKE_Swift_COMPILER_VERSION VERSION_LESS 5.9)
247-
# The 5.8 compiler does not expose all public declarations in the bridging header by default.
248-
target_compile_options(${name} PRIVATE
249-
"SHELL: -Xfrontend -clang-header-expose-decls=all-public"
250-
)
251-
# Explicitly link swiftCxx because it's not automatically liked by a Swift 5.8 compiler
252-
target_link_libraries(${name} PRIVATE -lswiftCxx)
253-
endif()
254-
255-
add_custom_command(
256-
TARGET ${name}
257-
POST_BUILD
258-
COMMAND ""
259-
BYPRODUCTS ${bridging_header_path}
260-
DEPENDS ${name}
261-
COMMENT "Empty command that targets can depend on so that the C++ bridging header gets generate before they are built"
262-
)
263-
264-
add_custom_target("${name}-cxx-briding-header" DEPENDS ${bridging_header_path})
265-
endif()
266-
267234
if(LLVM_USE_LINKER)
268235
target_link_options(${name} PRIVATE
269236
"-use-ld=${LLVM_USE_LINKER}"

Diff for: include/swift/CxxToSwift/IDEUtilsBridging.h

-26
This file was deleted.

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

+55
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,41 @@ typedef std::vector<ResolvedLoc> ResolvedLocVector;
9090
SWIFT_NAME("ResolvedLocVector.empty()")
9191
ResolvedLocVector ResolvedLocVector_createEmpty();
9292

93+
/// A heap-allocated `std::vector<ResoledLoc>` that can be represented by an
94+
/// opaque pointer value.
95+
///
96+
/// This allows us to perform all the memory management for the heap-allocated
97+
/// vector in C++. This makes it easier to manage because creating and
98+
/// destorying an object in C++ is consistent with whether elements within the
99+
/// vector are destroyed as well.
100+
///
101+
/// - Note: This should no longer be necessary when we use C++ to Swift interop.
102+
/// In that case `swift_SwiftIDEUtilsBridging_runNameMatcher` can return a
103+
/// `ResolvedLocVector`.
104+
class BridgedResolvedLocVector {
105+
const std::vector<ResolvedLoc> *vector;
106+
107+
public:
108+
/// Create heap-allocaed vector with the same elements as `vector`.
109+
BridgedResolvedLocVector(const std::vector<ResolvedLoc> &vector);
110+
111+
/// Create a `BridgedResolvedLocVector` from an opaque value obtained from
112+
/// `getOpaqueValue`.
113+
BridgedResolvedLocVector(const void *opaqueValue);
114+
115+
/// Get the underlying vector.
116+
const std::vector<ResolvedLoc> &unbridged();
117+
118+
/// Delete the heap-allocated memory owned by this object. Accessing
119+
/// `unbridged` is illegal after calling `destroy`.
120+
void destroy();
121+
122+
/// Get an opaque pointer value that describes this
123+
/// `BridgedResolvedLocVector`. This opaque value can be returned by a
124+
/// `@_cdecl` function in Swift.
125+
const void *getOpaqueValue() const;
126+
};
127+
93128
typedef std::vector<BridgedSourceLoc> SourceLocVector;
94129

95130
/// Needed so that we can manually conform `SourceLocVectorIterator` to
@@ -103,4 +138,24 @@ inline bool SourceLocVectorIterator_equal(const SourceLocVectorIterator &lhs,
103138
return lhs == rhs;
104139
}
105140

141+
#ifdef __cplusplus
142+
extern "C" {
143+
#endif
144+
145+
/// Entry point to run the NameMatcher written in swift-syntax.
146+
///
147+
/// - Parameters:
148+
/// - sourceFilePtr: A pointer to an `ExportedSourceFile`, used to access the
149+
/// syntax tree
150+
/// - locations: Pointer to a buffer of `BridgedSourceLoc` that should be
151+
/// resolved by the name matcher.
152+
/// - locationsCount: Number of elements in `locations`.
153+
/// - Returns: The opaque value of a `BridgedResolvedLocVector`.
154+
void *swift_SwiftIDEUtilsBridging_runNameMatcher(const void *sourceFilePtr,
155+
BridgedSourceLoc *locations,
156+
size_t locationsCount);
157+
#ifdef __cplusplus
158+
}
159+
#endif
160+
106161
#endif

Diff for: lib/ASTGen/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ add_pure_swift_host_library(swiftASTGen STATIC
6363
${ASTGen_Swift_dependencies}
6464
)
6565

66-
add_pure_swift_host_library(swiftIDEUtilsBridging GENERATE_CXX_BRIDGING_HEADER
66+
add_pure_swift_host_library(swiftIDEUtilsBridging
6767
Sources/SwiftIDEUtilsBridging/NameMatcherBridging.swift
6868

6969
DEPENDENCIES

Diff for: lib/ASTGen/Sources/SwiftIDEUtilsBridging/NameMatcherBridging.swift

+7-4
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,15 @@ fileprivate extension IDEBridging.ResolvedLocContext {
109109

110110
// MARK: - Run NameMatcher
111111

112+
@_cdecl("swift_SwiftIDEUtilsBridging_runNameMatcher")
112113
public func runNameMatcher(
113114
sourceFilePtr: UnsafeRawPointer,
114-
locations: SourceLocVector
115-
) -> ResolvedLocVector {
115+
locations: UnsafePointer<BridgedSourceLoc>,
116+
locationsCount: UInt
117+
) -> UnsafeRawPointer {
116118
let sourceFile = sourceFilePtr.bindMemory(to: ExportedSourceFile.self, capacity: 1).pointee
117-
let positions: [AbsolutePosition] = locations.compactMap { sourceFile.position(of: $0) }
119+
let locations = UnsafeBufferPointer(start: locations, count: Int(locationsCount))
120+
let positions: [AbsolutePosition] = locations.compactMap { sourceFile.position(of: $0) }
118121
let resolvedLocs = NameMatcher.resolve(baseNamePositions: positions, in: sourceFile.syntax)
119-
return ResolvedLocVector(from: resolvedLocs, in: sourceFile)
122+
return BridgedResolvedLocVector(ResolvedLocVector(from: resolvedLocs, in: sourceFile)).getOpaqueValue()
120123
}

Diff for: lib/IDE/IDEBridging.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "swift/IDE/IDEBridging.h"
14+
#include "llvm/Support/raw_ostream.h"
1415
#include <climits>
1516

1617
ResolvedLoc::ResolvedLoc(BridgedCharSourceRange range,
@@ -37,3 +38,20 @@ ResolvedLoc::ResolvedLoc() {}
3738
ResolvedLocVector ResolvedLocVector_createEmpty() {
3839
return ResolvedLocVector();
3940
}
41+
42+
BridgedResolvedLocVector::BridgedResolvedLocVector(
43+
const std::vector<ResolvedLoc> &vector)
44+
: vector(new std::vector<ResolvedLoc>(vector)) {}
45+
46+
BridgedResolvedLocVector::BridgedResolvedLocVector(const void *opaqueValue)
47+
: vector(static_cast<const std::vector<ResolvedLoc> *>(opaqueValue)) {}
48+
49+
const std::vector<ResolvedLoc> &BridgedResolvedLocVector::unbridged() {
50+
return *vector;
51+
}
52+
53+
void BridgedResolvedLocVector::destroy() { delete vector; }
54+
55+
const void *BridgedResolvedLocVector::getOpaqueValue() const {
56+
return static_cast<const void *>(vector);
57+
}

Diff for: lib/Refactoring/CMakeLists.txt

-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,5 @@ target_link_libraries(swiftRefactoring PRIVATE
5555
swiftParse
5656
swiftSema)
5757

58-
add_dependencies(swiftRefactoring "swiftIDEUtilsBridging-cxx-briding-header")
59-
6058
set_swift_llvm_is_available(swiftRefactoring)
6159

Diff for: lib/Refactoring/SyntacticRename.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "swift/AST/ASTContext.h"
1414
#include "swift/AST/DiagnosticsRefactoring.h"
1515
#include "swift/AST/SourceFile.h"
16-
#include "swift/CxxToSwift/IDEUtilsBridging.h"
1716
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
1817
#include "swift/IDE/IDEBridging.h"
1918
#include "swift/Parse/Lexer.h"
@@ -78,19 +77,24 @@ swift::ide::resolveRenameLocations(ArrayRef<RenameLoc> RenameLocs,
7877
BridgedUnresolvedLocs.push_back(BridgedSourceLoc(Loc));
7978
}
8079

81-
std::vector<ResolvedLoc> resolvedLocsInSourceOrder =
82-
swiftIDEUtilsBridging::runNameMatcher(SF.getExportedSourceFile(),
83-
BridgedUnresolvedLocs);
80+
BridgedResolvedLocVector bridgedResolvedLocs =
81+
swift_SwiftIDEUtilsBridging_runNameMatcher(SF.getExportedSourceFile(),
82+
BridgedUnresolvedLocs.data(),
83+
BridgedUnresolvedLocs.size());
84+
SWIFT_DEFER { bridgedResolvedLocs.destroy(); };
85+
const std::vector<ResolvedLoc> &resolvedLocsInSourceOrder =
86+
bridgedResolvedLocs.unbridged();
8487

8588
// Callers expect the resolved locs in the same order as the unresolved locs.
8689
// Sort them.
8790
// FIXME: (NameMatcher) Can we change the callers to not rely on this?
8891
std::vector<ResolvedLoc> resolvedLocsInRequestedOrder;
8992
for (SourceLoc unresolvedLoc : UnresolvedLocs) {
90-
auto found = llvm::find_if(
91-
resolvedLocsInSourceOrder, [unresolvedLoc](ResolvedLoc &resolved) {
92-
return resolved.range.getStart() == unresolvedLoc;
93-
});
93+
auto found =
94+
llvm::find_if(resolvedLocsInSourceOrder,
95+
[unresolvedLoc](const ResolvedLoc &resolved) {
96+
return resolved.range.getStart() == unresolvedLoc;
97+
});
9498
if (found == resolvedLocsInSourceOrder.end()) {
9599
resolvedLocsInRequestedOrder.push_back(
96100
ResolvedLoc(CharSourceRange(),

0 commit comments

Comments
 (0)