forked from OSchip/llvm-project
[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 llvm-svn: 296599
This commit is contained in:
parent
eedf7ec07f
commit
9108644dbf
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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){}}();
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue