From: Richard Smith Date: Tue, 8 Apr 2014 12:19:28 +0000 (+0000) Subject: PR19346: Adding 0 to a null pointer has defined behavior in C++. Allow it in constant... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=aee470d9302c5ec5eb3bd21c495c795db1c8de63;p=clang PR19346: Adding 0 to a null pointer has defined behavior in C++. Allow it in constant expressions. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@205757 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp index 9c69080652..16c5a5214d 100644 --- a/lib/AST/ExprConstant.cpp +++ b/lib/AST/ExprConstant.cpp @@ -968,7 +968,7 @@ namespace { // any object: we won't use such a designator for anything. if (!Info.getLangOpts().CPlusPlus11) Designator.setInvalid(); - return checkNullPointer(Info, E, CSK) && + return (CSK == CSK_ArrayToPointer || checkNullPointer(Info, E, CSK)) && Designator.checkSubobject(Info, E, CSK); } @@ -986,7 +986,7 @@ namespace { Designator.addComplexUnchecked(EltTy, Imag); } void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { - if (checkNullPointer(Info, E, CSK_ArrayIndex)) + if (N && checkNullPointer(Info, E, CSK_ArrayIndex)) Designator.adjustIndex(Info, E, N); } }; diff --git a/test/CXX/expr/expr.const/p2-0x.cpp b/test/CXX/expr/expr.const/p2-0x.cpp index 84688278f0..bcf45a0c05 100644 --- a/test/CXX/expr/expr.const/p2-0x.cpp +++ b/test/CXX/expr/expr.const/p2-0x.cpp @@ -202,7 +202,9 @@ namespace UndefinedBehavior { static_assert((A*)nb == 0, ""); static_assert((B*)na == 0, ""); constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}} - constexpr const int &np = (*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot access array element of null pointer}} + constexpr const int *np1 = (int*)nullptr + 0; // ok + constexpr const int *np2 = &(*(int(*)[4])nullptr)[0]; // ok + constexpr const int *np3 = &(*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot perform pointer arithmetic on null pointer}} struct C { constexpr int f() const { return 0; }