diff options
5 files changed, 33 insertions, 3 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp b/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp index d02972dde18..138240a3d87 100644 --- a/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp @@ -72,12 +72,14 @@ FunctionSizeCheck::FunctionSizeCheck(StringRef Name, ClangTidyContext *Context) : 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 @@ void FunctionSizeCheck::check(const MatchFinder::MatchResult &Result) { } } + 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 @@ void FunctionSizeCheck::check(const MatchFinder::MatchResult &Result) { 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 diff --git a/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.h b/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.h index ed40330c3cd..905a0d79e84 100644 --- a/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.h +++ b/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.h @@ -27,6 +27,8 @@ namespace readability { /// 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 @@ private: const unsigned LineThreshold; const unsigned StatementThreshold; const unsigned BranchThreshold; + const unsigned ParameterThreshold; }; } // namespace readability diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index e793a4ada07..f54d49a837b 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -67,6 +67,10 @@ Improvements to clang-tidy Finds misleading indentation where braces should be introduced or the code should be reformatted. +- Added `ParameterThreshold` to `readability-function-size`. + + Finds functions that have more then `ParameterThreshold` parameters and emits a warning. + - New `safety-no-assembler <http://clang.llvm.org/extra/clang-tidy/checks/safety-no-assembler.html>`_ check diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability-function-size.rst b/clang-tools-extra/docs/clang-tidy/checks/readability-function-size.rst index fbefa971987..ecb3cde7d62 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/readability-function-size.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/readability-function-size.rst @@ -25,3 +25,8 @@ Options 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. diff --git a/clang-tools-extra/test/clang-tidy/readability-function-size.cpp b/clang-tools-extra/test/clang-tidy/readability-function-size.cpp index 03c94bd7b55..d98682c2b74 100644 --- a/clang-tools-extra/test/clang-tidy/readability-function-size.cpp +++ b/clang-tools-extra/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 @@ int x = foo6(0); // 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){}}(); |