2015-03-09 20:18:39 +08:00
|
|
|
//===--- FunctionSizeCheck.h - clang-tidy -----------------------*- C++ -*-===//
|
2014-09-15 20:48:25 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-09-15 20:48:25 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-09 20:18:39 +08:00
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_FUNCTIONSIZECHECK_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_FUNCTIONSIZECHECK_H
|
2014-09-15 20:48:25 +08:00
|
|
|
|
2019-03-25 20:38:26 +08:00
|
|
|
#include "../ClangTidyCheck.h"
|
2014-09-15 20:48:25 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2014-10-15 18:51:57 +08:00
|
|
|
namespace readability {
|
2014-09-15 20:48:25 +08:00
|
|
|
|
2015-08-28 02:01:58 +08:00
|
|
|
/// Checks for large functions based on various metrics.
|
|
|
|
///
|
|
|
|
/// These options are supported:
|
|
|
|
///
|
|
|
|
/// * `LineThreshold` - flag functions exceeding this number of lines. The
|
|
|
|
/// default is `-1` (ignore the number of lines).
|
|
|
|
/// * `StatementThreshold` - flag functions exceeding this number of
|
|
|
|
/// statements. This may differ significantly from the number of lines for
|
|
|
|
/// 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).
|
2017-06-09 22:22:10 +08:00
|
|
|
/// * `ParameterThreshold` - flag functions having a high number of
|
|
|
|
/// parameters. The default is `-1` (ignore the number of parameters).
|
|
|
|
/// * `NestingThreshold` - flag compound statements which create next nesting
|
|
|
|
/// level after `NestingThreshold`. This may differ significantly from the
|
|
|
|
/// expected value for macro-heavy code. The default is `-1` (ignore the
|
|
|
|
/// nesting level).
|
[clang-tidy] readability-function-size: add VariableThreshold param.
Summary:
Pretty straight-forward, just count all the variable declarations in the function's body, and if more than the configured threshold - do complain.
Note that this continues perverse practice of disabling the new option by default.
I'm not certain where is the balance point between not being too noisy, and actually enforcing the good practice.
If we really want to not disable this by default, but also to not cause too many new warnings, we could default to 50 or so.
But that is a lot of variables too...
I was able to find one coding style referencing variable count:
- https://www.kernel.org/doc/html/v4.15/process/coding-style.html#functions
> Another measure of the function is the number of local variables. They shouldn’t exceed 5-10, or you’re doing something wrong.
Reviewers: hokein, xazax.hun, JonasToth, aaron.ballman, alexfh
Reviewed By: aaron.ballman
Subscribers: kimgr, Eugene.Zelenko, rnkovacs, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D44602
llvm-svn: 329902
2018-04-12 20:06:42 +08:00
|
|
|
/// * `VariableThreshold` - flag functions having a high number of variable
|
|
|
|
/// declarations. The default is `-1` (ignore the number of variables).
|
2014-09-15 20:48:25 +08:00
|
|
|
class FunctionSizeCheck : public ClangTidyCheck {
|
|
|
|
public:
|
|
|
|
FunctionSizeCheck(StringRef Name, ClangTidyContext *Context);
|
|
|
|
|
|
|
|
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
|
|
|
|
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
|
|
|
|
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const unsigned LineThreshold;
|
|
|
|
const unsigned StatementThreshold;
|
|
|
|
const unsigned BranchThreshold;
|
2017-03-01 18:17:32 +08:00
|
|
|
const unsigned ParameterThreshold;
|
2017-06-09 22:22:10 +08:00
|
|
|
const unsigned NestingThreshold;
|
[clang-tidy] readability-function-size: add VariableThreshold param.
Summary:
Pretty straight-forward, just count all the variable declarations in the function's body, and if more than the configured threshold - do complain.
Note that this continues perverse practice of disabling the new option by default.
I'm not certain where is the balance point between not being too noisy, and actually enforcing the good practice.
If we really want to not disable this by default, but also to not cause too many new warnings, we could default to 50 or so.
But that is a lot of variables too...
I was able to find one coding style referencing variable count:
- https://www.kernel.org/doc/html/v4.15/process/coding-style.html#functions
> Another measure of the function is the number of local variables. They shouldn’t exceed 5-10, or you’re doing something wrong.
Reviewers: hokein, xazax.hun, JonasToth, aaron.ballman, alexfh
Reviewed By: aaron.ballman
Subscribers: kimgr, Eugene.Zelenko, rnkovacs, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D44602
llvm-svn: 329902
2018-04-12 20:06:42 +08:00
|
|
|
const unsigned VariableThreshold;
|
2014-09-15 20:48:25 +08:00
|
|
|
};
|
|
|
|
|
2014-10-15 18:51:57 +08:00
|
|
|
} // namespace readability
|
2014-09-15 20:48:25 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|
|
|
|
|
2015-03-09 20:18:39 +08:00
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_FUNCTIONSIZECHECK_H
|