Skip to contentSkip to conversation

compiler_attributes.h: move __compiletime_{error|warning}

PATCH Superseded Nick Desaulniers 2 participants 3 messages Jul 20, 2021 Compiler attributes

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 compiler_attributes.h: move __compiletime_{error|warning}

I'm working on adding support for __attribute__((__error__(""))) and __attribute__((__warning__(""))) to Clang. 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.

---
Note that this feature hasn't landed yet in clang! I expect it to land in clang-13 though soon. We can still move this in anticipation of the feature landing, though perhaps the versions might not be precise if it slips to clang-14 (though I don't anticipate that).

 include/linux/compiler-gcc.h        |  3 ---
 include/linux/compiler_attributes.h | 30 ++++++++++++++++++++++++++---
 include/linux/compiler_types.h      |  6 ------
 3 files changed, 27 insertions(+), 12 deletions(-)
include/linux/compiler-gcc.h +0 -3
index cb9217fc60af..21c36b69eb06 100644
@@ -43,9 +43,6 @@
 
 #define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
 
-#define __compiletime_warning(message) __attribute__((__warning__(message)))
-#define __compiletime_error(message) __attribute__((__error__(message)))
-
 #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
 #define __latent_entropy __attribute__((latent_entropy))
 #endif
include/linux/compiler_attributes.h +27 -3
index 2487be0e7199..5f474b593425 100644
@@ -30,14 +30,16 @@
 # define __GCC4_has_attribute___assume_aligned__      1
 # define __GCC4_has_attribute___copy__                0
 # define __GCC4_has_attribute___designated_init__     0
+# define __GCC4_has_attribute___error__               1
 # define __GCC4_has_attribute___externally_visible__  1
+# define __GCC4_has_attribute___fallthrough__         0
 # define __GCC4_has_attribute___no_caller_saved_registers__ 0
-# define __GCC4_has_attribute___noclone__             1
 # define __GCC4_has_attribute___no_profile_instrument_function__ 0
-# define __GCC4_has_attribute___nonstring__           0
 # define __GCC4_has_attribute___no_sanitize_address__ 1
 # define __GCC4_has_attribute___no_sanitize_undefined__ 1
-# define __GCC4_has_attribute___fallthrough__         0
+# define __GCC4_has_attribute___noclone__             1
+# define __GCC4_has_attribute___nonstring__           0
+# define __GCC4_has_attribute___warning__             1
 #endif
 
 /*
@@ -137,6 +139,17 @@
 # define __designated_init
 #endif
 
+/*
+ * Optional: only supported since clang >= 13.0
+ *
+ *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-error-function-attribute
+ */
+#if __has_attribute(__error__)
+# define __compiletime_error(msg)       __attribute__((__error__(msg)))
+#else
+# define __compiletime_error(msg)
+#endif
+
 /*
  * Optional: not supported by clang
  *
@@ -298,6 +311,17 @@
  */
 #define __must_check                    __attribute__((__warn_unused_result__))
 
