Skip to content

Commit

Permalink
apply MR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ebehner committed Apr 10, 2024
1 parent 8ec1b84 commit 1f9a914
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ def __init__(self, candidates: List[AbstractSyntaxTreeNode]) -> None:
param candidates:: list of all AST-nodes that we want to cluster into conditions.
- _candidates: maps all relevant ast-nodes to their formula (reaching condition)
- candidates: maps all relevant ast-nodes to their formula (reaching condition)
- unconsidered_nodes: a set of all nodes that we still have to consider for grouping into conditions.
- logic_graph: representation of all logic-formulas relevant
- _formulas_containing_symbol: maps each symbol to all formulas that contain this symbol
- _symbol_of_formula: maps to each formula all symbols that it contains.
"""
self._candidates: Dict[AbstractSyntaxTreeNode, Formula] = {c: Formula(c.reaching_condition, c) for c in candidates}
self._unconsidered_nodes: InsertionOrderedSet[AbstractSyntaxTreeNode] = InsertionOrderedSet()
Expand Down Expand Up @@ -103,7 +102,6 @@ def candidates(self) -> Iterator[AbstractSyntaxTreeNode]:
"""Iterates over all candidates considered for grouping into conditions."""
yield from self._candidates

@property
def maximum_subexpression_size(self) -> int:
"""Returns the maximum possible subexpression that is relevant to consider for clustering into conditions."""
if len(self._candidates) < 2:
Expand All @@ -112,14 +110,14 @@ def maximum_subexpression_size(self) -> int:
all_sizes.remove(max(all_sizes))
return max(all_sizes)

def get_symbols_of(self, node: AbstractSyntaxTreeNode) -> Set[str]:
def get_symbol_names_of(self, node: AbstractSyntaxTreeNode) -> Set[str]:
"""Return all symbols that are used in the formula of the given ast-node."""
return {symbol.name for symbol in self._auxiliary_graph.successors(self._candidates[node])}

def get_next_subexpression(self) -> Iterator[Tuple[AbstractSyntaxTreeNode, LogicCondition]]:
"""Consider Candidates in sequence-node order and start with the largest possible subexpression."""
self._unconsidered_nodes = InsertionOrderedSet(self._candidates)
while self._unconsidered_nodes and len(self._candidates) > 1 and ((max_expr_size := self.maximum_subexpression_size) != 0):
while self._unconsidered_nodes and len(self._candidates) > 1 and ((max_expr_size := self.maximum_subexpression_size()) != 0):
ast_node = self._unconsidered_nodes.pop(0)
clauses = self._get_clauses(ast_node)
current_size = min(len(clauses), max_expr_size)
Expand Down Expand Up @@ -216,22 +214,6 @@ def _remove_symbols(self, removing_symbols: Set[Symbol]):
self._remove_formula_node(clause.formula)
self._remove_symbols(new_single_formula_nodes)

# def get_next_subexpression(self) -> Iterator[Tuple[AbstractSyntaxTreeNode, LogicCondition]]:
# """Get the next subexpression together with the node it comes from and start with the largest possible subexpression!"""
# current_size = self.maximum_subexpression_size
# while current_size > 0:
# for ast_node in [c for c, p in self._candidates.items() if self._logic_graph.out_degree(p) >= current_size]:
# if ast_node not in self._candidates:
# continue
# if current_size > self.maximum_subexpression_size:
# break
# clauses = self._get_clauses(ast_node)
# for new_operands in combinations(clauses, current_size):
# yield ast_node, LogicCondition.conjunction_of(new_operands)
# if ast_node not in self._candidates or current_size > self.maximum_subexpression_size:
# break
# current_size = min(self.maximum_subexpression_size, current_size - 1)


class ConditionBasedRefinement:
"""
Expand Down Expand Up @@ -351,7 +333,7 @@ def _cluster_by_condition(
symbols_of_condition = set(sub_expression.get_symbols_as_string())
negated_condition = None
for ast_node in condition_candidates.candidates:
if symbols_of_condition - condition_candidates.get_symbols_of(ast_node):
if symbols_of_condition - condition_candidates.get_symbol_names_of(ast_node):
continue
if ast_node == node_with_subexpression or self._is_subexpression_of_cnf_formula(sub_expression, ast_node.reaching_condition):
true_children.append(ast_node)
Expand Down
2 changes: 1 addition & 1 deletion decompiler/structures/graphs/nxgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __contains__(self, obj: Union[NODE, EDGE]):
"""Check if a node or edge is contained in the graph."""
if isinstance(obj, GraphNodeInterface):
return obj in self._graph
return any(obj == data["data"] for _, _, data in self._graph.edges(data=True))
return (obj.source, obj.sink, {"data": obj}) in self._graph.edges(data=True)

def iter_depth_first(self, source: NODE) -> Iterator[NODE]:
"""Iterate all nodes in dfs fashion."""
Expand Down

0 comments on commit 1f9a914

Please sign in to comment.