Skip to content

Commit 7af4973

Browse files
committed
Reapply [cxx-interop][libswift] Use std::string instead of BridgedStringRef
This reverts commit 2900dec
1 parent 8b57db7 commit 7af4973

File tree

7 files changed

+30
-26
lines changed

7 files changed

+30
-26
lines changed

SwiftCompilerSources/Sources/SIL/BasicBlock.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ final public class BasicBlock : ListNode, CustomStringConvertible, HasName {
2525
public var function: Function { SILBasicBlock_getFunction(bridged).function }
2626

2727
public var description: String {
28-
SILBasicBlock_debugDescription(bridged).takeString()
28+
var s = SILBasicBlock_debugDescription(bridged)
29+
return String(cString: s.c_str())
2930
}
3031

3132
public var arguments: ArgumentArray { ArgumentArray(block: self) }

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ final public class Function : CustomStringConvertible, HasName {
2121
}
2222

2323
final public var description: String {
24-
return SILFunction_debugDescription(bridged).takeString()
24+
var s = SILFunction_debugDescription(bridged)
25+
return String(cString: s.c_str())
2526
}
2627

2728
public var entryBlock: BasicBlock {

SwiftCompilerSources/Sources/SIL/GlobalVariable.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ final public class GlobalVariable : CustomStringConvertible, HasName {
1919
}
2020

2121
public var description: String {
22-
return SILGlobalVariable_debugDescription(bridged).takeString()
22+
var s = SILGlobalVariable_debugDescription(bridged)
23+
return String(cString: s.c_str())
2324
}
2425

2526
// TODO: initializer instructions

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public class Instruction : ListNode, CustomStringConvertible, Hashable {
3838
final public var function: Function { block.function }
3939

4040
final public var description: String {
41-
SILNode_debugDescription(bridgedNode).takeString()
41+
var s = SILNode_debugDescription(bridgedNode)
42+
return String(cString: s.c_str())
4243
}
4344

4445
final public var operands: OperandArray {
@@ -135,7 +136,8 @@ public class SingleValueInstruction : Instruction, Value {
135136

136137
public final class MultipleValueInstructionResult : Value {
137138
final public var description: String {
138-
SILNode_debugDescription(bridgedNode).takeString()
139+
var s = SILNode_debugDescription(bridgedNode)
140+
return String(cString: s.c_str())
139141
}
140142

141143
public var instruction: Instruction {

SwiftCompilerSources/Sources/SIL/Value.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public protocol Value : AnyObject, CustomStringConvertible {
2121

2222
extension Value {
2323
public var description: String {
24-
SILNode_debugDescription(bridgedNode).takeString()
24+
var s = SILNode_debugDescription(bridgedNode)
25+
return String(cString: s.c_str())
2526
}
2627

2728
public var uses: UseList {

include/swift/SIL/SILBridging.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
#include "swift/Basic/BridgedSwiftObject.h"
1818
#include <stdbool.h>
1919
#include <stddef.h>
20-
21-
#ifdef __cplusplus
22-
extern "C" {
23-
#endif
20+
#include <string>
2421

2522
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
2623

@@ -179,7 +176,7 @@ void PassContext_eraseInstruction(BridgedPassContext passContext,
179176
BridgedInstruction inst);
180177

181178
BridgedStringRef SILFunction_getName(BridgedFunction function);
182-
BridgedStringRef SILFunction_debugDescription(BridgedFunction function);
179+
std::string SILFunction_debugDescription(BridgedFunction function);
183180
OptionalBridgedBasicBlock SILFunction_firstBlock(BridgedFunction function);
184181
OptionalBridgedBasicBlock SILFunction_lastBlock(BridgedFunction function);
185182
SwiftInt SILFunction_numIndirectResultArguments(BridgedFunction function);
@@ -190,12 +187,12 @@ BridgedType SILFunction_getSILResultType(BridgedFunction function);
190187
SwiftInt SILFunction_isSwift51RuntimeAvailable(BridgedFunction function);
191188

192189
BridgedStringRef SILGlobalVariable_getName(BridgedGlobalVar global);
193-
BridgedStringRef SILGlobalVariable_debugDescription(BridgedGlobalVar global);
190+
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global);
194191

195192
OptionalBridgedBasicBlock SILBasicBlock_next(BridgedBasicBlock block);
196193
OptionalBridgedBasicBlock SILBasicBlock_previous(BridgedBasicBlock block);
197194
BridgedFunction SILBasicBlock_getFunction(BridgedBasicBlock block);
198-
BridgedStringRef SILBasicBlock_debugDescription(BridgedBasicBlock block);
195+
std::string SILBasicBlock_debugDescription(BridgedBasicBlock block);
199196
OptionalBridgedInstruction SILBasicBlock_firstInst(BridgedBasicBlock block);
200197
OptionalBridgedInstruction SILBasicBlock_lastInst(BridgedBasicBlock block);
201198
SwiftInt SILBasicBlock_getNumArguments(BridgedBasicBlock block);
@@ -210,7 +207,7 @@ OptionalBridgedOperand Operand_nextUse(BridgedOperand);
210207
BridgedInstruction Operand_getUser(BridgedOperand);
211208
SwiftInt Operand_isTypeDependent(BridgedOperand);
212209

213-
BridgedStringRef SILNode_debugDescription(BridgedNode node);
210+
std::string SILNode_debugDescription(BridgedNode node);
214211
BridgedFunction SILNode_getFunction(BridgedNode node);
215212
OptionalBridgedOperand SILValue_firstUse(BridgedValue value);
216213
BridgedType SILValue_getType(BridgedValue value);
@@ -317,8 +314,4 @@ BridgedInstruction SILBuilder_createApply(BridgedInstruction insertionPoint,
317314

318315
SWIFT_END_NULLABILITY_ANNOTATIONS
319316

320-
#ifdef __cplusplus
321-
} // extern "C"
322-
#endif
323-
324317
#endif

lib/SIL/Utils/SILBridging.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/SIL/SILBridgingUtils.h"
1717
#include "swift/SIL/SILGlobalVariable.h"
1818
#include "swift/SIL/SILBuilder.h"
19+
#include <string>
1920

2021
using namespace swift;
2122

@@ -149,11 +150,12 @@ BridgedStringRef SILFunction_getName(BridgedFunction function) {
149150
return getBridgedStringRef(castToFunction(function)->getName());
150151
}
151152

152-
BridgedStringRef SILFunction_debugDescription(BridgedFunction function) {
153+
std::string SILFunction_debugDescription(BridgedFunction function) {
153154
std::string str;
154155
llvm::raw_string_ostream os(str);
155156
castToFunction(function)->print(os);
156-
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
157+
str.pop_back(); // Remove trailing newline.
158+
return str;
157159
}
158160

159161
OptionalBridgedBasicBlock SILFunction_firstBlock(BridgedFunction function) {
@@ -239,11 +241,12 @@ BridgedFunction SILBasicBlock_getFunction(BridgedBasicBlock block) {
239241
return {castToBasicBlock(block)->getParent()};
240242
}
241243

242-
BridgedStringRef SILBasicBlock_debugDescription(BridgedBasicBlock block) {
244+
std::string SILBasicBlock_debugDescription(BridgedBasicBlock block) {
243245
std::string str;
244246
llvm::raw_string_ostream os(str);
245247
castToBasicBlock(block)->print(os);
246-
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
248+
str.pop_back(); // Remove trailing newline.
249+
return str;
247250
}
248251

249252
OptionalBridgedInstruction SILBasicBlock_firstInst(BridgedBasicBlock block) {
@@ -308,11 +311,12 @@ SwiftInt SILArgument_isExclusiveIndirectParameter(BridgedArgument argument) {
308311
static_assert(BridgedOperandSize == sizeof(Operand),
309312
"wrong bridged Operand size");
310313

311-
BridgedStringRef SILNode_debugDescription(BridgedNode node) {
314+
std::string SILNode_debugDescription(BridgedNode node) {
312315
std::string str;
313316
llvm::raw_string_ostream os(str);
314317
castToSILNode(node)->print(os);
315-
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
318+
str.pop_back(); // Remove trailing newline.
319+
return str;
316320
}
317321

318322
BridgedFunction SILNode_getFunction(BridgedNode node) {
@@ -459,11 +463,12 @@ BridgedStringRef SILGlobalVariable_getName(BridgedGlobalVar global) {
459463
return getBridgedStringRef(castToGlobal(global)->getName());
460464
}
461465

462-
BridgedStringRef SILGlobalVariable_debugDescription(BridgedGlobalVar global) {
466+
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global) {
463467
std::string str;
464468
llvm::raw_string_ostream os(str);
465469
castToGlobal(global)->print(os);
466-
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
470+
str.pop_back(); // Remove trailing newline.
471+
return str;
467472
}
468473

469474
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)