Skip to content

Commit 5fdbab9

Browse files
committed
Updated per comments
1 parent 2f5a648 commit 5fdbab9

File tree

6 files changed

+43
-26
lines changed

6 files changed

+43
-26
lines changed

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

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

153-
if let Some(error) = check_for_blacklisted_attrs(&f.attrs) {
153+
if let Err(error) = check_attr_whitelist(&f.attrs) {
154154
return error;
155155
}
156156

@@ -351,7 +351,7 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
351351
let tramp_ident = Ident::new(&format!("{}_trampoline", f.sig.ident), Span::call_site());
352352
let ident = &f.sig.ident;
353353

354-
if let Some(error) = check_for_blacklisted_attrs(&f.attrs) {
354+
if let Err(error) = check_attr_whitelist(&f.attrs) {
355355
return error;
356356
}
357357

@@ -412,7 +412,7 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
412412
let tramp_ident = Ident::new(&format!("{}_trampoline", f.sig.ident), Span::call_site());
413413
let ident = &f.sig.ident;
414414

415-
if let Some(error) = check_for_blacklisted_attrs(&f.attrs) {
415+
if let Err(error) = check_attr_whitelist(&f.attrs) {
416416
return error;
417417
}
418418

@@ -505,7 +505,7 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
505505
})
506506
.collect::<Vec<_>>();
507507

508-
if let Some(error) = check_for_blacklisted_attrs(&f.attrs) {
508+
if let Err(error) = check_attr_whitelist(&f.attrs) {
509509
return error;
510510
}
511511

@@ -682,7 +682,7 @@ pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream {
682682
})
683683
.collect::<Vec<_>>();
684684

685-
if let Some(error) = check_for_blacklisted_attrs(&f.attrs) {
685+
if let Err(error) = check_attr_whitelist(&f.attrs) {
686686
return error;
687687
}
688688

@@ -830,23 +830,8 @@ fn extract_cfgs(attrs: Vec<Attribute>) -> (Vec<Attribute>, Vec<Attribute>) {
830830
(cfgs, not_cfgs)
831831
}
832832

833-
fn check_for_blacklisted_attrs(attrs: &[Attribute]) -> Option<TokenStream> {
834-
if let Some(val) = containts_blacklist_attrs(attrs) {
835-
return Some(
836-
parse::Error::new(
837-
val.span(),
838-
"this attribute is not allowed on a function controlled by cortex-m-rt",
839-
)
840-
.to_compile_error()
841-
.into(),
842-
);
843-
}
844-
845-
None
846-
}
847-
848-
fn containts_blacklist_attrs(attrs: &[Attribute]) -> Option<Attribute> {
849-
let whitelist = &["doc", "link_section"];
833+
fn check_attr_whitelist(attrs: &[Attribute]) -> Result<(), TokenStream> {
834+
let whitelist = &["doc", "link_section", "cfg", "allow", "warn", "deny", "forbid", "cold"];
850835

851836
'o: for attr in attrs {
852837
for val in whitelist {
@@ -855,10 +840,18 @@ fn containts_blacklist_attrs(attrs: &[Attribute]) -> Option<Attribute> {
855840
}
856841
}
857842

858-
return Some(attr.clone());
859-
}
843+
return Err(
844+
parse::Error::new(
845+
attr.span(),
846+
"this attribute is not allowed on a function controlled by cortex-m-rt",
847+
)
848+
.to_compile_error()
849+
.into(),
850+
);
851+
852+
};
860853

861-
None
854+
Ok(())
862855
}
863856

864857
/// Returns `true` if `attr.path` matches `name`

cortex-m-rt/tests/compile-fail/blacklist-1.rs renamed to cortex-m-rt/tests/compile-fail/whitelist-1.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ fn SysTick() {}
1919
#[allow(non_camel_case_types)]
2020
enum interrupt {
2121
USART1,
22+
USART2,
2223
}
2324

2425
#[inline] //~ ERROR this attribute is not allowed on a function controlled by cortex-m-rt
2526
#[interrupt]
2627
fn USART1() {}
28+
29+
#[cfg(feature = "device")]
30+
#[cfg_attr(feature = "device", inline)] //~ ERROR this attribute is not allowed on a function controlled by cortex-m-rt
31+
#[interrupt]
32+
fn USART2() {}

cortex-m-rt/tests/compile-fail/blacklist-2.rs renamed to cortex-m-rt/tests/compile-fail/whitelist-2.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ fn SysTick() {}
1919
#[allow(non_camel_case_types)]
2020
enum interrupt {
2121
USART1,
22+
USART2,
2223
}
2324

2425
#[export_name = "not_allowed"] //~ ERROR this attribute is not allowed on a function controlled by cortex-m-rt
2526
#[interrupt]
2627
fn USART1() {}
28+
29+
#[cfg(feature = "device")]
30+
#[cfg_attr(feature = "device", export_name = "not_allowed")] //~ ERROR this attribute is not allowed on a function controlled by cortex-m-rt
31+
#[interrupt]
32+
fn USART2() {}

cortex-m-rt/tests/compile-fail/blacklist-3.rs renamed to cortex-m-rt/tests/compile-fail/whitelist-3.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ fn SysTick() {}
1919
#[allow(non_camel_case_types)]
2020
enum interrupt {
2121
USART1,
22+
USART2,
2223
}
2324

2425
#[no_mangle] //~ ERROR this attribute is not allowed on a function controlled by cortex-m-rt
2526
#[interrupt]
2627
fn USART1() {}
28+
29+
#[cfg(feature = "device")]
30+
#[cfg_attr(feature = "device", no_mangle)] //~ ERROR this attribute is not allowed on a function controlled by cortex-m-rt
31+
#[interrupt]
32+
fn USART2() {}

cortex-m-rt/tests/compile-fail/blacklist-4.rs renamed to cortex-m-rt/tests/compile-fail/whitelist-4.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ fn SysTick() {}
1919
#[allow(non_camel_case_types)]
2020
enum interrupt {
2121
USART1,
22+
USART2,
2223
}
2324

2425
#[must_use] //~ ERROR this attribute is not allowed on a function controlled by cortex-m-rt
2526
#[interrupt]
2627
fn USART1() {}
28+
29+
#[cfg(feature = "device")]
30+
#[cfg_attr(feature = "device", must_use)] //~ ERROR this attribute is not allowed on a function controlled by cortex-m-rt
31+
#[interrupt]
32+
fn USART2() {}

cortex-m-rt/tests/compiletest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn run_mode(mode: &'static str) {
99
config.src_base = PathBuf::from(format!("tests/{}", mode));
1010
// config.link_deps(); // Populate config.target_rustcflags with dependencies on the path
1111
config.target_rustcflags =
12-
Some("-L target/debug -L target/debug/deps -C panic=abort".to_owned());
12+
Some("-L target/debug -L target/debug/deps -C panic=abort --cfg feature=\"device\"".to_owned());
1313
// config.clean_rmeta(); // If your tests import the parent crate, this helps with E0464
1414

1515
compiletest::run_tests(&config);

0 commit comments

Comments
 (0)