Add an explicit toggle for the static analyzer in clang-tidy

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
This commit is contained in:
Nico Weber 2020-09-03 19:37:29 -04:00
parent 018f6936db
commit 33c9dbbd38
16 changed files with 76 additions and 29 deletions

View 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)

View 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)

View File

@ -20,11 +20,11 @@
#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"
@ -47,10 +47,10 @@
#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;

View 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;

View File

@ -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

View 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.

View 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
)

View 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

View 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

View 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)

View 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 "")

View 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)

View 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" ]
}
}

View File

@ -0,0 +1,4 @@
declare_args() {
# Whether to include the static analyzer in the clang-tidy binary.
clang_tidy_enable_static_analyzer = true
}

View 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",

View 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) {