diff --git a/include/v8config.h b/include/v8config.h index ae89edb2c9..0886f691d5 100644 --- a/include/v8config.h +++ b/include/v8config.h @@ -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_ diff --git a/src/base/platform/mutex.h b/src/base/platform/mutex.h index 2db68b8f1f..1911bdbfd4 100644 --- a/src/base/platform/mutex.h +++ b/src/base/platform/mutex.h @@ -282,7 +282,7 @@ class V8_BASE_EXPORT SharedMutex final { enum class NullBehavior { kRequireNotNull, kIgnoreIfNull }; template -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 -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 -class SharedMutexGuardIf final { +class V8_NODISCARD SharedMutexGuardIf final { public: SharedMutexGuardIf(SharedMutex* mutex, bool enable_mutex) { if (enable_mutex) mutex_.emplace(mutex); diff --git a/src/common/assert-scope.h b/src/common/assert-scope.h index e37a302980..339d1be910 100644 --- a/src/common/assert-scope.h +++ b/src/common/assert-scope.h @@ -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()) {} diff --git a/src/execution/stack-guard.h b/src/execution/stack-guard.h index b3a577e542..93aa5170d4 100644 --- a/src/execution/stack-guard.h +++ b/src/execution/stack-guard.h @@ -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) {} diff --git a/src/heap/parked-scope.h b/src/heap/parked-scope.h index e26f98aa09..561440527d 100644 --- a/src/heap/parked-scope.h +++ b/src/heap/parked-scope.h @@ -44,7 +44,7 @@ class UnparkedScope { LocalHeap* const local_heap_; }; -class ParkedMutexGuard { +class V8_NODISCARD ParkedMutexGuard { base::Mutex* guard_; public: diff --git a/src/libsampler/sampler.h b/src/libsampler/sampler.h index e81ba9c90e..81e69580c9 100644 --- a/src/libsampler/sampler.h +++ b/src/libsampler/sampler.h @@ -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 diff --git a/src/objects/string-inl.h b/src/objects/string-inl.h index e9bce36fe7..a7e7059191 100644 --- a/src/objects/string-inl.h +++ b/src/objects/string-inl.h @@ -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 for the string access if: // A) {str} is not a read only string, and