diff options
| author | Alexander Kornienko <alexfh@google.com> | 2014-10-15 10:51:57 +0000 |
|---|---|---|
| committer | Alexander Kornienko <alexfh@google.com> | 2014-10-15 10:51:57 +0000 |
| commit | 35ddae4a9b22da8fd43511a2e5f0836230776b50 (patch) | |
| tree | 0ee137958546266fa7d4b35a73cdbd399174ef95 /clang-tools-extra/clang-tidy/readability/FunctionSize.cpp | |
| parent | 14c5ec51945be51c55c58cc4d2a1e4f1cfb56533 (diff) | |
| download | bcm5719-llvm-35ddae4a9b22da8fd43511a2e5f0836230776b50.tar.gz bcm5719-llvm-35ddae4a9b22da8fd43511a2e5f0836230776b50.zip | |
[clang-tidy] Move some of the misc checks to readability/
Summary:
Some of the misc checks belong to readability/. I'm moving them there
without changing check names for now. As the next step, I want to register some
of these checks in the google and llvm modules with suitable settings (e.g.
BracesAroundStatementsCheck). I'm not sure if we want to create a "readability"
module, probably not.
Reviewers: djasper
Reviewed By: djasper
Subscribers: curdeius, cfe-commits
Differential Revision: http://reviews.llvm.org/D5792
llvm-svn: 219786
Diffstat (limited to 'clang-tools-extra/clang-tidy/readability/FunctionSize.cpp')
| -rw-r--r-- | clang-tools-extra/clang-tidy/readability/FunctionSize.cpp | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/FunctionSize.cpp b/clang-tools-extra/clang-tidy/readability/FunctionSize.cpp new file mode 100644 index 00000000000..0c980e6a996 --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/FunctionSize.cpp @@ -0,0 +1,106 @@ +//===--- FunctionSize.cpp - clang-tidy ------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "FunctionSize.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace readability { + +FunctionSizeCheck::FunctionSizeCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + LineThreshold(Options.get("LineThreshold", -1U)), + StatementThreshold(Options.get("StatementThreshold", 800U)), + BranchThreshold(Options.get("BranchThreshold", -1U)) {} + +void FunctionSizeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "LineThreshold", LineThreshold); + Options.store(Opts, "StatementThreshold", StatementThreshold); + Options.store(Opts, "BranchThreshold", BranchThreshold); +} + +void FunctionSizeCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( + functionDecl( + unless(isInstantiated()), + forEachDescendant( + stmt(unless(compoundStmt()), + hasParent(stmt(anyOf(compoundStmt(), ifStmt(), + anyOf(whileStmt(), doStmt(), + forRangeStmt(), forStmt()))))) + .bind("stmt"))).bind("func"), + this); +} + +void FunctionSizeCheck::check(const MatchFinder::MatchResult &Result) { + const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func"); + + FunctionInfo &FI = FunctionInfos[Func]; + + // Count the lines including whitespace and comments. Really simple. + if (!FI.Lines) { + if (const Stmt *Body = Func->getBody()) { + SourceManager *SM = Result.SourceManager; + if (SM->isWrittenInSameFile(Body->getLocStart(), Body->getLocEnd())) { + FI.Lines = SM->getSpellingLineNumber(Body->getLocEnd()) - + SM->getSpellingLineNumber(Body->getLocStart()); + } + } + } + + const auto *Statement = Result.Nodes.getNodeAs<Stmt>("stmt"); + ++FI.Statements; + + // TODO: switch cases, gotos + if (isa<IfStmt>(Statement) || isa<WhileStmt>(Statement) || + isa<ForStmt>(Statement) || isa<SwitchStmt>(Statement) || + isa<DoStmt>(Statement) || isa<CXXForRangeStmt>(Statement)) + ++FI.Branches; +} + +void FunctionSizeCheck::onEndOfTranslationUnit() { + // If we're above the limit emit a warning. + for (const auto &P : FunctionInfos) { + const FunctionInfo &FI = P.second; + if (FI.Lines > LineThreshold || FI.Statements > StatementThreshold || + FI.Branches > BranchThreshold) { + diag(P.first->getLocation(), + "function '%0' exceeds recommended size/complexity thresholds") + << P.first->getNameAsString(); + } + + if (FI.Lines > LineThreshold) { + diag(P.first->getLocation(), + "%0 lines including whitespace and comments (threshold %1)", + DiagnosticIDs::Note) + << FI.Lines << LineThreshold; + } + + if (FI.Statements > StatementThreshold) { + diag(P.first->getLocation(), "%0 statements (threshold %1)", + DiagnosticIDs::Note) + << FI.Statements << StatementThreshold; + } + + if (FI.Branches > BranchThreshold) { + diag(P.first->getLocation(), "%0 branches (threshold %1)", + DiagnosticIDs::Note) + << FI.Branches << BranchThreshold; + } + } + + FunctionInfos.clear(); +} + +} // namespace readability +} // namespace tidy +} // namespace clang |

