Skip to content

Commit 6332edb

Browse files
authored
Merge pull request #42297 from apple/egorzhdan/libswift-std-string
Reapply [cxx-interop][libswift] Use `std::string` instead of `BridgedStringRef`
2 parents fb0ba6a + d4ac213 commit 6332edb

File tree

11 files changed

+89
-32
lines changed

11 files changed

+89
-32
lines changed

SwiftCompilerSources/CMakeLists.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,27 @@ function(add_swift_compiler_modules_library name)
8989

9090
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
9191
set(deployment_version "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
92-
set(sdk_option "-sdk" "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}")
92+
set(sdk_path "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}")
93+
set(sdk_option "-sdk" "${sdk_path}")
9394
if(${BOOTSTRAPPING_MODE} STREQUAL "CROSSCOMPILE-WITH-HOSTLIBS")
9495
# Let the cross-compiled compile don't pick up the compiled stdlib by providing
9596
# an (almost) empty resource dir.
9697
# The compiler will instead pick up the stdlib from the SDK.
9798
get_filename_component(swift_exec_bin_dir ${ALS_SWIFT_EXEC} DIRECTORY)
9899
set(sdk_option ${sdk_option} "-resource-dir" "${swift_exec_bin_dir}/../bootstrapping0/lib/swift")
99100
endif()
101+
if(NOT EXISTS "${sdk_path}/usr/include/c++")
102+
# Darwin SDKs in Xcode 12 or older do not include libc++, which prevents clang from finding libc++ when invoked
103+
# from ClangImporter. This results in build errors. To workaround this, let's explicitly pass the path to libc++
104+
# to clang.
105+
message(WARNING "Building with an outdated Darwin SDK: libc++ missing from the ${SWIFT_HOST_VARIANT_SDK} SDK. Will use libc++ from the toolchain.")
106+
get_filename_component(absolute_libcxx_path "${CMAKE_C_COMPILER}/../../include/c++/v1" REALPATH)
107+
if (EXISTS "${absolute_libcxx_path}")
108+
set(sdk_option ${sdk_option} "-Xcc" "-isystem" "-Xcc" "${absolute_libcxx_path}")
109+
else()
110+
message(ERROR "libc++ not found in the toolchain.")
111+
endif()
112+
endif()
100113
elseif(${BOOTSTRAPPING_MODE} STREQUAL "CROSSCOMPILE")
101114
set(sdk_option "-sdk" "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}")
102115
get_filename_component(swift_exec_bin_dir ${ALS_SWIFT_EXEC} DIRECTORY)
@@ -212,6 +225,10 @@ else()
212225
list(APPEND b0_deps swiftDarwin-bootstrapping0)
213226
list(APPEND b1_deps swiftDarwin-bootstrapping1)
214227
endif()
228+
if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_LIBSTDCXX_PLATFORMS)
229+
list(APPEND b0_deps copy-libstdcxx-modulemap-bootstrapping0 copy-libstdcxx-header-bootstrapping0)
230+
list(APPEND b1_deps copy-libstdcxx-modulemap-bootstrapping1 copy-libstdcxx-header-bootstrapping1)
231+
endif()
215232
endif()
216233
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
217234
set(platform ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR})

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 {

cmake/modules/AddSwift.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ include(SwiftList)
33
include(SwiftXcodeSupport)
44
include(SwiftWindowsSupport)
55
include(SwiftAndroidSupport)
6+
include(SwiftCXXUtils)
67

78
function(_swift_gyb_target_sources target scope)
89
file(GLOB GYB_UNICODE_DATA ${SWIFT_SOURCE_DIR}/utils/UnicodeData/*)

cmake/modules/SwiftCXXUtils.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Platforms that use libstdc++ as the system-wide default C++ standard library.
2+
set(SWIFT_LIBSTDCXX_PLATFORMS
3+
"LINUX"
4+
"FREEBSD"
5+
"OPENBSD"
6+
"CYGWIN"
7+
"HAIKU")

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

@@ -181,7 +178,7 @@ void PassContext_eraseInstruction(BridgedPassContext passContext,
181178
BridgedInstruction inst);
182179

183180
BridgedStringRef SILFunction_getName(BridgedFunction function);
184-
BridgedStringRef SILFunction_debugDescription(BridgedFunction function);
181+
std::string SILFunction_debugDescription(BridgedFunction function);
185182
OptionalBridgedBasicBlock SILFunction_firstBlock(BridgedFunction function);
186183
OptionalBridgedBasicBlock SILFunction_lastBlock(BridgedFunction function);
187184
SwiftInt SILFunction_numIndirectResultArguments(BridgedFunction function);
@@ -192,12 +189,12 @@ BridgedType SILFunction_getSILResultType(BridgedFunction function);
192189
SwiftInt SILFunction_isSwift51RuntimeAvailable(BridgedFunction function);
193190

194191
BridgedStringRef SILGlobalVariable_getName(BridgedGlobalVar global);
195-
BridgedStringRef SILGlobalVariable_debugDescription(BridgedGlobalVar global);
192+
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global);
196193

197194
OptionalBridgedBasicBlock SILBasicBlock_next(BridgedBasicBlock block);
198195
OptionalBridgedBasicBlock SILBasicBlock_previous(BridgedBasicBlock block);
199196
BridgedFunction SILBasicBlock_getFunction(BridgedBasicBlock block);
200-
BridgedStringRef SILBasicBlock_debugDescription(BridgedBasicBlock block);
197+
std::string SILBasicBlock_debugDescription(BridgedBasicBlock block);
201198
OptionalBridgedInstruction SILBasicBlock_firstInst(BridgedBasicBlock block);
202199
OptionalBridgedInstruction SILBasicBlock_lastInst(BridgedBasicBlock block);
203200
SwiftInt SILBasicBlock_getNumArguments(BridgedBasicBlock block);
@@ -212,7 +209,7 @@ OptionalBridgedOperand Operand_nextUse(BridgedOperand);
212209
BridgedInstruction Operand_getUser(BridgedOperand);
213210
SwiftInt Operand_isTypeDependent(BridgedOperand);
214211

215-
BridgedStringRef SILNode_debugDescription(BridgedNode node);
212+
std::string SILNode_debugDescription(BridgedNode node);
216213
BridgedFunction SILNode_getFunction(BridgedNode node);
217214
OptionalBridgedOperand SILValue_firstUse(BridgedValue value);
218215
BridgedType SILValue_getType(BridgedValue value);
@@ -321,8 +318,4 @@ BridgedInstruction SILBuilder_createApply(BridgedInstruction insertionPoint,
321318

322319
SWIFT_END_NULLABILITY_ANNOTATIONS
323320

324-
#ifdef __cplusplus
325-
} // extern "C"
326-
#endif
327-
328321
#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) {
@@ -465,11 +469,12 @@ BridgedStringRef SILGlobalVariable_getName(BridgedGlobalVar global) {
465469
return getBridgedStringRef(castToGlobal(global)->getName());
466470
}
467471

468-
BridgedStringRef SILGlobalVariable_debugDescription(BridgedGlobalVar global) {
472+
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global) {
469473
std::string str;
470474
llvm::raw_string_ostream os(str);
471475
castToGlobal(global)->print(os);
472-
return getCopiedBridgedStringRef(str, /*removeTrailingNewline*/ true);
476+
str.pop_back(); // Remove trailing newline.
477+
return str;
473478
}
474479

