diff options
-rw-r--r-- | include/clang/Basic/DiagnosticSemaKinds.td | 2 | ||||
-rw-r--r-- | include/clang/Sema/Sema.h | 5 | ||||
-rw-r--r-- | lib/Sema/SemaExpr.cpp | 5 | ||||
-rw-r--r-- | lib/Sema/SemaStmt.cpp | 49 | ||||
-rw-r--r-- | test/Sema/warn-outof-range-assign-enum.c | 32 |
5 files changed, 92 insertions, 1 deletions
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 0c5c01a6e1..d42f21fa0c 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -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< diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index 0813d4baf1..519e023b99 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -6548,6 +6548,11 @@ public: QualType DstType, QualType SrcType, 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. diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 34fb9d06a7..915663e622 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -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); diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index 54ec58af41..9affe9823a 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/lib/Sema/SemaStmt.cpp @@ -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) { diff --git a/test/Sema/warn-outof-range-assign-enum.c b/test/Sema/warn-outof-range-assign-enum.c new file mode 100644 index 0000000000..2e79e66f49 --- /dev/null +++ b/test/Sema/warn-outof-range-assign-enum.c @@ -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); +} + |