public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Alexandre Oliva <aoliva@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc(refs/users/aoliva/heads/testme)] Expose expected_throw attribute
Date: Thu,  8 Jun 2023 09:17:26 +0000 (GMT)	[thread overview]
Message-ID: <20230608091726.7B0783857708@sourceware.org> (raw)

https://gcc.gnu.org/g:2b92db9168c78e900c80d30eae7e205dcb2008a9

commit 2b92db9168c78e900c80d30eae7e205dcb2008a9
Author: Alexandre Oliva <oliva@adacore.com>
Date:   Wed Oct 19 20:36:26 2022 -0300

    Expose expected_throw attribute
    
    Add support for explicit expected_throw attributes to be added in
    C-family languages.  Document it.
    
    
    for  gcc/ChangeLog
    
            * c-family/c-attribs.cc (handle_expected_throw_attribute):
            New.
            (c_common_attribute_table): Add expected_throw.
            * doc/extend.texi: Document it.
    
    for  gcc/testsuite/ChangeLog
    
            * g++.dg/torture/harden-cfr-thro-wnot-always-expected.C: New.

Diff:
---
 gcc/c-family/c-attribs.cc                          | 22 ++++++++++++++++++++++
 gcc/doc/extend.texi                                | 11 +++++++++++
 .../torture/harden-cfr-throw-not-always-expected.C | 16 ++++++++++++++++
 3 files changed, 49 insertions(+)

diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
index e2792ca6898..c12211cb4d4 100644
--- a/gcc/c-family/c-attribs.cc
+++ b/gcc/c-family/c-attribs.cc
@@ -136,6 +136,7 @@ static tree handle_vector_mask_attribute (tree *, tree, tree, int,
 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
 static tree handle_nonstring_attribute (tree *, tree, tree, int, bool *);
 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
+static tree handle_expected_throw_attribute (tree *, tree, tree, int, bool *);
 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
 						 bool *);
@@ -437,6 +438,8 @@ const struct attribute_spec c_common_attribute_table[] =
 			      handle_nonstring_attribute, NULL },
   { "nothrow",                0, 0, true,  false, false, false,
 			      handle_nothrow_attribute, NULL },
+  { "expected_throw",         0, 0, true,  false, false, false,
+			      handle_expected_throw_attribute, NULL },
   { "may_alias",	      0, 0, false, true, false, false, NULL, NULL },
   { "cleanup",		      1, 1, true, false, false, false,
 			      handle_cleanup_attribute, NULL },
@@ -5412,6 +5415,25 @@ handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
   return NULL_TREE;
 }
 
+/* Handle a "nothrow" attribute; arguments as in
+   struct attribute_spec.handler.  */
+
+static tree
+handle_expected_throw_attribute (tree *node, tree name, tree ARG_UNUSED (args),
+				 int ARG_UNUSED (flags), bool *no_add_attrs)
+{
+  if (TREE_CODE (*node) == FUNCTION_DECL)
+    /* No flag to set here.  */;
+  /* ??? TODO: Support types.  */
+  else
+    {
+      warning (OPT_Wattributes, "%qE attribute ignored", name);
+      *no_add_attrs = true;
+    }
+
+  return NULL_TREE;
+}
+
 /* Handle a "cleanup" attribute; arguments as in
    struct attribute_spec.handler.  */
 
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index cdbd4b34a35..2de212c8c2d 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -3000,6 +3000,17 @@ when using these attributes the problem is diagnosed
 earlier and with exact location of the call even in presence of inline
 functions or when not emitting debugging information.
 
+@cindex @code{expected_throw} function attribute
+@item expected_throw
+This attribute, attached to a function, tells the compiler the function
+is more likely to raise or propagate an exception than to return, loop
+forever, or terminate the program.
+
+This hint is mostly ignored by the compiler.  The only effect is when
+it's applied to @code{noreturn} functions and
+@samp{-fharden-control-flow-redundancy} is enabled, and
+@samp{-fhardcfr-check-noreturn-calls=not-always} is not overridden.
+
 @cindex @code{externally_visible} function attribute
 @item externally_visible
 This attribute, attached to a global variable or function, nullifies
diff --git a/gcc/testsuite/g++.dg/torture/harden-cfr-throw-not-always-expected.C b/gcc/testsuite/g++.dg/torture/harden-cfr-throw-not-always-expected.C
new file mode 100644
index 00000000000..f42f870bdcf
--- /dev/null
+++ b/gcc/testsuite/g++.dg/torture/harden-cfr-throw-not-always-expected.C
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-fharden-control-flow-redundancy -fno-hardcfr-check-returning-calls -fhardcfr-check-noreturn-calls=not-always -fdump-tree-hardcfr -ffat-lto-objects" } */
+
+/* Check that we insert cleanups for checking around the bodies of
+   maybe-throwing functions, and also checking before noreturn
+   calls.  */
+
+extern void __attribute__ ((__noreturn__, __expected_throw__)) g (void);
+extern void __attribute__ ((__noreturn__, __expected_throw__)) g2 (void);
+
+#include "harden-cfr-throw.C"
+
+/* In f and h3, there are checkpoints at return and exception escape.  .  */
+/* { dg-final { scan-tree-dump-times "hardcfr_check" 4 "hardcfr" } } */
+/* Other functions get a single cleanup checkpoint.  */
+/* { dg-final { scan-tree-dump-times "builtin_trap" 5 "hardcfr" } } */

             reply	other threads:[~2023-06-08  9:17 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-08  9:17 Alexandre Oliva [this message]
  -- strict thread matches above, loose matches on Subject: below --
2023-06-23 20:13 Alexandre Oliva
2023-06-23 20:12 Alexandre Oliva
2023-06-09  8:07 Alexandre Oliva
2023-06-09  6:17 Alexandre Oliva
2023-06-09  6:16 Alexandre Oliva
2023-06-08 10:58 Alexandre Oliva
2023-06-08 10:58 Alexandre Oliva
2023-06-08 10:43 Alexandre Oliva
2023-06-08 10:43 Alexandre Oliva
2023-06-08  9:17 Alexandre Oliva
2023-06-08  4:47 Alexandre Oliva
2023-06-08  4:47 Alexandre Oliva
2022-10-25  2:52 Alexandre Oliva
2022-10-25  2:52 Alexandre Oliva
2022-10-20 22:32 Alexandre Oliva
2022-10-20 22:32 Alexandre Oliva
2022-10-20  4:09 Alexandre Oliva
2022-10-20  4:09 Alexandre Oliva
2022-10-20  4:09 Alexandre Oliva

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230608091726.7B0783857708@sourceware.org \
    --to=aoliva@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).