summaryrefslogtreecommitdiff
path: root/src/codegen
diff options
context:
space:
mode:
authorChristian Legnitto <christian@legnitto.com>2019-01-07 20:06:34 -0800
committerChristian Legnitto <christian@legnitto.com>2019-01-08 08:56:19 -0800
commit3994a9a3a75245fcec59a2a3e62f163c1fa2e24b (patch)
treeba331e1ce2f9216896394783085003da86526ea8 /src/codegen
parent28c0eb4505865220d6f2a8f779f5876dc753feac (diff)
Support #[repr(packed(N))] on Rust 1.33+
Fixes https://github.com/rust-lang/rust-bindgen/issues/537.
Diffstat (limited to 'src/codegen')
-rw-r--r--src/codegen/helpers.rs3
-rw-r--r--src/codegen/mod.rs5
2 files changed, 6 insertions, 2 deletions
diff --git a/src/codegen/helpers.rs b/src/codegen/helpers.rs
index d6377194..457979b6 100644
--- a/src/codegen/helpers.rs
+++ b/src/codegen/helpers.rs
@@ -7,6 +7,7 @@ use quote::TokenStreamExt;
pub mod attributes {
use proc_macro2::{Ident, Span, TokenStream};
+ use std::str::FromStr;
pub fn repr(which: &str) -> TokenStream {
let which = Ident::new(which, Span::call_site());
@@ -16,7 +17,7 @@ pub mod attributes {
}
pub fn repr_list(which_ones: &[&str]) -> TokenStream {
- let which_ones = which_ones.iter().cloned().map(|one| Ident::new(one, Span::call_site()));
+ let which_ones = which_ones.iter().cloned().map(|one| TokenStream::from_str(one).expect("repr to be valid"));
quote! {
#[repr( #( #which_ones ),* )]
}
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs
index 6fd7e7d3..d3395edf 100644
--- a/src/codegen/mod.rs
+++ b/src/codegen/mod.rs
@@ -1673,7 +1673,10 @@ impl CodeGenerator for CompInfo {
attributes.push(attributes::doc(comment));
}
if packed && !is_opaque {
- attributes.push(attributes::repr_list(&["C", "packed"]));
+ let n = layout.map_or(1, |l| l.align);
+ assert!(ctx.options().rust_features().repr_packed_n || n == 1);
+ let packed_repr = if n == 1 { "packed".to_string() } else { format!("packed({})", n) };
+ attributes.push(attributes::repr_list(&["C", &packed_repr]));
} else {
attributes.push(attributes::repr("C"));
}