Ignore [clang-diagnostic-error] clang-tidy caused by 3rd party headers

user2930353 picture user2930353 · May 15, 2018 · Viewed 9.2k times · Source

I am using clang-tidy as a "linter" tool in development. I started to integrate 3rd party software into my code and when I include their header files using:

-I/path/to/include 

tons of errors are generated, I haven't even #include the headers yet.

error: too many errors emitted, stopping now [clang-diagnostic-error]
...
/path/to/include/wchar.h:81:1: error: unknown type name 'wint_t' [clang-diagnostic-error]
wint_t fgetwc(FILE *__stream);
^
/path/to/include/wchar.h:81:15: error: unknown type name 'FILE' [clang-diagnostic-error]
wint_t fgetwc(FILE *__stream);
              ^
...

I compile my program using:

/usr/bin/clang-tidy-4.0 /path/to/main.cpp -checks=-*,cppcoreguidelines* -- -lang-c++ -I/path/to/include -std=gnu++11 -Wall -Werror -O0 -g -D<define variables>

It seems that these "clang-diagnostic-errors" do not stop compilation, as it continues to compile and runs fine. Is there a flag to turn this error off/suppress it? I do not want to see it since I did not write these header files.

If I get rid of the argument -I/path/to/include everything compiles fine with no errors.

Answer

pablo285 picture pablo285 · May 11, 2021

For clang-tidy to work the analyzed code needs to be compile-able by the clang backend to generate an AST. This is apparently not the case since clang-diagnostic-error is basically a compilation error.

The problem is you are including headers that cannot be compiled by clang (I'm guessing windows headers intended for MSVC), there is no way to ignore that.