- Added configurability via ClangTidyOptions.
- Turned the unit test into a lit test.
http://reviews.llvm.org/D4986
Files:
clang-tidy/misc/CMakeLists.txt
clang-tidy/misc/FunctionSize.cpp
clang-tidy/misc/FunctionSize.h
clang-tidy/misc/MiscTidyModule.cpp
clang-tidy/tool/ClangTidyMain.cpp
test/clang-tidy/misc-function-size.cpp
unittests/clang-tidy/CMakeLists.txt
Index: clang-tidy/misc/CMakeLists.txt
===================================================================
--- clang-tidy/misc/CMakeLists.txt
+++ clang-tidy/misc/CMakeLists.txt
@@ -3,6 +3,7 @@
add_clang_library(clangTidyMiscModule
ArgumentCommentCheck.cpp
BoolPointerImplicitConversion.cpp
+ FunctionSize.cpp
MiscTidyModule.cpp
RedundantSmartptrGet.cpp
SwappedArgumentsCheck.cpp
Index: clang-tidy/misc/FunctionSize.cpp
===================================================================
--- /dev/null
+++ 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", 800)),
+ 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
Index: clang-tidy/misc/FunctionSize.h
===================================================================
--- /dev/null
+++ clang-tidy/misc/FunctionSize.h
@@ -0,0 +1,45 @@
+//===--- 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 {
+ 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;
+
+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;
+};
+
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_FUNCTIONSIZE_H
Index: clang-tidy/misc/MiscTidyModule.cpp
===================================================================
--- clang-tidy/misc/MiscTidyModule.cpp
+++ 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 @@
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>(
Index: clang-tidy/tool/ClangTidyMain.cpp
===================================================================
--- clang-tidy/tool/ClangTidyMain.cpp
+++ clang-tidy/tool/ClangTidyMain.cpp
@@ -103,6 +103,12 @@
"code with clang-apply-replacements."),
cl::value_desc("filename"), cl::cat(ClangTidyCategory));
+static cl::opt<std::string>
+ ConfigFile("config",
+ cl::desc("File to override the configuration. Specifing a\nfile "
+ "will disable the default configuration file\nsearch."),
+ cl::value_desc("filename"), cl::cat(ClangTidyCategory));
+
namespace clang {
namespace tidy {
@@ -160,7 +166,23 @@
GlobalOptions, FallbackOptions, OverrideOptions);
std::string FileName = OptionsParser.getSourcePathList().front();
- ClangTidyOptions EffectiveOptions = OptionsProvider->getOptions(FileName);
+ ClangTidyOptions EffectiveOptions;
+ if (!ConfigFile.empty()) {
+ llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
+ llvm::MemoryBuffer::getFile(ConfigFile.c_str());
+ if (std::error_code EC = Text.getError()) {
+ llvm::errs() << "Error opening config file: " << EC.message() << '\n';
+ return 1;
+ }
+ if (std::error_code EC =
+ parseConfiguration((*Text)->getBuffer(), EffectiveOptions)) {
+ llvm::errs() << "Error reading config file: " << EC.message() << '\n';
+ return 1;
+ }
+ EffectiveOptions.mergeWith(OverrideOptions);
+ } else {
+ EffectiveOptions = OptionsProvider->getOptions(FileName);
+ }
std::vector<std::string> EnabledChecks = getCheckNames(EffectiveOptions);
// FIXME: Allow using --list-checks without positional arguments.
Index: test/clang-tidy/misc-function-size.cpp
===================================================================
--- /dev/null
+++ test/clang-tidy/misc-function-size.cpp
@@ -0,0 +1,55 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: sed 's#// *[A-Z-][A-Z-]*:.*#//#' %s > %t/t.cpp
+// RUN: echo '{ Checks: "-*,misc-function-size", CheckOptions: [{key: misc-function-size.LineThreshold, value: 0}, {key: misc-function-size.StatementThreshold, value: 0}, {key: misc-function-size.BranchThreshold, value: 0}]}' > %t/.clang-tidy
+// RUN: clang-tidy %t/t.cpp -- -std=c++11 2>&1 | FileCheck %s -implicit-check-not='{{warning:|error:}}'
+
+void foo1() {
+}
+
+void foo2() {;}
+// CHECK: warning: function 'foo2' exceeds recommended size/complexity thresholds
+// CHECK: 1 statements (threshold 0)
+
+void foo3() {
+;
+
+}
+// CHECK: warning: function 'foo3' exceeds recommended size/complexity thresholds
+// CHECK: 3 lines including whitespace and comments (threshold 0)
+// CHECK: 1 statements (threshold 0)
+
+void foo4(int i) { if (i) {} else; {}
+}
+// CHECK: warning: function 'foo4' exceeds recommended size/complexity thresholds
+// CHECK: 1 lines including whitespace and comments (threshold 0)
+// CHECK: 3 statements (threshold 0)
+// CHECK: 1 branches (threshold 0)
+
+void foo5(int i) {for(;i;)while(i)
+do;while(i);
+}
+// CHECK: warning: function 'foo5' exceeds recommended size/complexity thresholds
+// CHECK: 2 lines including whitespace and comments (threshold 0)
+// CHECK: 7 statements (threshold 0)
+// CHECK: 3 branches (threshold 0)
+
+template <typename T> T foo6(T i) {return i;
+}
+int x = foo6(0);
+// CHECK: warning: function 'foo6' exceeds recommended size/complexity thresholds
+// CHECK: 1 lines including whitespace and comments (threshold 0)
+// CHECK: 1 statements (threshold 0)
+
+void bar1() { [](){;;;;;;;;;;;if(1){}}();
+
+
+}
+// CHECK: warning: function 'bar1' exceeds recommended size/complexity thresholds
+// CHECK: 3 lines including whitespace and comments (threshold 0)
+// CHECK: 14 statements (threshold 0)
+// CHECK: 1 branches (threshold 0)
+
+void bar2() { class A { void x() {} }; }
+// CHECK: warning: function 'bar2' exceeds recommended size/complexity thresholds
+// CHECK: 1 statements (threshold 0)
Index: unittests/clang-tidy/CMakeLists.txt
===================================================================
--- unittests/clang-tidy/CMakeLists.txt
+++ unittests/clang-tidy/CMakeLists.txt
@@ -9,8 +9,8 @@
add_extra_unittest(ClangTidyTests
ClangTidyDiagnosticConsumerTest.cpp
ClangTidyOptionsTest.cpp
- LLVMModuleTest.cpp
GoogleModuleTest.cpp
+ LLVMModuleTest.cpp
MiscModuleTest.cpp)
target_link_libraries(ClangTidyTests
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits