Index: clang-tidy/readability/FunctionSizeCheck.h =================================================================== --- clang-tidy/readability/FunctionSizeCheck.h +++ clang-tidy/readability/FunctionSizeCheck.h @@ -27,6 +27,8 @@ /// macro-heavy code. The default is `800`. /// * `BranchThreshold` - flag functions exceeding this number of control /// statements. The default is `-1` (ignore the number of branches). +/// * `ParameterThreshold` - flag functions having a high number of parameters. +/// The default is `6`. class FunctionSizeCheck : public ClangTidyCheck { public: FunctionSizeCheck(StringRef Name, ClangTidyContext *Context); @@ -39,6 +41,7 @@ const unsigned LineThreshold; const unsigned StatementThreshold; const unsigned BranchThreshold; + const unsigned ParameterThreshold; }; } // namespace readability Index: clang-tidy/readability/FunctionSizeCheck.cpp =================================================================== --- clang-tidy/readability/FunctionSizeCheck.cpp +++ clang-tidy/readability/FunctionSizeCheck.cpp @@ -72,12 +72,14 @@ : ClangTidyCheck(Name, Context), LineThreshold(Options.get("LineThreshold", -1U)), StatementThreshold(Options.get("StatementThreshold", 800U)), - BranchThreshold(Options.get("BranchThreshold", -1U)) {} + BranchThreshold(Options.get("BranchThreshold", -1U)), + ParameterThreshold(Options.get("ParameterThreshold", 6)) {} void FunctionSizeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "LineThreshold", LineThreshold); Options.store(Opts, "StatementThreshold", StatementThreshold); Options.store(Opts, "BranchThreshold", BranchThreshold); + Options.store(Opts, "ParameterThreshold", ParameterThreshold); } void FunctionSizeCheck::registerMatchers(MatchFinder *Finder) { @@ -103,8 +105,11 @@ } } + unsigned ActualNumberParameters = Func->getNumParams(); + if (FI.Lines > LineThreshold || FI.Statements > StatementThreshold || - FI.Branches > BranchThreshold) { + FI.Branches > BranchThreshold || + ActualNumberParameters > ParameterThreshold) { diag(Func->getLocation(), "function %0 exceeds recommended size/complexity thresholds") << Func; @@ -127,6 +132,12 @@ diag(Func->getLocation(), "%0 branches (threshold %1)", DiagnosticIDs::Note) << FI.Branches << BranchThreshold; } + + if (ActualNumberParameters > ParameterThreshold) { + diag(Func->getLocation(), "%0 parameters (threshold %1)", + DiagnosticIDs::Note) + << ActualNumberParameters << ParameterThreshold; + } } } // namespace readability Index: test/clang-tidy/readability-function-size.cpp =================================================================== --- test/clang-tidy/readability-function-size.cpp +++ test/clang-tidy/readability-function-size.cpp @@ -1,54 +1,79 @@ -// RUN: %check_clang_tidy %s readability-function-size %t -- -config='{CheckOptions: [{key: readability-function-size.LineThreshold, value: 0}, {key: readability-function-size.StatementThreshold, value: 0}, {key: readability-function-size.BranchThreshold, value: 0}]}' -- -std=c++11 +// RUN: %check_clang_tidy %s readability-function-size %t -- -config='{CheckOptions: [{key: readability-function-size.LineThreshold, value: 0}, {key: readability-function-size.StatementThreshold, value: 0}, {key: readability-function-size.BranchThreshold, value: 0}, {key: readability-function-size.ParameterThreshold, value: 5}]}' -- -std=c++11 void foo1() { } -void foo2() {;} +void foo2() { ; } // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'foo2' exceeds recommended size/complexity thresholds [readability-function-size] // CHECK-MESSAGES: :[[@LINE-2]]:6: note: 1 statements (threshold 0) void foo3() { -; - + ; } -// CHECK-MESSAGES: :[[@LINE-4]]:6: warning: function 'foo3' exceeds recommended size/complexity -// CHECK-MESSAGES: :[[@LINE-5]]:6: note: 3 lines including whitespace and comments (threshold 0) -// CHECK-MESSAGES: :[[@LINE-6]]:6: note: 1 statements (threshold 0) +// CHECK-MESSAGES: :[[@LINE-3]]:6: warning: function 'foo3' exceeds recommended size/complexity thresholds [readability-function-size] +// CHECK-MESSAGES: :[[@LINE-4]]:6: note: 2 lines including whitespace and comments (threshold 0) +// CHECK-MESSAGES: :[[@LINE-5]]:6: note: 1 statements (threshold 0) -void foo4(int i) { if (i) {} else; {} +void foo4(int i) { + if (i) { + } else + ; + {} } -// CHECK-MESSAGES: :[[@LINE-2]]:6: warning: function 'foo4' exceeds recommended size/complexity -// CHECK-MESSAGES: :[[@LINE-3]]:6: note: 1 lines including whitespace and comments (threshold 0) -// CHECK-MESSAGES: :[[@LINE-4]]:6: note: 3 statements (threshold 0) -// CHECK-MESSAGES: :[[@LINE-5]]:6: note: 1 branches (threshold 0) +// CHECK-MESSAGES: :[[@LINE-6]]:6: warning: function 'foo4' exceeds recommended size/complexity thresholds [readability-function-size] +// CHECK-MESSAGES: :[[@LINE-7]]:6: note: 5 lines including whitespace and comments (threshold 0) +// CHECK-MESSAGES: :[[@LINE-8]]:6: note: 3 statements (threshold 0) +// CHECK-MESSAGES: :[[@LINE-9]]:6: note: 1 branches (threshold 0) -void foo5(int i) {for(;i;)while(i) -do;while(i); +void foo5(int i) { + for (; i;) + while (i) + do + ; + while (i); } -// CHECK-MESSAGES: :[[@LINE-3]]:6: warning: function 'foo5' exceeds recommended size/complexity -// CHECK-MESSAGES: :[[@LINE-4]]:6: note: 2 lines including whitespace and comments (threshold 0) -// CHECK-MESSAGES: :[[@LINE-5]]:6: note: 7 statements (threshold 0) -// CHECK-MESSAGES: :[[@LINE-6]]:6: note: 3 branches (threshold 0) +// CHECK-MESSAGES: :[[@LINE-7]]:6: warning: function 'foo5' exceeds recommended size/complexity thresholds [readability-function-size] +// CHECK-MESSAGES: :[[@LINE-8]]:6: note: 6 lines including whitespace and comments (threshold 0) +// CHECK-MESSAGES: :[[@LINE-9]]:6: note: 7 statements (threshold 0) +// CHECK-MESSAGES: :[[@LINE-10]]:6: note: 3 branches (threshold 0) -template T foo6(T i) {return i; +template +T foo6(T i) { + return i; } int x = foo6(0); -// CHECK-MESSAGES: :[[@LINE-3]]:25: warning: function 'foo6' exceeds recommended size/complexity -// CHECK-MESSAGES: :[[@LINE-4]]:25: note: 1 lines including whitespace and comments (threshold 0) -// CHECK-MESSAGES: :[[@LINE-5]]:25: note: 1 statements (threshold 0) - -void bar1() { [](){;;;;;;;;;;;if(1){}}(); +// CHECK-MESSAGES: :[[@LINE-4]]:3: warning: function 'foo6' exceeds recommended size/complexity thresholds [readability-function-size] +// CHECK-MESSAGES: :[[@LINE-5]]:3: note: 2 lines including whitespace and comments (threshold 0) +// CHECK-MESSAGES: :[[@LINE-6]]:3: note: 1 statements (threshold 0) +void foo7(int p1, int p2, int p3, int p4, int p5, int p6, int p7) { + ; +} +// CHECK-MESSAGES: :[[@LINE-3]]:6: warning: function 'foo7' exceeds recommended size/complexity thresholds [readability-function-size] +// CHECK-MESSAGES: :[[@LINE-4]]:6: note: 2 lines including whitespace and comments (threshold 0) +// CHECK-MESSAGES: :[[@LINE-5]]:6: note: 1 statements (threshold 0) +// CHECK-MESSAGES: :[[@LINE-6]]:6: note: 7 parameters (threshold 5) +void bar1() { + []() {;;;;;;;;;;;if(1){} }(); } -// CHECK-MESSAGES: :[[@LINE-4]]:6: warning: function 'bar1' exceeds recommended size/complexity -// CHECK-MESSAGES: :[[@LINE-5]]:6: note: 3 lines including whitespace and comments (threshold 0) -// CHECK-MESSAGES: :[[@LINE-6]]:6: note: 14 statements (threshold 0) -// CHECK-MESSAGES: :[[@LINE-7]]:6: note: 1 branches (threshold 0) +// CHECK-MESSAGES: :[[@LINE-3]]:6: warning: function 'bar1' exceeds recommended size/complexity thresholds [readability-function-size] +// CHECK-MESSAGES: :[[@LINE-4]]:6: note: 2 lines including whitespace and comments (threshold 0) +// CHECK-MESSAGES: :[[@LINE-5]]:6: note: 14 statements (threshold 0) +// CHECK-MESSAGES: :[[@LINE-6]]:6: note: 1 branches (threshold 0) -void bar2() { class A { void barx() {;;} }; } -// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'bar2' exceeds recommended size/complexity -// CHECK-MESSAGES: :[[@LINE-2]]:6: note: 3 statements (threshold 0) +void bar2() { + class A { + void barx() { + ; + ; + } + }; +} +// CHECK-MESSAGES: :[[@LINE-8]]:6: warning: function 'bar2' exceeds recommended size/complexity thresholds [readability-function-size] +// CHECK-MESSAGES: :[[@LINE-9]]:6: note: 7 lines including whitespace and comments (threshold 0) +// CHECK-MESSAGES: :[[@LINE-10]]:6: note: 3 statements (threshold 0) // -// CHECK-MESSAGES: :[[@LINE-4]]:30: warning: function 'barx' exceeds recommended size/complexity -// CHECK-MESSAGES: :[[@LINE-5]]:30: note: 2 statements (threshold 0) +// CHECK-MESSAGES: :[[@LINE-10]]:10: warning: function 'barx' exceeds recommended size/complexity thresholds [readability-function-size] +// CHECK-MESSAGES: :[[@LINE-11]]:10: note: 3 lines including whitespace and comments (threshold 0) +// CHECK-MESSAGES: :[[@LINE-12]]:10: note: 2 statements (threshold 0)