Skip to content

ctest: add tests for aliases, structs, unions, as well as a test crate. #4543

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion ctest-next/src/ast/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::BoxStr;
/// Represents a constant variable defined in Rust.
#[derive(Debug, Clone)]
pub struct Const {
#[expect(unused)]
pub(crate) public: bool,
pub(crate) ident: BoxStr,
pub(crate) ty: syn::Type,
Expand Down
1 change: 0 additions & 1 deletion ctest-next/src/ast/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::BoxStr;
/// Represents a field in a struct or union defined in Rust.
#[derive(Debug, Clone)]
pub struct Field {
#[expect(unused)]
pub(crate) public: bool,
pub(crate) ident: BoxStr,
pub(crate) ty: syn::Type,
Expand Down
7 changes: 6 additions & 1 deletion ctest-next/src/ast/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use crate::{Abi, BoxStr, Parameter};
/// This structure is only used for parsing functions in extern blocks.
#[derive(Debug, Clone)]
pub struct Fn {
#[expect(unused)]
pub(crate) public: bool,
#[expect(unused)]
pub(crate) abi: Abi,
pub(crate) ident: BoxStr,
pub(crate) link_name: Option<BoxStr>,
#[expect(unused)]
pub(crate) parameters: Vec<Parameter>,
#[expect(unused)]
Expand All @@ -21,4 +21,9 @@ impl Fn {
pub fn ident(&self) -> &str {
&self.ident
}

/// Return the name of the function to be linked C side with.
pub fn link_name(&self) -> Option<&str> {
self.link_name.as_deref()
}
}
1 change: 0 additions & 1 deletion ctest-next/src/ast/static_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{Abi, BoxStr};
/// as a result it does not have a field for storing the expression.
#[derive(Debug, Clone)]
pub struct Static {
#[expect(unused)]
pub(crate) public: bool,
#[expect(unused)]
pub(crate) abi: Abi,
Expand Down
7 changes: 5 additions & 2 deletions ctest-next/src/ast/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ use crate::{BoxStr, Field};
/// Represents a struct defined in Rust.
#[derive(Debug, Clone)]
pub struct Struct {
#[expect(unused)]
pub(crate) public: bool,
pub(crate) ident: BoxStr,
#[expect(unused)]
pub(crate) fields: Vec<Field>,
}

Expand All @@ -15,4 +13,9 @@ impl Struct {
pub fn ident(&self) -> &str {
&self.ident
}

/// Return the public fields of the struct.
pub fn public_fields(&self) -> impl Iterator<Item = &Field> {
self.fields.iter().filter(|f| f.public)
}
}
1 change: 0 additions & 1 deletion ctest-next/src/ast/type_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::BoxStr;
/// Represents a type alias defined in Rust.
#[derive(Debug, Clone)]
pub struct Type {
#[expect(unused)]
pub(crate) public: bool,
pub(crate) ident: BoxStr,
pub(crate) ty: syn::Type,
Expand Down
6 changes: 5 additions & 1 deletion ctest-next/src/ast/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub struct Union {
#[expect(unused)]
pub(crate) public: bool,
pub(crate) ident: BoxStr,
#[expect(unused)]
pub(crate) fields: Vec<Field>,
}

Expand All @@ -15,4 +14,9 @@ impl Union {
pub fn ident(&self) -> &str {
&self.ident
}

/// Return the public fields of the union.
pub(crate) fn public_fields(&self) -> impl Iterator<Item = &Field> {
self.fields.iter().filter(|f| f.public)
}
}
23 changes: 22 additions & 1 deletion ctest-next/src/ffi_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl FfiItems {
}

/// Return a list of all type aliases found.
#[cfg_attr(not(test), expect(unused))]
pub(crate) fn aliases(&self) -> &Vec<Type> {
&self.aliases
}
Expand Down Expand Up @@ -123,10 +122,32 @@ fn visit_foreign_item_fn(table: &mut FfiItems, i: &syn::ForeignItemFn, abi: &Abi
syn::ReturnType::Type(_, ty) => Some(ty.deref().clone()),
};

let mut link_name_iter = i
.attrs
.iter()
.filter(|attr| attr.path().is_ident("link_name"));

let link_name = link_name_iter.next().and_then(|attr| match &attr.meta {
syn::Meta::NameValue(nv) => {
if let syn::Expr::Lit(expr_lit) = &nv.value {
if let syn::Lit::Str(lit_str) = &expr_lit.lit {
return Some(lit_str.value().into_boxed_str());
}
}
None
}
_ => None,
});

if let Some(attr) = link_name_iter.next() {
panic!("multiple `#[link_name = ...]` attributes found: {attr:?}");
}

table.foreign_functions.push(Fn {
public,
abi,
ident,
link_name,
parameters,
return_type,
});
Expand Down
Loading