Skip to content

Commit 4279525

Browse files
committed
code simplifications
1 parent 59bfdf6 commit 4279525

267 files changed

Lines changed: 2899 additions & 4132 deletions

File tree

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: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,13 +2473,21 @@ void Converter::ConvertIntegralToBooleanCast(clang::ImplicitCastExpr *expr) {
24732473
return;
24742474
}
24752475

2476-
if (auto binop = clang::dyn_cast<clang::BinaryOperator>(stripped)) {
2477-
// Comparisons and logical ops already produces bool, no wrap needed.
2478-
if ((binop->isComparisonOp() || binop->isLogicalOp()) &&
2479-
binop->getType()->isBooleanType()) {
2480-
Convert(sub_expr);
2481-
return;
2482-
}
2476+
if (auto binop = clang::dyn_cast<clang::BinaryOperator>(stripped);
2477+
binop && (binop->isComparisonOp() || binop->isLogicalOp())) {
2478+
// Comparisons and logical ops produce a bool, which C makes an int.
2479+
PushParen paren(*this);
2480+
ConvertBinaryOperator(binop);
2481+
return;
2482+
}
2483+
if (auto *unop = clang::dyn_cast<clang::UnaryOperator>(stripped);
2484+
unop && unop->getOpcode() == clang::UO_LNot) {
2485+
Convert(NormalizeToBool(unop, ctx_));
2486+
return;
2487+
}
2488+
if (auto *literal = clang::dyn_cast<clang::IntegerLiteral>(stripped)) {
2489+
EmitFresh(literal->getValue().isZero() ? keyword::kFalse : keyword::kTrue);
2490+
return;
24832491
}
24842492

24852493
PushParen paren(*this);

‎libcc2rs/src/cstr.rs‎

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::fmt;
77
use std::rc::Rc;
88

99
use crate::CStringIterator;
10-
use crate::rc::{FieldShape, Ptr, PtrKind};
10+
use crate::rc::{Ptr, PtrKind};
1111
use crate::reinterpret::{ByteRepr, with_scratch};
1212

1313
// The element types of C strings: `char` and `signed char` map to `i8`,
@@ -196,13 +196,8 @@ impl<T: CChar> Ptr<T> {
196196
}
197197
}
198198

199-
#[allow(clippy::explicit_counter_loop)]
200199
pub fn memset(&self, value: T, num: usize) {
201-
let mut dst = self.clone();
202-
for _ in 0..num {
203-
dst.write(value);
204-
dst += 1;
205-
}
200+
self.with_slice_mut(num, |s| s.fill(value))
206201
}
207202

208203
#[allow(clippy::explicit_counter_loop)]
@@ -235,38 +230,14 @@ impl<T: CChar> Ptr<T> {
235230
None => panic!("ub: unterminated string"),
236231
}
237232
}
238-
match &self.kind {
239-
PtrKind::Null => panic!("ub: null pointer"),
240-
PtrKind::StackSingle(weak) | PtrKind::HeapSingle(weak) => {
241-
assert_eq!(self.offset, 0, "ub: invalid offset");
242-
let rc = weak.upgrade().expect("ub: dangling pointer");
243-
let b = rc.borrow();
244-
f(until_nul(std::slice::from_ref(&*b)))
245-
}
246-
PtrKind::StackArray(weak) | PtrKind::HeapArray(weak) => {
247-
let rc = weak.upgrade().expect("ub: dangling pointer");
248-
let b = rc.borrow();
249-
f(until_nul(&b[self.offset..]))
250-
}
251-
PtrKind::StackVec(weak) | PtrKind::HeapVec(weak) => {
252-
let rc = weak.upgrade().expect("ub: dangling pointer");
253-
let b = rc.borrow();
254-
f(until_nul(&b[self.offset..]))
255-
}
256-
PtrKind::Nested(parent) => {
257-
let root = parent.root();
258-
f(until_nul(&parent.borrow_slice(&root)[self.offset..]))
259-
}
260-
PtrKind::Field(root, _, FieldShape::Single) => {
261-
let root = crate::field::upgrade(root);
262-
let b = Ptr::<T>::borrow_field(&*root, self.offset);
263-
f(until_nul(std::slice::from_ref(&*b)))
264-
}
265-
PtrKind::Field(..) => {
266-
self.with_slice(self.len() - self.get_offset(), |s| f(until_nul(s)))
267-
}
268-
PtrKind::Reinterpreted(_) => f(&self.to_c_string_iterator().collect::<Vec<T>>()),
233+
// Reinterpreted memory is read until the NUL, instead of until the end
234+
// of the allocation.
235+
if let PtrKind::Reinterpreted(_) = self.kind {
236+
return f(&self.to_c_string_iterator().collect::<Vec<T>>());
269237
}
238+
// The string is in the rest of the allocation.
239+
let len = self.len().saturating_sub(self.get_offset());
240+
self.with_slice(len, |s| f(until_nul(s)))
270241
}
271242

