diff --git a/mfbt/Array.h b/mfbt/Array.h index 1dd777a09965..5ae502b12aff 100644 --- a/mfbt/Array.h +++ b/mfbt/Array.h @@ -14,7 +14,6 @@ #include "mozilla/Move.h" #include "mozilla/ReverseIterator.h" -#include #include namespace mozilla { @@ -89,13 +88,6 @@ class Array { } }; -// MOZ_DBG support - -template -std::ostream& operator<<(std::ostream& aOut, const Array& aArray) { - return aOut << MakeSpan(aArray); -} - } /* namespace mozilla */ #endif /* mozilla_Array_h */ diff --git a/mfbt/DbgMacro.h b/mfbt/DbgMacro.h deleted file mode 100644 index 770d1130786c..000000000000 --- a/mfbt/DbgMacro.h +++ /dev/null @@ -1,177 +0,0 @@ -/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set ts=8 sts=2 et sw=2 tw=80: */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#ifndef mozilla_DbgMacro_h -#define mozilla_DbgMacro_h - -/* a MOZ_DBG macro that outputs a wrapped value to stderr then returns it */ - -#include "mozilla/MacroForEach.h" -#include "mozilla/Span.h" - -#include -#include - -namespace mozilla { - -namespace detail { - -// Predicate to check whether T can be dereferenced and then inserted into an -// ostream. -template () - << *std::declval())> -std::true_type supports_os_deref_test(const T&); -std::false_type supports_os_deref_test(...); - -template -using supports_os_deref = decltype(supports_os_deref_test(std::declval())); - -} // namespace detail - -// Helper function to write a value to an ostream. -// -// This handles pointer values where the type being pointed to supports -// operator<<, and we write out the value being pointed to in addition to the -// pointer value. -template -auto DebugValue(std::ostream& aOut, T&& aValue) -> std::enable_if_t< - std::is_pointer::type>::value && - mozilla::detail::supports_os_deref::value, - std::ostream&> { - if (aValue) { - aOut << *aValue << " @ " << aValue; - } else { - aOut << "null"; - } - return aOut; -} - -// Helper function to write a value to an ostream. -// -// This handles all other types by calling into operator<<. -template -auto DebugValue(std::ostream& aOut, T&& aValue) -> std::enable_if_t< - !(std::is_pointer::type>::value && - mozilla::detail::supports_os_deref::value), - std::ostream&> { - return aOut << aValue; -} - -namespace detail { - -// Helper function template for MOZ_DBG. -template -auto&& MozDbg(const char* aFile, int aLine, const char* aExpression, - T&& aValue) { - std::ostringstream s; - s << '[' << aFile << ':' << aLine << "] " << aExpression << " = "; - mozilla::DebugValue(s, std::forward(aValue)); - s << '\n'; - fputs(s.str().c_str(), stderr); - return std::forward(aValue); -} - -} // namespace detail - -} // namespace mozilla - -template -std::ostream& operator<<(std::ostream& aOut, - const mozilla::Span& aSpan) { - aOut << '['; - if (!aSpan.IsEmpty()) { - aOut << aSpan[0]; - for (size_t i = 1; i < aSpan.Length(); ++i) { - aOut << ", " << aSpan[i]; - } - } - return aOut << ']'; -} - -// Don't define this for char[], since operator<<(ostream&, char*) is already -// defined. -template ::value>> -std::ostream& operator<<(std::ostream& aOut, const T (&aArray)[N]) { - return aOut << mozilla::MakeSpan(aArray); - // return aOut << mozilla::Span(aArray); -} - -// MOZ_DBG is a macro like the Rust dbg!() macro -- it will print out the -// expression passed to it to stderr and then return the value. It is available -// only in MOZILLA_OFFICIAL builds, so you shouldn't land any uses of it in the -// tree. -// -// It should work for any type T that has an operator<<(std::ostream&, const T&) -// defined for it. -// -// Note 1: Using MOZ_DBG may cause copies to be made of temporary values: -// -// struct A { -// A(int); -// A(const A&); -// -// int x; -// }; -// -// void f(A); -// -// f(A{1}); // may (and, in C++17, will) elide the creation of a temporary -// // for A{1} and instead initialize the function argument -// // directly using the A(int) constructor -// -// f(MOZ_DBG(A{1})); // will create and return a temporary for A{1}, which -// // then will be passed to the A(const A&) copy -// // constructor to initialize f's argument -// -// Note 2: MOZ_DBG cannot be used to wrap a prvalue that is being used to -// initialize an object if its type has no move constructor: -// -// struct B { -// B() = default; -// B(B&&) = delete; -// }; -// -// B b1 = B(); // fine, initializes b1 directly -// -// B b2 = MOZ_DBG(B()); // compile error: MOZ_DBG needs to materialize a -// // temporary for B() so it can be passed to -// // operator<<, but that temporary is returned from -// // MOZ_DBG as an rvalue reference and so wants to -// // invoke B's move constructor to initialize b2 -#ifndef MOZILLA_OFFICIAL -# define MOZ_DBG(expression_...) \ - mozilla::detail::MozDbg(__FILE__, __LINE__, #expression_, expression_) -#endif - -// Helper macro for MOZ_DEFINE_DBG. -#define MOZ_DBG_FIELD(name_) << #name_ << " = " << aValue.name_ - -// Macro to define an operator<<(ostream&) for a struct or class that displays -// the type name and the values of the specified member variables. Must be -// called inside the struct or class. -// -// For example: -// -// struct Point { -// float x; -// float y; -// -// MOZ_DEFINE_DBG(Point, x, y) -// }; -// -// generates an operator<< that outputs strings like -// "Point { x = 1.0, y = 2.0 }". -#define MOZ_DEFINE_DBG(type_, members_...) \ - friend std::ostream& operator<<(std::ostream& aOut, const type_& aValue) { \ - return aOut << #type_ \ - << (MOZ_ARG_COUNT(members_) == 0 ? "{ " : "") \ - MOZ_FOR_EACH_SEPARATED(MOZ_DBG_FIELD, (<< ", "), (), \ - (members_)) \ - << (MOZ_ARG_COUNT(members_) == 0 ? " }" : ""); \ - } - -#endif // mozilla_DbgMacro_h diff --git a/mfbt/RefPtr.h b/mfbt/RefPtr.h index 1736d99f9143..144f21ffd818 100644 --- a/mfbt/RefPtr.h +++ b/mfbt/RefPtr.h @@ -10,7 +10,6 @@ #include "mozilla/AlreadyAddRefed.h" #include "mozilla/Assertions.h" #include "mozilla/Attributes.h" -#include "mozilla/DbgMacro.h" /*****************************************************************************/ @@ -524,13 +523,6 @@ inline bool operator!=(decltype(nullptr), const RefPtr& aRhs) { return nullptr != aRhs.get(); } -// MOZ_DBG support - -template -std::ostream& operator<<(std::ostream& aOut, const RefPtr& aObj) { - return mozilla::DebugValue(aOut, aObj.get()); -} - /*****************************************************************************/ template diff --git a/mfbt/moz.build b/mfbt/moz.build index fbf46864de9c..197048ef901a 100644 --- a/mfbt/moz.build +++ b/mfbt/moz.build @@ -29,7 +29,6 @@ EXPORTS.mozilla = [ 'CheckedInt.h', 'Compiler.h', 'Compression.h', - 'DbgMacro.h', 'DebugOnly.h', 'decimal/Decimal.h', 'DefineEnum.h', diff --git a/mfbt/tests/gtest/TestMozDbg.cpp b/mfbt/tests/gtest/TestMozDbg.cpp deleted file mode 100644 index ddfcbbf74f86..000000000000 --- a/mfbt/tests/gtest/TestMozDbg.cpp +++ /dev/null @@ -1,174 +0,0 @@ -/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set ts=8 sts=2 et sw=2 tw=80: */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include - -#include "gtest/gtest.h" -#include "mozilla/DbgMacro.h" -#include "mozilla/Unused.h" - -using namespace mozilla; - -#define TEST_MOZ_DBG_TYPE_IS(type_, expression_...) \ - static_assert(IsSame::value, \ - "MOZ_DBG should return the indicated type") - -#define TEST_MOZ_DBG_TYPE_SAME(expression_...) \ - static_assert( \ - IsSame::value, \ - "MOZ_DBG should return the same type") - -struct Number { - explicit Number(int aValue) : mValue(aValue) {} - - Number(const Number& aOther) : mValue(aOther.mValue) {} - - Number(Number&& aOther) : mValue(aOther.mValue) { aOther.mValue = 0; } - - Number& operator=(const Number& aOther) { - mValue = aOther.mValue; - return *this; - } - - Number& operator=(Number&& aOther) { - mValue = aOther.mValue; - aOther.mValue = 0; - return *this; - } - - ~Number() { mValue = -999; } - - int mValue; - - MOZ_DEFINE_DBG(Number, mValue) -}; - -struct MoveOnly { - explicit MoveOnly(int aValue) : mValue(aValue) {} - - MoveOnly(const MoveOnly& aOther) = delete; - - MoveOnly(MoveOnly&& aOther) : mValue(aOther.mValue) { aOther.mValue = 0; } - - MoveOnly& operator=(MoveOnly& aOther) { - mValue = aOther.mValue; - return *this; - } - - MoveOnly& operator=(MoveOnly&& aOther) { - mValue = aOther.mValue; - aOther.mValue = 0; - return *this; - } - - int mValue; - - MOZ_DEFINE_DBG(MoveOnly) -}; - -void StaticAssertions() { - int x = 123; - Number y(123); - Number z(234); - MoveOnly w(456); - - // Static assertions. - - // lvalues - TEST_MOZ_DBG_TYPE_SAME(x); // int& - TEST_MOZ_DBG_TYPE_SAME(y); // Number& - TEST_MOZ_DBG_TYPE_SAME(x = 234); // int& - TEST_MOZ_DBG_TYPE_SAME(y = z); // Number& - TEST_MOZ_DBG_TYPE_SAME(w); // MoveOnly& - - // prvalues (which MOZ_DBG turns into xvalues by creating objects for them) - TEST_MOZ_DBG_TYPE_IS(int&&, 123); - TEST_MOZ_DBG_TYPE_IS(int&&, 1 + 2); - TEST_MOZ_DBG_TYPE_IS(int*&&, &x); - TEST_MOZ_DBG_TYPE_IS(int&&, x++); - TEST_MOZ_DBG_TYPE_IS(Number&&, Number(123)); - TEST_MOZ_DBG_TYPE_IS(MoveOnly&&, MoveOnly(123)); - - // xvalues - TEST_MOZ_DBG_TYPE_SAME(std::move(y)); // int&& - TEST_MOZ_DBG_TYPE_SAME(std::move(y)); // Number&& - TEST_MOZ_DBG_TYPE_SAME(std::move(w)); // MoveOnly& - - Unused << x; - Unused << y; - Unused << z; -} - -TEST(MozDbg, ObjectValues) { - // Test that moves and assignments all operate correctly with MOZ_DBG wrapped - // around various parts of the expression. - - Number a(1); - Number b(4); - - ASSERT_EQ(a.mValue, 1); - - MOZ_DBG(a.mValue); - ASSERT_EQ(a.mValue, 1); - - MOZ_DBG(a.mValue + 1); - ASSERT_EQ(a.mValue, 1); - - MOZ_DBG(a.mValue = 2); - ASSERT_EQ(a.mValue, 2); - - MOZ_DBG(a).mValue = 3; - ASSERT_EQ(a.mValue, 3); - - MOZ_DBG(a = b); - ASSERT_EQ(a.mValue, 4); - ASSERT_EQ(b.mValue, 4); - - b.mValue = 5; - MOZ_DBG(a) = b; - ASSERT_EQ(a.mValue, 5); - ASSERT_EQ(b.mValue, 5); - - b.mValue = 6; - MOZ_DBG(a = std::move(b)); - ASSERT_EQ(a.mValue, 6); - ASSERT_EQ(b.mValue, 0); - - b.mValue = 7; - MOZ_DBG(a) = std::move(b); - ASSERT_EQ(a.mValue, 7); - ASSERT_EQ(b.mValue, 0); - - b.mValue = 8; - a = std::move(MOZ_DBG(b)); - ASSERT_EQ(a.mValue, 8); - ASSERT_EQ(b.mValue, 0); - - a = MOZ_DBG(Number(9)); - ASSERT_EQ(a.mValue, 9); - - MoveOnly c(1); - MoveOnly d(2); - - c = std::move(MOZ_DBG(d)); - ASSERT_EQ(c.mValue, 2); - ASSERT_EQ(d.mValue, 0); - - c.mValue = 3; - d.mValue = 4; - c = MOZ_DBG(std::move(d)); - ASSERT_EQ(c.mValue, 4); - ASSERT_EQ(d.mValue, 0); - - c.mValue = 5; - d.mValue = 6; - MOZ_DBG(c = std::move(d)); - ASSERT_EQ(c.mValue, 6); - ASSERT_EQ(d.mValue, 0); - - c = MOZ_DBG(MoveOnly(7)); - ASSERT_EQ(c.mValue, 7); -} diff --git a/mfbt/tests/gtest/moz.build b/mfbt/tests/gtest/moz.build index f54b6fbb80b5..29fe24c29156 100644 --- a/mfbt/tests/gtest/moz.build +++ b/mfbt/tests/gtest/moz.build @@ -10,12 +10,6 @@ UNIFIED_SOURCES += [ 'TestSpan.cpp', ] -if not CONFIG['MOZILLA_OFFICIAL']: - UNIFIED_SOURCES += [ - # MOZ_DBG is not defined in MOZILLA_OFFICIAL builds. - 'TestMozDbg.cpp', - ] - #LOCAL_INCLUDES += [ # '../../base', #] diff --git a/xpcom/base/nsCOMPtr.h b/xpcom/base/nsCOMPtr.h index c4bb85357924..1281df9f2bac 100644 --- a/xpcom/base/nsCOMPtr.h +++ b/xpcom/base/nsCOMPtr.h @@ -1476,11 +1476,4 @@ inline already_AddRefed do_AddRef(const nsCOMPtr& aObj) { return ref.forget(); } -// MOZ_DBG support - -template -std::ostream& operator<<(std::ostream& aOut, const nsCOMPtr& aObj) { - return aOut << aObj.get(); -} - #endif // !defined(nsCOMPtr_h___) diff --git a/xpcom/base/nsDebug.h b/xpcom/base/nsDebug.h index 1195088dc74b..b8a2096a163b 100644 --- a/xpcom/base/nsDebug.h +++ b/xpcom/base/nsDebug.h @@ -12,7 +12,6 @@ #include "nsXPCOM.h" #include "mozilla/Assertions.h" -#include "mozilla/DbgMacro.h" #include "mozilla/Likely.h" #include diff --git a/xpcom/ds/nsTArray.h b/xpcom/ds/nsTArray.h index b7d371a4d18c..f58c2b4a154e 100644 --- a/xpcom/ds/nsTArray.h +++ b/xpcom/ds/nsTArray.h @@ -14,7 +14,6 @@ #include "mozilla/Attributes.h" #include "mozilla/BinarySearch.h" #include "mozilla/CheckedInt.h" -#include "mozilla/DbgMacro.h" #include "mozilla/fallible.h" #include "mozilla/FunctionTypeTraits.h" #include "mozilla/MathAlgorithms.h" @@ -37,7 +36,6 @@ #include #include #include -#include namespace JS { template @@ -2649,14 +2647,6 @@ Span MakeSpan( } // namespace mozilla -// MOZ_DBG support - -template -std::ostream& operator<<(std::ostream& aOut, - const nsTArray_Impl& aTArray) { - return aOut << mozilla::MakeSpan(aTArray); -} - // Assert that AutoTArray doesn't have any extra padding inside. // // It's important that the data stored in this auto array takes up a multiple of diff --git a/xpcom/string/nsString.h b/xpcom/string/nsString.h index c70d0f985052..a97ac0fe8f86 100644 --- a/xpcom/string/nsString.h +++ b/xpcom/string/nsString.h @@ -7,8 +7,6 @@ #ifndef nsString_h___ #define nsString_h___ -#include - #include "mozilla/Attributes.h" #include "nsStringFwd.h" @@ -124,18 +122,6 @@ class NS_ConvertUTF8toUTF16 : public nsAutoString { NS_ConvertUTF8toUTF16(char16_t) = delete; }; -// MOZ_DBG support - -inline std::ostream& operator<<(std::ostream& aOut, const nsACString& aString) { - aOut << '"'; - aOut.write(aString.Data(), aString.Length()); - return aOut << '"'; -} - -inline std::ostream& operator<<(std::ostream& aOut, const nsAString& aString) { - return aOut << NS_ConvertUTF16toUTF8(aString); -} - // the following are included/declared for backwards compatibility #include "nsDependentString.h" #include "nsLiteralString.h"