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
19 changes: 16 additions & 3 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,16 @@ bool Converter::Convert(clang::Expr *expr,
return false;
}
if (NeedsImplicitScalarCast(ctx_, expr->IgnoreImplicit()->getType(), to)) {
StrCat(ConvertScalarCast(ToString(expr), to));
std::string str;
{
Buffer buf(*this);
{
PushParen inner(*this, Mapper::Contains(ctx_, expr));
Convert(expr);
}
str = std::move(buf).str();
}
StrCat(ConvertScalarCast(std::move(str), to));
return false;
}
}
Expand Down Expand Up @@ -2754,7 +2763,8 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
PushParen outer(*this);
{
// as binds tighter than binary operators.
PushParen inner(*this, clang::isa<clang::BinaryOperator>(sub_expr));
PushParen inner(*this, clang::isa<clang::BinaryOperator>(sub_expr) ||
Mapper::Contains(ctx_, sub_expr));
Convert(sub_expr);
}
ConvertCast(type);
Expand Down Expand Up @@ -2853,7 +2863,10 @@ bool Converter::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
}
{
PushParen paren(*this);
Convert(sub_expr);
{
PushParen inner(*this, Mapper::Contains(ctx_, sub_expr));
Convert(sub_expr);
}
if (auto *unary_oper = clang::dyn_cast<clang::UnaryOperator>(sub_expr);
unary_oper && unary_oper->getOpcode() == clang::UO_AddrOf &&
(clang::isa<clang::ArraySubscriptExpr>(unary_oper->getSubExpr()) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ impl T {
}
}
pub fn sum_0(s: Ptr<S>) -> i32 {
return ({ ({ ((*s.upgrade().deref()).v.len() as i32) } + { (array_elem!(s, n, 0).read()) }) }
+ { (array_elem!(s, n, 1).read()) });
return ({
({ (((*s.upgrade().deref()).v.len()) as i32) } + { (array_elem!(s, n, 0).read()) })
} + { (array_elem!(s, n, 1).read()) });
}
pub fn main() {
__cpp2rust_init_globals();
Expand Down Expand Up @@ -115,7 +116,7 @@ pub trait TImpl {
}
impl TImpl for Ptr<T> {
fn size(&self) -> i32 {
return ((*(*self).upgrade().deref()).v.len() as i32);
return (((*(*self).upgrade().deref()).v.len()) as i32);
}
fn move_assign(&self, _a0: Ptr<T>) -> Ptr<T> {
field!((*self), v).with_mut(|__b0| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub struct T {
}
impl T {
pub unsafe fn size(&self) -> i32 {
return (self.v.len() as i32);
return ((self.v.len()) as i32);
}
pub unsafe fn move_from(_a0: *mut T) -> Self {
let mut this = Self {
Expand All @@ -69,7 +69,7 @@ impl T {
}
}
pub unsafe fn sum_0(s: *const S) -> i32 {
return ((((*s).v.len() as i32) + ((*s).n[(0) as usize])) + ((*s).n[(1) as usize]));
return (((((*s).v.len()) as i32) + ((*s).n[(0) as usize])) + ((*s).n[(1) as usize]));
}
pub fn main() {
unsafe {
Expand Down
4 changes: 2 additions & 2 deletions tests/std/class/class.mem/out/unsafe/class.free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ impl p3_deallocation_function_is_static_Deallocates {
}
}
pub unsafe fn test_3() {
let mut one: *mut i32 = (libcc2rs::malloc_unsafe(::std::mem::size_of::<i32>()) as *mut i32);
let mut one: *mut i32 = ((libcc2rs::malloc_unsafe(::std::mem::size_of::<i32>())) as *mut i32);
(unsafe {
p3_deallocation_function_is_static_Deallocates::operator_delete(
(one as *mut ::libc::c_void),
)
});
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut deallocations_2)) == (1)));
let mut many: *mut i32 =
(libcc2rs::malloc_unsafe((::std::mem::size_of::<i32>() as usize).wrapping_mul(2_usize))
((libcc2rs::malloc_unsafe((::std::mem::size_of::<i32>() as usize).wrapping_mul(2_usize)))
as *mut i32);
(unsafe {
p3_deallocation_function_is_static_Deallocates::operator_delete_arr(
Expand Down
23 changes: 23 additions & 0 deletions tests/std/support/cmp/cmp.categories/cmp.strongord.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// ADDITIONAL_COMPILE_FLAGS: -std=c++20
#include <cassert>
#include <compare>

// N4950 [cmp.strongord]/4: Returns: v.value @ 0 for operator@.
namespace p4_compare_with_literal_zero {
int less_than_zero(std::strong_ordering o) { return o < 0; }

void test() {
std::strong_ordering o = 1 <=> 2;
int a = o < 0;
assert(a == 1);
assert(less_than_zero(o) == 1);
assert(static_cast<int>(o < 0) == 1);
long d = o > 0;
assert(d == 0);
}
} // namespace p4_compare_with_literal_zero

int main() {
p4_compare_with_literal_zero::test();
return 0;
}
30 changes: 30 additions & 0 deletions tests/std/support/cmp/cmp.categories/out/refcount/cmp.strongord.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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 less_than_zero_0(o: std::cmp::Ordering) -> i32 {
let o: Value<std::cmp::Ordering> = Rc::new(RefCell::new(o));
return ((({ (*o.borrow()) }) == std::cmp::Ordering::Less) as i32);
}
pub fn test_1() {
let o: Value<std::cmp::Ordering> = Rc::new(RefCell::new(std::cmp::Ord::cmp(&(1), &(2))));
let mut a: i32 = ((({ (*o.borrow()) }) == std::cmp::Ordering::Less) as i32);
assert!((a == 1));
assert!(({ (({ less_than_zero_0((*o.borrow()),) }) == 1) }));
assert!((((({ (*o.borrow()) }) == std::cmp::Ordering::Less) as i32) == 1));
let mut d: i64 = ((({ (*o.borrow()) }) == std::cmp::Ordering::Greater) as i64);
assert!((d == 0_i64));
}
pub fn main() {
__cpp2rust_init_globals();
libcc2rs::cpp2rust_exit(main_0());
}
fn main_0() -> i32 {
({ test_1() });
return 0;
}
pub fn __cpp2rust_init_globals() {}
31 changes: 31 additions & 0 deletions tests/std/support/cmp/cmp.categories/out/unsafe/cmp.strongord.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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 unsafe fn less_than_zero_0(mut o: std::cmp::Ordering) -> i32 {
return ((o == std::cmp::Ordering::Less) as i32);
}
pub unsafe fn test_1() {
let mut o: std::cmp::Ordering = std::cmp::Ord::cmp(&(1), &(2));
let mut a: i32 = ((o == std::cmp::Ordering::Less) as i32);
assert!(((a) == (1)));
assert!(((unsafe { less_than_zero_0(o,) }) == (1)));
assert!((((o == std::cmp::Ordering::Less) as i32) == (1)));
let mut d: i64 = ((o == std::cmp::Ordering::Greater) as i64);
assert!(((d) == (0_i64)));
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
libcc2rs::cpp2rust_exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
(unsafe { test_1() });
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
2 changes: 1 addition & 1 deletion tests/std/utilities/pairs/out/unsafe/pairs.pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub unsafe fn test_1() {
referent = 6;
assert!(((*bound.0) == (6)));
assert!(((bound.0) == ((&mut referent as *mut i32).cast_const())));
assert!(((bound.1 as i32) == (('c' as libc::c_char) as i32)));
assert!((((bound.1) as i32) == (('c' as libc::c_char) as i32)));
}
pub unsafe fn test_2() {
let mut source: (i32, Option<Box<i32>>) = (
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn test_clz_2() {
assert!((2147483648_u32.leading_zeros() as i32 == 0));
}
pub fn test_bswap16_3() {
assert!((((4660 as u16).swap_bytes() as i32) == 13330));
assert!(((((4660 as u16).swap_bytes()) as i32) == 13330));
}
pub fn test_bswap32_4() {
assert!((305419896_u32.swap_bytes() == 2018915346_u32));
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/container_field_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn run_1(mut h: Ptr<S>) {
});
let mut pv: Ptr<Vec<i32>> = (field_ref!(h, v).as_pointer());
{
let __a1 = ((*pv.upgrade().deref()).len() as i32);
let __a1 = (((*pv.upgrade().deref()).len()) as i32);
pv.with_mut(|__v: &mut Vec<i32>| __v.push(__a1))
};
assert!(
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/out/refcount/fft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ fn main_0() -> i32 {
{
let mut i: i32 = 0;
'loop_: while (i < N) {
let __rhs = (b.decay().offset((i as usize)).with(|__s| __s.re).round() as i32);
let __rhs = ((b.decay().offset((i as usize)).with(|__s| __s.re).round()) as i32);
reals.decay().offset((i as usize)).write(__rhs);
let __rhs = (b.decay().offset((i as usize)).with(|__s| __s.img).round() as i32);
let __rhs = ((b.decay().offset((i as usize)).with(|__s| __s.img).round()) as i32);
imgs.decay().offset((i as usize)).write(__rhs);
i.prefix_inc();
}
Expand All @@ -203,8 +203,8 @@ fn main_0() -> i32 {
assert!((ll.abs() == 6000000000_i64));
let mut small: i64 = (-7_i32 as i64);
let mut small_ll: i64 = (-8_i32 as i64);
let mut narrowed: i32 = (small.abs() as i32);
let mut narrowed_ll: i32 = (small_ll.abs() as i32);
let mut narrowed: i32 = ((small.abs()) as i32);
let mut narrowed_ll: i32 = ((small_ll.abs()) as i32);
assert!((narrowed == 7));
assert!((narrowed_ll == 8));
return 0;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/lambda_capture_library_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main_0() -> i32 {
let v: Value<Vec<i32>> = Rc::new(RefCell::new((*v.borrow()).clone()));
},
|| -> i32 {
return ((*v.borrow()).len() as i32);
return (((*v.borrow()).len()) as i32);
}
)));
let g: Value<FnPtr<fn() -> i32>> = Rc::new(RefCell::new((*f.borrow()).copy_from()));
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/out/refcount/max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ fn main_0() -> i32 {
{
let mut i: i32 = 0;
'loop_: while (i < 3) {
let __rhs = ({
let __rhs = (({
let __tmp_13: Value<u64> = Rc::new(RefCell::new((largest as u64)));
(match (Rc::new(RefCell::new((({ size_of_2(i) }) as u64))).as_pointer(),) {
(__tmp_12,) => {
Expand All @@ -202,7 +202,7 @@ fn main_0() -> i32 {
}
}
.read())
} as usize);
}) as usize);
largest = __rhs;
i.prefix_inc();
}
Expand Down
28 changes: 14 additions & 14 deletions tests/unit/out/refcount/size_t_ssize_t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn main_0() -> i32 {
);
let mut fr2: u64 = ({ take_ulong_1((sz as u64)) });
assert!((fr2 == 21_u64));
let mut lo: usize = ({
let mut lo: usize = (({
let __tmp_0: Value<u64> = Rc::new(RefCell::new(
(::std::mem::size_of::<i64>() as u64).wrapping_add((*ul.borrow())),
));
Expand All @@ -68,8 +68,8 @@ fn main_0() -> i32 {
__tmp_1.as_pointer()
}
.read())
} as usize);
let mut hi: usize = ({
}) as usize);
let mut hi: usize = (({
let __tmp_2: Value<u64> = Rc::new(RefCell::new(
(::std::mem::size_of::<i32>() as u64).wrapping_add((sz as u64)),
));
Expand All @@ -79,10 +79,10 @@ fn main_0() -> i32 {
__tmp_2.as_pointer()
}
.read())
} as usize);
}) as usize);
assert!((lo == (::std::mem::size_of::<i64>() as usize).wrapping_add(10_usize)));
assert!((hi == (::std::mem::size_of::<i32>() as usize).wrapping_add(21_usize)));
let mut bound: usize = ({
let mut bound: usize = (({
let __tmp_3: Value<u64> = Rc::new(RefCell::new((4_usize as u64)));
let __tmp_4: Value<u64> = Rc::new(RefCell::new((sz as u64)));
(if CppLess::less(&__tmp_3.as_pointer(), &__tmp_4.as_pointer()) {
Expand All @@ -91,7 +91,7 @@ fn main_0() -> i32 {
__tmp_4.as_pointer()
}
.read())
} as usize);
}) as usize);
assert!((bound == 4_usize));
let mut data: [i32; 8] = [0_i32; 8];
let mut count: usize = (::std::mem::size_of::<[i32; 8]>() as usize)
Expand Down Expand Up @@ -134,7 +134,7 @@ fn main_0() -> i32 {
let mut sm: isize = (((s2 as i64) + l) as isize);
assert!((sm == 15_isize));
assert!((sm > (l as isize)));
let mut smin: isize = ({
let mut smin: isize = (({
let __tmp_5: Value<i64> = Rc::new(RefCell::new((sm as i64)));
let __tmp_6: Value<i64> = Rc::new(RefCell::new((sd as i64)));
(if CppLess::less(&__tmp_5.as_pointer(), &__tmp_6.as_pointer()) {
Expand All @@ -143,8 +143,8 @@ fn main_0() -> i32 {
__tmp_6.as_pointer()
}
.read())
} as isize);
let mut smax: isize = ({
}) as isize);
let mut smax: isize = (({
let __tmp_7: Value<i64> = Rc::new(RefCell::new((sd as i64)));
let __tmp_8: Value<i64> = Rc::new(RefCell::new((sm as i64)));
(if CppLess::less(&__tmp_7.as_pointer(), &__tmp_8.as_pointer()) {
Expand All @@ -153,7 +153,7 @@ fn main_0() -> i32 {
__tmp_7.as_pointer()
}
.read())
} as isize);
}) as isize);
assert!((smin == (-7_i32 as isize)));
assert!((smax == 15_isize));
let mut delta: isize = ((sz as isize) - ((*ul.borrow()) as isize));
Expand All @@ -171,7 +171,7 @@ fn main_0() -> i32 {
let mut mx: usize = 5_usize;
let mut mins: [usize; 4] = [
0_usize,
({
(({
let __tmp_9: Value<u64> = Rc::new(RefCell::new((mx as u64)));
let __tmp_10: Value<u64> = Rc::new(RefCell::new(1_u64));
(if CppLess::less(&__tmp_9.as_pointer(), &__tmp_10.as_pointer()) {
Expand All @@ -180,8 +180,8 @@ fn main_0() -> i32 {
__tmp_10.as_pointer()
}
.read())
} as usize),
({
}) as usize),
(({
let __tmp_11: Value<u64> = Rc::new(RefCell::new(((mx).wrapping_sub(3_usize) as u64)));
let __tmp_12: Value<u64> = Rc::new(RefCell::new((mx as u64)));
(if CppLess::less(&__tmp_11.as_pointer(), &__tmp_12.as_pointer()) {
Expand All @@ -190,7 +190,7 @@ fn main_0() -> i32 {
__tmp_12.as_pointer()
}
.read())
} as usize),
}) as usize),
mx,
];
assert!((mins[(1) as usize] == 1_usize));
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/unboxed_conditional_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn lehmer_3(mut sigma: Ptr<u32>, mut len: u32, mut code: Ptr<u32>) {
},
));
assert!((*it.borrow()) != items.decay().end());
let __rhs = ((*it.borrow()).iter_distance(&items.decay().begin()) as u32);
let __rhs = (((*it.borrow()).iter_distance(&items.decay().begin())) as u32);
elem!(code, i).write(__rhs);
match ({ <RandomAccessIterator<u32>>::from(({ (*it.borrow()).clone() })) },) {
(mut __tmp_2,) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/va_arg_printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn lenf_2(fmt: Ptr<i8>, __args: Box<[VaArg]>) -> i32 {
let ap: Value<Ptr<VaArg>> = Rc::new(RefCell::new(Ptr::<VaArg>::default()));
(*ap.borrow_mut()) = __args.decay();
let mut s: Ptr<i8> = (*ap.borrow_mut()).arg::<Ptr<i8>>();
let mut result: i32 = (s.to_c_string_iterator().count() as i32);
let mut result: i32 = ((s.to_c_string_iterator().count()) as i32);
return result;
}
pub fn main() {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/vector_subscript_direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn push_and_index_0(v: Ptr<Vec<i32>>) -> i32 {
let __a1 = 42;
v.with_mut(|__v: &mut Vec<i32>| __v.push(__a1))
};
return (((*v.upgrade().deref()).len() as i32) - 1);
return ((((*v.upgrade().deref()).len()) as i32) - 1);
}
pub fn sum_ref_1(v: Ptr<Vec<i32>>) -> i32 {
let mut s: i32 = 0;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub unsafe fn test_clz_2() {
assert!(((2147483648_u32.leading_zeros() as i32) == (0)));
}
pub unsafe fn test_bswap16_3() {
assert!((((4660 as u16).swap_bytes() as i32) == (13330)));
assert!(((((4660 as u16).swap_bytes()) as i32) == (13330)));
}
pub unsafe fn test_bswap32_4() {
assert!(((305419896_u32.swap_bytes()) == (2018915346_u32)));
Expand Down
Loading
Loading