Skip to content

Commit 2900dec

Browse files
committed
Revert [cxx-interop][libswift] Use std::string instead of BridgedStringRef
This causes problem with the Windows build and also causes a deserialization crash on Linux.
1 parent 1647cdd commit 2900dec

File tree

7 files changed

+26
-31
lines changed

7 files changed

+26
-31
lines changed

include/swift/SIL/SILBridging.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515

1616
#include "BridgedSwiftObject.h"
1717
#include <stddef.h>
18-
#include <string>
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
1922

2023
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
2124

@@ -161,19 +164,19 @@ BridgedSlab PassContext_freeSlab(BridgedPassContext passContext,
161164
BridgedSlab slab);
162165

163166
BridgedStringRef SILFunction_getName(BridgedFunction function);
164-
std::string SILFunction_debugDescription(BridgedFunction function);
167+
BridgedStringRef SILFunction_debugDescription(BridgedFunction function);
165168
OptionalBridgedBasicBlock SILFunction_firstBlock(BridgedFunction function);
166169
OptionalBridgedBasicBlock SILFunction_lastBlock(BridgedFunction function);
167170
SwiftInt SILFunction_numIndirectResultArguments(BridgedFunction function);
168171
SwiftInt SILFunction_getSelfArgumentIndex(BridgedFunction function);
169172

170173
BridgedStringRef SILGlobalVariable_getName(BridgedGlobalVar global);
171-
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global);
174+
BridgedStringRef SILGlobalVariable_debugDescription(BridgedGlobalVar global);
172175

173176
OptionalBridgedBasicBlock SILBasicBlock_next(BridgedBasicBlock block);
174177
OptionalBridgedBasicBlock SILBasicBlock_previous(BridgedBasicBlock block);
175178
BridgedFunction SILBasicBlock_getFunction(BridgedBasicBlock block);
176-
std::string SILBasicBlock_debugDescription(BridgedBasicBlock block);
179+
BridgedStringRef SILBasicBlock_debugDescription(BridgedBasicBlock block);
177180
OptionalBridgedInstruction SILBasicBlock_firstInst(BridgedBasicBlock block);
178181
OptionalBridgedInstruction SILBasicBlock_lastInst(BridgedBasicBlock block);
179182
SwiftInt SILBasicBlock_getNumArguments(BridgedBasicBlock block);
@@ -188,7 +191,7 @@ OptionalBridgedOperand Operand_nextUse(BridgedOperand);
188191
BridgedInstruction Operand_getUser(BridgedOperand);
189192
SwiftInt Operand_isTypeDependent(BridgedOperand);
190193

191-
std::string SILNode_debugDescription(BridgedNode node);
194+
BridgedStringRef SILNode_debugDescription(BridgedNode node);
192195
OptionalBridgedOperand SILValue_firstUse(BridgedValue value);
193196
BridgedType SILValue_getType(BridgedValue value);
194197

@@ -242,4 +245,8 @@ BridgedInstruction SILBuilder_createIntegerLiteral(BridgedInstruction insertionP
242245

243246
SWIFT_END_NULLABILITY_ANNOTATIONS
244247

248+
#ifdef __cplusplus
249+
} // extern "C"
250+
#endif
251+
245252
#endif

lib/SIL/Utils/SILBridging.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#include "swift/SIL/SILGlobalVariable.h"
1616
#include "swift/SIL/SILBuilder.h"
1717

18-
#include <string>
19-
2018
using namespace swift;
2119

