Skip to content

Commit

Permalink
Merge pull request #1636 from crytic/simplify-enums
Browse files Browse the repository at this point in the history
use enum strings instead of impl. __str__
  • Loading branch information
montyly authored Feb 14, 2023
2 parents 540985d + 6b50684 commit 8548bed
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 134 deletions.
81 changes: 21 additions & 60 deletions slither/core/cfg/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,80 +66,41 @@


class NodeType(Enum):
ENTRYPOINT = 0x0 # no expression
ENTRYPOINT = "ENTRY_POINT" # no expression

# Node with expression
# Nodes that may have an expression

EXPRESSION = 0x10 # normal case
RETURN = 0x11 # RETURN may contain an expression
IF = 0x12
VARIABLE = 0x13 # Declaration of variable
ASSEMBLY = 0x14
IFLOOP = 0x15
EXPRESSION = "EXPRESSION" # normal case
RETURN = "RETURN" # RETURN may contain an expression
IF = "IF"
VARIABLE = "NEW VARIABLE" # Variable declaration
ASSEMBLY = "INLINE ASM"
IFLOOP = "IF_LOOP"

# Merging nodes
# Nodes where control flow merges
# Can have phi IR operation
ENDIF = 0x50 # ENDIF node source mapping points to the if/else body
STARTLOOP = 0x51 # STARTLOOP node source mapping points to the entire loop body
ENDLOOP = 0x52 # ENDLOOP node source mapping points to the entire loop body
ENDIF = "END_IF" # ENDIF node source mapping points to the if/else "body"
STARTLOOP = "BEGIN_LOOP" # STARTLOOP node source mapping points to the entire loop "body"
ENDLOOP = "END_LOOP" # ENDLOOP node source mapping points to the entire loop "body"

# Below the nodes have no expression
# But are used to expression CFG structure
# Below the nodes do not have an expression but are used to expression CFG structure.

# Absorbing node
THROW = 0x20
THROW = "THROW"

# Loop related nodes
BREAK = 0x31
CONTINUE = 0x32
BREAK = "BREAK"
CONTINUE = "CONTINUE"

# Only modifier node
PLACEHOLDER = 0x40
PLACEHOLDER = "_"

TRY = 0x41
CATCH = 0x42
TRY = "TRY"
CATCH = "CATCH"

# Node not related to the CFG
# Use for state variable declaration
OTHER_ENTRYPOINT = 0x60

# @staticmethod
def __str__(self):
if self == NodeType.ENTRYPOINT:
return "ENTRY_POINT"
if self == NodeType.EXPRESSION:
return "EXPRESSION"
if self == NodeType.RETURN:
return "RETURN"
if self == NodeType.IF:
return "IF"
if self == NodeType.VARIABLE:
return "NEW VARIABLE"
if self == NodeType.ASSEMBLY:
return "INLINE ASM"
if self == NodeType.IFLOOP:
return "IF_LOOP"
if self == NodeType.THROW:
return "THROW"
if self == NodeType.BREAK:
return "BREAK"
if self == NodeType.CONTINUE:
return "CONTINUE"
if self == NodeType.PLACEHOLDER:
return "_"
if self == NodeType.TRY:
return "TRY"
if self == NodeType.CATCH:
return "CATCH"
if self == NodeType.ENDIF:
return "END_IF"
if self == NodeType.STARTLOOP:
return "BEGIN_LOOP"
if self == NodeType.ENDLOOP:
return "END_LOOP"
if self == NodeType.OTHER_ENTRYPOINT:
return "OTHER_ENTRYPOINT"
return f"Unknown type {hex(self.value)}"
OTHER_ENTRYPOINT = "OTHER_ENTRYPOINT"


# endregion
Expand Down Expand Up @@ -1014,7 +975,7 @@ def __str__(self):
additional_info += " " + str(self.expression)
elif self.variable_declaration:
additional_info += " " + str(self.variable_declaration)
txt = str(self._node_type) + additional_info
txt = self._node_type.value + additional_info
return txt


Expand Down
2 changes: 1 addition & 1 deletion slither/core/declarations/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ def slithir_cfg_to_dot_str(self, skip_expressions=False) -> str:
content = ""
content += "digraph{\n"
for node in self.nodes:
label = f"Node Type: {str(node.type)} {node.node_id}\n"
label = f"Node Type: {node.type.value} {node.node_id}\n"
if node.expression and not skip_expressions:
label += f"\nEXPRESSION:\n{node.expression}\n"
if node.irs and not skip_expressions:
Expand Down
83 changes: 21 additions & 62 deletions slither/slithir/operations/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@


