<div dir="ltr">ping<div class="gmail_extra"><br><div class="gmail_quote">On Mon, Nov 30, 2015 at 4:30 PM, Akira Hatanaka <span dir="ltr"><<a href="mailto:ahatanak@gmail.com" target="_blank">ahatanak@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">ahatanak created this revision.<br>
ahatanak added a subscriber: cfe-commits.<br>
<br>
Issue a warning if an initializing integer expression overflows.<br>
<br>
For example, clang should issue a warning when compiling the following code because 4 * 1024 * 1024 * 1024 doesn't fit into a 32-bit integer:<br>
<br>
struct s {<br>
unsigned x;<br>
} s = {<br>
 .x = 4 * 1024 * 1024 * 1024<br>
};<br>
<br>
<a href="http://reviews.llvm.org/D15097" rel="noreferrer" target="_blank">http://reviews.llvm.org/D15097</a><br>
<br>
Files:<br>
  lib/Sema/SemaChecking.cpp<br>
  test/Sema/integer-overflow.c<br>
<br>
Index: test/Sema/integer-overflow.c<br>
===================================================================<br>
--- test/Sema/integer-overflow.c<br>
+++ test/Sema/integer-overflow.c<br>
@@ -145,3 +145,11 @@<br>
 // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}<br>
   return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024)));<br>
 }<br>
+<br>
+struct s {<br>
+  unsigned x;<br>
+  unsigned y;<br>
+} s = {<br>
+  .y = 5,<br>
+  .x = 4 * 1024 * 1024 * 1024  // expected-warning {{overflow in expression; result is 0 with type 'int'}}<br>
+};<br>
Index: lib/Sema/SemaChecking.cpp<br>
===================================================================<br>
--- lib/Sema/SemaChecking.cpp<br>
+++ lib/Sema/SemaChecking.cpp<br>
@@ -7817,6 +7817,10 @@<br>
 void Sema::CheckForIntOverflow (Expr *E) {<br>
   if (isa<BinaryOperator>(E->IgnoreParenCasts()))<br>
     E->IgnoreParenCasts()->EvaluateForOverflow(Context);<br>
+  else if (auto InitList = dyn_cast<InitListExpr>(E))<br>
+    for (Expr *E : InitList->inits())<br>
+      if (isa<BinaryOperator>(E->IgnoreParenCasts()))<br>
+        E->IgnoreParenCasts()->EvaluateForOverflow(Context);<br>
 }<br>
<br>
 namespace {<br>
<br>
<br>
</blockquote></div><br></div></div>