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
3 changes: 3 additions & 0 deletions cpp2rust/converter/mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,9 @@ std::string ToString(clang::QualType qual_type) {
std::string ToString(const clang::NamedDecl *decl) {
if (auto *record = clang::dyn_cast<clang::RecordDecl>(decl);
record && !record->getIdentifier()) {
if (auto *typedef_decl = record->getTypedefNameForAnonDecl()) {
return ToString(clang::cast<clang::NamedDecl>(typedef_decl));
}
return synthesizeAnonRecordName(record);
}

Expand Down
69 changes: 69 additions & 0 deletions tests/unit/out/refcount/typedef-anon-struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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};
#[derive(Default)]
pub struct Outer_RunInfo {
pub block_idx: Value<i32>,
pub num_extra_zero_runs: Value<i32>,
}
impl Clone for Outer_RunInfo {
fn clone(&self) -> Self {
let mut this = Self {
block_idx: Rc::new(RefCell::new((*self.block_idx.borrow()))),
num_extra_zero_runs: Rc::new(RefCell::new((*self.num_extra_zero_runs.borrow()))),
};
this
}
}
impl ByteRepr for Outer_RunInfo {}
#[derive(Default)]
pub struct Outer {
pub runs: Value<Vec<Outer_RunInfo>>,
}
impl Clone for Outer {
fn clone(&self) -> Self {
let mut this = Self {
runs: Rc::new(RefCell::new((*self.runs.borrow()).clone())),
};
this
}
}
impl ByteRepr for Outer {}
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
let o: Value<Outer> = Rc::new(RefCell::new(<Outer>::default()));
let info: Value<Outer_RunInfo> = Rc::new(RefCell::new(<Outer_RunInfo>::default()));
(*(*info.borrow()).block_idx.borrow_mut()) = 1;
(*(*info.borrow()).num_extra_zero_runs.borrow_mut()) = 2;
{
let a0_clone = (*info.borrow()).clone();
(*(*o.borrow()).runs.borrow_mut()).push(a0_clone)
};
assert!(((*(*o.borrow()).runs.borrow()).len() as u64 == 1_u64));
assert!(
((*(*((*o.borrow()).runs.as_pointer() as Ptr<Outer_RunInfo>)
.offset(0_u64 as isize)
.upgrade()
.deref())
.block_idx
.borrow())
== 1)
);
assert!(
((*(*((*o.borrow()).runs.as_pointer() as Ptr<Outer_RunInfo>)
.offset(0_u64 as isize)
.upgrade()
.deref())
.num_extra_zero_runs
.borrow())
== 2)
);
return 0;
}
38 changes: 38 additions & 0 deletions tests/unit/out/unsafe/typedef-anon-struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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;
#[repr(C)]
#[derive(Copy, Clone, Default)]
pub struct Outer_RunInfo {
pub block_idx: i32,
pub num_extra_zero_runs: i32,
}
#[repr(C)]
#[derive(Clone, Default)]
pub struct Outer {
pub runs: Vec<Outer_RunInfo>,
}
pub fn main() {
unsafe {
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
let mut o: Outer = <Outer>::default();
let mut info: Outer_RunInfo = <Outer_RunInfo>::default();
info.block_idx = 1;
info.num_extra_zero_runs = 2;
{
let a0_clone = info.clone();
o.runs.push(a0_clone)
};
assert!(((o.runs.len() as u64) == (1_u64)));
assert!(((o.runs[(0_u64) as usize].block_idx) == (1)));
assert!(((o.runs[(0_u64) as usize].num_extra_zero_runs) == (2)));
return 0;
}
26 changes: 26 additions & 0 deletions tests/unit/typedef-anon-struct.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <cassert>
#include <vector>

struct Outer {
typedef struct {
int block_idx;
int num_extra_zero_runs;
} RunInfo;

std::vector<RunInfo> runs;
};

int main() {
Outer o;

Outer::RunInfo info;
info.block_idx = 1;
info.num_extra_zero_runs = 2;
o.runs.push_back(info);

assert(o.runs.size() == 1);
assert(o.runs[0].block_idx == 1);
assert(o.runs[0].num_extra_zero_runs == 2);

return 0;
}
Loading