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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ add_custom_target("check-unit"
"${PROJECT_SOURCE_DIR}/tests/lit/lit.py"
${LIT_FLAGS}
"${PROJECT_SOURCE_DIR}/tests/unit"
"${PROJECT_SOURCE_DIR}/tests/std"
"${PROJECT_SOURCE_DIR}/tests/ub"
"${PROJECT_SOURCE_DIR}/tests/multi-file"
DEPENDS cpp2rust libcc2rs
Expand Down
3 changes: 2 additions & 1 deletion cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,8 @@ static const clang::FieldDecl *
AsLambdaCaptureField(const clang::FunctionDecl *fn, const clang::Expr *expr) {
auto call = AsLambdaOperatorCall(fn);
auto ref = clang::dyn_cast<clang::DeclRefExpr>(expr);
if (!call || !ref || !ref->refersToEnclosingVariableOrCapture()) {
if (!call || !ref || !ref->refersToEnclosingVariableOrCapture() ||
ref->isNonOdrUse() != clang::NOUR_None) {
return nullptr;
}
llvm::DenseMap<const clang::ValueDecl *, clang::FieldDecl *> fields;
Expand Down
3 changes: 3 additions & 0 deletions tests/lit/lit/formats/Cpp2RustTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ def getTestsInDirectory(self, testSuite, path_in_suite, litConfig, localConfig):
yield lit.Test.Test(testSuite, path_in_suite, localConfig)
return
for entry in source_path.iterdir():
if entry.name in localConfig.excludes:
continue
if entry.is_file() and entry.suffix in (".cpp", ".c"):
yield lit.Test.Test(
testSuite, path_in_suite + (entry.name,), localConfig
Expand Down Expand Up @@ -485,6 +487,7 @@ def cpp2rust_command(cc_input, build_dir, model, rs_file, extra_cxxflags):
model,
"-o",
str(rs_file),
f"-cxxflags=-I{cc_input.parent}",
]
for flag in extra_cxxflags:
cmd.append(f"-cxxflags={flag}")
Expand Down
4 changes: 4 additions & 0 deletions tests/std/basic/basic.def.odr/lit.local.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Clang treats a discarded-value use as an odr-use, so [=] captures the variable
# and the C++ program itself fails its asserts:
# https://github.com/llvm/llvm-project/issues/127086
config.excludes = list(config.excludes) + ["p5.3-discarded-value-in-lambda.cpp"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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};
thread_local!(
pub static ctorCount_0: Value<i32> = Rc::new(RefCell::new(0));
);
thread_local!(
pub static copyCount_1: Value<i32> = Rc::new(RefCell::new(0));
);
thread_local!(
pub static dtorCount_2: Value<i32> = Rc::new(RefCell::new(0));
);
thread_local!(
pub static useCount_3: Value<i32> = Rc::new(RefCell::new(0));
);
#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)]
#[byte_size(4)]
pub struct LifetimeTracked {
#[offset(0)]
pub uses: i32,
}
impl LifetimeTracked {
pub fn new() -> Self {
let __this: LifetimeTracked = Self { uses: 0 };
(*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc();
__this
}
pub fn copy_from(_a0: Ptr<LifetimeTracked>) -> Self {
let __this: LifetimeTracked = Self { uses: 0 };
(*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc();
__this
}
}
impl Clone for LifetimeTracked {
fn clone(&self) -> Self {
let __src: Value<LifetimeTracked> = Rc::new(RefCell::new(LifetimeTracked {
uses: self.uses.clone(),
}));
LifetimeTracked::copy_from(__src.as_pointer())
}
}
impl Default for LifetimeTracked {
fn default() -> Self {
{ LifetimeTracked::new() }
}
}
thread_local!(
pub static else_runs_4: Value<i32> = Rc::new(RefCell::new(0_i32));
);
pub fn g_5(mut _a0: i32) {
{
(*else_runs_4.with(Value::clone).borrow_mut()).prefix_inc();
}
}
pub fn g_6(mut _a0: i8) {
{
(*else_runs_4.with(Value::clone).borrow_mut()).prefix_inc();
}
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
}
fn main_0() -> i32 {
({ g_5(1) });
({ g_6(('c' as i8)) });
assert!((else_runs_4.with(|rc| *rc.borrow()) == 2));
assert!((ctorCount_0.with(|rc| *rc.borrow()) == 0));
assert!((useCount_3.with(|rc| *rc.borrow()) == 0));
return 0;
}
pub trait LifetimeTrackedImpl {
fn destructor(&self);
fn use_(&self);
fn copy_assign(&self, _a0: Ptr<LifetimeTracked>) -> Ptr<LifetimeTracked>;
}
impl LifetimeTrackedImpl for Ptr<LifetimeTracked> {
fn destructor(&self) {
(*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc();
}
fn use_(&self) {
field!((*self), uses).with_mut(|__v| __v.prefix_inc());
(*useCount_3.with(Value::clone).borrow_mut()).prefix_inc();
}
fn copy_assign(&self, _a0: Ptr<LifetimeTracked>) -> Ptr<LifetimeTracked> {
field!((*self), uses).write({ _a0.with(|__s| __s.uses) });
return (*self).clone();
}
}
pub fn __cpp2rust_init_globals() {
let _ = ctorCount_0.with(|_| ());
let _ = copyCount_1.with(|_| ());
let _ = dtorCount_2.with(|_| ());
let _ = useCount_3.with(|_| ());
let _ = else_runs_4.with(|_| ());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
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};
thread_local!(
pub static ctorCount_0: Value<i32> = Rc::new(RefCell::new(0));
);
thread_local!(
pub static copyCount_1: Value<i32> = Rc::new(RefCell::new(0));
);
thread_local!(
pub static dtorCount_2: Value<i32> = Rc::new(RefCell::new(0));
);
thread_local!(
pub static useCount_3: Value<i32> = Rc::new(RefCell::new(0));
);
#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)]
#[byte_size(4)]
pub struct LifetimeTracked {
#[offset(0)]
pub uses: i32,
}
impl LifetimeTracked {
pub fn new() -> Self {
let __this: LifetimeTracked = Self { uses: 0 };
(*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc();
__this
}
pub fn copy_from(_a0: Ptr<LifetimeTracked>) -> Self {
let __this: LifetimeTracked = Self { uses: 0 };
(*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc();
__this
}
}
impl Clone for LifetimeTracked {
fn clone(&self) -> Self {
let __src: Value<LifetimeTracked> = Rc::new(RefCell::new(LifetimeTracked {
uses: self.uses.clone(),
}));
LifetimeTracked::copy_from(__src.as_pointer())
}
}
impl Default for LifetimeTracked {
fn default() -> Self {
{ LifetimeTracked::new() }
}
}
thread_local!(
pub static else_runs_4: Value<i32> = Rc::new(RefCell::new(0_i32));
);
pub fn g_5() {
{
(*else_runs_4.with(Value::clone).borrow_mut()).prefix_inc();
}
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
}
fn main_0() -> i32 {
({ g_5() });
assert!((else_runs_4.with(|rc| *rc.borrow()) == 1));
assert!((ctorCount_0.with(|rc| *rc.borrow()) == 0));
assert!((useCount_3.with(|rc| *rc.borrow()) == 0));
return 0;
}
pub trait LifetimeTrackedImpl {
fn destructor(&self);
fn use_(&self);
fn copy_assign(&self, _a0: Ptr<LifetimeTracked>) -> Ptr<LifetimeTracked>;
}
impl LifetimeTrackedImpl for Ptr<LifetimeTracked> {
fn destructor(&self) {
(*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc();
}
fn use_(&self) {
field!((*self), uses).with_mut(|__v| __v.prefix_inc());
(*useCount_3.with(Value::clone).borrow_mut()).prefix_inc();
}
fn copy_assign(&self, _a0: Ptr<LifetimeTracked>) -> Ptr<LifetimeTracked> {
field!((*self), uses).write({ _a0.with(|__s| __s.uses) });
return (*self).clone();
}
}
pub fn __cpp2rust_init_globals() {
let _ = ctorCount_0.with(|_| ());
let _ = copyCount_1.with(|_| ());
let _ = dtorCount_2.with(|_| ());
let _ = useCount_3.with(|_| ());
let _ = else_runs_4.with(|_| ());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
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};
thread_local!(
pub static ctorCount_0: Value<i32> = Rc::new(RefCell::new(0));
);
thread_local!(
pub static copyCount_1: Value<i32> = Rc::new(RefCell::new(0));
);
thread_local!(
pub static dtorCount_2: Value<i32> = Rc::new(RefCell::new(0));
);
thread_local!(
pub static useCount_3: Value<i32> = Rc::new(RefCell::new(0));
);
#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)]
#[byte_size(4)]
pub struct LifetimeTracked {
#[offset(0)]
pub uses: i32,
}
impl LifetimeTracked {
pub fn new() -> Self {
let __this: LifetimeTracked = Self { uses: 0 };
(*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc();
__this
}
pub fn copy_from(_a0: Ptr<LifetimeTracked>) -> Self {
let __this: LifetimeTracked = Self { uses: 0 };
(*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc();
__this
}
}
impl Clone for LifetimeTracked {
fn clone(&self) -> Self {
let __src: Value<LifetimeTracked> = Rc::new(RefCell::new(LifetimeTracked {
uses: self.uses.clone(),
}));
LifetimeTracked::copy_from(__src.as_pointer())
}
}
impl Default for LifetimeTracked {
fn default() -> Self {
{ LifetimeTracked::new() }
}
}
thread_local!(
pub static else_runs_4: Value<i32> = Rc::new(RefCell::new(0_i32));
);
pub fn g_6() {
{
(*else_runs_4.with(Value::clone).borrow_mut()).prefix_inc();
}
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
}
fn main_0() -> i32 {
({ g_6() });
assert!((else_runs_4.with(|rc| *rc.borrow()) == 1));
assert!((ctorCount_0.with(|rc| *rc.borrow()) == 0));
assert!((useCount_3.with(|rc| *rc.borrow()) == 0));
return 0;
}
pub trait LifetimeTrackedImpl {
fn destructor(&self);
fn use_(&self);
fn copy_assign(&self, _a0: Ptr<LifetimeTracked>) -> Ptr<LifetimeTracked>;
}
impl LifetimeTrackedImpl for Ptr<LifetimeTracked> {
fn destructor(&self) {
(*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc();
}
fn use_(&self) {
field!((*self), uses).with_mut(|__v| __v.prefix_inc());
(*useCount_3.with(Value::clone).borrow_mut()).prefix_inc();
}
fn copy_assign(&self, _a0: Ptr<LifetimeTracked>) -> Ptr<LifetimeTracked> {
field!((*self), uses).write({ _a0.with(|__s| __s.uses) });
return (*self).clone();
}
}
pub fn __cpp2rust_init_globals() {
let _ = ctorCount_0.with(|_| ());
let _ = copyCount_1.with(|_| ());
let _ = dtorCount_2.with(|_| ());
let _ = useCount_3.with(|_| ());
let _ = else_runs_4.with(|_| ());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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};
thread_local!(
pub static g_calls_0: Value<i32> = Rc::new(RefCell::new(0_i32));
);
pub fn g_1() {
{}
(*g_calls_0.with(Value::clone).borrow_mut()).prefix_inc();
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
}
fn main_0() -> i32 {
({ g_1() });
assert!((g_calls_0.with(|rc| *rc.borrow()) == 1));
return 0;
}
pub fn __cpp2rust_init_globals() {
let _ = g_calls_0.with(|_| ());
}
Loading
Loading