<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Dec 11, 2014 at 11:47 AM, David Majnemer <span dir="ltr"><<a href="mailto:david.majnemer@gmail.com" target="_blank">david.majnemer@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div class="h5">On Thu, Dec 11, 2014 at 11:28 AM, Richard Smith <span dir="ltr"><<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div>On Tue, Dec 9, 2014 at 3:32 PM, David Majnemer <span dir="ltr"><<a href="mailto:david.majnemer@gmail.com" target="_blank">david.majnemer@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Author: majnemer<br>
Date: Tue Dec 9 17:32:34 2014<br>
New Revision: 223852<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=223852&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=223852&view=rev</a><br>
Log:<br>
AST: Don't assume two zero sized objects live at different addresses<br>
<br>
Zero sized objects may overlap with each other or any other object.<br>
<br>
This fixes PR21786.<br>
<br>
Modified:<br>
cfe/trunk/lib/AST/ExprConstant.cpp<br>
cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp<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=223852&r1=223851&r2=223852&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=223852&r1=223851&r2=223852&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)<br>
+++ cfe/trunk/lib/AST/ExprConstant.cpp Tue Dec 9 17:32:34 2014<br>
@@ -1422,6 +1422,12 @@ static bool IsWeakLValue(const LValue &V<br>
return Decl && Decl->isWeak();<br>
}<br>
<br>
+static bool isZeroSized(const LValue &Value) {<br>
+ const ValueDecl *Decl = GetLValueBaseDecl(Value);<br>
+ return Decl && isa<VarDecl>(Decl) &&<br>
+ Decl->getASTContext().getTypeSize(Decl->getType()) == 0;<br>
+}<br>
+<br>
static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) {<br>
// A null base expression indicates a null pointer. These are always<br>
// evaluatable, and they are false unless the offset is zero.<br>
@@ -6979,6 +6985,10 @@ bool IntExprEvaluator::VisitBinaryOperat<br>
(RHSValue.Base && RHSValue.Offset.isZero() &&<br>
isOnePastTheEndOfCompleteObject(Info.Ctx, LHSValue)))<br>
return Error(E);<br>
+ // We can't tell whether an object is at the same address as another<br>
+ // zero sized object.<br>
+ if (isZeroSized(LHSValue) || isZeroSized(RHSValue))<br>
+ return Error(E);<br></blockquote><div><br></div></div></div><div>We can do better here: one of the pointers must be to a zero-sized object, and the other must be a past-the-end pointer (where a pointer to a zero-sized object is considered to be a past-the-end pointer).</div></div></div></div></blockquote><div><br></div></div></div><div>Ah, clever.</div></div></div></div></blockquote><div><br></div><div>Actually, must the other be a past-the-end pointer?</div><div><br></div><div><div>#include <stdio.h></div><div>extern "C" void (*__preinit_array_start[])();</div><div>void __attribute((section(".preinit_array"))) f() {}</div><div>int main() {</div><div> printf("%p\n", &__preinit_array_start[0]);</div><div> printf("%p\n", &f);</div><div>}</div></div><div><br></div><div>gives me:</div><div><div>0x600de0</div><div>0x600de0</div></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class=""><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
// Pointers with different bases cannot represent the same object.<br>
// (Note that clang defaults to -fmerge-all-constants, which can<br>
// lead to inconsistent results for comparisons involving the address<br>
<br>
Modified: cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp?rev=223852&r1=223851&r2=223852&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp?rev=223852&r1=223851&r2=223852&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp (original)<br>
+++ cfe/trunk/test/SemaCXX/constant-expression-cxx11.cpp Tue Dec 9 17:32:34 2014<br>
@@ -1955,3 +1955,9 @@ namespace EmptyClass {<br>
constexpr E2 e2b(e2); // expected-error {{constant expression}} expected-note{{read of non-const}} expected-note {{in call}}<br>
constexpr E3 e3b(e3);<br>
}<br>
+<br>
+namespace PR21786 {<br>
+ extern void (*start[])();<br>
+ extern void (*end[])();<br>
+ static_assert(&start != &end, ""); // expected-error {{constant expression}}<br>
+}<br></blockquote><div><br></div></span><div>This testcase looks like valid C++ code to me; the comparison is a constant expression under the C++ rules and evaluates to true. I don't think we can apply this check in this case, only when we have a complete type that is zero-sized. That means we'll constant-fold equality comparisons to 'false' even if they turn out to be true, but that seems to be unavoidable.</div></div></div></div>
</blockquote></span></div><br></div><div class="gmail_extra">I don't quite understand why we should fold that comparison to false, GCC and ICC both consider that expression to be non-constant.</div></div>
</blockquote></div><br></div></div>