compiler.h: add IS_ENABLED

Import from zephyr
This commit is contained in:
Marc Kleine-Budde 2022-11-20 20:33:21 +01:00 committed by Marc Kleine-Budde
parent 0207095915
commit 26134e6a7e
3 changed files with 101 additions and 0 deletions

View File

@ -32,6 +32,8 @@
#ifndef _LINUXKPI_LINUX_COMPILER_H_
#define _LINUXKPI_LINUX_COMPILER_H_
#include "util_macro.h"
#ifndef __aligned
#define __aligned(x) __attribute__((__aligned__(x)))
#endif

50
include/util_internal.h Normal file
View File

@ -0,0 +1,50 @@
/*
* Copyright (c) 2011-2014, Wind River Systems, Inc.
* Copyright (c) 2020, Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief Misc utilities
*
* Repetitive or obscure helper macros needed by sys/util.h.
*/
#ifndef ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_H_
#define ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_H_
/* IS_ENABLED() helpers */
/* This is called from IS_ENABLED(), and sticks on a "_XXXX" prefix,
* it will now be "_XXXX1" if config_macro is "1", or just "_XXXX" if it's
* undefined.
* ENABLED: Z_IS_ENABLED2(_XXXX1)
* DISABLED Z_IS_ENABLED2(_XXXX)
*/
#define Z_IS_ENABLED1(config_macro) Z_IS_ENABLED2(_XXXX##config_macro)
/* Here's the core trick, we map "_XXXX1" to "_YYYY," (i.e. a string
* with a trailing comma), so it has the effect of making this a
* two-argument tuple to the preprocessor only in the case where the
* value is defined to "1"
* ENABLED: _YYYY, <--- note comma!
* DISABLED: _XXXX
*/
#define _XXXX1 _YYYY,
/* Then we append an extra argument to fool the gcc preprocessor into
* accepting it as a varargs macro.
* arg1 arg2 arg3
* ENABLED: Z_IS_ENABLED3(_YYYY, 1, 0)
* DISABLED Z_IS_ENABLED3(_XXXX 1, 0)
*/
#define Z_IS_ENABLED2(one_or_two_args) Z_IS_ENABLED3(one_or_two_args 1, 0)
/* And our second argument is thus now cooked to be 1 in the case
* where the value is defined to 1, and 0 if not:
*/
#define Z_IS_ENABLED3(ignore_this, val, ...) val
#endif /* ZEPHYR_INCLUDE_SYS_UTIL_INTERNAL_H_ */

49
include/util_macro.h Normal file
View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2011-2014, Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_SYS_UTIL_MACROS_H_
#define ZEPHYR_INCLUDE_SYS_UTIL_MACROS_H_
/*
* Most of the eldritch implementation details for all the macrobatics
* below (APIs like IS_ENABLED(), COND_CODE_1(), etc.) are hidden away
* in this file.
*/
#include "util_internal.h"
/**
* @brief Check for macro definition in compiler-visible expressions
*
* This trick was pioneered in Linux as the config_enabled() macro. It
* has the effect of taking a macro value that may be defined to "1"
* or may not be defined at all and turning it into a literal
* expression that can be handled by the C compiler instead of just
* the preprocessor. It is often used with a @p CONFIG_FOO macro which
* may be defined to 1 via Kconfig, or left undefined.
*
* That is, it works similarly to <tt>\#if defined(CONFIG_FOO)</tt>
* except that its expansion is a C expression. Thus, much <tt>\#ifdef</tt>
* usage can be replaced with equivalents like:
*
* if (IS_ENABLED(CONFIG_FOO)) {
* do_something_with_foo
* }
*
* This is cleaner since the compiler can generate errors and warnings
* for @p do_something_with_foo even when @p CONFIG_FOO is undefined.
*
* @param config_macro Macro to check
* @return 1 if @p config_macro is defined to 1, 0 otherwise (including
* if @p config_macro is not defined)
*/
#define IS_ENABLED(config_macro) Z_IS_ENABLED1(config_macro)
/* INTERNAL: the first pass above is just to expand any existing
* macros, we need the macro value to be e.g. a literal "1" at
* expansion time in the next macro, not "(1)", etc... Standard
* recursive expansion does not work.
*/
#endif /* ZEPHYR_INCLUDE_SYS_UTIL_MACROS_H_ */