[cfe-commits] r172016 - in /cfe/trunk: include/clang/AST/Expr.h lib/AST/ExprConstant.cpp lib/Sema/SemaStmt.cpp test/Sema/switch-1.c

jahanian fjahanian at apple.com
Mon Jan 14 12:23:06 PST 2013


On Jan 11, 2013, at 2:55 PM, Richard Smith <richard at metafoo.co.uk> wrote:

> There are two different kinds of overflow that can happen in a case expression:
> 
> 1) An overflow occurs while evaluating the case value:
> 
> switch (n)
>   case 123456 * 123456:
> 
> 2) An overflow occurs while converting the evaluated case value to the promoted type of the switch expression:
> 
> int n = /*...*/;
> switch (n)
>   case 1ULL << 45:
> 
> For situation (1), it doesn't seem pertinent that this is happening within a case expression; we should warn no matter where it happens; this is the focus of PR2729. (2) should already be being checked by the call to ConvertIntegerToTypeWarnOnOverflow.

This Patch checks and warns for overflow.

	


> Yes, that would make sense to me, although we will need some mechanism to suppress the warning in the case where the overflow produces an error (as happens in constant expressions in C++11), and we'd need to avoid repeatedly evaluating subexpressions.

I am afraid I don't see how  this warning can be avoided in case of constant expressions in C++11. Expression evaluations are disjoint from declarations specs.
For example, clang already issues this warning (and error for constant expressions ) in the case of left-shift operations. 
constexpr int ll = (2147483647 << 2);

Can't we have the additional warning? Please advise.

- Fariborz



>  
>>> Also, I changed the warning per your other comments.
>>> In r172102.
>> 
>> Thanks! The "results in a new value" wording seems a bit awkward, how
>> about just saying "overflow in case constant expression results in
>> value %0"?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130114/64944d5a/attachment.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: patch-intoverflow.txt
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130114/64944d5a/attachment.txt>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130114/64944d5a/attachment-0001.html>


More information about the cfe-commits mailing list