llvm - clang-tidy 未能通过错误检查
问题描述
我正在尝试clang-tidy
使用以下文件:
#include <stdio.h>
int main(int argc, char **argv)
{
int i=2; int j=1;
if (argc = 5) { return 2; }
while (i<argc) { j++; }
return 0;
}
我的目标是检测无限循环:
$ clang-tidy -checks=bugprone-infinite-loop main.c
但clang-tidy
找到的只是=
代替的==
东西:
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "main.c"
No compilation database found in /home/oren or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
1 warning generated.
/home/oren/main.c:6:11: warning: using the result of an assignment as a condition without parentheses [clang-diagnostic-parentheses]
if (argc = 5) { return 2; }
~~~~ ^ ~
( == )
/home/oren/main.c:6:11: note: place parentheses around the assignment to silence this warning
/home/oren/main.c:6:11: note: use '==' to turn this assignment into an equality comparison
解决方案
您正在使用尚未发布的 LLVM (10.0.0) 版本中的功能。
在我的系统(Windows)上,您的文件按预期工作:
>clang-tidy10 -checks=bugprone-infinite-loop infloop.c --
1 warning generated.
\infloop.c:6:5: warning: this loop is infinite; none of its con
dition variables (i, argc) are updated in the loop body [bugprone-infinite-loop]
while (i<argc) { j++; }
^
我对文件所做的唯一修改是删除了不必要的#include。我还在--
命令中添加了(双破折号)以消除缺少的编译数据库错误。我正在使用来自https://llvm.org/builds/的预构建二进制文件
我在这里最好的猜测是,您使用的是较旧的 clang-tidy 版本,但未检测到这一点。作为参考,我的版本是 10.0.0-e20a1e486e1,您可以通过运行查看您的版本:
>clang-tidy --version
我还会检查您是否真的在运行您希望通过以下方式运行的检查:
$ clang-tidy -checks=bugprone-infinite-loop main.c --list-checks
PS您最初收到的警告消息是基于clang-diagnostic的,这与clang-tidy无关,而是与clang编译有关
推荐阅读
- laravel - 数据透视表中与 fk 的多对多关系
- ruby-on-rails - Axios Post 200 响应后不返回数据
- java - Android:自下而上裁剪图像
- reactjs - 从用户当前位置计算里程
- java - 如何仅在.sendkeys(“密码”)之后制作.click()?
- c# - 在代码隐藏中访问 XAML 绑定
- c# - 从富文本块中的代码生成的超链接导航到新框架
- aws-lambda - 如何在我的 lambda 代码中使用 AWS Custom Authorizer 中生成的权限?
- reactjs - 我如何在 React native 中创建倒计时功能?
- python - Python 2.7(套接字) - 如果在前一个端口上发生 socket.error,如何在下一个端口上继续