+/*
+ * Optional: only supported since clang >= 13.0
+ *
+ *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-warning-function-attribute
+ */
+#if __has_attribute(warning)
+# define __compiletime_warning(msg)     __attribute__((__warning__(msg)))
+#else
+# define __compiletime_warning(msg)
+#endif
+
 /*
  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-weak-function-attribute
  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-weak-variable-attribute
include/linux/compiler_types.h +0 -6
index e4ea86fc584d..b6ff83a714ca 100644
@@ -294,12 +294,6 @@ struct ftrace_likely_data {
 #ifndef __compiletime_object_size
 # define __compiletime_object_size(obj) -1
 #endif
-#ifndef __compiletime_warning
-# define __compiletime_warning(message)
-#endif
-#ifndef __compiletime_error
-# define __compiletime_error(message)
-#endif
 
 #ifdef __OPTIMIZE__
 # define __compiletime_assert(condition, msg, prefix, suffix)		\
Signature · 1 line
2.32.0.402.g57bb445576-goog
#1 lore raw
Quoted 79 lines · Nick Desaulniers

I'm working on adding support for __attribute__((__error__(""))) and __attribute__((__warning__(""))) to Clang. 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.

---
Note that this feature hasn't landed yet in clang! I expect it to land in clang-13 though soon. We can still move this in anticipation of the feature landing, though perhaps the versions might not be precise if it slips to clang-14 (though I don't anticipate that).

  include/linux/compiler-gcc.h        |  3 ---
  include/linux/compiler_attributes.h | 30 ++++++++++++++++++++++++++---
  include/linux/compiler_types.h      |  6 ------
  3 files changed, 27 insertions(+), 12 deletions(-)
include/linux/compiler-gcc.h +0 -3
index cb9217fc60af..21c36b69eb06 100644
@@ -43,9 +43,6 @@
 
  #define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
 
-#define __compiletime_warning(message) __attribute__((__warning__(message)))
-#define __compiletime_error(message) __attribute__((__error__(message)))
-
  #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
  #define __latent_entropy __attribute__((latent_entropy))
  #endif
include/linux/compiler_attributes.h +16 -3
index 2487be0e7199..5f474b593425 100644
@@ -30,14 +30,16 @@
  # define __GCC4_has_attribute___assume_aligned__      1
  # define __GCC4_has_attribute___copy__                0
  # define __GCC4_has_attribute___designated_init__     0
+# define __GCC4_has_attribute___error__               1
  # define __GCC4_has_attribute___externally_visible__  1
+# define __GCC4_has_attribute___fallthrough__         0
  # define __GCC4_has_attribute___no_caller_saved_registers__ 0
-# define __GCC4_has_attribute___noclone__             1
  # define __GCC4_has_attribute___no_profile_instrument_function__ 0
-# define __GCC4_has_attribute___nonstring__           0
  # define __GCC4_has_attribute___no_sanitize_address__ 1
  # define __GCC4_has_attribute___no_sanitize_undefined__ 1
-# define __GCC4_has_attribute___fallthrough__         0
+# define __GCC4_has_attribute___noclone__             1
+# define __GCC4_has_attribute___nonstring__           0
+# define __GCC4_has_attribute___warning__             1
  #endif
 
  /*
@@ -137,6 +139,17 @@
  # define __designated_init
  #endif
 
+/*
+ * Optional: only supported since clang >= 13.0
+ *
+ *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-error-function-attribute
+ */
+#if __has_attribute(__error__)
+# define __compiletime_error(msg)       __attribute__((__error__(msg)))
+#else
+# define __compiletime_error(msg)
+#endif
+
  /*
   * Optional: not supported by clang
   *

include/linux/compiler_attributes.h line 319

Quoted 9 lines
include/linux/compiler_attributes.h +6 -0
@@ -298,6 +311,17 @@
   */
  #define __must_check                    __attribute__((__warn_unused_result__))
 
+/*
+ * Optional: only supported since clang >= 13.0
+ *
+ *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-warning-function-attribute
+ */
+#if __has_attribute(warning)

Should this be

#if __has_attribute(__warning__)

?

