From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 76C34C4332F for ; Sat, 13 Nov 2021 20:37:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5BA0961026 for ; Sat, 13 Nov 2021 20:37:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232555AbhKMUkj (ORCPT ); Sat, 13 Nov 2021 15:40:39 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:57798 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233692AbhKMUki (ORCPT ); Sat, 13 Nov 2021 15:40:38 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1636835865; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=f4T7gGs0/F73buSzDODMeMiO1sj9UJsLvOu0IwdWUiQ=; b=dXhXzW3stHaoubiS6Etlc3Fqosedu/kC9l5i1Ud9wEC/56ocBP5e81cUfCcfoS4ioMWAhB T2aJ85bF987EvlddqjLwPyPo3URSmEOzNvPfxSq/OjGJenEs+NBIHkPl2OyTv6sieL/1pl qOXAxhOPwdn+/K7Nt4QcqF9zWVUK/EI= 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) From: David Malcolm To: gcc-patches@gcc.gnu.org, linux-toolchains@vger.kernel.org Cc: David Malcolm 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 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=dmalcolm@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" Precedence: bulk List-ID: X-Mailing-List: linux-toolchains@vger.kernel.org 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'" } */ -- 2.26.3