Skip to content
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
4 changes: 4 additions & 0 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2758,6 +2758,10 @@ bool Converter::VisitInitListExpr(clang::InitListExpr *expr) {
StrCat(token::kComma);
}
} else {
if (IsInitExprOfStringLiteral(expr)) {
Convert(expr->getInit(0)->IgnoreParenImpCasts());
return false;
}
PushBracket bracket(*this);
for (auto *init : expr->inits()) {
ConvertVarInit(init->getType(), init);
Expand Down
8 changes: 8 additions & 0 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ bool IsAsciiStringLiteral(const clang::StringLiteral *str) {
return true;
}

bool IsInitExprOfStringLiteral(const clang::InitListExpr *expr) {
auto type = expr->getType();
return expr->getNumInits() == 1 && type->isArrayType() &&
type->getArrayElementTypeNoTypeQual()->isCharType() &&
clang::isa<clang::StringLiteral>(
expr->getInit(0)->IgnoreParenImpCasts());
}

std::vector<clang::CXXConstructorDecl *>
GetTemplateInstantiatedCtors(clang::CXXRecordDecl *decl) {
std::vector<clang::CXXConstructorDecl *> out;
Expand Down
2 changes: 2 additions & 0 deletions cpp2rust/converter/converter_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ bool IsCallToOstream(clang::CallExpr *expr);

bool IsAsciiStringLiteral(const clang::StringLiteral *str);

bool IsInitExprOfStringLiteral(const clang::InitListExpr *expr);

std::vector<clang::CXXConstructorDecl *>
GetTemplateInstantiatedCtors(clang::CXXRecordDecl *decl);

Expand Down
6 changes: 6 additions & 0 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,12 @@ bool ConverterRefCount::VisitInitListExpr(clang::InitListExpr *expr) {
return false;
}

if (IsInitExprOfStringLiteral(expr)) {
Convert(expr->getInit(0)->IgnoreParenImpCasts());
computed_expr_type_ = ComputedExprType::FreshValue;
return false;
}

auto conv = getConversionKind();
// 2D arrays are FullRefCount'ed on the second level as well.
PushConversionKind push(
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/out/refcount/string_literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,12 @@ fn main_0() -> i32 {
let _str: Ptr<u8> = (immutable_empty_arr.as_pointer() as Ptr<u8>);
foo_const_1(_str)
});
let inited_through_init_list: Value<Box<[u8]>> = Rc::new(RefCell::new(Box::<[u8]>::from(
b"papanasi cu smantana\0".as_slice(),
)));
({
let _str: Ptr<u8> = (inited_through_init_list.as_pointer() as Ptr<u8>);
foo_const_1(_str)
});
return 0;
}
7 changes: 7 additions & 0 deletions tests/unit/out/refcount/string_literals_c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,12 @@ fn main_0() -> i32 {
let _str: Ptr<u8> = (immutable_empty_arr.as_pointer() as Ptr<u8>);
foo_const_1(_str)
});
let inited_through_init_list: Value<Box<[u8]>> = Rc::new(RefCell::new(Box::<[u8]>::from(
b"papanasi cu smantana\0".as_slice(),
)));
({
let _str: Ptr<u8> = (inited_through_init_list.as_pointer() as Ptr<u8>);
foo_const_1(_str)
});
return 0;
}
5 changes: 5 additions & 0 deletions tests/unit/out/unsafe/string_literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,10 @@ unsafe fn main_0() -> i32 {
let _str: *const u8 = immutable_empty_arr.as_ptr();
foo_const_1(_str)
});
let inited_through_init_list: [u8; 21] = *b"papanasi cu smantana\0";
(unsafe {
let _str: *const u8 = inited_through_init_list.as_ptr();
foo_const_1(_str)
});
return 0;
}
5 changes: 5 additions & 0 deletions tests/unit/out/unsafe/string_literals_c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,10 @@ unsafe fn main_0() -> i32 {
let _str: *const u8 = immutable_empty_arr.as_ptr();
foo_const_1(_str)
});
let inited_through_init_list: [u8; 21] = *b"papanasi cu smantana\0";
(unsafe {
let _str: *const u8 = inited_through_init_list.as_ptr();
foo_const_1(_str)
});
return 0;
}
4 changes: 4 additions & 0 deletions tests/unit/string_literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ int main() {
foo_const("");
foo_const(immutable_empty);
foo_const(immutable_empty_arr);

const char inited_through_init_list[] = {"papanasi cu smantana"};
foo_const(inited_through_init_list);

return 0;
}
4 changes: 4 additions & 0 deletions tests/unit/string_literals_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@ int main() {
foo_const(immutable_empty);
foo_const(mutable_empty_arr);
foo_const(immutable_empty_arr);

const char inited_through_init_list[] = {"papanasi cu smantana"};
foo_const(inited_through_init_list);

return 0;
}
Loading