<div dir="ltr">Friendly ping.</div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/6/22 Serge Pavlov <span dir="ltr"><<a href="mailto:sepavloff@gmail.com" target="_blank">sepavloff@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">  Updated patch.<br>
<div class="im"><br>
Hi rsmith,<br>
<br>
<a href="http://llvm-reviews.chandlerc.com/D637" target="_blank">http://llvm-reviews.chandlerc.com/D637</a><br>
<br>
CHANGE SINCE LAST DIFF<br>
</div>  <a href="http://llvm-reviews.chandlerc.com/D637?vs=2518&id=2538#toc" target="_blank">http://llvm-reviews.chandlerc.com/D637?vs=2518&id=2538#toc</a><br>
<br>
Files:<br>
<div class="im">  include/clang/Basic/DiagnosticSemaKinds.td<br>
  lib/AST/ExprConstant.cpp<br>
  lib/Sema/SemaExpr.cpp<br>
  test/Sema/empty1.c<br>
  test/Sema/empty1.cpp<br>
<br>
</div><div class="im">Index: include/clang/Basic/DiagnosticSemaKinds.td<br>
===================================================================<br>
--- include/clang/Basic/DiagnosticSemaKinds.td<br>
+++ include/clang/Basic/DiagnosticSemaKinds.td<br>
</div><div class="im">@@ -4113,6 +4113,9 @@<br>
 def warn_offsetof_non_standardlayout_type : ExtWarn<<br>
   "offset of on non-standard-layout type %0">, InGroup<InvalidOffsetof>;<br>
 def err_offsetof_bitfield : Error<"cannot compute offset of bit-field %0">;<br>
+def warn_sub_ptr_zero_size_types : Warning<<br>
+  "subtraction of pointers to type %0 of zero size has undefined behavior">,<br>
+  InGroup<PointerArith>;<br>
<br>
 def warn_floatingpoint_eq : Warning<<br>
   "comparing floating point with == or != is unsafe">,<br>
Index: lib/AST/ExprConstant.cpp<br>
===================================================================<br>
--- lib/AST/ExprConstant.cpp<br>
+++ lib/AST/ExprConstant.cpp<br>
@@ -6405,6 +6405,14 @@<br>
         if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize))<br>
           return false;<br>
<br>
+        // As an extension, a type may have zero size (empty struct or union in<br>
+        // C, array of zero length).  Meaning of pointer difference in such<br>
+        // case is unspecified.<br>
+        if (ElementSize.isZero()) {<br>
</div>+          Result = APValue(Info.Ctx.MakeIntValue(0, E->getType()));<br>
+          return true;<br>
<div class="im">+        }<br>
+<br>
         // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime,<br>
         // and produce incorrect results when it overflows. Such behavior<br>
         // appears to be non-conforming, but is common, so perhaps we should<br>
</div><div class="im">Index: lib/Sema/SemaExpr.cpp<br>
===================================================================<br>
--- lib/Sema/SemaExpr.cpp<br>
+++ lib/Sema/SemaExpr.cpp<br>
</div>@@ -6870,6 +6870,18 @@<br>
<div class="im">                                                LHS.get(), RHS.get()))<br>
         return QualType();<br>
<br>
+      // The pointee type may have zero size.  As an extension, a structure or<br>
+      // union may have zero size or an array may have zero length.  In this<br>
+      // case subtraction does not make sense.<br>
+      if (!rpointee->isVoidType() && !rpointee->isFunctionType()) {<br>
</div><div class="im">+        CharUnits ElementSize = Context.getTypeSizeInChars(rpointee);<br>
</div><div class="im">+        if (ElementSize.isZero()) {<br>
+          Diag(Loc,diag::warn_sub_ptr_zero_size_types)<br>
+            << rpointee.getUnqualifiedType()<br>
</div><div class="im">+            << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();<br>
+        }<br>
</div><div class="im">+      }<br>
+<br>
       if (CompLHSTy) *CompLHSTy = LHS.get()->getType();<br>
       return Context.getPointerDiffType();<br>
     }<br>
