Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(coverage): special functions have no name #9441

Merged
merged 6 commits into from
Dec 2, 2024
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
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