Dominic Chen | 184c624 | 2017-03-03 18:02:02 +0000 | [diff] [blame] | 1 | // RUN: %clang_analyze_cc1 -std=c++11 -analyzer-checker=alpha.clone.CloneChecker -analyzer-config alpha.clone.CloneChecker:MinimumCloneComplexity=10 -verify %s |
Artem Dergachev | 51b9a0e | 2016-08-20 10:06:59 +0000 | [diff] [blame] | 2 | |
| 3 | // Tests that the complexity value of a macro expansion is about the same as |
| 4 | // the complexity value of a normal function call and the the macro body doesn't |
| 5 | // influence the complexity. See the CloneSignature class in CloneDetection.h |
| 6 | // for more information about complexity values of clones. |
| 7 | |
| 8 | #define MACRO_FOO(a, b) a > b ? -a * a : -b * b; |
| 9 | |
| 10 | // First, manually apply MACRO_FOO and see if the code gets detected as a clone. |
| 11 | // This confirms that with the current configuration the macro body would be |
| 12 | // considered large enough to pass the MinimumCloneComplexity constraint. |
| 13 | |
Artem Dergachev | 4eca0de | 2016-10-08 10:54:30 +0000 | [diff] [blame] | 14 | int manualMacro(int a, int b) { // expected-warning{{Duplicate code detected}} |
Artem Dergachev | 51b9a0e | 2016-08-20 10:06:59 +0000 | [diff] [blame] | 15 | return a > b ? -a * a : -b * b; |
| 16 | } |
| 17 | |
Artem Dergachev | 4eca0de | 2016-10-08 10:54:30 +0000 | [diff] [blame] | 18 | int manualMacroClone(int a, int b) { // expected-note{{Similar code here}} |
Artem Dergachev | 51b9a0e | 2016-08-20 10:06:59 +0000 | [diff] [blame] | 19 | return a > b ? -a * a : -b * b; |
| 20 | } |
| 21 | |
| 22 | // Now we actually use the macro to generate the same AST as above. They |
| 23 | // shouldn't be reported because the macros only slighly increase the complexity |
| 24 | // value and the resulting code will never pass the MinimumCloneComplexity |
| 25 | // constraint. |
| 26 | |
| 27 | int macro(int a, int b) { |
| 28 | return MACRO_FOO(a, b); |
| 29 | } |
| 30 | |
| 31 | int macroClone(int a, int b) { |
| 32 | return MACRO_FOO(a, b); |
| 33 | } |
| 34 | |
| 35 | // So far we only tested that macros increase the complexity by a lesser amount |
| 36 | // than normal code. We also need to be sure this amount is not zero because |
| 37 | // we otherwise macro code would be 'invisible' for the CloneDetector. |
| 38 | // This tests that it is possible to increase the reach the minimum complexity |
| 39 | // by only using macros. This is only possible if the complexity value is bigger |
| 40 | // than zero. |
| 41 | |
| 42 | #define NEG(A) -(A) |
| 43 | |
Artem Dergachev | 4eca0de | 2016-10-08 10:54:30 +0000 | [diff] [blame] | 44 | int nestedMacros() { // expected-warning{{Duplicate code detected}} |
Artem Dergachev | 51b9a0e | 2016-08-20 10:06:59 +0000 | [diff] [blame] | 45 | return NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(1)))))))))); |
| 46 | } |
| 47 | |
Artem Dergachev | 4eca0de | 2016-10-08 10:54:30 +0000 | [diff] [blame] | 48 | int nestedMacrosClone() { // expected-note{{Similar code here}} |
Artem Dergachev | 51b9a0e | 2016-08-20 10:06:59 +0000 | [diff] [blame] | 49 | return NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(1)))))))))); |
| 50 | } |