From f83586caa9df7ebae5226227614513df4af4b402 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Sat, 29 Apr 2017 09:33:46 +0000 Subject: [PATCH] Remove Sema::CheckForIntOverflow, and instead check all full-expressions. CheckForIntOverflow used to implement a whitelist of top-level expressions to send to the constant expression evaluator, which handled many more expressions than the CheckForIntOverflow whitelist did. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@301742 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Sema/Sema.h | 1 - lib/AST/ExprConstant.cpp | 13 ++++++---- lib/Sema/SemaChecking.cpp | 24 +------------------ ...ute_parallel_for_simd_aligned_messages.cpp | 5 ++-- .../distribute_simd_aligned_messages.cpp | 5 ++-- test/OpenMP/for_simd_aligned_messages.cpp | 5 ++-- .../parallel_for_simd_aligned_messages.cpp | 5 ++-- test/OpenMP/simd_aligned_messages.cpp | 5 ++-- ...get_parallel_for_simd_aligned_messages.cpp | 5 ++-- test/OpenMP/target_simd_aligned_messages.cpp | 5 ++-- ...ute_parallel_for_simd_aligned_messages.cpp | 5 ++-- ...teams_distribute_simd_aligned_messages.cpp | 5 ++-- .../OpenMP/taskloop_simd_aligned_messages.cpp | 5 ++-- ...ute_parallel_for_simd_aligned_messages.cpp | 5 ++-- ...teams_distribute_simd_aligned_messages.cpp | 5 ++-- test/Sema/integer-overflow.c | 8 ++++++- 16 files changed, 41 insertions(+), 65 deletions(-) diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index 19c42a8c56..e24d6436f3 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -10160,7 +10160,6 @@ private: void CheckFloatComparison(SourceLocation Loc, Expr* LHS, Expr* RHS); void CheckImplicitConversions(Expr *E, SourceLocation CC = SourceLocation()); void CheckBoolLikeConversion(Expr *E, SourceLocation CC); - void CheckForIntOverflow(Expr *E); void CheckUnsequencedOperations(Expr *E); /// \brief Perform semantic checks on a completed expression. This will either diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp index dcbbe8a944..75bb0cac51 100644 --- a/lib/AST/ExprConstant.cpp +++ b/lib/AST/ExprConstant.cpp @@ -6217,6 +6217,10 @@ bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { // the initializer list. ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); const Expr *Init = HaveInit ? E->getInit(ElementNo++) : &VIE; + if (Init->isValueDependent()) { + Success = false; + continue; + } // Temporarily override This, in case there's a CXXDefaultInitExpr in here. ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, @@ -9927,7 +9931,8 @@ static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { } static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result, - const ASTContext &Ctx, bool &IsConst) { + const ASTContext &Ctx, bool &IsConst, + bool IsCheckingForOverflow) { // Fast-path evaluations of integer literals, since we sometimes see files // containing vast quantities of these. if (const IntegerLiteral *L = dyn_cast(Exp)) { @@ -9948,7 +9953,7 @@ static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result, // performance problems. Only do so in C++11 for now. if (Exp->isRValue() && (Exp->getType()->isArrayType() || Exp->getType()->isRecordType()) && - !Ctx.getLangOpts().CPlusPlus11) { + !Ctx.getLangOpts().CPlusPlus11 && !IsCheckingForOverflow) { IsConst = false; return true; } @@ -9963,7 +9968,7 @@ static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result, /// will be applied to the result. bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { bool IsConst; - if (FastEvaluateAsRValue(this, Result, Ctx, IsConst)) + if (FastEvaluateAsRValue(this, Result, Ctx, IsConst, false)) return IsConst; EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); @@ -10088,7 +10093,7 @@ APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx, void Expr::EvaluateForOverflow(const ASTContext &Ctx) const { bool IsConst; EvalResult EvalResult; - if (!FastEvaluateAsRValue(this, EvalResult, Ctx, IsConst)) { + if (!FastEvaluateAsRValue(this, EvalResult, Ctx, IsConst, true)) { EvalInfo Info(Ctx, EvalResult, EvalInfo::EM_EvaluateForOverflow); (void)::EvaluateAsRValue(Info, this, EvalResult.Val); } diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index ea6f16e1c8..b3ba86e068 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -9866,28 +9866,6 @@ void Sema::CheckBoolLikeConversion(Expr *E, SourceLocation CC) { ::CheckBoolLikeConversion(*this, E, CC); } -/// Diagnose when expression is an integer constant expression and its evaluation -/// results in integer overflow -void Sema::CheckForIntOverflow (Expr *E) { - // Use a work list to deal with nested struct initializers. - SmallVector Exprs(1, E); - - do { - Expr *E = Exprs.pop_back_val(); - - if (isa(E->IgnoreParenCasts())) { - E->IgnoreParenCasts()->EvaluateForOverflow(Context); - continue; - } - - if (auto InitList = dyn_cast(E)) - Exprs.append(InitList->inits().begin(), InitList->inits().end()); - - if (isa(E)) - E->IgnoreParenCasts()->EvaluateForOverflow(Context); - } while (!Exprs.empty()); -} - namespace { /// \brief Visitor for expressions which looks for unsequenced operations on the /// same object. @@ -10389,7 +10367,7 @@ void Sema::CheckCompletedExpr(Expr *E, SourceLocation CheckLoc, if (!E->isInstantiationDependent()) CheckUnsequencedOperations(E); if (!IsConstexpr && !E->isValueDependent()) - CheckForIntOverflow(E); + E->EvaluateForOverflow(Context); DiagnoseMisalignedMembers(); } diff --git a/test/OpenMP/distribute_parallel_for_simd_aligned_messages.cpp b/test/OpenMP/distribute_parallel_for_simd_aligned_messages.cpp index f865e0be42..2d99d7d8f4 100644 --- a/test/OpenMP/distribute_parallel_for_simd_aligned_messages.cpp +++ b/test/OpenMP/distribute_parallel_for_simd_aligned_messages.cpp @@ -134,9 +134,8 @@ S3 h; // expected-note 2 {{'h' defined here}} template int foomain(I argc, C **argv) { I e(argc); I g(argc); - int i; // expected-note {{declared here}} expected-note {{'i' defined here}} - // expected-note@+2 {{declared here}} - // expected-note@+1 {{reference to 'i' is not a constant expression}} + int i; // expected-note {{'i' defined here}} + // expected-note@+1 {{declared here}} int &j = i; #pragma omp target diff --git a/test/OpenMP/distribute_simd_aligned_messages.cpp b/test/OpenMP/distribute_simd_aligned_messages.cpp index 10beb7198a..21b1bc9da1 100644 --- a/test/OpenMP/distribute_simd_aligned_messages.cpp +++ b/test/OpenMP/distribute_simd_aligned_messages.cpp @@ -134,9 +134,8 @@ S3 h; // expected-note 2 {{'h' defined here}} template int foomain(I argc, C **argv) { I e(argc); I g(argc); - int i; // expected-note {{declared here}} expected-note {{'i' defined here}} - // expected-note@+2 {{declared here}} - // expected-note@+1 {{reference to 'i' is not a constant expression}} + int i; // expected-note {{'i' defined here}} + // expected-note@+1 {{declared here}} int &j = i; #pragma omp target diff --git a/test/OpenMP/for_simd_aligned_messages.cpp b/test/OpenMP/for_simd_aligned_messages.cpp index 1d0918e449..3ff25870e5 100644 --- a/test/OpenMP/for_simd_aligned_messages.cpp +++ b/test/OpenMP/for_simd_aligned_messages.cpp @@ -107,9 +107,8 @@ S3 h; // expected-note 2 {{'h' defined here}} template int foomain(I argc, C **argv) { I e(argc); I g(argc); - int i; // expected-note {{declared here}} expected-note {{'i' defined here}} - // expected-note@+2 {{declared here}} - // expected-note@+1 {{reference to 'i' is not a constant expression}} + int i; // expected-note {{'i' defined here}} + // expected-note@+1 {{declared here}} int &j = i; #pragma omp for simd aligned // expected-error {{expected '(' after 'aligned'}} for (I k = 0; k < argc; ++k) ++k; diff --git a/test/OpenMP/parallel_for_simd_aligned_messages.cpp b/test/OpenMP/parallel_for_simd_aligned_messages.cpp index fc0f88cc38..eed8017003 100644 --- a/test/OpenMP/parallel_for_simd_aligned_messages.cpp +++ b/test/OpenMP/parallel_for_simd_aligned_messages.cpp @@ -107,9 +107,8 @@ S3 h; // expected-note 2 {{'h' defined here}} template int foomain(I argc, C **argv) { I e(argc); I g(argc); - int i; // expected-note {{declared here}} expected-note {{'i' defined here}} - // expected-note@+2 {{declared here}} - // expected-note@+1 {{reference to 'i' is not a constant expression}} + int i; // expected-note {{'i' defined here}} + // expected-note@+1 {{declared here}} int &j = i; #pragma omp parallel for simd aligned // expected-error {{expected '(' after 'aligned'}} for (I k = 0; k < argc; ++k) ++k; diff --git a/test/OpenMP/simd_aligned_messages.cpp b/test/OpenMP/simd_aligned_messages.cpp index 81aec960f2..d936fed014 100644 --- a/test/OpenMP/simd_aligned_messages.cpp +++ b/test/OpenMP/simd_aligned_messages.cpp @@ -107,9 +107,8 @@ S3 h; // expected-note 2 {{'h' defined here}} template int foomain(I argc, C **argv) { I e(argc); I g(argc); - int i; // expected-note {{declared here}} expected-note {{'i' defined here}} - // expected-note@+2 {{declared here}} - // expected-note@+1 {{reference to 'i' is not a constant expression}} + int i; // expected-note {{'i' defined here}} + // expected-note@+1 {{declared here}} int &j = i; #pragma omp simd aligned // expected-error {{expected '(' after 'aligned'}} for (I k = 0; k < argc; ++k) ++k; diff --git a/test/OpenMP/target_parallel_for_simd_aligned_messages.cpp b/test/OpenMP/target_parallel_for_simd_aligned_messages.cpp index 538d65b82b..655f90642f 100644 --- a/test/OpenMP/target_parallel_for_simd_aligned_messages.cpp +++ b/test/OpenMP/target_parallel_for_simd_aligned_messages.cpp @@ -107,9 +107,8 @@ S3 h; // expected-note 2 {{'h' defined here}} template int foomain(I argc, C **argv) { I e(argc); I g(argc); - int i; // expected-note {{declared here}} expected-note {{'i' defined here}} - // expected-note@+2 {{declared here}} - // expected-note@+1 {{reference to 'i' is not a constant expression}} + int i; // expected-note {{'i' defined here}} + // expected-note@+1 {{declared here}} int &j = i; #pragma omp target parallel for simd aligned // expected-error {{expected '(' after 'aligned'}} for (I k = 0; k < argc; ++k) ++k; diff --git a/test/OpenMP/target_simd_aligned_messages.cpp b/test/OpenMP/target_simd_aligned_messages.cpp index ae2859d5b6..76a37f6498 100644 --- a/test/OpenMP/target_simd_aligned_messages.cpp +++ b/test/OpenMP/target_simd_aligned_messages.cpp @@ -107,9 +107,8 @@ S3 h; // expected-note 2 {{'h' defined here}} template int foomain(I argc, C **argv) { I e(argc); I g(argc); - int i; // expected-note {{declared here}} expected-note {{'i' defined here}} - // expected-note@+2 {{declared here}} - // expected-note@+1 {{reference to 'i' is not a constant expression}} + int i; // expected-note {{'i' defined here}} + // expected-note@+1 {{declared here}} int &j = i; #pragma omp target simd aligned // expected-error {{expected '(' after 'aligned'}} for (I k = 0; k < argc; ++k) ++k; diff --git a/test/OpenMP/target_teams_distribute_parallel_for_simd_aligned_messages.cpp b/test/OpenMP/target_teams_distribute_parallel_for_simd_aligned_messages.cpp index df5468c826..f121512733 100644 --- a/test/OpenMP/target_teams_distribute_parallel_for_simd_aligned_messages.cpp +++ b/test/OpenMP/target_teams_distribute_parallel_for_simd_aligned_messages.cpp @@ -110,9 +110,8 @@ S3 h; // expected-note 2 {{'h' defined here}} template int foomain(I argc, C **argv) { I e(argc); I g(argc); - int i; // expected-note {{declared here}} expected-note {{'i' defined here}} - // expected-note@+2 {{declared here}} - // expected-note@+1 {{reference to 'i' is not a constant expression}} + int i; // expected-note {{'i' defined here}} + // expected-note@+1 {{declared here}} int &j = i; #pragma omp target teams distribute parallel for simd aligned // expected-error {{expected '(' after 'aligned'}} diff --git a/test/OpenMP/target_teams_distribute_simd_aligned_messages.cpp b/test/OpenMP/target_teams_distribute_simd_aligned_messages.cpp index e9df563ded..d91cfa9b54 100644 --- a/test/OpenMP/target_teams_distribute_simd_aligned_messages.cpp +++ b/test/OpenMP/target_teams_distribute_simd_aligned_messages.cpp @@ -110,9 +110,8 @@ S3 h; // expected-note 2 {{'h' defined here}} template int foomain(I argc, C **argv) { I e(argc); I g(argc); - int i; // expected-note {{declared here}} expected-note {{'i' defined here}} - // expected-note@+2 {{declared here}} - // expected-note@+1 {{reference to 'i' is not a constant expression}} + int i; // expected-note {{'i' defined here}} + // expected-note@+1 {{declared here}} int &j = i; #pragma omp target teams distribute simd aligned // expected-error {{expected '(' after 'aligned'}} diff --git a/test/OpenMP/taskloop_simd_aligned_messages.cpp b/test/OpenMP/taskloop_simd_aligned_messages.cpp index 6085660b70..c4c41ebbaf 100644 --- a/test/OpenMP/taskloop_simd_aligned_messages.cpp +++ b/test/OpenMP/taskloop_simd_aligned_messages.cpp @@ -107,9 +107,8 @@ S3 h; // expected-note 2 {{'h' defined here}} template int foomain(I argc, C **argv) { I e(argc); I g(argc); - int i; // expected-note {{declared here}} expected-note {{'i' defined here}} - // expected-note@+2 {{declared here}} - // expected-note@+1 {{reference to 'i' is not a constant expression}} + int i; // expected-note {{'i' defined here}} + // expected-note@+1 {{declared here}} int &j = i; #pragma omp taskloop simd aligned // expected-error {{expected '(' after 'aligned'}} for (I k = 0; k < argc; ++k) ++k; diff --git a/test/OpenMP/teams_distribute_parallel_for_simd_aligned_messages.cpp b/test/OpenMP/teams_distribute_parallel_for_simd_aligned_messages.cpp index 674057712e..97b200bb16 100644 --- a/test/OpenMP/teams_distribute_parallel_for_simd_aligned_messages.cpp +++ b/test/OpenMP/teams_distribute_parallel_for_simd_aligned_messages.cpp @@ -123,9 +123,8 @@ S3 h; // expected-note 2 {{'h' defined here}} template int foomain(I argc, C **argv) { I e(argc); I g(argc); - int i; // expected-note {{declared here}} expected-note {{'i' defined here}} - // expected-note@+2 {{declared here}} - // expected-note@+1 {{reference to 'i' is not a constant expression}} + int i; // expected-note {{'i' defined here}} + // expected-note@+1 {{declared here}} int &j = i; #pragma omp target diff --git a/test/OpenMP/teams_distribute_simd_aligned_messages.cpp b/test/OpenMP/teams_distribute_simd_aligned_messages.cpp index 95a85b2d0d..885432bb9a 100644 --- a/test/OpenMP/teams_distribute_simd_aligned_messages.cpp +++ b/test/OpenMP/teams_distribute_simd_aligned_messages.cpp @@ -123,9 +123,8 @@ S3 h; // expected-note 2 {{'h' defined here}} template int foomain(I argc, C **argv) { I e(argc); I g(argc); - int i; // expected-note {{declared here}} expected-note {{'i' defined here}} - // expected-note@+2 {{declared here}} - // expected-note@+1 {{reference to 'i' is not a constant expression}} + int i; // expected-note {{'i' defined here}} + // expected-note@+1 {{declared here}} int &j = i; #pragma omp target diff --git a/test/Sema/integer-overflow.c b/test/Sema/integer-overflow.c index 44c2629ebf..62ee33e3d1 100644 --- a/test/Sema/integer-overflow.c +++ b/test/Sema/integer-overflow.c @@ -152,7 +152,13 @@ uint64_t check_integer_overflows(int i) { uint64_t b2 = b[4608 * 1024 * 1024] + 1; // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}} - (void)((i ? (4608 * 1024 * 1024) : (4608 * 1024 * 1024)) + 1); + int j1 = i ? (4608 * 1024 * 1024) : (4608 * 1024 * 1024); + +// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} + int j2 = -(4608 * 1024 * 1024); + +// expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}} + uint64_t j3 = b[4608 * 1024 * 1024]; // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}} return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024))); -- 2.40.0