[clang] fix error detection in consteval calls
authorTyker <tyker1@outlook.com>
Wed, 26 Feb 2020 17:59:09 +0000 (18:59 +0100)
committerTyker <tyker1@outlook.com>
Wed, 26 Feb 2020 20:09:31 +0000 (21:09 +0100)
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
clang/test/SemaCXX/cxx2a-consteval.cpp

index d07c324..f50a77a 100644 (file)
@@ -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))
index db6bca0..6d51b89 100644 (file)
@@ -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}}