clang-tools 22.0.0git
CustomTidyModule.cpp
Go to the documentation of this file.
1#include "../ClangTidy.h"
4#include "QueryCheck.h"
5#include "llvm/ADT/SmallSet.h"
6#include "llvm/ADT/SmallString.h"
7#include "llvm/ADT/StringRef.h"
8#include <cassert>
9#include <memory>
10
11namespace clang::tidy {
12namespace custom {
13
14// We need to register the checks more flexibly than builtin modules. The checks
15// will changed dynamically when switching to different source file.
16static void registerCustomChecks(const ClangTidyOptions &Options,
17 ClangTidyCheckFactories &Factories) {
18 static llvm::SmallSet<llvm::SmallString<32>, 8> CustomCheckNames{};
19 if (!Options.CustomChecks.has_value() || Options.CustomChecks->empty())
20 return;
21 for (const llvm::SmallString<32> &Name : CustomCheckNames)
22 Factories.eraseCheck(Name);
24 Options.CustomChecks.value()) {
25 llvm::SmallString<32> Name = llvm::StringRef{"custom-" + V.Name};
26 Factories.registerCheckFactory(
27 // add custom- prefix to avoid conflicts with builtin checks
28 Name, [&V](llvm::StringRef Name, ClangTidyContext *Context) {
29 return std::make_unique<custom::QueryCheck>(Name, V, Context);
30 });
31 CustomCheckNames.insert(std::move(Name));
32 }
33}
34
35namespace {
36
37struct CustomChecksRegisterInitializer {
38 CustomChecksRegisterInitializer() noexcept {
40 }
41};
42
43class CustomModule : public ClangTidyModule {
44public:
45 void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {}
46};
47
48} // namespace
49
50static CustomChecksRegisterInitializer Init{};
51
52} // namespace custom
53
54// Register the CustomTidyModule using this statically initialized variable.
55static ClangTidyModuleRegistry::Add<custom::CustomModule>
56 X("custom-module", "Adds custom query lint checks.");
57
58// This anchor is used to force the linker to link in the generated object file
59// and thus register the AlteraModule.
60volatile int CustomModuleAnchorSource = 0; // NOLINT (misc-use-internal-linkage)
61
62} // namespace clang::tidy
A collection of ClangTidyCheckFactory instances.
void registerCheckFactory(llvm::StringRef Name, CheckFactory Factory)
Registers check Factory with name Name.
void eraseCheck(llvm::StringRef CheckName)
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void(* RegisterCustomChecks)(const ClangTidyOptions &O, ClangTidyCheckFactories &Factories)
static CustomChecksRegisterInitializer Init
static void registerCustomChecks(const ClangTidyOptions &Options, ClangTidyCheckFactories &Factories)
volatile int CustomModuleAnchorSource
static ClangTidyModuleRegistry::Add< altera::AlteraModule > X("altera-module", "Adds Altera FPGA OpenCL lint checks.")
Contains options for clang-tidy.
std::optional< CustomCheckValueList > CustomChecks