Skip to content

Commit

Permalink
reformatted by black
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiko7454 committed Jul 28, 2023
1 parent cd7430d commit 5dbdb20
Showing 1 changed file with 22 additions and 33 deletions.
55 changes: 22 additions & 33 deletions slither/solc_parsing/declarations/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def link_underlying_nodes(node1: NodeSolc, node2: NodeSolc):


class FunctionSolc(CallerContextExpression):

# elems = [(type, name)]
temp_var_num: int = 0

Expand Down Expand Up @@ -600,7 +599,6 @@ def _parse_for(self, statement: Dict, node: NodeSolc, scope: Scope) -> NodeSolc:
return node_endLoop

def _parse_dowhile(self, do_while_statement: Dict, node: NodeSolc, scope: Scope) -> NodeSolc:

node_startDoWhile = self._new_node(NodeType.STARTLOOP, do_while_statement["src"], scope)
condition_scope = Scope(scope.is_checked, False, scope)

Expand Down Expand Up @@ -1103,7 +1101,6 @@ def _parse_unchecked_block(self, block: Dict, node: NodeSolc, scope):
return node

def _parse_cfg(self, cfg: Dict) -> None:

assert cfg[self.get_key()] == "Block"

node = self._new_node(NodeType.ENTRYPOINT, cfg["src"], self.underlying_function)
Expand Down Expand Up @@ -1210,7 +1207,6 @@ def _fix_catch(self, node: Node, end_node: Node, visited: Set[Node]) -> None:
self._fix_catch(son, end_node, visited)

def _add_param(self, param: Dict, initialized: bool = False) -> LocalVariableSolc:

local_var = LocalVariable()
local_var.set_function(self._function)
local_var.set_offset(param["src"], self._function.compilation_unit)
Expand All @@ -1230,7 +1226,6 @@ def _add_param(self, param: Dict, initialized: bool = False) -> LocalVariableSol
return local_var_parser

def _add_param_init_tuple(self, statement: Dict, index: int) -> LocalVariableInitFromTupleSolc:

local_var = LocalVariableInitFromTuple()
local_var.set_function(self._function)
local_var.set_offset(statement["src"], self._function.compilation_unit)
Expand All @@ -1256,7 +1251,6 @@ def _parse_params(self, params: Dict):
self._function.add_parameters(local_var.underlying_variable)

def _parse_returns(self, returns: Dict):

assert returns[self.get_key()] == "ParameterList"

self._function.returns_src().set_offset(returns["src"], self._function.compilation_unit)
Expand Down Expand Up @@ -1402,40 +1396,28 @@ def _remove_alone_endif(self) -> None:
###################################################################################

def __make_temporary_variable_declaration_node_from_condition(
self,
node: Node,
temp_var: Optional[LocalVariable] = None
self, node: Node, temp_var: Optional[LocalVariable] = None
) -> (Node, LocalVariable):
temp_var_node_parser = self._new_node(
NodeType.VARIABLE,
node.source_mapping,
node.scope
)
temp_var_node_parser = self._new_node(NodeType.VARIABLE, node.source_mapping, node.scope)
temp_var_node = temp_var_node_parser.underlying_node
if temp_var is None:
FunctionSolc.temp_var_num += 1
temp_var = LocalVariable()
temp_var.name = f'temp-var-{FunctionSolc.temp_var_num}'
temp_var.type = ElementaryType('bool')
temp_var.name = f"temp-var-{FunctionSolc.temp_var_num}"
temp_var.type = ElementaryType("bool")
temp_var.initialized = True
temp_var.set_location('default')
temp_var.set_location("default")
temp_var.set_function(node.function)
temp_var_node.add_expression(
AssignmentOperation(
Identifier(temp_var),
node.expression,
AssignmentOperationType.ASSIGN,
None
Identifier(temp_var), node.expression, AssignmentOperationType.ASSIGN, None
)
)
temp_var_node.add_variable_declaration(temp_var)
return (temp_var_node, temp_var)

def __link_node_immediately_before(
self,
new_node: Node,
node_in_cfg: Node,
node_not_to_be_detached: Optional[Node] = None
self, new_node: Node, node_in_cfg: Node, node_not_to_be_detached: Optional[Node] = None
) -> None:
for father in node_in_cfg.fathers:
if father == node_not_to_be_detached:
Expand All @@ -1454,29 +1436,34 @@ def __find_start_loop_in_nodes_fathers(self, node: Node) -> Optional[Node]:
return begin_loop_node

def __inject_condition_expression_from_conditional_node(self, node: Node) -> Node:
temp_var_node, temp_var = self.__make_temporary_variable_declaration_node_from_condition(node)
temp_var_node, temp_var = self.__make_temporary_variable_declaration_node_from_condition(
node
)
if node.type == NodeType.IF:
self.__link_node_immediately_before(temp_var_node, node)
elif node.type == NodeType.IFLOOP:
if_loop_node = node
begin_loop_node: Optional[Node] = self.__find_start_loop_in_nodes_fathers(if_loop_node)
if begin_loop_node: # if BEGIN_LOOP is IF_LOOP's father, IF_LOOP represents `while`
temp_var_node_pre_loop = temp_var_node
temp_var_node_during_loop, _ = self.__make_temporary_variable_declaration_node_from_condition(
if_loop_node,
temp_var
(
temp_var_node_during_loop,
_,
) = self.__make_temporary_variable_declaration_node_from_condition(
if_loop_node, temp_var
)
self.__link_node_immediately_before(
temp_var_node_during_loop, if_loop_node, begin_loop_node
)
self.__link_node_immediately_before(temp_var_node_during_loop, if_loop_node, begin_loop_node)
self.__link_node_immediately_before(temp_var_node_pre_loop, begin_loop_node)
else:
self.__link_node_immediately_before(temp_var_node, if_loop_node)
else:
raise TypeError(f'Unknown conditional type {node.type}')
raise TypeError(f"Unknown conditional type {node.type}")

node.add_expression(Identifier(temp_var), bypass_verif_empty=True)
return temp_var_node


def _rewrite_ternary_as_if_else(self) -> bool:
ternary_found = True
updated = False
Expand All @@ -1486,7 +1473,9 @@ def _rewrite_ternary_as_if_else(self) -> bool:
has_cond = HasConditional(node.expression)
if has_cond.result():
if node.is_conditional():
temp_var_node = self.__inject_condition_expression_from_conditional_node(node)
temp_var_node = self.__inject_condition_expression_from_conditional_node(
node
)
node = temp_var_node # goes back by new cfg
st = SplitTernaryExpression(node.expression)
condition = st.condition
Expand Down

0 comments on commit 5dbdb20

Please sign in to comment.