Skip to content

[5.9][interop][SwiftToCxx] add swift::String overlay constructor for const… #65754

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
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
11 changes: 11 additions & 0 deletions lib/PrintAsClang/_SwiftStdlibCxxOverlay.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ SWIFT_INLINE_THUNK T_0_0 get() const

#ifndef SWIFT_CXX_INTEROP_HIDE_STL_OVERLAY

/// Constructs a Swift string from a C string.
SWIFT_INLINE_THUNK String(const char *cString) noexcept {
if (!cString) {
auto res = _impl::$sS2SycfC();
memcpy(_getOpaquePointer(), &res, sizeof(res));
return;
}
auto res = _impl::$sSS7cStringSSSPys4Int8VG_tcfC(cString);
memcpy(_getOpaquePointer(), &res, sizeof(res));
}

/// Constructs a Swift string from a C++ string.
SWIFT_INLINE_THUNK String(const std::string &str) noexcept {
auto res = _impl::$sSS7cStringSSSPys4Int8VG_tcfC(str.c_str());
Expand Down
9 changes: 9 additions & 0 deletions test/Interop/SwiftToCxx/stdlib/string/string-conversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,17 @@ int main() {
{
auto s = String("hello world");
printString(s);
swift::String s2 = "Hello literal";
printString(s2);
const char *literal = "Test literal via ptr";
printString(literal);
swift::String s3 = nullptr;
printString(s3);
}
// CHECK: '''hello world'''
// CHECK-NEXT: '''Hello literal'''
// CHECK-NEXT: '''Test literal via ptr'''
// CHECK-NEXT: ''''''

{
std::string str = "test std::string";
Expand Down