Search code examples
c++clangllvmlibcclang-tidy

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).

I include C libraries in C++ code like this:

#include <cstddef>

What should I change to fix this Clang Tidy check? Should it be fixed at all?


Solution

  • The check enforces that the programmer uses compiler provided (libc) headers.

    It's used mainly by the team that develops llvm's libc in order to avoid using system provided headers.

    From the Phabricator web interface of the clang-tidy project:

    This adds a new module to enforce standards specific to the llvm-libc project. 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.

    And further

    [...] I think the checker name should be generalized, as it does not need to be coupled with 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/).

    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.

    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.

    To disable it you can specify the unwanted check in clang-tidy's arguments.

    CMake example:

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