]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/main.rs
Rollup merge of #41249 - GuillaumeGomez:rustdoc-render, r=steveklabnik,frewsxcv
[rust.git] / src / tools / tidy / src / main.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Tidy checks for source code in this repository
12 //!
13 //! This program runs all of the various tidy checks for style, cleanliness,
14 //! etc. This is run by default on `make check` and as part of the auto
15 //! builders.
16
17 extern crate regex;
18
19 use std::env;
20 use std::fs;
21 use std::io::{self, Write};
22 use std::path::{PathBuf, Path};
23 use std::process;
24
25 macro_rules! t {
26     ($e:expr, $p:expr) => (match $e {
27         Ok(e) => e,
28         Err(e) => panic!("{} failed on {} with {}", stringify!($e), ($p).display(), e),
29     });
30
31     ($e:expr) => (match $e {
32         Ok(e) => e,
33         Err(e) => panic!("{} failed with {}", stringify!($e), e),
34     })
35 }
36
37 macro_rules! tidy_error {
38     ($bad:expr, $fmt:expr, $($arg:tt)*) => ({
39         use std::io::Write;
40         *$bad = true;
41         write!(::std::io::stderr(), "tidy error: ").expect("could not write to stderr");
42         writeln!(::std::io::stderr(), $fmt, $($arg)*).expect("could not write to stderr");
43     });
44 }
45
46 mod bins;
47 mod style;
48 mod errors;
49 mod features;
50 mod cargo;
51 mod pal;
52 mod deps;
53 mod unstable_book;
54
55 fn main() {
56     let path = env::args_os().skip(1).next().expect("need an argument");
57     let path = PathBuf::from(path);
58
59     let args: Vec<String> = env::args().skip(1).collect();
60
61     let mut bad = false;
62     bins::check(&path, &mut bad);
63     style::check(&path, &mut bad);
64     errors::check(&path, &mut bad);
65     cargo::check(&path, &mut bad);
66     features::check(&path, &mut bad);
67     pal::check(&path, &mut bad);
68     unstable_book::check(&path, &mut bad);
69     if !args.iter().any(|s| *s == "--no-vendor") {
70         deps::check(&path, &mut bad);
71     }
72
73     if bad {
74         writeln!(io::stderr(), "some tidy checks failed").expect("could not write to stderr");
75         process::exit(1);
76     }
77 }
78
79 fn filter_dirs(path: &Path) -> bool {
80     let skip = [
81         "src/jemalloc",
82         "src/llvm",
83         "src/libbacktrace",
84         "src/compiler-rt",
85         "src/rustllvm",
86         "src/rust-installer",
87         "src/liblibc",
88         "src/vendor",
89     ];
90     skip.iter().any(|p| path.ends_with(p))
91 }
92
93 fn walk_many(paths: &[&Path], skip: &mut FnMut(&Path) -> bool, f: &mut FnMut(&Path)) {
94     for path in paths {
95         walk(path, skip, f);
96     }
97 }
98
99 fn walk(path: &Path, skip: &mut FnMut(&Path) -> bool, f: &mut FnMut(&Path)) {
100     for entry in t!(fs::read_dir(path), path) {
101         let entry = t!(entry);
102         let kind = t!(entry.file_type());
103         let path = entry.path();
104         if kind.is_dir() {
105             if !skip(&path) {
106                 walk(&path, skip, f);
107             }
108         } else {
109             f(&path);
110         }
111     }
112 }