Skip to content

Commit 330ed60

Browse files
committed
[SourceKit] Don't use C++ interop to append elements to a ResolvedLocVector
This was causing build issues on Linux with Swift 5.8. Instead, wrap the `std::vector` in a `BridgedResolvedLocVector` that has a pointer to a heap-allocated `std::vector`
1 parent d224549 commit 330ed60

File tree

3 files changed

+32
-39
lines changed

3 files changed

+32
-39
lines changed

include/swift/IDE/IDEBridging.h

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,6 @@ struct ResolvedLoc {
8080
ResolvedLoc();
8181
};
8282

83-
typedef std::vector<ResolvedLoc> ResolvedLocVector;
84-
85-
/// Create an empty `std::vector<ResolvedLoc>`.
86-
///
87-
/// - Note: This can't be imported as an initializer on
88-
/// `BridgedResolvedLocVector`
89-
/// because initializers without any arguments aren't imported to Swift
90-
SWIFT_NAME("ResolvedLocVector.empty()")
91-
ResolvedLocVector ResolvedLocVector_createEmpty();
92-
9383
/// A heap-allocated `std::vector<ResoledLoc>` that can be represented by an
9484
/// opaque pointer value.
9585
///
@@ -102,29 +92,40 @@ ResolvedLocVector ResolvedLocVector_createEmpty();
10292
/// In that case `swift_SwiftIDEUtilsBridging_runNameMatcher` can return a
10393
/// `ResolvedLocVector`.
10494
class BridgedResolvedLocVector {
105-
const std::vector<ResolvedLoc> *vector;
95+
friend void *BridgedResolvedLocVector_getOpaqueValue(const BridgedResolvedLocVector &vector);
96+
97+
std::vector<ResolvedLoc> *vector;
10698

10799
public:
108100
/// Create heap-allocaed vector with the same elements as `vector`.
109101
BridgedResolvedLocVector(const std::vector<ResolvedLoc> &vector);
110102

111103
/// Create a `BridgedResolvedLocVector` from an opaque value obtained from
112104
/// `getOpaqueValue`.
113-
BridgedResolvedLocVector(const void *opaqueValue);
105+
BridgedResolvedLocVector(void *opaqueValue);
106+
107+
void push_back(const ResolvedLoc &Loc);
114108

115109
/// Get the underlying vector.
116110
const std::vector<ResolvedLoc> &unbridged();
117111

118112
/// Delete the heap-allocated memory owned by this object. Accessing
119113
/// `unbridged` is illegal after calling `destroy`.
120114
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;
126115
};
127116

117+
118+
SWIFT_NAME("BridgedResolvedLocVector.empty()")
119+
BridgedResolvedLocVector BridgedResolvedLocVector_createEmpty();
120+
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+
128129
typedef std::vector<BridgedSourceLoc> SourceLocVector;
129130

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

lib/ASTGen/Sources/SwiftIDEUtilsBridging/NameMatcherBridging.swift

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,6 @@ import SwiftSyntax
1515
import swiftASTGen
1616
import IDEBridging
1717

18-
// MARK: - SourceLocVector Sequence conformance
19-
20-
#if swift(<5.9)
21-
22-
/// Needed because C++ interop does not automatically conform SourceLocVector to CxxSequence with a Swift 5.8 compiler.
23-
extension SourceLocVectorIterator: UnsafeCxxInputIterator {
24-
public static func ==(lhs: SourceLocVectorIterator, rhs: SourceLocVectorIterator) -> Bool { Self.equals(lhs, rhs) }
25-
}
26-
extension SourceLocVector: CxxSequence {
27-
public typealias RawIterator = SourceLocVectorIterator
28-
}
29-
#endif
30-
3118
// MARK: - Bridged type initializers
3219

3320
fileprivate extension BridgedCharSourceRange {
@@ -48,7 +35,7 @@ fileprivate extension CharSourceRangeVector {
4835
}
4936
}
5037

51-
fileprivate extension ResolvedLocVector {
38+
fileprivate extension BridgedResolvedLocVector {
5239
init(from resolvedLocs: some Sequence<DeclNameLocation>, in sourceFile: ExportedSourceFile) {
5340
self = .empty()
5441
for resolvedLoc in resolvedLocs {
@@ -114,10 +101,11 @@ public func runNameMatcher(
114101
sourceFilePtr: UnsafeRawPointer,
115102
locations: UnsafePointer<BridgedSourceLoc>,
116103
locationsCount: UInt
117-
) -> UnsafeRawPointer {
104+
) -> UnsafeMutableRawPointer? {
118105
let sourceFile = sourceFilePtr.bindMemory(to: ExportedSourceFile.self, capacity: 1).pointee
119106
let locations = UnsafeBufferPointer(start: locations, count: Int(locationsCount))
120107
let positions: [AbsolutePosition] = locations.compactMap { sourceFile.position(of: $0) }
121108
let resolvedLocs = NameMatcher.resolve(baseNamePositions: positions, in: sourceFile.syntax)
122-
return BridgedResolvedLocVector(ResolvedLocVector(from: resolvedLocs, in: sourceFile)).getOpaqueValue()
109+
let bridged = BridgedResolvedLocVector(from: resolvedLocs, in: sourceFile)
110+
return BridgedResolvedLocVector_getOpaqueValue(bridged)
123111
}

lib/IDE/IDEBridging.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,27 @@ ResolvedLoc::ResolvedLoc(swift::CharSourceRange range,
3535

3636
ResolvedLoc::ResolvedLoc() {}
3737

38-
ResolvedLocVector ResolvedLocVector_createEmpty() {
39-
return ResolvedLocVector();
38+
BridgedResolvedLocVector BridgedResolvedLocVector_createEmpty() {
39+
return BridgedResolvedLocVector(new std::vector<ResolvedLoc>());
40+
}
41+
42+
void BridgedResolvedLocVector::push_back(const ResolvedLoc &Loc) {
43+
this->vector->push_back(Loc);
4044
}
4145

4246
BridgedResolvedLocVector::BridgedResolvedLocVector(
4347
const std::vector<ResolvedLoc> &vector)
4448
: vector(new std::vector<ResolvedLoc>(vector)) {}
4549

46-
BridgedResolvedLocVector::BridgedResolvedLocVector(const void *opaqueValue)
47-
: vector(static_cast<const std::vector<ResolvedLoc> *>(opaqueValue)) {}
50+
BridgedResolvedLocVector::BridgedResolvedLocVector(void *opaqueValue)
51+
: vector(static_cast<std::vector<ResolvedLoc> *>(opaqueValue)) {}
4852

4953
const std::vector<ResolvedLoc> &BridgedResolvedLocVector::unbridged() {
5054
return *vector;
5155
}
5256

5357
void BridgedResolvedLocVector::destroy() { delete vector; }
5458

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

0 commit comments

Comments
 (0)