Index: test/Sema/switch-1.c =================================================================== --- test/Sema/switch-1.c (revision 172727) +++ test/Sema/switch-1.c (working copy) @@ -4,11 +4,9 @@ int f(int i) { switch (i) { - case 2147483647 + 2: // expected-note {{value 2147483649 is outside the range of representable values of type 'int'}} \ - // expected-warning {{overflow in case constant expression results in value -2147483647}} + case 2147483647 + 2: // expected-warning {{integer constant overflow in expression}} return 1; - case 9223372036854775807L * 4 : // expected-note {{value 36893488147419103228 is outside the range of representable values of type 'long'}} \ - // expected-warning {{overflow in case constant expression results in value -4}} + case 9223372036854775807L * 4: // expected-warning {{integer constant overflow in expression}} return 2; case 2147483647: return 0; Index: include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- include/clang/Basic/DiagnosticSemaKinds.td (revision 172727) +++ include/clang/Basic/DiagnosticSemaKinds.td (working copy) @@ -5727,9 +5727,9 @@ def warn_case_value_overflow : Warning< "overflow converting case value to switch condition type (%0 to %1)">, InGroup; -def warn_case_constant_overflow : Warning< - "overflow in case constant expression results in value %0">, - InGroup; +def warn_integer_constant_overflow : Warning< + "integer constant overflow in expression">, + InGroup>; def err_duplicate_case : Error<"duplicate case value '%0'">; def err_duplicate_case_differing_expr : Error< "duplicate case value: '%0' and '%1' both equal '%2'">; Index: include/clang/Sema/Sema.h =================================================================== --- include/clang/Sema/Sema.h (revision 172727) +++ include/clang/Sema/Sema.h (working copy) @@ -3983,7 +3983,8 @@ : SourceLocation()); } ExprResult ActOnFinishFullExpr(Expr *Expr, SourceLocation CC, - bool DiscardedValue = false); + bool DiscardedValue = false, + bool IsConstexpr = false); StmtResult ActOnFinishFullStmt(Stmt *Stmt); // Marks SS invalid if it represents an incomplete type. @@ -7321,11 +7322,15 @@ SourceLocation ReturnLoc); void CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr* RHS); void CheckImplicitConversions(Expr *E, SourceLocation CC = SourceLocation()); + void CheckForIntOverflow(Expr *E); + void DiagnoseIntegerOverflow(ExprResult &LHS, ExprResult &RHS, + SourceRange R, unsigned Opc); void CheckUnsequencedOperations(Expr *E); /// \brief Perform semantic checks on a completed expression. This will either /// be a full-expression or a default argument expression. - void CheckCompletedExpr(Expr *E, SourceLocation CheckLoc = SourceLocation()); + void CheckCompletedExpr(Expr *E, SourceLocation CheckLoc = SourceLocation(), + bool IsConstexpr = false); void CheckBitFieldInitialization(SourceLocation InitLoc, FieldDecl *Field, Expr *Init); Index: lib/Sema/SemaExprCXX.cpp =================================================================== --- lib/Sema/SemaExprCXX.cpp (revision 172727) +++ lib/Sema/SemaExprCXX.cpp (working copy) @@ -5469,7 +5469,8 @@ } ExprResult Sema::ActOnFinishFullExpr(Expr *FE, SourceLocation CC, - bool DiscardedValue) { + bool DiscardedValue, + bool IsConstexpr) { ExprResult FullExpr = Owned(FE); if (!FullExpr.get()) @@ -5497,7 +5498,7 @@ return ExprError(); } - CheckCompletedExpr(FullExpr.get(), CC); + CheckCompletedExpr(FullExpr.get(), CC, IsConstexpr); return MaybeCreateExprWithCleanups(FullExpr); } Index: lib/Sema/SemaDecl.cpp =================================================================== --- lib/Sema/SemaDecl.cpp (revision 172727) +++ lib/Sema/SemaDecl.cpp (working copy) @@ -6978,7 +6978,9 @@ // struct T { S a, b; } t = { Temp(), Temp() } // // we should destroy the first Temp before constructing the second. - ExprResult Result = ActOnFinishFullExpr(Init, VDecl->getLocation()); + ExprResult Result = ActOnFinishFullExpr(Init, VDecl->getLocation(), + false, + VDecl->isConstexpr()); if (Result.isInvalid()) { VDecl->setInvalidDecl(); return; Index: lib/Sema/SemaStmt.cpp =================================================================== --- lib/Sema/SemaStmt.cpp (revision 172727) +++ lib/Sema/SemaStmt.cpp (working copy) @@ -338,6 +338,10 @@ // Recover from an error by just forgetting about it. } } + + LHSVal = ActOnFinishFullExpr(LHSVal).take(); + if (RHSVal) + RHSVal = ActOnFinishFullExpr(RHSVal).take(); CaseStmt *CS = new (Context) CaseStmt(LHSVal, RHSVal, CaseLoc, DotDotDotLoc, ColonLoc); @@ -732,14 +736,7 @@ } else { // We already verified that the expression has a i-c-e value (C99 // 6.8.4.2p3) - get that value now. - SmallVector Diags; - LoVal = Lo->EvaluateKnownConstInt(Context, &Diags); - if (Diags.size() == 1 && - Diags[0].second.getDiagID() == diag::note_constexpr_overflow) { - Diag(Lo->getLocStart(), diag::warn_case_constant_overflow) << - LoVal.toString(10); - Diag(Diags[0].first, Diags[0].second); - } + LoVal = Lo->EvaluateKnownConstInt(Context); // If the LHS is not the same type as the condition, insert an implicit // cast. Index: lib/Sema/SemaChecking.cpp =================================================================== --- lib/Sema/SemaChecking.cpp (revision 172727) +++ lib/Sema/SemaChecking.cpp (working copy) @@ -5171,6 +5171,21 @@ AnalyzeImplicitConversions(*this, E, CC); } +/// Diagnose when expression is an integer constant expression and its evaluation +/// results in integer overflow +void Sema::CheckForIntOverflow (Expr *E) { + if (const BinaryOperator *BExpr = dyn_cast(E)) { + unsigned Opc = BExpr->getOpcode(); + if (Opc != BO_Add && Opc != BO_Sub && Opc != BO_Mul) + return; + + ExprResult LHS = BExpr->getLHS()->IgnoreParens(); + + ExprResult RHS = BExpr->getRHS()->IgnoreParens(); + DiagnoseIntegerOverflow(LHS, RHS, SourceRange(E->getExprLoc()), Opc); + } +} + namespace { /// \brief Visitor for expressions which looks for unsequenced operations on the /// same object. @@ -5584,9 +5599,12 @@ SequenceChecker(*this, E); } -void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc) { +void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc, + bool IsConstexpr) { CheckImplicitConversions(E, CheckLoc); CheckUnsequencedOperations(E); + if (!IsConstexpr) + CheckForIntOverflow(E); } void Sema::CheckBitFieldInitialization(SourceLocation InitLoc, Index: lib/Sema/SemaExpr.cpp =================================================================== --- lib/Sema/SemaExpr.cpp (revision 172727) +++ lib/Sema/SemaExpr.cpp (working copy) @@ -6138,6 +6138,46 @@ return QualType(); } +template +static bool CheckedIntOverflow(const llvm::APSInt &LHS, const llvm::APSInt &RHS, + unsigned BitWidth, Operation Op) { + if (LHS.isUnsigned()) + return false; + + llvm::APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); + llvm::APSInt Result = Value.trunc(LHS.getBitWidth()); + return (Result.extend(BitWidth) != Value); +} + +void Sema::DiagnoseIntegerOverflow(ExprResult &LHS, ExprResult &RHS, + SourceRange R, unsigned Opc) { + llvm::APSInt Right; + if (RHS.get()->isValueDependent() || + !RHS.get()->isIntegerConstantExpr(Right, Context)) + return; + + llvm::APSInt Left; + if (LHS.get()->isValueDependent() || + !LHS.get()->isIntegerConstantExpr(Left, Context)) + return; + bool overflow = false; + + if (Opc == BO_Add) + overflow = CheckedIntOverflow(Left, Right, Left.getBitWidth() + 1, + std::plus()); + else if (Opc == BO_Sub) + overflow = CheckedIntOverflow(Left, Right, Left.getBitWidth() + 1, + std::minus()); + else if (Opc == BO_Mul) + overflow = CheckedIntOverflow(Left, Right, Left.getBitWidth() * 2, + std::multiplies()); + else + llvm_unreachable("Unknown operator when checking for integer overflow"); + + if (overflow) + Diag(R.getBegin(), diag::warn_integer_constant_overflow) << R ; +} + // checkArithmeticNull - Detect when a NULL constant is used improperly in an // expression. These are mainly cases where the null pointer is used as an // integer instead of a pointer. @@ -6190,7 +6230,6 @@ if (LHS.isInvalid() || RHS.isInvalid()) return QualType(); - if (compType.isNull() || !compType->isArithmeticType()) return InvalidOperands(Loc, LHS, RHS);