Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(macro): support #[repr(align(N))] #57

Merged
merged 1 commit into from
Apr 26, 2024
Merged
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
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(())
}
}