10#include "clang/AST/RecursiveASTVisitor.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "llvm/ADT/BitVector.h"
23 bool VisitVarDecl(VarDecl *VD) {
27 !(isa<ParmVarDecl>(VD) || isa<DecompositionDecl>(VD)))
31 bool VisitBindingDecl(BindingDecl *BD) {
38 bool TraverseStmt(Stmt *Node) {
40 return Base::TraverseStmt(Node);
45 switch (
Node->getStmtClass()) {
46 case Stmt::IfStmtClass:
47 case Stmt::WhileStmtClass:
48 case Stmt::DoStmtClass:
49 case Stmt::CXXForRangeStmtClass:
50 case Stmt::ForStmtClass:
51 case Stmt::SwitchStmtClass:
54 case Stmt::CompoundStmtClass:
62 Base::TraverseStmt(Node);
69 bool TraverseCompoundStmt(CompoundStmt *Node) {
73 if (CurrentNestingLevel ==
Info.NestingThreshold)
74 Info.NestingThresholders.push_back(
Node->getBeginLoc());
76 ++CurrentNestingLevel;
77 Base::TraverseCompoundStmt(Node);
78 --CurrentNestingLevel;
83 bool TraverseDecl(
Decl *Node) {
85 Base::TraverseDecl(Node);
90 bool TraverseLambdaExpr(LambdaExpr *Node) {
92 Base::TraverseLambdaExpr(Node);
97 bool TraverseCXXRecordDecl(CXXRecordDecl *Node) {
99 Base::TraverseCXXRecordDecl(Node);
104 bool TraverseStmtExpr(StmtExpr *SE) {
106 Base::TraverseStmtExpr(SE);
111 struct FunctionInfo {
122 unsigned CurrentNestingLevel = 0;
129 LineThreshold(Options.get(
"LineThreshold", -1U)),
130 StatementThreshold(Options.get(
"StatementThreshold", 800U)),
131 BranchThreshold(Options.get(
"BranchThreshold", -1U)),
132 ParameterThreshold(Options.get(
"ParameterThreshold", -1U)),
134 VariableThreshold(Options.get(
"VariableThreshold", -1U)) {}
138 Options.
store(Opts,
"StatementThreshold", StatementThreshold);
139 Options.
store(Opts,
"BranchThreshold", BranchThreshold);
140 Options.
store(Opts,
"ParameterThreshold", ParameterThreshold);
141 Options.
store(Opts,
"NestingThreshold", NestingThreshold);
142 Options.
store(Opts,
"VariableThreshold", VariableThreshold);
148 Finder->addMatcher(functionDecl(unless(isInstantiated()),
149 unless(cxxMethodDecl(ofClass(isLambda()))))
155 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>(
"func");
157 FunctionASTVisitor Visitor;
158 Visitor.Info.NestingThreshold = NestingThreshold;
159 Visitor.TraverseDecl(
const_cast<FunctionDecl *
>(Func));
160 auto &FI = Visitor.Info;
162 if (FI.Statements == 0)
166 if (
const Stmt *Body = Func->getBody()) {
167 SourceManager *SM = Result.SourceManager;
168 if (SM->isWrittenInSameFile(Body->getBeginLoc(), Body->getEndLoc())) {
169 FI.Lines = SM->getSpellingLineNumber(Body->getEndLoc()) -
170 SM->getSpellingLineNumber(Body->getBeginLoc());
174 unsigned ActualNumberParameters = Func->getNumParams();
176 if (FI.Lines > LineThreshold || FI.Statements > StatementThreshold ||
177 FI.Branches > BranchThreshold ||
178 ActualNumberParameters > ParameterThreshold ||
179 !FI.NestingThresholders.empty() || FI.Variables > VariableThreshold) {
180 diag(Func->getLocation(),
181 "function %0 exceeds recommended size/complexity thresholds")
185 if (FI.Lines > LineThreshold) {
186 diag(Func->getLocation(),
187 "%0 lines including whitespace and comments (threshold %1)",
189 << FI.Lines << LineThreshold;
192 if (FI.Statements > StatementThreshold) {
193 diag(Func->getLocation(),
"%0 statements (threshold %1)",
195 << FI.Statements << StatementThreshold;
198 if (FI.Branches > BranchThreshold) {
199 diag(Func->getLocation(),
"%0 branches (threshold %1)", DiagnosticIDs::Note)
200 << FI.Branches << BranchThreshold;
203 if (ActualNumberParameters > ParameterThreshold) {
204 diag(Func->getLocation(),
"%0 parameters (threshold %1)",
206 << ActualNumberParameters << ParameterThreshold;
209 for (
const auto &CSPos : FI.NestingThresholders) {
210 diag(CSPos,
"nesting level %0 starts here (threshold %1)",
212 << NestingThreshold + 1 << NestingThreshold;
215 if (FI.Variables > VariableThreshold) {
216 diag(Func->getLocation(),
"%0 variables (threshold %1)",
218 << FI.Variables << VariableThreshold;
const FunctionDecl * Decl
unsigned NestingThreshold
std::vector< SourceLocation > NestingThresholders
llvm::BitVector TrackedParent
::clang::DynTypedNode Node
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
FunctionSizeCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
llvm::StringMap< ClangTidyValue > OptionMap