Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a686695
origin pr
DeNiCoN Mar 17, 2025
421b26b
Revert "origin pr"
HerrCai0907 Mar 17, 2025
6fa8f7e
impl option
HerrCai0907 Mar 17, 2025
6ab7149
impl
HerrCai0907 Mar 17, 2025
db6b315
extend func
HerrCai0907 Mar 17, 2025
dba8a8c
wip
HerrCai0907 Mar 17, 2025
0df6f7e
test
HerrCai0907 Mar 17, 2025
5bb2c6c
doc
HerrCai0907 Mar 18, 2025
64c45dc
fix comment
HerrCai0907 Mar 18, 2025
b650594
Update clang-tools-extra/clang-tidy/custom/CustomTidyModule.cpp
HerrCai0907 Mar 19, 2025
b56c07c
fix review
HerrCai0907 Mar 19, 2025
5bbf53d
Merge branch 'main' into users/ccc/clang-tidy/query-check
HerrCai0907 Mar 25, 2025
b43991f
Merge remote-tracking branch 'origin/users/ccc/clang-tidy/query-check…
HerrCai0907 May 25, 2025
b2cecb8
add CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS cmake config
HerrCai0907 May 25, 2025
71eeaa1
Merge branch 'main' of https://github.com/llvm/llvm-project into user…
HerrCai0907 Jun 13, 2025
8be412c
remove ERROR level
HerrCai0907 Jun 13, 2025
1367f0b
fix typo
HerrCai0907 Jun 13, 2025
595e0a6
test, doc
HerrCai0907 Jun 13, 2025
190ad52
typo
HerrCai0907 Jun 13, 2025
5500699
fix review
HerrCai0907 Jun 16, 2025
4ce48a5
Apply suggestions from code review
HerrCai0907 Jun 16, 2025
fd15b05
Apply suggestions from code review
HerrCai0907 Jun 22, 2025
73dfc6e
Merge branch 'main' into users/ccc/clang-tidy/query-check
HerrCai0907 Jul 6, 2025
dce9682
Merge remote-tracking branch 'origin/main' into users/ccc/clang-tidy/…
HerrCai0907 Aug 30, 2025
b534fe9
add --enable-experimental-custom-checks
HerrCai0907 Aug 30, 2025
a373c55
rename
HerrCai0907 Aug 30, 2025
0a99d0c
format
HerrCai0907 Aug 30, 2025
9c1828f
Merge remote-tracking branch 'origin/main' into users/ccc/clang-tidy/…
HerrCai0907 Sep 4, 2025
2a3e0f1
doc
HerrCai0907 Sep 4, 2025
c292960
Merge remote-tracking branch 'origin/main' into users/ccc/clang-tidy/…
HerrCai0907 Sep 18, 2025
296eea3
fix link issue
HerrCai0907 Sep 18, 2025
0263db7
Revert "Revert "[clang-tidy] Fix bazel build after #131804 (lazy styl…
HerrCai0907 Sep 18, 2025
a902f69
fix build
HerrCai0907 Sep 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang-tools-extra/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ include(GNUInstallDirs)

option(CLANG_TIDY_ENABLE_STATIC_ANALYZER
"Include static analyzer checks in clang-tidy" ON)
option(CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS
"Enable query-based custom checks in clang-tidy" ON)

if(CLANG_INCLUDE_TESTS)
umbrella_lit_testsuite_begin(check-clang-tools)
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/clang-tidy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ add_subdirectory(bugprone)
add_subdirectory(cert)
add_subdirectory(concurrency)
add_subdirectory(cppcoreguidelines)
add_subdirectory(custom)
add_subdirectory(darwin)
add_subdirectory(fuchsia)
add_subdirectory(google)
Expand Down Expand Up @@ -101,6 +102,10 @@ set(ALL_CLANG_TIDY_CHECKS
clangTidyReadabilityModule
clangTidyZirconModule
)

