Add an explicit toggle for the static analyzer in clang-tidy
authorNico Weber <thakis@chromium.org>
Thu, 3 Sep 2020 23:37:29 +0000 (19:37 -0400)
committerNico Weber <thakis@chromium.org>
Thu, 10 Sep 2020 14:48:17 +0000 (10:48 -0400)
Instead of using CLANG_ENABLE_STATIC_ANALYZER for use of the
static analyzer in both clang and clang-tidy, add a second
toggle CLANG_TIDY_ENABLE_STATIC_ANALYZER.

This allows enabling the static analyzer in clang-tidy while
disabling it in clang.

Differential Revison: https://reviews.llvm.org/D87118

16 files changed:
clang-tools-extra/CMakeLists.txt
clang-tools-extra/clang-tidy/CMakeLists.txt
clang-tools-extra/clang-tidy/ClangTidy.cpp
clang-tools-extra/clang-tidy/ClangTidyForceLinker.h
clang-tools-extra/clang-tidy/clang-tidy-config.h.cmake [new file with mode: 0644]
clang-tools-extra/docs/clang-tidy/Contributing.rst
clang-tools-extra/test/CMakeLists.txt
clang-tools-extra/test/lit.cfg.py
clang-tools-extra/test/lit.site.cfg.py.in
clang/CMakeLists.txt
clang/cmake/caches/Android.cmake
clang/lib/CMakeLists.txt
llvm/utils/gn/secondary/clang-tools-extra/clang-tidy/BUILD.gn
llvm/utils/gn/secondary/clang-tools-extra/clang-tidy/enable.gni [new file with mode: 0644]
llvm/utils/gn/secondary/clang-tools-extra/clang-tidy/tool/BUILD.gn
llvm/utils/gn/secondary/clang-tools-extra/test/BUILD.gn

index 57bb970..2e73b6b 100644 (file)
@@ -1,5 +1,8 @@
 include(CMakeDependentOption)
 
+option(CLANG_TIDY_ENABLE_STATIC_ANALYZER
+  "Include static analyzer checks in clang-tidy" ON)
+
 add_subdirectory(clang-apply-replacements)
 add_subdirectory(clang-reorder-fields)
 add_subdirectory(modularize)
