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

Add more types #1624

Merged
merged 20 commits into from
Feb 15, 2023
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
16 changes: 11 additions & 5 deletions slither/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
from slither.printers.abstract_printer import AbstractPrinter
from slither.slither import Slither
from slither.utils import codex
from slither.utils.output import output_to_json, output_to_zip, output_to_sarif, ZIP_TYPES_ACCEPTED
from slither.utils.output import (
output_to_json,
output_to_zip,
output_to_sarif,
ZIP_TYPES_ACCEPTED,
Output,
)
from slither.utils.output_capture import StandardOutputCapture
from slither.utils.colors import red, set_colorization_enabled
from slither.utils.command_line import (
Expand Down Expand Up @@ -112,7 +118,7 @@ def _process(
slither: Slither,
detector_classes: List[Type[AbstractDetector]],
printer_classes: List[Type[AbstractPrinter]],
) -> Tuple[Slither, List[Dict], List[Dict], int]:
) -> Tuple[Slither, List[Dict], List[Output], int]:
for detector_cls in detector_classes:
slither.register_detector(detector_cls)

Expand All @@ -125,9 +131,9 @@ def _process(
results_printers = []

if not printer_classes:
detector_results = slither.run_detectors()
detector_results = [x for x in detector_results if x] # remove empty results
detector_results = [item for sublist in detector_results for item in sublist] # flatten
detector_resultss = slither.run_detectors()
detector_resultss = [x for x in detector_resultss if x] # remove empty results
detector_results = [item for sublist in detector_resultss for item in sublist] # flatten
results_detectors.extend(detector_results)

else:
Expand Down
57 changes: 28 additions & 29 deletions slither/core/cfg/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
from enum import Enum
from typing import Optional, List, Set, Dict, Tuple, Union, TYPE_CHECKING

from slither.all_exceptions import SlitherException
from slither.core.children.child_function import ChildFunction
from slither.core.declarations import Contract, Function
from slither.core.declarations.solidity_variables import (
SolidityVariable,
SolidityFunction,
)
from slither.core.expressions.expression import Expression
from slither.core.solidity_types import ElementaryType
from slither.core.source_mapping.source_mapping import SourceMapping
from slither.core.variables.local_variable import LocalVariable
from slither.core.variables.state_variable import StateVariable
from slither.core.variables.variable import Variable
from slither.core.solidity_types import ElementaryType
from slither.slithir.convert import convert_expression
from slither.slithir.operations import (
HighLevelCall,
Expand All @@ -38,10 +41,6 @@
TemporaryVariable,
TupleVariable,
)
from slither.all_exceptions import SlitherException
from slither.core.declarations import Contract, Function

from slither.core.expressions.expression import Expression

if TYPE_CHECKING:
from slither.slithir.variables.variable import SlithIRVariable
Expand Down Expand Up @@ -119,7 +118,7 @@ def __init__(
node_id: int,
scope: Union["Scope", "Function"],
file_scope: "FileScope",
):
) -> None:
super().__init__()
self._node_type = node_type

Expand Down Expand Up @@ -474,11 +473,11 @@ def expression(self) -> Optional[Expression]:
"""
return self._expression

def add_expression(self, expression: Expression, bypass_verif_empty: bool = False):
def add_expression(self, expression: Expression, bypass_verif_empty: bool = False) -> None:
assert self._expression is None or bypass_verif_empty
self._expression = expression

def add_variable_declaration(self, var: LocalVariable):
def add_variable_declaration(self, var: LocalVariable) -> None:
assert self._variable_declaration is None
self._variable_declaration = var
if var.expression:
Expand Down Expand Up @@ -511,7 +510,7 @@ def contains_require_or_assert(self) -> bool:
for c in self.internal_calls
)

def contains_if(self, include_loop=True) -> bool:
def contains_if(self, include_loop: bool = True) -> bool:
"""
Check if the node is a IF node
Returns:
Expand All @@ -521,7 +520,7 @@ def contains_if(self, include_loop=True) -> bool:
return self.type in [NodeType.IF, NodeType.IFLOOP]
return self.type == NodeType.IF

