2

I have a problem with clang-tidy. Basically, it analyzes each of my project files, but for the headers that are included in more than one .cpp file, it prints redundant errors.

The problem is, Visual Studio Code has its PROBLEMS tab, which picks every single one of them, so for a file definitions.hpp which is included in 3 separate .cpp files I end up with something like this:

enter image description here

The console output is:

[build] [3/4  25% :: 14.699] Building CXX object CMakeFiles\solver.dir\src\definitions.cpp.obj
[build] [...]\build\..\src/definitions.hpp:1:9: warning: header guard does not follow preferred style [llvm-header-guard]
[build] #ifndef DEFINITIONS_HPP
[build]         ^~~~~~~~~~~~~~~
[...]
[build] [3/4  50% :: 16.138] Building CXX object CMakeFiles\solver.dir\src\genetic_algorithm.cpp.obj
[build] [...]\build\..\src/definitions.hpp:1:9: warning: header guard does not follow preferred style [llvm-header-guard]
[build] #ifndef DEFINITIONS_HPP
[build]         ^~~~~~~~~~~~~~~
[...]
[build] [3/4  75% :: 17.362] Building CXX object CMakeFiles\solver.dir\src\main.cpp.obj
[build] [...]\build\..\src/definitions.hpp:1:9: warning: header guard does not follow preferred style [llvm-header-guard]
[build] #ifndef DEFINITIONS_HPP
[build]         ^~~~~~~~~~~~~~~

So, is there a way to prevent something like this? I mean it doubles of triples my error list.

@Edit

So this is my clang-tidy-relevant part of CMakeLists.txt:

if(CMAKE_VERSION VERSION_GREATER 3.6)
    option(CLANG_TIDY_FIX "Perform fixes for Clang-Tidy" OFF)
    find_program(
        CLANG_TIDY_EXE
        NAMES "clang-tidy"
        DOC "Path to clang-tidy executable"
    )

    if(CLANG_TIDY_EXE)
        if(CLANG_TIDY_FIX)
            set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_EXE}" "-fix")
        else()
            message("SETTING UP CLANG TIDY")
            set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_EXE}")
        endif()
    endif()
endif()

And this is my .clang-tidy file:

---
Checks:          '*'
HeaderFilterRegex: '.*'
AnalyzeTemporaryDtors: false
FormatStyle:     none
...
4

1 回答 1

1

这可能是 Visual Studio Code 如何实现其对 clang-tidy 的支持的问题。

Clang-tidy 本身提供run-clang-tidy.py脚本文件,该文件在编译数据库中的所有文件上运行 clang-tidy。它还可以防止在相同的代码位置多次应用修复。

您的选择是:

  • 修复代码,以免发出警告
  • 使用//NOLINT//NOLINTNEXTLINE抑制这些警告

编辑:在讨论之后,这在我看来就像 cmake 如何调用 clang-tidy 的问题 - 它在每个目标上单独运行,并且 clang-tidy 无法知道它之前报告了一些错误。您应该只使用 CMake 生成compile_commands.json然后通过run-clang-tidy.py运行 clang-tidy 。

This article似乎证实了我对clang-tidy与CMake集成的怀疑。

于 2019-04-24T10:06:06.417 回答