Skip to content

Commit b83a908

Browse files
nickdesaulniersojeda
authored andcommitted
compiler_attributes.h: move __compiletime_{error|warning}
Clang 14 will add support for __attribute__((__error__(""))) and __attribute__((__warning__(""))). To make use of these in __compiletime_error and __compiletime_warning (as used by BUILD_BUG and friends) for newer clang and detect/fallback for older versions of clang, move these to compiler_attributes.h and guard them with __has_attribute preprocessor guards. Link: https://reviews.llvm.org/D106030 Link: https://bugs.llvm.org/show_bug.cgi?id=16428 Link: ClangBuiltLinux/linux#1173 Signed-off-by: Nick Desaulniers <[email protected]> Reviewed-by: Nathan Chancellor <[email protected]> Reviewed-by: Kees Cook <[email protected]> [Reworded, landed in Clang 14] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 1ca70b2 commit b83a908

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

include/linux/compiler-gcc.h

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

4444
#define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
4545

46-
#define __compiletime_warning(message) __attribute__((__warning__(message)))
47-
#define __compiletime_error(message) __attribute__((__error__(message)))
48-
4946
#if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
5047
#define __latent_entropy __attribute__((latent_entropy))
5148
#endif

include/linux/compiler_attributes.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
# define __GCC4_has_attribute___assume_aligned__ (__GNUC_MINOR__ >= 9)
3131
# define __GCC4_has_attribute___copy__ 0
3232
# define __GCC4_has_attribute___designated_init__ 0
33+
# define __GCC4_has_attribute___error__ 1
3334
# define __GCC4_has_attribute___externally_visible__ 1
3435
# define __GCC4_has_attribute___no_caller_saved_registers__ 0
3536
# define __GCC4_has_attribute___noclone__ 1
@@ -38,6 +39,7 @@
3839
# define __GCC4_has_attribute___no_sanitize_undefined__ (__GNUC_MINOR__ >= 9)
3940
# define __GCC4_has_attribute___no_sanitize_coverage__ 0
4041
# define __GCC4_has_attribute___fallthrough__ 0
42+
# define __GCC4_has_attribute___warning__ 1
4143
#endif
4244

4345
/*
@@ -137,6 +139,17 @@
137139
# define __designated_init
138140
#endif
139141

142+
/*
143+
* Optional: only supported since clang >= 14.0
144+
*
145+
* gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-error-function-attribute
146+
*/
147+
#if __has_attribute(__error__)
148+
# define __compiletime_error(msg) __attribute__((__error__(msg)))
149+
#else
150+
# define __compiletime_error(msg)
151+
#endif
152+
140153
/*
141154
* Optional: not supported by clang
142155
*
@@ -286,6 +299,17 @@
286299
*/
287300
#define __must_check __attribute__((__warn_unused_result__))
288301

302+
/*
303+
* Optional: only supported since clang >= 14.0
304+
*
305+
* gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-warning-function-attribute
306+
*/
307+
#if __has_attribute(__warning__)
308+
# define __compiletime_warning(msg) __attribute__((__warning__(msg)))
309+
#else
310+
# define __compiletime_warning(msg)
311+
#endif
312+
289313
/*
290314
* gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-weak-function-attribute
291315
* gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-weak-variable-attribute

include/linux/compiler_types.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,6 @@ struct ftrace_likely_data {
294294
#ifndef __compiletime_object_size
295295
# define __compiletime_object_size(obj) -1
296296
#endif
297-
#ifndef __compiletime_warning
298-
# define __compiletime_warning(message)
299-
#endif
300-
#ifndef __compiletime_error
301-
# define __compiletime_error(message)
302-
#endif
303297

304298
#ifdef __OPTIMIZE__
305299
# define __compiletime_assert(condition, msg, prefix, suffix) \

0 commit comments

Comments
 (0)