Static Analysis In Bazel

This page details how to use/interact with the static analysis functionality we have in Bazel.

Supported Checkers

The following checkers have been approved by IT for use at Satelles. Use cases and pros/cons for each is below.

To install: sudo apt-get install cppcheck

Configuration: None

Use case: To catch simple/obvious things wrong with C++ code, such as undefined behaviors and dangerous coding constructs. Emphasis on very few false positives.

Pros: Runs VERY fast, even on complex code.

Cons: Relatively limited set of things it can flag/check for.

See here for full tool documentation.

Choosing A Checker To Run

If you are at the point of wanting to check over some code changes you’ve made, you will want to eventually run your changes through ALL of the above checkers, because that is what the CI/CD pipeline will ultimately do. But, in terms of your development usage, a good order to run the checkers in–in terms of bang-for-the-buck–is:

  • clang-check -> catch things which are bugs/obvious problems

  • cppcheck -> catch “easy” undefined behavior/bad coding style things

  • clang-tidy -> Catch things which don’t conform to selected coding style

Running a Checker

Checkers use Bazel aspects to hook into the build dependency graph, so they only run on things that Bazel is going to actually try to compile on a given invocation. So, the recommended two-step approach to running a checker is:

bazel clean
bazel build --config={cppcheck,clang-tidy,clang-check} <targets>

to ensure that the checker actually runs on ALL the files on your targets of interest.