Skip to content

Commit e40661d

Browse files
Abseil Teamcopybara-github
Abseil Team
authored andcommitted
Update C++ feature detection in gtest-port.h to rely on feature test macros where possible.
This also avoids conflating C++ language versions with standard library versions, which don't always align. PiperOrigin-RevId: 567662118 Change-Id: I7c023bd043c81c540c9430eaeb7b450feaadb206
1 parent 80306a7 commit e40661d

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

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

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,22 @@
281281
#error C++ versions less than C++14 are not supported.
282282
#endif
283283

284+
// MSVC >= 19.11 (VS 2017 Update 3) supports __has_include.
285+
#ifdef __has_include
286+
#define GTEST_INTERNAL_HAS_INCLUDE __has_include
287+
#else
288+
#define GTEST_INTERNAL_HAS_INCLUDE(...) 0
289+
#endif
290+
291+
// Detect C++ feature test macros as gracefully as possible.
292+
// MSVC >= 19.15, Clang >= 3.4.1, and GCC >= 4.1.2 support feature test macros.
293+
#if GTEST_INTERNAL_CPLUSPLUS_LANG >= 202002L && \
294+
(!defined(__has_include) || GTEST_INTERNAL_HAS_INCLUDE(<version>))
295+
#include <version> // C++20 and later
296+
#elif (!defined(__has_include) || GTEST_INTERNAL_HAS_INCLUDE(<ciso646>))
297+
#include <ciso646> // Pre-C++20
298+
#endif
299+
284300
#include <ctype.h> // for isspace, etc
285301
#include <stddef.h> // for ptrdiff_t
286302
#include <stdio.h>
@@ -2351,9 +2367,9 @@ using Any = ::absl::any;
23512367
} // namespace internal
23522368
} // namespace testing
23532369
#else
2354-
#ifdef __has_include
2355-
#if __has_include(<any>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L && \
2356-
(!defined(_MSC_VER) || GTEST_HAS_RTTI)
2370+
#if defined(__cpp_lib_any) || (GTEST_INTERNAL_HAS_INCLUDE(<any>) && \
2371+
GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L && \
2372+
(!defined(_MSC_VER) || GTEST_HAS_RTTI))
23572373
// Otherwise for C++17 and higher use std::any for UniversalPrinter<>
23582374
// specializations.
23592375
#define GTEST_INTERNAL_HAS_ANY 1
@@ -2365,8 +2381,7 @@ using Any = ::std::any;
23652381
} // namespace testing
23662382
// The case where absl is configured NOT to alias std::any is not
23672383
// supported.
2368-
#endif // __has_include(<any>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
2369-
#endif // __has_include
2384+
#endif // __cpp_lib_any
23702385
#endif // GTEST_HAS_ABSL
23712386

23722387
#ifndef GTEST_INTERNAL_HAS_ANY
@@ -2386,8 +2401,8 @@ inline ::absl::nullopt_t Nullopt() { return ::absl::nullopt; }
23862401
} // namespace internal
23872402
} // namespace testing
23882403
#else
2389-
#ifdef __has_include
2390-
#if __has_include(<optional>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
2404+
#if defined(__cpp_lib_optional) || (GTEST_INTERNAL_HAS_INCLUDE(<optional>) && \
2405+
GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L)
23912406
// Otherwise for C++17 and higher use std::optional for UniversalPrinter<>
23922407
// specializations.
23932408
#define GTEST_INTERNAL_HAS_OPTIONAL 1
@@ -2401,19 +2416,17 @@ inline ::std::nullopt_t Nullopt() { return ::std::nullopt; }
24012416
} // namespace testing
24022417
// The case where absl is configured NOT to alias std::optional is not
24032418
// supported.
2404-
#endif // __has_include(<optional>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
2405-
#endif // __has_include
2419+
#endif // __cpp_lib_optional
24062420
#endif // GTEST_HAS_ABSL
24072421

