1

I'm experimenting with clang-tidy using the following file:

#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;
}

I aim to detect the infinite loop with:

$ clang-tidy -checks=bugprone-infinite-loop main.c

but all that clang-tidy finds is the = instead of == thing:

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

OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
  • I initially thought the error was missed because of the if, which makes the loop unreachable, but my version of clang tidy fails to find the problem with the if fixed as well. – PaulR Feb 02 '20 at 12:32

1 Answers1

1

You are using a feature from a yet unreleased version of LLVM (10.0.0).

On my system (windows) your file works as expected:

>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++; }
^

The only modification I did on the file is removing the unecessary #include. I also added -- (double dash) to the command to get rid of missing compilation database errors. I am using a prebuilt binary from https://llvm.org/builds/

My best guess here is that you are using an older build of clang-tidy where this is not detected. For reference my version is 10.0.0-e20a1e486e1, you can see yours by running:

>clang-tidy --version

I'd also check if you are actually running the check(s) you are expected to run via:

$ clang-tidy -checks=bugprone-infinite-loop main.c --list-checks

P.S. The warning message you got at first was clang-diagnostic based, this has nothing to do with clang-tidy but rather with clang compilation

pablo285
  • 2,460
  • 4
  • 14
  • 38