Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -std=c++11 -analyzer-checker=alpha.clone.CloneChecker -verify %s |
| 2 | |
| 3 | // This tests if we search for clones in functions. |
| 4 | |
| 5 | void log(); |
| 6 | |
Artem Dergachev | c87d2a6 | 2016-10-03 08:11:50 +0000 | [diff] [blame^] | 7 | int max(int a, int b) { // expected-warning{{Duplicate code detected}} |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 8 | log(); |
| 9 | if (a > b) |
| 10 | return a; |
| 11 | return b; |
| 12 | } |
| 13 | |
Artem Dergachev | c87d2a6 | 2016-10-03 08:11:50 +0000 | [diff] [blame^] | 14 | int maxClone(int x, int y) { // expected-note{{Similar code here}} |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 15 | log(); |
| 16 | if (x > y) |
| 17 | return x; |
| 18 | return y; |
| 19 | } |
| 20 | |
| 21 | // Functions below are not clones and should not be reported. |
| 22 | |
Artem Dergachev | 78692ea | 2016-08-02 12:21:09 +0000 | [diff] [blame] | 23 | // The next two functions test that statement classes are still respected when |
| 24 | // checking for clones in expressions. This will show that the statement |
| 25 | // specific data of all base classes is collected, and not just the data of the |
| 26 | // first base class. |
| 27 | int testBaseClass(int a, int b) { // no-warning |
| 28 | log(); |
| 29 | if (a > b) |
| 30 | return true ? a : b; |
| 31 | return b; |
| 32 | } |
| 33 | int testBaseClass2(int a, int b) { // no-warning |
| 34 | log(); |
| 35 | if (a > b) |
| 36 | return __builtin_choose_expr(true, a, b); |
| 37 | return b; |
| 38 | } |
| 39 | |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 40 | // No clone because of the different comparison operator. |
Artem Dergachev | 78692ea | 2016-08-02 12:21:09 +0000 | [diff] [blame] | 41 | int min1(int a, int b) { // no-warning |
| 42 | log(); |
| 43 | if (a < b) |
| 44 | return a; |
| 45 | return b; |
| 46 | } |
| 47 | |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 48 | // No clone because of the different pattern in which the variables are used. |
| 49 | int min2(int a, int b) { // no-warning |
| 50 | log(); |
| 51 | if (a > b) |
| 52 | return b; |
| 53 | return a; |
| 54 | } |
| 55 | |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 56 | int foo(int a, int b) { // no-warning |
| 57 | return a + b; |
| 58 | } |