[PATCH] clang warning when division of two integer literals loses precision

Matt Arsenault arsenm2 at gmail.com
Sat Aug 24 10:14:07 PDT 2013


On Aug 24, 2013, at 3:51 , Yaron Keren <yaron.keren at gmail.com> wrote:

> Hello,
> 
> Attached is a patch I made to make clang warn where division of two integer literals loses precision. For instance:
> 
> double x = 1/2;
> 
> Which results in x being 0. This is not the programmer's intention. 
> While experienced users will know to code 1.0/2 to force floating division, new C/C++ programmers will fall in this trap.
> 
> The patch computes the reminder in the division operation and if only if it is non-zero, warns and suggests a fix.
> 
> This is the first code I write for LLVM so please review it carefully. Specifically, I was not sure whether to create another warning group in DiagnosticSemaKinds.td or reuse an existing one. Maybe the group should be renamed from "DivZero" to "DivProblems".
> 
> Yaron
> 
> <clang_integer_division_warning.diff>_______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits

Hi,

Thanks for this! I've fixed this kind of bug a good number of times and wished there was a warning for it. This belongs on cfe-commits, and needs a test.

Thanks


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130824/4e5340bf/attachment.html>


More information about the llvm-commits mailing list