Skip to content

Commit 3b1caac

Browse files
committed
[cxx-interop] Add NewCxxMethodSafetyHeuristics feature and guard swift compiler sources changes on it.
1 parent 8f999ff commit 3b1caac

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

SwiftCompilerSources/Sources/Basic/SourceLoc.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ public struct SourceLoc {
2727
guard bridged.isValid() else {
2828
return nil
2929
}
30+
#if hasFeature(NewCxxMethodSafetyHeuristics)
31+
self.locationInFile = bridged.getOpaquePointerValue().assumingMemoryBound(to: UInt8.self)
32+
#else
3033
self.locationInFile = bridged.__getOpaquePointerValueUnsafe().assumingMemoryBound(to: UInt8.self)
34+
#endif
3135
}
3236

3337
public var bridged: swift.SourceLoc {
@@ -57,9 +61,15 @@ public struct CharSourceRange {
5761
}
5862

5963
public init?(bridged: swift.CharSourceRange) {
64+
#if hasFeature(NewCxxMethodSafetyHeuristics)
65+
guard let start = SourceLoc(bridged: bridged.getStart()) else {
66+
return nil
67+
}
68+
#else
6069
guard let start = SourceLoc(bridged: bridged.__getStartUnsafe()) else {
6170
return nil
6271
}
72+
#endif
6373
self.init(start: start, byteLength: bridged.getByteLength())
6474
}
6575

SwiftCompilerSources/Sources/Basic/Utils.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,34 @@ public struct StringRef : CustomStringConvertible, NoReflectionChildren {
6666
public var description: String { string }
6767

6868
public var count: Int {
69+
#if hasFeature(NewCxxMethodSafetyHeuristics)
70+
Int(_bridged.bytes_end() - _bridged.bytes_begin())
71+
#else
6972
Int(_bridged.__bytes_endUnsafe() - _bridged.__bytes_beginUnsafe())
73+
#endif
7074
}
7175

7276
public subscript(index: Int) -> UInt8 {
73-
let buffer = UnsafeBufferPointer<UInt8>(start: _bridged.__bytes_beginUnsafe(),
77+
#if hasFeature(NewCxxMethodSafetyHeuristics)
78+
let buffer = UnsafeBufferPointer<UInt8>(start: _bridged.bytes_begin(),
7479
count: count)
80+
#else
81+
let buffer = UnsafeBufferPointer<UInt8>(start: _bridged.bytes_begin(),
82+
count: count)
83+
#endif
7584
return buffer[index]
7685
}
7786

7887
public static func ==(lhs: StringRef, rhs: StaticString) -> Bool {
88+
#if hasFeature(NewCxxMethodSafetyHeuristics)
89+
let lhsBuffer = UnsafeBufferPointer<UInt8>(
90+
start: lhs._bridged.bytes_begin(),
91+
count: lhs.count)
92+
#else
7993
let lhsBuffer = UnsafeBufferPointer<UInt8>(
8094
start: lhs._bridged.__bytes_beginUnsafe(),
8195
count: lhs.count)
96+
#endif
8297
return rhs.withUTF8Buffer { (rhsBuffer: UnsafeBufferPointer<UInt8>) in
8398
if lhsBuffer.count != rhsBuffer.count { return false }
8499
return lhsBuffer.elementsEqual(rhsBuffer, by: ==)

include/swift/Basic/Features.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ EXPERIMENTAL_FEATURE(RuntimeDiscoverableAttrs, false)
205205
/// signatures.
206206
EXPERIMENTAL_FEATURE(ImportSymbolicCXXDecls, false)
207207

208+
// Only import C++ methods that return pointers (projections) on owned types as
209+
// unsafe.
210+
EXPERIMENTAL_FEATURE(NewCxxMethodSafetyHeuristics, true)
211+
208212
/// Generate bindings for functions that 'throw' in the C++ section of the generated Clang header.
209213
EXPERIMENTAL_FEATURE(GenerateBindingsForThrowingFunctionsInCXX, false)
210214

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3480,6 +3480,10 @@ static bool usesFeatureDeferredSendableChecking(Decl *decl) {
34803480
return false;
34813481
}
34823482

3483+
static bool usesFeatureNewCxxMethodSafetyHeuristics(Decl *decl) {
3484+
return decl->hasClangNode();
3485+
}
3486+
34833487
/// Suppress the printing of a particular feature.
34843488
static void suppressingFeature(PrintOptions &options, Feature feature,
34853489
llvm::function_ref<void()> action) {

0 commit comments

Comments
 (0)