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

make defmt attributes forward input attributes #293

Merged
merged 6 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ unstable-test = ["defmt-macros/unstable-test"]
defmt-macros = { path = "macros", version = "0.1.1" }
heapless = "0.5.6"

[dev-dependencies]
trybuild = "1.0.37"

[workspace]
members = [
".",
Expand Down
42 changes: 39 additions & 3 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,35 @@ use syn::{
parse_macro_input,
punctuated::Punctuated,
spanned::Spanned as _,
Data, DeriveInput, Expr, ExprPath, Fields, FieldsNamed, FieldsUnnamed, GenericParam, ItemFn,
ItemStruct, LitStr, Path, PathArguments, PathSegment, ReturnType, Token, Type, WhereClause,
WherePredicate,
Attribute, Data, DeriveInput, Expr, ExprPath, Fields, FieldsNamed, FieldsUnnamed, GenericParam,
ItemFn, ItemStruct, LitStr, Path, PathArguments, PathSegment, ReturnType, Token, Type,
WhereClause, WherePredicate,
};

fn reject_attributes(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a short description of what this function does?

(nit: the function name is a bit misleading, I would've found something like check_attribute_conflict more helpful but that's really splitting hairs.)

// appears in the error message
attr_name: &str,
attrs_to_check: &[Attribute],
reject_list: &[&str],
) -> parse::Result<()> {
for attr in attrs_to_check {
if let Some(ident) = attr.path.get_ident() {
let ident = ident.to_string();
if reject_list.contains(&&*ident) {
return Err(parse::Error::new(
attr.span(),
format!(
"`#[{}]` attribute cannot be used together with `#[{}]`",
attr_name, ident
),
));
}
}
}

Ok(())
}

#[proc_macro_attribute]
pub fn global_logger(args: TokenStream, input: TokenStream) -> TokenStream {
if !args.is_empty() {
Expand All @@ -42,8 +66,10 @@ pub fn global_logger(args: TokenStream, input: TokenStream) -> TokenStream {
.into();
}

let attrs = &s.attrs;
let vis = &s.vis;
quote!(
#(#attrs)*
#vis struct #ident;

#[no_mangle]
Expand Down Expand Up @@ -95,8 +121,13 @@ pub fn panic_handler(args: TokenStream, input: TokenStream) -> TokenStream {
.into();
}

let attrs = &f.attrs;
if let Err(e) = reject_attributes("panic_handler", attrs, &["export_name", "no_mangle"]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo possible attribute clashes should be documented in https://defmt.ferrous-systems.com/panic.html?highlight=panic_han#defmtpanic_handler

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call

return e.to_compile_error().into();
}
let block = &f.block;
quote!(
#(#attrs)*
#[export_name = "_defmt_panic"]
fn #ident() -> ! {
#block
Expand Down Expand Up @@ -141,9 +172,14 @@ pub fn timestamp(args: TokenStream, input: TokenStream) -> TokenStream {
.into();
}

let attrs = &f.attrs;
if let Err(e) = reject_attributes("timestamp", attrs, &["export_name", "no_mangle"]) {
return e.to_compile_error().into();
}
let block = &f.block;
quote!(
#[export_name = "_defmt_timestamp"]
#(#attrs)*
fn #ident() -> u64 {
#block
}
Expand Down
5 changes: 5 additions & 0 deletions tests/ui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[test]
fn ui() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/*.rs");
}
8 changes: 8 additions & 0 deletions tests/ui/panic-handler-export-name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![no_main]
#![no_std]

#[defmt::panic_handler]
#[export_name = "hello"]
fn foo() -> ! {
loop {}
}
5 changes: 5 additions & 0 deletions tests/ui/panic-handler-export-name.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: `#[panic_handler]` attribute cannot be used together with `#[export_name]`
--> $DIR/panic-handler-export-name.rs:5:1
|
5 | #[export_name = "hello"]
| ^^^^^^^^^^^^^^^^^^^^^^^^
8 changes: 8 additions & 0 deletions tests/ui/panic-handler-no-mangle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![no_main]
#![no_std]

#[defmt::panic_handler]
#[no_mangle]
fn panic() -> ! {
loop {}
}
5 changes: 5 additions & 0 deletions tests/ui/panic-handler-no-mangle.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: `#[panic_handler]` attribute cannot be used together with `#[no_mangle]`
--> $DIR/panic-handler-no-mangle.rs:5:1
|
5 | #[no_mangle]
| ^^^^^^^^^^^^
8 changes: 8 additions & 0 deletions tests/ui/timestamp-export-name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![no_main]
#![no_std]

#[defmt::timestamp]
#[export_name = "hello"]
fn foo() -> u64 {
0
}
5 changes: 5 additions & 0 deletions tests/ui/timestamp-export-name.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: `#[timestamp]` attribute cannot be used together with `#[export_name]`
--> $DIR/timestamp-export-name.rs:5:1
|
5 | #[export_name = "hello"]
| ^^^^^^^^^^^^^^^^^^^^^^^^
8 changes: 8 additions & 0 deletions tests/ui/timestamp-no-mangle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![no_main]
#![no_std]

#[defmt::timestamp]
#[no_mangle]
fn foo() -> u64 {
0
}
5 changes: 5 additions & 0 deletions tests/ui/timestamp-no-mangle.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: `#[timestamp]` attribute cannot be used together with `#[no_mangle]`
--> $DIR/timestamp-no-mangle.rs:5:1
|
5 | #[no_mangle]
| ^^^^^^^^^^^^