diff options
author | Jordan Rose <jordan_rose@apple.com> | 2013-12-10 18:18:06 +0000 |
---|---|---|
committer | Jordan Rose <jordan_rose@apple.com> | 2013-12-10 18:18:06 +0000 |
commit | 60bd88d34135aa2c4996160259e91354b2e06e6d (patch) | |
tree | 40e5935c6ad6777a94b7dacd5888813f77a889d5 /clang/test/Analysis/identical-expressions.cpp | |
parent | 8d96c803dfbbffa94522f4ceb51f2d500263a6fb (diff) | |
download | bcm5719-llvm-60bd88d34135aa2c4996160259e91354b2e06e6d.tar.gz bcm5719-llvm-60bd88d34135aa2c4996160259e91354b2e06e6d.zip |
[analyzer] Extend IdenticalExprChecker to check ternary operator results.
Warn if both result expressions of a ternary operator (? :) are the same.
Because only one of them will be executed, this warning will fire even if
the expressions have side effects.
Patch by Anders Rönnholm and Per Viberg!
llvm-svn: 196937
Diffstat (limited to 'clang/test/Analysis/identical-expressions.cpp')
-rw-r--r-- | clang/test/Analysis/identical-expressions.cpp | 218 |
1 files changed, 218 insertions, 0 deletions
diff --git a/clang/test/Analysis/identical-expressions.cpp b/clang/test/Analysis/identical-expressions.cpp index 50f341d3933..3a331fb8f2f 100644 --- a/clang/test/Analysis/identical-expressions.cpp +++ b/clang/test/Analysis/identical-expressions.cpp @@ -2,6 +2,21 @@ /* Only one expected warning per function allowed at the very end. */ +int func(void) +{ + return 0; +} + +int func2(void) +{ + return 0; +} + +int funcParam(int a) +{ + return 0; +} + /* '!=' operator*/ /* '!=' with float */ @@ -295,6 +310,38 @@ int checkNotEqualNestedBinaryOpIntPointerCompare2(void) { } /* end '!=' int* */ +/* '!=' with function*/ + +int checkNotEqualSameFunction() { + unsigned a = 0; + unsigned b = 1; + int res = (a+func() != a+func()); // no warning + return (0); +} + +int checkNotEqualDifferentFunction() { + unsigned a = 0; + unsigned b = 1; + int res = (a+func() != a+func2()); // no warning + return (0); +} + +int checkNotEqualSameFunctionSameParam() { + unsigned a = 0; + unsigned b = 1; + int res = (a+funcParam(a) != a+funcParam(a)); // no warning + return (0); +} + +int checkNotEqualSameFunctionDifferentParam() { + unsigned a = 0; + unsigned b = 1; + int res = (a+funcParam(a) != a+funcParam(b)); // no warning + return (0); +} + +/* end '!=' with function*/ + /* end '!=' */ @@ -526,6 +573,37 @@ int checkEqualNestedBinaryOpIntCompare3(void) { return (0); } +/* '==' with function*/ + +int checkEqualSameFunction() { + unsigned a = 0; + unsigned b = 1; + int res = (a+func() == a+func()); // no warning + return (0); +} + +int checkEqualDifferentFunction() { + unsigned a = 0; + unsigned b = 1; + int res = (a+func() == a+func2()); // no warning + return (0); +} + +int checkEqualSameFunctionSameParam() { + unsigned a = 0; + unsigned b = 1; + int res = (a+funcParam(a) == a+funcParam(a)); // no warning + return (0); +} + +int checkEqualSameFunctionDifferentParam() { + unsigned a = 0; + unsigned b = 1; + int res = (a+funcParam(a) == a+funcParam(b)); // no warning + return (0); +} + +/* end '==' with function*/ /* end EQ int */ @@ -940,3 +1018,143 @@ int checkGreaterThanNestedBinaryOpIntCompare3(void) { /* end GT with int */ /* end GT */ + + +/* Checking use of identical expressions in conditional operator*/ + +unsigned test_unsigned(unsigned a) { + unsigned b = 1; + a = a > 5 ? b : b; // expected-warning {{identical expressions on both sides of ':' in conditional expression}} + return a; +} + +void test_signed() { + int a = 0; + a = a > 5 ? a : a; // expected-warning {{identical expressions on both sides of ':' in conditional expression}} +} + +void test_bool(bool a) { + a = a > 0 ? a : a; // expected-warning {{identical expressions on both sides of ':' in conditional expression}} +} + +void test_float() { + float a = 0; + float b = 0; + a = a > 5 ? a : a; // expected-warning {{identical expressions on both sides of ':' in conditional expression}} +} + +void test_unsigned_expr() { + unsigned a = 0; + unsigned b = 0; + a = a > 5 ? a+b : a+b; // expected-warning {{identical expressions on both sides of ':' in conditional expression}} +} + +void test_signed_expr() { + int a = 0; + int b = 1; + a = a > 5 ? a+b : a+b; // expected-warning {{identical expressions on both sides of ':' in conditional expression}} +} + +void test_bool_expr(bool a) { + bool b = 0; + a = a > 0 ? a&&b : a&&b; // expected-warning {{identical expressions on both sides of ':' in conditional expression}} +} + +void test_unsigned_expr_negative() { + unsigned a = 0; + unsigned b = 0; + a = a > 5 ? a+b : b+a; // no warning +} + +void test_signed_expr_negative() { + int a = 0; + int b = 1; + a = a > 5 ? b+a : a+b; // no warning +} + +void test_bool_expr_negative(bool a) { + bool b = 0; + a = a > 0 ? a&&b : b&&a; // no warning +} + +void test_float_expr_positive() { + float a = 0; + float b = 0; + a = a > 5 ? a+b : a+b; // expected-warning {{identical expressions on both sides of ':' in conditional expression}} +} + +void test_expr_positive_func() { + unsigned a = 0; + unsigned b = 1; + a = a > 5 ? a+func() : a+func(); // expected-warning {{identical expressions on both sides of ':' in conditional expression}} +} + +void test_expr_negative_func() { + unsigned a = 0; + unsigned b = 1; + a = a > 5 ? a+func() : a+func2(); // no warning +} + +void test_expr_positive_funcParam() { + unsigned a = 0; + unsigned b = 1; + a = a > 5 ? a+funcParam(b) : a+funcParam(b); // expected-warning {{identical expressions on both sides of ':' in conditional expression}} +} + +void test_expr_negative_funcParam() { + unsigned a = 0; + unsigned b = 1; + a = a > 5 ? a+funcParam(a) : a+funcParam(b); // no warning +} + +void test_expr_positive_inc() { + unsigned a = 0; + unsigned b = 1; + a = a > 5 ? a++ : a++; // expected-warning {{identical expressions on both sides of ':' in conditional expression}} +} + +void test_expr_negative_inc() { + unsigned a = 0; + unsigned b = 1; + a = a > 5 ? a++ : b++; // no warning +} + +void test_expr_positive_assign() { + unsigned a = 0; + unsigned b = 1; + a = a > 5 ? a=1 : a=1; // expected-warning {{identical expressions on both sides of ':' in conditional expression}} +} + +void test_expr_negative_assign() { + unsigned a = 0; + unsigned b = 1; + a = a > 5 ? a=1 : a=2; // no warning +} + +void test_signed_nested_expr() { + int a = 0; + int b = 1; + int c = 3; + a = a > 5 ? a+b+(c+a)*(a + b*(c+a)) : a+b+(c+a)*(a + b*(c+a)); // expected-warning {{identical expressions on both sides of ':' in conditional expression}} +} + +void test_signed_nested_expr_negative() { + int a = 0; + int b = 1; + int c = 3; + a = a > 5 ? a+b+(c+a)*(a + b*(c+a)) : a+b+(c+a)*(a + b*(a+c)); // no warning +} + +void test_signed_nested_cond_expr_negative() { + int a = 0; + int b = 1; + int c = 3; + a = a > 5 ? (b > 5 ? 1 : 4) : (b > 5 ? 2 : 4); // no warning +} + +void test_signed_nested_cond_expr() { + int a = 0; + int b = 1; + int c = 3; + a = a > 5 ? (b > 5 ? 1 : 4) : (b > 5 ? 4 : 4); // expected-warning {{identical expressions on both sides of ':' in conditional expression}} +} |