2220
namespace {
@@ -165,12 +163,11 @@ BridgedStringRef SILFunction_getName(BridgedFunction function) {
165163
return getBridgedStringRef(castToFunction(function)->getName());
166164
}
167165

168-
std::string SILFunction_debugDescription(BridgedFunction function) {
166+
BridgedStringRef SILFunction_debugDescription(BridgedFunction function) {
169167
std::string str;
170168
llvm::raw_string_ostream os(str);
171169
castToFunction(function)->print(os);
172-
str.pop_back(); // Remove trailing newline.
173-
return str;
170+
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
174171
}
175172

176173
OptionalBridgedBasicBlock SILFunction_firstBlock(BridgedFunction function) {
@@ -226,12 +223,11 @@ BridgedFunction SILBasicBlock_getFunction(BridgedBasicBlock block) {
226223
return {castToBasicBlock(block)->getParent()};
227224
}
228225

229-
std::string SILBasicBlock_debugDescription(BridgedBasicBlock block) {
226+
BridgedStringRef SILBasicBlock_debugDescription(BridgedBasicBlock block) {
230227
std::string str;
231228
llvm::raw_string_ostream os(str);
232229
castToBasicBlock(block)->print(os);
233-
str.pop_back(); // Remove trailing newline.
234-
return str;
230+
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
235231
}
236232

237233
OptionalBridgedInstruction SILBasicBlock_firstInst(BridgedBasicBlock block) {
@@ -291,12 +287,11 @@ BridgedBasicBlock SILArgument_getParent(BridgedArgument argument) {
291287
static_assert(BridgedOperandSize == sizeof(Operand),
292288
"wrong bridged Operand size");
293289

294-
std::string SILNode_debugDescription(BridgedNode node) {
290+
BridgedStringRef SILNode_debugDescription(BridgedNode node) {
295291
std::string str;
296292
llvm::raw_string_ostream os(str);
297293
castToSILNode(node)->print(os);
298-
str.pop_back(); // Remove trailing newline.
299-
return str;
294+
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
300295
}
301296

302297
static Operand *castToOperand(BridgedOperand operand) {
@@ -347,12 +342,11 @@ BridgedStringRef SILGlobalVariable_getName(BridgedGlobalVar global) {
347342
return getBridgedStringRef(castToGlobal(global)->getName());
348343
}
349344

350-
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global) {
345+
BridgedStringRef SILGlobalVariable_debugDescription(BridgedGlobalVar global) {
351346
std::string str;
352347
llvm::raw_string_ostream os(str);
353348
castToGlobal(global)->print(os);
354-
str.pop_back(); // Remove trailing newline.
355-
return str;
349+
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
356350
}
357351

358352
//===----------------------------------------------------------------------===//

libswift/Sources/SIL/BasicBlock.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ final public class BasicBlock : ListNode, CustomStringConvertible {
1919
public var function: Function { SILBasicBlock_getFunction(bridged).function }
2020

2121
public var description: String {
22-
var s = SILBasicBlock_debugDescription(bridged)
23-
return String(cString: s.c_str())
22+
SILBasicBlock_debugDescription(bridged).takeString()
2423
}
2524

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

libswift/Sources/SIL/Function.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ final public class Function : CustomStringConvertible {
1818
}
1919

2020
final public var description: String {
21-
var s = SILFunction_debugDescription(bridged)
22-
return String(cString: s.c_str())
21+
return SILFunction_debugDescription(bridged).takeString()
2322
}
2423

2524
public var entryBlock: BasicBlock {

libswift/Sources/SIL/GlobalVariable.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ final public class GlobalVariable : CustomStringConvertible {
1818
}
1919

2020
public var description: String {
21-
var s = SILGlobalVariable_debugDescription(bridged)
22-
return String(cString: s.c_str())
21+
return SILGlobalVariable_debugDescription(bridged).takeString()
2322
}
2423

2524
// TODO: initializer instructions

libswift/Sources/SIL/Instruction.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public class Instruction : ListNode, CustomStringConvertible, Hashable {
3030
}
3131

3232
final public var description: String {
33-
var s = SILNode_debugDescription(bridgedNode)
34-
return String(cString: s.c_str())
33+
SILNode_debugDescription(bridgedNode).takeString()
3534
}
3635

3736
final public var operands: OperandArray {
@@ -124,8 +123,7 @@ public class SingleValueInstruction : Instruction, Value {
124123

125124
public final class MultipleValueInstructionResult : Value {
126125
final public var description: String {
127-
var s = SILNode_debugDescription(bridgedNode)
128-
return String(cString: s.c_str())
126+
SILNode_debugDescription(bridgedNode).takeString()
129127
}
130128

131129
public var instruction: Instruction {

libswift/Sources/SIL/Value.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ public protocol Value : AnyObject, CustomStringConvertible {
2020

2121
extension Value {
2222
public var description: String {
23-
var s = SILNode_debugDescription(bridgedNode)
24-
return String(cString: s.c_str())
23+
SILNode_debugDescription(bridgedNode).takeString()
2524
}
2625

2726
public var uses: UseList {

0 commit comments

Comments
 (0)