1b89a7cc2SEnji Cooper // Copyright 2005, Google Inc. 2b89a7cc2SEnji Cooper // All rights reserved. 3b89a7cc2SEnji Cooper // 4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without 5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are 6b89a7cc2SEnji Cooper // met: 7b89a7cc2SEnji Cooper // 8b89a7cc2SEnji Cooper // * Redistributions of source code must retain the above copyright 9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer. 10b89a7cc2SEnji Cooper // * Redistributions in binary form must reproduce the above 11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer 12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the 13b89a7cc2SEnji Cooper // distribution. 14b89a7cc2SEnji Cooper // * Neither the name of Google Inc. nor the names of its 15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from 16b89a7cc2SEnji Cooper // this software without specific prior written permission. 17b89a7cc2SEnji Cooper // 18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2928f6c2f2SEnji Cooper 30b89a7cc2SEnji Cooper // Low-level types and utilities for porting Google Test to various 31b89a7cc2SEnji Cooper // platforms. All macros ending with _ and symbols defined in an 32b89a7cc2SEnji Cooper // internal namespace are subject to change without notice. Code 33b89a7cc2SEnji Cooper // outside Google Test MUST NOT USE THEM DIRECTLY. Macros that don't 34b89a7cc2SEnji Cooper // end with _ are part of Google Test's public API and can be used by 35b89a7cc2SEnji Cooper // code outside Google Test. 36b89a7cc2SEnji Cooper // 37b89a7cc2SEnji Cooper // This file is fundamental to Google Test. All other Google Test source 38b89a7cc2SEnji Cooper // files are expected to #include this. Therefore, it cannot #include 39b89a7cc2SEnji Cooper // any other Google Test header. 40b89a7cc2SEnji Cooper 4128f6c2f2SEnji Cooper // IWYU pragma: private, include "gtest/gtest.h" 4228f6c2f2SEnji Cooper // IWYU pragma: friend gtest/.* 4328f6c2f2SEnji Cooper // IWYU pragma: friend gmock/.* 44b89a7cc2SEnji Cooper 4528f6c2f2SEnji Cooper #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 4628f6c2f2SEnji Cooper #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 47b89a7cc2SEnji Cooper 48b89a7cc2SEnji Cooper // Environment-describing macros 49b89a7cc2SEnji Cooper // ----------------------------- 50b89a7cc2SEnji Cooper // 51b89a7cc2SEnji Cooper // Google Test can be used in many different environments. Macros in 52b89a7cc2SEnji Cooper // this section tell Google Test what kind of environment it is being 53b89a7cc2SEnji Cooper // used in, such that Google Test can provide environment-specific 54b89a7cc2SEnji Cooper // features and implementations. 55b89a7cc2SEnji Cooper // 56b89a7cc2SEnji Cooper // Google Test tries to automatically detect the properties of its 57b89a7cc2SEnji Cooper // environment, so users usually don't need to worry about these 58b89a7cc2SEnji Cooper // macros. However, the automatic detection is not perfect. 59b89a7cc2SEnji Cooper // Sometimes it's necessary for a user to define some of the following 60b89a7cc2SEnji Cooper // macros in the build script to override Google Test's decisions. 61b89a7cc2SEnji Cooper // 62b89a7cc2SEnji Cooper // If the user doesn't define a macro in the list, Google Test will 63b89a7cc2SEnji Cooper // provide a default definition. After this header is #included, all 64b89a7cc2SEnji Cooper // macros in this list will be defined to either 1 or 0. 65b89a7cc2SEnji Cooper // 66b89a7cc2SEnji Cooper // Notes to maintainers: 67b89a7cc2SEnji Cooper // - Each macro here is a user-tweakable knob; do not grow the list 68b89a7cc2SEnji Cooper // lightly. 69b89a7cc2SEnji Cooper // - Use #if to key off these macros. Don't use #ifdef or "#if 70b89a7cc2SEnji Cooper // defined(...)", which will not work as these macros are ALWAYS 71b89a7cc2SEnji Cooper // defined. 72b89a7cc2SEnji Cooper // 73b89a7cc2SEnji Cooper // GTEST_HAS_CLONE - Define it to 1/0 to indicate that clone(2) 74b89a7cc2SEnji Cooper // is/isn't available. 75b89a7cc2SEnji Cooper // GTEST_HAS_EXCEPTIONS - Define it to 1/0 to indicate that exceptions 76b89a7cc2SEnji Cooper // are enabled. 77b89a7cc2SEnji Cooper // GTEST_HAS_POSIX_RE - Define it to 1/0 to indicate that POSIX regular 78b89a7cc2SEnji Cooper // expressions are/aren't available. 79b89a7cc2SEnji Cooper // GTEST_HAS_PTHREAD - Define it to 1/0 to indicate that <pthread.h> 80b89a7cc2SEnji Cooper // is/isn't available. 81b89a7cc2SEnji Cooper // GTEST_HAS_RTTI - Define it to 1/0 to indicate that RTTI is/isn't 82b89a7cc2SEnji Cooper // enabled. 83b89a7cc2SEnji Cooper // GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that 84b89a7cc2SEnji Cooper // std::wstring does/doesn't work (Google Test can 85b89a7cc2SEnji Cooper // be used where std::wstring is unavailable). 8628f6c2f2SEnji Cooper // GTEST_HAS_FILE_SYSTEM - Define it to 1/0 to indicate whether or not a 8728f6c2f2SEnji Cooper // file system is/isn't available. 88b89a7cc2SEnji Cooper // GTEST_HAS_SEH - Define it to 1/0 to indicate whether the 89b89a7cc2SEnji Cooper // compiler supports Microsoft's "Structured 90b89a7cc2SEnji Cooper // Exception Handling". 91b89a7cc2SEnji Cooper // GTEST_HAS_STREAM_REDIRECTION 92b89a7cc2SEnji Cooper // - Define it to 1/0 to indicate whether the 93b89a7cc2SEnji Cooper // platform supports I/O stream redirection using 94b89a7cc2SEnji Cooper // dup() and dup2(). 95b89a7cc2SEnji Cooper // GTEST_LINKED_AS_SHARED_LIBRARY 96b89a7cc2SEnji Cooper // - Define to 1 when compiling tests that use 97b89a7cc2SEnji Cooper // Google Test as a shared library (known as 98b89a7cc2SEnji Cooper // DLL on Windows). 99b89a7cc2SEnji Cooper // GTEST_CREATE_SHARED_LIBRARY 100b89a7cc2SEnji Cooper // - Define to 1 when compiling Google Test itself 101b89a7cc2SEnji Cooper // as a shared library. 102b89a7cc2SEnji Cooper // GTEST_DEFAULT_DEATH_TEST_STYLE 103b89a7cc2SEnji Cooper // - The default value of --gtest_death_test_style. 104b89a7cc2SEnji Cooper // The legacy default has been "fast" in the open 105b89a7cc2SEnji Cooper // source version since 2008. The recommended value 106b89a7cc2SEnji Cooper // is "threadsafe", and can be set in 107b89a7cc2SEnji Cooper // custom/gtest-port.h. 108b89a7cc2SEnji Cooper 109b89a7cc2SEnji Cooper // Platform-indicating macros 110b89a7cc2SEnji Cooper // -------------------------- 111b89a7cc2SEnji Cooper // 112b89a7cc2SEnji Cooper // Macros indicating the platform on which Google Test is being used 113b89a7cc2SEnji Cooper // (a macro is defined to 1 if compiled on the given platform; 114b89a7cc2SEnji Cooper // otherwise UNDEFINED -- it's never defined to 0.). Google Test 115b89a7cc2SEnji Cooper // defines these macros automatically. Code outside Google Test MUST 116b89a7cc2SEnji Cooper // NOT define them. 117b89a7cc2SEnji Cooper // 118b89a7cc2SEnji Cooper // GTEST_OS_AIX - IBM AIX 119b89a7cc2SEnji Cooper // GTEST_OS_CYGWIN - Cygwin 12028f6c2f2SEnji Cooper // GTEST_OS_DRAGONFLY - DragonFlyBSD 121b89a7cc2SEnji Cooper // GTEST_OS_FREEBSD - FreeBSD 122b89a7cc2SEnji Cooper // GTEST_OS_FUCHSIA - Fuchsia 12328f6c2f2SEnji Cooper // GTEST_OS_GNU_HURD - GNU/Hurd 12428f6c2f2SEnji Cooper // GTEST_OS_GNU_KFREEBSD - GNU/kFreeBSD 12528f6c2f2SEnji Cooper // GTEST_OS_HAIKU - Haiku 126b89a7cc2SEnji Cooper // GTEST_OS_HPUX - HP-UX 127b89a7cc2SEnji Cooper // GTEST_OS_LINUX - Linux 128b89a7cc2SEnji Cooper // GTEST_OS_LINUX_ANDROID - Google Android 129b89a7cc2SEnji Cooper // GTEST_OS_MAC - Mac OS X 130b89a7cc2SEnji Cooper // GTEST_OS_IOS - iOS 131b89a7cc2SEnji Cooper // GTEST_OS_NACL - Google Native Client (NaCl) 132b89a7cc2SEnji Cooper // GTEST_OS_NETBSD - NetBSD 133b89a7cc2SEnji Cooper // GTEST_OS_OPENBSD - OpenBSD 13428f6c2f2SEnji Cooper // GTEST_OS_OS2 - OS/2 135b89a7cc2SEnji Cooper // GTEST_OS_QNX - QNX 136b89a7cc2SEnji Cooper // GTEST_OS_SOLARIS - Sun Solaris 137b89a7cc2SEnji Cooper // GTEST_OS_WINDOWS - Windows (Desktop, MinGW, or Mobile) 138b89a7cc2SEnji Cooper // GTEST_OS_WINDOWS_DESKTOP - Windows Desktop 139b89a7cc2SEnji Cooper // GTEST_OS_WINDOWS_MINGW - MinGW 140b89a7cc2SEnji Cooper // GTEST_OS_WINDOWS_MOBILE - Windows Mobile 141b89a7cc2SEnji Cooper // GTEST_OS_WINDOWS_PHONE - Windows Phone 142b89a7cc2SEnji Cooper // GTEST_OS_WINDOWS_RT - Windows Store App/WinRT 143b89a7cc2SEnji Cooper // GTEST_OS_ZOS - z/OS 144b89a7cc2SEnji Cooper // 14528f6c2f2SEnji Cooper // Among the platforms, Cygwin, Linux, Mac OS X, and Windows have the 146b89a7cc2SEnji Cooper // most stable support. Since core members of the Google Test project 147b89a7cc2SEnji Cooper // don't have access to other platforms, support for them may be less 148b89a7cc2SEnji Cooper // stable. If you notice any problems on your platform, please notify 149b89a7cc2SEnji Cooper // googletestframework@googlegroups.com (patches for fixing them are 150b89a7cc2SEnji Cooper // even more welcome!). 151b89a7cc2SEnji Cooper // 152b89a7cc2SEnji Cooper // It is possible that none of the GTEST_OS_* macros are defined. 153b89a7cc2SEnji Cooper 154b89a7cc2SEnji Cooper // Feature-indicating macros 155b89a7cc2SEnji Cooper // ------------------------- 156b89a7cc2SEnji Cooper // 157b89a7cc2SEnji Cooper // Macros indicating which Google Test features are available (a macro 158b89a7cc2SEnji Cooper // is defined to 1 if the corresponding feature is supported; 159b89a7cc2SEnji Cooper // otherwise UNDEFINED -- it's never defined to 0.). Google Test 160b89a7cc2SEnji Cooper // defines these macros automatically. Code outside Google Test MUST 161b89a7cc2SEnji Cooper // NOT define them. 162b89a7cc2SEnji Cooper // 163b89a7cc2SEnji Cooper // These macros are public so that portable tests can be written. 16428f6c2f2SEnji Cooper // Such tests typically surround code using a feature with an #ifdef 165b89a7cc2SEnji Cooper // which controls that code. For example: 166b89a7cc2SEnji Cooper // 16728f6c2f2SEnji Cooper // #ifdef GTEST_HAS_DEATH_TEST 168b89a7cc2SEnji Cooper // EXPECT_DEATH(DoSomethingDeadly()); 169b89a7cc2SEnji Cooper // #endif 170b89a7cc2SEnji Cooper // 171b89a7cc2SEnji Cooper // GTEST_HAS_DEATH_TEST - death tests 172b89a7cc2SEnji Cooper // GTEST_HAS_TYPED_TEST - typed tests 173b89a7cc2SEnji Cooper // GTEST_HAS_TYPED_TEST_P - type-parameterized tests 174b89a7cc2SEnji Cooper // GTEST_IS_THREADSAFE - Google Test is thread-safe. 17528f6c2f2SEnji Cooper // GTEST_USES_RE2 - the RE2 regular expression library is used 176b89a7cc2SEnji Cooper // GTEST_USES_POSIX_RE - enhanced POSIX regex is used. Do not confuse with 177b89a7cc2SEnji Cooper // GTEST_HAS_POSIX_RE (see above) which users can 178b89a7cc2SEnji Cooper // define themselves. 179b89a7cc2SEnji Cooper // GTEST_USES_SIMPLE_RE - our own simple regex is used; 180b89a7cc2SEnji Cooper // the above RE\b(s) are mutually exclusive. 18128f6c2f2SEnji Cooper // GTEST_HAS_ABSL - Google Test is compiled with Abseil. 182b89a7cc2SEnji Cooper 183b89a7cc2SEnji Cooper // Misc public macros 184b89a7cc2SEnji Cooper // ------------------ 185b89a7cc2SEnji Cooper // 186b89a7cc2SEnji Cooper // GTEST_FLAG(flag_name) - references the variable corresponding to 187b89a7cc2SEnji Cooper // the given Google Test flag. 188b89a7cc2SEnji Cooper 189b89a7cc2SEnji Cooper // Internal utilities 190b89a7cc2SEnji Cooper // ------------------ 191b89a7cc2SEnji Cooper // 192b89a7cc2SEnji Cooper // The following macros and utilities are for Google Test's INTERNAL 193b89a7cc2SEnji Cooper // use only. Code outside Google Test MUST NOT USE THEM DIRECTLY. 194b89a7cc2SEnji Cooper // 195b89a7cc2SEnji Cooper // Macros for basic C++ coding: 196b89a7cc2SEnji Cooper // GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning. 197b89a7cc2SEnji Cooper // GTEST_MUST_USE_RESULT_ - declares that a function's result must be used. 198b89a7cc2SEnji Cooper // GTEST_INTENTIONAL_CONST_COND_PUSH_ - start code section where MSVC C4127 is 199b89a7cc2SEnji Cooper // suppressed (constant conditional). 200b89a7cc2SEnji Cooper // GTEST_INTENTIONAL_CONST_COND_POP_ - finish code section where MSVC C4127 201b89a7cc2SEnji Cooper // is suppressed. 20228f6c2f2SEnji Cooper // GTEST_INTERNAL_HAS_ANY - for enabling UniversalPrinter<std::any> or 20328f6c2f2SEnji Cooper // UniversalPrinter<absl::any> specializations. 20428f6c2f2SEnji Cooper // Always defined to 0 or 1. 20528f6c2f2SEnji Cooper // GTEST_INTERNAL_HAS_OPTIONAL - for enabling UniversalPrinter<std::optional> 20628f6c2f2SEnji Cooper // or 20728f6c2f2SEnji Cooper // UniversalPrinter<absl::optional> 20828f6c2f2SEnji Cooper // specializations. Always defined to 0 or 1. 209*5ca8c28cSEnji Cooper // GTEST_INTERNAL_HAS_STD_SPAN - for enabling UniversalPrinter<std::span> 210*5ca8c28cSEnji Cooper // specializations. Always defined to 0 or 1 21128f6c2f2SEnji Cooper // GTEST_INTERNAL_HAS_STRING_VIEW - for enabling Matcher<std::string_view> or 21228f6c2f2SEnji Cooper // Matcher<absl::string_view> 21328f6c2f2SEnji Cooper // specializations. Always defined to 0 or 1. 21428f6c2f2SEnji Cooper // GTEST_INTERNAL_HAS_VARIANT - for enabling UniversalPrinter<std::variant> or 21528f6c2f2SEnji Cooper // UniversalPrinter<absl::variant> 21628f6c2f2SEnji Cooper // specializations. Always defined to 0 or 1. 21728f6c2f2SEnji Cooper // GTEST_USE_OWN_FLAGFILE_FLAG_ - Always defined to 0 or 1. 21828f6c2f2SEnji Cooper // GTEST_HAS_CXXABI_H_ - Always defined to 0 or 1. 21928f6c2f2SEnji Cooper // GTEST_CAN_STREAM_RESULTS_ - Always defined to 0 or 1. 22028f6c2f2SEnji Cooper // GTEST_HAS_ALT_PATH_SEP_ - Always defined to 0 or 1. 22128f6c2f2SEnji Cooper // GTEST_WIDE_STRING_USES_UTF16_ - Always defined to 0 or 1. 22228f6c2f2SEnji Cooper // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ - Always defined to 0 or 1. 22328f6c2f2SEnji Cooper // GTEST_HAS_NOTIFICATION_- Always defined to 0 or 1. 224b89a7cc2SEnji Cooper // 225b89a7cc2SEnji Cooper // Synchronization: 226b89a7cc2SEnji Cooper // Mutex, MutexLock, ThreadLocal, GetThreadCount() 227b89a7cc2SEnji Cooper // - synchronization primitives. 228b89a7cc2SEnji Cooper // 229b89a7cc2SEnji Cooper // Regular expressions: 23028f6c2f2SEnji Cooper // RE - a simple regular expression class using 23128f6c2f2SEnji Cooper // 1) the RE2 syntax on all platforms when built with RE2 23228f6c2f2SEnji Cooper // and Abseil as dependencies 23328f6c2f2SEnji Cooper // 2) the POSIX Extended Regular Expression syntax on 23428f6c2f2SEnji Cooper // UNIX-like platforms, 23528f6c2f2SEnji Cooper // 3) A reduced regular exception syntax on other platforms, 23628f6c2f2SEnji Cooper // including Windows. 237b89a7cc2SEnji Cooper // Logging: 238b89a7cc2SEnji Cooper // GTEST_LOG_() - logs messages at the specified severity level. 239b89a7cc2SEnji Cooper // LogToStderr() - directs all log messages to stderr. 240b89a7cc2SEnji Cooper // FlushInfoLog() - flushes informational log messages. 241b89a7cc2SEnji Cooper // 242b89a7cc2SEnji Cooper // Stdout and stderr capturing: 243b89a7cc2SEnji Cooper // CaptureStdout() - starts capturing stdout. 244b89a7cc2SEnji Cooper // GetCapturedStdout() - stops capturing stdout and returns the captured 245b89a7cc2SEnji Cooper // string. 246b89a7cc2SEnji Cooper // CaptureStderr() - starts capturing stderr. 247b89a7cc2SEnji Cooper // GetCapturedStderr() - stops capturing stderr and returns the captured 248b89a7cc2SEnji Cooper // string. 249b89a7cc2SEnji Cooper // 250b89a7cc2SEnji Cooper // Integer types: 251b89a7cc2SEnji Cooper // TypeWithSize - maps an integer to a int type. 25228f6c2f2SEnji Cooper // TimeInMillis - integers of known sizes. 253b89a7cc2SEnji Cooper // BiggestInt - the biggest signed integer type. 254b89a7cc2SEnji Cooper // 255b89a7cc2SEnji Cooper // Command-line utilities: 256b89a7cc2SEnji Cooper // GetInjectableArgvs() - returns the command line as a vector of strings. 257b89a7cc2SEnji Cooper // 258b89a7cc2SEnji Cooper // Environment variable utilities: 259b89a7cc2SEnji Cooper // GetEnv() - gets the value of an environment variable. 260b89a7cc2SEnji Cooper // BoolFromGTestEnv() - parses a bool environment variable. 26128f6c2f2SEnji Cooper // Int32FromGTestEnv() - parses an int32_t environment variable. 262b89a7cc2SEnji Cooper // StringFromGTestEnv() - parses a string environment variable. 26328f6c2f2SEnji Cooper // 26428f6c2f2SEnji Cooper // Deprecation warnings: 26528f6c2f2SEnji Cooper // GTEST_INTERNAL_DEPRECATED(message) - attribute marking a function as 26628f6c2f2SEnji Cooper // deprecated; calling a marked function 26728f6c2f2SEnji Cooper // should generate a compiler warning 26828f6c2f2SEnji Cooper 26928f6c2f2SEnji Cooper // The definition of GTEST_INTERNAL_CPLUSPLUS_LANG comes first because it can 27028f6c2f2SEnji Cooper // potentially be used as an #include guard. 27128f6c2f2SEnji Cooper #if defined(_MSVC_LANG) 27228f6c2f2SEnji Cooper #define GTEST_INTERNAL_CPLUSPLUS_LANG _MSVC_LANG 27328f6c2f2SEnji Cooper #elif defined(__cplusplus) 27428f6c2f2SEnji Cooper #define GTEST_INTERNAL_CPLUSPLUS_LANG __cplusplus 27528f6c2f2SEnji Cooper #endif 27628f6c2f2SEnji Cooper 27728f6c2f2SEnji Cooper #if !defined(GTEST_INTERNAL_CPLUSPLUS_LANG) || \ 27828f6c2f2SEnji Cooper GTEST_INTERNAL_CPLUSPLUS_LANG < 201402L 27928f6c2f2SEnji Cooper #error C++ versions less than C++14 are not supported. 28028f6c2f2SEnji Cooper #endif 281b89a7cc2SEnji Cooper 282*5ca8c28cSEnji Cooper // MSVC >= 19.11 (VS 2017 Update 3) supports __has_include. 283*5ca8c28cSEnji Cooper #ifdef __has_include 284*5ca8c28cSEnji Cooper #define GTEST_INTERNAL_HAS_INCLUDE __has_include 285*5ca8c28cSEnji Cooper #else 286*5ca8c28cSEnji Cooper #define GTEST_INTERNAL_HAS_INCLUDE(...) 0 287*5ca8c28cSEnji Cooper #endif 288*5ca8c28cSEnji Cooper 289*5ca8c28cSEnji Cooper // Detect C++ feature test macros as gracefully as possible. 290*5ca8c28cSEnji Cooper // MSVC >= 19.15, Clang >= 3.4.1, and GCC >= 4.1.2 support feature test macros. 291*5ca8c28cSEnji Cooper #if GTEST_INTERNAL_CPLUSPLUS_LANG >= 202002L && \ 292*5ca8c28cSEnji Cooper (!defined(__has_include) || GTEST_INTERNAL_HAS_INCLUDE(<version>)) 293*5ca8c28cSEnji Cooper #include <version> // C++20 and later 294*5ca8c28cSEnji Cooper #elif (!defined(__has_include) || GTEST_INTERNAL_HAS_INCLUDE(<ciso646>)) 295*5ca8c28cSEnji Cooper #include <ciso646> // Pre-C++20 296*5ca8c28cSEnji Cooper #endif 297*5ca8c28cSEnji Cooper 298b89a7cc2SEnji Cooper #include <ctype.h> // for isspace, etc 299b89a7cc2SEnji Cooper #include <stddef.h> // for ptrdiff_t 300b89a7cc2SEnji Cooper #include <stdio.h> 30128f6c2f2SEnji Cooper #include <stdlib.h> 302b89a7cc2SEnji Cooper #include <string.h> 30328f6c2f2SEnji Cooper 30428f6c2f2SEnji Cooper #include <cerrno> 30528f6c2f2SEnji Cooper // #include <condition_variable> // Guarded by GTEST_IS_THREADSAFE below 30628f6c2f2SEnji Cooper #include <cstdint> 30728f6c2f2SEnji Cooper #include <iostream> 30828f6c2f2SEnji Cooper #include <limits> 30928f6c2f2SEnji Cooper #include <locale> 31028f6c2f2SEnji Cooper #include <memory> 31128f6c2f2SEnji Cooper #include <ostream> 31228f6c2f2SEnji Cooper #include <string> 31328f6c2f2SEnji Cooper // #include <mutex> // Guarded by GTEST_IS_THREADSAFE below 31428f6c2f2SEnji Cooper #include <tuple> 31528f6c2f2SEnji Cooper #include <type_traits> 31628f6c2f2SEnji Cooper #include <vector> 31728f6c2f2SEnji Cooper 318b89a7cc2SEnji Cooper #ifndef _WIN32_WCE 319b89a7cc2SEnji Cooper #include <sys/stat.h> 32028f6c2f2SEnji Cooper #include <sys/types.h> 321b89a7cc2SEnji Cooper #endif // !_WIN32_WCE 322b89a7cc2SEnji Cooper 323b89a7cc2SEnji Cooper #if defined __APPLE__ 324b89a7cc2SEnji Cooper #include <AvailabilityMacros.h> 325b89a7cc2SEnji Cooper #include <TargetConditionals.h> 326b89a7cc2SEnji Cooper #endif 327b89a7cc2SEnji Cooper 328b89a7cc2SEnji Cooper #include "gtest/internal/custom/gtest-port.h" 32928f6c2f2SEnji Cooper #include "gtest/internal/gtest-port-arch.h" 33028f6c2f2SEnji Cooper 33128f6c2f2SEnji Cooper #ifndef GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ 33228f6c2f2SEnji Cooper #define GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ 0 33328f6c2f2SEnji Cooper #endif 33428f6c2f2SEnji Cooper 33528f6c2f2SEnji Cooper #ifndef GTEST_HAS_NOTIFICATION_ 33628f6c2f2SEnji Cooper #define GTEST_HAS_NOTIFICATION_ 0 33728f6c2f2SEnji Cooper #endif 33828f6c2f2SEnji Cooper 339*5ca8c28cSEnji Cooper #if defined(GTEST_HAS_ABSL) && !defined(GTEST_NO_ABSL_FLAGS) 340*5ca8c28cSEnji Cooper #define GTEST_INTERNAL_HAS_ABSL_FLAGS // Used only in this file. 34128f6c2f2SEnji Cooper #include "absl/flags/declare.h" 34228f6c2f2SEnji Cooper #include "absl/flags/flag.h" 34328f6c2f2SEnji Cooper #include "absl/flags/reflection.h" 34428f6c2f2SEnji Cooper #endif 345b89a7cc2SEnji Cooper 346b89a7cc2SEnji Cooper #if !defined(GTEST_DEV_EMAIL_) 347b89a7cc2SEnji Cooper #define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com" 348b89a7cc2SEnji Cooper #define GTEST_FLAG_PREFIX_ "gtest_" 349b89a7cc2SEnji Cooper #define GTEST_FLAG_PREFIX_DASH_ "gtest-" 350b89a7cc2SEnji Cooper #define GTEST_FLAG_PREFIX_UPPER_ "GTEST_" 351b89a7cc2SEnji Cooper #define GTEST_NAME_ "Google Test" 352b89a7cc2SEnji Cooper #define GTEST_PROJECT_URL_ "https://github.com/google/googletest/" 353b89a7cc2SEnji Cooper #endif // !defined(GTEST_DEV_EMAIL_) 354b89a7cc2SEnji Cooper 355b89a7cc2SEnji Cooper #if !defined(GTEST_INIT_GOOGLE_TEST_NAME_) 356b89a7cc2SEnji Cooper #define GTEST_INIT_GOOGLE_TEST_NAME_ "testing::InitGoogleTest" 357b89a7cc2SEnji Cooper #endif // !defined(GTEST_INIT_GOOGLE_TEST_NAME_) 358b89a7cc2SEnji Cooper 359b89a7cc2SEnji Cooper // Determines the version of gcc that is used to compile this. 360b89a7cc2SEnji Cooper #ifdef __GNUC__ 361b89a7cc2SEnji Cooper // 40302 means version 4.3.2. 362b89a7cc2SEnji Cooper #define GTEST_GCC_VER_ \ 363b89a7cc2SEnji Cooper (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) 364b89a7cc2SEnji Cooper #endif // __GNUC__ 365b89a7cc2SEnji Cooper 366b89a7cc2SEnji Cooper // Macros for disabling Microsoft Visual C++ warnings. 367b89a7cc2SEnji Cooper // 368b89a7cc2SEnji Cooper // GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 4385) 369b89a7cc2SEnji Cooper // /* code that triggers warnings C4800 and C4385 */ 370b89a7cc2SEnji Cooper // GTEST_DISABLE_MSC_WARNINGS_POP_() 37128f6c2f2SEnji Cooper #if defined(_MSC_VER) 372b89a7cc2SEnji Cooper #define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) \ 37328f6c2f2SEnji Cooper __pragma(warning(push)) __pragma(warning(disable : warnings)) 37428f6c2f2SEnji Cooper #define GTEST_DISABLE_MSC_WARNINGS_POP_() __pragma(warning(pop)) 375b89a7cc2SEnji Cooper #else 37628f6c2f2SEnji Cooper // Not all compilers are MSVC 377b89a7cc2SEnji Cooper #define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) 378b89a7cc2SEnji Cooper #define GTEST_DISABLE_MSC_WARNINGS_POP_() 379b89a7cc2SEnji Cooper #endif 380b89a7cc2SEnji Cooper 381b89a7cc2SEnji Cooper // Clang on Windows does not understand MSVC's pragma warning. 382b89a7cc2SEnji Cooper // We need clang-specific way to disable function deprecation warning. 383b89a7cc2SEnji Cooper #ifdef __clang__ 384b89a7cc2SEnji Cooper #define GTEST_DISABLE_MSC_DEPRECATED_PUSH_() \ 385b89a7cc2SEnji Cooper _Pragma("clang diagnostic push") \ 386b89a7cc2SEnji Cooper _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") \ 387b89a7cc2SEnji Cooper _Pragma("clang diagnostic ignored \"-Wdeprecated-implementations\"") 38828f6c2f2SEnji Cooper #define GTEST_DISABLE_MSC_DEPRECATED_POP_() _Pragma("clang diagnostic pop") 389b89a7cc2SEnji Cooper #else 390b89a7cc2SEnji Cooper #define GTEST_DISABLE_MSC_DEPRECATED_PUSH_() \ 391b89a7cc2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996) 39228f6c2f2SEnji Cooper #define GTEST_DISABLE_MSC_DEPRECATED_POP_() GTEST_DISABLE_MSC_WARNINGS_POP_() 393b89a7cc2SEnji Cooper #endif 394b89a7cc2SEnji Cooper 395b89a7cc2SEnji Cooper // Brings in definitions for functions used in the testing::internal::posix 396b89a7cc2SEnji Cooper // namespace (read, write, close, chdir, isatty, stat). We do not currently 397b89a7cc2SEnji Cooper // use them on Windows Mobile. 39828f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS 39928f6c2f2SEnji Cooper #ifndef GTEST_OS_WINDOWS_MOBILE 400b89a7cc2SEnji Cooper #include <direct.h> 401b89a7cc2SEnji Cooper #include <io.h> 402b89a7cc2SEnji Cooper #endif 403b89a7cc2SEnji Cooper // In order to avoid having to include <windows.h>, use forward declaration 40428f6c2f2SEnji Cooper #if defined(GTEST_OS_WINDOWS_MINGW) && !defined(__MINGW64_VERSION_MAJOR) 405b89a7cc2SEnji Cooper // MinGW defined _CRITICAL_SECTION and _RTL_CRITICAL_SECTION as two 406b89a7cc2SEnji Cooper // separate (equivalent) structs, instead of using typedef 407b89a7cc2SEnji Cooper typedef struct _CRITICAL_SECTION GTEST_CRITICAL_SECTION; 408b89a7cc2SEnji Cooper #else 409b89a7cc2SEnji Cooper // Assume CRITICAL_SECTION is a typedef of _RTL_CRITICAL_SECTION. 410b89a7cc2SEnji Cooper // This assumption is verified by 411b89a7cc2SEnji Cooper // WindowsTypesTest.CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION. 412b89a7cc2SEnji Cooper typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION; 413b89a7cc2SEnji Cooper #endif 41428f6c2f2SEnji Cooper #elif defined(GTEST_OS_XTENSA) 41528f6c2f2SEnji Cooper #include <unistd.h> 41628f6c2f2SEnji Cooper // Xtensa toolchains define strcasecmp in the string.h header instead of 41728f6c2f2SEnji Cooper // strings.h. string.h is already included. 418b89a7cc2SEnji Cooper #else 419b89a7cc2SEnji Cooper // This assumes that non-Windows OSes provide unistd.h. For OSes where this 420b89a7cc2SEnji Cooper // is not the case, we need to include headers that provide the functions 421b89a7cc2SEnji Cooper // mentioned above. 422b89a7cc2SEnji Cooper #include <strings.h> 42328f6c2f2SEnji Cooper #include <unistd.h> 424b89a7cc2SEnji Cooper #endif // GTEST_OS_WINDOWS 425b89a7cc2SEnji Cooper 42628f6c2f2SEnji Cooper #ifdef GTEST_OS_LINUX_ANDROID 427b89a7cc2SEnji Cooper // Used to define __ANDROID_API__ matching the target NDK API level. 428b89a7cc2SEnji Cooper #include <android/api-level.h> // NOLINT 429b89a7cc2SEnji Cooper #endif 430b89a7cc2SEnji Cooper 43128f6c2f2SEnji Cooper // Defines this to true if and only if Google Test can use POSIX regular 43228f6c2f2SEnji Cooper // expressions. 433b89a7cc2SEnji Cooper #ifndef GTEST_HAS_POSIX_RE 43428f6c2f2SEnji Cooper #ifdef GTEST_OS_LINUX_ANDROID 435b89a7cc2SEnji Cooper // On Android, <regex.h> is only available starting with Gingerbread. 436b89a7cc2SEnji Cooper #define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9) 437b89a7cc2SEnji Cooper #else 43828f6c2f2SEnji Cooper #if !(defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_XTENSA) || \ 43928f6c2f2SEnji Cooper defined(GTEST_OS_QURT)) 44028f6c2f2SEnji Cooper #define GTEST_HAS_POSIX_RE 1 441b89a7cc2SEnji Cooper #else 44228f6c2f2SEnji Cooper #define GTEST_HAS_POSIX_RE 0 44328f6c2f2SEnji Cooper #endif 44428f6c2f2SEnji Cooper #endif // GTEST_OS_LINUX_ANDROID 44528f6c2f2SEnji Cooper #endif 446b89a7cc2SEnji Cooper 44728f6c2f2SEnji Cooper // Select the regular expression implementation. 44828f6c2f2SEnji Cooper #ifdef GTEST_HAS_ABSL 44928f6c2f2SEnji Cooper // When using Abseil, RE2 is required. 45028f6c2f2SEnji Cooper #include "absl/strings/string_view.h" 45128f6c2f2SEnji Cooper #include "re2/re2.h" 45228f6c2f2SEnji Cooper #define GTEST_USES_RE2 1 45328f6c2f2SEnji Cooper #elif GTEST_HAS_POSIX_RE 45428f6c2f2SEnji Cooper #include <regex.h> // NOLINT 45528f6c2f2SEnji Cooper #define GTEST_USES_POSIX_RE 1 45628f6c2f2SEnji Cooper #else 45728f6c2f2SEnji Cooper // Use our own simple regex implementation. 458b89a7cc2SEnji Cooper #define GTEST_USES_SIMPLE_RE 1 45928f6c2f2SEnji Cooper #endif 460b89a7cc2SEnji Cooper 461b89a7cc2SEnji Cooper #ifndef GTEST_HAS_EXCEPTIONS 462b89a7cc2SEnji Cooper // The user didn't tell us whether exceptions are enabled, so we need 463b89a7cc2SEnji Cooper // to figure it out. 464b89a7cc2SEnji Cooper #if defined(_MSC_VER) && defined(_CPPUNWIND) 46528f6c2f2SEnji Cooper // MSVC defines _CPPUNWIND to 1 if and only if exceptions are enabled. 466b89a7cc2SEnji Cooper #define GTEST_HAS_EXCEPTIONS 1 467b89a7cc2SEnji Cooper #elif defined(__BORLANDC__) 468b89a7cc2SEnji Cooper // C++Builder's implementation of the STL uses the _HAS_EXCEPTIONS 469b89a7cc2SEnji Cooper // macro to enable exceptions, so we'll do the same. 470b89a7cc2SEnji Cooper // Assumes that exceptions are enabled by default. 471b89a7cc2SEnji Cooper #ifndef _HAS_EXCEPTIONS 472b89a7cc2SEnji Cooper #define _HAS_EXCEPTIONS 1 473b89a7cc2SEnji Cooper #endif // _HAS_EXCEPTIONS 474b89a7cc2SEnji Cooper #define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS 475b89a7cc2SEnji Cooper #elif defined(__clang__) 47628f6c2f2SEnji Cooper // clang defines __EXCEPTIONS if and only if exceptions are enabled before clang 47728f6c2f2SEnji Cooper // 220714, but if and only if cleanups are enabled after that. In Obj-C++ files, 47828f6c2f2SEnji Cooper // there can be cleanups for ObjC exceptions which also need cleanups, even if 47928f6c2f2SEnji Cooper // C++ exceptions are disabled. clang has __has_feature(cxx_exceptions) which 48028f6c2f2SEnji Cooper // checks for C++ exceptions starting at clang r206352, but which checked for 48128f6c2f2SEnji Cooper // cleanups prior to that. To reliably check for C++ exception availability with 48228f6c2f2SEnji Cooper // clang, check for 483b89a7cc2SEnji Cooper // __EXCEPTIONS && __has_feature(cxx_exceptions). 48428f6c2f2SEnji Cooper #if defined(__EXCEPTIONS) && __EXCEPTIONS && __has_feature(cxx_exceptions) 48528f6c2f2SEnji Cooper #define GTEST_HAS_EXCEPTIONS 1 48628f6c2f2SEnji Cooper #else 48728f6c2f2SEnji Cooper #define GTEST_HAS_EXCEPTIONS 0 48828f6c2f2SEnji Cooper #endif 48928f6c2f2SEnji Cooper #elif defined(__GNUC__) && defined(__EXCEPTIONS) && __EXCEPTIONS 49028f6c2f2SEnji Cooper // gcc defines __EXCEPTIONS to 1 if and only if exceptions are enabled. 491b89a7cc2SEnji Cooper #define GTEST_HAS_EXCEPTIONS 1 492b89a7cc2SEnji Cooper #elif defined(__SUNPRO_CC) 493b89a7cc2SEnji Cooper // Sun Pro CC supports exceptions. However, there is no compile-time way of 494b89a7cc2SEnji Cooper // detecting whether they are enabled or not. Therefore, we assume that 495b89a7cc2SEnji Cooper // they are enabled unless the user tells us otherwise. 496b89a7cc2SEnji Cooper #define GTEST_HAS_EXCEPTIONS 1 49728f6c2f2SEnji Cooper #elif defined(__IBMCPP__) && defined(__EXCEPTIONS) && __EXCEPTIONS 49828f6c2f2SEnji Cooper // xlC defines __EXCEPTIONS to 1 if and only if exceptions are enabled. 499b89a7cc2SEnji Cooper #define GTEST_HAS_EXCEPTIONS 1 500b89a7cc2SEnji Cooper #elif defined(__HP_aCC) 501b89a7cc2SEnji Cooper // Exception handling is in effect by default in HP aCC compiler. It has to 502b89a7cc2SEnji Cooper // be turned of by +noeh compiler option if desired. 503b89a7cc2SEnji Cooper #define GTEST_HAS_EXCEPTIONS 1 504b89a7cc2SEnji Cooper #else 505b89a7cc2SEnji Cooper // For other compilers, we assume exceptions are disabled to be 506b89a7cc2SEnji Cooper // conservative. 507b89a7cc2SEnji Cooper #define GTEST_HAS_EXCEPTIONS 0 508b89a7cc2SEnji Cooper #endif // defined(_MSC_VER) || defined(__BORLANDC__) 509b89a7cc2SEnji Cooper #endif // GTEST_HAS_EXCEPTIONS 510b89a7cc2SEnji Cooper 511b89a7cc2SEnji Cooper #ifndef GTEST_HAS_STD_WSTRING 512b89a7cc2SEnji Cooper // The user didn't tell us whether ::std::wstring is available, so we need 513b89a7cc2SEnji Cooper // to figure it out. 514b89a7cc2SEnji Cooper // Cygwin 1.7 and below doesn't support ::std::wstring. 515b89a7cc2SEnji Cooper // Solaris' libc++ doesn't support it either. Android has 516b89a7cc2SEnji Cooper // no support for it at least as recent as Froyo (2.2). 51728f6c2f2SEnji Cooper #if (!(defined(GTEST_OS_LINUX_ANDROID) || defined(GTEST_OS_CYGWIN) || \ 51828f6c2f2SEnji Cooper defined(GTEST_OS_SOLARIS) || defined(GTEST_OS_HAIKU) || \ 51928f6c2f2SEnji Cooper defined(GTEST_OS_ESP32) || defined(GTEST_OS_ESP8266) || \ 52028f6c2f2SEnji Cooper defined(GTEST_OS_XTENSA) || defined(GTEST_OS_QURT) || \ 52128f6c2f2SEnji Cooper defined(GTEST_OS_NXP_QN9090) || defined(GTEST_OS_NRF52))) 52228f6c2f2SEnji Cooper #define GTEST_HAS_STD_WSTRING 1 52328f6c2f2SEnji Cooper #else 52428f6c2f2SEnji Cooper #define GTEST_HAS_STD_WSTRING 0 52528f6c2f2SEnji Cooper #endif 526b89a7cc2SEnji Cooper #endif // GTEST_HAS_STD_WSTRING 527b89a7cc2SEnji Cooper 52828f6c2f2SEnji Cooper #ifndef GTEST_HAS_FILE_SYSTEM 52928f6c2f2SEnji Cooper // Most platforms support a file system. 53028f6c2f2SEnji Cooper #define GTEST_HAS_FILE_SYSTEM 1 53128f6c2f2SEnji Cooper #endif // GTEST_HAS_FILE_SYSTEM 532b89a7cc2SEnji Cooper 533b89a7cc2SEnji Cooper // Determines whether RTTI is available. 534b89a7cc2SEnji Cooper #ifndef GTEST_HAS_RTTI 535b89a7cc2SEnji Cooper // The user didn't tell us whether RTTI is enabled, so we need to 536b89a7cc2SEnji Cooper // figure it out. 537b89a7cc2SEnji Cooper 538b89a7cc2SEnji Cooper #ifdef _MSC_VER 539b89a7cc2SEnji Cooper 54028f6c2f2SEnji Cooper #ifdef _CPPRTTI // MSVC defines this macro if and only if RTTI is enabled. 541b89a7cc2SEnji Cooper #define GTEST_HAS_RTTI 1 542b89a7cc2SEnji Cooper #else 543b89a7cc2SEnji Cooper #define GTEST_HAS_RTTI 0 544b89a7cc2SEnji Cooper #endif 545b89a7cc2SEnji Cooper 54628f6c2f2SEnji Cooper // Starting with version 4.3.2, gcc defines __GXX_RTTI if and only if RTTI is 54728f6c2f2SEnji Cooper // enabled. 54828f6c2f2SEnji Cooper #elif defined(__GNUC__) 549b89a7cc2SEnji Cooper 550b89a7cc2SEnji Cooper #ifdef __GXX_RTTI 551b89a7cc2SEnji Cooper // When building against STLport with the Android NDK and with 552b89a7cc2SEnji Cooper // -frtti -fno-exceptions, the build fails at link time with undefined 553b89a7cc2SEnji Cooper // references to __cxa_bad_typeid. Note sure if STL or toolchain bug, 554b89a7cc2SEnji Cooper // so disable RTTI when detected. 55528f6c2f2SEnji Cooper #if defined(GTEST_OS_LINUX_ANDROID) && defined(_STLPORT_MAJOR) && \ 556b89a7cc2SEnji Cooper !defined(__EXCEPTIONS) 557b89a7cc2SEnji Cooper #define GTEST_HAS_RTTI 0 558b89a7cc2SEnji Cooper #else 559b89a7cc2SEnji Cooper #define GTEST_HAS_RTTI 1 560b89a7cc2SEnji Cooper #endif // GTEST_OS_LINUX_ANDROID && __STLPORT_MAJOR && !__EXCEPTIONS 561b89a7cc2SEnji Cooper #else 562b89a7cc2SEnji Cooper #define GTEST_HAS_RTTI 0 563b89a7cc2SEnji Cooper #endif // __GXX_RTTI 564b89a7cc2SEnji Cooper 565b89a7cc2SEnji Cooper // Clang defines __GXX_RTTI starting with version 3.0, but its manual recommends 566b89a7cc2SEnji Cooper // using has_feature instead. has_feature(cxx_rtti) is supported since 2.7, the 567b89a7cc2SEnji Cooper // first version with C++ support. 568b89a7cc2SEnji Cooper #elif defined(__clang__) 569b89a7cc2SEnji Cooper 570b89a7cc2SEnji Cooper #define GTEST_HAS_RTTI __has_feature(cxx_rtti) 571b89a7cc2SEnji Cooper 572b89a7cc2SEnji Cooper // Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if 573b89a7cc2SEnji Cooper // both the typeid and dynamic_cast features are present. 574b89a7cc2SEnji Cooper #elif defined(__IBMCPP__) && (__IBMCPP__ >= 900) 575b89a7cc2SEnji Cooper 576b89a7cc2SEnji Cooper #ifdef __RTTI_ALL__ 577b89a7cc2SEnji Cooper #define GTEST_HAS_RTTI 1 578b89a7cc2SEnji Cooper #else 579b89a7cc2SEnji Cooper #define GTEST_HAS_RTTI 0 580b89a7cc2SEnji Cooper #endif 581b89a7cc2SEnji Cooper 582b89a7cc2SEnji Cooper #else 583b89a7cc2SEnji Cooper 584b89a7cc2SEnji Cooper // For all other compilers, we assume RTTI is enabled. 585b89a7cc2SEnji Cooper #define GTEST_HAS_RTTI 1 586b89a7cc2SEnji Cooper 587b89a7cc2SEnji Cooper #endif // _MSC_VER 588b89a7cc2SEnji Cooper 589b89a7cc2SEnji Cooper #endif // GTEST_HAS_RTTI 590b89a7cc2SEnji Cooper 591b89a7cc2SEnji Cooper // It's this header's responsibility to #include <typeinfo> when RTTI 592b89a7cc2SEnji Cooper // is enabled. 593b89a7cc2SEnji Cooper #if GTEST_HAS_RTTI 594b89a7cc2SEnji Cooper #include <typeinfo> 595b89a7cc2SEnji Cooper #endif 596b89a7cc2SEnji Cooper 597b89a7cc2SEnji Cooper // Determines whether Google Test can use the pthreads library. 598b89a7cc2SEnji Cooper #ifndef GTEST_HAS_PTHREAD 599b89a7cc2SEnji Cooper // The user didn't tell us explicitly, so we make reasonable assumptions about 600b89a7cc2SEnji Cooper // which platforms have pthreads support. 601b89a7cc2SEnji Cooper // 602b89a7cc2SEnji Cooper // To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0 603b89a7cc2SEnji Cooper // to your compiler flags. 60428f6c2f2SEnji Cooper #if (defined(GTEST_OS_LINUX) || defined(GTEST_OS_MAC) || \ 60528f6c2f2SEnji Cooper defined(GTEST_OS_HPUX) || defined(GTEST_OS_QNX) || \ 60628f6c2f2SEnji Cooper defined(GTEST_OS_FREEBSD) || defined(GTEST_OS_NACL) || \ 60728f6c2f2SEnji Cooper defined(GTEST_OS_NETBSD) || defined(GTEST_OS_FUCHSIA) || \ 60828f6c2f2SEnji Cooper defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_GNU_KFREEBSD) || \ 60928f6c2f2SEnji Cooper defined(GTEST_OS_OPENBSD) || defined(GTEST_OS_HAIKU) || \ 610*5ca8c28cSEnji Cooper defined(GTEST_OS_GNU_HURD) || defined(GTEST_OS_SOLARIS) || \ 611*5ca8c28cSEnji Cooper defined(GTEST_OS_AIX) || defined(GTEST_OS_ZOS)) 61228f6c2f2SEnji Cooper #define GTEST_HAS_PTHREAD 1 61328f6c2f2SEnji Cooper #else 61428f6c2f2SEnji Cooper #define GTEST_HAS_PTHREAD 0 61528f6c2f2SEnji Cooper #endif 616b89a7cc2SEnji Cooper #endif // GTEST_HAS_PTHREAD 617b89a7cc2SEnji Cooper 618b89a7cc2SEnji Cooper #if GTEST_HAS_PTHREAD 619b89a7cc2SEnji Cooper // gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is 620b89a7cc2SEnji Cooper // true. 621b89a7cc2SEnji Cooper #include <pthread.h> // NOLINT 622b89a7cc2SEnji Cooper 623b89a7cc2SEnji Cooper // For timespec and nanosleep, used below. 624b89a7cc2SEnji Cooper #include <time.h> // NOLINT 625b89a7cc2SEnji Cooper #endif 626b89a7cc2SEnji Cooper 627b89a7cc2SEnji Cooper // Determines whether clone(2) is supported. 628b89a7cc2SEnji Cooper // Usually it will only be available on Linux, excluding 629b89a7cc2SEnji Cooper // Linux on the Itanium architecture. 630*5ca8c28cSEnji Cooper // Also see https://linux.die.net/man/2/clone. 631b89a7cc2SEnji Cooper #ifndef GTEST_HAS_CLONE 632b89a7cc2SEnji Cooper // The user didn't tell us, so we need to figure it out. 633b89a7cc2SEnji Cooper 63428f6c2f2SEnji Cooper #if defined(GTEST_OS_LINUX) && !defined(__ia64__) 63528f6c2f2SEnji Cooper #if defined(GTEST_OS_LINUX_ANDROID) 636b89a7cc2SEnji Cooper // On Android, clone() became available at different API levels for each 32-bit 637b89a7cc2SEnji Cooper // architecture. 63828f6c2f2SEnji Cooper #if defined(__LP64__) || (defined(__arm__) && __ANDROID_API__ >= 9) || \ 639b89a7cc2SEnji Cooper (defined(__mips__) && __ANDROID_API__ >= 12) || \ 640b89a7cc2SEnji Cooper (defined(__i386__) && __ANDROID_API__ >= 17) 641b89a7cc2SEnji Cooper #define GTEST_HAS_CLONE 1 642b89a7cc2SEnji Cooper #else 643b89a7cc2SEnji Cooper #define GTEST_HAS_CLONE 0 644b89a7cc2SEnji Cooper #endif 645b89a7cc2SEnji Cooper #else 646b89a7cc2SEnji Cooper #define GTEST_HAS_CLONE 1 647b89a7cc2SEnji Cooper #endif 648b89a7cc2SEnji Cooper #else 649b89a7cc2SEnji Cooper #define GTEST_HAS_CLONE 0 650b89a7cc2SEnji Cooper #endif // GTEST_OS_LINUX && !defined(__ia64__) 651b89a7cc2SEnji Cooper 652b89a7cc2SEnji Cooper #endif // GTEST_HAS_CLONE 653b89a7cc2SEnji Cooper 654b89a7cc2SEnji Cooper // Determines whether to support stream redirection. This is used to test 655b89a7cc2SEnji Cooper // output correctness and to implement death tests. 656b89a7cc2SEnji Cooper #ifndef GTEST_HAS_STREAM_REDIRECTION 657b89a7cc2SEnji Cooper // By default, we assume that stream redirection is supported on all 65828f6c2f2SEnji Cooper // platforms except known mobile / embedded ones. Also, if the port doesn't have 65928f6c2f2SEnji Cooper // a file system, stream redirection is not supported. 66028f6c2f2SEnji Cooper #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_WINDOWS_PHONE) || \ 661*5ca8c28cSEnji Cooper defined(GTEST_OS_WINDOWS_RT) || defined(GTEST_OS_WINDOWS_GAMES) || \ 662*5ca8c28cSEnji Cooper defined(GTEST_OS_ESP8266) || defined(GTEST_OS_XTENSA) || \ 663*5ca8c28cSEnji Cooper defined(GTEST_OS_QURT) || !GTEST_HAS_FILE_SYSTEM 664b89a7cc2SEnji Cooper #define GTEST_HAS_STREAM_REDIRECTION 0 665b89a7cc2SEnji Cooper #else 666b89a7cc2SEnji Cooper #define GTEST_HAS_STREAM_REDIRECTION 1 66728f6c2f2SEnji Cooper #endif // !GTEST_OS_WINDOWS_MOBILE 668b89a7cc2SEnji Cooper #endif // GTEST_HAS_STREAM_REDIRECTION 669b89a7cc2SEnji Cooper 670b89a7cc2SEnji Cooper // Determines whether to support death tests. 671b89a7cc2SEnji Cooper // pops up a dialog window that cannot be suppressed programmatically. 67228f6c2f2SEnji Cooper #if (defined(GTEST_OS_LINUX) || defined(GTEST_OS_CYGWIN) || \ 673*5ca8c28cSEnji Cooper defined(GTEST_OS_SOLARIS) || defined(GTEST_OS_ZOS) || \ 67428f6c2f2SEnji Cooper (defined(GTEST_OS_MAC) && !defined(GTEST_OS_IOS)) || \ 67528f6c2f2SEnji Cooper (defined(GTEST_OS_WINDOWS_DESKTOP) && _MSC_VER) || \ 67628f6c2f2SEnji Cooper defined(GTEST_OS_WINDOWS_MINGW) || defined(GTEST_OS_AIX) || \ 67728f6c2f2SEnji Cooper defined(GTEST_OS_HPUX) || defined(GTEST_OS_OPENBSD) || \ 67828f6c2f2SEnji Cooper defined(GTEST_OS_QNX) || defined(GTEST_OS_FREEBSD) || \ 67928f6c2f2SEnji Cooper defined(GTEST_OS_NETBSD) || defined(GTEST_OS_FUCHSIA) || \ 68028f6c2f2SEnji Cooper defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_GNU_KFREEBSD) || \ 68128f6c2f2SEnji Cooper defined(GTEST_OS_HAIKU) || defined(GTEST_OS_GNU_HURD)) 68228f6c2f2SEnji Cooper // Death tests require a file system to work properly. 68328f6c2f2SEnji Cooper #if GTEST_HAS_FILE_SYSTEM 684b89a7cc2SEnji Cooper #define GTEST_HAS_DEATH_TEST 1 68528f6c2f2SEnji Cooper #endif // GTEST_HAS_FILE_SYSTEM 686b89a7cc2SEnji Cooper #endif 687b89a7cc2SEnji Cooper 688b89a7cc2SEnji Cooper // Determines whether to support type-driven tests. 689b89a7cc2SEnji Cooper 690b89a7cc2SEnji Cooper // Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0, 691b89a7cc2SEnji Cooper // Sun Pro CC, IBM Visual Age, and HP aCC support. 69228f6c2f2SEnji Cooper #if defined(__GNUC__) || defined(_MSC_VER) || defined(__SUNPRO_CC) || \ 693b89a7cc2SEnji Cooper defined(__IBMCPP__) || defined(__HP_aCC) 694b89a7cc2SEnji Cooper #define GTEST_HAS_TYPED_TEST 1 695b89a7cc2SEnji Cooper #define GTEST_HAS_TYPED_TEST_P 1 696b89a7cc2SEnji Cooper #endif 697b89a7cc2SEnji Cooper 69828f6c2f2SEnji Cooper // Determines whether the system compiler uses UTF-16 for encoding wide strings. 69928f6c2f2SEnji Cooper #if defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_CYGWIN) || \ 70028f6c2f2SEnji Cooper defined(GTEST_OS_AIX) || defined(GTEST_OS_OS2) 70128f6c2f2SEnji Cooper #define GTEST_WIDE_STRING_USES_UTF16_ 1 70228f6c2f2SEnji Cooper #else 70328f6c2f2SEnji Cooper #define GTEST_WIDE_STRING_USES_UTF16_ 0 704b89a7cc2SEnji Cooper #endif 705b89a7cc2SEnji Cooper 706b89a7cc2SEnji Cooper // Determines whether test results can be streamed to a socket. 70728f6c2f2SEnji Cooper #if defined(GTEST_OS_LINUX) || defined(GTEST_OS_GNU_KFREEBSD) || \ 70828f6c2f2SEnji Cooper defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_FREEBSD) || \ 70928f6c2f2SEnji Cooper defined(GTEST_OS_NETBSD) || defined(GTEST_OS_OPENBSD) || \ 71028f6c2f2SEnji Cooper defined(GTEST_OS_GNU_HURD) || defined(GTEST_OS_MAC) 711b89a7cc2SEnji Cooper #define GTEST_CAN_STREAM_RESULTS_ 1 71228f6c2f2SEnji Cooper #else 71328f6c2f2SEnji Cooper #define GTEST_CAN_STREAM_RESULTS_ 0 714b89a7cc2SEnji Cooper #endif 715b89a7cc2SEnji Cooper 716b89a7cc2SEnji Cooper // Defines some utility macros. 717b89a7cc2SEnji Cooper 718b89a7cc2SEnji Cooper // The GNU compiler emits a warning if nested "if" statements are followed by 719b89a7cc2SEnji Cooper // an "else" statement and braces are not used to explicitly disambiguate the 720b89a7cc2SEnji Cooper // "else" binding. This leads to problems with code like: 721b89a7cc2SEnji Cooper // 722b89a7cc2SEnji Cooper // if (gate) 723b89a7cc2SEnji Cooper // ASSERT_*(condition) << "Some message"; 724b89a7cc2SEnji Cooper // 725b89a7cc2SEnji Cooper // The "switch (0) case 0:" idiom is used to suppress this. 726b89a7cc2SEnji Cooper #ifdef __INTEL_COMPILER 727b89a7cc2SEnji Cooper #define GTEST_AMBIGUOUS_ELSE_BLOCKER_ 728b89a7cc2SEnji Cooper #else 72928f6c2f2SEnji Cooper #define GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 73028f6c2f2SEnji Cooper switch (0) \ 73128f6c2f2SEnji Cooper case 0: \ 73228f6c2f2SEnji Cooper default: // NOLINT 733b89a7cc2SEnji Cooper #endif 734b89a7cc2SEnji Cooper 73528f6c2f2SEnji Cooper // GTEST_HAVE_ATTRIBUTE_ 736b89a7cc2SEnji Cooper // 73728f6c2f2SEnji Cooper // A function-like feature checking macro that is a wrapper around 73828f6c2f2SEnji Cooper // `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a 73928f6c2f2SEnji Cooper // nonzero constant integer if the attribute is supported or 0 if not. 740b89a7cc2SEnji Cooper // 74128f6c2f2SEnji Cooper // It evaluates to zero if `__has_attribute` is not defined by the compiler. 74228f6c2f2SEnji Cooper // 74328f6c2f2SEnji Cooper // GCC: https://gcc.gnu.org/gcc-5/changes.html 74428f6c2f2SEnji Cooper // Clang: https://clang.llvm.org/docs/LanguageExtensions.html 74528f6c2f2SEnji Cooper #ifdef __has_attribute 74628f6c2f2SEnji Cooper #define GTEST_HAVE_ATTRIBUTE_(x) __has_attribute(x) 74728f6c2f2SEnji Cooper #else 74828f6c2f2SEnji Cooper #define GTEST_HAVE_ATTRIBUTE_(x) 0 74928f6c2f2SEnji Cooper #endif 75028f6c2f2SEnji Cooper 751*5ca8c28cSEnji Cooper // GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE 752*5ca8c28cSEnji Cooper // 753*5ca8c28cSEnji Cooper // A function-like feature checking macro that accepts C++11 style attributes. 754*5ca8c28cSEnji Cooper // It's a wrapper around `__has_cpp_attribute`, defined by ISO C++ SD-6 755*5ca8c28cSEnji Cooper // (https://en.cppreference.com/w/cpp/experimental/feature_test). If we don't 756*5ca8c28cSEnji Cooper // find `__has_cpp_attribute`, will evaluate to 0. 757*5ca8c28cSEnji Cooper #if defined(__has_cpp_attribute) 758*5ca8c28cSEnji Cooper // NOTE: requiring __cplusplus above should not be necessary, but 759*5ca8c28cSEnji Cooper // works around https://bugs.llvm.org/show_bug.cgi?id=23435. 760*5ca8c28cSEnji Cooper #define GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) 761*5ca8c28cSEnji Cooper #else 762*5ca8c28cSEnji Cooper #define GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(x) 0 763*5ca8c28cSEnji Cooper #endif 764*5ca8c28cSEnji Cooper 76528f6c2f2SEnji Cooper // GTEST_HAVE_FEATURE_ 76628f6c2f2SEnji Cooper // 76728f6c2f2SEnji Cooper // A function-like feature checking macro that is a wrapper around 76828f6c2f2SEnji Cooper // `__has_feature`. 76928f6c2f2SEnji Cooper #ifdef __has_feature 77028f6c2f2SEnji Cooper #define GTEST_HAVE_FEATURE_(x) __has_feature(x) 77128f6c2f2SEnji Cooper #else 77228f6c2f2SEnji Cooper #define GTEST_HAVE_FEATURE_(x) 0 77328f6c2f2SEnji Cooper #endif 77428f6c2f2SEnji Cooper 77528f6c2f2SEnji Cooper // Use this annotation after a variable or parameter declaration to tell the 776*5ca8c28cSEnji Cooper // compiler the variable/parameter may be used. 77728f6c2f2SEnji Cooper // Example: 77828f6c2f2SEnji Cooper // 779*5ca8c28cSEnji Cooper // GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED int foo = bar(); 780*5ca8c28cSEnji Cooper // 781*5ca8c28cSEnji Cooper // This can be removed once we only support only C++17 or newer and 782*5ca8c28cSEnji Cooper // [[maybe_unused]] is available on all supported platforms. 783*5ca8c28cSEnji Cooper #if GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(maybe_unused) 784*5ca8c28cSEnji Cooper #define GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED [[maybe_unused]] 785*5ca8c28cSEnji Cooper #elif GTEST_HAVE_ATTRIBUTE_(unused) 786*5ca8c28cSEnji Cooper // This is inferior to [[maybe_unused]] as it can produce a 787*5ca8c28cSEnji Cooper // -Wused-but-marked-unused warning on optionally used symbols, but it is all we 788*5ca8c28cSEnji Cooper // have. 789*5ca8c28cSEnji Cooper #define GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED __attribute__((__unused__)) 79028f6c2f2SEnji Cooper #else 791*5ca8c28cSEnji Cooper #define GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED 792b89a7cc2SEnji Cooper #endif 793b89a7cc2SEnji Cooper 794b89a7cc2SEnji Cooper // Use this annotation before a function that takes a printf format string. 79528f6c2f2SEnji Cooper #if GTEST_HAVE_ATTRIBUTE_(format) && defined(__MINGW_PRINTF_FORMAT) 796b89a7cc2SEnji Cooper // MinGW has two different printf implementations. Ensure the format macro 797b89a7cc2SEnji Cooper // matches the selected implementation. See 798b89a7cc2SEnji Cooper // https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/. 799b89a7cc2SEnji Cooper #define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \ 80028f6c2f2SEnji Cooper __attribute__((format(__MINGW_PRINTF_FORMAT, string_index, first_to_check))) 80128f6c2f2SEnji Cooper #elif GTEST_HAVE_ATTRIBUTE_(format) 802b89a7cc2SEnji Cooper #define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \ 80328f6c2f2SEnji Cooper __attribute__((format(printf, string_index, first_to_check))) 804b89a7cc2SEnji Cooper #else 805b89a7cc2SEnji Cooper #define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) 806b89a7cc2SEnji Cooper #endif 807b89a7cc2SEnji Cooper 808b89a7cc2SEnji Cooper // Tell the compiler to warn about unused return values for functions declared 809b89a7cc2SEnji Cooper // with this macro. The macro should be used on function declarations 810b89a7cc2SEnji Cooper // following the argument list: 811b89a7cc2SEnji Cooper // 812b89a7cc2SEnji Cooper // Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_; 81328f6c2f2SEnji Cooper #if GTEST_HAVE_ATTRIBUTE_(warn_unused_result) 814b89a7cc2SEnji Cooper #define GTEST_MUST_USE_RESULT_ __attribute__((warn_unused_result)) 815b89a7cc2SEnji Cooper #else 816b89a7cc2SEnji Cooper #define GTEST_MUST_USE_RESULT_ 81728f6c2f2SEnji Cooper #endif 818b89a7cc2SEnji Cooper 819b89a7cc2SEnji Cooper // MS C++ compiler emits warning when a conditional expression is compile time 820b89a7cc2SEnji Cooper // constant. In some contexts this warning is false positive and needs to be 821b89a7cc2SEnji Cooper // suppressed. Use the following two macros in such cases: 822b89a7cc2SEnji Cooper // 823b89a7cc2SEnji Cooper // GTEST_INTENTIONAL_CONST_COND_PUSH_() 824b89a7cc2SEnji Cooper // while (true) { 825b89a7cc2SEnji Cooper // GTEST_INTENTIONAL_CONST_COND_POP_() 826b89a7cc2SEnji Cooper // } 827b89a7cc2SEnji Cooper #define GTEST_INTENTIONAL_CONST_COND_PUSH_() \ 828b89a7cc2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127) 82928f6c2f2SEnji Cooper #define GTEST_INTENTIONAL_CONST_COND_POP_() GTEST_DISABLE_MSC_WARNINGS_POP_() 830b89a7cc2SEnji Cooper 831b89a7cc2SEnji Cooper // Determine whether the compiler supports Microsoft's Structured Exception 832b89a7cc2SEnji Cooper // Handling. This is supported by several Windows compilers but generally 833b89a7cc2SEnji Cooper // does not exist on any other system. 834b89a7cc2SEnji Cooper #ifndef GTEST_HAS_SEH 835b89a7cc2SEnji Cooper // The user didn't tell us, so we need to figure it out. 836b89a7cc2SEnji Cooper 837b89a7cc2SEnji Cooper #if defined(_MSC_VER) || defined(__BORLANDC__) 838b89a7cc2SEnji Cooper // These two compilers are known to support SEH. 839b89a7cc2SEnji Cooper #define GTEST_HAS_SEH 1 840b89a7cc2SEnji Cooper #else 841b89a7cc2SEnji Cooper // Assume no SEH. 842b89a7cc2SEnji Cooper #define GTEST_HAS_SEH 0 843b89a7cc2SEnji Cooper #endif 844b89a7cc2SEnji Cooper 845b89a7cc2SEnji Cooper #endif // GTEST_HAS_SEH 846b89a7cc2SEnji Cooper 84728f6c2f2SEnji Cooper #ifndef GTEST_IS_THREADSAFE 84828f6c2f2SEnji Cooper 84928f6c2f2SEnji Cooper #if (GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ || \ 85028f6c2f2SEnji Cooper (defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_PHONE) && \ 85128f6c2f2SEnji Cooper !defined(GTEST_OS_WINDOWS_RT)) || \ 85228f6c2f2SEnji Cooper GTEST_HAS_PTHREAD) 85328f6c2f2SEnji Cooper #define GTEST_IS_THREADSAFE 1 85428f6c2f2SEnji Cooper #endif 85528f6c2f2SEnji Cooper 85628f6c2f2SEnji Cooper #endif // GTEST_IS_THREADSAFE 85728f6c2f2SEnji Cooper 85828f6c2f2SEnji Cooper #ifdef GTEST_IS_THREADSAFE 85928f6c2f2SEnji Cooper // Some platforms don't support including these threading related headers. 86028f6c2f2SEnji Cooper #include <condition_variable> // NOLINT 86128f6c2f2SEnji Cooper #include <mutex> // NOLINT 86228f6c2f2SEnji Cooper #endif // GTEST_IS_THREADSAFE 86328f6c2f2SEnji Cooper 864b89a7cc2SEnji Cooper // GTEST_API_ qualifies all symbols that must be exported. The definitions below 865b89a7cc2SEnji Cooper // are guarded by #ifndef to give embedders a chance to define GTEST_API_ in 866b89a7cc2SEnji Cooper // gtest/internal/custom/gtest-port.h 867b89a7cc2SEnji Cooper #ifndef GTEST_API_ 868b89a7cc2SEnji Cooper 869b89a7cc2SEnji Cooper #ifdef _MSC_VER 870*5ca8c28cSEnji Cooper #if defined(GTEST_LINKED_AS_SHARED_LIBRARY) && GTEST_LINKED_AS_SHARED_LIBRARY 871b89a7cc2SEnji Cooper #define GTEST_API_ __declspec(dllimport) 872*5ca8c28cSEnji Cooper #elif defined(GTEST_CREATE_SHARED_LIBRARY) && GTEST_CREATE_SHARED_LIBRARY 873b89a7cc2SEnji Cooper #define GTEST_API_ __declspec(dllexport) 874b89a7cc2SEnji Cooper #endif 87528f6c2f2SEnji Cooper #elif GTEST_HAVE_ATTRIBUTE_(visibility) 876b89a7cc2SEnji Cooper #define GTEST_API_ __attribute__((visibility("default"))) 877b89a7cc2SEnji Cooper #endif // _MSC_VER 878b89a7cc2SEnji Cooper 879b89a7cc2SEnji Cooper #endif // GTEST_API_ 880b89a7cc2SEnji Cooper 881b89a7cc2SEnji Cooper #ifndef GTEST_API_ 882b89a7cc2SEnji Cooper #define GTEST_API_ 883b89a7cc2SEnji Cooper #endif // GTEST_API_ 884b89a7cc2SEnji Cooper 885b89a7cc2SEnji Cooper #ifndef GTEST_DEFAULT_DEATH_TEST_STYLE 886b89a7cc2SEnji Cooper #define GTEST_DEFAULT_DEATH_TEST_STYLE "fast" 887b89a7cc2SEnji Cooper #endif // GTEST_DEFAULT_DEATH_TEST_STYLE 888b89a7cc2SEnji Cooper 88928f6c2f2SEnji Cooper #if GTEST_HAVE_ATTRIBUTE_(noinline) 890b89a7cc2SEnji Cooper // Ask the compiler to never inline a given function. 891b89a7cc2SEnji Cooper #define GTEST_NO_INLINE_ __attribute__((noinline)) 892b89a7cc2SEnji Cooper #else 893b89a7cc2SEnji Cooper #define GTEST_NO_INLINE_ 894b89a7cc2SEnji Cooper #endif 895b89a7cc2SEnji Cooper 89628f6c2f2SEnji Cooper #if GTEST_HAVE_ATTRIBUTE_(disable_tail_calls) 89728f6c2f2SEnji Cooper // Ask the compiler not to perform tail call optimization inside 89828f6c2f2SEnji Cooper // the marked function. 89928f6c2f2SEnji Cooper #define GTEST_NO_TAIL_CALL_ __attribute__((disable_tail_calls)) 90028f6c2f2SEnji Cooper #elif defined(__GNUC__) && !defined(__NVCOMPILER) 90128f6c2f2SEnji Cooper #define GTEST_NO_TAIL_CALL_ \ 90228f6c2f2SEnji Cooper __attribute__((optimize("no-optimize-sibling-calls"))) 90328f6c2f2SEnji Cooper #else 90428f6c2f2SEnji Cooper #define GTEST_NO_TAIL_CALL_ 90528f6c2f2SEnji Cooper #endif 90628f6c2f2SEnji Cooper 907b89a7cc2SEnji Cooper // _LIBCPP_VERSION is defined by the libc++ library from the LLVM project. 908b89a7cc2SEnji Cooper #if !defined(GTEST_HAS_CXXABI_H_) 909b89a7cc2SEnji Cooper #if defined(__GLIBCXX__) || (defined(_LIBCPP_VERSION) && !defined(_MSC_VER)) 910b89a7cc2SEnji Cooper #define GTEST_HAS_CXXABI_H_ 1 911b89a7cc2SEnji Cooper #else 912b89a7cc2SEnji Cooper #define GTEST_HAS_CXXABI_H_ 0 913b89a7cc2SEnji Cooper #endif 914b89a7cc2SEnji Cooper #endif 915b89a7cc2SEnji Cooper 916b89a7cc2SEnji Cooper // A function level attribute to disable checking for use of uninitialized 917b89a7cc2SEnji Cooper // memory when built with MemorySanitizer. 91828f6c2f2SEnji Cooper #if GTEST_HAVE_ATTRIBUTE_(no_sanitize_memory) 91928f6c2f2SEnji Cooper #define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ __attribute__((no_sanitize_memory)) 920b89a7cc2SEnji Cooper #else 921b89a7cc2SEnji Cooper #define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ 92228f6c2f2SEnji Cooper #endif 923b89a7cc2SEnji Cooper 924b89a7cc2SEnji Cooper // A function level attribute to disable AddressSanitizer instrumentation. 92528f6c2f2SEnji Cooper #if GTEST_HAVE_ATTRIBUTE_(no_sanitize_address) 926b89a7cc2SEnji Cooper #define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ \ 927b89a7cc2SEnji Cooper __attribute__((no_sanitize_address)) 928b89a7cc2SEnji Cooper #else 929b89a7cc2SEnji Cooper #define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ 93028f6c2f2SEnji Cooper #endif 93128f6c2f2SEnji Cooper 93228f6c2f2SEnji Cooper // A function level attribute to disable HWAddressSanitizer instrumentation. 93328f6c2f2SEnji Cooper #if GTEST_HAVE_FEATURE_(hwaddress_sanitizer) && \ 93428f6c2f2SEnji Cooper GTEST_HAVE_ATTRIBUTE_(no_sanitize) 93528f6c2f2SEnji Cooper #define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ \ 93628f6c2f2SEnji Cooper __attribute__((no_sanitize("hwaddress"))) 937b89a7cc2SEnji Cooper #else 93828f6c2f2SEnji Cooper #define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ 93928f6c2f2SEnji Cooper #endif 940b89a7cc2SEnji Cooper 941b89a7cc2SEnji Cooper // A function level attribute to disable ThreadSanitizer instrumentation. 94228f6c2f2SEnji Cooper #if GTEST_HAVE_ATTRIBUTE_(no_sanitize_thread) 94328f6c2f2SEnji Cooper #define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ __attribute((no_sanitize_thread)) 944b89a7cc2SEnji Cooper #else 945b89a7cc2SEnji Cooper #define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ 94628f6c2f2SEnji Cooper #endif 947b89a7cc2SEnji Cooper 948b89a7cc2SEnji Cooper namespace testing { 949b89a7cc2SEnji Cooper 950b89a7cc2SEnji Cooper class Message; 951b89a7cc2SEnji Cooper 95228f6c2f2SEnji Cooper // Legacy imports for backwards compatibility. 95328f6c2f2SEnji Cooper // New code should use std:: names directly. 95428f6c2f2SEnji Cooper using std::get; 95528f6c2f2SEnji Cooper using std::make_tuple; 95628f6c2f2SEnji Cooper using std::tuple; 95728f6c2f2SEnji Cooper using std::tuple_element; 95828f6c2f2SEnji Cooper using std::tuple_size; 959b89a7cc2SEnji Cooper 960b89a7cc2SEnji Cooper namespace internal { 961b89a7cc2SEnji Cooper 962b89a7cc2SEnji Cooper // A secret type that Google Test users don't know about. It has no 96328f6c2f2SEnji Cooper // accessible constructors on purpose. Therefore it's impossible to create a 964b89a7cc2SEnji Cooper // Secret object, which is what we want. 96528f6c2f2SEnji Cooper class Secret { 96628f6c2f2SEnji Cooper Secret(const Secret&) = delete; 967b89a7cc2SEnji Cooper }; 968b89a7cc2SEnji Cooper 969b89a7cc2SEnji Cooper // A helper for suppressing warnings on constant condition. It just 970b89a7cc2SEnji Cooper // returns 'condition'. 971b89a7cc2SEnji Cooper GTEST_API_ bool IsTrue(bool condition); 972b89a7cc2SEnji Cooper 97328f6c2f2SEnji Cooper // Defines RE. 974b89a7cc2SEnji Cooper 97528f6c2f2SEnji Cooper #ifdef GTEST_USES_RE2 97628f6c2f2SEnji Cooper 97728f6c2f2SEnji Cooper // This is almost `using RE = ::RE2`, except it is copy-constructible, and it 97828f6c2f2SEnji Cooper // needs to disambiguate the `std::string`, `absl::string_view`, and `const 97928f6c2f2SEnji Cooper // char*` constructors. 98028f6c2f2SEnji Cooper class GTEST_API_ RE { 981b89a7cc2SEnji Cooper public: 98228f6c2f2SEnji Cooper RE(absl::string_view regex) : regex_(regex) {} // NOLINT 98328f6c2f2SEnji Cooper RE(const char* regex) : RE(absl::string_view(regex)) {} // NOLINT 98428f6c2f2SEnji Cooper RE(const std::string& regex) : RE(absl::string_view(regex)) {} // NOLINT 98528f6c2f2SEnji Cooper RE(const RE& other) : RE(other.pattern()) {} 986b89a7cc2SEnji Cooper 98728f6c2f2SEnji Cooper const std::string& pattern() const { return regex_.pattern(); } 988b89a7cc2SEnji Cooper 98928f6c2f2SEnji Cooper static bool FullMatch(absl::string_view str, const RE& re) { 99028f6c2f2SEnji Cooper return RE2::FullMatch(str, re.regex_); 991b89a7cc2SEnji Cooper } 99228f6c2f2SEnji Cooper static bool PartialMatch(absl::string_view str, const RE& re) { 99328f6c2f2SEnji Cooper return RE2::PartialMatch(str, re.regex_); 994b89a7cc2SEnji Cooper } 995b89a7cc2SEnji Cooper 996b89a7cc2SEnji Cooper private: 99728f6c2f2SEnji Cooper RE2 regex_; 998b89a7cc2SEnji Cooper }; 999b89a7cc2SEnji Cooper 100028f6c2f2SEnji Cooper #elif defined(GTEST_USES_POSIX_RE) || defined(GTEST_USES_SIMPLE_RE) 100128f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ 100228f6c2f2SEnji Cooper /* class A needs to have dll-interface to be used by clients of class B */) 1003b89a7cc2SEnji Cooper 1004b89a7cc2SEnji Cooper // A simple C++ wrapper for <regex.h>. It uses the POSIX Extended 1005b89a7cc2SEnji Cooper // Regular Expression syntax. 1006b89a7cc2SEnji Cooper class GTEST_API_ RE { 1007b89a7cc2SEnji Cooper public: 1008b89a7cc2SEnji Cooper // A copy constructor is required by the Standard to initialize object 1009b89a7cc2SEnji Cooper // references from r-values. 1010b89a7cc2SEnji Cooper RE(const RE& other) { Init(other.pattern()); } 1011b89a7cc2SEnji Cooper 1012b89a7cc2SEnji Cooper // Constructs an RE from a string. 1013b89a7cc2SEnji Cooper RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT 1014b89a7cc2SEnji Cooper 1015b89a7cc2SEnji Cooper RE(const char* regex) { Init(regex); } // NOLINT 1016b89a7cc2SEnji Cooper ~RE(); 1017b89a7cc2SEnji Cooper 1018b89a7cc2SEnji Cooper // Returns the string representation of the regex. 101928f6c2f2SEnji Cooper const char* pattern() const { return pattern_.c_str(); } 1020b89a7cc2SEnji Cooper 102128f6c2f2SEnji Cooper // FullMatch(str, re) returns true if and only if regular expression re 102228f6c2f2SEnji Cooper // matches the entire str. 102328f6c2f2SEnji Cooper // PartialMatch(str, re) returns true if and only if regular expression re 1024b89a7cc2SEnji Cooper // matches a substring of str (including str itself). 1025b89a7cc2SEnji Cooper static bool FullMatch(const ::std::string& str, const RE& re) { 1026b89a7cc2SEnji Cooper return FullMatch(str.c_str(), re); 1027b89a7cc2SEnji Cooper } 1028b89a7cc2SEnji Cooper static bool PartialMatch(const ::std::string& str, const RE& re) { 1029b89a7cc2SEnji Cooper return PartialMatch(str.c_str(), re); 1030b89a7cc2SEnji Cooper } 1031b89a7cc2SEnji Cooper 1032b89a7cc2SEnji Cooper static bool FullMatch(const char* str, const RE& re); 1033b89a7cc2SEnji Cooper static bool PartialMatch(const char* str, const RE& re); 1034b89a7cc2SEnji Cooper 1035b89a7cc2SEnji Cooper private: 1036b89a7cc2SEnji Cooper void Init(const char* regex); 103728f6c2f2SEnji Cooper std::string pattern_; 1038b89a7cc2SEnji Cooper bool is_valid_; 1039b89a7cc2SEnji Cooper 104028f6c2f2SEnji Cooper #ifdef GTEST_USES_POSIX_RE 1041b89a7cc2SEnji Cooper 1042b89a7cc2SEnji Cooper regex_t full_regex_; // For FullMatch(). 1043b89a7cc2SEnji Cooper regex_t partial_regex_; // For PartialMatch(). 1044b89a7cc2SEnji Cooper 1045b89a7cc2SEnji Cooper #else // GTEST_USES_SIMPLE_RE 1046b89a7cc2SEnji Cooper 104728f6c2f2SEnji Cooper std::string full_pattern_; // For FullMatch(); 1048b89a7cc2SEnji Cooper 1049b89a7cc2SEnji Cooper #endif 1050b89a7cc2SEnji Cooper }; 105128f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 105228f6c2f2SEnji Cooper #endif // ::testing::internal::RE implementation 1053b89a7cc2SEnji Cooper 1054b89a7cc2SEnji Cooper // Formats a source file path and a line number as they would appear 1055b89a7cc2SEnji Cooper // in an error message from the compiler used to compile this code. 1056b89a7cc2SEnji Cooper GTEST_API_ ::std::string FormatFileLocation(const char* file, int line); 1057b89a7cc2SEnji Cooper 1058b89a7cc2SEnji Cooper // Formats a file location for compiler-independent XML output. 1059b89a7cc2SEnji Cooper // Although this function is not platform dependent, we put it next to 1060b89a7cc2SEnji Cooper // FormatFileLocation in order to contrast the two functions. 1061b89a7cc2SEnji Cooper GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file, 1062b89a7cc2SEnji Cooper int line); 1063b89a7cc2SEnji Cooper 1064b89a7cc2SEnji Cooper // Defines logging utilities: 1065b89a7cc2SEnji Cooper // GTEST_LOG_(severity) - logs messages at the specified severity level. The 1066b89a7cc2SEnji Cooper // message itself is streamed into the macro. 1067b89a7cc2SEnji Cooper // LogToStderr() - directs all log messages to stderr. 1068b89a7cc2SEnji Cooper // FlushInfoLog() - flushes informational log messages. 1069b89a7cc2SEnji Cooper 107028f6c2f2SEnji Cooper enum GTestLogSeverity { GTEST_INFO, GTEST_WARNING, GTEST_ERROR, GTEST_FATAL }; 1071b89a7cc2SEnji Cooper 1072b89a7cc2SEnji Cooper // Formats log entry severity, provides a stream object for streaming the 1073b89a7cc2SEnji Cooper // log message, and terminates the message with a newline when going out of 1074b89a7cc2SEnji Cooper // scope. 1075b89a7cc2SEnji Cooper class GTEST_API_ GTestLog { 1076b89a7cc2SEnji Cooper public: 1077b89a7cc2SEnji Cooper GTestLog(GTestLogSeverity severity, const char* file, int line); 1078b89a7cc2SEnji Cooper 1079b89a7cc2SEnji Cooper // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program. 1080b89a7cc2SEnji Cooper ~GTestLog(); 1081b89a7cc2SEnji Cooper 1082b89a7cc2SEnji Cooper ::std::ostream& GetStream() { return ::std::cerr; } 1083b89a7cc2SEnji Cooper 1084b89a7cc2SEnji Cooper private: 1085b89a7cc2SEnji Cooper const GTestLogSeverity severity_; 1086b89a7cc2SEnji Cooper 108728f6c2f2SEnji Cooper GTestLog(const GTestLog&) = delete; 108828f6c2f2SEnji Cooper GTestLog& operator=(const GTestLog&) = delete; 1089b89a7cc2SEnji Cooper }; 1090b89a7cc2SEnji Cooper 1091b89a7cc2SEnji Cooper #if !defined(GTEST_LOG_) 1092b89a7cc2SEnji Cooper 1093b89a7cc2SEnji Cooper #define GTEST_LOG_(severity) \ 1094b89a7cc2SEnji Cooper ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \ 109528f6c2f2SEnji Cooper __FILE__, __LINE__) \ 109628f6c2f2SEnji Cooper .GetStream() 1097b89a7cc2SEnji Cooper 1098b89a7cc2SEnji Cooper inline void LogToStderr() {} 109928f6c2f2SEnji Cooper inline void FlushInfoLog() { fflush(nullptr); } 1100b89a7cc2SEnji Cooper 1101b89a7cc2SEnji Cooper #endif // !defined(GTEST_LOG_) 1102b89a7cc2SEnji Cooper 1103b89a7cc2SEnji Cooper #if !defined(GTEST_CHECK_) 1104b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE. 1105b89a7cc2SEnji Cooper // 1106b89a7cc2SEnji Cooper // GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition 1107b89a7cc2SEnji Cooper // is not satisfied. 110828f6c2f2SEnji Cooper // Synopsis: 1109b89a7cc2SEnji Cooper // GTEST_CHECK_(boolean_condition); 1110b89a7cc2SEnji Cooper // or 1111b89a7cc2SEnji Cooper // GTEST_CHECK_(boolean_condition) << "Additional message"; 1112b89a7cc2SEnji Cooper // 1113b89a7cc2SEnji Cooper // This checks the condition and if the condition is not satisfied 1114b89a7cc2SEnji Cooper // it prints message about the condition violation, including the 1115b89a7cc2SEnji Cooper // condition itself, plus additional message streamed into it, if any, 1116b89a7cc2SEnji Cooper // and then it aborts the program. It aborts the program irrespective of 1117b89a7cc2SEnji Cooper // whether it is built in the debug mode or not. 1118b89a7cc2SEnji Cooper #define GTEST_CHECK_(condition) \ 1119b89a7cc2SEnji Cooper GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 1120b89a7cc2SEnji Cooper if (::testing::internal::IsTrue(condition)) \ 1121b89a7cc2SEnji Cooper ; \ 1122b89a7cc2SEnji Cooper else \ 1123b89a7cc2SEnji Cooper GTEST_LOG_(FATAL) << "Condition " #condition " failed. " 1124b89a7cc2SEnji Cooper #endif // !defined(GTEST_CHECK_) 1125b89a7cc2SEnji Cooper 1126b89a7cc2SEnji Cooper // An all-mode assert to verify that the given POSIX-style function 1127b89a7cc2SEnji Cooper // call returns 0 (indicating success). Known limitation: this 1128b89a7cc2SEnji Cooper // doesn't expand to a balanced 'if' statement, so enclose the macro 1129b89a7cc2SEnji Cooper // in {} if you need to use it as the only statement in an 'if' 1130b89a7cc2SEnji Cooper // branch. 1131b89a7cc2SEnji Cooper #define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \ 1132b89a7cc2SEnji Cooper if (const int gtest_error = (posix_call)) \ 113328f6c2f2SEnji Cooper GTEST_LOG_(FATAL) << #posix_call << "failed with error " << gtest_error 1134b89a7cc2SEnji Cooper 1135b89a7cc2SEnji Cooper // Transforms "T" into "const T&" according to standard reference collapsing 1136b89a7cc2SEnji Cooper // rules (this is only needed as a backport for C++98 compilers that do not 1137b89a7cc2SEnji Cooper // support reference collapsing). Specifically, it transforms: 1138b89a7cc2SEnji Cooper // 1139b89a7cc2SEnji Cooper // char ==> const char& 1140b89a7cc2SEnji Cooper // const char ==> const char& 1141b89a7cc2SEnji Cooper // char& ==> char& 1142b89a7cc2SEnji Cooper // const char& ==> const char& 1143b89a7cc2SEnji Cooper // 1144b89a7cc2SEnji Cooper // Note that the non-const reference will not have "const" added. This is 1145b89a7cc2SEnji Cooper // standard, and necessary so that "T" can always bind to "const T&". 1146b89a7cc2SEnji Cooper template <typename T> 114728f6c2f2SEnji Cooper struct ConstRef { 114828f6c2f2SEnji Cooper typedef const T& type; 114928f6c2f2SEnji Cooper }; 1150b89a7cc2SEnji Cooper template <typename T> 115128f6c2f2SEnji Cooper struct ConstRef<T&> { 115228f6c2f2SEnji Cooper typedef T& type; 115328f6c2f2SEnji Cooper }; 1154b89a7cc2SEnji Cooper 1155b89a7cc2SEnji Cooper // The argument T must depend on some template parameters. 1156b89a7cc2SEnji Cooper #define GTEST_REFERENCE_TO_CONST_(T) \ 1157b89a7cc2SEnji Cooper typename ::testing::internal::ConstRef<T>::type 1158b89a7cc2SEnji Cooper 1159b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. 1160b89a7cc2SEnji Cooper // 1161b89a7cc2SEnji Cooper // Use ImplicitCast_ as a safe version of static_cast for upcasting in 1162b89a7cc2SEnji Cooper // the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a 1163b89a7cc2SEnji Cooper // const Foo*). When you use ImplicitCast_, the compiler checks that 1164b89a7cc2SEnji Cooper // the cast is safe. Such explicit ImplicitCast_s are necessary in 1165b89a7cc2SEnji Cooper // surprisingly many situations where C++ demands an exact type match 116628f6c2f2SEnji Cooper // instead of an argument type convertible to a target type. 1167b89a7cc2SEnji Cooper // 1168b89a7cc2SEnji Cooper // The syntax for using ImplicitCast_ is the same as for static_cast: 1169b89a7cc2SEnji Cooper // 1170b89a7cc2SEnji Cooper // ImplicitCast_<ToType>(expr) 1171b89a7cc2SEnji Cooper // 1172b89a7cc2SEnji Cooper // ImplicitCast_ would have been part of the C++ standard library, 1173b89a7cc2SEnji Cooper // but the proposal was submitted too late. It will probably make 1174b89a7cc2SEnji Cooper // its way into the language in the future. 1175b89a7cc2SEnji Cooper // 1176b89a7cc2SEnji Cooper // This relatively ugly name is intentional. It prevents clashes with 1177b89a7cc2SEnji Cooper // similar functions users may have (e.g., implicit_cast). The internal 1178b89a7cc2SEnji Cooper // namespace alone is not enough because the function can be found by ADL. 1179b89a7cc2SEnji Cooper template <typename To> 118028f6c2f2SEnji Cooper inline To ImplicitCast_(To x) { 118128f6c2f2SEnji Cooper return x; 1182b89a7cc2SEnji Cooper } 1183b89a7cc2SEnji Cooper 1184b89a7cc2SEnji Cooper // Downcasts the pointer of type Base to Derived. 1185b89a7cc2SEnji Cooper // Derived must be a subclass of Base. The parameter MUST 1186b89a7cc2SEnji Cooper // point to a class of type Derived, not any subclass of it. 1187b89a7cc2SEnji Cooper // When RTTI is available, the function performs a runtime 1188b89a7cc2SEnji Cooper // check to enforce this. 1189b89a7cc2SEnji Cooper template <class Derived, class Base> 1190b89a7cc2SEnji Cooper Derived* CheckedDowncastToActualType(Base* base) { 119128f6c2f2SEnji Cooper static_assert(std::is_base_of<Base, Derived>::value, 119228f6c2f2SEnji Cooper "target type not derived from source type"); 1193b89a7cc2SEnji Cooper #if GTEST_HAS_RTTI 119428f6c2f2SEnji Cooper GTEST_CHECK_(base == nullptr || dynamic_cast<Derived*>(base) != nullptr); 1195b89a7cc2SEnji Cooper #endif 119628f6c2f2SEnji Cooper return static_cast<Derived*>(base); 1197b89a7cc2SEnji Cooper } 1198b89a7cc2SEnji Cooper 1199b89a7cc2SEnji Cooper #if GTEST_HAS_STREAM_REDIRECTION 1200b89a7cc2SEnji Cooper 1201b89a7cc2SEnji Cooper // Defines the stderr capturer: 1202b89a7cc2SEnji Cooper // CaptureStdout - starts capturing stdout. 1203b89a7cc2SEnji Cooper // GetCapturedStdout - stops capturing stdout and returns the captured string. 1204b89a7cc2SEnji Cooper // CaptureStderr - starts capturing stderr. 1205b89a7cc2SEnji Cooper // GetCapturedStderr - stops capturing stderr and returns the captured string. 1206b89a7cc2SEnji Cooper // 1207b89a7cc2SEnji Cooper GTEST_API_ void CaptureStdout(); 1208b89a7cc2SEnji Cooper GTEST_API_ std::string GetCapturedStdout(); 1209b89a7cc2SEnji Cooper GTEST_API_ void CaptureStderr(); 1210b89a7cc2SEnji Cooper GTEST_API_ std::string GetCapturedStderr(); 1211b89a7cc2SEnji Cooper 1212b89a7cc2SEnji Cooper #endif // GTEST_HAS_STREAM_REDIRECTION 1213b89a7cc2SEnji Cooper // Returns the size (in bytes) of a file. 1214b89a7cc2SEnji Cooper GTEST_API_ size_t GetFileSize(FILE* file); 1215b89a7cc2SEnji Cooper 1216b89a7cc2SEnji Cooper // Reads the entire content of a file as a string. 1217b89a7cc2SEnji Cooper GTEST_API_ std::string ReadEntireFile(FILE* file); 1218b89a7cc2SEnji Cooper 1219b89a7cc2SEnji Cooper // All command line arguments. 1220b89a7cc2SEnji Cooper GTEST_API_ std::vector<std::string> GetArgvs(); 1221b89a7cc2SEnji Cooper 122228f6c2f2SEnji Cooper #ifdef GTEST_HAS_DEATH_TEST 1223b89a7cc2SEnji Cooper 1224b89a7cc2SEnji Cooper std::vector<std::string> GetInjectableArgvs(); 1225b89a7cc2SEnji Cooper // Deprecated: pass the args vector by value instead. 1226b89a7cc2SEnji Cooper void SetInjectableArgvs(const std::vector<std::string>* new_argvs); 1227b89a7cc2SEnji Cooper void SetInjectableArgvs(const std::vector<std::string>& new_argvs); 1228b89a7cc2SEnji Cooper void ClearInjectableArgvs(); 1229b89a7cc2SEnji Cooper 1230b89a7cc2SEnji Cooper #endif // GTEST_HAS_DEATH_TEST 1231b89a7cc2SEnji Cooper 1232b89a7cc2SEnji Cooper // Defines synchronization primitives. 123328f6c2f2SEnji Cooper #ifdef GTEST_IS_THREADSAFE 1234b89a7cc2SEnji Cooper 123528f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS 1236b89a7cc2SEnji Cooper // Provides leak-safe Windows kernel handle ownership. 1237b89a7cc2SEnji Cooper // Used in death tests and in threading support. 1238b89a7cc2SEnji Cooper class GTEST_API_ AutoHandle { 1239b89a7cc2SEnji Cooper public: 1240b89a7cc2SEnji Cooper // Assume that Win32 HANDLE type is equivalent to void*. Doing so allows us to 1241b89a7cc2SEnji Cooper // avoid including <windows.h> in this header file. Including <windows.h> is 1242b89a7cc2SEnji Cooper // undesirable because it defines a lot of symbols and macros that tend to 1243b89a7cc2SEnji Cooper // conflict with client code. This assumption is verified by 1244b89a7cc2SEnji Cooper // WindowsTypesTest.HANDLEIsVoidStar. 1245b89a7cc2SEnji Cooper typedef void* Handle; 1246b89a7cc2SEnji Cooper AutoHandle(); 1247b89a7cc2SEnji Cooper explicit AutoHandle(Handle handle); 1248b89a7cc2SEnji Cooper 1249b89a7cc2SEnji Cooper ~AutoHandle(); 1250b89a7cc2SEnji Cooper 1251b89a7cc2SEnji Cooper Handle Get() const; 1252b89a7cc2SEnji Cooper void Reset(); 1253b89a7cc2SEnji Cooper void Reset(Handle handle); 1254b89a7cc2SEnji Cooper 1255b89a7cc2SEnji Cooper private: 125628f6c2f2SEnji Cooper // Returns true if and only if the handle is a valid handle object that can be 125728f6c2f2SEnji Cooper // closed. 1258b89a7cc2SEnji Cooper bool IsCloseable() const; 1259b89a7cc2SEnji Cooper 1260b89a7cc2SEnji Cooper Handle handle_; 1261b89a7cc2SEnji Cooper 126228f6c2f2SEnji Cooper AutoHandle(const AutoHandle&) = delete; 126328f6c2f2SEnji Cooper AutoHandle& operator=(const AutoHandle&) = delete; 1264b89a7cc2SEnji Cooper }; 126528f6c2f2SEnji Cooper #endif 126628f6c2f2SEnji Cooper 126728f6c2f2SEnji Cooper #if GTEST_HAS_NOTIFICATION_ 126828f6c2f2SEnji Cooper // Notification has already been imported into the namespace. 126928f6c2f2SEnji Cooper // Nothing to do here. 127028f6c2f2SEnji Cooper 127128f6c2f2SEnji Cooper #else 127228f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ 127328f6c2f2SEnji Cooper /* class A needs to have dll-interface to be used by clients of class B */) 1274b89a7cc2SEnji Cooper 1275b89a7cc2SEnji Cooper // Allows a controller thread to pause execution of newly created 1276b89a7cc2SEnji Cooper // threads until notified. Instances of this class must be created 1277b89a7cc2SEnji Cooper // and destroyed in the controller thread. 1278b89a7cc2SEnji Cooper // 1279b89a7cc2SEnji Cooper // This class is only for testing Google Test's own constructs. Do not 1280b89a7cc2SEnji Cooper // use it in user tests, either directly or indirectly. 128128f6c2f2SEnji Cooper // TODO(b/203539622): Replace unconditionally with absl::Notification. 1282b89a7cc2SEnji Cooper class GTEST_API_ Notification { 1283b89a7cc2SEnji Cooper public: 128428f6c2f2SEnji Cooper Notification() : notified_(false) {} 128528f6c2f2SEnji Cooper Notification(const Notification&) = delete; 128628f6c2f2SEnji Cooper Notification& operator=(const Notification&) = delete; 128728f6c2f2SEnji Cooper 128828f6c2f2SEnji Cooper // Notifies all threads created with this notification to start. Must 128928f6c2f2SEnji Cooper // be called from the controller thread. 129028f6c2f2SEnji Cooper void Notify() { 129128f6c2f2SEnji Cooper std::lock_guard<std::mutex> lock(mu_); 129228f6c2f2SEnji Cooper notified_ = true; 129328f6c2f2SEnji Cooper cv_.notify_all(); 129428f6c2f2SEnji Cooper } 129528f6c2f2SEnji Cooper 129628f6c2f2SEnji Cooper // Blocks until the controller thread notifies. Must be called from a test 129728f6c2f2SEnji Cooper // thread. 129828f6c2f2SEnji Cooper void WaitForNotification() { 129928f6c2f2SEnji Cooper std::unique_lock<std::mutex> lock(mu_); 130028f6c2f2SEnji Cooper cv_.wait(lock, [this]() { return notified_; }); 130128f6c2f2SEnji Cooper } 1302b89a7cc2SEnji Cooper 1303b89a7cc2SEnji Cooper private: 130428f6c2f2SEnji Cooper std::mutex mu_; 130528f6c2f2SEnji Cooper std::condition_variable cv_; 130628f6c2f2SEnji Cooper bool notified_; 1307b89a7cc2SEnji Cooper }; 130828f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 1309b89a7cc2SEnji Cooper #endif // GTEST_HAS_NOTIFICATION_ 1310b89a7cc2SEnji Cooper 1311b89a7cc2SEnji Cooper // On MinGW, we can have both GTEST_OS_WINDOWS and GTEST_HAS_PTHREAD 1312b89a7cc2SEnji Cooper // defined, but we don't want to use MinGW's pthreads implementation, which 1313b89a7cc2SEnji Cooper // has conformance problems with some versions of the POSIX standard. 131428f6c2f2SEnji Cooper #if GTEST_HAS_PTHREAD && !defined(GTEST_OS_WINDOWS_MINGW) 1315b89a7cc2SEnji Cooper 1316b89a7cc2SEnji Cooper // As a C-function, ThreadFuncWithCLinkage cannot be templated itself. 1317b89a7cc2SEnji Cooper // Consequently, it cannot select a correct instantiation of ThreadWithParam 1318b89a7cc2SEnji Cooper // in order to call its Run(). Introducing ThreadWithParamBase as a 1319b89a7cc2SEnji Cooper // non-templated base class for ThreadWithParam allows us to bypass this 1320b89a7cc2SEnji Cooper // problem. 1321b89a7cc2SEnji Cooper class ThreadWithParamBase { 1322b89a7cc2SEnji Cooper public: 132328f6c2f2SEnji Cooper virtual ~ThreadWithParamBase() = default; 1324b89a7cc2SEnji Cooper virtual void Run() = 0; 1325b89a7cc2SEnji Cooper }; 1326b89a7cc2SEnji Cooper 1327b89a7cc2SEnji Cooper // pthread_create() accepts a pointer to a function type with the C linkage. 1328b89a7cc2SEnji Cooper // According to the Standard (7.5/1), function types with different linkages 1329b89a7cc2SEnji Cooper // are different even if they are otherwise identical. Some compilers (for 1330b89a7cc2SEnji Cooper // example, SunStudio) treat them as different types. Since class methods 1331b89a7cc2SEnji Cooper // cannot be defined with C-linkage we need to define a free C-function to 1332b89a7cc2SEnji Cooper // pass into pthread_create(). 1333b89a7cc2SEnji Cooper extern "C" inline void* ThreadFuncWithCLinkage(void* thread) { 1334b89a7cc2SEnji Cooper static_cast<ThreadWithParamBase*>(thread)->Run(); 133528f6c2f2SEnji Cooper return nullptr; 1336b89a7cc2SEnji Cooper } 1337b89a7cc2SEnji Cooper 1338b89a7cc2SEnji Cooper // Helper class for testing Google Test's multi-threading constructs. 1339b89a7cc2SEnji Cooper // To use it, write: 1340b89a7cc2SEnji Cooper // 1341b89a7cc2SEnji Cooper // void ThreadFunc(int param) { /* Do things with param */ } 1342b89a7cc2SEnji Cooper // Notification thread_can_start; 1343b89a7cc2SEnji Cooper // ... 1344b89a7cc2SEnji Cooper // // The thread_can_start parameter is optional; you can supply NULL. 1345b89a7cc2SEnji Cooper // ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start); 1346b89a7cc2SEnji Cooper // thread_can_start.Notify(); 1347b89a7cc2SEnji Cooper // 1348b89a7cc2SEnji Cooper // These classes are only for testing Google Test's own constructs. Do 1349b89a7cc2SEnji Cooper // not use them in user tests, either directly or indirectly. 1350b89a7cc2SEnji Cooper template <typename T> 1351b89a7cc2SEnji Cooper class ThreadWithParam : public ThreadWithParamBase { 1352b89a7cc2SEnji Cooper public: 1353b89a7cc2SEnji Cooper typedef void UserThreadFunc(T); 1354b89a7cc2SEnji Cooper 1355b89a7cc2SEnji Cooper ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start) 1356b89a7cc2SEnji Cooper : func_(func), 1357b89a7cc2SEnji Cooper param_(param), 1358b89a7cc2SEnji Cooper thread_can_start_(thread_can_start), 1359b89a7cc2SEnji Cooper finished_(false) { 1360b89a7cc2SEnji Cooper ThreadWithParamBase* const base = this; 1361b89a7cc2SEnji Cooper // The thread can be created only after all fields except thread_ 1362b89a7cc2SEnji Cooper // have been initialized. 1363b89a7cc2SEnji Cooper GTEST_CHECK_POSIX_SUCCESS_( 136428f6c2f2SEnji Cooper pthread_create(&thread_, nullptr, &ThreadFuncWithCLinkage, base)); 1365b89a7cc2SEnji Cooper } 136628f6c2f2SEnji Cooper ~ThreadWithParam() override { Join(); } 1367b89a7cc2SEnji Cooper 1368b89a7cc2SEnji Cooper void Join() { 1369b89a7cc2SEnji Cooper if (!finished_) { 137028f6c2f2SEnji Cooper GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, nullptr)); 1371b89a7cc2SEnji Cooper finished_ = true; 1372b89a7cc2SEnji Cooper } 1373b89a7cc2SEnji Cooper } 1374b89a7cc2SEnji Cooper 137528f6c2f2SEnji Cooper void Run() override { 137628f6c2f2SEnji Cooper if (thread_can_start_ != nullptr) thread_can_start_->WaitForNotification(); 1377b89a7cc2SEnji Cooper func_(param_); 1378b89a7cc2SEnji Cooper } 1379b89a7cc2SEnji Cooper 1380b89a7cc2SEnji Cooper private: 1381b89a7cc2SEnji Cooper UserThreadFunc* const func_; // User-supplied thread function. 1382b89a7cc2SEnji Cooper const T param_; // User-supplied parameter to the thread function. 1383b89a7cc2SEnji Cooper // When non-NULL, used to block execution until the controller thread 1384b89a7cc2SEnji Cooper // notifies. 1385b89a7cc2SEnji Cooper Notification* const thread_can_start_; 138628f6c2f2SEnji Cooper bool finished_; // true if and only if we know that the thread function has 138728f6c2f2SEnji Cooper // finished. 1388b89a7cc2SEnji Cooper pthread_t thread_; // The native thread object. 1389b89a7cc2SEnji Cooper 139028f6c2f2SEnji Cooper ThreadWithParam(const ThreadWithParam&) = delete; 139128f6c2f2SEnji Cooper ThreadWithParam& operator=(const ThreadWithParam&) = delete; 1392b89a7cc2SEnji Cooper }; 1393b89a7cc2SEnji Cooper #endif // !GTEST_OS_WINDOWS && GTEST_HAS_PTHREAD || 1394b89a7cc2SEnji Cooper // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ 1395b89a7cc2SEnji Cooper 1396b89a7cc2SEnji Cooper #if GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ 1397b89a7cc2SEnji Cooper // Mutex and ThreadLocal have already been imported into the namespace. 1398b89a7cc2SEnji Cooper // Nothing to do here. 1399b89a7cc2SEnji Cooper 140028f6c2f2SEnji Cooper #elif defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_PHONE) && \ 140128f6c2f2SEnji Cooper !defined(GTEST_OS_WINDOWS_RT) 1402b89a7cc2SEnji Cooper 1403b89a7cc2SEnji Cooper // Mutex implements mutex on Windows platforms. It is used in conjunction 1404b89a7cc2SEnji Cooper // with class MutexLock: 1405b89a7cc2SEnji Cooper // 1406b89a7cc2SEnji Cooper // Mutex mutex; 1407b89a7cc2SEnji Cooper // ... 1408b89a7cc2SEnji Cooper // MutexLock lock(&mutex); // Acquires the mutex and releases it at the 1409b89a7cc2SEnji Cooper // // end of the current scope. 1410b89a7cc2SEnji Cooper // 1411b89a7cc2SEnji Cooper // A static Mutex *must* be defined or declared using one of the following 1412b89a7cc2SEnji Cooper // macros: 1413b89a7cc2SEnji Cooper // GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex); 1414b89a7cc2SEnji Cooper // GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex); 1415b89a7cc2SEnji Cooper // 1416b89a7cc2SEnji Cooper // (A non-static Mutex is defined/declared in the usual way). 1417b89a7cc2SEnji Cooper class GTEST_API_ Mutex { 1418b89a7cc2SEnji Cooper public: 1419b89a7cc2SEnji Cooper enum MutexType { kStatic = 0, kDynamic = 1 }; 1420b89a7cc2SEnji Cooper // We rely on kStaticMutex being 0 as it is to what the linker initializes 1421b89a7cc2SEnji Cooper // type_ in static mutexes. critical_section_ will be initialized lazily 1422b89a7cc2SEnji Cooper // in ThreadSafeLazyInit(). 1423b89a7cc2SEnji Cooper enum StaticConstructorSelector { kStaticMutex = 0 }; 1424b89a7cc2SEnji Cooper 1425b89a7cc2SEnji Cooper // This constructor intentionally does nothing. It relies on type_ being 1426b89a7cc2SEnji Cooper // statically initialized to 0 (effectively setting it to kStatic) and on 1427b89a7cc2SEnji Cooper // ThreadSafeLazyInit() to lazily initialize the rest of the members. 1428b89a7cc2SEnji Cooper explicit Mutex(StaticConstructorSelector /*dummy*/) {} 1429b89a7cc2SEnji Cooper 1430b89a7cc2SEnji Cooper Mutex(); 1431b89a7cc2SEnji Cooper ~Mutex(); 1432b89a7cc2SEnji Cooper 1433b89a7cc2SEnji Cooper void Lock(); 1434b89a7cc2SEnji Cooper 1435b89a7cc2SEnji Cooper void Unlock(); 1436b89a7cc2SEnji Cooper 1437b89a7cc2SEnji Cooper // Does nothing if the current thread holds the mutex. Otherwise, crashes 1438b89a7cc2SEnji Cooper // with high probability. 1439b89a7cc2SEnji Cooper void AssertHeld(); 1440b89a7cc2SEnji Cooper 1441b89a7cc2SEnji Cooper private: 1442b89a7cc2SEnji Cooper // Initializes owner_thread_id_ and critical_section_ in static mutexes. 1443b89a7cc2SEnji Cooper void ThreadSafeLazyInit(); 1444b89a7cc2SEnji Cooper 1445b89a7cc2SEnji Cooper // Per https://blogs.msdn.microsoft.com/oldnewthing/20040223-00/?p=40503, 1446b89a7cc2SEnji Cooper // we assume that 0 is an invalid value for thread IDs. 1447b89a7cc2SEnji Cooper unsigned int owner_thread_id_; 1448b89a7cc2SEnji Cooper 1449b89a7cc2SEnji Cooper // For static mutexes, we rely on these members being initialized to zeros 1450b89a7cc2SEnji Cooper // by the linker. 1451b89a7cc2SEnji Cooper MutexType type_; 1452b89a7cc2SEnji Cooper long critical_section_init_phase_; // NOLINT 1453b89a7cc2SEnji Cooper GTEST_CRITICAL_SECTION* critical_section_; 1454b89a7cc2SEnji Cooper 145528f6c2f2SEnji Cooper Mutex(const Mutex&) = delete; 145628f6c2f2SEnji Cooper Mutex& operator=(const Mutex&) = delete; 1457b89a7cc2SEnji Cooper }; 1458b89a7cc2SEnji Cooper 1459b89a7cc2SEnji Cooper #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ 1460b89a7cc2SEnji Cooper extern ::testing::internal::Mutex mutex 1461b89a7cc2SEnji Cooper 1462b89a7cc2SEnji Cooper #define GTEST_DEFINE_STATIC_MUTEX_(mutex) \ 1463b89a7cc2SEnji Cooper ::testing::internal::Mutex mutex(::testing::internal::Mutex::kStaticMutex) 1464b89a7cc2SEnji Cooper 1465b89a7cc2SEnji Cooper // We cannot name this class MutexLock because the ctor declaration would 1466b89a7cc2SEnji Cooper // conflict with a macro named MutexLock, which is defined on some 1467b89a7cc2SEnji Cooper // platforms. That macro is used as a defensive measure to prevent against 1468b89a7cc2SEnji Cooper // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than 1469b89a7cc2SEnji Cooper // "MutexLock l(&mu)". Hence the typedef trick below. 1470b89a7cc2SEnji Cooper class GTestMutexLock { 1471b89a7cc2SEnji Cooper public: 147228f6c2f2SEnji Cooper explicit GTestMutexLock(Mutex* mutex) : mutex_(mutex) { mutex_->Lock(); } 1473b89a7cc2SEnji Cooper 1474b89a7cc2SEnji Cooper ~GTestMutexLock() { mutex_->Unlock(); } 1475b89a7cc2SEnji Cooper 1476b89a7cc2SEnji Cooper private: 1477b89a7cc2SEnji Cooper Mutex* const mutex_; 1478b89a7cc2SEnji Cooper 147928f6c2f2SEnji Cooper GTestMutexLock(const GTestMutexLock&) = delete; 148028f6c2f2SEnji Cooper GTestMutexLock& operator=(const GTestMutexLock&) = delete; 1481b89a7cc2SEnji Cooper }; 1482b89a7cc2SEnji Cooper 1483b89a7cc2SEnji Cooper typedef GTestMutexLock MutexLock; 1484b89a7cc2SEnji Cooper 1485b89a7cc2SEnji Cooper // Base class for ValueHolder<T>. Allows a caller to hold and delete a value 1486b89a7cc2SEnji Cooper // without knowing its type. 1487b89a7cc2SEnji Cooper class ThreadLocalValueHolderBase { 1488b89a7cc2SEnji Cooper public: 1489b89a7cc2SEnji Cooper virtual ~ThreadLocalValueHolderBase() {} 1490b89a7cc2SEnji Cooper }; 1491b89a7cc2SEnji Cooper 1492b89a7cc2SEnji Cooper // Provides a way for a thread to send notifications to a ThreadLocal 1493b89a7cc2SEnji Cooper // regardless of its parameter type. 1494b89a7cc2SEnji Cooper class ThreadLocalBase { 1495b89a7cc2SEnji Cooper public: 1496b89a7cc2SEnji Cooper // Creates a new ValueHolder<T> object holding a default value passed to 1497b89a7cc2SEnji Cooper // this ThreadLocal<T>'s constructor and returns it. It is the caller's 1498b89a7cc2SEnji Cooper // responsibility not to call this when the ThreadLocal<T> instance already 1499b89a7cc2SEnji Cooper // has a value on the current thread. 1500b89a7cc2SEnji Cooper virtual ThreadLocalValueHolderBase* NewValueForCurrentThread() const = 0; 1501b89a7cc2SEnji Cooper 1502b89a7cc2SEnji Cooper protected: 1503b89a7cc2SEnji Cooper ThreadLocalBase() {} 1504b89a7cc2SEnji Cooper virtual ~ThreadLocalBase() {} 1505b89a7cc2SEnji Cooper 1506b89a7cc2SEnji Cooper private: 150728f6c2f2SEnji Cooper ThreadLocalBase(const ThreadLocalBase&) = delete; 150828f6c2f2SEnji Cooper ThreadLocalBase& operator=(const ThreadLocalBase&) = delete; 1509b89a7cc2SEnji Cooper }; 1510b89a7cc2SEnji Cooper 1511b89a7cc2SEnji Cooper // Maps a thread to a set of ThreadLocals that have values instantiated on that 1512b89a7cc2SEnji Cooper // thread and notifies them when the thread exits. A ThreadLocal instance is 1513b89a7cc2SEnji Cooper // expected to persist until all threads it has values on have terminated. 1514b89a7cc2SEnji Cooper class GTEST_API_ ThreadLocalRegistry { 1515b89a7cc2SEnji Cooper public: 1516b89a7cc2SEnji Cooper // Registers thread_local_instance as having value on the current thread. 1517b89a7cc2SEnji Cooper // Returns a value that can be used to identify the thread from other threads. 1518b89a7cc2SEnji Cooper static ThreadLocalValueHolderBase* GetValueOnCurrentThread( 1519b89a7cc2SEnji Cooper const ThreadLocalBase* thread_local_instance); 1520b89a7cc2SEnji Cooper 1521b89a7cc2SEnji Cooper // Invoked when a ThreadLocal instance is destroyed. 1522b89a7cc2SEnji Cooper static void OnThreadLocalDestroyed( 1523b89a7cc2SEnji Cooper const ThreadLocalBase* thread_local_instance); 1524b89a7cc2SEnji Cooper }; 1525b89a7cc2SEnji Cooper 1526b89a7cc2SEnji Cooper class GTEST_API_ ThreadWithParamBase { 1527b89a7cc2SEnji Cooper public: 1528b89a7cc2SEnji Cooper void Join(); 1529b89a7cc2SEnji Cooper 1530b89a7cc2SEnji Cooper protected: 1531b89a7cc2SEnji Cooper class Runnable { 1532b89a7cc2SEnji Cooper public: 1533b89a7cc2SEnji Cooper virtual ~Runnable() {} 1534b89a7cc2SEnji Cooper virtual void Run() = 0; 1535b89a7cc2SEnji Cooper }; 1536b89a7cc2SEnji Cooper 1537b89a7cc2SEnji Cooper ThreadWithParamBase(Runnable* runnable, Notification* thread_can_start); 1538b89a7cc2SEnji Cooper virtual ~ThreadWithParamBase(); 1539b89a7cc2SEnji Cooper 1540b89a7cc2SEnji Cooper private: 1541b89a7cc2SEnji Cooper AutoHandle thread_; 1542b89a7cc2SEnji Cooper }; 1543b89a7cc2SEnji Cooper 1544b89a7cc2SEnji Cooper // Helper class for testing Google Test's multi-threading constructs. 1545b89a7cc2SEnji Cooper template <typename T> 1546b89a7cc2SEnji Cooper class ThreadWithParam : public ThreadWithParamBase { 1547b89a7cc2SEnji Cooper public: 1548b89a7cc2SEnji Cooper typedef void UserThreadFunc(T); 1549b89a7cc2SEnji Cooper 1550b89a7cc2SEnji Cooper ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start) 155128f6c2f2SEnji Cooper : ThreadWithParamBase(new RunnableImpl(func, param), thread_can_start) {} 1552b89a7cc2SEnji Cooper virtual ~ThreadWithParam() {} 1553b89a7cc2SEnji Cooper 1554b89a7cc2SEnji Cooper private: 1555b89a7cc2SEnji Cooper class RunnableImpl : public Runnable { 1556b89a7cc2SEnji Cooper public: 155728f6c2f2SEnji Cooper RunnableImpl(UserThreadFunc* func, T param) : func_(func), param_(param) {} 1558b89a7cc2SEnji Cooper virtual ~RunnableImpl() {} 155928f6c2f2SEnji Cooper virtual void Run() { func_(param_); } 1560b89a7cc2SEnji Cooper 1561b89a7cc2SEnji Cooper private: 1562b89a7cc2SEnji Cooper UserThreadFunc* const func_; 1563b89a7cc2SEnji Cooper const T param_; 1564b89a7cc2SEnji Cooper 156528f6c2f2SEnji Cooper RunnableImpl(const RunnableImpl&) = delete; 156628f6c2f2SEnji Cooper RunnableImpl& operator=(const RunnableImpl&) = delete; 1567b89a7cc2SEnji Cooper }; 1568b89a7cc2SEnji Cooper 156928f6c2f2SEnji Cooper ThreadWithParam(const ThreadWithParam&) = delete; 157028f6c2f2SEnji Cooper ThreadWithParam& operator=(const ThreadWithParam&) = delete; 1571b89a7cc2SEnji Cooper }; 1572b89a7cc2SEnji Cooper 1573b89a7cc2SEnji Cooper // Implements thread-local storage on Windows systems. 1574b89a7cc2SEnji Cooper // 1575b89a7cc2SEnji Cooper // // Thread 1 1576b89a7cc2SEnji Cooper // ThreadLocal<int> tl(100); // 100 is the default value for each thread. 1577b89a7cc2SEnji Cooper // 1578b89a7cc2SEnji Cooper // // Thread 2 1579b89a7cc2SEnji Cooper // tl.set(150); // Changes the value for thread 2 only. 1580b89a7cc2SEnji Cooper // EXPECT_EQ(150, tl.get()); 1581b89a7cc2SEnji Cooper // 1582b89a7cc2SEnji Cooper // // Thread 1 1583b89a7cc2SEnji Cooper // EXPECT_EQ(100, tl.get()); // In thread 1, tl has the original value. 1584b89a7cc2SEnji Cooper // tl.set(200); 1585b89a7cc2SEnji Cooper // EXPECT_EQ(200, tl.get()); 1586b89a7cc2SEnji Cooper // 1587b89a7cc2SEnji Cooper // The template type argument T must have a public copy constructor. 1588b89a7cc2SEnji Cooper // In addition, the default ThreadLocal constructor requires T to have 1589b89a7cc2SEnji Cooper // a public default constructor. 1590b89a7cc2SEnji Cooper // 1591b89a7cc2SEnji Cooper // The users of a TheadLocal instance have to make sure that all but one 1592b89a7cc2SEnji Cooper // threads (including the main one) using that instance have exited before 1593b89a7cc2SEnji Cooper // destroying it. Otherwise, the per-thread objects managed for them by the 1594b89a7cc2SEnji Cooper // ThreadLocal instance are not guaranteed to be destroyed on all platforms. 1595b89a7cc2SEnji Cooper // 1596b89a7cc2SEnji Cooper // Google Test only uses global ThreadLocal objects. That means they 1597b89a7cc2SEnji Cooper // will die after main() has returned. Therefore, no per-thread 1598b89a7cc2SEnji Cooper // object managed by Google Test will be leaked as long as all threads 1599b89a7cc2SEnji Cooper // using Google Test have exited when main() returns. 1600b89a7cc2SEnji Cooper template <typename T> 1601b89a7cc2SEnji Cooper class ThreadLocal : public ThreadLocalBase { 1602b89a7cc2SEnji Cooper public: 1603b89a7cc2SEnji Cooper ThreadLocal() : default_factory_(new DefaultValueHolderFactory()) {} 1604b89a7cc2SEnji Cooper explicit ThreadLocal(const T& value) 1605b89a7cc2SEnji Cooper : default_factory_(new InstanceValueHolderFactory(value)) {} 1606b89a7cc2SEnji Cooper 160728f6c2f2SEnji Cooper ~ThreadLocal() override { ThreadLocalRegistry::OnThreadLocalDestroyed(this); } 1608b89a7cc2SEnji Cooper 1609b89a7cc2SEnji Cooper T* pointer() { return GetOrCreateValue(); } 1610b89a7cc2SEnji Cooper const T* pointer() const { return GetOrCreateValue(); } 1611b89a7cc2SEnji Cooper const T& get() const { return *pointer(); } 1612b89a7cc2SEnji Cooper void set(const T& value) { *pointer() = value; } 1613b89a7cc2SEnji Cooper 1614b89a7cc2SEnji Cooper private: 1615b89a7cc2SEnji Cooper // Holds a value of T. Can be deleted via its base class without the caller 1616b89a7cc2SEnji Cooper // knowing the type of T. 1617b89a7cc2SEnji Cooper class ValueHolder : public ThreadLocalValueHolderBase { 1618b89a7cc2SEnji Cooper public: 1619b89a7cc2SEnji Cooper ValueHolder() : value_() {} 1620b89a7cc2SEnji Cooper explicit ValueHolder(const T& value) : value_(value) {} 1621b89a7cc2SEnji Cooper 1622b89a7cc2SEnji Cooper T* pointer() { return &value_; } 1623b89a7cc2SEnji Cooper 1624b89a7cc2SEnji Cooper private: 1625b89a7cc2SEnji Cooper T value_; 162628f6c2f2SEnji Cooper ValueHolder(const ValueHolder&) = delete; 162728f6c2f2SEnji Cooper ValueHolder& operator=(const ValueHolder&) = delete; 1628b89a7cc2SEnji Cooper }; 1629b89a7cc2SEnji Cooper 1630b89a7cc2SEnji Cooper T* GetOrCreateValue() const { 1631b89a7cc2SEnji Cooper return static_cast<ValueHolder*>( 163228f6c2f2SEnji Cooper ThreadLocalRegistry::GetValueOnCurrentThread(this)) 163328f6c2f2SEnji Cooper ->pointer(); 1634b89a7cc2SEnji Cooper } 1635b89a7cc2SEnji Cooper 163628f6c2f2SEnji Cooper ThreadLocalValueHolderBase* NewValueForCurrentThread() const override { 1637b89a7cc2SEnji Cooper return default_factory_->MakeNewHolder(); 1638b89a7cc2SEnji Cooper } 1639b89a7cc2SEnji Cooper 1640b89a7cc2SEnji Cooper class ValueHolderFactory { 1641b89a7cc2SEnji Cooper public: 1642b89a7cc2SEnji Cooper ValueHolderFactory() {} 1643b89a7cc2SEnji Cooper virtual ~ValueHolderFactory() {} 1644b89a7cc2SEnji Cooper virtual ValueHolder* MakeNewHolder() const = 0; 1645b89a7cc2SEnji Cooper 1646b89a7cc2SEnji Cooper private: 164728f6c2f2SEnji Cooper ValueHolderFactory(const ValueHolderFactory&) = delete; 164828f6c2f2SEnji Cooper ValueHolderFactory& operator=(const ValueHolderFactory&) = delete; 1649b89a7cc2SEnji Cooper }; 1650b89a7cc2SEnji Cooper 1651b89a7cc2SEnji Cooper class DefaultValueHolderFactory : public ValueHolderFactory { 1652b89a7cc2SEnji Cooper public: 1653b89a7cc2SEnji Cooper DefaultValueHolderFactory() {} 165428f6c2f2SEnji Cooper ValueHolder* MakeNewHolder() const override { return new ValueHolder(); } 1655b89a7cc2SEnji Cooper 1656b89a7cc2SEnji Cooper private: 165728f6c2f2SEnji Cooper DefaultValueHolderFactory(const DefaultValueHolderFactory&) = delete; 165828f6c2f2SEnji Cooper DefaultValueHolderFactory& operator=(const DefaultValueHolderFactory&) = 165928f6c2f2SEnji Cooper delete; 1660b89a7cc2SEnji Cooper }; 1661b89a7cc2SEnji Cooper 1662b89a7cc2SEnji Cooper class InstanceValueHolderFactory : public ValueHolderFactory { 1663b89a7cc2SEnji Cooper public: 1664b89a7cc2SEnji Cooper explicit InstanceValueHolderFactory(const T& value) : value_(value) {} 166528f6c2f2SEnji Cooper ValueHolder* MakeNewHolder() const override { 1666b89a7cc2SEnji Cooper return new ValueHolder(value_); 1667b89a7cc2SEnji Cooper } 1668b89a7cc2SEnji Cooper 1669b89a7cc2SEnji Cooper private: 1670b89a7cc2SEnji Cooper const T value_; // The value for each thread. 1671b89a7cc2SEnji Cooper 167228f6c2f2SEnji Cooper InstanceValueHolderFactory(const InstanceValueHolderFactory&) = delete; 167328f6c2f2SEnji Cooper InstanceValueHolderFactory& operator=(const InstanceValueHolderFactory&) = 167428f6c2f2SEnji Cooper delete; 1675b89a7cc2SEnji Cooper }; 1676b89a7cc2SEnji Cooper 167728f6c2f2SEnji Cooper std::unique_ptr<ValueHolderFactory> default_factory_; 1678b89a7cc2SEnji Cooper 167928f6c2f2SEnji Cooper ThreadLocal(const ThreadLocal&) = delete; 168028f6c2f2SEnji Cooper ThreadLocal& operator=(const ThreadLocal&) = delete; 1681b89a7cc2SEnji Cooper }; 1682b89a7cc2SEnji Cooper 1683b89a7cc2SEnji Cooper #elif GTEST_HAS_PTHREAD 1684b89a7cc2SEnji Cooper 1685b89a7cc2SEnji Cooper // MutexBase and Mutex implement mutex on pthreads-based platforms. 1686b89a7cc2SEnji Cooper class MutexBase { 1687b89a7cc2SEnji Cooper public: 1688b89a7cc2SEnji Cooper // Acquires this mutex. 1689b89a7cc2SEnji Cooper void Lock() { 1690b89a7cc2SEnji Cooper GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_)); 1691b89a7cc2SEnji Cooper owner_ = pthread_self(); 1692b89a7cc2SEnji Cooper has_owner_ = true; 1693b89a7cc2SEnji Cooper } 1694b89a7cc2SEnji Cooper 1695b89a7cc2SEnji Cooper // Releases this mutex. 1696b89a7cc2SEnji Cooper void Unlock() { 1697b89a7cc2SEnji Cooper // Since the lock is being released the owner_ field should no longer be 1698b89a7cc2SEnji Cooper // considered valid. We don't protect writing to has_owner_ here, as it's 1699b89a7cc2SEnji Cooper // the caller's responsibility to ensure that the current thread holds the 1700b89a7cc2SEnji Cooper // mutex when this is called. 1701b89a7cc2SEnji Cooper has_owner_ = false; 1702b89a7cc2SEnji Cooper GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_)); 1703b89a7cc2SEnji Cooper } 1704b89a7cc2SEnji Cooper 1705b89a7cc2SEnji Cooper // Does nothing if the current thread holds the mutex. Otherwise, crashes 1706b89a7cc2SEnji Cooper // with high probability. 1707b89a7cc2SEnji Cooper void AssertHeld() const { 1708b89a7cc2SEnji Cooper GTEST_CHECK_(has_owner_ && pthread_equal(owner_, pthread_self())) 1709b89a7cc2SEnji Cooper << "The current thread is not holding the mutex @" << this; 1710b89a7cc2SEnji Cooper } 1711b89a7cc2SEnji Cooper 1712b89a7cc2SEnji Cooper // A static mutex may be used before main() is entered. It may even 1713b89a7cc2SEnji Cooper // be used before the dynamic initialization stage. Therefore we 1714b89a7cc2SEnji Cooper // must be able to initialize a static mutex object at link time. 1715b89a7cc2SEnji Cooper // This means MutexBase has to be a POD and its member variables 1716b89a7cc2SEnji Cooper // have to be public. 1717b89a7cc2SEnji Cooper public: 1718b89a7cc2SEnji Cooper pthread_mutex_t mutex_; // The underlying pthread mutex. 1719b89a7cc2SEnji Cooper // has_owner_ indicates whether the owner_ field below contains a valid thread 1720b89a7cc2SEnji Cooper // ID and is therefore safe to inspect (e.g., to use in pthread_equal()). All 1721b89a7cc2SEnji Cooper // accesses to the owner_ field should be protected by a check of this field. 1722b89a7cc2SEnji Cooper // An alternative might be to memset() owner_ to all zeros, but there's no 1723b89a7cc2SEnji Cooper // guarantee that a zero'd pthread_t is necessarily invalid or even different 1724b89a7cc2SEnji Cooper // from pthread_self(). 1725b89a7cc2SEnji Cooper bool has_owner_; 1726b89a7cc2SEnji Cooper pthread_t owner_; // The thread holding the mutex. 1727b89a7cc2SEnji Cooper }; 1728b89a7cc2SEnji Cooper 1729b89a7cc2SEnji Cooper // Forward-declares a static mutex. 1730b89a7cc2SEnji Cooper #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ 1731b89a7cc2SEnji Cooper extern ::testing::internal::MutexBase mutex 1732b89a7cc2SEnji Cooper 1733b89a7cc2SEnji Cooper // Defines and statically (i.e. at link time) initializes a static mutex. 1734b89a7cc2SEnji Cooper // The initialization list here does not explicitly initialize each field, 1735b89a7cc2SEnji Cooper // instead relying on default initialization for the unspecified fields. In 1736b89a7cc2SEnji Cooper // particular, the owner_ field (a pthread_t) is not explicitly initialized. 1737b89a7cc2SEnji Cooper // This allows initialization to work whether pthread_t is a scalar or struct. 1738b89a7cc2SEnji Cooper // The flag -Wmissing-field-initializers must not be specified for this to work. 1739b89a7cc2SEnji Cooper #define GTEST_DEFINE_STATIC_MUTEX_(mutex) \ 1740b89a7cc2SEnji Cooper ::testing::internal::MutexBase mutex = {PTHREAD_MUTEX_INITIALIZER, false, 0} 1741b89a7cc2SEnji Cooper 1742b89a7cc2SEnji Cooper // The Mutex class can only be used for mutexes created at runtime. It 1743b89a7cc2SEnji Cooper // shares its API with MutexBase otherwise. 1744b89a7cc2SEnji Cooper class Mutex : public MutexBase { 1745b89a7cc2SEnji Cooper public: 1746b89a7cc2SEnji Cooper Mutex() { 174728f6c2f2SEnji Cooper GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, nullptr)); 1748b89a7cc2SEnji Cooper has_owner_ = false; 1749b89a7cc2SEnji Cooper } 175028f6c2f2SEnji Cooper ~Mutex() { GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_)); } 1751b89a7cc2SEnji Cooper 1752b89a7cc2SEnji Cooper private: 175328f6c2f2SEnji Cooper Mutex(const Mutex&) = delete; 175428f6c2f2SEnji Cooper Mutex& operator=(const Mutex&) = delete; 1755b89a7cc2SEnji Cooper }; 1756b89a7cc2SEnji Cooper 1757b89a7cc2SEnji Cooper // We cannot name this class MutexLock because the ctor declaration would 1758b89a7cc2SEnji Cooper // conflict with a macro named MutexLock, which is defined on some 1759b89a7cc2SEnji Cooper // platforms. That macro is used as a defensive measure to prevent against 1760b89a7cc2SEnji Cooper // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than 1761b89a7cc2SEnji Cooper // "MutexLock l(&mu)". Hence the typedef trick below. 1762b89a7cc2SEnji Cooper class GTestMutexLock { 1763b89a7cc2SEnji Cooper public: 176428f6c2f2SEnji Cooper explicit GTestMutexLock(MutexBase* mutex) : mutex_(mutex) { mutex_->Lock(); } 1765b89a7cc2SEnji Cooper 1766b89a7cc2SEnji Cooper ~GTestMutexLock() { mutex_->Unlock(); } 1767b89a7cc2SEnji Cooper 1768b89a7cc2SEnji Cooper private: 1769b89a7cc2SEnji Cooper MutexBase* const mutex_; 1770b89a7cc2SEnji Cooper 177128f6c2f2SEnji Cooper GTestMutexLock(const GTestMutexLock&) = delete; 177228f6c2f2SEnji Cooper GTestMutexLock& operator=(const GTestMutexLock&) = delete; 1773b89a7cc2SEnji Cooper }; 1774b89a7cc2SEnji Cooper 1775b89a7cc2SEnji Cooper typedef GTestMutexLock MutexLock; 1776b89a7cc2SEnji Cooper 1777b89a7cc2SEnji Cooper // Helpers for ThreadLocal. 1778b89a7cc2SEnji Cooper 1779b89a7cc2SEnji Cooper // pthread_key_create() requires DeleteThreadLocalValue() to have 1780b89a7cc2SEnji Cooper // C-linkage. Therefore it cannot be templatized to access 1781b89a7cc2SEnji Cooper // ThreadLocal<T>. Hence the need for class 1782b89a7cc2SEnji Cooper // ThreadLocalValueHolderBase. 178328f6c2f2SEnji Cooper class GTEST_API_ ThreadLocalValueHolderBase { 1784b89a7cc2SEnji Cooper public: 178528f6c2f2SEnji Cooper virtual ~ThreadLocalValueHolderBase() = default; 1786b89a7cc2SEnji Cooper }; 1787b89a7cc2SEnji Cooper 1788b89a7cc2SEnji Cooper // Called by pthread to delete thread-local data stored by 1789b89a7cc2SEnji Cooper // pthread_setspecific(). 1790b89a7cc2SEnji Cooper extern "C" inline void DeleteThreadLocalValue(void* value_holder) { 1791b89a7cc2SEnji Cooper delete static_cast<ThreadLocalValueHolderBase*>(value_holder); 1792b89a7cc2SEnji Cooper } 1793b89a7cc2SEnji Cooper 1794b89a7cc2SEnji Cooper // Implements thread-local storage on pthreads-based systems. 1795b89a7cc2SEnji Cooper template <typename T> 1796b89a7cc2SEnji Cooper class GTEST_API_ ThreadLocal { 1797b89a7cc2SEnji Cooper public: 1798b89a7cc2SEnji Cooper ThreadLocal() 1799b89a7cc2SEnji Cooper : key_(CreateKey()), default_factory_(new DefaultValueHolderFactory()) {} 1800b89a7cc2SEnji Cooper explicit ThreadLocal(const T& value) 1801b89a7cc2SEnji Cooper : key_(CreateKey()), 1802b89a7cc2SEnji Cooper default_factory_(new InstanceValueHolderFactory(value)) {} 1803b89a7cc2SEnji Cooper 1804b89a7cc2SEnji Cooper ~ThreadLocal() { 1805b89a7cc2SEnji Cooper // Destroys the managed object for the current thread, if any. 1806b89a7cc2SEnji Cooper DeleteThreadLocalValue(pthread_getspecific(key_)); 1807b89a7cc2SEnji Cooper 1808b89a7cc2SEnji Cooper // Releases resources associated with the key. This will *not* 1809b89a7cc2SEnji Cooper // delete managed objects for other threads. 1810b89a7cc2SEnji Cooper GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_)); 1811b89a7cc2SEnji Cooper } 1812b89a7cc2SEnji Cooper 1813b89a7cc2SEnji Cooper T* pointer() { return GetOrCreateValue(); } 1814b89a7cc2SEnji Cooper const T* pointer() const { return GetOrCreateValue(); } 1815b89a7cc2SEnji Cooper const T& get() const { return *pointer(); } 1816b89a7cc2SEnji Cooper void set(const T& value) { *pointer() = value; } 1817b89a7cc2SEnji Cooper 1818b89a7cc2SEnji Cooper private: 1819b89a7cc2SEnji Cooper // Holds a value of type T. 1820b89a7cc2SEnji Cooper class ValueHolder : public ThreadLocalValueHolderBase { 1821b89a7cc2SEnji Cooper public: 1822b89a7cc2SEnji Cooper ValueHolder() : value_() {} 1823b89a7cc2SEnji Cooper explicit ValueHolder(const T& value) : value_(value) {} 1824b89a7cc2SEnji Cooper 1825b89a7cc2SEnji Cooper T* pointer() { return &value_; } 1826b89a7cc2SEnji Cooper 1827b89a7cc2SEnji Cooper private: 1828b89a7cc2SEnji Cooper T value_; 182928f6c2f2SEnji Cooper ValueHolder(const ValueHolder&) = delete; 183028f6c2f2SEnji Cooper ValueHolder& operator=(const ValueHolder&) = delete; 1831b89a7cc2SEnji Cooper }; 1832b89a7cc2SEnji Cooper 1833b89a7cc2SEnji Cooper static pthread_key_t CreateKey() { 1834b89a7cc2SEnji Cooper pthread_key_t key; 1835b89a7cc2SEnji Cooper // When a thread exits, DeleteThreadLocalValue() will be called on 1836b89a7cc2SEnji Cooper // the object managed for that thread. 1837b89a7cc2SEnji Cooper GTEST_CHECK_POSIX_SUCCESS_( 1838b89a7cc2SEnji Cooper pthread_key_create(&key, &DeleteThreadLocalValue)); 1839b89a7cc2SEnji Cooper return key; 1840b89a7cc2SEnji Cooper } 1841b89a7cc2SEnji Cooper 1842b89a7cc2SEnji Cooper T* GetOrCreateValue() const { 1843b89a7cc2SEnji Cooper ThreadLocalValueHolderBase* const holder = 1844b89a7cc2SEnji Cooper static_cast<ThreadLocalValueHolderBase*>(pthread_getspecific(key_)); 184528f6c2f2SEnji Cooper if (holder != nullptr) { 1846b89a7cc2SEnji Cooper return CheckedDowncastToActualType<ValueHolder>(holder)->pointer(); 1847b89a7cc2SEnji Cooper } 1848b89a7cc2SEnji Cooper 1849b89a7cc2SEnji Cooper ValueHolder* const new_holder = default_factory_->MakeNewHolder(); 1850b89a7cc2SEnji Cooper ThreadLocalValueHolderBase* const holder_base = new_holder; 1851b89a7cc2SEnji Cooper GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base)); 1852b89a7cc2SEnji Cooper return new_holder->pointer(); 1853b89a7cc2SEnji Cooper } 1854b89a7cc2SEnji Cooper 1855b89a7cc2SEnji Cooper class ValueHolderFactory { 1856b89a7cc2SEnji Cooper public: 185728f6c2f2SEnji Cooper ValueHolderFactory() = default; 185828f6c2f2SEnji Cooper virtual ~ValueHolderFactory() = default; 1859b89a7cc2SEnji Cooper virtual ValueHolder* MakeNewHolder() const = 0; 1860b89a7cc2SEnji Cooper 1861b89a7cc2SEnji Cooper private: 186228f6c2f2SEnji Cooper ValueHolderFactory(const ValueHolderFactory&) = delete; 186328f6c2f2SEnji Cooper ValueHolderFactory& operator=(const ValueHolderFactory&) = delete; 1864b89a7cc2SEnji Cooper }; 1865b89a7cc2SEnji Cooper 1866b89a7cc2SEnji Cooper class DefaultValueHolderFactory : public ValueHolderFactory { 1867b89a7cc2SEnji Cooper public: 186828f6c2f2SEnji Cooper DefaultValueHolderFactory() = default; 186928f6c2f2SEnji Cooper ValueHolder* MakeNewHolder() const override { return new ValueHolder(); } 1870b89a7cc2SEnji Cooper 1871b89a7cc2SEnji Cooper private: 187228f6c2f2SEnji Cooper DefaultValueHolderFactory(const DefaultValueHolderFactory&) = delete; 187328f6c2f2SEnji Cooper DefaultValueHolderFactory& operator=(const DefaultValueHolderFactory&) = 187428f6c2f2SEnji Cooper delete; 1875b89a7cc2SEnji Cooper }; 1876b89a7cc2SEnji Cooper 1877b89a7cc2SEnji Cooper class InstanceValueHolderFactory : public ValueHolderFactory { 1878b89a7cc2SEnji Cooper public: 1879b89a7cc2SEnji Cooper explicit InstanceValueHolderFactory(const T& value) : value_(value) {} 188028f6c2f2SEnji Cooper ValueHolder* MakeNewHolder() const override { 1881b89a7cc2SEnji Cooper return new ValueHolder(value_); 1882b89a7cc2SEnji Cooper } 1883b89a7cc2SEnji Cooper 1884b89a7cc2SEnji Cooper private: 1885b89a7cc2SEnji Cooper const T value_; // The value for each thread. 1886b89a7cc2SEnji Cooper 188728f6c2f2SEnji Cooper InstanceValueHolderFactory(const InstanceValueHolderFactory&) = delete; 188828f6c2f2SEnji Cooper InstanceValueHolderFactory& operator=(const InstanceValueHolderFactory&) = 188928f6c2f2SEnji Cooper delete; 1890b89a7cc2SEnji Cooper }; 1891b89a7cc2SEnji Cooper 1892b89a7cc2SEnji Cooper // A key pthreads uses for looking up per-thread values. 1893b89a7cc2SEnji Cooper const pthread_key_t key_; 189428f6c2f2SEnji Cooper std::unique_ptr<ValueHolderFactory> default_factory_; 1895b89a7cc2SEnji Cooper 189628f6c2f2SEnji Cooper ThreadLocal(const ThreadLocal&) = delete; 189728f6c2f2SEnji Cooper ThreadLocal& operator=(const ThreadLocal&) = delete; 1898b89a7cc2SEnji Cooper }; 1899b89a7cc2SEnji Cooper 1900b89a7cc2SEnji Cooper #endif // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ 1901b89a7cc2SEnji Cooper 1902b89a7cc2SEnji Cooper #else // GTEST_IS_THREADSAFE 1903b89a7cc2SEnji Cooper 1904b89a7cc2SEnji Cooper // A dummy implementation of synchronization primitives (mutex, lock, 1905b89a7cc2SEnji Cooper // and thread-local variable). Necessary for compiling Google Test where 1906b89a7cc2SEnji Cooper // mutex is not supported - using Google Test in multiple threads is not 1907b89a7cc2SEnji Cooper // supported on such platforms. 1908b89a7cc2SEnji Cooper 1909b89a7cc2SEnji Cooper class Mutex { 1910b89a7cc2SEnji Cooper public: 1911b89a7cc2SEnji Cooper Mutex() {} 1912b89a7cc2SEnji Cooper void Lock() {} 1913b89a7cc2SEnji Cooper void Unlock() {} 1914b89a7cc2SEnji Cooper void AssertHeld() const {} 1915b89a7cc2SEnji Cooper }; 1916b89a7cc2SEnji Cooper 1917b89a7cc2SEnji Cooper #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ 1918b89a7cc2SEnji Cooper extern ::testing::internal::Mutex mutex 1919b89a7cc2SEnji Cooper 1920b89a7cc2SEnji Cooper #define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex 1921b89a7cc2SEnji Cooper 1922b89a7cc2SEnji Cooper // We cannot name this class MutexLock because the ctor declaration would 1923b89a7cc2SEnji Cooper // conflict with a macro named MutexLock, which is defined on some 1924b89a7cc2SEnji Cooper // platforms. That macro is used as a defensive measure to prevent against 1925b89a7cc2SEnji Cooper // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than 1926b89a7cc2SEnji Cooper // "MutexLock l(&mu)". Hence the typedef trick below. 1927b89a7cc2SEnji Cooper class GTestMutexLock { 1928b89a7cc2SEnji Cooper public: 1929b89a7cc2SEnji Cooper explicit GTestMutexLock(Mutex*) {} // NOLINT 1930b89a7cc2SEnji Cooper }; 1931b89a7cc2SEnji Cooper 1932b89a7cc2SEnji Cooper typedef GTestMutexLock MutexLock; 1933b89a7cc2SEnji Cooper 1934b89a7cc2SEnji Cooper template <typename T> 1935b89a7cc2SEnji Cooper class GTEST_API_ ThreadLocal { 1936b89a7cc2SEnji Cooper public: 1937b89a7cc2SEnji Cooper ThreadLocal() : value_() {} 1938b89a7cc2SEnji Cooper explicit ThreadLocal(const T& value) : value_(value) {} 1939b89a7cc2SEnji Cooper T* pointer() { return &value_; } 1940b89a7cc2SEnji Cooper const T* pointer() const { return &value_; } 1941b89a7cc2SEnji Cooper const T& get() const { return value_; } 1942b89a7cc2SEnji Cooper void set(const T& value) { value_ = value; } 194328f6c2f2SEnji Cooper 1944b89a7cc2SEnji Cooper private: 1945b89a7cc2SEnji Cooper T value_; 1946b89a7cc2SEnji Cooper }; 1947b89a7cc2SEnji Cooper 1948b89a7cc2SEnji Cooper #endif // GTEST_IS_THREADSAFE 1949b89a7cc2SEnji Cooper 1950b89a7cc2SEnji Cooper // Returns the number of threads running in the process, or 0 to indicate that 1951b89a7cc2SEnji Cooper // we cannot detect it. 1952b89a7cc2SEnji Cooper GTEST_API_ size_t GetThreadCount(); 1953b89a7cc2SEnji Cooper 195428f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS 1955b89a7cc2SEnji Cooper #define GTEST_PATH_SEP_ "\\" 1956b89a7cc2SEnji Cooper #define GTEST_HAS_ALT_PATH_SEP_ 1 1957b89a7cc2SEnji Cooper #else 1958b89a7cc2SEnji Cooper #define GTEST_PATH_SEP_ "/" 1959b89a7cc2SEnji Cooper #define GTEST_HAS_ALT_PATH_SEP_ 0 1960b89a7cc2SEnji Cooper #endif // GTEST_OS_WINDOWS 1961b89a7cc2SEnji Cooper 1962b89a7cc2SEnji Cooper // Utilities for char. 1963b89a7cc2SEnji Cooper 1964b89a7cc2SEnji Cooper // isspace(int ch) and friends accept an unsigned char or EOF. char 1965b89a7cc2SEnji Cooper // may be signed, depending on the compiler (or compiler flags). 1966b89a7cc2SEnji Cooper // Therefore we need to cast a char to unsigned char before calling 1967b89a7cc2SEnji Cooper // isspace(), etc. 1968b89a7cc2SEnji Cooper 1969b89a7cc2SEnji Cooper inline bool IsAlpha(char ch) { 1970b89a7cc2SEnji Cooper return isalpha(static_cast<unsigned char>(ch)) != 0; 1971b89a7cc2SEnji Cooper } 1972b89a7cc2SEnji Cooper inline bool IsAlNum(char ch) { 1973b89a7cc2SEnji Cooper return isalnum(static_cast<unsigned char>(ch)) != 0; 1974b89a7cc2SEnji Cooper } 1975b89a7cc2SEnji Cooper inline bool IsDigit(char ch) { 1976b89a7cc2SEnji Cooper return isdigit(static_cast<unsigned char>(ch)) != 0; 1977b89a7cc2SEnji Cooper } 1978b89a7cc2SEnji Cooper inline bool IsLower(char ch) { 1979b89a7cc2SEnji Cooper return islower(static_cast<unsigned char>(ch)) != 0; 1980b89a7cc2SEnji Cooper } 1981b89a7cc2SEnji Cooper inline bool IsSpace(char ch) { 1982b89a7cc2SEnji Cooper return isspace(static_cast<unsigned char>(ch)) != 0; 1983b89a7cc2SEnji Cooper } 1984b89a7cc2SEnji Cooper inline bool IsUpper(char ch) { 1985b89a7cc2SEnji Cooper return isupper(static_cast<unsigned char>(ch)) != 0; 1986b89a7cc2SEnji Cooper } 1987b89a7cc2SEnji Cooper inline bool IsXDigit(char ch) { 1988b89a7cc2SEnji Cooper return isxdigit(static_cast<unsigned char>(ch)) != 0; 1989b89a7cc2SEnji Cooper } 199028f6c2f2SEnji Cooper #ifdef __cpp_lib_char8_t 199128f6c2f2SEnji Cooper inline bool IsXDigit(char8_t ch) { 199228f6c2f2SEnji Cooper return isxdigit(static_cast<unsigned char>(ch)) != 0; 199328f6c2f2SEnji Cooper } 199428f6c2f2SEnji Cooper #endif 199528f6c2f2SEnji Cooper inline bool IsXDigit(char16_t ch) { 199628f6c2f2SEnji Cooper const unsigned char low_byte = static_cast<unsigned char>(ch); 199728f6c2f2SEnji Cooper return ch == low_byte && isxdigit(low_byte) != 0; 199828f6c2f2SEnji Cooper } 199928f6c2f2SEnji Cooper inline bool IsXDigit(char32_t ch) { 200028f6c2f2SEnji Cooper const unsigned char low_byte = static_cast<unsigned char>(ch); 200128f6c2f2SEnji Cooper return ch == low_byte && isxdigit(low_byte) != 0; 200228f6c2f2SEnji Cooper } 2003b89a7cc2SEnji Cooper inline bool IsXDigit(wchar_t ch) { 2004b89a7cc2SEnji Cooper const unsigned char low_byte = static_cast<unsigned char>(ch); 2005b89a7cc2SEnji Cooper return ch == low_byte && isxdigit(low_byte) != 0; 2006b89a7cc2SEnji Cooper } 2007b89a7cc2SEnji Cooper 2008b89a7cc2SEnji Cooper inline char ToLower(char ch) { 2009b89a7cc2SEnji Cooper return static_cast<char>(tolower(static_cast<unsigned char>(ch))); 2010b89a7cc2SEnji Cooper } 2011b89a7cc2SEnji Cooper inline char ToUpper(char ch) { 2012b89a7cc2SEnji Cooper return static_cast<char>(toupper(static_cast<unsigned char>(ch))); 2013b89a7cc2SEnji Cooper } 2014b89a7cc2SEnji Cooper 2015b89a7cc2SEnji Cooper inline std::string StripTrailingSpaces(std::string str) { 2016b89a7cc2SEnji Cooper std::string::iterator it = str.end(); 201728f6c2f2SEnji Cooper while (it != str.begin() && IsSpace(*--it)) it = str.erase(it); 2018b89a7cc2SEnji Cooper return str; 2019b89a7cc2SEnji Cooper } 2020b89a7cc2SEnji Cooper 2021b89a7cc2SEnji Cooper // The testing::internal::posix namespace holds wrappers for common 2022b89a7cc2SEnji Cooper // POSIX functions. These wrappers hide the differences between 2023b89a7cc2SEnji Cooper // Windows/MSVC and POSIX systems. Since some compilers define these 2024b89a7cc2SEnji Cooper // standard functions as macros, the wrapper cannot have the same name 2025b89a7cc2SEnji Cooper // as the wrapped function. 2026b89a7cc2SEnji Cooper 2027b89a7cc2SEnji Cooper namespace posix { 2028b89a7cc2SEnji Cooper 202928f6c2f2SEnji Cooper // File system porting. 2030*5ca8c28cSEnji Cooper // Note: Not every I/O-related function is related to file systems, so don't 2031*5ca8c28cSEnji Cooper // just disable all of them here. For example, fileno() and isatty(), etc. must 2032*5ca8c28cSEnji Cooper // always be available in order to detect if a pipe points to a terminal. 203328f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS 2034b89a7cc2SEnji Cooper 2035b89a7cc2SEnji Cooper typedef struct _stat StatStruct; 2036b89a7cc2SEnji Cooper 203728f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS_MOBILE 2038b89a7cc2SEnji Cooper inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); } 2039b89a7cc2SEnji Cooper // Stat(), RmDir(), and IsDir() are not needed on Windows CE at this 2040b89a7cc2SEnji Cooper // time and thus not defined there. 2041b89a7cc2SEnji Cooper #else 2042b89a7cc2SEnji Cooper inline int FileNo(FILE* file) { return _fileno(file); } 2043*5ca8c28cSEnji Cooper #if GTEST_HAS_FILE_SYSTEM 2044b89a7cc2SEnji Cooper inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); } 2045b89a7cc2SEnji Cooper inline int RmDir(const char* dir) { return _rmdir(dir); } 204628f6c2f2SEnji Cooper inline bool IsDir(const StatStruct& st) { return (_S_IFDIR & st.st_mode) != 0; } 2047*5ca8c28cSEnji Cooper #endif 2048b89a7cc2SEnji Cooper #endif // GTEST_OS_WINDOWS_MOBILE 2049b89a7cc2SEnji Cooper 205028f6c2f2SEnji Cooper #elif defined(GTEST_OS_ESP8266) 205128f6c2f2SEnji Cooper typedef struct stat StatStruct; 205228f6c2f2SEnji Cooper 205328f6c2f2SEnji Cooper inline int FileNo(FILE* file) { return fileno(file); } 2054*5ca8c28cSEnji Cooper #if GTEST_HAS_FILE_SYSTEM 205528f6c2f2SEnji Cooper inline int Stat(const char* path, StatStruct* buf) { 205628f6c2f2SEnji Cooper // stat function not implemented on ESP8266 205728f6c2f2SEnji Cooper return 0; 205828f6c2f2SEnji Cooper } 205928f6c2f2SEnji Cooper inline int RmDir(const char* dir) { return rmdir(dir); } 206028f6c2f2SEnji Cooper inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } 2061*5ca8c28cSEnji Cooper #endif 206228f6c2f2SEnji Cooper 2063b89a7cc2SEnji Cooper #else 2064b89a7cc2SEnji Cooper 2065b89a7cc2SEnji Cooper typedef struct stat StatStruct; 2066b89a7cc2SEnji Cooper 2067b89a7cc2SEnji Cooper inline int FileNo(FILE* file) { return fileno(file); } 2068*5ca8c28cSEnji Cooper #if GTEST_HAS_FILE_SYSTEM 2069b89a7cc2SEnji Cooper inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); } 207028f6c2f2SEnji Cooper #ifdef GTEST_OS_QURT 207128f6c2f2SEnji Cooper // QuRT doesn't support any directory functions, including rmdir 207228f6c2f2SEnji Cooper inline int RmDir(const char*) { return 0; } 207328f6c2f2SEnji Cooper #else 2074b89a7cc2SEnji Cooper inline int RmDir(const char* dir) { return rmdir(dir); } 207528f6c2f2SEnji Cooper #endif 2076b89a7cc2SEnji Cooper inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } 2077*5ca8c28cSEnji Cooper #endif 2078b89a7cc2SEnji Cooper 2079b89a7cc2SEnji Cooper #endif // GTEST_OS_WINDOWS 208028f6c2f2SEnji Cooper 208128f6c2f2SEnji Cooper // Other functions with a different name on Windows. 208228f6c2f2SEnji Cooper 208328f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS 208428f6c2f2SEnji Cooper 208528f6c2f2SEnji Cooper #ifdef __BORLANDC__ 208628f6c2f2SEnji Cooper inline int DoIsATTY(int fd) { return isatty(fd); } 208728f6c2f2SEnji Cooper inline int StrCaseCmp(const char* s1, const char* s2) { 208828f6c2f2SEnji Cooper return stricmp(s1, s2); 208928f6c2f2SEnji Cooper } 209028f6c2f2SEnji Cooper #else // !__BORLANDC__ 209128f6c2f2SEnji Cooper #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_ZOS) || \ 209228f6c2f2SEnji Cooper defined(GTEST_OS_IOS) || defined(GTEST_OS_WINDOWS_PHONE) || \ 209328f6c2f2SEnji Cooper defined(GTEST_OS_WINDOWS_RT) || defined(ESP_PLATFORM) 209428f6c2f2SEnji Cooper inline int DoIsATTY(int /* fd */) { return 0; } 209528f6c2f2SEnji Cooper #else 209628f6c2f2SEnji Cooper inline int DoIsATTY(int fd) { return _isatty(fd); } 209728f6c2f2SEnji Cooper #endif // GTEST_OS_WINDOWS_MOBILE 209828f6c2f2SEnji Cooper inline int StrCaseCmp(const char* s1, const char* s2) { 209928f6c2f2SEnji Cooper return _stricmp(s1, s2); 210028f6c2f2SEnji Cooper } 210128f6c2f2SEnji Cooper #endif // __BORLANDC__ 210228f6c2f2SEnji Cooper 210328f6c2f2SEnji Cooper #else 210428f6c2f2SEnji Cooper 210528f6c2f2SEnji Cooper inline int DoIsATTY(int fd) { return isatty(fd); } 210628f6c2f2SEnji Cooper inline int StrCaseCmp(const char* s1, const char* s2) { 210728f6c2f2SEnji Cooper return strcasecmp(s1, s2); 210828f6c2f2SEnji Cooper } 210928f6c2f2SEnji Cooper 211028f6c2f2SEnji Cooper #endif // GTEST_OS_WINDOWS 211128f6c2f2SEnji Cooper 211228f6c2f2SEnji Cooper inline int IsATTY(int fd) { 211328f6c2f2SEnji Cooper // DoIsATTY might change errno (for example ENOTTY in case you redirect stdout 211428f6c2f2SEnji Cooper // to a file on Linux), which is unexpected, so save the previous value, and 211528f6c2f2SEnji Cooper // restore it after the call. 211628f6c2f2SEnji Cooper int savedErrno = errno; 211728f6c2f2SEnji Cooper int isAttyValue = DoIsATTY(fd); 211828f6c2f2SEnji Cooper errno = savedErrno; 211928f6c2f2SEnji Cooper 212028f6c2f2SEnji Cooper return isAttyValue; 212128f6c2f2SEnji Cooper } 2122b89a7cc2SEnji Cooper 2123b89a7cc2SEnji Cooper // Functions deprecated by MSVC 8.0. 2124b89a7cc2SEnji Cooper 2125b89a7cc2SEnji Cooper GTEST_DISABLE_MSC_DEPRECATED_PUSH_() 2126b89a7cc2SEnji Cooper 2127b89a7cc2SEnji Cooper // ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and 2128b89a7cc2SEnji Cooper // StrError() aren't needed on Windows CE at this time and thus not 2129b89a7cc2SEnji Cooper // defined there. 213028f6c2f2SEnji Cooper #if GTEST_HAS_FILE_SYSTEM 213128f6c2f2SEnji Cooper #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_WINDOWS_PHONE) && \ 2132*5ca8c28cSEnji Cooper !defined(GTEST_OS_WINDOWS_RT) && !defined(GTEST_OS_WINDOWS_GAMES) && \ 2133*5ca8c28cSEnji Cooper !defined(GTEST_OS_ESP8266) && !defined(GTEST_OS_XTENSA) && \ 2134*5ca8c28cSEnji Cooper !defined(GTEST_OS_QURT) 2135b89a7cc2SEnji Cooper inline int ChDir(const char* dir) { return chdir(dir); } 2136b89a7cc2SEnji Cooper #endif 2137b89a7cc2SEnji Cooper inline FILE* FOpen(const char* path, const char* mode) { 213828f6c2f2SEnji Cooper #if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MINGW) 213928f6c2f2SEnji Cooper struct wchar_codecvt : public std::codecvt<wchar_t, char, std::mbstate_t> {}; 214028f6c2f2SEnji Cooper std::wstring_convert<wchar_codecvt> converter; 214128f6c2f2SEnji Cooper std::wstring wide_path = converter.from_bytes(path); 214228f6c2f2SEnji Cooper std::wstring wide_mode = converter.from_bytes(mode); 214328f6c2f2SEnji Cooper return _wfopen(wide_path.c_str(), wide_mode.c_str()); 214428f6c2f2SEnji Cooper #else // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW 2145b89a7cc2SEnji Cooper return fopen(path, mode); 214628f6c2f2SEnji Cooper #endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW 2147b89a7cc2SEnji Cooper } 214828f6c2f2SEnji Cooper #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT) 2149b89a7cc2SEnji Cooper inline FILE* FReopen(const char* path, const char* mode, FILE* stream) { 2150b89a7cc2SEnji Cooper return freopen(path, mode, stream); 2151b89a7cc2SEnji Cooper } 2152b89a7cc2SEnji Cooper inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); } 215328f6c2f2SEnji Cooper #endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT 2154b89a7cc2SEnji Cooper inline int FClose(FILE* fp) { return fclose(fp); } 215528f6c2f2SEnji Cooper #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT) 2156b89a7cc2SEnji Cooper inline int Read(int fd, void* buf, unsigned int count) { 2157b89a7cc2SEnji Cooper return static_cast<int>(read(fd, buf, count)); 2158b89a7cc2SEnji Cooper } 2159b89a7cc2SEnji Cooper inline int Write(int fd, const void* buf, unsigned int count) { 2160b89a7cc2SEnji Cooper return static_cast<int>(write(fd, buf, count)); 2161b89a7cc2SEnji Cooper } 2162b89a7cc2SEnji Cooper inline int Close(int fd) { return close(fd); } 216328f6c2f2SEnji Cooper #endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT 216428f6c2f2SEnji Cooper #endif // GTEST_HAS_FILE_SYSTEM 216528f6c2f2SEnji Cooper 216628f6c2f2SEnji Cooper #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT) 2167b89a7cc2SEnji Cooper inline const char* StrError(int errnum) { return strerror(errnum); } 216828f6c2f2SEnji Cooper #endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT 216928f6c2f2SEnji Cooper 2170b89a7cc2SEnji Cooper inline const char* GetEnv(const char* name) { 217128f6c2f2SEnji Cooper #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_WINDOWS_PHONE) || \ 217228f6c2f2SEnji Cooper defined(GTEST_OS_ESP8266) || defined(GTEST_OS_XTENSA) || \ 217328f6c2f2SEnji Cooper defined(GTEST_OS_QURT) 217428f6c2f2SEnji Cooper // We are on an embedded platform, which has no environment variables. 2175b89a7cc2SEnji Cooper static_cast<void>(name); // To prevent 'unused argument' warning. 217628f6c2f2SEnji Cooper return nullptr; 2177b89a7cc2SEnji Cooper #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9) 2178b89a7cc2SEnji Cooper // Environment variables which we programmatically clear will be set to the 2179b89a7cc2SEnji Cooper // empty string rather than unset (NULL). Handle that case. 2180b89a7cc2SEnji Cooper const char* const env = getenv(name); 218128f6c2f2SEnji Cooper return (env != nullptr && env[0] != '\0') ? env : nullptr; 2182b89a7cc2SEnji Cooper #else 2183b89a7cc2SEnji Cooper return getenv(name); 2184b89a7cc2SEnji Cooper #endif 2185b89a7cc2SEnji Cooper } 2186b89a7cc2SEnji Cooper 2187b89a7cc2SEnji Cooper GTEST_DISABLE_MSC_DEPRECATED_POP_() 2188b89a7cc2SEnji Cooper 218928f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS_MOBILE 2190b89a7cc2SEnji Cooper // Windows CE has no C library. The abort() function is used in 2191b89a7cc2SEnji Cooper // several places in Google Test. This implementation provides a reasonable 2192b89a7cc2SEnji Cooper // imitation of standard behaviour. 219328f6c2f2SEnji Cooper [[noreturn]] void Abort(); 2194b89a7cc2SEnji Cooper #else 219528f6c2f2SEnji Cooper [[noreturn]] inline void Abort() { abort(); } 2196b89a7cc2SEnji Cooper #endif // GTEST_OS_WINDOWS_MOBILE 2197b89a7cc2SEnji Cooper 2198b89a7cc2SEnji Cooper } // namespace posix 2199b89a7cc2SEnji Cooper 2200b89a7cc2SEnji Cooper // MSVC "deprecates" snprintf and issues warnings wherever it is used. In 2201b89a7cc2SEnji Cooper // order to avoid these warnings, we need to use _snprintf or _snprintf_s on 2202b89a7cc2SEnji Cooper // MSVC-based platforms. We map the GTEST_SNPRINTF_ macro to the appropriate 2203b89a7cc2SEnji Cooper // function in order to achieve that. We use macro definition here because 2204b89a7cc2SEnji Cooper // snprintf is a variadic function. 220528f6c2f2SEnji Cooper #if defined(_MSC_VER) && !defined(GTEST_OS_WINDOWS_MOBILE) 2206b89a7cc2SEnji Cooper // MSVC 2005 and above support variadic macros. 2207b89a7cc2SEnji Cooper #define GTEST_SNPRINTF_(buffer, size, format, ...) \ 2208b89a7cc2SEnji Cooper _snprintf_s(buffer, size, size, format, __VA_ARGS__) 2209b89a7cc2SEnji Cooper #elif defined(_MSC_VER) 221028f6c2f2SEnji Cooper // Windows CE does not define _snprintf_s 2211b89a7cc2SEnji Cooper #define GTEST_SNPRINTF_ _snprintf 2212b89a7cc2SEnji Cooper #else 2213b89a7cc2SEnji Cooper #define GTEST_SNPRINTF_ snprintf 2214b89a7cc2SEnji Cooper #endif 2215b89a7cc2SEnji Cooper 221628f6c2f2SEnji Cooper // The biggest signed integer type the compiler supports. 2217b89a7cc2SEnji Cooper // 221828f6c2f2SEnji Cooper // long long is guaranteed to be at least 64-bits in C++11. 221928f6c2f2SEnji Cooper using BiggestInt = long long; // NOLINT 222028f6c2f2SEnji Cooper 222128f6c2f2SEnji Cooper // The maximum number a BiggestInt can represent. 222228f6c2f2SEnji Cooper constexpr BiggestInt kMaxBiggestInt = (std::numeric_limits<BiggestInt>::max)(); 2223b89a7cc2SEnji Cooper 2224b89a7cc2SEnji Cooper // This template class serves as a compile-time function from size to 2225b89a7cc2SEnji Cooper // type. It maps a size in bytes to a primitive type with that 2226b89a7cc2SEnji Cooper // size. e.g. 2227b89a7cc2SEnji Cooper // 2228b89a7cc2SEnji Cooper // TypeWithSize<4>::UInt 2229b89a7cc2SEnji Cooper // 2230b89a7cc2SEnji Cooper // is typedef-ed to be unsigned int (unsigned integer made up of 4 2231b89a7cc2SEnji Cooper // bytes). 2232b89a7cc2SEnji Cooper // 2233b89a7cc2SEnji Cooper // Such functionality should belong to STL, but I cannot find it 2234b89a7cc2SEnji Cooper // there. 2235b89a7cc2SEnji Cooper // 2236b89a7cc2SEnji Cooper // Google Test uses this class in the implementation of floating-point 2237b89a7cc2SEnji Cooper // comparison. 2238b89a7cc2SEnji Cooper // 2239b89a7cc2SEnji Cooper // For now it only handles UInt (unsigned int) as that's all Google Test 2240b89a7cc2SEnji Cooper // needs. Other types can be easily added in the future if need 2241b89a7cc2SEnji Cooper // arises. 2242b89a7cc2SEnji Cooper template <size_t size> 2243b89a7cc2SEnji Cooper class TypeWithSize { 2244b89a7cc2SEnji Cooper public: 2245b89a7cc2SEnji Cooper // This prevents the user from using TypeWithSize<N> with incorrect 2246b89a7cc2SEnji Cooper // values of N. 224728f6c2f2SEnji Cooper using UInt = void; 2248b89a7cc2SEnji Cooper }; 2249b89a7cc2SEnji Cooper 2250b89a7cc2SEnji Cooper // The specialization for size 4. 2251b89a7cc2SEnji Cooper template <> 2252b89a7cc2SEnji Cooper class TypeWithSize<4> { 2253b89a7cc2SEnji Cooper public: 225428f6c2f2SEnji Cooper using Int = std::int32_t; 225528f6c2f2SEnji Cooper using UInt = std::uint32_t; 2256b89a7cc2SEnji Cooper }; 2257b89a7cc2SEnji Cooper 2258b89a7cc2SEnji Cooper // The specialization for size 8. 2259b89a7cc2SEnji Cooper template <> 2260b89a7cc2SEnji Cooper class TypeWithSize<8> { 2261b89a7cc2SEnji Cooper public: 226228f6c2f2SEnji Cooper using Int = std::int64_t; 226328f6c2f2SEnji Cooper using UInt = std::uint64_t; 2264b89a7cc2SEnji Cooper }; 2265b89a7cc2SEnji Cooper 2266b89a7cc2SEnji Cooper // Integer types of known sizes. 226728f6c2f2SEnji Cooper using TimeInMillis = int64_t; // Represents time in milliseconds. 2268b89a7cc2SEnji Cooper 2269b89a7cc2SEnji Cooper // Utilities for command line flags and environment variables. 2270b89a7cc2SEnji Cooper 2271b89a7cc2SEnji Cooper // Macro for referencing flags. 2272b89a7cc2SEnji Cooper #if !defined(GTEST_FLAG) 227328f6c2f2SEnji Cooper #define GTEST_FLAG_NAME_(name) gtest_##name 2274b89a7cc2SEnji Cooper #define GTEST_FLAG(name) FLAGS_gtest_##name 2275b89a7cc2SEnji Cooper #endif // !defined(GTEST_FLAG) 2276b89a7cc2SEnji Cooper 227728f6c2f2SEnji Cooper // Pick a command line flags implementation. 2278*5ca8c28cSEnji Cooper #ifdef GTEST_INTERNAL_HAS_ABSL_FLAGS 2279b89a7cc2SEnji Cooper 2280b89a7cc2SEnji Cooper // Macros for defining flags. 2281b89a7cc2SEnji Cooper #define GTEST_DEFINE_bool_(name, default_val, doc) \ 228228f6c2f2SEnji Cooper ABSL_FLAG(bool, GTEST_FLAG_NAME_(name), default_val, doc) 2283b89a7cc2SEnji Cooper #define GTEST_DEFINE_int32_(name, default_val, doc) \ 228428f6c2f2SEnji Cooper ABSL_FLAG(int32_t, GTEST_FLAG_NAME_(name), default_val, doc) 2285b89a7cc2SEnji Cooper #define GTEST_DEFINE_string_(name, default_val, doc) \ 228628f6c2f2SEnji Cooper ABSL_FLAG(std::string, GTEST_FLAG_NAME_(name), default_val, doc) 2287b89a7cc2SEnji Cooper 228828f6c2f2SEnji Cooper // Macros for declaring flags. 228928f6c2f2SEnji Cooper #define GTEST_DECLARE_bool_(name) \ 229028f6c2f2SEnji Cooper ABSL_DECLARE_FLAG(bool, GTEST_FLAG_NAME_(name)) 229128f6c2f2SEnji Cooper #define GTEST_DECLARE_int32_(name) \ 229228f6c2f2SEnji Cooper ABSL_DECLARE_FLAG(int32_t, GTEST_FLAG_NAME_(name)) 229328f6c2f2SEnji Cooper #define GTEST_DECLARE_string_(name) \ 229428f6c2f2SEnji Cooper ABSL_DECLARE_FLAG(std::string, GTEST_FLAG_NAME_(name)) 229528f6c2f2SEnji Cooper 229628f6c2f2SEnji Cooper #define GTEST_FLAG_SAVER_ ::absl::FlagSaver 229728f6c2f2SEnji Cooper 229828f6c2f2SEnji Cooper #define GTEST_FLAG_GET(name) ::absl::GetFlag(GTEST_FLAG(name)) 229928f6c2f2SEnji Cooper #define GTEST_FLAG_SET(name, value) \ 230028f6c2f2SEnji Cooper (void)(::absl::SetFlag(>EST_FLAG(name), value)) 230128f6c2f2SEnji Cooper #define GTEST_USE_OWN_FLAGFILE_FLAG_ 0 230228f6c2f2SEnji Cooper 2303*5ca8c28cSEnji Cooper #undef GTEST_INTERNAL_HAS_ABSL_FLAGS 2304*5ca8c28cSEnji Cooper #else // ndef GTEST_INTERNAL_HAS_ABSL_FLAGS 230528f6c2f2SEnji Cooper 230628f6c2f2SEnji Cooper // Macros for defining flags. 230728f6c2f2SEnji Cooper #define GTEST_DEFINE_bool_(name, default_val, doc) \ 230828f6c2f2SEnji Cooper namespace testing { \ 230928f6c2f2SEnji Cooper GTEST_API_ bool GTEST_FLAG(name) = (default_val); \ 231028f6c2f2SEnji Cooper } \ 231128f6c2f2SEnji Cooper static_assert(true, "no-op to require trailing semicolon") 231228f6c2f2SEnji Cooper #define GTEST_DEFINE_int32_(name, default_val, doc) \ 231328f6c2f2SEnji Cooper namespace testing { \ 231428f6c2f2SEnji Cooper GTEST_API_ std::int32_t GTEST_FLAG(name) = (default_val); \ 231528f6c2f2SEnji Cooper } \ 231628f6c2f2SEnji Cooper static_assert(true, "no-op to require trailing semicolon") 231728f6c2f2SEnji Cooper #define GTEST_DEFINE_string_(name, default_val, doc) \ 231828f6c2f2SEnji Cooper namespace testing { \ 231928f6c2f2SEnji Cooper GTEST_API_ ::std::string GTEST_FLAG(name) = (default_val); \ 232028f6c2f2SEnji Cooper } \ 232128f6c2f2SEnji Cooper static_assert(true, "no-op to require trailing semicolon") 232228f6c2f2SEnji Cooper 232328f6c2f2SEnji Cooper // Macros for declaring flags. 232428f6c2f2SEnji Cooper #define GTEST_DECLARE_bool_(name) \ 232528f6c2f2SEnji Cooper namespace testing { \ 232628f6c2f2SEnji Cooper GTEST_API_ extern bool GTEST_FLAG(name); \ 232728f6c2f2SEnji Cooper } \ 232828f6c2f2SEnji Cooper static_assert(true, "no-op to require trailing semicolon") 232928f6c2f2SEnji Cooper #define GTEST_DECLARE_int32_(name) \ 233028f6c2f2SEnji Cooper namespace testing { \ 233128f6c2f2SEnji Cooper GTEST_API_ extern std::int32_t GTEST_FLAG(name); \ 233228f6c2f2SEnji Cooper } \ 233328f6c2f2SEnji Cooper static_assert(true, "no-op to require trailing semicolon") 233428f6c2f2SEnji Cooper #define GTEST_DECLARE_string_(name) \ 233528f6c2f2SEnji Cooper namespace testing { \ 233628f6c2f2SEnji Cooper GTEST_API_ extern ::std::string GTEST_FLAG(name); \ 233728f6c2f2SEnji Cooper } \ 233828f6c2f2SEnji Cooper static_assert(true, "no-op to require trailing semicolon") 233928f6c2f2SEnji Cooper 234028f6c2f2SEnji Cooper #define GTEST_FLAG_SAVER_ ::testing::internal::GTestFlagSaver 234128f6c2f2SEnji Cooper 234228f6c2f2SEnji Cooper #define GTEST_FLAG_GET(name) ::testing::GTEST_FLAG(name) 234328f6c2f2SEnji Cooper #define GTEST_FLAG_SET(name, value) (void)(::testing::GTEST_FLAG(name) = value) 234428f6c2f2SEnji Cooper #define GTEST_USE_OWN_FLAGFILE_FLAG_ 1 234528f6c2f2SEnji Cooper 2346*5ca8c28cSEnji Cooper #endif // GTEST_INTERNAL_HAS_ABSL_FLAGS 2347b89a7cc2SEnji Cooper 2348b89a7cc2SEnji Cooper // Thread annotations 2349b89a7cc2SEnji Cooper #if !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_) 2350b89a7cc2SEnji Cooper #define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks) 2351b89a7cc2SEnji Cooper #define GTEST_LOCK_EXCLUDED_(locks) 2352b89a7cc2SEnji Cooper #endif // !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_) 2353b89a7cc2SEnji Cooper 2354b89a7cc2SEnji Cooper // Parses 'str' for a 32-bit signed integer. If successful, writes the result 2355b89a7cc2SEnji Cooper // to *value and returns true; otherwise leaves *value unchanged and returns 2356b89a7cc2SEnji Cooper // false. 235728f6c2f2SEnji Cooper GTEST_API_ bool ParseInt32(const Message& src_text, const char* str, 235828f6c2f2SEnji Cooper int32_t* value); 2359b89a7cc2SEnji Cooper 236028f6c2f2SEnji Cooper // Parses a bool/int32_t/string from the environment variable 2361b89a7cc2SEnji Cooper // corresponding to the given Google Test flag. 2362b89a7cc2SEnji Cooper bool BoolFromGTestEnv(const char* flag, bool default_val); 236328f6c2f2SEnji Cooper GTEST_API_ int32_t Int32FromGTestEnv(const char* flag, int32_t default_val); 2364b89a7cc2SEnji Cooper std::string OutputFlagAlsoCheckEnvVar(); 2365b89a7cc2SEnji Cooper const char* StringFromGTestEnv(const char* flag, const char* default_val); 2366b89a7cc2SEnji Cooper 2367b89a7cc2SEnji Cooper } // namespace internal 2368b89a7cc2SEnji Cooper } // namespace testing 2369b89a7cc2SEnji Cooper 237028f6c2f2SEnji Cooper #if !defined(GTEST_INTERNAL_DEPRECATED) 237128f6c2f2SEnji Cooper 237228f6c2f2SEnji Cooper // Internal Macro to mark an API deprecated, for googletest usage only 237328f6c2f2SEnji Cooper // Usage: class GTEST_INTERNAL_DEPRECATED(message) MyClass or 237428f6c2f2SEnji Cooper // GTEST_INTERNAL_DEPRECATED(message) <return_type> myFunction(); Every usage of 237528f6c2f2SEnji Cooper // a deprecated entity will trigger a warning when compiled with 237628f6c2f2SEnji Cooper // `-Wdeprecated-declarations` option (clang, gcc, any __GNUC__ compiler). 237728f6c2f2SEnji Cooper // For msvc /W3 option will need to be used 237828f6c2f2SEnji Cooper // Note that for 'other' compilers this macro evaluates to nothing to prevent 237928f6c2f2SEnji Cooper // compilations errors. 238028f6c2f2SEnji Cooper #if defined(_MSC_VER) 238128f6c2f2SEnji Cooper #define GTEST_INTERNAL_DEPRECATED(message) __declspec(deprecated(message)) 238228f6c2f2SEnji Cooper #elif defined(__GNUC__) 238328f6c2f2SEnji Cooper #define GTEST_INTERNAL_DEPRECATED(message) __attribute__((deprecated(message))) 238428f6c2f2SEnji Cooper #else 238528f6c2f2SEnji Cooper #define GTEST_INTERNAL_DEPRECATED(message) 238628f6c2f2SEnji Cooper #endif 238728f6c2f2SEnji Cooper 238828f6c2f2SEnji Cooper #endif // !defined(GTEST_INTERNAL_DEPRECATED) 238928f6c2f2SEnji Cooper 239028f6c2f2SEnji Cooper #ifdef GTEST_HAS_ABSL 239128f6c2f2SEnji Cooper // Always use absl::any for UniversalPrinter<> specializations if googletest 239228f6c2f2SEnji Cooper // is built with absl support. 239328f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_ANY 1 239428f6c2f2SEnji Cooper #include "absl/types/any.h" 239528f6c2f2SEnji Cooper namespace testing { 239628f6c2f2SEnji Cooper namespace internal { 239728f6c2f2SEnji Cooper using Any = ::absl::any; 239828f6c2f2SEnji Cooper } // namespace internal 239928f6c2f2SEnji Cooper } // namespace testing 240028f6c2f2SEnji Cooper #else 2401*5ca8c28cSEnji Cooper #if defined(__cpp_lib_any) || (GTEST_INTERNAL_HAS_INCLUDE(<any>) && \ 2402*5ca8c28cSEnji Cooper GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L && \ 2403*5ca8c28cSEnji Cooper (!defined(_MSC_VER) || GTEST_HAS_RTTI)) 240428f6c2f2SEnji Cooper // Otherwise for C++17 and higher use std::any for UniversalPrinter<> 240528f6c2f2SEnji Cooper // specializations. 240628f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_ANY 1 240728f6c2f2SEnji Cooper #include <any> 240828f6c2f2SEnji Cooper namespace testing { 240928f6c2f2SEnji Cooper namespace internal { 241028f6c2f2SEnji Cooper using Any = ::std::any; 241128f6c2f2SEnji Cooper } // namespace internal 241228f6c2f2SEnji Cooper } // namespace testing 241328f6c2f2SEnji Cooper // The case where absl is configured NOT to alias std::any is not 241428f6c2f2SEnji Cooper // supported. 2415*5ca8c28cSEnji Cooper #endif // __cpp_lib_any 241628f6c2f2SEnji Cooper #endif // GTEST_HAS_ABSL 241728f6c2f2SEnji Cooper 241828f6c2f2SEnji Cooper #ifndef GTEST_INTERNAL_HAS_ANY 241928f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_ANY 0 242028f6c2f2SEnji Cooper #endif 242128f6c2f2SEnji Cooper 242228f6c2f2SEnji Cooper #ifdef GTEST_HAS_ABSL 242328f6c2f2SEnji Cooper // Always use absl::optional for UniversalPrinter<> specializations if 242428f6c2f2SEnji Cooper // googletest is built with absl support. 242528f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_OPTIONAL 1 242628f6c2f2SEnji Cooper #include "absl/types/optional.h" 242728f6c2f2SEnji Cooper namespace testing { 242828f6c2f2SEnji Cooper namespace internal { 242928f6c2f2SEnji Cooper template <typename T> 243028f6c2f2SEnji Cooper using Optional = ::absl::optional<T>; 243128f6c2f2SEnji Cooper inline ::absl::nullopt_t Nullopt() { return ::absl::nullopt; } 243228f6c2f2SEnji Cooper } // namespace internal 243328f6c2f2SEnji Cooper } // namespace testing 243428f6c2f2SEnji Cooper #else 2435*5ca8c28cSEnji Cooper #if defined(__cpp_lib_optional) || (GTEST_INTERNAL_HAS_INCLUDE(<optional>) && \ 2436*5ca8c28cSEnji Cooper GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L) 243728f6c2f2SEnji Cooper // Otherwise for C++17 and higher use std::optional for UniversalPrinter<> 243828f6c2f2SEnji Cooper // specializations. 243928f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_OPTIONAL 1 244028f6c2f2SEnji Cooper #include <optional> 244128f6c2f2SEnji Cooper namespace testing { 244228f6c2f2SEnji Cooper namespace internal { 244328f6c2f2SEnji Cooper template <typename T> 244428f6c2f2SEnji Cooper using Optional = ::std::optional<T>; 244528f6c2f2SEnji Cooper inline ::std::nullopt_t Nullopt() { return ::std::nullopt; } 244628f6c2f2SEnji Cooper } // namespace internal 244728f6c2f2SEnji Cooper } // namespace testing 244828f6c2f2SEnji Cooper // The case where absl is configured NOT to alias std::optional is not 244928f6c2f2SEnji Cooper // supported. 2450*5ca8c28cSEnji Cooper #endif // __cpp_lib_optional 245128f6c2f2SEnji Cooper #endif // GTEST_HAS_ABSL 245228f6c2f2SEnji Cooper 245328f6c2f2SEnji Cooper #ifndef GTEST_INTERNAL_HAS_OPTIONAL 245428f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_OPTIONAL 0 245528f6c2f2SEnji Cooper #endif 245628f6c2f2SEnji Cooper 2457*5ca8c28cSEnji Cooper #if defined(__cpp_lib_span) || (GTEST_INTERNAL_HAS_INCLUDE(<span>) && \ 2458*5ca8c28cSEnji Cooper GTEST_INTERNAL_CPLUSPLUS_LANG >= 202002L) 2459*5ca8c28cSEnji Cooper #define GTEST_INTERNAL_HAS_STD_SPAN 1 2460*5ca8c28cSEnji Cooper #endif // __cpp_lib_span 2461*5ca8c28cSEnji Cooper 2462*5ca8c28cSEnji Cooper #ifndef GTEST_INTERNAL_HAS_STD_SPAN 2463*5ca8c28cSEnji Cooper #define GTEST_INTERNAL_HAS_STD_SPAN 0 2464*5ca8c28cSEnji Cooper #endif 2465*5ca8c28cSEnji Cooper 246628f6c2f2SEnji Cooper #ifdef GTEST_HAS_ABSL 246728f6c2f2SEnji Cooper // Always use absl::string_view for Matcher<> specializations if googletest 246828f6c2f2SEnji Cooper // is built with absl support. 246928f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_STRING_VIEW 1 247028f6c2f2SEnji Cooper #include "absl/strings/string_view.h" 247128f6c2f2SEnji Cooper namespace testing { 247228f6c2f2SEnji Cooper namespace internal { 247328f6c2f2SEnji Cooper using StringView = ::absl::string_view; 247428f6c2f2SEnji Cooper } // namespace internal 247528f6c2f2SEnji Cooper } // namespace testing 247628f6c2f2SEnji Cooper #else 2477*5ca8c28cSEnji Cooper #if defined(__cpp_lib_string_view) || \ 2478*5ca8c28cSEnji Cooper (GTEST_INTERNAL_HAS_INCLUDE(<string_view>) && \ 2479*5ca8c28cSEnji Cooper GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L) 248028f6c2f2SEnji Cooper // Otherwise for C++17 and higher use std::string_view for Matcher<> 248128f6c2f2SEnji Cooper // specializations. 248228f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_STRING_VIEW 1 248328f6c2f2SEnji Cooper #include <string_view> 248428f6c2f2SEnji Cooper namespace testing { 248528f6c2f2SEnji Cooper namespace internal { 248628f6c2f2SEnji Cooper using StringView = ::std::string_view; 248728f6c2f2SEnji Cooper } // namespace internal 248828f6c2f2SEnji Cooper } // namespace testing 248928f6c2f2SEnji Cooper // The case where absl is configured NOT to alias std::string_view is not 249028f6c2f2SEnji Cooper // supported. 2491*5ca8c28cSEnji Cooper #endif // __cpp_lib_string_view 249228f6c2f2SEnji Cooper #endif // GTEST_HAS_ABSL 249328f6c2f2SEnji Cooper 249428f6c2f2SEnji Cooper #ifndef GTEST_INTERNAL_HAS_STRING_VIEW 249528f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_STRING_VIEW 0 249628f6c2f2SEnji Cooper #endif 249728f6c2f2SEnji Cooper 249828f6c2f2SEnji Cooper #ifdef GTEST_HAS_ABSL 249928f6c2f2SEnji Cooper // Always use absl::variant for UniversalPrinter<> specializations if googletest 250028f6c2f2SEnji Cooper // is built with absl support. 250128f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_VARIANT 1 250228f6c2f2SEnji Cooper #include "absl/types/variant.h" 250328f6c2f2SEnji Cooper namespace testing { 250428f6c2f2SEnji Cooper namespace internal { 250528f6c2f2SEnji Cooper template <typename... T> 250628f6c2f2SEnji Cooper using Variant = ::absl::variant<T...>; 250728f6c2f2SEnji Cooper } // namespace internal 250828f6c2f2SEnji Cooper } // namespace testing 250928f6c2f2SEnji Cooper #else 2510*5ca8c28cSEnji Cooper #if defined(__cpp_lib_variant) || (GTEST_INTERNAL_HAS_INCLUDE(<variant>) && \ 2511*5ca8c28cSEnji Cooper GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L) 251228f6c2f2SEnji Cooper // Otherwise for C++17 and higher use std::variant for UniversalPrinter<> 251328f6c2f2SEnji Cooper // specializations. 251428f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_VARIANT 1 251528f6c2f2SEnji Cooper #include <variant> 251628f6c2f2SEnji Cooper namespace testing { 251728f6c2f2SEnji Cooper namespace internal { 251828f6c2f2SEnji Cooper template <typename... T> 251928f6c2f2SEnji Cooper using Variant = ::std::variant<T...>; 252028f6c2f2SEnji Cooper } // namespace internal 252128f6c2f2SEnji Cooper } // namespace testing 252228f6c2f2SEnji Cooper // The case where absl is configured NOT to alias std::variant is not supported. 2523*5ca8c28cSEnji Cooper #endif // __cpp_lib_variant 252428f6c2f2SEnji Cooper #endif // GTEST_HAS_ABSL 252528f6c2f2SEnji Cooper 252628f6c2f2SEnji Cooper #ifndef GTEST_INTERNAL_HAS_VARIANT 252728f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_VARIANT 0 252828f6c2f2SEnji Cooper #endif 252928f6c2f2SEnji Cooper 2530*5ca8c28cSEnji Cooper #if (defined(__cpp_constexpr) && !defined(__cpp_inline_variables)) || \ 2531*5ca8c28cSEnji Cooper (defined(GTEST_INTERNAL_CPLUSPLUS_LANG) && \ 2532*5ca8c28cSEnji Cooper GTEST_INTERNAL_CPLUSPLUS_LANG < 201703L) 253328f6c2f2SEnji Cooper #define GTEST_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL 1 253428f6c2f2SEnji Cooper #endif 253528f6c2f2SEnji Cooper 253628f6c2f2SEnji Cooper #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 2537