[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: Leszek Swirski <leszeks@chromium.org>
Reviewed-by: Ross McIlroy <rmcilroy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#71397}
This commit is contained in:
Santiago Aboy Solanes 2020-11-25 10:50:49 +00:00 committed by Commit Bot
parent f290177fb4
commit 03a940ebee
7 changed files with 34 additions and 8 deletions

View File

@ -239,6 +239,7 @@
// V8_HAS_ATTRIBUTE_VISIBILITY - __attribute__((visibility)) supported // V8_HAS_ATTRIBUTE_VISIBILITY - __attribute__((visibility)) supported
// V8_HAS_ATTRIBUTE_WARN_UNUSED_RESULT - __attribute__((warn_unused_result)) // V8_HAS_ATTRIBUTE_WARN_UNUSED_RESULT - __attribute__((warn_unused_result))
// supported // supported
// V8_HAS_CPP_ATTRIBUTE_NODISCARD - [[nodiscard]] supported
// V8_HAS_BUILTIN_BSWAP16 - __builtin_bswap16() supported // V8_HAS_BUILTIN_BSWAP16 - __builtin_bswap16() supported
// V8_HAS_BUILTIN_BSWAP32 - __builtin_bswap32() supported // V8_HAS_BUILTIN_BSWAP32 - __builtin_bswap32() supported
// V8_HAS_BUILTIN_BSWAP64 - __builtin_bswap64() supported // V8_HAS_BUILTIN_BSWAP64 - __builtin_bswap64() supported
@ -262,6 +263,12 @@
// ... // ...
// #endif // #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(__clang__)
#if defined(__GNUC__) // Clang in gcc mode. #if defined(__GNUC__) // Clang in gcc mode.
@ -276,6 +283,8 @@
# define V8_HAS_ATTRIBUTE_WARN_UNUSED_RESULT \ # define V8_HAS_ATTRIBUTE_WARN_UNUSED_RESULT \
(__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_ASSUME_ALIGNED (__has_builtin(__builtin_assume_aligned))
# define V8_HAS_BUILTIN_BSWAP16 (__has_builtin(__builtin_bswap16)) # define V8_HAS_BUILTIN_BSWAP16 (__has_builtin(__builtin_bswap16))
# define V8_HAS_BUILTIN_BSWAP32 (__has_builtin(__builtin_bswap32)) # define V8_HAS_BUILTIN_BSWAP32 (__has_builtin(__builtin_bswap32))
@ -319,6 +328,7 @@
# define V8_HAS_ATTRIBUTE_UNUSED 1 # define V8_HAS_ATTRIBUTE_UNUSED 1
# define V8_HAS_ATTRIBUTE_VISIBILITY 1 # define V8_HAS_ATTRIBUTE_VISIBILITY 1
# define V8_HAS_ATTRIBUTE_WARN_UNUSED_RESULT (!V8_CC_INTEL) # 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_ASSUME_ALIGNED 1
# define V8_HAS_BUILTIN_CLZ 1 # define V8_HAS_BUILTIN_CLZ 1
@ -436,6 +446,20 @@
#define V8_WARN_UNUSED_RESULT /* NOT SUPPORTED */ #define V8_WARN_UNUSED_RESULT /* NOT SUPPORTED */
#endif #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. // Helper macro to define no_sanitize attributes only with clang.
#if defined(__clang__) && defined(__has_attribute) #if defined(__clang__) && defined(__has_attribute)
#if __has_attribute(no_sanitize) #if __has_attribute(no_sanitize)
@ -484,4 +508,6 @@ V8 shared library set USING_V8_SHARED.
// clang-format on // clang-format on
#undef V8_HAS_CPP_ATTRIBUTE
#endif // V8CONFIG_H_ #endif // V8CONFIG_H_

View File

@ -282,7 +282,7 @@ class V8_BASE_EXPORT SharedMutex final {
enum class NullBehavior { kRequireNotNull, kIgnoreIfNull }; enum class NullBehavior { kRequireNotNull, kIgnoreIfNull };
template <typename Mutex, NullBehavior Behavior = NullBehavior::kRequireNotNull> template <typename Mutex, NullBehavior Behavior = NullBehavior::kRequireNotNull>
class LockGuard final { class V8_NODISCARD LockGuard final {
public: public:
explicit LockGuard(Mutex* mutex) : mutex_(mutex) { explicit LockGuard(Mutex* mutex) : mutex_(mutex) {
if (has_mutex()) mutex_->Lock(); if (has_mutex()) mutex_->Lock();
@ -310,7 +310,7 @@ enum MutexSharedType : bool { kShared = true, kExclusive = false };
template <MutexSharedType kIsShared, template <MutexSharedType kIsShared,
NullBehavior Behavior = NullBehavior::kRequireNotNull> NullBehavior Behavior = NullBehavior::kRequireNotNull>
class SharedMutexGuard final { class V8_NODISCARD SharedMutexGuard final {
public: public:
explicit SharedMutexGuard(SharedMutex* mutex) : mutex_(mutex) { explicit SharedMutexGuard(SharedMutex* mutex) : mutex_(mutex) {
if (!has_mutex()) return; if (!has_mutex()) return;
@ -343,7 +343,7 @@ class SharedMutexGuard final {
template <MutexSharedType kIsShared, template <MutexSharedType kIsShared,
NullBehavior Behavior = NullBehavior::kRequireNotNull> NullBehavior Behavior = NullBehavior::kRequireNotNull>
class SharedMutexGuardIf final { class V8_NODISCARD SharedMutexGuardIf final {
public: public:
SharedMutexGuardIf(SharedMutex* mutex, bool enable_mutex) { SharedMutexGuardIf(SharedMutex* mutex, bool enable_mutex) {
if (enable_mutex) mutex_.emplace(mutex); if (enable_mutex) mutex_.emplace(mutex);

View File

@ -236,7 +236,7 @@ class DisallowHeapAccessIf {
// Like MutexGuard but also asserts that no garbage collection happens while // Like MutexGuard but also asserts that no garbage collection happens while
// we're holding the mutex. // we're holding the mutex.
class NoGarbageCollectionMutexGuard { class V8_NODISCARD NoGarbageCollectionMutexGuard {
public: public:
explicit NoGarbageCollectionMutexGuard(base::Mutex* mutex) explicit NoGarbageCollectionMutexGuard(base::Mutex* mutex)
: guard_(mutex), mutex_(mutex), no_gc_(new DisallowGarbageCollection()) {} : guard_(mutex), mutex_(mutex), no_gc_(new DisallowGarbageCollection()) {}

View File

@ -21,7 +21,7 @@ class RootVisitor;
// StackGuard contains the handling of the limits that are used to limit the // 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 // number of nested invocations of JavaScript and the stack size used in each
// invocation. // invocation.
class V8_EXPORT_PRIVATE StackGuard final { class V8_EXPORT_PRIVATE V8_NODISCARD StackGuard final {
public: public:
explicit StackGuard(Isolate* isolate) : isolate_(isolate) {} explicit StackGuard(Isolate* isolate) : isolate_(isolate) {}

View File

@ -44,7 +44,7 @@ class UnparkedScope {
LocalHeap* const local_heap_; LocalHeap* const local_heap_;
}; };
class ParkedMutexGuard { class V8_NODISCARD ParkedMutexGuard {
base::Mutex* guard_; base::Mutex* guard_;
public: public:

View File

@ -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 // A helper that uses an std::atomic_bool to create a lock that is obtained on
// construction and released on destruction. // construction and released on destruction.
class V8_EXPORT_PRIVATE AtomicGuard { class V8_EXPORT_PRIVATE V8_NODISCARD AtomicGuard {
public: public:
// Attempt to obtain the lock represented by |atomic|. |is_blocking| // Attempt to obtain the lock represented by |atomic|. |is_blocking|
// determines whether we will block to obtain the lock, or only make one // determines whether we will block to obtain the lock, or only make one

View File

@ -26,7 +26,7 @@ namespace internal {
#include "torque-generated/src/objects/string-tq-inl.inc" #include "torque-generated/src/objects/string-tq-inl.inc"
class SharedStringAccessGuardIfNeeded { class V8_NODISCARD SharedStringAccessGuardIfNeeded {
public: public:
// Creates a SharedMutexGuard<kShared> for the string access if: // Creates a SharedMutexGuard<kShared> for the string access if:
// A) {str} is not a read only string, and // A) {str} is not a read only string, and