24082422
#ifndef GTEST_INTERNAL_HAS_OPTIONAL
24092423
#define GTEST_INTERNAL_HAS_OPTIONAL 0
24102424
#endif
24112425

2412-
#ifdef __has_include
2413-
#if __has_include(<span>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 202002L
2426+
#if defined(__cpp_lib_span) || (GTEST_INTERNAL_HAS_INCLUDE(<span>) && \
2427+
GTEST_INTERNAL_CPLUSPLUS_LANG >= 202002L)
24142428
#define GTEST_INTERNAL_HAS_STD_SPAN 1
2415-
#endif // __has_include(<span>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 202002L
2416-
#endif // __has_include
2429+
#endif // __cpp_lib_span
24172430

24182431
#ifndef GTEST_INTERNAL_HAS_STD_SPAN
24192432
#define GTEST_INTERNAL_HAS_STD_SPAN 0
@@ -2430,8 +2443,9 @@ using StringView = ::absl::string_view;
24302443
} // namespace internal
24312444
} // namespace testing
24322445
#else
2433-
#ifdef __has_include
2434-
#if __has_include(<string_view>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
2446+
#if defined(__cpp_lib_string_view) || \
2447+
(GTEST_INTERNAL_HAS_INCLUDE(<string_view>) && \
2448+
GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L)
24352449
// Otherwise for C++17 and higher use std::string_view for Matcher<>
24362450
// specializations.
24372451
#define GTEST_INTERNAL_HAS_STRING_VIEW 1
@@ -2443,9 +2457,7 @@ using StringView = ::std::string_view;
24432457
} // namespace testing
24442458
// The case where absl is configured NOT to alias std::string_view is not
24452459
// supported.
2446-
#endif // __has_include(<string_view>) && GTEST_INTERNAL_CPLUSPLUS_LANG >=
2447-
// 201703L
2448-
#endif // __has_include
2460+
#endif // __cpp_lib_string_view
24492461
#endif // GTEST_HAS_ABSL
24502462

24512463
#ifndef GTEST_INTERNAL_HAS_STRING_VIEW
@@ -2464,8 +2476,8 @@ using Variant = ::absl::variant<T...>;
24642476
} // namespace internal
24652477
} // namespace testing
24662478
#else
2467-
#ifdef __has_include
2468-
#if __has_include(<variant>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
2479+
#if defined(__cpp_lib_variant) || (GTEST_INTERNAL_HAS_INCLUDE(<variant>) && \
2480+
GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L)
24692481
// Otherwise for C++17 and higher use std::variant for UniversalPrinter<>
24702482
// specializations.
24712483
#define GTEST_INTERNAL_HAS_VARIANT 1
@@ -2477,16 +2489,16 @@ using Variant = ::std::variant<T...>;
24772489
} // namespace internal
24782490
} // namespace testing
24792491
// The case where absl is configured NOT to alias std::variant is not supported.
2480-
#endif // __has_include(<variant>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
2481-
#endif // __has_include
2492+
#endif // __cpp_lib_variant
24822493
#endif // GTEST_HAS_ABSL
24832494

24842495
#ifndef GTEST_INTERNAL_HAS_VARIANT
24852496
#define GTEST_INTERNAL_HAS_VARIANT 0
24862497
#endif
24872498

2488-
#if defined(GTEST_INTERNAL_CPLUSPLUS_LANG) && \
2489-
GTEST_INTERNAL_CPLUSPLUS_LANG < 201703L
2499+
#if (defined(__cpp_constexpr) && !defined(__cpp_inline_variables)) || \
2500+
(defined(GTEST_INTERNAL_CPLUSPLUS_LANG) && \
2501+
GTEST_INTERNAL_CPLUSPLUS_LANG < 201703L)
24902502
#define GTEST_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL 1
24912503
#endif
24922504

0 commit comments

Comments
 (0)