Added a path-sensitive idempotent operation checker (-analyzer-idempotent-operation). Finds idempotent and/or tautological operations in a path sensitive context, flagging operations that have no effect or a predictable effect.
Example:
{
int a = 1;
int b = 5;
int c = b / a; // a is 1 on all paths
}
- New IdempotentOperationChecker class
- Moved recursive Stmt functions in r107675 to IdempotentOperationChecker
- Minor refactoring of SVal to allow checking for any integer
- Added command line option for check
- Added basic test cases
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@107706 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/test/Analysis/idempotent-operations.c b/test/Analysis/idempotent-operations.c
new file mode 100644
index 0000000..418eb7a
--- /dev/null
+++ b/test/Analysis/idempotent-operations.c
@@ -0,0 +1,52 @@
+// RUN: %clang_cc1 -Wunused-variable -analyze -analyzer-idempotent-operation -analyzer-store=basic -analyzer-constraints=range
+
+// Basic tests
+
+extern void test(int i);
+
+void basic() {
+ int x = 10, zero = 0, one = 1;
+
+ // x op x
+ x = x; // expected-warning {{idempotent operation; both operands are always equal in value}}
+ test(x - x); // expected-warning {{idempotent operation; both operands are always equal in value}}
+ x -= x; // expected-warning {{idempotent operation; both operands are always equal in value}}
+ x = 10; // no-warning
+ test(x / x); // expected-warning {{idempotent operation; both operands are always equal in value}}
+ x /= x; // expected-warning {{idempotent operation; both operands are always equal in value}}
+ x = 10; // no-warning
+ test(x & x); // expected-warning {{idempotent operation; both operands are always equal in value}}
+ x &= x; // expected-warning {{idempotent operation; both operands are always equal in value}}
+ test(x | x); // expected-warning {{idempotent operation; both operands are always equal in value}}
+ x |= x; // expected-warning {{idempotent operation; both operands are always equal in value}}
+
+ // x op 1
+ test(x * one); // expected-warning {{idempotent operation; the right operand is always 1}}
+ x *= one; // expected-warning {{idempotent operation; the right operand is always 1}}
+ test(x / one); // expected-warning {{idempotent operation; the right operand is always 1}}
+ x /= one; // expected-warning {{idempotent operation; the right operand is always 1}}
+
+ // 1 op x
+ test(one * x); // expected-warning {{idempotent operation; the left operand is always 1}}
+
+ // x op 0
+ test(x + zero); // expected-warning {{idempotent operation; the right operand is always 0}}
+ test(x - zero); // expected-warning {{idempotent operation; the right operand is always 0}}
+ test(x * zero); // expected-warning {{idempotent operation; the right operand is always 0}}
+ test(x & zero); // expected-warning {{idempotent operation; the right operand is always 0}}
+ test(x | zero); // expected-warning {{idempotent operation; the right operand is always 0}}
+ test(x ^ zero); // expected-warning {{idempotent operation; the right operand is always 0}}
+ test(x << zero); // expected-warning {{idempotent operation; the right operand is always 0}}
+ test(x >> zero); // expected-warning {{idempotent operation; the right operand is always 0}}
+
+ // 0 op x
+ test(zero + x); // expected-warning {{idempotent operation; the left operand is always 0}}
+ test(zero - x); // expected-warning {{idempotent operation; the left operand is always 0}}
+ test(zero / x); // expected-warning {{idempotent operation; the left operand is always 0}}
+ test(zero * x); // expected-warning {{idempotent operation; the left operand is always 0}}
+ test(zero & x); // expected-warning {{idempotent operation; the left operand is always 0}}
+ test(zero | x); // expected-warning {{idempotent operation; the left operand is always 0}}
+ test(zero ^ x); // expected-warning {{idempotent operation; the left operand is always 0}}
+ test(zero << x); // expected-warning {{idempotent operation; the left operand is always 0}}
+ test(zero >> x); // expected-warning {{idempotent operation; the left operand is always 0}}
+}