Skip to content

Commit

Permalink
Merge pull request #57 from mkroening/macro-repr
Browse files Browse the repository at this point in the history
fix(macro): support `#[repr(align(N))]`
  • Loading branch information
phil-opp authored Apr 26, 2024
2 parents 1b97156 + bd7fdac commit 846c5c5
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions volatile-macro/src/volatile.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use quote::format_ident;
use syn::punctuated::Punctuated;
use syn::{
parse_quote, Attribute, Fields, Ident, Item, ItemImpl, ItemStruct, ItemTrait, Path, Result,
Signature, Visibility,
parse_quote, Attribute, Fields, Ident, Item, ItemImpl, ItemStruct, ItemTrait, Meta, Path,
Result, Signature, Token, Visibility,
};

fn validate_input(input: &ItemStruct) -> Result<()> {
Expand All @@ -22,9 +23,13 @@ fn validate_input(input: &ItemStruct) -> Result<()> {
let mut valid_repr = false;
for attr in &input.attrs {
if attr.path().is_ident("repr") {
let ident = attr.parse_args::<Ident>()?;
if ident == "C" || ident == "transparent" {
valid_repr = true;
let nested = attr.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)?;
for meta in nested {
if let Meta::Path(path) = meta {
if path.is_ident("C") || path.is_ident("transparent") {
valid_repr = true;
}
}
}
}
}
Expand Down Expand Up @@ -212,4 +217,36 @@ mod tests {

Ok(())
}

#[test]
fn test_align() -> Result<()> {
let input = parse_quote! {
#[repr(C, align(8))]
#[derive(VolatileFieldAccess)]
pub struct DeviceConfig {}
};

let result = derive_volatile(input)?;

let expected_trait = quote! {
#[allow(non_camel_case_types)]
pub trait DeviceConfigVolatileFieldAccess<'a> {}
};

let expected_impl = quote! {
#[automatically_derived]
impl<'a> DeviceConfigVolatileFieldAccess<'a> for ::volatile::VolatilePtr<'a, DeviceConfig, ::volatile::access::ReadWrite> {}
};

assert_eq!(
expected_trait.to_string(),
result[0].to_token_stream().to_string()
);
assert_eq!(
expected_impl.to_string(),
result[1].to_token_stream().to_string()
);

Ok(())
}
}

0 comments on commit 846c5c5

Please sign in to comment.