clang-tools  3.9.0
FunctionSizeCheck.cpp
Go to the documentation of this file.
1 //===--- FunctionSize.cpp - clang-tidy ------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "FunctionSizeCheck.h"
11 #include "clang/AST/RecursiveASTVisitor.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace readability {
19 
20 class FunctionASTVisitor : public RecursiveASTVisitor<FunctionASTVisitor> {
21  using Base = RecursiveASTVisitor<FunctionASTVisitor>;
22 
23 public:
24  bool TraverseStmt(Stmt *Node) {
25  if (!Node)
26  return Base::TraverseStmt(Node);
27 
28  if (TrackedParent.back() && !isa<CompoundStmt>(Node))
29  ++Info.Statements;
30 
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:
38  ++Info.Branches;
39  // fallthrough
40  case Stmt::CompoundStmtClass:
41  TrackedParent.push_back(true);
42  break;
43  default:
44  TrackedParent.push_back(false);
45  break;
46  }
47 
48  Base::TraverseStmt(Node);
49 
50  TrackedParent.pop_back();
51  return true;
52  }
53 
54  bool TraverseDecl(Decl *Node) {
55  TrackedParent.push_back(false);
56  Base::TraverseDecl(Node);
57  TrackedParent.pop_back();
58  return true;
59  }
60 
61  struct FunctionInfo {
62  FunctionInfo() : Lines(0), Statements(0), Branches(0) {}
63  unsigned Lines;
64  unsigned Statements;
65  unsigned Branches;
66  };
68  std::vector<bool> TrackedParent;
69 };
70 
71 FunctionSizeCheck::FunctionSizeCheck(StringRef Name, ClangTidyContext *Context)
72  : ClangTidyCheck(Name, Context),
73  LineThreshold(Options.get("LineThreshold", -1U)),
74  StatementThreshold(Options.get("StatementThreshold", 800U)),
75  BranchThreshold(Options.get("BranchThreshold", -1U)) {}
76 
78  Options.store(Opts, "LineThreshold", LineThreshold);
79  Options.store(Opts, "StatementThreshold", StatementThreshold);
80  Options.store(Opts, "BranchThreshold", BranchThreshold);
81 }
82 
84  Finder->addMatcher(functionDecl(unless(isInstantiated())).bind("func"), this);
85 }
86 
87 void FunctionSizeCheck::check(const MatchFinder::MatchResult &Result) {
88  const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
89 
90  FunctionASTVisitor Visitor;
91  Visitor.TraverseDecl(const_cast<FunctionDecl *>(Func));
92  auto &FI = Visitor.Info;
93 
94  if (FI.Statements == 0)
95  return;
96 
97  // Count the lines including whitespace and comments. Really simple.
98  if (const Stmt *Body = Func->getBody()) {
99  SourceManager *SM = Result.SourceManager;
100  if (SM->isWrittenInSameFile(Body->getLocStart(), Body->getLocEnd())) {
101  FI.Lines = SM->getSpellingLineNumber(Body->getLocEnd()) -
102  SM->getSpellingLineNumber(Body->getLocStart());
103  }
104  }
105 
106  if (FI.Lines > LineThreshold || FI.Statements > StatementThreshold ||
107  FI.Branches > BranchThreshold) {
108  diag(Func->getLocation(),
109  "function %0 exceeds recommended size/complexity thresholds")
110  << Func;
111  }
112 
113  if (FI.Lines > LineThreshold) {
114  diag(Func->getLocation(),
115  "%0 lines including whitespace and comments (threshold %1)",
116  DiagnosticIDs::Note)
117  << FI.Lines << LineThreshold;
118  }
119 
120  if (FI.Statements > StatementThreshold) {
121  diag(Func->getLocation(), "%0 statements (threshold %1)",
122  DiagnosticIDs::Note)
123  << FI.Statements << StatementThreshold;
124  }
125 
126  if (FI.Branches > BranchThreshold) {
127  diag(Func->getLocation(), "%0 branches (threshold %1)", DiagnosticIDs::Note)
128  << FI.Branches << BranchThreshold;
129  }
130 }
131 
132 } // namespace readability
133 } // namespace tidy
134 } // namespace clang
const std::string Name
Definition: USRFinder.cpp:140
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:210
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.
Definition: ClangTidy.h:110
SourceManager & SM
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.
Definition: ClangTidy.cpp:385
std::map< std::string, std::string > OptionMap
ClangTidyContext & Context
Definition: ClangTidy.cpp:93
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 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.
Definition: ClangTidy.cpp:352
const NamedDecl * Result
Definition: USRFinder.cpp:137