Skip to content

Commit 9b606a3

Browse files
committed
Speed up file walking in tidy
- Skip files in `skip` wherever possible to avoid reading their contents - Don't look for `tidy-alphabetic-start` in tests. It's never currently used and slows the check down a lot. - Add new `filter_not_rust` helper function
1 parent c76e260 commit 9b606a3

File tree

6 files changed

+80
-83
lines changed

6 files changed

+80
-83
lines changed

src/tools/tidy/src/debug_artifacts.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
//! Tidy check to prevent creation of unnecessary debug artifacts while running tests.
22
3-
use crate::walk::{filter_dirs, walk};
3+
use crate::walk::{filter_dirs, filter_not_rust, walk};
44
use std::path::Path;
55

66
const GRAPHVIZ_POSTFLOW_MSG: &str = "`borrowck_graphviz_postflow` attribute in test";
77

88
pub fn check(test_dir: &Path, bad: &mut bool) {
9-
walk(test_dir, filter_dirs, &mut |entry, contents| {
10-
let filename = entry.path();
11-
let is_rust = filename.extension().map_or(false, |ext| ext == "rs");
12-
if !is_rust {
13-
return;
14-
}
15-
9+
walk(test_dir, |path| filter_dirs(path) || filter_not_rust(path), &mut |entry, contents| {
1610
for (i, line) in contents.lines().enumerate() {
1711
if line.contains("borrowck_graphviz_postflow") {
18-
tidy_error!(bad, "{}:{}: {}", filename.display(), i + 1, GRAPHVIZ_POSTFLOW_MSG);
12+
tidy_error!(bad, "{}:{}: {}", entry.path().display(), i + 1, GRAPHVIZ_POSTFLOW_MSG);
1913
}
2014
}
2115
});

src/tools/tidy/src/features.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
//! * All unstable lang features have tests to ensure they are actually unstable.
1010
//! * Language features in a group are sorted by feature name.
1111
12-
use crate::walk::{filter_dirs, walk, walk_many};
12+
use crate::walk::{filter_dirs, filter_not_rust, walk, walk_many};
1313
use std::collections::hash_map::{Entry, HashMap};
14+
use std::ffi::OsStr;
1415
use std::fmt;
1516
use std::fs;
1617
use std::num::NonZeroU32;
@@ -101,17 +102,15 @@ pub fn check(
101102
&tests_path.join("rustdoc-ui"),
102103
&tests_path.join("rustdoc"),
103104
],
104-
filter_dirs,
105+
|path| {
106+
filter_dirs(path)
107+
|| filter_not_rust(path)
108+
|| path.file_name() == Some(OsStr::new("features.rs"))
109+
|| path.file_name() == Some(OsStr::new("diagnostic_list.rs"))
110+
},
105111
&mut |entry, contents| {
106112
let file = entry.path();
107113
let filename = file.file_name().unwrap().to_string_lossy();
108-
if !filename.ends_with(".rs")
109-
|| filename == "features.rs"
110-
|| filename == "diagnostic_list.rs"
111-
{
112-
return;
113-
}
114-
115114
let filen_underscore = filename.replace('-', "_").replace(".rs", "");
116115
let filename_is_gate_test = test_filen_gate(&filen_underscore, &mut features);
117116

src/tools/tidy/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ fn main() {
120120
check!(edition, &library_path);
121121

122122
check!(alphabetical, &src_path);
123-
check!(alphabetical, &tests_path);
124123
check!(alphabetical, &compiler_path);
125124
check!(alphabetical, &library_path);
126125

src/tools/tidy/src/style.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
use crate::walk::{filter_dirs, walk};
2121
use regex::{Regex, RegexSet};
22-
use std::path::Path;
22+
use std::{ffi::OsStr, path::Path};
2323

2424
/// Error code markdown is restricted to 80 columns because they can be
2525
/// displayed on the console with --example.
@@ -228,21 +228,28 @@ fn is_unexplained_ignore(extension: &str, line: &str) -> bool {
228228

229229
pub fn check(path: &Path, bad: &mut bool) {
230230
fn skip(path: &Path) -> bool {
231-
filter_dirs(path) || skip_markdown_path(path)
231+
if filter_dirs(path) || skip_markdown_path(path) {
232+
return true;
233+
}
234+
235+
let extensions = ["rs", "py", "js", "sh", "c", "cpp", "h", "md", "css", "ftl", "goml"];
236+
if extensions.iter().all(|e| path.extension() != Some(OsStr::new(e))) {
237+
return true;
238+
}
239+
240+
// We only check CSS files in rustdoc.
241+
path.extension().map_or(false, |e| e == "css") && !is_in(path, "src", "librustdoc")
232242
}
243+
233244
let problematic_consts_strings: Vec<String> = (PROBLEMATIC_CONSTS.iter().map(u32::to_string))
234245
.chain(PROBLEMATIC_CONSTS.iter().map(|v| format!("{:x}", v)))
235246
.chain(PROBLEMATIC_CONSTS.iter().map(|v| format!("{:X}", v)))
236247
.collect();
237248
let problematic_regex = RegexSet::new(problematic_consts_strings.as_slice()).unwrap();
249+
238250
walk(path, skip, &mut |entry, contents| {
239251
let file = entry.path();
240252
let filename = file.file_name().unwrap().to_string_lossy();
241-
let extensions =
242-
[".rs", ".py", ".js", ".sh", ".c", ".cpp", ".h", ".md", ".css", ".ftl", ".goml"];
243-
if extensions.iter().all(|e| !filename.ends_with(e)) || filename.starts_with(".#") {
244-
return;
245-
}
246253

247254
let is_style_file = filename.ends_with(".css");
248255
let under_rustfmt = filename.ends_with(".rs") &&
@@ -253,11 +260,6 @@ pub fn check(path: &Path, bad: &mut bool) {
253260
a.ends_with("src/doc/book")
254261
});
255262

256-
if is_style_file && !is_in(file, "src", "librustdoc") {
257-
// We only check CSS files in rustdoc.
258-
return;
259-
}
260-
261263
if contents.is_empty() {
262264
tidy_error!(bad, "{}: empty file", file.display());
263265
}

src/tools/tidy/src/target_specific_tests.rs

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
use std::collections::BTreeMap;
55
use std::path::Path;
66

7+
use crate::walk::filter_not_rust;
8+
79
const COMMENT: &str = "//";
810
const LLVM_COMPONENTS_HEADER: &str = "needs-llvm-components:";
911
const COMPILE_FLAGS_HEADER: &str = "compile-flags:";
@@ -35,61 +37,57 @@ struct RevisionInfo<'a> {
3537
}
3638

3739
pub fn check(path: &Path, bad: &mut bool) {
38-
crate::walk::walk(
39-
path,
40-
|path| path.extension().map(|p| p == "rs") == Some(false),
41-
&mut |entry, content| {
42-
let file = entry.path().display();
43-
let mut header_map = BTreeMap::new();
44-
iter_header(content, &mut |cfg, directive| {
45-
if let Some(value) = directive.strip_prefix(LLVM_COMPONENTS_HEADER) {
46-
let info = header_map.entry(cfg).or_insert(RevisionInfo::default());
47-
let comp_vec = info.llvm_components.get_or_insert(Vec::new());
48-
for component in value.split(' ') {
49-
let component = component.trim();
50-
if !component.is_empty() {
51-
comp_vec.push(component);
52-
}
53-
}
54-
} else if directive.starts_with(COMPILE_FLAGS_HEADER) {
55-
let compile_flags = &directive[COMPILE_FLAGS_HEADER.len()..];
56-
if let Some((_, v)) = compile_flags.split_once("--target") {
57-
if let Some((arch, _)) =
58-
v.trim_start_matches(|c| c == ' ' || c == '=').split_once("-")
59-
{
60-
let info = header_map.entry(cfg).or_insert(RevisionInfo::default());
61-
info.target_arch.replace(arch);
62-
} else {
63-
eprintln!("{file}: seems to have a malformed --target value");
64-
*bad = true;
65-
}
40+
crate::walk::walk(path, filter_not_rust, &mut |entry, content| {
41+
let file = entry.path().display();
42+
let mut header_map = BTreeMap::new();
43+
iter_header(content, &mut |cfg, directive| {
44+
if let Some(value) = directive.strip_prefix(LLVM_COMPONENTS_HEADER) {
45+
let info = header_map.entry(cfg).or_insert(RevisionInfo::default());
46+
let comp_vec = info.llvm_components.get_or_insert(Vec::new());
47+
for component in value.split(' ') {
48+
let component = component.trim();
49+
if !component.is_empty() {
50+
comp_vec.push(component);
6651
}
6752
}
68-
});
69-
for (rev, RevisionInfo { target_arch, llvm_components }) in &header_map {
70-
let rev = rev.unwrap_or("[unspecified]");
71-
match (target_arch, llvm_components) {
72-
(None, None) => {}
73-
(Some(_), None) => {
74-
eprintln!(
75-
"{}: revision {} should specify `{}` as it has `--target` set",
76-
file, rev, LLVM_COMPONENTS_HEADER
77-
);
53+
} else if directive.starts_with(COMPILE_FLAGS_HEADER) {
54+
let compile_flags = &directive[COMPILE_FLAGS_HEADER.len()..];
55+
if let Some((_, v)) = compile_flags.split_once("--target") {
56+
if let Some((arch, _)) =
57+
v.trim_start_matches(|c| c == ' ' || c == '=').split_once("-")
58+
{
59+
let info = header_map.entry(cfg).or_insert(RevisionInfo::default());
60+
info.target_arch.replace(arch);
61+
} else {
62+
eprintln!("{file}: seems to have a malformed --target value");
7863
*bad = true;
7964
}
80-
(None, Some(_)) => {
81-
eprintln!(
82-
"{}: revision {} should not specify `{}` as it doesn't need `--target`",
83-
file, rev, LLVM_COMPONENTS_HEADER
84-
);
85-
*bad = true;
86-
}
87-
(Some(_), Some(_)) => {
88-
// FIXME: check specified components against the target architectures we
89-
// gathered.
90-
}
9165
}
9266
}
93-
},
94-
);
67+
});
68+
for (rev, RevisionInfo { target_arch, llvm_components }) in &header_map {
69+
let rev = rev.unwrap_or("[unspecified]");
70+
match (target_arch, llvm_components) {
71+
(None, None) => {}
72+
(Some(_), None) => {
73+
eprintln!(
74+
"{}: revision {} should specify `{}` as it has `--target` set",
75+
file, rev, LLVM_COMPONENTS_HEADER
76+
);
77+
*bad = true;
78+
}
79+
(None, Some(_)) => {
80+
eprintln!(
81+
"{}: revision {} should not specify `{}` as it doesn't need `--target`",
82+
file, rev, LLVM_COMPONENTS_HEADER
83+
);
84+
*bad = true;
85+
}
86+
(Some(_), Some(_)) => {
87+
// FIXME: check specified components against the target architectures we
88+
// gathered.
89+
}
90+
}
91+
}
92+
});
9593
}

src/tools/tidy/src/walk.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ignore::DirEntry;
22

3-
use std::{fs::File, io::Read, path::Path};
3+
use std::{ffi::OsStr, fs::File, io::Read, path::Path};
44

55
/// The default directory filter.
66
pub fn filter_dirs(path: &Path) -> bool {
@@ -33,6 +33,11 @@ pub fn filter_dirs(path: &Path) -> bool {
3333
skip.iter().any(|p| path.ends_with(p))
3434
}
3535

36+
/// Filter for only files that end in `.rs`.
37+
pub fn filter_not_rust(path: &Path) -> bool {
38+
!path.is_dir() && path.extension() != Some(OsStr::new("rs"))
39+
}
40+
3641
pub fn walk_many(
3742
paths: &[&Path],
3843
skip: impl Clone + Send + Sync + 'static + Fn(&Path) -> bool,

0 commit comments

Comments
 (0)