Skip to content

Commit 16d4f8e

Browse files
derekmaurocopybara-github
authored andcommitted
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
1 parent bac6a8f commit 16d4f8e

File tree

5 files changed

+27
-166
lines changed

5 files changed

+27
-166
lines changed

BUILD.bazel

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,7 @@ cc_library(
151151
"@abseil-cpp//absl/flags:reflection",
152152
"@abseil-cpp//absl/flags:usage",
153153
"@abseil-cpp//absl/strings",
154-
"@abseil-cpp//absl/types:any",
155-
"@abseil-cpp//absl/types:optional",
156-
"@abseil-cpp//absl/types:variant",
157-
"@re2//:re2",
154+
"@re2",
158155
],
159156
"//conditions:default": [],
160157
}) + select({
@@ -174,8 +171,8 @@ cc_library(
174171
# `gtest`, but testonly. See guidance on `gtest` for when to use this.
175172
alias(
176173
name = "gtest_for_library",
177-
actual = ":gtest",
178174
testonly = True,
175+
actual = ":gtest",
179176
)
180177

181178
# Implements main() for tests using gtest. Prefer to depend on `gtest` as well

googletest/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,6 @@ if(GTEST_HAS_ABSL)
132132
absl::flags_reflection
133133
absl::flags_usage
134134
absl::strings
135-
absl::any
136-
absl::optional
137-
absl::variant
138135
re2::re2
139136
)
140137
endif()

googletest/include/gtest/gtest-printers.h

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,18 @@
104104
#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
105105
#define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
106106

107+
#include <any>
107108
#include <functional>
108109
#include <memory>
110+
#include <optional>
109111
#include <ostream> // NOLINT
110112
#include <sstream>
111113
#include <string>
112114
#include <tuple>
113115
#include <type_traits>
114116
#include <typeinfo>
115117
#include <utility>
118+
#include <variant>
116119
#include <vector>
117120

118121
#ifdef GTEST_HAS_ABSL
@@ -894,14 +897,11 @@ class UniversalPrinter {
894897
template <typename T>
895898
class UniversalPrinter<const T> : public UniversalPrinter<T> {};
896899

897-
#if GTEST_INTERNAL_HAS_ANY
898-
899-
// Printer for std::any / absl::any
900-
900+
// Printer for std::any
901901
template <>
902-
class UniversalPrinter<Any> {
902+
class UniversalPrinter<std::any> {
903903
public:
904-
static void Print(const Any& value, ::std::ostream* os) {
904+
static void Print(const std::any& value, ::std::ostream* os) {
905905
if (value.has_value()) {
906906
*os << "value of type " << GetTypeName(value);
907907
} else {
@@ -910,7 +910,7 @@ class UniversalPrinter<Any> {
910910
}
911911

912912
private:
913-
static std::string GetTypeName(const Any& value) {
913+
static std::string GetTypeName(const std::any& value) {
914914
#if GTEST_HAS_RTTI
915915
return internal::GetTypeName(value.type());
916916
#else
@@ -920,16 +920,11 @@ class UniversalPrinter<Any> {
920920
}
921921
};
922922

923-
#endif // GTEST_INTERNAL_HAS_ANY
924-
925-
#if GTEST_INTERNAL_HAS_OPTIONAL
926-
927-
// Printer for std::optional / absl::optional
928-
923+
// Printer for std::optional
929924
template <typename T>
930-
class UniversalPrinter<Optional<T>> {
925+
class UniversalPrinter<std::optional<T>> {
931926
public:
932-
static void Print(const Optional<T>& value, ::std::ostream* os) {
927+
static void Print(const std::optional<T>& value, ::std::ostream* os) {
933928
*os << '(';
934929
if (!value) {
935930
*os << "nullopt";
@@ -941,29 +936,18 @@ class UniversalPrinter<Optional<T>> {
941936
};
942937

943938
template <>
944-
class UniversalPrinter<decltype(Nullopt())> {
939+
class UniversalPrinter<std::nullopt_t> {
945940
public:
946-
static void Print(decltype(Nullopt()), ::std::ostream* os) {
947-
*os << "(nullopt)";
948-
}
941+
static void Print(std::nullopt_t, ::std::ostream* os) { *os << "(nullopt)"; }
949942
};
950943

951-
#endif // GTEST_INTERNAL_HAS_OPTIONAL
952-
953-
#if GTEST_INTERNAL_HAS_VARIANT
954-
955-
// Printer for std::variant / absl::variant
956-
944+
// Printer for std::variant
957945
template <typename... T>
958-
class UniversalPrinter<Variant<T...>> {
946+
class UniversalPrinter<std::variant<T...>> {
959947
public:
960-
static void Print(const Variant<T...>& value, ::std::ostream* os) {
948+
static void Print(const std::variant<T...>& value, ::std::ostream* os) {
961949
*os << '(';
962-
#ifdef GTEST_HAS_ABSL
963-
absl::visit(Visitor{os, value.index()}, value);
964-
#else
965950
std::visit(Visitor{os, value.index()}, value);
966-
#endif // GTEST_HAS_ABSL
967951
*os << ')';
968952
}
969953

@@ -980,8 +964,6 @@ class UniversalPrinter<Variant<T...>> {
980964
};
981965
};
982966

983-
#endif // GTEST_INTERNAL_HAS_VARIANT
984-
985967
// UniversalPrintArray(begin, len, os) prints an array of 'len'
986968
// elements, starting at address 'begin'.
987969
template <typename T>

googletest/include/gtest/internal/gtest-port.h

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -198,21 +198,8 @@
198198
// suppressed (constant conditional).
199199
// GTEST_INTENTIONAL_CONST_COND_POP_ - finish code section where MSVC C4127
200200
// is suppressed.
201-
// GTEST_INTERNAL_HAS_ANY - for enabling UniversalPrinter<std::any> or
202-
// UniversalPrinter<absl::any> specializations.
203-
// Always defined to 0 or 1.
204-
// GTEST_INTERNAL_HAS_OPTIONAL - for enabling UniversalPrinter<std::optional>
205-
// or
206-
// UniversalPrinter<absl::optional>
207-
// specializations. Always defined to 0 or 1.
208201
// GTEST_INTERNAL_HAS_STD_SPAN - for enabling UniversalPrinter<std::span>
209202
// specializations. Always defined to 0 or 1
210-
// GTEST_INTERNAL_HAS_STRING_VIEW - for enabling Matcher<std::string_view> or
211-
// Matcher<absl::string_view>
212-
// specializations. Always defined to 0 or 1.
213-
// GTEST_INTERNAL_HAS_VARIANT - for enabling UniversalPrinter<std::variant> or
214-
// UniversalPrinter<absl::variant>
215-
// specializations. Always defined to 0 or 1.
216203
// GTEST_USE_OWN_FLAGFILE_FLAG_ - Always defined to 0 or 1.
217204
// GTEST_HAS_CXXABI_H_ - Always defined to 0 or 1.
218205
// GTEST_CAN_STREAM_RESULTS_ - Always defined to 0 or 1.
@@ -2335,73 +2322,6 @@ const char* StringFromGTestEnv(const char* flag, const char* default_val);
23352322
} // namespace internal
23362323
} // namespace testing
23372324

2338-
#ifdef GTEST_HAS_ABSL
2339-
// Always use absl::any for UniversalPrinter<> specializations if googletest
2340-
// is built with absl support.
2341-
#define GTEST_INTERNAL_HAS_ANY 1
2342-
#include "absl/types/any.h"
2343-
namespace testing {
2344-
namespace internal {
2345-
using Any = ::absl::any;
2346-
} // namespace internal
2347-
} // namespace testing
2348-
#else
2349-
#if defined(__cpp_lib_any) || (GTEST_INTERNAL_HAS_INCLUDE(<any>) && \
2350-
GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L && \
2351-
(!defined(_MSC_VER) || GTEST_HAS_RTTI))
2352-
// Otherwise for C++17 and higher use std::any for UniversalPrinter<>
2353-
// specializations.
2354-
#define GTEST_INTERNAL_HAS_ANY 1
2355-
#include <any>
2356-
namespace testing {
2357-
namespace internal {
2358-
using Any = ::std::any;
2359-
} // namespace internal
2360-
} // namespace testing
2361-
// The case where absl is configured NOT to alias std::any is not
2362-
// supported.
2363-
#endif // __cpp_lib_any
2364-
#endif // GTEST_HAS_ABSL
2365-
2366-
#ifndef GTEST_INTERNAL_HAS_ANY
2367-
#define GTEST_INTERNAL_HAS_ANY 0
2368-
#endif
2369-
2370-
#ifdef GTEST_HAS_ABSL
2371-
// Always use absl::optional for UniversalPrinter<> specializations if
2372-
// googletest is built with absl support.
2373-
#define GTEST_INTERNAL_HAS_OPTIONAL 1
2374-
#include "absl/types/optional.h"
2375-
namespace testing {
2376-
namespace internal {
2377-
template <typename T>
2378-
using Optional = ::absl::optional<T>;
2379-
inline ::absl::nullopt_t Nullopt() { return ::absl::nullopt; }
2380-
} // namespace internal
2381-
} // namespace testing
2382-
#else
2383-
#if defined(__cpp_lib_optional) || (GTEST_INTERNAL_HAS_INCLUDE(<optional>) && \
2384-
GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L)
2385-
// Otherwise for C++17 and higher use std::optional for UniversalPrinter<>
2386-
// specializations.
2387-
#define GTEST_INTERNAL_HAS_OPTIONAL 1
2388-
#include <optional>
2389-
namespace testing {
2390-
namespace internal {
2391-
template <typename T>
2392-
using Optional = ::std::optional<T>;
2393-
inline ::std::nullopt_t Nullopt() { return ::std::nullopt; }
2394-
} // namespace internal
2395-
} // namespace testing
2396-
// The case where absl is configured NOT to alias std::optional is not
2397-
// supported.
2398-
#endif // __cpp_lib_optional
2399-
#endif // GTEST_HAS_ABSL
2400-
2401-
#ifndef GTEST_INTERNAL_HAS_OPTIONAL
2402-
#define GTEST_INTERNAL_HAS_OPTIONAL 0
2403-
#endif
2404-
24052325
#if defined(__cpp_lib_span) || (GTEST_INTERNAL_HAS_INCLUDE(<span>) && \
24062326
GTEST_INTERNAL_CPLUSPLUS_LANG >= 202002L)
24072327
#define GTEST_INTERNAL_HAS_STD_SPAN 1
@@ -2443,38 +2363,6 @@ using StringView = ::std::string_view;
24432363
#define GTEST_INTERNAL_HAS_STRING_VIEW 0
24442364
#endif
24452365

2446-
#ifdef GTEST_HAS_ABSL
2447-
// Always use absl::variant for UniversalPrinter<> specializations if googletest
2448-
// is built with absl support.
2449-
#define GTEST_INTERNAL_HAS_VARIANT 1
2450-
#include "absl/types/variant.h"
2451-
namespace testing {
2452-
namespace internal {
2453-
template <typename... T>
2454-
using Variant = ::absl::variant<T...>;
2455-
} // namespace internal
2456-
} // namespace testing
2457-
#else
2458-
#if defined(__cpp_lib_variant) || (GTEST_INTERNAL_HAS_INCLUDE(<variant>) && \
2459-
GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L)
2460-
// Otherwise for C++17 and higher use std::variant for UniversalPrinter<>
2461-
// specializations.
2462-
#define GTEST_INTERNAL_HAS_VARIANT 1
2463-
#include <variant>
2464-
namespace testing {
2465-
namespace internal {
2466-
template <typename... T>
2467-
using Variant = ::std::variant<T...>;
2468-
} // namespace internal
2469-
} // namespace testing
2470-
// The case where absl is configured NOT to alias std::variant is not supported.
2471-
#endif // __cpp_lib_variant
2472-
#endif // GTEST_HAS_ABSL
2473-
2474-
#ifndef GTEST_INTERNAL_HAS_VARIANT
2475-
#define GTEST_INTERNAL_HAS_VARIANT 0
2476-
#endif
2477-
24782366
#if (defined(__cpp_lib_three_way_comparison) || \
24792367
(GTEST_INTERNAL_HAS_INCLUDE(<compare>) && \
24802368
GTEST_INTERNAL_CPLUSPLUS_LANG >= 201907L))

googletest/test/googletest-printers-test.cc

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
// This file tests the universal value printer.
3333

3434
#include <algorithm>
35+
#include <any>
3536
#include <cctype>
3637
#include <cstdint>
3738
#include <cstring>
@@ -42,6 +43,7 @@
4243
#include <list>
4344
#include <map>
4445
#include <memory>
46+
#include <optional>
4547
#include <ostream>
4648
#include <set>
4749
#include <sstream>
@@ -50,6 +52,7 @@
5052
#include <unordered_map>
5153
#include <unordered_set>
5254
#include <utility>
55+
#include <variant>
5356
#include <vector>
5457

5558
#include "gtest/gtest-printers.h"
@@ -1920,7 +1923,6 @@ TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
19201923
EXPECT_EQ("\"a\"", result[1]);
19211924
}
19221925

1923-
#if GTEST_INTERNAL_HAS_ANY
19241926
class PrintAnyTest : public ::testing::Test {
19251927
protected:
19261928
template <typename T>
@@ -1934,12 +1936,12 @@ class PrintAnyTest : public ::testing::Test {
19341936
};
19351937

19361938
TEST_F(PrintAnyTest, Empty) {
1937-
internal::Any any;
1939+
std::any any;
19381940
EXPECT_EQ("no value", PrintToString(any));
19391941
}
19401942

19411943
TEST_F(PrintAnyTest, NonEmpty) {
1942-
internal::Any any;
1944+
std::any any;
19431945
constexpr int val1 = 10;
19441946
const std::string val2 = "content";
19451947

@@ -1950,27 +1952,23 @@ TEST_F(PrintAnyTest, NonEmpty) {
19501952
EXPECT_EQ("value of type " + ExpectedTypeName<std::string>(),
19511953
PrintToString(any));
19521954
}
1953-
#endif // GTEST_INTERNAL_HAS_ANY
19541955

1955-
#if GTEST_INTERNAL_HAS_OPTIONAL
19561956
TEST(PrintOptionalTest, Basic) {
1957-
EXPECT_EQ("(nullopt)", PrintToString(internal::Nullopt()));
1958-
internal::Optional<int> value;
1957+
EXPECT_EQ("(nullopt)", PrintToString(std::nullopt));
1958+
std::optional<int> value;
19591959
EXPECT_EQ("(nullopt)", PrintToString(value));
19601960
value = {7};
19611961
EXPECT_EQ("(7)", PrintToString(value));
1962-
EXPECT_EQ("(1.1)", PrintToString(internal::Optional<double>{1.1}));
1963-
EXPECT_EQ("(\"A\")", PrintToString(internal::Optional<std::string>{"A"}));
1962+
EXPECT_EQ("(1.1)", PrintToString(std::optional<double>{1.1}));
1963+
EXPECT_EQ("(\"A\")", PrintToString(std::optional<std::string>{"A"}));
19641964
}
1965-
#endif // GTEST_INTERNAL_HAS_OPTIONAL
19661965

1967-
#if GTEST_INTERNAL_HAS_VARIANT
19681966
struct NonPrintable {
19691967
unsigned char contents = 17;
19701968
};
19711969

19721970
TEST(PrintOneofTest, Basic) {
1973-
using Type = internal::Variant<int, StreamableInGlobal, NonPrintable>;
1971+
using Type = std::variant<int, StreamableInGlobal, NonPrintable>;
19741972
EXPECT_EQ("('int(index = 0)' with value 7)", PrintToString(Type(7)));
19751973
EXPECT_EQ("('StreamableInGlobal(index = 1)' with value StreamableInGlobal)",
19761974
PrintToString(Type(StreamableInGlobal{})));
@@ -1979,7 +1977,6 @@ TEST(PrintOneofTest, Basic) {
19791977
"1-byte object <11>)",
19801978
PrintToString(Type(NonPrintable{})));
19811979
}
1982-
#endif // GTEST_INTERNAL_HAS_VARIANT
19831980

19841981
#if GTEST_INTERNAL_HAS_COMPARE_LIB
19851982
TEST(PrintOrderingTest, Basic) {

0 commit comments

Comments
 (0)