Replace macros with C++17 features (see issue #3362)

- Convert ALLOW_UNUSED_LOCAL to [[maybe_unused]]
- Convert WARN_UNUSED_RESULT to [[nodiscard]]
This commit is contained in:
Marshall Greenblatt 2022-07-27 12:52:42 -04:00
parent 02d7a758fe
commit a9043f2e80
8 changed files with 16 additions and 47 deletions

View File

@ -52,7 +52,6 @@
#include "include/base/cef_bind.h" #include "include/base/cef_bind.h"
#include "include/base/cef_callback.h" #include "include/base/cef_callback.h"
#include "include/base/cef_compiler_specific.h"
#include "include/base/cef_logging.h" #include "include/base/cef_logging.h"
namespace base { namespace base {
@ -184,7 +183,7 @@ class ScopedClosureRunner {
void ReplaceClosure(OnceClosure closure); void ReplaceClosure(OnceClosure closure);
// Releases the Closure without calling. // Releases the Closure without calling.
OnceClosure Release() WARN_UNUSED_RESULT; [[nodiscard]] OnceClosure Release();
private: private:
OnceClosure closure_; OnceClosure closure_;

View File

@ -98,7 +98,6 @@
#include "include/base/cef_bind.h" #include "include/base/cef_bind.h"
#include "include/base/cef_callback.h" #include "include/base/cef_callback.h"
#include "include/base/cef_callback_helpers.h" #include "include/base/cef_callback_helpers.h"
#include "include/base/cef_compiler_specific.h"
#include "include/base/cef_logging.h" #include "include/base/cef_logging.h"
#include "include/base/cef_weak_ptr.h" #include "include/base/cef_weak_ptr.h"
@ -191,7 +190,7 @@ class CallbackListBase {
// Registers |cb| for future notifications. Returns a CallbackListSubscription // Registers |cb| for future notifications. Returns a CallbackListSubscription
// whose destruction will cancel |cb|. // whose destruction will cancel |cb|.
CallbackListSubscription Add(CallbackType cb) WARN_UNUSED_RESULT { [[nodiscard]] CallbackListSubscription Add(CallbackType cb) {
DCHECK(!cb.is_null()); DCHECK(!cb.is_null());
return CallbackListSubscription(base::BindOnce( return CallbackListSubscription(base::BindOnce(
&CallbackListBase::CancelCallback, weak_ptr_factory_.GetWeakPtr(), &CallbackListBase::CancelCallback, weak_ptr_factory_.GetWeakPtr(),

View File

@ -386,26 +386,4 @@ inline constexpr bool AnalyzerAssumeTrue(bool arg) {
#endif #endif
#endif // !USING_CHROMIUM_INCLUDES #endif // !USING_CHROMIUM_INCLUDES
// Annotate a function indicating the caller must examine the return value.
// Use like:
// int foo() WARN_UNUSED_RESULT;
// To explicitly ignore a result, use std::ignore from <tuple>.
// Alternately use `[[nodiscard]]` with code that supports C++17.
#undef WARN_UNUSED_RESULT
#if defined(COMPILER_GCC) || defined(__clang__)
#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
#else
#define WARN_UNUSED_RESULT
#endif
// Annotate a variable indicating it's ok if the variable is not used.
// (Typically used to silence a compiler warning when the assignment
// is important for some other reason.)
// Use like:
// int x = ...;
// ALLOW_UNUSED_LOCAL(x);
// Alternately use `[[maybe_unused]]` with code that supports C++17.
#define ALLOW_UNUSED_LOCAL(x) (void)x
#endif // CEF_INCLUDE_BASE_CEF_COMPILER_SPECIFIC_H_ #endif // CEF_INCLUDE_BASE_CEF_COMPILER_SPECIFIC_H_

View File

@ -46,7 +46,6 @@
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
#include "include/base/cef_compiler_specific.h"
#include "include/base/cef_logging.h" #include "include/base/cef_logging.h"
template <class T> template <class T>
@ -289,7 +288,7 @@ class TRIVIAL_ABI scoped_refptr {
// Returns the owned pointer (if any), releasing ownership to the caller. The // Returns the owned pointer (if any), releasing ownership to the caller. The
// caller is responsible for managing the lifetime of the reference. // caller is responsible for managing the lifetime of the reference.
T* release() WARN_UNUSED_RESULT; [[nodiscard]] T* release();
void swap(scoped_refptr& r) noexcept { std::swap(ptr_, r.ptr_); } void swap(scoped_refptr& r) noexcept { std::swap(ptr_, r.ptr_); }

View File

@ -74,16 +74,15 @@
// If the Chromium implementation diverges the below implementation should be // If the Chromium implementation diverges the below implementation should be
// updated to match. // updated to match.
#include "include/base/cef_compiler_specific.h"
#include "include/base/cef_logging.h" #include "include/base/cef_logging.h"
#include "include/base/internal/cef_scoped_policy.h" #include "include/base/internal/cef_scoped_policy.h"
namespace base { namespace base {
template<typename T> template <typename T>
struct ScopedTypeRefTraits; struct ScopedTypeRefTraits;
template<typename T, typename Traits = ScopedTypeRefTraits<T>> template <typename T, typename Traits = ScopedTypeRefTraits<T>>
class ScopedTypeRef { class ScopedTypeRef {
public: public:
using element_type = T; using element_type = T;
@ -96,8 +95,7 @@ class ScopedTypeRef {
object_ = Traits::Retain(object_); object_ = Traits::Retain(object_);
} }
ScopedTypeRef(const ScopedTypeRef<T, Traits>& that) ScopedTypeRef(const ScopedTypeRef<T, Traits>& that) : object_(that.object_) {
: object_(that.object_) {
if (object_) if (object_)
object_ = Traits::Retain(object_); object_ = Traits::Retain(object_);
} }
@ -127,7 +125,7 @@ class ScopedTypeRef {
// This is to be used only to take ownership of objects that are created // This is to be used only to take ownership of objects that are created
// by pass-by-pointer create functions. To enforce this, require that the // by pass-by-pointer create functions. To enforce this, require that the
// object be reset to NULL before this may be used. // object be reset to NULL before this may be used.
element_type* InitializeInto() WARN_UNUSED_RESULT { [[nodiscard]] element_type* InitializeInto() {
DCHECK(!object_); DCHECK(!object_);
return &object_; return &object_;
} }
@ -163,7 +161,7 @@ class ScopedTypeRef {
// ScopedTypeRef<>::release() is like std::unique_ptr<>::release. It is NOT // ScopedTypeRef<>::release() is like std::unique_ptr<>::release. It is NOT
// a wrapper for Release(). To force a ScopedTypeRef<> object to call // a wrapper for Release(). To force a ScopedTypeRef<> object to call
// Release(), use ScopedTypeRef<>::reset(). // Release(), use ScopedTypeRef<>::reset().
element_type release() WARN_UNUSED_RESULT { [[nodiscard]] element_type release() {
element_type temp = object_; element_type temp = object_;
object_ = Traits::InvalidValue(); object_ = Traits::InvalidValue();
return temp; return temp;

View File

@ -71,23 +71,23 @@ class CefScopedTempDir {
// Creates a unique directory in TempPath, and takes ownership of it. // Creates a unique directory in TempPath, and takes ownership of it.
// See file_util::CreateNewTemporaryDirectory. // See file_util::CreateNewTemporaryDirectory.
/// ///
bool CreateUniqueTempDir() WARN_UNUSED_RESULT; [[nodiscard]] bool CreateUniqueTempDir();
/// ///
// Creates a unique directory under a given path, and takes ownership of it. // Creates a unique directory under a given path, and takes ownership of it.
/// ///
bool CreateUniqueTempDirUnderPath(const CefString& path) WARN_UNUSED_RESULT; [[nodiscard]] bool CreateUniqueTempDirUnderPath(const CefString& path);
/// ///
// Takes ownership of directory at |path|, creating it if necessary. // Takes ownership of directory at |path|, creating it if necessary.
// Don't call multiple times unless Take() has been called first. // Don't call multiple times unless Take() has been called first.
/// ///
bool Set(const CefString& path) WARN_UNUSED_RESULT; [[nodiscard]] bool Set(const CefString& path);
/// ///
// Deletes the temporary directory wrapped by this object. // Deletes the temporary directory wrapped by this object.
/// ///
bool Delete() WARN_UNUSED_RESULT; [[nodiscard]] bool Delete();
/// ///
// Caller takes ownership of the temporary directory so it won't be destroyed // Caller takes ownership of the temporary directory so it won't be destroyed

View File

@ -18,8 +18,7 @@ class ScopedGLContext {
public: public:
ScopedGLContext(HDC hdc, HGLRC hglrc, bool swap_buffers) ScopedGLContext(HDC hdc, HGLRC hglrc, bool swap_buffers)
: hdc_(hdc), swap_buffers_(swap_buffers) { : hdc_(hdc), swap_buffers_(swap_buffers) {
BOOL result = wglMakeCurrent(hdc, hglrc); [[maybe_unused]] BOOL result = wglMakeCurrent(hdc, hglrc);
ALLOW_UNUSED_LOCAL(result);
DCHECK(result); DCHECK(result);
} }
~ScopedGLContext() { ~ScopedGLContext() {
@ -188,8 +187,7 @@ void OsrRenderHandlerWinGL::DisableGL() {
if (IsWindow(hwnd())) { if (IsWindow(hwnd())) {
// wglDeleteContext will make the context not current before deleting it. // wglDeleteContext will make the context not current before deleting it.
BOOL result = wglDeleteContext(hrc_); [[maybe_unused]] BOOL result = wglDeleteContext(hrc_);
ALLOW_UNUSED_LOCAL(result);
DCHECK(result); DCHECK(result);
ReleaseDC(hwnd(), hdc_); ReleaseDC(hwnd(), hdc_);
} }

View File

@ -722,8 +722,7 @@ void RootWindowWin::OnSize(bool minimized) {
SWP_NOZORDER); SWP_NOZORDER);
} }
BOOL result = EndDeferWindowPos(hdwp); [[maybe_unused]] BOOL result = EndDeferWindowPos(hdwp);
ALLOW_UNUSED_LOCAL(result);
DCHECK(result); DCHECK(result);
} else if (browser_window_) { } else if (browser_window_) {
// Size the browser window to the whole client area. // Size the browser window to the whole client area.
@ -1164,9 +1163,8 @@ void UnSubclassWindow(HWND hWnd) {
LONG_PTR hParentWndProc = LONG_PTR hParentWndProc =
reinterpret_cast<LONG_PTR>(::GetPropW(hWnd, kParentWndProc)); reinterpret_cast<LONG_PTR>(::GetPropW(hWnd, kParentWndProc));
if (hParentWndProc) { if (hParentWndProc) {
LONG_PTR hPreviousWndProc = [[maybe_unused]] LONG_PTR hPreviousWndProc =
SetWindowLongPtr(hWnd, GWLP_WNDPROC, hParentWndProc); SetWindowLongPtr(hWnd, GWLP_WNDPROC, hParentWndProc);
ALLOW_UNUSED_LOCAL(hPreviousWndProc);
DCHECK_EQ(hPreviousWndProc, DCHECK_EQ(hPreviousWndProc,
reinterpret_cast<LONG_PTR>(SubclassedWindowProc)); reinterpret_cast<LONG_PTR>(SubclassedWindowProc));
} }