Clang-Tidy is a clang-based C++ “linter” tool. Its purpose is to provide an extensible framework for diagnosing and fixing typical programming errors, like style violations, interface misuse, or bugs that can be deduced via static analysis. Official page can be found here.
Clang-Tidy supports a large variety of static checks that may not be covered by the compiler itself. These checks are static analysis checks that can be performed at a source code level.
Some of the defects that might be detected include:
- Buffer overflow
- Potential NULL pointer dereferences
- Use of memory that has already been deallocated
- Out of scope memory usage
- Failure to set a return value from a subroutine
[env:myenv] platform = ... board = ... check_tool = clangtidy
Useful options that can be used used for adjusting check process:
There are currently the following groups of most used checks (By default all checks are enabled):
Checks related to Abseil library.
Checks related to Boost library.
Checks that target bugprone code constructs.
Checks related to CERT Secure Coding Guidelines.
Checks related to C++ Core Guidelines.
Clang Static Analyzer checks.
Checks related to Google coding conventions.
Checks related to High Integrity C++ Coding Standard.
Checks that advocate usage of modern (currently modern means
C++11) language constructs.
Checks that target performance-related issues.
Checks that target portability-related issues that don’t relate to any particular coding style.
Checks that target readability-related issues that don’t relate to any particular coding style.
The full list of supported checks can be found on the official webpage.
Useful flags that can help more precisely configure Clang-Tidy to satisfy your project requirements:
Comma-separated list of enabled checks (
Apply suggested fixes. Without
-fix-errorsclang-tidy will bail out if any compilation errors were found.
Apply suggested fixes even if compilation errors were found. If compiler errors have attached fix-its, clang-tidy will apply them as well.
Style for formatting code around applied fixes:
Display the errors from system headers.
An example with enabling specific checks and fixing code on the fly:
[env:myenv] platform = ... board = ... check_tool = clangtidy check_flags = clangtidy: --checks=-*,cert-*,clang-analyzer-* --fix