Skip to content

Commit 7b41d8b

Browse files
aero_proc::syscall: use proc_macro_error for error reporting
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent 6f7eada commit 7b41d8b

File tree

2 files changed

+41
-50
lines changed

2 files changed

+41
-50
lines changed

src/aero_proc/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use proc_macro::TokenStream;
3838
/// }
3939
/// ```
4040
#[proc_macro_attribute]
41+
#[proc_macro_error]
4142
pub fn test(attr: TokenStream, item: TokenStream) -> TokenStream {
4243
test::parse(attr, item)
4344
}
@@ -48,6 +49,7 @@ pub fn test(attr: TokenStream, item: TokenStream) -> TokenStream {
4849
/// have a valid return-type of `Result<usize, AeroSyscallError>`. In addition, the function cannot
4950
/// have generic parameters.
5051
#[proc_macro_attribute]
52+
#[proc_macro_error]
5153
pub fn syscall(attr: TokenStream, item: TokenStream) -> TokenStream {
5254
syscall::parse(attr, item)
5355
}

src/aero_proc/src/syscall.rs

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717
* along with Aero. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
use proc_macro::{Diagnostic, Level, TokenStream};
20+
use proc_macro::TokenStream;
2121
use proc_macro2::{Ident, Span};
22+
2223
use quote::quote;
23-
use syn::{punctuated::Punctuated, spanned::Spanned, Expr, FnArg, Pat, Type};
24+
25+
use syn::punctuated::Punctuated;
26+
use syn::spanned::Spanned;
27+
use syn::{Expr, FnArg, Pat, Type};
2428

2529
enum ArgType {
2630
Array(bool), // mutable?
@@ -33,53 +37,42 @@ enum ArgType {
3337

3438
pub fn parse(_: TokenStream, item: TokenStream) -> TokenStream {
3539
let parsed_fn = syn::parse_macro_input!(item as syn::ItemFn);
40+
let signature = &parsed_fn.sig;
3641

37-
if let Some(constness) = parsed_fn.sig.constness {
38-
Diagnostic::spanned(
39-
constness.span().unwrap(),
40-
Level::Error,
41-
"syscall functions cannot be const",
42-
)
43-
.emit();
44-
}
42+
signature
43+
.constness
44+
.map(|e| emit_error!(e.span(), "syscall functions cannot be `const`"));
4545

46-
if let Some(asyncness) = parsed_fn.sig.asyncness {
47-
Diagnostic::spanned(
48-
asyncness.span().unwrap(),
49-
Level::Error,
50-
"syscall functions cannot be async",
51-
)
52-
.emit();
53-
}
46+
signature
47+
.asyncness
48+
.map(|e| emit_error!(e.span(), "syscall functions cannot be `async`"));
5449

55-
if let Some(unsafety) = parsed_fn.sig.unsafety {
56-
Diagnostic::spanned(
57-
unsafety.span().unwrap(),
58-
Level::Error,
59-
"syscall functions cannot be unsafe",
60-
)
61-
.emit();
62-
}
50+
signature
51+
.unsafety
52+
.map(|e| emit_error!(e.span(), "syscalls functions cannot be `unsafe`"));
53+
54+
let generics = &signature.generics;
55+
56+
// NOTE: if `lt_token` is present then `gt_token` will also be present (else invalid syntax).
57+
if generics.lt_token.is_some() {
58+
let lt_span = generics.lt_token.span().unwrap();
59+
let gt_span = generics.gt_token.span().unwrap();
6360

64-
if let Some(_) = parsed_fn.sig.generics.lt_token {
65-
let lt_span = parsed_fn.sig.generics.lt_token.span().unwrap();
66-
let gt_span = parsed_fn.sig.generics.gt_token.span().unwrap();
61+
let span_range = lt_span.join(gt_span).unwrap();
6762

68-
Diagnostic::spanned(
69-
lt_span.join(gt_span).unwrap(),
70-
Level::Error,
71-
"syscall functions cannot have generic parameters",
72-
)
73-
.emit();
63+
emit_error!(
64+
span_range,
65+
"syscall functions cannot have generic parameters"
66+
);
7467
}
7568

7669
let attrs = &parsed_fn.attrs;
7770
let vis = &parsed_fn.vis;
78-
let name = &parsed_fn.sig.ident;
79-
let orig_args = &parsed_fn.sig.inputs;
71+
let name = &signature.ident;
72+
let orig_args = &signature.inputs;
8073
let processed_args = process_args(orig_args);
8174
let call_args = process_call_args(orig_args);
82-
let ret = &parsed_fn.sig.output;
75+
let ret = &signature.output;
8376
let body = &parsed_fn.block;
8477

8578
let result = quote! {
@@ -150,21 +143,17 @@ fn process_args(args: &Punctuated<FnArg, syn::Token![,]>) -> Vec<FnArg> {
150143
};
151144
}
152145
_ => {
153-
Diagnostic::spanned(
154-
arg.span().unwrap(),
155-
Level::Error,
156-
"syscall function arguments cannot have non-ident patterns",
157-
)
158-
.emit();
146+
emit_error!(
147+
arg.span(),
148+
"syscall function arguments cannot have non-ident patterns"
149+
);
159150
}
160151
},
161152
FnArg::Receiver(_) => {
162-
Diagnostic::spanned(
163-
arg.span().unwrap(),
164-
Level::Error,
165-
"syscall functions cannot have receiver arguments",
166-
)
167-
.emit();
153+
emit_error!(
154+
arg.span(),
155+
"syscall functions cannot have receiver arguments"
156+
);
168157
}
169158
}
170159
}

0 commit comments

Comments
 (0)