272243
// The length of the C string, like `strlen`.

‎libcc2rs/src/rc.rs‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ impl<T: 'static> Ptr<T> {
122122
pub(crate) fn borrow_in<'a>(&self, root: &'a SliceRoot) -> Ref<'a, T> {
123123
let offset = self.offset;
124124
match &self.kind {
125-
PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) => root.alloc::<T>().borrow(),
125+
PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) => {
126+
check_single(offset);
127+
root.alloc::<T>().borrow()
128+
}
126129
PtrKind::StackVec(_) | PtrKind::HeapVec(_) => {
127130
Ref::map(root.alloc::<Vec<T>>().borrow(), |v| &v[offset])
128131
}
@@ -144,7 +147,10 @@ impl<T: 'static> Ptr<T> {
144147
pub(crate) fn borrow_in_mut<'a>(&self, root: &'a SliceRoot) -> RefMut<'a, T> {
145148
let offset = self.offset;
146149
match &self.kind {
147-
PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) => root.alloc::<T>().borrow_mut(),
150+
PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) => {
151+
check_single(offset);
152+
root.alloc::<T>().borrow_mut()
153+
}
148154
PtrKind::StackVec(_) | PtrKind::HeapVec(_) => {
149155
RefMut::map(root.alloc::<Vec<T>>().borrow_mut(), |v| &mut v[offset])
150156
}
@@ -595,6 +601,7 @@ impl<T> Ptr<T> {
595601
match &self.kind {
596602
PtrKind::Null => null_deref(),
597603
PtrKind::StackSingle(weak) | PtrKind::HeapSingle(weak) => {
604+
check_single(self.offset);
598605
StrongPtr::StackSingle(weak.upgrade().unwrap_or_else(|| dangling()))
599606
}
600607
PtrKind::StackVec(weak) | PtrKind::HeapVec(weak) => StrongPtr::Vec {
@@ -766,6 +773,7 @@ impl<T> Ptr<T> {
766773
{
767774
match &self.kind {
768775
PtrKind::StackSingle(weak) | PtrKind::HeapSingle(weak) => {
776+
check_single(offset);
769777
let rc = weak.upgrade().unwrap_or_else(|| dangling());
770778
let mut borrow = rc.borrow_mut();
771779
f(&mut *borrow)
@@ -887,6 +895,7 @@ impl<T> Ptr<T> {
887895
{
888896
match &self.kind {
889897
PtrKind::StackSingle(weak) | PtrKind::HeapSingle(weak) => {
898+
check_single(offset);
890899
let rc = weak.upgrade().unwrap_or_else(|| dangling());
891900
let borrow = rc.borrow();
892901
f(&*borrow)
@@ -957,6 +966,22 @@ pub(crate) fn dangling() -> ! {
957966
panic!("ub: dangling pointer")
958967
}
959968

969+
// A pointer to a single object can only be dereferenced at offset 0, not
970+
// past the object.
971+
#[inline(always)]
972+
#[track_caller]
973+
fn check_single(offset: usize) {
974+
#[cold]
975+
#[inline(never)]
976+
#[track_caller]
977+
fn out_of_bounds(offset: usize) -> ! {
978+
panic!("ub: access at offset {offset} of a single object")
979+
}
980+
if offset != 0 {
981+
out_of_bounds(offset)
982+
}
983+
}
984+
960985
impl<T: CChar> Ptr<T> {
961986
pub fn with_slice_mut<R>(&self, len: usize, f: impl FnOnce(&mut [T]) -> R) -> R {
962987
let off = self.offset;
@@ -1537,6 +1562,13 @@ mod tests {
15371562
p.decay().delete();
15381563
}
15391564

1565+
#[test]
1566+
#[should_panic(expected = "ub: access at offset 1 of a single object")]
1567+
fn write_past_single_panics() {
1568+
let v: Value<i32> = Rc::new(RefCell::new(1));
1569+
v.as_pointer().offset(1).write(2);
1570+
}
1571+
15401572
#[test]
15411573
#[should_panic(expected = "ub: dangling pointer")]
15421574
fn read_dangling_panics() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ pub fn main() {
1818
std::process::exit(main_0());
1919
}
2020
fn main_0() -> i32 {
21-
assert!((((({ a_value_1() }) == 7) as i32) != 0));
22-
assert!((((({ b_value_2() }) == 9) as i32) != 0));
21+
assert!((({ a_value_1() }) == 7));
22+
assert!((({ b_value_2() }) == 9));
2323
return 0;
2424
}
2525
pub type anon_3 = u32;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pub fn main() {
2020
}
2121
}
2222
unsafe fn main_0() -> i32 {
23-
assert!(((((unsafe { a_value_1() }) == (7)) as i32) != 0));
24-
assert!(((((unsafe { b_value_2() }) == (9)) as i32) != 0));
23+
assert!(((unsafe { a_value_1() }) == (7)));
24+
assert!(((unsafe { b_value_2() }) == (9)));
2525
return 0;
2626
}
2727
pub type anon_3 = u32;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ pub fn main() {
2222
std::process::exit(main_0());
2323
}
2424
fn main_0() -> i32 {
25-
assert!((((({ a_value_0() }) == 11) as i32) != 0));
26-
assert!((((({ b_value_1() }) == 2) as i32) != 0));
25+
assert!((({ a_value_0() }) == 11));
26+
assert!((({ b_value_1() }) == 2));
2727
return 0;
2828
}
2929
pub type widget_enum = u32;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ pub fn main() {
2323
}
2424
}
2525
unsafe fn main_0() -> i32 {
26-
assert!(((((unsafe { a_value_0() }) == (11)) as i32) != 0));
27-
assert!(((((unsafe { b_value_1() }) == (2)) as i32) != 0));
26+
assert!(((unsafe { a_value_0() }) == (11)));
27+
assert!(((unsafe { b_value_1() }) == (2)));
2828
return 0;
2929
}
3030
pub type widget_enum = u32;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn main() {
1111
std::process::exit(main_0());
1212
}
1313
fn main_0() -> i32 {
14-
assert!((((({ helper_0(42,) }) == 43) as i32) != 0));
14+
assert!((({ helper_0(42,) }) == 43));
1515
return 0;
1616
}
1717
pub fn unrelated1_1() -> i32 {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn main() {
1313
}
1414
}
1515
unsafe fn main_0() -> i32 {
16-
assert!(((((unsafe { helper_0(42,) }) == (43)) as i32) != 0));
16+
assert!(((unsafe { helper_0(42,) }) == (43)));
1717
return 0;
1818
}
1919
pub unsafe fn unrelated1_1() -> i32 {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn main() {
1111
std::process::exit(main_0());
1212
}
1313
fn main_0() -> i32 {
14-
assert!((((({ helper_0(42,) }) == 43) as i32) != 0));
14+
assert!((({ helper_0(42,) }) == 43));
1515
return 0;
1616
}
1717
pub fn unrelated1_1() -> i32 {

0 commit comments

Comments
 (0)