1 // RUN: %check_clang_tidy %s readability-function-size %t -- \
2 // RUN:     -config='{CheckOptions: [ \
3 // RUN:         {key: readability-function-size.LineThreshold, value: 0}, \
4 // RUN:         {key: readability-function-size.StatementThreshold, value: 0}, \
5 // RUN:         {key: readability-function-size.BranchThreshold, value: 0}, \
6 // RUN:         {key: readability-function-size.ParameterThreshold, value: 5}, \
7 // RUN:         {key: readability-function-size.NestingThreshold, value: 2}, \
8 // RUN:         {key: readability-function-size.VariableThreshold, value: 1} \
9 // RUN:     ]}'
10 
11 // Bad formatting is intentional, don't run clang-format over the whole file!
12 
foo1()13 void foo1() {
14 }
15 
foo2()16 void foo2() {;}
17 // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'foo2' exceeds recommended size/complexity thresholds [readability-function-size]
18 // CHECK-MESSAGES: :[[@LINE-2]]:6: note: 1 statements (threshold 0)
19 
foo3()20 void foo3() {
21 ;
22 
23 }
24 // CHECK-MESSAGES: :[[@LINE-4]]:6: warning: function 'foo3' exceeds recommended size/complexity
25 // CHECK-MESSAGES: :[[@LINE-5]]:6: note: 3 lines including whitespace and comments (threshold 0)
26 // CHECK-MESSAGES: :[[@LINE-6]]:6: note: 1 statements (threshold 0)
27 
foo4(int i)28 void foo4(int i) { if (i) {} else; {}
29 }
30 // CHECK-MESSAGES: :[[@LINE-2]]:6: warning: function 'foo4' exceeds recommended size/complexity
31 // CHECK-MESSAGES: :[[@LINE-3]]:6: note: 1 lines including whitespace and comments (threshold 0)
32 // CHECK-MESSAGES: :[[@LINE-4]]:6: note: 3 statements (threshold 0)
33 // CHECK-MESSAGES: :[[@LINE-5]]:6: note: 1 branches (threshold 0)
34 
foo5(int i)35 void foo5(int i) {for(;i;)while(i)
36 do;while(i);
37 }
38 // CHECK-MESSAGES: :[[@LINE-3]]:6: warning: function 'foo5' exceeds recommended size/complexity
39 // CHECK-MESSAGES: :[[@LINE-4]]:6: note: 2 lines including whitespace and comments (threshold 0)
40 // CHECK-MESSAGES: :[[@LINE-5]]:6: note: 7 statements (threshold 0)
41 // CHECK-MESSAGES: :[[@LINE-6]]:6: note: 3 branches (threshold 0)
42 
foo6(T i)43 template <typename T> T foo6(T i) {return i;
44 }
45 int x = foo6(0);
46 // CHECK-MESSAGES: :[[@LINE-3]]:25: warning: function 'foo6' exceeds recommended size/complexity
47 // CHECK-MESSAGES: :[[@LINE-4]]:25: note: 1 lines including whitespace and comments (threshold 0)
48 // CHECK-MESSAGES: :[[@LINE-5]]:25: note: 1 statements (threshold 0)
49 
foo7(int p1,int p2,int p3,int p4,int p5,int p6)50 void foo7(int p1, int p2, int p3, int p4, int p5, int p6) {;}
51 // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'foo7' exceeds recommended size/complexity
52 // CHECK-MESSAGES: :[[@LINE-2]]:6: note: 1 statements (threshold 0)
53 // CHECK-MESSAGES: :[[@LINE-3]]:6: note: 6 parameters (threshold 5)
54 
bar1()55 void bar1() { [](){;;;;;;;;;;;if(1){}}();
56 
57 
58 }
59 // CHECK-MESSAGES: :[[@LINE-4]]:6: warning: function 'bar1' exceeds recommended size/complexity
60 // CHECK-MESSAGES: :[[@LINE-5]]:6: note: 3 lines including whitespace and comments (threshold 0)
61 // CHECK-MESSAGES: :[[@LINE-6]]:6: note: 14 statements (threshold 0)
62 // CHECK-MESSAGES: :[[@LINE-7]]:6: note: 1 branches (threshold 0)
63 
bar2()64 void bar2() { class A { void barx() {;;} }; }
65 // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'bar2' exceeds recommended size/complexity
66 // CHECK-MESSAGES: :[[@LINE-2]]:6: note: 3 statements (threshold 0)
67 //
68 // CHECK-MESSAGES: :[[@LINE-4]]:30: warning: function 'barx' exceeds recommended size/complexity
69 // CHECK-MESSAGES: :[[@LINE-5]]:30: note: 2 statements (threshold 0)
70 
71 #define macro() {int x; {int y; {int z;}}}
72 
baz0()73 void baz0() { // 1
74   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'baz0' exceeds recommended size/complexity
75   // CHECK-MESSAGES: :[[@LINE-2]]:6: note: 28 lines including whitespace and comments (threshold 0)
76   // CHECK-MESSAGES: :[[@LINE-3]]:6: note: 9 statements (threshold 0)
77   int a;
78   { // 2
79     int b;
80     { // 3
81 // CHECK-MESSAGES: :[[@LINE-1]]:5: note: nesting level 3 starts here (threshold 2)
82       int c;
83       { // 4
84         int d;
85       }
86     }
87   }
88   { // 2
89     int e;
90   }
91   { // 2
92     { // 3
93 // CHECK-MESSAGES: :[[@LINE-1]]:5: note: nesting level 3 starts here (threshold 2)
94       int j;
95     }
96   }
97   macro()
98   // CHECK-MESSAGES: :[[@LINE-1]]:3: note: nesting level 3 starts here (threshold 2)
99   // CHECK-MESSAGES: :[[@LINE-28]]:25: note: expanded from macro 'macro'
100   // CHECK-MESSAGES: :[[@LINE-27]]:6: note: 9 variables (threshold 1)
101 }
102 
103 // check that nested if's are not reported. this was broken initially
nesting_if()104 void nesting_if() { // 1
105   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'nesting_if' exceeds recommended size/complexity
106   // CHECK-MESSAGES: :[[@LINE-2]]:6: note: 23 lines including whitespace and comments (threshold 0)
107   // CHECK-MESSAGES: :[[@LINE-3]]:6: note: 18 statements (threshold 0)
108   // CHECK-MESSAGES: :[[@LINE-4]]:6: note: 6 branches (threshold 0)
109   if (true) { // 2
110      int j;
111   } else if (true) { // 2
112      int j;
113      if (true) { // 3
114        // CHECK-MESSAGES: :[[@LINE-1]]:16: note: nesting level 3 starts here (threshold 2)
115        int j;
116      }
117   } else if (true) { // 2
118      int j;
119      if (true) { // 3
120        // CHECK-MESSAGES: :[[@LINE-1]]:16: note: nesting level 3 starts here (threshold 2)
121        int j;
122      }
123   } else if (true) { // 2
124      int j;
125   }
126   // CHECK-MESSAGES: :[[@LINE-22]]:6: note: 6 variables (threshold 1)
127 }
128 
129 // however this should warn
bad_if_nesting()130 void bad_if_nesting() { // 1
131 // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'bad_if_nesting' exceeds recommended size/complexity
132 // CHECK-MESSAGES: :[[@LINE-2]]:6: note: 23 lines including whitespace and comments (threshold 0)
133 // CHECK-MESSAGES: :[[@LINE-3]]:6: note: 12 statements (threshold 0)
134 // CHECK-MESSAGES: :[[@LINE-4]]:6: note: 4 branches (threshold 0)
135   if (true) {    // 2
136     int j;
137   } else { // 2
138     if (true) { // 3
139       // CHECK-MESSAGES: :[[@LINE-1]]:15: note: nesting level 3 starts here (threshold 2)
140       int j;
141     } else { // 3
142       // CHECK-MESSAGES: :[[@LINE-1]]:12: note: nesting level 3 starts here (threshold 2)
143       if (true) { // 4
144         int j;
145       } else { // 4
146         if (true) { // 5
147           int j;
148         }
149       }
150     }
151   }
152   // CHECK-MESSAGES: :[[@LINE-22]]:6: note: 4 variables (threshold 1)
153 }
154 
variables_0()155 void variables_0() {
156   int i;
157 }
158 // CHECK-MESSAGES: :[[@LINE-3]]:6: warning: function 'variables_0' exceeds recommended size/complexity thresholds [readability-function-size]
159 // CHECK-MESSAGES: :[[@LINE-4]]:6: note: 2 lines including whitespace and comments (threshold 0)
160 // CHECK-MESSAGES: :[[@LINE-5]]:6: note: 1 statements (threshold 0)
variables_1(int i)161 void variables_1(int i) {
162   int j;
163 }
164 // CHECK-MESSAGES: :[[@LINE-3]]:6: warning: function 'variables_1' exceeds recommended size/complexity thresholds [readability-function-size]
165 // CHECK-MESSAGES: :[[@LINE-4]]:6: note: 2 lines including whitespace and comments (threshold 0)
166 // CHECK-MESSAGES: :[[@LINE-5]]:6: note: 1 statements (threshold 0)
variables_2(int i,int j)167 void variables_2(int i, int j) {
168   ;
169 }
170 // CHECK-MESSAGES: :[[@LINE-3]]:6: warning: function 'variables_2' exceeds recommended size/complexity thresholds [readability-function-size]
171 // CHECK-MESSAGES: :[[@LINE-4]]:6: note: 2 lines including whitespace and comments (threshold 0)
172 // CHECK-MESSAGES: :[[@LINE-5]]:6: note: 1 statements (threshold 0)
variables_3()173 void variables_3() {
174   int i[2];
175 }
176 // CHECK-MESSAGES: :[[@LINE-3]]:6: warning: function 'variables_3' exceeds recommended size/complexity thresholds [readability-function-size]
177 // CHECK-MESSAGES: :[[@LINE-4]]:6: note: 2 lines including whitespace and comments (threshold 0)
178 // CHECK-MESSAGES: :[[@LINE-5]]:6: note: 1 statements (threshold 0)
variables_4()179 void variables_4() {
180   int i;
181   int j;
182 }
183 // CHECK-MESSAGES: :[[@LINE-4]]:6: warning: function 'variables_4' exceeds recommended size/complexity thresholds [readability-function-size]
184 // CHECK-MESSAGES: :[[@LINE-5]]:6: note: 3 lines including whitespace and comments (threshold 0)
185 // CHECK-MESSAGES: :[[@LINE-6]]:6: note: 2 statements (threshold 0)
186 // CHECK-MESSAGES: :[[@LINE-7]]:6: note: 2 variables (threshold 1)
variables_5()187 void variables_5() {
188   int i, j;
189 }
190 // CHECK-MESSAGES: :[[@LINE-3]]:6: warning: function 'variables_5' exceeds recommended size/complexity thresholds [readability-function-size]
191 // CHECK-MESSAGES: :[[@LINE-4]]:6: note: 2 lines including whitespace and comments (threshold 0)
192 // CHECK-MESSAGES: :[[@LINE-5]]:6: note: 1 statements (threshold 0)
193 // CHECK-MESSAGES: :[[@LINE-6]]:6: note: 2 variables (threshold 1)
variables_6()194 void variables_6() {
195   for (int i;;)
196     for (int j;;)
197       ;
198 }
199 // CHECK-MESSAGES: :[[@LINE-5]]:6: warning: function 'variables_6' exceeds recommended size/complexity thresholds [readability-function-size]
200 // CHECK-MESSAGES: :[[@LINE-6]]:6: note: 4 lines including whitespace and comments (threshold 0)
201 // CHECK-MESSAGES: :[[@LINE-7]]:6: note: 5 statements (threshold 0)
202 // CHECK-MESSAGES: :[[@LINE-8]]:6: note: 2 branches (threshold 0)
203 // CHECK-MESSAGES: :[[@LINE-9]]:6: note: 2 variables (threshold 1)
variables_7()204 void variables_7() {
205   if (int a = 1)
206     if (int b = 2)
207       ;
208 }
209 // CHECK-MESSAGES: :[[@LINE-5]]:6: warning: function 'variables_7' exceeds recommended size/complexity thresholds [readability-function-size]
210 // CHECK-MESSAGES: :[[@LINE-6]]:6: note: 4 lines including whitespace and comments (threshold 0)
211 // CHECK-MESSAGES: :[[@LINE-7]]:6: note: 7 statements (threshold 0)
212 // CHECK-MESSAGES: :[[@LINE-8]]:6: note: 2 branches (threshold 0)
213 // CHECK-MESSAGES: :[[@LINE-9]]:6: note: 2 variables (threshold 1)
variables_8()214 void variables_8() {
215   int a[2];
216   for (auto i : a)
217     for (auto j : a)
218       ;
219 }
220 // CHECK-MESSAGES: :[[@LINE-6]]:6: warning: function 'variables_8' exceeds recommended size/complexity thresholds [readability-function-size]
221 // CHECK-MESSAGES: :[[@LINE-7]]:6: note: 5 lines including whitespace and comments (threshold 0)
222 // CHECK-MESSAGES: :[[@LINE-8]]:6: note: 8 statements (threshold 0)
223 // CHECK-MESSAGES: :[[@LINE-9]]:6: note: 2 branches (threshold 0)
224 // CHECK-MESSAGES: :[[@LINE-10]]:6: note: 3 variables (threshold 1)
variables_9()225 void variables_9() {
226   int a, b;
227   struct A {
228     A(int c, int d) {
229       int e, f;
230     }
231   };
232 }
233 // CHECK-MESSAGES: :[[@LINE-8]]:6: warning: function 'variables_9' exceeds recommended size/complexity thresholds [readability-function-size]
234 // CHECK-MESSAGES: :[[@LINE-9]]:6: note: 7 lines including whitespace and comments (threshold 0)
235 // CHECK-MESSAGES: :[[@LINE-10]]:6: note: 3 statements (threshold 0)
236 // CHECK-MESSAGES: :[[@LINE-11]]:6: note: 2 variables (threshold 1)
237 // CHECK-MESSAGES: :[[@LINE-9]]:5: warning: function 'A' exceeds recommended size/complexity thresholds [readability-function-size]
238 // CHECK-MESSAGES: :[[@LINE-10]]:5: note: 2 lines including whitespace and comments (threshold 0)
239 // CHECK-MESSAGES: :[[@LINE-11]]:5: note: 1 statements (threshold 0)
240 // CHECK-MESSAGES: :[[@LINE-12]]:5: note: 2 variables (threshold 1)
variables_10()241 void variables_10() {
242   int a, b;
243   struct A {
244     int c;
245     int d;
246   };
247 }
248 // CHECK-MESSAGES: :[[@LINE-7]]:6: warning: function 'variables_10' exceeds recommended size/complexity thresholds [readability-function-size]
249 // CHECK-MESSAGES: :[[@LINE-8]]:6: note: 6 lines including whitespace and comments (threshold 0)
250 // CHECK-MESSAGES: :[[@LINE-9]]:6: note: 2 statements (threshold 0)
251 // CHECK-MESSAGES: :[[@LINE-10]]:6: note: 2 variables (threshold 1)
variables_11()252 void variables_11() {
253   struct S {
254     void bar() {
255       int a, b;
256     }
257   };
258 }
259 // CHECK-MESSAGES: :[[@LINE-7]]:6: warning: function 'variables_11' exceeds recommended size/complexity thresholds [readability-function-size]
260 // CHECK-MESSAGES: :[[@LINE-8]]:6: note: 6 lines including whitespace and comments (threshold 0)
261 // CHECK-MESSAGES: :[[@LINE-7]]:10: warning: function 'bar' exceeds recommended size/complexity thresholds [readability-function-size]
262 // CHECK-MESSAGES: :[[@LINE-8]]:10: note: 2 lines including whitespace and comments (threshold 0)
263 // CHECK-MESSAGES: :[[@LINE-9]]:10: note: 2 variables (threshold 1)
variables_12()264 void variables_12() {
265   int v;
266   auto test = [](int a, int b) -> void {};
267   test({}, {});
268 }
269 // CHECK-MESSAGES: :[[@LINE-5]]:6: warning: function 'variables_12' exceeds recommended size/complexity thresholds [readability-function-size]
270 // CHECK-MESSAGES: :[[@LINE-6]]:6: note: 4 lines including whitespace and comments (threshold 0)
271 // CHECK-MESSAGES: :[[@LINE-7]]:6: note: 3 statements (threshold 0)
272 // CHECK-MESSAGES: :[[@LINE-8]]:6: note: 2 variables (threshold 1)
variables_13()273 void variables_13() {
274   int v;
275   auto test = []() -> void {
276     int a;
277     int b;
278   };
279   test();
280 }
281 // CHECK-MESSAGES: :[[@LINE-8]]:6: warning: function 'variables_13' exceeds recommended size/complexity thresholds [readability-function-size]
282 // CHECK-MESSAGES: :[[@LINE-9]]:6: note: 7 lines including whitespace and comments (threshold 0)
283 // CHECK-MESSAGES: :[[@LINE-10]]:6: note: 5 statements (threshold 0)
284 // CHECK-MESSAGES: :[[@LINE-11]]:6: note: 2 variables (threshold 1)
variables_14()285 void variables_14() {
286   (void)({int a = 12; a; });
287   (void)({int a = 12; a; });
288 }
289 // CHECK-MESSAGES: :[[@LINE-4]]:6: warning: function 'variables_14' exceeds recommended size/complexity thresholds [readability-function-size]
290 // CHECK-MESSAGES: :[[@LINE-5]]:6: note: 3 lines including whitespace and comments (threshold 0)
291 // CHECK-MESSAGES: :[[@LINE-6]]:6: note: 6 statements (threshold 0)
292 #define SWAP(x, y) ({__typeof__(x) temp = x; x = y; y = temp; })
variables_15()293 void variables_15() {
294   int a = 10, b = 12;
295   SWAP(a, b);
296 }
297 // CHECK-MESSAGES: :[[@LINE-4]]:6: warning: function 'variables_15' exceeds recommended size/complexity thresholds [readability-function-size]
298 // CHECK-MESSAGES: :[[@LINE-5]]:6: note: 3 lines including whitespace and comments (threshold 0)
299 // CHECK-MESSAGES: :[[@LINE-6]]:6: note: 5 statements (threshold 0)
300 // CHECK-MESSAGES: :[[@LINE-7]]:6: note: 2 variables (threshold 1)
301 #define vardecl(type, name) type name;
variables_16()302 void variables_16() {
303   vardecl(int, a);
304   vardecl(int, b);
305 }
306 // CHECK-MESSAGES: :[[@LINE-4]]:6: warning: function 'variables_16' exceeds recommended size/complexity thresholds [readability-function-size]
307 // CHECK-MESSAGES: :[[@LINE-5]]:6: note: 3 lines including whitespace and comments (threshold 0)
308 // CHECK-MESSAGES: :[[@LINE-6]]:6: note: 4 statements (threshold 0)
309 // CHECK-MESSAGES: :[[@LINE-7]]:6: note: 2 variables (threshold 1)
310