Skip to content

Revert "Revert [cxx-interop][libswift] Use std::string instead of BridgedStringRef" #40820

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion SwiftCompilerSources/Sources/SIL/BasicBlock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ final public class BasicBlock : ListNode, CustomStringConvertible {
public var function: Function { SILBasicBlock_getFunction(bridged).function }

public var description: String {
SILBasicBlock_debugDescription(bridged).takeString()
var s = SILBasicBlock_debugDescription(bridged)
return String(cString: s.c_str())
}

public var arguments: ArgumentArray { ArgumentArray(block: self) }
Expand Down
3 changes: 2 additions & 1 deletion SwiftCompilerSources/Sources/SIL/Function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ final public class Function : CustomStringConvertible {
}

final public var description: String {
return SILFunction_debugDescription(bridged).takeString()
var s = SILFunction_debugDescription(bridged)
return String(cString: s.c_str())
}

public var entryBlock: BasicBlock {
Expand Down
3 changes: 2 additions & 1 deletion SwiftCompilerSources/Sources/SIL/GlobalVariable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ final public class GlobalVariable : CustomStringConvertible {
}

public var description: String {
return SILGlobalVariable_debugDescription(bridged).takeString()
var s = SILGlobalVariable_debugDescription(bridged)
return String(cString: s.c_str())
}

// TODO: initializer instructions
Expand Down
6 changes: 4 additions & 2 deletions SwiftCompilerSources/Sources/SIL/Instruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public class Instruction : ListNode, CustomStringConvertible, Hashable {
}

final public var description: String {
SILNode_debugDescription(bridgedNode).takeString()
var s = SILNode_debugDescription(bridgedNode)
return String(cString: s.c_str())
}

final public var operands: OperandArray {
Expand Down Expand Up @@ -123,7 +124,8 @@ public class SingleValueInstruction : Instruction, Value {

public final class MultipleValueInstructionResult : Value {
final public var description: String {
SILNode_debugDescription(bridgedNode).takeString()
var s = SILNode_debugDescription(bridgedNode)
return String(cString: s.c_str())
}

public var instruction: Instruction {
Expand Down
3 changes: 2 additions & 1 deletion SwiftCompilerSources/Sources/SIL/Value.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public protocol Value : AnyObject, CustomStringConvertible {

extension Value {
public var description: String {
SILNode_debugDescription(bridgedNode).takeString()
var s = SILNode_debugDescription(bridgedNode)
return String(cString: s.c_str())
}

public var uses: UseList {
Expand Down
17 changes: 5 additions & 12 deletions include/swift/SIL/SILBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@

#include "BridgedSwiftObject.h"
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif
#include <string>

SWIFT_BEGIN_NULLABILITY_ANNOTATIONS

Expand Down Expand Up @@ -164,19 +161,19 @@ BridgedSlab PassContext_freeSlab(BridgedPassContext passContext,
BridgedSlab slab);

BridgedStringRef SILFunction_getName(BridgedFunction function);
BridgedStringRef SILFunction_debugDescription(BridgedFunction function);
std::string SILFunction_debugDescription(BridgedFunction function);
OptionalBridgedBasicBlock SILFunction_firstBlock(BridgedFunction function);
OptionalBridgedBasicBlock SILFunction_lastBlock(BridgedFunction function);
SwiftInt SILFunction_numIndirectResultArguments(BridgedFunction function);
SwiftInt SILFunction_getSelfArgumentIndex(BridgedFunction function);

BridgedStringRef SILGlobalVariable_getName(BridgedGlobalVar global);
BridgedStringRef SILGlobalVariable_debugDescription(BridgedGlobalVar global);
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global);

OptionalBridgedBasicBlock SILBasicBlock_next(BridgedBasicBlock block);
OptionalBridgedBasicBlock SILBasicBlock_previous(BridgedBasicBlock block);
BridgedFunction SILBasicBlock_getFunction(BridgedBasicBlock block);
BridgedStringRef SILBasicBlock_debugDescription(BridgedBasicBlock block);
std::string SILBasicBlock_debugDescription(BridgedBasicBlock block);
OptionalBridgedInstruction SILBasicBlock_firstInst(BridgedBasicBlock block);
OptionalBridgedInstruction SILBasicBlock_lastInst(BridgedBasicBlock block);
SwiftInt SILBasicBlock_getNumArguments(BridgedBasicBlock block);
Expand All @@ -191,7 +188,7 @@ OptionalBridgedOperand Operand_nextUse(BridgedOperand);
BridgedInstruction Operand_getUser(BridgedOperand);
SwiftInt Operand_isTypeDependent(BridgedOperand);

BridgedStringRef SILNode_debugDescription(BridgedNode node);
std::string SILNode_debugDescription(BridgedNode node);
OptionalBridgedOperand SILValue_firstUse(BridgedValue value);
BridgedType SILValue_getType(BridgedValue value);

Expand Down Expand Up @@ -246,8 +243,4 @@ BridgedInstruction SILBuilder_createIntegerLiteral(BridgedInstruction insertionP

SWIFT_END_NULLABILITY_ANNOTATIONS

#ifdef __cplusplus
} // extern "C"
#endif

#endif
22 changes: 14 additions & 8 deletions lib/SIL/Utils/SILBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "swift/SIL/SILGlobalVariable.h"
#include "swift/SIL/SILBuilder.h"

#include <string>

using namespace swift;

namespace {
Expand Down Expand Up @@ -162,11 +164,12 @@ BridgedStringRef SILFunction_getName(BridgedFunction function) {
return getBridgedStringRef(castToFunction(function)->getName());
}

BridgedStringRef SILFunction_debugDescription(BridgedFunction function) {
std::string SILFunction_debugDescription(BridgedFunction function) {
std::string str;
llvm::raw_string_ostream os(str);
castToFunction(function)->print(os);
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
str.pop_back(); // Remove trailing newline.
return str;
}

OptionalBridgedBasicBlock SILFunction_firstBlock(BridgedFunction function) {
Expand Down Expand Up @@ -222,11 +225,12 @@ BridgedFunction SILBasicBlock_getFunction(BridgedBasicBlock block) {
return {castToBasicBlock(block)->getParent()};
}

BridgedStringRef SILBasicBlock_debugDescription(BridgedBasicBlock block) {
std::string SILBasicBlock_debugDescription(BridgedBasicBlock block) {
std::string str;
llvm::raw_string_ostream os(str);
castToBasicBlock(block)->print(os);
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
str.pop_back(); // Remove trailing newline.
return str;
}

OptionalBridgedInstruction SILBasicBlock_firstInst(BridgedBasicBlock block) {
Expand Down Expand Up @@ -286,11 +290,12 @@ BridgedBasicBlock SILArgument_getParent(BridgedArgument argument) {
static_assert(BridgedOperandSize == sizeof(Operand),
"wrong bridged Operand size");

BridgedStringRef SILNode_debugDescription(BridgedNode node) {
std::string SILNode_debugDescription(BridgedNode node) {
std::string str;
llvm::raw_string_ostream os(str);
castToSILNode(node)->print(os);
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
str.pop_back(); // Remove trailing newline.
return str;
}

static Operand *castToOperand(BridgedOperand operand) {
Expand Down Expand Up @@ -341,11 +346,12 @@ BridgedStringRef SILGlobalVariable_getName(BridgedGlobalVar global) {
return getBridgedStringRef(castToGlobal(global)->getName());
}

BridgedStringRef SILGlobalVariable_debugDescription(BridgedGlobalVar global) {
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global) {
std::string str;
llvm::raw_string_ostream os(str);
castToGlobal(global)->print(os);
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
str.pop_back(); // Remove trailing newline.
return str;
}

//===----------------------------------------------------------------------===//
Expand Down