Running clang tidy for a qmake project from the command line
-
Context: my project (Qt 5.12, qmake-based) is developed on Windows with Qt Creator and automatically tested on an Ubuntu 20.04 build server. I'd like to use
clang-tidyto get additional static analysis checks in my CI system (e.g, fail the build ifclang-tidyreports something.)I've tried to use bear to generate a
compile_commands.jsonfile when my project is compiled in CI:#!/bin/bash # Compile mkdir build && cd build qmake -spec linux-clang ../main.pro make qmake_all bear make # generates compile_commands.json # Lint run-clang-tidyThis almost works (
clang-tidywill run with the settings defined in my.clang-tidyfile), but the generatedcompile_commands.jsonis written in terms ofmoc_*.cppfiles, which meansclang-tidyis reporting warnings on the code thatmocgenerated -- not my actual code. For example, I see stuff like this:/home/user/Code/MyProject/build/src/core/moc_mysourcefile.cpp:97:9: warning: function-like macro 'QT_MOC_LITERAL' used; consider a 'constexpr' template function [cppcoreguidelines-macro-usage] #define QT_MOC_LITERAL(idx, ofs, len) \ ^/home/user/Code/MyProject/build/src/core/moc_mysourcefile.cpp:99:93: warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses] qptrdiff(offsetof(qt_meta_stringdata_NotificationFullDateTimeDelegate_t, stringdata0) + ofs \ ^ ( )What I really want is the kind of warnings I get when I do
Analyze > Clang-Tidyin Qt Creator on Windows. In Qt Creator,clang-tidyis able to provide static analysis on my actual source code.Is there a way to get good
clang-tidychecks in my headless Linux CI system?Things I've looked at:
-
If you still have the same problem I published a bash solution which uses compiledb which allows to generate the file compile_commands.json with qmake on macOS: https://github.com/MiroYld/QtBuildTool
-
this Bash solution didn't work for me, on account of a multi-level project structure that i was dealing with. (at least, i think it was multi-level.)
What did work for me was something akin to the following:
qmake ./my_project.pro -spec linux-clang CONFIG+=release CONFIG+=DISABLE_DEVELOPER_MODULES CONFIG+=force_debug_info && /usr/bin/make qmake_all make -j $CORES VERBOSE=y all &>make_output.txt compiledb --parse make_output.txt mkdir build/yamls run-clang-tidy -p . -j $CORES -clang-tidy-binary $CLANG_TIDY_PATH -checks=-*,bugprone-*,clang-*,cppcoreguidelines-*,modernize-* -export-fixes build/yamlsthis gives me a bunch of
moc_*complaints, but also complaints on my actual code. themoc_*complaints seem to be clang-tidy's complaints about Qt itself, so i filter those out.