[clang-tools-extra] r219923 - [clang-tidy] Default options in modules.

Alexander Kornienko alexfh at google.com
Thu Oct 16 04:27:57 PDT 2014


Author: alexfh
Date: Thu Oct 16 06:27:57 2014
New Revision: 219923

URL: http://llvm.org/viewvc/llvm-project?rev=219923&view=rev
Log:
[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

Added:
    clang-tools-extra/trunk/test/clang-tidy/google-module.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/ClangTidyModule.cpp
    clang-tools-extra/trunk/clang-tidy/ClangTidyModule.h
    clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.cpp
    clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h
    clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp
    clang-tools-extra/trunk/clang-tidy/readability/NamespaceCommentCheck.cpp

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidyModule.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidyModule.cpp?rev=219923&r1=219922&r2=219923&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyModule.cpp Thu Oct 16 06:27:57 2014
@@ -31,5 +31,9 @@ void ClangTidyCheckFactories::createChec
   }
 }
 
+ClangTidyOptions ClangTidyModule::getModuleOptions() {
+  return ClangTidyOptions();
+}
+
 } // namespace tidy
 } // namespace clang

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidyModule.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidyModule.h?rev=219923&r1=219922&r2=219923&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyModule.h (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyModule.h Thu Oct 16 06:27:57 2014
@@ -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

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.cpp?rev=219923&r1=219922&r2=219923&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.cpp Thu Oct 16 06:27:57 2014
@@ -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<ClangTi
 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;

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h?rev=219923&r1=219922&r2=219923&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidyOptions.h Thu Oct 16 06:27:57 2014
@@ -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.

Modified: clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp?rev=219923&r1=219922&r2=219923&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/google/GoogleTidyModule.cpp Thu Oct 16 06:27:57 2014
@@ -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;
   }
 };
 

Modified: clang-tools-extra/trunk/clang-tidy/readability/NamespaceCommentCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/NamespaceCommentCheck.cpp?rev=219923&r1=219922&r2=219923&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/NamespaceCommentCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/NamespaceCommentCheck.cpp Thu Oct 16 06:27:57 2014
@@ -26,8 +26,7 @@ NamespaceCommentCheck::NamespaceCommentC
                               "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);

Added: clang-tools-extra/trunk/test/clang-tidy/google-module.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/google-module.cpp?rev=219923&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/google-module.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/google-module.cpp Thu Oct 16 06:27:57 2014
@@ -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'}}





More information about the cfe-commits mailing list