From 507df861668b330be3336c4934de53b19bdd5a0a Mon Sep 17 00:00:00 2001 From: Erik Carstensen Date: Fri, 29 Sep 2023 23:22:54 +0200 Subject: [PATCH] Report WNEGCONSTCOMP also on smaller unsigned int types --- py/dml/ctree.py | 17 ++++++++++++----- test/1.4/errors/T_WNEGCONSTCOMP.dml | 5 +++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/py/dml/ctree.py b/py/dml/ctree.py index 5d9fad091..ad605dde6 100644 --- a/py/dml/ctree.py +++ b/py/dml/ctree.py @@ -1243,6 +1243,12 @@ def make(cls, site, lh, rh): if (lhtype.is_arith and rhtype.is_arith and lh.constant and rh.constant): return mkBoolConstant(site, cls.eval_const(lh.value, rh.value)) + if (lhtype.is_int and rhtype.is_int + and ((not lhtype.signed and rh.constant and rh.value < 0) + or (not rhtype.signed and lh.constant and lh.value < 0))): + (signed_expr, unsigned_expr) = ((lh, rh) if lhtype.signed + else (rh, lh)) + report(WNEGCONSTCOMP(site, signed_expr, unsigned_expr.ctype())) if lhtype.is_int: lh = as_int(lh) lhtype = realtype(lh.ctype()) @@ -1254,9 +1260,6 @@ def make(cls, site, lh, rh): and lhtype.signed != rhtype.signed): (signed_expr, unsigned_expr) = ((lh, rh) if lhtype.signed else (rh, lh)) - if signed_expr.constant and signed_expr.value < 0: - report(WNEGCONSTCOMP(site, signed_expr, - unsigned_expr.ctype())) # we must convert (uint64)x < (int64)y to DML_lt(x, y), because # C:'s < would do an unsigned comparison. No need to do this if y # is a small constant, though. @@ -1450,6 +1453,12 @@ def make(cls, site, lh, rh): assert isinstance(expr, (NullConstant, StringConstant, AddressOfMethod)) return mkBoolConstant(site, False) + if (lhtype.is_int and rhtype.is_int + and ((not lhtype.signed and rh.constant and rh.value < 0) + or (not rhtype.signed and lh.constant and lh.value < 0))): + (signed_expr, unsigned_expr) = ((lh, rh) if lhtype.signed + else (rh, lh)) + report(WNEGCONSTCOMP(site, signed_expr, unsigned_expr.ctype())) if lhtype.is_int: lh = as_int(lh) lhtype = realtype(lh.ctype()) @@ -1465,8 +1474,6 @@ def make(cls, site, lh, rh): # unsigned to a constant literal. (signed_expr, unsigned_expr) = ((lh, rh) if lhtype.signed else (rh, lh)) - if signed_expr.constant and signed_expr.value < 0: - report(WNEGCONSTCOMP(site, signed_expr, unsigned_expr.ctype())) if not (signed_expr.constant and 0 <= signed_expr.value < 1 << 63): return mkApply( site, mkLit( diff --git a/test/1.4/errors/T_WNEGCONSTCOMP.dml b/test/1.4/errors/T_WNEGCONSTCOMP.dml index c13119313..7f7ea32dd 100644 --- a/test/1.4/errors/T_WNEGCONSTCOMP.dml +++ b/test/1.4/errors/T_WNEGCONSTCOMP.dml @@ -18,6 +18,11 @@ method init() { assert a >= -1; /// WARNING WNEGCONSTCOMP assert !(a <= -1); + local uint32 c = -1; + /// WARNING WNEGCONSTCOMP + assert c != -1; + /// WARNING WNEGCONSTCOMP + assert c > -1; // Sanity local int64 b = -1;