Commit 03a940eb authored by Santiago Aboy Solanes's avatar Santiago Aboy Solanes Committed by Commit Bot

[config] Add [[nodiscard]] as an attribute to v8config

This allows us to assert at compile time that a class instance is
assigned, which is particularly useful for Guard classes.

Change-Id: Id16b2bb70d29573566e821c908c1169d49ec57af
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2552415
Commit-Queue: Santiago Aboy Solanes <solanes@chromium.org>
Reviewed-by: 's avatarLeszek Swirski <leszeks@chromium.org>
Reviewed-by: 's avatarRoss McIlroy <rmcilroy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#71397}
parent f290177f
......@@ -239,6 +239,7 @@
// V8_HAS_ATTRIBUTE_VISIBILITY - __attribute__((visibility)) supported
// V8_HAS_ATTRIBUTE_WARN_UNUSED_RESULT - __attribute__((warn_unused_result))
// supported
// V8_HAS_CPP_ATTRIBUTE_NODISCARD - [[nodiscard]] supported
// V8_HAS_BUILTIN_BSWAP16 - __builtin_bswap16() supported
// V8_HAS_BUILTIN_BSWAP32 - __builtin_bswap32() supported
// V8_HAS_BUILTIN_BSWAP64 - __builtin_bswap64() supported
......@@ -262,6 +263,12 @@
// ...
// #endif
#if defined(__has_cpp_attribute)
#define V8_HAS_CPP_ATTRIBUTE(FEATURE) __has_cpp_attribute(FEATURE)
#else
#define V8_HAS_CPP_ATTRIBUTE(FEATURE) 0
#endif
#if defined(__clang__)
#if defined(__GNUC__) // Clang in gcc mode.
......@@ -276,6 +283,8 @@
# define V8_HAS_ATTRIBUTE_WARN_UNUSED_RESULT \
(__has_attribute(warn_unused_result))
# define V8_HAS_CPP_ATTRIBUTE_NODISCARD (V8_HAS_CPP_ATTRIBUTE(nodiscard))
# define V8_HAS_BUILTIN_ASSUME_ALIGNED (__has_builtin(__builtin_assume_aligned))
# define V8_HAS_BUILTIN_BSWAP16 (__has_builtin(__builtin_bswap16))
# define V8_HAS_BUILTIN_BSWAP32 (__has_builtin(__builtin_bswap32))
......@@ -319,6 +328,7 @@
# define V8_HAS_ATTRIBUTE_UNUSED 1
# define V8_HAS_ATTRIBUTE_VISIBILITY 1
# define V8_HAS_ATTRIBUTE_WARN_UNUSED_RESULT (!V8_CC_INTEL)
# define V8_HAS_CPP_ATTRIBUTE_NODISCARD (V8_HAS_CPP_ATTRIBUTE(nodiscard))
# define V8_HAS_BUILTIN_ASSUME_ALIGNED 1
# define V8_HAS_BUILTIN_CLZ 1
......@@ -436,6 +446,20 @@
#define V8_WARN_UNUSED_RESULT /* NOT SUPPORTED */
#endif
// Annotate a class or constructor indicating the caller must assign the
// constructed instances.
// Apply to the whole class like:
// class V8_NODISCARD Foo() { ... };
// or apply to just one constructor like:
// V8_NODISCARD Foo() { ... };
// [[nodiscard]] comes in C++17 but supported in clang with -std >= c++11.
#if V8_HAS_CPP_ATTRIBUTE_NODISCARD
#define V8_NODISCARD [[nodiscard]]
#else
#define V8_NODISCARD /* NOT SUPPORTED */
#endif
// Helper macro to define no_sanitize attributes only with clang.
#if defined(__clang__) && defined(__has_attribute)
#if __has_attribute(no_sanitize)
......@@ -484,4 +508,6 @@ V8 shared library set USING_V8_SHARED.
// clang-format on
#undef V8_HAS_CPP_ATTRIBUTE
#endif // V8CONFIG_H_
......@@ -282,7 +282,7 @@ class V8_BASE_EXPORT SharedMutex final {
enum class NullBehavior { kRequireNotNull, kIgnoreIfNull };
template <typename Mutex, NullBehavior Behavior = NullBehavior::kRequireNotNull>
class LockGuard final {
class V8_NODISCARD LockGuard final {
public:
explicit LockGuard(Mutex* mutex) : mutex_(mutex) {
if (has_mutex()) mutex_->Lock();
......@@ -310,7 +310,7 @@ enum MutexSharedType : bool { kShared = true, kExclusive = false };
template <MutexSharedType kIsShared,
NullBehavior Behavior = NullBehavior::kRequireNotNull>
class SharedMutexGuard final {
class V8_NODISCARD SharedMutexGuard final {
public:
explicit SharedMutexGuard(SharedMutex* mutex) : mutex_(mutex) {
if (!has_mutex()) return;
......@@ -343,7 +343,7 @@ class SharedMutexGuard final {
template <MutexSharedType kIsShared,
NullBehavior Behavior = NullBehavior::kRequireNotNull>
class SharedMutexGuardIf final {
class V8_NODISCARD SharedMutexGuardIf final {
public:
SharedMutexGuardIf(SharedMutex* mutex, bool enable_mutex) {
if (enable_mutex) mutex_.emplace(mutex);
......
......@@ -236,7 +236,7 @@ class DisallowHeapAccessIf {
// Like MutexGuard but also asserts that no garbage collection happens while
// we're holding the mutex.
class NoGarbageCollectionMutexGuard {
class V8_NODISCARD NoGarbageCollectionMutexGuard {
public:
explicit NoGarbageCollectionMutexGuard(base::Mutex* mutex)
: guard_(mutex), mutex_(mutex), no_gc_(new DisallowGarbageCollection()) {}
......
......@@ -21,7 +21,7 @@ class RootVisitor;
// StackGuard contains the handling of the limits that are used to limit the
// number of nested invocations of JavaScript and the stack size used in each
// invocation.
class V8_EXPORT_PRIVATE StackGuard final {
class V8_EXPORT_PRIVATE V8_NODISCARD StackGuard final {
public:
explicit StackGuard(Isolate* isolate) : isolate_(isolate) {}
......
......@@ -44,7 +44,7 @@ class UnparkedScope {
LocalHeap* const local_heap_;
};
class ParkedMutexGuard {
class V8_NODISCARD ParkedMutexGuard {
base::Mutex* guard_;
public:
......
......@@ -97,7 +97,7 @@ using AtomicMutex = std::atomic_bool;
// A helper that uses an std::atomic_bool to create a lock that is obtained on
// construction and released on destruction.
class V8_EXPORT_PRIVATE AtomicGuard {
class V8_EXPORT_PRIVATE V8_NODISCARD AtomicGuard {
public:
// Attempt to obtain the lock represented by |atomic|. |is_blocking|
// determines whether we will block to obtain the lock, or only make one
......
......@@ -26,7 +26,7 @@ namespace internal {
#include "torque-generated/src/objects/string-tq-inl.inc"
class SharedStringAccessGuardIfNeeded {
class V8_NODISCARD SharedStringAccessGuardIfNeeded {
public:
// Creates a SharedMutexGuard<kShared> for the string access if:
// A) {str} is not a read only string, and
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment