From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2140) id 75B74383FBBE; Thu, 20 Oct 2022 22:32:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 75B74383FBBE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666305132; bh=VL33BB8wsdlcK3MJEHdyC9ltkVbapEvvkWfBWvAVFyQ=; h=From:To:Subject:Date:From; b=dN80C6qxP2CYNlsogl/CQpzgEQsxgLcHX6GujeR71n4inB0Yr0X/4ESgBBszPqCkE HFPkCsyhLi5MWl8vgyayCCqLA9lE6CPAjV1JGapC5c2kG8DrLvFCoy0SvF3Ed5uJy8 TMpxJUPhg4hJ1l84hT0LCEsL9ivq4+hi2DionOoA= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Alexandre Oliva To: gcc-cvs@gcc.gnu.org Subject: [gcc(refs/users/aoliva/heads/testme)] Expose expected_throw attribute X-Act-Checkin: gcc X-Git-Author: Alexandre Oliva X-Git-Refname: refs/users/aoliva/heads/testme X-Git-Oldrev: 11842645a31b166a7ac55bd2b2f7aa3ce6596ca8 X-Git-Newrev: 53ad4b10c5edbda6dca728a5b4d7738c7712e27a Message-Id: <20221020223212.75B74383FBBE@sourceware.org> Date: Thu, 20 Oct 2022 22:32:11 +0000 (GMT) List-Id: https://gcc.gnu.org/g:53ad4b10c5edbda6dca728a5b4d7738c7712e27a commit 53ad4b10c5edbda6dca728a5b4d7738c7712e27a Author: Alexandre Oliva 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 92ac93ba2ce..0d2bcf57616 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 }, @@ -5398,6 +5401,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 04af0584d82..3532728aa1a 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -2991,6 +2991,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. +@item expected_throw +@cindex @code{expected_throw} function attribute +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. + @item externally_visible @cindex @code{externally_visible} function attribute 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" } } */