From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2140) id A3FC7385829D; Tue, 25 Oct 2022 02:52:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A3FC7385829D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666666330; bh=lP/6JtSAkBTuRFVamSM2v6OZpLyCoPUPHTmfk1O1sd4=; h=From:To:Subject:Date:From; b=Hb9WRMGUIoFOrhWrRtM5SDheSh7UqtqCknIvB/JYuhy4qEpcamcnJyXuSbW/7JumL cBc2k8DWZQHAXVouyrsUNfUzcxsw0/FDPMeoUDldMI1eV7ahAIr2Iw7KJ76oHC7/Gz g7/HCFB+2jZ8m9AV+ivDiDbpnndmCr2r0RFNmGDs= 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: a917fac963436c8a3a033c2cb879d11d7a7ce034 X-Git-Newrev: 01df13453d3729b15f33266dff84912ac9371897 Message-Id: <20221025025210.A3FC7385829D@sourceware.org> Date: Tue, 25 Oct 2022 02:52:10 +0000 (GMT) List-Id: https://gcc.gnu.org/g:01df13453d3729b15f33266dff84912ac9371897 commit 01df13453d3729b15f33266dff84912ac9371897 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 bf11956c467..16f161ebf8a 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" } } */