xref: /llvm-project/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.h (revision 7a73da4c85a12341752a4573c55ebff46ba20db0)
1 //===--- FunctionSizeCheck.h - clang-tidy -----------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_FUNCTIONSIZECHECK_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_FUNCTIONSIZECHECK_H
11 
12 #include "../ClangTidyCheck.h"
13 
14 namespace clang::tidy::readability {
15 
16 /// Checks for large functions based on various metrics.
17 ///
18 /// These options are supported:
19 ///
20 ///   * `LineThreshold` - flag functions exceeding this number of lines. The
21 ///     default is `-1` (ignore the number of lines).
22 ///   * `StatementThreshold` - flag functions exceeding this number of
23 ///     statements. This may differ significantly from the number of lines for
24 ///     macro-heavy code. The default is `800`.
25 ///   * `BranchThreshold` - flag functions exceeding this number of control
26 ///     statements. The default is `-1` (ignore the number of branches).
27 ///   * `ParameterThreshold` - flag functions having a high number of
28 ///     parameters. The default is `-1` (ignore the number of parameters).
29 ///   * `NestingThreshold` - flag compound statements which create next nesting
30 ///     level after `NestingThreshold`. This may differ significantly from the
31 ///     expected value for macro-heavy code. The default is `-1` (ignore the
32 ///     nesting level).
33 ///   * `VariableThreshold` - flag functions having a high number of variable
34 ///     declarations. The default is `-1` (ignore the number of variables).
35 class FunctionSizeCheck : public ClangTidyCheck {
36 public:
37   FunctionSizeCheck(StringRef Name, ClangTidyContext *Context);
38 
39   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
40   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
41   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
42 
43 private:
44   const std::optional<unsigned> LineThreshold;
45   const std::optional<unsigned> StatementThreshold;
46   const std::optional<unsigned> BranchThreshold;
47   const std::optional<unsigned> ParameterThreshold;
48   const std::optional<unsigned> NestingThreshold;
49   const std::optional<unsigned> VariableThreshold;
50 
51   static constexpr std::optional<unsigned> DefaultLineThreshold = std::nullopt;
52   static constexpr std::optional<unsigned> DefaultStatementThreshold = 800U;
53   static constexpr std::optional<unsigned> DefaultBranchThreshold =
54       std::nullopt;
55   static constexpr std::optional<unsigned> DefaultParameterThreshold =
56       std::nullopt;
57   static constexpr std::optional<unsigned> DefaultNestingThreshold =
58       std::nullopt;
59   static constexpr std::optional<unsigned> DefaultVariableThreshold =
60       std::nullopt;
61 };
62 
63 } // namespace clang::tidy::readability
64 
65 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_FUNCTIONSIZECHECK_H
66