[clang] 96a79cb - Fix a tautological comparison bug caught during post-commit

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 28 11:23:47 PDT 2022


Author: Aaron Ballman
Date: 2022-09-28T14:23:28-04:00
New Revision: 96a79cb308d1b8c00a83b180d9fecc5d54bacb9c

URL: https://github.com/llvm/llvm-project/commit/96a79cb308d1b8c00a83b180d9fecc5d54bacb9c
DIFF: https://github.com/llvm/llvm-project/commit/96a79cb308d1b8c00a83b180d9fecc5d54bacb9c.diff

LOG: Fix a tautological comparison bug caught during post-commit

This amends fd874e5fb119e1d9f427a299ffa5bbabaeba9455 to correctly set
the bit width of a '!' operator to be the same width as an 'int'. This
fixes a failed assertion about unexpected bit widths that was reported
during post-commit testing.

Added: 
    

Modified: 
    clang/lib/Analysis/CFG.cpp
    clang/test/Sema/warn-bitwise-compare.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Analysis/CFG.cpp b/clang/lib/Analysis/CFG.cpp
index f042468ebba72..20c6c68e44a07 100644
--- a/clang/lib/Analysis/CFG.cpp
+++ b/clang/lib/Analysis/CFG.cpp
@@ -1002,7 +1002,7 @@ class CFGBuilder {
         if (BuildOpts.Observer)
           BuildOpts.Observer->compareBitwiseEquality(B,
                                                      B->getOpcode() != BO_EQ);
-        TryResult(B->getOpcode() != BO_EQ);
+        return TryResult(B->getOpcode() != BO_EQ);
       }
     } else if (BoolExpr->isKnownToHaveBooleanValue()) {
       if ((*IntLiteral1 == 1) || (*IntLiteral1 == 0)) {
@@ -1041,7 +1041,7 @@ class CFGBuilder {
         case UO_Not:
           return ~Value;
         case UO_LNot:
-          return llvm::APInt(Value.getBitWidth(), !Value);
+          return llvm::APInt(Context->getTypeSize(Context->IntTy), !Value);
         default:
           assert(false && "Unexpected unary operator!");
           return llvm::None;

diff  --git a/clang/test/Sema/warn-bitwise-compare.c b/clang/test/Sema/warn-bitwise-compare.c
index ccd83613b8065..e66c58ee5ae3a 100644
--- a/clang/test/Sema/warn-bitwise-compare.c
+++ b/clang/test/Sema/warn-bitwise-compare.c
@@ -91,6 +91,8 @@ void f(int x) {
   if ((x | mydefine2) == 4) {}
 
   if ((x & 1) == 1L) {}
+
+  if (0 != (x | !0LL)) {} // expected-warning {{bitwise comparison always evaluates to true}}
 }
 
 void g(int x) {


        


More information about the cfe-commits mailing list