Skip to content

Commit 69d42ee

Browse files
committed
[Clang] Show type in enum out of range diagnostic
When the diagnostic for an out of range enum value is printed, it currently does not show the actual enum type in question, for example: v8/src/base/bit-field.h:43:29: error: integer value 7 is outside the valid range of values [0, 3] for this enumeration type [-Wenum-constexpr-conversion] static constexpr T kMax = static_cast<T>(kNumValues - 1); ^ This can make it cumbersome to find the cause for the problem. Add the enum type to the diagnostic message, to make it easier. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D152788
1 parent 505829e commit 69d42ee

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ Improvements to Clang's diagnostics
344344
(`#42992: <https://github.com/llvm/llvm-project/issues/42992>`_)
345345
- Clang now diagnoses unused const-qualified variable template as
346346
"unused variable template" rather than "unused variable".
347+
- When diagnosing a constant expression where an enum without a fixed underlying
348+
type is set to a value outside the range of the enum's values, clang will now
349+
print the name of the enum in question.
347350

348351
Bug Fixes in This Version
349352
-------------------------

clang/include/clang/Basic/DiagnosticASTKinds.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ def warn_fixedpoint_constant_overflow : Warning<
395395
"overflow in expression; result is %0 with type %1">,
396396
InGroup<DiagGroup<"fixed-point-overflow">>;
397397
def warn_constexpr_unscoped_enum_out_of_range : Warning<
398-
"integer value %0 is outside the valid range of values [%1, %2] for this "
399-
"enumeration type">, DefaultError, InGroup<DiagGroup<"enum-constexpr-conversion">>;
398+
"integer value %0 is outside the valid range of values [%1, %2] for the "
399+
"enumeration type %3">, DefaultError, InGroup<DiagGroup<"enum-constexpr-conversion">>;
400400

401401
// This is a temporary diagnostic, and shall be removed once our
402402
// implementation is complete, and like the preceding constexpr notes belongs

clang/lib/AST/ExprConstant.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13708,12 +13708,13 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
1370813708
Info.Ctx.getDiagnostics().Report(
1370913709
E->getExprLoc(), diag::warn_constexpr_unscoped_enum_out_of_range)
1371013710
<< llvm::toString(Result.getInt(), 10) << Min.getSExtValue()
13711-
<< Max.getSExtValue();
13711+
<< Max.getSExtValue() << ED;
1371213712
else if (!ED->getNumNegativeBits() && ConstexprVar &&
1371313713
Max.ult(Result.getInt().getZExtValue()))
13714-
Info.Ctx.getDiagnostics().Report(E->getExprLoc(),
13715-
diag::warn_constexpr_unscoped_enum_out_of_range)
13716-
<< llvm::toString(Result.getInt(),10) << Min.getZExtValue() << Max.getZExtValue();
13714+
Info.Ctx.getDiagnostics().Report(
13715+
E->getExprLoc(), diag::warn_constexpr_unscoped_enum_out_of_range)
13716+
<< llvm::toString(Result.getInt(), 10) << Min.getZExtValue()
13717+
<< Max.getZExtValue() << ED;
1371713718
}
1371813719
}
1371913720

