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
7 changes: 6 additions & 1 deletion cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2108,13 +2108,17 @@ void Converter::ConvertBinaryOperator(clang::BinaryOperator *expr) {
if (expr->isCompoundAssignmentOp() &&
expr->getLHS()->getType()->isPointerType() &&
expr->getRHS()->getType()->isIntegralOrEnumerationType()) {
PushBrace brace(*this, !isVoid());
Convert(lhs);
StrCat(token::kAssign);
{
PushParen paren(*this);
ConvertUnsignedArithOperand(lhs, type);
}
ConvertUnsignedArithBinaryOperator(expr, rhs);
if (!isVoid()) {
StrCat(token::kSemiColon, ConvertRValue(lhs));
}
} else {
ConvertAssignment(lhs, rhs, opcode_as_string);
}
Expand Down Expand Up @@ -3030,7 +3034,8 @@ bool Converter::VisitSwitchStmt(clang::SwitchStmt *stmt) {
if (has_fallthrough) {
StrCat("match", ToString(stmt->getCond()));
} else {
StrCat(std::format("let __match_cond = {};", ToString(stmt->getCond())));
StrCat(
std::format("let __match_cond = {};", ConvertRValue(stmt->getCond())));
StrCat("match __match_cond");
}

Expand Down
2 changes: 1 addition & 1 deletion cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1879,7 +1879,7 @@ void ConverterRefCount::ConvertAssignment(clang::Expr *lhs, clang::Expr *rhs,
}

if (isRValue()) {
StrCat(token::kSemiColon, ConvertRValue(lhs));
StrCat(token::kSemiColon, ConvertFreshRValue(lhs));
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/unit/assign_as_value.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <assert.h>

int main(void) {
char buf[2];
char *p = buf, *q;
q = p += 1;
assert(q == buf + 1);

char out;
switch (out = 'x') {
case 'x':
assert(1);
break;
default:
assert(0);
break;
}
assert(out == 'x');
return 0;
}
48 changes: 48 additions & 0 deletions tests/unit/out/refcount/assign_as_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
let buf: Value<Box<[u8]>> = Rc::new(RefCell::new(
(0..2).map(|_| <u8>::default()).collect::<Box<[u8]>>(),
));
let p: Value<Ptr<u8>> = Rc::new(RefCell::new((buf.as_pointer() as Ptr<u8>)));
let q: Value<Ptr<u8>> = Rc::new(RefCell::new(Ptr::<u8>::null()));
(*q.borrow_mut()) = {
(*p.borrow_mut()) += 1;
(*p.borrow()).clone()
};
assert!(
((({
let _lhs = (*q.borrow()).clone();
_lhs == (buf.as_pointer() as Ptr<u8>).offset((1) as isize)
}) as i32)
!= 0)
);
let out: Value<u8> = <Value<u8>>::default();
'switch: {
let __match_cond = (({
(*out.borrow_mut()) = (('x' as i32) as u8);
(*out.borrow())
}) as i32);
match __match_cond {
v if v == ('x' as i32) => {
assert!((1 != 0));
break 'switch;
}
_ => {
assert!((0 != 0));
break 'switch;
}
}
};
assert!((((((*out.borrow()) as i32) == ('x' as i32)) as i32) != 0));
return 0;
}
42 changes: 42 additions & 0 deletions tests/unit/out/unsafe/assign_as_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
extern crate libc;
use libc::*;
extern crate libcc2rs;
use libcc2rs::*;
use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
pub fn main() {
unsafe {
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
let mut buf: [u8; 2] = [0_u8; 2];
let mut p: *mut u8 = buf.as_mut_ptr();
let mut q: *mut u8 = std::ptr::null_mut();
q = {
p = (p).wrapping_add(1 as i32 as usize);
p
};
assert!(((((q) == (buf.as_mut_ptr().offset((1) as isize))) as i32) != 0));
let mut out: u8 = 0_u8;
'switch: {
let __match_cond = (({
out = (('x' as i32) as u8);
out
}) as i32);
match __match_cond {
v if v == ('x' as i32) => {
assert!((1 != 0));
break 'switch;
}
_ => {
assert!((0 != 0));
break 'switch;
}
}
};
assert!(((((out as i32) == ('x' as i32)) as i32) != 0));
return 0;
}
4 changes: 2 additions & 2 deletions tests/unit/out/unsafe/union_tagged_struct_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn main() {
}
}
unsafe fn main_0() -> i32 {
static mut items_0: [*mut u8; 3] = unsafe {
static mut items_4: [*mut u8; 3] = unsafe {
[
b"a\0".as_ptr().cast_mut(),
b"b\0".as_ptr().cast_mut(),
Expand All @@ -83,7 +83,7 @@ unsafe fn main_0() -> i32 {
let mut p_list: Branch = <Branch>::default();
p_list.choice = Choice::C_LIST;
p_list.index = 0;
p_list.v.list.items = items_0.as_mut_ptr();
p_list.v.list.items = items_4.as_mut_ptr();
p_list.v.list.count = 3_i64;
p_list.v.list.cursor = 1_i64;
assert!(((((p_list.v.list.count) == (3_i64)) as i32) != 0));
Expand Down
Loading