malcolm.parsons created this revision.
malcolm.parsons added reviewers: aaron.ballman, klimek, alexfh.
malcolm.parsons added a subscriber: cfe-commits.
Herald added a subscriber: nemanjai.

It is easy to configure clang-tidy with both aliased and original
checks enabled.  This causes the check to be appear under both names
in -list-checks and -dump-config, and the check is run twice.

This changeset tells the CheckFactory about aliases so that an alias
is only enabled when the original is not enabled.


https://reviews.llvm.org/D25659

Files:
  clang-tidy/ClangTidy.cpp
  clang-tidy/ClangTidyModule.cpp
  clang-tidy/ClangTidyModule.h
  clang-tidy/cert/CERTTidyModule.cpp
  clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
  clang-tidy/google/GoogleTidyModule.cpp

Index: clang-tidy/google/GoogleTidyModule.cpp
===================================================================
--- clang-tidy/google/GoogleTidyModule.cpp
+++ clang-tidy/google/GoogleTidyModule.cpp
@@ -10,10 +10,6 @@
 #include "../ClangTidy.h"
 #include "../ClangTidyModule.h"
 #include "../ClangTidyModuleRegistry.h"
-#include "../readability/BracesAroundStatementsCheck.h"
-#include "../readability/FunctionSizeCheck.h"
-#include "../readability/NamespaceCommentCheck.h"
-#include "../readability/RedundantSmartptrGetCheck.h"
 #include "AvoidCStyleCastsCheck.h"
 #include "DefaultArgumentsCheck.h"
 #include "ExplicitConstructorCheck.h"
@@ -61,19 +57,16 @@
         "google-readability-casting");
     CheckFactories.registerCheck<readability::TodoCommentCheck>(
         "google-readability-todo");
-    CheckFactories
-        .registerCheck<clang::tidy::readability::BracesAroundStatementsCheck>(
-            "google-readability-braces-around-statements");
+    CheckFactories.registerAlias("google-readability-braces-around-statements",
+                                 "readability-braces-around-statements");
     CheckFactories.registerCheck<readability::GlobalNamesInHeadersCheck>(
         "google-global-names-in-headers");
-    CheckFactories.registerCheck<clang::tidy::readability::FunctionSizeCheck>(
-        "google-readability-function-size");
-    CheckFactories
-        .registerCheck<clang::tidy::readability::NamespaceCommentCheck>(
-            "google-readability-namespace-comments");
-    CheckFactories
-        .registerCheck<clang::tidy::readability::RedundantSmartptrGetCheck>(
-            "google-readability-redundant-smartptr-get");
+    CheckFactories.registerAlias("google-readability-function-size",
+                                 "readability-function-size");
+    CheckFactories.registerAlias("google-readability-namespace-comments",
+                                 "llvm-namespace-comment");
+    CheckFactories.registerAlias("google-readability-redundant-smartptr-get",
+                                 "readability-redundant-smartptr-get");
   }
 
   ClangTidyOptions getModuleOptions() override {
Index: clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
===================================================================
--- clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
+++ clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp
@@ -10,7 +10,6 @@
 #include "../ClangTidy.h"
 #include "../ClangTidyModule.h"
 #include "../ClangTidyModuleRegistry.h"
-#include "../misc/UnconventionalAssignOperatorCheck.h"
 #include "InterfacesGlobalInitCheck.h"
 #include "ProBoundsArrayToPointerDecayCheck.h"
 #include "ProBoundsConstantArrayIndexCheck.h"
@@ -33,6 +32,9 @@
 class CppCoreGuidelinesModule : public ClangTidyModule {
 public:
   void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
+    CheckFactories.registerAlias(
+        "cppcoreguidelines-c-copy-assignment-signature",
+        "misc-unconventional-assign-operator");
     CheckFactories.registerCheck<InterfacesGlobalInitCheck>(
         "cppcoreguidelines-interfaces-global-init");
     CheckFactories.registerCheck<ProBoundsArrayToPointerDecayCheck>(
@@ -59,8 +61,6 @@
         "cppcoreguidelines-special-member-functions");
     CheckFactories.registerCheck<SlicingCheck>(
         "cppcoreguidelines-slicing");
-    CheckFactories.registerCheck<misc::UnconventionalAssignOperatorCheck>(
-        "cppcoreguidelines-c-copy-assignment-signature");
   }
 };
 
Index: clang-tidy/cert/CERTTidyModule.cpp
===================================================================
--- clang-tidy/cert/CERTTidyModule.cpp
+++ clang-tidy/cert/CERTTidyModule.cpp
@@ -10,12 +10,6 @@
 #include "../ClangTidy.h"
 #include "../ClangTidyModule.h"
 #include "../ClangTidyModuleRegistry.h"
-#include "../google/UnnamedNamespaceInHeaderCheck.h"
-#include "../misc/MoveConstructorInitCheck.h"
-#include "../misc/NewDeleteOverloadsCheck.h"
-#include "../misc/NonCopyableObjects.h"
-#include "../misc/StaticAssertCheck.h"
-#include "../misc/ThrowByValueCatchByReferenceCheck.h"
 #include "CommandProcessorCheck.h"
 #include "FloatLoopCounter.h"
 #include "SetLongJmpCheck.h"