def is_conditional(self, include_loop=True) -> bool:
def is_conditional(self, include_loop: bool = True) -> bool:
"""
Check if the node is a conditional node
A conditional node is either a IF or a require/assert or a RETURN bool
Expand Down Expand Up @@ -550,7 +549,7 @@ def is_conditional(self, include_loop=True) -> bool:
def inline_asm(self) -> Optional[Union[str, Dict]]:
return self._asm_source_code

def add_inline_asm(self, asm: Union[str, Dict]):
def add_inline_asm(self, asm: Union[str, Dict]) -> None:
self._asm_source_code = asm

# endregion
Expand All @@ -560,7 +559,7 @@ def add_inline_asm(self, asm: Union[str, Dict]):
###################################################################################
###################################################################################

def add_father(self, father: "Node"):
def add_father(self, father: "Node") -> None:
"""Add a father node

Args:
Expand All @@ -585,31 +584,31 @@ def fathers(self) -> List["Node"]:
"""
return list(self._fathers)

def remove_father(self, father: "Node"):
def remove_father(self, father: "Node") -> None:
"""Remove the father node. Do nothing if the node is not a father

Args:
:param father:
"""
self._fathers = [x for x in self._fathers if x.node_id != father.node_id]

def remove_son(self, son: "Node"):
def remove_son(self, son: "Node") -> None:
"""Remove the son node. Do nothing if the node is not a son

Args:
:param son:
"""
self._sons = [x for x in self._sons if x.node_id != son.node_id]

def add_son(self, son: "Node"):
def add_son(self, son: "Node") -> None:
"""Add a son node

Args:
son: son to add
"""
self._sons.append(son)

def set_sons(self, sons: List["Node"]):
def set_sons(self, sons: List["Node"]) -> None:
"""Set the son nodes

Args:
Expand Down Expand Up @@ -667,14 +666,14 @@ def irs_ssa(self) -> List[Operation]:
def irs_ssa(self, irs):
self._irs_ssa = irs

def add_ssa_ir(self, ir: Operation):
def add_ssa_ir(self, ir: Operation) -> None:
"""
Use to place phi operation
"""
ir.set_node(self)
self._irs_ssa.append(ir)

def slithir_generation(self):
def slithir_generation(self) -> None:
if self.expression:
expression = self.expression
self._irs = convert_expression(expression, self)
Expand All @@ -691,11 +690,11 @@ def all_slithir_operations(self) -> List[Operation]:
return self._all_slithir_operations

@staticmethod
def _is_non_slithir_var(var: Variable):
def _is_non_slithir_var(var: Variable) -> bool:
return not isinstance(var, (Constant, ReferenceVariable, TemporaryVariable, TupleVariable))

@staticmethod
def _is_valid_slithir_var(var: Variable):
def _is_valid_slithir_var(var: Variable) -> bool:
return isinstance(var, (ReferenceVariable, TemporaryVariable, TupleVariable))

# endregion
Expand Down Expand Up @@ -746,7 +745,7 @@ def dominance_frontier(self, doms: Set["Node"]):
self._dominance_frontier = doms

@property
def dominator_successors(self):
def dominator_successors(self) -> Set["Node"]:
return self._dom_successors

