From 577d188f72398461650a4e76d81396bf0abea0f4 Mon Sep 17 00:00:00 2001 From: Hugo van der Wijst Date: Sun, 10 Feb 2019 15:25:33 -0800 Subject: [PATCH] Make generated names stable when sorting. --- ci/script.sh | 12 +++++++----- macros/src/codegen.rs | 35 ++++++++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/ci/script.sh b/ci/script.sh index 1bb0ae2edb..4b9cd22d39 100644 --- a/ci/script.sh +++ b/ci/script.sh @@ -27,13 +27,15 @@ arm_example() { else cargo $COMMAND $CARGO_FLAGS fi - arm-none-eabi-objcopy -O ihex target/$TARGET/$BUILD_MODE/examples/$EXAMPLE ${EXAMPLE}_${FEATURES_STR}${BUILD_MODE}_${BUILD_NUM}.hex + arm-none-eabi-objcopy -O ihex target/$TARGET/$BUILD_MODE/examples/$EXAMPLE ci/builds/${EXAMPLE}_${FEATURES_STR}${BUILD_MODE}_${BUILD_NUM}.hex } main() { local T=$TARGET + mkdir -p ci/builds + if [ $T = x86_64-unknown-linux-gnu ]; then # compile-fail and compile-pass tests case $TRAVIS_RUST_VERSION in @@ -138,16 +140,16 @@ main() { if [ $ex != types ]; then arm_example "build" $ex "debug" "" "2" - cmp ${ex}_debug_1.hex ${ex}_debug_2.hex + cmp ci/builds/${ex}_debug_1.hex ci/builds/${ex}_debug_2.hex arm_example "build" $ex "release" "" "2" - cmp ${ex}_release_1.hex ${ex}_release_2.hex + cmp ci/builds/${ex}_release_1.hex ci/builds/${ex}_release_2.hex fi if [ $TARGET != thumbv6m-none-eabi ]; then arm_example "build" $ex "debug" "timer-queue" "2" - cmp ${ex}_timer-queue_debug_1.hex ${ex}_timer-queue_debug_2.hex + cmp ci/builds/${ex}_timer-queue_debug_1.hex ci/builds/${ex}_timer-queue_debug_2.hex arm_example "build" $ex "release" "timer-queue" "2" - cmp ${ex}_timer-queue_release_1.hex ${ex}_timer-queue_release_2.hex + cmp ci/builds/${ex}_timer-queue_release_1.hex ci/builds/${ex}_timer-queue_release_2.hex fi done esac diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index 9fc0e271c4..e2e5ac44ad 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -3,6 +3,7 @@ use proc_macro::TokenStream; use std::{ collections::{BTreeMap, HashMap}, + time::{SystemTime, UNIX_EPOCH}, }; use proc_macro2::Span; @@ -1993,30 +1994,48 @@ fn mk_typenum_capacity(capacity: u8, power_of_two: bool) -> proc_macro2::TokenSt } struct IdentGenerator { + call_count: u32, rng: rand::rngs::SmallRng, } impl IdentGenerator { fn new() -> IdentGenerator { - let crate_name = env!("CARGO_PKG_NAME"); - let seed = [0u8; 16]; - for (i, b) in crate_name.bytes().enumerate() { - seed[i%seed.len()].wrapping_add(b); + let elapsed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); + + let secs = elapsed.as_secs(); + let nanos = elapsed.subsec_nanos(); + + let mut seed: [u8; 16] = [0; 16]; + + for (i, v) in seed.iter_mut().take(8).enumerate() { + *v = ((secs >> (i * 8)) & 0xFF) as u8 + } + + for (i, v) in seed.iter_mut().skip(8).take(4).enumerate() { + *v = ((nanos >> (i * 8)) & 0xFF) as u8 + } + + let rng = rand::rngs::SmallRng::from_seed(seed); + + IdentGenerator { + call_count: 0, + rng, } - IdentGenerator { rng: rand::rngs::SmallRng::from_seed(seed) } } fn mk_ident(&mut self, name: Option<&str>) -> Ident { let n; - let mut s = if let Some(name) = name { + let s = if let Some(name) = name { n = 4; format!("{}_", name) } else { let crate_name = env!("CARGO_PKG_NAME").replace("-", "_").to_lowercase(); - n = 16; + n = 4; format!("{}__internal__", crate_name) }; + let mut s = format!("{}{}_", s, self.call_count); + for i in 0..n { if i == 0 || self.rng.gen() { s.push(('a' as u8 + self.rng.gen::() % 25) as char) @@ -2025,6 +2044,8 @@ impl IdentGenerator { } } + self.call_count += 1; + Ident::new(&s, Span::call_site()) } }