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: docs/clang-tidy/checks/readability-function-size.rst =================================================================== --- docs/clang-tidy/checks/readability-function-size.rst +++ docs/clang-tidy/checks/readability-function-size.rst @@ -25,3 +25,8 @@ Flag functions exceeding this number of control statements. The default is `-1` (ignore the number of branches). + +.. option:: ParameterThreshold + + Flag functions that exceed a specified number of parameters. The default + is 6. Index: test/clang-tidy/readability-function-size.cpp =================================================================== --- test/clang-tidy/readability-function-size.cpp +++ test/clang-tidy/readability-function-size.cpp @@ -1,4 +1,6 @@ -// 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 + +// Bad formatting is intentional, don't run clang-format over the whole file! void foo1() { } @@ -37,6 +39,11 @@ // 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 foo7(int p1, int p2, int p3, int p4, int p5, int p6) {;} +// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'foo7' exceeds recommended size/complexity +// CHECK-MESSAGES: :[[@LINE-2]]:6: note: 1 statements (threshold 0) +// CHECK-MESSAGES: :[[@LINE-3]]:6: note: 6 parameters (threshold 5) + void bar1() { [](){;;;;;;;;;;;if(1){}}();