From nobody Fri May 9 22:50:35 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4ZvPNW4cMKz5wFM2; Fri, 09 May 2025 22:50:35 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4ZvPNW3pxXz42fS; Fri, 09 May 2025 22:50:35 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1746831035; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=lKjxTRqcYK/kZ9vQb3AqQrjDfTfixiJZ3HTFojVrIyQ=; b=jyRYb6tU49OMDdofAZIElqhxA4s1YhmN24rmnuJmg7Pvn0IXlY7ygbrbaKAhxfOlmKITrz tN+D2IfliS4UI/pEiSlWkzZsf6PZce6WvnifERBQFXidtdxb2MYi7ymjvDUXPUTVOkk5d9 Qz4LXi8/umjliLkIYd6d0o8zeoZrV5Dzssw7lkn6R81Hb5yXXaFveLPEQSqmwXU5IL9+0d Jv7QCrRmkvuw2y3YPMmeH9ZUqzNIl7zXdG72kyu3Jer1iWkuSfo6FNye8t6tqbKQmqpcGZ kWY09hjhgVBzOlGbk6qQ5VLDDRiaob+FwKgbRExOAO+ypAmMfGhR6STHRNwhEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1746831035; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=lKjxTRqcYK/kZ9vQb3AqQrjDfTfixiJZ3HTFojVrIyQ=; b=QfGxuA6Kxpdnkc/Ymtb242VnAGE18jNZPs8G96beMrGmcM56z2T0ekfKO/58GnuQaC7YZZ BjEjvzYjRyvZmwI/42HEDYZAu31e1G3fCBX4RmzitUGa/nLekjKScruK6VgYXdK2W0HHRP 1VNbJTbxgXRq+wfdKRz6zH+vAOWZFRp3oifYibJLuiI9u/uel3lUTuqdHsL0y7daVVMVFl t/tmLvE+qoPK3yr2LU5W7W61RhKZ7vmNkKFEmAzMK31MJmsR0GvOTIEG8MREJLaaBl15ik W+CR0urOxX2JAZ8ulLfMtkYfDSJP3lbTNjcTysyfIBOY/tepCY4yFOT0x54G0g== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1746831035; a=rsa-sha256; cv=none; b=qF+F/lDm+TcMfNZBILGk/XNN966UcLCryymvFnk0gy6dRQGrMR4vK5Vc2tKcKMFN0Kh8KS EGDBZlfSqZlXU4MJK/jbBAw2r6X11u7Jdk/EVqDDirv+UUuiEUWumvYWRy7UtpuYnUku0r 9nCjCuMsn4+bMWzMPPox37Zv+9SuQSqgBf/tmuSGALiqku5ivbbuZAr8bk9c1Y1vpDPGys l57ftjcirUkW5aowm6gJLQ4krYEsoS7MjcJflDd/2R6jcFiHjz1o4IwhFVjFOi5SkNcizE bRD8lgygkdcEgzBCuFTyBuVnLa3ZU6YJhQwk9sYjCOsQ0grFpU7qm1h7KUFrtg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4ZvPNW37z9z4CJ; Fri, 09 May 2025 22:50:35 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 549MoZQe028154; Fri, 9 May 2025 22:50:35 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 549MoZsx028151; Fri, 9 May 2025 22:50:35 GMT (envelope-from git) Date: Fri, 9 May 2025 22:50:35 GMT Message-Id: <202505092250.549MoZsx028151@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Lexi Winter Subject: git: 7e0c5e0128c4 - main - sys/cdefs.h: add __nodiscard annotation List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: ivy X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 7e0c5e0128c43bbae78190911aead8e1d9475ba5 Auto-Submitted: auto-generated The branch main has been updated by ivy: URL: https://cgit.FreeBSD.org/src/commit/?id=7e0c5e0128c43bbae78190911aead8e1d9475ba5 commit 7e0c5e0128c43bbae78190911aead8e1d9475ba5 Author: Lexi Winter AuthorDate: 2025-05-09 21:28:14 +0000 Commit: Lexi Winter CommitDate: 2025-05-09 22:50:23 +0000 sys/cdefs.h: add __nodiscard annotation __nodiscard adds the [[nodiscard]] attribute to a function, type or constructor in C or C++, causing a value so marked to issue a compiler warning if it is discarded (i.e., not used or assigned) other than by casting it to void. this replaces the existing __result_use_or_ignore_check, which has a similar purpose but different semantics. since __nodiscard provides more functionality (at least in GCC) and __result_use_or_ignore_check only had a single user, remove __result_use_or_ignore_check. [[nodiscard]] has been supported in C++ since C++17, but only in C since C23; however, both LLVM and GCC implement it even in older language versions, so it should always be available with a relatively modern compiler. for Clang, [[nodiscard]] in C is only available since LLVM 17, but we can fall back to __attribute__((__warn_unused_result__)) which has the same semantics and provides support back to (at least) LLVM 11. GCC supports [[nodiscard]] in both C and C++ since at least GCC 11. for GCC, we can't provide a fallback as the semantics of its warn_unused_result are different, but since __result_use_or_ignore_check isn't defined for GCC anyway, we don't lose anything here. MFC after: 2 weeks Reviewed by: des, emaste Approved by: des (mentor) Differential Revision: https://reviews.freebsd.org/D50217 --- share/man/man9/cdefs.9 | 19 +++++++++++++++---- sys/sys/cdefs.h | 50 ++++++++++++++++++++++++++++++++++++++------------ sys/sys/systm.h | 14 +++++++------- 3 files changed, 60 insertions(+), 23 deletions(-) diff --git a/share/man/man9/cdefs.9 b/share/man/man9/cdefs.9 index 4efce132d393..397ddb0891bb 100644 --- a/share/man/man9/cdefs.9 +++ b/share/man/man9/cdefs.9 @@ -3,7 +3,7 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" -.Dd January 6, 2025 +.Dd May 9, 2025 .Dt CDEFS 9 .Os .Sh NAME @@ -118,9 +118,20 @@ family of functions. .It Sy __fastcall Ta Use the .Dq fastcall ABI to call and name mangle this function. -.It Sy __result_use_check Ta Warn if function caller does not use it's return value -.It Sy __result_use_or_ignore_check Ta Warn if function caller does not use it's return value. -Allows the value to be explicitly ignored with a (void) cast. +.It Sy __result_use_check Ta Warn if function caller does not use its return value +.It Sy __nodiscard Ta Equivalent to the standard +.Dq [[nodiscard]] +attribute. +If applied to a function, warn if function caller does not use its +return value. +The warning may be silenced using a cast to +.Vt void , +or in C++, using an assignment to +.Va std::ignore . +If applied to a struct, C++ class or enum, this applies to all functions +returning values of that type. +If applied to a C++ constructor, this applies to creating instances of +the class using that constructor. .It Sy __returns_twice Ta Returns multiple times, like .Xr fork 2 .It Sy __unreachable Ta This code is not reachable at runtime diff --git a/sys/sys/cdefs.h b/sys/sys/cdefs.h index c46be9f35842..e79cf2972f3f 100644 --- a/sys/sys/cdefs.h +++ b/sys/sys/cdefs.h @@ -253,18 +253,6 @@ #define __noinline __attribute__ ((__noinline__)) #define __fastcall __attribute__((__fastcall__)) #define __result_use_check __attribute__((__warn_unused_result__)) -#ifdef __clang__ -/* - * clang and gcc have different semantics for __warn_unused_result__: the latter - * does not permit the use of a void cast to suppress the warning. Use - * __result_use_or_ignore_check in places where a void cast is acceptable. - * This can be implemented by [[nodiscard]] from C23. - */ -#define __result_use_or_ignore_check __result_use_check -#else -#define __result_use_or_ignore_check -#endif /* !__clang__ */ - #define __returns_twice __attribute__((__returns_twice__)) #define __unreachable() __builtin_unreachable() @@ -295,6 +283,44 @@ #define __noexcept_if(__c) #endif +/* + * nodiscard attribute added in C++17 and C23, but supported by both LLVM and + * GCC in earlier language versions, so we use __has_c{,pp}_attribute to test + * for it. + * + * __nodiscard may be used on a function: + * __nodiscard int f(); + * + * or on a struct, union or enum: + * struct __nodiscard S{}; + * struct S f(); + * + * or in C++, on an object constructor. + */ + +#if defined(__cplusplus) && defined(__has_cpp_attribute) +#if __has_cpp_attribute(nodiscard) +#define __nodiscard [[nodiscard]] +#endif +#elif defined(__STDC_VERSION__) && defined(__has_c_attribute) +#if __has_c_attribute(__nodiscard__) +#define __nodiscard [[__nodiscard__]] +#endif +#endif + +#ifndef __nodiscard +/* + * LLVM 16 and earlier don't support [[nodiscard]] in C, but they do support + * __warn_unused_result__ with the same semantics, so fall back to that. + * We can't do this for GCC because the semantics are different. + */ +#ifdef __clang__ +#define __nodiscard __attribute__((__warn_unused_result__)) +#else +#define __nodiscard +#endif +#endif + /* * We use `__restrict' as a way to define the `restrict' type qualifier * without disturbing older software that is unaware of C99 keywords. diff --git a/sys/sys/systm.h b/sys/sys/systm.h index f542a9a86018..7cc02c77bea4 100644 --- a/sys/sys/systm.h +++ b/sys/sys/systm.h @@ -309,9 +309,9 @@ int __result_use_check copyin(const void * __restrict udaddr, void * _Nonnull __restrict kaddr, size_t len); int __result_use_check copyin_nofault(const void * __restrict udaddr, void * _Nonnull __restrict kaddr, size_t len); -int __result_use_or_ignore_check copyout(const void * _Nonnull __restrict kaddr, +__nodiscard int copyout(const void * _Nonnull __restrict kaddr, void * __restrict udaddr, size_t len); -int __result_use_or_ignore_check copyout_nofault( +__nodiscard int copyout_nofault( const void * _Nonnull __restrict kaddr, void * __restrict udaddr, size_t len); @@ -334,11 +334,11 @@ int64_t fuword64(volatile const void *base); int __result_use_check fueword(volatile const void *base, long *val); int __result_use_check fueword32(volatile const void *base, int32_t *val); int __result_use_check fueword64(volatile const void *base, int64_t *val); -int __result_use_or_ignore_check subyte(volatile void *base, int byte); -int __result_use_or_ignore_check suword(volatile void *base, long word); -int __result_use_or_ignore_check suword16(volatile void *base, int word); -int __result_use_or_ignore_check suword32(volatile void *base, int32_t word); -int __result_use_or_ignore_check suword64(volatile void *base, int64_t word); +__nodiscard int subyte(volatile void *base, int byte); +__nodiscard int suword(volatile void *base, long word); +__nodiscard int suword16(volatile void *base, int word); +__nodiscard int suword32(volatile void *base, int32_t word); +__nodiscard int suword64(volatile void *base, int64_t word); uint32_t casuword32(volatile uint32_t *base, uint32_t oldval, uint32_t newval); u_long casuword(volatile u_long *p, u_long oldval, u_long newval); int casueword32(volatile uint32_t *base, uint32_t oldval, uint32_t *oldvalp,