c++ clang-tidy 给出与 llvm-libc 相关的错误

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

我一直无法让 clang-tidy 在我的本地计算机上工作。我的代码充满了这三个错误:

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]

我阅读了这些错误的解释,但我仍然不明白我需要做什么。这似乎与 llvm 中可用的 headers 有关?我通常使用 gcc 附带的 libstdc++,是否需要改用 llvm 库?例如,如果我想使用 cassert,如何消除这些错误?如果我能解决这个问题,我宁愿不只是在我的 .clang-tidy 中添加一些东西。

编辑:这是要求的附加信息。

程序版本: GCC 11.1.0: libstdc++.so.6.0.29

LLVM version 13.0.0

clang-tidy 是 运行 并且: clang-tidy *.cpp -std=c++11

下面是 .clang-tidy 的内容:(此文件是使用旧版本的 LLVM 提供的,应该如何更新 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  }

clang-tidy 由几个模块组成,可以通过多种方式激活或停用。您可以使用所有这些,none 个或其中的一些。这是当前的检查列表:

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 的模块有时非常迂腐和多余。它们中的每一个都是由给定的群体创建的,并且服务于不一定与您的目的一致的目的。这是模块的完整列表:

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

到 select 哪些模块并检查到 select 你的代码,你可以在命令行上传递一个类似查询的字符串,或者(更容易)放置一个 .clang-tidy 文件在项目的根目录中。这是我的:

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

要转储默认配置,请执行:

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

clang-tidy 的手册页有更多信息:

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

这是一般的 clang-tidy 文档: https://clang.llvm.org/extra/clang-tidy/