[PATCH] Warn on suspicious use of absolute value function

Jay Foad jay.foad at gmail.com
Tue Oct 7 03:47:55 PDT 2014


Hi Richard,

I get:

$ cat a.c
#include <stdlib.h>
int f(unsigned x, unsigned y) { return abs(x - y); }
$ ./clang -fsyntax-only a.c
a.c:2:40: warning: taking the absolute value of unsigned type
'unsigned int' has no effect [-Wabsolute-value]
int f(unsigned x, unsigned y) { return abs(x - y); }
[...]

Is it really right that the call to abs "has no effect"? Surely it
does have an effect: the unsigned value x-y is converted to int, and
if that value is negative then abs() will negate it.

Or have I misunderstood C?

Thanks,
Jay.

On 20 November 2013 02:08, Richard Trieu <rtrieu at google.com> wrote:
> This is a new warning to detect improper uses of the absolute value function.  Three new warnings are introduced with this patch.
>
> 1) Attempting to take the absolute value of an unsigned integer.  Includes fix-it to remove the absolute value function call.
> 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.
> 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.
>
> I have already run this over a large code base and found hundreds of questionable usages of absolute value.
>
> http://llvm-reviews.chandlerc.com/D2224
>
> Files:
>   test/Sema/warn-absolute-value.c
>   include/clang/Basic/DiagnosticSemaKinds.td
>   include/clang/Basic/DiagnosticGroups.td
>   include/clang/Sema/Sema.h
>   include/clang/AST/Decl.h
>   lib/Sema/SemaChecking.cpp
>   lib/AST/Decl.cpp
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>



More information about the cfe-commits mailing list