Merge commit 69d42eef4bec from llvm-project (by Dimitry Andric):

[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

PR:		271047
MFC after:	1 month
This commit is contained in:
Dimitry Andric 2023-06-14 20:49:59 +02:00
parent 2efbaac7a0
commit cf24393421
2 changed files with 7 additions and 6 deletions

View File

@ -393,8 +393,8 @@ def warn_fixedpoint_constant_overflow : Warning<
"overflow in expression; result is %0 with type %1">,
InGroup<DiagGroup<"fixed-point-overflow">>;
def warn_constexpr_unscoped_enum_out_of_range : Warning<
"integer value %0 is outside the valid range of values [%1, %2] for this "
"enumeration type">, DefaultError, InGroup<DiagGroup<"enum-constexpr-conversion">>;
"integer value %0 is outside the valid range of values [%1, %2] for the "
"enumeration type %3">, DefaultError, InGroup<DiagGroup<"enum-constexpr-conversion">>;
// This is a temporary diagnostic, and shall be removed once our
// implementation is complete, and like the preceding constexpr notes belongs

View File

@ -13682,12 +13682,13 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
Info.Ctx.getDiagnostics().Report(
E->getExprLoc(), diag::warn_constexpr_unscoped_enum_out_of_range)
<< llvm::toString(Result.getInt(), 10) << Min.getSExtValue()
<< Max.getSExtValue();
<< Max.getSExtValue() << ED;
else if (!ED->getNumNegativeBits() && ConstexprVar &&
Max.ult(Result.getInt().getZExtValue()))
Info.Ctx.getDiagnostics().Report(E->getExprLoc(),
diag::warn_constexpr_unscoped_enum_out_of_range)
<< llvm::toString(Result.getInt(),10) << Min.getZExtValue() << Max.getZExtValue();
Info.Ctx.getDiagnostics().Report(
E->getExprLoc(), diag::warn_constexpr_unscoped_enum_out_of_range)
<< llvm::toString(Result.getInt(), 10) << Min.getZExtValue()
<< Max.getZExtValue() << ED;
}
}