diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index fd4a3b5c30..47ae7a8fd0 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -2510,6 +2510,18 @@ bool Converter::VisitSourceLocExpr(clang::SourceLocExpr *expr) { } } +std::string Converter::GetCharArrayLiteral(const clang::StringLiteral *expr) { + std::string out = "(&["; + for (char c : expr->getString()) { + auto value = CharRustTypeIsSigned() + ? std::to_string(static_cast(c)) + : std::to_string(static_cast(c)); + out += getTypedLiteral(value.c_str(), CharRustType()) + ", "; + } + out += getTypedLiteral("0", CharRustType()) + "])"; + return out; +} + bool Converter::VisitStringLiteral(clang::StringLiteral *expr) { if (IsCodeUnitStringLiteral(expr)) { EmitFresh(GetCodeUnitArrayLiteral(expr)); @@ -2539,15 +2551,7 @@ bool Converter::VisitStringLiteral(clang::StringLiteral *expr) { return false; } if (expr->getString().contains('\0')) { - std::string out = "(&["; - for (char c : expr->getString()) { - auto value = CharRustTypeIsSigned() - ? std::to_string(static_cast(c)) - : std::to_string(static_cast(c)); - out += getTypedLiteral(value.c_str(), CharRustType()) + ", "; - } - out += getTypedLiteral("0", CharRustType()) + "])"; - EmitFresh(out); + EmitFresh(GetCharArrayLiteral(expr)); return false; } EmitFresh(std::format("c{}", GetEscapedStringLiteral(expr, 0))); @@ -5404,6 +5408,14 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg, return std::move(buf).str(); } + if (auto *literal = clang::dyn_cast( + arg->IgnoreParens()->IgnoreImplicit()); + literal && arg->getType()->isArrayType() && + !IsCodeUnitStringLiteral(literal)) { + MarkFresh(); + return GetCharArrayLiteral(literal); + } + if (ph_ctx.needs_materialization()) { auto materialized = ph_ctx.materialize_ctx->GetOrMaterialize( static_cast(ph_ctx.materialize_idx), diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index 6db8f2e372..642b845c14 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -494,6 +494,9 @@ class Converter : public clang::RecursiveASTVisitor { virtual bool VisitCharacterLiteral(clang::CharacterLiteral *expr); std::string GetCodeUnitArrayLiteral(const clang::StringLiteral *expr); + + std::string GetCharArrayLiteral(const clang::StringLiteral *expr); + bool IsArrayInitContext() const; std::string GetEscapedUTF8CharLiteral(clang::Expr *expr) const; diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 9916a2ee43..f29485075c 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -433,6 +433,9 @@ bool ConverterRefCount::ParamIsElementPtr(clang::Expr *expr, clang::Expr *arg, } std::string ConverterRefCount::ConvertElementPtr(clang::Expr *arg) { + if (IsStringLiteralExpr(arg)) { + return ConvertStringLiteralPtr(arg); + } return ConvertObject(arg, ObjectShape::Element); } diff --git a/rules/vector/tgt_unsafe.rs b/rules/vector/tgt_unsafe.rs index 4c990f3d1a..90d1b7b81b 100644 --- a/rules/vector/tgt_unsafe.rs +++ b/rules/vector/tgt_unsafe.rs @@ -44,7 +44,8 @@ unsafe fn f38(a0: usize, a1: bool) -> Vec { } unsafe fn f40(a0: Vec) -> *const T1 { - a0.as_ptr().add(a0.len()) + let __array = &a0; + __array.as_ptr().add(__array.len()) } unsafe fn f47(a0: *mut bool) -> bool { diff --git a/tests/unit/out/refcount/vector_iter_range_ctor.rs b/tests/unit/out/refcount/vector_iter_range_ctor.rs index 99d53ce027..9b36cf4a77 100644 --- a/tests/unit/out/refcount/vector_iter_range_ctor.rs +++ b/tests/unit/out/refcount/vector_iter_range_ctor.rs @@ -84,6 +84,9 @@ fn main_0() -> i32 { }; assert!((v4.len() == 3_usize)); assert!(((v4[0_usize] == 1_u32) && (v4[1_usize] == 2_u32)) && (v4[2_usize] == 3_u32)); + let mut lit_end: Ptr = Ptr::::from_string_literal(b"abc").to_end(); + assert!(((((lit_end.offset(-((1) as isize))).read()) as i32) == (('\0' as i8) as i32))); + assert!(((((lit_end.offset(-((4) as isize))).read()) as i32) == (('a' as i8) as i32))); let buf: Value> = Rc::new(RefCell::new(Box::new([10_u8, 20_u8, 30_u8, 40_u8, 50_u8]))); let mut start: Ptr = buf.decay(); diff --git a/tests/unit/out/unsafe/size_t_ssize_t.rs b/tests/unit/out/unsafe/size_t_ssize_t.rs index 9128b5349b..0ded0241b2 100644 --- a/tests/unit/out/unsafe/size_t_ssize_t.rs +++ b/tests/unit/out/unsafe/size_t_ssize_t.rs @@ -193,8 +193,11 @@ unsafe fn main_0() -> i32 { let mut pr: (u64, i32) = ((sz as u64), 1); assert!(((pr.0) == (21_u64))); let sizes: [usize; 4] = [1_usize, 2_usize, 3_usize, 4_usize]; - let mut sizes_end: *const usize = - (sizes.as_ptr().add(sizes.len()) as *const u64).cast::(); + let mut sizes_end: *const usize = ({ + let __array = &sizes; + __array.as_ptr().add(__array.len()) + } as *const u64) + .cast::(); assert!( ((((sizes_end as usize - sizes.as_ptr() as usize) / ::std::mem::size_of::()) as i64) diff --git a/tests/unit/out/unsafe/vector_iter_range_ctor.rs b/tests/unit/out/unsafe/vector_iter_range_ctor.rs index d2c844b9c5..46bdac9f51 100644 --- a/tests/unit/out/unsafe/vector_iter_range_ctor.rs +++ b/tests/unit/out/unsafe/vector_iter_range_ctor.rs @@ -83,7 +83,13 @@ unsafe fn main_0() -> i32 { assert!(((v3.len()) == (3_usize))); assert!((((v3[(0_usize)]) == (1)) && ((v3[(1_usize)]) == (2))) && ((v3[(2_usize)]) == (3))); let src1: [u32; 3] = [1_u32, 2_u32, 3_u32]; - let mut v4: Vec = match ({ src1.as_ptr().add(src1.len()) },) { + let mut v4: Vec = match ({ + { + let __array = &src1; + __array.as_ptr().add(__array.len()) + } + },) + { (mut __tmp_3,) => { let mut __v = Vec::new(); let mut __it = src1.as_ptr(); @@ -99,6 +105,17 @@ unsafe fn main_0() -> i32 { (((v4[(0_usize)]) == (1_u32)) && ((v4[(1_usize)]) == (2_u32))) && ((v4[(2_usize)]) == (3_u32)) ); + let mut lit_end: *const libc::c_char = { + let __array = &(&[ + (97 as libc::c_char), + (98 as libc::c_char), + (99 as libc::c_char), + (0 as libc::c_char), + ]); + __array.as_ptr().add(__array.len()) + }; + assert!((((*(lit_end.offset(-((1) as isize)))) as i32) == (('\0' as libc::c_char) as i32))); + assert!((((*(lit_end.offset(-((4) as isize)))) as i32) == (('a' as libc::c_char) as i32))); let mut buf: [u8; 5] = [10_u8, 20_u8, 30_u8, 40_u8, 50_u8]; let mut start: *const u8 = (buf.as_mut_ptr()).cast_const(); let mut len: usize = 5_usize; diff --git a/tests/unit/out/unsafe/vector_with_allocator.rs b/tests/unit/out/unsafe/vector_with_allocator.rs index 8b3da6a921..6380a837e1 100644 --- a/tests/unit/out/unsafe/vector_with_allocator.rs +++ b/tests/unit/out/unsafe/vector_with_allocator.rs @@ -303,7 +303,13 @@ unsafe fn main_0() -> i32 { assert!(((v11.len()) == (3_usize))); assert!((((v11[(0_usize)]) == (1)) && ((v11[(1_usize)]) == (2))) && ((v11[(2_usize)]) == (3))); let src1: [u32; 3] = [1_u32, 2_u32, 3_u32]; - let mut v12: Vec = match ({ src1.as_ptr().add(src1.len()) },) { + let mut v12: Vec = match ({ + { + let __array = &src1; + __array.as_ptr().add(__array.len()) + } + },) + { (mut __tmp_5,) => { let mut __v = Vec::new(); let mut __it = src1.as_ptr(); diff --git a/tests/unit/vector_iter_range_ctor.cpp b/tests/unit/vector_iter_range_ctor.cpp index d6d2a05958..554a04e62a 100644 --- a/tests/unit/vector_iter_range_ctor.cpp +++ b/tests/unit/vector_iter_range_ctor.cpp @@ -37,6 +37,10 @@ int main() { assert(v4.size() == 3); assert(v4[0] == 1 && v4[1] == 2 && v4[2] == 3); + const char *lit_end = std::end("abc"); + assert(*(lit_end - 1) == '\0'); + assert(*(lit_end - 4) == 'a'); + uint8_t buf[5] = {10, 20, 30, 40, 50}; const uint8_t *start = buf; size_t len = 5;