From patchwork Thu Sep 5 12:45:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 97168 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 6A43E384F4B9 for ; Thu, 5 Sep 2024 12:46:24 +0000 (GMT) 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 [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id C6D33385843B for ; Thu, 5 Sep 2024 12:45:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C6D33385843B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C6D33385843B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725540355; cv=none; b=eqTubYqN/VFsICMDRJnUVR/lE/KngmCAYIeXZyeey9UTQjMKdbHVqEPvhszGmqdtYmrUPm+gd29065EUoix1veTVuKCUSPPAKGDITIQvKNvD5U2dGPoLUiHZgJ6Ki8K+LMi2oyVk3JzAyMON92qy8dhxJq0EYsyGxeeOUDW6EbY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725540355; c=relaxed/simple; bh=B+ellwoj4rF6o3Ju4rqtvaQJtrs9xn6FRiffXxpeLLo=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=RtYPriDmDQM1rxCmCl0Yli19FbDCzh18biv08pQYxHNpKlmUVuYCRwNkS1LXG1hTXiFiHY6du9PG+tk5sQRwyRQMzOoKrno+7dBc7Wdp3l6qfL5oo2gjymdhhkPkMo5WjVrV6m6ecCO+1Bm//mhAMhj3OH1mEpn/9+ocuSmVvI4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725540352; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=FMgSyvfOBerc35pp+MkTH7lfeymcm3vku9V3xr/rRfQ=; b=BAMilFNTM7Py7d0SKAJO5/ppoJAe2VpMiLxd8MmRkyzF7LIXxNMcbHe2y0//tYlG2foA5e hcoY6Q1Nd0oAF9ogkbnXJY4JVaBNWvJbPKr3xEmRDDijyIdR9ryC44KIy+dljAFmMw6IBa 3nxn2lIY+o14O5TXIdaAY8qD7hJzrcs= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-12-AGPH-yUMMFSecW2eoOWRMA-1; Thu, 05 Sep 2024 08:45:51 -0400 X-MC-Unique: AGPH-yUMMFSecW2eoOWRMA-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3973F1936B88 for ; Thu, 5 Sep 2024 12:45:48 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.29]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CA59319772C4; Thu, 5 Sep 2024 12:45:41 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 485CjcmI1865053 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 5 Sep 2024 14:45:39 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 485CjcKG1865052; Thu, 5 Sep 2024 14:45:38 +0200 Date: Thu, 5 Sep 2024 14:45:38 +0200 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: c++: Add nodiscard attribute further test coverage [PR110345] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Hi! Fairly non-problematic attribute, again on top of the whole series. Tested on x86_64-linux, ok for trunk? 2024-09-05 Jakub Jelinek PR c++/110345 * g++.dg/cpp0x/attr-nodiscard1.C: New test. Jakub --- gcc/testsuite/g++.dg/cpp0x/attr-nodiscard1.C.jj 2024-09-05 13:11:26.914049570 +0200 +++ gcc/testsuite/g++.dg/cpp0x/attr-nodiscard1.C 2024-09-05 13:38:05.456626161 +0200 @@ -0,0 +1,155 @@ +// C++ 26 P2552R3 - On the ignorability of standard attributes +// { dg-do compile { target c++11 } } + +int arr[2]; +struct S { int a, b; }; +S arr2[2]; + +void +foo (int n) +{ + struct [[nodiscard]] S1 {}; + struct [[nodiscard ("foobar")]] S2 {}; + struct [[nodiscard (0)]] S3 {}; // { dg-error "'nodiscard' attribute argument must be a string constant" } + struct [[nodiscard ("foo", "bar", "baz")]] S4 {}; // { dg-error "wrong number of arguments specified for 'nodiscard' attribute" } + struct [[nodiscard (0, 1, 2)]] S5 {}; // { dg-error "wrong number of arguments specified for 'nodiscard' attribute" } + + auto a = [] [[nodiscard]] () {}; + auto b = [] constexpr [[nodiscard]] {}; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + // { dg-error "parameter declaration before lambda declaration specifiers only optional with" "" { target c++20_down } .-1 } + // { dg-error "'constexpr' lambda only available with" "" { target c++14_down } .-2 } + auto c = [] noexcept [[nodiscard]] {}; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + // { dg-error "parameter declaration before lambda exception specification only optional with" "" { target c++20_down } .-1 } + auto d = [] () [[nodiscard]] {}; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + auto e = new int [n] [[nodiscard]]; // { dg-warning "attributes ignored on outermost array type in new expression" } + auto e2 = new int [n] [[nodiscard]] [42]; // { dg-warning "attributes ignored on outermost array type in new expression" } + auto f = new int [n][42] [[nodiscard]]; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + [[nodiscard]]; // { dg-warning "attributes at the beginning of statement are ignored" } + [[nodiscard]] {} // { dg-warning "attributes at the beginning of statement are ignored" } + [[nodiscard]] if (true) {} // { dg-warning "attributes at the beginning of statement are ignored" } + [[nodiscard]] while (false) {} // { dg-warning "attributes at the beginning of statement are ignored" } + [[nodiscard]] goto lab; // { dg-warning "attributes at the beginning of statement are ignored" } + [[nodiscard]] lab:; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + [[nodiscard]] try {} catch (int) {} // { dg-warning "attributes at the beginning of statement are ignored" } + if ([[nodiscard]] int x = 0) {} // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + switch (n) + { + [[nodiscard]] case 1: // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + [[nodiscard]] break; // { dg-warning "attributes at the beginning of statement are ignored" } + [[nodiscard]] default: // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + break; + } + for ([[nodiscard]] auto a : arr) {} // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + for ([[nodiscard]] auto [a, b] : arr2) {} // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + // { dg-error "structured bindings only available with" "" { target c++14_down } .-1 } + [[nodiscard]] asm (""); // { dg-warning "attributes ignored on 'asm' declaration" } + try {} catch ([[nodiscard]] int x) {} // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + try {} catch ([[nodiscard]] int) {} // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + try {} catch (int [[nodiscard]] x) {} // { dg-warning "attribute ignored" } + try {} catch (int [[nodiscard]]) {} // { dg-warning "attribute ignored" } + try {} catch (int x [[nodiscard]]) {} // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +} + +[[nodiscard]] int bar (); +using foobar [[nodiscard]] = int; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +[[nodiscard]] int a; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +[[nodiscard]] auto [b, c] = arr; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + // { dg-error "structured bindings only available with" "" { target c++14_down } .-1 } +[[nodiscard]]; // { dg-warning "attribute ignored" } +inline [[nodiscard]] void baz () {} // { dg-warning "attribute ignored" } + // { dg-error "standard attributes in middle of decl-specifiers" "" { target *-*-* } .-1 } +constexpr [[nodiscard]] int qux () { return 0; } // { dg-warning "attribute ignored" } + // { dg-error "standard attributes in middle of decl-specifiers" "" { target *-*-* } .-1 } +int [[nodiscard]] d; // { dg-warning "attribute ignored" } +int const [[nodiscard]] e = 1; // { dg-warning "attribute ignored" } +struct A {} [[nodiscard]]; // { dg-warning "attribute ignored in declaration of 'struct A'" } +struct A [[nodiscard]]; // { dg-warning "attribute ignored" } +struct A [[nodiscard]] a1; // { dg-warning "attribute ignored" } +A [[nodiscard]] a2; // { dg-warning "attribute ignored" } +enum B { B0 } [[nodiscard]]; // { dg-warning "attribute ignored in declaration of 'enum B'" } +enum B [[nodiscard]]; // { dg-warning "attribute ignored" } +enum B [[nodiscard]] b1; // { dg-warning "attribute ignored" } +B [[nodiscard]] b2; // { dg-warning "attribute ignored" } +struct [[nodiscard]] C {}; +int f [[nodiscard]]; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +int g[2] [[nodiscard]]; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +int g2 [[nodiscard]] [2]; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +int corge () [[nodiscard]]; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +int *[[nodiscard]] h; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +int & [[nodiscard]] i = f; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +int && [[nodiscard]] j = 0; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +int S::* [[nodiscard]] k; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +auto l = sizeof (int [2] [[nodiscard]]); // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +int freddy ([[nodiscard]] int a, // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + [[nodiscard]] int, // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + [[nodiscard]] int c = 0, // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + [[nodiscard]] int = 0); // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +void +corge ([[nodiscard]] int a, // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + [[nodiscard]] int, // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + [[nodiscard]] int c = 0, // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + [[nodiscard]] int = 0) // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +{ +} +[[nodiscard]] void +garply () // { dg-warning "'nodiscard' attribute applied to 'void garply\\\(\\\)' with void return type" } +{ +} +[[nodiscard]] int +xyzzyy () +{ + return 0; +} +int grault (int [[nodiscard]] a, // { dg-warning "attribute ignored" } + int [[nodiscard]], // { dg-warning "attribute ignored" } + int [[nodiscard]] c = 0, // { dg-warning "attribute ignored" } + int [[nodiscard]] = 0); // { dg-warning "attribute ignored" } +void +waldo (int [[nodiscard]] a, // { dg-warning "attribute ignored" } + int [[nodiscard]], // { dg-warning "attribute ignored" } + int [[nodiscard]] c = 0, // { dg-warning "attribute ignored" } + int [[nodiscard]] = 0) // { dg-warning "attribute ignored" } +{ +} +int plugh (int a [[nodiscard]], // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + int b [[nodiscard]] = 0); // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +void +thud (int a [[nodiscard]], // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + int b [[nodiscard]] = 0) // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +{ +} +enum [[nodiscard]] D { D0 }; +enum class [[nodiscard]] E { E0 }; +enum F {}; +enum [[nodiscard]] F; // { dg-warning "type attributes ignored after type is already defined" } +enum G { + G0 [[nodiscard]], // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + G1 [[nodiscard]] = 2 // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +}; +namespace [[nodiscard]] H { using H0 = int; } // { dg-warning "'nodiscard' attribute directive ignored" } +namespace [[nodiscard]] {} // { dg-warning "'nodiscard' attribute directive ignored" } +[[nodiscard]] using namespace H; // { dg-warning "'nodiscard' attribute directive ignored" } +struct [[nodiscard]] I +{ + [[nodiscard]]; // { dg-error "declaration does not declare anything" } + [[nodiscard]] int i; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + [[nodiscard]] int foo (); + [[nodiscard]] int bar () { return 1; } + [[nodiscard]] int : 0; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + [[nodiscard]] int i2 : 5; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + [[nodiscard]] static int i3; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } + static int i4; +}; +[[nodiscard]] int I::i4 = 0; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +struct J : [[nodiscard]] C {}; // { dg-warning "attributes on base specifiers are ignored" } +#if __cpp_concepts >= 201907L +template +concept K [[nodiscard]] = requires { true; }; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" "" { target c++20 } } +#endif +typedef int L [[nodiscard]]; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +template +struct M {}; +template <> +struct [[nodiscard]] M { int m; }; +typedef int N[2] [[nodiscard]]; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" } +typedef int O [[nodiscard]] [2]; // { dg-warning "'nodiscard' attribute can only be applied to functions or to class or enumeration types" }