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

xtask: Allow unions in uefi-raw #1018

Merged
merged 1 commit into from Dec 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
87 changes: 79 additions & 8 deletions xtask/src/check_raw.rs
Expand Up @@ -16,8 +16,8 @@ use syn::spanned::Spanned;
use syn::token::Comma;
use syn::{
parenthesized, Abi, Attribute, Field, Fields, FieldsNamed, FieldsUnnamed, File, Item,
ItemConst, ItemMacro, ItemStruct, ItemType, LitInt, ReturnType, Type, TypeArray, TypeBareFn,
TypePtr, Visibility,
ItemConst, ItemMacro, ItemStruct, ItemType, ItemUnion, LitInt, ReturnType, Type, TypeArray,
TypeBareFn, TypePtr, Visibility,
};
use walkdir::WalkDir;

Expand Down Expand Up @@ -253,26 +253,46 @@ fn check_fields(fields: &Punctuated<Field, Comma>, src: &Path) -> Result<(), Err
Ok(())
}

fn check_type_attrs(attrs: &[Attribute], spanned: &dyn Spanned, src: &Path) -> Result<(), Error> {
let attrs = parse_attrs(attrs, src)?;
let reprs = get_reprs(&attrs);

let allowed_reprs: &[&[Repr]] = &[&[Repr::C], &[Repr::C, Repr::Packed], &[Repr::Transparent]];

if allowed_reprs.contains(&reprs.as_slice()) {
Ok(())
} else {
Err(Error::new(ErrorKind::ForbiddenRepr, src, spanned))
}
}

/// Validate a struct.
fn check_struct(item: &ItemStruct, src: &Path) -> Result<(), Error> {
if !is_pub(&item.vis) {
return Err(Error::new(ErrorKind::MissingPub, src, &item.struct_token));
}

let attrs = parse_attrs(&item.attrs, src)?;

match &item.fields {
Fields::Named(FieldsNamed { named, .. }) => check_fields(named, src)?,
Fields::Unnamed(FieldsUnnamed { unnamed, .. }) => check_fields(unnamed, src)?,
Fields::Unit => {}
}

let reprs = get_reprs(&attrs);
let allowed_reprs: &[&[Repr]] = &[&[Repr::C], &[Repr::C, Repr::Packed], &[Repr::Transparent]];
if !allowed_reprs.contains(&reprs.as_slice()) {
return Err(Error::new(ErrorKind::ForbiddenRepr, src, item));
check_type_attrs(&item.attrs, item, src)?;

Ok(())
}

/// Validate a union.
fn check_union(item: &ItemUnion, src: &Path) -> Result<(), Error> {
if !is_pub(&item.vis) {
return Err(Error::new(ErrorKind::MissingPub, src, &item.union_token));
}

check_fields(&item.fields.named, src)?;

check_type_attrs(&item.attrs, item, src)?;

Ok(())
}

Expand Down Expand Up @@ -319,6 +339,9 @@ fn check_item(item: &Item, src: &Path) -> Result<(), Error> {
Item::Struct(item) => {
check_struct(item, src)?;
}
Item::Union(item) => {
check_union(item, src)?;
}
Item::Macro(item) => {
check_macro(item, src)?;
}
Expand Down Expand Up @@ -555,4 +578,52 @@ mod tests {
ErrorKind::ForbiddenType,
);
}

#[test]
fn test_union() {
// Valid union.
assert!(check_union(
&parse_quote! {
#[repr(C)]
pub union U {
pub a: u32,
pub b: u64,
}
},
src(),
)
.is_ok());

// Missing `pub` on union.
check_item_err(
parse_quote! {
#[repr(C)]
struct U {
pub f: u32,
}
},
ErrorKind::MissingPub,
);

// Missing `pub` on field.
check_item_err(
parse_quote! {
#[repr(C)]
pub struct U {
f: u32,
}
},
ErrorKind::MissingPub,
);

// Forbidden `repr`.
check_item_err(
parse_quote! {
pub struct S {
pub f: u32,
}
},
ErrorKind::ForbiddenRepr,
);
}
}