Skip to content

Commit 4dab7e2

Browse files
committed
cext: Add nodiscard.
1 parent bc939dd commit 4dab7e2

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/csnip/cext.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@
5252
# define csnip_cext_import
5353
#endif
5454

55+
/** More portable version of C23's [[nodiscard]].
56+
*
57+
* For the common pre-C23 compilers, it uses a compiler-specific
58+
* alternative.
59+
*/
60+
#if __STDC_VERSION >= 202311L
61+
# define csnip_cext_nodiscard [[nodiscard]]
62+
#elif defined(__GNUC__) || defined(__clang__)
63+
# define csnip_cext_nodiscard __attribute__((warn_unused_result))
64+
#elif defined(_MSC_VER)
65+
# define csnip_cext_nodiscard _Check_return_
66+
#else
67+
# define csnip_cext_nodiscard /* nothing */
68+
#endif
69+
5570
/**@}*/
5671

5772
#endif /* CSNIP_CEXT_H */
@@ -60,5 +75,6 @@
6075
#define cext_unused csnip_cext_unused
6176
#define cext_export csnip_cext_export
6277
#define cext_import csnip_cext_import
78+
#define cext_nodiscard csnip_cext_nodiscard
6379
#define CSNIP_CEXT_HAVE_SHORT_NAMES
6480
#endif /* CSNIP_SHORT_NAMES && !CSNIP_CEXT_HAVE_SHORT_NAMES */

0 commit comments

Comments
 (0)