diff options
author | Eli Friedman <efriedma@quicinc.com> | 2025-06-06 08:57:11 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-06-06 08:57:11 -0700 |
commit | 609023213d3fcc35f6ee3d47dceaf37ffa55e66b (patch) | |
tree | 2d07dc3fcc5998e89ac49b11c76cb9fc156c1e27 /clang/lib/AST/ExprConstant.cpp | |
parent | cef5a3155bab9a2db5389f782471d56f1dd15b61 (diff) | |
download | llvm-609023213d3fcc35f6ee3d47dceaf37ffa55e66b.zip llvm-609023213d3fcc35f6ee3d47dceaf37ffa55e66b.tar.gz llvm-609023213d3fcc35f6ee3d47dceaf37ffa55e66b.tar.bz2 |
[clang] Check constexpr int->enum conversions consistently. (#143034)
In 8de51375f12d91675a18d17f262276e65f43fbe0 and related patches, we
added some code to avoid triggering -Wenum-constexpr-conversion in some
cases. This isn't necessary anymore because -Wenum-constexpr-conversion
doesn't exist anymore. And the checks are subtly wrong: they exclude
cases where we actually do need to check the conversion. This patch gets
rid of the unnecessary checks.
Diffstat (limited to 'clang/lib/AST/ExprConstant.cpp')
-rw-r--r-- | clang/lib/AST/ExprConstant.cpp | 20 |
1 files changed, 3 insertions, 17 deletions
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index dd86edd..fa4e10e 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -15245,21 +15245,7 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); } - if (Info.Ctx.getLangOpts().CPlusPlus && Info.InConstantContext && - Info.EvalMode == EvalInfo::EM_ConstantExpression && - DestType->isEnumeralType()) { - - bool ConstexprVar = true; - - // We know if we are here that we are in a context that we might require - // a constant expression or a context that requires a constant - // value. But if we are initializing a value we don't know if it is a - // constexpr variable or not. We can check the EvaluatingDecl to determine - // if it constexpr or not. If not then we don't want to emit a diagnostic. - if (const auto *VD = dyn_cast_or_null<VarDecl>( - Info.EvaluatingDecl.dyn_cast<const ValueDecl *>())) - ConstexprVar = VD->isConstexpr(); - + if (Info.Ctx.getLangOpts().CPlusPlus && DestType->isEnumeralType()) { const EnumType *ET = dyn_cast<EnumType>(DestType.getCanonicalType()); const EnumDecl *ED = ET->getDecl(); // Check that the value is within the range of the enumeration values. @@ -15279,13 +15265,13 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { ED->getValueRange(Max, Min); --Max; - if (ED->getNumNegativeBits() && ConstexprVar && + if (ED->getNumNegativeBits() && (Max.slt(Result.getInt().getSExtValue()) || Min.sgt(Result.getInt().getSExtValue()))) Info.CCEDiag(E, diag::note_constexpr_unscoped_enum_out_of_range) << llvm::toString(Result.getInt(), 10) << Min.getSExtValue() << Max.getSExtValue() << ED; - else if (!ED->getNumNegativeBits() && ConstexprVar && + else if (!ED->getNumNegativeBits() && Max.ult(Result.getInt().getZExtValue())) Info.CCEDiag(E, diag::note_constexpr_unscoped_enum_out_of_range) << llvm::toString(Result.getInt(), 10) << Min.getZExtValue() |