I've got a .h file that is included by both C and C++ source files. Its contents is wrapped in
#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif
Yet, when I include it in a .cpp file, clang-tidy issues C++-specific messages, like
- warning: including 'stdbool.h' has no effect in C++; consider removing it [hicpp-deprecated-headers,modernize-deprecated-headers]
- warning: inclusion of deprecated C++ header 'stdlib.h'; consider using 'cstdlib' instead [hicpp-deprecated-headers,modernize-deprecated-headers]
- warning: use 'using' instead of 'typedef' [modernize-use-using]
I like these checks and I want to keep them active in my clang-tidy configuration, but of course for C++ code only. I can't change the header file to use using
instead of typedef
or <cstdlib>
instead of <stdlib.h>
because it's also included by C sources, hence the extern "C"
.
Is there any way to tell clang-tidy to treat code in extern "C"
as C instead of C++, even if included from a .cpp file?
The clang-tidy version is 12.0.0.
extern
block and inclusion of C version of headers is not recommended (formally is UB) – Overlook