Skip to content

Commit a081b41

Browse files
authored
Fix default for 2D array (#242)
1 parent 8264141 commit a081b41

4 files changed

Lines changed: 125 additions & 1 deletion

File tree

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1897,7 +1897,9 @@ ConverterRefCount::GetArrayDefaultAsString(clang::QualType qual_type) {
18971897
const auto &size = array_type->getSize();
18981898
auto size_as_string = GetNumAsString(size);
18991899
auto element_type = array_type->getElementType();
1900-
PushConversionKind push(*this, ConversionKind::Unboxed);
1900+
PushConversionKind push(*this, element_type->isArrayType()
1901+
? ConversionKind::FullRefCount
1902+
: ConversionKind::Unboxed);
19011903
auto element_type_as_string = ToString(element_type);
19021904
auto default_as_string = GetDefaultAsString(element_type);
19031905
return std::format("(0..{}).map(|_| {}).collect::<Box<[{}]>>()",

tests/unit/array_2d_default.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <assert.h>
2+
3+
static void fill_row(char *row, char c) {
4+
row[0] = c;
5+
row[1] = '\0';
6+
}
7+
8+
int main(void) {
9+
char grid[3][6];
10+
for (int i = 0; i < 3; i++) {
11+
fill_row(grid[i], (char)('a' + i));
12+
}
13+
assert(grid[0][0] == 'a');
14+
assert(grid[1][0] == 'b');
15+
assert(grid[2][0] == 'c');
16+
assert(grid[1][1] == '\0');
17+
grid[2][5] = 'z';
18+
assert(grid[2][5] == 'z');
19+
return 0;
20+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
pub fn fill_row_0(row: Ptr<u8>, c: u8) {
10+
let row: Value<Ptr<u8>> = Rc::new(RefCell::new(row));
11+
let c: Value<u8> = Rc::new(RefCell::new(c));
12+
let __rhs = (*c.borrow());
13+
(*row.borrow()).offset((0) as isize).write(__rhs);
14+
(*row.borrow())
15+
.offset((1) as isize)
16+
.write((('\0' as i32) as u8));
17+
}
18+
pub fn main() {
19+
std::process::exit(main_0());
20+
}
21+
fn main_0() -> i32 {
22+
let grid: Value<Box<[Value<Box<[u8]>>]>> = Rc::new(RefCell::new(
23+
(0..3)
24+
.map(|_| {
25+
Rc::new(RefCell::new(
26+
(0..6).map(|_| <u8>::default()).collect::<Box<[u8]>>(),
27+
))
28+
})
29+
.collect::<Box<[Value<Box<[u8]>>]>>(),
30+
));
31+
let i: Value<i32> = Rc::new(RefCell::new(0));
32+
'loop_: while ((((*i.borrow()) < 3) as i32) != 0) {
33+
({
34+
let _row: Ptr<u8> = (((grid.as_pointer() as Ptr<Value<Box<[u8]>>>)
35+
.offset((*i.borrow()))
36+
.read()
37+
.as_pointer()) as Ptr<u8>);
38+
let _c: u8 = ((('a' as i32) + (*i.borrow())) as u8);
39+
fill_row_0(_row, _c)
40+
});
41+
(*i.borrow_mut()).postfix_inc();
42+
}
43+
assert!(
44+
(((((*grid.borrow())[(0) as usize].borrow()[(0) as usize] as i32) == ('a' as i32)) as i32)
45+
!= 0)
46+
);
47+
assert!(
48+
(((((*grid.borrow())[(1) as usize].borrow()[(0) as usize] as i32) == ('b' as i32)) as i32)
49+
!= 0)
50+
);
51+
assert!(
52+
(((((*grid.borrow())[(2) as usize].borrow()[(0) as usize] as i32) == ('c' as i32)) as i32)
53+
!= 0)
54+
);
55+
assert!(
56+
(((((*grid.borrow())[(1) as usize].borrow()[(1) as usize] as i32) == ('\0' as i32))
57+
as i32)
58+
!= 0)
59+
);
60+
(*grid.borrow())[(2) as usize].borrow_mut()[(5) as usize] = (('z' as i32) as u8);
61+
assert!(
62+
(((((*grid.borrow())[(2) as usize].borrow()[(5) as usize] as i32) == ('z' as i32)) as i32)
63+
!= 0)
64+
);
65+
return 0;
66+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub unsafe fn fill_row_0(mut row: *mut libc::c_char, mut c: libc::c_char) {
10+
(*row.offset((0) as isize)) = c;
11+
(*row.offset((1) as isize)) = (('\0' as i32) as libc::c_char);
12+
}
13+
pub fn main() {
14+
unsafe {
15+
std::process::exit(main_0() as i32);
16+
}
17+
}
18+
unsafe fn main_0() -> i32 {
19+
let mut grid: [[libc::c_char; 6]; 3] = [[(0 as libc::c_char); 6]; 3];
20+
let mut i: i32 = 0;
21+
'loop_: while ((((i) < (3)) as i32) != 0) {
22+
(unsafe {
23+
let _row: *mut libc::c_char = grid[(i) as usize].as_mut_ptr();
24+
let _c: libc::c_char = ((('a' as i32) + (i)) as libc::c_char);
25+
fill_row_0(_row, _c)
26+
});
27+
i.postfix_inc();
28+
}
29+
assert!(((((grid[(0) as usize][(0) as usize] as i32) == ('a' as i32)) as i32) != 0));
30+
assert!(((((grid[(1) as usize][(0) as usize] as i32) == ('b' as i32)) as i32) != 0));
31+
assert!(((((grid[(2) as usize][(0) as usize] as i32) == ('c' as i32)) as i32) != 0));
32+
assert!(((((grid[(1) as usize][(1) as usize] as i32) == ('\0' as i32)) as i32) != 0));
33+
grid[(2) as usize][(5) as usize] = (('z' as i32) as libc::c_char);
34+
assert!(((((grid[(2) as usize][(5) as usize] as i32) == ('z' as i32)) as i32) != 0));
35+
return 0;
36+
}

0 commit comments

Comments
 (0)