Skip to content

Commit 14bc814

Browse files
committed
Don’t include vector in `BasicBridging.h
1 parent b313982 commit 14bc814

File tree

4 files changed

+36
-40
lines changed

4 files changed

+36
-40
lines changed

include/swift/Basic/BasicBridging.h

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
#include <stddef.h>
2727
#include <stdint.h>
28-
#include <vector>
2928
#ifdef USED_IN_CPP_SOURCE
3029
// Workaround to avoid a compiler error because `cas::ObjectRef` is not defined
3130
// when including VirtualFileSystem.h
@@ -35,6 +34,7 @@
3534
#include "swift/Basic/SourceLoc.h"
3635
#include "llvm/ADT/StringRef.h"
3736
#include <string>
37+
#include <vector>
3838
#endif
3939

4040
// FIXME: We ought to be importing '<swift/bridging>' instead.
@@ -346,26 +346,36 @@ BridgedCharSourceRange_byteLength(BridgedCharSourceRange range) {
346346
// MARK: std::vector<BridgedCharSourceRange>
347347
//===----------------------------------------------------------------------===//
348348

349-
typedef std::vector<BridgedCharSourceRange> BridgedCharSourceRangeVector;
350-
351-
/// Create an empty `std::vector<ResolvedLoc>`.
349+
/// An opaque, heap-allocated `std::vector<CharSourceRange>`.
352350
///
353-
/// - Note: This can't be imported as an initializer on
354-
/// `BridgedResolvedLocVector`
355-
/// because initializers without any arguments aren't imported to Swift
356-
SWIFT_NAME("BridgedCharSourceRangeVector.empty()")
357-
BridgedCharSourceRangeVector BridgedCharSourceRangeVector_createEmpty();
351+
/// This type is manually memory managed. The creator of the object needs to
352+
/// ensure that `takeUnbridged` is called to free the memory.
353+
class BridgedCharSourceRangeVector {
354+
/// Opaque pointer to `std::vector<CharSourceRange>`.
355+
void *_Nonnull vector;
358356

359-
/// Append the given `BridgedCharSourceRange` to `vector`.
360-
SWIFT_NAME("BridgedCharSourceRangeVector.push_back(self:_:)")
361-
void BridgedCharSourceRangeVector_push_back_BridgedCharSourceRange(
362-
BridgedCharSourceRangeVector &vector, BridgedCharSourceRange range);
357+
public:
358+
BridgedCharSourceRangeVector();
359+
360+
SWIFT_NAME("append(_:)")
361+
void push_back(BridgedCharSourceRange range);
363362

364363
#ifdef USED_IN_CPP_SOURCE
365-
/// Convert the `BridgedCharSourceRange` to a `CharSourceRange`.
366-
std::vector<swift::CharSourceRange> BridgedCharSourceRangeVector_unbridged(
367-
const BridgedCharSourceRangeVector &vector);
364+
/// Returns the `std::vector<swift::CharSourceRange>` that this
365+
/// `BridgedCharSourceRangeVector` represents and frees the memory owned by
366+
/// this `BridgedCharSourceRangeVector`.
367+
///
368+
/// No operations should be called on `BridgedCharSourceRangeVector` after
369+
/// `takeUnbridged` is called.
370+
std::vector<swift::CharSourceRange> takeUnbridged() {
371+
auto *vectorPtr =
372+
static_cast<std::vector<swift::CharSourceRange> *>(vector);
373+
std::vector<swift::CharSourceRange> unbridged = *vectorPtr;
374+
delete vectorPtr;
375+
return unbridged;
376+
}
368377
#endif
378+
};
369379

370380
//===----------------------------------------------------------------------===//
371381
// MARK: Plugins

lib/ASTGen/Sources/SwiftIDEUtilsBridging/NameMatcherBridging.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ fileprivate extension BridgedCharSourceRange {
2828

2929
fileprivate extension BridgedCharSourceRangeVector {
3030
init(from ranges: some Sequence<Range<AbsolutePosition>>, in sourceFile: ExportedSourceFile) {
31-
self = .empty()
31+
self = .init()
3232
for range in ranges {
33-
self.push_back(BridgedCharSourceRange(from: range, in: sourceFile))
33+
self.append(BridgedCharSourceRange(from: range, in: sourceFile))
3434
}
3535
}
3636
}
@@ -74,7 +74,7 @@ extension IDEBridging.ResolvedLoc {
7474
}
7575
self.init(
7676
range: BridgedCharSourceRange(from: resolvedLoc.baseNameRange, in: sourceFile),
77-
labelRanges: BridgedCharSourceRangeVector(from: arguments.map { $0.range }, in: sourceFile),
77+
labelRanges: BridgedCharSourceRangeVector(from: arguments.map { $0.range }, in: sourceFile),
7878
firstTrailingLabel: firstTrailingClosureIndex,
7979
labelType: LabelRangeType(resolvedLoc.arguments),
8080
isActive: resolvedLoc.isActive,

lib/Basic/BasicBridging.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,30 +58,17 @@ void BridgedData_free(BridgedData data) {
5858
}
5959

6060
//===----------------------------------------------------------------------===//
61-
// MARK: std::vector<CharSourceRange>
61+
// MARK: BridgedCharSourceRangeVector
6262
//===----------------------------------------------------------------------===//
6363

64-
BridgedCharSourceRangeVector BridgedCharSourceRangeVector_createEmpty() {
65-
return BridgedCharSourceRangeVector();
66-
}
64+
BridgedCharSourceRangeVector::BridgedCharSourceRangeVector()
65+
: vector(new std::vector<CharSourceRange>()) {}
6766

68-
void BridgedCharSourceRangeVector_push_back_BridgedCharSourceRange(
69-
BridgedCharSourceRangeVector &vector, BridgedCharSourceRange range) {
70-
vector.push_back(range);
67+
void BridgedCharSourceRangeVector::push_back(BridgedCharSourceRange range) {
68+
static_cast<std::vector<CharSourceRange> *>(vector)->push_back(
69+
range.unbridged());
7170
}
7271

73-
#ifdef USED_IN_CPP_SOURCE
74-
std::vector<swift::CharSourceRange> BridgedCharSourceRangeVector_unbridged(
75-
const BridgedCharSourceRangeVector &vector) {
76-
std::vector<swift::CharSourceRange> unbridged;
77-
unbridged.reserve(vector.size());
78-
for (auto bridgedCharSourceRange : vector) {
79-
unbridged.push_back(bridgedCharSourceRange.unbridged());
80-
}
81-
return unbridged;
82-
}
83-
#endif
84-
8572
//===----------------------------------------------------------------------===//
8673
// MARK: JSON
8774
//===----------------------------------------------------------------------===//

lib/IDE/IDEBridging.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ ResolvedLoc::ResolvedLoc(BridgedCharSourceRange range,
1818
BridgedCharSourceRangeVector labelRanges,
1919
unsigned firstTrailingLabel, LabelRangeType labelType,
2020
bool isActive, ResolvedLocContext context)
21-
: range(range.unbridged()),
22-
labelRanges(BridgedCharSourceRangeVector_unbridged(labelRanges)),
21+
: range(range.unbridged()), labelRanges(labelRanges.takeUnbridged()),
2322
firstTrailingLabel(firstTrailingLabel == UINT_MAX
2423
? llvm::None
2524
: llvm::Optional<unsigned>(firstTrailingLabel)),

0 commit comments

Comments
 (0)