Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit ea30a9d

Browse files
committed
Rework count function without glob
1 parent 348c183 commit ea30a9d

File tree

6 files changed

+47
-28
lines changed

6 files changed

+47
-28
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3461,10 +3461,10 @@ name = "run_make_support"
34613461
version = "0.2.0"
34623462
dependencies = [
34633463
"gimli 0.28.1",
3464-
"glob",
34653464
"object 0.34.0",
34663465
"regex",
34673466
"similar",
3467+
"walkdir",
34683468
"wasmparser",
34693469
]
34703470

src/tools/run-make-support/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ similar = "2.5.0"
99
wasmparser = "0.118.2"
1010
regex = "1.8" # 1.8 to avoid memchr 2.6.0, as 2.5.0 is pinned in the workspace
1111
gimli = "0.28.1"
12-
glob = "0.3.1"
12+
walkdir = "2.5.0"

src/tools/run-make-support/src/lib.rs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use std::panic;
2222
use std::path::{Path, PathBuf};
2323

2424
pub use gimli;
25-
pub use glob;
2625
pub use object;
2726
pub use regex;
27+
pub use walkdir;
2828
pub use wasmparser;
2929

3030
pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
@@ -245,21 +245,37 @@ pub fn uname() -> String {
245245
output.stdout_utf8()
246246
}
247247

248-
/// Inside a glob pattern of files (paths), read their contents and count the
248+
/// Search for all files in the current working directory with the extension `ext`,
249+
/// read their contents and count the
249250
/// number of regex matches with a given expression (re).
250251
#[track_caller]
251-
pub fn count_regex_matches_in_file_glob(re: &str, paths: &str) -> usize {
252-
let re = regex::Regex::new(re).expect(format!("Regex expression {re} is not valid.").as_str());
253-
let paths = glob::glob(paths).expect(format!("Glob expression {paths} is not valid.").as_str());
254-
use io::BufRead;
255-
paths
256-
.filter_map(|entry| entry.ok())
257-
.filter(|entry| entry.as_path().is_file())
258-
.filter_map(|path| fs::File::open(&path).ok())
259-
.map(|file| io::BufReader::new(file))
260-
.flat_map(|reader| reader.lines().filter_map(|entry| entry.ok()))
261-
.filter(|line| re.is_match(line))
262-
.count()
252+
pub fn count_regex_matches_in_files_with_extension(re: &regex::Regex, ext: &str) -> usize {
253+
use std::io::BufRead;
254+
use walkdir::{DirEntry, WalkDir};
255+
256+
let walker = WalkDir::new(cwd()).into_iter();
257+
258+
fn is_hidden(entry: &DirEntry) -> bool {
259+
entry.file_name().to_str().map(|s| s.starts_with(".")).unwrap_or(false)
260+
}
261+
262+
let mut count = 0;
263+
264+
for entry in walker.filter_entry(|e| !is_hidden(e)) {
265+
let entry = entry.expect("failed to get DirEntry");
266+
if !entry.path().is_file() {
267+
continue;
268+
}
269+
270+
if !entry.path().extension().is_some_and(|e| e == ext) {
271+
continue;
272+
}
273+
274+
let content = fs_wrapper::read(entry.path());
275+
count += content.lines().filter(|line| re.is_match(&line.as_ref().unwrap())).count();
276+
}
277+
278+
count
263279
}
264280

265281
fn handle_failed_output(cmd: &Command, output: CompletedProcess, caller_line_number: u32) -> ! {

tests/run-make/sepcomp-cci-copies/rmake.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
// created for each source module (see `rustc_const_eval::monomorphize::partitioning`).
55
// See https://github.com/rust-lang/rust/pull/16367
66

7-
use run_make_support::{count_regex_matches_in_file_glob, rustc};
7+
use run_make_support::{count_regex_matches_in_files_with_extension, regex, rustc};
88

99
fn main() {
1010
rustc().input("cci_lib.rs").run();
1111
rustc().input("foo.rs").emit("llvm-ir").codegen_units(6).arg("-Zinline-in-all-cgus").run();
12-
assert_eq!(count_regex_matches_in_file_glob(r#"define\ .*cci_fn"#, "foo.*.ll"), 2);
12+
let re = regex::Regex::new(r#"define\ .*cci_fn"#).unwrap();
13+
assert_eq!(count_regex_matches_in_files_with_extension(&re, "ll"), 2);
1314
}

tests/run-make/sepcomp-inlining/rmake.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
// in only one compilation unit.
66
// See https://github.com/rust-lang/rust/pull/16367
77

8-
use run_make_support::{count_regex_matches_in_file_glob, glob, regex, rustc};
8+
use run_make_support::{count_regex_matches_in_files_with_extension, regex, rustc};
99

1010
fn main() {
1111
rustc().input("foo.rs").emit("llvm-ir").codegen_units(3).arg("-Zinline-in-all-cgus").run();
12-
assert_eq!(count_regex_matches_in_file_glob(r#"define\ i32\ .*inlined"#, "foo.*.ll"), 0);
13-
assert_eq!(count_regex_matches_in_file_glob(r#"define\ internal\ .*inlined"#, "foo.*.ll"), 2);
14-
assert_eq!(count_regex_matches_in_file_glob(r#"define\ hidden\ i32\ .*normal"#, "foo.*.ll"), 1);
15-
assert_eq!(
16-
count_regex_matches_in_file_glob(r#"declare\ hidden\ i32\ .*normal"#, "foo.*.ll"),
17-
2
18-
);
12+
let re = regex::Regex::new(r#"define\ i32\ .*inlined"#).unwrap();
13+
assert_eq!(count_regex_matches_in_files_with_extension(&re, "ll"), 0);
14+
let re = regex::Regex::new(r#"define\ internal\ .*inlined"#).unwrap();
15+
assert_eq!(count_regex_matches_in_files_with_extension(&re, "ll"), 2);
16+
let re = regex::Regex::new(r#"define\ hidden\ i32\ .*normal"#).unwrap();
17+
assert_eq!(count_regex_matches_in_files_with_extension(&re, "ll"), 1);
18+
let re = regex::Regex::new(r#"declare\ hidden\ i32\ .*normal"#).unwrap();
19+
assert_eq!(count_regex_matches_in_files_with_extension(&re, "ll"), 2);
1920
}

tests/run-make/sepcomp-separate/rmake.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
// wind up in three different compilation units.
44
// See https://github.com/rust-lang/rust/pull/16367
55

6-
use run_make_support::{count_regex_matches_in_file_glob, rustc};
6+
use run_make_support::{count_regex_matches_in_files_with_extension, regex, rustc};
77

88
fn main() {
99
rustc().input("foo.rs").emit("llvm-ir").codegen_units(3).run();
10-
assert_eq!(count_regex_matches_in_file_glob(r#"define\ .*magic_fn"#, "foo.*.ll"), 3);
10+
let re = regex::Regex::new(r#"define\ .*magic_fn"#).unwrap();
11+
assert_eq!(count_regex_matches_in_files_with_extension(&re, "ll"), 3);
1112
}

0 commit comments

Comments
 (0)