From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2140) id 4D45A3857719; Thu, 8 Jun 2023 10:43:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4D45A3857719 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1686220988; bh=9YiJ6AQu/xCVNAw1Bm03flvzlrjIoxvZuujgc2lXElA=; h=From:To:Subject:Date:From; b=rSalGEft58hFeAuJwE9pSrvp74YUGKxvcc4HJtrKC7keLKot5vH0ZhXk7LnqtliUA e8IRK2DVxlu+Dfy6AnEPdWexEWzOKwFE6BX38uJU5b2eKAwTqabZEkAl51a8o/2vVp cOe4kp5ZgTOcZDjl6GOjpnfDtK81VlFRHxnk2TtQ= 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: 20f802affb148b8ae67f1046969a58e401bb92d9 X-Git-Newrev: 14952c214d032162b891479b490044a6491574a9 Message-Id: <20230608104308.4D45A3857719@sourceware.org> Date: Thu, 8 Jun 2023 10:43:08 +0000 (GMT) List-Id: https://gcc.gnu.org/g:14952c214d032162b891479b490044a6491574a9 commit 14952c214d032162b891479b490044a6491574a9 Author: Alexandre Oliva Date: Thu Jun 8 01:35:20 2023 -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" } } */