Skip to content

Commit

Permalink
Fix incorrect braces on call/memberaccess
Browse files Browse the repository at this point in the history
  • Loading branch information
rihi committed Dec 6, 2023
1 parent 77804c4 commit 2870569
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
11 changes: 5 additions & 6 deletions decompiler/backend/cexpressiongenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ def visit_list_operation(self, op: operations.ListOperation) -> str:

def visit_unary_operation(self, op: operations.UnaryOperation) -> str:
"""Return a string representation of the given unary operation (e.g. !a or &a)."""
operand = self._visit_bracketed(op.operand) if self._has_lower_precedence(op.operand, op) else self.visit(op.operand)
if isinstance(op, MemberAccess):
operator_str = "->" if isinstance(op.struct_variable.type, Pointer) else self.C_SYNTAX[op.operation]
return f"{self.visit(op.struct_variable)}{operator_str}{op.member_name}"
operand = self._visit_bracketed(op.operand) if self._has_lower_precedence(op.operand, op) else self.visit(op.operand)
return f"{operand}{operator_str}{op.member_name}"
if op.operation == OperationType.cast and op.contraction:
return f"({int(op.type.size / 8)}: ){operand}"
if op.operation == OperationType.cast:
Expand Down Expand Up @@ -209,10 +209,9 @@ def visit_call(self, op: operations.Call) -> str:
Generic labels starting with 'arg' e.g. 'arg1', 'arg2' are being filtered.
Additionally we filter ellipsis argument '...' that is lifted from type string.
"""
func_name = self.visit(op.function)
if isinstance(op.function, expressions.Constant):
func_name = func_name.strip('"')
output = f"{func_name}("
func_expr_str = self._visit_bracketed(op.function) if self._has_lower_precedence(op.function, op) else self.visit(op.function)

output = f"{func_expr_str}("
if op.meta_data is not None:
parameter_names = op.meta_data.get("param_names", [])
is_tailcall = op.meta_data.get("is_tailcall")
Expand Down
17 changes: 17 additions & 0 deletions tests/backend/test_codegenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,22 @@ def test_array_element_access_aggressive(self, operation, result):
),
"ptr->x->z->w",
),
(
MemberAccess(
offset=0,
member_name="x",
operands=[BinaryOperation(
OperationType.plus,
[
Variable("ptr", Pointer(Integer.int32_t())),
Constant(1, Integer.int32_t())
],
Pointer(Integer.int32_t())
)],
vartype=Integer.int32_t()
),
"(ptr + 1)->x"
)
],
)
def test_member_access(self, operation, result):
Expand Down Expand Up @@ -816,6 +832,7 @@ def test_member_access(self, operation, result):
),
"foo(/* param1 */ x, /* param2 */ y, z)",
),
(Assignment(ListOperation([]), Call(UnaryOperation(OperationType.dereference, [var_x]), [])), "(*x)()")
],
)
def test_call(self, expr, result):
Expand Down

0 comments on commit 2870569

Please sign in to comment.