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

feat: coverage for modifiers #7669

Merged
merged 1 commit into from
Apr 16, 2024
Merged
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
34 changes: 30 additions & 4 deletions crates/evm/coverage/src/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ impl<'a> ContractVisitor<'a> {
pub fn visit(mut self, contract_ast: Node) -> eyre::Result<Self> {
// Find all functions and walk their AST
for node in contract_ast.nodes {
if node.node_type == NodeType::FunctionDefinition {
self.visit_function_definition(node.clone())?;
match &node.node_type {
NodeType::FunctionDefinition => {
self.visit_function_definition(node)?;
}
NodeType::ModifierDefinition => {
self.visit_modifier_definition(node)?;
}
_ => {}
}
}

Expand Down Expand Up @@ -68,6 +74,23 @@ impl<'a> ContractVisitor<'a> {
}
}

fn visit_modifier_definition(&mut self, mut node: Node) -> eyre::Result<()> {
let name: String =
node.attribute("name").ok_or_else(|| eyre::eyre!("Modifier has no name"))?;

match node.body.take() {
Some(body) => {
self.push_item(CoverageItem {
kind: CoverageItemKind::Function { name },
loc: self.source_location_for(&node.src),
hits: 0,
});
self.visit_block(*body)
}
_ => Ok(()),
}
}

fn visit_block(&mut self, node: Node) -> eyre::Result<()> {
let statements: Vec<Node> = node.attribute("statements").unwrap_or_default();

Expand All @@ -93,7 +116,6 @@ impl<'a> ContractVisitor<'a> {
NodeType::Break |
NodeType::Continue |
NodeType::EmitStatement |
NodeType::PlaceholderStatement |
NodeType::RevertStatement |
NodeType::YulAssignment |
NodeType::YulBreak |
Expand All @@ -107,6 +129,9 @@ impl<'a> ContractVisitor<'a> {
Ok(())
}

// Skip placeholder statements as they are never referenced in source maps.
NodeType::PlaceholderStatement => Ok(()),

// Return with eventual subcall
NodeType::Return => {
self.push_item(CoverageItem {
Expand Down Expand Up @@ -341,12 +366,13 @@ impl<'a> ContractVisitor<'a> {
NodeType::ForStatement |
NodeType::IfStatement |
NodeType::InlineAssembly |
NodeType::PlaceholderStatement |
NodeType::Return |
NodeType::RevertStatement |
NodeType::TryStatement |
NodeType::VariableDeclarationStatement |
NodeType::WhileStatement => self.visit_statement(node),
// Skip placeholder statements as they are never referenced in source maps.
NodeType::PlaceholderStatement => Ok(()),
_ => {
warn!("unexpected node type, expected block or statement: {:?}", node.node_type);
Ok(())
Expand Down
Loading