From patchwork Sat Nov 13 20:37:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 47619 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0626D3858410 for ; Sat, 13 Nov 2021 20:38:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0626D3858410 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1636835894; bh=f4T7gGs0/F73buSzDODMeMiO1sj9UJsLvOu0IwdWUiQ=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=g+xM0QZ8NPCh4Hqrj6v+f0YZO0xSb5IPU6rE6MtHfVaU/NQZ3FavLVGsLeUk2BN01 3CSw+0wZVPdXGNMGU/cKb6N1GpIwCzs0mp6dyo04QTNUVPrejQFdjmmCe0bK98raTv APq/TJHJv0Gi1yCS2Q8GdRSueRlxhWNh8UgdgPFo= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTPS id E98AC3858410 for ; Sat, 13 Nov 2021 20:37:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E98AC3858410 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-591-5Y027zXeNIiaTJImO0XgQg-1; Sat, 13 Nov 2021 15:37:42 -0500 X-MC-Unique: 5Y027zXeNIiaTJImO0XgQg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6CA5415720; Sat, 13 Nov 2021 20:37:41 +0000 (UTC) Received: from t14s.localdomain.com (ovpn-113-54.phx2.redhat.com [10.3.113.54]) by smtp.corp.redhat.com (Postfix) with ESMTP id F015E19723; Sat, 13 Nov 2021 20:37:40 +0000 (UTC) To: gcc-patches@gcc.gnu.org, linux-toolchains@vger.kernel.org Subject: [PATCH 2/6] Add returns_zero_on_success/failure attributes Date: Sat, 13 Nov 2021 15:37:27 -0500 Message-Id: <20211113203732.2098220-4-dmalcolm@redhat.com> In-Reply-To: <20211113203732.2098220-1-dmalcolm@redhat.com> References: <20211113203732.2098220-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: David Malcolm via Gcc-patches From: David Malcolm Reply-To: David Malcolm Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This patch adds two new attributes. The followup patch makes use of the attributes in -fanalyzer. gcc/c-family/ChangeLog: * c-attribs.c (attr_noreturn_exclusions): Add "returns_zero_on_failure" and "returns_zero_on_success". (attr_returns_twice_exclusions): Likewise. (attr_returns_zero_on_exclusions): New. (c_common_attribute_table): Add "returns_zero_on_failure" and "returns_zero_on_success". (handle_returns_zero_on_attributes): New. gcc/ChangeLog: * doc/extend.texi (Common Function Attributes): Document "returns_zero_on_failure" and "returns_zero_on_success". gcc/testsuite/ChangeLog: * c-c++-common/attr-returns-zero-on-1.c: New test. Signed-off-by: David Malcolm --- gcc/c-family/c-attribs.c | 37 ++++++++++ gcc/doc/extend.texi | 16 +++++ .../c-c++-common/attr-returns-zero-on-1.c | 68 +++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 gcc/testsuite/c-c++-common/attr-returns-zero-on-1.c diff --git a/gcc/c-family/c-attribs.c b/gcc/c-family/c-attribs.c index 100c2dabab2..9e03156de5e 100644 --- a/gcc/c-family/c-attribs.c +++ b/gcc/c-family/c-attribs.c @@ -153,6 +153,7 @@ static tree handle_argspec_attribute (tree *, tree, tree, int, bool *); static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *); static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *); static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *); +static tree handle_returns_zero_on_attributes (tree *, tree, tree, int, bool *); static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *); static tree handle_omp_declare_variant_attribute (tree *, tree, tree, int, @@ -221,6 +222,8 @@ extern const struct attribute_spec::exclusions attr_noreturn_exclusions[] = ATTR_EXCL ("pure", true, true, true), ATTR_EXCL ("returns_twice", true, true, true), ATTR_EXCL ("warn_unused_result", true, true, true), + ATTR_EXCL ("returns_zero_on_failure", true, true, true), + ATTR_EXCL ("returns_zero_on_success", true, true, true), ATTR_EXCL (NULL, false, false, false), }; @@ -235,6 +238,8 @@ attr_warn_unused_result_exclusions[] = static const struct attribute_spec::exclusions attr_returns_twice_exclusions[] = { ATTR_EXCL ("noreturn", true, true, true), + ATTR_EXCL ("returns_zero_on_failure", true, true, true), + ATTR_EXCL ("returns_zero_on_success", true, true, true), ATTR_EXCL (NULL, false, false, false), }; @@ -275,6 +280,16 @@ static const struct attribute_spec::exclusions attr_stack_protect_exclusions[] = ATTR_EXCL (NULL, false, false, false), }; +/* Exclusions that apply to the returns_zero_on_* attributes. */ +static const struct attribute_spec::exclusions + attr_returns_zero_on_exclusions[] = +{ + ATTR_EXCL ("noreturn", true, true, true), + ATTR_EXCL ("returns_twice", true, true, true), + ATTR_EXCL ("returns_zero_on_failure", true, true, true), + ATTR_EXCL ("returns_zero_on_success", true, true, true), + ATTR_EXCL (NULL, false, false, false), +}; /* Table of machine-independent attributes common to all C-like languages. @@ -493,6 +508,12 @@ const struct attribute_spec c_common_attribute_table[] = handle_warn_unused_attribute, NULL }, { "returns_nonnull", 0, 0, false, true, true, false, handle_returns_nonnull_attribute, NULL }, + { "returns_zero_on_failure",0, 0, false, true, true, false, + handle_returns_zero_on_attributes, + attr_returns_zero_on_exclusions }, + { "returns_zero_on_success",0, 0, false, true, true, false, + handle_returns_zero_on_attributes, + attr_returns_zero_on_exclusions }, { "omp declare simd", 0, -1, true, false, false, false, handle_omp_declare_simd_attribute, NULL }, { "omp declare variant base", 0, -1, true, false, false, false, @@ -5660,6 +5681,22 @@ handle_returns_nonnull_attribute (tree *node, tree name, tree, int, return NULL_TREE; } +/* Handle "returns_zero_on_failure" and "returns_zero_on_success" attributes; + arguments as in struct attribute_spec.handler. */ + +static tree +handle_returns_zero_on_attributes (tree *node, tree name, tree, int, + bool *no_add_attrs) +{ + if (!INTEGRAL_TYPE_P (TREE_TYPE (*node))) + { + error ("%qE attribute on a function not returning an integral type", + name); + *no_add_attrs = true; + } + return NULL_TREE; +} + /* Handle a "designated_init" attribute; arguments as in struct attribute_spec.handler. */ diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index e9f47519df2..5a6ef464779 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -3784,6 +3784,22 @@ function. Examples of such functions are @code{setjmp} and @code{vfork}. The @code{longjmp}-like counterpart of such function, if any, might need to be marked with the @code{noreturn} attribute. +@item returns_zero_on_failure +@cindex @code{returns_zero_on_failure} function attribute +The @code{returns_zero_on_failure} attribute hints that the function +can succeed or fail, returning non-zero on success and zero on failure. +This is used by the @option{-fanalyzer} option to consider both outcomes +separately, which may improve how it explores error-handling paths, and +how such outcomes are labelled in diagnostics. It is also a hint +to the human reader of the source code. + +@item returns_zero_on_success +@cindex @code{returns_zero_on_success} function attribute +The @code{returns_zero_on_success} attribute is identical to the +@code{returns_zero_on_failure} attribute, apart from having the +opposite interpretation of the return value: zero on success, non-zero +on failure. + @item section ("@var{section-name}") @cindex @code{section} function attribute @cindex functions in arbitrary sections diff --git a/gcc/testsuite/c-c++-common/attr-returns-zero-on-1.c b/gcc/testsuite/c-c++-common/attr-returns-zero-on-1.c new file mode 100644 index 00000000000..5475dfe61db --- /dev/null +++ b/gcc/testsuite/c-c++-common/attr-returns-zero-on-1.c @@ -0,0 +1,68 @@ +/* Verify the parsing of the "returns_zero_on_{sucess|failure}" attributes. */ + +/* Correct usage. */ + +extern int test_int_return_s () + __attribute__((returns_zero_on_success)); +extern long test_long_return_f () + __attribute__((returns_zero_on_failure)); + +/* Should complain if not a function. */ + +extern int not_a_function_s + __attribute__((returns_zero_on_success)); /* { dg-warning "'returns_zero_on_success' attribute only applies to function types" } */ +extern int not_a_function_f + __attribute__((returns_zero_on_failure)); /* { dg-warning "'returns_zero_on_failure' attribute only applies to function types" } */ + +/* Should complain if return type is not integral. */ + +extern void test_void_return_s () + __attribute__((returns_zero_on_success)); /* { dg-error "'returns_zero_on_success' attribute on a function not returning an integral type" } */ +extern void test_void_return_f () + __attribute__((returns_zero_on_failure)); /* { dg-error "'returns_zero_on_failure' attribute on a function not returning an integral type" } */ + +extern void *test_void_star_return_s () + __attribute__((returns_zero_on_success)); /* { dg-error "'returns_zero_on_success' attribute on a function not returning an integral type" } */ +extern void *test_void_star_return_f () + __attribute__((returns_zero_on_failure)); /* { dg-error "'returns_zero_on_failure' attribute on a function not returning an integral type" } */ + +/* (and this prevents mixing with returns_non_null, which requires a pointer). */ + +/* Should complain if more than one returns_* attribute. */ + +extern int test_void_returns_s_f () + __attribute__((returns_zero_on_success)) + __attribute__((returns_zero_on_failure)); /* { dg-warning "ignoring attribute 'returns_zero_on_failure' because it conflicts with attribute 'returns_zero_on_success'" } */ +extern int test_void_returns_f_s () + __attribute__((returns_zero_on_failure)) + __attribute__((returns_zero_on_success)); /* { dg-warning "ignoring attribute 'returns_zero_on_success' because it conflicts with attribute 'returns_zero_on_failure'" } */ + +/* Should complain if mixed with "noreturn". */ + +extern int test_noreturn_returns_s () + __attribute__((noreturn)) + __attribute__((returns_zero_on_success)); /* { dg-warning "ignoring attribute 'returns_zero_on_success' because it conflicts with attribute 'noreturn'" } */ +extern int test_returns_s_noreturn () + __attribute__((returns_zero_on_success)) + __attribute__((noreturn)); /* { dg-warning "ignoring attribute 'noreturn' because it conflicts with attribute 'returns_zero_on_success'" } */ +extern int test_noreturn_returns_f () + __attribute__((noreturn)) + __attribute__((returns_zero_on_failure)); /* { dg-warning "ignoring attribute 'returns_zero_on_failure' because it conflicts with attribute 'noreturn'" } */ +extern int test_returns_f_noreturn () + __attribute__((returns_zero_on_failure)) + __attribute__((noreturn)); /* { dg-warning "ignoring attribute 'noreturn' because it conflicts with attribute 'returns_zero_on_failure'" } */ + +/* Should complain if mixed with "returns_twice". */ + +extern int test_returns_twice_returns_s () + __attribute__((returns_twice)) + __attribute__((returns_zero_on_success)); /* { dg-warning "ignoring attribute 'returns_zero_on_success' because it conflicts with attribute 'returns_twice'" } */ +extern int test_returns_s_returns_twice () + __attribute__((returns_zero_on_success)) + __attribute__((returns_twice)); /* { dg-warning "ignoring attribute 'returns_twice' because it conflicts with attribute 'returns_zero_on_success'" } */ +extern int test_returns_twice_returns_f () + __attribute__((returns_twice)) + __attribute__((returns_zero_on_failure)); /* { dg-warning "ignoring attribute 'returns_zero_on_failure' because it conflicts with attribute 'returns_twice'" } */ +extern int test_returns_f_returns_twice () + __attribute__((returns_zero_on_failure)) + __attribute__((returns_twice)); /* { dg-warning "ignoring attribute 'returns_twice' because it conflicts with attribute 'returns_zero_on_failure'" } */