Skip to content

Commit

Permalink
Bug 1571631 - Replace MOZ_MUST_USE with [[nodiscard]] in mfbt. r=sg
Browse files Browse the repository at this point in the history
Also move MOZ_MUST_USE before function declarations' specifiers and return type. While clang and gcc's __attribute__((warn_unused_result)) can appear before, between, or after function specifiers and return types, the [[nodiscard]] attribute must precede the function specifiers.

Depends on D108344

Differential Revision: https://phabricator.services.mozilla.com/D108345
  • Loading branch information
cpeterso committed Mar 17, 2021
1 parent 0ea5ad6 commit 7bc752e
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 111 deletions.
4 changes: 2 additions & 2 deletions mfbt/AllocPolicy.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class MallocAllocPolicy {

void reportAllocOverflow() const {}

MOZ_MUST_USE bool checkSimulatedOOM() const { return true; }
[[nodiscard]] bool checkSimulatedOOM() const { return true; }
};

/*
Expand Down Expand Up @@ -167,7 +167,7 @@ class NeverAllocPolicy {

void reportAllocOverflow() const {}

MOZ_MUST_USE bool checkSimulatedOOM() const { return true; }
[[nodiscard]] bool checkSimulatedOOM() const { return true; }
};

} // namespace mozilla
Expand Down
2 changes: 1 addition & 1 deletion mfbt/AlreadyAddRefed.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ struct MOZ_MUST_USE_TYPE MOZ_NON_AUTOABLE already_AddRefed {
aUnused << mutableAlreadyAddRefed->take();
}

MOZ_MUST_USE T* take() {
[[nodiscard]] T* take() {
T* rawPtr = mRawPtr;
mRawPtr = nullptr;
return rawPtr;
Expand Down
2 changes: 1 addition & 1 deletion mfbt/Assertions.h
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ struct AssertionConditionType {
#define MOZ_ALWAYS_TRUE(expr) \
do { \
if (MOZ_LIKELY(expr)) { \
/* Silence MOZ_MUST_USE. */ \
/* Silence [[nodiscard]]. */ \
} else { \
MOZ_DIAGNOSTIC_ASSERT(false, #expr); \
} \
Expand Down
6 changes: 3 additions & 3 deletions mfbt/BufferList.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ class BufferList : private AllocPolicy {

// Copies aSize bytes from aData into the BufferList. The storage for these
// bytes may be split across multiple buffers. Size() is increased by aSize.
inline MOZ_MUST_USE bool WriteBytes(const char* aData, size_t aSize);
[[nodiscard]] inline bool WriteBytes(const char* aData, size_t aSize);

// Allocates a buffer of at most |aMaxBytes| bytes and, if successful, returns
// that buffer, and places its size in |aSize|. If unsuccessful, returns null
Expand Down Expand Up @@ -398,8 +398,8 @@ class BufferList : private AllocPolicy {
};

template <typename AllocPolicy>
MOZ_MUST_USE bool BufferList<AllocPolicy>::WriteBytes(const char* aData,
size_t aSize) {
[[nodiscard]] bool BufferList<AllocPolicy>::WriteBytes(const char* aData,
size_t aSize) {
MOZ_RELEASE_ASSERT(mOwning);
MOZ_RELEASE_ASSERT(mStandardCapacity);

Expand Down
18 changes: 9 additions & 9 deletions mfbt/Compression.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ class LZ4 {
* buffer (necessarily <= aMaxOutputSize)
* @return true on success, false on failure
*/
static MFBT_API MOZ_MUST_USE bool decompress(const char* aSource,
size_t aInputSize, char* aDest,
size_t aMaxOutputSize,
size_t* aOutputSize);
[[nodiscard]] static MFBT_API bool decompress(const char* aSource,
size_t aInputSize, char* aDest,
size_t aMaxOutputSize,
size_t* aOutputSize);

/**
* If the source stream is malformed, the function will stop decoding
Expand All @@ -105,11 +105,11 @@ class LZ4 {
* buffer (necessarily <= aMaxOutputSize)
* @return true on success, false on failure
*/
static MFBT_API MOZ_MUST_USE bool decompressPartial(const char* aSource,
size_t aInputSize,
char* aDest,
size_t aMaxOutputSize,
size_t* aOutputSize);
[[nodiscard]] static MFBT_API bool decompressPartial(const char* aSource,
size_t aInputSize,
char* aDest,
size_t aMaxOutputSize,
size_t* aOutputSize);

/*
* Provides the maximum size that LZ4 may output in a "worst case"
Expand Down
28 changes: 14 additions & 14 deletions mfbt/EndianUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,42 +280,42 @@ template <Endianness ThisEndian>
class Endian : private EndianUtils {
protected:
/** Read a uint16_t in ThisEndian endianness from |aPtr| and return it. */
static MOZ_MUST_USE uint16_t readUint16(const void* aPtr) {
[[nodiscard]] static uint16_t readUint16(const void* aPtr) {
return read<uint16_t>(aPtr);
}

/** Read a uint32_t in ThisEndian endianness from |aPtr| and return it. */
static MOZ_MUST_USE uint32_t readUint32(const void* aPtr) {
[[nodiscard]] static uint32_t readUint32(const void* aPtr) {
return read<uint32_t>(aPtr);
}

/** Read a uint64_t in ThisEndian endianness from |aPtr| and return it. */
static MOZ_MUST_USE uint64_t readUint64(const void* aPtr) {
[[nodiscard]] static uint64_t readUint64(const void* aPtr) {
return read<uint64_t>(aPtr);
}

/** Read a uintptr_t in ThisEndian endianness from |aPtr| and return it. */
static MOZ_MUST_USE uintptr_t readUintptr(const void* aPtr) {
[[nodiscard]] static uintptr_t readUintptr(const void* aPtr) {
return read<uintptr_t>(aPtr);
}

/** Read an int16_t in ThisEndian endianness from |aPtr| and return it. */
static MOZ_MUST_USE int16_t readInt16(const void* aPtr) {
[[nodiscard]] static int16_t readInt16(const void* aPtr) {
return read<int16_t>(aPtr);
}

/** Read an int32_t in ThisEndian endianness from |aPtr| and return it. */
static MOZ_MUST_USE int32_t readInt32(const void* aPtr) {
[[nodiscard]] static int32_t readInt32(const void* aPtr) {
return read<uint32_t>(aPtr);
}

/** Read an int64_t in ThisEndian endianness from |aPtr| and return it. */
static MOZ_MUST_USE int64_t readInt64(const void* aPtr) {
[[nodiscard]] static int64_t readInt64(const void* aPtr) {
return read<int64_t>(aPtr);
}

/** Read an intptr_t in ThisEndian endianness from |aPtr| and return it. */
static MOZ_MUST_USE intptr_t readIntptr(const void* aPtr) {
[[nodiscard]] static intptr_t readIntptr(const void* aPtr) {
return read<intptr_t>(aPtr);
}

Expand Down Expand Up @@ -353,7 +353,7 @@ class Endian : private EndianUtils {
* format for transmission.
*/
template <typename T>
MOZ_MUST_USE static T swapToLittleEndian(T aValue) {
[[nodiscard]] static T swapToLittleEndian(T aValue) {
return maybeSwap<ThisEndian, Little>(aValue);
}

Expand Down Expand Up @@ -382,7 +382,7 @@ class Endian : private EndianUtils {
* Converts a value of type T to big-endian format.
*/
template <typename T>
MOZ_MUST_USE static T swapToBigEndian(T aValue) {
[[nodiscard]] static T swapToBigEndian(T aValue) {
return maybeSwap<ThisEndian, Big>(aValue);
}

Expand Down Expand Up @@ -413,7 +413,7 @@ class Endian : private EndianUtils {
*/

template <typename T>
MOZ_MUST_USE static T swapToNetworkOrder(T aValue) {
[[nodiscard]] static T swapToNetworkOrder(T aValue) {
return swapToBigEndian(aValue);
}

Expand All @@ -432,7 +432,7 @@ class Endian : private EndianUtils {
* Converts a value of type T from little-endian format.
*/
template <typename T>
MOZ_MUST_USE static T swapFromLittleEndian(T aValue) {
[[nodiscard]] static T swapFromLittleEndian(T aValue) {
return maybeSwap<Little, ThisEndian>(aValue);
}

Expand Down Expand Up @@ -461,7 +461,7 @@ class Endian : private EndianUtils {
* Converts a value of type T from big-endian format.
*/
template <typename T>
MOZ_MUST_USE static T swapFromBigEndian(T aValue) {
[[nodiscard]] static T swapFromBigEndian(T aValue) {
return maybeSwap<Big, ThisEndian>(aValue);
}

Expand Down Expand Up @@ -491,7 +491,7 @@ class Endian : private EndianUtils {
* in network code.
*/
template <typename T>
MOZ_MUST_USE static T swapFromNetworkOrder(T aValue) {
[[nodiscard]] static T swapFromNetworkOrder(T aValue) {
return swapFromBigEndian(aValue);
}

Expand Down
3 changes: 1 addition & 2 deletions mfbt/FloatingPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -633,8 +633,7 @@ static MOZ_ALWAYS_INLINE bool FuzzyEqualsMultiplicative(
* representable (even though the bit patterns of double precision NaNs can't
* all be exactly represented in single precision).
*/
MOZ_MUST_USE
extern MFBT_API bool IsFloat32Representable(double aValue);
[[nodiscard]] extern MFBT_API bool IsFloat32Representable(double aValue);

} /* namespace mozilla */

Expand Down
40 changes: 20 additions & 20 deletions mfbt/HashFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ inline HashNumber AddUintptrToHash<8>(HashNumber aHash, uintptr_t aValue) {
template <typename T, bool TypeIsNotIntegral = !std::is_integral_v<T>,
bool TypeIsNotEnum = !std::is_enum_v<T>,
std::enable_if_t<TypeIsNotIntegral && TypeIsNotEnum, int> = 0>
MOZ_MUST_USE inline HashNumber AddToHash(HashNumber aHash, T aA) {
[[nodiscard]] inline HashNumber AddToHash(HashNumber aHash, T aA) {
/*
* Try to convert |A| to uint32_t implicitly. If this works, great. If not,
* we'll error out.
Expand All @@ -190,7 +190,7 @@ MOZ_MUST_USE inline HashNumber AddToHash(HashNumber aHash, T aA) {
}

template <typename A>
MOZ_MUST_USE inline HashNumber AddToHash(HashNumber aHash, A* aA) {
[[nodiscard]] inline HashNumber AddToHash(HashNumber aHash, A* aA) {
/*
* You might think this function should just take a void*. But then we'd only
* catch data pointers and couldn't handle function pointers.
Expand All @@ -206,20 +206,20 @@ MOZ_MUST_USE inline HashNumber AddToHash(HashNumber aHash, A* aA) {
// first implicitly converted to 32 bits and then passed to AddUintptrToHash()
// to be hashed.
template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
MOZ_MUST_USE constexpr HashNumber AddToHash(HashNumber aHash, T aA) {
[[nodiscard]] constexpr HashNumber AddToHash(HashNumber aHash, T aA) {
return detail::AddUintptrToHash<sizeof(T)>(aHash, aA);
}

template <typename T, std::enable_if_t<std::is_enum_v<T>, int> = 0>
MOZ_MUST_USE constexpr HashNumber AddToHash(HashNumber aHash, T aA) {
[[nodiscard]] constexpr HashNumber AddToHash(HashNumber aHash, T aA) {
// Hash using AddUintptrToHash with the underlying type of the enum type
using UnderlyingType = typename std::underlying_type<T>::type;
return detail::AddUintptrToHash<sizeof(UnderlyingType)>(
aHash, static_cast<UnderlyingType>(aA));
}

template <typename A, typename... Args>
MOZ_MUST_USE HashNumber AddToHash(HashNumber aHash, A aArg, Args... aArgs) {
[[nodiscard]] HashNumber AddToHash(HashNumber aHash, A aArg, Args... aArgs) {
return AddToHash(AddToHash(aHash, aArg), aArgs...);
}

Expand All @@ -231,7 +231,7 @@ MOZ_MUST_USE HashNumber AddToHash(HashNumber aHash, A aArg, Args... aArgs) {
* that x has already been hashed.
*/
template <typename... Args>
MOZ_MUST_USE inline HashNumber HashGeneric(Args... aArgs) {
[[nodiscard]] inline HashNumber HashGeneric(Args... aArgs) {
return AddToHash(0, aArgs...);
}

Expand All @@ -248,7 +248,7 @@ MOZ_MUST_USE inline HashNumber HashGeneric(Args... aArgs) {
* marginally faster.
*/
template <typename Iterator>
MOZ_MUST_USE constexpr HashNumber HashStringUntilZero(Iterator aIter) {
[[nodiscard]] constexpr HashNumber HashStringUntilZero(Iterator aIter) {
HashNumber hash = 0;
for (; auto c = *aIter; ++aIter) {
hash = AddToHash(hash, c);
Expand All @@ -260,8 +260,8 @@ MOZ_MUST_USE constexpr HashNumber HashStringUntilZero(Iterator aIter) {
* Hash successive |aIter[i]| up to |i == aLength|.
*/
template <typename Iterator>
MOZ_MUST_USE constexpr HashNumber HashStringKnownLength(Iterator aIter,
size_t aLength) {
[[nodiscard]] constexpr HashNumber HashStringKnownLength(Iterator aIter,
size_t aLength) {
HashNumber hash = 0;
for (size_t i = 0; i < aLength; i++) {
hash = AddToHash(hash, aIter[i]);
Expand All @@ -275,30 +275,30 @@ MOZ_MUST_USE constexpr HashNumber HashStringKnownLength(Iterator aIter,
* These functions are non-template functions so that users can 1) overload them
* with their own types 2) in a way that allows implicit conversions to happen.
*/
MOZ_MUST_USE inline HashNumber HashString(const char* aStr) {
[[nodiscard]] inline HashNumber HashString(const char* aStr) {
// Use the |const unsigned char*| version of the above so that all ordinary
// character data hashes identically.
return HashStringUntilZero(reinterpret_cast<const unsigned char*>(aStr));
}

MOZ_MUST_USE inline HashNumber HashString(const char* aStr, size_t aLength) {
[[nodiscard]] inline HashNumber HashString(const char* aStr, size_t aLength) {
// Delegate to the |const unsigned char*| version of the above to share
// template instantiations.
return HashStringKnownLength(reinterpret_cast<const unsigned char*>(aStr),
aLength);
}

MOZ_MUST_USE
inline HashNumber HashString(const unsigned char* aStr, size_t aLength) {
[[nodiscard]] inline HashNumber HashString(const unsigned char* aStr,
size_t aLength) {
return HashStringKnownLength(aStr, aLength);
}

MOZ_MUST_USE constexpr HashNumber HashString(const char16_t* aStr) {
[[nodiscard]] constexpr HashNumber HashString(const char16_t* aStr) {
return HashStringUntilZero(aStr);
}

MOZ_MUST_USE inline HashNumber HashString(const char16_t* aStr,
size_t aLength) {
[[nodiscard]] inline HashNumber HashString(const char16_t* aStr,
size_t aLength) {
return HashStringKnownLength(aStr, aLength);
}

Expand All @@ -308,14 +308,14 @@ MOZ_MUST_USE inline HashNumber HashString(const char16_t* aStr,
template <typename WCharT, typename = typename std::enable_if<
std::is_same<WCharT, wchar_t>::value &&
!std::is_same<wchar_t, char16_t>::value>::type>
MOZ_MUST_USE inline HashNumber HashString(const WCharT* aStr) {
[[nodiscard]] inline HashNumber HashString(const WCharT* aStr) {
return HashStringUntilZero(aStr);
}

template <typename WCharT, typename = typename std::enable_if<
std::is_same<WCharT, wchar_t>::value &&
!std::is_same<wchar_t, char16_t>::value>::type>
MOZ_MUST_USE inline HashNumber HashString(const WCharT* aStr, size_t aLength) {
[[nodiscard]] inline HashNumber HashString(const WCharT* aStr, size_t aLength) {
return HashStringKnownLength(aStr, aLength);
}

Expand All @@ -325,8 +325,8 @@ MOZ_MUST_USE inline HashNumber HashString(const WCharT* aStr, size_t aLength) {
* This hash walks word-by-word, rather than byte-by-byte, so you won't get the
* same result out of HashBytes as you would out of HashString.
*/
MOZ_MUST_USE extern MFBT_API HashNumber HashBytes(const void* bytes,
size_t aLength);
[[nodiscard]] extern MFBT_API HashNumber HashBytes(const void* bytes,
size_t aLength);

/**
* A pseudorandom function mapping 32-bit integers to 32-bit integers.
Expand Down
Loading

0 comments on commit 7bc752e

Please sign in to comment.