[clang] [clang-tools-extra] [clang][AST] Make Result parameter of `isCXX11ConstantExpr` mandatory (PR #226493)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 29 01:27:18 PDT 2026
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/226493
>From 00d3f88ecdfe338efc4e036e50ccb1ca0e6259e2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
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.
---
.../bugprone/NarrowingConversionsCheck.cpp | 2 +-
clang/include/clang/AST/Expr.h | 2 +-
clang/lib/AST/ExprConstant.cpp | 16 ++++++----------
clang/lib/Sema/SemaHLSL.cpp | 2 +-
clang/lib/Sema/SemaOverload.cpp | 2 +-
5 files changed, 10 insertions(+), 14 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
index 934b365a07cad5..45aaa05765bdb7 100644
--- a/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
@@ -198,7 +198,7 @@ static APValue getConstantExprValue(const ASTContext &Ctx, const Expr &E) {
if (auto IntegerConstant = E.getIntegerConstantExpr(Ctx))
return APValue(*IntegerConstant);
APValue Constant;
- if (Ctx.getLangOpts().CPlusPlus && E.isCXX11ConstantExpr(Ctx, &Constant))
+ if (Ctx.getLangOpts().CPlusPlus && E.isCXX11ConstantExpr(Ctx, Constant))
return Constant;
return {};
}
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 12a84f7c818d24..d06b249d68280d 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -22894,7 +22894,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())
@@ -22971,7 +22971,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.");
@@ -22981,12 +22981,8 @@ bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result,
assert(Ctx.getLangOpts().CPlusPlus);
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;
- }
bool IsConstExpr;
Expr::EvalStatus Status;
@@ -22995,13 +22991,13 @@ bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result,
if (Ctx.getLangOpts().EnableNewConstInterp) {
interp::EvalSettings Settings(EvaluationMode::ConstantExpression, Status);
- IsConstExpr = Ctx.getInterpContext().evaluateAsRValue(
- Settings, this, Result ? *Result : Scratch);
+ IsConstExpr =
+ Ctx.getInterpContext().evaluateAsRValue(Settings, this, Result);
} else {
// Build evaluation settings.
EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);
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 dd6923ee3c7bb5..f7f1c453ea2650 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -6668,7 +6668,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 8e305d5d418d64..4adc9f8913eafe 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)
More information about the cfe-commits
mailing list