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 E77EBC433F5 for ; Mon, 15 Nov 2021 07:04:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CAFF361AA7 for ; Mon, 15 Nov 2021 07:04:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230227AbhKOHG5 (ORCPT ); Mon, 15 Nov 2021 02:06:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230090AbhKOHGw (ORCPT ); Mon, 15 Nov 2021 02:06:52 -0500 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DEDEDC061746 for ; Sun, 14 Nov 2021 23:03:53 -0800 (PST) Received: by mail-ed1-x529.google.com with SMTP id y12so10821931eda.12 for ; Sun, 14 Nov 2021 23:03:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=D0zYE42iqAXWSVm904KUrHLOdqlQPh3EGC5qZ4YDE9c=; b=M+qFKpSkeFiegtFmS604js7XcuUBx1CjBurNBJ57rc4LwCb/sHHZW7uCILWJfFQjtv fvrsiWDOHXOiSFaEewc93hAXhD8ofd/DGk8XGcXhsak40E+byI6zvEHnuz+nSsyZxUGi PfftfINpcxQ4TttiGZUPDk7K9soGKzwqvBKnDxPzG1Z1u0Je3wnOf2wdCfUdrIFXzDds TtcnBiTqEIKv1hl8xBk9l8WXE2HZq6GlE2tKlDVHv4un0TlAtzjkc/vrxS1PoTCYGQVr 8JK+vXn98J4ytKUAzS8FgtTkPV9iK4pWZj1R6vgi8LjcsjZkXbVThDoSViMt0Z3wPYQv R9QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=D0zYE42iqAXWSVm904KUrHLOdqlQPh3EGC5qZ4YDE9c=; b=e1Q7OEdSyNwSU+57OMUJ336Iy3Y+eWBeXpXS62exLoLg9ckdl4zRNVNgs8vRPuxS2O c8vGD6cCG0QZOj/JHJ1qRzAyfDctwDVkRpcliAP+rJ7E2iJWbkg52DLrhf6RKTTtoR1T urIWQiTFgmLtfz42YTMCQ7Ph8aut2U6mL9mksQBiPCLEW2r8T4LMHu7lZcNKtxKx0Pca qF6q1nOrWl7wZVdJx+QlFPV6TIbTRcLBtZhPiLvQsXNNt+8T37k1yjCjumyNTnbbcj95 t3B6okHjzqZARLW8g7S3e5LDh50/Kw8nDiM2RurhYVHtrK1vNmPbuwa+R2FMWSV2tp1Z 45IA== X-Gm-Message-State: AOAM531pN1rkcTO/BTN/nKpm99HgvmZoblZz0FOMcHgNUipE/wNdmDUY GBDzD4oYYjjpawk8tGh5F0fGDcvnTOOcpTgzFlGbc3ped8c= X-Google-Smtp-Source: ABdhPJzgFf7A7zmzCqtjoY0/Tj7uKDH/1n1yvpGX9/8IeyPOZozJGWgqgFV1hCBgStupC84sI9DI8/fHR0BWHjONpfE= X-Received: by 2002:a05:6402:1e93:: with SMTP id f19mr3561634edf.138.1636959832391; Sun, 14 Nov 2021 23:03:52 -0800 (PST) MIME-Version: 1.0 References: <20211113203732.2098220-1-dmalcolm@redhat.com> <20211113203732.2098220-4-dmalcolm@redhat.com> In-Reply-To: <20211113203732.2098220-4-dmalcolm@redhat.com> From: Prathamesh Kulkarni Date: Mon, 15 Nov 2021 12:33:16 +0530 Message-ID: Subject: Re: [PATCH 2/6] Add returns_zero_on_success/failure attributes To: David Malcolm Cc: gcc-patches@gcc.gnu.org, linux-toolchains@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-toolchains@vger.kernel.org On Sun, 14 Nov 2021 at 02:07, David Malcolm via Gcc-patches wrote: > > 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; Hi David, Just curious if a warning should be emitted if the function is marked with the attribute but it's return value isn't actually 0 ? There are other constants like -1 or 1 that are often used to indicate error, so maybe tweak the attribute to take the integer as an argument ? Sth like returns_int_on_success(cst) / returns_int_on_failure(cst) ? Also, would it make sense to extend it for pointers too for returning NULL on success / failure ? Thanks, Prathamesh > +} > + > /* 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 >