-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option for generating coverage reports
Add a `--coverage` option in the `test` subcommand of the miri script. This option, when set, will generate a coverage report after running the tests. `cargo-binutils` is needed as a dependency to generate the reports.
- Loading branch information
1 parent
9045930
commit 682a86b
Showing
8 changed files
with
152 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -175,4 +175,4 @@ case $HOST_TARGET in | |
echo "FATAL: unknown host target: $HOST_TARGET" | ||
exit 1 | ||
;; | ||
esac | ||
esac |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ rustc_version = "0.4" | |
dunce = "1.0.4" | ||
directories = "5" | ||
serde_json = "1" | ||
tempfile = "3.13.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use std::ffi::OsString; | ||
|
||
use anyhow::{Context, Result}; | ||
use xshell::cmd; | ||
|
||
use crate::util::MiriEnv; | ||
|
||
pub struct CoverageReport { | ||
path: tempfile::TempDir, | ||
} | ||
|
||
impl CoverageReport { | ||
pub fn new() -> Result<Self> { | ||
Ok(Self { path: tempfile::TempDir::new()? }) | ||
} | ||
|
||
pub fn add_env_vars(&self, e: &mut MiriEnv) -> Result<()> { | ||
let rustflags = e.sh.var("RUSTFLAGS")?; | ||
let rustflags = format!("{rustflags} -C instrument-coverage"); | ||
e.sh.set_var("RUSTFLAGS", rustflags); | ||
|
||
let file_template = self.path.path().join("default1_%m_%p.profraw"); | ||
e.sh.set_var("LLVM_PROFILE_FILE", file_template); | ||
Ok(()) | ||
} | ||
|
||
pub fn show_coverage_report(&self, e: &MiriEnv) -> Result<()> { | ||
let profraw_files: Vec<_> = self.profraw_files()?; | ||
|
||
let mut profdata_bin = e.libdir.clone(); | ||
profdata_bin.push(".."); | ||
profdata_bin.push("bin"); | ||
profdata_bin.push("llvm-profdata"); | ||
|
||
let merged_file = self.path.path().join("merged.profdata"); | ||
|
||
// Merge the profraw files | ||
let profraw_files_cloned = profraw_files.iter(); | ||
cmd!(e.sh, "{profdata_bin} merge -sparse {profraw_files_cloned...} -o {merged_file}") | ||
.quiet() | ||
.run()?; | ||
|
||
// Create the coverage report. | ||
let mut cov_bin = e.libdir.clone(); | ||
cov_bin.push(".."); | ||
cov_bin.push("bin"); | ||
cov_bin.push("llvm-cov"); | ||
let miri_bin = | ||
e.build_get_binary(".").context("failed to get filename of miri executable")?; | ||
cmd!( | ||
e.sh, | ||
"{cov_bin} report --instr-profile={merged_file} --object {miri_bin} --sources src/" | ||
) | ||
.run()?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn profraw_files(&self) -> Result<Vec<OsString>> { | ||
Ok(std::fs::read_dir(&self.path)? | ||
.filter_map(|r| r.ok()) | ||
.map(|e| e.path()) | ||
.filter(|p| p.extension().map(|e| e == "profraw").unwrap_or(false)) | ||
.map(|p| p.as_os_str().to_os_string()) | ||
.collect()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters