r177806 - [analyzer] Teach constraint managers about unsigned comparisons.

Jordan Rose jordan_rose at apple.com
Fri Mar 22 18:21:34 PDT 2013


Author: jrose
Date: Fri Mar 22 20:21:33 2013
New Revision: 177806

URL: http://llvm.org/viewvc/llvm-project?rev=177806&view=rev
Log:
[analyzer] Teach constraint managers about unsigned comparisons.

In C, comparisons between signed and unsigned numbers are always done in
unsigned-space. Thus, we should know that "i >= 0U" is always true, even
if 'i' is signed. Similarly, "u >= 0" is also always true, even though '0'
is signed.

Part of <rdar://problem/13239003> (false positives related to std::vector)

Modified:
    cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h
    cfe/trunk/lib/StaticAnalyzer/Core/APSIntType.cpp
    cfe/trunk/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp
    cfe/trunk/lib/StaticAnalyzer/Core/SimpleConstraintManager.cpp
    cfe/trunk/test/Analysis/additive-folding-range-constraints.c
    cfe/trunk/test/Analysis/additive-folding.cpp

Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h?rev=177806&r1=177805&r2=177806&view=diff
==============================================================================
--- cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h Fri Mar 22 20:21:33 2013
@@ -81,9 +81,12 @@ public:
 
   /// Tests whether a given value is losslessly representable using this type.
   ///
