Skip to content

[cxx-interop][SwiftCompilerSources] Remove getCopiedBridgedStringRef and freeBridgedStringRef #59908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions SwiftCompilerSources/Sources/Basic/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ extension BridgedStringRef {
let buffer = UnsafeBufferPointer<UInt8>(start: data, count: Int(length))
return String(decoding: buffer, as: UTF8.self)
}

public func takeString() -> String {
let str = string
freeBridgedStringRef(self)
return str
}
}

extension String {
Expand Down
4 changes: 3 additions & 1 deletion SwiftCompilerSources/Sources/SIL/Type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public struct Type : CustomStringConvertible, CustomReflectable {
return idx >= 0 ? idx : nil
}

public var description: String { SILType_debugDescription(bridged).takeString() }
public var description: String {
String(_cxxString: SILType_debugDescription(bridged))
}

public var customMirror: Mirror { Mirror(self, children: []) }
}
Expand Down
16 changes: 0 additions & 16 deletions include/swift/Basic/BridgingUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,6 @@ getBridgedCharSourceRange(const CharSourceRange &range) {
return {range.getStart(), range.getByteLength()};
}

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

} // namespace swift

#endif
2 changes: 1 addition & 1 deletion include/swift/SIL/SILBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ OptionalBridgedOperand SILValue_firstUse(BridgedValue value);
BridgedType SILValue_getType(BridgedValue value);
BridgedOwnership SILValue_getOwnership(BridgedValue value);

BridgedStringRef SILType_debugDescription(BridgedType);
std::string SILType_debugDescription(BridgedType);
SwiftInt SILType_isAddress(BridgedType);
SwiftInt SILType_isTrivial(BridgedType, BridgedFunction);
SwiftInt SILType_isReferenceCounted(BridgedType type, BridgedFunction);
Expand Down
6 changes: 0 additions & 6 deletions lib/Basic/BasicBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
//===----------------------------------------------------------------------===//

#include "swift/Basic/BridgingUtils.h"
#include "swift/Basic/LLVM.h"
#include "llvm/Support/raw_ostream.h"

using namespace swift;
Expand All @@ -24,8 +23,3 @@ void OStream_write(BridgedOStream os, BridgedStringRef str) {
static_cast<raw_ostream *>(os.streamAddr)
->write((const char *)(str.data), str.length);
}

/// Frees a string which was allocated by getCopiedBridgedStringRef.
void freeBridgedStringRef(BridgedStringRef str) {
llvm::MallocAllocator().Deallocate(str.data, str.length);
}
5 changes: 3 additions & 2 deletions lib/SIL/Utils/SILBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,12 @@ BridgedOwnership SILValue_getOwnership(BridgedValue value) {
// SILType
//===----------------------------------------------------------------------===//

BridgedStringRef SILType_debugDescription(BridgedType type) {
std::string SILType_debugDescription(BridgedType type) {
std::string str;
llvm::raw_string_ostream os(str);
castToSILType(type).print(os);
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
str.pop_back(); // Remove trailing newline.
return str;
}

SwiftInt SILType_isAddress(BridgedType type) {
Expand Down