diff options
author | Alexander Kornienko <alexfh@google.com> | 2014-10-16 11:27:57 +0000 |
---|---|---|
committer | Alexander Kornienko <alexfh@google.com> | 2014-10-16 11:27:57 +0000 |
commit | 1efc425551c0c7ac95cacad8f63dc31eb46c1f04 (patch) | |
tree | f1abd4eee68b1245551d67fcb968d46b64700efc /clang-tools-extra | |
parent | 0445380f4f6f40c56871147d207cb3bf85529967 (diff) | |
download | bcm5719-llvm-1efc425551c0c7ac95cacad8f63dc31eb46c1f04.tar.gz bcm5719-llvm-1efc425551c0c7ac95cacad8f63dc31eb46c1f04.zip |
[clang-tidy] Default options in modules.
Summary:
This patch allows modules to specify default options for the checks
defined in them. This way a sufficiently configurable check can be registered in
multiple modules with different default options. E.g. the SpacesBeforeComments
option may be set to 1 for the "llvm-namespace-comments" check and to 2 for the
"google-readability-namespace-comment" check without modifying or extending the
check code.
This patch also registers the google-readability-braces-around-statements check
with suitable defaults.
Reviewers: djasper
Reviewed By: djasper
Subscribers: curdeius, cfe-commits
Differential Revision: http://reviews.llvm.org/D5798
llvm-svn: 219923
Diffstat (limited to 'clang-tools-extra')
7 files changed, 47 insertions, 11 deletions
diff --git a/clang-tools-extra/clang-tidy/ClangTidyModule.cpp b/clang-tools-extra/clang-tidy/ClangTidyModule.cpp index f6e4daf1c30..e1e798ddccf 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidyModule.cpp @@ -31,5 +31,9 @@ void ClangTidyCheckFactories::createChecks( } } +ClangTidyOptions ClangTidyModule::getModuleOptions() { + return ClangTidyOptions(); +} + } // namespace tidy } // namespace clang diff --git a/clang-tools-extra/clang-tidy/ClangTidyModule.h b/clang-tools-extra/clang-tidy/ClangTidyModule.h index 6406d63651b..c1851b9ed58 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyModule.h +++ b/clang-tools-extra/clang-tidy/ClangTidyModule.h @@ -87,6 +87,9 @@ public: /// \brief Implement this function in order to register all \c CheckFactories /// belonging to this module. virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0; + + /// \brief Gets default options for checks defined in this module. + virtual ClangTidyOptions getModuleOptions(); }; } // end namespace tidy diff --git a/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp b/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp index a4bfe0488c1..5283a5cd2cc 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// #include "ClangTidyOptions.h" +#include "ClangTidyModuleRegistry.h" #include "clang/Basic/LLVM.h" #include "llvm/ADT/SmallString.h" #include "llvm/Support/Errc.h" @@ -96,6 +97,19 @@ template <> struct MappingTraits<ClangTidyOptions> { namespace clang { namespace tidy { +ClangTidyOptions ClangTidyOptions::getDefaults() { + ClangTidyOptions Options; + Options.Checks = ""; + Options.HeaderFilterRegex = ""; + Options.AnalyzeTemporaryDtors = false; + Options.User = llvm::None; + for (ClangTidyModuleRegistry::iterator I = ClangTidyModuleRegistry::begin(), + E = ClangTidyModuleRegistry::end(); + I != E; ++I) + Options = Options.mergeWith(I->instantiate()->getModuleOptions()); + return Options; +} + ClangTidyOptions ClangTidyOptions::mergeWith(const ClangTidyOptions &Other) const { ClangTidyOptions Result = *this; diff --git a/clang-tools-extra/clang-tidy/ClangTidyOptions.h b/clang-tools-extra/clang-tidy/ClangTidyOptions.h index 82a740cec22..c45baf3a0f0 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyOptions.h +++ b/clang-tools-extra/clang-tidy/ClangTidyOptions.h @@ -49,15 +49,10 @@ struct ClangTidyOptions { /// \brief These options are used for all settings that haven't been /// overridden by the \c OptionsProvider. /// - /// Allow no checks and no headers by default. - static ClangTidyOptions getDefaults() { - ClangTidyOptions Options; - Options.Checks = ""; - Options.HeaderFilterRegex = ""; - Options.AnalyzeTemporaryDtors = false; - Options.User = llvm::None; - return Options; - } + /// Allow no checks and no headers by default. This method initializes + /// check-specific options by calling \c ClangTidyModule::getModuleOptions() + /// of each registered \c ClangTidyModule. + static ClangTidyOptions getDefaults(); /// \brief Creates a new \c ClangTidyOptions instance combined from all fields /// of this instance overridden by the fields of \p Other that have a value. diff --git a/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp b/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp index b839fd0e55e..9ff5caec377 100644 --- a/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/google/GoogleTidyModule.cpp @@ -21,6 +21,7 @@ #include "TodoCommentCheck.h" #include "UnnamedNamespaceInHeaderCheck.h" #include "UsingNamespaceDirectiveCheck.h" +#include "../readability/BracesAroundStatementsCheck.h" #include "../readability/NamespaceCommentCheck.h" using namespace clang::ast_matchers; @@ -55,6 +56,18 @@ public: "google-readability-todo"); CheckFactories.registerCheck<readability::NamespaceCommentCheck>( "google-readability-namespace-comments"); + CheckFactories.registerCheck<readability::BracesAroundStatementsCheck>( + "google-readability-braces-around-statements"); + } + + ClangTidyOptions getModuleOptions() override { + ClangTidyOptions Options; + auto &Opts = Options.CheckOptions; + Opts["google-readability-braces-around-statements.ShortStatementLines"] = + "1"; + Opts["google-readability-namespace-comments.ShortNamespaceLines"] = "1"; + Opts["google-readability-namespace-comments.SpacesBeforeComments"] = "2"; + return Options; } }; diff --git a/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp b/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp index da29c59f5b8..3464a98b0c4 100644 --- a/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp @@ -26,8 +26,7 @@ NamespaceCommentCheck::NamespaceCommentCheck(StringRef Name, "namespace( +([a-zA-Z0-9_]+))? *(\\*/)?$", llvm::Regex::IgnoreCase), ShortNamespaceLines(Options.get("ShortNamespaceLines", 1u)), - SpacesBeforeComments(Options.get("SpacesBeforeComments", - Name.startswith("google") ? 2u : 1u)) {} + SpacesBeforeComments(Options.get("SpacesBeforeComments", 1u)) {} void NamespaceCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "ShortNamespaceLines", ShortNamespaceLines); diff --git a/clang-tools-extra/test/clang-tidy/google-module.cpp b/clang-tools-extra/test/clang-tidy/google-module.cpp new file mode 100644 index 00000000000..b7be164a95e --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/google-module.cpp @@ -0,0 +1,8 @@ +// RUN: clang-tidy -checks='-*,google*' -config='{}' -dump-config - -- | FileCheck %s +// CHECK: CheckOptions: +// CHECK: {{- key: *google-readability-braces-around-statements.ShortStatementLines}} +// CHECK-NEXT: {{value: *'1'}} +// CHECK: {{- key: *google-readability-namespace-comments.ShortNamespaceLines}} +// CHECK-NEXT: {{value: *'1'}} +// CHECK: {{- key: *google-readability-namespace-comments.SpacesBeforeComments}} +// CHECK-NEXT: {{value: *'2'}} |