我有一些自定义的叮当声检查。例如,一个在cppcoreguidelines
模块中,另一个在misc
模块中。它们工作正常。现在,我通过自定义模块扩展了clang-tidy以将它们组织成其中。
当我重建时,它成功了,但是当我运行./clang-tidy -list-checks -checks=*
时,我的检查没有显示。
以下是我为添加名为sw
的自定义模块所做的工作:
-
在
/clang-tools-extra/clang-tidy/
下创建了一个子目录sw
-
更新
/clang-tools-extra/clang-tidy/CMakeLists.txt
:- 在
add_subdirectory(readability)
正下方添加add_subdirectory(sw)
- 在
set(ALL_CLANG_TIDY_CHECKS ...)
命令中列出clangTidySWModule
- 在
-
添加了包含以下内容的
/clang-tools-extra/clang-tidy/sw/CMakeLists.txt
:
set(LLVM_LINK_COMPONENTS
FrontendOpenMP
Support
)
add_clang_library(clangTidySWModule
AllCapsEnumeratorsCheck.cpp
CatchByConstReferenceCheck.cpp
SWTidyModule.cpp
LINK_LIBS
clangTidy
clangTidyUtils
DEPENDS
omp_gen
)
clang_target_link_libraries(clangTidySWModule
PRIVATE
clangAnalysis
clangAST
clangASTMatchers
clangBasic
clangLex
clangTooling
)
- 添加了包含以下内容的
/clang-tools-extra/clang-tidy/sw/SWTidyModule.cpp
:
#include "../ClangTidy.h"
#include "../ClangTidyModule.h"
#include "../ClangTidyModuleRegistry.h"
#include "AllCapsEnumeratorsCheck.h"
#include "CatchByConstReferenceCheck.h"
namespace clang {
namespace tidy {
namespace sw {
class SWModule : public ClangTidyModule {
public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
CheckFactories.registerCheck<AllCapsEnumeratorsCheck>(
"sw-all-caps-enumerators");
CheckFactories.registerCheck<CatchByConstReferenceCheck>(
"sw-catch-by-const-reference");
}
};
// Register the SWModule using this statically initialized variable.
static ClangTidyModuleRegistry::Add<SWModule>
X("sw-module", "Adds my custom checks.");
} // namespace sw
// This anchor is used to force the linker to link in the generated object file
// and thus register the ReadabilityModule.
volatile int SWModuleAnchorSource = 0;
} // namespace tidy
} // namespace clang
我添加了两个检查all-caps-enumerators
和catch-by-const-reference
,就像我之前在其他模块下所做的那样:
python3 add_new_check.py sw all-caps-enumerators
python3 add_new_check.py sw catch-by-const-reference
我错过了什么吗?为什么我的支票没有显示?
我在这里找到了我缺少的东西。希望这篇文章至少能帮助其他人更快地找到答案,以及在哪里准确添加缺失的部分。
我在/clang-tools-extra/clang-tidy/ClangTidyForceLinker.h
中缺少以下内容:
// This anchor is used to force the linker to link the SWModule.
extern volatile int SWModuleAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED SWModuleAnchorDestination =
SWModuleAnchorSource;
现在我的支票出现了,我很高兴(: