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

Riyaz Puthiyapurayil via cfe-dev cfe-dev at lists.llvm.org
Thu Apr 12 12:20:55 PDT 2018


I agree. Clang should warn in this case. If 'const' is changed to 'volatile', then gcc doesn't warn either (as expected).



I will file a bug.



/Riyaz

From: Matthew Del Buono [mailto:mpdelbuono at gmail.com]
Sent: Thursday, April 12, 2018 9:29 AM
To: Maurizio Vitale <mav at uber.com>
Cc: Riyaz Puthiyapurayil <Riyaz.Puthiyapurayil at synopsys.com>; cfe-dev at lists.llvm.org
Subject: Re: [cfe-dev] Clang tautological-compare fails to warn in cases where gcc doesn't

It's not volatile, so it seems inappropriate for clang to think that it could be modified in between. In fact, at -O1, the comparisons are optimized out anyway, so it's clear that the warning should be in place to tell developers that this comparison is not going to be performed:

cmp(Foo const*, Foo const*): # @cmp(Foo const*, Foo const*)
xor eax, eax
ret

(clang 6.0.0)

On Thu, Apr 12, 2018 at 8:35 AM, Maurizio Vitale via cfe-dev <cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>> wrote:
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<mailto: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<mailto:cfe-dev at lists.llvm.org>
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev<https://urldefense.proofpoint.com/v2/url?u=http-3A__lists.llvm.org_cgi-2Dbin_mailman_listinfo_cfe-2Ddev&d=DwMFaQ&c=DPL6_X_6JkXFx7AXWqB0tg&r=mMZWMrEZcvPMLSsEQSah9FOTwza1UudSDkAneN47U9lD3qu6gt3kpnIb4MWV77cM&m=AQn-peRFphfxCRAi2gdLzc1TRpzAeWxKBR6gbFaKHNg&s=03282vflfI1Ee2bIGudRge2mczWrBmfzXyi2t8hxBqI&e=>


_______________________________________________
cfe-dev mailing list
cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev<https://urldefense.proofpoint.com/v2/url?u=http-3A__lists.llvm.org_cgi-2Dbin_mailman_listinfo_cfe-2Ddev&d=DwMFaQ&c=DPL6_X_6JkXFx7AXWqB0tg&r=mMZWMrEZcvPMLSsEQSah9FOTwza1UudSDkAneN47U9lD3qu6gt3kpnIb4MWV77cM&m=AQn-peRFphfxCRAi2gdLzc1TRpzAeWxKBR6gbFaKHNg&s=03282vflfI1Ee2bIGudRge2mczWrBmfzXyi2t8hxBqI&e=>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180412/cc29e4ce/attachment.html>


More information about the cfe-dev mailing list