<div dir="ltr">On Mon, Aug 19, 2013 at 9:10 AM, Serge Pavlov <span dir="ltr"><<a href="mailto:sepavloff@gmail.com" target="_blank">sepavloff@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Friendly ping.</div><div class="gmail_extra"><div><div class="h5"><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><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> 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>Index: include/clang/Basic/DiagnosticSemaKinds.td<br>
===================================================================<br>
--- include/clang/Basic/DiagnosticSemaKinds.td<br>
+++ include/clang/Basic/DiagnosticSemaKinds.td<br>
</div><div>@@ -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>+ }<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>Index: lib/Sema/SemaExpr.cpp<br>
===================================================================<br>
--- lib/Sema/SemaExpr.cpp<br>
+++ lib/Sema/SemaExpr.cpp<br>
</div>@@ -6870,6 +6870,18 @@<br>
<div> 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>+ CharUnits ElementSize = Context.getTypeSizeInChars(rpointee);<br>
</div><div>+ if (ElementSize.isZero()) {<br>
+ Diag(Loc,diag::warn_sub_ptr_zero_size_types)<br>
+ << rpointee.getUnqualifiedType()<br>
</div><div>+ << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();<br>
+ }<br>
</div><div>+ }<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>+ 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>+<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>+<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>
</div><div><br></div></blockquote></div></div></div></div></blockquote><div><br></div><div>C++-specific tests should go into test/SemaCXX/</div><div><br></div><div>Otherwise looks fine, but please let Richard take another look at the ExprConstant.cpp changes first.</div>
<div><br></div><div>-Eli </div></div></div></div>