diff options
Diffstat (limited to 'clang-tools-extra/clang-tidy/misc')
| -rw-r--r-- | clang-tools-extra/clang-tidy/misc/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | clang-tools-extra/clang-tidy/misc/FunctionSize.cpp | 104 | ||||
| -rw-r--r-- | clang-tools-extra/clang-tidy/misc/FunctionSize.h | 46 | ||||
| -rw-r--r-- | clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp | 2 |
4 files changed, 153 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt index fbe5bb060b9..33bac3f2def 100644 --- a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt @@ -3,6 +3,7 @@ set(LLVM_LINK_COMPONENTS support) add_clang_library(clangTidyMiscModule ArgumentCommentCheck.cpp BoolPointerImplicitConversion.cpp + FunctionSize.cpp MiscTidyModule.cpp RedundantSmartptrGet.cpp SwappedArgumentsCheck.cpp diff --git a/clang-tools-extra/clang-tidy/misc/FunctionSize.cpp b/clang-tools-extra/clang-tidy/misc/FunctionSize.cpp new file mode 100644 index 00000000000..fe502fc94f8 --- /dev/null +++ b/clang-tools-extra/clang-tidy/misc/FunctionSize.cpp @@ -0,0 +1,104 @@ +//===--- 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 { + +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 tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/misc/FunctionSize.h b/clang-tools-extra/clang-tidy/misc/FunctionSize.h new file mode 100644 index 00000000000..967058e4a54 --- /dev/null +++ b/clang-tools-extra/clang-tidy/misc/FunctionSize.h @@ -0,0 +1,46 @@ +//===--- FunctionSize.h - clang-tidy ----------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_FUNCTIONSIZE_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_FUNCTIONSIZE_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { + +/// \brief Checks for large functions based on various metrics. +class FunctionSizeCheck : public ClangTidyCheck { +public: + FunctionSizeCheck(StringRef Name, ClangTidyContext *Context); + + void storeOptions(ClangTidyOptions::OptionMap &Opts) override; + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + void onEndOfTranslationUnit() override; + +private: + struct FunctionInfo { + FunctionInfo() : Lines(0), Statements(0), Branches(0) {} + unsigned Lines; + unsigned Statements; + unsigned Branches; + }; + + const unsigned LineThreshold; + const unsigned StatementThreshold; + const unsigned BranchThreshold; + + llvm::DenseMap<const FunctionDecl *, FunctionInfo> FunctionInfos; +}; + +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_FUNCTIONSIZE_H diff --git a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp index 489f5b31dd7..6cfc967fbed 100644 --- a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp @@ -12,6 +12,7 @@ #include "../ClangTidyModuleRegistry.h" #include "ArgumentCommentCheck.h" #include "BoolPointerImplicitConversion.h" +#include "FunctionSize.h" #include "RedundantSmartptrGet.h" #include "SwappedArgumentsCheck.h" #include "UndelegatedConstructor.h" @@ -27,6 +28,7 @@ public: CheckFactories.registerCheck<ArgumentCommentCheck>("misc-argument-comment"); CheckFactories.registerCheck<BoolPointerImplicitConversion>( "misc-bool-pointer-implicit-conversion"); + CheckFactories.registerCheck<FunctionSizeCheck>("misc-function-size"); CheckFactories.registerCheck<RedundantSmartptrGet>( "misc-redundant-smartptr-get"); CheckFactories.registerCheck<SwappedArgumentsCheck>( |

