189a1d03eSRichard // RUN: %check_clang_tidy %s readability-function-cognitive-complexity %t -- \ 289a1d03eSRichard // RUN: -config='{CheckOptions: \ 3*e8a3ddafSNathan James // RUN: {readability-function-cognitive-complexity.Threshold: 0, \ 4*e8a3ddafSNathan James // RUN: readability-function-cognitive-complexity.DescribeBasicIncrements: "false"}}' 589a1d03eSRichard // RUN: %check_clang_tidy -check-suffix=THRESHOLD5 %s readability-function-cognitive-complexity %t -- \ 689a1d03eSRichard // RUN: -config='{CheckOptions: \ 7*e8a3ddafSNathan James // RUN: {readability-function-cognitive-complexity.Threshold: 5, \ 8*e8a3ddafSNathan James // RUN: readability-function-cognitive-complexity.DescribeBasicIncrements: "false"}}' 989a1d03eSRichard // RUN: %check_clang_tidy -check-suffix=IGNORE-MACROS %s readability-function-cognitive-complexity %t -- \ 1089a1d03eSRichard // RUN: -config='{CheckOptions: \ 11*e8a3ddafSNathan James // RUN: {readability-function-cognitive-complexity.Threshold: 0, \ 12*e8a3ddafSNathan James // RUN: readability-function-cognitive-complexity.IgnoreMacros: "true", \ 13*e8a3ddafSNathan James // RUN: readability-function-cognitive-complexity.DescribeBasicIncrements: "false"}}' 1489a1d03eSRichard // RUN: %check_clang_tidy -check-suffix=GLOBAL-IGNORE-MACROS %s readability-function-cognitive-complexity %t -- \ 1589a1d03eSRichard // RUN: -config='{CheckOptions: \ 16*e8a3ddafSNathan James // RUN: {readability-function-cognitive-complexity.Threshold: 0, \ 17*e8a3ddafSNathan James // RUN: IgnoreMacros: "true", \ 18*e8a3ddafSNathan James // RUN: readability-function-cognitive-complexity.DescribeBasicIncrements: "false"}}' 1989a1d03eSRichard func_of_complexity_4()2089a1d03eSRichardvoid func_of_complexity_4() { 2189a1d03eSRichard // CHECK-NOTES: :[[@LINE-1]]:6: warning: function 'func_of_complexity_4' has cognitive complexity of 4 (threshold 0) [readability-function-cognitive-complexity] 2289a1d03eSRichard // CHECK-NOTES-IGNORE-MACROS: :[[@LINE-2]]:6: warning: function 'func_of_complexity_4' has cognitive complexity of 4 (threshold 0) [readability-function-cognitive-complexity] 2389a1d03eSRichard // CHECK-NOTES-GLOBAL-IGNORE-MACROS: :[[@LINE-3]]:6: warning: function 'func_of_complexity_4' has cognitive complexity of 4 (threshold 0) [readability-function-cognitive-complexity] 2489a1d03eSRichard if (1) { 2589a1d03eSRichard if (1) { 2689a1d03eSRichard } 2789a1d03eSRichard } 2889a1d03eSRichard if (1) { 2989a1d03eSRichard } 3089a1d03eSRichard } 3189a1d03eSRichard 3289a1d03eSRichard #define MacroOfComplexity10 \ 3389a1d03eSRichard if (1) { \ 3489a1d03eSRichard if (1) { \ 3589a1d03eSRichard if (1) { \ 3689a1d03eSRichard if (1) { \ 3789a1d03eSRichard } \ 3889a1d03eSRichard } \ 3989a1d03eSRichard } \ 4089a1d03eSRichard } 4189a1d03eSRichard function_with_macro()4289a1d03eSRichardvoid function_with_macro() { 4389a1d03eSRichard // CHECK-NOTES: :[[@LINE-1]]:6: warning: function 'function_with_macro' has cognitive complexity of 11 (threshold 0) [readability-function-cognitive-complexity] 4489a1d03eSRichard // CHECK-NOTES-THRESHOLD5: :[[@LINE-2]]:6: warning: function 'function_with_macro' has cognitive complexity of 11 (threshold 5) [readability-function-cognitive-complexity] 4589a1d03eSRichard // CHECK-NOTES-IGNORE-MACROS: :[[@LINE-3]]:6: warning: function 'function_with_macro' has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity] 4689a1d03eSRichard // CHECK-NOTES-GLOBAL-IGNORE-MACROS: :[[@LINE-4]]:6: warning: function 'function_with_macro' has cognitive complexity of 11 (threshold 0) [readability-function-cognitive-complexity] 4789a1d03eSRichard 4889a1d03eSRichard MacroOfComplexity10; 4989a1d03eSRichard 5089a1d03eSRichard if (1) { 5189a1d03eSRichard } 5289a1d03eSRichard } 5389a1d03eSRichard 5489a1d03eSRichard #define noop \ 5589a1d03eSRichard {} 5689a1d03eSRichard 5789a1d03eSRichard #define SomeMacro(x) \ 5889a1d03eSRichard if (1) { \ 5989a1d03eSRichard x; \ 6089a1d03eSRichard } 6189a1d03eSRichard func_macro_1()6289a1d03eSRichardvoid func_macro_1() { 6389a1d03eSRichard // CHECK-NOTES: :[[@LINE-1]]:6: warning: function 'func_macro_1' has cognitive complexity of 2 (threshold 0) [readability-function-cognitive-complexity] 6489a1d03eSRichard // CHECK-NOTES-IGNORE-MACROS: :[[@LINE-2]]:6: warning: function 'func_macro_1' has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity] 6589a1d03eSRichard // CHECK-NOTES-GLOBAL-IGNORE-MACROS: :[[@LINE-3]]:6: warning: function 'func_macro_1' has cognitive complexity of 2 (threshold 0) [readability-function-cognitive-complexity] 6689a1d03eSRichard 6789a1d03eSRichard if (1) { 6889a1d03eSRichard } 6989a1d03eSRichard SomeMacro(noop); 7089a1d03eSRichard } 7189a1d03eSRichard func_macro_2()7289a1d03eSRichardvoid func_macro_2() { 7389a1d03eSRichard // CHECK-NOTES: :[[@LINE-1]]:6: warning: function 'func_macro_2' has cognitive complexity of 4 (threshold 0) [readability-function-cognitive-complexity] 7489a1d03eSRichard // CHECK-NOTES-IGNORE-MACROS: :[[@LINE-2]]:6: warning: function 'func_macro_2' has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity] 7589a1d03eSRichard // CHECK-NOTES-GLOBAL-IGNORE-MACROS: :[[@LINE-3]]:6: warning: function 'func_macro_2' has cognitive complexity of 4 (threshold 0) [readability-function-cognitive-complexity] 7689a1d03eSRichard 7789a1d03eSRichard if (1) { 7889a1d03eSRichard } 7989a1d03eSRichard // Note that if the IgnoreMacro option is set to 'true', currently also macro 8089a1d03eSRichard // arguments are ignored. Optimally, macros should be treated like function 8189a1d03eSRichard // calls, i.e. the arguments account to the complexity so that the overall 8289a1d03eSRichard // complexity of this function is 2 (1 for the if statement above + 1 for 8389a1d03eSRichard // the if statement in the argument). 8489a1d03eSRichard SomeMacro(if (1) { noop; }); 8589a1d03eSRichard } 86