我使用以下命令在 Ubuntu 上安装了 clang-tidy:
sudo apt install clang-tidy
Run Code Online (Sandbox Code Playgroud)
我在一个简单的 C++ 17 文件上运行它,并收到警告和错误:
/home/erelsgl/Dropbox/ariel/CPLUSPLUS/intro/01-single-file/ptr.cpp:17:3: warning: 'auto' type specifier is a C++11 extension [clang-diagnostic-c++11-extensions]
auto i = make_unique<int>();
^
/home/erelsgl/Dropbox/ariel/CPLUSPLUS/intro/01-single-file/ptr.cpp:17:12: error: use of undeclared identifier 'make_unique' [clang-diagnostic-error]
auto i = make_unique<int>();
Run Code Online (Sandbox Code Playgroud)
我如何告诉 clang-tidy 根据 c++17 标准检查这个文件?
注意:要构建程序,我运行:
clang++-5.0 --std=c++17 ptr.cpp
Run Code Online (Sandbox Code Playgroud) 我想使用 clang-tidy 'readability-identifier-naming' 模块来清理我的代码,但我未能在具有类属性和方法的简短示例中正确使用它。
我使用了以下 .clang-tidy 文件:
Checks: '-*,readability-identifier-naming'
CheckOptions:
- { key: readability-identifier-naming.ClassCase, value: CamelCase }
- { key: readability-identifier-naming.VariableCase, value: lower_case }
- { key: readability-identifier-naming.FunctionCase, value: lower_case }
- { key: readability-identifier-naming.MemberPrefix, value: m_ }
- { key: readability-identifier-naming.ParameterCase, value: lower_case }
Run Code Online (Sandbox Code Playgroud)
在此代码上:
class one_class
{
public:
int OneMethod(int OneArgument);
int OneAttribute;
};
int one_class::OneMethod(int OneArgument)
{
OneAttribute = 42;
return OneArgument + 1;
}
int main(void)
{
int OneVariable = 0;
one_class c;
OneVariable = c.OneMethod(OneVariable); …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 make use of clang-tidy与cmake 的集成,我想传递这个-check
论点。我尝试-DCMAKE_CXX_CLANG_TIDY="/usr/local/opt/llvm38/bin/clang-tidy-3.8;-checks=*"
在调用 cmake 时添加,但我的 makefile 命令最终看起来像:
/usr/local/Cellar/cmake/3.6.2/bin/cmake -E __run_iwyu --tidy="/usr/local/opt/llvm38/bin/clang-tidy-3.8;-checks=*" --source=/Users/ellery/work/.....
换句话说,它似乎是 ; 分离的 args 没有被分开解析。我还尝试CXX_CLANG_TIDY
使用相同的值直接在我的目标上设置目标属性,并且我得到了相同的行为。
有没有人clang-tidy
通过 cmake成功调用了额外的参数?
在 clang tidy 中,检查[llvm-header-guard]查找 LLVM 样式的头保护,但我找不到任何正确的 LLVM 头保护样式的示例,特别是给定名称的结构,编码标准页面没有提到任何东西。
我正在尝试使用 clang-tidy 来解析由 arm-none-eabi-g++ 编译的项目。
不幸的是,clang-tidy 无法找到编译器头,即使给出了它们的包含路径。
我的compile_commands.json是
[
{
"directory": "C:/LMA/repo/clang-tidy",
"arguments": [
"arm-none-eabi-c++",
"-IC:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1",
"-IC:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1/arm-none-eabi/arm/v5te/hard",
"test.cpp"
],
"file": "./test.cpp" } ]
Run Code Online (Sandbox Code Playgroud)
示例 test.cpp 文件是:
#include <cstdint>
#include <cstdlib>
int test()
{
int temp;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Clang-tidy 显示错误:
C:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1\cstdlib:75:15: error: 'stdlib.h' file not found [clang-diagnostic-error]
#include_next <stdlib.h>
Run Code Online (Sandbox Code Playgroud)
因此,它正确地找到并包含 cstdlib,但是无法找到位于完全相同的文件夹中的 stdint.h。更令人恼火的是,它不包括 stdlib.h,即使我添加
-include C:/nxp/MCUXpressoIDE_11.2.1_4149/ide/tools/arm-none-eabi/include/c++/9.2.1/stdlib.h 到编译器参数以强制预包含。
非常感谢任何如何解决此问题的建议。
我有以下目录结构
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 .clang 格式
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 .clang-tidy
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 外部
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 脚本
\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 源
\n为了我的 clang 整洁执行,我只想包含source
文件夹中的标头并排除external
. 以下设置不适用于任一匹配 正则表达式HeaderFilterRegex: \'^(source)+(?!.*external).*\'
的格式正确
在文件中.clang-tidy
如果我只包含HeaderFilterRegex: \'^(?!.*external).*\'
它会排除该external
文件夹但不包含source
头文件
同样,如果我只包含此检查,HeaderFilterRegex: \'(src/)\'
它不会排除外部文件。
更新
\n好吧,这似乎与 clang 使用 Posix ERE 正则表达式风格有关(感谢 @pablo285),并且这不支持负向前瞻。我本以为这是使用 clang-tidy 的相当标准的要求,从分析中排除某些文件夹。
\n我有一个包含在 C 和 C++ 源文件中的 .h 文件。它的内容被包裹在
#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)
然而,当我将它包含在 .cpp 文件中时,clang-tidy 会发出特定于 C++ 的消息,例如
我喜欢这些检查,我想让它们在我的 clang-tidy 配置中保持活动状态,但当然仅适用于 C++ 代码。我无法更改头文件以使用using
代替typedef
或<cstdlib>
代替,<stdlib.h>
因为它也包含在 C 源中,因此extern "C"
.
有什么方法可以告诉 clang-tidy 将代码extern "C"
视为 C 而不是 C++,即使包含在 .cpp 文件中?
clang-tidy 版本是 12.0.0。
.clang-tidy
使用 clang-tidy 静态分析器,我可以在项目的根目录中
保留一个文件 ( ),其中包含我想要激活或停用的警告。clang-tidy
将查找此文件(据我所知)并使用其中定义的选项。这使我不必在 CMake 或 Makefile 中对长命令行进行硬编码。
cppcheck
静态分析器可以做同样的事情吗?
目前我有这个很长的命令行硬编码:
cppcheck --max-ctu-depth=3 --enable=all --inline-suppr --suppress=*:*thrust/complex* --suppress=missingInclude --suppress=syntaxError --suppress=unmatchedSuppression --suppress=preprocessorErrorDirective --language=c++ --std=c++14 --error-exitcode=666
Run Code Online (Sandbox Code Playgroud)
.clang-tidy
这是我保存在项目根目录下的配置文件示例:
---
Checks: '
*,
-readability-magic-numbers,
-modernize-use-nodiscard,
-altera-struct-pack-align,
-cert-err58-cpp,
-cppcoreguidelines-avoid-non-const-global-variables,
-cppcoreguidelines-macro-usage,
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
-cppcoreguidelines-pro-type-vararg,
-cppcoreguidelines-avoid-magic-numbers,
-fuchsia-default-arguments-calls,
-fuchsia-trailing-return,
-fuchsia-statically-constructed-objects,
-fuchsia-overloaded-operator,
-hicpp-vararg,
-hicpp-no-array-decay,
-llvm-header-guard,
-llvmlibc-restrict-system-libc-headers,
-llvmlibc-implementation-in-namespace,
-llvmlibc-callee-namespace
'
WarningsAsErrors: '*'
HeaderFilterRegex: '.'
AnalyzeTemporaryDtors: false
FormatStyle: file
...
Run Code Online (Sandbox Code Playgroud) 目前我正在我的项目中使用 Clang Format 实用程序。为了在我的团队中共享其设置,我将 .clang 格式的配置文件放在项目文件夹的根目录中,现在 IDE 在处理项目时会自动加载它。同样,我想使用 Clang Tidy 实用程序。但是,与 Clang Format 不同,我找不到配置文件格式的描述或创建它的实用程序。我还需要 IDE 自动加载这些设置并在自动格式化时考虑它们,因此我不可能使用向其传递必要参数的脚本来运行该实用程序。有办法实现我所需要的吗?
clang-tidy
我正在一个具有以下三个文件夹的中型项目中使用:
srcA
srcB
external
Run Code Online (Sandbox Code Playgroud)
我试图external
从分析中排除文件夹,但没有成功。我正在使用的命令是:
clang-tidy $SRC -p build/ --extra-arg=-ferror-limit=0'
Run Code Online (Sandbox Code Playgroud)
和
SRC=srcA/file.cpp srcA/fileN.cpp srcB/file.cpp srcB/fileN.cpp ...
Run Code Online (Sandbox Code Playgroud)
build/
以及由生成的编译数据库cmake
。请注意,SRC 不包含任何external
文件,仅包含srcA
和srcB
(.cpp 和 .hpp)。此外,显然,srcA
和srcB
下的一些文件正在使用external
.
80% 的错误来自clang-tidy
文件external/
,我无法修复这些错误,因为有第三方库。
下面是.clang-tidy
我正在使用的文件:
Checks: '-*,readability-identifier-naming'
WarningsAsErrors: "*"
CheckOptions:
- { key: readability-identifier-naming.ClassCase, value: CamelCase }
- { key: readability-identifier-naming.ClassMethodCase, value: camelBack }
- { key: readability-identifier-naming.VariableCase, value: camelBack }
- { key: readability-identifier-naming.PrivateMemberPrefix, value: m_ } …
Run Code Online (Sandbox Code Playgroud) clang-tidy ×10
c++ ×8
cmake ×2
llvm ×2
c ×1
c++17 ×1
cppcheck ×1
linter ×1
llvm-clang ×1
regex ×1