@property
Expand Down Expand Up @@ -788,14 +787,14 @@ def phi_origins_state_variables(
# def phi_origin_member_variables(self) -> Dict[str, Tuple[MemberVariable, Set["Node"]]]:
# return self._phi_origins_member_variables

def add_phi_origin_local_variable(self, variable: LocalVariable, node: "Node"):
def add_phi_origin_local_variable(self, variable: LocalVariable, node: "Node") -> None:
if variable.name not in self._phi_origins_local_variables:
self._phi_origins_local_variables[variable.name] = (variable, set())
(v, nodes) = self._phi_origins_local_variables[variable.name]
assert v == variable
nodes.add(node)

def add_phi_origin_state_variable(self, variable: StateVariable, node: "Node"):
def add_phi_origin_state_variable(self, variable: StateVariable, node: "Node") -> None:
if variable.canonical_name not in self._phi_origins_state_variables:
self._phi_origins_state_variables[variable.canonical_name] = (
variable,
Expand All @@ -819,7 +818,7 @@ def add_phi_origin_state_variable(self, variable: StateVariable, node: "Node"):
###################################################################################
###################################################################################

def _find_read_write_call(self): # pylint: disable=too-many-statements
def _find_read_write_call(self) -> None: # pylint: disable=too-many-statements

for ir in self.irs:

Expand Down Expand Up @@ -895,7 +894,7 @@ def _find_read_write_call(self): # pylint: disable=too-many-statements
self._low_level_calls = list(set(self._low_level_calls))

@staticmethod
def _convert_ssa(v: Variable):
def _convert_ssa(v: Variable) -> Optional[Union[StateVariable, LocalVariable]]:
if isinstance(v, StateIRVariable):
contract = v.contract
non_ssa_var = contract.get_state_variable_from_name(v.name)
Expand All @@ -905,7 +904,7 @@ def _convert_ssa(v: Variable):
non_ssa_var = function.get_local_variable_from_name(v.name)
return non_ssa_var

def update_read_write_using_ssa(self):
def update_read_write_using_ssa(self) -> None:
if not self.expression:
return
for ir in self.irs_ssa:
Expand Down Expand Up @@ -969,7 +968,7 @@ def update_read_write_using_ssa(self):
###################################################################################
###################################################################################

def __str__(self):
def __str__(self) -> str:
additional_info = ""
if self.expression:
additional_info += " " + str(self.expression)
Expand All @@ -987,12 +986,12 @@ def __str__(self):
###################################################################################


def link_nodes(node1: Node, node2: Node):
def link_nodes(node1: Node, node2: Node) -> None:
node1.add_son(node2)
node2.add_father(node1)


def insert_node(origin: Node, node_inserted: Node):
def insert_node(origin: Node, node_inserted: Node) -> None:
sons = origin.sons
link_nodes(origin, node_inserted)
for son in sons:
Expand Down
2 changes: 1 addition & 1 deletion slither/core/cfg/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# pylint: disable=too-few-public-methods
class Scope:
def __init__(self, is_checked: bool, is_yul: bool, scope: Union["Scope", "Function"]):
def __init__(self, is_checked: bool, is_yul: bool, scope: Union["Scope", "Function"]) -> None:
self.nodes: List["Node"] = []
self.is_checked = is_checked
self.is_yul = is_yul
Expand Down
4 changes: 2 additions & 2 deletions slither/core/children/child_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@


class ChildContract(SourceMapping):
def __init__(self):
def __init__(self) -> None:
super().__init__()
self._contract = None

def set_contract(self, contract: "Contract"):
def set_contract(self, contract: "Contract") -> None:
self._contract = contract

@property
Expand Down
2 changes: 1 addition & 1 deletion slither/core/children/child_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class ChildEvent:
def __init__(self):
def __init__(self) -> None:
super().__init__()
self._event = None

Expand Down
9 changes: 5 additions & 4 deletions slither/core/children/child_expression.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Union

if TYPE_CHECKING:
from slither.core.expressions.expression import Expression
from slither.slithir.operations import Operation


class ChildExpression:
def __init__(self):
def __init__(self) -> None:
super().__init__()
self._expression = None

def set_expression(self, expression: "Expression"):
def set_expression(self, expression: Union["Expression", "Operation"]) -> None:
self._expression = expression

@property
def expression(self) -> "Expression":
def expression(self) -> Union["Expression", "Operation"]:
return self._expression
4 changes: 2 additions & 2 deletions slither/core/children/child_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@


class ChildInheritance:
def __init__(self):
def __init__(self) -> None:
super().__init__()
self._contract_declarer = None

def set_contract_declarer(self, contract: "Contract"):
def set_contract_declarer(self, contract: "Contract") -> None:
self._contract_declarer = contract

@property
Expand Down
4 changes: 2 additions & 2 deletions slither/core/children/child_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@


class ChildNode:
def __init__(self):
def __init__(self) -> None:
super().__init__()
self._node = None

def set_node(self, node: "Node"):
def set_node(self, node: "Node") -> None:
self._node = node

@property
Expand Down
4 changes: 2 additions & 2 deletions slither/core/children/child_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@


class ChildStructure:
def __init__(self):
def __init__(self) -> None:
super().__init__()
self._structure = None

def set_structure(self, structure: "Structure"):
def set_structure(self, structure: "Structure") -> None:
self._structure = structure

@property
Expand Down
Loading