constexpr.c (cxx_eval_constant_expression): If not skipping upon entry to body...
authorJakub Jelinek <jakub@redhat.com>
Wed, 2 Oct 2019 22:32:56 +0000 (00:32 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Wed, 2 Oct 2019 22:32:56 +0000 (00:32 +0200)
* constexpr.c (cxx_eval_constant_expression) <case CLEANUP_STMT>: If
not skipping upon entry to body, run cleanup with the same *jump_target
as it started to run the cleanup even if the body returns, breaks or
continues.
(potential_constant_expression_1): Allow CLEANUP_STMT.

* g++.dg/ext/constexpr-attr-cleanup1.C: New test.

From-SVN: r276494

gcc/cp/ChangeLog
gcc/cp/constexpr.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/ext/constexpr-attr-cleanup1.C [new file with mode: 0644]

index f2c1aa2363da3988900e2b25fd0d8a85e05631e6..72790626500a5ef03194142e22e1ea3c2a82cc00 100644 (file)
@@ -1,5 +1,11 @@
 2019-10-03  Jakub Jelinek  <jakub@redhat.com>
 
+       * constexpr.c (cxx_eval_constant_expression) <case CLEANUP_STMT>: If
+       not skipping upon entry to body, run cleanup with the same *jump_target
+       as it started to run the cleanup even if the body returns, breaks or
+       continues.
+       (potential_constant_expression_1): Allow CLEANUP_STMT.
+
        * constexpr.c (cxx_eval_store_expression): Formatting fix.  Handle
        const_object_being_modified with array type.
 
index a6a55b940dd691a18ba10ce61d152a414249557c..200879378c532af47a51edb60c5d9b394ce8f22c 100644 (file)
@@ -4899,14 +4899,21 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
       break;
 
     case CLEANUP_STMT:
-      r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
+      {
+       tree initial_jump_target = jump_target ? *jump_target : NULL_TREE;
+       r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
+                                         non_constant_p, overflow_p,
+                                         jump_target);
+       if (!CLEANUP_EH_ONLY (t) && !*non_constant_p)
+         /* Also evaluate the cleanup.  If we weren't skipping at the
+            start of the CLEANUP_BODY, change jump_target temporarily
+            to &initial_jump_target, so that even a return or break or
+            continue in the body doesn't skip the cleanup.  */
+         cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), true,
                                        non_constant_p, overflow_p,
-                                       jump_target);
-      if (!CLEANUP_EH_ONLY (t) && !*non_constant_p)
-       /* Also evaluate the cleanup.  */
-       cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), true,
-                                     non_constant_p, overflow_p,
-                                     jump_target);
+                                       jump_target ? &initial_jump_target
+                                       : NULL);
+      }
       break;
 
       /* These differ from cxx_eval_unary_expression in that this doesn't
@@ -6975,6 +6982,12 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
       return true;
 
     case CLEANUP_STMT:
+      if (!RECUR (CLEANUP_BODY (t), any))
+       return false;
+      if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
+       return false;
+      return true;
+
     case EMPTY_CLASS_EXPR:
     case PREDICT_EXPR:
       return false;
index d321861da9994c44906f8f673a738fe7a4ac3cb2..b9fcbb08332861c397b76a08867ec40468d3a3a5 100644 (file)
@@ -1,3 +1,7 @@
+2019-10-03  Jakub Jelinek  <jakub@redhat.com>
+
+       * g++.dg/ext/constexpr-attr-cleanup1.C: New test.
+
 2019-10-02  Martin Sebor  <msebor@redhat.com>
 
        PR tree-optimization/80936
diff --git a/gcc/testsuite/g++.dg/ext/constexpr-attr-cleanup1.C b/gcc/testsuite/g++.dg/ext/constexpr-attr-cleanup1.C
new file mode 100644 (file)
index 0000000..2c4f61a
--- /dev/null
@@ -0,0 +1,30 @@
+// { dg-do compile { target c++2a } }
+
+constexpr void
+cleanup (int *x)
+{
+  if (x)
+    asm ("");          // { dg-error "inline assembly is not a constant expression" }
+}                      // { dg-message "only unevaluated inline assembly is allowed in a 'constexpr' function" "" { target *-*-* } .-1 }
+
+constexpr void
+cleanup2 (int *x)
+{
+}
+
+constexpr bool
+foo ()
+{
+  int a __attribute__((cleanup (cleanup))) = 1;
+  return true;
+}
+
+constexpr bool
+bar ()
+{
+  int a __attribute__((cleanup (cleanup2))) = 1;
+  return true;
+}
+
+constexpr auto x = foo ();     // { dg-message "in 'constexpr' expansion of" }
+constexpr auto y = bar ();