Skip to content

Commit

Permalink
toolchain: Add macros to disable compiler warnings
Browse files Browse the repository at this point in the history
These macros allow disabling compiler warnings for all compilers or only
gcc or only clang.

Signed-off-by: Tom Hughes <tomhughes@chromium.org>
  • Loading branch information
thughes committed Feb 7, 2025
1 parent a85eb14 commit 7b0398d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
64 changes: 64 additions & 0 deletions include/zephyr/toolchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,70 @@
#define TOOLCHAIN_IGNORE_WSHADOW_END
#endif

/**
* @def TOOLCHAIN_PRAGMA
* @brief Helper for using pragma in macros.
*/
#ifdef TOOLCHAIN_HAS_PRAGMA_DIAG
#define TOOLCHAIN_PRAGMA(x) _Pragma(#x)
#else
#define TOOLCHAIN_PRAGMA(x)
#endif

/**
* @def TOOLCHAIN_DISABLE_WARNING
* @brief Disable the specified compiler warning for all compilers.
*/
#ifndef TOOLCHAIN_DISABLE_WARNING
#define TOOLCHAIN_DISABLE_WARNING(warning)
#endif

/**
* @def TOOLCHAIN_ENABLE_WARNING
* @brief Re-enable the specified compiler warning for all compilers.
*
* Can only be used after a call to @ref TOOLCHAIN_DISABLE_WARNING.
*/
#ifndef TOOLCHAIN_ENABLE_WARNING
#define TOOLCHAIN_ENABLE_WARNING(warning)
#endif

/**
* @def TOOLCHAIN_DISABLE_CLANG_WARNING
* @brief Disable the specified compiler warning for clang.
*/
#ifndef TOOLCHAIN_DISABLE_CLANG_WARNING
#define TOOLCHAIN_DISABLE_CLANG_WARNING(warning)
#endif

/**
* @def TOOLCHAIN_ENABLE_CLANG_WARNING
* @brief Re-enable the specified compiler warning for clang.
*
* Can only be used after a call to @ref TOOLCHAIN_DISABLE_CLANG_WARNING.
*/
#ifndef TOOLCHAIN_ENABLE_CLANG_WARNING
#define TOOLCHAIN_ENABLE_CLANG_WARNING(warning)
#endif

/**
* @def TOOLCHAIN_DISABLE_GCC_WARNING
* @brief Disable the specified compiler warning for gcc.
*/
#ifndef TOOLCHAIN_DISABLE_GCC_WARNING
#define TOOLCHAIN_DISABLE_GCC_WARNING(warning)
#endif

/**
* @def TOOLCHAIN_ENABLE_GCC_WARNING
* @brief Re-enable the specified compiler warning for gcc.
*
* Can only be used after a call to @ref TOOLCHAIN_DISABLE_GCC_WARNING.
*/
#ifndef TOOLCHAIN_ENABLE_GCC_WARNING
#define TOOLCHAIN_ENABLE_GCC_WARNING(warning)
#endif

/*
* Ensure that __BYTE_ORDER__ and related preprocessor definitions are defined,
* and that they match the Kconfig option that is used in the code itself to
Expand Down
15 changes: 15 additions & 0 deletions include/zephyr/toolchain/gcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -687,4 +687,19 @@ do { \
_Pragma("GCC diagnostic pop")

#endif /* !_LINKER */

#define _TOOLCHAIN_DISABLE_WARNING(compiler, warning) \
TOOLCHAIN_PRAGMA(compiler diagnostic push) \
TOOLCHAIN_PRAGMA(compiler diagnostic ignored warning)

#define _TOOLCHAIN_ENABLE_WARNING(compiler, warning) TOOLCHAIN_PRAGMA(compiler diagnostic pop)

#define TOOLCHAIN_DISABLE_WARNING(warning) _TOOLCHAIN_DISABLE_WARNING(GCC, warning)
#define TOOLCHAIN_ENABLE_WARNING(warning) _TOOLCHAIN_ENABLE_WARNING(GCC, warning)

#if defined(__GNUC__) && !defined(__clang__)
#define TOOLCHAIN_DISABLE_GCC_WARNING(warning) _TOOLCHAIN_DISABLE_WARNING(GCC, warning)
#define TOOLCHAIN_ENABLE_GCC_WARNING(warning) _TOOLCHAIN_ENABLE_WARNING(GCC, warning)
#endif

#endif /* ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_ */
3 changes: 3 additions & 0 deletions include/zephyr/toolchain/llvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@

#include <zephyr/toolchain/gcc.h>

#define TOOLCHAIN_DISABLE_CLANG_WARNING(warning) _TOOLCHAIN_DISABLE_WARNING(clang, warning)
#define TOOLCHAIN_ENABLE_CLANG_WARNING(warning) _TOOLCHAIN_ENABLE_WARNING(clang, warning)

/*
* Provide these definitions only when minimal libc is used.
* Avoid collision with defines from include/zephyr/toolchain/zephyr_stdint.h
Expand Down

0 comments on commit 7b0398d

Please sign in to comment.