<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">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><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="m_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_3575411032359691794Code">struct Foo {<u></u><u></u></p>
<p class="m_3575411032359691794Code"> int a;<u></u><u></u></p>
<p class="m_3575411032359691794Code">};<u></u><u></u></p>
<p class="m_3575411032359691794Code"><u></u> <u></u></p>
<p class="m_3575411032359691794Code">// Case 1<u></u><u></u></p>
<p class="m_3575411032359691794Code">int cmp(const Foo* x, const Foo* y);<u></u><u></u></p>
<p class="m_3575411032359691794Code">int cmp(const Foo* x, const Foo* y)<u></u><u></u></p>
<p class="m_3575411032359691794Code">{<u></u><u></u></p>
<p class="m_3575411032359691794Code"> if (x->a < x->a) {<u></u><u></u></p>
<p class="m_3575411032359691794Code"> return -1;<u></u><u></u></p>
<p class="m_3575411032359691794Code"> } else if (y->a > y->a) {<u></u><u></u></p>
<p class="m_3575411032359691794Code"> return 1;<u></u><u></u></p>
<p class="m_3575411032359691794Code"> }<u></u><u></u></p>
<p class="m_3575411032359691794Code"> return 0;<u></u><u></u></p>
<p class="m_3575411032359691794Code">}<u></u><u></u></p>
<p class="m_3575411032359691794Code"><u></u> <u></u></p>
<p class="m_3575411032359691794Code">// Case 2<u></u><u></u></p>
<p class="m_3575411032359691794Code">int cmp2(int x, int y);<u></u><u></u></p>
<p class="m_3575411032359691794Code">int cmp2(int x, int y)<u></u><u></u></p>
<p class="m_3575411032359691794Code">{<u></u><u></u></p>
<p class="m_3575411032359691794Code"> if (x < x) {<u></u><u></u></p>
<p class="m_3575411032359691794Code"> return -1;<u></u><u></u></p>
<p class="m_3575411032359691794Code"> } else if (y > y) {<u></u><u></u></p>
<p class="m_3575411032359691794Code"> return 1;<u></u><u></u></p>
<p class="m_3575411032359691794Code"> }<u></u><u></u></p>
<p class="m_3575411032359691794Code"> return 0;<u></u><u></u></p>
<p class="m_3575411032359691794Code">}<u></u><u></u></p>
<p class="m_3575411032359691794Code"><u></u> <u></u></p>
<p class="m_3575411032359691794Code">// gcc 6.2 with -Wall<u></u><u></u></p>
<p class="m_3575411032359691794Code">x.cc: In function ?int cmp(const Foo*, const Foo*)?:<u></u><u></u></p>
<p class="m_3575411032359691794Code">x.cc:7:14: warning: self-comparison always evaluates to false [-Wtautological-compare]<u></u><u></u></p>
<p class="m_3575411032359691794Code"> if (x->a < x->a) {<u></u><u></u></p>
<p class="m_3575411032359691794Code"> ~~~~~^~~~~~<u></u><u></u></p>
<p class="m_3575411032359691794Code">x.cc:9:21: warning: self-comparison always evaluates to false [-Wtautological-compare]<u></u><u></u></p>
<p class="m_3575411032359691794Code"> } else if (y->a > y->a) {<u></u><u></u></p>
<p class="m_3575411032359691794Code"> ~~~~~^~~~~~<u></u><u></u></p>
<p class="m_3575411032359691794Code">x.cc: In function ?int cmp2(int, int)?:<u></u><u></u></p>
<p class="m_3575411032359691794Code">x.cc:17:11: warning: self-comparison always evaluates to false [-Wtautological-compare]<u></u><u></u></p>
<p class="m_3575411032359691794Code"> if (x < x) {<u></u><u></u></p>
<p class="m_3575411032359691794Code"> ~~^~~<u></u><u></u></p>
<p class="m_3575411032359691794Code">x.cc:19:18: warning: self-comparison always evaluates to false [-Wtautological-compare]<u></u><u></u></p>
<p class="m_3575411032359691794Code"> } else if (y > y) {<u></u><u></u></p>
<p class="m_3575411032359691794Code"> ~~^~~<u></u><u></u></p>
<p class="m_3575411032359691794Code"><u></u> <u></u></p>
<p class="m_3575411032359691794Code">// clang 6.0.1 -Wall<u></u><u></u></p>
<p class="m_3575411032359691794Code">x.cc:17:11: warning: self-comparison always evaluates to false [-Wtautological-compare]<u></u><u></u></p>
<p class="m_3575411032359691794Code"> if (x < x) {<u></u><u></u></p>
<p class="m_3575411032359691794Code"> ^<u></u><u></u></p>
<p class="m_3575411032359691794Code">x.cc:19:18: warning: self-comparison always evaluates to false [-Wtautological-compare]<u></u><u></u></p>
<p class="m_3575411032359691794Code"> } else if (y > y) {<u></u><u></u></p>
<p class="m_3575411032359691794Code"> ^<u></u><u></u></p>
<p class="m_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>______________________________<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>