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