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

Yaron Keren yaron.keren at gmail.com
Sat Aug 24 03:51:05 PDT 2013


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130824/b2629685/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: clang_integer_division_warning.diff
Type: application/octet-stream
Size: 1751 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130824/b2629685/attachment.obj>


More information about the llvm-commits mailing list