Provide the way to override the panic handler.

This commit is contained in:
Zhang Junyang
2024-09-25 18:04:44 +08:00
committed by Tate, Hongliang Tian
parent 3c857d746e
commit 131a25c15c
12 changed files with 129 additions and 74 deletions

View File

@ -31,8 +31,6 @@ pub fn main(_attr: TokenStream, item: TokenStream) -> TokenStream {
#[no_mangle]
#[linkage = "weak"]
extern "Rust" fn __ostd_main() -> ! {
// SAFETY: The function is called only once on the BSP.
unsafe { ostd::init() };
#main_fn_name();
ostd::prelude::abort();
}
@ -58,8 +56,6 @@ pub fn test_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
quote!(
#[no_mangle]
extern "Rust" fn __ostd_main() -> ! {
// SAFETY: The function is called only once on the BSP.
unsafe { ostd::init() };
#main_fn_name();
ostd::prelude::abort();
}
@ -69,6 +65,49 @@ pub fn test_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
.into()
}
/// A macro attribute for the panic handler.
///
/// The attributed function will be used to override OSTD's default
/// implementation of Rust's `#[panic_handler]`. The function takes a single
/// parameter of type `&core::panic::PanicInfo` and does not return.
#[proc_macro_attribute]
pub fn panic_handler(_attr: TokenStream, item: TokenStream) -> TokenStream {
let handler_fn = parse_macro_input!(item as ItemFn);
let handler_fn_name = &handler_fn.sig.ident;
quote!(
#[cfg(not(ktest))]
#[no_mangle]
extern "Rust" fn __ostd_panic_handler(info: &core::panic::PanicInfo) -> ! {
#handler_fn_name(info);
}
#[cfg(not(ktest))]
#handler_fn
)
.into()
}
/// A macro attribute for the panic handler.
///
/// This macro is used for internal OSDK implementation. Do not use it
/// directly.
#[proc_macro_attribute]
pub fn test_panic_handler(_attr: TokenStream, item: TokenStream) -> TokenStream {
let handler_fn = parse_macro_input!(item as ItemFn);
let handler_fn_name = &handler_fn.sig.ident;
quote!(
#[no_mangle]
extern "Rust" fn __ostd_panic_handler(info: &core::panic::PanicInfo) -> ! {
#handler_fn_name(info);
}
#handler_fn
)
.into()
}
/// The test attribute macro to mark a test function.
///
/// # Example