diff --git a/AK/NonnullOwnPtr.h b/AK/NonnullOwnPtr.h index cd4a8624609..1553a9c562f 100644 --- a/AK/NonnullOwnPtr.h +++ b/AK/NonnullOwnPtr.h @@ -98,17 +98,26 @@ public: return exchange(m_ptr, nullptr); } - T* ptr() { return m_ptr; } - const T* ptr() const { return m_ptr; } + ALWAYS_INLINE RETURNS_NONNULL T* ptr() + { + VERIFY(m_ptr); + return m_ptr; + } - T* operator->() { return m_ptr; } - const T* operator->() const { return m_ptr; } + ALWAYS_INLINE RETURNS_NONNULL const T* ptr() const + { + VERIFY(m_ptr); + return m_ptr; + } - T& operator*() { return *m_ptr; } - const T& operator*() const { return *m_ptr; } + ALWAYS_INLINE RETURNS_NONNULL T* operator->() { return ptr(); } + ALWAYS_INLINE RETURNS_NONNULL const T* operator->() const { return ptr(); } - operator const T*() const { return m_ptr; } - operator T*() { return m_ptr; } + ALWAYS_INLINE T& operator*() { return *ptr(); } + ALWAYS_INLINE const T& operator*() const { return *ptr(); } + + ALWAYS_INLINE RETURNS_NONNULL operator const T*() const { return ptr(); } + ALWAYS_INLINE RETURNS_NONNULL operator T*() { return ptr(); } operator bool() const = delete; bool operator!() const = delete; diff --git a/AK/NonnullRefPtr.h b/AK/NonnullRefPtr.h index 22936a2dfc4..b65f044ae91 100644 --- a/AK/NonnullRefPtr.h +++ b/AK/NonnullRefPtr.h @@ -156,20 +156,20 @@ public: return *ptr; } - ALWAYS_INLINE T* ptr() + ALWAYS_INLINE RETURNS_NONNULL T* ptr() { return as_nonnull_ptr(); } - ALWAYS_INLINE const T* ptr() const + ALWAYS_INLINE RETURNS_NONNULL const T* ptr() const { return as_nonnull_ptr(); } - ALWAYS_INLINE T* operator->() + ALWAYS_INLINE RETURNS_NONNULL T* operator->() { return as_nonnull_ptr(); } - ALWAYS_INLINE const T* operator->() const + ALWAYS_INLINE RETURNS_NONNULL const T* operator->() const { return as_nonnull_ptr(); } @@ -183,11 +183,11 @@ public: return *as_nonnull_ptr(); } - ALWAYS_INLINE operator T*() + ALWAYS_INLINE RETURNS_NONNULL operator T*() { return as_nonnull_ptr(); } - ALWAYS_INLINE operator const T*() const + ALWAYS_INLINE RETURNS_NONNULL operator const T*() const { return as_nonnull_ptr(); } @@ -232,7 +232,7 @@ private: return (T*)(m_bits.load(AK::MemoryOrder::memory_order_relaxed) & ~(FlatPtr)1); } - ALWAYS_INLINE T* as_nonnull_ptr() const + ALWAYS_INLINE RETURNS_NONNULL T* as_nonnull_ptr() const { T* ptr = (T*)(m_bits.load(AK::MemoryOrder::memory_order_relaxed) & ~(FlatPtr)1); VERIFY(ptr); diff --git a/AK/Platform.h b/AK/Platform.h index 54264348b4b..56aa4a1c5b3 100644 --- a/AK/Platform.h +++ b/AK/Platform.h @@ -40,6 +40,11 @@ #endif #define FLATTEN __attribute__((flatten)) +#ifdef RETURNS_NONNULL +# undef RETURNS_NONNULL +#endif +#define RETURNS_NONNULL __attribute__((returns_nonnull)) + #ifdef NO_SANITIZE_ADDRESS # undef NO_SANITIZE_ADDRESS #endif