11 #include "clang/AST/RecursiveASTVisitor.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 18 namespace readability {
21 class FunctionASTVisitor :
public RecursiveASTVisitor<FunctionASTVisitor> {
22 using Base = RecursiveASTVisitor<FunctionASTVisitor>;
25 bool TraverseStmt(Stmt *Node) {
27 return Base::TraverseStmt(Node);
32 switch (Node->getStmtClass()) {
33 case Stmt::IfStmtClass:
34 case Stmt::WhileStmtClass:
35 case Stmt::DoStmtClass:
36 case Stmt::CXXForRangeStmtClass:
37 case Stmt::ForStmtClass:
38 case Stmt::SwitchStmtClass:
41 case Stmt::CompoundStmtClass:
49 Base::TraverseStmt(Node);
56 bool TraverseCompoundStmt(CompoundStmt *Node) {
61 Info.NestingThresholders.push_back(Node->getLocStart());
64 Base::TraverseCompoundStmt(Node);
70 bool TraverseDecl(Decl *Node) {
72 Base::TraverseDecl(Node);
93 LineThreshold(Options.get(
"LineThreshold", -1U)),
94 StatementThreshold(Options.get(
"StatementThreshold", 800U)),
95 BranchThreshold(Options.get(
"BranchThreshold", -1U)),
96 ParameterThreshold(Options.get(
"ParameterThreshold", -1U)),
101 Options.
store(Opts,
"StatementThreshold", StatementThreshold);
102 Options.
store(Opts,
"BranchThreshold", BranchThreshold);
103 Options.
store(Opts,
"ParameterThreshold", ParameterThreshold);
104 Options.
store(Opts,
"NestingThreshold", NestingThreshold);
108 Finder->addMatcher(functionDecl(unless(isInstantiated())).bind(
"func"),
this);
112 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>(
"func");
114 FunctionASTVisitor Visitor;
115 Visitor.Info.NestingThreshold = NestingThreshold;
116 Visitor.TraverseDecl(const_cast<FunctionDecl *>(Func));
117 auto &FI = Visitor.Info;
119 if (FI.Statements == 0)
123 if (
const Stmt *Body = Func->getBody()) {
124 SourceManager *SM = Result.SourceManager;
125 if (SM->isWrittenInSameFile(Body->getLocStart(), Body->getLocEnd())) {
126 FI.Lines = SM->getSpellingLineNumber(Body->getLocEnd()) -
127 SM->getSpellingLineNumber(Body->getLocStart());
131 unsigned ActualNumberParameters = Func->getNumParams();
133 if (FI.Lines > LineThreshold || FI.Statements > StatementThreshold ||
134 FI.Branches > BranchThreshold ||
135 ActualNumberParameters > ParameterThreshold ||
136 !FI.NestingThresholders.empty()) {
137 diag(Func->getLocation(),
138 "function %0 exceeds recommended size/complexity thresholds")
142 if (FI.Lines > LineThreshold) {
143 diag(Func->getLocation(),
144 "%0 lines including whitespace and comments (threshold %1)",
146 << FI.Lines << LineThreshold;
149 if (FI.Statements > StatementThreshold) {
150 diag(Func->getLocation(),
"%0 statements (threshold %1)",
152 << FI.Statements << StatementThreshold;
155 if (FI.Branches > BranchThreshold) {
156 diag(Func->getLocation(),
"%0 branches (threshold %1)", DiagnosticIDs::Note)
157 << FI.Branches << BranchThreshold;
160 if (ActualNumberParameters > ParameterThreshold) {
161 diag(Func->getLocation(),
"%0 parameters (threshold %1)",
163 << ActualNumberParameters << ParameterThreshold;
166 for (
const auto &CSPos : FI.NestingThresholders) {
167 diag(CSPos,
"nesting level %0 starts here (threshold %1)",
169 << NestingThreshold + 1 << NestingThreshold;
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.
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
std::vector< SourceLocation > NestingThresholders
std::map< std::string, std::string > OptionMap
unsigned CurrentNestingLevel
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.
unsigned NestingThreshold
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.