https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/226493
We want to discourage people from just checking if something is a constant expression without using the value, which we always compute anyways. There are only three call sites, all in clang, and all pass a value already. Change the parameter type to a reference to enforce this. >From c840575e5c03af318db5538bba6c44e1ca982109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]> Date: Fri, 25 Sep 2026 15:38:37 +0200 Subject: [PATCH] [clang][AST] Mak Result parameter of isCXX11ConstantExpr mandatory We want to discourage people from just checking if something is a constant expression without using the value, which we always compute anyways. There are only three call sites, all in clang, and all pass a value already. Change the parameter type to a reference to enforce this. --- clang/include/clang/AST/Expr.h | 2 +- clang/lib/AST/ExprConstant.cpp | 11 ++++------- clang/lib/Sema/SemaHLSL.cpp | 2 +- clang/lib/Sema/SemaOverload.cpp | 2 +- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h index c03c88232e13d5..a0324240ce34da 100644 --- a/clang/include/clang/AST/Expr.h +++ b/clang/include/clang/AST/Expr.h @@ -584,7 +584,7 @@ class Expr : public ValueStmt { /// If \p AllowRelaxedEval is \c true, this will allow certain constructs that /// are not valid per the specification. // FIXME: Add proper documentation about the constructs we allow. - bool isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result = nullptr, + bool isCXX11ConstantExpr(const ASTContext &Ctx, APValue &Result, bool AllowRelaxedEval = false) const; /// isPotentialConstantExpr - Return true if this function's definition diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index dea859382918b0..9fbaf63e51e1f9 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -22791,7 +22791,7 @@ EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx, const Expr *E, return false; APValue Result; - if (!E->isCXX11ConstantExpr(Ctx, &Result, AllowRelaxedEval)) + if (!E->isCXX11ConstantExpr(Ctx, Result, AllowRelaxedEval)) return false; if (!Result.isInt()) @@ -22857,7 +22857,7 @@ bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const { return CheckICE(this, Ctx).Kind == IK_ICE; } -bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result, +bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue &Result, bool AllowRelaxedEval) const { assert(!isValueDependent() && "Expression evaluator can't be called on a dependent expression."); @@ -22868,11 +22868,8 @@ bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result, bool IsConst; APValue Scratch; - if (FastEvaluateAsRValue(this, Scratch, Ctx, IsConst) && Scratch.hasValue()) { - if (Result) - *Result = std::move(Scratch); + if (FastEvaluateAsRValue(this, Result, Ctx, IsConst) && Result.hasValue()) return true; - } // Build evaluation settings. Expr::EvalStatus Status; @@ -22881,7 +22878,7 @@ bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result, Status.ExtendedDiag = AllowRelaxedEval ? &MSRelaxedDiag : nullptr; bool IsConstExpr = - ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch) && + ::EvaluateAsRValue(Info, this, Result) && // NOTE: We don't produce a diagnostic for this, but the callers that // call us on arbitrary full-expressions should generally not care. Info.discardCleanups() && !Status.HasSideEffects; diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp index 124f5bd23a623e..187b8d286a26f2 100644 --- a/clang/lib/Sema/SemaHLSL.cpp +++ b/clang/lib/Sema/SemaHLSL.cpp @@ -6661,7 +6661,7 @@ bool SemaHLSL::handleInitialization(VarDecl *VDecl, Expr *&Init) { ASTContext &Context = SemaRef.getASTContext(); APValue InitValue; - if (!Init->isCXX11ConstantExpr(Context, &InitValue)) { + if (!Init->isCXX11ConstantExpr(Context, InitValue)) { Diag(VDecl->getLocation(), diag::err_specialization_const); VDecl->setInvalidDecl(); return false; diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 31422ab51d8b6e..86c029ecd6995d 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -483,7 +483,7 @@ NarrowingKind StandardConversionSequence::getNarrowingKind( Expr::EvalResult R; if ((Ctx.getLangOpts().C23 && Initializer->EvaluateAsRValue(R, Ctx)) || ((Ctx.getLangOpts().CPlusPlus && - Initializer->isCXX11ConstantExpr(Ctx, &ConstantValue, + Initializer->isCXX11ConstantExpr(Ctx, ConstantValue, AllowRelaxedEval)))) { // Constant! if (Ctx.getLangOpts().C23) _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
