<div class="gmail_quote">On Mon, Apr 16, 2012 at 12:23 PM, Eli Friedman <span dir="ltr"><<a href="mailto:eli.friedman@gmail.com">eli.friedman@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Author: efriedma<br>
Date: Mon Apr 16 14:23:57 2012<br>
New Revision: 154849<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=154849&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=154849&view=rev</a><br>
Log:<br>
Per Richard's comments on r154794, add the checks necessary to handle constant-folding relational comparisons safely in case the user is using -fwrapv or equivalent.<br>
<br>
<br>
Modified:<br>
cfe/trunk/lib/AST/ExprConstant.cpp<br>
cfe/trunk/test/Sema/const-eval.c<br>
<br>
Modified: cfe/trunk/lib/AST/ExprConstant.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=154849&r1=154848&r2=154849&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=154849&r1=154848&r2=154849&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)<br>
+++ cfe/trunk/lib/AST/ExprConstant.cpp Mon Apr 16 14:23:57 2012<br>
@@ -5090,8 +5090,6 @@<br>
<br>
// The comparison here must be unsigned, and performed with the same<br>
// width as the pointer.<br>
- // FIXME: Knowing the base is the same for the LHS and RHS isn't enough<br>
- // for relational operators.<br>
unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy);<br>
uint64_t CompareLHS = LHSOffset.getQuantity();<br>
uint64_t CompareRHS = RHSOffset.getQuantity();<br>
@@ -5100,6 +5098,19 @@<br>
CompareLHS &= Mask;<br>
CompareRHS &= Mask;<br>
<br>
+ // If there is a base and this is a relational operator, we can only<br>
+ // compare pointers within the object in question; otherwise, the result<br>
+ // depends on where the object is located in memory.<br>
+ if (!LHSValue.Base.isNull() && E->isRelationalOp()) {<br>
+ QualType BaseTy = getType(LHSValue.Base);<br>
+ if (BaseTy->isIncompleteType())<br>
+ return Error(E);<br></blockquote><div><br></div><div>Can we treat incomplete types as having size 0? AFAICS this is legal in C++11:</div><div><br></div><div>struct S a;</div><div>constexpr bool b = &a < &a;</div>
</div>