index 9239761..ca7a5af 100644 (file)
@@ -3,6 +3,11 @@ set(LLVM_LINK_COMPONENTS
   Support
   )
 
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/clang-tidy-config.h.cmake
+  ${CMAKE_CURRENT_BINARY_DIR}/clang-tidy-config.h)
+include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
+
 add_clang_library(clangTidy
   ClangTidy.cpp
   ClangTidyCheck.cpp
@@ -34,7 +39,7 @@ clang_target_link_libraries(clangTidy
   clangToolingCore
   )
 
-if(CLANG_ENABLE_STATIC_ANALYZER)
+if(CLANG_TIDY_ENABLE_STATIC_ANALYZER)
   clang_target_link_libraries(clangTidy
     PRIVATE
     clangStaticAnalyzerCore
@@ -60,7 +65,7 @@ add_subdirectory(llvm)
 add_subdirectory(llvmlibc)
 add_subdirectory(misc)
 add_subdirectory(modernize)
-if(CLANG_ENABLE_STATIC_ANALYZER)
+if(CLANG_TIDY_ENABLE_STATIC_ANALYZER)
   add_subdirectory(mpi)
 endif()
 add_subdirectory(objc)
@@ -93,7 +98,7 @@ set(ALL_CLANG_TIDY_CHECKS
   clangTidyReadabilityModule
   clangTidyZirconModule
   )
-if(CLANG_ENABLE_STATIC_ANALYZER)
+if(CLANG_TIDY_ENABLE_STATIC_ANALYZER)
   list(APPEND ALL_CLANG_TIDY_CHECKS clangTidyMPIModule)
 endif()
 set(ALL_CLANG_TIDY_CHECKS ${ALL_CLANG_TIDY_CHECKS} PARENT_SCOPE)
index 90b3934..1f94ab4 100644 (file)
 #include "ClangTidyModuleRegistry.h"
 #include "ClangTidyProfiling.h"
 #include "ExpandModularHeadersPPCallbacks.h"
+#include "clang-tidy-config.h"
 #include "clang/AST/ASTConsumer.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Decl.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
-#include "clang/Config/config.h"
 #include "clang/Format/Format.h"
 #include "clang/Frontend/ASTConsumers.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include <algorithm>
 #include <utility>
 
-#if CLANG_ENABLE_STATIC_ANALYZER
+#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
 #include "clang/Analysis/PathDiagnostic.h"
 #include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
-#endif // CLANG_ENABLE_STATIC_ANALYZER
+#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
 
 using namespace clang::ast_matchers;
 using namespace clang::driver;
@@ -63,7 +63,7 @@ namespace clang {
 namespace tidy {
 
 namespace {
-#if CLANG_ENABLE_STATIC_ANALYZER
+#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
 static const char *AnalyzerCheckNamePrefix = "clang-analyzer-";
 
 class AnalyzerDiagnosticConsumer : public ento::PathDiagnosticConsumer {
@@ -95,7 +95,7 @@ public:
 private:
   ClangTidyContext &Context;
 };
-#endif // CLANG_ENABLE_STATIC_ANALYZER
+#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
 
 class ErrorReporter {
 public:
@@ -324,7 +324,7 @@ ClangTidyASTConsumerFactory::ClangTidyASTConsumerFactory(
   }
 }
 
-#if CLANG_ENABLE_STATIC_ANALYZER
+#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
 static void setStaticAnalyzerCheckerOpts(const ClangTidyOptions &Opts,
                                          AnalyzerOptionsRef AnalyzerOptions) {
   StringRef AnalyzerPrefix(AnalyzerCheckNamePrefix);
@@ -369,7 +369,7 @@ static CheckersList getAnalyzerCheckersAndPackages(ClangTidyContext &Context,
   }
   return List;
 }
-#endif // CLANG_ENABLE_STATIC_ANALYZER
+#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
 
 std::unique_ptr<clang::ASTConsumer>
 ClangTidyASTConsumerFactory::CreateASTConsumer(
@@ -424,7 +424,7 @@ ClangTidyASTConsumerFactory::CreateASTConsumer(
   if (!Checks.empty())
     Consumers.push_back(Finder->newASTConsumer());
 
-#if CLANG_ENABLE_STATIC_ANALYZER
+#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
   AnalyzerOptionsRef AnalyzerOptions = Compiler.getAnalyzerOpts();
   AnalyzerOptions->CheckersAndPackages = getAnalyzerCheckersAndPackages(
       Context, Context.canEnableAnalyzerAlphaCheckers());
@@ -440,7 +440,7 @@ ClangTidyASTConsumerFactory::CreateASTConsumer(
         new AnalyzerDiagnosticConsumer(Context));
     Consumers.push_back(std::move(AnalysisConsumer));
   }
-#endif // CLANG_ENABLE_STATIC_ANALYZER
+#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
   return std::make_unique<ClangTidyASTConsumer>(
       std::move(Consumers), std::move(Profiling), std::move(Finder),
       std::move(Checks));
@@ -453,11 +453,11 @@ std::vector<std::string> ClangTidyASTConsumerFactory::getCheckNames() {
       CheckNames.emplace_back(CheckFactory.getKey());
   }
 
-#if CLANG_ENABLE_STATIC_ANALYZER
+#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
   for (const auto &AnalyzerCheck : getAnalyzerCheckersAndPackages(
            Context, Context.canEnableAnalyzerAlphaCheckers()))
     CheckNames.push_back(AnalyzerCheckNamePrefix + AnalyzerCheck.first);
-#endif // CLANG_ENABLE_STATIC_ANALYZER
+#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
 
   llvm::sort(CheckNames);
   return CheckNames;
index 63e681f..3a5330c 100644 (file)
@@ -9,7 +9,7 @@
 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYFORCELINKER_H
 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYFORCELINKER_H
 
-#include "clang/Config/config.h"
+#include "clang-tidy-config.h"
 #include "llvm/Support/Compiler.h"
 
 namespace clang {
@@ -95,7 +95,7 @@ extern volatile int ModernizeModuleAnchorSource;
 static int LLVM_ATTRIBUTE_UNUSED ModernizeModuleAnchorDestination =
     ModernizeModuleAnchorSource;
 
-#if CLANG_ENABLE_STATIC_ANALYZER &&                                            \
+#if CLANG_TIDY_ENABLE_STATIC_ANALYZER &&                                       \
     !defined(CLANG_TIDY_DISABLE_STATIC_ANALYZER_CHECKS)
 // This anchor is used to force the linker to link the MPIModule.
 extern volatile int MPIModuleAnchorSource;
diff --git a/clang-tools-extra/clang-tidy/clang-tidy-config.h.cmake b/clang-tools-extra/clang-tidy/clang-tidy-config.h.cmake
new file mode 100644 (file)
index 0000000..f4d1a4b
--- /dev/null
@@ -0,0 +1,10 @@
+/* This generated file is for internal use. Do not include it from headers. */
+
+#ifdef CLANG_TIDY_CONFIG_H
+#error clang-tidy-config.h can only be included once
+#else
+#define CLANG_TIDY_CONFIG_H
+
+#cmakedefine01 CLANG_TIDY_ENABLE_STATIC_ANALYZER
+
+#endif
index 6b7af47..c7e7e80 100644 (file)
@@ -27,7 +27,7 @@ There are a few tools particularly useful when developing clang-tidy checks:
   * `clang-check`_ with the ``-ast-dump`` (and optionally ``-ast-dump-filter``)
     provides a convenient way to dump AST of a C++ program.
 
-If CMake is configured with ``CLANG_ENABLE_STATIC_ANALYZER``,
+If CMake is configured with ``CLANG_TIDY_ENABLE_STATIC_ANALYZER=NO``,
 :program:`clang-tidy` will not be built with support for the
 ``clang-analyzer-*`` checks or the ``mpi-*`` checks.
 
index 60217b8..15b756f 100644 (file)
@@ -16,7 +16,7 @@ endif ()
 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} CLANG_TOOLS_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
 
 llvm_canonicalize_cmake_booleans(
-  CLANG_ENABLE_STATIC_ANALYZER
+  CLANG_TIDY_ENABLE_STATIC_ANALYZER
   LIBCLANG_INCLUDE_CLANG_TOOLS_EXTRA
   )
 
index 2366f46..24cabd8 100644 (file)
@@ -115,7 +115,7 @@ if not platform.system() in ['Windows'] or not execute_external:
 if platform.system() not in ['Windows']:
     config.available_features.add('ansi-escape-sequences')
 
-if config.clang_staticanalyzer:
+if config.clang_tidy_staticanalyzer:
     config.available_features.add('static-analyzer')
 
 # Get shlex.quote if available (added in 3.3), and fall back to pipes.quote if
index 31ce2ea..7eef661 100644 (file)
@@ -10,7 +10,7 @@ config.clang_tools_dir = "@CLANG_TOOLS_DIR@"
 config.clang_libs_dir = "@SHLIBDIR@"
 config.python_executable = "@Python3_EXECUTABLE@"
 config.target_triple = "@TARGET_TRIPLE@"
-config.clang_staticanalyzer = @CLANG_ENABLE_STATIC_ANALYZER@
+config.clang_tidy_staticanalyzer = @CLANG_TIDY_ENABLE_STATIC_ANALYZER@
 config.libclang_include_clang_tools_extra = @LIBCLANG_INCLUDE_CLANG_TOOLS_EXTRA@
 
 # Support substitution of the tools and libs dirs with user parameters. This is
index f015951..3db476c 100644 (file)
@@ -473,7 +473,8 @@ option(CLANG_BUILD_TOOLS
   "Build the Clang tools. If OFF, just generate build targets." ON)
 
 option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
-option(CLANG_ENABLE_STATIC_ANALYZER "Build static analyzer." ON)
+option(CLANG_ENABLE_STATIC_ANALYZER
+  "Include static analyzer in clang binary." ON)
 
 option(CLANG_ENABLE_PROTO_FUZZER "Build Clang protobuf fuzzer." OFF)
 
index 6fbc4a5..9e15fff 100644 (file)
@@ -4,6 +4,7 @@ set(LLVM_TARGETS_TO_BUILD X86 CACHE STRING "")
 
 set(CLANG_ENABLE_ARCMT OFF CACHE BOOL "")
 set(CLANG_ENABLE_STATIC_ANALYZER OFF CACHE BOOL "")
+set(CLANG_TIDY_ENABLE_STATIC_ANALYZER OFF CACHE BOOL "")
 set(CLANG_VENDOR Android CACHE STRING "")
 
 set(CMAKE_BUILD_TYPE RELEASE CACHE STRING "")
index 2308278..1068288 100644 (file)
@@ -21,8 +21,6 @@ add_subdirectory(Tooling)
 add_subdirectory(DirectoryWatcher)
 add_subdirectory(Index)
 add_subdirectory(IndexSerialization)
-if(CLANG_ENABLE_STATIC_ANALYZER)
-  add_subdirectory(StaticAnalyzer)
-endif()
+add_subdirectory(StaticAnalyzer)
 add_subdirectory(Format)
 add_subdirectory(Testing)
index 18aa728..69217b7 100644 (file)
@@ -1,9 +1,32 @@
 import("//clang/lib/StaticAnalyzer/Frontend/enable.gni")
+import("//llvm/utils/gn/build/write_cmake_config.gni")
+import("enable.gni")
+
+config("clang-tidy-config_Config") {
+  visibility = [ ":clang-tidy-config" ]
+  include_dirs = [ "$target_gen_dir" ]
+}
+
+write_cmake_config("clang-tidy-config") {
+  input = "clang-tidy-config.h.cmake"
+  output = "$target_gen_dir/clang-tidy-config.h"
+  values = []
+
+  if (clang_tidy_enable_static_analyzer) {
+    values += [ "CLANG_TIDY_ENABLE_STATIC_ANALYZER=1" ]
+  } else {
+    values += [ "CLANG_TIDY_ENABLE_STATIC_ANALYZER=" ]
+  }
+
+  # Let targets depending on this find the generated file.
+  public_configs = [ ":clang-tidy-config_Config" ]
+}
 
 static_library("clang-tidy") {
   output_name = "clangTidy"
   configs += [ "//llvm/utils/gn/build:clang_code" ]
   deps = [
+    ":clang-tidy-config",
     "//clang/include/clang/StaticAnalyzer/Checkers",
     "//clang/lib/AST",
     "//clang/lib/ASTMatchers",
@@ -19,7 +42,7 @@ static_library("clang-tidy") {
     "//llvm/lib/Support",
   ]
 
-  if (clang_enable_static_analyzer) {
+  if (clang_tidy_enable_static_analyzer) {
     deps += [
       "//clang/lib/StaticAnalyzer/Core",
       "//clang/lib/StaticAnalyzer/Frontend",
@@ -64,7 +87,7 @@ group("all-checks") {
     "//clang-tools-extra/clang-tidy/readability",
     "//clang-tools-extra/clang-tidy/zircon",
   ]
-  if (clang_enable_static_analyzer) {
+  if (clang_tidy_enable_static_analyzer) {
     deps += [ "//clang-tools-extra/clang-tidy/mpi" ]
   }
 }
diff --git a/llvm/utils/gn/secondary/clang-tools-extra/clang-tidy/enable.gni b/llvm/utils/gn/secondary/clang-tools-extra/clang-tidy/enable.gni
new file mode 100644 (file)
index 0000000..9fc3e6e
--- /dev/null
@@ -0,0 +1,4 @@
+declare_args() {
+  # Whether to include the static analyzer in the clang-tidy binary.
+  clang_tidy_enable_static_analyzer = true
+}
index 3f06214..7ee93b5 100644 (file)
@@ -3,6 +3,7 @@ executable("clang-tidy") {
   deps = [
     "//clang-tools-extra/clang-tidy",
     "//clang-tools-extra/clang-tidy:all-checks",
+    "//clang-tools-extra/clang-tidy:clang-tidy-config",
     "//clang/lib/AST",
     "//clang/lib/ASTMatchers",
     "//clang/lib/Basic",
index 383cb2e..e8b1f15 100644 (file)
@@ -1,3 +1,4 @@
+import("//clang-tools-extra/clang-tidy/enable.gni")
 import("//clang/lib/StaticAnalyzer/Frontend/enable.gni")
 import("//clang/tools/libclang/include_clang_tools_extra.gni")
 import("//llvm/triples.gni")
@@ -38,10 +39,10 @@ write_lit_config("lit_site_cfg") {
     "Python3_EXECUTABLE=$python_path",
   ]
 
-  if (clang_enable_static_analyzer) {
-    extra_values += [ "CLANG_ENABLE_STATIC_ANALYZER=1" ]
+  if (clang_tidy_enable_static_analyzer) {
+    extra_values += [ "CLANG_TIDY_ENABLE_STATIC_ANALYZER=1" ]
   } else {
-    extra_values += [ "CLANG_ENABLE_STATIC_ANALYZER=0" ]
+    extra_values += [ "CLANG_TIDY_ENABLE_STATIC_ANALYZER=0" ]
   }
 
   if (libclang_include_clang_tools_extra) {