diff options
| author | Jordan Rose <jordan_rose@apple.com> | 2014-02-19 17:44:11 +0000 |
|---|---|---|
| committer | Jordan Rose <jordan_rose@apple.com> | 2014-02-19 17:44:11 +0000 |
| commit | 70e7e8718e14f8c29ff57c4c7e2fe07ad226ec36 (patch) | |
| tree | 0aad9321b648a0b1ead544f7e478288a20cba52e /clang/test/Analysis/identical-expressions.cpp | |
| parent | daeafb4c2ae969cefc54cd04bf4b114003740b00 (diff) | |
| download | bcm5719-llvm-70e7e8718e14f8c29ff57c4c7e2fe07ad226ec36.tar.gz bcm5719-llvm-70e7e8718e14f8c29ff57c4c7e2fe07ad226ec36.zip | |
[analyzer] Extend IdenticalExprChecker to check the two branches of an if.
This extends the checks for identical expressions to handle identical
statements, and compares the consequent and alternative ("then" and "else")
branches of an if-statement to see if they are identical, treating a single
statement surrounded by braces as equivalent to one without braces.
This does /not/ check subsequent branches in an if/else chain, let alone
branches that are not consecutive. This may improve in a future patch, but
it would certainly take more work.
Patch by Daniel Fahlgren!
llvm-svn: 201701
Diffstat (limited to 'clang/test/Analysis/identical-expressions.cpp')
| -rw-r--r-- | clang/test/Analysis/identical-expressions.cpp | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/clang/test/Analysis/identical-expressions.cpp b/clang/test/Analysis/identical-expressions.cpp index 3a331fb8f2f..acb7c8c57b0 100644 --- a/clang/test/Analysis/identical-expressions.cpp +++ b/clang/test/Analysis/identical-expressions.cpp @@ -1043,6 +1043,11 @@ void test_float() { a = a > 5 ? a : a; // expected-warning {{identical expressions on both sides of ':' in conditional expression}} } +const char *test_string() { + float a = 0; + return a > 5 ? "abc" : "abc"; // expected-warning {{identical expressions on both sides of ':' in conditional expression}} +} + void test_unsigned_expr() { unsigned a = 0; unsigned b = 0; @@ -1158,3 +1163,149 @@ void test_signed_nested_cond_expr() { 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}} } + +void test_identical_branches1(bool b) { + int i = 0; + if (b) { // expected-warning {{true and false branches are identical}} + ++i; + } else { + ++i; + } +} + +void test_identical_branches2(bool b) { + int i = 0; + if (b) { // expected-warning {{true and false branches are identical}} + ++i; + } else + ++i; +} + +void test_identical_branches3(bool b) { + int i = 0; + if (b) { // no warning + ++i; + } else { + i++; + } +} + +void test_identical_branches4(bool b) { + int i = 0; + if (b) { // expected-warning {{true and false branches are identical}} + } else { + } +} + +void test_identical_branches_break(bool b) { + while (true) { + if (b) // expected-warning {{true and false branches are identical}} + break; + else + break; + } +} + +void test_identical_branches_continue(bool b) { + while (true) { + if (b) // expected-warning {{true and false branches are identical}} + continue; + else + continue; + } +} + +void test_identical_branches_func(bool b) { + if (b) // expected-warning {{true and false branches are identical}} + func(); + else + func(); +} + +void test_identical_branches_func_arguments(bool b) { + if (b) // no-warning + funcParam(1); + else + funcParam(2); +} + +void test_identical_branches_cast1(bool b) { + long v = -7; + if (b) // no-warning + v = (signed int) v; + else + v = (unsigned int) v; +} + +void test_identical_branches_cast2(bool b) { + long v = -7; + if (b) // expected-warning {{true and false branches are identical}} + v = (signed int) v; + else + v = (signed int) v; +} + +int test_identical_branches_return_int(bool b) { + int i = 0; + if (b) { // expected-warning {{true and false branches are identical}} + i++; + return i; + } else { + i++; + return i; + } +} + +int test_identical_branches_return_func(bool b) { + if (b) { // expected-warning {{true and false branches are identical}} + return func(); + } else { + return func(); + } +} + +void test_identical_branches_for(bool b) { + int i; + int j; + if (b) { // expected-warning {{true and false branches are identical}} + for (i = 0, j = 0; i < 10; i++) + j += 4; + } else { + for (i = 0, j = 0; i < 10; i++) + j += 4; + } +} + +void test_identical_branches_while(bool b) { + int i = 10; + if (b) { // expected-warning {{true and false branches are identical}} + while (func()) + i--; + } else { + while (func()) + i--; + } +} + +void test_identical_branches_do_while(bool b) { + int i = 10; + if (b) { // expected-warning {{true and false branches are identical}} + do { + i--; + } while (func()); + } else { + do { + i--; + } while (func()); + } +} + +void test_identical_branches_if(bool b, int i) { + if (b) { // expected-warning {{true and false branches are identical}} + if (i < 5) + i += 10; + } else { + if (i < 5) + i += 10; + } +} |

