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:
bors[bot] 2020-04-22 14:18:32 +00:00 committed by GitHub
commit 7406f77a4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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) = let (const_app_init, root_init, user_init, call_init) =
init::codegen(core, app, analysis, extra); 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) = let (const_app_idle, root_idle, user_idle, call_idle) =
idle::codegen(core, app, analysis, extra); idle::codegen(core, app, analysis, extra);

View file

@ -44,8 +44,12 @@ pub fn codegen(
.iter() .iter()
.map(|name| { .map(|name| {
let ty = &app.late_resources[name].ty; 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<_>>() .collect::<Vec<_>>()
}) })

View file

@ -1,11 +1,13 @@
use proc_macro2::TokenStream as TokenStream2; use proc_macro2::TokenStream as TokenStream2;
use quote::quote; use quote::quote;
use rtfm_syntax::ast::App;
use crate::{analyze::Analysis, check::Extra, codegen::util}; use crate::{analyze::Analysis, check::Extra, codegen::util};
/// Generates code that runs after `#[init]` returns /// Generates code that runs after `#[init]` returns
pub fn codegen( pub fn codegen(
core: u8, core: u8,
app: &App,
analysis: &Analysis, analysis: &Analysis,
extra: &Extra, extra: &Extra,
) -> (Vec<TokenStream2>, Vec<TokenStream2>) { ) -> (Vec<TokenStream2>, Vec<TokenStream2>) {
@ -14,10 +16,16 @@ pub fn codegen(
// initialize late resources // initialize late resources
if let Some(late_resources) = analysis.late_resources.get(&core) { if let Some(late_resources) = analysis.late_resources.get(&core) {
for name in late_resources { for name in late_resources {
// if it's live // if it's live
let cfgs = app.late_resources[name].cfgs.clone();
if analysis.locations.get(name).is_some() { 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() { let ptr = if expr.is_none() {
quote!(#name.as_mut_ptr()) quote!(
#(#cfgs)*
#name.as_mut_ptr()
)
} else { } else {
quote!(&mut #name) quote!(
#(#cfgs)*
&mut #name
)
}; };
const_app.push(util::impl_mutex( const_app.push(util::impl_mutex(