<div dir="ltr">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:<div><br></div><div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254)"><div><span style="color:rgb(0,128,128)">cmp(Foo const*, Foo const*):</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,128,0)"># @cmp(Foo const*, Foo const*)</span></div><div><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,255)">xor</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(72,100,170)">eax</span><span style="color:rgb(0,0,0)">, </span><span style="color:rgb(72,100,170)">eax</span></div><div><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,255)">ret</span></div></div><br></div><div>(clang 6.0.0)</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Apr 12, 2018 at 8:35 AM, Maurizio Vitale via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">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.<div>All it knows is that the function cmp cannot modify it.<br>I'd say clang is correct, but I haven't studied the problem much.</div><div><br></div><div>Maurizio</div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Thu, Apr 12, 2018 at 10:39 AM, Riyaz Puthiyapurayil via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">
<div lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="m_-2865762710056028528m_3575411032359691794WordSection1">
<p class="MsoNormal">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? <u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<div style="border:solid #f2f2f2 1.0pt;padding:1.0pt 4.0pt 1.0pt 4.0pt">
<p class="m_-2865762710056028528m_3575411032359691794Code">struct Foo {<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> int a;<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">};<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"><u></u> <u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">// Case 1<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">int cmp(const Foo* x, const Foo* y);<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">int cmp(const Foo* x, const Foo* y)<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">{<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> if (x->a < x->a) {<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> return -1;<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> } else if (y->a > y->a) {<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> return 1;<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> }<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> return 0;<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">}<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"><u></u> <u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">// Case 2<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">int cmp2(int x, int y);<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">int cmp2(int x, int y)<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">{<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> if (x < x) {<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> return -1;<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> } else if (y > y) {<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> return 1;<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> }<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> return 0;<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">}<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"><u></u> <u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">// gcc 6.2 with -Wall<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">x.cc: In function ?int cmp(const Foo*, const Foo*)?:<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">x.cc:7:14: warning: self-comparison always evaluates to false [-Wtautological-compare]<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> if (x->a < x->a) {<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> ~~~~~^~~~~~<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">x.cc:9:21: warning: self-comparison always evaluates to false [-Wtautological-compare]<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> } else if (y->a > y->a) {<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> ~~~~~^~~~~~<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">x.cc: In function ?int cmp2(int, int)?:<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">x.cc:17:11: warning: self-comparison always evaluates to false [-Wtautological-compare]<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> if (x < x) {<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> ~~^~~<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">x.cc:19:18: warning: self-comparison always evaluates to false [-Wtautological-compare]<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> } else if (y > y) {<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> ~~^~~<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"><u></u> <u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">// clang 6.0.1 -Wall<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">x.cc:17:11: warning: self-comparison always evaluates to false [-Wtautological-compare]<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> if (x < x) {<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> ^<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">x.cc:19:18: warning: self-comparison always evaluates to false [-Wtautological-compare]<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> } else if (y > y) {<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code"> ^<u></u><u></u></p>
<p class="m_-2865762710056028528m_3575411032359691794Code">2 warnings generated.<u></u><u></u></p>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
<br></div></div>______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div>
<br>______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div>