Skip to content

Commit 0bef2dc

Browse files
committed
remove explicit temporary from rule
1 parent 8cc028c commit 0bef2dc

6 files changed

Lines changed: 41 additions & 48 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,11 +2550,18 @@ bool Converter::VisitStringLiteral(clang::StringLiteral *expr) {
25502550
GetEscapedStringLiteral(expr, 1)));
25512551
return false;
25522552
}
2553+
std::string literal;
25532554
if (expr->getString().contains('\0')) {
2554-
EmitFresh(GetCharArrayLiteral(expr));
2555-
return false;
2555+
literal = GetCharArrayLiteral(expr);
2556+
} else {
2557+
literal = std::format("c{}", GetEscapedStringLiteral(expr, 0));
2558+
}
2559+
// A pointer to the array.
2560+
if (isAddrOf()) {
2561+
literal = std::format("{}.as_ptr().cast::<{}>()", literal,
2562+
ToString(expr->getType()));
25562563
}
2557-
EmitFresh(std::format("c{}", GetEscapedStringLiteral(expr, 0)));
2564+
EmitFresh(literal);
25582565
return false;
25592566
}
25602567

‎cpp2rust/converter/translation_rule.cpp‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,16 @@ std::vector<bool>
268268
ExprRule::argsToBind(clang::ASTContext &ctx, const clang::Expr *expr,
269269
llvm::ArrayRef<clang::Expr *> args) const {
270270
// The number of evaluations of each argument, two standing for any other
271-
// than one, and the position of the first.
272-
std::vector<unsigned> uses(args.size()), first(args.size());
271+
// than one, the position of the first, and the number of placeholders.
272+
std::vector<unsigned> uses(args.size()), first(args.size()),
273+
copies(args.size());
273274
unsigned position = 0;
274275
AnyFragment(body, [&](const BodyFragment &frag) {
275276
if (auto *ph = std::get_if<PlaceholderFragment>(&frag)) {
276277
if (uses[ph->n]++ == 0) {
277278
first[ph->n] = position;
278279
}
280+
++copies[ph->n];
279281
if (ph->conditional) {
280282
++uses[ph->n];
281283
}
@@ -327,6 +329,11 @@ ExprRule::argsToBind(clang::ASTContext &ctx, const clang::Expr *expr,
327329
// A value computed from others is not computed again, nor before the
328330
// side effects of the previous arguments.
329331
bind[i] = bind_rest = true;
332+
} else if (copies[i] > 1 &&
333+
clang::isa<clang::StringLiteral>(arg->IgnoreParens())) {
334+
// A string literal is one object, but each of its translations is a
335+
// new array.
336+
bind[i] = true;
330337
}
331338
}
332339
return bind;

‎rules/vector/tgt_unsafe.rs‎

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

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

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

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,8 @@ 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-
let __array = &sizes;
198-
__array.as_ptr().add(__array.len())
199-
} as *const u64)
200-
.cast::<usize>();
196+
let mut sizes_end: *const usize =
197+
(sizes.as_ptr().add(sizes.len()) as *const u64).cast::<usize>();
201198
assert!(
202199
((((sizes_end as usize - sizes.as_ptr() as usize) / ::std::mem::size_of::<usize>())
203200
as i64)

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

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,7 @@ 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 ({
87-
{
88-
let __array = &src1;
89-
__array.as_ptr().add(__array.len())
90-
}
91-
},)
92-
{
86+
let mut v4: Vec<u32> = match ({ src1.as_ptr().add(src1.len()) },) {
9387
(mut __tmp_3,) => {
9488
let mut __v = Vec::new();
9589
let mut __it = src1.as_ptr();
@@ -105,25 +99,20 @@ unsafe fn main_0() -> i32 {
10599
(((v4[(0_usize)]) == (1_u32)) && ((v4[(1_usize)]) == (2_u32)))
106100
&& ((v4[(2_usize)]) == (3_u32))
107101
);
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-
};
102+
let mut lit_end: *const libc::c_char =
103+
match ((c"abc".as_ptr().cast::<[libc::c_char; 4]>() as *const [libc::c_char; 4]),) {
104+
(__tmp_4,) => (*__tmp_4).as_ptr().add((*__tmp_4).len()),
105+
};
117106
assert!((((*(lit_end.offset(-((1) as isize)))) as i32) == (('\0' as libc::c_char) as i32)));
118107
assert!((((*(lit_end.offset(-((4) as isize)))) as i32) == (('a' as libc::c_char) as i32)));
119108
let mut buf: [u8; 5] = [10_u8, 20_u8, 30_u8, 40_u8, 50_u8];
120109
let mut start: *const u8 = (buf.as_mut_ptr()).cast_const();
121110
let mut len: usize = 5_usize;
122111
let mut v5: Vec<u8> = match ({ start.offset((len) as isize) },) {
123-
(mut __tmp_4,) => {
112+
(mut __tmp_5,) => {
124113
let mut __v = Vec::new();
125114
let mut __it = start;
126-
while __it != __tmp_4 {
115+
while __it != __tmp_5 {
127116
__v.push((*__it.iter_deref()));
128117
__it.iter_inc();
129118
}
@@ -137,10 +126,10 @@ unsafe fn main_0() -> i32 {
137126
(*m.subscript_move(&mut 2, |__k| cpp_move_unsafe!(__k))) = 20;
138127
(*m.subscript_move(&mut 3, |__k| cpp_move_unsafe!(__k))) = 30;
139128
let mut v6: Vec<(i32, i32)> = match ({ m.begin() }, { m.end() }) {
140-
(mut __tmp_5, mut __tmp_6) => {
129+
(mut __tmp_6, mut __tmp_7) => {
141130
let mut __v = Vec::new();
142-
let mut __it = (__tmp_5).clone();
143-
while __it != __tmp_6 {
131+
let mut __it = (__tmp_6).clone();
132+
while __it != __tmp_7 {
144133
__v.push(<(i32, i32) as CppFromUnsafe<(i32, i32)>>::cpp_from(
145134
__it.iter_deref(),
146135
));
@@ -157,12 +146,12 @@ unsafe fn main_0() -> i32 {
157146
{ m.begin() },
158147
{ m.end() },
159148
) {
160-
(mut __tmp_7, mut __tmp_8, mut __tmp_9) => {
149+
(mut __tmp_8, mut __tmp_9, mut __tmp_10) => {
161150
let __off =
162-
__tmp_7.iter_distance(&UnsafeRandomAccessIterator::Vec(v6.as_mut_ptr())) as usize;
151+
__tmp_8.iter_distance(&UnsafeRandomAccessIterator::Vec(v6.as_mut_ptr())) as usize;
163152
let mut __tmp = Vec::new();
164-
let mut __it = (__tmp_8).clone();
165-
while __it != __tmp_9 {
153+
let mut __it = (__tmp_9).clone();
154+
while __it != __tmp_10 {
166155
__tmp.push((*__it.iter_deref()).clone());
167156
__it.iter_inc();
168157
}
@@ -176,10 +165,10 @@ unsafe fn main_0() -> i32 {
176165
);
177166
let mut ints: [i32; 3] = [4, 5, 6];
178167
let mut v7: Vec<Degrees> = match ({ ints.as_mut_ptr().offset((3) as isize) },) {
179-
(mut __tmp_10,) => {
168+
(mut __tmp_11,) => {
180169
let mut __v = Vec::new();
181170
let mut __it = ints.as_mut_ptr();
182-
while __it != __tmp_10 {
171+
while __it != __tmp_11 {
183172
__v.push(<Degrees as CppFromUnsafe<i32>>::cpp_from(__it.iter_deref()));
184173
__it.iter_inc();
185174
}
@@ -189,10 +178,10 @@ unsafe fn main_0() -> i32 {
189178
assert!(((v7[(1_usize)].value) == (5)));
190179
let mut readings: [Reading; 2] = [Reading { raw_: 4 }, Reading { raw_: 5 }];
191180
let mut v8: Vec<i64> = match ({ readings.as_mut_ptr().offset((2) as isize) },) {
192-
(mut __tmp_11,) => {
181+
(mut __tmp_12,) => {
193182
let mut __v = Vec::new();
194183
let mut __it = readings.as_mut_ptr();
195-
while __it != __tmp_11 {
184+
while __it != __tmp_12 {
196185
__v.push(<i64 as CppFromUnsafe<Reading>>::cpp_from(__it.iter_deref()));
197186
__it.iter_inc();
198187
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,7 @@ 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 ({
307-
{
308-
let __array = &src1;
309-
__array.as_ptr().add(__array.len())
310-
}
311-
},)
312-
{
306+
let mut v12: Vec<u32> = match ({ src1.as_ptr().add(src1.len()) },) {
313307
(mut __tmp_5,) => {
314308
let mut __v = Vec::new();
315309
let mut __it = src1.as_ptr();

0 commit comments

Comments
 (0)