Make generated names stable when sorting.

This commit is contained in:
Hugo van der Wijst 2019-02-10 15:25:33 -08:00 committed by Jorge Aparicio
parent a654d13eef
commit 577d188f72
2 changed files with 35 additions and 12 deletions

View file

@ -27,13 +27,15 @@ arm_example() {
else else
cargo $COMMAND $CARGO_FLAGS cargo $COMMAND $CARGO_FLAGS
fi 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() { main() {
local T=$TARGET local T=$TARGET
mkdir -p ci/builds
if [ $T = x86_64-unknown-linux-gnu ]; then if [ $T = x86_64-unknown-linux-gnu ]; then
# compile-fail and compile-pass tests # compile-fail and compile-pass tests
case $TRAVIS_RUST_VERSION in case $TRAVIS_RUST_VERSION in
@ -138,16 +140,16 @@ main() {
if [ $ex != types ]; then if [ $ex != types ]; then
arm_example "build" $ex "debug" "" "2" 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" 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 fi
if [ $TARGET != thumbv6m-none-eabi ]; then if [ $TARGET != thumbv6m-none-eabi ]; then
arm_example "build" $ex "debug" "timer-queue" "2" 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" 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 fi
done done
esac esac

View file

@ -3,6 +3,7 @@
use proc_macro::TokenStream; use proc_macro::TokenStream;
use std::{ use std::{
collections::{BTreeMap, HashMap}, collections::{BTreeMap, HashMap},
time::{SystemTime, UNIX_EPOCH},
}; };
use proc_macro2::Span; use proc_macro2::Span;
@ -1993,30 +1994,48 @@ fn mk_typenum_capacity(capacity: u8, power_of_two: bool) -> proc_macro2::TokenSt
} }
struct IdentGenerator { struct IdentGenerator {
call_count: u32,
rng: rand::rngs::SmallRng, rng: rand::rngs::SmallRng,
} }
impl IdentGenerator { impl IdentGenerator {
fn new() -> IdentGenerator { fn new() -> IdentGenerator {
let crate_name = env!("CARGO_PKG_NAME"); let elapsed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
let seed = [0u8; 16];
for (i, b) in crate_name.bytes().enumerate() { let secs = elapsed.as_secs();
seed[i%seed.len()].wrapping_add(b); 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 { fn mk_ident(&mut self, name: Option<&str>) -> Ident {
let n; let n;
let mut s = if let Some(name) = name { let s = if let Some(name) = name {
n = 4; n = 4;
format!("{}_", name) format!("{}_", name)
} else { } else {
let crate_name = env!("CARGO_PKG_NAME").replace("-", "_").to_lowercase(); let crate_name = env!("CARGO_PKG_NAME").replace("-", "_").to_lowercase();
n = 16; n = 4;
format!("{}__internal__", crate_name) format!("{}__internal__", crate_name)
}; };
let mut s = format!("{}{}_", s, self.call_count);
for i in 0..n { for i in 0..n {
if i == 0 || self.rng.gen() { if i == 0 || self.rng.gen() {
s.push(('a' as u8 + self.rng.gen::<u8>() % 25) as char) s.push(('a' as u8 + self.rng.gen::<u8>() % 25) as char)
@ -2025,6 +2044,8 @@ impl IdentGenerator {
} }
} }
self.call_count += 1;
Ident::new(&s, Span::call_site()) Ident::new(&s, Span::call_site())
} }
} }