11 #include "clang/AST/RecursiveASTVisitor.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 using namespace clang::ast_matchers;
18 namespace readability {
21 using Base = RecursiveASTVisitor<FunctionASTVisitor>;
26 return Base::TraverseStmt(Node);
28 if (TrackedParent.back() && !isa<CompoundStmt>(Node))
31 switch (Node->getStmtClass()) {
32 case Stmt::IfStmtClass:
33 case Stmt::WhileStmtClass:
34 case Stmt::DoStmtClass:
35 case Stmt::CXXForRangeStmtClass:
36 case Stmt::ForStmtClass:
37 case Stmt::SwitchStmtClass:
40 case Stmt::CompoundStmtClass:
41 TrackedParent.push_back(
true);
44 TrackedParent.push_back(
false);
48 Base::TraverseStmt(Node);
50 TrackedParent.pop_back();
59 if (CurrentNestingLevel == Info.NestingThreshold)
60 Info.NestingThresholders.push_back(Node->getLocStart());
62 ++CurrentNestingLevel;
63 Base::TraverseCompoundStmt(Node);
64 --CurrentNestingLevel;
70 TrackedParent.push_back(
false);
71 Base::TraverseDecl(Node);
72 TrackedParent.pop_back();
78 unsigned Statements = 0;
79 unsigned Branches = 0;
80 unsigned NestingThreshold = 0;
85 unsigned CurrentNestingLevel = 0;
90 LineThreshold(Options.get(
"LineThreshold", -1U)),
91 StatementThreshold(Options.get(
"StatementThreshold", 800U)),
92 BranchThreshold(Options.get(
"BranchThreshold", -1U)),
93 ParameterThreshold(Options.get(
"ParameterThreshold", -1U)),
94 NestingThreshold(Options.get(
"NestingThreshold", -1U)) {}
98 Options.
store(Opts,
"StatementThreshold", StatementThreshold);
100 Options.
store(Opts,
"ParameterThreshold", ParameterThreshold);
101 Options.
store(Opts,
"NestingThreshold", NestingThreshold);
105 Finder->addMatcher(functionDecl(unless(isInstantiated())).bind(
"func"),
this);
109 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>(
"func");
113 Visitor.TraverseDecl(const_cast<FunctionDecl *>(Func));
114 auto &FI = Visitor.Info;
116 if (FI.Statements == 0)
120 if (
const Stmt *Body = Func->getBody()) {
121 SourceManager *
SM = Result.SourceManager;
122 if (SM->isWrittenInSameFile(Body->getLocStart(), Body->getLocEnd())) {
123 FI.Lines = SM->getSpellingLineNumber(Body->getLocEnd()) -
124 SM->getSpellingLineNumber(Body->getLocStart());
128 unsigned ActualNumberParameters = Func->getNumParams();
130 if (FI.Lines > LineThreshold || FI.Statements > StatementThreshold ||
131 FI.Branches > BranchThreshold ||
132 ActualNumberParameters > ParameterThreshold ||
133 !FI.NestingThresholders.empty()) {
134 diag(Func->getLocation(),
135 "function %0 exceeds recommended size/complexity thresholds")
139 if (FI.Lines > LineThreshold) {
140 diag(Func->getLocation(),
141 "%0 lines including whitespace and comments (threshold %1)",
143 << FI.Lines << LineThreshold;
146 if (FI.Statements > StatementThreshold) {
147 diag(Func->getLocation(),
"%0 statements (threshold %1)",
149 << FI.Statements << StatementThreshold;
152 if (FI.Branches > BranchThreshold) {
153 diag(Func->getLocation(),
"%0 branches (threshold %1)", DiagnosticIDs::Note)
154 << FI.Branches << BranchThreshold;
157 if (ActualNumberParameters > ParameterThreshold) {
158 diag(Func->getLocation(),
"%0 parameters (threshold %1)",
160 << ActualNumberParameters << ParameterThreshold;
163 for (
const auto &CSPos : FI.NestingThresholders) {
164 diag(CSPos,
"nesting level %0 starts here (threshold %1)",
166 << NestingThreshold + 1 << NestingThreshold;
unsigned NestingThreshold
std::unique_ptr< ast_matchers::MatchFinder > Finder
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Base class for all clang-tidy checks.
std::vector< bool > TrackedParent
bool TraverseStmt(Stmt *Node)
std::vector< SourceLocation > NestingThresholders
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.
bool TraverseDecl(Decl *Node)
std::map< std::string, std::string > OptionMap
ClangTidyContext & Context
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.
bool TraverseCompoundStmt(CompoundStmt *Node)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.