Skip to content

Commit cc50765

Browse files
thugheskartben
authored andcommitted
toolchain: Add macros to disable compiler warnings
These macros allow disabling compiler warnings for all compilers or only gcc or only clang. Signed-off-by: Tom Hughes <tomhughes@chromium.org>
1 parent 5e7df92 commit cc50765

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

include/zephyr/toolchain.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,70 @@
145145
#define TOOLCHAIN_IGNORE_WSHADOW_END
146146
#endif
147147

148+
/**
149+
* @def TOOLCHAIN_PRAGMA
150+
* @brief Helper for using pragma in macros.
151+
*/
152+
#ifdef TOOLCHAIN_HAS_PRAGMA_DIAG
153+
#define TOOLCHAIN_PRAGMA(x) _Pragma(#x)
154+
#else
155+
#define TOOLCHAIN_PRAGMA(x)
156+
#endif
157+
158+
/**
159+
* @def TOOLCHAIN_DISABLE_WARNING
160+
* @brief Disable the specified compiler warning for all compilers.
161+
*/
162+
#ifndef TOOLCHAIN_DISABLE_WARNING
163+
#define TOOLCHAIN_DISABLE_WARNING(warning)
164+
#endif
165+
166+
/**
167+
* @def TOOLCHAIN_ENABLE_WARNING
168+
* @brief Re-enable the specified compiler warning for all compilers.
169+
*
170+
* Can only be used after a call to @ref TOOLCHAIN_DISABLE_WARNING.
171+
*/
172+
#ifndef TOOLCHAIN_ENABLE_WARNING
173+
#define TOOLCHAIN_ENABLE_WARNING(warning)
174+
#endif
175+
176+
/**
177+
* @def TOOLCHAIN_DISABLE_CLANG_WARNING
178+
* @brief Disable the specified compiler warning for clang.
179+
*/
180+
#ifndef TOOLCHAIN_DISABLE_CLANG_WARNING
181+
#define TOOLCHAIN_DISABLE_CLANG_WARNING(warning)
182+
#endif
183+
184+
/**
185+
* @def TOOLCHAIN_ENABLE_CLANG_WARNING
186+
* @brief Re-enable the specified compiler warning for clang.
187+
*
188+
* Can only be used after a call to @ref TOOLCHAIN_DISABLE_CLANG_WARNING.
189+
*/
190+
#ifndef TOOLCHAIN_ENABLE_CLANG_WARNING
191+
#define TOOLCHAIN_ENABLE_CLANG_WARNING(warning)
192+
#endif
193+
194+
/**
195+
* @def TOOLCHAIN_DISABLE_GCC_WARNING
196+
* @brief Disable the specified compiler warning for gcc.
197+
*/
198+
#ifndef TOOLCHAIN_DISABLE_GCC_WARNING
199+
#define TOOLCHAIN_DISABLE_GCC_WARNING(warning)
200+
#endif
201+
202+
/**
203+
* @def TOOLCHAIN_ENABLE_GCC_WARNING
204+
* @brief Re-enable the specified compiler warning for gcc.
205+
*
206+
* Can only be used after a call to @ref TOOLCHAIN_DISABLE_GCC_WARNING.
207+
*/
208+
#ifndef TOOLCHAIN_ENABLE_GCC_WARNING
209+
#define TOOLCHAIN_ENABLE_GCC_WARNING(warning)
210+
#endif
211+
148212
/*
149213
* Ensure that __BYTE_ORDER__ and related preprocessor definitions are defined,
150214
* and that they match the Kconfig option that is used in the code itself to

include/zephyr/toolchain/gcc.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,4 +687,19 @@ do { \
687687
_Pragma("GCC diagnostic pop")
688688

689689
#endif /* !_LINKER */
690+
691+
#define _TOOLCHAIN_DISABLE_WARNING(compiler, warning) \
692+
TOOLCHAIN_PRAGMA(compiler diagnostic push) \
693+
TOOLCHAIN_PRAGMA(compiler diagnostic ignored warning)
694+
695+
#define _TOOLCHAIN_ENABLE_WARNING(compiler, warning) TOOLCHAIN_PRAGMA(compiler diagnostic pop)
696+
697+
#define TOOLCHAIN_DISABLE_WARNING(warning) _TOOLCHAIN_DISABLE_WARNING(GCC, warning)
698+
#define TOOLCHAIN_ENABLE_WARNING(warning) _TOOLCHAIN_ENABLE_WARNING(GCC, warning)
699+
700+
#if defined(__GNUC__) && !defined(__clang__)
701+
#define TOOLCHAIN_DISABLE_GCC_WARNING(warning) _TOOLCHAIN_DISABLE_WARNING(GCC, warning)
702+
#define TOOLCHAIN_ENABLE_GCC_WARNING(warning) _TOOLCHAIN_ENABLE_WARNING(GCC, warning)
703+
#endif
704+
690705
#endif /* ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_ */

include/zephyr/toolchain/llvm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030

3131
#include <zephyr/toolchain/gcc.h>
3232

33+
#define TOOLCHAIN_DISABLE_CLANG_WARNING(warning) _TOOLCHAIN_DISABLE_WARNING(clang, warning)
34+
#define TOOLCHAIN_ENABLE_CLANG_WARNING(warning) _TOOLCHAIN_ENABLE_WARNING(clang, warning)
35+
3336
/*
3437
* Provide these definitions only when minimal libc is used.
3538
* Avoid collision with defines from include/zephyr/toolchain/zephyr_stdint.h

0 commit comments

Comments
 (0)