Skip to content

Reapply [cxx-interop][libswift] Use std::string instead of BridgedStringRef #42297

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 3 commits into from
May 5, 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
19 changes: 18 additions & 1 deletion SwiftCompilerSources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,27 @@ function(add_swift_compiler_modules_library name)

if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
set(deployment_version "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
set(sdk_option "-sdk" "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}")
set(sdk_path "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}")
set(sdk_option "-sdk" "${sdk_path}")
if(${BOOTSTRAPPING_MODE} STREQUAL "CROSSCOMPILE-WITH-HOSTLIBS")
# Let the cross-compiled compile don't pick up the compiled stdlib by providing
# an (almost) empty resource dir.
# The compiler will instead pick up the stdlib from the SDK.
get_filename_component(swift_exec_bin_dir ${ALS_SWIFT_EXEC} DIRECTORY)
set(sdk_option ${sdk_option} "-resource-dir" "${swift_exec_bin_dir}/../bootstrapping0/lib/swift")
endif()
if(NOT EXISTS "${sdk_path}/usr/include/c++")
# Darwin SDKs in Xcode 12 or older do not include libc++, which prevents clang from finding libc++ when invoked
# from ClangImporter. This results in build errors. To workaround this, let's explicitly pass the path to libc++
# to clang.
message(WARNING "Building with an outdated Darwin SDK: libc++ missing from the ${SWIFT_HOST_VARIANT_SDK} SDK. Will use libc++ from the toolchain.")
get_filename_component(absolute_libcxx_path "${CMAKE_C_COMPILER}/../../include/c++/v1" REALPATH)
if (EXISTS "${absolute_libcxx_path}")
set(sdk_option ${sdk_option} "-Xcc" "-isystem" "-Xcc" "${absolute_libcxx_path}")
else()
message(ERROR "libc++ not found in the toolchain.")
endif()
endif()
elseif(${BOOTSTRAPPING_MODE} STREQUAL "CROSSCOMPILE")
set(sdk_option "-sdk" "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}")
get_filename_component(swift_exec_bin_dir ${ALS_SWIFT_EXEC} DIRECTORY)
Expand Down Expand Up @@ -212,6 +225,10 @@ else()
list(APPEND b0_deps swiftDarwin-bootstrapping0)
list(APPEND b1_deps swiftDarwin-bootstrapping1)
endif()
if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_LIBSTDCXX_PLATFORMS)
list(APPEND b0_deps copy-libstdcxx-modulemap-bootstrapping0 copy-libstdcxx-header-bootstrapping0)
list(APPEND b1_deps copy-libstdcxx-modulemap-bootstrapping1 copy-libstdcxx-header-bootstrapping1)
endif()
endif()
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
set(platform ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR})
Expand Down
3 changes: 2 additions & 1 deletion SwiftCompilerSources/Sources/SIL/BasicBlock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ final public class BasicBlock : ListNode, CustomStringConvertible, HasName {
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 @@ -21,7 +21,8 @@ final public class Function : CustomStringConvertible, HasName {
}

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 @@ -19,7 +19,8 @@ final public class GlobalVariable : CustomStringConvertible, HasName {
}

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 @@ -38,7 +38,8 @@ public class Instruction : ListNode, CustomStringConvertible, Hashable {
final public var function: Function { block.function }

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 @@ -135,7 +136,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 @@ -21,7 +21,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
1 change: 1 addition & 0 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include(SwiftList)
include(SwiftXcodeSupport)
include(SwiftWindowsSupport)
include(SwiftAndroidSupport)
include(SwiftCXXUtils)

function(_swift_gyb_target_sources target scope)
file(GLOB GYB_UNICODE_DATA ${SWIFT_SOURCE_DIR}/utils/UnicodeData/*)
Expand Down
7 changes: 7 additions & 0 deletions cmake/modules/SwiftCXXUtils.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Platforms that use libstdc++ as the system-wide default C++ standard library.
set(SWIFT_LIBSTDCXX_PLATFORMS
"LINUX"
"FREEBSD"
"OPENBSD"
"CYGWIN"
"HAIKU")
17 changes: 5 additions & 12 deletions include/swift/SIL/SILBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
#include "swift/Basic/BridgedSwiftObject.h"
#include <stdbool.h>
#include <stddef.h>

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

SWIFT_BEGIN_NULLABILITY_ANNOTATIONS

Expand Down Expand Up @@ -181,7 +178,7 @@ void PassContext_eraseInstruction(BridgedPassContext passContext,
BridgedInstruction inst);

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);
Expand All @@ -192,12 +189,12 @@ BridgedType SILFunction_getSILResultType(BridgedFunction function);
SwiftInt SILFunction_isSwift51RuntimeAvailable(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 @@ -212,7 +209,7 @@ OptionalBridgedOperand Operand_nextUse(BridgedOperand);
BridgedInstruction Operand_getUser(BridgedOperand);
SwiftInt Operand_isTypeDependent(BridgedOperand);

BridgedStringRef SILNode_debugDescription(BridgedNode node);
std::string SILNode_debugDescription(BridgedNode node);
BridgedFunction SILNode_getFunction(BridgedNode node);
OptionalBridgedOperand SILValue_firstUse(BridgedValue value);
BridgedType SILValue_getType(BridgedValue value);
Expand Down Expand Up @@ -321,8 +318,4 @@ BridgedInstruction SILBuilder_createApply(BridgedInstruction insertionPoint,

SWIFT_END_NULLABILITY_ANNOTATIONS

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

#endif
21 changes: 13 additions & 8 deletions lib/SIL/Utils/SILBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "swift/SIL/SILBridgingUtils.h"
#include "swift/SIL/SILGlobalVariable.h"
#include "swift/SIL/SILBuilder.h"
#include <string>

using namespace swift;

Expand Down Expand Up @@ -149,11 +150,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 @@ -239,11 +241,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 @@ -308,11 +311,12 @@ SwiftInt SILArgument_isExclusiveIndirectParameter(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;
}

BridgedFunction SILNode_getFunction(BridgedNode node) {
Expand Down Expand Up @@ -465,11 +469,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
38 changes: 33 additions & 5 deletions stdlib/public/Cxx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
set(libstdcxx_modulemap_target_list)
foreach(sdk ${SWIFT_SDKS})
if(NOT "${sdk}" STREQUAL "LINUX" AND
NOT "${sdk}" STREQUAL "FREEBSD" AND
NOT "${sdk}" STREQUAL "OPENBSD" AND
NOT "${sdk}" STREQUAL "CYGWIN" AND
NOT "${sdk}" STREQUAL "HAIKU")
if(NOT ${sdk} IN_LIST SWIFT_LIBSTDCXX_PLATFORMS)
continue()
endif()

Expand Down Expand Up @@ -89,6 +85,38 @@ foreach(sdk ${SWIFT_SDKS})
DESTINATION "lib/swift_static/${arch_subdir}"
COMPONENT sdk-overlay)
endif()

if(${BOOTSTRAPPING_MODE} MATCHES "BOOTSTRAPPING.*")
foreach(bootstrapping "0" "1")
get_bootstrapping_path(bootstrapping_dir ${module_dir} ${bootstrapping})
set(libstdcxx_modulemap_out_bootstrapping "${bootstrapping_dir}/libstdcxx.modulemap")
set(libstdcxx_header_out_bootstrapping "${bootstrapping_dir}/libstdcxx.h")

add_custom_command_target(unused_var
COMMAND
"${CMAKE_COMMAND}" "-E" "make_directory" "${bootstrapping_dir}"
COMMAND
"${CMAKE_COMMAND}" "-E" "copy"
"${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_modulemap}" "${libstdcxx_modulemap_out_bootstrapping}"

CUSTOM_TARGET_NAME "copy-libstdcxx-modulemap-bootstrapping${bootstrapping}"
OUTPUT "${libstdcxx_modulemap_out_bootstrapping}"
DEPENDS ${libstdcxx_modulemap}
COMMENT "Copying libstdcxx modulemap to resources for bootstrapping${bootstrapping}")

add_custom_command_target(unused_var
COMMAND
"${CMAKE_COMMAND}" "-E" "make_directory" "${bootstrapping_dir}"
COMMAND
"${CMAKE_COMMAND}" "-E" "copy"
"${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_header}" "${libstdcxx_header_out_bootstrapping}"

CUSTOM_TARGET_NAME "copy-libstdcxx-header-bootstrapping${bootstrapping}"
OUTPUT "${libstdcxx_header_out_bootstrapping}"
DEPENDS ${libstdcxx_header}
COMMENT "Copying libstdcxx header to resources for bootstrapping${bootstrapping}")
endforeach()
endif()
endforeach()
endforeach()
add_custom_target(libstdcxx-modulemap DEPENDS ${libstdcxx_modulemap_target_list})
Expand Down