Re: r359361 - Revert Fix interactions between __builtin_constant_p and constexpr to match current trunk GCC.

2019-04-30 Thread Eric Fiselier via cfe-commits
Jorge,

Why did you revert this?

/Eric

On Sat, Apr 27, 2019 at 6:01 AM Roman Lebedev via cfe-commits <
cfe-commits@lists.llvm.org> wrote:

> On Sat, Apr 27, 2019 at 3:29 AM Jorge Gorbe Moya via cfe-commits
>  wrote:
> >
> > Author: jgorbe
> > Date: Fri Apr 26 17:32:04 2019
> > New Revision: 359361
> >
> > URL: http://llvm.org/viewvc/llvm-project?rev=359361=rev
> > Log:
> > Revert Fix interactions between __builtin_constant_p and constexpr to
> match current trunk GCC.
> >
> > This reverts r359059 (git commit
> 0b098754b73f3b96d00ecb1c7605760b11c90298)
> It is common to specify the *reason* for the revert, so it is recorded
> in change log.
>
> > Removed:
> > cfe/trunk/test/SemaCXX/builtin-constant-p.cpp
> > Modified:
> > cfe/trunk/lib/AST/ExprConstant.cpp
> > cfe/trunk/lib/Sema/SemaChecking.cpp
> > cfe/trunk/test/SemaCXX/enable_if.cpp
> >
> > Modified: cfe/trunk/lib/AST/ExprConstant.cpp
> > URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=359361=359360=359361=diff
> >
> ==
> > --- cfe/trunk/lib/AST/ExprConstant.cpp (original)
> > +++ cfe/trunk/lib/AST/ExprConstant.cpp Fri Apr 26 17:32:04 2019
> > @@ -7801,33 +7801,19 @@ EvaluateBuiltinClassifyType(const CallEx
> >  }
> >
> >  /// EvaluateBuiltinConstantPForLValue - Determine the result of
> > -/// __builtin_constant_p when applied to the given pointer.
> > +/// __builtin_constant_p when applied to the given lvalue.
> >  ///
> > -/// A pointer is only "constant" if it is null (or a pointer cast to
> integer)
> > -/// or it points to the first character of a string literal.
> > -static bool EvaluateBuiltinConstantPForLValue(const APValue ) {
> > -  APValue::LValueBase Base = LV.getLValueBase();
> > -  if (Base.isNull()) {
> > -// A null base is acceptable.
> > -return true;
> > -  } else if (const Expr *E = Base.dyn_cast()) {
> > -if (!isa(E))
> > -  return false;
> > -return LV.getLValueOffset().isZero();
> > -  } else {
> > -// Any other base is not constant enough for GCC.
> > -return false;
> > -  }
> > +/// An lvalue is only "constant" if it is a pointer or reference to the
> first
> > +/// character of a string literal.
> > +template
> > +static bool EvaluateBuiltinConstantPForLValue(const LValue ) {
> > +  const Expr *E = LV.getLValueBase().template dyn_cast();
> > +  return E && isa(E) && LV.getLValueOffset().isZero();
> >  }
> >
> >  /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as
> similarly to
> >  /// GCC as we can manage.
> > -static bool EvaluateBuiltinConstantP(EvalInfo , const Expr *Arg) {
> > -  // Constant-folding is always enabled for the operand of
> __builtin_constant_p
> > -  // (even when the enclosing evaluation context otherwise requires a
> strict
> > -  // language-specific constant expression).
> > -  FoldConstant Fold(Info, true);
> > -
> > +static bool EvaluateBuiltinConstantP(ASTContext , const Expr *Arg) {
> >QualType ArgType = Arg->getType();
> >
> >// __builtin_constant_p always has one operand. The rules which gcc
> follows
> > @@ -7835,27 +7821,34 @@ static bool EvaluateBuiltinConstantP(Eva
> >//
> >//  - If the operand is of integral, floating, complex or enumeration
> type,
> >//and can be folded to a known value of that type, it returns 1.
> > -  //  - If the operand can be folded to a pointer to the first character
> > -  //of a string literal (or such a pointer cast to an integral type)
> > -  //or to a null pointer or an integer cast to a pointer, it
> returns 1.
> > +  //  - If the operand and can be folded to a pointer to the first
> character
> > +  //of a string literal (or such a pointer cast to an integral
> type), it
> > +  //returns 1.
> >//
> >// Otherwise, it returns 0.
> >//
> >// FIXME: GCC also intends to return 1 for literals of aggregate
> types, but
> >// its support for this does not currently work.
> > -  if (ArgType->isIntegralOrEnumerationType() ||
> ArgType->isFloatingType() ||
> > -  ArgType->isAnyComplexType() || ArgType->isPointerType() ||
> > -  ArgType->isNullPtrType()) {
> > -APValue V;
> > -if (!::EvaluateAsRValue(Info, Arg, V))
> > +  if (ArgType->isIntegralOrEnumerationType()) {
> > +Expr::EvalResult Result;
> > +if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects)
> >return false;
> >
> > -// For a pointer (possibly cast to integer), there are special
> rules.
> > +APValue  = Result.Val;
> > +if (V.getKind() == APValue::Int)
> > +  return true;
> >  if (V.getKind() == APValue::LValue)
> >return EvaluateBuiltinConstantPForLValue(V);
> > -
> > -// Otherwise, any constant value is good enough.
> > -return V.getKind() != APValue::Uninitialized;
> > +  } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) {
> > +return Arg->isEvaluatable(Ctx);
> > +  } else if 

Re: r359361 - Revert Fix interactions between __builtin_constant_p and constexpr to match current trunk GCC.

2019-04-27 Thread Roman Lebedev via cfe-commits
On Sat, Apr 27, 2019 at 3:29 AM Jorge Gorbe Moya via cfe-commits
 wrote:
>
> Author: jgorbe
> Date: Fri Apr 26 17:32:04 2019
> New Revision: 359361
>
> URL: http://llvm.org/viewvc/llvm-project?rev=359361=rev
> Log:
> Revert Fix interactions between __builtin_constant_p and constexpr to match 
> current trunk GCC.
>
> This reverts r359059 (git commit 0b098754b73f3b96d00ecb1c7605760b11c90298)
It is common to specify the *reason* for the revert, so it is recorded
in change log.

> Removed:
> cfe/trunk/test/SemaCXX/builtin-constant-p.cpp
> Modified:
> cfe/trunk/lib/AST/ExprConstant.cpp
> cfe/trunk/lib/Sema/SemaChecking.cpp
> cfe/trunk/test/SemaCXX/enable_if.cpp
>
> Modified: cfe/trunk/lib/AST/ExprConstant.cpp
> URL: 
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=359361=359360=359361=diff
> ==
> --- cfe/trunk/lib/AST/ExprConstant.cpp (original)
> +++ cfe/trunk/lib/AST/ExprConstant.cpp Fri Apr 26 17:32:04 2019
> @@ -7801,33 +7801,19 @@ EvaluateBuiltinClassifyType(const CallEx
>  }
>
>  /// EvaluateBuiltinConstantPForLValue - Determine the result of
> -/// __builtin_constant_p when applied to the given pointer.
> +/// __builtin_constant_p when applied to the given lvalue.
>  ///
> -/// A pointer is only "constant" if it is null (or a pointer cast to integer)
> -/// or it points to the first character of a string literal.
> -static bool EvaluateBuiltinConstantPForLValue(const APValue ) {
> -  APValue::LValueBase Base = LV.getLValueBase();
> -  if (Base.isNull()) {
> -// A null base is acceptable.
> -return true;
> -  } else if (const Expr *E = Base.dyn_cast()) {
> -if (!isa(E))
> -  return false;
> -return LV.getLValueOffset().isZero();
> -  } else {
> -// Any other base is not constant enough for GCC.
> -return false;
> -  }
> +/// An lvalue is only "constant" if it is a pointer or reference to the first
> +/// character of a string literal.
> +template
> +static bool EvaluateBuiltinConstantPForLValue(const LValue ) {
> +  const Expr *E = LV.getLValueBase().template dyn_cast();
> +  return E && isa(E) && LV.getLValueOffset().isZero();
>  }
>
>  /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to
>  /// GCC as we can manage.
> -static bool EvaluateBuiltinConstantP(EvalInfo , const Expr *Arg) {
> -  // Constant-folding is always enabled for the operand of 
> __builtin_constant_p
> -  // (even when the enclosing evaluation context otherwise requires a strict
> -  // language-specific constant expression).
> -  FoldConstant Fold(Info, true);
> -
> +static bool EvaluateBuiltinConstantP(ASTContext , const Expr *Arg) {
>QualType ArgType = Arg->getType();
>
>// __builtin_constant_p always has one operand. The rules which gcc follows
> @@ -7835,27 +7821,34 @@ static bool EvaluateBuiltinConstantP(Eva
>//
>//  - If the operand is of integral, floating, complex or enumeration type,
>//and can be folded to a known value of that type, it returns 1.
> -  //  - If the operand can be folded to a pointer to the first character
> -  //of a string literal (or such a pointer cast to an integral type)
> -  //or to a null pointer or an integer cast to a pointer, it returns 1.
> +  //  - If the operand and can be folded to a pointer to the first character
> +  //of a string literal (or such a pointer cast to an integral type), it
> +  //returns 1.
>//
>// Otherwise, it returns 0.
>//
>// FIXME: GCC also intends to return 1 for literals of aggregate types, but
>// its support for this does not currently work.
> -  if (ArgType->isIntegralOrEnumerationType() || ArgType->isFloatingType() ||
> -  ArgType->isAnyComplexType() || ArgType->isPointerType() ||
> -  ArgType->isNullPtrType()) {
> -APValue V;
> -if (!::EvaluateAsRValue(Info, Arg, V))
> +  if (ArgType->isIntegralOrEnumerationType()) {
> +Expr::EvalResult Result;
> +if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects)
>return false;
>
> -// For a pointer (possibly cast to integer), there are special rules.
> +APValue  = Result.Val;
> +if (V.getKind() == APValue::Int)
> +  return true;
>  if (V.getKind() == APValue::LValue)
>return EvaluateBuiltinConstantPForLValue(V);
> -
> -// Otherwise, any constant value is good enough.
> -return V.getKind() != APValue::Uninitialized;
> +  } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) {
> +return Arg->isEvaluatable(Ctx);
> +  } else if (ArgType->isPointerType() || Arg->isGLValue()) {
> +LValue LV;
> +Expr::EvalStatus Status;
> +EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold);
> +if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info)
> +  : EvaluatePointer(Arg, LV, Info)) &&
> +!Status.HasSideEffects)
> +  return EvaluateBuiltinConstantPForLValue(LV);
>

r359361 - Revert Fix interactions between __builtin_constant_p and constexpr to match current trunk GCC.

2019-04-26 Thread Jorge Gorbe Moya via cfe-commits
Author: jgorbe
Date: Fri Apr 26 17:32:04 2019
New Revision: 359361

URL: http://llvm.org/viewvc/llvm-project?rev=359361=rev
Log:
Revert Fix interactions between __builtin_constant_p and constexpr to match 
current trunk GCC.

This reverts r359059 (git commit 0b098754b73f3b96d00ecb1c7605760b11c90298)

Removed:
cfe/trunk/test/SemaCXX/builtin-constant-p.cpp
Modified:
cfe/trunk/lib/AST/ExprConstant.cpp
cfe/trunk/lib/Sema/SemaChecking.cpp
cfe/trunk/test/SemaCXX/enable_if.cpp

Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=359361=359360=359361=diff
==
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Fri Apr 26 17:32:04 2019
@@ -7801,33 +7801,19 @@ EvaluateBuiltinClassifyType(const CallEx
 }
 
 /// EvaluateBuiltinConstantPForLValue - Determine the result of
-/// __builtin_constant_p when applied to the given pointer.
+/// __builtin_constant_p when applied to the given lvalue.
 ///
-/// A pointer is only "constant" if it is null (or a pointer cast to integer)
-/// or it points to the first character of a string literal.
-static bool EvaluateBuiltinConstantPForLValue(const APValue ) {
-  APValue::LValueBase Base = LV.getLValueBase();
-  if (Base.isNull()) {
-// A null base is acceptable.
-return true;
-  } else if (const Expr *E = Base.dyn_cast()) {
-if (!isa(E))
-  return false;
-return LV.getLValueOffset().isZero();
-  } else {
-// Any other base is not constant enough for GCC.
-return false;
-  }
+/// An lvalue is only "constant" if it is a pointer or reference to the first
+/// character of a string literal.
+template
+static bool EvaluateBuiltinConstantPForLValue(const LValue ) {
+  const Expr *E = LV.getLValueBase().template dyn_cast();
+  return E && isa(E) && LV.getLValueOffset().isZero();
 }
 
 /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to
 /// GCC as we can manage.
-static bool EvaluateBuiltinConstantP(EvalInfo , const Expr *Arg) {
-  // Constant-folding is always enabled for the operand of __builtin_constant_p
-  // (even when the enclosing evaluation context otherwise requires a strict
-  // language-specific constant expression).
-  FoldConstant Fold(Info, true);
-
+static bool EvaluateBuiltinConstantP(ASTContext , const Expr *Arg) {
   QualType ArgType = Arg->getType();
 
   // __builtin_constant_p always has one operand. The rules which gcc follows
@@ -7835,27 +7821,34 @@ static bool EvaluateBuiltinConstantP(Eva
   //
   //  - If the operand is of integral, floating, complex or enumeration type,
   //and can be folded to a known value of that type, it returns 1.
-  //  - If the operand can be folded to a pointer to the first character
-  //of a string literal (or such a pointer cast to an integral type)
-  //or to a null pointer or an integer cast to a pointer, it returns 1.
+  //  - If the operand and can be folded to a pointer to the first character
+  //of a string literal (or such a pointer cast to an integral type), it
+  //returns 1.
   //
   // Otherwise, it returns 0.
   //
   // FIXME: GCC also intends to return 1 for literals of aggregate types, but
   // its support for this does not currently work.
-  if (ArgType->isIntegralOrEnumerationType() || ArgType->isFloatingType() ||
-  ArgType->isAnyComplexType() || ArgType->isPointerType() ||
-  ArgType->isNullPtrType()) {
-APValue V;
-if (!::EvaluateAsRValue(Info, Arg, V))
+  if (ArgType->isIntegralOrEnumerationType()) {
+Expr::EvalResult Result;
+if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects)
   return false;
 
-// For a pointer (possibly cast to integer), there are special rules.
+APValue  = Result.Val;
+if (V.getKind() == APValue::Int)
+  return true;
 if (V.getKind() == APValue::LValue)
   return EvaluateBuiltinConstantPForLValue(V);
-
-// Otherwise, any constant value is good enough.
-return V.getKind() != APValue::Uninitialized;
+  } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) {
+return Arg->isEvaluatable(Ctx);
+  } else if (ArgType->isPointerType() || Arg->isGLValue()) {
+LValue LV;
+Expr::EvalStatus Status;
+EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold);
+if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info)
+  : EvaluatePointer(Arg, LV, Info)) &&
+!Status.HasSideEffects)
+  return EvaluateBuiltinConstantPForLValue(LV);
   }
 
   // Anything else isn't considered to be sufficiently constant.
@@ -8266,7 +8259,7 @@ bool IntExprEvaluator::VisitBuiltinCallE
 
   case Builtin::BI__builtin_constant_p: {
 auto Arg = E->getArg(0);
-if (EvaluateBuiltinConstantP(Info, Arg))
+if (EvaluateBuiltinConstantP(Info.Ctx, Arg))
   return Success(true, E);
 auto ArgTy =