From 16d4f8eff6d7cefca6975d82a53f8fc995a6feb7 Mon Sep 17 00:00:00 2001 From: Derek Mauro Date: Tue, 20 May 2025 10:25:04 -0700 Subject: [PATCH] Delete the absl polyfill support for std::any, std::optional and std::variant now that the absl types are aliases of the std types PiperOrigin-RevId: 761136061 Change-Id: I702c3e1e8c58d003b8f4da99e7c84c9e5dbe1863 --- BUILD.bazel | 7 +- googletest/CMakeLists.txt | 3 - googletest/include/gtest/gtest-printers.h | 48 +++----- .../include/gtest/internal/gtest-port.h | 112 ------------------ googletest/test/googletest-printers-test.cc | 23 ++-- 5 files changed, 27 insertions(+), 166 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index 53501454c..cc2544578 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -151,10 +151,7 @@ cc_library( "@abseil-cpp//absl/flags:reflection", "@abseil-cpp//absl/flags:usage", "@abseil-cpp//absl/strings", - "@abseil-cpp//absl/types:any", - "@abseil-cpp//absl/types:optional", - "@abseil-cpp//absl/types:variant", - "@re2//:re2", + "@re2", ], "//conditions:default": [], }) + select({ @@ -174,8 +171,8 @@ cc_library( # `gtest`, but testonly. See guidance on `gtest` for when to use this. alias( name = "gtest_for_library", - actual = ":gtest", testonly = True, + actual = ":gtest", ) # Implements main() for tests using gtest. Prefer to depend on `gtest` as well diff --git a/googletest/CMakeLists.txt b/googletest/CMakeLists.txt index dce6a7c9e..2e5388945 100644 --- a/googletest/CMakeLists.txt +++ b/googletest/CMakeLists.txt @@ -132,9 +132,6 @@ if(GTEST_HAS_ABSL) absl::flags_reflection absl::flags_usage absl::strings - absl::any - absl::optional - absl::variant re2::re2 ) endif() diff --git a/googletest/include/gtest/gtest-printers.h b/googletest/include/gtest/gtest-printers.h index bbb0fa7e7..cb73ef257 100644 --- a/googletest/include/gtest/gtest-printers.h +++ b/googletest/include/gtest/gtest-printers.h @@ -104,8 +104,10 @@ #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ #define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ +#include #include #include +#include #include // NOLINT #include #include @@ -113,6 +115,7 @@ #include #include #include +#include #include #ifdef GTEST_HAS_ABSL @@ -894,14 +897,11 @@ class UniversalPrinter { template class UniversalPrinter : public UniversalPrinter {}; -#if GTEST_INTERNAL_HAS_ANY - -// Printer for std::any / absl::any - +// Printer for std::any template <> -class UniversalPrinter { +class UniversalPrinter { public: - static void Print(const Any& value, ::std::ostream* os) { + static void Print(const std::any& value, ::std::ostream* os) { if (value.has_value()) { *os << "value of type " << GetTypeName(value); } else { @@ -910,7 +910,7 @@ class UniversalPrinter { } private: - static std::string GetTypeName(const Any& value) { + static std::string GetTypeName(const std::any& value) { #if GTEST_HAS_RTTI return internal::GetTypeName(value.type()); #else @@ -920,16 +920,11 @@ class UniversalPrinter { } }; -#endif // GTEST_INTERNAL_HAS_ANY - -#if GTEST_INTERNAL_HAS_OPTIONAL - -// Printer for std::optional / absl::optional - +// Printer for std::optional template -class UniversalPrinter> { +class UniversalPrinter> { public: - static void Print(const Optional& value, ::std::ostream* os) { + static void Print(const std::optional& value, ::std::ostream* os) { *os << '('; if (!value) { *os << "nullopt"; @@ -941,29 +936,18 @@ class UniversalPrinter> { }; template <> -class UniversalPrinter { +class UniversalPrinter { public: - static void Print(decltype(Nullopt()), ::std::ostream* os) { - *os << "(nullopt)"; - } + static void Print(std::nullopt_t, ::std::ostream* os) { *os << "(nullopt)"; } }; -#endif // GTEST_INTERNAL_HAS_OPTIONAL - -#if GTEST_INTERNAL_HAS_VARIANT - -// Printer for std::variant / absl::variant - +// Printer for std::variant template -class UniversalPrinter> { +class UniversalPrinter> { public: - static void Print(const Variant& value, ::std::ostream* os) { + static void Print(const std::variant& value, ::std::ostream* os) { *os << '('; -#ifdef GTEST_HAS_ABSL - absl::visit(Visitor{os, value.index()}, value); -#else std::visit(Visitor{os, value.index()}, value); -#endif // GTEST_HAS_ABSL *os << ')'; } @@ -980,8 +964,6 @@ class UniversalPrinter> { }; }; -#endif // GTEST_INTERNAL_HAS_VARIANT - // UniversalPrintArray(begin, len, os) prints an array of 'len' // elements, starting at address 'begin'. template diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index 25b7d194d..06a5a8e6f 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -198,21 +198,8 @@ // suppressed (constant conditional). // GTEST_INTENTIONAL_CONST_COND_POP_ - finish code section where MSVC C4127 // is suppressed. -// GTEST_INTERNAL_HAS_ANY - for enabling UniversalPrinter or -// UniversalPrinter specializations. -// Always defined to 0 or 1. -// GTEST_INTERNAL_HAS_OPTIONAL - for enabling UniversalPrinter -// or -// UniversalPrinter -// specializations. Always defined to 0 or 1. // GTEST_INTERNAL_HAS_STD_SPAN - for enabling UniversalPrinter // specializations. Always defined to 0 or 1 -// GTEST_INTERNAL_HAS_STRING_VIEW - for enabling Matcher or -// Matcher -// specializations. Always defined to 0 or 1. -// GTEST_INTERNAL_HAS_VARIANT - for enabling UniversalPrinter or -// UniversalPrinter -// specializations. Always defined to 0 or 1. // GTEST_USE_OWN_FLAGFILE_FLAG_ - Always defined to 0 or 1. // GTEST_HAS_CXXABI_H_ - Always defined to 0 or 1. // GTEST_CAN_STREAM_RESULTS_ - Always defined to 0 or 1. @@ -2335,73 +2322,6 @@ const char* StringFromGTestEnv(const char* flag, const char* default_val); } // namespace internal } // namespace testing -#ifdef GTEST_HAS_ABSL -// Always use absl::any for UniversalPrinter<> specializations if googletest -// is built with absl support. -#define GTEST_INTERNAL_HAS_ANY 1 -#include "absl/types/any.h" -namespace testing { -namespace internal { -using Any = ::absl::any; -} // namespace internal -} // namespace testing -#else -#if defined(__cpp_lib_any) || (GTEST_INTERNAL_HAS_INCLUDE() && \ - GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L && \ - (!defined(_MSC_VER) || GTEST_HAS_RTTI)) -// Otherwise for C++17 and higher use std::any for UniversalPrinter<> -// specializations. -#define GTEST_INTERNAL_HAS_ANY 1 -#include -namespace testing { -namespace internal { -using Any = ::std::any; -} // namespace internal -} // namespace testing -// The case where absl is configured NOT to alias std::any is not -// supported. -#endif // __cpp_lib_any -#endif // GTEST_HAS_ABSL - -#ifndef GTEST_INTERNAL_HAS_ANY -#define GTEST_INTERNAL_HAS_ANY 0 -#endif - -#ifdef GTEST_HAS_ABSL -// Always use absl::optional for UniversalPrinter<> specializations if -// googletest is built with absl support. -#define GTEST_INTERNAL_HAS_OPTIONAL 1 -#include "absl/types/optional.h" -namespace testing { -namespace internal { -template -using Optional = ::absl::optional; -inline ::absl::nullopt_t Nullopt() { return ::absl::nullopt; } -} // namespace internal -} // namespace testing -#else -#if defined(__cpp_lib_optional) || (GTEST_INTERNAL_HAS_INCLUDE() && \ - GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L) -// Otherwise for C++17 and higher use std::optional for UniversalPrinter<> -// specializations. -#define GTEST_INTERNAL_HAS_OPTIONAL 1 -#include -namespace testing { -namespace internal { -template -using Optional = ::std::optional; -inline ::std::nullopt_t Nullopt() { return ::std::nullopt; } -} // namespace internal -} // namespace testing -// The case where absl is configured NOT to alias std::optional is not -// supported. -#endif // __cpp_lib_optional -#endif // GTEST_HAS_ABSL - -#ifndef GTEST_INTERNAL_HAS_OPTIONAL -#define GTEST_INTERNAL_HAS_OPTIONAL 0 -#endif - #if defined(__cpp_lib_span) || (GTEST_INTERNAL_HAS_INCLUDE() && \ GTEST_INTERNAL_CPLUSPLUS_LANG >= 202002L) #define GTEST_INTERNAL_HAS_STD_SPAN 1 @@ -2443,38 +2363,6 @@ using StringView = ::std::string_view; #define GTEST_INTERNAL_HAS_STRING_VIEW 0 #endif -#ifdef GTEST_HAS_ABSL -// Always use absl::variant for UniversalPrinter<> specializations if googletest -// is built with absl support. -#define GTEST_INTERNAL_HAS_VARIANT 1 -#include "absl/types/variant.h" -namespace testing { -namespace internal { -template -using Variant = ::absl::variant; -} // namespace internal -} // namespace testing -#else -#if defined(__cpp_lib_variant) || (GTEST_INTERNAL_HAS_INCLUDE() && \ - GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L) -// Otherwise for C++17 and higher use std::variant for UniversalPrinter<> -// specializations. -#define GTEST_INTERNAL_HAS_VARIANT 1 -#include -namespace testing { -namespace internal { -template -using Variant = ::std::variant; -} // namespace internal -} // namespace testing -// The case where absl is configured NOT to alias std::variant is not supported. -#endif // __cpp_lib_variant -#endif // GTEST_HAS_ABSL - -#ifndef GTEST_INTERNAL_HAS_VARIANT -#define GTEST_INTERNAL_HAS_VARIANT 0 -#endif - #if (defined(__cpp_lib_three_way_comparison) || \ (GTEST_INTERNAL_HAS_INCLUDE() && \ GTEST_INTERNAL_CPLUSPLUS_LANG >= 201907L)) diff --git a/googletest/test/googletest-printers-test.cc b/googletest/test/googletest-printers-test.cc index 52b2c497d..a016e8a14 100644 --- a/googletest/test/googletest-printers-test.cc +++ b/googletest/test/googletest-printers-test.cc @@ -32,6 +32,7 @@ // This file tests the universal value printer. #include +#include #include #include #include @@ -42,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -50,6 +52,7 @@ #include #include #include +#include #include #include "gtest/gtest-printers.h" @@ -1920,7 +1923,6 @@ TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) { EXPECT_EQ("\"a\"", result[1]); } -#if GTEST_INTERNAL_HAS_ANY class PrintAnyTest : public ::testing::Test { protected: template @@ -1934,12 +1936,12 @@ class PrintAnyTest : public ::testing::Test { }; TEST_F(PrintAnyTest, Empty) { - internal::Any any; + std::any any; EXPECT_EQ("no value", PrintToString(any)); } TEST_F(PrintAnyTest, NonEmpty) { - internal::Any any; + std::any any; constexpr int val1 = 10; const std::string val2 = "content"; @@ -1950,27 +1952,23 @@ TEST_F(PrintAnyTest, NonEmpty) { EXPECT_EQ("value of type " + ExpectedTypeName(), PrintToString(any)); } -#endif // GTEST_INTERNAL_HAS_ANY -#if GTEST_INTERNAL_HAS_OPTIONAL TEST(PrintOptionalTest, Basic) { - EXPECT_EQ("(nullopt)", PrintToString(internal::Nullopt())); - internal::Optional value; + EXPECT_EQ("(nullopt)", PrintToString(std::nullopt)); + std::optional value; EXPECT_EQ("(nullopt)", PrintToString(value)); value = {7}; EXPECT_EQ("(7)", PrintToString(value)); - EXPECT_EQ("(1.1)", PrintToString(internal::Optional{1.1})); - EXPECT_EQ("(\"A\")", PrintToString(internal::Optional{"A"})); + EXPECT_EQ("(1.1)", PrintToString(std::optional{1.1})); + EXPECT_EQ("(\"A\")", PrintToString(std::optional{"A"})); } -#endif // GTEST_INTERNAL_HAS_OPTIONAL -#if GTEST_INTERNAL_HAS_VARIANT struct NonPrintable { unsigned char contents = 17; }; TEST(PrintOneofTest, Basic) { - using Type = internal::Variant; + using Type = std::variant; EXPECT_EQ("('int(index = 0)' with value 7)", PrintToString(Type(7))); EXPECT_EQ("('StreamableInGlobal(index = 1)' with value StreamableInGlobal)", PrintToString(Type(StreamableInGlobal{}))); @@ -1979,7 +1977,6 @@ TEST(PrintOneofTest, Basic) { "1-byte object <11>)", PrintToString(Type(NonPrintable{}))); } -#endif // GTEST_INTERNAL_HAS_VARIANT #if GTEST_INTERNAL_HAS_COMPARE_LIB TEST(PrintOrderingTest, Basic) {