diff options
-rw-r--r-- | cortex-m-rt/macros/src/lib.rs | 64 | ||||
-rw-r--r-- | cortex-m-rt/tests/compile-fail/blacklist-1.rs | 26 | ||||
-rw-r--r-- | cortex-m-rt/tests/compile-fail/blacklist-2.rs | 26 | ||||
-rw-r--r-- | cortex-m-rt/tests/compile-fail/blacklist-3.rs | 26 | ||||
-rw-r--r-- | cortex-m-rt/tests/compile-fail/blacklist-4.rs | 26 |
5 files changed, 168 insertions, 0 deletions
diff --git a/cortex-m-rt/macros/src/lib.rs b/cortex-m-rt/macros/src/lib.rs index 763b721..9755153 100644 --- a/cortex-m-rt/macros/src/lib.rs +++ b/cortex-m-rt/macros/src/lib.rs @@ -150,7 +150,15 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream { }) .collect::<Vec<_>>(); + if let Some(error) = check_for_blacklisted_attrs(&f.attrs) { + return error; + } + + let (ref cfgs, ref attrs) = extract_cfgs(f.attrs.clone()); + quote!( + #(#cfgs)* + #(#attrs)* #[doc(hidden)] #[export_name = "main"] pub unsafe extern "C" fn #tramp_ident() { @@ -343,7 +351,15 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream { let tramp_ident = Ident::new(&format!("{}_trampoline", f.sig.ident), Span::call_site()); let ident = &f.sig.ident; + if let Some(error) = check_for_blacklisted_attrs(&f.attrs) { + return error; + } + + let (ref cfgs, ref attrs) = extract_cfgs(f.attrs.clone()); + quote!( + #(#cfgs)* + #(#attrs)* #[doc(hidden)] #[export_name = #ident_s] pub unsafe extern "C" fn #tramp_ident() { @@ -396,7 +412,15 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream { let tramp_ident = Ident::new(&format!("{}_trampoline", f.sig.ident), Span::call_site()); let ident = &f.sig.ident; + if let Some(error) = check_for_blacklisted_attrs(&f.attrs) { + return error; + } + + let (ref cfgs, ref attrs) = extract_cfgs(f.attrs.clone()); + quote!( + #(#cfgs)* + #(#attrs)* #[doc(hidden)] #[export_name = "HardFault"] #[link_section = ".HardFault.user"] @@ -481,7 +505,15 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream { }) .collect::<Vec<_>>(); + if let Some(error) = check_for_blacklisted_attrs(&f.attrs) { + return error; + } + + let (ref cfgs, ref attrs) = extract_cfgs(f.attrs.clone()); + quote!( + #(#cfgs)* + #(#attrs)* #[doc(hidden)] #[export_name = #ident_s] pub unsafe extern "C" fn #tramp_ident() { @@ -650,7 +682,15 @@ pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream { }) .collect::<Vec<_>>(); + if let Some(error) = check_for_blacklisted_attrs(&f.attrs) { + return error; + } + + let (ref cfgs, ref attrs) = extract_cfgs(f.attrs.clone()); + quote!( + #(#cfgs)* + #(#attrs)* #[doc(hidden)] #[export_name = #ident_s] pub unsafe extern "C" fn #tramp_ident() { @@ -790,6 +830,30 @@ fn extract_cfgs(attrs: Vec<Attribute>) -> (Vec<Attribute>, Vec<Attribute>) { (cfgs, not_cfgs) } +fn check_for_blacklisted_attrs(attrs: &[Attribute]) -> Option<TokenStream> { + if let Some(val) = containts_blacklist_attrs(attrs) { + return Some(parse::Error::new(val.span(), "This attribute is not allowed [blacklisted]") + .to_compile_error() + .into()); + } + + None +} + +fn containts_blacklist_attrs(attrs: &[Attribute]) -> Option<Attribute> { + let blacklist = &["inline", "export_name", "no_mangle", "must_use"]; + + for attr in attrs { + for val in blacklist { + if eq(&attr, &val) { + return Some(attr.clone()); + } + } + } + + None +} + /// Returns `true` if `attr.path` matches `name` fn eq(attr: &Attribute, name: &str) -> bool { attr.style == AttrStyle::Outer && attr.path.is_ident(name) diff --git a/cortex-m-rt/tests/compile-fail/blacklist-1.rs b/cortex-m-rt/tests/compile-fail/blacklist-1.rs new file mode 100644 index 0000000..57d90e3 --- /dev/null +++ b/cortex-m-rt/tests/compile-fail/blacklist-1.rs @@ -0,0 +1,26 @@ +#![no_main] +#![no_std] + +extern crate cortex_m_rt; +extern crate panic_halt; + +use cortex_m_rt::{entry, exception, interrupt}; + +#[inline] //~ ERROR This attribute is not allowed [blacklisted] +#[entry] +fn foo() -> ! { + loop {} +} + +#[inline] //~ ERROR This attribute is not allowed [blacklisted] +#[exception] +fn SysTick() {} + +#[allow(non_camel_case_types)] +enum interrupt { + USART1, +} + +#[inline] //~ ERROR This attribute is not allowed [blacklisted] +#[interrupt] +fn USART1() {} diff --git a/cortex-m-rt/tests/compile-fail/blacklist-2.rs b/cortex-m-rt/tests/compile-fail/blacklist-2.rs new file mode 100644 index 0000000..ab30235 --- /dev/null +++ b/cortex-m-rt/tests/compile-fail/blacklist-2.rs @@ -0,0 +1,26 @@ +#![no_main] +#![no_std] + +extern crate cortex_m_rt; +extern crate panic_halt; + +use cortex_m_rt::{entry, exception, interrupt}; + +#[export_name = "not_allowed"] //~ ERROR This attribute is not allowed [blacklisted] +#[entry] +fn foo() -> ! { + loop {} +} + +#[export_name = "not_allowed"] //~ ERROR This attribute is not allowed [blacklisted] +#[exception] +fn SysTick() {} + +#[allow(non_camel_case_types)] +enum interrupt { + USART1, +} + +#[export_name = "not_allowed"] //~ ERROR This attribute is not allowed [blacklisted] +#[interrupt] +fn USART1() {} diff --git a/cortex-m-rt/tests/compile-fail/blacklist-3.rs b/cortex-m-rt/tests/compile-fail/blacklist-3.rs new file mode 100644 index 0000000..c0990bb --- /dev/null +++ b/cortex-m-rt/tests/compile-fail/blacklist-3.rs @@ -0,0 +1,26 @@ +#![no_main] +#![no_std] + +extern crate cortex_m_rt; +extern crate panic_halt; + +use cortex_m_rt::{entry, exception, interrupt}; + +#[no_mangle] //~ ERROR This attribute is not allowed [blacklisted] +#[entry] +fn foo() -> ! { + loop {} +} + +#[no_mangle] //~ ERROR This attribute is not allowed [blacklisted] +#[exception] +fn SysTick() {} + +#[allow(non_camel_case_types)] +enum interrupt { + USART1, +} + +#[no_mangle] //~ ERROR This attribute is not allowed [blacklisted] +#[interrupt] +fn USART1() {} diff --git a/cortex-m-rt/tests/compile-fail/blacklist-4.rs b/cortex-m-rt/tests/compile-fail/blacklist-4.rs new file mode 100644 index 0000000..d0753e0 --- /dev/null +++ b/cortex-m-rt/tests/compile-fail/blacklist-4.rs @@ -0,0 +1,26 @@ +#![no_main] +#![no_std] + +extern crate cortex_m_rt; +extern crate panic_halt; + +use cortex_m_rt::{entry, exception, interrupt}; + +#[must_use] //~ ERROR This attribute is not allowed [blacklisted] +#[entry] +fn foo() -> ! { + loop {} +} + +#[must_use] //~ ERROR This attribute is not allowed [blacklisted] +#[exception] +fn SysTick() {} + +#[allow(non_camel_case_types)] +enum interrupt { + USART1, +} + +#[must_use] //~ ERROR This attribute is not allowed [blacklisted] +#[interrupt] +fn USART1() {} |