Skip to content

Commit d810898

Browse files
committed
Librarify tidy
Convert tidy into a library so that the data it creates can be used by external tools.
1 parent e40ef96 commit d810898

File tree

2 files changed

+96
-72
lines changed

2 files changed

+96
-72
lines changed

src/tools/tidy/src/lib.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2017 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+
//! Library used by tidy and other tools
12+
//!
13+
//! This library contains the tidy lints and exposes it
14+
//! to be used by tools.
15+
16+
#![deny(warnings)]
17+
18+
use std::fs;
19+
20+
use std::path::Path;
21+
22+
macro_rules! t {
23+
($e:expr, $p:expr) => (match $e {
24+
Ok(e) => e,
25+
Err(e) => panic!("{} failed on {} with {}", stringify!($e), ($p).display(), e),
26+
});
27+
28+
($e:expr) => (match $e {
29+
Ok(e) => e,
30+
Err(e) => panic!("{} failed with {}", stringify!($e), e),
31+
})
32+
}
33+
34+
macro_rules! tidy_error {
35+
($bad:expr, $fmt:expr, $($arg:tt)*) => ({
36+
use std::io::Write;
37+
*$bad = true;
38+
write!(::std::io::stderr(), "tidy error: ").expect("could not write to stderr");
39+
writeln!(::std::io::stderr(), $fmt, $($arg)*).expect("could not write to stderr");
40+
});
41+
}
42+
43+
pub mod bins;
44+
pub mod style;
45+
pub mod errors;
46+
pub mod features;
47+
pub mod cargo;
48+
pub mod pal;
49+
pub mod deps;
50+
pub mod unstable_book;
51+
52+
fn filter_dirs(path: &Path) -> bool {
53+
let skip = [
54+
"src/jemalloc",
55+
"src/llvm",
56+
"src/libbacktrace",
57+
"src/compiler-rt",
58+
"src/rustllvm",
59+
"src/liblibc",
60+
"src/vendor",
61+
"src/rt/hoedown",
62+
"src/tools/cargo",
63+
"src/tools/rls",
64+
"src/tools/rust-installer",
65+
];
66+
skip.iter().any(|p| path.ends_with(p))
67+
}
68+
69+
fn walk_many(paths: &[&Path], skip: &mut FnMut(&Path) -> bool, f: &mut FnMut(&Path)) {
70+
for path in paths {
71+
walk(path, skip, f);
72+
}
73+
}
74+
75+
fn walk(path: &Path, skip: &mut FnMut(&Path) -> bool, f: &mut FnMut(&Path)) {
76+
for entry in t!(fs::read_dir(path), path) {
77+
let entry = t!(entry);
78+
let kind = t!(entry.file_type());
79+
let path = entry.path();
80+
if kind.is_dir() {
81+
if !skip(&path) {
82+
walk(&path, skip, f);
83+
}
84+
} else {
85+
f(&path);
86+
}
87+
}
88+
}

src/tools/tidy/src/main.rs

Lines changed: 8 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,21 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! Tidy checks for source code in this repository
11+
//! Tidy checks source code in this repository
1212
//!
1313
//! This program runs all of the various tidy checks for style, cleanliness,
1414
//! etc. This is run by default on `make check` and as part of the auto
1515
//! builders.
1616
17-
use std::env;
18-
use std::fs;
19-
use std::io::{self, Write};
20-
use std::path::{PathBuf, Path};
21-
use std::process;
17+
#![deny(warnings)]
2218

23-
macro_rules! t {
24-
($e:expr, $p:expr) => (match $e {
25-
Ok(e) => e,
26-
Err(e) => panic!("{} failed on {} with {}", stringify!($e), ($p).display(), e),
27-
});
28-
29-
($e:expr) => (match $e {
30-
Ok(e) => e,
31-
Err(e) => panic!("{} failed with {}", stringify!($e), e),
32-
})
33-
}
19+
extern crate tidy;
20+
use tidy::*;
3421

35-
macro_rules! tidy_error {
36-
($bad:expr, $fmt:expr, $($arg:tt)*) => ({
37-
use std::io::Write;
38-
*$bad = true;
39-
write!(::std::io::stderr(), "tidy error: ").expect("could not write to stderr");
40-
writeln!(::std::io::stderr(), $fmt, $($arg)*).expect("could not write to stderr");
41-
});
42-
}
43-
44-
mod bins;
45-
mod style;
46-
mod errors;
47-
mod features;
48-
mod cargo;
49-
mod pal;
50-
mod deps;
51-
mod unstable_book;
22+
use std::process;
23+
use std::path::PathBuf;
24+
use std::env;
25+
use std::io::{self, Write};
5226

5327
fn main() {
5428
let path = env::args_os().skip(1).next().expect("need an argument");
@@ -74,41 +48,3 @@ fn main() {
7448
process::exit(1);
7549
}
7650
}
77-
78-
fn filter_dirs(path: &Path) -> bool {
79-
let skip = [
80-
"src/jemalloc",
81-
"src/llvm",
82-
"src/libbacktrace",
83-
"src/compiler-rt",
84-
"src/rustllvm",
85-
"src/liblibc",
86-
"src/vendor",
87-
"src/rt/hoedown",
88-
"src/tools/cargo",
89-
"src/tools/rls",
90-
"src/tools/rust-installer",
91-
];
92-
skip.iter().any(|p| path.ends_with(p))
93-
}
94-
95-
fn walk_many(paths: &[&Path], skip: &mut FnMut(&Path) -> bool, f: &mut FnMut(&Path)) {
96-
for path in paths {
97-
walk(path, skip, f);
98-
}
99-
}
100-
101-
fn walk(path: &Path, skip: &mut FnMut(&Path) -> bool, f: &mut FnMut(&Path)) {
102-
for entry in t!(fs::read_dir(path), path) {
103-
let entry = t!(entry);
104-
let kind = t!(entry.file_type());
105-
let path = entry.path();
106-
if kind.is_dir() {
107-
if !skip(&path) {
108-
walk(&path, skip, f);
109-
}
110-
} else {
111-
f(&path);
112-
}
113-
}
114-
}

0 commit comments

Comments
 (0)