Use C++17 [[nodiscard]] and [[fallthrough]] attributes

Bug: dawn:824
Change-Id: Ied4f2eb736e0c3488a79e4872e7ffa3eb2fdaac5
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/75063
Reviewed-by: Austin Eng <enga@chromium.org>
Reviewed-by: Loko Kung <lokokung@google.com>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Corentin Wallez 2022-01-06 09:02:38 +00:00 committed by Dawn LUCI CQ
parent 3f011e6807
commit 2994d2e7b9
10 changed files with 19 additions and 45 deletions

View File

@ -530,7 +530,7 @@
return result;
}
DAWN_NO_DISCARD WireResult SerializeChainedStruct({{ChainedStructPtr}} chainedStruct,
[[nodiscard]] WireResult SerializeChainedStruct({{ChainedStructPtr}} chainedStruct,
SerializeBuffer* buffer,
const ObjectIdProvider& provider) {
ASSERT(chainedStruct != nullptr);
@ -702,7 +702,7 @@ namespace dawn_wire {
};
size_t GetChainedStructExtraRequiredSize(const WGPUChainedStruct* chainedStruct);
DAWN_NO_DISCARD WireResult SerializeChainedStruct(const WGPUChainedStruct* chainedStruct,
[[nodiscard]] WireResult SerializeChainedStruct(const WGPUChainedStruct* chainedStruct,
SerializeBuffer* buffer,
const ObjectIdProvider& provider);
WireResult DeserializeChainedStruct(const WGPUChainedStruct** outChainNext,
@ -711,7 +711,7 @@ namespace dawn_wire {
const ObjectIdResolver& resolver);
size_t GetChainedStructExtraRequiredSize(WGPUChainedStructOut* chainedStruct);
DAWN_NO_DISCARD WireResult SerializeChainedStruct(WGPUChainedStructOut* chainedStruct,
[[nodiscard]] WireResult SerializeChainedStruct(WGPUChainedStructOut* chainedStruct,
SerializeBuffer* buffer,
const ObjectIdProvider& provider);
WireResult DeserializeChainedStruct(WGPUChainedStructOut** outChainNext,

View File

@ -19,7 +19,6 @@
// - DAWN_COMPILER_[CLANG|GCC|MSVC]: Compiler detection
// - DAWN_BREAKPOINT(): Raises an exception and breaks in the debugger
// - DAWN_BUILTIN_UNREACHABLE(): Hints the compiler that a code path is unreachable
// - DAWN_NO_DISCARD: An attribute that is C++17 [[nodiscard]] where available
// - DAWN_(UN)?LIKELY(EXPR): Where available, hints the compiler that the expression will be true
// (resp. false) to help it generate code that leads to better branch prediction.
// - DAWN_UNUSED(EXPR): Prevents unused variable/expression warnings on EXPR.
@ -51,15 +50,6 @@
# define __has_cpp_attribute(name) 0
# endif
// Use warn_unused_result on clang otherwise we can a c++1z extension warning in C++14 mode
// Also avoid warn_unused_result with GCC because it is only a function attribute and not a type
// attribute.
# if __has_cpp_attribute(warn_unused_result) && defined(__clang__)
# define DAWN_NO_DISCARD __attribute__((warn_unused_result))
# elif DAWN_CPP_VERSION >= 17 && __has_cpp_attribute(nodiscard)
# define DAWN_NO_DISCARD [[nodiscard]]
# endif
# define DAWN_DECLARE_UNUSED __attribute__((unused))
# if defined(NDEBUG)
# define DAWN_FORCE_INLINE inline __attribute__((always_inline))
@ -75,11 +65,6 @@ extern void __cdecl __debugbreak(void);
# define DAWN_BUILTIN_UNREACHABLE() __assume(false)
// Visual Studio 2017 15.3 adds support for [[nodiscard]]
# if _MSC_VER >= 1911 && DAWN_CPP_VERSION >= 17
# define DAWN_NO_DISCARD [[nodiscard]]
# endif
# define DAWN_DECLARE_UNUSED
# if defined(NDEBUG)
# define DAWN_FORCE_INLINE __forceinline
@ -102,9 +87,6 @@ extern void __cdecl __debugbreak(void);
#if !defined(DAWN_UNLIKELY)
# define DAWN_UNLIKELY(X) X
#endif
#if !defined(DAWN_NO_DISCARD)
# define DAWN_NO_DISCARD
#endif
#if !defined(DAWN_FORCE_INLINE)
# define DAWN_FORCE_INLINE inline
#endif
@ -112,10 +94,4 @@ extern void __cdecl __debugbreak(void);
# define DAWN_NOINLINE
#endif
#if defined(__clang__)
# define DAWN_FALLTHROUGH [[clang::fallthrough]]
#else
# define DAWN_FALLTHROUGH
#endif
#endif // COMMON_COMPILER_H_

View File

@ -134,7 +134,7 @@ class RefBase {
return mValue;
}
T Detach() DAWN_NO_DISCARD {
[[nodiscard]] T Detach() {
T value{std::move(mValue)};
mValue = Traits::kNullValue;
return value;
@ -145,7 +145,7 @@ class RefBase {
mValue = value;
}
T* InitializeInto() DAWN_NO_DISCARD {
[[nodiscard]] T* InitializeInto() {
ASSERT(mValue == Traits::kNullValue);
return &mValue;
}

View File

@ -58,7 +58,7 @@ class Result;
// Specialization of Result for returning errors only via pointers. It is basically a pointer
// where nullptr is both Success and Empty.
template <typename E>
class DAWN_NO_DISCARD Result<void, E> {
class [[nodiscard]] Result<void, E> {
public:
Result();
Result(std::unique_ptr<E> error);
@ -109,7 +109,7 @@ namespace detail {
} // namespace detail
template <typename T, typename E>
class DAWN_NO_DISCARD Result<T*, E> {
class [[nodiscard]] Result<T*, E> {
public:
static_assert(alignof_if_defined_else_default<T, 4> >= 4,
"Result<T*, E*> reserves two bits for tagging pointers");
@ -141,7 +141,7 @@ class DAWN_NO_DISCARD Result<T*, E> {
};
template <typename T, typename E>
class DAWN_NO_DISCARD Result<const T*, E> {
class [[nodiscard]] Result<const T*, E> {
public:
static_assert(alignof_if_defined_else_default<T, 4> >= 4,
"Result<T*, E*> reserves two bits for tagging pointers");
@ -170,7 +170,7 @@ template <typename T>
class Ref;
template <typename T, typename E>
class DAWN_NO_DISCARD Result<Ref<T>, E> {
class [[nodiscard]] Result<Ref<T>, E> {
public:
static_assert(alignof_if_defined_else_default<T, 4> >= 4,
"Result<Ref<T>, E> reserves two bits for tagging pointers");
@ -205,7 +205,7 @@ class DAWN_NO_DISCARD Result<Ref<T>, E> {
// a tagged union instead if it turns out to be a hotspot. T and E must be movable and default
// constructible.
template <typename T, typename E>
class DAWN_NO_DISCARD Result {
class [[nodiscard]] Result {
public:
Result(T&& success);
Result(std::unique_ptr<E> error);

View File

@ -217,7 +217,7 @@ namespace dawn_native {
"Filtering sampler %s is incompatible with non-filtering sampler "
"binding.",
entry.sampler);
DAWN_FALLTHROUGH;
[[fallthrough]];
case wgpu::SamplerBindingType::Filtering:
DAWN_INVALID_IF(
entry.sampler->IsComparison(),

View File

@ -33,12 +33,10 @@ namespace dawn {
namespace dawn_native {
enum class InternalErrorType : uint32_t;
class DAWN_NO_DISCARD ErrorData {
class [[nodiscard]] ErrorData {
public:
static DAWN_NO_DISCARD std::unique_ptr<ErrorData> Create(InternalErrorType type,
std::string message,
const char* file,
const char* function,
[[nodiscard]] static std::unique_ptr<ErrorData> Create(
InternalErrorType type, std::string message, const char* file, const char* function,
int line);
ErrorData(InternalErrorType type, std::string message);

View File

@ -1275,7 +1275,7 @@ namespace dawn_native { namespace d3d12 {
switch (descriptor->dimension) {
case wgpu::TextureViewDimension::e2DArray:
ASSERT(texture->GetArrayLayers() == 1);
DAWN_FALLTHROUGH;
[[fallthrough]];
case wgpu::TextureViewDimension::e2D:
ASSERT(texture->GetDimension() == wgpu::TextureDimension::e2D);
mSrvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DMS;

View File

@ -756,7 +756,7 @@ namespace dawn_native { namespace opengl {
break;
}
// Implementation for 2D array is the same as 3D.
DAWN_FALLTHROUGH;
[[fallthrough]];
}
case wgpu::TextureDimension::e3D: {

View File

@ -19,7 +19,7 @@
namespace dawn_wire {
enum class DAWN_NO_DISCARD WireResult {
enum class [[nodiscard]] WireResult{
Success,
FatalError,
};

View File

@ -41,7 +41,7 @@ namespace utils {
* // do rendering ...
* }
*/
class DAWN_NO_DISCARD ScopedAutoreleasePool {
class [[nodiscard]] ScopedAutoreleasePool {
public:
ScopedAutoreleasePool();
~ScopedAutoreleasePool();