首页 > 解决方案 > 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

标签: llvmanti-patternsclang-tidy

解决方案


您正在使用尚未发布的 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编译有关


推荐阅读