<div dir="ltr">Hi Jay,<div><br></div><div><div class="gmail_extra">This warning makes a few assumptions about your code:</div><div class="gmail_extra">1) Using an unsigned value means you only want non-negative values</div><div class="gmail_extra">2) You expect the absolute value function to be as perfect as possible</div><div class="gmail_extra"><br></div><div class="gmail_extra">With both assumptions, unsigned values are already non-negative, so the best absolute value function would simply return the input value. Thus, Clang suggests removing the call to abs().</div><div class="gmail_extra"><br></div><div class="gmail_extra">From your example, since unsigned and int are the same size, conversion between unsigned and int will give you the correct difference if the difference is less than INT_MAX. Above that and you have wrapping issues. The best way to get the difference is:</div><div class="gmail_extra"><br></div><div class="gmail_extra">unsigned f(unsigned a, unsigned b) {</div><div class="gmail_extra"> if (a > b)</div><div class="gmail_extra"> return a - b;</div><div class="gmail_extra"> else</div><div class="gmail_extra"> return b - a;</div><div class="gmail_extra">}</div><div class="gmail_extra"><br></div><div class="gmail_extra">This works for any pair of unsigned values.</div><div class="gmail_extra"><br></div><div class="gmail_extra">Richard</div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Oct 7, 2014 at 3:47 AM, Jay Foad <span dir="ltr"><<a href="mailto:jay.foad@gmail.com" target="_blank">jay.foad@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Richard,<br>
<br>
I get:<br>
<br>
$ cat a.c<br>
#include <stdlib.h><br>
int f(unsigned x, unsigned y) { return abs(x - y); }<br>
$ ./clang -fsyntax-only a.c<br>
a.c:2:40: warning: taking the absolute value of unsigned type<br>
'unsigned int' has no effect [-Wabsolute-value]<br>
int f(unsigned x, unsigned y) { return abs(x - y); }<br>
[...]<br>
<br>
Is it really right that the call to abs "has no effect"? Surely it<br>
does have an effect: the unsigned value x-y is converted to int, and<br>
if that value is negative then abs() will negate it.<br>
<br>
Or have I misunderstood C?<br>
<br>
Thanks,<br>
Jay.<br>
<div><div class="h5"><br>
On 20 November 2013 02:08, Richard Trieu <<a href="mailto:rtrieu@google.com">rtrieu@google.com</a>> wrote:<br>
> This is a new warning to detect improper uses of the absolute value function. Three new warnings are introduced with this patch.<br>
><br>
> 1) Attempting to take the absolute value of an unsigned integer. Includes fix-it to remove the absolute value function call.<br>
> 2) Using an absolute value function that is too small. For instance, using abs(int) on a long would give a warning and a fix-it to use labs.<br>
> 3) Using the wrong type of absolute value (integer, floating point, complex) for the argument given. Includes a fix-it to the proper absolute value type.<br>
><br>
> I have already run this over a large code base and found hundreds of questionable usages of absolute value.<br>
><br>
> <a href="http://llvm-reviews.chandlerc.com/D2224" target="_blank">http://llvm-reviews.chandlerc.com/D2224</a><br>
><br>
> Files:<br>
> test/Sema/warn-absolute-value.c<br>
> include/clang/Basic/DiagnosticSemaKinds.td<br>
> include/clang/Basic/DiagnosticGroups.td<br>
> include/clang/Sema/Sema.h<br>
> include/clang/AST/Decl.h<br>
> lib/Sema/SemaChecking.cpp<br>
> lib/AST/Decl.cpp<br>
><br>
</div></div>> _______________________________________________<br>
> cfe-commits mailing list<br>
> <a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
><br>
</blockquote></div><br></div></div></div>