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

Matthew Del Buono via cfe-dev cfe-dev at lists.llvm.org
Thu Apr 12 09:29:17 PDT 2018


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> 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> 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
>>
>>
>
> _______________________________________________
> 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/2927e6bd/attachment.html>


More information about the cfe-dev mailing list