if(CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS)
list(APPEND ALL_CLANG_TIDY_CHECKS clangTidyCustomModule)
endif()
if(CLANG_TIDY_ENABLE_STATIC_ANALYZER)
list(APPEND ALL_CLANG_TIDY_CHECKS clangTidyMPIModule)
endif()
Expand Down
39 changes: 29 additions & 10 deletions clang-tools-extra/clang-tidy/ClangTidy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ LLVM_INSTANTIATE_REGISTRY(clang::tidy::ClangTidyModuleRegistry)

namespace clang::tidy {

#if CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS
namespace custom {
void (*RegisterCustomChecks)(const ClangTidyOptions &O,
ClangTidyCheckFactories &Factories) = nullptr;
} // namespace custom
#endif

namespace {
#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
#define ANALYZER_CHECK_NAME_PREFIX "clang-analyzer-"
Expand Down Expand Up @@ -342,6 +349,10 @@ ClangTidyASTConsumerFactory::ClangTidyASTConsumerFactory(
IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS)
: Context(Context), OverlayFS(std::move(OverlayFS)),
CheckFactories(new ClangTidyCheckFactories) {
#if CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS
if (Context.canExperimentalCustomChecks() && custom::RegisterCustomChecks)
custom::RegisterCustomChecks(Context.getOptions(), *CheckFactories);
#endif
for (ClangTidyModuleRegistry::entry E : ClangTidyModuleRegistry::entries()) {
std::unique_ptr<ClangTidyModule> Module = E.instantiate();
Module->addCheckFactories(*CheckFactories);
Expand Down Expand Up @@ -411,7 +422,10 @@ ClangTidyASTConsumerFactory::createASTConsumer(
.getCurrentWorkingDirectory();
if (WorkingDir)
Context.setCurrentBuildDirectory(WorkingDir.get());

#if CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS
if (Context.canExperimentalCustomChecks() && custom::RegisterCustomChecks)
custom::RegisterCustomChecks(Context.getOptions(), *CheckFactories);
#endif
std::vector<std::unique_ptr<ClangTidyCheck>> Checks =
CheckFactories->createChecksForLanguage(&Context);

Expand Down Expand Up @@ -497,13 +511,13 @@ ClangTidyOptions::OptionMap ClangTidyASTConsumerFactory::getCheckOptions() {
return Options;
}

std::vector<std::string>
getCheckNames(const ClangTidyOptions &Options,
bool AllowEnablingAnalyzerAlphaCheckers) {
std::vector<std::string> getCheckNames(const ClangTidyOptions &Options,
bool AllowEnablingAnalyzerAlphaCheckers,
bool ExperimentalCustomChecks) {
clang::tidy::ClangTidyContext Context(
std::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(),
Options),
AllowEnablingAnalyzerAlphaCheckers);
AllowEnablingAnalyzerAlphaCheckers, false, ExperimentalCustomChecks);
ClangTidyASTConsumerFactory Factory(Context);
return Factory.getCheckNames();
}
Expand All @@ -524,11 +538,12 @@ void filterCheckOptions(ClangTidyOptions &Options,

ClangTidyOptions::OptionMap
getCheckOptions(const ClangTidyOptions &Options,
bool AllowEnablingAnalyzerAlphaCheckers) {
bool AllowEnablingAnalyzerAlphaCheckers,
bool ExperimentalCustomChecks) {
clang::tidy::ClangTidyContext Context(
std::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(),
Options),
AllowEnablingAnalyzerAlphaCheckers);
AllowEnablingAnalyzerAlphaCheckers, false, ExperimentalCustomChecks);
ClangTidyDiagnosticConsumer DiagConsumer(Context);
auto DiagOpts = std::make_unique<DiagnosticOptions>();
DiagnosticsEngine DE(llvm::makeIntrusiveRefCnt<DiagnosticIDs>(), *DiagOpts,
Expand Down Expand Up @@ -665,15 +680,19 @@ void exportReplacements(const llvm::StringRef MainFilePath,
YAML << TUD;
}

ChecksAndOptions
getAllChecksAndOptions(bool AllowEnablingAnalyzerAlphaCheckers) {
ChecksAndOptions getAllChecksAndOptions(bool AllowEnablingAnalyzerAlphaCheckers,
bool ExperimentalCustomChecks) {
ChecksAndOptions Result;
ClangTidyOptions Opts;
Opts.Checks = "*";
clang::tidy::ClangTidyContext Context(
std::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(), Opts),
AllowEnablingAnalyzerAlphaCheckers);
AllowEnablingAnalyzerAlphaCheckers, false, ExperimentalCustomChecks);
ClangTidyCheckFactories Factories;
#if CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS
if (ExperimentalCustomChecks && custom::RegisterCustomChecks)
custom::RegisterCustomChecks(Context.getOptions(), Factories);
#endif
for (const ClangTidyModuleRegistry::entry &Module :
ClangTidyModuleRegistry::entries()) {
Module.instantiate()->addCheckFactories(Factories);
Expand Down
14 changes: 10 additions & 4 deletions clang-tools-extra/clang-tidy/ClangTidy.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ class ClangTidyASTConsumerFactory {
/// Fills the list of check names that are enabled when the provided
/// filters are applied.
std::vector<std::string> getCheckNames(const ClangTidyOptions &Options,
bool AllowEnablingAnalyzerAlphaCheckers);
bool AllowEnablingAnalyzerAlphaCheckers,
bool ExperimentalCustomChecks);

struct ChecksAndOptions {
llvm::StringSet<> Checks;
llvm::StringSet<> Options;
};

ChecksAndOptions
getAllChecksAndOptions(bool AllowEnablingAnalyzerAlphaCheckers = true);
ChecksAndOptions getAllChecksAndOptions(bool AllowEnablingAnalyzerAlphaCheckers,
bool ExperimentalCustomChecks);

/// Returns the effective check-specific options.
///
Expand All @@ -74,7 +75,8 @@ getAllChecksAndOptions(bool AllowEnablingAnalyzerAlphaCheckers = true);
/// Options.
ClangTidyOptions::OptionMap
getCheckOptions(const ClangTidyOptions &Options,
bool AllowEnablingAnalyzerAlphaCheckers);
bool AllowEnablingAnalyzerAlphaCheckers,
bool ExperimentalCustomChecks);

/// Filters CheckOptions in \p Options to only include options specified in
/// the \p EnabledChecks which is a sorted vector.
Expand Down Expand Up @@ -125,6 +127,10 @@ void exportReplacements(StringRef MainFilePath,
const std::vector<ClangTidyError> &Errors,
raw_ostream &OS);

namespace custom {
extern void (*RegisterCustomChecks)(const ClangTidyOptions &O,
ClangTidyCheckFactories &Factories);
} // namespace custom
} // end namespace tidy
} // end namespace clang

Expand Down
7 changes: 4 additions & 3 deletions clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,12 @@ ClangTidyError::ClangTidyError(StringRef CheckName,

ClangTidyContext::ClangTidyContext(
std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
bool AllowEnablingAnalyzerAlphaCheckers, bool EnableModuleHeadersParsing)
bool AllowEnablingAnalyzerAlphaCheckers, bool EnableModuleHeadersParsing,
bool ExperimentalCustomChecks)
: OptionsProvider(std::move(OptionsProvider)),

AllowEnablingAnalyzerAlphaCheckers(AllowEnablingAnalyzerAlphaCheckers),
EnableModuleHeadersParsing(EnableModuleHeadersParsing) {
EnableModuleHeadersParsing(EnableModuleHeadersParsing),
ExperimentalCustomChecks(ExperimentalCustomChecks) {
// Before the first translation unit we can get errors related to command-line
// parsing, use dummy string for the file name in this case.
setCurrentFile("dummy");
Expand Down
13 changes: 11 additions & 2 deletions clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/Regex.h"
#include <optional>
#include <utility>

namespace clang {

Expand Down Expand Up @@ -68,10 +69,13 @@ struct ClangTidyStats {
/// \endcode
class ClangTidyContext {
public:
ClangTidyContext(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider)
: ClangTidyContext(std::move(OptionsProvider), false, false, false) {}
/// Initializes \c ClangTidyContext instance.
ClangTidyContext(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
bool AllowEnablingAnalyzerAlphaCheckers = false,
bool EnableModuleHeadersParsing = false);
bool AllowEnablingAnalyzerAlphaCheckers,
bool EnableModuleHeadersParsing,
bool ExperimentalCustomChecks);
/// Sets the DiagnosticsEngine that diag() will emit diagnostics to.
// FIXME: this is required initialization, and should be a constructor param.
// Fix the context -> diag engine -> consumer -> context initialization cycle.
Expand Down Expand Up @@ -210,6 +214,10 @@ class ClangTidyContext {
return EnableModuleHeadersParsing;
}

// whether experimental custom checks can be enabled.
// enabled with `--experimental-custom-checks`
bool canExperimentalCustomChecks() const { return ExperimentalCustomChecks; }

void setSelfContainedDiags(bool Value) { SelfContainedDiags = Value; }

bool areDiagsSelfContained() const { return SelfContainedDiags; }
Expand Down Expand Up @@ -258,6 +266,7 @@ class ClangTidyContext {

bool AllowEnablingAnalyzerAlphaCheckers;
bool EnableModuleHeadersParsing;
bool ExperimentalCustomChecks;

bool SelfContainedDiags = false;

Expand Down
7 changes: 7 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidyForceLinker.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ extern volatile int CppCoreGuidelinesModuleAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED CppCoreGuidelinesModuleAnchorDestination =
CppCoreGuidelinesModuleAnchorSource;

#if CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS
// This anchor is used to force the linker to link the CustomModule.
extern volatile int CustomModuleAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED CustomModuleAnchorDestination =
CustomModuleAnchorSource;
#endif

// This anchor is used to force the linker to link the DarwinModule.
extern volatile int DarwinModuleAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED DarwinModuleAnchorDestination =
Expand Down
2 changes: 2 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidyModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class ClangTidyCheckFactories {
});
}

void eraseCheck(llvm::StringRef CheckName) { Factories.erase(CheckName); }

/// Create instances of checks that are enabled.
std::vector<std::unique_ptr<ClangTidyCheck>>
createChecks(ClangTidyContext *Context) const;
Expand Down
51 changes: 50 additions & 1 deletion clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

#include "ClangTidyOptions.h"
#include "ClangTidyModuleRegistry.h"
#include "clang/Basic/DiagnosticIDs.h"
#include "clang/Basic/LLVM.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBufferRef.h"
Expand Down Expand Up @@ -129,6 +131,51 @@ void yamlize(IO &IO, ClangTidyOptions::OptionMap &Val, bool,
}
}

namespace {
struct MultiLineString {
std::string &S;
};
} // namespace

template <> struct BlockScalarTraits<MultiLineString> {
static void output(const MultiLineString &S, void *Ctxt, raw_ostream &OS) {
OS << S.S;
}
static StringRef input(StringRef Str, void *Ctxt, MultiLineString &S) {
S.S = Str;
return "";
}
};

template <> struct ScalarEnumerationTraits<clang::DiagnosticIDs::Level> {
static void enumeration(IO &IO, clang::DiagnosticIDs::Level &Level) {
IO.enumCase(Level, "Warning", clang::DiagnosticIDs::Level::Warning);
IO.enumCase(Level, "Note", clang::DiagnosticIDs::Level::Note);
}
};
template <> struct SequenceElementTraits<ClangTidyOptions::CustomCheckDiag> {
static const bool flow = false;
};
template <> struct MappingTraits<ClangTidyOptions::CustomCheckDiag> {
static void mapping(IO &IO, ClangTidyOptions::CustomCheckDiag &D) {
IO.mapRequired("BindName", D.BindName);
MultiLineString MLS{D.Message};
IO.mapRequired("Message", MLS);
IO.mapOptional("Level", D.Level);
}
};
template <> struct SequenceElementTraits<ClangTidyOptions::CustomCheckValue> {
static const bool flow = false;
};
template <> struct MappingTraits<ClangTidyOptions::CustomCheckValue> {
static void mapping(IO &IO, ClangTidyOptions::CustomCheckValue &V) {
IO.mapRequired("Name", V.Name);
MultiLineString MLS{V.Query};
IO.mapRequired("Query", MLS);
IO.mapRequired("Diagnostic", V.Diags);
}
};

struct ChecksVariant {
std::optional<std::string> AsString;
std::optional<std::vector<std::string>> AsVector;
Expand Down Expand Up @@ -184,6 +231,7 @@ template <> struct MappingTraits<ClangTidyOptions> {
IO.mapOptional("InheritParentConfig", Options.InheritParentConfig);
IO.mapOptional("UseColor", Options.UseColor);
IO.mapOptional("SystemHeaders", Options.SystemHeaders);
IO.mapOptional("CustomChecks", Options.CustomChecks);
}
};

Expand Down Expand Up @@ -245,7 +293,8 @@ ClangTidyOptions &ClangTidyOptions::mergeWith(const ClangTidyOptions &Other,
overrideValue(UseColor, Other.UseColor);
mergeVectors(ExtraArgs, Other.ExtraArgs);
mergeVectors(ExtraArgsBefore, Other.ExtraArgsBefore);

// FIXME: how to handle duplicate names check?
mergeVectors(CustomChecks, Other.CustomChecks);
for (const auto &KeyValue : Other.CheckOptions) {
CheckOptions.insert_or_assign(
KeyValue.getKey(),
Expand Down
14 changes: 14 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidyOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H

#include "clang/Basic/DiagnosticIDs.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringMap.h"
Expand Down Expand Up @@ -129,6 +130,19 @@ struct ClangTidyOptions {
/// Key-value mapping used to store check-specific options.
OptionMap CheckOptions;

struct CustomCheckDiag {
std::string BindName;
std::string Message;
std::optional<DiagnosticIDs::Level> Level;
};
struct CustomCheckValue {
std::string Name;
std::string Query;
llvm::SmallVector<CustomCheckDiag> Diags;
};
using CustomCheckValueList = llvm::SmallVector<CustomCheckValue>;
std::optional<CustomCheckValueList> CustomChecks;

using ArgList = std::vector<std::string>;

/// Add extra compilation arguments to the end of the list.
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clang-tidy/clang-tidy-config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
#define CLANG_TIDY_CONFIG_H

#cmakedefine01 CLANG_TIDY_ENABLE_STATIC_ANALYZER
#cmakedefine01 CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS

#endif
38 changes: 38 additions & 0 deletions clang-tools-extra/clang-tidy/custom/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
if(CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS)
set(LLVM_LINK_COMPONENTS
FrontendOpenMP
support
)

add_clang_library(clangTidyCustomModule STATIC
CustomTidyModule.cpp
QueryCheck.cpp

LINK_LIBS
clangTidy
clangTidyBugproneModule
clangTidyMiscModule
clangTidyModernizeModule
clangTidyPerformanceModule
clangTidyReadabilityModule
clangTidyUtils

DEPENDS
omp_gen
ClangDriverOptions
)

clang_target_link_libraries(clangTidyCustomModule
PRIVATE
clangAnalysis
clangAST
clangASTMatchers
clangBasic
clangDynamicASTMatchers
clangFrontend
clangLex
clangQuery
clangSerialization
clangTooling
)
endif()
Loading