小编Wol*_*ler的帖子

clang-tidy:如何抑制 C 头文件中的 C++ 警告?

我有一个包含在 C 和 C++ 源文件中的 .h 文件。它的内容被包裹在

#ifdef __cplusplus
extern "C" {
#endif

...

#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)

然而,当我将它包含在 .cpp 文件中时,clang-tidy 会发出特定于 C++ 的消息,例如

  • 警告:包括“stdbool.h”在 C++ 中无效;考虑删除它 [hicpp-deprecated-headers,modernize-deprecated-headers]
  • 警告:包含已弃用的 C++ 头文件“stdlib.h”;考虑使用“cstdlib”代替 [hicpp-deprecated-headers,modernize-deprecated-headers]
  • 警告:使用 'using' 而不是 'typedef' [modernize-use-using]

我喜欢这些检查,我想让它们在我的 clang-tidy 配置中保持活动状态,但当然仅适用于 C++ 代码。我无法更改头文件以使用using代替typedef<cstdlib>代替,<stdlib.h>因为它也包含在 C 源中,因此extern "C".

有什么方法可以告诉 clang-tidy 将代码extern "C"视为 C 而不是 C++,即使包含在 .cpp 文件中?

clang-tidy 版本是 12.0.0。

c c++ static-analysis clang-tidy

7
推荐指数
2
解决办法
269
查看次数

那个函数参数真的可以是指向常量的指针吗?

在下面的 C 程序中,函数fgh本质上是相同的,但是 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)

c pointers clang-tidy

5
推荐指数
1
解决办法
160
查看次数

标签 统计

c ×2

clang-tidy ×2

c++ ×1

pointers ×1

static-analysis ×1