From nobody Mon May 19 04:30:23 2025 X-Original-To: dev-commits-src-all@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 4b14VR5dhdz5wRtF; Mon, 19 May 2025 04:30:23 +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 4b14VR3NWnz3v2Q; Mon, 19 May 2025 04:30:23 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1747629023; 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=DYO50RzQwrXGaBBDZXRGd3GxftNlL9Mio71SXboFGdg=; b=vx5zk5FYSATV1Q/Kz0kAWuw+cam1CeoyVmTDLg/p/LFyWSYJ/OAtqOZjwxvqU8cpHv3upf IOOumVnU9YFbfuXfWAi2QKXfhg1bRUdMpuvlXguivBexKbj1BDfhPZJZ99ocGRUkdJ4HD9 FeydJIFI3Rbt5kjMq4FcIw0+GeMT1KC0EAplRaANFYc1sBoIyGHJROzpymOszxady07DsR ycdQtRqq+m9D4QIeFxjgfYqnEb7PsI0CFtkJ+1SXYq9+aqSx3+UT64RJaobEw/1wN5943l yyHtkIb69fwJ+bj7YWqHQhQ9wP3a8PkXEZbOs3kTJ9UDRZoBe4Xe8EBgIw+FkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1747629023; 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=DYO50RzQwrXGaBBDZXRGd3GxftNlL9Mio71SXboFGdg=; b=T74otjhAY7IBy53y/6H7tF4pmSLRj8zQPGJ6BmhAVI+A9TdxVhlUwfVKdZCdcnFJZMxhDn EiQvNRNyQXkfKkT9bZGwuU+e7KDR4cIOdsK/24nQnhkZoLPvDOUoEmejfj/7yFVkDUtKHO FsTiO9b7wpZvU+Ndn1j3n4U+akuKazDbNQ3Q7GsVoHlnxYBiRFUfQeJUjv0MbYh8ekie9R oUGdQv+6LczHhp9EXPGLz6XHx0QZFwrx068zBWJ1dwYJxtA/NGwqCjyBKM8GoeyGs3khPs wg3uKgDdmeSlYMomDKbDy2AT5jjr2RFH+qVwwW7Y9yA2ukXjW0CSelIXVPeAiA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1747629023; a=rsa-sha256; cv=none; b=u2uFQiJIQbQXOu3WWZzcin6w9MLMy9HzDUh+rxdOOfhVRww2DeL1Cf0TF38t0LeKVRfFGx ApMnHAp10VSnAciZIRJHN5f/tcMp9JyF3Ql+EfUgQjV+xKDKTMUcxmPPE4TAIObnyaZWC1 htGoag0V9fzqAE20vv0mRjIst14O4/WbkNXIryL4KR6uAAgAtCIPiW12lVj1ZkWeckQhKB RNc/xiu7GpYxIH8Qi/ExGdu0TDa0QFM1UheKXlgqqsyeLEaeYAko1Lzlq/myVL/P3U5O4T prXEr21ORE2AA4SRwjTcfjDAThl8L8vtw6NobuBdrhzkFZ8/eycP5usW5AJ18A== 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 4b14VR1dNJzptx; Mon, 19 May 2025 04:30:23 +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 54J4UN76059576; Mon, 19 May 2025 04:30:23 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 54J4UNKh059573; Mon, 19 May 2025 04:30:23 GMT (envelope-from git) Date: Mon, 19 May 2025 04:30:23 GMT Message-Id: <202505190430.54J4UNKh059573@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Lexi Winter Subject: git: cf47e6d4e14a - stable/14 - sys/cdefs.h: add __nodiscard annotation List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@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/stable/14 X-Git-Reftype: branch X-Git-Commit: cf47e6d4e14ab97d42317cb653926a0b6f9a21e9 Auto-Submitted: auto-generated The branch stable/14 has been updated by ivy: URL: https://cgit.FreeBSD.org/src/commit/?id=cf47e6d4e14ab97d42317cb653926a0b6f9a21e9 commit cf47e6d4e14ab97d42317cb653926a0b6f9a21e9 Author: Lexi Winter AuthorDate: 2025-05-09 21:28:14 +0000 Commit: Lexi Winter CommitDate: 2025-05-19 03:04:26 +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 (cherry picked from commit 7e0c5e0128c43bbae78190911aead8e1d9475ba5) --- sys/sys/cdefs.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/sys/sys/cdefs.h b/sys/sys/cdefs.h index dc5221343dc3..209acc842839 100644 --- a/sys/sys/cdefs.h +++ b/sys/sys/cdefs.h @@ -393,6 +393,44 @@ #endif #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.