diff --git a/common/include/nodiscard.h b/common/include/nodiscard.h new file mode 100644 index 0000000000..5d51254e97 --- /dev/null +++ b/common/include/nodiscard.h @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* Copyright (C) 2024 Intel Corporation + * Mariusz Zaborski + */ + +/* + * This file contains a definition for NODISCARD, a macro that wraps + * 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 the macros and use [[nodiscard]] directly, after dropping *EL8 + * and Ubuntu 20.04 support. + */ + +#pragma once + +#if defined(__has_c_attribute) +#if __has_c_attribute(nodiscard) +#define NODISCARD [[nodiscard]] +#endif +#endif + +#ifndef NODISCARD +#define NODISCARD +#endif diff --git a/common/include/pal_error.h b/common/include/pal_error.h index b71a9ccc07..89cb1d8dbe 100644 --- a/common/include/pal_error.h +++ b/common/include/pal_error.h @@ -7,9 +7,10 @@ #pragma once +#include #include -typedef enum _pal_error_t { +typedef enum NODISCARD { PAL_ERROR_SUCCESS = 0, PAL_ERROR_NOTIMPLEMENTED, PAL_ERROR_NOTDEFINED, diff --git a/meson.build b/meson.build index ce50fac3ff..329272b844 100644 --- a/meson.build +++ b/meson.build @@ -16,6 +16,18 @@ project( ], ) +# If C23 (or experimetal C23 - C2x) is available, use it. +# 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 +# least gcc 14). +if meson.get_compiler('c').has_argument('-std=c23') + add_project_arguments('-std=c23', language: 'c') +elif meson.get_compiler('c').has_argument('-std=c2x') + add_project_arguments('-std=c2x', language: 'c') +endif + # we need this subdir() early, because we need scripts defined there for setting up global vars subdir('scripts')