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-tidy to get additional static analysis checks in my CI system (e.g, fail the build if clang-tidy reports something.)

    I've tried to use bear to generate a compile_commands.json file when my project is compiled in CI:

    # Compile
    mkdir build && cd build
    qmake -spec linux-clang ../
    make qmake_all
    bear make  # generates compile_commands.json
    # Lint

    This almost works (clang-tidy will run with the settings defined in my .clang-tidy file), but the generated compile_commands.json is written in terms of moc_*.cpp files, which means clang-tidy is reporting warnings on the code that moc generated -- 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-Tidy in Qt Creator on Windows. In Qt Creator, clang-tidy is able to provide static analysis on my actual source code.

    Is there a way to get good clang-tidy checks in my headless Linux CI system?

    Things I've looked at: