forked from OSchip/llvm-project
Issue warning when assigning out-of-range integer values to enums.
Due to performance cost, this is an opt-in option placed under -Wassign-enum. // rdar://11824807 llvm-svn: 160382
This commit is contained in:
parent
6ca05ebd50
commit
268fec1e2e
|
@ -5557,6 +5557,8 @@ 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 "
|
||||
"of enumerated type %0">, InGroup<DiagGroup<"assign-enum">>, DefaultIgnore;
|
||||
def err_typecheck_statement_requires_scalar : Error<
|
||||
"statement requires expression of scalar type (%0 invalid)">;
|
||||
def err_typecheck_statement_requires_integer : Error<
|
||||
|
|
|
@ -6549,6 +6549,11 @@ public:
|
|||
Expr *SrcExpr, AssignmentAction Action,
|
||||
bool *Complained = 0);
|
||||
|
||||
/// DiagnoseAssignmentEnum - Warn if assignment to enum is a constant
|
||||
/// integer not in the range of enum values.
|
||||
void DiagnoseAssignmentEnum(QualType DstType, QualType SrcType,
|
||||
Expr *SrcExpr);
|
||||
|
||||
/// CheckAssignmentConstraints - Perform type checking for assignment,
|
||||
/// argument passing, variable initialization, and function return values.
|
||||
/// C99 6.5.16.
|
||||
|
|
|
@ -9617,7 +9617,10 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
|
|||
bool MayHaveFunctionDiff = false;
|
||||
|
||||
switch (ConvTy) {
|
||||
case Compatible: return false;
|
||||
case Compatible:
|
||||
DiagnoseAssignmentEnum(DstType, SrcType, SrcExpr);
|
||||
return false;
|
||||
|
||||
case PointerToInt:
|
||||
DiagKind = diag::ext_typecheck_convert_pointer_int;
|
||||
ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this);
|
||||
|
|
|
@ -1059,6 +1059,55 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
|
|||
return Owned(SS);
|
||||
}
|
||||
|
||||
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)
|
||||
return;
|
||||
|
||||
if (const EnumType *ET = DstType->getAs<EnumType>())
|
||||
if (!Context.hasSameType(SrcType, DstType) &&
|
||||
SrcType->isIntegerType()) {
|
||||
if (!SrcExpr->isTypeDependent() && !SrcExpr->isValueDependent() &&
|
||||
SrcExpr->isIntegerConstantExpr(Context)) {
|
||||
// Get the bitwidth of the enum value before promotions.
|
||||
unsigned DstWith = Context.getIntWidth(DstType);
|
||||
bool DstIsSigned = DstType->isSignedIntegerOrEnumerationType();
|
||||
|
||||
llvm::APSInt RhsVal = SrcExpr->EvaluateKnownConstInt(Context);
|
||||
const EnumDecl *ED = ET->getDecl();
|
||||
typedef SmallVector<std::pair<llvm::APSInt, EnumConstantDecl*>, 64>
|
||||
EnumValsTy;
|
||||
EnumValsTy EnumVals;
|
||||
|
||||
// Gather all enum values, set their type and sort them,
|
||||
// allowing easier comparison with rhs constant.
|
||||
for (EnumDecl::enumerator_iterator EDI = ED->enumerator_begin();
|
||||
EDI != ED->enumerator_end(); ++EDI) {
|
||||
llvm::APSInt Val = EDI->getInitVal();
|
||||
AdjustAPSInt(Val, DstWith, DstIsSigned);
|
||||
EnumVals.push_back(std::make_pair(Val, *EDI));
|
||||
}
|
||||
if (EnumVals.empty())
|
||||
return;
|
||||
std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals);
|
||||
EnumValsTy::iterator EIend =
|
||||
std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals);
|
||||
|
||||
// See which case values aren't in 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)
|
||||
<< DstType;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
StmtResult
|
||||
Sema::ActOnWhileStmt(SourceLocation WhileLoc, FullExprArg Cond,
|
||||
Decl *CondVar, Stmt *Body) {
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
// RUN: %clang_cc1 -fsyntax-only -verify -Wassign-enum %s
|
||||
// rdar://11824807
|
||||
|
||||
typedef enum CCTestEnum
|
||||
{
|
||||
One,
|
||||
Two=4,
|
||||
Three
|
||||
} CCTestEnum;
|
||||
|
||||
CCTestEnum test = 50; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
|
||||
CCTestEnum test1 = -50; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
|
||||
|
||||
CCTestEnum foo(CCTestEnum r) {
|
||||
return 20; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
|
||||
}
|
||||
|
||||
enum Test2 { K_zero, K_one };
|
||||
enum Test2 test2(enum Test2 *t) {
|
||||
*t = 20; // 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'}}
|
||||
}
|
||||
|
||||
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'}}
|
||||
foo(2); // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
|
||||
foo(-1); // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
|
||||
foo(4);
|
||||
foo(Two+1);
|
||||
}
|
||||
|
Loading…
Reference in New Issue