Skip to content

Commit

Permalink
[meson,common] Enable C23 and nodiscard feature when available
Browse files Browse the repository at this point in the history
Signed-off-by: Mariusz Zaborski <oshogbo@invisiblethingslab.com>
  • Loading branch information
oshogbo committed May 10, 2024
1 parent fda2f87 commit ab24fbc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
23 changes: 23 additions & 0 deletions common/include/nodiscard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2024 Intel Corporation
* Mariusz Zaborski <oshogbo@invisiblethingslab.com>
*/

/*
* 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.
*/

#pragma once

#if defined(__has_c_attribute)
#if __has_c_attribute(nodiscard)
#define NODISCARD [[nodiscard]]
#endif
#endif

#ifndef NODISCARD
#define NODISCARD
#endif
3 changes: 2 additions & 1 deletion common/include/pal_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

#pragma once

#include <nodiscard.h>
#include <stddef.h>

typedef enum _pal_error_t {
typedef enum NODISCARD _pal_error_t {
PAL_ERROR_SUCCESS = 0,
PAL_ERROR_NOTIMPLEMENTED,
PAL_ERROR_NOTDEFINED,
Expand Down
9 changes: 9 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ 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')
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')

Expand Down

0 comments on commit ab24fbc

Please sign in to comment.