diff options
-rw-r--r-- | pw_preprocessor/docs.rst | 25 | ||||
-rw-r--r-- | pw_preprocessor/public/pw_preprocessor/compiler.h | 36 |
2 files changed, 60 insertions, 1 deletions
diff --git a/pw_preprocessor/docs.rst b/pw_preprocessor/docs.rst index ac6fd9991..15e9104e4 100644 --- a/pw_preprocessor/docs.rst +++ b/pw_preprocessor/docs.rst @@ -166,6 +166,12 @@ Macros for compiler-specific features, such as attributes or builtins. evaluates to a non zero constant integer if the attribute is supported or 0 if not. +.. c:macro:: PW_HAVE_CPP_ATTRIBUTE(x) + + Wrapper around `__has_cpp_attribute`, which was introduced in the C++20 + standard. It is supported by compilers even if C++20 is not in use. Evaluates + to a non zero constant integer if the C++ attribute is supported or 0 if not. + .. c:macro:: PW_PRAGMA(contents) Expands to a _Pragma with the contents as a string. _Pragma must take a @@ -197,6 +203,25 @@ Macros for compiler-specific features, such as attributes or builtins. // Driver handler replaced with default unless overridden. void USART_DriverHandler(void) PW_ALIAS(DefaultDriverHandler); +.. c:macro:: PW_ATTRIBUTE_LIFETIME_BOUND + + PW_ATTRIBUTE_LIFETIME_BOUND indicates that a resource owned by a function + parameter or implicit object parameter is retained by the return value of the + annotated function (or, for a parameter of a constructor, in the value of the + constructed object). This attribute causes warnings to be produced if a + temporary object does not live long enough. + + When applied to a reference parameter, the referenced object is assumed to be + retained by the return value of the function. When applied to a non-reference + parameter (for example, a pointer or a class type), all temporaries + referenced by the parameter are assumed to be retained by the return value of + the function. + + See also the upstream documentation: + https://clang.llvm.org/docs/AttributeReference.html#lifetimebound + + This is a copy of ABSL_ATTRIBUTE_LIFETIME_BOUND. + Modifying compiler diagnostics ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``pw_preprocessor/compiler.h`` provides macros for enabling or disabling diff --git a/pw_preprocessor/public/pw_preprocessor/compiler.h b/pw_preprocessor/public/pw_preprocessor/compiler.h index d682260b4..690a061b9 100644 --- a/pw_preprocessor/public/pw_preprocessor/compiler.h +++ b/pw_preprocessor/public/pw_preprocessor/compiler.h @@ -127,7 +127,17 @@ #define PW_HAVE_ATTRIBUTE(x) __has_attribute(x) #else #define PW_HAVE_ATTRIBUTE(x) 0 -#endif +#endif // __has_attribute + +// A function-like feature checking macro that accepts C++11 style attributes. +// It's a wrapper around __has_cpp_attribute +// (https://en.cppreference.com/w/cpp/feature_test), borrowed from +// ABSL_HAVE_CPP_ATTRIBUTE. If there is no __has_cpp_attribute, evaluates to 0. +#if defined(__cplusplus) && defined(__has_cpp_attribute) +#define PW_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) +#else +#define PW_HAVE_CPP_ATTRIBUTE(x) 0 +#endif // defined(__cplusplus) && defined(__has_cpp_attribute) #define _PW_REQUIRE_SEMICOLON \ static_assert(1, "This macro must be terminated with a semicolon") @@ -182,3 +192,27 @@ // // Driver handler replaced with default unless overridden. // void USART_DriverHandler(void) PW_ALIAS(DefaultDriverHandler); #define PW_ALIAS(aliased_to) __attribute__((weak, alias(#aliased_to))) + +// PW_ATTRIBUTE_LIFETIME_BOUND indicates that a resource owned by a function +// parameter or implicit object parameter is retained by the return value of the +// annotated function (or, for a parameter of a constructor, in the value of the +// constructed object). This attribute causes warnings to be produced if a +// temporary object does not live long enough. +// +// When applied to a reference parameter, the referenced object is assumed to be +// retained by the return value of the function. When applied to a non-reference +// parameter (for example, a pointer or a class type), all temporaries +// referenced by the parameter are assumed to be retained by the return value of +// the function. +// +// See also the upstream documentation: +// https://clang.llvm.org/docs/AttributeReference.html#lifetimebound +// +// This is a copy of ABSL_ATTRIBUTE_LIFETIME_BOUND. +#if PW_HAVE_CPP_ATTRIBUTE(clang::lifetimebound) +#define PW_ATTRIBUTE_LIFETIME_BOUND [[clang::lifetimebound]] +#elif PW_HAVE_ATTRIBUTE(lifetimebound) +#define PW_ATTRIBUTE_LIFETIME_BOUND __attribute__((lifetimebound)) +#else +#define PW_ATTRIBUTE_LIFETIME_BOUND +#endif // PW_ATTRIBUTE_LIFETIME_BOUND |