Skip to content

Commit 9d3e6a8

Browse files
authored
Add tests for basic.def.odr (#620)
Part of #619 There are some cpp2rust bugs that I found while writing the tests: * `p3.5-pointer-to-member.cpp` - we don't support pointer to member yet * `p4.1-pure-virtual-qualified-call.cpp` - unsupported inheritance * `p4.1-pure-virtual-member-pointer.cpp` - pointer to member * `p6-evaluated-binding-is-captured.cpp` - structured binding not captured * `p14.5.1-inline-function-reads-per-file-constant` - converter generates one global, two globals must be generated * `p15-function-template` - static local is never initialised I added tests only for behavioural differences between C++ and Rust. I ignored things that have to do with: ill formed programs, diagnostic required, clasue that only defines terms, etc. I also caught a limitation in Clang in `p5.3-discarded-value-in-lambda.cpp`, Clang captures a variable that it should not.
1 parent da8b7be commit 9d3e6a8

137 files changed

Lines changed: 6606 additions & 1 deletion

File tree

Some content is hidden

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

‎CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ add_custom_target("check-unit"
152152
"${PROJECT_SOURCE_DIR}/tests/lit/lit.py"
153153
${LIT_FLAGS}
154154
"${PROJECT_SOURCE_DIR}/tests/unit"
155+
"${PROJECT_SOURCE_DIR}/tests/std"
155156
"${PROJECT_SOURCE_DIR}/tests/ub"
156157
"${PROJECT_SOURCE_DIR}/tests/multi-file"
157158
DEPENDS cpp2rust libcc2rs

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,8 @@ static const clang::FieldDecl *
15071507
AsLambdaCaptureField(const clang::FunctionDecl *fn, const clang::Expr *expr) {
15081508
auto call = AsLambdaOperatorCall(fn);
15091509
auto ref = clang::dyn_cast<clang::DeclRefExpr>(expr);
1510-
if (!call || !ref || !ref->refersToEnclosingVariableOrCapture()) {
1510+
if (!call || !ref || !ref->refersToEnclosingVariableOrCapture() ||
1511+
ref->isNonOdrUse() != clang::NOUR_None) {
15111512
return nullptr;
15121513
}
15131514
llvm::DenseMap<const clang::ValueDecl *, clang::FieldDecl *> fields;

‎tests/lit/lit/formats/Cpp2RustTest.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@ def getTestsInDirectory(self, testSuite, path_in_suite, litConfig, localConfig):
373373
yield lit.Test.Test(testSuite, path_in_suite, localConfig)
374374
return
375375
for entry in source_path.iterdir():
376+
if entry.name in localConfig.excludes:
377+
continue
376378
if entry.is_file() and entry.suffix in (".cpp", ".c"):
377379
yield lit.Test.Test(
378380
testSuite, path_in_suite + (entry.name,), localConfig
@@ -485,6 +487,7 @@ def cpp2rust_command(cc_input, build_dir, model, rs_file, extra_cxxflags):
485487
model,
486488
"-o",
487489
str(rs_file),
490+
f"-cxxflags=-I{cc_input.parent}",
488491
]
489492
for flag in extra_cxxflags:
490493
cmd.append(f"-cxxflags={flag}")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Clang treats a discarded-value use as an odr-use, so [=] captures the variable
2+
# and the C++ program itself fails its asserts:
3+
# https://github.com/llvm/llvm-project/issues/127086
4+
config.excludes = list(config.excludes) + ["p5.3-discarded-value-in-lambda.cpp"]
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
thread_local!(
10+
pub static ctorCount_0: Value<i32> = Rc::new(RefCell::new(0));
11+
);
12+
thread_local!(
13+
pub static copyCount_1: Value<i32> = Rc::new(RefCell::new(0));
14+
);
15+
thread_local!(
16+
pub static dtorCount_2: Value<i32> = Rc::new(RefCell::new(0));
17+
);
18+
thread_local!(
19+
pub static useCount_3: Value<i32> = Rc::new(RefCell::new(0));
20+
);
21+
#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)]
22+
#[byte_size(4)]
23+
pub struct LifetimeTracked {
24+
#[offset(0)]
25+
pub uses: i32,
26+
}
27+
impl LifetimeTracked {
28+
pub fn new() -> Self {
29+
let __this: LifetimeTracked = Self { uses: 0 };
30+
(*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc();
31+
__this
32+
}
33+
pub fn copy_from(_a0: Ptr<LifetimeTracked>) -> Self {
34+
let __this: LifetimeTracked = Self { uses: 0 };
35+
(*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc();
36+
__this
37+
}
38+
}
39+
impl Clone for LifetimeTracked {
40+
fn clone(&self) -> Self {
41+
let __src: Value<LifetimeTracked> = Rc::new(RefCell::new(LifetimeTracked {
42+
uses: self.uses.clone(),
43+
}));
44+
LifetimeTracked::copy_from(__src.as_pointer())
45+
}
46+
}
47+
impl Default for LifetimeTracked {
48+
fn default() -> Self {
49+
{ LifetimeTracked::new() }
50+
}
51+
}
52+
thread_local!(
53+
pub static else_runs_4: Value<i32> = Rc::new(RefCell::new(0_i32));
54+
);
55+
pub fn g_5(mut _a0: i32) {
56+
{
57+
(*else_runs_4.with(Value::clone).borrow_mut()).prefix_inc();
58+
}
59+
}
60+
pub fn g_6(mut _a0: i8) {
61+
{
62+
(*else_runs_4.with(Value::clone).borrow_mut()).prefix_inc();
63+
}
64+
}
65+
pub fn main() {
66+
__cpp2rust_init_globals();
67+
std::process::exit(main_0());
68+
}
69+
fn main_0() -> i32 {
70+
({ g_5(1) });
71+
({ g_6(('c' as i8)) });
72+
assert!((else_runs_4.with(|rc| *rc.borrow()) == 2));
73+
assert!((ctorCount_0.with(|rc| *rc.borrow()) == 0));
74+
assert!((useCount_3.with(|rc| *rc.borrow()) == 0));
75+
return 0;
76+
}
77+
pub trait LifetimeTrackedImpl {
78+
fn destructor(&self);
79+
fn use_(&self);
80+
fn copy_assign(&self, _a0: Ptr<LifetimeTracked>) -> Ptr<LifetimeTracked>;
81+
}
82+
impl LifetimeTrackedImpl for Ptr<LifetimeTracked> {
83+
fn destructor(&self) {
84+
(*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc();
85+
}
86+
fn use_(&self) {
87+
field!((*self), uses).with_mut(|__v| __v.prefix_inc());
88+
(*useCount_3.with(Value::clone).borrow_mut()).prefix_inc();
89+
}
90+
fn copy_assign(&self, _a0: Ptr<LifetimeTracked>) -> Ptr<LifetimeTracked> {
91+
field!((*self), uses).write({ _a0.with(|__s| __s.uses) });
92+
return (*self).clone();
93+
}
94+
}
95+
pub fn __cpp2rust_init_globals() {
96+
let _ = ctorCount_0.with(|_| ());
97+
let _ = copyCount_1.with(|_| ());
98+
let _ = dtorCount_2.with(|_| ());
99+
let _ = useCount_3.with(|_| ());
100+
let _ = else_runs_4.with(|_| ());
101+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
thread_local!(
10+
pub static ctorCount_0: Value<i32> = Rc::new(RefCell::new(0));
11+
);
12+
thread_local!(
13+
pub static copyCount_1: Value<i32> = Rc::new(RefCell::new(0));
14+
);
15+
thread_local!(
16+
pub static dtorCount_2: Value<i32> = Rc::new(RefCell::new(0));
17+
);
18+
thread_local!(
19+
pub static useCount_3: Value<i32> = Rc::new(RefCell::new(0));
20+
);
21+
#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)]
22+
#[byte_size(4)]
23+
pub struct LifetimeTracked {
24+
#[offset(0)]
25+
pub uses: i32,
26+
}
27+
impl LifetimeTracked {
28+
pub fn new() -> Self {
29+
let __this: LifetimeTracked = Self { uses: 0 };
30+
(*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc();
31+
__this
32+
}
33+
pub fn copy_from(_a0: Ptr<LifetimeTracked>) -> Self {
34+
let __this: LifetimeTracked = Self { uses: 0 };
35+
(*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc();
36+
__this
37+
}
38+
}
39+
impl Clone for LifetimeTracked {
40+
fn clone(&self) -> Self {
41+
let __src: Value<LifetimeTracked> = Rc::new(RefCell::new(LifetimeTracked {
42+
uses: self.uses.clone(),
43+
}));
44+
LifetimeTracked::copy_from(__src.as_pointer())
45+
}
46+
}
47+
impl Default for LifetimeTracked {
48+
fn default() -> Self {
49+
{ LifetimeTracked::new() }
50+
}
51+
}
52+
thread_local!(
53+
pub static else_runs_4: Value<i32> = Rc::new(RefCell::new(0_i32));
54+
);
55+
pub fn g_5() {
56+
{
57+
(*else_runs_4.with(Value::clone).borrow_mut()).prefix_inc();
58+
}
59+
}
60+
pub fn main() {
61+
__cpp2rust_init_globals();
62+
std::process::exit(main_0());
63+
}
64+
fn main_0() -> i32 {
65+
({ g_5() });
66+
assert!((else_runs_4.with(|rc| *rc.borrow()) == 1));
67+
assert!((ctorCount_0.with(|rc| *rc.borrow()) == 0));
68+
assert!((useCount_3.with(|rc| *rc.borrow()) == 0));
69+
return 0;
70+
}
71+
pub trait LifetimeTrackedImpl {
72+
fn destructor(&self);
73+
fn use_(&self);
74+
fn copy_assign(&self, _a0: Ptr<LifetimeTracked>) -> Ptr<LifetimeTracked>;
75+
}
76+
impl LifetimeTrackedImpl for Ptr<LifetimeTracked> {
77+
fn destructor(&self) {
78+
(*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc();
79+
}
80+
fn use_(&self) {
81+
field!((*self), uses).with_mut(|__v| __v.prefix_inc());
82+
(*useCount_3.with(Value::clone).borrow_mut()).prefix_inc();
83+
}
84+
fn copy_assign(&self, _a0: Ptr<LifetimeTracked>) -> Ptr<LifetimeTracked> {
85+
field!((*self), uses).write({ _a0.with(|__s| __s.uses) });
86+
return (*self).clone();
87+
}
88+
}
89+
pub fn __cpp2rust_init_globals() {
90+
let _ = ctorCount_0.with(|_| ());
91+
let _ = copyCount_1.with(|_| ());
92+
let _ = dtorCount_2.with(|_| ());
93+
let _ = useCount_3.with(|_| ());
94+
let _ = else_runs_4.with(|_| ());
95+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
thread_local!(
10+
pub static ctorCount_0: Value<i32> = Rc::new(RefCell::new(0));
11+
);
12+
thread_local!(
13+
pub static copyCount_1: Value<i32> = Rc::new(RefCell::new(0));
14+
);
15+
thread_local!(
16+
pub static dtorCount_2: Value<i32> = Rc::new(RefCell::new(0));
17+
);
18+
thread_local!(
19+
pub static useCount_3: Value<i32> = Rc::new(RefCell::new(0));
20+
);
21+
#[derive(Record, ByteRepr, VaArg, FnPtrArg, CopyAssign, Destructor)]
22+
#[byte_size(4)]
23+
pub struct LifetimeTracked {
24+
#[offset(0)]
25+
pub uses: i32,
26+
}
27+
impl LifetimeTracked {
28+
pub fn new() -> Self {
29+
let __this: LifetimeTracked = Self { uses: 0 };
30+
(*ctorCount_0.with(Value::clone).borrow_mut()).prefix_inc();
31+
__this
32+
}
33+
pub fn copy_from(_a0: Ptr<LifetimeTracked>) -> Self {
34+
let __this: LifetimeTracked = Self { uses: 0 };
35+
(*copyCount_1.with(Value::clone).borrow_mut()).prefix_inc();
36+
__this
37+
}
38+
}
39+
impl Clone for LifetimeTracked {
40+
fn clone(&self) -> Self {
41+
let __src: Value<LifetimeTracked> = Rc::new(RefCell::new(LifetimeTracked {
42+
uses: self.uses.clone(),
43+
}));
44+
LifetimeTracked::copy_from(__src.as_pointer())
45+
}
46+
}
47+
impl Default for LifetimeTracked {
48+
fn default() -> Self {
49+
{ LifetimeTracked::new() }
50+
}
51+
}
52+
thread_local!(
53+
pub static else_runs_4: Value<i32> = Rc::new(RefCell::new(0_i32));
54+
);
55+
pub fn g_6() {
56+
{
57+
(*else_runs_4.with(Value::clone).borrow_mut()).prefix_inc();
58+
}
59+
}
60+
pub fn main() {
61+
__cpp2rust_init_globals();
62+
std::process::exit(main_0());
63+
}
64+
fn main_0() -> i32 {
65+
({ g_6() });
66+
assert!((else_runs_4.with(|rc| *rc.borrow()) == 1));
67+
assert!((ctorCount_0.with(|rc| *rc.borrow()) == 0));
68+
assert!((useCount_3.with(|rc| *rc.borrow()) == 0));
69+
return 0;
70+
}
71+
pub trait LifetimeTrackedImpl {
72+
fn destructor(&self);
73+
fn use_(&self);
74+
fn copy_assign(&self, _a0: Ptr<LifetimeTracked>) -> Ptr<LifetimeTracked>;
75+
}
76+
impl LifetimeTrackedImpl for Ptr<LifetimeTracked> {
77+
fn destructor(&self) {
78+
(*dtorCount_2.with(Value::clone).borrow_mut()).prefix_inc();
79+
}
80+
fn use_(&self) {
81+
field!((*self), uses).with_mut(|__v| __v.prefix_inc());
82+
(*useCount_3.with(Value::clone).borrow_mut()).prefix_inc();
83+
}
84+
fn copy_assign(&self, _a0: Ptr<LifetimeTracked>) -> Ptr<LifetimeTracked> {
85+
field!((*self), uses).write({ _a0.with(|__s| __s.uses) });
86+
return (*self).clone();
87+
}
88+
}
89+
pub fn __cpp2rust_init_globals() {
90+
let _ = ctorCount_0.with(|_| ());
91+
let _ = copyCount_1.with(|_| ());
92+
let _ = dtorCount_2.with(|_| ());
93+
let _ = useCount_3.with(|_| ());
94+
let _ = else_runs_4.with(|_| ());
95+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
thread_local!(
10+
pub static g_calls_0: Value<i32> = Rc::new(RefCell::new(0_i32));
11+
);
12+
pub fn g_1() {
13+
{}
14+
(*g_calls_0.with(Value::clone).borrow_mut()).prefix_inc();
15+
}
16+
pub fn main() {
17+
__cpp2rust_init_globals();
18+
std::process::exit(main_0());
19+
}
20+
fn main_0() -> i32 {
21+
({ g_1() });
22+
assert!((g_calls_0.with(|rc| *rc.borrow()) == 1));
23+
return 0;
24+
}
25+
pub fn __cpp2rust_init_globals() {
26+
let _ = g_calls_0.with(|_| ());
27+
}

0 commit comments

Comments
 (0)