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
5 changes: 5 additions & 0 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,11 @@ void ConverterRefCount::ConvertDeclRefValue(clang::Expr *expr,

if (ShouldReplaceWithMappedBody(decl)) {
if (auto str = GetMappedAsString(expr); !str.empty()) {
if (RuleRegistry::ReturnsReference(expr) && !isAddrOf() &&
!Mapper::IsRefExpr(ctx_, expr)) {
EmitPointee(str, expr->getType(), /*fresh=*/true);
return;
}
EmitFresh(str);
return;
}
Expand Down
34 changes: 33 additions & 1 deletion rules/compare/shim.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

use crate::{ArgRepr, ByteRepr, FnPtrArg, record_from_repr};
use std::cell::RefCell;
use std::cmp::Ordering;
use std::rc::Rc;

use crate::{ArgRepr, AsPointer, ByteRepr, FnPtrArg, Ptr, Value, record_from_repr};

impl ByteRepr for std::cmp::Ordering {}

Expand All @@ -14,3 +18,31 @@ impl FnPtrArg for std::cmp::Ordering {
record_from_repr(r)
}
}

pub static STRONG_ORDERING_LESS: Ordering = Ordering::Less;
pub static STRONG_ORDERING_EQUAL: Ordering = Ordering::Equal;
pub static STRONG_ORDERING_EQUIVALENT: Ordering = Ordering::Equal;
pub static STRONG_ORDERING_GREATER: Ordering = Ordering::Greater;

thread_local! {
static STRONG_ORDERING_LESS_VALUE: Value<Ordering> = Rc::new(RefCell::new(Ordering::Less));
static STRONG_ORDERING_EQUAL_VALUE: Value<Ordering> = Rc::new(RefCell::new(Ordering::Equal));
static STRONG_ORDERING_EQUIVALENT_VALUE: Value<Ordering> = Rc::new(RefCell::new(Ordering::Equal));
static STRONG_ORDERING_GREATER_VALUE: Value<Ordering> = Rc::new(RefCell::new(Ordering::Greater));
}

pub fn strong_ordering_less() -> Ptr<Ordering> {
STRONG_ORDERING_LESS_VALUE.with(AsPointer::as_pointer)
}

pub fn strong_ordering_equal() -> Ptr<Ordering> {
STRONG_ORDERING_EQUAL_VALUE.with(AsPointer::as_pointer)
}

pub fn strong_ordering_equivalent() -> Ptr<Ordering> {
STRONG_ORDERING_EQUIVALENT_VALUE.with(AsPointer::as_pointer)
}

pub fn strong_ordering_greater() -> Ptr<Ordering> {
STRONG_ORDERING_GREATER_VALUE.with(AsPointer::as_pointer)
}
8 changes: 4 additions & 4 deletions rules/compare/src_cxx20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

typedef std::strong_ordering t1;

std::strong_ordering f1() { return std::strong_ordering::less; }
const std::strong_ordering &f1() { return std::strong_ordering::less; }

std::strong_ordering f2() { return std::strong_ordering::equal; }
const std::strong_ordering &f2() { return std::strong_ordering::equal; }

std::strong_ordering f3() { return std::strong_ordering::equivalent; }
const std::strong_ordering &f3() { return std::strong_ordering::equivalent; }

std::strong_ordering f4() { return std::strong_ordering::greater; }
const std::strong_ordering &f4() { return std::strong_ordering::greater; }

bool f5(std::strong_ordering a0, std::strong_ordering a1) { return a0 == a1; }

Expand Down
20 changes: 20 additions & 0 deletions rules/compare/tgt_refcount.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

use libcc2rs::*;

fn f1() -> Ptr<std::cmp::Ordering> {
libcc2rs::strong_ordering_less()
}

fn f2() -> Ptr<std::cmp::Ordering> {
libcc2rs::strong_ordering_equal()
}

fn f3() -> Ptr<std::cmp::Ordering> {
libcc2rs::strong_ordering_equivalent()
}

fn f4() -> Ptr<std::cmp::Ordering> {
libcc2rs::strong_ordering_greater()
}
16 changes: 8 additions & 8 deletions rules/compare/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ fn t1() -> std::cmp::Ordering {
std::cmp::Ordering::Equal
}

fn f1() -> std::cmp::Ordering {
std::cmp::Ordering::Less
unsafe fn f1() -> *const std::cmp::Ordering {
&raw const libcc2rs::STRONG_ORDERING_LESS
}

fn f2() -> std::cmp::Ordering {
std::cmp::Ordering::Equal
unsafe fn f2() -> *const std::cmp::Ordering {
&raw const libcc2rs::STRONG_ORDERING_EQUAL
}

fn f3() -> std::cmp::Ordering {
std::cmp::Ordering::Equal
unsafe fn f3() -> *const std::cmp::Ordering {
&raw const libcc2rs::STRONG_ORDERING_EQUIVALENT
}

fn f4() -> std::cmp::Ordering {
std::cmp::Ordering::Greater
unsafe fn f4() -> *const std::cmp::Ordering {
&raw const libcc2rs::STRONG_ORDERING_GREATER
}

fn f5(a0: std::cmp::Ordering, a1: std::cmp::Ordering) -> bool {
Expand Down
26 changes: 26 additions & 0 deletions tests/std/support/cmp/cmp.strongord.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// ADDITIONAL_COMPILE_FLAGS: -std=c++20
#include <cassert>
#include <compare>

// N4950 [cmp.strongord]: "static const strong_ordering less;
// static const strong_ordering equal; static const strong_ordering equivalent;
// static const strong_ordering greater;" A reference bound to a valid value
// refers to that static data member.
namespace valid_values {
void test() {
const std::strong_ordering &l = std::strong_ordering::less;
const std::strong_ordering &e = std::strong_ordering::equal;
const std::strong_ordering &g = std::strong_ordering::greater;
assert(&l == &std::strong_ordering::less);
assert(&e == &std::strong_ordering::equal);
assert(&g == &std::strong_ordering::greater);
assert(l < 0);
assert(e == 0);
assert(g > 0);
}
} // namespace valid_values

int main() {
valid_values::test();
return 0;
}
28 changes: 28 additions & 0 deletions tests/std/support/cmp/out/refcount/cmp.strongord.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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 test_0() {
let l: Ptr<std::cmp::Ordering> = libcc2rs::strong_ordering_less();
let e: Ptr<std::cmp::Ordering> = libcc2rs::strong_ordering_equal();
let g: Ptr<std::cmp::Ordering> = libcc2rs::strong_ordering_greater();
assert!(({ (l).clone() } == { (libcc2rs::strong_ordering_less()) }));
assert!(({ (e).clone() } == { (libcc2rs::strong_ordering_equal()) }));
assert!(({ (g).clone() } == { (libcc2rs::strong_ordering_greater()) }));
assert!(({ (*l.upgrade().deref()) }) == std::cmp::Ordering::Less);
assert!(({ (*e.upgrade().deref()) }) == std::cmp::Ordering::Equal);
assert!(({ (*g.upgrade().deref()) }) == std::cmp::Ordering::Greater);
}
pub fn main() {
__cpp2rust_init_globals();
libcc2rs::cpp2rust_exit(main_0());
}
fn main_0() -> i32 {
({ test_0() });
return 0;
}
pub fn __cpp2rust_init_globals() {}
30 changes: 30 additions & 0 deletions tests/std/support/cmp/out/unsafe/cmp.strongord.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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 test_0() {
let l: *const std::cmp::Ordering = &libcc2rs::STRONG_ORDERING_LESS;
let e: *const std::cmp::Ordering = &libcc2rs::STRONG_ORDERING_EQUAL;
let g: *const std::cmp::Ordering = &libcc2rs::STRONG_ORDERING_GREATER;
assert!(((l) == (&raw const libcc2rs::STRONG_ORDERING_LESS as *const std::cmp::Ordering)));
assert!(((e) == (&raw const libcc2rs::STRONG_ORDERING_EQUAL as *const std::cmp::Ordering)));
assert!(((g) == (&raw const libcc2rs::STRONG_ORDERING_GREATER as *const std::cmp::Ordering)));
assert!((*l) == std::cmp::Ordering::Less);
assert!((*e) == std::cmp::Ordering::Equal);
assert!((*g) == std::cmp::Ordering::Greater);
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
libcc2rs::cpp2rust_exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
(unsafe { test_0() });
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
5 changes: 4 additions & 1 deletion tests/unit/out/refcount/fn_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ fn main_0() -> i32 {
assert!(!({ of.call(std::cmp::Ord::cmp(&(2), &(1)),) }));
let o: Value<std::cmp::Ordering> = Rc::new(RefCell::new(std::cmp::Ord::cmp(&(1), &(2))));
let mut op: Ptr<std::cmp::Ordering> = (o.as_pointer());
assert!(({ (*op.upgrade().deref()) }) == std::cmp::Ordering::Less);
assert!(
({ (*op.upgrade().deref()) })
== ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) })
);
return 0;
}
pub fn __cpp2rust_init_globals() {
Expand Down
23 changes: 12 additions & 11 deletions tests/unit/out/refcount/operator_comparison_defaulted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ fn main_0() -> i32 {
})
);
assert!(
({ CmpImpl::operator_cmp(&c1.as_pointer(), c2.as_pointer(),) }) == std::cmp::Ordering::Less
({ CmpImpl::operator_cmp(&c1.as_pointer(), c2.as_pointer(),) })
== ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) })
);
let b1: Value<Both> = Rc::new(RefCell::new(Both { a: 1 }));
let b2: Value<Both> = Rc::new(RefCell::new(Both { a: 2 }));
Expand All @@ -187,7 +188,7 @@ fn main_0() -> i32 {
);
assert!(
({ OrdOnlyImpl::operator_cmp(&o2.as_pointer(), o1.as_pointer(),) })
== std::cmp::Ordering::Greater
== ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) })
);
let x1: Value<Outer> = Rc::new(RefCell::new(Outer {
i: Inner { x: 1 },
Expand All @@ -208,7 +209,7 @@ fn main_0() -> i32 {
assert!(({ OuterImpl::operator_eq(&x1.as_pointer(), x3.as_pointer(),) }));
assert!(
({ OuterImpl::operator_cmp(&x2.as_pointer(), x1.as_pointer(),) })
== std::cmp::Ordering::Greater
== ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) })
);
let s1: Value<Secondary> = Rc::new(RefCell::new(Secondary { a: 1 }));
let s2: Value<Secondary> = Rc::new(RefCell::new(Secondary { a: 2 }));
Expand All @@ -229,7 +230,7 @@ fn main_0() -> i32 {
assert!(({ PtrMemberImpl::operator_eq(&p1.as_pointer(), p3.as_pointer(),) }));
assert!(
({ PtrMemberImpl::operator_cmp(&p2.as_pointer(), p1.as_pointer(),) })
== std::cmp::Ordering::Greater
== ({ (*libcc2rs::strong_ordering_greater().upgrade().deref()) })
);
return 0;
}
Expand All @@ -251,7 +252,7 @@ impl BothImpl for Ptr<Both> {
return (*cmp.borrow_mut());
}
}
return std::cmp::Ordering::Equal;
return (*libcc2rs::strong_ordering_equal().upgrade().deref());
}
}
pub trait CmpImpl {
Expand All @@ -278,7 +279,7 @@ impl CmpImpl for Ptr<Cmp> {
return (*cmp.borrow_mut());
}
}
return std::cmp::Ordering::Equal;
return (*libcc2rs::strong_ordering_equal().upgrade().deref());
}
fn operator_eq(&self, _a0: Ptr<Cmp>) -> bool {
return ({ (*self).with(|__s| __s.a) } == { _a0.with(|__s| __s.a) })
Expand Down Expand Up @@ -309,7 +310,7 @@ impl InnerImpl for Ptr<Inner> {
return (*cmp.borrow_mut());
}
}
return std::cmp::Ordering::Equal;
return (*libcc2rs::strong_ordering_equal().upgrade().deref());
}
fn operator_eq(&self, _a0: Ptr<Inner>) -> bool {
return ({ (*self).with(|__s| __s.x) } == { _a0.with(|__s| __s.x) });
Expand All @@ -329,7 +330,7 @@ impl OrdOnlyImpl for Ptr<OrdOnly> {
return (*cmp.borrow_mut());
}
}
return std::cmp::Ordering::Equal;
return (*libcc2rs::strong_ordering_equal().upgrade().deref());
}
}
pub trait OuterImpl {
Expand Down Expand Up @@ -358,7 +359,7 @@ impl OuterImpl for Ptr<Outer> {
return (*cmp.borrow_mut());
}
}
return std::cmp::Ordering::Equal;
return (*libcc2rs::strong_ordering_equal().upgrade().deref());
}
fn operator_eq(&self, _a0: Ptr<Outer>) -> bool {
return ({
Expand All @@ -382,7 +383,7 @@ impl PtrMemberImpl for Ptr<PtrMember> {
return (*cmp.borrow_mut());
}
}
return std::cmp::Ordering::Equal;
return (*libcc2rs::strong_ordering_equal().upgrade().deref());
}
fn operator_eq(&self, _a0: Ptr<PtrMember>) -> bool {
return ({ (*self).with(|__s| __s.p.clone()) } == { _a0.with(|__s| __s.p.clone()) });
Expand Down Expand Up @@ -415,7 +416,7 @@ impl SecondaryImpl for Ptr<Secondary> {
return (*cmp.borrow_mut());
}
}
return std::cmp::Ordering::Equal;
return (*libcc2rs::strong_ordering_equal().upgrade().deref());
}
fn operator_lt(&self, _a0: Ptr<Secondary>) -> bool {
return ({
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/out/refcount/operator_friend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn operator_cmp_1(_a0: Ptr<DefaultedOrd>, _a1: Ptr<DefaultedOrd>) -> std::cm
return (*cmp.borrow_mut());
}
}
return std::cmp::Ordering::Equal;
return (*libcc2rs::strong_ordering_equal().upgrade().deref());
}
pub fn operator_eq_2(_a0: Ptr<DefaultedOrd>, _a1: Ptr<DefaultedOrd>) -> bool {
return ({ _a0.with(|__s| __s.a) } == { _a1.with(|__s| __s.a) });
Expand Down Expand Up @@ -198,7 +198,7 @@ fn main_0() -> i32 {
({
let _arg0: Ptr<DefaultedOrd> = o1.as_pointer();
operator_cmp_1(_arg0, o2.as_pointer())
}) == std::cmp::Ordering::Less
}) == ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) })
);
let i1: Value<Inline> = Rc::new(RefCell::new(Inline { a: 1 }));
let i2: Value<Inline> = Rc::new(RefCell::new(Inline { a: 2 }));
Expand Down
9 changes: 5 additions & 4 deletions tests/unit/out/refcount/operator_three_way.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ fn main_0() -> i32 {
);
assert!(!({ SImpl::operator_eq(&a.as_pointer(), b.as_pointer(),) }));
assert!(
({ SImpl::operator_cmp(&a.as_pointer(), b.as_pointer(),) }) == std::cmp::Ordering::Less
({ SImpl::operator_cmp(&a.as_pointer(), b.as_pointer(),) })
== ({ (*libcc2rs::strong_ordering_less().upgrade().deref()) })
);
return 0;
}
Expand All @@ -54,12 +55,12 @@ pub trait SImpl {
impl SImpl for Ptr<S> {
fn operator_cmp(&self, o: Ptr<S>) -> std::cmp::Ordering {
if ({ (*self).with(|__s| __s.v) } < { o.with(|__s| __s.v) }) {
return std::cmp::Ordering::Less;
return (*libcc2rs::strong_ordering_less().upgrade().deref());
}
if ({ (*self).with(|__s| __s.v) } > { o.with(|__s| __s.v) }) {
return std::cmp::Ordering::Greater;
return (*libcc2rs::strong_ordering_greater().upgrade().deref());
}
return std::cmp::Ordering::Equal;
return (*libcc2rs::strong_ordering_equal().upgrade().deref());
}
fn operator_eq(&self, o: Ptr<S>) -> bool {
return ({ (*self).with(|__s| __s.v) } == { o.with(|__s| __s.v) });
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/fn_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ unsafe fn main_0() -> i32 {
assert!(!(unsafe { (of).unwrap()(std::cmp::Ord::cmp(&(2), &(1)),) }));
let mut o: std::cmp::Ordering = std::cmp::Ord::cmp(&(1), &(2));
let mut op: *mut std::cmp::Ordering = (&mut o as *mut std::cmp::Ordering);
assert!((*op) == std::cmp::Ordering::Less);
assert!((*op) == libcc2rs::STRONG_ORDERING_LESS);
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {
Expand Down
Loading
Loading