1 // RUN: %check_clang_tidy %s readability-function-cognitive-complexity %t -- \
2 // RUN: -config='{CheckOptions: \
3 // RUN: [{key: readability-function-cognitive-complexity.Threshold, \
4 // RUN: value: 0}, \
5 // RUN: {key: readability-function-cognitive-complexity.DescribeBasicIncrements, \
6 // RUN: value: "false"} ]}'
7 // RUN: %check_clang_tidy -check-suffix=THRESHOLD5 %s readability-function-cognitive-complexity %t -- \
8 // RUN: -config='{CheckOptions: \
9 // RUN: [{key: readability-function-cognitive-complexity.Threshold, \
10 // RUN: value: 5}, \
11 // RUN: {key: readability-function-cognitive-complexity.DescribeBasicIncrements, \
12 // RUN: value: "false"} ]}'
13 // RUN: %check_clang_tidy -check-suffix=IGNORE-MACROS %s readability-function-cognitive-complexity %t -- \
14 // RUN: -config='{CheckOptions: \
15 // RUN: [{key: readability-function-cognitive-complexity.Threshold, \
16 // RUN: value: 0}, \
17 // RUN: {key: readability-function-cognitive-complexity.IgnoreMacros, \
18 // RUN: value: "true"}, \
19 // RUN: {key: readability-function-cognitive-complexity.DescribeBasicIncrements, \
20 // RUN: value: "false"} ]}'
21 // RUN: %check_clang_tidy -check-suffix=GLOBAL-IGNORE-MACROS %s readability-function-cognitive-complexity %t -- \
22 // RUN: -config='{CheckOptions: \
23 // RUN: [{key: readability-function-cognitive-complexity.Threshold, \
24 // RUN: value: 0}, \
25 // RUN: {key: IgnoreMacros, \
26 // RUN: value: "true"}, \
27 // RUN: {key: readability-function-cognitive-complexity.DescribeBasicIncrements, \
28 // RUN: value: "false"} ]}'
29
func_of_complexity_4()30 void func_of_complexity_4() {
31 // CHECK-NOTES: :[[@LINE-1]]:6: warning: function 'func_of_complexity_4' has cognitive complexity of 4 (threshold 0) [readability-function-cognitive-complexity]
32 // CHECK-NOTES-IGNORE-MACROS: :[[@LINE-2]]:6: warning: function 'func_of_complexity_4' has cognitive complexity of 4 (threshold 0) [readability-function-cognitive-complexity]
33 // 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]
34 if (1) {
35 if (1) {
36 }
37 }
38 if (1) {
39 }
40 }
41
42 #define MacroOfComplexity10 \
43 if (1) { \
44 if (1) { \
45 if (1) { \
46 if (1) { \
47 } \
48 } \
49 } \
50 }
51
function_with_macro()52 void function_with_macro() {
53 // CHECK-NOTES: :[[@LINE-1]]:6: warning: function 'function_with_macro' has cognitive complexity of 11 (threshold 0) [readability-function-cognitive-complexity]
54 // CHECK-NOTES-THRESHOLD5: :[[@LINE-2]]:6: warning: function 'function_with_macro' has cognitive complexity of 11 (threshold 5) [readability-function-cognitive-complexity]
55 // CHECK-NOTES-IGNORE-MACROS: :[[@LINE-3]]:6: warning: function 'function_with_macro' has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity]
56 // CHECK-NOTES-GLOBAL-IGNORE-MACROS: :[[@LINE-4]]:6: warning: function 'function_with_macro' has cognitive complexity of 11 (threshold 0) [readability-function-cognitive-complexity]
57
58 MacroOfComplexity10;
59
60 if (1) {
61 }
62 }
63
64 #define noop \
65 {}
66
67 #define SomeMacro(x) \
68 if (1) { \
69 x; \
70 }
71
func_macro_1()72 void func_macro_1() {
73 // CHECK-NOTES: :[[@LINE-1]]:6: warning: function 'func_macro_1' has cognitive complexity of 2 (threshold 0) [readability-function-cognitive-complexity]
74 // CHECK-NOTES-IGNORE-MACROS: :[[@LINE-2]]:6: warning: function 'func_macro_1' has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity]
75 // CHECK-NOTES-GLOBAL-IGNORE-MACROS: :[[@LINE-3]]:6: warning: function 'func_macro_1' has cognitive complexity of 2 (threshold 0) [readability-function-cognitive-complexity]
76
77 if (1) {
78 }
79 SomeMacro(noop);
80 }
81
func_macro_2()82 void func_macro_2() {
83 // CHECK-NOTES: :[[@LINE-1]]:6: warning: function 'func_macro_2' has cognitive complexity of 4 (threshold 0) [readability-function-cognitive-complexity]
84 // CHECK-NOTES-IGNORE-MACROS: :[[@LINE-2]]:6: warning: function 'func_macro_2' has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity]
85 // CHECK-NOTES-GLOBAL-IGNORE-MACROS: :[[@LINE-3]]:6: warning: function 'func_macro_2' has cognitive complexity of 4 (threshold 0) [readability-function-cognitive-complexity]
86
87 if (1) {
88 }
89 // Note that if the IgnoreMacro option is set to 'true', currently also macro
90 // arguments are ignored. Optimally, macros should be treated like function
91 // calls, i.e. the arguments account to the complexity so that the overall
92 // complexity of this function is 2 (1 for the if statement above + 1 for
93 // the if statement in the argument).
94 SomeMacro(if (1) { noop; });
95 }
96