tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit 1f1fbc3a57847b4ffb5c65fa1ab459919eaab5b9
parent d25533a3139886897113894cb89dbf40ad544ef4
Author: serge-sans-paille <sguelton@mozilla.com>
Date:   Fri,  3 Oct 2025 06:06:35 +0000

Bug 1991857 - Modernize mfbt/DbgMacro.h r=emilio

Using `if constexpr` make usage of `std::enable_if` obsolete.

Differential Revision: https://phabricator.services.mozilla.com/D267236

Diffstat:
Mmfbt/DbgMacro.h | 72+++++++++++++++++++++++++++---------------------------------------------
Mstorage/ReadOnlyNoLockVFS.cpp | 2++
Mxpcom/base/nsDebug.h | 1-
3 files changed, 29 insertions(+), 46 deletions(-)

diff --git a/mfbt/DbgMacro.h b/mfbt/DbgMacro.h @@ -12,7 +12,7 @@ #include "mozilla/MacroForEach.h" #include "mozilla/Span.h" -#include <stdio.h> +#include <cstdio> #include <sstream> template <typename T> @@ -27,63 +27,46 @@ namespace mozilla { namespace detail { // Predicate to check whether T can be inserted into an ostream. -template <typename T, typename = decltype(std::declval<std::ostream&>() - << std::declval<T>())> -std::true_type supports_os_test(const T&); -std::false_type supports_os_test(...); +template <typename T, typename = void> +struct supports_os : std::false_type {}; template <typename T> -using supports_os = decltype(supports_os_test(std::declval<T>())); +struct supports_os<T, std::void_t<decltype(std::declval<std::ostream&>() + << std::declval<T&>())>> + : std::true_type {}; } // namespace detail // Helper function to write a value to an ostream. // -// This handles pointer values where the type being pointed to supports being -// inserted into an ostream, and we write out the value being pointed to in -// addition to the pointer value. +// This handles both pointer values where the type being pointed to supports +// being inserted into an ostream (in which case we write out the value being +// pointed to in addition to the pointer value), and pointer types that cannot +// be dereferenced (in which cases we just write the pointer value). template <typename T> -auto DebugValue(std::ostream& aOut, T* aValue) - -> std::enable_if_t<mozilla::detail::supports_os<T>::value, std::ostream&> { - if (aValue) { - aOut << *aValue << " @ " << aValue; +std::ostream& DebugValue(std::ostream& aOut, T* aValue) { + if constexpr (detail::supports_os<T>::value) { + if (aValue) { + return aOut << *aValue << " @ " << aValue; + } else { + return aOut << "null"; + } } else { - aOut << "null"; + return aOut << aValue; } - return aOut; } // Helper function to write a value to an ostream. // -// This handles all pointer types that cannot be dereferenced and inserted into -// an ostream. +// This handle all non-pointer types, with a specialization for XPCOM types. template <typename T> -auto DebugValue(std::ostream& aOut, T* aValue) - -> std::enable_if_t<!mozilla::detail::supports_os<T>::value, - std::ostream&> { - return aOut << aValue; -} - -// Helper function to write a value to an ostream. -// -// This handles XPCOM string types. -template <typename T> -auto DebugValue(std::ostream& aOut, const T& aValue) - -> std::enable_if_t<std::is_base_of<nsTSubstring<char>, T>::value || - std::is_base_of<nsTSubstring<char16_t>, T>::value, - std::ostream&> { - return aOut << '"' << aValue << '"'; -} - -// Helper function to write a value to an ostream. -// -// This handles all other types. -template <typename T> -auto DebugValue(std::ostream& aOut, const T& aValue) - -> std::enable_if_t<!std::is_base_of<nsTSubstring<char>, T>::value && - !std::is_base_of<nsTSubstring<char16_t>, T>::value, - std::ostream&> { - return aOut << aValue; +std::ostream& DebugValue(std::ostream& aOut, const T& aValue) { + if constexpr (std::is_base_of<nsTSubstring<char>, T>::value || + std::is_base_of<nsTSubstring<char16_t>, T>::value) { + return aOut << '"' << aValue << '"'; + } else { + return aOut << aValue; + } } namespace detail { @@ -94,8 +77,7 @@ auto&& MozDbg(const char* aFile, int aLine, const char* aExpression, T&& aValue) { std::ostringstream s; s << "[MozDbg] [" << aFile << ':' << aLine << "] " << aExpression << " = "; - mozilla::DebugValue(s, std::forward<T>(aValue)); - s << '\n'; + mozilla::DebugValue(s, std::forward<T>(aValue)) << '\n'; #ifdef ANDROID __android_log_print(ANDROID_LOG_INFO, "Gecko", "%s", s.str().c_str()); #else diff --git a/storage/ReadOnlyNoLockVFS.cpp b/storage/ReadOnlyNoLockVFS.cpp @@ -18,6 +18,8 @@ #include "nsDebug.h" #include "sqlite3.h" +#include "mozilla/UniquePtr.h" + #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData)) #if defined(XP_WIN) diff --git a/xpcom/base/nsDebug.h b/xpcom/base/nsDebug.h @@ -13,7 +13,6 @@ #include "nsXPCOM.h" #include "mozilla/Assertions.h" #include "mozilla/glue/Debug.h" -#include "mozilla/DbgMacro.h" #include "mozilla/Likely.h" #include <stdarg.h>