Merge pull request #1636 from crytic/simplify-enums

use enum strings instead of impl. __str__
pull/1691/head
Feist Josselin 2 years ago committed by GitHub
commit 8548bed8e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 81
      slither/core/cfg/node.py
  2. 2
      slither/core/declarations/function.py
  3. 83
      slither/slithir/operations/binary.py
  4. 14
      slither/slithir/operations/unary.py

@ -66,80 +66,41 @@ if TYPE_CHECKING:
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
@ -1014,7 +975,7 @@ class Node(SourceMapping, ChildFunction): # pylint: disable=too-many-public-met
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

@ -1378,7 +1378,7 @@ class Function(SourceMapping, metaclass=ABCMeta): # pylint: disable=too-many-pu
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:

@ -12,25 +12,25 @@ logger = logging.getLogger("BinaryOperationIR")
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):
@ -98,47 +98,6 @@ class BinaryType(Enum):
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):
@ -178,8 +137,8 @@ class Binary(OperationWithLValue):
@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):

@ -9,8 +9,8 @@ logger = logging.getLogger("BinaryOperationIR")
class UnaryType(Enum):
BANG = 0 # !
TILD = 1 # ~
BANG = "!"
TILD = "~"
@staticmethod
def get_type(operation_type, isprefix):
@ -21,14 +21,6 @@ class UnaryType(Enum):
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):
@ -53,7 +45,7 @@ class Unary(OperationWithLValue):
@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} "

Loading…
Cancel
Save