Retain cfg-attributes on resources

This commit is contained in:
Henrik Tjäder 2020-04-08 14:43:47 +00:00
parent bb59606b7c
commit f58f37b2b9
5 changed files with 63 additions and 5 deletions

View 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 {}
}
};
}

View file

@ -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);

View file

@ -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<_>>()
})

View file

@ -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);
));
}
}
}

View file

@ -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(