Skip to content

Commit 4bd25de

Browse files
Merge pull request #336 from bjoernQ/allow-extern-c
Allow `extern "C"`
2 parents fcde9ff + a52caf2 commit 4bd25de

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

riscv-rt/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
4444
`a2` is preserved in `a5`, as there are only two callee-saved registers.
4545
New documentation of startup functions (`_mp_hook` and `__pre_init`) now provide
4646
additional implementation guidelines to ensure a correct behavior of the runtime.
47+
- Allow `extern "C"` functions for exceptions, core-interrupts and external-interrupts.
4748

4849
### Removed
4950

riscv-rt/macros/src/lib.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,8 @@ impl RiscvPacItem {
688688

689689
fn valid_signature(&self) -> &str {
690690
match self {
691-
Self::Exception => "`[unsafe] fn([&[mut] riscv_rt::TrapFrame]) [-> !]`",
692-
_ => "`[unsafe] fn() [-> !]`",
691+
Self::Exception => "`[unsafe] [extern \"C\"] fn([&[mut] riscv_rt::TrapFrame]) [-> !]`",
692+
_ => "`[unsafe] [extern \"C\"] fn() [-> !]`",
693693
}
694694
}
695695

@@ -719,7 +719,14 @@ impl RiscvPacItem {
719719
&& f.sig.constness.is_none()
720720
&& f.sig.asyncness.is_none()
721721
&& f.vis == Visibility::Inherited
722-
&& f.sig.abi.is_none()
722+
&& match &f.sig.abi {
723+
None => true,
724+
Some(syn::Abi {
725+
extern_token: _,
726+
name: Some(name),
727+
}) if name.value() == "C" => true,
728+
_ => false,
729+
}
723730
&& f.sig.generics.params.is_empty()
724731
&& f.sig.generics.where_clause.is_none()
725732
&& f.sig.variadic.is_none()
@@ -741,7 +748,7 @@ impl RiscvPacItem {
741748
#[proc_macro_attribute]
742749
/// Attribute to declare an exception handler.
743750
///
744-
/// The function must have the signature `[unsafe] fn([&[mut] riscv_rt::TrapFrame]) [-> !]`.
751+
/// The function must have the signature `[unsafe] [extern "C"] fn([&[mut] riscv_rt::TrapFrame]) [-> !]`.
745752
///
746753
/// The argument of the macro must be a path to a variant of an enum that implements the `riscv_rt::ExceptionNumber` trait.
747754
///
@@ -760,7 +767,7 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
760767
#[proc_macro_attribute]
761768
/// Attribute to declare a core interrupt handler.
762769
///
763-
/// The function must have the signature `[unsafe] fn() [-> !]`.
770+
/// The function must have the signature `[unsafe] [extern "C"] fn() [-> !]`.
764771
///
765772
/// The argument of the macro must be a path to a variant of an enum that implements the `riscv_rt::CoreInterruptNumber` trait.
766773
///
@@ -787,7 +794,7 @@ pub fn core_interrupt(args: TokenStream, input: TokenStream) -> TokenStream {
787794
#[proc_macro_attribute]
788795
/// Attribute to declare an external interrupt handler.
789796
///
790-
/// The function must have the signature `[unsafe] fn() [-> !]`.
797+
/// The function must have the signature `[unsafe] [extern "C"] fn() [-> !]`.
791798
///
792799
/// The argument of the macro must be a path to a variant of an enum that implements the `riscv_rt::ExternalInterruptNumber` trait.
793800
///

tests-trybuild/tests/riscv-rt/core_interrupt/fail_signatures.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
error: `#[core_interrupt]` function must have signature `[unsafe] fn() [-> !]`
1+
error: `#[core_interrupt]` function must have signature `[unsafe] [extern "C"] fn() [-> !]`
22
--> tests/riscv-rt/core_interrupt/fail_signatures.rs:2:1
33
|
44
2 | fn my_interrupt(code: usize) {}
55
| ^^
66

7-
error: `#[core_interrupt]` function must have signature `[unsafe] fn() [-> !]`
7+
error: `#[core_interrupt]` function must have signature `[unsafe] [extern "C"] fn() [-> !]`
88
--> tests/riscv-rt/core_interrupt/fail_signatures.rs:5:1
99
|
1010
5 | fn my_other_interrupt() -> usize {}
1111
| ^^
1212

13-
error: `#[core_interrupt]` function must have signature `[unsafe] fn() [-> !]`
13+
error: `#[core_interrupt]` function must have signature `[unsafe] [extern "C"] fn() [-> !]`
1414
--> tests/riscv-rt/core_interrupt/fail_signatures.rs:8:1
1515
|
1616
8 | async fn my_async_interrupt() {}

tests-trybuild/tests/riscv-rt/exception/fail_signatures.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
error: `#[exception]` function must have signature `[unsafe] fn([&[mut] riscv_rt::TrapFrame]) [-> !]`
1+
error: `#[exception]` function must have signature `[unsafe] [extern "C"] fn([&[mut] riscv_rt::TrapFrame]) [-> !]`
22
--> tests/riscv-rt/exception/fail_signatures.rs:2:1
33
|
44
2 | fn my_exception(code: usize) {}
55
| ^^
66

7-
error: `#[exception]` function must have signature `[unsafe] fn([&[mut] riscv_rt::TrapFrame]) [-> !]`
7+
error: `#[exception]` function must have signature `[unsafe] [extern "C"] fn([&[mut] riscv_rt::TrapFrame]) [-> !]`
88
--> tests/riscv-rt/exception/fail_signatures.rs:5:1
99
|
1010
5 | fn my_other_exception(trap_frame: &riscv_rt::TrapFrame, code: usize) {}
1111
| ^^
1212

13-
error: `#[exception]` function must have signature `[unsafe] fn([&[mut] riscv_rt::TrapFrame]) [-> !]`
13+
error: `#[exception]` function must have signature `[unsafe] [extern "C"] fn([&[mut] riscv_rt::TrapFrame]) [-> !]`
1414
--> tests/riscv-rt/exception/fail_signatures.rs:8:1
1515
|
1616
8 | async fn my_async_exception(trap_frame: &riscv_rt::TrapFrame, code: usize) {}

tests-trybuild/tests/riscv-rt/external_interrupt/fail_signatures.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
error: `#[external_interrupt]` function must have signature `[unsafe] fn() [-> !]`
1+
error: `#[external_interrupt]` function must have signature `[unsafe] [extern "C"] fn() [-> !]`
22
--> tests/riscv-rt/external_interrupt/fail_signatures.rs:31:1
33
|
44
31 | fn my_interrupt() -> usize {}
55
| ^^
66

7-
error: `#[external_interrupt]` function must have signature `[unsafe] fn() [-> !]`
7+
error: `#[external_interrupt]` function must have signature `[unsafe] [extern "C"] fn() [-> !]`
88
--> tests/riscv-rt/external_interrupt/fail_signatures.rs:34:1
99
|
1010
34 | fn my_other_interrupt(code: usize) -> usize {}
1111
| ^^
1212

13-
error: `#[external_interrupt]` function must have signature `[unsafe] fn() [-> !]`
13+
error: `#[external_interrupt]` function must have signature `[unsafe] [extern "C"] fn() [-> !]`
1414
--> tests/riscv-rt/external_interrupt/fail_signatures.rs:37:1
1515
|
1616
37 | async fn my_async_interrupt(code: usize) -> usize {}

0 commit comments

Comments
 (0)