Skip to content

Commit cd37b02

Browse files
authored
Translate unchecked derived to base (#643)
Needed to make std::ofstream usable.
1 parent 1a610f9 commit cd37b02

4 files changed

Lines changed: 89 additions & 0 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2720,6 +2720,7 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
27202720
}
27212721
case clang::CastKind::CK_ConstructorConversion:
27222722
case clang::CastKind::CK_DerivedToBase:
2723+
case clang::CastKind::CK_UncheckedDerivedToBase:
27232724
Convert(sub_expr);
27242725
break;
27252726
case clang::CastKind::CK_IntegralToBoolean:

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,38 @@ fn main_0() -> i32 {
3838
__out.try_clone().unwrap()
3939
}
4040
};
41+
write!(ofs.as_pointer(), "{:}", 42,);
4142
}
43+
let mut f: Ptr<CFile> = match CFile::open(
44+
&file.decay().to_rust_string(),
45+
&Ptr::<i8>::from_string_literal(b"rb").to_rust_string(),
46+
) {
47+
Some(__f) => Ptr::alloc(__f),
48+
None => Ptr::null(),
49+
};
50+
let buf: Value<Box<[i8]>> = Rc::new(RefCell::new(Box::new([
51+
0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8,
52+
0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8, 0_i8,
53+
0_i8, 0_i8,
54+
])));
55+
let mut n: usize = libcc2rs::fread_refcount(
56+
(buf.decay()).to_any(),
57+
1_usize,
58+
::std::mem::size_of::<[i8; 32]>(),
59+
(f).clone(),
60+
);
61+
{
62+
let __r = f.with(|__f| __f.close());
63+
f.delete();
64+
__r
65+
};
66+
assert!((n == 16_usize));
67+
assert!(
68+
({
69+
(((*buf.borrow())[(14) as usize] as i32) == (('4' as i8) as i32))
70+
&& (((*buf.borrow())[(15) as usize] as i32) == (('2' as i8) as i32))
71+
})
72+
);
4273
match nix::unistd::unlink(file.decay().to_rust_string().as_str()) {
4374
Ok(()) => 0,
4475
Err(__e) => {

‎tests/unit/out/unsafe/stdcopy_ostream.rs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,55 @@ unsafe fn main_0() -> i32 {
4141
__out.try_clone().unwrap()
4242
}
4343
};
44+
write!(ofs, "{:}", 42,);
4445
}
46+
let mut f: *mut ::libc::FILE = libc::fopen(file.as_ptr(), c"rb".as_ptr());
47+
let mut buf: [libc::c_char; 32] = [
48+
(0 as libc::c_char),
49+
(0 as libc::c_char),
50+
(0 as libc::c_char),
51+
(0 as libc::c_char),
52+
(0 as libc::c_char),
53+
(0 as libc::c_char),
54+
(0 as libc::c_char),
55+
(0 as libc::c_char),
56+
(0 as libc::c_char),
57+
(0 as libc::c_char),
58+
(0 as libc::c_char),
59+
(0 as libc::c_char),
60+
(0 as libc::c_char),
61+
(0 as libc::c_char),
62+
(0 as libc::c_char),
63+
(0 as libc::c_char),
64+
(0 as libc::c_char),
65+
(0 as libc::c_char),
66+
(0 as libc::c_char),
67+
(0 as libc::c_char),
68+
(0 as libc::c_char),
69+
(0 as libc::c_char),
70+
(0 as libc::c_char),
71+
(0 as libc::c_char),
72+
(0 as libc::c_char),
73+
(0 as libc::c_char),
74+
(0 as libc::c_char),
75+
(0 as libc::c_char),
76+
(0 as libc::c_char),
77+
(0 as libc::c_char),
78+
(0 as libc::c_char),
79+
(0 as libc::c_char),
80+
];
81+
let mut n: usize = libcc2rs::fread_unsafe(
82+
(buf.as_mut_ptr() as *mut ::libc::c_void),
83+
1_usize,
84+
::std::mem::size_of::<[libc::c_char; 32]>(),
85+
f,
86+
);
87+
libc::fclose(f);
88+
assert!(((n) == (16_usize)));
89+
assert!(
90+
((buf[(14) as usize] as i32) == (('4' as libc::c_char) as i32))
91+
&& ((buf[(15) as usize] as i32) == (('2' as libc::c_char) as i32))
92+
);
4593
libc::unlink(file.as_ptr());
4694
return 0;
4795
}

‎tests/unit/stdcopy_ostream.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include <algorithm>
2+
#include <cassert>
3+
#include <cstdio>
24
#include <fstream>
35
#include <iterator>
46
#include <string>
@@ -10,7 +12,14 @@ int main() {
1012
{
1113
std::ofstream ofs(file, std::ios::binary);
1214
std::copy(str.begin(), str.end(), std::ostream_iterator<char>(ofs));
15+
ofs << 42;
1316
}
17+
FILE *f = fopen(file, "rb");
18+
char buf[32] = {0};
19+
size_t n = fread(buf, 1, sizeof(buf), f);
20+
fclose(f);
21+
assert(n == 16);
22+
assert(buf[14] == '4' && buf[15] == '2');
1423
unlink(file);
1524
return 0;
1625
}

0 commit comments

Comments
 (0)