Search code examples
c++clangllvmclang-tidy

c++ clang-tidy gives errors related to llvm-libc


I have been having trouble getting clang-tidy to work on my local computer. My code is filled with these three errors:

error: declaration must be declared within the '__llvm_libc' namespace [llvmlibc-implementation-in-namespace,-warnings-as-errors]

error: 'connect2AtLevel' must resolve to a function declared within the '__llvm_libc' namespace [llvmlibc-callee-namespace,-warnings-as-errors]

error: system include cassert not allowed [llvmlibc-restrict-system-libc-headers,-warnings-as-errors]

I read the explanations of these errors and I still don't understand what I need to do. It seems to be something to do with what headers are available in llvm? I normally use libstdc++ that is included with gcc, do I need to use the llvm library instead? If for instance I wanted to use cassert, what would that look like to get these errors to go away? I would rather not just add something to my .clang-tidy if I can fix this.

EDIT: Here is the additional information requested.

Program versions: GCC 11.1.0: libstdc++.so.6.0.29

LLVM version 13.0.0

clang-tidy is run with: clang-tidy *.cpp -std=c++11

And here is the contents of .clang-tidy: (this file was given using an older version of LLVM, what should be done to update the HeaderFilterRegex?):


WarningsAsErrors:  '*'                                                                     
HeaderFilterRegex: '.*'                                                                    
                                                                                           
CheckOptions:                                                                              
 { key: readability-identifier-naming.ClassCase,           value: CamelCase  }           
 { key: readability-identifier-naming.StructCase,          value: CamelCase  }           
 { key: readability-identifier-naming.EnumCase,            value: CamelCase  }           
 { key: readability-identifier-naming.GlobalConstantCase,  value: UPPER_CASE }           
                                                                                         
 { key: readability-identifier-naming.VariableCase,        value: camelBack  }           
 { key: readability-identifier-naming.ParameterCase,       value: camelBack  }           
 { key: readability-identifier-naming.PublicMemberCase,    value: camelBack  }

Solution

  • clang-tidy is composed of several modules that can be activated or deactivated in several ways. You might use all of them, none of them or some of them. This is the current list of checks:

    Name prefix     Description
    abseil-     Checks related to Abseil library.
    altera-     Checks related to OpenCL programming for FPGAs.
    android-    Checks related to Android.
    boost-      Checks related to Boost library.
    bugprone-   Checks that target bug-prone code constructs.
    cert-       Checks related to CERT Secure Coding Guidelines.
    clang-analyzer-     Clang Static Analyzer checks.
    concurrency-    Checks related to concurrent programming (including threads, fibers, coroutines, etc.).
    cppcoreguidelines-  Checks related to C++ Core Guidelines.
    darwin-     Checks related to Darwin coding conventions.
    fuchsia-    Checks related to Fuchsia coding conventions.
    google-     Checks related to Google coding conventions.
    hicpp-      Checks related to High Integrity C++ Coding Standard.
    linuxkernel-    Checks related to the Linux Kernel coding conventions.
    llvm-       Checks related to the LLVM coding conventions.
    llvmlibc-   Checks related to the LLVM-libc coding standards.
    misc-       Checks that we didn’t have a better category for.
    modernize-  Checks that advocate usage of modern (currently “modern” means “C++11”) language constructs.
    mpi-        Checks related to MPI (Message Passing Interface).
    objc-       Checks related to Objective-C coding conventions.
    openmp-     Checks related to OpenMP API.
    performance-    Checks that target performance-related issues.
    portability-    Checks that target portability-related issues that don’t relate to any particular coding style.
    readability-    Checks that target readability-related issues that don’t relate to any particular coding style.
    zircon-     Checks related to Zircon kernel coding conventions.
    

    clang-tidy's modules sometimes are very pedantic and excessive. Each of them were created by a given group and serve a purpose that do not necessarily align with yours. Here's the complete list of modules:

    https://clang.llvm.org/extra/clang-tidy/checks/list.html

    To select what modules and checks to select for your code, you can pass a query-like string on the command line or (easier) to place a .clang-tidy file in the root of your project. Here is mine:

    ---
    Checks:          '-*,clang-diagnostic-*,-clang-diagnostic-unused-value,clang-analyzer-*,-*,bugprone-*,performance-*,readability-*,-readability-magic-numbers,-readability-braces-around-statements,-readability-inconsistent-declaration-parameter-name,-readability-named-parameter'
    HeaderFilterRegex: ''
    WarningsAsErrors: '*'
    AnalyzeTemporaryDtors: false
    ...
    

    To dump the default configuration, do:

    $ clang-tidy -dump-config > .clang-tidy
    

    clang-tidy's man pages has more information:

    http://manpages.ubuntu.com/manpages/focal/man1/clang-tidy-6.0.1.html

    Here's the general clang-tidy documentation: https://clang.llvm.org/extra/clang-tidy/