1184c6242SDominic Chen // RUN: %clang_analyze_cc1 -std=c++11 -analyzer-checker=alpha.clone.CloneChecker -analyzer-config alpha.clone.CloneChecker:MinimumCloneComplexity=10 -verify %s
251b9a0e8SArtem Dergachev
351b9a0e8SArtem Dergachev // Tests that the complexity value of a macro expansion is about the same as
456939f7eSHiroshi Inoue // the complexity value of a normal function call and the macro body doesn't
551b9a0e8SArtem Dergachev // influence the complexity. See the CloneSignature class in CloneDetection.h
651b9a0e8SArtem Dergachev // for more information about complexity values of clones.
751b9a0e8SArtem Dergachev
851b9a0e8SArtem Dergachev #define MACRO_FOO(a, b) a > b ? -a * a : -b * b;
951b9a0e8SArtem Dergachev
1051b9a0e8SArtem Dergachev // First, manually apply MACRO_FOO and see if the code gets detected as a clone.
1151b9a0e8SArtem Dergachev // This confirms that with the current configuration the macro body would be
1251b9a0e8SArtem Dergachev // considered large enough to pass the MinimumCloneComplexity constraint.
1351b9a0e8SArtem Dergachev
manualMacro(int a,int b)144eca0de7SArtem Dergachev int manualMacro(int a, int b) { // expected-warning{{Duplicate code detected}}
1551b9a0e8SArtem Dergachev return a > b ? -a * a : -b * b;
1651b9a0e8SArtem Dergachev }
1751b9a0e8SArtem Dergachev
manualMacroClone(int a,int b)184eca0de7SArtem Dergachev int manualMacroClone(int a, int b) { // expected-note{{Similar code here}}
1951b9a0e8SArtem Dergachev return a > b ? -a * a : -b * b;
2051b9a0e8SArtem Dergachev }
2151b9a0e8SArtem Dergachev
2251b9a0e8SArtem Dergachev // Now we actually use the macro to generate the same AST as above. They
23*2a8c18d9SAlexander Kornienko // shouldn't be reported because the macros only slightly increase the complexity
2451b9a0e8SArtem Dergachev // value and the resulting code will never pass the MinimumCloneComplexity
2551b9a0e8SArtem Dergachev // constraint.
2651b9a0e8SArtem Dergachev
macro(int a,int b)2751b9a0e8SArtem Dergachev int macro(int a, int b) {
2851b9a0e8SArtem Dergachev return MACRO_FOO(a, b);
2951b9a0e8SArtem Dergachev }
3051b9a0e8SArtem Dergachev
macroClone(int a,int b)3151b9a0e8SArtem Dergachev int macroClone(int a, int b) {
3251b9a0e8SArtem Dergachev return MACRO_FOO(a, b);
3351b9a0e8SArtem Dergachev }
3451b9a0e8SArtem Dergachev
3551b9a0e8SArtem Dergachev // So far we only tested that macros increase the complexity by a lesser amount
3651b9a0e8SArtem Dergachev // than normal code. We also need to be sure this amount is not zero because
3751b9a0e8SArtem Dergachev // we otherwise macro code would be 'invisible' for the CloneDetector.
3851b9a0e8SArtem Dergachev // This tests that it is possible to increase the reach the minimum complexity
3951b9a0e8SArtem Dergachev // by only using macros. This is only possible if the complexity value is bigger
4051b9a0e8SArtem Dergachev // than zero.
4151b9a0e8SArtem Dergachev
4251b9a0e8SArtem Dergachev #define NEG(A) -(A)
4351b9a0e8SArtem Dergachev
nestedMacros()444eca0de7SArtem Dergachev int nestedMacros() { // expected-warning{{Duplicate code detected}}
4551b9a0e8SArtem Dergachev return NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(1))))))))));
4651b9a0e8SArtem Dergachev }
4751b9a0e8SArtem Dergachev
nestedMacrosClone()484eca0de7SArtem Dergachev int nestedMacrosClone() { // expected-note{{Similar code here}}
4951b9a0e8SArtem Dergachev return NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(NEG(1))))))))));
5051b9a0e8SArtem Dergachev }
51