mirror of
https://github.com/rtic-rs/rtic.git
synced 2024-12-24 19:09:33 +01:00
Merge #306
306: Retain cfg-attributes on resources r=korken89 a=AfoHT When rust 1.43 lands as stable this will resolve #301 and allow for the kind of conditional compilation exemplified in the issue. Tested on beta and nightly. Co-authored-by: Henrik Tjäder <henrik@tjaders.com>
This commit is contained in:
commit
7406f77a4e
5 changed files with 63 additions and 5 deletions
40
examples/t-cfg-resources.rs
Normal file
40
examples/t-cfg-resources.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
//! [compile-pass] check that `#[cfg]` attributes applied on resources work
|
||||
//!
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
use panic_halt as _;
|
||||
|
||||
#[cfg(rustc_is_nightly)]
|
||||
mod example {
|
||||
|
||||
#[rtfm::app(device = lm3s6965)]
|
||||
const APP: () = {
|
||||
struct Resources {
|
||||
// A resource
|
||||
#[init(0)]
|
||||
shared: u32,
|
||||
|
||||
// A conditionally compiled resource behind feature_x
|
||||
#[cfg(feature = "feature_x")]
|
||||
x: u32,
|
||||
|
||||
dummy: (),
|
||||
}
|
||||
|
||||
#[init]
|
||||
fn init(_: init::Context) -> init::LateResources {
|
||||
init::LateResources {
|
||||
// The feature needs to be applied everywhere x is defined or used
|
||||
#[cfg(feature = "feature_x")]
|
||||
x: 0,
|
||||
dummy: () // dummy such that we have at least one late resource
|
||||
}
|
||||
}
|
||||
|
||||
#[idle]
|
||||
fn idle(_cx: idle::Context) -> ! {
|
||||
loop {}
|
||||
}
|
||||
};
|
||||
}
|
|
@ -39,7 +39,7 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 {
|
|||
let (const_app_init, root_init, user_init, call_init) =
|
||||
init::codegen(core, app, analysis, extra);
|
||||
|
||||
let (const_app_post_init, post_init_stmts) = post_init::codegen(core, analysis, extra);
|
||||
let (const_app_post_init, post_init_stmts) = post_init::codegen(core, &app, analysis, extra);
|
||||
|
||||
let (const_app_idle, root_idle, user_idle, call_idle) =
|
||||
idle::codegen(core, app, analysis, extra);
|
||||
|
|
|
@ -44,8 +44,12 @@ pub fn codegen(
|
|||
.iter()
|
||||
.map(|name| {
|
||||
let ty = &app.late_resources[name].ty;
|
||||
let cfgs = &app.late_resources[name].cfgs;
|
||||
|
||||
quote!(pub #name: #ty)
|
||||
quote!(
|
||||
#(#cfgs)*
|
||||
pub #name: #ty
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::quote;
|
||||
use rtfm_syntax::ast::App;
|
||||
|
||||
use crate::{analyze::Analysis, check::Extra, codegen::util};
|
||||
|
||||
/// Generates code that runs after `#[init]` returns
|
||||
pub fn codegen(
|
||||
core: u8,
|
||||
app: &App,
|
||||
analysis: &Analysis,
|
||||
extra: &Extra,
|
||||
) -> (Vec<TokenStream2>, Vec<TokenStream2>) {
|
||||
|
@ -14,10 +16,16 @@ pub fn codegen(
|
|||
|
||||
// initialize late resources
|
||||
if let Some(late_resources) = analysis.late_resources.get(&core) {
|
||||
|
||||
for name in late_resources {
|
||||
// if it's live
|
||||
let cfgs = app.late_resources[name].cfgs.clone();
|
||||
if analysis.locations.get(name).is_some() {
|
||||
stmts.push(quote!(#name.as_mut_ptr().write(late.#name);));
|
||||
// Need to also include the cfgs
|
||||
stmts.push(quote!(
|
||||
#(#cfgs)*
|
||||
#name.as_mut_ptr().write(late.#name);
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -101,9 +101,15 @@ pub fn codegen(
|
|||
));
|
||||
|
||||
let ptr = if expr.is_none() {
|
||||
quote!(#name.as_mut_ptr())
|
||||
quote!(
|
||||
#(#cfgs)*
|
||||
#name.as_mut_ptr()
|
||||
)
|
||||
} else {
|
||||
quote!(&mut #name)
|
||||
quote!(
|
||||
#(#cfgs)*
|
||||
&mut #name
|
||||
)
|
||||
};
|
||||
|
||||
const_app.push(util::impl_mutex(
|
||||
|
|
Loading…
Reference in a new issue