60 lines · cpp
1#include "../ClangTidy.h"2#include "../ClangTidyModule.h"3#include "../ClangTidyModuleRegistry.h"4#include "../ClangTidyOptions.h"5#include "QueryCheck.h"6#include "llvm/ADT/SmallSet.h"7#include "llvm/ADT/SmallString.h"8#include "llvm/ADT/StringRef.h"9#include <cassert>10#include <memory>11 12namespace clang::tidy {13namespace custom {14 15class CustomModule : public ClangTidyModule {16public:17 void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {}18};19 20// We need to register the checks more flexibly than builtin modules. The checks21// will changed dynamically when switching to different source file.22extern void registerCustomChecks(const ClangTidyOptions &Options,23 ClangTidyCheckFactories &Factories) {24 static llvm::SmallSet<llvm::SmallString<32>, 8> CustomCheckNames{};25 if (!Options.CustomChecks.has_value() || Options.CustomChecks->empty())26 return;27 for (const llvm::SmallString<32> &Name : CustomCheckNames)28 Factories.eraseCheck(Name);29 for (const ClangTidyOptions::CustomCheckValue &V :30 Options.CustomChecks.value()) {31 llvm::SmallString<32> Name = llvm::StringRef{"custom-" + V.Name};32 Factories.registerCheckFactory(33 // add custom- prefix to avoid conflicts with builtin checks34 Name, [&V](llvm::StringRef Name, ClangTidyContext *Context) {35 return std::make_unique<custom::QueryCheck>(Name, V, Context);36 });37 CustomCheckNames.insert(std::move(Name));38 }39}40 41struct CustomChecksRegisterInitializer {42 CustomChecksRegisterInitializer() noexcept {43 RegisterCustomChecks = &custom::registerCustomChecks;44 }45};46 47static CustomChecksRegisterInitializer Init{};48 49} // namespace custom50 51// Register the CustomTidyModule using this statically initialized variable.52static ClangTidyModuleRegistry::Add<custom::CustomModule>53 X("custom-module", "Adds custom query lint checks.");54 55// This anchor is used to force the linker to link in the generated object file56// and thus register the AlteraModule.57volatile int CustomModuleAnchorSource = 0; // NOLINT (misc-use-internal-linkage)58 59} // namespace clang::tidy60