clang/test/SemaCXX/constant-expression-cxx11.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2440,42 +2440,51 @@ E2 testDefaultArgForParam(E2 e2Param = (E2)-1) { // ok, not a constant expressio
24402440
void testValueInRangeOfEnumerationValues() {
24412441
constexpr E1 x1 = static_cast<E1>(-8);
24422442
constexpr E1 x2 = static_cast<E1>(8);
2443-
// expected-error@-1 {{integer value 8 is outside the valid range of values [-8, 7] for this enumeration type}}
2443+
// expected-error@-1 {{integer value 8 is outside the valid range of values [-8, 7] for the enumeration type 'E1'}}
24442444
E1 x2b = static_cast<E1>(8); // ok, not a constant expression context
24452445

24462446
constexpr E2 x3 = static_cast<E2>(-8);
2447-
// expected-error@-1 {{integer value -8 is outside the valid range of values [0, 7] for this enumeration type}}
2447+
// expected-error@-1 {{integer value -8 is outside the valid range of values [0, 7] for the enumeration type 'E2'}}
24482448
constexpr E2 x4 = static_cast<E2>(0);
24492449
constexpr E2 x5 = static_cast<E2>(8);
2450-
// expected-error@-1 {{integer value 8 is outside the valid range of values [0, 7] for this enumeration type}}
2450+
// expected-error@-1 {{integer value 8 is outside the valid range of values [0, 7] for the enumeration type 'E2'}}
24512451

24522452
constexpr E3 x6 = static_cast<E3>(-2048);
24532453
constexpr E3 x7 = static_cast<E3>(-8);
24542454
constexpr E3 x8 = static_cast<E3>(0);
24552455
constexpr E3 x9 = static_cast<E3>(8);
24562456
constexpr E3 x10 = static_cast<E3>(2048);
2457-
// expected-error@-1 {{integer value 2048 is outside the valid range of values [-2048, 2047] for this enumeration type}}
2457+
// expected-error@-1 {{integer value 2048 is outside the valid range of values [-2048, 2047] for the enumeration type 'E3'}}
24582458

24592459
constexpr E4 x11 = static_cast<E4>(0);
24602460
constexpr E4 x12 = static_cast<E4>(1);
24612461
constexpr E4 x13 = static_cast<E4>(2);
2462-
// expected-error@-1 {{integer value 2 is outside the valid range of values [0, 1] for this enumeration type}}
2462+
// expected-error@-1 {{integer value 2 is outside the valid range of values [0, 1] for the enumeration type 'E4'}}
24632463

24642464
constexpr EEmpty x14 = static_cast<EEmpty>(0);
24652465
constexpr EEmpty x15 = static_cast<EEmpty>(1);
24662466
constexpr EEmpty x16 = static_cast<EEmpty>(2);
2467-
// expected-error@-1 {{integer value 2 is outside the valid range of values [0, 1] for this enumeration type}}
2467+
// expected-error@-1 {{integer value 2 is outside the valid range of values [0, 1] for the enumeration type 'EEmpty'}}
24682468

24692469
constexpr EFixed x17 = static_cast<EFixed>(100);
24702470
constexpr EScoped x18 = static_cast<EScoped>(100);
24712471

24722472
constexpr EMaxInt x19 = static_cast<EMaxInt>(__INT_MAX__-1);
24732473
constexpr EMaxInt x20 = static_cast<EMaxInt>((long)__INT_MAX__+1);
2474-
// expected-error@-1 {{integer value 2147483648 is outside the valid range of values [-2147483648, 2147483647] for this enumeration type}}
2474+
// expected-error@-1 {{integer value 2147483648 is outside the valid range of values [-2147483648, 2147483647] for the enumeration type 'EMaxInt'}}
24752475

24762476
const NumberType neg_one = (NumberType) ((NumberType) 0 - (NumberType) 1); // ok, not a constant expression context
24772477
}
24782478

2479+
template<class T, unsigned size> struct Bitfield {
2480+
static constexpr T max = static_cast<T>((1 << size) - 1); // #enum
2481+
};
2482+
2483+
void testValueInRangeOfEnumerationValuesViaTemplate() {
2484+
Bitfield<E2, 3> good;
2485+
Bitfield<E2, 4> bad; // cxx11-error@#enum {{integer value 15 is outside the valid range of values [0, 7] for the enumeration type 'E2'}}
2486+
}
2487+
24792488
enum SortOrder {
24802489
AscendingOrder,
24812490
DescendingOrder
@@ -2494,4 +2503,4 @@ void A::f(SortOrder order) {
24942503
GH50055::E2 GlobalInitNotCE1 = (GH50055::E2)-1; // ok, not a constant expression context
24952504
GH50055::E2 GlobalInitNotCE2 = GH50055::testDefaultArgForParam(); // ok, not a constant expression context
24962505
constexpr GH50055::E2 GlobalInitCE = (GH50055::E2)-1;
2497-
// expected-error@-1 {{integer value -1 is outside the valid range of values [0, 7] for this enumeration type}}
2506+
// expected-error@-1 {{integer value -1 is outside the valid range of values [0, 7] for the enumeration type 'E2'}}

clang/test/SemaCXX/cxx2a-consteval.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ consteval int aConstevalFunction() { // expected-error {{consteval function neve
900900
namespace GH50055 {
901901
enum E {e1=0, e2=1};
902902
consteval int testDefaultArgForParam(E eParam = (E)-1) {
903-
// expected-error@-1 {{integer value -1 is outside the valid range of values [0, 1] for this enumeration type}}
903+
// expected-error@-1 {{integer value -1 is outside the valid range of values [0, 1] for the enumeration type 'E'}}
904904
return (int)eParam;
905905
}
906906

0 commit comments

Comments
 (0)