Skip to content

Commit

Permalink
Enhancement/renaming thread (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
TilakMaddy authored Apr 8, 2024
1 parent b4151c8 commit fa46aa9
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use crate::{
visitor::ast_visitor::{ASTConstVisitor, Node},
};

pub trait GetParentChain {
pub trait GetAncestralLine {
/// Get the parent Chain of an ASTNode
fn parent_chain<'a>(&self, context: &'a WorkspaceContext) -> Option<Vec<&'a ASTNode>>;
fn ancestral_line<'a>(&self, context: &'a WorkspaceContext) -> Option<Vec<&'a ASTNode>>;
}

#[derive(Default)]
Expand All @@ -21,15 +21,15 @@ impl ASTConstVisitor for NodeIDReceiver {
}
}

impl<T: Node + ?Sized> GetParentChain for T {
fn parent_chain<'a>(&self, context: &'a WorkspaceContext) -> Option<Vec<&'a ASTNode>> {
impl<T: Node + ?Sized> GetAncestralLine for T {
fn ancestral_line<'a>(&self, context: &'a WorkspaceContext) -> Option<Vec<&'a ASTNode>> {
// Setup a Node ID receiver
let mut node_id_receiver = NodeIDReceiver::default();

// Find the ID of the node this method is called upon
self.accept_id(&mut node_id_receiver).ok()?;
let current_node_id = node_id_receiver.id?;

Some(context.get_parent_chain(current_node_id))
Some(context.get_ancestral_line(current_node_id))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use crate::{
visitor::ast_visitor::{ASTConstVisitor, Node},
};

