1

I have a few custom clang-tidy checks. For instance one in the cppcoreguidelines module and another in the misc module. They work fine. Now I extended clang-tidy by a custom module to organize them into.

When I rebuild, it succeeds but when I run ./clang-tidy -list-checks -checks=*, my checks do not show up.

Here is what I did to add my custom module called sw:

  • Created a subdirectory sw under /clang-tools-extra/clang-tidy/

  • Updated /clang-tools-extra/clang-tidy/CMakeLists.txt by:

    • adding add_subdirectory(sw) right below add_subdirectory(readability)
    • listing clangTidySWModule inside the set(ALL_CLANG_TIDY_CHECKS ...) command
  • Added /clang-tools-extra/clang-tidy/sw/CMakeLists.txt with the following content:

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
  )
  • Added /clang-tools-extra/clang-tidy/sw/SWTidyModule.cpp with the following content:
#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

I added the two checks all-caps-enumerators and catch-by-const-reference like I did under other modules before:

python3 add_new_check.py sw all-caps-enumerators
python3 add_new_check.py sw catch-by-const-reference

Am I missing something? Why are my checks not showing up?

SebastianWilke
  • 470
  • 1
  • 4
  • 15

1 Answers1

2

I found what I was missing here. Hopefully this post will at least help others finding an answer quicker plus where to exactly add the missing piece.

I was missing the following in /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;

Now my checks show up and I am happy (:

SebastianWilke
  • 470
  • 1
  • 4
  • 15