[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
Sun Sep 27 20:40:00 PDT 2026
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/226493
>From dfa929dfc801880b72e840cbcfa618e82f3abb84 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 | 12 ++++--------
clang/lib/Sema/SemaHLSL.cpp | 2 +-
clang/lib/Sema/SemaOverload.cpp | 2 +-
5 files changed, 8 insertions(+), 12 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
index 934b365a07cad..45aaa05765bdb 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 c03c88232e13d..a0324240ce34d 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 dea859382918b..c207f04fd7d84 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.");
@@ -22867,12 +22867,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;
- }
// Build evaluation settings.
Expr::EvalStatus Status;
@@ -22881,7 +22877,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 124f5bd23a623..187b8d286a26f 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 31422ab51d8b6..86c029ecd6995 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