Clang Project

clang_source_code/test/Analysis/copypaste/macro-complexity.cpp
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
14int manualMacro(int a, int b) { // expected-warning{{Duplicate code detected}}
15  return a > b ? -a * a : -b * b;
16}
17
18int 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
27int macro(int a, int b) {
28  return MACRO_FOO(a, b);
29}
30
31int 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
44int nestedMacros() { // expected-warning{{Duplicate code detected}}
45  return NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(1))))))))));
46}
47
48int nestedMacrosClone() { // expected-note{{Similar code here}}
49  return NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(1))))))))));
50}
51