Skip to content

Commit 135aa14

Browse files
committed
Get rid of random_ident
1 parent 9873225 commit 135aa14

File tree

2 files changed

+29
-59
lines changed

2 files changed

+29
-59
lines changed

macros/Cargo.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ proc-macro = true
1616
[dependencies]
1717
quote = "1.0.14"
1818
proc-macro2 = "1.0.36"
19-
rand_xoshiro = "0.6.0"
20-
21-
[dependencies.rand]
22-
default-features = false
23-
version = "0.8.4"
2419

2520
[dependencies.syn]
2621
features = ["extra-traits", "full"]

macros/src/lib.rs

Lines changed: 29 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
extern crate proc_macro;
22
extern crate proc_macro2;
33
extern crate quote;
4-
extern crate rand;
5-
extern crate rand_xoshiro;
64
extern crate syn;
75

86
use proc_macro::TokenStream;
9-
use std::{
10-
collections::HashSet,
11-
sync::atomic::{AtomicUsize, Ordering},
12-
time::{SystemTime, UNIX_EPOCH},
13-
};
7+
use std::collections::HashSet;
148

159
use proc_macro2::Span;
1610
use quote::{quote, quote_spanned};
17-
use rand::Rng;
18-
use rand_xoshiro::rand_core::SeedableRng;
1911
use syn::{
2012
parenthesized,
2113
parse::{self, Parse},
@@ -172,6 +164,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
172164
ReturnType::Type(_, ref ty) => matches!(**ty, Type::Never(_)),
173165
};
174166

167+
let fname = &f.sig.ident;
175168
let pair = match &interrupt_enable {
176169
Some(interrupt_enable) => interrupt_enable.extract_init_arg(&f.sig.inputs),
177170
None => extract_critical_section_arg(&f.sig.inputs),
@@ -181,7 +174,6 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
181174
// XXX should we blacklist other attributes?
182175
let attrs = f.attrs;
183176
let unsafety = f.sig.unsafety;
184-
let hash = random_ident();
185177
let (statics, stmts) = match extract_static_muts(f.block.stmts) {
186178
Err(e) => return e.to_compile_error().into(),
187179
Ok(x) => x,
@@ -220,12 +212,12 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
220212
#[no_mangle]
221213
#(#attrs)*
222214
pub #unsafety fn main() -> ! {
223-
#unsafety fn #hash<'a>(#fn_param) -> ! {
215+
#unsafety fn #fname<'a>(#fn_param) -> ! {
224216
#(#vars)*
225217
#(#stmts)*
226218
}
227219
#arg_def
228-
{ #hash(#arg_ident) }
220+
{ #fname(#arg_ident) }
229221
}
230222
)
231223
.into()
@@ -290,17 +282,15 @@ impl Parse for EntryInterruptEnable {
290282
impl EntryInterruptEnable {
291283
fn extract_init_arg(&self, list: &Punctuated<FnArg, Token![,]>) -> Result<ParamArgPair, ()> {
292284
if let Some(fn_name) = &self.pre_interrupt {
293-
let hash = random_ident();
294285
let fn_arg = Some(quote_spanned!(Span::mixed_site()=> {
295286
let cs = unsafe { msp430::interrupt::CriticalSection::new() };
296287

297288
// This struct forces the lifetime of the CriticalSection to match the lifetime of
298289
// the reference. Since the reference lifetime is restricted to this scope, the
299290
// compiler has to constrain the lifetime of the CriticalSection as well,
300291
// preventing the CriticalSection from being leaked as a return value.
301-
#[allow(non_camel_case_types)]
302-
struct #hash<'a>(&'a CriticalSection<'a>);
303-
let arg = #fn_name(*#hash(&cs).0);
292+
mod inner { pub struct Cs<'a>(pub &'a msp430::interrupt::CriticalSection<'a>); }
293+
let arg = #fn_name(*inner::Cs(&cs).0);
304294

305295
unsafe { msp430::interrupt::enable() };
306296
arg
@@ -485,18 +475,17 @@ pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream {
485475
.collect::<Vec<_>>();
486476

487477
let output = f.sig.output;
488-
let hash = random_ident();
489478
quote!(
490479
#[no_mangle]
491480
#(#attrs)*
492481
#unsafety extern "msp430-interrupt" fn #ident() {
493482
#check
494483

495-
#unsafety fn #hash<'a>(#fn_param) #output {
484+
#unsafety fn #ident<'a>(#fn_param) #output {
496485
#(#vars)*
497486
#(#stmts)*
498487
}
499-
{ #hash(#fn_arg) }
488+
{ #ident(#fn_arg) }
500489
}
501490
)
502491
.into()
@@ -629,41 +618,6 @@ fn extract_critical_section_arg(list: &Punctuated<FnArg, Token![,]>) -> Result<P
629618
Err(())
630619
}
631620

632-
// Creates a random identifier
633-
fn random_ident() -> Ident {
634-
static CALL_COUNT: AtomicUsize = AtomicUsize::new(0);
635-
636-
let secs = SystemTime::now()
637-
.duration_since(UNIX_EPOCH)
638-
.unwrap()
639-
.as_secs();
640-
641-
let count: u64 = CALL_COUNT.fetch_add(1, Ordering::SeqCst) as u64;
642-
let mut seed: [u8; 16] = [0; 16];
643-
644-
for (i, v) in seed.iter_mut().take(8).enumerate() {
645-
*v = ((secs >> (i * 8)) & 0xFF) as u8
646-
}
647-
648-
for (i, v) in seed.iter_mut().skip(8).enumerate() {
649-
*v = ((count >> (i * 8)) & 0xFF) as u8
650-
}
651-
652-
let mut rng = rand_xoshiro::Xoshiro128PlusPlus::from_seed(seed);
653-
Ident::new(
654-
&(0..16)
655-
.map(|i| {
656-
if i == 0 || rng.gen() {
657-
(b'a' + rng.gen::<u8>() % 25) as char
658-
} else {
659-
(b'0' + rng.gen::<u8>() % 10) as char
660-
}
661-
})
662-
.collect::<String>(),
663-
Span::call_site(),
664-
)
665-
}
666-
667621
/// Extracts `static mut` vars from the beginning of the given statements
668622
fn extract_static_muts(stmts: Vec<Stmt>) -> Result<(Vec<ItemStatic>, Vec<Stmt>), parse::Error> {
669623
let mut istmts = stmts.into_iter();
@@ -699,3 +653,24 @@ fn extract_static_muts(stmts: Vec<Stmt>) -> Result<(Vec<ItemStatic>, Vec<Stmt>),
699653

700654
Ok((statics, stmts))
701655
}
656+
657+
/// ``` no_run
658+
/// #![no_main]
659+
/// use msp430_rt_macros::{entry, interrupt};
660+
/// use msp430::interrupt::CriticalSection;
661+
///
662+
/// fn arg(cs: CriticalSection) -> u32 {
663+
/// arg(cs)
664+
/// }
665+
///
666+
/// #[entry(interrupt_enable(pre_interrupt = arg))]
667+
/// fn main(i: u32) -> ! {
668+
/// main(i)
669+
/// }
670+
/// ```
671+
#[cfg(doctest)]
672+
#[doc(hidden)]
673+
#[proc_macro]
674+
pub fn recursive_doc_test(t: TokenStream) -> TokenStream {
675+
t
676+
}

0 commit comments

Comments
 (0)