aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / syn / tests / test_round_trip.rs
| 1 | #![cfg(not(syn_disable_nightly_tests))] |
| 2 | #![cfg(not(miri))] |
| 3 | #![recursion_limit = "1024"] |
| 4 | #![feature(rustc_private)] |
| 5 | #![allow( |
| 6 | clippy::assert_is_empty, |
| 7 | clippy::blocks_in_conditions, |
| 8 | clippy::elidable_lifetime_names, |
| 9 | clippy::manual_assert, |
| 10 | clippy::manual_let_else, |
| 11 | clippy::match_like_matches_macro, |
| 12 | clippy::needless_lifetimes, |
| 13 | clippy::uninlined_format_args |
| 14 | )] |
| 15 | #![allow(mismatched_lifetime_syntaxes)] |
| 16 | |
| 17 | extern crate rustc_ast; |
| 18 | extern crate rustc_ast_pretty; |
| 19 | extern crate rustc_data_structures; |
| 20 | extern crate rustc_driver; |
| 21 | extern crate rustc_error_messages; |
| 22 | extern crate rustc_errors; |
| 23 | extern crate rustc_expand; |
| 24 | extern crate rustc_parse; |
| 25 | extern crate rustc_session; |
| 26 | extern crate rustc_span; |
| 27 | |
| 28 | use crate::common::eq::SpanlessEq; |
| 29 | use quote::quote; |
| 30 | use rustc_ast::ast::{ |
| 31 | AngleBracketedArg, Crate, GenericArg, GenericArgs, GenericParamKind, Generics, |
| 32 | }; |
| 33 | use rustc_ast::mut_visit::{self, MutVisitor}; |
| 34 | use rustc_ast_pretty::pprust; |
| 35 | use rustc_data_structures::flat_map_in_place::FlatMapInPlace; |
| 36 | use rustc_error_messages::DiagMessage; |
| 37 | use rustc_errors::{Diag, PResult}; |
| 38 | use rustc_parse::lexer::StripTokens; |
| 39 | use rustc_session::parse::ParseSess; |
| 40 | use rustc_span::FileName; |
| 41 | use std::borrow::Cow; |
| 42 | use std::fs; |
| 43 | use std::panic; |
| 44 | use std::path::Path; |
| 45 | use std::process; |
| 46 | use std::sync::atomic::{AtomicUsize, Ordering}; |
| 47 | use std::time::Instant; |
| 48 | |
| 49 | #[macro_use] |
| 50 | mod macros; |
| 51 | |
| 52 | mod common; |
| 53 | mod repo; |
| 54 | |
| 55 | #[test] |
| 56 | fn test_round_trip() { |
| 57 | repo::rayon_init(); |
| 58 | repo::clone_rust(); |
| 59 | let abort_after = repo::abort_after(); |
| 60 | if abort_after == 0 { |
| 61 | panic!("skipping all round_trip tests"); |
| 62 | } |
| 63 | |
| 64 | let failed = AtomicUsize::new(0); |
| 65 | |
| 66 | repo::for_each_rust_file(|path| test(path, &failed, abort_after)); |
| 67 | |
| 68 | let failed = failed.into_inner(); |
| 69 | if failed > 0 { |
| 70 | panic!("{} failures", failed); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | fn test(path: &Path, failed: &AtomicUsize, abort_after: usize) { |
| 75 | let failed = || { |
| 76 | let prev_failed = failed.fetch_add(1, Ordering::Relaxed); |
| 77 | if prev_failed + 1 >= abort_after { |
| 78 | process::exit(1); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | let content = fs::read_to_string(path).unwrap(); |
| 83 | |
| 84 | let (back, elapsed) = match panic::catch_unwind(|| { |
| 85 | let start = Instant::now(); |
| 86 | let result = syn::parse_file(&content); |
| 87 | let elapsed = start.elapsed(); |
| 88 | result.map(|krate| (quote!(#krate).to_string(), elapsed)) |
| 89 | }) { |
| 90 | Err(_) => { |
| 91 | errorf!("=== {}: syn panic\n", path.display()); |
| 92 | failed(); |
| 93 | return; |
| 94 | } |
| 95 | Ok(Err(msg)) => { |
| 96 | errorf!("=== {}: syn failed to parse\n{:?}\n", path.display(), msg); |
| 97 | failed(); |
| 98 | return; |
| 99 | } |
| 100 | Ok(Ok(result)) => result, |
| 101 | }; |
| 102 | |
| 103 | let edition = repo::edition(path).parse().unwrap(); |
| 104 | |
| 105 | rustc_span::create_session_if_not_set_then(edition, |_| { |
| 106 | let equal = match panic::catch_unwind(|| { |
| 107 | let sess = ParseSess::new(); |
| 108 | let before = match librustc_parse(content, &sess) { |
| 109 | Ok(before) => before, |
| 110 | Err(diagnostic) => { |
| 111 | errorf!( |
| 112 | "=== {}: ignore - librustc failed to parse original content: {}\n", |
| 113 | path.display(), |
| 114 | translate_message(&diagnostic), |
| 115 | ); |
| 116 | diagnostic.cancel(); |
| 117 | return Err(true); |
| 118 | } |
| 119 | }; |
| 120 | let after = match librustc_parse(back, &sess) { |
| 121 | Ok(after) => after, |
| 122 | Err(diagnostic) => { |
| 123 | errorf!("=== {}: librustc failed to parse", path.display()); |
| 124 | diagnostic.emit(); |
| 125 | return Err(false); |
| 126 | } |
| 127 | }; |
| 128 | Ok((before, after)) |
| 129 | }) { |
| 130 | Err(_) => { |
| 131 | errorf!("=== {}: ignoring librustc panic\n", path.display()); |
| 132 | true |
| 133 | } |
| 134 | Ok(Err(equal)) => equal, |
| 135 | Ok(Ok((mut before, mut after))) => { |
| 136 | normalize(&mut before); |
| 137 | normalize(&mut after); |
| 138 | if SpanlessEq::eq(&before, &after) { |
| 139 | errorf!( |
| 140 | "=== {}: pass in {}ms\n", |
| 141 | path.display(), |
| 142 | elapsed.as_secs() * 1000 + u64::from(elapsed.subsec_nanos()) / 1_000_000 |
| 143 | ); |
| 144 | true |
| 145 | } else { |
| 146 | errorf!( |
| 147 | "=== {}: FAIL\n{}\n!=\n{}\n", |
| 148 | path.display(), |
| 149 | pprust::crate_to_string_for_macros(&before), |
| 150 | pprust::crate_to_string_for_macros(&after), |
| 151 | ); |
| 152 | false |
| 153 | } |
| 154 | } |
| 155 | }; |
| 156 | if !equal { |
| 157 | failed(); |
| 158 | } |
| 159 | }); |
| 160 | } |
| 161 | |
| 162 | fn librustc_parse(content: String, sess: &ParseSess) -> PResult<Crate> { |
| 163 | static COUNTER: AtomicUsize = AtomicUsize::new(0); |
| 164 | let counter = COUNTER.fetch_add(1, Ordering::Relaxed); |
| 165 | let name = FileName::Custom(format!("test_round_trip{}", counter)); |
| 166 | let mut parser = rustc_parse::new_parser_from_source_str( |
| 167 | sess, |
| 168 | name, |
| 169 | content, |
| 170 | StripTokens::ShebangAndFrontmatter, |
| 171 | ) |
| 172 | .unwrap(); |
| 173 | parser.parse_crate_mod() |
| 174 | } |
| 175 | |
| 176 | fn translate_message(diagnostic: &Diag) -> Cow<'static, str> { |
| 177 | match &diagnostic.messages[0].0 { |
| 178 | DiagMessage::Str(msg) | DiagMessage::Inline(msg) => msg.clone(), |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | fn normalize(krate: &mut Crate) { |
| 183 | struct NormalizeVisitor; |
| 184 | |
| 185 | impl MutVisitor for NormalizeVisitor { |
| 186 | fn visit_generic_args(&mut self, e: &mut GenericArgs) { |
| 187 | if let GenericArgs::AngleBracketed(e) = e { |
| 188 | #[derive(Ord, PartialOrd, Eq, PartialEq)] |
| 189 | enum Group { |
| 190 | Lifetimes, |
| 191 | TypesAndConsts, |
| 192 | Constraints, |
| 193 | } |
| 194 | e.args.sort_by_key(|arg| match arg { |
| 195 | AngleBracketedArg::Arg(arg) => match arg { |
| 196 | GenericArg::Lifetime(_) => Group::Lifetimes, |
| 197 | GenericArg::Type(_) | GenericArg::Const(_) => Group::TypesAndConsts, |
| 198 | }, |
| 199 | AngleBracketedArg::Constraint(_) => Group::Constraints, |
| 200 | }); |
| 201 | } |
| 202 | mut_visit::walk_generic_args(self, e); |
| 203 | } |
| 204 | |
| 205 | fn visit_generics(&mut self, e: &mut Generics) { |
| 206 | #[derive(Ord, PartialOrd, Eq, PartialEq)] |
| 207 | enum Group { |
| 208 | Lifetimes, |
| 209 | TypesAndConsts, |
| 210 | } |
| 211 | e.params.sort_by_key(|param| match param.kind { |
| 212 | GenericParamKind::Lifetime => Group::Lifetimes, |
| 213 | GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => { |
| 214 | Group::TypesAndConsts |
| 215 | } |
| 216 | }); |
| 217 | e.params |
| 218 | .flat_map_in_place(|param| self.flat_map_generic_param(param)); |
| 219 | if e.where_clause.predicates.is_empty() { |
| 220 | e.where_clause.has_where_token = false; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | NormalizeVisitor.visit_crate(krate); |
| 226 | } |