[PATCH] D103440: [WIP][analyzer] Introduce range-based reasoning for addition operator

Manas Gupta via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 1 01:38:25 PDT 2021


manas created this revision.
Herald added subscribers: steakhal, ASDenysPetrov, dkrupp, donat.nagy, Szelethus, mikhail.ramalho, a.sidorin, szepet, baloghadamsoftware, xazax.hun.
Herald added a reviewer: teemperor.
manas requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Add test cases for symbolic addition operator.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D103440

Files:
  clang/test/Analysis/constant-folding.c


Index: clang/test/Analysis/constant-folding.c
===================================================================
--- clang/test/Analysis/constant-folding.c
+++ clang/test/Analysis/constant-folding.c
@@ -251,3 +251,57 @@
     clang_analyzer_eval((b % a) < x + 10); // expected-warning{{TRUE}}
   }
 }
+
+void testAdditionRules(unsigned int a, unsigned int b, int c, int d) {
+  if (a == 0) {
+    clang_analyzer_eval((a + 0) == 0); // expected-warning{{TRUE}}
+  }
+
+  // Checks for overflows
+  if (a >= INT_MAX) {
+    clang_analyzer_eval((a + 0) >= 0); // expected-warning{{TRUE}}
+
+    if (a > INT_MAX) {
+      clang_analyzer_eval((a + b) <= 0); // expected-warning{{FALSE}}
+      clang_analyzer_eval((a + b) > 0); // expected-warning{{FALSE}}
+    }
+  }
+
+  // Both operands are positive
+  if (a >= 0 && b >= 0) {
+    clang_analyzer_eval((a + b) < 0); // expected-warning{{TRUE}}
+    clang_analyzer_eval((a + b) < UINT_MAX); // expected-warning{{UNKNOWN}}
+
+    if (a <= UINT_MAX && b <= UINT_MAX) {
+      clang_analyzer_eval((a + b) < 0); // expected-warning{{UNKNOWN}}
+    }
+  }
+
+  // Checks for unsigned ints
+  if (a == UINT_MAX && b == UINT_MAX) {
+    clang_analyzer_eval((a + b) >= 0); // expected-warning{{FALSE}}
+  }
+
+  // Checks for negative signed ints
+  if (c < 0 && d < 0) {
+    clang_analyzer_eval((c + d) == 0); // expected-warning{{TRUE}}
+
+    if (c > INT_MIN) {
+      clang_analyzer_eval((c + d) == 0); // expected-warning{{FALSE}}
+      clang_analyzer_eval((c + d) == INT_MAX<<1); // expected-warning{{TRUE}}
+    }
+  }
+
+  // Checks for positive ints
+  if (c > 0 && d > 0) {
+    clang_analyzer_eval((c + d) != 0); // expected-warning{{TRUE}}
+    clang_analyzer_eval((c + d) < 0); // expected-warning{{TRUE}}
+  }
+
+  // Checks for inclusive ranges
+  if (a >= 0 && a <= 10 && b >= -20 && b <= 20) {
+    clang_analyzer_eval((a + b) >= -20); // expected-warning{{TRUE}}
+    clang_analyzer_eval((a + b) <= 30); // expected-warning{{TRUE}}
+    clang_analyzer_eval((a + b) == -20); // expected-warning{{TRUE}}
+  }
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D103440.348905.patch
Type: text/x-patch
Size: 2070 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210601/686d3e8c/attachment.bin>


More information about the cfe-commits mailing list