77 lines · cpp
1// RUN: %check_clang_tidy %s readability-function-cognitive-complexity %t -- \2// RUN: -config='{CheckOptions: \3// RUN: {readability-function-cognitive-complexity.Threshold: 0, \4// RUN: readability-function-cognitive-complexity.DescribeBasicIncrements: "false"}}'5// RUN: %check_clang_tidy -check-suffix=THRESHOLD5 %s readability-function-cognitive-complexity %t -- \6// RUN: -config='{CheckOptions: \7// RUN: {readability-function-cognitive-complexity.Threshold: 5, \8// RUN: readability-function-cognitive-complexity.DescribeBasicIncrements: "false"}}'9// RUN: %check_clang_tidy -check-suffix=IGNORE-MACROS %s readability-function-cognitive-complexity %t -- \10// RUN: -config='{CheckOptions: \11// RUN: {readability-function-cognitive-complexity.Threshold: 0, \12// RUN: readability-function-cognitive-complexity.IgnoreMacros: "true", \13// RUN: readability-function-cognitive-complexity.DescribeBasicIncrements: "false"}}'14 15void func_of_complexity_4() {16 // CHECK-NOTES: :[[@LINE-1]]:6: warning: function 'func_of_complexity_4' has cognitive complexity of 4 (threshold 0) [readability-function-cognitive-complexity]17 // CHECK-NOTES-IGNORE-MACROS: :[[@LINE-2]]:6: warning: function 'func_of_complexity_4' has cognitive complexity of 4 (threshold 0) [readability-function-cognitive-complexity]18 if (1) {19 if (1) {20 }21 }22 if (1) {23 }24}25 26#define MacroOfComplexity10 \27 if (1) { \28 if (1) { \29 if (1) { \30 if (1) { \31 } \32 } \33 } \34 }35 36void function_with_macro() {37 // CHECK-NOTES: :[[@LINE-1]]:6: warning: function 'function_with_macro' has cognitive complexity of 11 (threshold 0) [readability-function-cognitive-complexity]38 // CHECK-NOTES-THRESHOLD5: :[[@LINE-2]]:6: warning: function 'function_with_macro' has cognitive complexity of 11 (threshold 5) [readability-function-cognitive-complexity]39 // CHECK-NOTES-IGNORE-MACROS: :[[@LINE-3]]:6: warning: function 'function_with_macro' has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity]40 41 MacroOfComplexity10;42 43 if (1) {44 }45}46 47#define noop \48 {}49 50#define SomeMacro(x) \51 if (1) { \52 x; \53 }54 55void func_macro_1() {56 // CHECK-NOTES: :[[@LINE-1]]:6: warning: function 'func_macro_1' has cognitive complexity of 2 (threshold 0) [readability-function-cognitive-complexity]57 // CHECK-NOTES-IGNORE-MACROS: :[[@LINE-2]]:6: warning: function 'func_macro_1' has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity]58 59 if (1) {60 }61 SomeMacro(noop);62}63 64void func_macro_2() {65 // CHECK-NOTES: :[[@LINE-1]]:6: warning: function 'func_macro_2' has cognitive complexity of 4 (threshold 0) [readability-function-cognitive-complexity]66 // CHECK-NOTES-IGNORE-MACROS: :[[@LINE-2]]:6: warning: function 'func_macro_2' has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity]67 68 if (1) {69 }70 // Note that if the IgnoreMacro option is set to 'true', currently also macro71 // arguments are ignored. Optimally, macros should be treated like function72 // calls, i.e. the arguments account to the complexity so that the overall73 // complexity of this function is 2 (1 for the if statement above + 1 for74 // the if statement in the argument).75 SomeMacro(if (1) { noop; });76}77