Skip to content

Commit

Permalink
fix(coverage): special functions have no name (#9441)
Browse files Browse the repository at this point in the history
* fix(coverage): special functions have no name

* test: don't to_string

* test: rm --summary which is not --report=summary

* test: add regression test for #9437

* fmt

* docs
  • Loading branch information
DaniPopes authored Dec 2, 2024
1 parent ac81a53 commit 168b239
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 124 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

15 changes: 8 additions & 7 deletions crates/evm/coverage/src/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,20 @@ impl<'a> ContractVisitor<'a> {
fn visit_function_definition(&mut self, node: &Node) -> eyre::Result<()> {
let Some(body) = &node.body else { return Ok(()) };

let kind: String =
node.attribute("kind").ok_or_else(|| eyre::eyre!("Function has no kind"))?;

let name: String =
node.attribute("name").ok_or_else(|| eyre::eyre!("Function has no name"))?;
let kind: String =
node.attribute("kind").ok_or_else(|| eyre::eyre!("Function has no kind"))?;

// Do not add coverage item for constructors without statements.
if kind == "constructor" && !has_statements(body) {
return Ok(())
}

// `fallback`, `receive`, and `constructor` functions have an empty `name`.
// Use the `kind` itself as the name.
let name = if name.is_empty() { kind } else { name };

self.push_item_kind(CoverageItemKind::Function { name }, &node.src);
self.visit_block(body)
}
Expand Down Expand Up @@ -498,10 +502,7 @@ fn has_statements(node: &Node) -> bool {
NodeType::TryStatement |
NodeType::VariableDeclarationStatement |
NodeType::WhileStatement => true,
_ => {
let statements: Vec<Node> = node.attribute("statements").unwrap_or_default();
!statements.is_empty()
}
_ => node.attribute::<Vec<Node>>("statements").is_some_and(|s| !s.is_empty()),
}
}

Expand Down
5 changes: 3 additions & 2 deletions crates/forge/bin/cmd/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,10 @@ impl CoverageArgs {
}
}

// TODO: HTML
#[derive(Clone, Debug, ValueEnum)]
/// Coverage reports to generate.
#[derive(Clone, Debug, Default, ValueEnum)]
pub enum CoverageReportKind {
#[default]
Summary,
Lcov,
Debug,
Expand Down
5 changes: 3 additions & 2 deletions crates/forge/src/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ impl CoverageReporter for LcovReporter<'_> {

for item in items {
let line = item.loc.lines.start;
let line_end = item.loc.lines.end - 1;
// `lines` is half-open, so we need to subtract 1 to get the last included line.
let end_line = item.loc.lines.end - 1;
let hits = item.hits;
match item.kind {
CoverageItemKind::Function { ref name } => {
let name = format!("{}.{name}", item.loc.contract_name);
writeln!(self.out, "FN:{line},{line_end},{name}")?;
writeln!(self.out, "FN:{line},{end_line},{name}")?;
writeln!(self.out, "FNDA:{hits},{name}")?;
}
CoverageItemKind::Line => {
Expand Down
Loading

0 comments on commit 168b239

Please sign in to comment.