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