How to add a new clang-tidy module

Hi, all,

  I am planning to add clang-tidy checkers for my company. How to add a new module for my company? Please help, thanks in advance.

  I try to copy files from cert module, and rename cert to Misra, then add a rule named "m-0-1-1" by ./add_new_checker.py.

  then I run ninja check-clang-tool, but my case is failed due to below error

  Running ['clang-tidy', '/Users/zyg/Documents/workspace/llvm-project/llvm/cmake-build-debug/tools/clang/tools/extra/test/clang-tidy/checkers/Output/misra-m-0-1-1.cpp.tmp.cpp', '-fix', '--checks=-*,misra-m-0-1-1', '-format-style=none', '--', '-std=c++11', '-nostdinc++']... clang-tidy failed: Error: no checks enabled. USAGE: clang-tidy [options] <source0> [... <sourceN>]

Below is my diff

diff --git a/clang-tools-extra/clang-tidy/CMakeLists.txt b/clang-tools-extra/clang-tidy/CMakeLists.txt
index ca7a5afed6b..ccb77ef6a62 100644
--- a/clang-tools-extra/clang-tidy/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/CMakeLists.txt
@@ -64,6 +64,7 @@ add_subdirectory(linuxkernel)
add_subdirectory(llvm)
add_subdirectory(llvmlibc)
add_subdirectory(misc)
+add_subdirectory(misra)
add_subdirectory(modernize)
if(CLANG_TIDY_ENABLE_STATIC_ANALYZER)
   add_subdirectory(mpi)
@@ -90,6 +91,7 @@ set(ALL_CLANG_TIDY_CHECKS
   clangTidyLLVMModule
   clangTidyLLVMLibcModule
   clangTidyMiscModule
+ clangTidyMISRAModule
   clangTidyModernizeModule
   clangTidyObjCModule
   clangTidyOpenMPModule
diff --git a/clang-tools-extra/clang-tidy/misra/CMakeLists.txt b/clang-tools-extra/clang-tidy/misra/CMakeLists.txt
new file mode 100644
index 00000000000..ca345a946c9
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/misra/CMakeLists.txt
@@ -0,0 +1,28 @@
+set(LLVM_LINK_COMPONENTS
+ support
+ FrontendOpenMP
+ )

See https://reviews.llvm.org/D36836 for modern example of minimal scaffolding.

Roman

Hi, Roman,

Thanks a lot, root cause should be like below. I am not familiar with this linker magic, :frowning:

Hi, Roman,

  Thanks a lot, I have resolved this issue.

diff --git a/clang-tools-extra/clang-tidy/ClangTidyForceLinker.h b/clang-tools-extra/clang-tidy/ClangTidyForceLinker.h
index 3a5330c85c3..d9a5b486d6e 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyForceLinker.h
+++ b/clang-tools-extra/clang-tidy/ClangTidyForceLinker.h
@@ -90,6 +90,11 @@ extern volatile int MiscModuleAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED MiscModuleAnchorDestination =
     MiscModuleAnchorSource;

+// This anchor is used to force the linker to link the MISRAModule.
+extern volatile int MISRAModuleAnchorSource;
+static int LLVM_ATTRIBUTE_UNUSED MISRAModuleAnchorDestination =
+ MISRAModuleAnchorSource;