Skip to content

Commit

Permalink
Replace proc-macro-error with proc-macro2-diagnostics
Browse files Browse the repository at this point in the history
proc-macro-error is unmaintained.
  • Loading branch information
tamird authored and vadorovsky committed Dec 3, 2024
1 parent dae394e commit 5a43bed
Show file tree
Hide file tree
Showing 26 changed files with 642 additions and 566 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ nix = { version = "0.29.0", default-features = false }
num_enum = { version = "0.7", default-features = false }
object = { version = "0.36", default-features = false }
once_cell = { version = "1.20.1", default-features = false }
proc-macro-error = { version = "1.0", default-features = false }
proc-macro2 = { version = "1", default-features = false }
proc-macro2-diagnostics = { version = "0.10.1", default-features = false }
public-api = { version = "0.42.0", default-features = false }
quote = { version = "1", default-features = false }
rand = { version = "0.8", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion aya-ebpf-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ proc-macro = true

[dependencies]
proc-macro2 = { workspace = true }
proc-macro-error = { workspace = true }
proc-macro2-diagnostics = { workspace = true }
quote = { workspace = true }
syn = { workspace = true, default-features = true, features = ["full"] }

Expand Down
27 changes: 16 additions & 11 deletions aya-ebpf-macros/src/btf_tracepoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,33 @@ impl BtfTracePoint {
let function = pop_string_arg(&mut args, "function");
err_on_unknown_args(&args)?;

Ok(BtfTracePoint { item, function })
Ok(Self { item, function })
}

pub(crate) fn expand(&self) -> Result<TokenStream> {
let section_name: Cow<'_, _> = if let Some(function) = &self.function {
pub(crate) fn expand(&self) -> TokenStream {
let Self { item, function } = self;
let ItemFn {
attrs: _,
vis,
sig,
block: _,
} = item;
let section_name: Cow<'_, _> = if let Some(function) = function {
format!("tp_btf/{}", function).into()
} else {
"tp_btf".into()
};
let fn_vis = &self.item.vis;
let fn_name = self.item.sig.ident.clone();
let item = &self.item;
Ok(quote! {
let fn_name = &sig.ident;
quote! {
#[no_mangle]
#[link_section = #section_name]
#fn_vis fn #fn_name(ctx: *mut ::core::ffi::c_void) -> i32 {
#vis fn #fn_name(ctx: *mut ::core::ffi::c_void) -> i32 {
let _ = #fn_name(::aya_ebpf::programs::BtfTracePointContext::new(ctx));
return 0;

#item
}
})
}
}
}

