Skip to content

Commit

Permalink
Rework count function without glob
Browse files Browse the repository at this point in the history
  • Loading branch information
Oneirical committed Jun 17, 2024
1 parent 348c183 commit dc989e7
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
1 change: 0 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3461,7 +3461,6 @@ name = "run_make_support"
version = "0.2.0"
dependencies = [
"gimli 0.28.1",
"glob",
"object 0.34.0",
"regex",
"similar",
Expand Down
1 change: 0 additions & 1 deletion src/tools/run-make-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ similar = "2.5.0"
wasmparser = "0.118.2"
regex = "1.8" # 1.8 to avoid memchr 2.6.0, as 2.5.0 is pinned in the workspace
gimli = "0.28.1"
glob = "0.3.1"
14 changes: 7 additions & 7 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use std::panic;
use std::path::{Path, PathBuf};

pub use gimli;
pub use glob;
pub use object;
pub use regex;
pub use wasmparser;
Expand Down Expand Up @@ -245,17 +244,18 @@ pub fn uname() -> String {
output.stdout_utf8()
}

/// Inside a glob pattern of files (paths), read their contents and count the
/// Search for all files in the current working directory with the extension `ext`,
/// read their contents and count the
/// number of regex matches with a given expression (re).
#[track_caller]
pub fn count_regex_matches_in_file_glob(re: &str, paths: &str) -> usize {
pub fn count_regex_matches_in_files_with_extension(re: &str, ext: &str) -> usize {
let re = regex::Regex::new(re).expect(format!("Regex expression {re} is not valid.").as_str());
let paths = glob::glob(paths).expect(format!("Glob expression {paths} is not valid.").as_str());
use io::BufRead;
paths
fs_wrapper::read_dir(cwd())
.filter_map(|entry| entry.ok())
.filter(|entry| entry.as_path().is_file())
.filter_map(|path| fs::File::open(&path).ok())
.filter(|entry| entry.path().is_file())
.filter(|entry| entry.path().extension().unwrap() == ext)
.filter_map(|entry| fs::File::open(&entry.path()).ok())
.map(|file| io::BufReader::new(file))
.flat_map(|reader| reader.lines().filter_map(|entry| entry.ok()))
.filter(|line| re.is_match(line))
Expand Down
4 changes: 2 additions & 2 deletions tests/run-make/sepcomp-cci-copies/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
// created for each source module (see `rustc_const_eval::monomorphize::partitioning`).
// See https://github.com/rust-lang/rust/pull/16367

use run_make_support::{count_regex_matches_in_file_glob, rustc};
use run_make_support::{count_regex_matches_in_files_with_extension, rustc};

fn main() {
rustc().input("cci_lib.rs").run();
rustc().input("foo.rs").emit("llvm-ir").codegen_units(6).arg("-Zinline-in-all-cgus").run();
assert_eq!(count_regex_matches_in_file_glob(r#"define\ .*cci_fn"#, "foo.*.ll"), 2);
assert_eq!(count_regex_matches_in_files_with_extension(r#"define\ .*cci_fn"#, "ll"), 2);
}
16 changes: 11 additions & 5 deletions tests/run-make/sepcomp-inlining/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@
// in only one compilation unit.
// See https://github.com/rust-lang/rust/pull/16367

use run_make_support::{count_regex_matches_in_file_glob, glob, regex, rustc};
use run_make_support::{count_regex_matches_in_files_with_extension, glob, regex, rustc};

fn main() {
rustc().input("foo.rs").emit("llvm-ir").codegen_units(3).arg("-Zinline-in-all-cgus").run();
assert_eq!(count_regex_matches_in_file_glob(r#"define\ i32\ .*inlined"#, "foo.*.ll"), 0);
assert_eq!(count_regex_matches_in_file_glob(r#"define\ internal\ .*inlined"#, "foo.*.ll"), 2);
assert_eq!(count_regex_matches_in_file_glob(r#"define\ hidden\ i32\ .*normal"#, "foo.*.ll"), 1);
assert_eq!(count_regex_matches_in_files_with_extension(r#"define\ i32\ .*inlined"#, "ll"), 0);
assert_eq!(
count_regex_matches_in_file_glob(r#"declare\ hidden\ i32\ .*normal"#, "foo.*.ll"),
count_regex_matches_in_files_with_extension(r#"define\ internal\ .*inlined"#, "ll"),
2
);
assert_eq!(
count_regex_matches_in_files_with_extension(r#"define\ hidden\ i32\ .*normal"#, "ll"),
1
);
assert_eq!(
count_regex_matches_in_files_with_extension(r#"declare\ hidden\ i32\ .*normal"#, "ll"),
2
);
}
4 changes: 2 additions & 2 deletions tests/run-make/sepcomp-separate/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// wind up in three different compilation units.
// See https://github.com/rust-lang/rust/pull/16367

use run_make_support::{count_regex_matches_in_file_glob, rustc};
use run_make_support::{count_regex_matches_in_files_with_extension, rustc};

fn main() {
rustc().input("foo.rs").emit("llvm-ir").codegen_units(3).run();
assert_eq!(count_regex_matches_in_file_glob(r#"define\ .*magic_fn"#, "foo.*.ll"), 3);
assert_eq!(count_regex_matches_in_files_with_extension(r#"define\ .*magic_fn"#, "ll"), 3);
}

0 comments on commit dc989e7

Please sign in to comment.