Skip to content

Commit

Permalink
Add support for unions
Browse files Browse the repository at this point in the history
Closes #20
  • Loading branch information
alexcrichton committed Jul 30, 2017
1 parent 391ac85 commit f4e0e2f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,8 @@ impl<'a, 'v> Visitor<'v> for Generator<'a> {
self.test_type(&i.ident.to_string());
}

ast::ItemKind::Struct(ref s, ref generics) if public => {
ast::ItemKind::Struct(ref s, ref generics) |
ast::ItemKind::Union(ref s, ref generics) if public => {
self.assert_no_generics(i.ident, generics);
let is_c = i.attrs.iter().any(|a| {
attr::find_repr_attrs(self.sh, a).iter().any(|a| {
Expand Down
5 changes: 5 additions & 0 deletions testcrate/src/t1.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ struct T1Baz {
struct T1Bar b;
};

typedef union {
uint64_t a;
uint32_t b;
} T1Union;

void T1a(void);
void* T1b(void);
void* T1c(void*);
Expand Down
6 changes: 6 additions & 0 deletions testcrate/src/t1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ pub struct T1Baz {
pub b: T1Bar,
}

#[repr(C)]
pub union T1Union {
pub a: u64,
pub b: u32,
}

i! {
pub const T1C: u32 = 4;
}
Expand Down
5 changes: 5 additions & 0 deletions testcrate/src/t2.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ struct T2Baz {
uint32_t b;
};

typedef struct {
uint32_t a;
int64_t b;
} T2Union;

static void T2a(void) {}

#define T2C 4
Expand Down
6 changes: 6 additions & 0 deletions testcrate/src/t2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ pub struct T2Baz {
pub b: u32,
}

#[repr(C)]
pub union T2Union {
pub a: u32,
pub b: i64,
}

pub const T2C: i32 = 5;

i! {
Expand Down
3 changes: 3 additions & 0 deletions testcrate/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ fn t2() {
"bad T2a function pointer",
"bad T2C value at byte 0",
"bad T2S string",
"bad T2Union size",
"bad field type b of T2Union",
"bad field offset b of T2Union",
];
let mut errors = errors.iter().cloned().collect::<HashSet<_>>();

Expand Down

0 comments on commit f4e0e2f

Please sign in to comment.