Hi,

I have made a small patch which improves the CFG equality operator evaluation 
to detect logical errors in bitwise comparisons.

It finds logical errors in and/or operations that always evaluates to 
true/false such as.
(x & 8) == 4

//Anders
Index: lib/Analysis/CFG.cpp
===================================================================
--- lib/Analysis/CFG.cpp	(revision 208953)
+++ lib/Analysis/CFG.cpp	(working copy)
@@ -533,9 +533,10 @@
     }
   }
 
-  /// Find a equality comparison with an expression evaluating to a boolean and
-  /// a constant other than 0 and 1.
-  /// e.g. if (!x == 10)
+  /// Find an incorrect equality comparison. Either with an expression
+  /// evaluating to a boolean and a constant other than 0 and 1.
+  /// e.g. if (!x == 10) or a bitwise and/or operation that always evaluates to
+  /// true/false e.q. (x & 8) == 4.
   TryResult checkIncorrectEqualityOperator(const BinaryOperator *B) {
     const Expr *LHSExpr = B->getLHS()->IgnoreParens();
     const Expr *RHSExpr = B->getRHS()->IgnoreParens();
@@ -548,15 +549,43 @@
       BoolExpr = LHSExpr;
     }
 
-    if (!IntLiteral || !BoolExpr->isKnownToHaveBooleanValue())
+    if (!IntLiteral)
       return TryResult();
 
-    llvm::APInt IntValue = IntLiteral->getValue();
-    if ((IntValue == 1) || (IntValue == 0)) {
-      return TryResult();
+    const BinaryOperator *BitOp = dyn_cast<BinaryOperator>(BoolExpr);
+    if (BitOp && (BitOp->getOpcode() == BO_And ||
+                  BitOp->getOpcode() == BO_Or)) {
+      const Expr *LHSExpr2 = BitOp->getLHS()->IgnoreParens();
+      const Expr *RHSExpr2 = BitOp->getRHS()->IgnoreParens();
+
+      const IntegerLiteral *IntLiteral2 = dyn_cast<IntegerLiteral>(LHSExpr2);
+
+      if (!IntLiteral2)
+        IntLiteral2 = dyn_cast<IntegerLiteral>(RHSExpr2);
+
+      if (!IntLiteral2)
+        return TryResult();
+
+      llvm::APSInt L1, L2;
+      IntLiteral->EvaluateAsInt(L1, *Context);
+      IntLiteral2->EvaluateAsInt(L2, *Context);
+
+      if ((BitOp->getOpcode() == BO_And && (L2 & L1) != L1) ||
+          (BitOp->getOpcode() == BO_Or  && (L2 | L1) != L1)) {
+        if (BuildOpts.Observer)
+          BuildOpts.Observer->compareBitwiseEquality(B,
+                                                     B->getOpcode() != BO_EQ);
+        TryResult(B->getOpcode() != BO_EQ);
+      }
+    } else if (BoolExpr->isKnownToHaveBooleanValue()) {
+      llvm::APInt IntValue = IntLiteral->getValue();
+      if ((IntValue == 1) || (IntValue == 0)) {
+        return TryResult();
+      }
+      return TryResult(B->getOpcode() != BO_EQ);
     }
 
-    return TryResult(B->getOpcode() != BO_EQ);
+    return TryResult();
   }
 
   TryResult analyzeLogicOperatorCondition(BinaryOperatorKind Relation,
Index: lib/Sema/AnalysisBasedWarnings.cpp
===================================================================
--- lib/Sema/AnalysisBasedWarnings.cpp	(revision 208953)
+++ lib/Sema/AnalysisBasedWarnings.cpp	(working copy)
@@ -146,6 +146,15 @@
     S.Diag(B->getExprLoc(), diag::warn_tautological_overlap_comparison)
         << DiagRange << isAlwaysTrue;
   }
+
+  void compareBitwiseEquality(const BinaryOperator *B, bool isAlwaysTrue) {
+    if (HasMacroID(B))
+      return;
+
+    SourceRange DiagRange = B->getSourceRange();
+    S.Diag(B->getExprLoc(), diag::warn_comparison_bitwise_always)
+        << DiagRange << isAlwaysTrue;
+  }
 };
 
 
Index: test/Sema/warn-bitwise-compare.c
===================================================================
--- test/Sema/warn-bitwise-compare.c	(revision 0)
+++ test/Sema/warn-bitwise-compare.c	(revision 0)
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-compare %s
+
+#define mydefine 2
+
+void f(int x) {
+  if ((x & 8) == 4) {}  // expected-warning {{bitwise comparison always evaluates to false}}
+  if ((x & 8) != 4) {}  // expected-warning {{bitwise comparison always evaluates to true}}
+  if ((x | 4) == 3) {}  // expected-warning {{bitwise comparison always evaluates to false}}
+  if ((x | 4) != 3) {}  // expected-warning {{bitwise comparison always evaluates to true}}
+
+  if ((x & 8) == 8) {}
+  if ((x & 8) != 8) {}
+  if ((x | 4) == 4) {}
+  if ((x | 4) != 4){}
+
+  if ((x & mydefine) == 8) {}
+  if ((x | mydefine) == 4) {}
+}
+
+
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td	(revision 208953)
+++ include/clang/Basic/DiagnosticSemaKinds.td	(working copy)
@@ -6404,6 +6404,9 @@
 def warn_comparison_always : Warning<
   "%select{self-|array }0comparison always evaluates to %select{false|true|a constant}1">,
   InGroup<TautologicalCompare>;
+def warn_comparison_bitwise_always : Warning<
+  "bitwise comparison always evaluates to %select{false|true|}0">,
+  InGroup<TautologicalCompare>;
 def warn_tautological_overlap_comparison : Warning<
   "overlapping comparisons always evaluate to %select{false|true}0">,
   InGroup<TautologicalOverlapCompare>, DefaultIgnore;
Index: include/clang/Analysis/CFG.h
===================================================================
--- include/clang/Analysis/CFG.h	(revision 208953)
+++ include/clang/Analysis/CFG.h	(working copy)
@@ -706,6 +706,7 @@
 public:
   CFGCallback() {}
   virtual void compareAlwaysTrue(const BinaryOperator *B, bool isAlwaysTrue) {}
+  virtual void compareBitwiseEquality(const BinaryOperator *B, bool isAlwaysTrue) {}
   virtual ~CFGCallback() {}
 };
 
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to