Skip to content

Commit

Permalink
Simplify Base.identical() to utilize Base.canonicalize() (#569)
Browse files Browse the repository at this point in the history
  • Loading branch information
twizmwazin authored Nov 5, 2024
1 parent 7b71286 commit 28d9ea5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 33 deletions.
33 changes: 2 additions & 31 deletions claripy/ast/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,41 +470,12 @@ def _encoded_name(self) -> bytes:
self._cached_encoded_name = self.args[0].encode()
return self._cached_encoded_name

def _check_args_same(self, other_args: tuple[ArgType, ...], lenient_names=False) -> bool:
"""
Check if two ASTs are the same.
"""
# Several types inside of args don't support normall == comparison, so if we see those,
# we need compare them manually.
for a, b in zip(self.args, other_args, strict=True):
if isinstance(a, Base) and isinstance(b, Base):
if a._hash != b._hash:
return False
continue
if isinstance(a, float) and isinstance(b, float):
if math.isnan(a) and math.isnan(b):
continue
if math.isinf(a) and math.isinf(b):
continue
if a != b:
return False
if lenient_names and isinstance(a, str) and isinstance(b, str):
continue
if a != b:
return False

return True

def identical(self, other: Self, strict=False) -> bool:
def identical(self, other: Self) -> bool:
"""
Check if two ASTs are identical. If `strict` is False, the comparison
will be lenient on the names of the ASTs.
"""
return self._hash == other._hash or (
self.op == other.op
and self._check_args_same(other.args, lenient_names=not strict)
and self.annotations == other.annotations
)
return self.canonicalize() is other.canonicalize()

#
# Annotations
Expand Down
4 changes: 2 additions & 2 deletions claripy/ast/bv.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ def raw_to_bv(self):
def to_bv(self):
return self.raw_to_bv()

def identical(self, other: Self, strict=False) -> bool:
def identical(self, other: Self) -> bool:
with suppress(BackendError):
return claripy.backends.vsa.convert(self).identical(claripy.backends.vsa.convert(other))
return super().identical(other, strict)
return super().identical(other)


def BVS( # pylint:disable=redefined-builtin
Expand Down

0 comments on commit 28d9ea5

Please sign in to comment.