blob: d02972dde18a8765e096cd7ca8c74e4cd07fb4cc [file] [log] [blame]
Benjamin Kramer6e195422014-09-15 12:48:25 +00001//===--- 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
Alexander Kornienko1b677db2015-03-09 12:18:39 +000010#include "FunctionSizeCheck.h"
Samuel Benzaquen7663d3b2016-05-25 16:19:23 +000011#include "clang/AST/RecursiveASTVisitor.h"
Benjamin Kramer6e195422014-09-15 12:48:25 +000012#include "clang/ASTMatchers/ASTMatchFinder.h"
13
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace tidy {
Alexander Kornienko35ddae42014-10-15 10:51:57 +000018namespace readability {
Benjamin Kramer6e195422014-09-15 12:48:25 +000019
Samuel Benzaquen7663d3b2016-05-25 16:19:23 +000020class FunctionASTVisitor : public RecursiveASTVisitor<FunctionASTVisitor> {
21 using Base = RecursiveASTVisitor<FunctionASTVisitor>;
22
23public:
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 };
67 FunctionInfo Info;
68 std::vector<bool> TrackedParent;
69};
70
Benjamin Kramer6e195422014-09-15 12:48:25 +000071FunctionSizeCheck::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
77void FunctionSizeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
78 Options.store(Opts, "LineThreshold", LineThreshold);
79 Options.store(Opts, "StatementThreshold", StatementThreshold);
80 Options.store(Opts, "BranchThreshold", BranchThreshold);
81}
82
83void FunctionSizeCheck::registerMatchers(MatchFinder *Finder) {
Samuel Benzaquen7663d3b2016-05-25 16:19:23 +000084 Finder->addMatcher(functionDecl(unless(isInstantiated())).bind("func"), this);
Benjamin Kramer6e195422014-09-15 12:48:25 +000085}
86
87void FunctionSizeCheck::check(const MatchFinder::MatchResult &Result) {
88 const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
89
Samuel Benzaquen7663d3b2016-05-25 16:19:23 +000090 FunctionASTVisitor Visitor;
91 Visitor.TraverseDecl(const_cast<FunctionDecl *>(Func));
92 auto &FI = Visitor.Info;
93
94 if (FI.Statements == 0)
95 return;
Benjamin Kramer6e195422014-09-15 12:48:25 +000096
97 // Count the lines including whitespace and comments. Really simple.
Samuel Benzaquen7663d3b2016-05-25 16:19:23 +000098 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());
Benjamin Kramer6e195422014-09-15 12:48:25 +0000103 }
104 }
105
Samuel Benzaquen7663d3b2016-05-25 16:19:23 +0000106 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;
Benjamin Kramer6e195422014-09-15 12:48:25 +0000111 }
112
Samuel Benzaquen7663d3b2016-05-25 16:19:23 +0000113 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 }
Benjamin Kramer6e195422014-09-15 12:48:25 +0000130}
131
Alexander Kornienko35ddae42014-10-15 10:51:57 +0000132} // namespace readability
Benjamin Kramer6e195422014-09-15 12:48:25 +0000133} // namespace tidy
134} // namespace clang