xref: /llvm-project/clang/test/Analysis/copypaste/macro-complexity.cpp (revision 2a8c18d99118827ff752cc8952a2096f33715f31)
1 // RUN: %clang_analyze_cc1 -std=c++11 -analyzer-checker=alpha.clone.CloneChecker -analyzer-config alpha.clone.CloneChecker:MinimumCloneComplexity=10 -verify %s
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 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 
manualMacro(int a,int b)14 int manualMacro(int a, int b) { // expected-warning{{Duplicate code detected}}
15   return a > b ? -a * a : -b * b;
16 }
17 
manualMacroClone(int a,int b)18 int manualMacroClone(int a, int b) { // expected-note{{Similar code here}}
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 slightly increase the complexity
24 // value and the resulting code will never pass the MinimumCloneComplexity
25 // constraint.
26 
macro(int a,int b)27 int macro(int a, int b) {
28   return MACRO_FOO(a, b);
29 }
30 
macroClone(int a,int b)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 
nestedMacros()44 int nestedMacros() { // expected-warning{{Duplicate code detected}}
45   return NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(1))))))))));
46 }
47 
nestedMacrosClone()48 int nestedMacrosClone() { // expected-note{{Similar code here}}
49   return NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(1))))))))));
50 }
51