Fix a crash with -Wassign-enum, where we didn't adjust the APInt type of the

constant. Also fix some spelling mistakes and formatting issues.

Reviewed by Richard Smith over IRC.

Fixes PR15069.

llvm-svn: 183409
This commit is contained in:
Joey Gouly 2013-06-06 13:48:00 +00:00
parent 5fe8a4f88f
commit 1ba2733e2c
3 changed files with 24 additions and 11 deletions

View File

@ -6073,7 +6073,7 @@ def warn_unreachable_default : Warning<
InGroup<CoveredSwitchDefault>, DefaultIgnore;
def warn_not_in_enum : Warning<"case value not in enumerated type %0">,
InGroup<Switch>;
def warn_not_in_enum_assignement : Warning<"integer constant not in range "
def warn_not_in_enum_assignment : Warning<"integer constant not in range "
"of enumerated type %0">, InGroup<DiagGroup<"assign-enum">>, DefaultIgnore;
def err_typecheck_statement_requires_scalar : Error<
"statement requires expression of scalar type (%0 invalid)">;

View File

@ -1120,9 +1120,9 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
void
Sema::DiagnoseAssignmentEnum(QualType DstType, QualType SrcType,
Expr *SrcExpr) {
unsigned DIAG = diag::warn_not_in_enum_assignement;
if (Diags.getDiagnosticLevel(DIAG, SrcExpr->getExprLoc())
== DiagnosticsEngine::Ignored)
if (Diags.getDiagnosticLevel(diag::warn_not_in_enum_assignment,
SrcExpr->getExprLoc()) ==
DiagnosticsEngine::Ignored)
return;
if (const EnumType *ET = DstType->getAs<EnumType>())
@ -1131,12 +1131,13 @@ Sema::DiagnoseAssignmentEnum(QualType DstType, QualType SrcType,
if (!SrcExpr->isTypeDependent() && !SrcExpr->isValueDependent() &&
SrcExpr->isIntegerConstantExpr(Context)) {
// Get the bitwidth of the enum value before promotions.
unsigned DstWith = Context.getIntWidth(DstType);
unsigned DstWidth = Context.getIntWidth(DstType);
bool DstIsSigned = DstType->isSignedIntegerOrEnumerationType();
llvm::APSInt RhsVal = SrcExpr->EvaluateKnownConstInt(Context);
AdjustAPSInt(RhsVal, DstWidth, DstIsSigned);
const EnumDecl *ED = ET->getDecl();
typedef SmallVector<std::pair<llvm::APSInt, EnumConstantDecl*>, 64>
typedef SmallVector<std::pair<llvm::APSInt, EnumConstantDecl *>, 64>
EnumValsTy;
EnumValsTy EnumVals;
@ -1145,7 +1146,7 @@ Sema::DiagnoseAssignmentEnum(QualType DstType, QualType SrcType,
for (EnumDecl::enumerator_iterator EDI = ED->enumerator_begin();
EDI != ED->enumerator_end(); ++EDI) {
llvm::APSInt Val = EDI->getInitVal();
AdjustAPSInt(Val, DstWith, DstIsSigned);
AdjustAPSInt(Val, DstWidth, DstIsSigned);
EnumVals.push_back(std::make_pair(Val, *EDI));
}
if (EnumVals.empty())
@ -1154,12 +1155,12 @@ Sema::DiagnoseAssignmentEnum(QualType DstType, QualType SrcType,
EnumValsTy::iterator EIend =
std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals);
// See which case values aren't in enum.
// See which values aren't in the enum.
EnumValsTy::const_iterator EI = EnumVals.begin();
while (EI != EIend && EI->first < RhsVal)
EI++;
if (EI == EIend || EI->first != RhsVal) {
Diag(SrcExpr->getExprLoc(), diag::warn_not_in_enum_assignement)
Diag(SrcExpr->getExprLoc(), diag::warn_not_in_enum_assignment)
<< DstType;
}
}

View File

@ -21,6 +21,18 @@ enum Test2 test2(enum Test2 *t) {
return 10; // expected-warning {{integer constant not in range of enumerated type 'enum Test2'}}
}
// PR15069
typedef enum
{
a = 0
} T;
void f()
{
T x = a;
x += 1; // expected-warning {{integer constant not in range of enumerated type}}
}
int main() {
CCTestEnum test = 1; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
test = 600; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}