From a9a85fd1c13bcd258eb5c5f693fe57dfb975561d Mon Sep 17 00:00:00 2001 From: Sirac Ozmen Date: Thu, 5 Nov 2026 01:27:21 +0300 Subject: [PATCH] fix: make MUNI_NODISCARD the attribute under GCC 14's C23 too --- include/maul-unicode/base.h | 11 +++++++++-- test/test_base.c | 10 +++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/include/maul-unicode/base.h b/include/maul-unicode/base.h index 6ffeaf8..cdd880e 100644 --- a/include/maul-unicode/base.h +++ b/include/maul-unicode/base.h @@ -64,14 +64,21 @@ extern "C" // MUNI_NODISCARD marks a function whose result must be read: every // function that returns a status. The attribute is standard in C23 and // C++17 and left out for older dialects. MSVC keeps __cplusplus at -// 199711L unless /Zc:__cplusplus is given, so _MSVC_LANG is read too. +// 199711L unless /Zc:__cplusplus is given, so _MSVC_LANG is read too. In +// C, any dialect after C17 whose compiler has the attribute takes it: +// GCC 14 reports 202000L in its C23 mode. #if (defined(__cplusplus) && __cplusplus >= 201703L) || \ (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) #define MUNI_NODISCARD [[nodiscard]] -#elif !defined(__cplusplus) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L +#elif !defined(__cplusplus) && defined(__STDC_VERSION__) && __STDC_VERSION__ > 201710L && \ + defined(__has_c_attribute) +#if __has_c_attribute(nodiscard) #define MUNI_NODISCARD [[nodiscard]] #else #define MUNI_NODISCARD +#endif +#else +#define MUNI_NODISCARD #endif // The status a fallible function returns. Zero is success, positive diff --git a/test/test_base.c b/test/test_base.c index b33fa66..01c652a 100644 --- a/test/test_base.c +++ b/test/test_base.c @@ -1,7 +1,8 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2026 Sirac Ozmen // -// Versions and result names, and the layouts of the public types. +// Versions, result names, the result attribute, and the layouts of the +// public types. #include "test_harness.h" @@ -18,6 +19,13 @@ #include #include +// The suites build as C23, where every compiler the family takes has +// [[nodiscard]], so MUNI_NODISCARD expands to it and a dropped status +// warns (GCC 14 reports its C23 as 202000L). +#define TEXT(x) #x +#define EXPANDED(x) TEXT(x) +static_assert(sizeof(EXPANDED(MUNI_NODISCARD)) > 1, "MUNI_NODISCARD is the attribute in C23"); + static void TestVersionMatchesHeader(void) { muniVersion version = muniGetVersion();