@@ -35,36 +29,31 @@
     // DCL
     CheckFactories.registerCheck<VariadicFunctionDefCheck>(
         "cert-dcl50-cpp");
-    CheckFactories.registerCheck<misc::NewDeleteOverloadsCheck>(
-        "cert-dcl54-cpp");
-    CheckFactories.registerCheck<google::build::UnnamedNamespaceInHeaderCheck>(
-        "cert-dcl59-cpp");
+    CheckFactories.registerAlias("cert-dcl54-cpp", "misc-new-delete-overloads");
+    CheckFactories.registerAlias("cert-dcl59-cpp", "google-build-namespaces");
     // OOP
-    CheckFactories.registerCheck<misc::MoveConstructorInitCheck>(
-        "cert-oop11-cpp");
+    CheckFactories.registerAlias("cert-oop11-cpp", "misc-move-constructor-init");
     // ERR
     CheckFactories.registerCheck<SetLongJmpCheck>(
         "cert-err52-cpp");
     CheckFactories.registerCheck<StaticObjectExceptionCheck>(
         "cert-err58-cpp");
     CheckFactories.registerCheck<ThrownExceptionTypeCheck>(
         "cert-err60-cpp");
-    CheckFactories.registerCheck<misc::ThrowByValueCatchByReferenceCheck>(
-        "cert-err61-cpp");
+    CheckFactories.registerAlias("cert-err61-cpp",
+                                 "misc-throw-by-value-catch-by-reference");
 
     // C checkers
     // DCL
-    CheckFactories.registerCheck<misc::StaticAssertCheck>(
-        "cert-dcl03-c");
+    CheckFactories.registerAlias("cert-dcl03-c", "misc-static-assert");
     // ENV
     CheckFactories.registerCheck<CommandProcessorCheck>(
         "cert-env33-c");
     // FLP
     CheckFactories.registerCheck<FloatLoopCounter>(
         "cert-flp30-c");
     // FIO
-    CheckFactories.registerCheck<misc::NonCopyableObjectsCheck>(
-        "cert-fio38-c");
+    CheckFactories.registerAlias("cert-fio38-c", "misc-non-copyable-objects");
     // ERR
     CheckFactories.registerCheck<StrToNumCheck>(
         "cert-err34-c");
Index: clang-tidy/ClangTidyModule.h
===================================================================
--- clang-tidy/ClangTidyModule.h
+++ clang-tidy/ClangTidyModule.h
@@ -62,6 +62,11 @@
     });
   }
 
+  /// Register an alias for another check
+  void registerAlias(StringRef AliasName, StringRef CheckName) {
+    Aliases[AliasName] = CheckName;
+  }
+
   /// \brief Create instances of all checks matching \p CheckRegexString and
   /// store them in \p Checks.
   ///
@@ -74,8 +79,15 @@
   FactoryMap::const_iterator end() const { return Factories.end(); }
   bool empty() const { return Factories.empty(); }
 
+  typedef std::map<std::string, std::string> AliasMap;
+  const AliasMap& aliases() const { return Aliases; }
+  AliasMap::const_iterator aliases_begin() const { return Aliases.begin(); }
+  AliasMap::const_iterator aliases_end() const { return Aliases.end(); }
+  bool aliases_empty() const { return Aliases.empty(); }
+
 private:
   FactoryMap Factories;
+  AliasMap Aliases;
 };
 
 /// \brief A clang-tidy module groups a number of \c ClangTidyChecks and gives
Index: clang-tidy/ClangTidyModule.cpp
===================================================================
--- clang-tidy/ClangTidyModule.cpp
+++ clang-tidy/ClangTidyModule.cpp
@@ -29,6 +29,12 @@
     if (Filter.contains(Factory.first))
       Checks.emplace_back(Factory.second(Factory.first, Context));
   }
+
+  for (const auto &Alias : Aliases) {
+    assert(Factories.count(Alias.second) && "Aliased check does not exist");
+    if (Filter.contains(Alias.first) && !Filter.contains(Alias.second))
+      Checks.emplace_back(Factories[Alias.second](Alias.first, Context));
+  }
 }
 
 ClangTidyOptions ClangTidyModule::getModuleOptions() {
Index: clang-tidy/ClangTidy.cpp
===================================================================
--- clang-tidy/ClangTidy.cpp
+++ clang-tidy/ClangTidy.cpp
@@ -308,6 +308,11 @@
       CheckNames.push_back(CheckFactory.first);
   }
 
+  for (const auto &Alias : CheckFactories->aliases()) {
+    if (Filter.contains(Alias.first) && !Filter.contains(Alias.second))
+      CheckNames.push_back(Alias.first);
+  }
+
   for (const auto &AnalyzerCheck : getCheckersControlList(Filter))
     CheckNames.push_back(AnalyzerCheckNamePrefix + AnalyzerCheck.first);
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to