我有一个包含在 C 和 C++ 源文件中的 .h 文件。它的内容被包裹在
#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)
然而,当我将它包含在 .cpp 文件中时,clang-tidy 会发出特定于 C++ 的消息,例如
我喜欢这些检查,我想让它们在我的 clang-tidy 配置中保持活动状态,但当然仅适用于 C++ 代码。我无法更改头文件以使用using代替typedef或<cstdlib>代替,<stdlib.h>因为它也包含在 C 源中,因此extern "C".
有什么方法可以告诉 clang-tidy 将代码extern "C"视为 C 而不是 C++,即使包含在 .cpp 文件中?
clang-tidy 版本是 12.0.0。
在下面的 C 程序中,函数f、g和h本质上是相同的,但是 clang-tidy 说参数p可以是指向常量的指针 in gand h(但不是 in f)。我的理解是,p它们中的任何一个都不能是指向常量的指针。那是假阳性吗?
struct S { int *p; };
extern void x(struct S *s);
void f(int *p)
{
struct S s;
s.p = p;
x(&s);
}
void g(int *p)
{
struct S s = { .p = p };
x(&s);
}
void h(int *p)
{
struct S s = { p };
x(&s);
}
int main()
{
int a = …Run Code Online (Sandbox Code Playgroud)