[PATCH] D39423: [analyzer] Left shifting a negative value is undefined

Phabricator via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 30 10:07:16 PDT 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL316924: [analyzer] Left shifting a negative value is undefined (authored by xazax).

Changed prior to commit:
  https://reviews.llvm.org/D39423?vs=120837&id=120841#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D39423

Files:
  cfe/trunk/lib/StaticAnalyzer/Checkers/UndefResultChecker.cpp
  cfe/trunk/lib/StaticAnalyzer/Core/BasicValueFactory.cpp
  cfe/trunk/test/Analysis/bitwise-ops.c


Index: cfe/trunk/lib/StaticAnalyzer/Core/BasicValueFactory.cpp
===================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/BasicValueFactory.cpp
+++ cfe/trunk/lib/StaticAnalyzer/Core/BasicValueFactory.cpp
@@ -225,6 +225,8 @@
       // test these conditions symbolically.
 
       // FIXME: Expand these checks to include all undefined behavior.
+      if (V1.isSigned() && V1.isNegative())
+        return nullptr;
 
       if (V2.isSigned() && V2.isNegative())
         return nullptr;
Index: cfe/trunk/lib/StaticAnalyzer/Checkers/UndefResultChecker.cpp
===================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/UndefResultChecker.cpp
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/UndefResultChecker.cpp
@@ -137,6 +137,10 @@
 
         OS << " greater or equal to the width of type '"
            << B->getLHS()->getType().getAsString() << "'.";
+      } else if (B->getOpcode() == BinaryOperatorKind::BO_Shl &&
+                 C.isNegative(B->getLHS())) {
+        OS << "The result of the left shift is undefined because the left "
+              "operand is negative";
       } else {
         OS << "The result of the '"
            << BinaryOperator::getOpcodeStr(B->getOpcode())
Index: cfe/trunk/test/Analysis/bitwise-ops.c
===================================================================
--- cfe/trunk/test/Analysis/bitwise-ops.c
+++ cfe/trunk/test/Analysis/bitwise-ops.c
@@ -44,3 +44,10 @@
   }
   return 0;
 }
+
+int testNegativeLeftShift(int a) {
+  if (a == -3) {
+    return a << 1; // expected-warning{{The result of the left shift is undefined because the left operand is negative}}
+  }
+  return 0;
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39423.120841.patch
Type: text/x-patch
Size: 1718 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171030/d1b504d3/attachment.bin>


More information about the cfe-commits mailing list