While I am using PIO for quite some years now, I never looked under the hood; meaning, I used it out of the box as is, and din’t pay with any configurations.
I have now seen that I can install clang-tidy to complain about style violations. Sounds great, but how to get there?!
I am running Linux Mint (Linux x570 6.8.0-57-generic #59~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Mar 19 17:07:41 UTC 2 x86_64 x86_64 x86_64 GNU/Linux) with up-to-date VScode and PIO.
When running
clang-tidy --version
Ubuntu LLVM version 14.0.0
I checked for the latest version, being 18, and installed it.
I now have:
clang-tidy-18 --version
Ubuntu LLVM version 18.1.8
I have placed a .clang-tidy
into the project root folder:
Checks: 'readability-identifier-naming'
CheckOptions:
- key: readability-identifier-naming.ConstantCase
value: UPPER_CASE
- key: readability-identifier-naming.ConstantPrefix
value: ''
- key: readability-identifier-naming.MemberConstantCase
value: UPPER_CASE
- key: readability-identifier-naming.MemberConstantPrefix
value: ''
- key: readability-identifier-naming.PointerCase
value: UPPER_CASE # Broaden to all pointers
- key: readability-identifier-naming.PointerPrefix
value: ''
HeaderFilterRegex: 'src/.*'
I also read that a .vscode/settings.json
file is required:
{
"C_Cpp.codeAnalysis.clangTidy.enabled": true,
"C_Cpp.codeAnalysis.clangTidy.path": "/usr/bin/clang-tidy-18",
"C_Cpp.codeAnalysis.clangTidy.checks.enabled": [
"readability-identifier-naming"
],
"C_Cpp.codeAnalysis.clangTidy.args": [
"--checks=readability-identifier-naming",
"-p=${workspaceFolder}/.pio/build/uno"
],
"C_Cpp.codeAnalysis.clangTidy.readability-identifier-naming.ConstantCase": "UPPER_CASE",
"C_Cpp.codeAnalysis.clangTidy.readability-identifier-naming.ConstantPrefix": "",
"C_Cpp.codeAnalysis.clangTidy.readability-identifier-naming.MemberConstantCase": "UPPER_CASE",
"C_Cpp.codeAnalysis.clangTidy.readability-identifier-naming.MemberConstantPrefix": "",
"C_Cpp.codeAnalysis.clangTidy.readability-identifier-naming.PointerCase": "UPPER_CASE",
"C_Cpp.codeAnalysis.clangTidy.readability-identifier-naming.PointerPrefix": "",
"C_Cpp.codeAnalysis.clangTidy.loggingLevel": "Debug",
"C_Cpp.codeAnalysis.runOnSave": true
}
I have added this line:
check_flags =
clangtidy: --config-file=.clang-tidy
to platformio.ini
, but noticed it works without as well.
It seems that all is working…
However, here my questions:
- did I perform the correct install?
- Are there any files or paramters missing elsewhere?
- Since I work with PIO, is the settings.jason file even required?
- I was expecting that View|Output would give me output from clang-tidy, but it doesn’t. What am I missing? Is it supposed to work like I expect?
- the commandline
clang-tidy src/config.cpp --checks=readability-identifier-naming -p=.pio/build/uno --config-file=.clang-tidy --header-filter='src/.*'
is showing (here an excerpt):
/home/maxg/Workspaces/PlatformIO/Projects/HubController/src/config.h:29:19: warning: invalid case style for constant 'ethernet_select' [readability-identifier-naming]
const uint8_t ethernet_select; // Ethernet chip select
^~~~~~~~~~~~~~~
ETHERNET_SELECT
/home/maxg/Workspaces/PlatformIO/Projects/HubController/src/config.h:31:19: warning: invalid case style for constant 'num_sensors' [readability-identifier-naming]
const uint8_t num_sensors; // Number of sensors}
^~~~~~~~~~~
NUM_SENSORS
Suppressed 19 warnings (19 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
- What I also noted, clang-tidy does not pick up
const char
capitalisation errors (it does as shown above withconst uint8_t
). Why? - Is there a way to specify which directories to include in the tidy check?
Any hints appreciated. Thanks.