Skip to content

Commit 3e8867f

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

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
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: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,34 @@ public struct StringRef : CustomStringConvertible, NoReflectionChildren {
6464

6565
public var string: String { _bridged.string }
6666
public var description: String { string }
67-
67+
68+
public var count: Int {
69+
#if hasFeature(NewCxxMethodSafetyHeuristics)
70+
Int(_bridged.bytes_end() - _bridged.bytes_begin())
71+
#else
72+
Int(_bridged.__bytes_endUnsafe() - _bridged.__bytes_beginUnsafe())
73+
#endif
74+
}
75+
76+
public subscript(index: Int) -> UInt8 {
77+
#if hasFeature(NewCxxMethodSafetyHeuristics)
78+
let buffer = UnsafeBufferPointer<UInt8>(start: _bridged.bytes_begin(),
79+
count: count)
80+
#else
81+
let buffer = UnsafeBufferPointer<UInt8>(start: _bridged.bytes_begin(),
82+
count: count)
83+
#endif
84+
return buffer[index]
85+
}
86+
6887
public static func ==(lhs: StringRef, rhs: StaticString) -> Bool {
88+
#if hasFeature(NewCxxMethodSafetyHeuristics)
6989
let lhsBuffer = UnsafeBufferPointer<UInt8>(
70-
start: lhs._bridged.__bytes_beginUnsafe(),
90+
start: lhs._bridged.bytes_begin(),
91+
count: lhs.count)
92+
#else
7193
count: Int(lhs._bridged.__bytes_endUnsafe() - lhs._bridged.__bytes_beginUnsafe()))
94+
#endif
7295
return rhs.withUTF8Buffer { (rhsBuffer: UnsafeBufferPointer<UInt8>) in
7396
if lhsBuffer.count != rhsBuffer.count { return false }
7497
return lhsBuffer.elementsEqual(rhsBuffer, by: ==)

include/swift/Basic/Features.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ EXPERIMENTAL_FEATURE(RuntimeDiscoverableAttrs, false)
197197
/// signatures.
198198
EXPERIMENTAL_FEATURE(ImportSymbolicCXXDecls, false)
199199

200+
// Only import C++ methods that return pointers (projections) on owned types as
201+
// unsafe.
202+
EXPERIMENTAL_FEATURE(NewCxxMethodSafetyHeuristics, true)
203+
200204
/// Generate bindings for functions that 'throw' in the C++ section of the generated Clang header.
201205
EXPERIMENTAL_FEATURE(GenerateBindingsForThrowingFunctionsInCXX, false)
202206

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3448,6 +3448,10 @@ static bool usesFeatureBuiltinModule(Decl *decl) {
34483448
return false;
34493449
}
34503450

3451+
static bool usesFeatureNewCxxMethodSafetyHeuristics(Decl *decl) {
3452+
return decl->hasClangNode();
3453+
}
3454+
34513455
/// Suppress the printing of a particular feature.
34523456
static void suppressingFeature(PrintOptions &options, Feature feature,
34533457
llvm::function_ref<void()> action) {

0 commit comments

Comments
 (0)