This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] [C] Warn when calculating abs(unsigned_value)

On Tue, 14 Aug 2018, Martin Jambor wrote:

> when you try compiling a call to function abs and provide an unsigned
> int in the argument in C++, you will get an error about ambiguous
> overload.  In C however, it will pass without silently.  The following
> patch adds a warning for these cases, because I think it is likely that
> such code does not do what the author intended.

abs of unsigned short (promoted to int) seems harmless; I don't see any 
tests to make sure it doesn't warn.  Really the issue seems more like abs 
(or labs / llabs / imaxabs) of an argument whose value might be changed by 
the conversion to int.  Except that's more like a subset of -Wconversion, 
and highly likely to have false positives (for a long argument that is 
actually always in the range of int), so maybe a restriction to unsigned 
arguments makes sense for this warning, but should still only be for 
unsigned arguments at least as wide as the argument type of the abs 
function in question.

> +	      else if (DECL_FUNCTION_CODE (expr.value) == BUILT_IN_ABS

This looks like it would only handle abs, not labs / llabs / imaxabs.

> +@code{<} or @code{>=}.  When compiling C, also warn when calculating
> +an absolute value from an unsigned type.  This warning is also enabled

But this would suggest any absolute value function, not just abs.

Joseph S. Myers

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]