class BinaryType(Enum):
POWER = 0 # **
MULTIPLICATION = 1 # *
DIVISION = 2 # /
MODULO = 3 # %
ADDITION = 4 # +
SUBTRACTION = 5 # -
LEFT_SHIFT = 6 # <<
RIGHT_SHIFT = 7 # >>
AND = 8 # &
CARET = 9 # ^
OR = 10 # |
LESS = 11 # <
GREATER = 12 # >
LESS_EQUAL = 13 # <=
GREATER_EQUAL = 14 # >=
EQUAL = 15 # ==
NOT_EQUAL = 16 # !=
ANDAND = 17 # &&
OROR = 18 # ||
POWER = "**"
MULTIPLICATION = "*"
DIVISION = "/"
MODULO = "%"
ADDITION = "+"
SUBTRACTION = "-"
LEFT_SHIFT = "<<"
RIGHT_SHIFT = ">>"
AND = "&"
CARET = "^"
OR = "|"
LESS = "<"
GREATER = ">"
LESS_EQUAL = "<="
GREATER_EQUAL = ">="
EQUAL = "=="
NOT_EQUAL = "!="
ANDAND = "&&"
OROR = "||"

@staticmethod
def return_bool(operation_type):
Expand Down Expand Up @@ -98,47 +98,6 @@ def can_be_checked_for_overflow(self):
BinaryType.DIVISION,
]

def __str__(self): # pylint: disable=too-many-branches
if self == BinaryType.POWER:
return "**"
if self == BinaryType.MULTIPLICATION:
return "*"
if self == BinaryType.DIVISION:
return "/"
if self == BinaryType.MODULO:
return "%"
if self == BinaryType.ADDITION:
return "+"
if self == BinaryType.SUBTRACTION:
return "-"
if self == BinaryType.LEFT_SHIFT:
return "<<"
if self == BinaryType.RIGHT_SHIFT:
return ">>"
if self == BinaryType.AND:
return "&"
if self == BinaryType.CARET:
return "^"
if self == BinaryType.OR:
return "|"
if self == BinaryType.LESS:
return "<"
if self == BinaryType.GREATER:
return ">"
if self == BinaryType.LESS_EQUAL:
return "<="
if self == BinaryType.GREATER_EQUAL:
return ">="
if self == BinaryType.EQUAL:
return "=="
if self == BinaryType.NOT_EQUAL:
return "!="
if self == BinaryType.ANDAND:
return "&&"
if self == BinaryType.OROR:
return "||"
raise SlithIRError(f"str: Unknown operation type {self} {type(self)})")


class Binary(OperationWithLValue):
def __init__(self, result, left_variable, right_variable, operation_type: BinaryType):
Expand Down Expand Up @@ -178,8 +137,8 @@ def type(self):
@property
def type_str(self):
if self.node.scope.is_checked and self._type.can_be_checked_for_overflow():
return "(c)" + str(self._type)
return str(self._type)
return "(c)" + self._type.value
return self._type.value

def __str__(self):
if isinstance(self.lvalue, ReferenceVariable):
Expand Down
14 changes: 3 additions & 11 deletions slither/slithir/operations/unary.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@


class UnaryType(Enum):
BANG = 0 # !
TILD = 1 # ~
BANG = "!"
TILD = "~"

@staticmethod
def get_type(operation_type, isprefix):
Expand All @@ -21,14 +21,6 @@ def get_type(operation_type, isprefix):
return UnaryType.TILD
raise SlithIRError(f"get_type: Unknown operation type {operation_type}")

def __str__(self):
if self == UnaryType.BANG:
return "!"
if self == UnaryType.TILD:
return "~"

raise SlithIRError(f"str: Unknown operation type {self}")


class Unary(OperationWithLValue):
def __init__(self, result, variable, operation_type):
Expand All @@ -53,7 +45,7 @@ def type(self):

@property
def type_str(self):
return str(self._type)
return self._type.value

def __str__(self):
return f"{self.lvalue} = {self.type_str} {self.rvalue} "

0 comments on commit 8548bed

Please sign in to comment.