<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Dec 11, 2014 at 1:12 PM, 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><div class="h5"><div class="gmail_extra"><div class="gmail_quote">On Thu, Dec 11, 2014 at 1:02 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"><div dir="ltr"><div><div><div class="gmail_extra"><div class="gmail_quote">On Thu, Dec 11, 2014 at 12:45 PM, 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"><span>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></span><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"><div><div>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><span><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></div></div><br></div><div class="gmail_extra">That doesn't make them right. =) C++ does not have zero-sized types, nor the possibility of the above objects being at the same address. Per its constant evaluation rules, the above expression *is* a constant expression, and we are required to treat it as such. In this regard, zero-sized types are not a conforming extension.</div></div>
</blockquote></div><br></div></div></div><div class="gmail_extra">They are both (potentially) one-past-the-end objects though.  I think our hands are tied, seeing as how we use the constant expression evaluation to CodeGen if conditions and what-not.</div></div>
</blockquote></div><br></div></div></div><div class="gmail_extra">I don't think it's so clear. No valid C or C++ program can have an array of zero bound, and I think we should generally prioritize doing the right thing on conforming code over giving better semantics to a language extension. I think the question is, does any real code rely on this not being constant-folded for incomplete arrays that turn out to have a bound of zero?</div></div></blockquote><div><br></div><div>I'm not entirely sure how we can answer that but I found the following after a minute of digging around the linux kernel:</div><div><br></div><div>kernel_memsize = kernel_size + (_end - _edata);<br></div><div><br></div><div>_end and _edata are two linker generated symbols.  If people are subtracting these things, I can imagine that they are also comparing them.</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"><br></div><div class="gmail_extra">In any case, the incomplete-type case should be restricted to incomplete arrays, since incomplete class types can never have zero size in C++.</div></div>
</blockquote></div><br></div><div class="gmail_extra">I completely agree. In an ideal world, I'd stuff this zero-sized mumbo-jumbo under a hypothetical -fgcc-compatibility (or something similar).</div></div>