Expand All @@ -60,7 +65,7 @@ mod tests {
),
)
.unwrap();
let expanded = prog.expand().unwrap();
let expanded = prog.expand();
let expected = quote!(
#[no_mangle]
#[link_section = "tp_btf"]
Expand All @@ -87,7 +92,7 @@ mod tests {
),
)
.unwrap();
let expanded = prog.expand().unwrap();
let expanded = prog.expand();
let expected = quote!(
#[no_mangle]
#[link_section = "tp_btf/some_func"]
Expand Down
31 changes: 18 additions & 13 deletions aya-ebpf-macros/src/cgroup_device.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
use proc_macro2::TokenStream;
use proc_macro_error::abort;
use proc_macro2_diagnostics::{Diagnostic, SpanDiagnosticExt as _};
use quote::quote;
use syn::{ItemFn, Result};
use syn::{spanned::Spanned as _, ItemFn};

pub(crate) struct CgroupDevice {
item: ItemFn,
}

impl CgroupDevice {
pub(crate) fn parse(attrs: TokenStream, item: TokenStream) -> Result<Self> {
pub(crate) fn parse(attrs: TokenStream, item: TokenStream) -> Result<Self, Diagnostic> {
if !attrs.is_empty() {
abort!(attrs, "unexpected attribute")
return Err(attrs.span().error("unexpected attribute"));
}
let item = syn::parse2(item)?;
Ok(CgroupDevice { item })
Ok(Self { item })
}

pub(crate) fn expand(&self) -> Result<TokenStream> {
let fn_vis = &self.item.vis;
let fn_name = self.item.sig.ident.clone();
let item = &self.item;
Ok(quote! {
pub(crate) fn expand(&self) -> TokenStream {
let Self { item } = self;
let ItemFn {
attrs: _,
vis,
sig,
block: _,
} = item;
let fn_name = &sig.ident;
quote! {
#[no_mangle]
#[link_section = "cgroup/dev"]
#fn_vis fn #fn_name(ctx: *mut ::aya_ebpf::bindings::bpf_cgroup_dev_ctx) -> i32 {
#vis fn #fn_name(ctx: *mut ::aya_ebpf::bindings::bpf_cgroup_dev_ctx) -> i32 {
return #fn_name(::aya_ebpf::programs::DeviceContext::new(ctx));

#item
}
})
}
}
}

Expand All @@ -49,7 +54,7 @@ mod tests {
),
)
.unwrap();
let expanded = prog.expand().unwrap();
let expanded = prog.expand();
let expected = quote! {
#[no_mangle]
#[link_section = "cgroup/dev"]
Expand Down
62 changes: 33 additions & 29 deletions aya-ebpf-macros/src/cgroup_skb.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,52 @@
use std::borrow::Cow;

use proc_macro2::TokenStream;
use proc_macro_error::abort;
use proc_macro2_diagnostics::{Diagnostic, SpanDiagnosticExt as _};
use quote::quote;
use syn::{Ident, ItemFn, Result};
use syn::{Ident, ItemFn};

pub(crate) struct CgroupSkb {
item: ItemFn,
attach_type: Option<String>,
attach_type: Option<Ident>,
}

impl CgroupSkb {
pub(crate) fn parse(attrs: TokenStream, item: TokenStream) -> Result<CgroupSkb> {
pub(crate) fn parse(attrs: TokenStream, item: TokenStream) -> Result<Self, Diagnostic> {
let item: ItemFn = syn::parse2(item)?;
let mut attach_type = None;
if !attrs.is_empty() {
let attach_type = if attrs.is_empty() {
None
} else {
let ident: Ident = syn::parse2(attrs)?;
match ident.to_string().as_str() {
"ingress" | "egress" => (),
_ => abort!(ident, "invalid attach type"),
if ident != "ingress" && ident != "egress" {
return Err(ident.span().error("invalid attach type"));
}
attach_type = Some(ident.to_string());
}
Ok(CgroupSkb { item, attach_type })
Some(ident)
};
Ok(Self { item, attach_type })
}

pub(crate) fn expand(&self) -> Result<TokenStream> {
let section_name: Cow<'_, _> = if self.attach_type.is_some() {
format!("cgroup_skb/{}", self.attach_type.as_ref().unwrap()).into()
} else {
"cgroup/skb".into()
pub(crate) fn expand(&self) -> TokenStream {
let Self { item, attach_type } = self;
let ItemFn {
attrs: _,
vis,
sig,
block: _,
} = item;
let section_name: Cow<'_, _> = match attach_type {
Some(attach_type) => format!("cgroup_skb/{attach_type}").into(),
None => "cgroup/skb".into(),
};
let fn_vis = &self.item.vis;
let fn_name = self.item.sig.ident.clone();
let item = &self.item;
Ok(quote! {
let fn_name = &sig.ident;
quote! {
#[no_mangle]
#[link_section = #section_name]
#fn_vis fn #fn_name(ctx: *mut ::aya_ebpf::bindings::__sk_buff) -> i32 {
#vis fn #fn_name(ctx: *mut ::aya_ebpf::bindings::__sk_buff) -> i32 {
return #fn_name(::aya_ebpf::programs::SkBuffContext::new(ctx));

#item
}
})
}
}
}

Expand All @@ -63,7 +67,7 @@ mod tests {
),
)
.unwrap();
let expanded = prog.expand().unwrap();
let expanded = prog.expand();
let expected = quote! {
#[no_mangle]
#[link_section = "cgroup/skb"]
Expand All @@ -89,7 +93,7 @@ mod tests {
),
)
.unwrap();
let expanded = prog.expand().unwrap();
let expanded = prog.expand();
let expected = quote! {
#[no_mangle]
#[link_section = "cgroup_skb/egress"]
Expand All @@ -115,7 +119,7 @@ mod tests {
),
)
.unwrap();
let expanded = prog.expand().unwrap();
let expanded = prog.expand();
let expected = quote! {
#[no_mangle]
#[link_section = "cgroup_skb/ingress"]
Expand All @@ -141,7 +145,7 @@ mod tests {
),
)
.unwrap();
let expanded = prog.expand().unwrap();
let expanded = prog.expand();
let expected = quote! {
#[no_mangle]
#[link_section = "cgroup_skb/egress"]
Expand All @@ -167,7 +171,7 @@ mod tests {
),
)
.unwrap();
let expanded = prog.expand().unwrap();
let expanded = prog.expand();
let expected = quote! {
#[no_mangle]
#[link_section = "cgroup_skb/egress"]
Expand All @@ -193,7 +197,7 @@ mod tests {
),
)
.unwrap();
let expanded = prog.expand().unwrap();
let expanded = prog.expand();
let expected = quote! {
#[no_mangle]
#[link_section = "cgroup_skb/egress"]
Expand Down
53 changes: 29 additions & 24 deletions aya-ebpf-macros/src/cgroup_sock.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,51 @@
use std::borrow::Cow;

use proc_macro2::TokenStream;
use proc_macro_error::abort;
use proc_macro2_diagnostics::{Diagnostic, SpanDiagnosticExt as _};
use quote::quote;
use syn::{Ident, ItemFn, Result};
use syn::{spanned::Spanned as _, Ident, ItemFn};

pub(crate) struct CgroupSock {
item: ItemFn,
attach_type: String,
attach_type: Ident,
}

impl CgroupSock {
pub(crate) fn parse(attrs: TokenStream, item: TokenStream) -> Result<CgroupSock> {
pub(crate) fn parse(attrs: TokenStream, item: TokenStream) -> Result<Self, Diagnostic> {
if attrs.is_empty() {
abort!(attrs, "missing attach type")
return Err(attrs.span().error("missing attach type"));
}
let item: ItemFn = syn::parse2(item)?;
let attach_type: Ident = syn::parse2(attrs)?;
match attach_type.to_string().as_str() {
"post_bind4" | "post_bind6" | "sock_create" | "sock_release" => (),
_ => abort!(attach_type, "invalid attach type"),
if attach_type != "post_bind4"
&& attach_type != "post_bind6"
&& attach_type != "sock_create"
&& attach_type != "sock_release"
{
return Err(attach_type.span().error("invalid attach type"));
}
Ok(CgroupSock {
item,
attach_type: attach_type.to_string(),
})
Ok(Self { item, attach_type })
}

pub(crate) fn expand(&self) -> Result<TokenStream> {
let section_name: Cow<'_, _> = format!("cgroup/{}", self.attach_type).into();
let fn_vis = &self.item.vis;
let fn_name = self.item.sig.ident.clone();
let item = &self.item;
Ok(quote! {
pub(crate) fn expand(&self) -> TokenStream {
let Self { item, attach_type } = self;
let ItemFn {
attrs: _,
vis,
sig,
block: _,
} = item;
let section_name: Cow<'_, _> = format!("cgroup/{attach_type}").into();
let fn_name = &sig.ident;
quote! {
#[no_mangle]
#[link_section = #section_name]
#fn_vis fn #fn_name(ctx: *mut ::aya_ebpf::bindings::bpf_sock) -> i32 {
#vis fn #fn_name(ctx: *mut ::aya_ebpf::bindings::bpf_sock) -> i32 {
return #fn_name(::aya_ebpf::programs::SockContext::new(ctx));

#item
}
})
}
}
}

Expand All @@ -61,7 +66,7 @@ mod tests {
),
)
.unwrap();
let expanded = prog.expand().unwrap();
let expanded = prog.expand();
let expected = quote! {
#[no_mangle]
#[link_section = "cgroup/post_bind4"]
Expand All @@ -87,7 +92,7 @@ mod tests {
),
)
.unwrap();
let expanded = prog.expand().unwrap();
let expanded = prog.expand();
let expected = quote! {
#[no_mangle]
#[link_section = "cgroup/post_bind6"]
Expand All @@ -112,7 +117,7 @@ mod tests {
),
)
.unwrap();
let expanded = prog.expand().unwrap();
let expanded = prog.expand();
let expected = quote! {
#[no_mangle]
#[link_section = "cgroup/sock_create"]
Expand All @@ -137,7 +142,7 @@ mod tests {
),
)
.unwrap();
let expanded = prog.expand().unwrap();
let expanded = prog.expand();
let expected = quote! {
#[no_mangle]
#[link_section = "cgroup/sock_release"]
Expand Down
Loading

0 comments on commit 5a43bed

Please sign in to comment.