Skip to content

Commit 02744c5

Browse files
authored
[clang][diagnostics] Update note_constexpr_invalid_cast to use enum_select and adjust its uses (#130868)
Handles #123121 This patch updates `note_constexpr_invalid_cast` diagnostic to use `enum_select` instead of `select,` improving readability and reducing reliance on magic numbers in caller sites.
1 parent b087699 commit 02744c5

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

clang/include/clang/Basic/DiagnosticASTKinds.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ let Component = "AST" in {
1111
// Constant expression diagnostics. These (and their users) belong in Sema.
1212
def note_expr_divide_by_zero : Note<"division by zero">;
1313
def note_constexpr_invalid_cast : Note<
14-
"%select{reinterpret_cast|dynamic_cast|%select{this conversion|cast that"
15-
" performs the conversions of a reinterpret_cast}1|cast from %1}0"
14+
"%enum_select<ConstexprInvalidCastKind>{%Reinterpret{reinterpret_cast}|%Dynamic{dynamic_cast}|"
15+
"%ThisConversionOrReinterpret{%select{this conversion|cast that performs the conversions "
16+
"of a reinterpret_cast}1}|%CastFrom{cast from %1}}0"
1617
" is not allowed in a constant expression"
1718
"%select{| in C++ standards before C++20||}0">;
1819
def note_constexpr_invalid_void_star_cast : Note<

clang/lib/AST/ByteCode/Interp.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2369,12 +2369,14 @@ static inline bool PtrPtrCast(InterpState &S, CodePtr OpPC, bool SrcIsVoidPtr) {
23692369
} else if (!S.getLangOpts().CPlusPlus26) {
23702370
const SourceInfo &E = S.Current->getSource(OpPC);
23712371
S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2372-
<< 3 << "'void *'" << S.Current->getRange(OpPC);
2372+
<< diag::ConstexprInvalidCastKind::CastFrom << "'void *'"
2373+
<< S.Current->getRange(OpPC);
23732374
}
23742375
} else {
23752376
const SourceInfo &E = S.Current->getSource(OpPC);
23762377
S.CCEDiag(E, diag::note_constexpr_invalid_cast)
2377-
<< 2 << S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
2378+
<< diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
2379+
<< S.getLangOpts().CPlusPlus << S.Current->getRange(OpPC);
23782380
}
23792381

23802382
return true;
@@ -2739,7 +2741,8 @@ inline bool GetIntPtr(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
27392741

27402742
if (Desc)
27412743
S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast)
2742-
<< 2 << S.getLangOpts().CPlusPlus;
2744+
<< diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
2745+
<< S.getLangOpts().CPlusPlus;
27432746

27442747
S.Stk.push<Pointer>(static_cast<uint64_t>(IntVal), Desc);
27452748
return true;

clang/lib/AST/ExprConstant.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8103,12 +8103,14 @@ class ExprEvaluatorBase
81038103
}
81048104

81058105
bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) {
8106-
CCEDiag(E, diag::note_constexpr_invalid_cast) << 0;
8106+
CCEDiag(E, diag::note_constexpr_invalid_cast)
8107+
<< diag::ConstexprInvalidCastKind::Reinterpret;
81078108
return static_cast<Derived*>(this)->VisitCastExpr(E);
81088109
}
81098110
bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) {
81108111
if (!Info.Ctx.getLangOpts().CPlusPlus20)
8111-
CCEDiag(E, diag::note_constexpr_invalid_cast) << 1;
8112+
CCEDiag(E, diag::note_constexpr_invalid_cast)
8113+
<< diag::ConstexprInvalidCastKind::Dynamic;
81128114
return static_cast<Derived*>(this)->VisitCastExpr(E);
81138115
}
81148116
bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) {
@@ -8833,7 +8835,8 @@ class LValueExprEvaluator
88338835

88348836
case CK_LValueBitCast:
88358837
this->CCEDiag(E, diag::note_constexpr_invalid_cast)
8836-
<< 2 << Info.Ctx.getLangOpts().CPlusPlus;
8838+
<< diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
8839+
<< Info.Ctx.getLangOpts().CPlusPlus;
88378840
if (!Visit(E->getSubExpr()))
88388841
return false;
88398842
Result.Designator.setInvalid();
@@ -9670,10 +9673,12 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
96709673
<< E->getType()->getPointeeType();
96719674
else
96729675
CCEDiag(E, diag::note_constexpr_invalid_cast)
9673-
<< 3 << SubExpr->getType();
9676+
<< diag::ConstexprInvalidCastKind::CastFrom
9677+
<< SubExpr->getType();
96749678
} else
96759679
CCEDiag(E, diag::note_constexpr_invalid_cast)
9676-
<< 2 << Info.Ctx.getLangOpts().CPlusPlus;
9680+
<< diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
9681+
<< Info.Ctx.getLangOpts().CPlusPlus;
96779682
Result.Designator.setInvalid();
96789683
}
96799684
}
@@ -9712,7 +9717,8 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
97129717

97139718
case CK_IntegralToPointer: {
97149719
CCEDiag(E, diag::note_constexpr_invalid_cast)
9715-
<< 2 << Info.Ctx.getLangOpts().CPlusPlus;
9720+
<< diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
9721+
<< Info.Ctx.getLangOpts().CPlusPlus;
97169722

97179723
APValue Value;
97189724
if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
@@ -11177,7 +11183,8 @@ bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) {
1117711183
// Give up if the input isn't an int, float, or vector. For example, we
1117811184
// reject "(v4i16)(intptr_t)&a".
1117911185
Info.FFDiag(E, diag::note_constexpr_invalid_cast)
11180-
<< 2 << Info.Ctx.getLangOpts().CPlusPlus;
11186+
<< diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
11187+
<< Info.Ctx.getLangOpts().CPlusPlus;
1118111188
return false;
1118211189
}
1118311190

@@ -15201,7 +15208,8 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
1520115208

1520215209
case CK_PointerToIntegral: {
1520315210
CCEDiag(E, diag::note_constexpr_invalid_cast)
15204-
<< 2 << Info.Ctx.getLangOpts().CPlusPlus << E->getSourceRange();
15211+
<< diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
15212+
<< Info.Ctx.getLangOpts().CPlusPlus << E->getSourceRange();
1520515213

1520615214
LValue LV;
1520715215
if (!EvaluatePointer(SubExpr, LV, Info))

0 commit comments

Comments
 (0)