[clang-tools-extra] Add option to exclude headers from clang-tidy analysis (PR #91400)

SwanBai Lei27 via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 14 08:19:37 PDT 2024


Baiyi27 wrote:

I found a problem. In my project, I used `vcpkg` to manage the qt5 library.
```c++
#include "qapplication.h"
#include "qpushbutton.h"
#include "qstring.h"
#include "fmt/format.h"

class MainWindow: public QWidget {
public:
    MainWindow(QWidget *parent= nullptr)
        : QWidget(parent)
    {
        auto testMsg= fmt::format("This is {1} test: {0},{1}\n", "Hello", "Qt5");
        pButton_    = new QPushButton(QString::fromStdString(testMsg), this);
        resize(1000, 800);
    }

protected:
    void resizeEvent(QResizeEvent *event) override
    {
        int buttonWidth = width() / 4;
        int buttonHeight= height() / 4;

        pButton_->setGeometry((width() - buttonWidth) / 2,
                              (height() - buttonHeight) / 2,
                              buttonWidth,
                              buttonHeight);

        QWidget::resizeEvent(event);
    }

private:
    QPushButton *pButton_ { nullptr };
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    MainWindow mainWindow;
    mainWindow.show();

    return app.exec();
}
```
This is my `compile_commands.json` file configuration content:
```json
[{
  "directory": "D:/CXXProject/PlantSmash/build",
  "command": "D:\\Tools\\MSYS2\\mingw64\\bin\\c++.exe -DFMT_SHARED -DQT_CORE_LIB -DQT_GUI_LIB -DQT_WIDGETS_LIB -isystem D:/Tools/Vcpkg/installed/x64-mingw-dynamic/include/qt5 -isystem D:/Tools/Vcpkg/installed/x64-mingw-dynamic/include/qt5/QtCore -isystem D:/Tools/Vcpkg/installed/x64-mingw-dynamic/tools/qt5/mkspecs/win32-g++ -isystem D:/Tools/Vcpkg/installed/x64-mingw-dynamic/include/qt5/QtWidgets -isystem D:/Tools/Vcpkg/installed/x64-mingw-dynamic/include/qt5/QtGui -isystem D:/Tools/Vcpkg/installed/x64-mingw-dynamic/include -g -fdiagnostics-color=always -o tests\\Framework\\CMakeFiles\\Framework.dir\\Qt5Test.cc.obj -c D:\\CXXProject\\tests\\Framework\\Qt5Test.cc",
  "file": "D:\\CXXProject\\tests\\Framework\\Qt5Test.cc",
  "output": "tests\\Framework\\CMakeFiles\\Framework.dir\\Qt5Test.cc.obj"
}]
```
Use the command (clang-tidy-19) :
`clang-tidy -p .\build\ .\tests\Framework\Qt5Test.cc --header-filter='.*' --exclude-header-filter='.*\-q.*\.h'` This should correctly ignore the `q*.h` header files, and the output will not include diagnostic information about qt header files

Here is the output:
```diff
D:\CXXProject\tests\Framework\Qt5Test.cc:17:26: warning: no header providing "QWidget" is directly included [misc-include-cleaner]
   16 |
   17 | class MainWindow: public QWidget {
      |                          ^
D:\CXXProject\tests\Framework\Qt5Test.cc:19:5: warning: single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor]
   19 |     MainWindow(QWidget *parent= nullptr)
      |     ^
      |     explicit
D:\CXXProject\tests\Framework\Qt5Test.cc:19:25: warning: invalid case style for pointer parameter 'parent' [readability-identifier-naming]
   19 |     MainWindow(QWidget *parent= nullptr)
      |                         ^~~~~~
      |                         pArent
   20 |         : QWidget(parent)
      |                   ~~~~~~
      |                   pArent
D:\CXXProject\tests\Framework\Qt5Test.cc:24:16: warning: 1000 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
   24 |         resize(1000, 800);
      |                ^
D:\CXXProject\tests\Framework\Qt5Test.cc:24:22: warning: 800 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
   24 |         resize(1000, 800);
      |                      ^
D:\CXXProject\tests\Framework\Qt5Test.cc:28:22: warning: no header providing "QResizeEvent" is directly included [misc-include-cleaner]
   28 |     void resizeEvent(QResizeEvent *event) override
      |                      ^
D:\CXXProject\tests\Framework\Qt5Test.cc:28:36: warning: invalid case style for pointer parameter 'event' [readability-identifier-naming]
   28 |     void resizeEvent(QResizeEvent *event) override
      |                                    ^~~~~
      |                                    pEvent
   29 |     {
   30 |         int buttonWidth = width() / 4;
   31 |         int buttonHeight= height() / 4;
   32 |
   33 |         pButton_->setGeometry((width() - buttonWidth) / 2,
   34 |                               (height() - buttonHeight) / 2,
   35 |                               buttonWidth,
   36 |                               buttonHeight);
   37 |
   38 |         QWidget::resizeEvent(event);
      |                              ~~~~~
      |                              pEvent
D:\CXXProject\tests\Framework\Qt5Test.cc:42:18: warning: invalid case style for private member 'pButton_' [readability-identifier-naming]
   33 |         pButton_->setGeometry((width() - buttonWidth) / 2,
      |         ~~~~~~~~
      |         PButton_
   34 |                               (height() - buttonHeight) / 2,
   35 |                               buttonWidth,
   36 |                               buttonHeight);
   37 |
   38 |         QWidget::resizeEvent(event);
   39 |     }
   40 |
   41 | private:
   42 |     QPushButton *pButton_ { nullptr };
      |                  ^~~~~~~~
      |                  PButton_
D:\CXXProject\tests\Framework\Qt5Test.cc:52:12: warning: static member accessed through instance [readability-static-accessed-through-instance]
   52 |     return app.exec();
      |            ^~~~
      |            QApplication::

Suppressed 3386 warnings (3386 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.
Found compiler error(s).
```
But actually clang-tidy will still perform unnecessary analysis on qt header files, but will not output the results. I would like to ask if this function should directly skip the analysis of excluded header files?


https://github.com/llvm/llvm-project/pull/91400


More information about the cfe-commits mailing list