pub trait GetClosestParentOfTypeX {
pub trait GetClosestAncestorOfTypeX {
/// Get the parent Chain of an ASTNode
fn closest_parent_of_type<'a>(
fn closest_ancestor_of_type<'a>(
&self,
context: &'a WorkspaceContext,
node_type: NodeType,
Expand All @@ -25,8 +25,8 @@ impl ASTConstVisitor for NodeIDReceiver {
}
}

impl<T: Node + ?Sized> GetClosestParentOfTypeX for T {
fn closest_parent_of_type<'a>(
impl<T: Node + ?Sized> GetClosestAncestorOfTypeX for T {
fn closest_ancestor_of_type<'a>(
&self,
context: &'a WorkspaceContext,
node_type: NodeType,
Expand All @@ -37,6 +37,6 @@ impl<T: Node + ?Sized> GetClosestParentOfTypeX for T {
// Find the ID of the node this method is called upon
self.accept_id(&mut node_id_receiver).ok()?;
let current_node_id = node_id_receiver.id?;
context.get_closest_parent(current_node_id, node_type)
context.get_closest_ancestor(current_node_id, node_type)
}
}
8 changes: 4 additions & 4 deletions aderyn_core/src/context/browser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
mod closest_parent;
mod ancestral_line;
mod closest_ancestor;
mod extractor;
mod immediate_children;
mod location;
mod parent;
mod parent_chain;
mod sort_nodes;
pub use closest_parent::*;
pub use ancestral_line::*;
pub use closest_ancestor::*;
pub use extractor::*;
pub use immediate_children::*;
pub use location::*;
pub use parent::*;
pub use parent_chain::*;
pub use sort_nodes::*;
14 changes: 7 additions & 7 deletions aderyn_core/src/context/browser/sort_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ use std::collections::BTreeSet;
use crate::context::workspace_context::{ASTNode, WorkspaceContext};

pub trait SortNodeReferencesToSequence<'a> {
fn sort_by_line_nos(self, context: &'a WorkspaceContext) -> Option<Vec<&'a ASTNode>>;
fn sort_by_src_position(self, context: &'a WorkspaceContext) -> Option<Vec<&'a ASTNode>>;
}

pub trait SortOwnedNodesToSequence<'a> {
fn sort_by_line_nos(self, context: &'a WorkspaceContext) -> Option<Vec<ASTNode>>;
fn sort_by_src_position(self, context: &'a WorkspaceContext) -> Option<Vec<ASTNode>>;
}

impl<'a> SortNodeReferencesToSequence<'a> for &[&'a ASTNode] {
fn sort_by_line_nos(self, context: &'a WorkspaceContext) -> Option<Vec<&'a ASTNode>> {
sort_by_line_nos(self, context)
fn sort_by_src_position(self, context: &'a WorkspaceContext) -> Option<Vec<&'a ASTNode>> {
sort_by_src_position(self, context)
}
}

impl<'a> SortOwnedNodesToSequence<'a> for &[ASTNode] {
fn sort_by_line_nos(self, context: &'a WorkspaceContext) -> Option<Vec<ASTNode>> {
fn sort_by_src_position(self, context: &'a WorkspaceContext) -> Option<Vec<ASTNode>> {
let nodes = self.iter().collect::<Vec<_>>();
let sorted = sort_by_line_nos(&nodes, context);
let sorted = sort_by_src_position(&nodes, context);
if let Some(sorted_nodes) = sorted {
let owned_nodes = sorted_nodes.iter().map(|&x| x.clone()).collect::<Vec<_>>();
return Some(owned_nodes);
Expand All @@ -28,7 +28,7 @@ impl<'a> SortOwnedNodesToSequence<'a> for &[ASTNode] {
}
}

fn sort_by_line_nos<'a>(
fn sort_by_src_position<'a>(
nodes: &[&'a ASTNode],
context: &'a WorkspaceContext,
) -> Option<Vec<&'a ASTNode>> {
Expand Down
4 changes: 2 additions & 2 deletions aderyn_core/src/context/workspace_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ impl WorkspaceContext {
self.nodes.get(self.parent_link.get(&node_id)?)
}

pub fn get_parent_chain(&self, node_id: NodeID) -> Vec<&ASTNode> {
pub fn get_ancestral_line(&self, node_id: NodeID) -> Vec<&ASTNode> {
let mut chain = vec![];
let mut parent = self.nodes.get(&node_id);
while let Some(next_parent) = parent {
Expand All @@ -1225,7 +1225,7 @@ impl WorkspaceContext {
}
chain
}
pub fn get_closest_parent(&self, node_id: NodeID, node_type: NodeType) -> Option<&ASTNode> {
pub fn get_closest_ancestor(&self, node_id: NodeID, node_type: NodeType) -> Option<&ASTNode> {
let mut current_node_id = self.parent_link.get(&node_id)?;
while let Some(current) = self.nodes.get(current_node_id) {
if current.node_type() == node_type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use crate::{
ast::NodeID,
capture,
context::{
browser::{GetParentChain, SortNodeReferencesToSequence},
browser::{GetAncestralLine, SortNodeReferencesToSequence},
workspace_context::{ASTNode, WorkspaceContext},
},
detect::detector::{IssueDetector, IssueDetectorNamePool, IssueSeverity},
};
use eyre::Result;

#[derive(Default)]
pub struct ParentChainDemonstrator {
pub struct AncestralLineDemonstrator {
// Keys are source file name and line number
found_instances: BTreeMap<(String, usize, String), NodeID>,
}
Expand All @@ -23,12 +23,12 @@ pub struct ParentChainDemonstrator {
In ParentChainContract.sol, there is only 1 assignment done. The goal is to capture it first, second and third parent
*/

impl IssueDetector for ParentChainDemonstrator {
impl IssueDetector for AncestralLineDemonstrator {
fn detect(&mut self, context: &WorkspaceContext) -> Result<bool, Box<dyn Error>> {
for assignment in context.assignments() {
capture!(self, context, assignment);

if let Some(parent_chain) = assignment.parent_chain(context) {
if let Some(parent_chain) = assignment.ancestral_line(context) {
if let ASTNode::Block(_) = parent_chain[1] {
capture!(self, context, parent_chain[1]);
}
Expand All @@ -40,8 +40,8 @@ impl IssueDetector for ParentChainDemonstrator {
}
}

if let Some(mut parent_chain) = assignment.parent_chain(context) {
let sorted_chain = parent_chain.sort_by_line_nos(context).unwrap();
if let Some(mut parent_chain) = assignment.ancestral_line(context) {
let sorted_chain = parent_chain.sort_by_src_position(context).unwrap();
parent_chain.reverse();
assert_eq!(sorted_chain, parent_chain);
}
Expand Down Expand Up @@ -72,19 +72,19 @@ impl IssueDetector for ParentChainDemonstrator {
}

#[cfg(test)]
mod parent_chain_demo_tests {
mod ancestral_line_demo_tests {
use crate::detect::{
detector::{detector_test_helpers::load_contract, IssueDetector},
experimental::parent_chain::ParentChainDemonstrator,
experimental::ancestral_line::AncestralLineDemonstrator,
};

#[test]
fn test_parent_chain_demo() {
fn test_ancestral_line_demo() {
let context = load_contract(
"../tests/contract-playground/out/ParentChainContract.sol/ParentChainContract.json",
);

let mut detector = ParentChainDemonstrator::default();
let mut detector = AncestralLineDemonstrator::default();
let found = detector.detect(&context).unwrap();
assert!(found);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use crate::{
ast::{NodeID, NodeType},
capture,
context::{
browser::GetClosestParentOfTypeX,
browser::GetClosestAncestorOfTypeX,
workspace_context::{ASTNode, WorkspaceContext},
},
detect::detector::{IssueDetector, IssueDetectorNamePool, IssueSeverity},
};
use eyre::Result;

#[derive(Default)]
pub struct ClosestParentDemonstrator {
pub struct ClosestAncestorDemonstrator {
// Keys are source file name and line number
found_instances: BTreeMap<(String, usize, String), NodeID>,
}
Expand All @@ -23,24 +23,24 @@ pub struct ClosestParentDemonstrator {
In ParentChainContract.sol, there is only 1 assignment done. The goal is to capture it first, second and third parent
*/

impl IssueDetector for ClosestParentDemonstrator {
impl IssueDetector for ClosestAncestorDemonstrator {
fn detect(&mut self, context: &WorkspaceContext) -> Result<bool, Box<dyn Error>> {
for assignment in context.assignments() {
capture!(self, context, assignment);

if let Some(ASTNode::Block(block)) =
assignment.closest_parent_of_type(context, NodeType::Block)
assignment.closest_ancestor_of_type(context, NodeType::Block)
{
capture!(self, context, block);
}

if let Some(for_statement) =
assignment.closest_parent_of_type(context, NodeType::ForStatement)
assignment.closest_ancestor_of_type(context, NodeType::ForStatement)
{
capture!(self, context, for_statement);

if let Some(ASTNode::Block(block)) =
for_statement.closest_parent_of_type(context, NodeType::Block)
for_statement.closest_ancestor_of_type(context, NodeType::Block)
{
capture!(self, context, block);
}
Expand Down Expand Up @@ -72,19 +72,19 @@ impl IssueDetector for ClosestParentDemonstrator {
}

#[cfg(test)]
mod parent_chain_demo_tests {
mod closest_ancestor_demo_tests {
use crate::detect::{
detector::{detector_test_helpers::load_contract, IssueDetector},
experimental::closest_parent::ClosestParentDemonstrator,
experimental::closest_ancestor::ClosestAncestorDemonstrator,
};

#[test]
fn test_closest_parent() {
fn test_closest_ancestor() {
let context = load_contract(
"../tests/contract-playground/out/ParentChainContract.sol/ParentChainContract.json",
);

let mut detector = ClosestParentDemonstrator::default();
let mut detector = ClosestAncestorDemonstrator::default();
let found = detector.detect(&context).unwrap();
assert!(found);

Expand Down
4 changes: 2 additions & 2 deletions aderyn_core/src/detect/experimental/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub(crate) mod closest_parent;
pub(crate) mod ancestral_line;
pub(crate) mod closest_ancestor;
pub(crate) mod immediate_children;
pub(crate) mod immediate_parent;
pub(crate) mod parent_chain;
6 changes: 3 additions & 3 deletions aderyn_core/src/detect/low/deprecated_oz_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
ast::{NodeID, NodeType},
capture,
context::{
browser::GetClosestParentOfTypeX,
browser::GetClosestAncestorOfTypeX,
workspace_context::{ASTNode, WorkspaceContext},
},
detect::detector::{IssueDetector, IssueDetectorNamePool, IssueSeverity},
Expand All @@ -23,7 +23,7 @@ impl IssueDetector for DeprecatedOZFunctionsDetector {
// if source_unit has any ImportDirectives with absolute_path containing "openzeppelin"
// call identifier.accept(self)
if let Some(ASTNode::SourceUnit(source_unit)) =
identifier.closest_parent_of_type(context, NodeType::SourceUnit)
identifier.closest_ancestor_of_type(context, NodeType::SourceUnit)
{
let import_directives = source_unit.import_directives();
if import_directives.iter().any(|directive| {
Expand All @@ -43,7 +43,7 @@ impl IssueDetector for DeprecatedOZFunctionsDetector {
// if source_unit has any ImportDirectives with absolute_path containing "openzeppelin"
// call member_access.accept(self)
if let Some(ASTNode::SourceUnit(source_unit)) =
member_access.closest_parent_of_type(context, NodeType::SourceUnit)
member_access.closest_ancestor_of_type(context, NodeType::SourceUnit)
{
let import_directives = source_unit.import_directives();
if import_directives.iter().any(|directive| {
Expand Down
4 changes: 2 additions & 2 deletions aderyn_core/src/detect/medium/unsafe_oz_erc721_mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
ast::{NodeID, NodeType},
capture,
context::{
browser::GetClosestParentOfTypeX,
browser::GetClosestAncestorOfTypeX,
workspace_context::{ASTNode, WorkspaceContext},
},
detect::detector::{IssueDetector, IssueDetectorNamePool, IssueSeverity},
Expand All @@ -23,7 +23,7 @@ impl IssueDetector for UnsafeERC721MintDetector {
// if source_unit has any ImportDirectives with absolute_path containing "openzeppelin"
// call identifier.accept(self)
if let Some(ASTNode::SourceUnit(source_unit)) =
identifier.closest_parent_of_type(context, NodeType::SourceUnit)
identifier.closest_ancestor_of_type(context, NodeType::SourceUnit)
{
let import_directives = source_unit.import_directives();
if import_directives.iter().any(|directive| {
Expand Down
6 changes: 3 additions & 3 deletions aderyn_core/src/detect/nc/empty_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
ast::{FunctionKind, NodeID, NodeType},
capture,
context::{
browser::{GetClosestParentOfTypeX, GetParentChain},
browser::{GetAncestralLine, GetClosestAncestorOfTypeX},
workspace_context::{ASTNode, WorkspaceContext},
},
detect::detector::{IssueDetector, IssueDetectorNamePool, IssueSeverity},
Expand All @@ -21,7 +21,7 @@ impl IssueDetector for EmptyBlockDetector {
fn detect(&mut self, context: &WorkspaceContext) -> Result<bool, Box<dyn Error>> {
for empty_block in context.blocks().iter().filter(|b| b.statements.is_empty()) {
if let Some(ASTNode::FunctionDefinition(f)) =
&empty_block.closest_parent_of_type(context, NodeType::FunctionDefinition)
&empty_block.closest_ancestor_of_type(context, NodeType::FunctionDefinition)
{
// It's okay to have empty block if it's a constructor, receive, or fallback
if f.kind == FunctionKind::Function {
Expand All @@ -31,7 +31,7 @@ impl IssueDetector for EmptyBlockDetector {
|| f.kind == FunctionKind::Fallback
{
// It's not okay to have empty block nested somewhere inside constructor
if let Some(block_chain) = empty_block.parent_chain(context) {
if let Some(block_chain) = empty_block.ancestral_line(context) {
let function_definition_index = block_chain
.iter()
.position(|x| x.node_type() == NodeType::FunctionDefinition)
Expand Down
4 changes: 2 additions & 2 deletions aderyn_core/src/detect/nc/zero_address_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl IssueDetector for ZeroAddressCheckDetector {
mod zero_address_check_tests {
use crate::{
ast::NodeType,
context::{browser::GetClosestParentOfTypeX, workspace_context::ASTNode},
context::{browser::GetClosestAncestorOfTypeX, workspace_context::ASTNode},
detect::{
detector::{detector_test_helpers::load_contract, IssueDetector},
nc::zero_address_check::ZeroAddressCheckDetector,
Expand All @@ -209,7 +209,7 @@ mod zero_address_check_tests {
for node_id in detector.instances().values() {
if let ASTNode::Assignment(assignment) = context.nodes.get(node_id).unwrap() {
if let ASTNode::FunctionDefinition(function) = assignment
.closest_parent_of_type(&context, NodeType::FunctionDefinition)
.closest_ancestor_of_type(&context, NodeType::FunctionDefinition)
.unwrap()
{
assert!(function.name.contains("bad"));
Expand Down
Binary file modified nyth/archive.zip
Binary file not shown.

0 comments on commit fa46aa9

Please sign in to comment.