aboutsummaryrefslogtreecommitdiff
path: root/pw_preprocessor/public
diff options
context:
space:
mode:
authorWyatt Hepler <hepler@google.com>2022-02-23 11:02:28 -0800
committerCQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-03-23 18:14:14 +0000
commita996a5ecc8dbcf3b40fc7b9e7fb36b9ea3c9db16 (patch)
treec592204dc8c89c40656d5824f43a6d211d8b28d0 /pw_preprocessor/public
parent9f29451249c25f28f504bc6148e5a5612c4dcd94 (diff)
downloadpigweed-a996a5ecc8dbcf3b40fc7b9e7fb36b9ea3c9db16.tar.gz
pw_preprocessor: PW_HAVE_CPP_ATTRIBUTE and new attribute
- PW_HAVE_CPP_ATTRIBUTE() checks if a C++-style attribute is supported. - PW_ATTRIBUTE_LIFETIME_BOUND marks a returned reference as having a lifetime bound to a containing object. Change-Id: I59f2c8f7d7c8c1c1866a70ebd234b867f2377fd7 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/85521 Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com> Reviewed-by: Ewout van Bekkum <ewout@google.com> Commit-Queue: Wyatt Hepler <hepler@google.com>
Diffstat (limited to 'pw_preprocessor/public')
-rw-r--r--pw_preprocessor/public/pw_preprocessor/compiler.h36
1 files changed, 35 insertions, 1 deletions
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