Index: test/Sema/empty1.c<br>
===================================================================<br>
--- test/Sema/empty1.c<br>
+++ test/Sema/empty1.c<br>
@@ -36,3 +36,50 @@<br>
   struct emp_1 f1;<br>
   union emp_2 f2;<br>
 };<br>
+<br>
+<br>
+// Checks for pointer subtraction (PR15683)<br>
+<br>
</div>+struct emp_1* func_1p (struct emp_1* x) {<br>
<div class="im">+  return x - 5;<br>
+}<br>
+<br>
+int func_1 () {<br>
</div>+  struct emp_1 v[1];<br>
+  return v - v;  // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}}<br>
+}<br>
+<br>
+int func_2 (struct emp_1* x) {<br>
+  return 1 + x - x;  // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}}<br>
+}<br>
+<br>
+int func_3 (struct emp_1* x, struct emp_1* y) {<br>
+  return x - y;  // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}}<br>
+}<br>
+<br>
+int func_4 (struct emp_1* x, const struct emp_1* y) {<br>
+  return x - y;  // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}}<br>
+}<br>
+<br>
+int func_5 (volatile struct emp_1* x, const struct emp_1* y) {<br>
+  return x - y;  // expected-warning {{subtraction of pointers to type 'struct emp_1' of zero size has undefined behavior}}<br>
+}<br>
+<br>
+int func_6 () {<br>
+  union emp_2 v[1];<br>
+  return v - v;  // expected-warning {{subtraction of pointers to type 'union emp_2' of zero size has undefined behavior}}<br>
+}<br>
<div class="im">+<br>
+struct A;  // expected-note {{forward declaration of 'struct A'}}<br>
+<br>
+int func_7 (struct A* x, struct A* y) {<br>
+  return x - y;  // expected-error {{arithmetic on a pointer to an incomplete type 'struct A'}}<br>
+}<br>
+<br>
+int func_8 (struct emp_1 (*x)[10], struct emp_1 (*y)[10]) {<br>
</div>+  return x - y;  // expected-warning {{subtraction of pointers to type 'struct emp_1 [10]' of zero size has undefined behavior}}<br>
+}<br>
<div class="im">+<br>
+int func_9 (struct emp_1 (*x)[], struct emp_1 (*y)[]) {<br>
+  return x - y;  // expected-error {{arithmetic on a pointer to an incomplete type 'struct emp_1 []'}}<br>
+}<br>
Index: test/Sema/empty1.cpp<br>
===================================================================<br>
--- /dev/null<br>
+++ test/Sema/empty1.cpp<br>
</div>@@ -0,0 +1,17 @@<br>
+// RUN: %clang_cc1 %s -fsyntax-only -std=c++11 -verify<br>
<div class="im">+<br>
+int func_1(int (*p1)[0], int (*p2)[0]) {<br>
+  return p1 - p2;  // expected-warning {{subtraction of pointers to type 'int [0]' of zero size has undefined behavior}}<br>
+}<br>
</div>+<br>
+constexpr int (*p1)[0] = 0, (*p2)[0] = 0;<br>
+constexpr int k = p2 - p1;  // expected-warning {{subtraction of pointers to type 'int [0]' of zero size has undefined behavior}}<br>
+<br>
+constexpr int func_2(int (*x1)[0], int (*x2)[0]) {<br>
<div class="im">+  return x1 - x2;  // expected-warning {{subtraction of pointers to type 'int [0]' of zero size has undefined behavior}}<br>
+}<br>
</div>+<br>
+constexpr int func_3(int (*x1)[0], long (*x2)[0]) {<br>
+  return x1 - x2;  // expected-error {{int (*)[0]' and 'long (*)[0]' are not pointers to compatible types}} \<br>
+                   // expected-warning {{of zero size has undefined behavior}}<br>
+}<br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Thanks,<br>--Serge<br>
</div>