简体   繁体   English

如何修复 Clang Tidy 中的 llvmlibc-restrict-system-libc-headers 检查?

[英]How to fix llvmlibc-restrict-system-libc-headers check in Clang Tidy?

I do not understand the llvmlibc-restrict-system-libc-headers check in Clang Tidy ( link ).我不明白 Clang Tidy 中的llvmlibc-restrict-system-libc-headers检查( 链接)。

I include C libraries in C++ code like this:我在 C++ 代码中包含 C 库,如下所示:

#include <cstddef>

What should I change to fix this Clang Tidy check?我应该更改什么来修复此 Clang 整洁检查? Should it be fixed at all?它应该被修复吗?

The check enforces that the programmer uses compiler provided (libc) headers.检查强制程序员使用编译器提供的 (libc) 头文件。

It's used mainly by the team that develops llvm's libc in order to avoid using system provided headers.它主要由开发 llvm 的 libc 的团队使用,以避免使用系统提供的头文件。

From the Phabricator web interface of the clang-tidy project:来自 clang-tidy 项目的 Phabricator web 接口:

This adds a new module to enforce standards specific to the llvm-libc project.这添加了一个新模块来执行特定于 llvm-libc 项目的标准。 This change also adds the first check which restricts user from including system libc headers accidentally which can lead to subtle bugs that would be a challenge to detect.此更改还添加了第一个检查,该检查限制用户意外包含系统 libc 标头,这可能导致难以检测的细微错误。

And further并进一步

[...] I think the checker name should be generalized, as it does not need to be coupled with llvm-libc. [...] 我认为检查器名称应该是通用的,因为它不需要与 llvm-libc 结合使用。 Other projects may have similar needs.其他项目可能有类似的需求。 For example, they don't want to accidentally include a system zlib.h -> they may ship a bundled zlib (say, in third_party/zlib/).例如,他们不想意外包含系统 zlib.h -> 他们可能会发布捆绑的 zlib(例如,在 third_party/zlib/ 中)。

Thanks for the suggestions, the general check sounds like a great idea.感谢您的建议,一般检查听起来是个好主意。 I can see the use case for this as it can be used by anyone.我可以看到它的用例,因为它可以被任何人使用。 I took the time to port out fuchsia's check and flesh out the user facing documentation.我花时间移植了 fuchsia 的检查并充实了面向用户的文档。

You can fix it by providing the headers of intereset (here libc headers) explicitly in an include directory of your project and adjusting the linking paths correspondingly.您可以通过在项目的包含目录中显式提供感兴趣的标头(此处为 libc 标头)并相应地调整链接路径来修复它。

To disable it you can specify the unwanted check in clang-tidy's arguments.要禁用它,您可以在 clang-tidy 的 arguments 中指定不需要的检查。

CMake example: CMake 示例:

set(CMAKE_C_CLANG_TIDY
        clang-tidy;
            -header-filter=.*;
            -checks=*,-llvmlibc-restrict-system-libc-headers;
            -warnings-as-errors=*;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM