Skip to content

Commit 38edc7f

Browse files
committed
[cxx-interop][SwiftCompilerSources] Remove getCopiedBridgedStringRef and freeBridgedStringRef
`std::string`s can now be passed directly between Swift and C++. rdar://83361087
1 parent abc2157 commit 38edc7f

File tree

6 files changed

+7
-32
lines changed

6 files changed

+7
-32
lines changed

SwiftCompilerSources/Sources/Basic/Utils.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ extension BridgedStringRef {
4646
let buffer = UnsafeBufferPointer<UInt8>(start: data, count: Int(length))
4747
return String(decoding: buffer, as: UTF8.self)
4848
}
49-
50-
public func takeString() -> String {
51-
let str = string
52-
freeBridgedStringRef(self)
53-
return str
54-
}
5549
}
5650

5751
extension String {

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ public struct Type : CustomStringConvertible, CustomReflectable {
5050
return idx >= 0 ? idx : nil
5151
}
5252

53-
public var description: String { SILType_debugDescription(bridged).takeString() }
53+
public var description: String {
54+
String(_cxxString: SILType_debugDescription(bridged))
55+
}
5456

5557
public var customMirror: Mirror { Mirror(self, children: []) }
5658
}

include/swift/Basic/BridgingUtils.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,6 @@ getBridgedCharSourceRange(const CharSourceRange &range) {
4646
return {range.getStart(), range.getByteLength()};
4747
}
4848

49-
/// Copies the string in an malloc'ed memory and the caller is responsible for
50-
/// freeing it. 'freeBridgedStringRef()' is available in 'BasicBridging.h'
51-
inline BridgedStringRef
52-
getCopiedBridgedStringRef(std::string str, bool removeTrailingNewline = false) {
53-
// A couple of mallocs are needed for passing a std::string to Swift. But
54-
// it's currently only used or debug descriptions. So, its' maybe not so bad -
55-
// for now.
56-
// TODO: find a better way to pass std::strings to Swift.
57-
llvm::StringRef strRef(str);
58-
if (removeTrailingNewline)
59-
strRef.consume_back("\n");
60-
llvm::MallocAllocator allocator;
61-
llvm::StringRef copy = strRef.copy(allocator);
62-
return getBridgedStringRef(copy);
63-
}
64-
6549
} // namespace swift
6650

6751
#endif

include/swift/SIL/SILBridging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ OptionalBridgedOperand SILValue_firstUse(BridgedValue value);
247247
BridgedType SILValue_getType(BridgedValue value);
248248
BridgedOwnership SILValue_getOwnership(BridgedValue value);
249249

250-
BridgedStringRef SILType_debugDescription(BridgedType);
250+
std::string SILType_debugDescription(BridgedType);
251251
SwiftInt SILType_isAddress(BridgedType);
252252
SwiftInt SILType_isTrivial(BridgedType, BridgedFunction);
253253
SwiftInt SILType_isReferenceCounted(BridgedType type, BridgedFunction);

lib/Basic/BasicBridging.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "swift/Basic/BridgingUtils.h"
14-
#include "swift/Basic/LLVM.h"
1514
#include "llvm/Support/raw_ostream.h"
1615

1716
using namespace swift;
@@ -24,8 +23,3 @@ void OStream_write(BridgedOStream os, BridgedStringRef str) {
2423
static_cast<raw_ostream *>(os.streamAddr)
2524
->write((const char *)(str.data), str.length);
2625
}
27-
28-
/// Frees a string which was allocated by getCopiedBridgedStringRef.
29-
void freeBridgedStringRef(BridgedStringRef str) {
30-
llvm::MallocAllocator().Deallocate(str.data, str.length);
31-
}

lib/SIL/Utils/SILBridging.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,12 @@ BridgedOwnership SILValue_getOwnership(BridgedValue value) {
383383
// SILType
384384
//===----------------------------------------------------------------------===//
385385

386-
BridgedStringRef SILType_debugDescription(BridgedType type) {
386+
std::string SILType_debugDescription(BridgedType type) {
387387
std::string str;
388388
llvm::raw_string_ostream os(str);
389389
castToSILType(type).print(os);
390-
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
390+
str.pop_back(); // Remove trailing newline.
391+
return str;
391392
}
392393

393394
SwiftInt SILType_isAddress(BridgedType type) {

0 commit comments

Comments
 (0)