From 94008121fafecdf89f83a63249496e185bc9cd77 Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Wed, 19 Feb 2014 17:44:16 +0000 Subject: [analyzer] Extend IdenticalExprChecker to check logical and bitwise expressions. IdenticalExprChecker now warns if any expressions in a logical or bitwise chain (&&, ||, &, |, or ^) are the same. Unlike the previous patch, this actually checks all subexpressions against each other (an O(N^2) operation, but N is likely to be small). Patch by Daniel Fahlgren! llvm-svn: 201702 --- clang/test/Analysis/identical-expressions.cpp | 86 +++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) (limited to 'clang/test/Analysis/identical-expressions.cpp') diff --git a/clang/test/Analysis/identical-expressions.cpp b/clang/test/Analysis/identical-expressions.cpp index acb7c8c57b0..950cdd93ff4 100644 --- a/clang/test/Analysis/identical-expressions.cpp +++ b/clang/test/Analysis/identical-expressions.cpp @@ -1309,3 +1309,89 @@ void test_identical_branches_if(bool b, int i) { i += 10; } } + +void test_identical_bitwise1() { + int a = 5 | 5; // expected-warning {{identical expressions on both sides of bitwise operator}} +} + +void test_identical_bitwise2() { + int a = 5; + int b = a | a; // expected-warning {{identical expressions on both sides of bitwise operator}} +} + +void test_identical_bitwise3() { + int a = 5; + int b = (a | a); // expected-warning {{identical expressions on both sides of bitwise operator}} +} + +void test_identical_bitwise4() { + int a = 4; + int b = a | 4; // no-warning +} + +void test_identical_bitwise5() { + int a = 4; + int b = 4; + int c = a | b; // no-warning +} + +void test_identical_bitwise6() { + int a = 5; + int b = a | 4 | a; // expected-warning {{identical expressions on both sides of bitwise operator}} +} + +void test_identical_bitwise7() { + int a = 5; + int b = func() | func(); // no-warning +} + +void test_identical_logical1(int a) { + if (a == 4 && a == 4) // expected-warning {{identical expressions on both sides of logical operator}} + ; +} + +void test_identical_logical2(int a) { + if (a == 4 || a == 5 || a == 4) // expected-warning {{identical expressions on both sides of logical operator}} + ; +} + +void test_identical_logical3(int a) { + if (a == 4 || a == 5 || a == 6) // no-warning + ; +} + +void test_identical_logical4(int a) { + if (a == func() || a == func()) // no-warning + ; +} + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wlogical-op-parentheses" +void test_identical_logical5(int x, int y) { + if (x == 4 && y == 5 || x == 4 && y == 6) // no-warning + ; +} + +void test_identical_logical6(int x, int y) { + if (x == 4 && y == 5 || x == 4 && y == 5) // expected-warning {{identical expressions on both sides of logical operator}} + ; +} + +void test_identical_logical7(int x, int y) { + // FIXME: We should warn here + if (x == 4 && y == 5 || x == 4) + ; +} + +void test_identical_logical8(int x, int y) { + // FIXME: We should warn here + if (x == 4 || y == 5 && x == 4) + ; +} + +void test_identical_logical9(int x, int y) { + // FIXME: We should warn here + if (x == 4 || x == 4 && y == 5) + ; +} +#pragma clang diagnostic pop -- cgit v1.2.3