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; InGroup<CoveredSwitchDefault>, DefaultIgnore;
def warn_not_in_enum : Warning<"case value not in enumerated type %0">, def warn_not_in_enum : Warning<"case value not in enumerated type %0">,
InGroup<Switch>; 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; "of enumerated type %0">, InGroup<DiagGroup<"assign-enum">>, DefaultIgnore;
def err_typecheck_statement_requires_scalar : Error< def err_typecheck_statement_requires_scalar : Error<
"statement requires expression of scalar type (%0 invalid)">; "statement requires expression of scalar type (%0 invalid)">;

View File

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