diff --git a/common/include/nodiscard.h b/common/include/nodiscard.h index 7e9d3925e7..b456e333ba 100644 --- a/common/include/nodiscard.h +++ b/common/include/nodiscard.h @@ -8,16 +8,13 @@ * the nodiscard attribute introduced in C23. However, because Gramine supports * older systems that might not have support for C23, we have to wrap it on our * own and change it to a no-op on systems that don't support it. + * TODO: Remove this after dropping *EL8 and Ubuntu 20.04 support. */ #pragma once -#if defined(__has_c_attribute) -#if __has_c_attribute(nodiscard) +#if GRAMINE_HAS_NODISCARD #define NODISCARD [[nodiscard]] -#endif -#endif - -#ifndef NODISCARD +#else #define NODISCARD #endif diff --git a/meson.build b/meson.build index 4b590f359b..7349d56c39 100644 --- a/meson.build +++ b/meson.build @@ -17,12 +17,26 @@ project( ) # If C23 (or experimetal C23 - C2x) is available, use it. -# XXX: Gramine supports older versions of Ubuntu, so newer versions of compilers -# may not be available. -if meson.get_compiler('c').has_argument('-std=c23') +# TODO: Gramine supports older versions of gcc, so newer versions of compilers +# may not be available. +# We can change to c2x when we drop *EL8 and Ubuntu 20.04. +# We can't change to c23 for any supported versions yet (requires at +# leaast gcc 14). +nodiscard_code = '[[nodiscard]] int func() { return 0; }' +if meson.get_compiler('c').compiles( + nodiscard_code, + args : '-std=c23', + name : 'check for [[nodiscard]] and c23', + ) add_project_arguments('-std=c23', language: 'c') -elif meson.get_compiler('c').has_argument('-std=c2x') + add_global_arguments('-DGRAMINE_HAS_NODISCARD', language : 'c') +elif meson.get_compiler('c').compiles( + nodiscard_code, + args : '-std=c2x', + name : 'check for [[nodiscard]] and c2x', + ) add_project_arguments('-std=c2x', language: 'c') + add_global_arguments('-DGRAMINE_HAS_NODISCARD', language : 'c') endif # we need this subdir() early, because we need scripts defined there for setting up global vars