From ca50f09db9f86eb5ac7aa0cb53c52637f2b7effa Mon Sep 17 00:00:00 2001 From: Tyker Date: Wed, 26 Feb 2020 18:59:09 +0100 Subject: [PATCH] [clang] fix error detection in consteval calls Summary: code like: ``` consteval int f() { int *A = new int(0); return *A; } int i1 = f(); ``` currently doesn't generate any error. Reviewers: rsmith Reviewed By: rsmith Differential Revision: https://reviews.llvm.org/D74418 --- clang/lib/Sema/SemaExpr.cpp | 5 +++-- clang/test/SemaCXX/cxx2a-consteval.cpp | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index d07c324..f50a77a 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -15368,8 +15368,9 @@ static void EvaluateAndDiagnoseImmediateInvocation( Expr::EvalResult Eval; Eval.Diag = &Notes; ConstantExpr *CE = Candidate.getPointer(); - if (!CE->EvaluateAsConstantExpr(Eval, Expr::EvaluateForCodeGen, - SemaRef.getASTContext(), true)) { + bool Result = CE->EvaluateAsConstantExpr(Eval, Expr::EvaluateForCodeGen, + SemaRef.getASTContext(), true); + if (!Result || !Notes.empty()) { Expr *InnerExpr = CE->getSubExpr()->IgnoreImplicit(); FunctionDecl *FD = nullptr; if (auto *Call = dyn_cast(InnerExpr)) diff --git a/clang/test/SemaCXX/cxx2a-consteval.cpp b/clang/test/SemaCXX/cxx2a-consteval.cpp index db6bca0..6d51b89 100644 --- a/clang/test/SemaCXX/cxx2a-consteval.cpp +++ b/clang/test/SemaCXX/cxx2a-consteval.cpp @@ -309,6 +309,14 @@ void test() { namespace alloc { +consteval int f() { + int *A = new int(0); +// expected-note@-1+ {{allocation performed here was not deallocated}} + return *A; +} + +int i1 = f(); // expected-error {{is not a constant expression}} + struct A { int* p = new int(42); // expected-note@-1+ {{heap allocation performed here}} -- 2.7.4