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
30 changes: 21 additions & 9 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<signed char>(c))
: std::to_string(static_cast<unsigned char>(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));
Expand Down Expand Up @@ -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<signed char>(c))
: std::to_string(static_cast<unsigned char>(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)));
Expand Down Expand Up @@ -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<clang::StringLiteral>(
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<unsigned>(ph_ctx.materialize_idx),
Expand Down
3 changes: 3 additions & 0 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,9 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
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;
Expand Down
3 changes: 3 additions & 0 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion rules/vector/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ unsafe fn f38(a0: usize, a1: bool) -> Vec<bool> {
}

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

unsafe fn f47(a0: *mut bool) -> bool {
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/out/refcount/vector_iter_range_ctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i8> = Ptr::<i8>::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<Box<[u8]>> =
Rc::new(RefCell::new(Box::new([10_u8, 20_u8, 30_u8, 40_u8, 50_u8])));
let mut start: Ptr<u8> = buf.decay();
Expand Down
7 changes: 5 additions & 2 deletions tests/unit/out/unsafe/size_t_ssize_t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<usize>();
let mut sizes_end: *const usize = ({
let __array = &sizes;
__array.as_ptr().add(__array.len())
} as *const u64)
.cast::<usize>();
assert!(
((((sizes_end as usize - sizes.as_ptr() as usize) / ::std::mem::size_of::<usize>())
as i64)
Expand Down
19 changes: 18 additions & 1 deletion tests/unit/out/unsafe/vector_iter_range_ctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> = match ({ src1.as_ptr().add(src1.len()) },) {
let mut v4: Vec<u32> = match ({
{
let __array = &src1;
__array.as_ptr().add(__array.len())
}
},)
{
(mut __tmp_3,) => {
let mut __v = Vec::new();
let mut __it = src1.as_ptr();
Expand All @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/out/unsafe/vector_with_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> = match ({ src1.as_ptr().add(src1.len()) },) {
let mut v12: Vec<u32> = match ({
{
let __array = &src1;
__array.as_ptr().add(__array.len())
}
},)
{
(mut __tmp_5,) => {
let mut __v = Vec::new();
let mut __it = src1.as_ptr();
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/vector_iter_range_ctor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading