Skip to content

Commit bf2b23e

Browse files
authored
Add parens around applied rules (#658)
To fix problems as `a < b as i32` which should be `(a < b) as i32`
1 parent 412c2ca commit bf2b23e

43 files changed

Lines changed: 215 additions & 116 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cpp2rust/converter/converter.cpp‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,7 +1653,16 @@ bool Converter::Convert(clang::Expr *expr,
16531653
return false;
16541654
}
16551655
if (NeedsImplicitScalarCast(ctx_, expr->IgnoreImplicit()->getType(), to)) {
1656-
StrCat(ConvertScalarCast(ToString(expr), to));
1656+
std::string str;
1657+
{
1658+
Buffer buf(*this);
1659+
{
1660+
PushParen inner(*this, Mapper::Contains(ctx_, expr));
1661+
Convert(expr);
1662+
}
1663+
str = std::move(buf).str();
1664+
}
1665+
StrCat(ConvertScalarCast(std::move(str), to));
16571666
return false;
16581667
}
16591668
}
@@ -2763,7 +2772,8 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
27632772
PushParen outer(*this);
27642773
{
27652774
// as binds tighter than binary operators.
2766-
PushParen inner(*this, clang::isa<clang::BinaryOperator>(sub_expr));
2775+
PushParen inner(*this, clang::isa<clang::BinaryOperator>(sub_expr) ||
2776+
Mapper::Contains(ctx_, sub_expr));
27672777
Convert(sub_expr);
27682778
}
27692779
ConvertCast(type);
@@ -2862,7 +2872,10 @@ bool Converter::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
28622872
}
28632873
{
28642874
PushParen paren(*this);
2865-
Convert(sub_expr);
2875+
{
2876+
PushParen inner(*this, Mapper::Contains(ctx_, sub_expr));
2877+
Convert(sub_expr);
2878+
}
28662879
if (auto *unary_oper = clang::dyn_cast<clang::UnaryOperator>(sub_expr);
28672880
unary_oper && unary_oper->getOpcode() == clang::UO_AddrOf &&
28682881
(clang::isa<clang::ArraySubscriptExpr>(unary_oper->getSubExpr()) ||

‎tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ impl T {
5858
}
5959
}
6060
pub fn sum_0(s: Ptr<S>) -> i32 {
61-
return ({ ({ ((*s.upgrade().deref()).v.len() as i32) } + { (array_elem!(s, n, 0).read()) }) }
62-
+ { (array_elem!(s, n, 1).read()) });
61+
return ({
62+
({ (((*s.upgrade().deref()).v.len()) as i32) } + { (array_elem!(s, n, 0).read()) })
63+
} + { (array_elem!(s, n, 1).read()) });
6364
}
6465
pub fn main() {
6566
__cpp2rust_init_globals();
@@ -115,7 +116,7 @@ pub trait TImpl {
115116
}
116117
impl TImpl for Ptr<T> {
117118
fn size(&self) -> i32 {
118-
return ((*(*self).upgrade().deref()).v.len() as i32);
119+
return (((*(*self).upgrade().deref()).v.len()) as i32);
119120
}
120121
fn move_assign(&self, _a0: Ptr<T>) -> Ptr<T> {
121122
field!((*self), v).with_mut(|__b0| {

‎tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub struct T {
5353
}
5454
impl T {
5555
pub unsafe fn size(&self) -> i32 {
56-
return (self.v.len() as i32);
56+
return ((self.v.len()) as i32);
5757
}
5858
pub unsafe fn move_from(_a0: *mut T) -> Self {
5959
let mut this = Self {
@@ -69,7 +69,7 @@ impl T {
6969
}
7070
}
7171
pub unsafe fn sum_0(s: *const S) -> i32 {
72-
return ((((*s).v.len() as i32) + ((*s).n[(0) as usize])) + ((*s).n[(1) as usize]));
72+
return (((((*s).v.len()) as i32) + ((*s).n[(0) as usize])) + ((*s).n[(1) as usize]));
7373
}
7474
pub fn main() {
7575
unsafe {

‎tests/std/class/class.mem/out/unsafe/class.free.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ impl p3_deallocation_function_is_static_Deallocates {
5555
}
5656
}
5757
pub unsafe fn test_3() {
58-
let mut one: *mut i32 = (libcc2rs::malloc_unsafe(::std::mem::size_of::<i32>()) as *mut i32);
58+
let mut one: *mut i32 = ((libcc2rs::malloc_unsafe(::std::mem::size_of::<i32>())) as *mut i32);
5959
(unsafe {
6060
p3_deallocation_function_is_static_Deallocates::operator_delete(
6161
(one as *mut ::libc::c_void),
6262
)
6363
});
6464
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut deallocations_2)) == (1)));
6565
let mut many: *mut i32 =
66-
(libcc2rs::malloc_unsafe((::std::mem::size_of::<i32>() as usize).wrapping_mul(2_usize))
66+
((libcc2rs::malloc_unsafe((::std::mem::size_of::<i32>() as usize).wrapping_mul(2_usize)))
6767
as *mut i32);
6868
(unsafe {
6969
p3_deallocation_function_is_static_Deallocates::operator_delete_arr(
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// ADDITIONAL_COMPILE_FLAGS: -std=c++20
2+
#include <cassert>
3+
#include <compare>
4+
5+
// N4950 [cmp.strongord]/4: Returns: v.value @ 0 for operator@.
6+
namespace p4_compare_with_literal_zero {
7+
int less_than_zero(std::strong_ordering o) { return o < 0; }
8+
9+
void test() {
10+
std::strong_ordering o = 1 <=> 2;
11+
int a = o < 0;
12+
assert(a == 1);
13+
assert(less_than_zero(o) == 1);
14+
assert(static_cast<int>(o < 0) == 1);
15+
long d = o > 0;
16+
assert(d == 0);
17+
}
18+
} // namespace p4_compare_with_literal_zero
19+
20+
int main() {
21+
p4_compare_with_literal_zero::test();
22+
return 0;
23+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn less_than_zero_0(o: std::cmp::Ordering) -> i32 {
10+
let o: Value<std::cmp::Ordering> = Rc::new(RefCell::new(o));
11+
return ((({ (*o.borrow()) }) == std::cmp::Ordering::Less) as i32);
12+
}
13+
pub fn test_1() {
14+
let o: Value<std::cmp::Ordering> = Rc::new(RefCell::new(std::cmp::Ord::cmp(&(1), &(2))));
15+
let mut a: i32 = ((({ (*o.borrow()) }) == std::cmp::Ordering::Less) as i32);
16+
assert!((a == 1));
17+
assert!(({ (({ less_than_zero_0((*o.borrow()),) }) == 1) }));
18+
assert!((((({ (*o.borrow()) }) == std::cmp::Ordering::Less) as i32) == 1));
19+
let mut d: i64 = ((({ (*o.borrow()) }) == std::cmp::Ordering::Greater) as i64);
20+
assert!((d == 0_i64));
21+
}
22+
pub fn main() {
23+
__cpp2rust_init_globals();
24+
libcc2rs::cpp2rust_exit(main_0());
25+
}
26+
fn main_0() -> i32 {
27+
({ test_1() });
28+
return 0;
29+
}
30+
pub fn __cpp2rust_init_globals() {}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub unsafe fn less_than_zero_0(mut o: std::cmp::Ordering) -> i32 {
10+
return ((o == std::cmp::Ordering::Less) as i32);
11+
}
12+
pub unsafe fn test_1() {
13+
let mut o: std::cmp::Ordering = std::cmp::Ord::cmp(&(1), &(2));
14+
let mut a: i32 = ((o == std::cmp::Ordering::Less) as i32);
15+
assert!(((a) == (1)));
16+
assert!(((unsafe { less_than_zero_0(o,) }) == (1)));
17+
assert!((((o == std::cmp::Ordering::Less) as i32) == (1)));
18+
let mut d: i64 = ((o == std::cmp::Ordering::Greater) as i64);
19+
assert!(((d) == (0_i64)));
20+
}
21+
pub fn main() {
22+
unsafe {
23+
__cpp2rust_init_globals();
24+
libcc2rs::cpp2rust_exit(main_0() as i32);
25+
}
26+
}
27+
unsafe fn main_0() -> i32 {
28+
(unsafe { test_1() });
29+
return 0;
30+
}
31+
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/std/utilities/pairs/out/unsafe/pairs.pair.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub unsafe fn test_1() {
6565
referent = 6;
6666
assert!(((*bound.0) == (6)));
6767
assert!(((bound.0) == ((&mut referent as *mut i32).cast_const())));
68-
assert!(((bound.1 as i32) == (('c' as libc::c_char) as i32)));
68+
assert!((((bound.1) as i32) == (('c' as libc::c_char) as i32)));
6969
}
7070
pub unsafe fn test_2() {
7171
let mut source: (i32, Option<Box<i32>>) = (

‎tests/unit/out/refcount/builtin.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn test_clz_2() {
1919
assert!((2147483648_u32.leading_zeros() as i32 == 0));
2020
}
2121
pub fn test_bswap16_3() {
22-
assert!((((4660 as u16).swap_bytes() as i32) == 13330));
22+
assert!(((((4660 as u16).swap_bytes()) as i32) == 13330));
2323
}
2424
pub fn test_bswap32_4() {
2525
assert!((305419896_u32.swap_bytes() == 2018915346_u32));

‎tests/unit/out/refcount/container_field_addr.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn run_1(mut h: Ptr<S>) {
3737
});
3838
let mut pv: Ptr<Vec<i32>> = (field_ref!(h, v).as_pointer());
3939
{
40-
let __a1 = ((*pv.upgrade().deref()).len() as i32);
40+
let __a1 = (((*pv.upgrade().deref()).len()) as i32);
4141
pv.with_mut(|__v: &mut Vec<i32>| __v.push(__a1))
4242
};
4343
assert!(

0 commit comments

Comments
 (0)