[clang-tools-extra] r296599 - [clang-tidy] Add parametercount for readibility-function-size

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 1 02:17:33 PST 2017


Author: alexfh
Date: Wed Mar  1 04:17:32 2017
New Revision: 296599

URL: http://llvm.org/viewvc/llvm-project?rev=296599&view=rev
Log:
[clang-tidy] Add parametercount for readibility-function-size

Summary:
Add an option to function-size to warn about high parameter counts.

This might be relevant for cppcoreguidelines and the safety module as well. Since the safety module is not landed in master already, i did not create an alias, but that can be done later as well.

Reviewers: sbenza, alexfh, hokein

Reviewed By: alexfh, hokein

Subscribers: JDevlieghere

Patch by Jonas Toth!

Differential Revision: https://reviews.llvm.org/D29561

Modified:
    clang-tools-extra/trunk/clang-tidy/readability/FunctionSizeCheck.cpp
    clang-tools-extra/trunk/clang-tidy/readability/FunctionSizeCheck.h
    clang-tools-extra/trunk/docs/ReleaseNotes.rst
    clang-tools-extra/trunk/docs/clang-tidy/checks/readability-function-size.rst
    clang-tools-extra/trunk/test/clang-tidy/readability-function-size.cpp

Modified: clang-tools-extra/trunk/clang-tidy/readability/FunctionSizeCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/FunctionSizeCheck.cpp?rev=296599&r1=296598&r2=296599&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/FunctionSizeCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/FunctionSizeCheck.cpp Wed Mar  1 04:17:32 2017
@@ -72,12 +72,14 @@ FunctionSizeCheck::FunctionSizeCheck(Str
     : 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 Matc
     }
   }
 
+  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 Matc
     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

Modified: clang-tools-extra/trunk/clang-tidy/readability/FunctionSizeCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/FunctionSizeCheck.h?rev=296599&r1=296598&r2=296599&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/FunctionSizeCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/FunctionSizeCheck.h Wed Mar  1 04:17:32 2017
@@ -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

Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=296599&r1=296598&r2=296599&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Wed Mar  1 04:17:32 2017
@@ -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
 

Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/readability-function-size.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/readability-function-size.rst?rev=296599&r1=296598&r2=296599&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/readability-function-size.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/readability-function-size.rst Wed Mar  1 04:17:32 2017
@@ -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.

Modified: clang-tools-extra/trunk/test/clang-tidy/readability-function-size.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-function-size.cpp?rev=296599&r1=296598&r2=296599&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-function-size.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-function-size.cpp Wed Mar  1 04:17:32 2017
@@ -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){}}();
 
 




More information about the cfe-commits mailing list