Skip to content

Commit 1b5da39

Browse files
committed
Added blacklisted attributes
1 parent ec7a1a7 commit 1b5da39

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed

cortex-m-rt/macros/src/lib.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,15 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
150150
})
151151
.collect::<Vec<_>>();
152152

153+
if let Some(error) = check_for_blacklisted_attrs(&f.attrs) {
154+
return error;
155+
}
156+
157+
let (ref cfgs, ref attrs) = extract_cfgs(f.attrs.clone());
158+
153159
quote!(
160+
#(#cfgs)*
161+
#(#attrs)*
154162
#[doc(hidden)]
155163
#[export_name = "main"]
156164
pub unsafe extern "C" fn #tramp_ident() {
@@ -343,7 +351,15 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
343351
let tramp_ident = Ident::new(&format!("{}_trampoline", f.sig.ident), Span::call_site());
344352
let ident = &f.sig.ident;
345353

354+
if let Some(error) = check_for_blacklisted_attrs(&f.attrs) {
355+
return error;
356+
}
357+
358+
let (ref cfgs, ref attrs) = extract_cfgs(f.attrs.clone());
359+
346360
quote!(
361+
#(#cfgs)*
362+
#(#attrs)*
347363
#[doc(hidden)]
348364
#[export_name = #ident_s]
349365
pub unsafe extern "C" fn #tramp_ident() {
@@ -396,7 +412,15 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
396412
let tramp_ident = Ident::new(&format!("{}_trampoline", f.sig.ident), Span::call_site());
397413
let ident = &f.sig.ident;
398414

415+
if let Some(error) = check_for_blacklisted_attrs(&f.attrs) {
416+
return error;
417+
}
418+
419+
let (ref cfgs, ref attrs) = extract_cfgs(f.attrs.clone());
420+
399421
quote!(
422+
#(#cfgs)*
423+
#(#attrs)*
400424
#[doc(hidden)]
401425
#[export_name = "HardFault"]
402426
#[link_section = ".HardFault.user"]
@@ -481,7 +505,15 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
481505
})
482506
.collect::<Vec<_>>();
483507

508+
if let Some(error) = check_for_blacklisted_attrs(&f.attrs) {
509+
return error;
510+
}
511+
512+
let (ref cfgs, ref attrs) = extract_cfgs(f.attrs.clone());
513+
484514
quote!(
515+
#(#cfgs)*
516+
#(#attrs)*
485517
#[doc(hidden)]
486518
#[export_name = #ident_s]
487519
pub unsafe extern "C" fn #tramp_ident() {
@@ -650,7 +682,15 @@ pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream {
650682
})
651683
.collect::<Vec<_>>();
652684

685+
if let Some(error) = check_for_blacklisted_attrs(&f.attrs) {
686+
return error;
687+
}
688+
689+
let (ref cfgs, ref attrs) = extract_cfgs(f.attrs.clone());
690+
653691
quote!(
692+
#(#cfgs)*
693+
#(#attrs)*
654694
#[doc(hidden)]
655695
#[export_name = #ident_s]
656696
pub unsafe extern "C" fn #tramp_ident() {
@@ -790,6 +830,30 @@ fn extract_cfgs(attrs: Vec<Attribute>) -> (Vec<Attribute>, Vec<Attribute>) {
790830
(cfgs, not_cfgs)
791831
}
792832

833+
fn check_for_blacklisted_attrs(attrs: &[Attribute]) -> Option<TokenStream> {
834+
if let Some(val) = containts_blacklist_attrs(attrs) {
835+
return Some(parse::Error::new(val.span(), "This attribute is not allowed [blacklisted]")
836+
.to_compile_error()
837+
.into());
838+
}
839+
840+
None
841+
}
842+
843+
fn containts_blacklist_attrs(attrs: &[Attribute]) -> Option<Attribute> {
844+
let blacklist = &["inline", "export_name", "no_mangle", "must_use"];
845+
846+
for attr in attrs {
847+
for val in blacklist {
848+
if eq(&attr, &val) {
849+
return Some(attr.clone());
850+
}
851+
}
852+
}
853+
854+
None
855+
}
856+
793857
/// Returns `true` if `attr.path` matches `name`
794858
fn eq(attr: &Attribute, name: &str) -> bool {
795859
attr.style == AttrStyle::Outer && attr.path.is_ident(name)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
extern crate cortex_m_rt;
5+
extern crate panic_halt;
6+
7+
use cortex_m_rt::{entry, exception, interrupt};
8+
9+
#[inline] //~ ERROR This attribute is not allowed [blacklisted]
10+
#[entry]
11+
fn foo() -> ! {
12+
loop {}
13+
}
14+
15+
#[inline] //~ ERROR This attribute is not allowed [blacklisted]
16+
#[exception]
17+
fn SysTick() {}
18+
19+
#[allow(non_camel_case_types)]
20+
enum interrupt {
21+
USART1,
22+
}
23+
24+
#[inline] //~ ERROR This attribute is not allowed [blacklisted]
25+
#[interrupt]
26+
fn USART1() {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
extern crate cortex_m_rt;
5+
extern crate panic_halt;
6+
7+
use cortex_m_rt::{entry, exception, interrupt};
8+
9+
#[export_name = "not_allowed"] //~ ERROR This attribute is not allowed [blacklisted]
10+
#[entry]
11+
fn foo() -> ! {
12+
loop {}
13+
}
14+
15+
#[export_name = "not_allowed"] //~ ERROR This attribute is not allowed [blacklisted]
16+
#[exception]
17+
fn SysTick() {}
18+
19+
#[allow(non_camel_case_types)]
20+
enum interrupt {
21+
USART1,
22+
}
23+
24+
#[export_name = "not_allowed"] //~ ERROR This attribute is not allowed [blacklisted]
25+
#[interrupt]
26+
fn USART1() {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
extern crate cortex_m_rt;
5+
extern crate panic_halt;
6+
7+
use cortex_m_rt::{entry, exception, interrupt};
8+
9+
#[no_mangle] //~ ERROR This attribute is not allowed [blacklisted]
10+
#[entry]
11+
fn foo() -> ! {
12+
loop {}
13+
}
14+
15+
#[no_mangle] //~ ERROR This attribute is not allowed [blacklisted]
16+
#[exception]
17+
fn SysTick() {}
18+
19+
#[allow(non_camel_case_types)]
20+
enum interrupt {
21+
USART1,
22+
}
23+
24+
#[no_mangle] //~ ERROR This attribute is not allowed [blacklisted]
25+
#[interrupt]
26+
fn USART1() {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
extern crate cortex_m_rt;
5+
extern crate panic_halt;
6+
7+
use cortex_m_rt::{entry, exception, interrupt};
8+
9+
#[must_use] //~ ERROR This attribute is not allowed [blacklisted]
10+
#[entry]
11+
fn foo() -> ! {
12+
loop {}
13+
}
14+
15+
#[must_use] //~ ERROR This attribute is not allowed [blacklisted]
16+
#[exception]
17+
fn SysTick() {}
18+
19+
#[allow(non_camel_case_types)]
20+
enum interrupt {
21+
USART1,
22+
}
23+
24+
#[must_use] //~ ERROR This attribute is not allowed [blacklisted]
25+
#[interrupt]
26+
fn USART1() {}

0 commit comments

Comments
 (0)