-  /// Note that signedness conversions will be rejected, even with the same bit
-  /// pattern. For example, -1s8 is not in range for 'unsigned char' (u8).
-  RangeTestResultKind testInRange(const llvm::APSInt &Val) const LLVM_READONLY;
+  /// \param Val The value to test.
+  /// \param AllowMixedSign Whether or not to allow signedness conversions.
+  ///                       This determines whether -1s8 is considered in range
+  ///                       for 'unsigned char' (u8).
+  RangeTestResultKind testInRange(const llvm::APSInt &Val,
+                                  bool AllowMixedSign) const LLVM_READONLY;
   
   bool operator==(const APSIntType &Other) const {
     return BitWidth == Other.BitWidth && IsUnsigned == Other.IsUnsigned;

Modified: cfe/trunk/lib/StaticAnalyzer/Core/APSIntType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/APSIntType.cpp?rev=177806&r1=177805&r2=177806&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/APSIntType.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/APSIntType.cpp Fri Mar 22 20:21:33 2013
@@ -13,20 +13,31 @@ using namespace clang;
 using namespace ento;
 
 APSIntType::RangeTestResultKind
-APSIntType::testInRange(const llvm::APSInt &Value) const {
+APSIntType::testInRange(const llvm::APSInt &Value,
+                        bool AllowSignConversions) const {
+
   // Negative numbers cannot be losslessly converted to unsigned type.
-  if (IsUnsigned && Value.isSigned() && Value.isNegative())
+  if (IsUnsigned && !AllowSignConversions &&
+      Value.isSigned() && Value.isNegative())
     return RTR_Below;
 
-  // Signed integers can be converted to signed integers of the same width
-  // or (if positive) unsigned integers with one fewer bit.
-  // Unsigned integers can be converted to unsigned integers of the same width
-  // or signed integers with one more bit.
   unsigned MinBits;
-  if (Value.isSigned())
-    MinBits = Value.getMinSignedBits() - IsUnsigned;
-  else
-    MinBits = Value.getActiveBits() + !IsUnsigned;
+  if (AllowSignConversions) {
+    if (Value.isSigned() && !IsUnsigned)
+      MinBits = Value.getMinSignedBits();
+    else
+      MinBits = Value.getActiveBits();
+
+  } else {
+    // Signed integers can be converted to signed integers of the same width
+    // or (if positive) unsigned integers with one fewer bit.
+    // Unsigned integers can be converted to unsigned integers of the same width
+    // or signed integers with one more bit.
+    if (Value.isSigned())
+      MinBits = Value.getMinSignedBits() - IsUnsigned;
+    else
+      MinBits = Value.getActiveBits() + !IsUnsigned;
+  }
 
   if (MinBits <= BitWidth)
     return RTR_Within;

Modified: cfe/trunk/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp?rev=177806&r1=177805&r2=177806&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp Fri Mar 22 20:21:33 2013
@@ -153,8 +153,8 @@ private:
     // The function returns false if the described range is entirely outside
     // the range of values for the associated symbol.
     APSIntType Type(getMinValue());
-    APSIntType::RangeTestResultKind LowerTest = Type.testInRange(Lower);
-    APSIntType::RangeTestResultKind UpperTest = Type.testInRange(Upper);
+    APSIntType::RangeTestResultKind LowerTest = Type.testInRange(Lower, true);
+    APSIntType::RangeTestResultKind UpperTest = Type.testInRange(Upper, true);
 
     switch (LowerTest) {
     case APSIntType::RTR_Below:
@@ -419,7 +419,7 @@ RangeConstraintManager::assumeSymNE(Prog
                                     const llvm::APSInt &Adjustment) {
   // Before we do any real work, see if the value can even show up.
   APSIntType AdjustmentType(Adjustment);
-  if (AdjustmentType.testInRange(Int) != APSIntType::RTR_Within)
+  if (AdjustmentType.testInRange(Int, true) != APSIntType::RTR_Within)
     return St;
 
   llvm::APSInt Lower = AdjustmentType.convert(Int) - Adjustment;
@@ -439,7 +439,7 @@ RangeConstraintManager::assumeSymEQ(Prog
                                     const llvm::APSInt &Adjustment) {
   // Before we do any real work, see if the value can even show up.
   APSIntType AdjustmentType(Adjustment);
-  if (AdjustmentType.testInRange(Int) != APSIntType::RTR_Within)
+  if (AdjustmentType.testInRange(Int, true) != APSIntType::RTR_Within)
     return NULL;
 
   // [Int-Adjustment, Int-Adjustment]
@@ -454,7 +454,7 @@ RangeConstraintManager::assumeSymLT(Prog
                                     const llvm::APSInt &Adjustment) {
   // Before we do any real work, see if the value can even show up.
   APSIntType AdjustmentType(Adjustment);
-  switch (AdjustmentType.testInRange(Int)) {
+  switch (AdjustmentType.testInRange(Int, true)) {
   case APSIntType::RTR_Below:
     return NULL;
   case APSIntType::RTR_Within:
@@ -483,7 +483,7 @@ RangeConstraintManager::assumeSymGT(Prog
                                     const llvm::APSInt &Adjustment) {
   // Before we do any real work, see if the value can even show up.
   APSIntType AdjustmentType(Adjustment);
-  switch (AdjustmentType.testInRange(Int)) {
+  switch (AdjustmentType.testInRange(Int, true)) {
   case APSIntType::RTR_Below:
     return St;
   case APSIntType::RTR_Within:
@@ -512,7 +512,7 @@ RangeConstraintManager::assumeSymGE(Prog
                                     const llvm::APSInt &Adjustment) {
   // Before we do any real work, see if the value can even show up.
   APSIntType AdjustmentType(Adjustment);
-  switch (AdjustmentType.testInRange(Int)) {
+  switch (AdjustmentType.testInRange(Int, true)) {
   case APSIntType::RTR_Below:
     return St;
   case APSIntType::RTR_Within:
@@ -541,7 +541,7 @@ RangeConstraintManager::assumeSymLE(Prog
                                     const llvm::APSInt &Adjustment) {
   // Before we do any real work, see if the value can even show up.
   APSIntType AdjustmentType(Adjustment);
-  switch (AdjustmentType.testInRange(Int)) {
+  switch (AdjustmentType.testInRange(Int, true)) {
   case APSIntType::RTR_Below:
     return NULL;
   case APSIntType::RTR_Within:

Modified: cfe/trunk/lib/StaticAnalyzer/Core/SimpleConstraintManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/SimpleConstraintManager.cpp?rev=177806&r1=177805&r2=177806&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/SimpleConstraintManager.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/SimpleConstraintManager.cpp Fri Mar 22 20:21:33 2013
@@ -264,10 +264,14 @@ ProgramStateRef SimpleConstraintManager:
   APSIntType ComparisonType = std::max(WraparoundType, APSIntType(Int));
   llvm::APSInt ConvertedInt = ComparisonType.convert(Int);
 
+  // Prefer unsigned comparisons.
+  if (ComparisonType.getBitWidth() == WraparoundType.getBitWidth() &&
+      ComparisonType.isUnsigned() && !WraparoundType.isUnsigned())
+    Adjustment.setIsSigned(false);
+
   switch (op) {
   default:
-    // No logic yet for other operators.  assume the constraint is feasible.
-    return state;
+    llvm_unreachable("invalid operation not caught by assertion above");
 
   case BO_EQ:
     return assumeSymEQ(state, Sym, ConvertedInt, Adjustment);

Modified: cfe/trunk/test/Analysis/additive-folding-range-constraints.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/additive-folding-range-constraints.c?rev=177806&r1=177805&r2=177806&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/additive-folding-range-constraints.c (original)
+++ cfe/trunk/test/Analysis/additive-folding-range-constraints.c Fri Mar 22 20:21:33 2013
@@ -170,3 +170,135 @@ void mixedComparisons9(signed char a) {
   clang_analyzer_eval(a == 0x7F); // expected-warning{{UNKNOWN}}
   clang_analyzer_eval(a == -0x80); // expected-warning{{UNKNOWN}}
 }
+
+
+void mixedSignedness1(int a) {
+  unsigned max = UINT_MAX;
+  clang_analyzer_eval(a < max); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2) < max); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2U) < max); // expected-warning{{UNKNOWN}}
+}
+
+void mixedSignedness2(int a) {
+  unsigned max = UINT_MAX;
+  clang_analyzer_eval(a <= max); // expected-warning{{TRUE}}
+  clang_analyzer_eval((a + 2) <= max); // expected-warning{{TRUE}}
+  clang_analyzer_eval((a + 2U) <= max); // expected-warning{{TRUE}}
+}
+
+void mixedSignedness3(unsigned a) {
+  int max = INT_MAX;
+  clang_analyzer_eval(a < max); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2) < max); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2U) < max); // expected-warning{{UNKNOWN}}
+}
+
+void mixedSignedness4(unsigned a) {
+  int max = INT_MAX;
+  clang_analyzer_eval(a <= max); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2) <= max); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2U) <= max); // expected-warning{{UNKNOWN}}
+}
+
+void mixedSignedness5(unsigned a) {
+  int min = INT_MIN;
+  clang_analyzer_eval(a < min); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2) < min); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2U) < min); // expected-warning{{UNKNOWN}}
+}
+
+void mixedSignedness6(unsigned a) {
+  int min = INT_MIN;
+  clang_analyzer_eval(a <= min); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2) <= min); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2U) <= min); // expected-warning{{UNKNOWN}}
+}
+
+void mixedSignedness7(unsigned a) {
+  unsigned min = 0;
+  clang_analyzer_eval(a < min); // expected-warning{{FALSE}}
+  clang_analyzer_eval((a + 2) < min); // expected-warning{{FALSE}}
+  clang_analyzer_eval((a + 2U) < min); // expected-warning{{FALSE}}
+}
+
+void mixedSignedness8(unsigned a) {
+  unsigned min = 0;
+  clang_analyzer_eval(a <= min); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2) <= min); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2U) <= min); // expected-warning{{UNKNOWN}}
+}
+
+void mixedSignedness9(unsigned a) {
+  int min = 0;
+  clang_analyzer_eval(a < min); // expected-warning{{FALSE}}
+  clang_analyzer_eval((a + 2) < min); // expected-warning{{FALSE}}
+  clang_analyzer_eval((a + 2U) < min); // expected-warning{{FALSE}}
+}
+
+void mixedSignedness10(unsigned a) {
+  int min = 0;
+  clang_analyzer_eval(a <= min); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2) <= min); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2U) <= min); // expected-warning{{UNKNOWN}}
+}
+
+void mixedSignedness11(int a) {
+  int min = 0;
+  clang_analyzer_eval(a < min); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2) < min); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2U) < min); // expected-warning{{FALSE}}
+}
+
+void mixedSignedness12(int a) {
+  int min = 0;
+  clang_analyzer_eval(a <= min); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2) <= min); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2U) <= min); // expected-warning{{UNKNOWN}}
+}
+
+void mixedSignedness13(int a) {
+  unsigned max = INT_MAX;
+  clang_analyzer_eval(a < max); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2) < max); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2U) < max); // expected-warning{{UNKNOWN}}
+}
+
+void mixedSignedness14(int a) {
+  unsigned max = INT_MAX;
+  clang_analyzer_eval(a <= max); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2) <= max); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2U) <= max); // expected-warning{{UNKNOWN}}
+}
+
+void mixedSignedness15(int a) {
+  unsigned min = INT_MIN;
+  clang_analyzer_eval(a < min); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2) < min); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2U) < min); // expected-warning{{UNKNOWN}}
+}
+
+void mixedSignedness16(int a) {
+  unsigned min = INT_MIN;
+  clang_analyzer_eval(a <= min); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2) <= min); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval((a + 2U) <= min); // expected-warning{{UNKNOWN}}
+}
+
+void mixedSignedness17(int a) {
+  unsigned max = INT_MAX;
+  if (a < max)
+    return;
+
+  clang_analyzer_eval(a < 0); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval(a == 0); // expected-warning{{FALSE}}
+  clang_analyzer_eval(a == INT_MAX); // expected-warning{{UNKNOWN}}
+}
+
+void mixedSignedness18(int a) {
+  if (a >= 0)
+    return;
+
+  clang_analyzer_eval(a < 0); // expected-warning{{TRUE}}
+  clang_analyzer_eval(a == (unsigned)INT_MIN); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval(a == UINT_MAX); // expected-warning{{UNKNOWN}}
+}

Modified: cfe/trunk/test/Analysis/additive-folding.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/additive-folding.cpp?rev=177806&r1=177805&r2=177806&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/additive-folding.cpp (original)
+++ cfe/trunk/test/Analysis/additive-folding.cpp Fri Mar 22 20:21:33 2013
@@ -184,6 +184,18 @@ void mixedSignedness(int a, unsigned b)
   clang_analyzer_eval(b == uMin && b != sMin); // expected-warning{{FALSE}}
 }
 
+void mixedSignedness2(int a) {
+  if (a != -1)
+    return;
+  clang_analyzer_eval(a == UINT_MAX); // expected-warning{{TRUE}}
+}
+
+void mixedSignedness3(unsigned a) {
+  if (a != UINT_MAX)
+    return;
+  clang_analyzer_eval(a == -1); // expected-warning{{TRUE}}
+}
+
 
 void multiplicativeSanityTest(int x) {
   // At one point we were ignoring the *4 completely -- the constraint manager





More information about the cfe-commits mailing list