[cfe-commits] r173377 - in /cfe/trunk: include/clang/AST/Expr.h include/clang/Basic/DiagnosticASTKinds.td include/clang/Basic/DiagnosticSemaKinds.td include/clang/Sema/Sema.h lib/AST/ExprConstant.cpp lib/Sema/SemaChecking.cpp lib/Sema/SemaDecl.cpp lib/Sema/SemaExprCXX.cpp lib/Sema/SemaStmt.cpp test/Sema/switch-1.c
Dmitri Gribenko
gribozavr at gmail.com
Thu Jan 24 14:25:10 PST 2013
On Fri, Jan 25, 2013 at 12:11 AM, Fariborz Jahanian <fjahanian at apple.com> wrote:
> Author: fjahanian
> Date: Thu Jan 24 16:11:45 2013
> New Revision: 173377
>
> URL: http://llvm.org/viewvc/llvm-project?rev=173377&view=rev
> Log:
> Patch to check for integer overflow. It has been
> commented on and approved by Richard Smith.
>
> Modified:
> cfe/trunk/include/clang/AST/Expr.h
> cfe/trunk/include/clang/Basic/DiagnosticASTKinds.td
> cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
> cfe/trunk/include/clang/Sema/Sema.h
> cfe/trunk/lib/AST/ExprConstant.cpp
> cfe/trunk/lib/Sema/SemaChecking.cpp
> cfe/trunk/lib/Sema/SemaDecl.cpp
> cfe/trunk/lib/Sema/SemaExprCXX.cpp
> cfe/trunk/lib/Sema/SemaStmt.cpp
> cfe/trunk/test/Sema/switch-1.c
>
> Modified: cfe/trunk/include/clang/AST/Expr.h
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=173377&r1=173376&r2=173377&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/AST/Expr.h (original)
> +++ cfe/trunk/include/clang/AST/Expr.h Thu Jan 24 16:11:45 2013
> @@ -570,6 +570,9 @@
> /// integer.
> llvm::APSInt EvaluateKnownConstInt(const ASTContext &Ctx,
> SmallVectorImpl<PartialDiagnosticAt> *Diag=0) const;
> +
> + void EvaluateForOverflow(const ASTContext &Ctx,
> + SmallVectorImpl<PartialDiagnosticAt> *Diag) const;
>
> /// EvaluateAsLValue - Evaluate an expression to see if we can fold it to an
> /// lvalue with link time known address, with no side-effects.
>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticASTKinds.td
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticASTKinds.td?rev=173377&r1=173376&r2=173377&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/DiagnosticASTKinds.td (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticASTKinds.td Thu Jan 24 16:11:45 2013
> @@ -106,6 +106,9 @@
> "(skipping %0 call%s0 in backtrace; use -fconstexpr-backtrace-limit=0 to "
> "see all)">;
> def note_constexpr_call_here : Note<"in call to '%0'">;
> +def warn_integer_constant_overflow : Warning<
> + "overflow in case constant expression results in value %0 of type %1">,
> + InGroup<DiagGroup<"integer-overflow">>;
>
> // inline asm related.
> let CategoryName = "Inline Assembly Issue" in {
>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=173377&r1=173376&r2=173377&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Jan 24 16:11:45 2013
> @@ -5756,9 +5756,6 @@
> def warn_case_value_overflow : Warning<
> "overflow converting case value to switch condition type (%0 to %1)">,
> InGroup<Switch>;
> -def warn_case_constant_overflow : Warning<
> - "overflow in case constant expression results in value %0">,
> - InGroup<Switch>;
> 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'">;
>
> Modified: cfe/trunk/include/clang/Sema/Sema.h
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=173377&r1=173376&r2=173377&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/Sema/Sema.h (original)
> +++ cfe/trunk/include/clang/Sema/Sema.h Thu Jan 24 16:11:45 2013
> @@ -3993,7 +3993,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.
> @@ -7328,11 +7329,13 @@
> SourceLocation ReturnLoc);
> void CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr* RHS);
> void CheckImplicitConversions(Expr *E, SourceLocation CC = SourceLocation());
> + void CheckForIntOverflow(Expr *E);
> 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);
>
> Modified: cfe/trunk/lib/AST/ExprConstant.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=173377&r1=173376&r2=173377&view=diff
> ==============================================================================
> --- cfe/trunk/lib/AST/ExprConstant.cpp (original)
> +++ cfe/trunk/lib/AST/ExprConstant.cpp Thu Jan 24 16:11:45 2013
> @@ -384,13 +384,17 @@
> /// expression is a potential constant expression? If so, some diagnostics
> /// are suppressed.
> bool CheckingPotentialConstantExpression;
> +
> + bool IntOverflowCheckMode;
>
> - EvalInfo(const ASTContext &C, Expr::EvalStatus &S)
> + EvalInfo(const ASTContext &C, Expr::EvalStatus &S,
> + bool OverflowCheckMode=false)
> : Ctx(const_cast<ASTContext&>(C)), EvalStatus(S), CurrentCall(0),
> CallStackDepth(0), NextCallIndex(1),
> BottomFrame(*this, SourceLocation(), 0, 0, 0),
> EvaluatingDecl(0), EvaluatingDeclValue(0), HasActiveDiagnostic(false),
> - CheckingPotentialConstantExpression(false) {}
> + CheckingPotentialConstantExpression(false),
> + IntOverflowCheckMode(OverflowCheckMode) {}
>
> void setEvaluatingDecl(const VarDecl *VD, APValue &Value) {
> EvaluatingDecl = VD;
> @@ -474,6 +478,8 @@
> return OptionalDiagnostic();
> }
>
> + bool getIntOverflowCheckMode() { return IntOverflowCheckMode; }
> +
> /// Diagnose that the evaluation does not produce a C++11 core constant
> /// expression.
> template<typename LocArg>
> @@ -506,8 +512,12 @@
> /// Should we continue evaluation as much as possible after encountering a
> /// construct which can't be folded?
> bool keepEvaluatingAfterFailure() {
> - return CheckingPotentialConstantExpression &&
> - EvalStatus.Diag && EvalStatus.Diag->empty();
> + // Should return true in IntOverflowCheckMode, so that we check for
> + // overflow even if some subexpressions can't be evaluated as constants.
> + // subexpressions can't be evaluated as constants.
Hi Fariborz,
Duplicate phrase here: "subexpressions can't be evaluated as constants."
Dmitri
--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
More information about the cfe-commits
mailing list