premake-core icon indicating copy to clipboard operation
premake-core copied to clipboard

Add clangtidy for gmake2

Open theComputeKid opened this issue 5 months ago • 0 comments

ClangTidy code checks have been added to the Visual Studio generator in #2187. A request for the equivalent in gmake2 has been made in #2245 as well as in this comment. Static analysis is a necessary tool for software developers.

This PR enables clang tidy for gmake2 at the configuration level. It adds a build command to the cpp rule enforcing the checks.

When clangtidy is Off, the build command enforcing clang tidy expands to a no-op, and it is skipped. When clangtidy is On, the command first runs clang tidy on the file, and then compiles it if the checks are successful. This method of working allows finishing the build early if clang tidy fails. This is also how the Visual Studio generator works, as well as how CMake implements clang tidy.

Closes #2245

I may need more help with this PR. Due to the invasive nature of the changes (a new build command is added to the makefile, which may understandably make the code reviewers uncomfortable), the following alternatives were considered:

  1. Add a pre/post build command performing clang tidy checks on all files. This is not possible as it requires a compile_commands.json file, and would probably run every time the build is run. Not a good idea.
  2. Conditionally add the build rule only if clangtidy is On. I don't know how to do this, because cfg.clangtidy does not seem to be available during cpp.initialize.

Because of these reasons, I instead decided to go with the implementation in this PR.

Future work is possible to make clang tidy in premake more powerful:

  1. Enable per-file clang tidy.
  2. Set clang tidy location.
  3. Set clang tidy additional args, including config file location.
  4. etc etc.

Did you check all the boxes?

  • [x] Focus on a single fix or feature; remove any unrelated formatting or code changes
  • [x] Add unit tests showing fix or feature works; all tests pass
  • [x] Mention any related issues (put closes #XXXX in comment to auto-close issue when PR is merged)
  • [x] Follow our coding conventions
  • [x] Minimize the number of commits
  • [x] Align documentation to your changes

theComputeKid avatar Sep 11 '24 21:09 theComputeKid