[cfe-dev] Clang tautological-compare fails to warn in cases where gcc doesn't

Maurizio Vitale via cfe-dev cfe-dev at lists.llvm.org
Thu Apr 12 08:35:30 PDT 2018


I suspect that clang cannot satisfy itself that the x->a cannot be changed
by somebody else between the evaluation of the lhs and the rhs of the
comparison.
All it knows is that the function cmp cannot modify it.
I'd say clang is correct, but I haven't studied the problem much.

Maurizio

On Thu, Apr 12, 2018 at 10:39 AM, Riyaz Puthiyapurayil via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> Here is a simple test where clang fails to issue a warning in the first
> function but warns in the second function. gcc (6.x and above) gives a
> warning in both cases. Is the pointer dereference throwing clang off? Why
> should the pointer dereference matter in this case?
>
>
>
> struct Foo {
>
>     int a;
>
> };
>
>
>
> // Case 1
>
> int cmp(const Foo* x, const Foo* y);
>
> int cmp(const Foo* x, const Foo* y)
>
> {
>
>     if (x->a < x->a) {
>
>         return -1;
>
>     } else if (y->a > y->a) {
>
>         return 1;
>
>     }
>
>     return 0;
>
> }
>
>
>
> // Case 2
>
> int cmp2(int x, int y);
>
> int cmp2(int x, int y)
>
> {
>
>     if (x < x) {
>
>         return -1;
>
>     } else if (y > y) {
>
>         return 1;
>
>     }
>
>     return 0;
>
> }
>
>
>
> // gcc 6.2 with -Wall
>
> x.cc: In function ?int cmp(const Foo*, const Foo*)?:
>
> x.cc:7:14: warning: self-comparison always evaluates to false
> [-Wtautological-compare]
>
>      if (x->a < x->a) {
>
>          ~~~~~^~~~~~
>
> x.cc:9:21: warning: self-comparison always evaluates to false
> [-Wtautological-compare]
>
>      } else if (y->a > y->a) {
>
>                 ~~~~~^~~~~~
>
> x.cc: In function ?int cmp2(int, int)?:
>
> x.cc:17:11: warning: self-comparison always evaluates to false
> [-Wtautological-compare]
>
>      if (x < x) {
>
>          ~~^~~
>
> x.cc:19:18: warning: self-comparison always evaluates to false
> [-Wtautological-compare]
>
>      } else if (y > y) {
>
>                 ~~^~~
>
>
>
> // clang 6.0.1 -Wall
>
> x.cc:17:11: warning: self-comparison always evaluates to false
> [-Wtautological-compare]
>
>     if (x < x) {
>
>           ^
>
> x.cc:19:18: warning: self-comparison always evaluates to false
> [-Wtautological-compare]
>
>     } else if (y > y) {
>
>                  ^
>
> 2 warnings generated.
>
>
>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180412/ac1d51f4/attachment.html>


More information about the cfe-dev mailing list