Skip to content

Commit 540d1de

Browse files
committed
Added more tests and fixed a panic
1 parent 54877ba commit 540d1de

File tree

3 files changed

+62
-18
lines changed

3 files changed

+62
-18
lines changed

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

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ extern crate proc_macro;
77
use proc_macro::TokenStream;
88
use proc_macro2::Span;
99
use quote::quote;
10-
use std::collections::HashSet;
1110
use std::iter;
11+
use std::{collections::HashSet, fmt::Display};
1212
use syn::{
1313
parse::{self, Parse},
1414
parse_macro_input,
@@ -121,6 +121,17 @@ enum Exception {
121121
Other,
122122
}
123123

124+
impl Display for Exception {
125+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
126+
match self {
127+
Exception::DefaultHandler => write!(f, "`DefaultHandler`"),
128+
Exception::HardFault(_) => write!(f, "`HardFault` handler"),
129+
Exception::NonMaskableInt => write!(f, "`NonMaskableInt` handler"),
130+
Exception::Other => write!(f, "Other exception handler"),
131+
}
132+
}
133+
}
134+
124135
#[derive(Debug, PartialEq)]
125136
struct HardFaultArgs {
126137
trampoline: bool,
@@ -212,14 +223,16 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
212223
// NOTE that at this point we don't check if the exception is available on the target (e.g.
213224
// MemoryManagement is not available on Cortex-M0)
214225
"MemoryManagement" | "BusFault" | "UsageFault" | "SecureFault" | "SVCall"
215-
| "DebugMonitor" | "PendSV" | "SysTick" => Exception::Other,
216-
_ => {
226+
| "DebugMonitor" | "PendSV" | "SysTick" => {
217227
if !args.is_empty() {
218228
return parse::Error::new(Span::call_site(), "This attribute accepts no arguments")
219229
.to_compile_error()
220230
.into();
221231
}
222232

233+
Exception::Other
234+
}
235+
_ => {
223236
return parse::Error::new(ident.span(), "This is not a valid exception name")
224237
.to_compile_error()
225238
.into();
@@ -230,11 +243,7 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
230243
match exn {
231244
Exception::DefaultHandler | Exception::HardFault(_) | Exception::NonMaskableInt => {
232245
// These are unsafe to define.
233-
let name = if exn == Exception::DefaultHandler {
234-
"`DefaultHandler`".to_string()
235-
} else {
236-
format!("`{:?}` handler", exn)
237-
};
246+
let name = format!("{}", exn);
238247
return parse::Error::new(ident.span(), format_args!("defining a {} is unsafe and requires an `unsafe fn` (see the cortex-m-rt docs)", name))
239248
.to_compile_error()
240249
.into();
@@ -312,17 +321,16 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
312321
&& f.vis == Visibility::Inherited
313322
&& f.sig.abi.is_none()
314323
&& if args.trampoline {
315-
match &f.sig.inputs[0] {
316-
FnArg::Typed(arg) => match arg.ty.as_ref() {
317-
Type::Reference(r) => {
318-
r.lifetime.is_none()
319-
&& r.mutability.is_none()
320-
&& f.sig.inputs.len() == 1
321-
}
324+
f.sig.inputs.len() == 1
325+
&& match &f.sig.inputs[0] {
326+
FnArg::Typed(arg) => match arg.ty.as_ref() {
327+
Type::Reference(r) => {
328+
r.lifetime.is_none() && r.mutability.is_none()
329+
}
330+
_ => false,
331+
},
322332
_ => false,
323-
},
324-
_ => false,
325-
}
333+
}
326334
} else {
327335
f.sig.inputs.is_empty()
328336
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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, ExceptionFrame};
8+
9+
#[entry]
10+
fn foo() -> ! {
11+
loop {}
12+
}
13+
14+
#[exception(trampoline = true)]
15+
unsafe fn HardFault() -> ! {
16+
//~^ ERROR `HardFault` handler must have signature `unsafe fn(&ExceptionFrame) -> !`
17+
loop {}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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, ExceptionFrame};
8+
9+
#[entry]
10+
fn foo() -> ! {
11+
loop {}
12+
}
13+
14+
#[exception(trampoline = false)]
15+
unsafe fn HardFault(_ef: &ExceptionFrame) -> ! {
16+
//~^ ERROR `HardFault` handler must have signature `unsafe fn() -> !`
17+
loop {}
18+
}

0 commit comments

Comments
 (0)