Skip to content

Commit b6fe8e5

Browse files
authored
Fix string literal passed to rules that expect pointer (#641)
1 parent 0b6cc7f commit b6fe8e5

9 files changed

Lines changed: 66 additions & 14 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,6 +2510,18 @@ bool Converter::VisitSourceLocExpr(clang::SourceLocExpr *expr) {
25102510
}
25112511
}
25122512

2513+
std::string Converter::GetCharArrayLiteral(const clang::StringLiteral *expr) {
2514+
std::string out = "(&[";
2515+
for (char c : expr->getString()) {
2516+
auto value = CharRustTypeIsSigned()
2517+
? std::to_string(static_cast<signed char>(c))
2518+
: std::to_string(static_cast<unsigned char>(c));
2519+
out += getTypedLiteral(value.c_str(), CharRustType()) + ", ";
2520+
}
2521+
out += getTypedLiteral("0", CharRustType()) + "])";
2522+
return out;
2523+
}
2524+
25132525
bool Converter::VisitStringLiteral(clang::StringLiteral *expr) {
25142526
if (IsCodeUnitStringLiteral(expr)) {
25152527
EmitFresh(GetCodeUnitArrayLiteral(expr));
@@ -2539,15 +2551,7 @@ bool Converter::VisitStringLiteral(clang::StringLiteral *expr) {
25392551
return false;
25402552
}
25412553
if (expr->getString().contains('\0')) {
2542-
std::string out = "(&[";
2543-
for (char c : expr->getString()) {
2544-
auto value = CharRustTypeIsSigned()
2545-
? std::to_string(static_cast<signed char>(c))
2546-
: std::to_string(static_cast<unsigned char>(c));
2547-
out += getTypedLiteral(value.c_str(), CharRustType()) + ", ";
2548-
}
2549-
out += getTypedLiteral("0", CharRustType()) + "])";
2550-
EmitFresh(out);
2554+
EmitFresh(GetCharArrayLiteral(expr));
25512555
return false;
25522556
}
25532557
EmitFresh(std::format("c{}", GetEscapedStringLiteral(expr, 0)));
@@ -5409,6 +5413,14 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg,
54095413
return std::move(buf).str();
54105414
}
54115415

5416+
if (auto *literal = clang::dyn_cast<clang::StringLiteral>(
5417+
arg->IgnoreParens()->IgnoreImplicit());
5418+
literal && arg->getType()->isArrayType() &&
5419+
!IsCodeUnitStringLiteral(literal)) {
5420+
MarkFresh();
5421+
return GetCharArrayLiteral(literal);
5422+
}
5423+
54125424
if (ph_ctx.needs_materialization()) {
54135425
auto materialized = ph_ctx.materialize_ctx->GetOrMaterialize(
54145426
static_cast<unsigned>(ph_ctx.materialize_idx),

‎cpp2rust/converter/converter.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,9 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
494494
virtual bool VisitCharacterLiteral(clang::CharacterLiteral *expr);
495495

496496
std::string GetCodeUnitArrayLiteral(const clang::StringLiteral *expr);
497+
498+
std::string GetCharArrayLiteral(const clang::StringLiteral *expr);
499+
497500
bool IsArrayInitContext() const;
498501

499502
std::string GetEscapedUTF8CharLiteral(clang::Expr *expr) const;

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,9 @@ bool ConverterRefCount::ParamIsElementPtr(clang::Expr *expr, clang::Expr *arg,
433433
}
434434

435435
std::string ConverterRefCount::ConvertElementPtr(clang::Expr *arg) {
436+
if (IsStringLiteralExpr(arg)) {
437+
return ConvertStringLiteralPtr(arg);
438+
}
436439
return ConvertObject(arg, ObjectShape::Element);
437440
}
438441

‎rules/vector/tgt_unsafe.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ unsafe fn f38(a0: usize, a1: bool) -> Vec<bool> {
4444
}
4545

4646
unsafe fn f40<T1>(a0: Vec<T1>) -> *const T1 {
47-
a0.as_ptr().add(a0.len())
47+
let __array = &a0;
48+
__array.as_ptr().add(__array.len())
4849
}
4950

5051
unsafe fn f47(a0: *mut bool) -> bool {

‎tests/unit/out/refcount/vector_iter_range_ctor.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ fn main_0() -> i32 {
8484
};
8585
assert!((v4.len() == 3_usize));
8686
assert!(((v4[0_usize] == 1_u32) && (v4[1_usize] == 2_u32)) && (v4[2_usize] == 3_u32));
87+
let mut lit_end: Ptr<i8> = Ptr::<i8>::from_string_literal(b"abc").to_end();
88+
assert!(((((lit_end.offset(-((1) as isize))).read()) as i32) == (('\0' as i8) as i32)));
89+
assert!(((((lit_end.offset(-((4) as isize))).read()) as i32) == (('a' as i8) as i32)));
8790
let buf: Value<Box<[u8]>> =
8891
Rc::new(RefCell::new(Box::new([10_u8, 20_u8, 30_u8, 40_u8, 50_u8])));
8992
let mut start: Ptr<u8> = buf.decay();

‎tests/unit/out/unsafe/size_t_ssize_t.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,11 @@ unsafe fn main_0() -> i32 {
193193
let mut pr: (u64, i32) = ((sz as u64), 1);
194194
assert!(((pr.0) == (21_u64)));
195195
let sizes: [usize; 4] = [1_usize, 2_usize, 3_usize, 4_usize];
196-
let mut sizes_end: *const usize =
197-
(sizes.as_ptr().add(sizes.len()) as *const u64).cast::<usize>();
196+
let mut sizes_end: *const usize = ({
197+
let __array = &sizes;
198+
__array.as_ptr().add(__array.len())
199+
} as *const u64)
200+
.cast::<usize>();
198201
assert!(
199202
((((sizes_end as usize - sizes.as_ptr() as usize) / ::std::mem::size_of::<usize>())
200203
as i64)

‎tests/unit/out/unsafe/vector_iter_range_ctor.rs‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,13 @@ unsafe fn main_0() -> i32 {
8383
assert!(((v3.len()) == (3_usize)));
8484
assert!((((v3[(0_usize)]) == (1)) && ((v3[(1_usize)]) == (2))) && ((v3[(2_usize)]) == (3)));
8585
let src1: [u32; 3] = [1_u32, 2_u32, 3_u32];
86-
let mut v4: Vec<u32> = match ({ src1.as_ptr().add(src1.len()) },) {
86+
let mut v4: Vec<u32> = match ({
87+
{
88+
let __array = &src1;
89+
__array.as_ptr().add(__array.len())
90+
}
91+
},)
92+
{
8793
(mut __tmp_3,) => {
8894
let mut __v = Vec::new();
8995
let mut __it = src1.as_ptr();
@@ -99,6 +105,17 @@ unsafe fn main_0() -> i32 {
99105
(((v4[(0_usize)]) == (1_u32)) && ((v4[(1_usize)]) == (2_u32)))
100106
&& ((v4[(2_usize)]) == (3_u32))
101107
);
108+
let mut lit_end: *const libc::c_char = {
109+
let __array = &(&[
110+
(97 as libc::c_char),
111+
(98 as libc::c_char),
112+
(99 as libc::c_char),
113+
(0 as libc::c_char),
114+
]);
115+
__array.as_ptr().add(__array.len())
116+
};
117+
assert!((((*(lit_end.offset(-((1) as isize)))) as i32) == (('\0' as libc::c_char) as i32)));
118+
assert!((((*(lit_end.offset(-((4) as isize)))) as i32) == (('a' as libc::c_char) as i32)));
102119
let mut buf: [u8; 5] = [10_u8, 20_u8, 30_u8, 40_u8, 50_u8];
103120
let mut start: *const u8 = (buf.as_mut_ptr()).cast_const();
104121
let mut len: usize = 5_usize;

‎tests/unit/out/unsafe/vector_with_allocator.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,13 @@ unsafe fn main_0() -> i32 {
303303
assert!(((v11.len()) == (3_usize)));
304304
assert!((((v11[(0_usize)]) == (1)) && ((v11[(1_usize)]) == (2))) && ((v11[(2_usize)]) == (3)));
305305
let src1: [u32; 3] = [1_u32, 2_u32, 3_u32];
306-
let mut v12: Vec<u32> = match ({ src1.as_ptr().add(src1.len()) },) {
306+
let mut v12: Vec<u32> = match ({
307+
{
308+
let __array = &src1;
309+
__array.as_ptr().add(__array.len())
310+
}
311+
},)
312+
{
307313
(mut __tmp_5,) => {
308314
let mut __v = Vec::new();
309315
let mut __it = src1.as_ptr();

‎tests/unit/vector_iter_range_ctor.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ int main() {
3737
assert(v4.size() == 3);
3838
assert(v4[0] == 1 && v4[1] == 2 && v4[2] == 3);
3939

40+
const char *lit_end = std::end("abc");
41+
assert(*(lit_end - 1) == '\0');
42+
assert(*(lit_end - 4) == 'a');
43+
4044
uint8_t buf[5] = {10, 20, 30, 40, 50};
4145
const uint8_t *start = buf;
4246
size_t len = 5;

0 commit comments

Comments
 (0)