PiotrZSL created this revision.
Herald added subscribers: carlosgalvezp, manas, ASDenysPetrov, dkrupp, 
donat.nagy, Szelethus, a.sidorin, baloghadamsoftware, xazax.hun.
Herald added a reviewer: njames93.
Herald added a project: All.
PiotrZSL requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Before this change all checks from 'core' group were
enabled when any analyzer check were enabled.
Now only checks dependences are enabled.
Unfortunately even if this gives more flexibility to
user, Clang Static Analyzer got some hidden checks
that are somehow enabled by default there, but not in
Clang-Tidy.

Issues:
 https://github.com/llvm/llvm-project/issues/61520
 https://github.com/llvm/llvm-project/issues/59588
 https://github.com/llvm/llvm-project/issues/59589


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146396

Files:
  clang-tools-extra/clang-tidy/ClangTidy.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/test/clang-tidy/infrastructure/enable-analyzer-checks.cpp

Index: clang-tools-extra/test/clang-tidy/infrastructure/enable-analyzer-checks.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/infrastructure/enable-analyzer-checks.cpp
@@ -0,0 +1,10 @@
+// REQUIRES: static-analyzer
+
+// Check if enabling once check will enable only dependences
+// RUN: clang-tidy -checks=-*,clang-analyzer-core.CallAndMessage -list-checks | grep 'clang-analyzer-core.CallAndMessageModeling'
+// RUN: clang-tidy -checks=-*,clang-analyzer-core.CallAndMessage -list-checks | wc -l | grep -x '4'
+
+// Check if disabling dependency does nothing if check is enabled
+// RUN: clang-tidy -checks=-*,clang-analyzer-core.CallAndMessage,-clang-analyzer-core.clang-analyzer-core.CallAndMessageModeling \
+// RUN: -list-checks | grep 'clang-analyzer-core.CallAndMessageModeling'
+
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -98,6 +98,9 @@
   `ImplementationFileExtensions`, replacing the check-local options of the
   same name.
 
+- Clang Static Analyzer checks can now be individually enabled or disabled.
+  Use `--checks="clang-analyzer-core.*"` to preserve the previous behavior.
+
 New checks
 ^^^^^^^^^^
 
Index: clang-tools-extra/clang-tidy/ClangTidy.cpp
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidy.cpp
+++ clang-tools-extra/clang-tidy/ClangTidy.cpp
@@ -349,37 +349,52 @@
   }
 }
 
-typedef std::vector<std::pair<std::string, bool>> CheckersList;
+using CheckersList = std::vector<std::pair<std::string, bool>>;
+using CheckersSet = std::set<std::pair<std::string, bool>>;
+
+static void
+addAnalyzerCheckAndDependencies(CheckersSet &Checks,
+                                llvm::StringRef AnalyzerCheckFullName) {
+  if (!Checks.emplace(AnalyzerCheckFullName.str(), true).second)
+    return;
+
+#define GET_CHECKER_WEAK_DEPENDENCIES
+#define GET_CHECKER_DEPENDENCIES
+#define CHECKER_DEPENDENCY(Parent, Child)                                      \
+  if (Parent == AnalyzerCheckFullName)                                         \
+    addAnalyzerCheckAndDependencies(Checks, Child);
+#define CHECKER_WEAK_DEPENDENCY(Parent, Child)                                 \
+  if (Parent == AnalyzerCheckFullName)                                         \
+    addAnalyzerCheckAndDependencies(Checks, Child);
+#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
+#undef CHECKER_WEAK_DEPENDENCY
+#undef CHECKER_DEPENDENCY
+#undef GET_CHECKER_DEPENDENCIES
+#undef GET_CHECKER_WEAK_DEPENDENCIES
+}
 
 static CheckersList getAnalyzerCheckersAndPackages(ClangTidyContext &Context,
                                                    bool IncludeExperimental) {
-  CheckersList List;
-
-  const auto &RegisteredCheckers =
-      AnalyzerOptions::getRegisteredCheckers(IncludeExperimental);
-  bool AnalyzerChecksEnabled = false;
-  for (StringRef CheckName : RegisteredCheckers) {
-    std::string ClangTidyCheckName((AnalyzerCheckNamePrefix + CheckName).str());
-    AnalyzerChecksEnabled |= Context.isCheckEnabled(ClangTidyCheckName);
+  CheckersSet Checks;
+
+#define GET_CHECKERS
+#define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN)                 \
+  {                                                                            \
+    llvm::StringRef AnalyzerCheckFullName(FULLNAME);                           \
+    if (!AnalyzerCheckFullName.startswith("debug.") &&                         \
+        (IncludeExperimental ||                                                \
+         !AnalyzerCheckFullName.startswith("alpha."))) {                       \
+      std::string ClangTidyCheckName(                                          \
+          (AnalyzerCheckNamePrefix + AnalyzerCheckFullName).str());            \
+      if (Context.isCheckEnabled(ClangTidyCheckName))                          \
+        addAnalyzerCheckAndDependencies(Checks, AnalyzerCheckFullName);        \
+    }                                                                          \
   }
+#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
+#undef CHECKER
+#undef GET_CHECKERS
 
-  if (!AnalyzerChecksEnabled)
-    return List;
-
-  // List all static analyzer checkers that our filter enables.
-  //
-  // Always add all core checkers if any other static analyzer check is enabled.
-  // This is currently necessary, as other path sensitive checks rely on the
-  // core checkers.
-  for (StringRef CheckName : RegisteredCheckers) {
-    std::string ClangTidyCheckName((AnalyzerCheckNamePrefix + CheckName).str());
-
-    if (CheckName.startswith("core") ||
-        Context.isCheckEnabled(ClangTidyCheckName)) {
-      List.emplace_back(std::string(CheckName), true);
-    }
-  }
-  return List;
+  return {Checks.begin(), Checks.end()};
 }
 #endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to