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
...

1 Answer 1

1

This is probably a problem of how Visual Studio Code implements its support for clang-tidy.

Clang-tidy itself supplies run-clang-tidy.py script file that runs clang-tidy over all files in a compilation database. It also prevents applying fixes multiple times in the same places of code.

Your options are:

  • Fix the code so the warnings are not issued
  • Use //NOLINT or //NOLINTNEXTLINE to suppress those warnings

EDIT: After the discussion this looks to me like a problem of how clang-tidy is invoked by cmake - it runs separately on each target and clang-tidy has no way to know it reported some error before. You should only use CMake to generate compile_commands.json and then run clang-tidy via run-clang-tidy.py.

This article seems to confirm my suspicion about clang-tidy integration with CMake.

Sign up to request clarification or add additional context in comments.

6 Comments

The problem is, I want them. Isn't there a way to force clang-tidy to check if the error for a particular header was printed out before and if so, not print the error again? The I guess vscode would work to fine, the problem is it occurs 3 times in terminal
I think this is a problem of invoking clang-tidy by the VS Code Clang-tidy Linter (I assume that is what you're using) so you can submit this as an error report on their github.
Unfortunately not. I am invoking it in my build, in CMakeLists.txt
Ok then, this might be a problem of how you are invoking it, you should add that info to your question.
I've added my CMake clang-tidy configuration
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.