Quoted 26 lines
include/linux/compiler_attributes.h +5 -0
+# define __compiletime_warning(msg)     __attribute__((__warning__(msg)))
+#else
+# define __compiletime_warning(msg)
+#endif
+
  /*
   *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-weak-function-attribute
   *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-weak-variable-attribute
include/linux/compiler_types.h +0 -6
index e4ea86fc584d..b6ff83a714ca 100644
@@ -294,12 +294,6 @@ struct ftrace_likely_data {
  #ifndef __compiletime_object_size
  # define __compiletime_object_size(obj) -1
  #endif
-#ifndef __compiletime_warning
-# define __compiletime_warning(message)
-#endif
-#ifndef __compiletime_error
-# define __compiletime_error(message)
-#endif
 
  #ifdef __OPTIMIZE__
  # define __compiletime_assert(condition, msg, prefix, suffix)		\
#2 lore raw
Quoted 93 lines · Nathan Chancellor
Quoted 79 lines · Nick Desaulniers

I'm working on adding support for __attribute__((__error__(""))) and __attribute__((__warning__(""))) to Clang. 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.

---
Note that this feature hasn't landed yet in clang! I expect it to land in clang-13 though soon. We can still move this in anticipation of the feature landing, though perhaps the versions might not be precise if it slips to clang-14 (though I don't anticipate that).

  include/linux/compiler-gcc.h        |  3 ---
  include/linux/compiler_attributes.h | 30 ++++++++++++++++++++++++++---
  include/linux/compiler_types.h      |  6 ------
  3 files changed, 27 insertions(+), 12 deletions(-)
include/linux/compiler-gcc.h +0 -3
index cb9217fc60af..21c36b69eb06 100644
@@ -43,9 +43,6 @@
 
  #define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
 
-#define __compiletime_warning(message) __attribute__((__warning__(message)))
-#define __compiletime_error(message) __attribute__((__error__(message)))
-
  #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
  #define __latent_entropy __attribute__((latent_entropy))
  #endif
include/linux/compiler_attributes.h +16 -3
index 2487be0e7199..5f474b593425 100644
@@ -30,14 +30,16 @@
  # define __GCC4_has_attribute___assume_aligned__      1
  # define __GCC4_has_attribute___copy__                0
  # define __GCC4_has_attribute___designated_init__     0
+# define __GCC4_has_attribute___error__               1
  # define __GCC4_has_attribute___externally_visible__  1
+# define __GCC4_has_attribute___fallthrough__         0
  # define __GCC4_has_attribute___no_caller_saved_registers__ 0
-# define __GCC4_has_attribute___noclone__             1
  # define __GCC4_has_attribute___no_profile_instrument_function__ 0
-# define __GCC4_has_attribute___nonstring__           0
  # define __GCC4_has_attribute___no_sanitize_address__ 1
  # define __GCC4_has_attribute___no_sanitize_undefined__ 1
-# define __GCC4_has_attribute___fallthrough__         0
+# define __GCC4_has_attribute___noclone__             1
+# define __GCC4_has_attribute___nonstring__           0
+# define __GCC4_has_attribute___warning__             1
  #endif
 
  /*
@@ -137,6 +139,17 @@
  # define __designated_init
  #endif
 
+/*
+ * Optional: only supported since clang >= 13.0
+ *
+ *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-error-function-attribute
+ */
+#if __has_attribute(__error__)
+# define __compiletime_error(msg)       __attribute__((__error__(msg)))
+#else
+# define __compiletime_error(msg)
+#endif
+
  /*
   * Optional: not supported by clang
   *

include/linux/compiler_attributes.h line 319

Quoted 9 lines
include/linux/compiler_attributes.h +6 -0
@@ -298,6 +311,17 @@
   */
  #define __must_check                    __attribute__((__warn_unused_result__))
 
+/*
+ * Optional: only supported since clang >= 13.0
+ *
+ *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-warning-function-attribute
+ */
+#if __has_attribute(warning)

Should this be

Quoted 3 lines

#if __has_attribute(__warning__)

?

Yes. :-X

Quoted 27 lines
Quoted 27 lines
include/linux/compiler_attributes.h +5 -0
+# define __compiletime_warning(msg)     __attribute__((__warning__(msg)))
+#else
+# define __compiletime_warning(msg)
+#endif
+
  /*
   *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-weak-function-attribute
   *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-weak-variable-attribute
include/linux/compiler_types.h +0 -6
index e4ea86fc584d..b6ff83a714ca 100644
@@ -294,12 +294,6 @@ struct ftrace_likely_data {
  #ifndef __compiletime_object_size
  # define __compiletime_object_size(obj) -1
  #endif
-#ifndef __compiletime_warning
-# define __compiletime_warning(message)
-#endif
-#ifndef __compiletime_error
-# define __compiletime_error(message)
-#endif
 
  #ifdef __OPTIMIZE__
  # define __compiletime_assert(condition, msg, prefix, suffix)               \
Signature · 2 lines
Thanks,
~Nick Desaulniers