379 lines · cpp
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 23void foo1() {24}25 26void 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 30void foo3() {31;32 33}34// CHECK-MESSAGES: :[[@LINE-4]]:6: warning: function 'foo3' exceeds recommended size/complexity35// 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 38void foo4(int i) { if (i) {} else; {}39}40// CHECK-MESSAGES: :[[@LINE-2]]:6: warning: function 'foo4' exceeds recommended size/complexity41// 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 45void foo5(int i) {for(;i;)while(i)46do;while(i);47}48// CHECK-MESSAGES: :[[@LINE-3]]:6: warning: function 'foo5' exceeds recommended size/complexity49// 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 53template <typename T> T foo6(T i) {return i;54}55int x = foo6(0);56// CHECK-MESSAGES: :[[@LINE-3]]:25: warning: function 'foo6' exceeds recommended size/complexity57// 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 60void 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/complexity62// CHECK-MESSAGES: :[[@LINE-2]]:6: note: 1 statements (threshold 0)63// CHECK-MESSAGES: :[[@LINE-3]]:6: note: 6 parameters (threshold 5)64 65void bar1() { [](){;;;;;;;;;;;if(1){}}();66 67 68}69// CHECK-MESSAGES: :[[@LINE-4]]:6: warning: function 'bar1' exceeds recommended size/complexity70// 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 74void bar2() { class A { void barx() {;;} }; }75// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'bar2' exceeds recommended size/complexity76// CHECK-MESSAGES: :[[@LINE-2]]:6: note: 3 statements (threshold 0)77//78// CHECK-MESSAGES: :[[@LINE-4]]:30: warning: function 'barx' exceeds recommended size/complexity79// CHECK-MESSAGES: :[[@LINE-5]]:30: note: 2 statements (threshold 0)80 81#define macro() {int x; {int y; {int z;}}}82 83void baz0() { // 184 // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'baz0' exceeds recommended size/complexity85 // 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 { // 289 int b;90 { // 391// CHECK-MESSAGES: :[[@LINE-1]]:5: note: nesting level 3 starts here (threshold 2)92 int c;93 { // 494 int d;95 }96 }97 }98 { // 299 int e;100 }101 { // 2102 { // 3103// 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 initially114void nesting_if() { // 1115 // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'nesting_if' exceeds recommended size/complexity116 // 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/complexity120 // CHECK-MESSAGES-OPTIONAL: :[[@LINE-6]]:6: note: 6 branches (threshold 5)121 if (true) { // 2122 int j;123 } else if (true) { // 2124 int j;125 if (true) { // 3126 // CHECK-MESSAGES: :[[@LINE-1]]:16: note: nesting level 3 starts here (threshold 2)127 int j;128 }129 } else if (true) { // 2130 int j;131 if (true) { // 3132 // CHECK-MESSAGES: :[[@LINE-1]]:16: note: nesting level 3 starts here (threshold 2)133 int j;134 }135 } else if (true) { // 2136 int j;137 }138 // CHECK-MESSAGES: :[[@LINE-24]]:6: note: 6 variables (threshold 1)139}140 141// however this should warn142void bad_if_nesting() { // 1143// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'bad_if_nesting' exceeds recommended size/complexity144// 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) { // 2148 int j;149 } else { // 2150 if (true) { // 3151 // CHECK-MESSAGES: :[[@LINE-1]]:15: note: nesting level 3 starts here (threshold 2)152 int j;153 } else { // 3154 // CHECK-MESSAGES: :[[@LINE-1]]:12: note: nesting level 3 starts here (threshold 2)155 if (true) { // 4156 int j;157 } else { // 4158 if (true) { // 5159 int j;160 }161 }162 }163 }164 // CHECK-MESSAGES: :[[@LINE-22]]:6: note: 4 variables (threshold 1)165}166 167void 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)173void 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)179void 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)185void 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)191void 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)199void 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)206void 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)216void 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)226void 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)237void 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)253void 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)264void 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)276void 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)285void 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)297void 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; })305void 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;314void 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 323struct A {324 A(int c, int d) : a(0), b(c) { ; }325 int a;326 int b;327};328// CHECK-MESSAGES: :[[@LINE-4]]:3: warning: function 'A' exceeds recommended size/complexity thresholds [readability-function-size]329// CHECK-MESSAGES: :[[@LINE-5]]:3: note: 3 statements (threshold 0)330 331struct B {332 B(int x, int y, int z) : a(x + y * z), b(), c_a(y, z) {333 ;334 }335 int a;336 int b;337 A c_a;338};339// CHECK-MESSAGES: :[[@LINE-7]]:3: warning: function 'B' exceeds recommended size/complexity thresholds [readability-function-size]340// CHECK-MESSAGES: :[[@LINE-8]]:3: note: 2 lines including whitespace and comments (threshold 0)341// CHECK-MESSAGES: :[[@LINE-9]]:3: note: 4 statements (threshold 0)342 343struct C : A, B {344 C() : A(0, 4), B(1, 2, 3) {}345};346// CHECK-MESSAGES: :[[@LINE-2]]:3: warning: function 'C' exceeds recommended size/complexity thresholds [readability-function-size]347// CHECK-MESSAGES: :[[@LINE-3]]:3: note: 2 statements (threshold 0)348 349template<typename T>350struct TemplateC {351 // 0 statements352 TemplateC() : a(3) {}353 T a;354};355// CHECK-MESSAGES: :[[@LINE-3]]:3: warning: function 'TemplateC<T>' exceeds recommended size/complexity thresholds [readability-function-size]356// CHECK-MESSAGES: :[[@LINE-4]]:3: note: 1 statements (threshold 0)357 358template<typename T>359struct TemplateD {360 template<typename U>361 TemplateD(U&& val) : member(val) { 362 ;363 }364 365 T member;366};367// CHECK-MESSAGES: :[[@LINE-6]]:3: warning: function 'TemplateD<T>' exceeds recommended size/complexity thresholds [readability-function-size]368// CHECK-MESSAGES: :[[@LINE-7]]:3: note: 2 lines including whitespace and comments (threshold 0)369// CHECK-MESSAGES: :[[@LINE-8]]:3: note: 2 statements (threshold 0)370 371void instantiate() {372 TemplateC<int> c;373 TemplateD<int> d(5);374}375// CHECK-MESSAGES: :[[@LINE-4]]:6: warning: function 'instantiate' exceeds recommended size/complexity thresholds [readability-function-size]376// CHECK-MESSAGES: :[[@LINE-5]]:6: note: 3 lines including whitespace and comments (threshold 0)377// CHECK-MESSAGES: :[[@LINE-6]]:6: note: 2 statements (threshold 0)378// CHECK-MESSAGES: :[[@LINE-7]]:6: note: 2 variables (threshold 1)379