0

I wanted to add a custom check to clang-tidy so I followed the official documentation using the command clang-tidy/add_new_check.py misc passes (https://clang.llvm.org/extra/clang-tidy/Contributing.html)

Check is registered in CMakeLists.txt and MiscTidyModule.cpp

The new check named passes registered inside CMakeLists.txt

Check registered in MiscTidyModule.cpp as well

The check is still not available with clang-tidy - clang-tidy -checks="*" --list-checks | grep passes

  • After editing those files, what command did you run to recompile? Did it create a new `clang-tidy` with a recent timestamp? Is there any output from `strings clang-tidy | grep misc-passes`? – Scott McPeak Jul 22 '23 at 03:42
  • I build the entire code using by cd into `/clang-llvm/llvm-project/build` and then `ninja`. `clang-tidy -checks="*" --list-checks | grep passes` doesn't return anything. I think I'm asking a really basic question but how/where should I check if new clang-tidy is generated? or if I'm not building clang-tidy correctly. – Nachiket Deo Jul 22 '23 at 20:07
  • After running `ninja` in the `build` directory, there should now be a file called `bin/clang-tidy` with a timestamp more recent than when you edited those files. If you don't see `clang-tidy` there, but you do see other programs like `clang`, then you might have invoked `cmake` incorrectly; you need to have passed `-DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra"` to it in order to enable the `clang-tools-extra` project, which `clang-tidy` is a part of. – Scott McPeak Jul 23 '23 at 01:49
  • Thanks for this. When I run the clang-tidy from `build/bin/clang-tidy` the changes are reflected. I was previously running clang-tidy which was installed and not the one that was getting build – Nachiket Deo Jul 25 '23 at 02:40

1 Answers1

1

As now explained in the comments, the problem was due to invoking the originally installed clang-tidy rather than the one built from the modified sources. The latter is found in $BUILD/bin/clang-tidy where $BUILD is the directory in which cmake was run.

To make this Q+A potentially more useful to others, I'll summarize some relevant troubleshooting:

  • If $BUILD/bin/clang-tidy is missing but other programs like $BUILD/bin/clang are present, the problem is likely due to how cmake was invoked. In particular, one must pass -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" to cmake in order to enable the clang-tools-extra project, of which clang-tidy is a part.

  • If you have a clang-tidy, you can run the strings program on it to see if string literals from your custom check are present in the binary. If not, then the problem is likely related to the build system, such as a missing entry in CMakeLists.txt or failing to re-run the build properly.

  • If the check has been linked in, then it should appear in the output of $BUILD/bin/clang-tidy -checks="*" --list-checks. If it does not, then the problem is likely with how the check is being registered (with registerCheck; see ClangTidyCheckFactories).

Scott McPeak
  • 8,803
  • 2
  • 40
  • 79