Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ script:
- |
if [ "$TRAVIS_RUST_VERSION" = "nightly" ] && [ -z "$TRAVIS_TAG" ]; then
zip -0 ccov.zip `find . \( -name "grcov*.gc*" -o -name "llvmgcov.gc*" \) -print`;
./target/debug/grcov ccov.zip -s . -t lcov --llvm --branch --ignore-not-existing --ignore "/*" -o lcov.info;
Comment thread
jcdickinson marked this conversation as resolved.
./target/debug/grcov ccov.zip -s . -t lcov --llvm --branch --ignore-not-existing --ignore "/*" -o lcov.info --excl_line "#\[derive\(" --excl_br_line "#\[derive\(";
Comment thread
jcdickinson marked this conversation as resolved.
Outdated
bash <(curl -s https://codecov.io/bash) -f lcov.info;
fi
- |
Expand Down
62 changes: 43 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ fomat-macros = "^0.3"
chrono = "^0.4"
log = "^0.4"
simplelog = "^0.7"
regex = "^1.3"
rayon = "^1.3"

[target.'cfg(unix)'.dependencies]
#tcmalloc = { version = "^0.3", features = ["bundled"] }
tcmalloc = { version = "^0.3", optional = true }

[dev-dependencies]
regex = "^1.3"
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ FLAGS:

OPTIONS:
--commit-sha <COMMIT HASH> Sets the hash of the commit used to generate the code coverage data
--excl_br_line <regex>
Lines in covered files containing this marker will be excluded.
Comment thread
jcdickinson marked this conversation as resolved.
Outdated
--excl_br_start <regex>
Marks the beginning of an excluded section. The current line is part of this section.
Comment thread
jcdickinson marked this conversation as resolved.
Outdated
--excl_br_stop <regex>
Marks the end of an excluded section. The current line is part of this section.
Comment thread
jcdickinson marked this conversation as resolved.
Outdated
--excl_line <regex>
Lines in covered files containing this marker will be excluded.
--excl_start <regex>
Marks the beginning of an excluded section. The current line is part of this section.
--excl_stop <regex>
Marks the end of an excluded section. The current line is part of this section.
--filter <filter>
Filters out covered/uncovered files. Use 'covered' to only return covered files, 'uncovered' to only return
uncovered files [possible values: covered, uncovered]
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test_script:
If ($env:CHANNEL -eq "nightly" -And $env:APPVEYOR_REPO_TAG -eq "false") {
mkdir ccov_dir
Get-ChildItem -Path *\grcov*.gc* -Recurse | Copy-Item -Destination ccov_dir
.\target\debug\grcov ccov_dir -s . -t lcov --llvm --branch --ignore-not-existing --ignore "C:*" -o lcov.info
.\target\debug\grcov ccov_dir -s . -t lcov --llvm --branch --ignore-not-existing --ignore "C:*" -o lcov.info --excl_line "#\[derive\(" --excl_br_line "#\[derive\("
Comment thread
jcdickinson marked this conversation as resolved.
Outdated
(Get-Content lcov.info) | Foreach-Object {$_ -replace "\xEF\xBB\xBF", ""} | Set-Content lcov.info
((Get-Content lcov.info) -join "`n") + "`n" | Set-Content -NoNewline lcov.info
$env:PATH = "C:\msys64\usr\bin;" + $env:PATH
Expand Down
5 changes: 2 additions & 3 deletions benches/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ use test::{black_box, Bencher};
#[bench]
fn bench_parser_lcov(b: &mut Bencher) {
b.iter(|| {
let f = File::open("./test/prova.info").expect("Failed to open lcov file");
let file = BufReader::new(&f);
black_box(grcov::parse_lcov(file, true));
let file = std::fs::read("./test/prova.info").expect("Failed to open lcov file");
black_box(grcov::parse_lcov(file, true).unwrap());
});
}

Expand Down
129 changes: 129 additions & 0 deletions src/file_filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
use regex::Regex;
use std::path::PathBuf;

pub enum FilterType {
Line(u32),
Branch(u32),
Both(u32),
}

#[derive(Default)]
pub struct FileFilter {
excl_line: Option<Regex>,
excl_start: Option<Regex>,
excl_stop: Option<Regex>,
excl_br_line: Option<Regex>,
excl_br_start: Option<Regex>,
excl_br_stop: Option<Regex>,
}

fn matches(regex: &Option<Regex>, line: &str) -> bool {
if let Some(regex) = regex {
Comment thread
jcdickinson marked this conversation as resolved.
Outdated
Comment thread
jcdickinson marked this conversation as resolved.
Outdated
regex.is_match(line)
} else {
false
}
}

impl FileFilter {
pub fn new(
excl_line: Option<Regex>,
excl_start: Option<Regex>,
excl_stop: Option<Regex>,
excl_br_line: Option<Regex>,
excl_br_start: Option<Regex>,
excl_br_stop: Option<Regex>,
) -> Self {
Self {
excl_line,
excl_start,
excl_stop,
excl_br_line,
excl_br_start,
excl_br_stop,
}
}

pub fn create(&self, file: &PathBuf) -> Vec<FilterType> {
if self.excl_line.is_none()
&& self.excl_start.is_none()
&& self.excl_br_line.is_none()
&& self.excl_br_start.is_none()
{
return Vec::new();
}

let file = std::fs::read_to_string(file);
let file = if let Ok(file) = file {
file
} else {
return Vec::new();
};

let mut ignore_br = false;
let mut ignore = false;

file.split("\n")
Comment thread
jcdickinson marked this conversation as resolved.
Outdated
Comment thread
jcdickinson marked this conversation as resolved.
Outdated
.enumerate()
.into_iter()
Comment thread
jcdickinson marked this conversation as resolved.
Outdated
.filter_map(move |(number, line)| {
// Line numbers are 1-based.
let number = (number + 1) as u32;

// The file is split on \n, which may result in a trailing \r
// on Windows. Remove it.
let line = if line.ends_with("\r") {
Comment thread
jcdickinson marked this conversation as resolved.
Outdated
&line[..(line.len() - 1)]
} else {
line
};

// End a branch ignore region. Region endings are exclusive.
if ignore_br && matches(&self.excl_br_stop, line) {
ignore_br = false
}

// End a line ignore region. Region endings are exclusive.
if ignore && matches(&self.excl_stop, line) {
ignore = false
}

// Start a branch ignore region. Region starts are inclusive.
if !ignore_br && matches(&self.excl_br_start, line) {
ignore_br = true;
}

// Start a line ignore region. Region starts are inclusive.
if !ignore && matches(&self.excl_start, line) {
ignore = true;
}

if ignore_br {
// Consuming code has to eliminate each of these
// individually, so it has to know when both are ignored vs.
// either.
if ignore {
Some(FilterType::Both(number))
} else {
Some(FilterType::Branch(number))
}
} else if ignore {
Some(FilterType::Line(number))
} else if matches(&self.excl_br_line, line) {
// Single line exclusion. If single line exclusions occur
// inside a region they are meaningless (would be applied
// anway), so they are lower priority.
if matches(&self.excl_line, line) {
Some(FilterType::Both(number))
} else {
Some(FilterType::Branch(number))
}
} else if matches(&self.excl_line, line) {
Some(FilterType::Line(number))
} else {
None
}
})
.collect()
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub use crate::covdir::*;

pub mod html;

mod file_filter;
pub use crate::file_filter::*;

use log::error;
use std::collections::{btree_map, hash_map};
use std::fs;
Expand Down
Loading