[clang] ca50f09 - [clang] fix error detection in consteval calls

via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 26 12:10:02 PST 2020


Author: Tyker
Date: 2020-02-26T21:09:31+01:00
New Revision: ca50f09db9f86eb5ac7aa0cb53c52637f2b7effa

URL: https://github.com/llvm/llvm-project/commit/ca50f09db9f86eb5ac7aa0cb53c52637f2b7effa
DIFF: https://github.com/llvm/llvm-project/commit/ca50f09db9f86eb5ac7aa0cb53c52637f2b7effa.diff

LOG: [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

Added: 
    

Modified: 
    clang/lib/Sema/SemaExpr.cpp
    clang/test/SemaCXX/cxx2a-consteval.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index d07c32487c13..f50a77a40510 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<CallExpr>(InnerExpr))

diff  --git a/clang/test/SemaCXX/cxx2a-consteval.cpp b/clang/test/SemaCXX/cxx2a-consteval.cpp
index db6bca03fcc3..6d51b895cbd2 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 at -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 at -1+ {{heap allocation performed here}}


        


More information about the cfe-commits mailing list