From patchwork Wed Nov 4 06:50:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 1393671 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CQy3W0cMCz9sT6 for ; Wed, 4 Nov 2020 17:51:00 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CB2403850408; Wed, 4 Nov 2020 06:50:57 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa4.mentor.iphmx.com (esa4.mentor.iphmx.com [68.232.137.252]) by sourceware.org (Postfix) with ESMTPS id D213E3857C56 for ; Wed, 4 Nov 2020 06:50:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D213E3857C56 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=joseph_myers@mentor.com IronPort-SDR: Q+rzj/OzKf2+B1AV0IkDl2QmRMKDnp/axN/HO2RoP8h60KhS9DPxvirfQbEjY1UGQkdXFw3hD4 Xj7Objx3zm9kBM74ECZkzh0bm6kiqYSOLQGJlyWq5KXrpGORIRALpqEWwL3+v0YurWsWPpdTJ6 LAWhmlUaMtNz8vtZGYFB5lNbWva5UmB4RnzJ5hBK6p8ICnEwiw0qu9iTuOqyrsXqnn5x+13pWT zsW5Zm0cLZKo1zHBYRURSku4KTEP77iNBfW2s0uu74pQEhkWX48ZI1XpYbErdFVnEVY+k/Xcp+ vVk= X-IronPort-AV: E=Sophos;i="5.77,450,1596528000"; d="scan'208";a="54784858" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa4.mentor.iphmx.com with ESMTP; 03 Nov 2020 22:50:53 -0800 IronPort-SDR: WsOA8uJw6f43uNq5I6G7LenxuC6thoR2MHCsoK3joaM+nPO0YekXR24YBK661sj9BCu1Mt59Mj 6SPtBdZ90pTj/RAKp3TWgqkx3Ix/h3st+EmDdgO2OwfLqUnlg4296FyZdOY6ulSxOQF5RD1SMa LqVUHWRvWGzSOkRlp6efSgpB9fjiSMPW+uYxms/9VMll9zFO760KIKt5RIFjHIirGt0bcZu24M D8l9IZt8sGq3V2vixEz1NBNsHvZhG6xsULDdHiMduvfDEH0BHzUb9rqVhNqd8JL1QiQODkURLC 9ig= Date: Wed, 4 Nov 2020 06:50:47 +0000 From: Joseph Myers X-X-Sender: jsm28@digraph.polyomino.org.uk To: Subject: c: Implement C2x nodiscard attribute Message-ID: User-Agent: Alpine 2.22 (DEB 394 2020-01-19) MIME-Version: 1.0 X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: SVR-IES-MBX-03.mgc.mentorg.com (139.181.222.3) To svr-ies-mbx-01.mgc.mentorg.com (139.181.222.1) X-Spam-Status: No, score=-3131.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: , Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" C2x adds the nodiscard standard attribute, with an optional string argument, as in C++; implement it for C. Bootstrapped with no regressions for x86_64-pc-linux-gnu. Applied to mainline. gcc/c/ 2020-11-04 Joseph Myers * c-decl.c (handle_nodiscard_attribute): New. (std_attribute_table): Add nodiscard. * c-parser.c (c_parser_std_attribute): Expect argument to nodiscard attribute to be a string. Do not special-case ignoring nodiscard. * c-typeck.c (maybe_warn_nodiscard): New. (build_compound_expr, emit_side_effect_warnings): Call maybe_warn_nodiscard. (c_process_expr_stmt, c_finish_stmt_expr): Also call emit_side_effect_warnings if warn_unused_result. gcc/testsuite/ 2020-11-04 Joseph Myers * gcc.dg/c2x-attr-nodiscard-1.c, gcc.dg/c2x-attr-nodiscard-2.c, gcc.dg/c2x-attr-nodiscard-3.c, gcc.dg/c2x-attr-nodiscard-4.c: New tests. * gcc.dg/c2x-attr-syntax-5.c: Remove nodiscard test. diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index a5d0b158a26..d4179aad189 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -4400,6 +4400,31 @@ lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc) } +/* Handle the standard [[nodiscard]] attribute. */ + +static tree +handle_nodiscard_attribute (tree *node, tree name, tree /*args*/, + int /*flags*/, bool *no_add_attrs) +{ + if (TREE_CODE (*node) == FUNCTION_DECL) + { + if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))) + warning_at (DECL_SOURCE_LOCATION (*node), + OPT_Wattributes, "%qE attribute applied to %qD with void " + "return type", name, *node); + } + else if (RECORD_OR_UNION_TYPE_P (*node) + || TREE_CODE (*node) == ENUMERAL_TYPE) + /* OK */; + else + { + pedwarn (input_location, + OPT_Wattributes, "%qE attribute can only be applied to " + "functions or to structure, union or enumeration types", name); + *no_add_attrs = true; + } + return NULL_TREE; +} /* Table of supported standard (C2x) attributes. */ const struct attribute_spec std_attribute_table[] = { @@ -4411,6 +4436,8 @@ const struct attribute_spec std_attribute_table[] = handle_fallthrough_attribute, NULL }, { "maybe_unused", 0, 0, false, false, false, false, handle_unused_attribute, NULL }, + { "nodiscard", 0, 1, false, false, false, false, + handle_nodiscard_attribute, NULL }, { NULL, 0, 0, false, false, false, false, NULL, NULL } }; diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c index b921c4e3852..fc97aa3f95f 100644 --- a/gcc/c/c-parser.c +++ b/gcc/c/c-parser.c @@ -4950,7 +4950,8 @@ c_parser_std_attribute (c_parser *parser, bool for_tm) && attribute_takes_identifier_p (name)); bool require_string = (ns == NULL_TREE - && strcmp (IDENTIFIER_POINTER (name), "deprecated") == 0); + && (strcmp (IDENTIFIER_POINTER (name), "deprecated") == 0 + || strcmp (IDENTIFIER_POINTER (name), "nodiscard") == 0)); TREE_VALUE (attribute) = c_parser_attribute_arguments (parser, takes_identifier, require_string, false); @@ -4960,13 +4961,12 @@ c_parser_std_attribute (c_parser *parser, bool for_tm) parens.require_close (parser); } out: - if (ns == NULL_TREE && !for_tm && !as && !is_attribute_p ("nodiscard", name)) + if (ns == NULL_TREE && !for_tm && !as) { /* An attribute with standard syntax and no namespace specified is a constraint violation if it is not one of the known - standard attributes (of which nodiscard is the only one - without a handler in GCC). Diagnose it here with a pedwarn - and then discard it to prevent a duplicate warning later. */ + standard attributes. Diagnose it here with a pedwarn and + then discard it to prevent a duplicate warning later. */ pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored", name); return error_mark_node; diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 981cbe891a7..0d75ed4f8b1 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -5490,6 +5490,82 @@ build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp, return ret; } +/* EXPR is an expression, location LOC, whose result is discarded. + Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR + whose right-hand operand is such a call, possibly recursively). */ + +static void +maybe_warn_nodiscard (location_t loc, tree expr) +{ + if (VOID_TYPE_P (TREE_TYPE (expr))) + return; + while (TREE_CODE (expr) == COMPOUND_EXPR) + { + expr = TREE_OPERAND (expr, 1); + if (EXPR_HAS_LOCATION (expr)) + loc = EXPR_LOCATION (expr); + } + if (TREE_CODE (expr) != CALL_EXPR) + return; + tree fn = CALL_EXPR_FN (expr); + if (!fn) + return; + tree attr; + if (TREE_CODE (fn) == ADDR_EXPR + && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL + && (attr = lookup_attribute ("nodiscard", + DECL_ATTRIBUTES (TREE_OPERAND (fn, 0))))) + { + fn = TREE_OPERAND (fn, 0); + tree args = TREE_VALUE (attr); + if (args) + args = TREE_VALUE (args); + auto_diagnostic_group d; + int warned; + if (args) + warned = warning_at (loc, OPT_Wunused_result, + "ignoring return value of %qD, declared with " + "attribute %: %E", fn, args); + else + warned = warning_at (loc, OPT_Wunused_result, + "ignoring return value of %qD, declared with " + "attribute %", fn); + if (warned) + inform (DECL_SOURCE_LOCATION (fn), "declared here"); + } + else + { + tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))); + attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype)); + if (!attr) + return; + tree args = TREE_VALUE (attr); + if (args) + args = TREE_VALUE (args); + auto_diagnostic_group d; + int warned; + if (args) + warned = warning_at (loc, OPT_Wunused_result, + "ignoring return value of type %qT, declared " + "with attribute %: %E", + rettype, args); + else + warned = warning_at (loc, OPT_Wunused_result, + "ignoring return value of type %qT, declared " + "with attribute %", rettype); + if (warned) + { + if (TREE_CODE (fn) == ADDR_EXPR) + { + fn = TREE_OPERAND (fn, 0); + if (TREE_CODE (fn) == FUNCTION_DECL) + inform (DECL_SOURCE_LOCATION (fn), + "in call to %qD, declared here", fn); + } + } + } +} + /* Return a compound expression that performs two expressions and returns the value of the second of them. @@ -5561,6 +5637,8 @@ build_compound_expr (location_t loc, tree expr1, tree expr2) else if (warn_unused_value) warn_if_unused_value (expr1, loc); + maybe_warn_nodiscard (loc, expr1); + if (expr2 == error_mark_node) return error_mark_node; @@ -11072,6 +11150,9 @@ c_finish_bc_stmt (location_t loc, tree label, bool is_break) static void emit_side_effect_warnings (location_t loc, tree expr) { + maybe_warn_nodiscard (loc, expr); + if (!warn_unused_value) + return; if (expr == error_mark_node) ; else if (!TREE_SIDE_EFFECTS (expr)) @@ -11127,7 +11208,7 @@ c_process_expr_stmt (location_t loc, tree expr) Warnings for statement expressions will be emitted later, once we figure out which is the result. */ if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list) - && warn_unused_value) + && (warn_unused_value || warn_unused_result)) emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr); exprv = expr; @@ -11221,7 +11302,7 @@ c_finish_stmt_expr (location_t loc, tree body) /* If we're supposed to generate side effects warnings, process all of the statements except the last. */ - if (warn_unused_value) + if (warn_unused_value || warn_unused_result) { for (tree_stmt_iterator i = tsi_start (last); tsi_stmt (i) != tsi_stmt (l); tsi_next (&i)) diff --git a/gcc/testsuite/gcc.dg/c2x-attr-nodiscard-1.c b/gcc/testsuite/gcc.dg/c2x-attr-nodiscard-1.c new file mode 100644 index 00000000000..f4893bd123d --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-attr-nodiscard-1.c @@ -0,0 +1,62 @@ +/* Test C2x deprecated attribute: valid uses. */ +/* { dg-do compile } */ +/* { dg-options "-std=c2x -pedantic-errors" } */ + +[[nodiscard]] int c1 (void); /* { dg-message "declared here" } */ +[[__nodiscard__ ("some reason")]] int c2 (void); /* { dg-message "declared here" } */ + +struct [[nodiscard ("struct reason")]] s1 { int a; }; +struct [[__nodiscard__]] s2 { long b; }; +struct s1 cs1 (void); /* { dg-message "declared here" } */ +struct s2 cs2 (void); /* { dg-message "declared here" } */ +typedef struct s2 s2t; +s2t cs3 (void); /* { dg-message "declared here" } */ + +union [[nodiscard]] u1 { int a; long b; }; +union [[nodiscard ("union reason")]] u2 { short c; float d; }; +union u1 cu1 (void); /* { dg-message "declared here" } */ +union u2 cu2 (void); /* { dg-message "declared here" } */ + +enum [[nodiscard]] e1 { E1 }; +enum [[nodiscard ("enum reason")]] e2 { E2 }; +enum e1 ce1 (void); /* { dg-message "declared here" } */ +enum e2 ce2 (void); /* { dg-message "declared here" } */ +enum e1 ce1a (void); +int i; + +[[nodiscard]] void v (void); /* { dg-warning "void return type" } */ + +int ok (void); + +void +f (void) +{ + c1 (); /* { dg-warning "ignoring return value" } */ + c2 (); /* { dg-warning "some reason" } */ + cs1 (); /* { dg-warning "struct reason" } */ + cs2 (); /* { dg-warning "ignoring return value of type" } */ + cs3 (); /* { dg-warning "ignoring return value of type" } */ + cu1 (); /* { dg-warning "ignoring return value of type" } */ + cu2 (); /* { dg-warning "union reason" } */ + ce1 (); /* { dg-warning "ignoring return value of type" } */ + ce2 (); /* { dg-warning "enum reason" } */ + ok (); + c1 (), ok (); /* { dg-warning "ignoring return value" } */ + cs1 (), ok (); /* { dg-warning "struct reason" } */ + ok (), cu1 (); /* { dg-warning "ignoring return value" } */ + ok (), (ok (), (ok (), ce2 ())); /* { dg-warning "enum reason" } */ + (ok (), cu1 ()), ok (); /* { dg-warning "ignoring return value" } */ + v (); + (i ? ce1 : ce1a) (); /* { dg-warning "ignoring return value of type" } */ + (void) c1 (); + (void) c2 (); + (void) cs1 (); + (void) cs2 (); + (void) cs3 (); + (void) cu1 (); + (void) cu2 (); + (void) ce1 (); + (void) ce2 (); + (void) (ok (), cu1 ()); + (void) (i ? ce1 : ce1a) (); +} diff --git a/gcc/testsuite/gcc.dg/c2x-attr-nodiscard-2.c b/gcc/testsuite/gcc.dg/c2x-attr-nodiscard-2.c new file mode 100644 index 00000000000..45c4d50dee0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-attr-nodiscard-2.c @@ -0,0 +1,42 @@ +/* Test C2x nodiscard attribute: invalid contexts. */ +/* { dg-do compile } */ +/* { dg-options "-std=c2x -pedantic-errors" } */ + +/* This attribute is not valid on types other than their definitions, + or on declarations other than function declarations, or on + statements, or as an attribute-declaration. */ + +[[nodiscard]]; /* { dg-error "ignored" } */ + +int [[nodiscard]] var; /* { dg-error "ignored" } */ + +int [[nodiscard ("reason")]] var2; /* { dg-error "ignored" } */ + +int array_with_nod_type[2] [[nodiscard]]; /* { dg-error "ignored" } */ + +void fn_with_nod_type () [[nodiscard]]; /* { dg-error "ignored" } */ + +int z = sizeof (int [[__nodiscard__]]); /* { dg-error "ignored" } */ + +[[nodiscard]] typedef int nod_int; /* { dg-error "can only be applied" } */ + +[[nodiscard]] int nvar; /* { dg-error "can only be applied" } */ + +struct s { int a; }; + +[[nodiscard]] typedef struct s nod_s; /* { dg-error "can only be applied" } */ + +struct t { [[nodiscard]] int b; }; /* { dg-error "can only be applied" } */ + +enum e { E [[nodiscard]] }; /* { dg-error "can only be applied" } */ + +void fx ([[nodiscard]] int p); /* { dg-error "can only be applied" } */ + +void +f (void) +{ + int a; + [[nodiscard ("reason")]] int b = 1; /* { dg-error "can only be applied" } */ + [[nodiscard]]; /* { dg-error "ignored" } */ + [[nodiscard]] a = 1; /* { dg-error "ignored" } */ +} diff --git a/gcc/testsuite/gcc.dg/c2x-attr-nodiscard-3.c b/gcc/testsuite/gcc.dg/c2x-attr-nodiscard-3.c new file mode 100644 index 00000000000..2e70d12bff3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-attr-nodiscard-3.c @@ -0,0 +1,11 @@ +/* Test C2x nodiscard attribute: invalid syntax. */ +/* { dg-do compile } */ +/* { dg-options "-std=c2x -pedantic-errors" } */ + +[[nodiscard()]] int a (void); /* { dg-error "parentheses must be omitted if attribute argument list is empty" } */ + +[[nodiscard(0)]] int b (void); /* { dg-error "expected" } */ + +[[nodiscard("", 123)]] int c (void); /* { dg-error "expected" } */ + +[[nodiscard((""))]] int d (void); /* { dg-error "expected" } */ diff --git a/gcc/testsuite/gcc.dg/c2x-attr-nodiscard-4.c b/gcc/testsuite/gcc.dg/c2x-attr-nodiscard-4.c new file mode 100644 index 00000000000..278f55d1e5d --- /dev/null +++ b/gcc/testsuite/gcc.dg/c2x-attr-nodiscard-4.c @@ -0,0 +1,6 @@ +/* Test C2x nodiscard attribute: duplicates (allowed after N2557). */ +/* { dg-do compile } */ +/* { dg-options "-std=c2x -pedantic-errors" } */ + +[[nodiscard, __nodiscard__]] int f (void); +[[__nodiscard__, nodiscard("message")]] int g (void); diff --git a/gcc/testsuite/gcc.dg/c2x-attr-syntax-5.c b/gcc/testsuite/gcc.dg/c2x-attr-syntax-5.c index 37a24112f63..b261be067ce 100644 --- a/gcc/testsuite/gcc.dg/c2x-attr-syntax-5.c +++ b/gcc/testsuite/gcc.dg/c2x-attr-syntax-5.c @@ -49,8 +49,3 @@ func (void) [[unknown_attribute]] { /* { dg-error "attribute ignored" } */ [[unknown_attribute]] x: var = 2; /* { dg-error "attribute ignored" } */ for ([[unknown_attribute]] int zz = 1; zz < 10; zz++) ; /* { dg-error "attribute ignored" } */ } - -/* nodiscard is not yet implemented, but is a standard attribute, so - its use is not a constraint violation and should only receive a - warning. */ -[[nodiscard]] int ndfunc (void); /* { dg-warning "attribute directive ignored" } */