[clang-tools-extra] r323980 - [clang-tidy] misc-redundant-expression: fix a crash under ubsan

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 1 08:39:12 PST 2018


Author: alexfh
Date: Thu Feb  1 08:39:12 2018
New Revision: 323980

URL: http://llvm.org/viewvc/llvm-project?rev=323980&view=rev
Log:
[clang-tidy] misc-redundant-expression: fix a crash under ubsan

Modified:
    clang-tools-extra/trunk/clang-tidy/misc/RedundantExpressionCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/misc-redundant-expression.cpp

Modified: clang-tools-extra/trunk/clang-tidy/misc/RedundantExpressionCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/RedundantExpressionCheck.cpp?rev=323980&r1=323979&r2=323980&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/RedundantExpressionCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/RedundantExpressionCheck.cpp Thu Feb  1 08:39:12 2018
@@ -22,7 +22,6 @@
 #include "llvm/Support/Casting.h"
 #include <algorithm>
 #include <cassert>
-#include <cmath>
 #include <cstdint>
 #include <string>
 #include <vector>
@@ -967,6 +966,13 @@ void RedundantExpressionCheck::checkRela
   }
 }
 
+unsigned intLog2(uint64_t X) {
+  unsigned Result = 0;
+  while (X >>= 1)
+    ++Result;
+  return Result;
+}
+
 void RedundantExpressionCheck::check(const MatchFinder::MatchResult &Result) {
   if (const auto *BinOp = Result.Nodes.getNodeAs<BinaryOperator>("binary")) {
     // If the expression's constants are macros, check whether they are
@@ -1043,11 +1049,11 @@ void RedundantExpressionCheck::check(con
     // If ShiftingConst is shifted left with more bits than the position of the
     // leftmost 1 in the bit representation of AndValue, AndConstant is
     // ineffective.
-    if (floor(log2(AndValue.getExtValue())) >= ShiftingValue)
+    if (intLog2(AndValue.getExtValue()) >= ShiftingValue)
       return;
 
     auto Diag = diag(BinaryAndExpr->getOperatorLoc(),
-                     "ineffective bitwise and operation.");
+                     "ineffective bitwise and operation");
   }
 
   // Check for the following bound expressions:

Modified: clang-tools-extra/trunk/test/clang-tidy/misc-redundant-expression.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-redundant-expression.cpp?rev=323980&r1=323979&r2=323980&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-redundant-expression.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-redundant-expression.cpp Thu Feb  1 08:39:12 2018
@@ -674,7 +674,7 @@ int operatorConfusion(int X, int Y, long
 {
   // Ineffective & expressions.
   Y = (Y << 8) & 0xff;
-  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: ineffective bitwise and operation.
+  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: ineffective bitwise and operation
   Y = (Y << 12) & 0xfff;
   // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: ineffective bitwise and
   Y = (Y << 12) & 0xff;
@@ -686,7 +686,7 @@ int operatorConfusion(int X, int Y, long
 
   // Tests for unmatched types
   Z = (Z << 8) & 0xff;
-  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: ineffective bitwise and operation.
+  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: ineffective bitwise and operation
   Y = (Y << 12) & 0xfffL;
   // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: ineffective bitwise and
   Z = (Y << 12) & 0xffLL;
@@ -694,6 +694,11 @@ int operatorConfusion(int X, int Y, long
   Y = (Z << 8L) & 0x77L;
   // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: ineffective bitwise and
 
+  Y = (Y << 8) & 0;
+  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: ineffective bitwise and
+
+  Y = (Y << 8) & -1;
+
   // Effective expressions. Do not check.
   Y = (Y << 4) & 0x15;
   Y = (Y << 3) & 0x250;




More information about the cfe-commits mailing list