From: Richard Smith Date: Tue, 14 Feb 2012 22:35:28 +0000 (+0000) Subject: constexpr: evaluation support for nullptr comparisons. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=26f2cac83eeb4317738d74b9e567d3d58aa04ed9;p=clang constexpr: evaluation support for nullptr comparisons. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150521 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp index 787e722a7c..e33ca6dc65 100644 --- a/lib/AST/ExprConstant.cpp +++ b/lib/AST/ExprConstant.cpp @@ -4612,6 +4612,16 @@ bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); } + if (LHSTy->isNullPtrType()) { + assert(E->isComparisonOp() && "unexpected nullptr operation"); + assert(RHSTy->isNullPtrType() && "missing pointer conversion"); + // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t + // are compared, the result is true of the operator is <=, >= or ==, and + // false otherwise. + BinaryOperator::Opcode Opcode = E->getOpcode(); + return Success(Opcode == BO_EQ || Opcode == BO_LE || Opcode == BO_GE, E); + } + if (!LHSTy->isIntegralOrEnumerationType() || !RHSTy->isIntegralOrEnumerationType()) { // We can't continue from here for non-integral types. diff --git a/test/SemaCXX/nullptr.cpp b/test/SemaCXX/nullptr.cpp index 0e6771b57f..91757cf51e 100644 --- a/test/SemaCXX/nullptr.cpp +++ b/test/SemaCXX/nullptr.cpp @@ -113,15 +113,26 @@ int array0[__is_scalar(nullptr_t)? 1 : -1]; int array1[__is_pod(nullptr_t)? 1 : -1]; int array2[sizeof(nullptr_t) == sizeof(void*)? 1 : -1]; -// FIXME: when we implement constexpr, this will be testable. -#if 0 int relational0[nullptr < nullptr? -1 : 1]; int relational1[nullptr > nullptr? -1 : 1]; int relational2[nullptr <= nullptr? 1 : -1]; int relational3[nullptr >= nullptr? 1 : -1]; int equality[nullptr == nullptr? 1 : -1]; int inequality[nullptr != nullptr? -1 : 1]; -#endif + +int relational0_a[0 < nullptr? -1 : 1]; +int relational1_a[0 > nullptr? -1 : 1]; +int relational2_a[0 <= nullptr? 1 : -1]; +int relational3_a[0 >= nullptr? 1 : -1]; +int equality_a[0 == nullptr? 1 : -1]; +int inequality_a[0 != nullptr? -1 : 1]; + +int relationalnullptr_b[nullptr < 0? -1 : 1]; +int relational1_b[nullptr > 0? -1 : 1]; +int relational2_b[nullptr <= 0? 1 : -1]; +int relational3_b[nullptr >= 0? 1 : -1]; +int equality_b[nullptr == 0? 1 : -1]; +int inequality_b[nullptr != 0? -1 : 1]; namespace overloading { int &f1(int*);