475480
//===----------------------------------------------------------------------===//

stdlib/public/Cxx/CMakeLists.txt

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
set(libstdcxx_modulemap_target_list)
22
foreach(sdk ${SWIFT_SDKS})
3-
if(NOT "${sdk}" STREQUAL "LINUX" AND
4-
NOT "${sdk}" STREQUAL "FREEBSD" AND
5-
NOT "${sdk}" STREQUAL "OPENBSD" AND
6-
NOT "${sdk}" STREQUAL "CYGWIN" AND
7-
NOT "${sdk}" STREQUAL "HAIKU")
3+
if(NOT ${sdk} IN_LIST SWIFT_LIBSTDCXX_PLATFORMS)
84
continue()
95
endif()
106

@@ -89,6 +85,38 @@ foreach(sdk ${SWIFT_SDKS})
8985
DESTINATION "lib/swift_static/${arch_subdir}"
9086
COMPONENT sdk-overlay)
9187
endif()
88+
89+
if(${BOOTSTRAPPING_MODE} MATCHES "BOOTSTRAPPING.*")
90+
foreach(bootstrapping "0" "1")
91+
get_bootstrapping_path(bootstrapping_dir ${module_dir} ${bootstrapping})
92+
set(libstdcxx_modulemap_out_bootstrapping "${bootstrapping_dir}/libstdcxx.modulemap")
93+
set(libstdcxx_header_out_bootstrapping "${bootstrapping_dir}/libstdcxx.h")
94+
95+
add_custom_command_target(unused_var
96+
COMMAND
97+
"${CMAKE_COMMAND}" "-E" "make_directory" "${bootstrapping_dir}"
98+
COMMAND
99+
"${CMAKE_COMMAND}" "-E" "copy"
100+
"${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_modulemap}" "${libstdcxx_modulemap_out_bootstrapping}"
101+
102+
CUSTOM_TARGET_NAME "copy-libstdcxx-modulemap-bootstrapping${bootstrapping}"
103+
OUTPUT "${libstdcxx_modulemap_out_bootstrapping}"
104+
DEPENDS ${libstdcxx_modulemap}
105+
COMMENT "Copying libstdcxx modulemap to resources for bootstrapping${bootstrapping}")
106+
107+
add_custom_command_target(unused_var
108+
COMMAND
109+
"${CMAKE_COMMAND}" "-E" "make_directory" "${bootstrapping_dir}"
110+
COMMAND
111+
"${CMAKE_COMMAND}" "-E" "copy"
112+
"${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_header}" "${libstdcxx_header_out_bootstrapping}"
113+
114+
CUSTOM_TARGET_NAME "copy-libstdcxx-header-bootstrapping${bootstrapping}"
115+
OUTPUT "${libstdcxx_header_out_bootstrapping}"
116+
DEPENDS ${libstdcxx_header}
117+
COMMENT "Copying libstdcxx header to resources for bootstrapping${bootstrapping}")
118+
endforeach()
119+
endif()
92120
endforeach()
93121
endforeach()
94122
add_custom_target(libstdcxx-modulemap DEPENDS ${libstdcxx_modulemap_target_list})

0 commit comments

Comments
 (0)