Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:18:09

0001 // Copyright 2005, Google Inc.
0002 // All rights reserved.
0003 //
0004 // Redistribution and use in source and binary forms, with or without
0005 // modification, are permitted provided that the following conditions are
0006 // met:
0007 //
0008 //     * Redistributions of source code must retain the above copyright
0009 // notice, this list of conditions and the following disclaimer.
0010 //     * Redistributions in binary form must reproduce the above
0011 // copyright notice, this list of conditions and the following disclaimer
0012 // in the documentation and/or other materials provided with the
0013 // distribution.
0014 //     * Neither the name of Google Inc. nor the names of its
0015 // contributors may be used to endorse or promote products derived from
0016 // this software without specific prior written permission.
0017 //
0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0029 
0030 // Low-level types and utilities for porting Google Test to various
0031 // platforms.  All macros ending with _ and symbols defined in an
0032 // internal namespace are subject to change without notice.  Code
0033 // outside Google Test MUST NOT USE THEM DIRECTLY.  Macros that don't
0034 // end with _ are part of Google Test's public API and can be used by
0035 // code outside Google Test.
0036 //
0037 // This file is fundamental to Google Test.  All other Google Test source
0038 // files are expected to #include this.  Therefore, it cannot #include
0039 // any other Google Test header.
0040 
0041 // IWYU pragma: private, include "gtest/gtest.h"
0042 // IWYU pragma: friend gtest/.*
0043 // IWYU pragma: friend gmock/.*
0044 
0045 #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
0046 #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
0047 
0048 // Environment-describing macros
0049 // -----------------------------
0050 //
0051 // Google Test can be used in many different environments.  Macros in
0052 // this section tell Google Test what kind of environment it is being
0053 // used in, such that Google Test can provide environment-specific
0054 // features and implementations.
0055 //
0056 // Google Test tries to automatically detect the properties of its
0057 // environment, so users usually don't need to worry about these
0058 // macros.  However, the automatic detection is not perfect.
0059 // Sometimes it's necessary for a user to define some of the following
0060 // macros in the build script to override Google Test's decisions.
0061 //
0062 // If the user doesn't define a macro in the list, Google Test will
0063 // provide a default definition.  After this header is #included, all
0064 // macros in this list will be defined to either 1 or 0.
0065 //
0066 // Notes to maintainers:
0067 //   - Each macro here is a user-tweakable knob; do not grow the list
0068 //     lightly.
0069 //   - Use #if to key off these macros.  Don't use #ifdef or "#if
0070 //     defined(...)", which will not work as these macros are ALWAYS
0071 //     defined.
0072 //
0073 //   GTEST_HAS_CLONE          - Define it to 1/0 to indicate that clone(2)
0074 //                              is/isn't available.
0075 //   GTEST_HAS_EXCEPTIONS     - Define it to 1/0 to indicate that exceptions
0076 //                              are enabled.
0077 //   GTEST_HAS_POSIX_RE       - Define it to 1/0 to indicate that POSIX regular
0078 //                              expressions are/aren't available.
0079 //   GTEST_HAS_PTHREAD        - Define it to 1/0 to indicate that <pthread.h>
0080 //                              is/isn't available.
0081 //   GTEST_HAS_RTTI           - Define it to 1/0 to indicate that RTTI is/isn't
0082 //                              enabled.
0083 //   GTEST_HAS_STD_WSTRING    - Define it to 1/0 to indicate that
0084 //                              std::wstring does/doesn't work (Google Test can
0085 //                              be used where std::wstring is unavailable).
0086 //   GTEST_HAS_FILE_SYSTEM    - Define it to 1/0 to indicate whether or not a
0087 //                              file system is/isn't available.
0088 //   GTEST_HAS_SEH            - Define it to 1/0 to indicate whether the
0089 //                              compiler supports Microsoft's "Structured
0090 //                              Exception Handling".
0091 //   GTEST_HAS_STREAM_REDIRECTION
0092 //                            - Define it to 1/0 to indicate whether the
0093 //                              platform supports I/O stream redirection using
0094 //                              dup() and dup2().
0095 //   GTEST_LINKED_AS_SHARED_LIBRARY
0096 //                            - Define to 1 when compiling tests that use
0097 //                              Google Test as a shared library (known as
0098 //                              DLL on Windows).
0099 //   GTEST_CREATE_SHARED_LIBRARY
0100 //                            - Define to 1 when compiling Google Test itself
0101 //                              as a shared library.
0102 //   GTEST_DEFAULT_DEATH_TEST_STYLE
0103 //                            - The default value of --gtest_death_test_style.
0104 //                              The legacy default has been "fast" in the open
0105 //                              source version since 2008. The recommended value
0106 //                              is "threadsafe", and can be set in
0107 //                              custom/gtest-port.h.
0108 
0109 // Platform-indicating macros
0110 // --------------------------
0111 //
0112 // Macros indicating the platform on which Google Test is being used
0113 // (a macro is defined to 1 if compiled on the given platform;
0114 // otherwise UNDEFINED -- it's never defined to 0.).  Google Test
0115 // defines these macros automatically.  Code outside Google Test MUST
0116 // NOT define them.
0117 //
0118 //   GTEST_OS_AIX      - IBM AIX
0119 //   GTEST_OS_CYGWIN   - Cygwin
0120 //   GTEST_OS_DRAGONFLY - DragonFlyBSD
0121 //   GTEST_OS_FREEBSD  - FreeBSD
0122 //   GTEST_OS_FUCHSIA  - Fuchsia
0123 //   GTEST_OS_GNU_HURD - GNU/Hurd
0124 //   GTEST_OS_GNU_KFREEBSD - GNU/kFreeBSD
0125 //   GTEST_OS_HAIKU    - Haiku
0126 //   GTEST_OS_HPUX     - HP-UX
0127 //   GTEST_OS_LINUX    - Linux
0128 //     GTEST_OS_LINUX_ANDROID - Google Android
0129 //   GTEST_OS_MAC      - Mac OS X
0130 //     GTEST_OS_IOS    - iOS
0131 //   GTEST_OS_NACL     - Google Native Client (NaCl)
0132 //   GTEST_OS_NETBSD   - NetBSD
0133 //   GTEST_OS_OPENBSD  - OpenBSD
0134 //   GTEST_OS_OS2      - OS/2
0135 //   GTEST_OS_QNX      - QNX
0136 //   GTEST_OS_SOLARIS  - Sun Solaris
0137 //   GTEST_OS_WINDOWS  - Windows (Desktop, MinGW, or Mobile)
0138 //     GTEST_OS_WINDOWS_DESKTOP  - Windows Desktop
0139 //     GTEST_OS_WINDOWS_MINGW    - MinGW
0140 //     GTEST_OS_WINDOWS_MOBILE   - Windows Mobile
0141 //     GTEST_OS_WINDOWS_PHONE    - Windows Phone
0142 //     GTEST_OS_WINDOWS_RT       - Windows Store App/WinRT
0143 //   GTEST_OS_ZOS      - z/OS
0144 //
0145 // Among the platforms, Cygwin, Linux, Mac OS X, and Windows have the
0146 // most stable support.  Since core members of the Google Test project
0147 // don't have access to other platforms, support for them may be less
0148 // stable.  If you notice any problems on your platform, please notify
0149 // googletestframework@googlegroups.com (patches for fixing them are
0150 // even more welcome!).
0151 //
0152 // It is possible that none of the GTEST_OS_* macros are defined.
0153 
0154 // Feature-indicating macros
0155 // -------------------------
0156 //
0157 // Macros indicating which Google Test features are available (a macro
0158 // is defined to 1 if the corresponding feature is supported;
0159 // otherwise UNDEFINED -- it's never defined to 0.).  Google Test
0160 // defines these macros automatically.  Code outside Google Test MUST
0161 // NOT define them.
0162 //
0163 // These macros are public so that portable tests can be written.
0164 // Such tests typically surround code using a feature with an #ifdef
0165 // which controls that code.  For example:
0166 //
0167 // #ifdef GTEST_HAS_DEATH_TEST
0168 //   EXPECT_DEATH(DoSomethingDeadly());
0169 // #endif
0170 //
0171 //   GTEST_HAS_DEATH_TEST   - death tests
0172 //   GTEST_HAS_TYPED_TEST   - typed tests
0173 //   GTEST_HAS_TYPED_TEST_P - type-parameterized tests
0174 //   GTEST_IS_THREADSAFE    - Google Test is thread-safe.
0175 //   GTEST_USES_RE2         - the RE2 regular expression library is used
0176 //   GTEST_USES_POSIX_RE    - enhanced POSIX regex is used. Do not confuse with
0177 //                            GTEST_HAS_POSIX_RE (see above) which users can
0178 //                            define themselves.
0179 //   GTEST_USES_SIMPLE_RE   - our own simple regex is used;
0180 //                            the above RE\b(s) are mutually exclusive.
0181 //   GTEST_HAS_ABSL         - Google Test is compiled with Abseil.
0182 
0183 // Misc public macros
0184 // ------------------
0185 //
0186 //   GTEST_FLAG(flag_name)  - references the variable corresponding to
0187 //                            the given Google Test flag.
0188 
0189 // Internal utilities
0190 // ------------------
0191 //
0192 // The following macros and utilities are for Google Test's INTERNAL
0193 // use only.  Code outside Google Test MUST NOT USE THEM DIRECTLY.
0194 //
0195 // Macros for basic C++ coding:
0196 //   GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.
0197 //   GTEST_INTENTIONAL_CONST_COND_PUSH_ - start code section where MSVC C4127 is
0198 //                                        suppressed (constant conditional).
0199 //   GTEST_INTENTIONAL_CONST_COND_POP_  - finish code section where MSVC C4127
0200 //                                        is suppressed.
0201 //   GTEST_INTERNAL_HAS_ANY - for enabling UniversalPrinter<std::any> or
0202 //                            UniversalPrinter<absl::any> specializations.
0203 //                            Always defined to 0 or 1.
0204 //   GTEST_INTERNAL_HAS_OPTIONAL - for enabling UniversalPrinter<std::optional>
0205 //   or
0206 //                                 UniversalPrinter<absl::optional>
0207 //                                 specializations. Always defined to 0 or 1.
0208 //   GTEST_INTERNAL_HAS_STD_SPAN - for enabling UniversalPrinter<std::span>
0209 //                                 specializations. Always defined to 0 or 1
0210 //   GTEST_INTERNAL_HAS_STRING_VIEW - for enabling Matcher<std::string_view> or
0211 //                                    Matcher<absl::string_view>
0212 //                                    specializations. Always defined to 0 or 1.
0213 //   GTEST_INTERNAL_HAS_VARIANT - for enabling UniversalPrinter<std::variant> or
0214 //                                UniversalPrinter<absl::variant>
0215 //                                specializations. Always defined to 0 or 1.
0216 //   GTEST_USE_OWN_FLAGFILE_FLAG_ - Always defined to 0 or 1.
0217 //   GTEST_HAS_CXXABI_H_ - Always defined to 0 or 1.
0218 //   GTEST_CAN_STREAM_RESULTS_ - Always defined to 0 or 1.
0219 //   GTEST_HAS_ALT_PATH_SEP_ - Always defined to 0 or 1.
0220 //   GTEST_WIDE_STRING_USES_UTF16_ - Always defined to 0 or 1.
0221 //   GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ - Always defined to 0 or 1.
0222 //   GTEST_HAS_NOTIFICATION_- Always defined to 0 or 1.
0223 //
0224 // Synchronization:
0225 //   Mutex, MutexLock, ThreadLocal, GetThreadCount()
0226 //                            - synchronization primitives.
0227 //
0228 // Regular expressions:
0229 //   RE             - a simple regular expression class using
0230 //                     1) the RE2 syntax on all platforms when built with RE2
0231 //                        and Abseil as dependencies
0232 //                     2) the POSIX Extended Regular Expression syntax on
0233 //                        UNIX-like platforms,
0234 //                     3) A reduced regular exception syntax on other platforms,
0235 //                        including Windows.
0236 // Logging:
0237 //   GTEST_LOG_()   - logs messages at the specified severity level.
0238 //   LogToStderr()  - directs all log messages to stderr.
0239 //   FlushInfoLog() - flushes informational log messages.
0240 //
0241 // Stdout and stderr capturing:
0242 //   CaptureStdout()     - starts capturing stdout.
0243 //   GetCapturedStdout() - stops capturing stdout and returns the captured
0244 //                         string.
0245 //   CaptureStderr()     - starts capturing stderr.
0246 //   GetCapturedStderr() - stops capturing stderr and returns the captured
0247 //                         string.
0248 //
0249 // Integer types:
0250 //   TypeWithSize   - maps an integer to a int type.
0251 //   TimeInMillis   - integers of known sizes.
0252 //   BiggestInt     - the biggest signed integer type.
0253 //
0254 // Command-line utilities:
0255 //   GetInjectableArgvs() - returns the command line as a vector of strings.
0256 //
0257 // Environment variable utilities:
0258 //   GetEnv()             - gets the value of an environment variable.
0259 //   BoolFromGTestEnv()   - parses a bool environment variable.
0260 //   Int32FromGTestEnv()  - parses an int32_t environment variable.
0261 //   StringFromGTestEnv() - parses a string environment variable.
0262 
0263 // The definition of GTEST_INTERNAL_CPLUSPLUS_LANG comes first because it can
0264 // potentially be used as an #include guard.
0265 #if defined(_MSVC_LANG)
0266 #define GTEST_INTERNAL_CPLUSPLUS_LANG _MSVC_LANG
0267 #elif defined(__cplusplus)
0268 #define GTEST_INTERNAL_CPLUSPLUS_LANG __cplusplus
0269 #endif
0270 
0271 #if !defined(GTEST_INTERNAL_CPLUSPLUS_LANG) || \
0272     GTEST_INTERNAL_CPLUSPLUS_LANG < 201703L
0273 #error C++ versions less than C++17 are not supported.
0274 #endif
0275 
0276 // MSVC >= 19.11 (VS 2017 Update 3) supports __has_include.
0277 #ifdef __has_include
0278 #define GTEST_INTERNAL_HAS_INCLUDE __has_include
0279 #else
0280 #define GTEST_INTERNAL_HAS_INCLUDE(...) 0
0281 #endif
0282 
0283 // Detect C++ feature test macros as gracefully as possible.
0284 // MSVC >= 19.15, Clang >= 3.4.1, and GCC >= 4.1.2 support feature test macros.
0285 //
0286 // GCC15 warns that <ciso646> is deprecated in C++17 and suggests using
0287 // <version> instead, even though <version> is not available in C++17 mode prior
0288 // to GCC9.
0289 #if GTEST_INTERNAL_CPLUSPLUS_LANG >= 202002L || \
0290     GTEST_INTERNAL_HAS_INCLUDE(<version>)
0291 #include <version>  // C++20 or <version> support.
0292 #else
0293 #include <ciso646>  // Pre-C++20
0294 #endif
0295 
0296 #include <ctype.h>   // for isspace, etc
0297 #include <stddef.h>  // for ptrdiff_t
0298 #include <stdio.h>
0299 #include <stdlib.h>
0300 #include <string.h>
0301 
0302 #include <cerrno>
0303 // #include <condition_variable>  // Guarded by GTEST_IS_THREADSAFE below
0304 #include <cstdint>
0305 #include <iostream>
0306 #include <limits>
0307 #include <locale>
0308 #include <memory>
0309 #include <ostream>
0310 #include <string>
0311 // #include <mutex>  // Guarded by GTEST_IS_THREADSAFE below
0312 #include <tuple>
0313 #include <type_traits>
0314 #include <vector>
0315 
0316 #ifndef _WIN32_WCE
0317 #include <sys/stat.h>
0318 #include <sys/types.h>
0319 #endif  // !_WIN32_WCE
0320 
0321 #if defined __APPLE__
0322 #include <AvailabilityMacros.h>
0323 #include <TargetConditionals.h>
0324 #endif
0325 
0326 #include "gtest/internal/custom/gtest-port.h"
0327 #include "gtest/internal/gtest-port-arch.h"
0328 
0329 #ifndef GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
0330 #define GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ 0
0331 #endif
0332 
0333 #ifndef GTEST_HAS_NOTIFICATION_
0334 #define GTEST_HAS_NOTIFICATION_ 0
0335 #endif
0336 
0337 #if defined(GTEST_HAS_ABSL) && !defined(GTEST_NO_ABSL_FLAGS)
0338 #define GTEST_INTERNAL_HAS_ABSL_FLAGS  // Used only in this file.
0339 #include "absl/flags/declare.h"
0340 #include "absl/flags/flag.h"
0341 #include "absl/flags/reflection.h"
0342 #endif
0343 
0344 #if !defined(GTEST_DEV_EMAIL_)
0345 #define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
0346 #define GTEST_FLAG_PREFIX_ "gtest_"
0347 #define GTEST_FLAG_PREFIX_DASH_ "gtest-"
0348 #define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
0349 #define GTEST_NAME_ "Google Test"
0350 #define GTEST_PROJECT_URL_ "https://github.com/google/googletest/"
0351 #endif  // !defined(GTEST_DEV_EMAIL_)
0352 
0353 #if !defined(GTEST_INIT_GOOGLE_TEST_NAME_)
0354 #define GTEST_INIT_GOOGLE_TEST_NAME_ "testing::InitGoogleTest"
0355 #endif  // !defined(GTEST_INIT_GOOGLE_TEST_NAME_)
0356 
0357 // Determines the version of gcc that is used to compile this.
0358 #ifdef __GNUC__
0359 // 40302 means version 4.3.2.
0360 #define GTEST_GCC_VER_ \
0361   (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
0362 #endif  // __GNUC__
0363 
0364 // Macros for disabling Microsoft Visual C++ warnings.
0365 //
0366 //   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 4385)
0367 //   /* code that triggers warnings C4800 and C4385 */
0368 //   GTEST_DISABLE_MSC_WARNINGS_POP_()
0369 #if defined(_MSC_VER)
0370 #define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) \
0371   __pragma(warning(push)) __pragma(warning(disable : warnings))
0372 #define GTEST_DISABLE_MSC_WARNINGS_POP_() __pragma(warning(pop))
0373 #else
0374 // Not all compilers are MSVC
0375 #define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
0376 #define GTEST_DISABLE_MSC_WARNINGS_POP_()
0377 #endif
0378 
0379 // Clang on Windows does not understand MSVC's pragma warning.
0380 // We need clang-specific way to disable function deprecation warning.
0381 #ifdef __clang__
0382 #define GTEST_DISABLE_MSC_DEPRECATED_PUSH_()                            \
0383   _Pragma("clang diagnostic push")                                      \
0384       _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") \
0385           _Pragma("clang diagnostic ignored \"-Wdeprecated-implementations\"")
0386 #define GTEST_DISABLE_MSC_DEPRECATED_POP_() _Pragma("clang diagnostic pop")
0387 #else
0388 #define GTEST_DISABLE_MSC_DEPRECATED_PUSH_() \
0389   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996)
0390 #define GTEST_DISABLE_MSC_DEPRECATED_POP_() GTEST_DISABLE_MSC_WARNINGS_POP_()
0391 #endif
0392 
0393 // Brings in definitions for functions used in the testing::internal::posix
0394 // namespace (read, write, close, chdir, isatty, stat). We do not currently
0395 // use them on Windows Mobile.
0396 #ifdef GTEST_OS_WINDOWS
0397 #ifndef GTEST_OS_WINDOWS_MOBILE
0398 #include <direct.h>
0399 #include <io.h>
0400 #endif
0401 // In order to avoid having to include <windows.h>, use forward declaration
0402 #if defined(GTEST_OS_WINDOWS_MINGW) && !defined(__MINGW64_VERSION_MAJOR)
0403 // MinGW defined _CRITICAL_SECTION and _RTL_CRITICAL_SECTION as two
0404 // separate (equivalent) structs, instead of using typedef
0405 typedef struct _CRITICAL_SECTION GTEST_CRITICAL_SECTION;
0406 #else
0407 // Assume CRITICAL_SECTION is a typedef of _RTL_CRITICAL_SECTION.
0408 // This assumption is verified by
0409 // WindowsTypesTest.CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION.
0410 typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
0411 #endif
0412 #elif defined(GTEST_OS_XTENSA)
0413 #include <unistd.h>
0414 // Xtensa toolchains define strcasecmp in the string.h header instead of
0415 // strings.h. string.h is already included.
0416 #else
0417 // This assumes that non-Windows OSes provide unistd.h. For OSes where this
0418 // is not the case, we need to include headers that provide the functions
0419 // mentioned above.
0420 #include <strings.h>
0421 #include <unistd.h>
0422 #endif  // GTEST_OS_WINDOWS
0423 
0424 #ifdef GTEST_OS_LINUX_ANDROID
0425 // Used to define __ANDROID_API__ matching the target NDK API level.
0426 #include <android/api-level.h>  // NOLINT
0427 #endif
0428 
0429 // Defines this to true if and only if Google Test can use POSIX regular
0430 // expressions.
0431 #ifndef GTEST_HAS_POSIX_RE
0432 #ifdef GTEST_OS_LINUX_ANDROID
0433 // On Android, <regex.h> is only available starting with Gingerbread.
0434 #define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9)
0435 #else
0436 #if !(defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_XTENSA) || \
0437       defined(GTEST_OS_QURT))
0438 #define GTEST_HAS_POSIX_RE 1
0439 #else
0440 #define GTEST_HAS_POSIX_RE 0
0441 #endif
0442 #endif  // GTEST_OS_LINUX_ANDROID
0443 #endif
0444 
0445 // Select the regular expression implementation.
0446 #ifdef GTEST_HAS_ABSL
0447 // When using Abseil, RE2 is required.
0448 #include "absl/strings/string_view.h"
0449 #include "re2/re2.h"
0450 #define GTEST_USES_RE2 1
0451 #elif GTEST_HAS_POSIX_RE
0452 #include <regex.h>  // NOLINT
0453 #define GTEST_USES_POSIX_RE 1
0454 #else
0455 // Use our own simple regex implementation.
0456 #define GTEST_USES_SIMPLE_RE 1
0457 #endif
0458 
0459 #ifndef GTEST_HAS_EXCEPTIONS
0460 // The user didn't tell us whether exceptions are enabled, so we need
0461 // to figure it out.
0462 #if defined(_MSC_VER) && defined(_CPPUNWIND)
0463 // MSVC defines _CPPUNWIND to 1 if and only if exceptions are enabled.
0464 #define GTEST_HAS_EXCEPTIONS 1
0465 #elif defined(__BORLANDC__)
0466 // C++Builder's implementation of the STL uses the _HAS_EXCEPTIONS
0467 // macro to enable exceptions, so we'll do the same.
0468 // Assumes that exceptions are enabled by default.
0469 #ifndef _HAS_EXCEPTIONS
0470 #define _HAS_EXCEPTIONS 1
0471 #endif  // _HAS_EXCEPTIONS
0472 #define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
0473 #elif defined(__clang__)
0474 // clang defines __EXCEPTIONS if and only if exceptions are enabled before clang
0475 // 220714, but if and only if cleanups are enabled after that. In Obj-C++ files,
0476 // there can be cleanups for ObjC exceptions which also need cleanups, even if
0477 // C++ exceptions are disabled. clang has __has_feature(cxx_exceptions) which
0478 // checks for C++ exceptions starting at clang r206352, but which checked for
0479 // cleanups prior to that. To reliably check for C++ exception availability with
0480 // clang, check for
0481 // __EXCEPTIONS && __has_feature(cxx_exceptions).
0482 #if defined(__EXCEPTIONS) && __EXCEPTIONS && __has_feature(cxx_exceptions)
0483 #define GTEST_HAS_EXCEPTIONS 1
0484 #else
0485 #define GTEST_HAS_EXCEPTIONS 0
0486 #endif
0487 #elif defined(__GNUC__) && defined(__EXCEPTIONS) && __EXCEPTIONS
0488 // gcc defines __EXCEPTIONS to 1 if and only if exceptions are enabled.
0489 #define GTEST_HAS_EXCEPTIONS 1
0490 #elif defined(__SUNPRO_CC)
0491 // Sun Pro CC supports exceptions.  However, there is no compile-time way of
0492 // detecting whether they are enabled or not.  Therefore, we assume that
0493 // they are enabled unless the user tells us otherwise.
0494 #define GTEST_HAS_EXCEPTIONS 1
0495 #elif defined(__IBMCPP__) && defined(__EXCEPTIONS) && __EXCEPTIONS
0496 // xlC defines __EXCEPTIONS to 1 if and only if exceptions are enabled.
0497 #define GTEST_HAS_EXCEPTIONS 1
0498 #elif defined(__HP_aCC)
0499 // Exception handling is in effect by default in HP aCC compiler. It has to
0500 // be turned of by +noeh compiler option if desired.
0501 #define GTEST_HAS_EXCEPTIONS 1
0502 #else
0503 // For other compilers, we assume exceptions are disabled to be
0504 // conservative.
0505 #define GTEST_HAS_EXCEPTIONS 0
0506 #endif  // defined(_MSC_VER) || defined(__BORLANDC__)
0507 #endif  // GTEST_HAS_EXCEPTIONS
0508 
0509 #ifndef GTEST_HAS_STD_WSTRING
0510 // The user didn't tell us whether ::std::wstring is available, so we need
0511 // to figure it out.
0512 // Cygwin 1.7 and below doesn't support ::std::wstring.
0513 // Solaris' libc++ doesn't support it either.  Android has
0514 // no support for it at least as recent as Froyo (2.2).
0515 #if (!(defined(GTEST_OS_LINUX_ANDROID) || defined(GTEST_OS_CYGWIN) || \
0516        defined(GTEST_OS_SOLARIS) || defined(GTEST_OS_HAIKU) ||        \
0517        defined(GTEST_OS_ESP32) || defined(GTEST_OS_ESP8266) ||        \
0518        defined(GTEST_OS_XTENSA) || defined(GTEST_OS_QURT) ||          \
0519        defined(GTEST_OS_NXP_QN9090) || defined(GTEST_OS_NRF52)))
0520 #define GTEST_HAS_STD_WSTRING 1
0521 #else
0522 #define GTEST_HAS_STD_WSTRING 0
0523 #endif
0524 #endif  // GTEST_HAS_STD_WSTRING
0525 
0526 #ifndef GTEST_HAS_FILE_SYSTEM
0527 // Most platforms support a file system.
0528 #define GTEST_HAS_FILE_SYSTEM 1
0529 #endif  // GTEST_HAS_FILE_SYSTEM
0530 
0531 // Determines whether RTTI is available.
0532 #ifndef GTEST_HAS_RTTI
0533 // The user didn't tell us whether RTTI is enabled, so we need to
0534 // figure it out.
0535 
0536 #ifdef _MSC_VER
0537 
0538 #ifdef _CPPRTTI  // MSVC defines this macro if and only if RTTI is enabled.
0539 #define GTEST_HAS_RTTI 1
0540 #else
0541 #define GTEST_HAS_RTTI 0
0542 #endif
0543 
0544 // Starting with version 4.3.2, gcc defines __GXX_RTTI if and only if RTTI is
0545 // enabled.
0546 #elif defined(__GNUC__)
0547 
0548 #ifdef __GXX_RTTI
0549 // When building against STLport with the Android NDK and with
0550 // -frtti -fno-exceptions, the build fails at link time with undefined
0551 // references to __cxa_bad_typeid. Note sure if STL or toolchain bug,
0552 // so disable RTTI when detected.
0553 #if defined(GTEST_OS_LINUX_ANDROID) && defined(_STLPORT_MAJOR) && \
0554     !defined(__EXCEPTIONS)
0555 #define GTEST_HAS_RTTI 0
0556 #else
0557 #define GTEST_HAS_RTTI 1
0558 #endif  // GTEST_OS_LINUX_ANDROID && __STLPORT_MAJOR && !__EXCEPTIONS
0559 #else
0560 #define GTEST_HAS_RTTI 0
0561 #endif  // __GXX_RTTI
0562 
0563 // Clang defines __GXX_RTTI starting with version 3.0, but its manual recommends
0564 // using has_feature instead. has_feature(cxx_rtti) is supported since 2.7, the
0565 // first version with C++ support.
0566 #elif defined(__clang__)
0567 
0568 #define GTEST_HAS_RTTI __has_feature(cxx_rtti)
0569 
0570 // Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if
0571 // both the typeid and dynamic_cast features are present.
0572 #elif defined(__IBMCPP__) && (__IBMCPP__ >= 900)
0573 
0574 #ifdef __RTTI_ALL__
0575 #define GTEST_HAS_RTTI 1
0576 #else
0577 #define GTEST_HAS_RTTI 0
0578 #endif
0579 
0580 #else
0581 
0582 // For all other compilers, we assume RTTI is enabled.
0583 #define GTEST_HAS_RTTI 1
0584 
0585 #endif  // _MSC_VER
0586 
0587 #endif  // GTEST_HAS_RTTI
0588 
0589 // It's this header's responsibility to #include <typeinfo> when RTTI
0590 // is enabled.
0591 #if GTEST_HAS_RTTI
0592 #include <typeinfo>
0593 #endif
0594 
0595 // Determines whether Google Test can use the pthreads library.
0596 #ifndef GTEST_HAS_PTHREAD
0597 // The user didn't tell us explicitly, so we make reasonable assumptions about
0598 // which platforms have pthreads support.
0599 //
0600 // To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0
0601 // to your compiler flags.
0602 #if (defined(GTEST_OS_LINUX) || defined(GTEST_OS_MAC) ||              \
0603      defined(GTEST_OS_HPUX) || defined(GTEST_OS_QNX) ||               \
0604      defined(GTEST_OS_FREEBSD) || defined(GTEST_OS_NACL) ||           \
0605      defined(GTEST_OS_NETBSD) || defined(GTEST_OS_FUCHSIA) ||         \
0606      defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_GNU_KFREEBSD) || \
0607      defined(GTEST_OS_OPENBSD) || defined(GTEST_OS_HAIKU) ||          \
0608      defined(GTEST_OS_GNU_HURD) || defined(GTEST_OS_SOLARIS) ||       \
0609      defined(GTEST_OS_AIX) || defined(GTEST_OS_ZOS))
0610 #define GTEST_HAS_PTHREAD 1
0611 #else
0612 #define GTEST_HAS_PTHREAD 0
0613 #endif
0614 #endif  // GTEST_HAS_PTHREAD
0615 
0616 #if GTEST_HAS_PTHREAD
0617 // gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is
0618 // true.
0619 #include <pthread.h>  // NOLINT
0620 
0621 // For timespec and nanosleep, used below.
0622 #include <time.h>  // NOLINT
0623 #endif
0624 
0625 // Determines whether clone(2) is supported.
0626 // Usually it will only be available on Linux, excluding
0627 // Linux on the Itanium architecture.
0628 // Also see https://linux.die.net/man/2/clone.
0629 #ifndef GTEST_HAS_CLONE
0630 // The user didn't tell us, so we need to figure it out.
0631 
0632 #if defined(GTEST_OS_LINUX) && !defined(__ia64__)
0633 #if defined(GTEST_OS_LINUX_ANDROID)
0634 // On Android, clone() became available at different API levels for each 32-bit
0635 // architecture.
0636 #if defined(__LP64__) || (defined(__arm__) && __ANDROID_API__ >= 9) || \
0637     (defined(__mips__) && __ANDROID_API__ >= 12) ||                    \
0638     (defined(__i386__) && __ANDROID_API__ >= 17)
0639 #define GTEST_HAS_CLONE 1
0640 #else
0641 #define GTEST_HAS_CLONE 0
0642 #endif
0643 #else
0644 #define GTEST_HAS_CLONE 1
0645 #endif
0646 #else
0647 #define GTEST_HAS_CLONE 0
0648 #endif  // GTEST_OS_LINUX && !defined(__ia64__)
0649 
0650 #endif  // GTEST_HAS_CLONE
0651 
0652 // Determines whether to support stream redirection. This is used to test
0653 // output correctness and to implement death tests.
0654 #ifndef GTEST_HAS_STREAM_REDIRECTION
0655 // By default, we assume that stream redirection is supported on all
0656 // platforms except known mobile / embedded ones. Also, if the port doesn't have
0657 // a file system, stream redirection is not supported.
0658 #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_WINDOWS_PHONE) || \
0659     defined(GTEST_OS_WINDOWS_RT) || defined(GTEST_OS_WINDOWS_GAMES) ||     \
0660     defined(GTEST_OS_ESP8266) || defined(GTEST_OS_XTENSA) ||               \
0661     defined(GTEST_OS_QURT) || !GTEST_HAS_FILE_SYSTEM
0662 #define GTEST_HAS_STREAM_REDIRECTION 0
0663 #else
0664 #define GTEST_HAS_STREAM_REDIRECTION 1
0665 #endif  // !GTEST_OS_WINDOWS_MOBILE
0666 #endif  // GTEST_HAS_STREAM_REDIRECTION
0667 
0668 // Determines whether to support death tests.
0669 // pops up a dialog window that cannot be suppressed programmatically.
0670 #if (defined(GTEST_OS_LINUX) || defined(GTEST_OS_CYGWIN) ||           \
0671      defined(GTEST_OS_SOLARIS) || defined(GTEST_OS_ZOS) ||            \
0672      (defined(GTEST_OS_MAC) && !defined(GTEST_OS_IOS)) ||             \
0673      (defined(GTEST_OS_WINDOWS_DESKTOP) && _MSC_VER) ||               \
0674      defined(GTEST_OS_WINDOWS_MINGW) || defined(GTEST_OS_AIX) ||      \
0675      defined(GTEST_OS_HPUX) || defined(GTEST_OS_OPENBSD) ||           \
0676      defined(GTEST_OS_QNX) || defined(GTEST_OS_FREEBSD) ||            \
0677      defined(GTEST_OS_NETBSD) || defined(GTEST_OS_FUCHSIA) ||         \
0678      defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_GNU_KFREEBSD) || \
0679      defined(GTEST_OS_HAIKU) || defined(GTEST_OS_GNU_HURD))
0680 // Death tests require a file system to work properly.
0681 #if GTEST_HAS_FILE_SYSTEM
0682 #define GTEST_HAS_DEATH_TEST 1
0683 #endif  // GTEST_HAS_FILE_SYSTEM
0684 #endif
0685 
0686 // Determines whether to support type-driven tests.
0687 
0688 // Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0,
0689 // Sun Pro CC, IBM Visual Age, and HP aCC support.
0690 #if defined(__GNUC__) || defined(_MSC_VER) || defined(__SUNPRO_CC) || \
0691     defined(__IBMCPP__) || defined(__HP_aCC)
0692 #define GTEST_HAS_TYPED_TEST 1
0693 #define GTEST_HAS_TYPED_TEST_P 1
0694 #endif
0695 
0696 // Determines whether the system compiler uses UTF-16 for encoding wide strings.
0697 #if defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_CYGWIN) || \
0698     defined(GTEST_OS_AIX) || defined(GTEST_OS_OS2)
0699 #define GTEST_WIDE_STRING_USES_UTF16_ 1
0700 #else
0701 #define GTEST_WIDE_STRING_USES_UTF16_ 0
0702 #endif
0703 
0704 // Determines whether test results can be streamed to a socket.
0705 #if defined(GTEST_OS_LINUX) || defined(GTEST_OS_GNU_KFREEBSD) || \
0706     defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_FREEBSD) ||  \
0707     defined(GTEST_OS_NETBSD) || defined(GTEST_OS_OPENBSD) ||     \
0708     defined(GTEST_OS_GNU_HURD) || defined(GTEST_OS_MAC)
0709 #define GTEST_CAN_STREAM_RESULTS_ 1
0710 #else
0711 #define GTEST_CAN_STREAM_RESULTS_ 0
0712 #endif
0713 
0714 // Defines some utility macros.
0715 
0716 // The GNU compiler emits a warning if nested "if" statements are followed by
0717 // an "else" statement and braces are not used to explicitly disambiguate the
0718 // "else" binding.  This leads to problems with code like:
0719 //
0720 //   if (gate)
0721 //     ASSERT_*(condition) << "Some message";
0722 //
0723 // The "switch (0) case 0:" idiom is used to suppress this.
0724 #ifdef __INTEL_COMPILER
0725 #define GTEST_AMBIGUOUS_ELSE_BLOCKER_
0726 #else
0727 #define GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
0728   switch (0)                          \
0729   case 0:                             \
0730   default:  // NOLINT
0731 #endif
0732 
0733 // GTEST_HAVE_ATTRIBUTE_
0734 //
0735 // A function-like feature checking macro that is a wrapper around
0736 // `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a
0737 // nonzero constant integer if the attribute is supported or 0 if not.
0738 //
0739 // It evaluates to zero if `__has_attribute` is not defined by the compiler.
0740 //
0741 // GCC: https://gcc.gnu.org/gcc-5/changes.html
0742 // Clang: https://clang.llvm.org/docs/LanguageExtensions.html
0743 #ifdef __has_attribute
0744 #define GTEST_HAVE_ATTRIBUTE_(x) __has_attribute(x)
0745 #else
0746 #define GTEST_HAVE_ATTRIBUTE_(x) 0
0747 #endif
0748 
0749 // GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE
0750 //
0751 // A function-like feature checking macro that accepts C++11 style attributes.
0752 // It's a wrapper around `__has_cpp_attribute`, defined by ISO C++ SD-6
0753 // (https://en.cppreference.com/w/cpp/experimental/feature_test). If we don't
0754 // find `__has_cpp_attribute`, will evaluate to 0.
0755 #if defined(__has_cpp_attribute)
0756 // NOTE: requiring __cplusplus above should not be necessary, but
0757 // works around https://bugs.llvm.org/show_bug.cgi?id=23435.
0758 #define GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
0759 #else
0760 #define GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(x) 0
0761 #endif
0762 
0763 // GTEST_HAVE_FEATURE_
0764 //
0765 // A function-like feature checking macro that is a wrapper around
0766 // `__has_feature`.
0767 #ifdef __has_feature
0768 #define GTEST_HAVE_FEATURE_(x) __has_feature(x)
0769 #else
0770 #define GTEST_HAVE_FEATURE_(x) 0
0771 #endif
0772 
0773 // Use this annotation before a function that takes a printf format string.
0774 #if GTEST_HAVE_ATTRIBUTE_(format) && defined(__MINGW_PRINTF_FORMAT)
0775 // MinGW has two different printf implementations. Ensure the format macro
0776 // matches the selected implementation. See
0777 // https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/.
0778 #define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \
0779   __attribute__((format(__MINGW_PRINTF_FORMAT, string_index, first_to_check)))
0780 #elif GTEST_HAVE_ATTRIBUTE_(format)
0781 #define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \
0782   __attribute__((format(printf, string_index, first_to_check)))
0783 #else
0784 #define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check)
0785 #endif
0786 
0787 // MS C++ compiler emits warning when a conditional expression is compile time
0788 // constant. In some contexts this warning is false positive and needs to be
0789 // suppressed. Use the following two macros in such cases:
0790 //
0791 // GTEST_INTENTIONAL_CONST_COND_PUSH_()
0792 // while (true) {
0793 // GTEST_INTENTIONAL_CONST_COND_POP_()
0794 // }
0795 #define GTEST_INTENTIONAL_CONST_COND_PUSH_() \
0796   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127)
0797 #define GTEST_INTENTIONAL_CONST_COND_POP_() GTEST_DISABLE_MSC_WARNINGS_POP_()
0798 
0799 // Determine whether the compiler supports Microsoft's Structured Exception
0800 // Handling.  This is supported by several Windows compilers but generally
0801 // does not exist on any other system.
0802 #ifndef GTEST_HAS_SEH
0803 // The user didn't tell us, so we need to figure it out.
0804 
0805 #if defined(_MSC_VER) || defined(__BORLANDC__)
0806 // These two compilers are known to support SEH.
0807 #define GTEST_HAS_SEH 1
0808 #else
0809 // Assume no SEH.
0810 #define GTEST_HAS_SEH 0
0811 #endif
0812 
0813 #endif  // GTEST_HAS_SEH
0814 
0815 #ifndef GTEST_IS_THREADSAFE
0816 
0817 #if (GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ ||                              \
0818      (defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_PHONE) && \
0819       !defined(GTEST_OS_WINDOWS_RT)) ||                                \
0820      GTEST_HAS_PTHREAD)
0821 #define GTEST_IS_THREADSAFE 1
0822 #endif
0823 
0824 #endif  // GTEST_IS_THREADSAFE
0825 
0826 #ifdef GTEST_IS_THREADSAFE
0827 // Some platforms don't support including these threading related headers.
0828 #include <condition_variable>  // NOLINT
0829 #include <mutex>               // NOLINT
0830 #endif                         // GTEST_IS_THREADSAFE
0831 
0832 // GTEST_API_ qualifies all symbols that must be exported. The definitions below
0833 // are guarded by #ifndef to give embedders a chance to define GTEST_API_ in
0834 // gtest/internal/custom/gtest-port.h
0835 #ifndef GTEST_API_
0836 
0837 #ifdef _MSC_VER
0838 #if defined(GTEST_LINKED_AS_SHARED_LIBRARY) && GTEST_LINKED_AS_SHARED_LIBRARY
0839 #define GTEST_API_ __declspec(dllimport)
0840 #elif defined(GTEST_CREATE_SHARED_LIBRARY) && GTEST_CREATE_SHARED_LIBRARY
0841 #define GTEST_API_ __declspec(dllexport)
0842 #endif
0843 #elif GTEST_HAVE_ATTRIBUTE_(visibility)
0844 #define GTEST_API_ __attribute__((visibility("default")))
0845 #endif  // _MSC_VER
0846 
0847 #endif  // GTEST_API_
0848 
0849 #ifndef GTEST_API_
0850 #define GTEST_API_
0851 #endif  // GTEST_API_
0852 
0853 #ifndef GTEST_DEFAULT_DEATH_TEST_STYLE
0854 #define GTEST_DEFAULT_DEATH_TEST_STYLE "fast"
0855 #endif  // GTEST_DEFAULT_DEATH_TEST_STYLE
0856 
0857 #if GTEST_HAVE_ATTRIBUTE_(noinline)
0858 // Ask the compiler to never inline a given function.
0859 #define GTEST_NO_INLINE_ __attribute__((noinline))
0860 #else
0861 #define GTEST_NO_INLINE_
0862 #endif
0863 
0864 #if GTEST_HAVE_ATTRIBUTE_(disable_tail_calls)
0865 // Ask the compiler not to perform tail call optimization inside
0866 // the marked function.
0867 #define GTEST_NO_TAIL_CALL_ __attribute__((disable_tail_calls))
0868 #elif defined(__GNUC__) && !defined(__NVCOMPILER)
0869 #define GTEST_NO_TAIL_CALL_ \
0870   __attribute__((optimize("no-optimize-sibling-calls")))
0871 #else
0872 #define GTEST_NO_TAIL_CALL_
0873 #endif
0874 
0875 // _LIBCPP_VERSION is defined by the libc++ library from the LLVM project.
0876 #if !defined(GTEST_HAS_CXXABI_H_)
0877 #if defined(__GLIBCXX__) || (defined(_LIBCPP_VERSION) && !defined(_MSC_VER))
0878 #define GTEST_HAS_CXXABI_H_ 1
0879 #else
0880 #define GTEST_HAS_CXXABI_H_ 0
0881 #endif
0882 #endif
0883 
0884 // A function level attribute to disable checking for use of uninitialized
0885 // memory when built with MemorySanitizer.
0886 #if GTEST_HAVE_ATTRIBUTE_(no_sanitize_memory)
0887 #define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ __attribute__((no_sanitize_memory))
0888 #else
0889 #define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
0890 #endif
0891 
0892 // A function level attribute to disable AddressSanitizer instrumentation.
0893 #if GTEST_HAVE_ATTRIBUTE_(no_sanitize_address)
0894 #define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ \
0895   __attribute__((no_sanitize_address))
0896 #else
0897 #define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
0898 #endif
0899 
0900 // A function level attribute to disable HWAddressSanitizer instrumentation.
0901 #if GTEST_HAVE_FEATURE_(hwaddress_sanitizer) && \
0902     GTEST_HAVE_ATTRIBUTE_(no_sanitize)
0903 #define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ \
0904   __attribute__((no_sanitize("hwaddress")))
0905 #else
0906 #define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_
0907 #endif
0908 
0909 // A function level attribute to disable ThreadSanitizer instrumentation.
0910 #if GTEST_HAVE_ATTRIBUTE_(no_sanitize_thread)
0911 #define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ __attribute((no_sanitize_thread))
0912 #else
0913 #define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
0914 #endif
0915 
0916 namespace testing {
0917 
0918 class Message;
0919 
0920 // Legacy imports for backwards compatibility.
0921 // New code should use std:: names directly.
0922 using std::get;
0923 using std::make_tuple;
0924 using std::tuple;
0925 using std::tuple_element;
0926 using std::tuple_size;
0927 
0928 namespace internal {
0929 
0930 // A secret type that Google Test users don't know about.  It has no
0931 // accessible constructors on purpose.  Therefore it's impossible to create a
0932 // Secret object, which is what we want.
0933 class Secret {
0934   Secret(const Secret&) = delete;
0935 };
0936 
0937 // A helper for suppressing warnings on constant condition.  It just
0938 // returns 'condition'.
0939 GTEST_API_ bool IsTrue(bool condition);
0940 
0941 // Defines RE.
0942 
0943 #ifdef GTEST_USES_RE2
0944 
0945 // This is almost `using RE = ::RE2`, except it is copy-constructible, and it
0946 // needs to disambiguate the `std::string`, `absl::string_view`, and `const
0947 // char*` constructors.
0948 class GTEST_API_ RE {
0949  public:
0950   RE(absl::string_view regex) : regex_(regex) {}                  // NOLINT
0951   RE(const char* regex) : RE(absl::string_view(regex)) {}         // NOLINT
0952   RE(const std::string& regex) : RE(absl::string_view(regex)) {}  // NOLINT
0953   RE(const RE& other) : RE(other.pattern()) {}
0954 
0955   const std::string& pattern() const { return regex_.pattern(); }
0956 
0957   static bool FullMatch(absl::string_view str, const RE& re) {
0958     return RE2::FullMatch(str, re.regex_);
0959   }
0960   static bool PartialMatch(absl::string_view str, const RE& re) {
0961     return RE2::PartialMatch(str, re.regex_);
0962   }
0963 
0964  private:
0965   RE2 regex_;
0966 };
0967 
0968 #elif defined(GTEST_USES_POSIX_RE) || defined(GTEST_USES_SIMPLE_RE)
0969 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
0970 /* class A needs to have dll-interface to be used by clients of class B */)
0971 
0972 // A simple C++ wrapper for <regex.h>.  It uses the POSIX Extended
0973 // Regular Expression syntax.
0974 class GTEST_API_ RE {
0975  public:
0976   // A copy constructor is required by the Standard to initialize object
0977   // references from r-values.
0978   RE(const RE& other) { Init(other.pattern()); }
0979 
0980   // Constructs an RE from a string.
0981   RE(const ::std::string& regex) { Init(regex.c_str()); }  // NOLINT
0982 
0983   RE(const char* regex) { Init(regex); }  // NOLINT
0984   ~RE();
0985 
0986   // Returns the string representation of the regex.
0987   const char* pattern() const { return pattern_.c_str(); }
0988 
0989   // FullMatch(str, re) returns true if and only if regular expression re
0990   // matches the entire str.
0991   // PartialMatch(str, re) returns true if and only if regular expression re
0992   // matches a substring of str (including str itself).
0993   static bool FullMatch(const ::std::string& str, const RE& re) {
0994     return FullMatch(str.c_str(), re);
0995   }
0996   static bool PartialMatch(const ::std::string& str, const RE& re) {
0997     return PartialMatch(str.c_str(), re);
0998   }
0999 
1000   static bool FullMatch(const char* str, const RE& re);
1001   static bool PartialMatch(const char* str, const RE& re);
1002 
1003  private:
1004   void Init(const char* regex);
1005   std::string pattern_;
1006   bool is_valid_;
1007 
1008 #ifdef GTEST_USES_POSIX_RE
1009 
1010   regex_t full_regex_;     // For FullMatch().
1011   regex_t partial_regex_;  // For PartialMatch().
1012 
1013 #else  // GTEST_USES_SIMPLE_RE
1014 
1015   std::string full_pattern_;  // For FullMatch();
1016 
1017 #endif
1018 };
1019 GTEST_DISABLE_MSC_WARNINGS_POP_()  // 4251
1020 #endif  // ::testing::internal::RE implementation
1021 
1022 // Formats a source file path and a line number as they would appear
1023 // in an error message from the compiler used to compile this code.
1024 GTEST_API_ ::std::string FormatFileLocation(const char* file, int line);
1025 
1026 // Formats a file location for compiler-independent XML output.
1027 // Although this function is not platform dependent, we put it next to
1028 // FormatFileLocation in order to contrast the two functions.
1029 GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file,
1030                                                                int line);
1031 
1032 // Defines logging utilities:
1033 //   GTEST_LOG_(severity) - logs messages at the specified severity level. The
1034 //                          message itself is streamed into the macro.
1035 //   LogToStderr()  - directs all log messages to stderr.
1036 //   FlushInfoLog() - flushes informational log messages.
1037 
1038 enum GTestLogSeverity { GTEST_INFO, GTEST_WARNING, GTEST_ERROR, GTEST_FATAL };
1039 
1040 // Formats log entry severity, provides a stream object for streaming the
1041 // log message, and terminates the message with a newline when going out of
1042 // scope.
1043 class GTEST_API_ GTestLog {
1044  public:
1045   GTestLog(GTestLogSeverity severity, const char* file, int line);
1046 
1047   // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
1048   ~GTestLog();
1049 
1050   ::std::ostream& GetStream() { return ::std::cerr; }
1051 
1052  private:
1053   const GTestLogSeverity severity_;
1054 
1055   GTestLog(const GTestLog&) = delete;
1056   GTestLog& operator=(const GTestLog&) = delete;
1057 };
1058 
1059 #if !defined(GTEST_LOG_)
1060 
1061 #define GTEST_LOG_(severity)                                           \
1062   ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
1063                                 __FILE__, __LINE__)                    \
1064       .GetStream()
1065 
1066 inline void LogToStderr() {}
1067 inline void FlushInfoLog() { fflush(nullptr); }
1068 
1069 #endif  // !defined(GTEST_LOG_)
1070 
1071 #if !defined(GTEST_CHECK_)
1072 // INTERNAL IMPLEMENTATION - DO NOT USE.
1073 //
1074 // GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition
1075 // is not satisfied.
1076 //  Synopsis:
1077 //    GTEST_CHECK_(boolean_condition);
1078 //     or
1079 //    GTEST_CHECK_(boolean_condition) << "Additional message";
1080 //
1081 //    This checks the condition and if the condition is not satisfied
1082 //    it prints message about the condition violation, including the
1083 //    condition itself, plus additional message streamed into it, if any,
1084 //    and then it aborts the program. It aborts the program irrespective of
1085 //    whether it is built in the debug mode or not.
1086 #define GTEST_CHECK_(condition)               \
1087   GTEST_AMBIGUOUS_ELSE_BLOCKER_               \
1088   if (::testing::internal::IsTrue(condition)) \
1089     ;                                         \
1090   else                                        \
1091     GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
1092 #endif  // !defined(GTEST_CHECK_)
1093 
1094 // An all-mode assert to verify that the given POSIX-style function
1095 // call returns 0 (indicating success).  Known limitation: this
1096 // doesn't expand to a balanced 'if' statement, so enclose the macro
1097 // in {} if you need to use it as the only statement in an 'if'
1098 // branch.
1099 #define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \
1100   if (const int gtest_error = (posix_call))    \
1101   GTEST_LOG_(FATAL) << #posix_call << "failed with error " << gtest_error
1102 
1103 // Transforms "T" into "const T&" according to standard reference collapsing
1104 // rules (this is only needed as a backport for C++98 compilers that do not
1105 // support reference collapsing). Specifically, it transforms:
1106 //
1107 //   char         ==> const char&
1108 //   const char   ==> const char&
1109 //   char&        ==> char&
1110 //   const char&  ==> const char&
1111 //
1112 // Note that the non-const reference will not have "const" added. This is
1113 // standard, and necessary so that "T" can always bind to "const T&".
1114 template <typename T>
1115 struct ConstRef {
1116   typedef const T& type;
1117 };
1118 template <typename T>
1119 struct ConstRef<T&> {
1120   typedef T& type;
1121 };
1122 
1123 // The argument T must depend on some template parameters.
1124 #define GTEST_REFERENCE_TO_CONST_(T) \
1125   typename ::testing::internal::ConstRef<T>::type
1126 
1127 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
1128 //
1129 // Use ImplicitCast_ as a safe version of static_cast for upcasting in
1130 // the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a
1131 // const Foo*).  When you use ImplicitCast_, the compiler checks that
1132 // the cast is safe.  Such explicit ImplicitCast_s are necessary in
1133 // surprisingly many situations where C++ demands an exact type match
1134 // instead of an argument type convertible to a target type.
1135 //
1136 // The syntax for using ImplicitCast_ is the same as for static_cast:
1137 //
1138 //   ImplicitCast_<ToType>(expr)
1139 //
1140 // ImplicitCast_ would have been part of the C++ standard library,
1141 // but the proposal was submitted too late.  It will probably make
1142 // its way into the language in the future.
1143 //
1144 // This relatively ugly name is intentional. It prevents clashes with
1145 // similar functions users may have (e.g., implicit_cast). The internal
1146 // namespace alone is not enough because the function can be found by ADL.
1147 template <typename To>
1148 inline To ImplicitCast_(To x) {
1149   return x;
1150 }
1151 
1152 // Downcasts the pointer of type Base to Derived.
1153 // Derived must be a subclass of Base. The parameter MUST
1154 // point to a class of type Derived, not any subclass of it.
1155 // When RTTI is available, the function performs a runtime
1156 // check to enforce this.
1157 template <class Derived, class Base>
1158 Derived* CheckedDowncastToActualType(Base* base) {
1159   static_assert(std::is_base_of<Base, Derived>::value,
1160                 "target type not derived from source type");
1161 #if GTEST_HAS_RTTI
1162   GTEST_CHECK_(base == nullptr || dynamic_cast<Derived*>(base) != nullptr);
1163 #endif
1164   return static_cast<Derived*>(base);
1165 }
1166 
1167 #if GTEST_HAS_STREAM_REDIRECTION
1168 
1169 // Defines the stderr capturer:
1170 //   CaptureStdout     - starts capturing stdout.
1171 //   GetCapturedStdout - stops capturing stdout and returns the captured string.
1172 //   CaptureStderr     - starts capturing stderr.
1173 //   GetCapturedStderr - stops capturing stderr and returns the captured string.
1174 //
1175 GTEST_API_ void CaptureStdout();
1176 GTEST_API_ std::string GetCapturedStdout();
1177 GTEST_API_ void CaptureStderr();
1178 GTEST_API_ std::string GetCapturedStderr();
1179 
1180 #endif  // GTEST_HAS_STREAM_REDIRECTION
1181 // Returns the size (in bytes) of a file.
1182 GTEST_API_ size_t GetFileSize(FILE* file);
1183 
1184 // Reads the entire content of a file as a string.
1185 GTEST_API_ std::string ReadEntireFile(FILE* file);
1186 
1187 // All command line arguments.
1188 GTEST_API_ std::vector<std::string> GetArgvs();
1189 
1190 #ifdef GTEST_HAS_DEATH_TEST
1191 
1192 std::vector<std::string> GetInjectableArgvs();
1193 // Deprecated: pass the args vector by value instead.
1194 void SetInjectableArgvs(const std::vector<std::string>* new_argvs);
1195 void SetInjectableArgvs(const std::vector<std::string>& new_argvs);
1196 void ClearInjectableArgvs();
1197 
1198 #endif  // GTEST_HAS_DEATH_TEST
1199 
1200 // Defines synchronization primitives.
1201 #ifdef GTEST_IS_THREADSAFE
1202 
1203 #ifdef GTEST_OS_WINDOWS
1204 // Provides leak-safe Windows kernel handle ownership.
1205 // Used in death tests and in threading support.
1206 class GTEST_API_ AutoHandle {
1207  public:
1208   // Assume that Win32 HANDLE type is equivalent to void*. Doing so allows us to
1209   // avoid including <windows.h> in this header file. Including <windows.h> is
1210   // undesirable because it defines a lot of symbols and macros that tend to
1211   // conflict with client code. This assumption is verified by
1212   // WindowsTypesTest.HANDLEIsVoidStar.
1213   typedef void* Handle;
1214   AutoHandle();
1215   explicit AutoHandle(Handle handle);
1216 
1217   ~AutoHandle();
1218 
1219   Handle Get() const;
1220   void Reset();
1221   void Reset(Handle handle);
1222 
1223  private:
1224   // Returns true if and only if the handle is a valid handle object that can be
1225   // closed.
1226   bool IsCloseable() const;
1227 
1228   Handle handle_;
1229 
1230   AutoHandle(const AutoHandle&) = delete;
1231   AutoHandle& operator=(const AutoHandle&) = delete;
1232 };
1233 #endif
1234 
1235 #if GTEST_HAS_NOTIFICATION_
1236 // Notification has already been imported into the namespace.
1237 // Nothing to do here.
1238 
1239 #else
1240 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
1241 /* class A needs to have dll-interface to be used by clients of class B */)
1242 
1243 // Allows a controller thread to pause execution of newly created
1244 // threads until notified.  Instances of this class must be created
1245 // and destroyed in the controller thread.
1246 //
1247 // This class is only for testing Google Test's own constructs. Do not
1248 // use it in user tests, either directly or indirectly.
1249 // TODO(b/203539622): Replace unconditionally with absl::Notification.
1250 class GTEST_API_ Notification {
1251  public:
1252   Notification() : notified_(false) {}
1253   Notification(const Notification&) = delete;
1254   Notification& operator=(const Notification&) = delete;
1255 
1256   // Notifies all threads created with this notification to start. Must
1257   // be called from the controller thread.
1258   void Notify() {
1259     std::lock_guard<std::mutex> lock(mu_);
1260     notified_ = true;
1261     cv_.notify_all();
1262   }
1263 
1264   // Blocks until the controller thread notifies. Must be called from a test
1265   // thread.
1266   void WaitForNotification() {
1267     std::unique_lock<std::mutex> lock(mu_);
1268     cv_.wait(lock, [this]() { return notified_; });
1269   }
1270 
1271  private:
1272   std::mutex mu_;
1273   std::condition_variable cv_;
1274   bool notified_;
1275 };
1276 GTEST_DISABLE_MSC_WARNINGS_POP_()  // 4251
1277 #endif  // GTEST_HAS_NOTIFICATION_
1278 
1279 // On MinGW, we can have both GTEST_OS_WINDOWS and GTEST_HAS_PTHREAD
1280 // defined, but we don't want to use MinGW's pthreads implementation, which
1281 // has conformance problems with some versions of the POSIX standard.
1282 #if GTEST_HAS_PTHREAD && !defined(GTEST_OS_WINDOWS_MINGW)
1283 
1284 // As a C-function, ThreadFuncWithCLinkage cannot be templated itself.
1285 // Consequently, it cannot select a correct instantiation of ThreadWithParam
1286 // in order to call its Run(). Introducing ThreadWithParamBase as a
1287 // non-templated base class for ThreadWithParam allows us to bypass this
1288 // problem.
1289 class ThreadWithParamBase {
1290  public:
1291   virtual ~ThreadWithParamBase() = default;
1292   virtual void Run() = 0;
1293 };
1294 
1295 // pthread_create() accepts a pointer to a function type with the C linkage.
1296 // According to the Standard (7.5/1), function types with different linkages
1297 // are different even if they are otherwise identical.  Some compilers (for
1298 // example, SunStudio) treat them as different types.  Since class methods
1299 // cannot be defined with C-linkage we need to define a free C-function to
1300 // pass into pthread_create().
1301 extern "C" inline void* ThreadFuncWithCLinkage(void* thread) {
1302   static_cast<ThreadWithParamBase*>(thread)->Run();
1303   return nullptr;
1304 }
1305 
1306 // Helper class for testing Google Test's multi-threading constructs.
1307 // To use it, write:
1308 //
1309 //   void ThreadFunc(int param) { /* Do things with param */ }
1310 //   Notification thread_can_start;
1311 //   ...
1312 //   // The thread_can_start parameter is optional; you can supply NULL.
1313 //   ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start);
1314 //   thread_can_start.Notify();
1315 //
1316 // These classes are only for testing Google Test's own constructs. Do
1317 // not use them in user tests, either directly or indirectly.
1318 template <typename T>
1319 class ThreadWithParam : public ThreadWithParamBase {
1320  public:
1321   typedef void UserThreadFunc(T);
1322 
1323   ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start)
1324       : func_(func),
1325         param_(param),
1326         thread_can_start_(thread_can_start),
1327         finished_(false) {
1328     ThreadWithParamBase* const base = this;
1329     // The thread can be created only after all fields except thread_
1330     // have been initialized.
1331     GTEST_CHECK_POSIX_SUCCESS_(
1332         pthread_create(&thread_, nullptr, &ThreadFuncWithCLinkage, base));
1333   }
1334   ~ThreadWithParam() override { Join(); }
1335 
1336   void Join() {
1337     if (!finished_) {
1338       GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, nullptr));
1339       finished_ = true;
1340     }
1341   }
1342 
1343   void Run() override {
1344     if (thread_can_start_ != nullptr) thread_can_start_->WaitForNotification();
1345     func_(param_);
1346   }
1347 
1348  private:
1349   UserThreadFunc* const func_;  // User-supplied thread function.
1350   const T param_;  // User-supplied parameter to the thread function.
1351   // When non-NULL, used to block execution until the controller thread
1352   // notifies.
1353   Notification* const thread_can_start_;
1354   bool finished_;  // true if and only if we know that the thread function has
1355                    // finished.
1356   pthread_t thread_;  // The native thread object.
1357 
1358   ThreadWithParam(const ThreadWithParam&) = delete;
1359   ThreadWithParam& operator=(const ThreadWithParam&) = delete;
1360 };
1361 #endif  // !GTEST_OS_WINDOWS && GTEST_HAS_PTHREAD ||
1362         // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
1363 
1364 #if GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
1365 // Mutex and ThreadLocal have already been imported into the namespace.
1366 // Nothing to do here.
1367 
1368 #elif defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_PHONE) && \
1369     !defined(GTEST_OS_WINDOWS_RT)
1370 
1371 // Mutex implements mutex on Windows platforms.  It is used in conjunction
1372 // with class MutexLock:
1373 //
1374 //   Mutex mutex;
1375 //   ...
1376 //   MutexLock lock(&mutex);  // Acquires the mutex and releases it at the
1377 //                            // end of the current scope.
1378 //
1379 // A static Mutex *must* be defined or declared using one of the following
1380 // macros:
1381 //   GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex);
1382 //   GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex);
1383 //
1384 // (A non-static Mutex is defined/declared in the usual way).
1385 class GTEST_API_ Mutex {
1386  public:
1387   enum MutexType { kStatic = 0, kDynamic = 1 };
1388   // We rely on kStaticMutex being 0 as it is to what the linker initializes
1389   // type_ in static mutexes.  critical_section_ will be initialized lazily
1390   // in ThreadSafeLazyInit().
1391   enum StaticConstructorSelector { kStaticMutex = 0 };
1392 
1393   // This constructor intentionally does nothing.  It relies on type_ being
1394   // statically initialized to 0 (effectively setting it to kStatic) and on
1395   // ThreadSafeLazyInit() to lazily initialize the rest of the members.
1396   explicit Mutex(StaticConstructorSelector /*dummy*/) {}
1397 
1398   Mutex();
1399   ~Mutex();
1400 
1401   void Lock();
1402 
1403   void Unlock();
1404 
1405   // Does nothing if the current thread holds the mutex. Otherwise, crashes
1406   // with high probability.
1407   void AssertHeld();
1408 
1409  private:
1410   // Initializes owner_thread_id_ and critical_section_ in static mutexes.
1411   void ThreadSafeLazyInit();
1412 
1413   // Per https://blogs.msdn.microsoft.com/oldnewthing/20040223-00/?p=40503,
1414   // we assume that 0 is an invalid value for thread IDs.
1415   unsigned int owner_thread_id_;
1416 
1417   // For static mutexes, we rely on these members being initialized to zeros
1418   // by the linker.
1419   MutexType type_;
1420   long critical_section_init_phase_;  // NOLINT
1421   GTEST_CRITICAL_SECTION* critical_section_;
1422 
1423   Mutex(const Mutex&) = delete;
1424   Mutex& operator=(const Mutex&) = delete;
1425 };
1426 
1427 #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1428   extern ::testing::internal::Mutex mutex
1429 
1430 #define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1431   ::testing::internal::Mutex mutex(::testing::internal::Mutex::kStaticMutex)
1432 
1433 // We cannot name this class MutexLock because the ctor declaration would
1434 // conflict with a macro named MutexLock, which is defined on some
1435 // platforms. That macro is used as a defensive measure to prevent against
1436 // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than
1437 // "MutexLock l(&mu)".  Hence the typedef trick below.
1438 class GTestMutexLock {
1439  public:
1440   explicit GTestMutexLock(Mutex* mutex) : mutex_(mutex) { mutex_->Lock(); }
1441 
1442   ~GTestMutexLock() { mutex_->Unlock(); }
1443 
1444  private:
1445   Mutex* const mutex_;
1446 
1447   GTestMutexLock(const GTestMutexLock&) = delete;
1448   GTestMutexLock& operator=(const GTestMutexLock&) = delete;
1449 };
1450 
1451 typedef GTestMutexLock MutexLock;
1452 
1453 // Base class for ValueHolder<T>.  Allows a caller to hold and delete a value
1454 // without knowing its type.
1455 class ThreadLocalValueHolderBase {
1456  public:
1457   virtual ~ThreadLocalValueHolderBase() {}
1458 };
1459 
1460 // Provides a way for a thread to send notifications to a ThreadLocal
1461 // regardless of its parameter type.
1462 class ThreadLocalBase {
1463  public:
1464   // Creates a new ValueHolder<T> object holding a default value passed to
1465   // this ThreadLocal<T>'s constructor and returns it.  It is the caller's
1466   // responsibility not to call this when the ThreadLocal<T> instance already
1467   // has a value on the current thread.
1468   virtual ThreadLocalValueHolderBase* NewValueForCurrentThread() const = 0;
1469 
1470  protected:
1471   ThreadLocalBase() {}
1472   virtual ~ThreadLocalBase() {}
1473 
1474  private:
1475   ThreadLocalBase(const ThreadLocalBase&) = delete;
1476   ThreadLocalBase& operator=(const ThreadLocalBase&) = delete;
1477 };
1478 
1479 // Maps a thread to a set of ThreadLocals that have values instantiated on that
1480 // thread and notifies them when the thread exits.  A ThreadLocal instance is
1481 // expected to persist until all threads it has values on have terminated.
1482 class GTEST_API_ ThreadLocalRegistry {
1483  public:
1484   // Registers thread_local_instance as having value on the current thread.
1485   // Returns a value that can be used to identify the thread from other threads.
1486   static ThreadLocalValueHolderBase* GetValueOnCurrentThread(
1487       const ThreadLocalBase* thread_local_instance);
1488 
1489   // Invoked when a ThreadLocal instance is destroyed.
1490   static void OnThreadLocalDestroyed(
1491       const ThreadLocalBase* thread_local_instance);
1492 };
1493 
1494 class GTEST_API_ ThreadWithParamBase {
1495  public:
1496   void Join();
1497 
1498  protected:
1499   class Runnable {
1500    public:
1501     virtual ~Runnable() {}
1502     virtual void Run() = 0;
1503   };
1504 
1505   ThreadWithParamBase(Runnable* runnable, Notification* thread_can_start);
1506   virtual ~ThreadWithParamBase();
1507 
1508  private:
1509   AutoHandle thread_;
1510 };
1511 
1512 // Helper class for testing Google Test's multi-threading constructs.
1513 template <typename T>
1514 class ThreadWithParam : public ThreadWithParamBase {
1515  public:
1516   typedef void UserThreadFunc(T);
1517 
1518   ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start)
1519       : ThreadWithParamBase(new RunnableImpl(func, param), thread_can_start) {}
1520   virtual ~ThreadWithParam() {}
1521 
1522  private:
1523   class RunnableImpl : public Runnable {
1524    public:
1525     RunnableImpl(UserThreadFunc* func, T param) : func_(func), param_(param) {}
1526     virtual ~RunnableImpl() {}
1527     virtual void Run() { func_(param_); }
1528 
1529    private:
1530     UserThreadFunc* const func_;
1531     const T param_;
1532 
1533     RunnableImpl(const RunnableImpl&) = delete;
1534     RunnableImpl& operator=(const RunnableImpl&) = delete;
1535   };
1536 
1537   ThreadWithParam(const ThreadWithParam&) = delete;
1538   ThreadWithParam& operator=(const ThreadWithParam&) = delete;
1539 };
1540 
1541 // Implements thread-local storage on Windows systems.
1542 //
1543 //   // Thread 1
1544 //   ThreadLocal<int> tl(100);  // 100 is the default value for each thread.
1545 //
1546 //   // Thread 2
1547 //   tl.set(150);  // Changes the value for thread 2 only.
1548 //   EXPECT_EQ(150, tl.get());
1549 //
1550 //   // Thread 1
1551 //   EXPECT_EQ(100, tl.get());  // In thread 1, tl has the original value.
1552 //   tl.set(200);
1553 //   EXPECT_EQ(200, tl.get());
1554 //
1555 // The template type argument T must have a public copy constructor.
1556 // In addition, the default ThreadLocal constructor requires T to have
1557 // a public default constructor.
1558 //
1559 // The users of a TheadLocal instance have to make sure that all but one
1560 // threads (including the main one) using that instance have exited before
1561 // destroying it. Otherwise, the per-thread objects managed for them by the
1562 // ThreadLocal instance are not guaranteed to be destroyed on all platforms.
1563 //
1564 // Google Test only uses global ThreadLocal objects.  That means they
1565 // will die after main() has returned.  Therefore, no per-thread
1566 // object managed by Google Test will be leaked as long as all threads
1567 // using Google Test have exited when main() returns.
1568 template <typename T>
1569 class ThreadLocal : public ThreadLocalBase {
1570  public:
1571   ThreadLocal() : default_factory_(new DefaultValueHolderFactory()) {}
1572   explicit ThreadLocal(const T& value)
1573       : default_factory_(new InstanceValueHolderFactory(value)) {}
1574 
1575   ~ThreadLocal() override { ThreadLocalRegistry::OnThreadLocalDestroyed(this); }
1576 
1577   T* pointer() { return GetOrCreateValue(); }
1578   const T* pointer() const { return GetOrCreateValue(); }
1579   const T& get() const { return *pointer(); }
1580   void set(const T& value) { *pointer() = value; }
1581 
1582  private:
1583   // Holds a value of T.  Can be deleted via its base class without the caller
1584   // knowing the type of T.
1585   class ValueHolder : public ThreadLocalValueHolderBase {
1586    public:
1587     ValueHolder() : value_() {}
1588     explicit ValueHolder(const T& value) : value_(value) {}
1589 
1590     T* pointer() { return &value_; }
1591 
1592    private:
1593     T value_;
1594     ValueHolder(const ValueHolder&) = delete;
1595     ValueHolder& operator=(const ValueHolder&) = delete;
1596   };
1597 
1598   T* GetOrCreateValue() const {
1599     return static_cast<ValueHolder*>(
1600                ThreadLocalRegistry::GetValueOnCurrentThread(this))
1601         ->pointer();
1602   }
1603 
1604   ThreadLocalValueHolderBase* NewValueForCurrentThread() const override {
1605     return default_factory_->MakeNewHolder();
1606   }
1607 
1608   class ValueHolderFactory {
1609    public:
1610     ValueHolderFactory() {}
1611     virtual ~ValueHolderFactory() {}
1612     virtual ValueHolder* MakeNewHolder() const = 0;
1613 
1614    private:
1615     ValueHolderFactory(const ValueHolderFactory&) = delete;
1616     ValueHolderFactory& operator=(const ValueHolderFactory&) = delete;
1617   };
1618 
1619   class DefaultValueHolderFactory : public ValueHolderFactory {
1620    public:
1621     DefaultValueHolderFactory() {}
1622     ValueHolder* MakeNewHolder() const override { return new ValueHolder(); }
1623 
1624    private:
1625     DefaultValueHolderFactory(const DefaultValueHolderFactory&) = delete;
1626     DefaultValueHolderFactory& operator=(const DefaultValueHolderFactory&) =
1627         delete;
1628   };
1629 
1630   class InstanceValueHolderFactory : public ValueHolderFactory {
1631    public:
1632     explicit InstanceValueHolderFactory(const T& value) : value_(value) {}
1633     ValueHolder* MakeNewHolder() const override {
1634       return new ValueHolder(value_);
1635     }
1636 
1637    private:
1638     const T value_;  // The value for each thread.
1639 
1640     InstanceValueHolderFactory(const InstanceValueHolderFactory&) = delete;
1641     InstanceValueHolderFactory& operator=(const InstanceValueHolderFactory&) =
1642         delete;
1643   };
1644 
1645   std::unique_ptr<ValueHolderFactory> default_factory_;
1646 
1647   ThreadLocal(const ThreadLocal&) = delete;
1648   ThreadLocal& operator=(const ThreadLocal&) = delete;
1649 };
1650 
1651 #elif GTEST_HAS_PTHREAD
1652 
1653 // MutexBase and Mutex implement mutex on pthreads-based platforms.
1654 class MutexBase {
1655  public:
1656   // Acquires this mutex.
1657   void Lock() {
1658     GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_));
1659     owner_ = pthread_self();
1660     has_owner_ = true;
1661   }
1662 
1663   // Releases this mutex.
1664   void Unlock() {
1665     // Since the lock is being released the owner_ field should no longer be
1666     // considered valid. We don't protect writing to has_owner_ here, as it's
1667     // the caller's responsibility to ensure that the current thread holds the
1668     // mutex when this is called.
1669     has_owner_ = false;
1670     GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_));
1671   }
1672 
1673   // Does nothing if the current thread holds the mutex. Otherwise, crashes
1674   // with high probability.
1675   void AssertHeld() const {
1676     GTEST_CHECK_(has_owner_ && pthread_equal(owner_, pthread_self()))
1677         << "The current thread is not holding the mutex @" << this;
1678   }
1679 
1680   // A static mutex may be used before main() is entered.  It may even
1681   // be used before the dynamic initialization stage.  Therefore we
1682   // must be able to initialize a static mutex object at link time.
1683   // This means MutexBase has to be a POD and its member variables
1684   // have to be public.
1685  public:
1686   pthread_mutex_t mutex_;  // The underlying pthread mutex.
1687   // has_owner_ indicates whether the owner_ field below contains a valid thread
1688   // ID and is therefore safe to inspect (e.g., to use in pthread_equal()). All
1689   // accesses to the owner_ field should be protected by a check of this field.
1690   // An alternative might be to memset() owner_ to all zeros, but there's no
1691   // guarantee that a zero'd pthread_t is necessarily invalid or even different
1692   // from pthread_self().
1693   bool has_owner_;
1694   pthread_t owner_;  // The thread holding the mutex.
1695 };
1696 
1697 // Forward-declares a static mutex.
1698 #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1699   extern ::testing::internal::MutexBase mutex
1700 
1701 // Defines and statically (i.e. at link time) initializes a static mutex.
1702 // The initialization list here does not explicitly initialize each field,
1703 // instead relying on default initialization for the unspecified fields. In
1704 // particular, the owner_ field (a pthread_t) is not explicitly initialized.
1705 // This allows initialization to work whether pthread_t is a scalar or struct.
1706 // The flag -Wmissing-field-initializers must not be specified for this to work.
1707 #define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1708   ::testing::internal::MutexBase mutex = {PTHREAD_MUTEX_INITIALIZER, false, 0}
1709 
1710 // The Mutex class can only be used for mutexes created at runtime. It
1711 // shares its API with MutexBase otherwise.
1712 class Mutex : public MutexBase {
1713  public:
1714   Mutex() {
1715     GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, nullptr));
1716     has_owner_ = false;
1717   }
1718   ~Mutex() { GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_)); }
1719 
1720  private:
1721   Mutex(const Mutex&) = delete;
1722   Mutex& operator=(const Mutex&) = delete;
1723 };
1724 
1725 // We cannot name this class MutexLock because the ctor declaration would
1726 // conflict with a macro named MutexLock, which is defined on some
1727 // platforms. That macro is used as a defensive measure to prevent against
1728 // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than
1729 // "MutexLock l(&mu)".  Hence the typedef trick below.
1730 class GTestMutexLock {
1731  public:
1732   explicit GTestMutexLock(MutexBase* mutex) : mutex_(mutex) { mutex_->Lock(); }
1733 
1734   ~GTestMutexLock() { mutex_->Unlock(); }
1735 
1736  private:
1737   MutexBase* const mutex_;
1738 
1739   GTestMutexLock(const GTestMutexLock&) = delete;
1740   GTestMutexLock& operator=(const GTestMutexLock&) = delete;
1741 };
1742 
1743 typedef GTestMutexLock MutexLock;
1744 
1745 // Helpers for ThreadLocal.
1746 
1747 // pthread_key_create() requires DeleteThreadLocalValue() to have
1748 // C-linkage.  Therefore it cannot be templatized to access
1749 // ThreadLocal<T>.  Hence the need for class
1750 // ThreadLocalValueHolderBase.
1751 class GTEST_API_ ThreadLocalValueHolderBase {
1752  public:
1753   virtual ~ThreadLocalValueHolderBase() = default;
1754 };
1755 
1756 // Called by pthread to delete thread-local data stored by
1757 // pthread_setspecific().
1758 extern "C" inline void DeleteThreadLocalValue(void* value_holder) {
1759   delete static_cast<ThreadLocalValueHolderBase*>(value_holder);
1760 }
1761 
1762 // Implements thread-local storage on pthreads-based systems.
1763 template <typename T>
1764 class GTEST_API_ ThreadLocal {
1765  public:
1766   ThreadLocal()
1767       : key_(CreateKey()), default_factory_(new DefaultValueHolderFactory()) {}
1768   explicit ThreadLocal(const T& value)
1769       : key_(CreateKey()),
1770         default_factory_(new InstanceValueHolderFactory(value)) {}
1771 
1772   ~ThreadLocal() {
1773     // Destroys the managed object for the current thread, if any.
1774     DeleteThreadLocalValue(pthread_getspecific(key_));
1775 
1776     // Releases resources associated with the key.  This will *not*
1777     // delete managed objects for other threads.
1778     GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_));
1779   }
1780 
1781   T* pointer() { return GetOrCreateValue(); }
1782   const T* pointer() const { return GetOrCreateValue(); }
1783   const T& get() const { return *pointer(); }
1784   void set(const T& value) { *pointer() = value; }
1785 
1786  private:
1787   // Holds a value of type T.
1788   class ValueHolder : public ThreadLocalValueHolderBase {
1789    public:
1790     ValueHolder() : value_() {}
1791     explicit ValueHolder(const T& value) : value_(value) {}
1792 
1793     T* pointer() { return &value_; }
1794 
1795    private:
1796     T value_;
1797     ValueHolder(const ValueHolder&) = delete;
1798     ValueHolder& operator=(const ValueHolder&) = delete;
1799   };
1800 
1801   static pthread_key_t CreateKey() {
1802     pthread_key_t key;
1803     // When a thread exits, DeleteThreadLocalValue() will be called on
1804     // the object managed for that thread.
1805     GTEST_CHECK_POSIX_SUCCESS_(
1806         pthread_key_create(&key, &DeleteThreadLocalValue));
1807     return key;
1808   }
1809 
1810   T* GetOrCreateValue() const {
1811     ThreadLocalValueHolderBase* const holder =
1812         static_cast<ThreadLocalValueHolderBase*>(pthread_getspecific(key_));
1813     if (holder != nullptr) {
1814       return CheckedDowncastToActualType<ValueHolder>(holder)->pointer();
1815     }
1816 
1817     ValueHolder* const new_holder = default_factory_->MakeNewHolder();
1818     ThreadLocalValueHolderBase* const holder_base = new_holder;
1819     GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base));
1820     return new_holder->pointer();
1821   }
1822 
1823   class ValueHolderFactory {
1824    public:
1825     ValueHolderFactory() = default;
1826     virtual ~ValueHolderFactory() = default;
1827     virtual ValueHolder* MakeNewHolder() const = 0;
1828 
1829    private:
1830     ValueHolderFactory(const ValueHolderFactory&) = delete;
1831     ValueHolderFactory& operator=(const ValueHolderFactory&) = delete;
1832   };
1833 
1834   class DefaultValueHolderFactory : public ValueHolderFactory {
1835    public:
1836     DefaultValueHolderFactory() = default;
1837     ValueHolder* MakeNewHolder() const override { return new ValueHolder(); }
1838 
1839    private:
1840     DefaultValueHolderFactory(const DefaultValueHolderFactory&) = delete;
1841     DefaultValueHolderFactory& operator=(const DefaultValueHolderFactory&) =
1842         delete;
1843   };
1844 
1845   class InstanceValueHolderFactory : public ValueHolderFactory {
1846    public:
1847     explicit InstanceValueHolderFactory(const T& value) : value_(value) {}
1848     ValueHolder* MakeNewHolder() const override {
1849       return new ValueHolder(value_);
1850     }
1851 
1852    private:
1853     const T value_;  // The value for each thread.
1854 
1855     InstanceValueHolderFactory(const InstanceValueHolderFactory&) = delete;
1856     InstanceValueHolderFactory& operator=(const InstanceValueHolderFactory&) =
1857         delete;
1858   };
1859 
1860   // A key pthreads uses for looking up per-thread values.
1861   const pthread_key_t key_;
1862   std::unique_ptr<ValueHolderFactory> default_factory_;
1863 
1864   ThreadLocal(const ThreadLocal&) = delete;
1865   ThreadLocal& operator=(const ThreadLocal&) = delete;
1866 };
1867 
1868 #endif  // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
1869 
1870 #else  // GTEST_IS_THREADSAFE
1871 
1872 // A dummy implementation of synchronization primitives (mutex, lock,
1873 // and thread-local variable).  Necessary for compiling Google Test where
1874 // mutex is not supported - using Google Test in multiple threads is not
1875 // supported on such platforms.
1876 
1877 class Mutex {
1878  public:
1879   Mutex() {}
1880   void Lock() {}
1881   void Unlock() {}
1882   void AssertHeld() const {}
1883 };
1884 
1885 #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1886   extern ::testing::internal::Mutex mutex
1887 
1888 #define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex
1889 
1890 // We cannot name this class MutexLock because the ctor declaration would
1891 // conflict with a macro named MutexLock, which is defined on some
1892 // platforms. That macro is used as a defensive measure to prevent against
1893 // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than
1894 // "MutexLock l(&mu)".  Hence the typedef trick below.
1895 class GTestMutexLock {
1896  public:
1897   explicit GTestMutexLock(Mutex*) {}  // NOLINT
1898 };
1899 
1900 typedef GTestMutexLock MutexLock;
1901 
1902 template <typename T>
1903 class GTEST_API_ ThreadLocal {
1904  public:
1905   ThreadLocal() : value_() {}
1906   explicit ThreadLocal(const T& value) : value_(value) {}
1907   T* pointer() { return &value_; }
1908   const T* pointer() const { return &value_; }
1909   const T& get() const { return value_; }
1910   void set(const T& value) { value_ = value; }
1911 
1912  private:
1913   T value_;
1914 };
1915 
1916 #endif  // GTEST_IS_THREADSAFE
1917 
1918 // Returns the number of threads running in the process, or 0 to indicate that
1919 // we cannot detect it.
1920 GTEST_API_ size_t GetThreadCount();
1921 
1922 #ifdef GTEST_OS_WINDOWS
1923 #define GTEST_PATH_SEP_ "\\"
1924 #define GTEST_HAS_ALT_PATH_SEP_ 1
1925 #else
1926 #define GTEST_PATH_SEP_ "/"
1927 #define GTEST_HAS_ALT_PATH_SEP_ 0
1928 #endif  // GTEST_OS_WINDOWS
1929 
1930 // Utilities for char.
1931 
1932 // isspace(int ch) and friends accept an unsigned char or EOF.  char
1933 // may be signed, depending on the compiler (or compiler flags).
1934 // Therefore we need to cast a char to unsigned char before calling
1935 // isspace(), etc.
1936 
1937 inline bool IsAlpha(char ch) {
1938   return isalpha(static_cast<unsigned char>(ch)) != 0;
1939 }
1940 inline bool IsAlNum(char ch) {
1941   return isalnum(static_cast<unsigned char>(ch)) != 0;
1942 }
1943 inline bool IsDigit(char ch) {
1944   return isdigit(static_cast<unsigned char>(ch)) != 0;
1945 }
1946 inline bool IsLower(char ch) {
1947   return islower(static_cast<unsigned char>(ch)) != 0;
1948 }
1949 inline bool IsSpace(char ch) {
1950   return isspace(static_cast<unsigned char>(ch)) != 0;
1951 }
1952 inline bool IsUpper(char ch) {
1953   return isupper(static_cast<unsigned char>(ch)) != 0;
1954 }
1955 inline bool IsXDigit(char ch) {
1956   return isxdigit(static_cast<unsigned char>(ch)) != 0;
1957 }
1958 #ifdef __cpp_lib_char8_t
1959 inline bool IsXDigit(char8_t ch) {
1960   return isxdigit(static_cast<unsigned char>(ch)) != 0;
1961 }
1962 #endif
1963 inline bool IsXDigit(char16_t ch) {
1964   const unsigned char low_byte = static_cast<unsigned char>(ch);
1965   return ch == low_byte && isxdigit(low_byte) != 0;
1966 }
1967 inline bool IsXDigit(char32_t ch) {
1968   const unsigned char low_byte = static_cast<unsigned char>(ch);
1969   return ch == low_byte && isxdigit(low_byte) != 0;
1970 }
1971 inline bool IsXDigit(wchar_t ch) {
1972   const unsigned char low_byte = static_cast<unsigned char>(ch);
1973   return ch == low_byte && isxdigit(low_byte) != 0;
1974 }
1975 
1976 inline char ToLower(char ch) {
1977   return static_cast<char>(tolower(static_cast<unsigned char>(ch)));
1978 }
1979 inline char ToUpper(char ch) {
1980   return static_cast<char>(toupper(static_cast<unsigned char>(ch)));
1981 }
1982 
1983 inline std::string StripTrailingSpaces(std::string str) {
1984   std::string::iterator it = str.end();
1985   while (it != str.begin() && IsSpace(*--it)) it = str.erase(it);
1986   return str;
1987 }
1988 
1989 // The testing::internal::posix namespace holds wrappers for common
1990 // POSIX functions.  These wrappers hide the differences between
1991 // Windows/MSVC and POSIX systems.  Since some compilers define these
1992 // standard functions as macros, the wrapper cannot have the same name
1993 // as the wrapped function.
1994 
1995 namespace posix {
1996 
1997 // File system porting.
1998 // Note: Not every I/O-related function is related to file systems, so don't
1999 // just disable all of them here. For example, fileno() and isatty(), etc. must
2000 // always be available in order to detect if a pipe points to a terminal.
2001 #ifdef GTEST_OS_WINDOWS
2002 
2003 typedef struct _stat StatStruct;
2004 
2005 #ifdef GTEST_OS_WINDOWS_MOBILE
2006 inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
2007 // Stat(), RmDir(), and IsDir() are not needed on Windows CE at this
2008 // time and thus not defined there.
2009 #else
2010 inline int FileNo(FILE* file) { return _fileno(file); }
2011 #if GTEST_HAS_FILE_SYSTEM
2012 inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
2013 inline int RmDir(const char* dir) { return _rmdir(dir); }
2014 inline bool IsDir(const StatStruct& st) { return (_S_IFDIR & st.st_mode) != 0; }
2015 #endif
2016 #endif  // GTEST_OS_WINDOWS_MOBILE
2017 
2018 #elif defined(GTEST_OS_ESP8266)
2019 typedef struct stat StatStruct;
2020 
2021 inline int FileNo(FILE* file) { return fileno(file); }
2022 #if GTEST_HAS_FILE_SYSTEM
2023 inline int Stat(const char* path, StatStruct* buf) {
2024   // stat function not implemented on ESP8266
2025   return 0;
2026 }
2027 inline int RmDir(const char* dir) { return rmdir(dir); }
2028 inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
2029 #endif
2030 
2031 #else
2032 
2033 typedef struct stat StatStruct;
2034 
2035 inline int FileNo(FILE* file) { return fileno(file); }
2036 #if GTEST_HAS_FILE_SYSTEM
2037 inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
2038 #ifdef GTEST_OS_QURT
2039 // QuRT doesn't support any directory functions, including rmdir
2040 inline int RmDir(const char*) { return 0; }
2041 #else
2042 inline int RmDir(const char* dir) { return rmdir(dir); }
2043 #endif
2044 inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
2045 #endif
2046 
2047 #endif  // GTEST_OS_WINDOWS
2048 
2049 // Other functions with a different name on Windows.
2050 
2051 #ifdef GTEST_OS_WINDOWS
2052 
2053 #ifdef __BORLANDC__
2054 inline int DoIsATTY(int fd) { return isatty(fd); }
2055 inline int StrCaseCmp(const char* s1, const char* s2) {
2056   return stricmp(s1, s2);
2057 }
2058 #else  // !__BORLANDC__
2059 #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_ZOS) || \
2060     defined(GTEST_OS_IOS) || defined(GTEST_OS_WINDOWS_PHONE) ||  \
2061     defined(GTEST_OS_WINDOWS_RT) || defined(ESP_PLATFORM)
2062 inline int DoIsATTY(int /* fd */) { return 0; }
2063 #else
2064 inline int DoIsATTY(int fd) { return _isatty(fd); }
2065 #endif  // GTEST_OS_WINDOWS_MOBILE
2066 inline int StrCaseCmp(const char* s1, const char* s2) {
2067   return _stricmp(s1, s2);
2068 }
2069 #endif  // __BORLANDC__
2070 
2071 #else
2072 
2073 inline int DoIsATTY(int fd) { return isatty(fd); }
2074 inline int StrCaseCmp(const char* s1, const char* s2) {
2075   return strcasecmp(s1, s2);
2076 }
2077 
2078 #endif  // GTEST_OS_WINDOWS
2079 
2080 inline int IsATTY(int fd) {
2081   // DoIsATTY might change errno (for example ENOTTY in case you redirect stdout
2082   // to a file on Linux), which is unexpected, so save the previous value, and
2083   // restore it after the call.
2084   int savedErrno = errno;
2085   int isAttyValue = DoIsATTY(fd);
2086   errno = savedErrno;
2087 
2088   return isAttyValue;
2089 }
2090 
2091 // Functions deprecated by MSVC 8.0.
2092 
2093 GTEST_DISABLE_MSC_DEPRECATED_PUSH_()
2094 
2095 // ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and
2096 // StrError() aren't needed on Windows CE at this time and thus not
2097 // defined there.
2098 #if GTEST_HAS_FILE_SYSTEM
2099 #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_WINDOWS_PHONE) && \
2100     !defined(GTEST_OS_WINDOWS_RT) && !defined(GTEST_OS_WINDOWS_GAMES) &&     \
2101     !defined(GTEST_OS_ESP8266) && !defined(GTEST_OS_XTENSA) &&               \
2102     !defined(GTEST_OS_QURT)
2103 inline int ChDir(const char* dir) { return chdir(dir); }
2104 #endif
2105 inline FILE* FOpen(const char* path, const char* mode) {
2106 #if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MINGW)
2107   struct wchar_codecvt : public std::codecvt<wchar_t, char, std::mbstate_t> {};
2108   std::wstring_convert<wchar_codecvt> converter;
2109   std::wstring wide_path = converter.from_bytes(path);
2110   std::wstring wide_mode = converter.from_bytes(mode);
2111   return _wfopen(wide_path.c_str(), wide_mode.c_str());
2112 #else   // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW
2113   return fopen(path, mode);
2114 #endif  // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW
2115 }
2116 #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT)
2117 inline FILE* FReopen(const char* path, const char* mode, FILE* stream) {
2118   return freopen(path, mode, stream);
2119 }
2120 inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
2121 #endif  // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
2122 inline int FClose(FILE* fp) { return fclose(fp); }
2123 #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT)
2124 inline int Read(int fd, void* buf, unsigned int count) {
2125   return static_cast<int>(read(fd, buf, count));
2126 }
2127 inline int Write(int fd, const void* buf, unsigned int count) {
2128   return static_cast<int>(write(fd, buf, count));
2129 }
2130 inline int Close(int fd) { return close(fd); }
2131 #endif  // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
2132 #endif  // GTEST_HAS_FILE_SYSTEM
2133 
2134 #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT)
2135 inline const char* StrError(int errnum) { return strerror(errnum); }
2136 #endif  // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
2137 
2138 inline const char* GetEnv(const char* name) {
2139 #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_WINDOWS_PHONE) || \
2140     defined(GTEST_OS_ESP8266) || defined(GTEST_OS_XTENSA) ||               \
2141     defined(GTEST_OS_QURT)
2142   // We are on an embedded platform, which has no environment variables.
2143   static_cast<void>(name);  // To prevent 'unused argument' warning.
2144   return nullptr;
2145 #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
2146   // Environment variables which we programmatically clear will be set to the
2147   // empty string rather than unset (NULL).  Handle that case.
2148   const char* const env = getenv(name);
2149   return (env != nullptr && env[0] != '\0') ? env : nullptr;
2150 #else
2151   return getenv(name);
2152 #endif
2153 }
2154 
2155 GTEST_DISABLE_MSC_DEPRECATED_POP_()
2156 
2157 #ifdef GTEST_OS_WINDOWS_MOBILE
2158 // Windows CE has no C library. The abort() function is used in
2159 // several places in Google Test. This implementation provides a reasonable
2160 // imitation of standard behaviour.
2161 [[noreturn]] void Abort();
2162 #else
2163 [[noreturn]] inline void Abort() { abort(); }
2164 #endif  // GTEST_OS_WINDOWS_MOBILE
2165 
2166 }  // namespace posix
2167 
2168 // MSVC "deprecates" snprintf and issues warnings wherever it is used.  In
2169 // order to avoid these warnings, we need to use _snprintf or _snprintf_s on
2170 // MSVC-based platforms.  We map the GTEST_SNPRINTF_ macro to the appropriate
2171 // function in order to achieve that.  We use macro definition here because
2172 // snprintf is a variadic function.
2173 #if defined(_MSC_VER) && !defined(GTEST_OS_WINDOWS_MOBILE)
2174 // MSVC 2005 and above support variadic macros.
2175 #define GTEST_SNPRINTF_(buffer, size, format, ...) \
2176   _snprintf_s(buffer, size, size, format, __VA_ARGS__)
2177 #elif defined(_MSC_VER)
2178 // Windows CE does not define _snprintf_s
2179 #define GTEST_SNPRINTF_ _snprintf
2180 #else
2181 #define GTEST_SNPRINTF_ snprintf
2182 #endif
2183 
2184 // The biggest signed integer type the compiler supports.
2185 //
2186 // long long is guaranteed to be at least 64-bits in C++11.
2187 using BiggestInt = long long;  // NOLINT
2188 
2189 // The maximum number a BiggestInt can represent.
2190 constexpr BiggestInt kMaxBiggestInt = (std::numeric_limits<BiggestInt>::max)();
2191 
2192 // This template class serves as a compile-time function from size to
2193 // type.  It maps a size in bytes to a primitive type with that
2194 // size. e.g.
2195 //
2196 //   TypeWithSize<4>::UInt
2197 //
2198 // is typedef-ed to be unsigned int (unsigned integer made up of 4
2199 // bytes).
2200 //
2201 // Such functionality should belong to STL, but I cannot find it
2202 // there.
2203 //
2204 // Google Test uses this class in the implementation of floating-point
2205 // comparison.
2206 //
2207 // For now it only handles UInt (unsigned int) as that's all Google Test
2208 // needs.  Other types can be easily added in the future if need
2209 // arises.
2210 template <size_t size>
2211 class TypeWithSize {
2212  public:
2213   // This prevents the user from using TypeWithSize<N> with incorrect
2214   // values of N.
2215   using UInt = void;
2216 };
2217 
2218 // The specialization for size 4.
2219 template <>
2220 class TypeWithSize<4> {
2221  public:
2222   using Int = std::int32_t;
2223   using UInt = std::uint32_t;
2224 };
2225 
2226 // The specialization for size 8.
2227 template <>
2228 class TypeWithSize<8> {
2229  public:
2230   using Int = std::int64_t;
2231   using UInt = std::uint64_t;
2232 };
2233 
2234 // Integer types of known sizes.
2235 using TimeInMillis = int64_t;  // Represents time in milliseconds.
2236 
2237 // Utilities for command line flags and environment variables.
2238 
2239 // Macro for referencing flags.
2240 #if !defined(GTEST_FLAG)
2241 #define GTEST_FLAG_NAME_(name) gtest_##name
2242 #define GTEST_FLAG(name) FLAGS_gtest_##name
2243 #endif  // !defined(GTEST_FLAG)
2244 
2245 // Pick a command line flags implementation.
2246 #ifdef GTEST_INTERNAL_HAS_ABSL_FLAGS
2247 
2248 // Macros for defining flags.
2249 #define GTEST_DEFINE_bool_(name, default_val, doc) \
2250   ABSL_FLAG(bool, GTEST_FLAG_NAME_(name), default_val, doc)
2251 #define GTEST_DEFINE_int32_(name, default_val, doc) \
2252   ABSL_FLAG(int32_t, GTEST_FLAG_NAME_(name), default_val, doc)
2253 #define GTEST_DEFINE_string_(name, default_val, doc) \
2254   ABSL_FLAG(std::string, GTEST_FLAG_NAME_(name), default_val, doc)
2255 
2256 // Macros for declaring flags.
2257 #define GTEST_DECLARE_bool_(name) \
2258   ABSL_DECLARE_FLAG(bool, GTEST_FLAG_NAME_(name))
2259 #define GTEST_DECLARE_int32_(name) \
2260   ABSL_DECLARE_FLAG(int32_t, GTEST_FLAG_NAME_(name))
2261 #define GTEST_DECLARE_string_(name) \
2262   ABSL_DECLARE_FLAG(std::string, GTEST_FLAG_NAME_(name))
2263 
2264 #define GTEST_FLAG_SAVER_ ::absl::FlagSaver
2265 
2266 #define GTEST_FLAG_GET(name) ::absl::GetFlag(GTEST_FLAG(name))
2267 #define GTEST_FLAG_SET(name, value) \
2268   (void)(::absl::SetFlag(&GTEST_FLAG(name), value))
2269 #define GTEST_USE_OWN_FLAGFILE_FLAG_ 0
2270 
2271 #undef GTEST_INTERNAL_HAS_ABSL_FLAGS
2272 #else  // ndef GTEST_INTERNAL_HAS_ABSL_FLAGS
2273 
2274 // Macros for defining flags.
2275 #define GTEST_DEFINE_bool_(name, default_val, doc)  \
2276   namespace testing {                               \
2277   GTEST_API_ bool GTEST_FLAG(name) = (default_val); \
2278   }                                                 \
2279   static_assert(true, "no-op to require trailing semicolon")
2280 #define GTEST_DEFINE_int32_(name, default_val, doc)         \
2281   namespace testing {                                       \
2282   GTEST_API_ std::int32_t GTEST_FLAG(name) = (default_val); \
2283   }                                                         \
2284   static_assert(true, "no-op to require trailing semicolon")
2285 #define GTEST_DEFINE_string_(name, default_val, doc)         \
2286   namespace testing {                                        \
2287   GTEST_API_ ::std::string GTEST_FLAG(name) = (default_val); \
2288   }                                                          \
2289   static_assert(true, "no-op to require trailing semicolon")
2290 
2291 // Macros for declaring flags.
2292 #define GTEST_DECLARE_bool_(name)          \
2293   namespace testing {                      \
2294   GTEST_API_ extern bool GTEST_FLAG(name); \
2295   }                                        \
2296   static_assert(true, "no-op to require trailing semicolon")
2297 #define GTEST_DECLARE_int32_(name)                 \
2298   namespace testing {                              \
2299   GTEST_API_ extern std::int32_t GTEST_FLAG(name); \
2300   }                                                \
2301   static_assert(true, "no-op to require trailing semicolon")
2302 #define GTEST_DECLARE_string_(name)                 \
2303   namespace testing {                               \
2304   GTEST_API_ extern ::std::string GTEST_FLAG(name); \
2305   }                                                 \
2306   static_assert(true, "no-op to require trailing semicolon")
2307 
2308 #define GTEST_FLAG_SAVER_ ::testing::internal::GTestFlagSaver
2309 
2310 #define GTEST_FLAG_GET(name) ::testing::GTEST_FLAG(name)
2311 #define GTEST_FLAG_SET(name, value) (void)(::testing::GTEST_FLAG(name) = value)
2312 #define GTEST_USE_OWN_FLAGFILE_FLAG_ 1
2313 
2314 #endif  // GTEST_INTERNAL_HAS_ABSL_FLAGS
2315 
2316 // Thread annotations
2317 #if !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)
2318 #define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
2319 #define GTEST_LOCK_EXCLUDED_(locks)
2320 #endif  // !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)
2321 
2322 // Parses 'str' for a 32-bit signed integer.  If successful, writes the result
2323 // to *value and returns true; otherwise leaves *value unchanged and returns
2324 // false.
2325 GTEST_API_ bool ParseInt32(const Message& src_text, const char* str,
2326                            int32_t* value);
2327 
2328 // Parses a bool/int32_t/string from the environment variable
2329 // corresponding to the given Google Test flag.
2330 bool BoolFromGTestEnv(const char* flag, bool default_val);
2331 GTEST_API_ int32_t Int32FromGTestEnv(const char* flag, int32_t default_val);
2332 std::string OutputFlagAlsoCheckEnvVar();
2333 const char* StringFromGTestEnv(const char* flag, const char* default_val);
2334 
2335 }  // namespace internal
2336 }  // namespace testing
2337 
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 
2405 #if defined(__cpp_lib_span) || (GTEST_INTERNAL_HAS_INCLUDE(<span>) && \
2406                                 GTEST_INTERNAL_CPLUSPLUS_LANG >= 202002L)
2407 #define GTEST_INTERNAL_HAS_STD_SPAN 1
2408 #endif  // __cpp_lib_span
2409 
2410 #ifndef GTEST_INTERNAL_HAS_STD_SPAN
2411 #define GTEST_INTERNAL_HAS_STD_SPAN 0
2412 #endif
2413 
2414 #ifdef GTEST_HAS_ABSL
2415 // Always use absl::string_view for Matcher<> specializations if googletest
2416 // is built with absl support.
2417 #define GTEST_INTERNAL_HAS_STRING_VIEW 1
2418 #include "absl/strings/string_view.h"
2419 namespace testing {
2420 namespace internal {
2421 using StringView = ::absl::string_view;
2422 }  // namespace internal
2423 }  // namespace testing
2424 #else
2425 #if defined(__cpp_lib_string_view) ||             \
2426     (GTEST_INTERNAL_HAS_INCLUDE(<string_view>) && \
2427      GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L)
2428 // Otherwise for C++17 and higher use std::string_view for Matcher<>
2429 // specializations.
2430 #define GTEST_INTERNAL_HAS_STRING_VIEW 1
2431 #include <string_view>
2432 namespace testing {
2433 namespace internal {
2434 using StringView = ::std::string_view;
2435 }  // namespace internal
2436 }  // namespace testing
2437 // The case where absl is configured NOT to alias std::string_view is not
2438 // supported.
2439 #endif  // __cpp_lib_string_view
2440 #endif  // GTEST_HAS_ABSL
2441 
2442 #ifndef GTEST_INTERNAL_HAS_STRING_VIEW
2443 #define GTEST_INTERNAL_HAS_STRING_VIEW 0
2444 #endif
2445 
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 
2478 #if (defined(__cpp_lib_three_way_comparison) || \
2479      (GTEST_INTERNAL_HAS_INCLUDE(<compare>) &&  \
2480       GTEST_INTERNAL_CPLUSPLUS_LANG >= 201907L))
2481 #define GTEST_INTERNAL_HAS_COMPARE_LIB 1
2482 #else
2483 #define GTEST_INTERNAL_HAS_COMPARE_LIB 0
2484 #endif
2485 
2486 #endif  // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_