Skip to content

Commit 891afa8

Browse files
committed
Handle char array initialization through { string literal } (#175)
`const char var[] = {"string literal"}` is a valid initialization. Add support for it in codegen.
1 parent fe2f472 commit 891afa8

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2795,6 +2795,10 @@ bool Converter::VisitInitListExpr(clang::InitListExpr *expr) {
27952795
StrCat(token::kComma);
27962796
}
27972797
} else {
2798+
if (IsInitExprOfStringLiteral(expr)) {
2799+
Convert(expr->getInit(0)->IgnoreParenImpCasts());
2800+
return false;
2801+
}
27982802
PushBracket bracket(*this);
27992803
for (auto *init : expr->inits()) {
28002804
ConvertVarInit(init->getType(), init);

tests/unit/out/refcount/string_literals.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,12 @@ fn main_0() -> i32 {
6060
let _str: Ptr<u8> = (immutable_empty_arr.as_pointer() as Ptr<u8>);
6161
foo_const_1(_str)
6262
});
63+
let inited_through_init_list: Value<Box<[u8]>> = Rc::new(RefCell::new(Box::<[u8]>::from(
64+
b"papanasi cu smantana\0".as_slice(),
65+
)));
66+
({
67+
let _str: Ptr<u8> = (inited_through_init_list.as_pointer() as Ptr<u8>);
68+
foo_const_1(_str)
69+
});
6370
return 0;
6471
}

tests/unit/out/refcount/string_literals_c.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,12 @@ fn main_0() -> i32 {
9191
let _str: Ptr<u8> = (immutable_empty_arr.as_pointer() as Ptr<u8>);
9292
foo_const_1(_str)
9393
});
94+
let inited_through_init_list: Value<Box<[u8]>> = Rc::new(RefCell::new(Box::<[u8]>::from(
95+
b"papanasi cu smantana\0".as_slice(),
96+
)));
97+
({
98+
let _str: Ptr<u8> = (inited_through_init_list.as_pointer() as Ptr<u8>);
99+
foo_const_1(_str)
100+
});
94101
return 0;
95102
}

0 commit comments

Comments
 (0)