Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
54 changes: 27 additions & 27 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ edition = "2024"
cairo-lang-sierra = "2.10.1"
cairo-lang-sierra-to-casm = "2.10.1"
cairo-lang-starknet-classes = "2.10.1"
cairo-annotations = "0.2.2"
cairo-annotations = "0.3.0"

anyhow = "1.0.96"
assert_fs = "1.1.2"
camino = "1.1.9"
clap = { version = "4.5.30", features = ["derive"] }
clap = { version = "4.5.31", features = ["derive"] }
criterion = "0.5"
console = "0.15.10"
console = "0.15.11"
itertools = "0.14.0"
ignore = "0.4.23"
serde = "1.0.218"
serde_json = "1.0.139"
scarb-metadata = "1.13.0"
scarb-metadata = "1.14.0"
snapbox = "0.6.21"
semver = "1.0.25"
indoc = "2.0.5"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::build::filter::statement_category_filter::VIRTUAL_FILE_REGEX;
use anyhow::{Error, Result};
use camino::{Utf8Path, Utf8PathBuf};
use ignore::Match;
Expand Down Expand Up @@ -34,7 +33,7 @@ pub struct CairoCoverageIgnoreMatcher(Gitignore);
impl CairoCoverageIgnoreMatcher {
/// Check if the given path is ignored by the [`CAIRO_COVERAGE_IGNORE`] file.
pub fn is_ignored(&self, path: &str) -> bool {
let path: Utf8PathBuf = VIRTUAL_FILE_REGEX.replace_all(path, "").to_string().into();
let path: Utf8PathBuf = path.to_string().into();
let result = self.0.matched(&path, path.is_dir());
matches!(result, Match::Ignore(_))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@ use crate::build::filter::ignore_matcher::CairoCoverageIgnoreMatcher;
use crate::build::filter::libfuncs;
use crate::build::filter::libfuncs::NOT_RELIABLE_LIBFUNCS;
use crate::loading::enriched_program::EnrichedProgram;
use cairo_annotations::annotations::coverage::SourceFileFullPath;
use cairo_annotations::annotations::profiler::FunctionName;
use cairo_lang_sierra::program::StatementIdx;
use camino::Utf8PathBuf;
use regex::Regex;
use std::collections::{HashMap, HashSet};
use std::sync::LazyLock;

/// Regex to match virtual files like `/path/to/project/lib.cairo[array_inline_macro][assert_macro]`
/// where `array_inline_macro` and `assert_macro` is a virtual file.
pub static VIRTUAL_FILE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\[.*?]").unwrap());

/// Statement category filter that is used to filter out statements that should not be included in the coverage report.
/// `included_components` and `ignore_matcher` are references to reduce the amount of data that needs to be copied.
Expand Down Expand Up @@ -57,32 +50,29 @@ impl StatementCategoryFilter<'_> {
&self,
idx: StatementIdx,
function_name: &FunctionName,
source_file_full_path: &SourceFileFullPath,
source_file_full_path: &str,
is_macro: bool,
) -> bool {
self.is_allowed_macro(function_name, source_file_full_path)
self.is_allowed_macro(function_name, is_macro)
&& self.is_user_function(source_file_full_path)
&& self.is_reliable_libfunc(idx)
&& self.is_not_ignored(source_file_full_path)
}

fn is_allowed_macro(
&self,
function_name: &FunctionName,
source_file_full_path: &SourceFileFullPath,
) -> bool {
fn is_allowed_macro(&self, function_name: &FunctionName, is_macro: bool) -> bool {
if self.test_functions.contains(function_name) {
self.included_components
.contains(&IncludedComponent::TestFunctions)
} else if VIRTUAL_FILE_REGEX.is_match(&source_file_full_path.0) {
} else if is_macro {
self.included_components
.contains(&IncludedComponent::Macros)
} else {
true
}
}

fn is_user_function(&self, source_file_full_path: &SourceFileFullPath) -> bool {
source_file_full_path.0.contains(&self.user_project_path)
fn is_user_function(&self, source_file_full_path: &str) -> bool {
source_file_full_path.contains(&self.user_project_path)
}

fn is_reliable_libfunc(&self, idx: StatementIdx) -> bool {
Expand All @@ -92,7 +82,7 @@ impl StatementCategoryFilter<'_> {
.is_some_and(|libfunc_name| NOT_RELIABLE_LIBFUNCS.contains(libfunc_name))
}

fn is_not_ignored(&self, source_file_full_path: &SourceFileFullPath) -> bool {
!self.ignore_matcher.is_ignored(&source_file_full_path.0)
fn is_not_ignored(&self, source_file_full_path: &str) -> bool {
!self.ignore_matcher.is_ignored(source_file_full_path)
}
}
42 changes: 12 additions & 30 deletions crates/cairo-coverage-core/src/build/statement_information.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::build::filter::statement_category_filter::{
StatementCategoryFilter, VIRTUAL_FILE_REGEX,
};
use crate::build::filter::statement_category_filter::StatementCategoryFilter;
use cairo_annotations::annotations::coverage::{
CodeLocation, CoverageAnnotationsV1, LineNumber, SourceCodeSpan, SourceFileFullPath,
VersionedCoverageAnnotations,
Expand All @@ -26,16 +24,6 @@ pub struct StatementInformation {
pub line_range: LineRange,
}

impl StatementInformation {
pub fn remove_virtual_file_prefix(&mut self) {
self.source_file_full_path = SourceFileFullPath(
VIRTUAL_FILE_REGEX
.replace_all(&self.source_file_full_path.0, "")
.to_string(),
);
}
}

#[derive(Deserialize, Clone, Eq, PartialEq)]
pub struct LineRange {
/// Line number is 1-based
Expand Down Expand Up @@ -93,24 +81,18 @@ fn get_statement_information(
function_names: Vec<FunctionName>,
filter: &StatementCategoryFilter,
) -> Option<StatementInformation> {
code_locations
.into_iter()
.zip(function_names)
.find(|(CodeLocation(source_file_full_path, _), function_name)| {
filter.should_include(idx, function_name, source_file_full_path)
})
.map(
|(CodeLocation(source_file_full_path, line_range), function_name)| {
StatementInformation {
code_locations.into_iter().zip(function_names).find_map(
|(CodeLocation(source_file_full_path, line_range, is_macro), function_name)| {
let (path, marking) = source_file_full_path.remove_virtual_file_markings();
let is_macro = is_macro.unwrap_or(!marking.is_empty());
filter
.should_include(idx, &function_name, path, is_macro)
.then(|| StatementInformation {
function_name,
source_file_full_path,
source_file_full_path: SourceFileFullPath(path.to_string()),
line_range: line_range.into(),
idx,
}
},
)
.map(|mut statement_origin| {
statement_origin.remove_virtual_file_prefix();
statement_origin
})
})
},
)
}
Loading