1 // Copyright 2005, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 30 // Low-level types and utilities for porting Google Test to various 31 // platforms. All macros ending with _ and symbols defined in an 32 // internal namespace are subject to change without notice. Code 33 // outside Google Test MUST NOT USE THEM DIRECTLY. Macros that don't 34 // end with _ are part of Google Test's public API and can be used by 35 // code outside Google Test. 36 // 37 // This file is fundamental to Google Test. All other Google Test source 38 // files are expected to #include this. Therefore, it cannot #include 39 // any other Google Test header. 40 41 // IWYU pragma: private, include "gtest/gtest.h" 42 // IWYU pragma: friend gtest/.* 43 // IWYU pragma: friend gmock/.* 44 45 #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 46 #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 47 48 // Environment-describing macros 49 // ----------------------------- 50 // 51 // Google Test can be used in many different environments. Macros in 52 // this section tell Google Test what kind of environment it is being 53 // used in, such that Google Test can provide environment-specific 54 // features and implementations. 55 // 56 // Google Test tries to automatically detect the properties of its 57 // environment, so users usually don't need to worry about these 58 // macros. However, the automatic detection is not perfect. 59 // Sometimes it's necessary for a user to define some of the following 60 // macros in the build script to override Google Test's decisions. 61 // 62 // If the user doesn't define a macro in the list, Google Test will 63 // provide a default definition. After this header is #included, all 64 // macros in this list will be defined to either 1 or 0. 65 // 66 // Notes to maintainers: 67 // - Each macro here is a user-tweakable knob; do not grow the list 68 // lightly. 69 // - Use #if to key off these macros. Don't use #ifdef or "#if 70 // defined(...)", which will not work as these macros are ALWAYS 71 // defined. 72 // 73 // GTEST_HAS_CLONE - Define it to 1/0 to indicate that clone(2) 74 // is/isn't available. 75 // GTEST_HAS_EXCEPTIONS - Define it to 1/0 to indicate that exceptions 76 // are enabled. 77 // GTEST_HAS_POSIX_RE - Define it to 1/0 to indicate that POSIX regular 78 // expressions are/aren't available. 79 // GTEST_HAS_PTHREAD - Define it to 1/0 to indicate that <pthread.h> 80 // is/isn't available. 81 // GTEST_HAS_RTTI - Define it to 1/0 to indicate that RTTI is/isn't 82 // enabled. 83 // GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that 84 // std::wstring does/doesn't work (Google Test can 85 // be used where std::wstring is unavailable). 86 // GTEST_HAS_FILE_SYSTEM - Define it to 1/0 to indicate whether or not a 87 // file system is/isn't available. 88 // GTEST_HAS_SEH - Define it to 1/0 to indicate whether the 89 // compiler supports Microsoft's "Structured 90 // Exception Handling". 91 // GTEST_HAS_STREAM_REDIRECTION 92 // - Define it to 1/0 to indicate whether the 93 // platform supports I/O stream redirection using 94 // dup() and dup2(). 95 // GTEST_LINKED_AS_SHARED_LIBRARY 96 // - Define to 1 when compiling tests that use 97 // Google Test as a shared library (known as 98 // DLL on Windows). 99 // GTEST_CREATE_SHARED_LIBRARY 100 // - Define to 1 when compiling Google Test itself 101 // as a shared library. 102 // GTEST_DEFAULT_DEATH_TEST_STYLE 103 // - The default value of --gtest_death_test_style. 104 // The legacy default has been "fast" in the open 105 // source version since 2008. The recommended value 106 // is "threadsafe", and can be set in 107 // custom/gtest-port.h. 108 109 // Platform-indicating macros 110 // -------------------------- 111 // 112 // Macros indicating the platform on which Google Test is being used 113 // (a macro is defined to 1 if compiled on the given platform; 114 // otherwise UNDEFINED -- it's never defined to 0.). Google Test 115 // defines these macros automatically. Code outside Google Test MUST 116 // NOT define them. 117 // 118 // GTEST_OS_AIX - IBM AIX 119 // GTEST_OS_CYGWIN - Cygwin 120 // GTEST_OS_DRAGONFLY - DragonFlyBSD 121 // GTEST_OS_FREEBSD - FreeBSD 122 // GTEST_OS_FUCHSIA - Fuchsia 123 // GTEST_OS_GNU_HURD - GNU/Hurd 124 // GTEST_OS_GNU_KFREEBSD - GNU/kFreeBSD 125 // GTEST_OS_HAIKU - Haiku 126 // GTEST_OS_HPUX - HP-UX 127 // GTEST_OS_LINUX - Linux 128 // GTEST_OS_LINUX_ANDROID - Google Android 129 // GTEST_OS_MAC - Mac OS X 130 // GTEST_OS_IOS - iOS 131 // GTEST_OS_NACL - Google Native Client (NaCl) 132 // GTEST_OS_NETBSD - NetBSD 133 // GTEST_OS_OPENBSD - OpenBSD 134 // GTEST_OS_OS2 - OS/2 135 // GTEST_OS_QNX - QNX 136 // GTEST_OS_SOLARIS - Sun Solaris 137 // GTEST_OS_WINDOWS - Windows (Desktop, MinGW, or Mobile) 138 // GTEST_OS_WINDOWS_DESKTOP - Windows Desktop 139 // GTEST_OS_WINDOWS_MINGW - MinGW 140 // GTEST_OS_WINDOWS_MOBILE - Windows Mobile 141 // GTEST_OS_WINDOWS_PHONE - Windows Phone 142 // GTEST_OS_WINDOWS_RT - Windows Store App/WinRT 143 // GTEST_OS_ZOS - z/OS 144 // 145 // Among the platforms, Cygwin, Linux, Mac OS X, and Windows have the 146 // most stable support. Since core members of the Google Test project 147 // don't have access to other platforms, support for them may be less 148 // stable. If you notice any problems on your platform, please notify 149 // googletestframework@googlegroups.com (patches for fixing them are 150 // even more welcome!). 151 // 152 // It is possible that none of the GTEST_OS_* macros are defined. 153 154 // Feature-indicating macros 155 // ------------------------- 156 // 157 // Macros indicating which Google Test features are available (a macro 158 // is defined to 1 if the corresponding feature is supported; 159 // otherwise UNDEFINED -- it's never defined to 0.). Google Test 160 // defines these macros automatically. Code outside Google Test MUST 161 // NOT define them. 162 // 163 // These macros are public so that portable tests can be written. 164 // Such tests typically surround code using a feature with an #ifdef 165 // which controls that code. For example: 166 // 167 // #ifdef GTEST_HAS_DEATH_TEST 168 // EXPECT_DEATH(DoSomethingDeadly()); 169 // #endif 170 // 171 // GTEST_HAS_DEATH_TEST - death tests 172 // GTEST_HAS_TYPED_TEST - typed tests 173 // GTEST_HAS_TYPED_TEST_P - type-parameterized tests 174 // GTEST_IS_THREADSAFE - Google Test is thread-safe. 175 // GTEST_USES_RE2 - the RE2 regular expression library is used 176 // GTEST_USES_POSIX_RE - enhanced POSIX regex is used. Do not confuse with 177 // GTEST_HAS_POSIX_RE (see above) which users can 178 // define themselves. 179 // GTEST_USES_SIMPLE_RE - our own simple regex is used; 180 // the above RE\b(s) are mutually exclusive. 181 // GTEST_HAS_ABSL - Google Test is compiled with Abseil. 182 183 // Misc public macros 184 // ------------------ 185 // 186 // GTEST_FLAG(flag_name) - references the variable corresponding to 187 // the given Google Test flag. 188 189 // Internal utilities 190 // ------------------ 191 // 192 // The following macros and utilities are for Google Test's INTERNAL 193 // use only. Code outside Google Test MUST NOT USE THEM DIRECTLY. 194 // 195 // Macros for basic C++ coding: 196 // GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning. 197 // GTEST_MUST_USE_RESULT_ - declares that a function's result must be used. 198 // GTEST_INTENTIONAL_CONST_COND_PUSH_ - start code section where MSVC C4127 is 199 // suppressed (constant conditional). 200 // GTEST_INTENTIONAL_CONST_COND_POP_ - finish code section where MSVC C4127 201 // is suppressed. 202 // GTEST_INTERNAL_HAS_ANY - for enabling UniversalPrinter<std::any> or 203 // UniversalPrinter<absl::any> specializations. 204 // Always defined to 0 or 1. 205 // GTEST_INTERNAL_HAS_OPTIONAL - for enabling UniversalPrinter<std::optional> 206 // or 207 // UniversalPrinter<absl::optional> 208 // specializations. Always defined to 0 or 1. 209 // GTEST_INTERNAL_HAS_STD_SPAN - for enabling UniversalPrinter<std::span> 210 // specializations. Always defined to 0 or 1 211 // GTEST_INTERNAL_HAS_STRING_VIEW - for enabling Matcher<std::string_view> or 212 // Matcher<absl::string_view> 213 // specializations. Always defined to 0 or 1. 214 // GTEST_INTERNAL_HAS_VARIANT - for enabling UniversalPrinter<std::variant> or 215 // UniversalPrinter<absl::variant> 216 // specializations. Always defined to 0 or 1. 217 // GTEST_USE_OWN_FLAGFILE_FLAG_ - Always defined to 0 or 1. 218 // GTEST_HAS_CXXABI_H_ - Always defined to 0 or 1. 219 // GTEST_CAN_STREAM_RESULTS_ - Always defined to 0 or 1. 220 // GTEST_HAS_ALT_PATH_SEP_ - Always defined to 0 or 1. 221 // GTEST_WIDE_STRING_USES_UTF16_ - Always defined to 0 or 1. 222 // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ - Always defined to 0 or 1. 223 // GTEST_HAS_NOTIFICATION_- Always defined to 0 or 1. 224 // 225 // Synchronization: 226 // Mutex, MutexLock, ThreadLocal, GetThreadCount() 227 // - synchronization primitives. 228 // 229 // Regular expressions: 230 // RE - a simple regular expression class using 231 // 1) the RE2 syntax on all platforms when built with RE2 232 // and Abseil as dependencies 233 // 2) the POSIX Extended Regular Expression syntax on 234 // UNIX-like platforms, 235 // 3) A reduced regular exception syntax on other platforms, 236 // including Windows. 237 // Logging: 238 // GTEST_LOG_() - logs messages at the specified severity level. 239 // LogToStderr() - directs all log messages to stderr. 240 // FlushInfoLog() - flushes informational log messages. 241 // 242 // Stdout and stderr capturing: 243 // CaptureStdout() - starts capturing stdout. 244 // GetCapturedStdout() - stops capturing stdout and returns the captured 245 // string. 246 // CaptureStderr() - starts capturing stderr. 247 // GetCapturedStderr() - stops capturing stderr and returns the captured 248 // string. 249 // 250 // Integer types: 251 // TypeWithSize - maps an integer to a int type. 252 // TimeInMillis - integers of known sizes. 253 // BiggestInt - the biggest signed integer type. 254 // 255 // Command-line utilities: 256 // GetInjectableArgvs() - returns the command line as a vector of strings. 257 // 258 // Environment variable utilities: 259 // GetEnv() - gets the value of an environment variable. 260 // BoolFromGTestEnv() - parses a bool environment variable. 261 // Int32FromGTestEnv() - parses an int32_t environment variable. 262 // StringFromGTestEnv() - parses a string environment variable. 263 // 264 // Deprecation warnings: 265 // GTEST_INTERNAL_DEPRECATED(message) - attribute marking a function as 266 // deprecated; calling a marked function 267 // should generate a compiler warning 268 269 // The definition of GTEST_INTERNAL_CPLUSPLUS_LANG comes first because it can 270 // potentially be used as an #include guard. 271 #if defined(_MSVC_LANG) 272 #define GTEST_INTERNAL_CPLUSPLUS_LANG _MSVC_LANG 273 #elif defined(__cplusplus) 274 #define GTEST_INTERNAL_CPLUSPLUS_LANG __cplusplus 275 #endif 276 277 #if !defined(GTEST_INTERNAL_CPLUSPLUS_LANG) || \ 278 GTEST_INTERNAL_CPLUSPLUS_LANG < 201402L 279 #error C++ versions less than C++14 are not supported. 280 #endif 281 282 // MSVC >= 19.11 (VS 2017 Update 3) supports __has_include. 283 #ifdef __has_include 284 #define GTEST_INTERNAL_HAS_INCLUDE __has_include 285 #else 286 #define GTEST_INTERNAL_HAS_INCLUDE(...) 0 287 #endif 288 289 // Detect C++ feature test macros as gracefully as possible. 290 // MSVC >= 19.15, Clang >= 3.4.1, and GCC >= 4.1.2 support feature test macros. 291 #if GTEST_INTERNAL_CPLUSPLUS_LANG >= 202002L && \ 292 (!defined(__has_include) || GTEST_INTERNAL_HAS_INCLUDE(<version>)) 293 #include <version> // C++20 and later 294 #elif (!defined(__has_include) || GTEST_INTERNAL_HAS_INCLUDE(<ciso646>)) 295 #include <ciso646> // Pre-C++20 296 #endif 297 298 #include <ctype.h> // for isspace, etc 299 #include <stddef.h> // for ptrdiff_t 300 #include <stdio.h> 301 #include <stdlib.h> 302 #include <string.h> 303 304 #include <cerrno> 305 // #include <condition_variable> // Guarded by GTEST_IS_THREADSAFE below 306 #include <cstdint> 307 #include <iostream> 308 #include <limits> 309 #include <locale> 310 #include <memory> 311 #include <ostream> 312 #include <string> 313 // #include <mutex> // Guarded by GTEST_IS_THREADSAFE below 314 #include <tuple> 315 #include <type_traits> 316 #include <vector> 317 318 #ifndef _WIN32_WCE 319 #include <sys/stat.h> 320 #include <sys/types.h> 321 #endif // !_WIN32_WCE 322 323 #if defined __APPLE__ 324 #include <AvailabilityMacros.h> 325 #include <TargetConditionals.h> 326 #endif 327 328 #include "gtest/internal/custom/gtest-port.h" 329 #include "gtest/internal/gtest-port-arch.h" 330 331 #ifndef GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ 332 #define GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ 0 333 #endif 334 335 #ifndef GTEST_HAS_NOTIFICATION_ 336 #define GTEST_HAS_NOTIFICATION_ 0 337 #endif 338 339 #if defined(GTEST_HAS_ABSL) && !defined(GTEST_NO_ABSL_FLAGS) 340 #define GTEST_INTERNAL_HAS_ABSL_FLAGS // Used only in this file. 341 #include "absl/flags/declare.h" 342 #include "absl/flags/flag.h" 343 #include "absl/flags/reflection.h" 344 #endif 345 346 #if !defined(GTEST_DEV_EMAIL_) 347 #define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com" 348 #define GTEST_FLAG_PREFIX_ "gtest_" 349 #define GTEST_FLAG_PREFIX_DASH_ "gtest-" 350 #define GTEST_FLAG_PREFIX_UPPER_ "GTEST_" 351 #define GTEST_NAME_ "Google Test" 352 #define GTEST_PROJECT_URL_ "https://github.com/google/googletest/" 353 #endif // !defined(GTEST_DEV_EMAIL_) 354 355 #if !defined(GTEST_INIT_GOOGLE_TEST_NAME_) 356 #define GTEST_INIT_GOOGLE_TEST_NAME_ "testing::InitGoogleTest" 357 #endif // !defined(GTEST_INIT_GOOGLE_TEST_NAME_) 358 359 // Determines the version of gcc that is used to compile this. 360 #ifdef __GNUC__ 361 // 40302 means version 4.3.2. 362 #define GTEST_GCC_VER_ \ 363 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) 364 #endif // __GNUC__ 365 366 // Macros for disabling Microsoft Visual C++ warnings. 367 // 368 // GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 4385) 369 // /* code that triggers warnings C4800 and C4385 */ 370 // GTEST_DISABLE_MSC_WARNINGS_POP_() 371 #if defined(_MSC_VER) 372 #define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) \ 373 __pragma(warning(push)) __pragma(warning(disable : warnings)) 374 #define GTEST_DISABLE_MSC_WARNINGS_POP_() __pragma(warning(pop)) 375 #else 376 // Not all compilers are MSVC 377 #define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) 378 #define GTEST_DISABLE_MSC_WARNINGS_POP_() 379 #endif 380 381 // Clang on Windows does not understand MSVC's pragma warning. 382 // We need clang-specific way to disable function deprecation warning. 383 #ifdef __clang__ 384 #define GTEST_DISABLE_MSC_DEPRECATED_PUSH_() \ 385 _Pragma("clang diagnostic push") \ 386 _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") \ 387 _Pragma("clang diagnostic ignored \"-Wdeprecated-implementations\"") 388 #define GTEST_DISABLE_MSC_DEPRECATED_POP_() _Pragma("clang diagnostic pop") 389 #else 390 #define GTEST_DISABLE_MSC_DEPRECATED_PUSH_() \ 391 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996) 392 #define GTEST_DISABLE_MSC_DEPRECATED_POP_() GTEST_DISABLE_MSC_WARNINGS_POP_() 393 #endif 394 395 // Brings in definitions for functions used in the testing::internal::posix 396 // namespace (read, write, close, chdir, isatty, stat). We do not currently 397 // use them on Windows Mobile. 398 #ifdef GTEST_OS_WINDOWS 399 #ifndef GTEST_OS_WINDOWS_MOBILE 400 #include <direct.h> 401 #include <io.h> 402 #endif 403 // In order to avoid having to include <windows.h>, use forward declaration 404 #if defined(GTEST_OS_WINDOWS_MINGW) && !defined(__MINGW64_VERSION_MAJOR) 405 // MinGW defined _CRITICAL_SECTION and _RTL_CRITICAL_SECTION as two 406 // separate (equivalent) structs, instead of using typedef 407 typedef struct _CRITICAL_SECTION GTEST_CRITICAL_SECTION; 408 #else 409 // Assume CRITICAL_SECTION is a typedef of _RTL_CRITICAL_SECTION. 410 // This assumption is verified by 411 // WindowsTypesTest.CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION. 412 typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION; 413 #endif 414 #elif defined(GTEST_OS_XTENSA) 415 #include <unistd.h> 416 // Xtensa toolchains define strcasecmp in the string.h header instead of 417 // strings.h. string.h is already included. 418 #else 419 // This assumes that non-Windows OSes provide unistd.h. For OSes where this 420 // is not the case, we need to include headers that provide the functions 421 // mentioned above. 422 #include <strings.h> 423 #include <unistd.h> 424 #endif // GTEST_OS_WINDOWS 425 426 #ifdef GTEST_OS_LINUX_ANDROID 427 // Used to define __ANDROID_API__ matching the target NDK API level. 428 #include <android/api-level.h> // NOLINT 429 #endif 430 431 // Defines this to true if and only if Google Test can use POSIX regular 432 // expressions. 433 #ifndef GTEST_HAS_POSIX_RE 434 #ifdef GTEST_OS_LINUX_ANDROID 435 // On Android, <regex.h> is only available starting with Gingerbread. 436 #define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9) 437 #else 438 #if !(defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_XTENSA) || \ 439 defined(GTEST_OS_QURT)) 440 #define GTEST_HAS_POSIX_RE 1 441 #else 442 #define GTEST_HAS_POSIX_RE 0 443 #endif 444 #endif // GTEST_OS_LINUX_ANDROID 445 #endif 446 447 // Select the regular expression implementation. 448 #ifdef GTEST_HAS_ABSL 449 // When using Abseil, RE2 is required. 450 #include "absl/strings/string_view.h" 451 #include "re2/re2.h" 452 #define GTEST_USES_RE2 1 453 #elif GTEST_HAS_POSIX_RE 454 #include <regex.h> // NOLINT 455 #define GTEST_USES_POSIX_RE 1 456 #else 457 // Use our own simple regex implementation. 458 #define GTEST_USES_SIMPLE_RE 1 459 #endif 460 461 #ifndef GTEST_HAS_EXCEPTIONS 462 // The user didn't tell us whether exceptions are enabled, so we need 463 // to figure it out. 464 #if defined(_MSC_VER) && defined(_CPPUNWIND) 465 // MSVC defines _CPPUNWIND to 1 if and only if exceptions are enabled. 466 #define GTEST_HAS_EXCEPTIONS 1 467 #elif defined(__BORLANDC__) 468 // C++Builder's implementation of the STL uses the _HAS_EXCEPTIONS 469 // macro to enable exceptions, so we'll do the same. 470 // Assumes that exceptions are enabled by default. 471 #ifndef _HAS_EXCEPTIONS 472 #define _HAS_EXCEPTIONS 1 473 #endif // _HAS_EXCEPTIONS 474 #define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS 475 #elif defined(__clang__) 476 // clang defines __EXCEPTIONS if and only if exceptions are enabled before clang 477 // 220714, but if and only if cleanups are enabled after that. In Obj-C++ files, 478 // there can be cleanups for ObjC exceptions which also need cleanups, even if 479 // C++ exceptions are disabled. clang has __has_feature(cxx_exceptions) which 480 // checks for C++ exceptions starting at clang r206352, but which checked for 481 // cleanups prior to that. To reliably check for C++ exception availability with 482 // clang, check for 483 // __EXCEPTIONS && __has_feature(cxx_exceptions). 484 #if defined(__EXCEPTIONS) && __EXCEPTIONS && __has_feature(cxx_exceptions) 485 #define GTEST_HAS_EXCEPTIONS 1 486 #else 487 #define GTEST_HAS_EXCEPTIONS 0 488 #endif 489 #elif defined(__GNUC__) && defined(__EXCEPTIONS) && __EXCEPTIONS 490 // gcc defines __EXCEPTIONS to 1 if and only if exceptions are enabled. 491 #define GTEST_HAS_EXCEPTIONS 1 492 #elif defined(__SUNPRO_CC) 493 // Sun Pro CC supports exceptions. However, there is no compile-time way of 494 // detecting whether they are enabled or not. Therefore, we assume that 495 // they are enabled unless the user tells us otherwise. 496 #define GTEST_HAS_EXCEPTIONS 1 497 #elif defined(__IBMCPP__) && defined(__EXCEPTIONS) && __EXCEPTIONS 498 // xlC defines __EXCEPTIONS to 1 if and only if exceptions are enabled. 499 #define GTEST_HAS_EXCEPTIONS 1 500 #elif defined(__HP_aCC) 501 // Exception handling is in effect by default in HP aCC compiler. It has to 502 // be turned of by +noeh compiler option if desired. 503 #define GTEST_HAS_EXCEPTIONS 1 504 #else 505 // For other compilers, we assume exceptions are disabled to be 506 // conservative. 507 #define GTEST_HAS_EXCEPTIONS 0 508 #endif // defined(_MSC_VER) || defined(__BORLANDC__) 509 #endif // GTEST_HAS_EXCEPTIONS 510 511 #ifndef GTEST_HAS_STD_WSTRING 512 // The user didn't tell us whether ::std::wstring is available, so we need 513 // to figure it out. 514 // Cygwin 1.7 and below doesn't support ::std::wstring. 515 // Solaris' libc++ doesn't support it either. Android has 516 // no support for it at least as recent as Froyo (2.2). 517 #if (!(defined(GTEST_OS_LINUX_ANDROID) || defined(GTEST_OS_CYGWIN) || \ 518 defined(GTEST_OS_SOLARIS) || defined(GTEST_OS_HAIKU) || \ 519 defined(GTEST_OS_ESP32) || defined(GTEST_OS_ESP8266) || \ 520 defined(GTEST_OS_XTENSA) || defined(GTEST_OS_QURT) || \ 521 defined(GTEST_OS_NXP_QN9090) || defined(GTEST_OS_NRF52))) 522 #define GTEST_HAS_STD_WSTRING 1 523 #else 524 #define GTEST_HAS_STD_WSTRING 0 525 #endif 526 #endif // GTEST_HAS_STD_WSTRING 527 528 #ifndef GTEST_HAS_FILE_SYSTEM 529 // Most platforms support a file system. 530 #define GTEST_HAS_FILE_SYSTEM 1 531 #endif // GTEST_HAS_FILE_SYSTEM 532 533 // Determines whether RTTI is available. 534 #ifndef GTEST_HAS_RTTI 535 // The user didn't tell us whether RTTI is enabled, so we need to 536 // figure it out. 537 538 #ifdef _MSC_VER 539 540 #ifdef _CPPRTTI // MSVC defines this macro if and only if RTTI is enabled. 541 #define GTEST_HAS_RTTI 1 542 #else 543 #define GTEST_HAS_RTTI 0 544 #endif 545 546 // Starting with version 4.3.2, gcc defines __GXX_RTTI if and only if RTTI is 547 // enabled. 548 #elif defined(__GNUC__) 549 550 #ifdef __GXX_RTTI 551 // When building against STLport with the Android NDK and with 552 // -frtti -fno-exceptions, the build fails at link time with undefined 553 // references to __cxa_bad_typeid. Note sure if STL or toolchain bug, 554 // so disable RTTI when detected. 555 #if defined(GTEST_OS_LINUX_ANDROID) && defined(_STLPORT_MAJOR) && \ 556 !defined(__EXCEPTIONS) 557 #define GTEST_HAS_RTTI 0 558 #else 559 #define GTEST_HAS_RTTI 1 560 #endif // GTEST_OS_LINUX_ANDROID && __STLPORT_MAJOR && !__EXCEPTIONS 561 #else 562 #define GTEST_HAS_RTTI 0 563 #endif // __GXX_RTTI 564 565 // Clang defines __GXX_RTTI starting with version 3.0, but its manual recommends 566 // using has_feature instead. has_feature(cxx_rtti) is supported since 2.7, the 567 // first version with C++ support. 568 #elif defined(__clang__) 569 570 #define GTEST_HAS_RTTI __has_feature(cxx_rtti) 571 572 // Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if 573 // both the typeid and dynamic_cast features are present. 574 #elif defined(__IBMCPP__) && (__IBMCPP__ >= 900) 575 576 #ifdef __RTTI_ALL__ 577 #define GTEST_HAS_RTTI 1 578 #else 579 #define GTEST_HAS_RTTI 0 580 #endif 581 582 #else 583 584 // For all other compilers, we assume RTTI is enabled. 585 #define GTEST_HAS_RTTI 1 586 587 #endif // _MSC_VER 588 589 #endif // GTEST_HAS_RTTI 590 591 // It's this header's responsibility to #include <typeinfo> when RTTI 592 // is enabled. 593 #if GTEST_HAS_RTTI 594 #include <typeinfo> 595 #endif 596 597 // Determines whether Google Test can use the pthreads library. 598 #ifndef GTEST_HAS_PTHREAD 599 // The user didn't tell us explicitly, so we make reasonable assumptions about 600 // which platforms have pthreads support. 601 // 602 // To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0 603 // to your compiler flags. 604 #if (defined(GTEST_OS_LINUX) || defined(GTEST_OS_MAC) || \ 605 defined(GTEST_OS_HPUX) || defined(GTEST_OS_QNX) || \ 606 defined(GTEST_OS_FREEBSD) || defined(GTEST_OS_NACL) || \ 607 defined(GTEST_OS_NETBSD) || defined(GTEST_OS_FUCHSIA) || \ 608 defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_GNU_KFREEBSD) || \ 609 defined(GTEST_OS_OPENBSD) || defined(GTEST_OS_HAIKU) || \ 610 defined(GTEST_OS_GNU_HURD) || defined(GTEST_OS_SOLARIS) || \ 611 defined(GTEST_OS_AIX) || defined(GTEST_OS_ZOS)) 612 #define GTEST_HAS_PTHREAD 1 613 #else 614 #define GTEST_HAS_PTHREAD 0 615 #endif 616 #endif // GTEST_HAS_PTHREAD 617 618 #if GTEST_HAS_PTHREAD 619 // gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is 620 // true. 621 #include <pthread.h> // NOLINT 622 623 // For timespec and nanosleep, used below. 624 #include <time.h> // NOLINT 625 #endif 626 627 // Determines whether clone(2) is supported. 628 // Usually it will only be available on Linux, excluding 629 // Linux on the Itanium architecture. 630 // Also see https://linux.die.net/man/2/clone. 631 #ifndef GTEST_HAS_CLONE 632 // The user didn't tell us, so we need to figure it out. 633 634 #if defined(GTEST_OS_LINUX) && !defined(__ia64__) 635 #if defined(GTEST_OS_LINUX_ANDROID) 636 // On Android, clone() became available at different API levels for each 32-bit 637 // architecture. 638 #if defined(__LP64__) || (defined(__arm__) && __ANDROID_API__ >= 9) || \ 639 (defined(__mips__) && __ANDROID_API__ >= 12) || \ 640 (defined(__i386__) && __ANDROID_API__ >= 17) 641 #define GTEST_HAS_CLONE 1 642 #else 643 #define GTEST_HAS_CLONE 0 644 #endif 645 #else 646 #define GTEST_HAS_CLONE 1 647 #endif 648 #else 649 #define GTEST_HAS_CLONE 0 650 #endif // GTEST_OS_LINUX && !defined(__ia64__) 651 652 #endif // GTEST_HAS_CLONE 653 654 // Determines whether to support stream redirection. This is used to test 655 // output correctness and to implement death tests. 656 #ifndef GTEST_HAS_STREAM_REDIRECTION 657 // By default, we assume that stream redirection is supported on all 658 // platforms except known mobile / embedded ones. Also, if the port doesn't have 659 // a file system, stream redirection is not supported. 660 #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_WINDOWS_PHONE) || \ 661 defined(GTEST_OS_WINDOWS_RT) || defined(GTEST_OS_WINDOWS_GAMES) || \ 662 defined(GTEST_OS_ESP8266) || defined(GTEST_OS_XTENSA) || \ 663 defined(GTEST_OS_QURT) || !GTEST_HAS_FILE_SYSTEM 664 #define GTEST_HAS_STREAM_REDIRECTION 0 665 #else 666 #define GTEST_HAS_STREAM_REDIRECTION 1 667 #endif // !GTEST_OS_WINDOWS_MOBILE 668 #endif // GTEST_HAS_STREAM_REDIRECTION 669 670 // Determines whether to support death tests. 671 // pops up a dialog window that cannot be suppressed programmatically. 672 #if (defined(GTEST_OS_LINUX) || defined(GTEST_OS_CYGWIN) || \ 673 defined(GTEST_OS_SOLARIS) || defined(GTEST_OS_ZOS) || \ 674 (defined(GTEST_OS_MAC) && !defined(GTEST_OS_IOS)) || \ 675 (defined(GTEST_OS_WINDOWS_DESKTOP) && _MSC_VER) || \ 676 defined(GTEST_OS_WINDOWS_MINGW) || defined(GTEST_OS_AIX) || \ 677 defined(GTEST_OS_HPUX) || defined(GTEST_OS_OPENBSD) || \ 678 defined(GTEST_OS_QNX) || defined(GTEST_OS_FREEBSD) || \ 679 defined(GTEST_OS_NETBSD) || defined(GTEST_OS_FUCHSIA) || \ 680 defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_GNU_KFREEBSD) || \ 681 defined(GTEST_OS_HAIKU) || defined(GTEST_OS_GNU_HURD)) 682 // Death tests require a file system to work properly. 683 #if GTEST_HAS_FILE_SYSTEM 684 #define GTEST_HAS_DEATH_TEST 1 685 #endif // GTEST_HAS_FILE_SYSTEM 686 #endif 687 688 // Determines whether to support type-driven tests. 689 690 // Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0, 691 // Sun Pro CC, IBM Visual Age, and HP aCC support. 692 #if defined(__GNUC__) || defined(_MSC_VER) || defined(__SUNPRO_CC) || \ 693 defined(__IBMCPP__) || defined(__HP_aCC) 694 #define GTEST_HAS_TYPED_TEST 1 695 #define GTEST_HAS_TYPED_TEST_P 1 696 #endif 697 698 // Determines whether the system compiler uses UTF-16 for encoding wide strings. 699 #if defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_CYGWIN) || \ 700 defined(GTEST_OS_AIX) || defined(GTEST_OS_OS2) 701 #define GTEST_WIDE_STRING_USES_UTF16_ 1 702 #else 703 #define GTEST_WIDE_STRING_USES_UTF16_ 0 704 #endif 705 706 // Determines whether test results can be streamed to a socket. 707 #if defined(GTEST_OS_LINUX) || defined(GTEST_OS_GNU_KFREEBSD) || \ 708 defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_FREEBSD) || \ 709 defined(GTEST_OS_NETBSD) || defined(GTEST_OS_OPENBSD) || \ 710 defined(GTEST_OS_GNU_HURD) || defined(GTEST_OS_MAC) 711 #define GTEST_CAN_STREAM_RESULTS_ 1 712 #else 713 #define GTEST_CAN_STREAM_RESULTS_ 0 714 #endif 715 716 // Defines some utility macros. 717 718 // The GNU compiler emits a warning if nested "if" statements are followed by 719 // an "else" statement and braces are not used to explicitly disambiguate the 720 // "else" binding. This leads to problems with code like: 721 // 722 // if (gate) 723 // ASSERT_*(condition) << "Some message"; 724 // 725 // The "switch (0) case 0:" idiom is used to suppress this. 726 #ifdef __INTEL_COMPILER 727 #define GTEST_AMBIGUOUS_ELSE_BLOCKER_ 728 #else 729 #define GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 730 switch (0) \ 731 case 0: \ 732 default: // NOLINT 733 #endif 734 735 // GTEST_HAVE_ATTRIBUTE_ 736 // 737 // A function-like feature checking macro that is a wrapper around 738 // `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a 739 // nonzero constant integer if the attribute is supported or 0 if not. 740 // 741 // It evaluates to zero if `__has_attribute` is not defined by the compiler. 742 // 743 // GCC: https://gcc.gnu.org/gcc-5/changes.html 744 // Clang: https://clang.llvm.org/docs/LanguageExtensions.html 745 #ifdef __has_attribute 746 #define GTEST_HAVE_ATTRIBUTE_(x) __has_attribute(x) 747 #else 748 #define GTEST_HAVE_ATTRIBUTE_(x) 0 749 #endif 750 751 // GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE 752 // 753 // A function-like feature checking macro that accepts C++11 style attributes. 754 // It's a wrapper around `__has_cpp_attribute`, defined by ISO C++ SD-6 755 // (https://en.cppreference.com/w/cpp/experimental/feature_test). If we don't 756 // find `__has_cpp_attribute`, will evaluate to 0. 757 #if defined(__has_cpp_attribute) 758 // NOTE: requiring __cplusplus above should not be necessary, but 759 // works around https://bugs.llvm.org/show_bug.cgi?id=23435. 760 #define GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) 761 #else 762 #define GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(x) 0 763 #endif 764 765 // GTEST_HAVE_FEATURE_ 766 // 767 // A function-like feature checking macro that is a wrapper around 768 // `__has_feature`. 769 #ifdef __has_feature 770 #define GTEST_HAVE_FEATURE_(x) __has_feature(x) 771 #else 772 #define GTEST_HAVE_FEATURE_(x) 0 773 #endif 774 775 // Use this annotation after a variable or parameter declaration to tell the 776 // compiler the variable/parameter may be used. 777 // Example: 778 // 779 // GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED int foo = bar(); 780 // 781 // This can be removed once we only support only C++17 or newer and 782 // [[maybe_unused]] is available on all supported platforms. 783 #if GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(maybe_unused) 784 #define GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED [[maybe_unused]] 785 #elif GTEST_HAVE_ATTRIBUTE_(unused) 786 // This is inferior to [[maybe_unused]] as it can produce a 787 // -Wused-but-marked-unused warning on optionally used symbols, but it is all we 788 // have. 789 #define GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED __attribute__((__unused__)) 790 #else 791 #define GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED 792 #endif 793 794 // Use this annotation before a function that takes a printf format string. 795 #if GTEST_HAVE_ATTRIBUTE_(format) && defined(__MINGW_PRINTF_FORMAT) 796 // MinGW has two different printf implementations. Ensure the format macro 797 // matches the selected implementation. See 798 // https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/. 799 #define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \ 800 __attribute__((format(__MINGW_PRINTF_FORMAT, string_index, first_to_check))) 801 #elif GTEST_HAVE_ATTRIBUTE_(format) 802 #define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \ 803 __attribute__((format(printf, string_index, first_to_check))) 804 #else 805 #define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) 806 #endif 807 808 // Tell the compiler to warn about unused return values for functions declared 809 // with this macro. The macro should be used on function declarations 810 // following the argument list: 811 // 812 // Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_; 813 #if GTEST_HAVE_ATTRIBUTE_(warn_unused_result) 814 #define GTEST_MUST_USE_RESULT_ __attribute__((warn_unused_result)) 815 #else 816 #define GTEST_MUST_USE_RESULT_ 817 #endif 818 819 // MS C++ compiler emits warning when a conditional expression is compile time 820 // constant. In some contexts this warning is false positive and needs to be 821 // suppressed. Use the following two macros in such cases: 822 // 823 // GTEST_INTENTIONAL_CONST_COND_PUSH_() 824 // while (true) { 825 // GTEST_INTENTIONAL_CONST_COND_POP_() 826 // } 827 #define GTEST_INTENTIONAL_CONST_COND_PUSH_() \ 828 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127) 829 #define GTEST_INTENTIONAL_CONST_COND_POP_() GTEST_DISABLE_MSC_WARNINGS_POP_() 830 831 // Determine whether the compiler supports Microsoft's Structured Exception 832 // Handling. This is supported by several Windows compilers but generally 833 // does not exist on any other system. 834 #ifndef GTEST_HAS_SEH 835 // The user didn't tell us, so we need to figure it out. 836 837 #if defined(_MSC_VER) || defined(__BORLANDC__) 838 // These two compilers are known to support SEH. 839 #define GTEST_HAS_SEH 1 840 #else 841 // Assume no SEH. 842 #define GTEST_HAS_SEH 0 843 #endif 844 845 #endif // GTEST_HAS_SEH 846 847 #ifndef GTEST_IS_THREADSAFE 848 849 #if (GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ || \ 850 (defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_PHONE) && \ 851 !defined(GTEST_OS_WINDOWS_RT)) || \ 852 GTEST_HAS_PTHREAD) 853 #define GTEST_IS_THREADSAFE 1 854 #endif 855 856 #endif // GTEST_IS_THREADSAFE 857 858 #ifdef GTEST_IS_THREADSAFE 859 // Some platforms don't support including these threading related headers. 860 #include <condition_variable> // NOLINT 861 #include <mutex> // NOLINT 862 #endif // GTEST_IS_THREADSAFE 863 864 // GTEST_API_ qualifies all symbols that must be exported. The definitions below 865 // are guarded by #ifndef to give embedders a chance to define GTEST_API_ in 866 // gtest/internal/custom/gtest-port.h 867 #ifndef GTEST_API_ 868 869 #ifdef _MSC_VER 870 #if defined(GTEST_LINKED_AS_SHARED_LIBRARY) && GTEST_LINKED_AS_SHARED_LIBRARY 871 #define GTEST_API_ __declspec(dllimport) 872 #elif defined(GTEST_CREATE_SHARED_LIBRARY) && GTEST_CREATE_SHARED_LIBRARY 873 #define GTEST_API_ __declspec(dllexport) 874 #endif 875 #elif GTEST_HAVE_ATTRIBUTE_(visibility) 876 #define GTEST_API_ __attribute__((visibility("default"))) 877 #endif // _MSC_VER 878 879 #endif // GTEST_API_ 880 881 #ifndef GTEST_API_ 882 #define GTEST_API_ 883 #endif // GTEST_API_ 884 885 #ifndef GTEST_DEFAULT_DEATH_TEST_STYLE 886 #define GTEST_DEFAULT_DEATH_TEST_STYLE "fast" 887 #endif // GTEST_DEFAULT_DEATH_TEST_STYLE 888 889 #if GTEST_HAVE_ATTRIBUTE_(noinline) 890 // Ask the compiler to never inline a given function. 891 #define GTEST_NO_INLINE_ __attribute__((noinline)) 892 #else 893 #define GTEST_NO_INLINE_ 894 #endif 895 896 #if GTEST_HAVE_ATTRIBUTE_(disable_tail_calls) 897 // Ask the compiler not to perform tail call optimization inside 898 // the marked function. 899 #define GTEST_NO_TAIL_CALL_ __attribute__((disable_tail_calls)) 900 #elif defined(__GNUC__) && !defined(__NVCOMPILER) 901 #define GTEST_NO_TAIL_CALL_ \ 902 __attribute__((optimize("no-optimize-sibling-calls"))) 903 #else 904 #define GTEST_NO_TAIL_CALL_ 905 #endif 906 907 // _LIBCPP_VERSION is defined by the libc++ library from the LLVM project. 908 #if !defined(GTEST_HAS_CXXABI_H_) 909 #if defined(__GLIBCXX__) || (defined(_LIBCPP_VERSION) && !defined(_MSC_VER)) 910 #define GTEST_HAS_CXXABI_H_ 1 911 #else 912 #define GTEST_HAS_CXXABI_H_ 0 913 #endif 914 #endif 915 916 // A function level attribute to disable checking for use of uninitialized 917 // memory when built with MemorySanitizer. 918 #if GTEST_HAVE_ATTRIBUTE_(no_sanitize_memory) 919 #define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ __attribute__((no_sanitize_memory)) 920 #else 921 #define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ 922 #endif 923 924 // A function level attribute to disable AddressSanitizer instrumentation. 925 #if GTEST_HAVE_ATTRIBUTE_(no_sanitize_address) 926 #define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ \ 927 __attribute__((no_sanitize_address)) 928 #else 929 #define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ 930 #endif 931 932 // A function level attribute to disable HWAddressSanitizer instrumentation. 933 #if GTEST_HAVE_FEATURE_(hwaddress_sanitizer) && \ 934 GTEST_HAVE_ATTRIBUTE_(no_sanitize) 935 #define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ \ 936 __attribute__((no_sanitize("hwaddress"))) 937 #else 938 #define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ 939 #endif 940 941 // A function level attribute to disable ThreadSanitizer instrumentation. 942 #if GTEST_HAVE_ATTRIBUTE_(no_sanitize_thread) 943 #define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ __attribute((no_sanitize_thread)) 944 #else 945 #define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ 946 #endif 947 948 namespace testing { 949 950 class Message; 951 952 // Legacy imports for backwards compatibility. 953 // New code should use std:: names directly. 954 using std::get; 955 using std::make_tuple; 956 using std::tuple; 957 using std::tuple_element; 958 using std::tuple_size; 959 960 namespace internal { 961 962 // A secret type that Google Test users don't know about. It has no 963 // accessible constructors on purpose. Therefore it's impossible to create a 964 // Secret object, which is what we want. 965 class Secret { 966 Secret(const Secret&) = delete; 967 }; 968 969 // A helper for suppressing warnings on constant condition. It just 970 // returns 'condition'. 971 GTEST_API_ bool IsTrue(bool condition); 972 973 // Defines RE. 974 975 #ifdef GTEST_USES_RE2 976 977 // This is almost `using RE = ::RE2`, except it is copy-constructible, and it 978 // needs to disambiguate the `std::string`, `absl::string_view`, and `const 979 // char*` constructors. 980 class GTEST_API_ RE { 981 public: 982 RE(absl::string_view regex) : regex_(regex) {} // NOLINT 983 RE(const char* regex) : RE(absl::string_view(regex)) {} // NOLINT 984 RE(const std::string& regex) : RE(absl::string_view(regex)) {} // NOLINT 985 RE(const RE& other) : RE(other.pattern()) {} 986 987 const std::string& pattern() const { return regex_.pattern(); } 988 989 static bool FullMatch(absl::string_view str, const RE& re) { 990 return RE2::FullMatch(str, re.regex_); 991 } 992 static bool PartialMatch(absl::string_view str, const RE& re) { 993 return RE2::PartialMatch(str, re.regex_); 994 } 995 996 private: 997 RE2 regex_; 998 }; 999 1000 #elif defined(GTEST_USES_POSIX_RE) || defined(GTEST_USES_SIMPLE_RE) 1001 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ 1002 /* class A needs to have dll-interface to be used by clients of class B */) 1003 1004 // A simple C++ wrapper for <regex.h>. It uses the POSIX Extended 1005 // Regular Expression syntax. 1006 class GTEST_API_ RE { 1007 public: 1008 // A copy constructor is required by the Standard to initialize object 1009 // references from r-values. 1010 RE(const RE& other) { Init(other.pattern()); } 1011 1012 // Constructs an RE from a string. 1013 RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT 1014 1015 RE(const char* regex) { Init(regex); } // NOLINT 1016 ~RE(); 1017 1018 // Returns the string representation of the regex. 1019 const char* pattern() const { return pattern_.c_str(); } 1020 1021 // FullMatch(str, re) returns true if and only if regular expression re 1022 // matches the entire str. 1023 // PartialMatch(str, re) returns true if and only if regular expression re 1024 // matches a substring of str (including str itself). 1025 static bool FullMatch(const ::std::string& str, const RE& re) { 1026 return FullMatch(str.c_str(), re); 1027 } 1028 static bool PartialMatch(const ::std::string& str, const RE& re) { 1029 return PartialMatch(str.c_str(), re); 1030 } 1031 1032 static bool FullMatch(const char* str, const RE& re); 1033 static bool PartialMatch(const char* str, const RE& re); 1034 1035 private: 1036 void Init(const char* regex); 1037 std::string pattern_; 1038 bool is_valid_; 1039 1040 #ifdef GTEST_USES_POSIX_RE 1041 1042 regex_t full_regex_; // For FullMatch(). 1043 regex_t partial_regex_; // For PartialMatch(). 1044 1045 #else // GTEST_USES_SIMPLE_RE 1046 1047 std::string full_pattern_; // For FullMatch(); 1048 1049 #endif 1050 }; 1051 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 1052 #endif // ::testing::internal::RE implementation 1053 1054 // Formats a source file path and a line number as they would appear 1055 // in an error message from the compiler used to compile this code. 1056 GTEST_API_ ::std::string FormatFileLocation(const char* file, int line); 1057 1058 // Formats a file location for compiler-independent XML output. 1059 // Although this function is not platform dependent, we put it next to 1060 // FormatFileLocation in order to contrast the two functions. 1061 GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file, 1062 int line); 1063 1064 // Defines logging utilities: 1065 // GTEST_LOG_(severity) - logs messages at the specified severity level. The 1066 // message itself is streamed into the macro. 1067 // LogToStderr() - directs all log messages to stderr. 1068 // FlushInfoLog() - flushes informational log messages. 1069 1070 enum GTestLogSeverity { GTEST_INFO, GTEST_WARNING, GTEST_ERROR, GTEST_FATAL }; 1071 1072 // Formats log entry severity, provides a stream object for streaming the 1073 // log message, and terminates the message with a newline when going out of 1074 // scope. 1075 class GTEST_API_ GTestLog { 1076 public: 1077 GTestLog(GTestLogSeverity severity, const char* file, int line); 1078 1079 // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program. 1080 ~GTestLog(); 1081 1082 ::std::ostream& GetStream() { return ::std::cerr; } 1083 1084 private: 1085 const GTestLogSeverity severity_; 1086 1087 GTestLog(const GTestLog&) = delete; 1088 GTestLog& operator=(const GTestLog&) = delete; 1089 }; 1090 1091 #if !defined(GTEST_LOG_) 1092 1093 #define GTEST_LOG_(severity) \ 1094 ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \ 1095 __FILE__, __LINE__) \ 1096 .GetStream() 1097 1098 inline void LogToStderr() {} 1099 inline void FlushInfoLog() { fflush(nullptr); } 1100 1101 #endif // !defined(GTEST_LOG_) 1102 1103 #if !defined(GTEST_CHECK_) 1104 // INTERNAL IMPLEMENTATION - DO NOT USE. 1105 // 1106 // GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition 1107 // is not satisfied. 1108 // Synopsis: 1109 // GTEST_CHECK_(boolean_condition); 1110 // or 1111 // GTEST_CHECK_(boolean_condition) << "Additional message"; 1112 // 1113 // This checks the condition and if the condition is not satisfied 1114 // it prints message about the condition violation, including the 1115 // condition itself, plus additional message streamed into it, if any, 1116 // and then it aborts the program. It aborts the program irrespective of 1117 // whether it is built in the debug mode or not. 1118 #define GTEST_CHECK_(condition) \ 1119 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 1120 if (::testing::internal::IsTrue(condition)) \ 1121 ; \ 1122 else \ 1123 GTEST_LOG_(FATAL) << "Condition " #condition " failed. " 1124 #endif // !defined(GTEST_CHECK_) 1125 1126 // An all-mode assert to verify that the given POSIX-style function 1127 // call returns 0 (indicating success). Known limitation: this 1128 // doesn't expand to a balanced 'if' statement, so enclose the macro 1129 // in {} if you need to use it as the only statement in an 'if' 1130 // branch. 1131 #define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \ 1132 if (const int gtest_error = (posix_call)) \ 1133 GTEST_LOG_(FATAL) << #posix_call << "failed with error " << gtest_error 1134 1135 // Transforms "T" into "const T&" according to standard reference collapsing 1136 // rules (this is only needed as a backport for C++98 compilers that do not 1137 // support reference collapsing). Specifically, it transforms: 1138 // 1139 // char ==> const char& 1140 // const char ==> const char& 1141 // char& ==> char& 1142 // const char& ==> const char& 1143 // 1144 // Note that the non-const reference will not have "const" added. This is 1145 // standard, and necessary so that "T" can always bind to "const T&". 1146 template <typename T> 1147 struct ConstRef { 1148 typedef const T& type; 1149 }; 1150 template <typename T> 1151 struct ConstRef<T&> { 1152 typedef T& type; 1153 }; 1154 1155 // The argument T must depend on some template parameters. 1156 #define GTEST_REFERENCE_TO_CONST_(T) \ 1157 typename ::testing::internal::ConstRef<T>::type 1158 1159 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. 1160 // 1161 // Use ImplicitCast_ as a safe version of static_cast for upcasting in 1162 // the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a 1163 // const Foo*). When you use ImplicitCast_, the compiler checks that 1164 // the cast is safe. Such explicit ImplicitCast_s are necessary in 1165 // surprisingly many situations where C++ demands an exact type match 1166 // instead of an argument type convertible to a target type. 1167 // 1168 // The syntax for using ImplicitCast_ is the same as for static_cast: 1169 // 1170 // ImplicitCast_<ToType>(expr) 1171 // 1172 // ImplicitCast_ would have been part of the C++ standard library, 1173 // but the proposal was submitted too late. It will probably make 1174 // its way into the language in the future. 1175 // 1176 // This relatively ugly name is intentional. It prevents clashes with 1177 // similar functions users may have (e.g., implicit_cast). The internal 1178 // namespace alone is not enough because the function can be found by ADL. 1179 template <typename To> 1180 inline To ImplicitCast_(To x) { 1181 return x; 1182 } 1183 1184 // Downcasts the pointer of type Base to Derived. 1185 // Derived must be a subclass of Base. The parameter MUST 1186 // point to a class of type Derived, not any subclass of it. 1187 // When RTTI is available, the function performs a runtime 1188 // check to enforce this. 1189 template <class Derived, class Base> 1190 Derived* CheckedDowncastToActualType(Base* base) { 1191 static_assert(std::is_base_of<Base, Derived>::value, 1192 "target type not derived from source type"); 1193 #if GTEST_HAS_RTTI 1194 GTEST_CHECK_(base == nullptr || dynamic_cast<Derived*>(base) != nullptr); 1195 #endif 1196 return static_cast<Derived*>(base); 1197 } 1198 1199 #if GTEST_HAS_STREAM_REDIRECTION 1200 1201 // Defines the stderr capturer: 1202 // CaptureStdout - starts capturing stdout. 1203 // GetCapturedStdout - stops capturing stdout and returns the captured string. 1204 // CaptureStderr - starts capturing stderr. 1205 // GetCapturedStderr - stops capturing stderr and returns the captured string. 1206 // 1207 GTEST_API_ void CaptureStdout(); 1208 GTEST_API_ std::string GetCapturedStdout(); 1209 GTEST_API_ void CaptureStderr(); 1210 GTEST_API_ std::string GetCapturedStderr(); 1211 1212 #endif // GTEST_HAS_STREAM_REDIRECTION 1213 // Returns the size (in bytes) of a file. 1214 GTEST_API_ size_t GetFileSize(FILE* file); 1215 1216 // Reads the entire content of a file as a string. 1217 GTEST_API_ std::string ReadEntireFile(FILE* file); 1218 1219 // All command line arguments. 1220 GTEST_API_ std::vector<std::string> GetArgvs(); 1221 1222 #ifdef GTEST_HAS_DEATH_TEST 1223 1224 std::vector<std::string> GetInjectableArgvs(); 1225 // Deprecated: pass the args vector by value instead. 1226 void SetInjectableArgvs(const std::vector<std::string>* new_argvs); 1227 void SetInjectableArgvs(const std::vector<std::string>& new_argvs); 1228 void ClearInjectableArgvs(); 1229 1230 #endif // GTEST_HAS_DEATH_TEST 1231 1232 // Defines synchronization primitives. 1233 #ifdef GTEST_IS_THREADSAFE 1234 1235 #ifdef GTEST_OS_WINDOWS 1236 // Provides leak-safe Windows kernel handle ownership. 1237 // Used in death tests and in threading support. 1238 class GTEST_API_ AutoHandle { 1239 public: 1240 // Assume that Win32 HANDLE type is equivalent to void*. Doing so allows us to 1241 // avoid including <windows.h> in this header file. Including <windows.h> is 1242 // undesirable because it defines a lot of symbols and macros that tend to 1243 // conflict with client code. This assumption is verified by 1244 // WindowsTypesTest.HANDLEIsVoidStar. 1245 typedef void* Handle; 1246 AutoHandle(); 1247 explicit AutoHandle(Handle handle); 1248 1249 ~AutoHandle(); 1250 1251 Handle Get() const; 1252 void Reset(); 1253 void Reset(Handle handle); 1254 1255 private: 1256 // Returns true if and only if the handle is a valid handle object that can be 1257 // closed. 1258 bool IsCloseable() const; 1259 1260 Handle handle_; 1261 1262 AutoHandle(const AutoHandle&) = delete; 1263 AutoHandle& operator=(const AutoHandle&) = delete; 1264 }; 1265 #endif 1266 1267 #if GTEST_HAS_NOTIFICATION_ 1268 // Notification has already been imported into the namespace. 1269 // Nothing to do here. 1270 1271 #else 1272 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ 1273 /* class A needs to have dll-interface to be used by clients of class B */) 1274 1275 // Allows a controller thread to pause execution of newly created 1276 // threads until notified. Instances of this class must be created 1277 // and destroyed in the controller thread. 1278 // 1279 // This class is only for testing Google Test's own constructs. Do not 1280 // use it in user tests, either directly or indirectly. 1281 // TODO(b/203539622): Replace unconditionally with absl::Notification. 1282 class GTEST_API_ Notification { 1283 public: 1284 Notification() : notified_(false) {} 1285 Notification(const Notification&) = delete; 1286 Notification& operator=(const Notification&) = delete; 1287 1288 // Notifies all threads created with this notification to start. Must 1289 // be called from the controller thread. 1290 void Notify() { 1291 std::lock_guard<std::mutex> lock(mu_); 1292 notified_ = true; 1293 cv_.notify_all(); 1294 } 1295 1296 // Blocks until the controller thread notifies. Must be called from a test 1297 // thread. 1298 void WaitForNotification() { 1299 std::unique_lock<std::mutex> lock(mu_); 1300 cv_.wait(lock, [this]() { return notified_; }); 1301 } 1302 1303 private: 1304 std::mutex mu_; 1305 std::condition_variable cv_; 1306 bool notified_; 1307 }; 1308 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 1309 #endif // GTEST_HAS_NOTIFICATION_ 1310 1311 // On MinGW, we can have both GTEST_OS_WINDOWS and GTEST_HAS_PTHREAD 1312 // defined, but we don't want to use MinGW's pthreads implementation, which 1313 // has conformance problems with some versions of the POSIX standard. 1314 #if GTEST_HAS_PTHREAD && !defined(GTEST_OS_WINDOWS_MINGW) 1315 1316 // As a C-function, ThreadFuncWithCLinkage cannot be templated itself. 1317 // Consequently, it cannot select a correct instantiation of ThreadWithParam 1318 // in order to call its Run(). Introducing ThreadWithParamBase as a 1319 // non-templated base class for ThreadWithParam allows us to bypass this 1320 // problem. 1321 class ThreadWithParamBase { 1322 public: 1323 virtual ~ThreadWithParamBase() = default; 1324 virtual void Run() = 0; 1325 }; 1326 1327 // pthread_create() accepts a pointer to a function type with the C linkage. 1328 // According to the Standard (7.5/1), function types with different linkages 1329 // are different even if they are otherwise identical. Some compilers (for 1330 // example, SunStudio) treat them as different types. Since class methods 1331 // cannot be defined with C-linkage we need to define a free C-function to 1332 // pass into pthread_create(). 1333 extern "C" inline void* ThreadFuncWithCLinkage(void* thread) { 1334 static_cast<ThreadWithParamBase*>(thread)->Run(); 1335 return nullptr; 1336 } 1337 1338 // Helper class for testing Google Test's multi-threading constructs. 1339 // To use it, write: 1340 // 1341 // void ThreadFunc(int param) { /* Do things with param */ } 1342 // Notification thread_can_start; 1343 // ... 1344 // // The thread_can_start parameter is optional; you can supply NULL. 1345 // ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start); 1346 // thread_can_start.Notify(); 1347 // 1348 // These classes are only for testing Google Test's own constructs. Do 1349 // not use them in user tests, either directly or indirectly. 1350 template <typename T> 1351 class ThreadWithParam : public ThreadWithParamBase { 1352 public: 1353 typedef void UserThreadFunc(T); 1354 1355 ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start) 1356 : func_(func), 1357 param_(param), 1358 thread_can_start_(thread_can_start), 1359 finished_(false) { 1360 ThreadWithParamBase* const base = this; 1361 // The thread can be created only after all fields except thread_ 1362 // have been initialized. 1363 GTEST_CHECK_POSIX_SUCCESS_( 1364 pthread_create(&thread_, nullptr, &ThreadFuncWithCLinkage, base)); 1365 } 1366 ~ThreadWithParam() override { Join(); } 1367 1368 void Join() { 1369 if (!finished_) { 1370 GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, nullptr)); 1371 finished_ = true; 1372 } 1373 } 1374 1375 void Run() override { 1376 if (thread_can_start_ != nullptr) thread_can_start_->WaitForNotification(); 1377 func_(param_); 1378 } 1379 1380 private: 1381 UserThreadFunc* const func_; // User-supplied thread function. 1382 const T param_; // User-supplied parameter to the thread function. 1383 // When non-NULL, used to block execution until the controller thread 1384 // notifies. 1385 Notification* const thread_can_start_; 1386 bool finished_; // true if and only if we know that the thread function has 1387 // finished. 1388 pthread_t thread_; // The native thread object. 1389 1390 ThreadWithParam(const ThreadWithParam&) = delete; 1391 ThreadWithParam& operator=(const ThreadWithParam&) = delete; 1392 }; 1393 #endif // !GTEST_OS_WINDOWS && GTEST_HAS_PTHREAD || 1394 // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ 1395 1396 #if GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ 1397 // Mutex and ThreadLocal have already been imported into the namespace. 1398 // Nothing to do here. 1399 1400 #elif defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_PHONE) && \ 1401 !defined(GTEST_OS_WINDOWS_RT) 1402 1403 // Mutex implements mutex on Windows platforms. It is used in conjunction 1404 // with class MutexLock: 1405 // 1406 // Mutex mutex; 1407 // ... 1408 // MutexLock lock(&mutex); // Acquires the mutex and releases it at the 1409 // // end of the current scope. 1410 // 1411 // A static Mutex *must* be defined or declared using one of the following 1412 // macros: 1413 // GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex); 1414 // GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex); 1415 // 1416 // (A non-static Mutex is defined/declared in the usual way). 1417 class GTEST_API_ Mutex { 1418 public: 1419 enum MutexType { kStatic = 0, kDynamic = 1 }; 1420 // We rely on kStaticMutex being 0 as it is to what the linker initializes 1421 // type_ in static mutexes. critical_section_ will be initialized lazily 1422 // in ThreadSafeLazyInit(). 1423 enum StaticConstructorSelector { kStaticMutex = 0 }; 1424 1425 // This constructor intentionally does nothing. It relies on type_ being 1426 // statically initialized to 0 (effectively setting it to kStatic) and on 1427 // ThreadSafeLazyInit() to lazily initialize the rest of the members. 1428 explicit Mutex(StaticConstructorSelector /*dummy*/) {} 1429 1430 Mutex(); 1431 ~Mutex(); 1432 1433 void Lock(); 1434 1435 void Unlock(); 1436 1437 // Does nothing if the current thread holds the mutex. Otherwise, crashes 1438 // with high probability. 1439 void AssertHeld(); 1440 1441 private: 1442 // Initializes owner_thread_id_ and critical_section_ in static mutexes. 1443 void ThreadSafeLazyInit(); 1444 1445 // Per https://blogs.msdn.microsoft.com/oldnewthing/20040223-00/?p=40503, 1446 // we assume that 0 is an invalid value for thread IDs. 1447 unsigned int owner_thread_id_; 1448 1449 // For static mutexes, we rely on these members being initialized to zeros 1450 // by the linker. 1451 MutexType type_; 1452 long critical_section_init_phase_; // NOLINT 1453 GTEST_CRITICAL_SECTION* critical_section_; 1454 1455 Mutex(const Mutex&) = delete; 1456 Mutex& operator=(const Mutex&) = delete; 1457 }; 1458 1459 #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ 1460 extern ::testing::internal::Mutex mutex 1461 1462 #define GTEST_DEFINE_STATIC_MUTEX_(mutex) \ 1463 ::testing::internal::Mutex mutex(::testing::internal::Mutex::kStaticMutex) 1464 1465 // We cannot name this class MutexLock because the ctor declaration would 1466 // conflict with a macro named MutexLock, which is defined on some 1467 // platforms. That macro is used as a defensive measure to prevent against 1468 // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than 1469 // "MutexLock l(&mu)". Hence the typedef trick below. 1470 class GTestMutexLock { 1471 public: 1472 explicit GTestMutexLock(Mutex* mutex) : mutex_(mutex) { mutex_->Lock(); } 1473 1474 ~GTestMutexLock() { mutex_->Unlock(); } 1475 1476 private: 1477 Mutex* const mutex_; 1478 1479 GTestMutexLock(const GTestMutexLock&) = delete; 1480 GTestMutexLock& operator=(const GTestMutexLock&) = delete; 1481 }; 1482 1483 typedef GTestMutexLock MutexLock; 1484 1485 // Base class for ValueHolder<T>. Allows a caller to hold and delete a value 1486 // without knowing its type. 1487 class ThreadLocalValueHolderBase { 1488 public: 1489 virtual ~ThreadLocalValueHolderBase() {} 1490 }; 1491 1492 // Provides a way for a thread to send notifications to a ThreadLocal 1493 // regardless of its parameter type. 1494 class ThreadLocalBase { 1495 public: 1496 // Creates a new ValueHolder<T> object holding a default value passed to 1497 // this ThreadLocal<T>'s constructor and returns it. It is the caller's 1498 // responsibility not to call this when the ThreadLocal<T> instance already 1499 // has a value on the current thread. 1500 virtual ThreadLocalValueHolderBase* NewValueForCurrentThread() const = 0; 1501 1502 protected: 1503 ThreadLocalBase() {} 1504 virtual ~ThreadLocalBase() {} 1505 1506 private: 1507 ThreadLocalBase(const ThreadLocalBase&) = delete; 1508 ThreadLocalBase& operator=(const ThreadLocalBase&) = delete; 1509 }; 1510 1511 // Maps a thread to a set of ThreadLocals that have values instantiated on that 1512 // thread and notifies them when the thread exits. A ThreadLocal instance is 1513 // expected to persist until all threads it has values on have terminated. 1514 class GTEST_API_ ThreadLocalRegistry { 1515 public: 1516 // Registers thread_local_instance as having value on the current thread. 1517 // Returns a value that can be used to identify the thread from other threads. 1518 static ThreadLocalValueHolderBase* GetValueOnCurrentThread( 1519 const ThreadLocalBase* thread_local_instance); 1520 1521 // Invoked when a ThreadLocal instance is destroyed. 1522 static void OnThreadLocalDestroyed( 1523 const ThreadLocalBase* thread_local_instance); 1524 }; 1525 1526 class GTEST_API_ ThreadWithParamBase { 1527 public: 1528 void Join(); 1529 1530 protected: 1531 class Runnable { 1532 public: 1533 virtual ~Runnable() {} 1534 virtual void Run() = 0; 1535 }; 1536 1537 ThreadWithParamBase(Runnable* runnable, Notification* thread_can_start); 1538 virtual ~ThreadWithParamBase(); 1539 1540 private: 1541 AutoHandle thread_; 1542 }; 1543 1544 // Helper class for testing Google Test's multi-threading constructs. 1545 template <typename T> 1546 class ThreadWithParam : public ThreadWithParamBase { 1547 public: 1548 typedef void UserThreadFunc(T); 1549 1550 ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start) 1551 : ThreadWithParamBase(new RunnableImpl(func, param), thread_can_start) {} 1552 virtual ~ThreadWithParam() {} 1553 1554 private: 1555 class RunnableImpl : public Runnable { 1556 public: 1557 RunnableImpl(UserThreadFunc* func, T param) : func_(func), param_(param) {} 1558 virtual ~RunnableImpl() {} 1559 virtual void Run() { func_(param_); } 1560 1561 private: 1562 UserThreadFunc* const func_; 1563 const T param_; 1564 1565 RunnableImpl(const RunnableImpl&) = delete; 1566 RunnableImpl& operator=(const RunnableImpl&) = delete; 1567 }; 1568 1569 ThreadWithParam(const ThreadWithParam&) = delete; 1570 ThreadWithParam& operator=(const ThreadWithParam&) = delete; 1571 }; 1572 1573 // Implements thread-local storage on Windows systems. 1574 // 1575 // // Thread 1 1576 // ThreadLocal<int> tl(100); // 100 is the default value for each thread. 1577 // 1578 // // Thread 2 1579 // tl.set(150); // Changes the value for thread 2 only. 1580 // EXPECT_EQ(150, tl.get()); 1581 // 1582 // // Thread 1 1583 // EXPECT_EQ(100, tl.get()); // In thread 1, tl has the original value. 1584 // tl.set(200); 1585 // EXPECT_EQ(200, tl.get()); 1586 // 1587 // The template type argument T must have a public copy constructor. 1588 // In addition, the default ThreadLocal constructor requires T to have 1589 // a public default constructor. 1590 // 1591 // The users of a TheadLocal instance have to make sure that all but one 1592 // threads (including the main one) using that instance have exited before 1593 // destroying it. Otherwise, the per-thread objects managed for them by the 1594 // ThreadLocal instance are not guaranteed to be destroyed on all platforms. 1595 // 1596 // Google Test only uses global ThreadLocal objects. That means they 1597 // will die after main() has returned. Therefore, no per-thread 1598 // object managed by Google Test will be leaked as long as all threads 1599 // using Google Test have exited when main() returns. 1600 template <typename T> 1601 class ThreadLocal : public ThreadLocalBase { 1602 public: 1603 ThreadLocal() : default_factory_(new DefaultValueHolderFactory()) {} 1604 explicit ThreadLocal(const T& value) 1605 : default_factory_(new InstanceValueHolderFactory(value)) {} 1606 1607 ~ThreadLocal() override { ThreadLocalRegistry::OnThreadLocalDestroyed(this); } 1608 1609 T* pointer() { return GetOrCreateValue(); } 1610 const T* pointer() const { return GetOrCreateValue(); } 1611 const T& get() const { return *pointer(); } 1612 void set(const T& value) { *pointer() = value; } 1613 1614 private: 1615 // Holds a value of T. Can be deleted via its base class without the caller 1616 // knowing the type of T. 1617 class ValueHolder : public ThreadLocalValueHolderBase { 1618 public: 1619 ValueHolder() : value_() {} 1620 explicit ValueHolder(const T& value) : value_(value) {} 1621 1622 T* pointer() { return &value_; } 1623 1624 private: 1625 T value_; 1626 ValueHolder(const ValueHolder&) = delete; 1627 ValueHolder& operator=(const ValueHolder&) = delete; 1628 }; 1629 1630 T* GetOrCreateValue() const { 1631 return static_cast<ValueHolder*>( 1632 ThreadLocalRegistry::GetValueOnCurrentThread(this)) 1633 ->pointer(); 1634 } 1635 1636 ThreadLocalValueHolderBase* NewValueForCurrentThread() const override { 1637 return default_factory_->MakeNewHolder(); 1638 } 1639 1640 class ValueHolderFactory { 1641 public: 1642 ValueHolderFactory() {} 1643 virtual ~ValueHolderFactory() {} 1644 virtual ValueHolder* MakeNewHolder() const = 0; 1645 1646 private: 1647 ValueHolderFactory(const ValueHolderFactory&) = delete; 1648 ValueHolderFactory& operator=(const ValueHolderFactory&) = delete; 1649 }; 1650 1651 class DefaultValueHolderFactory : public ValueHolderFactory { 1652 public: 1653 DefaultValueHolderFactory() {} 1654 ValueHolder* MakeNewHolder() const override { return new ValueHolder(); } 1655 1656 private: 1657 DefaultValueHolderFactory(const DefaultValueHolderFactory&) = delete; 1658 DefaultValueHolderFactory& operator=(const DefaultValueHolderFactory&) = 1659 delete; 1660 }; 1661 1662 class InstanceValueHolderFactory : public ValueHolderFactory { 1663 public: 1664 explicit InstanceValueHolderFactory(const T& value) : value_(value) {} 1665 ValueHolder* MakeNewHolder() const override { 1666 return new ValueHolder(value_); 1667 } 1668 1669 private: 1670 const T value_; // The value for each thread. 1671 1672 InstanceValueHolderFactory(const InstanceValueHolderFactory&) = delete; 1673 InstanceValueHolderFactory& operator=(const InstanceValueHolderFactory&) = 1674 delete; 1675 }; 1676 1677 std::unique_ptr<ValueHolderFactory> default_factory_; 1678 1679 ThreadLocal(const ThreadLocal&) = delete; 1680 ThreadLocal& operator=(const ThreadLocal&) = delete; 1681 }; 1682 1683 #elif GTEST_HAS_PTHREAD 1684 1685 // MutexBase and Mutex implement mutex on pthreads-based platforms. 1686 class MutexBase { 1687 public: 1688 // Acquires this mutex. 1689 void Lock() { 1690 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_)); 1691 owner_ = pthread_self(); 1692 has_owner_ = true; 1693 } 1694 1695 // Releases this mutex. 1696 void Unlock() { 1697 // Since the lock is being released the owner_ field should no longer be 1698 // considered valid. We don't protect writing to has_owner_ here, as it's 1699 // the caller's responsibility to ensure that the current thread holds the 1700 // mutex when this is called. 1701 has_owner_ = false; 1702 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_)); 1703 } 1704 1705 // Does nothing if the current thread holds the mutex. Otherwise, crashes 1706 // with high probability. 1707 void AssertHeld() const { 1708 GTEST_CHECK_(has_owner_ && pthread_equal(owner_, pthread_self())) 1709 << "The current thread is not holding the mutex @" << this; 1710 } 1711 1712 // A static mutex may be used before main() is entered. It may even 1713 // be used before the dynamic initialization stage. Therefore we 1714 // must be able to initialize a static mutex object at link time. 1715 // This means MutexBase has to be a POD and its member variables 1716 // have to be public. 1717 public: 1718 pthread_mutex_t mutex_; // The underlying pthread mutex. 1719 // has_owner_ indicates whether the owner_ field below contains a valid thread 1720 // ID and is therefore safe to inspect (e.g., to use in pthread_equal()). All 1721 // accesses to the owner_ field should be protected by a check of this field. 1722 // An alternative might be to memset() owner_ to all zeros, but there's no 1723 // guarantee that a zero'd pthread_t is necessarily invalid or even different 1724 // from pthread_self(). 1725 bool has_owner_; 1726 pthread_t owner_; // The thread holding the mutex. 1727 }; 1728 1729 // Forward-declares a static mutex. 1730 #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ 1731 extern ::testing::internal::MutexBase mutex 1732 1733 // Defines and statically (i.e. at link time) initializes a static mutex. 1734 // The initialization list here does not explicitly initialize each field, 1735 // instead relying on default initialization for the unspecified fields. In 1736 // particular, the owner_ field (a pthread_t) is not explicitly initialized. 1737 // This allows initialization to work whether pthread_t is a scalar or struct. 1738 // The flag -Wmissing-field-initializers must not be specified for this to work. 1739 #define GTEST_DEFINE_STATIC_MUTEX_(mutex) \ 1740 ::testing::internal::MutexBase mutex = {PTHREAD_MUTEX_INITIALIZER, false, 0} 1741 1742 // The Mutex class can only be used for mutexes created at runtime. It 1743 // shares its API with MutexBase otherwise. 1744 class Mutex : public MutexBase { 1745 public: 1746 Mutex() { 1747 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, nullptr)); 1748 has_owner_ = false; 1749 } 1750 ~Mutex() { GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_)); } 1751 1752 private: 1753 Mutex(const Mutex&) = delete; 1754 Mutex& operator=(const Mutex&) = delete; 1755 }; 1756 1757 // We cannot name this class MutexLock because the ctor declaration would 1758 // conflict with a macro named MutexLock, which is defined on some 1759 // platforms. That macro is used as a defensive measure to prevent against 1760 // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than 1761 // "MutexLock l(&mu)". Hence the typedef trick below. 1762 class GTestMutexLock { 1763 public: 1764 explicit GTestMutexLock(MutexBase* mutex) : mutex_(mutex) { mutex_->Lock(); } 1765 1766 ~GTestMutexLock() { mutex_->Unlock(); } 1767 1768 private: 1769 MutexBase* const mutex_; 1770 1771 GTestMutexLock(const GTestMutexLock&) = delete; 1772 GTestMutexLock& operator=(const GTestMutexLock&) = delete; 1773 }; 1774 1775 typedef GTestMutexLock MutexLock; 1776 1777 // Helpers for ThreadLocal. 1778 1779 // pthread_key_create() requires DeleteThreadLocalValue() to have 1780 // C-linkage. Therefore it cannot be templatized to access 1781 // ThreadLocal<T>. Hence the need for class 1782 // ThreadLocalValueHolderBase. 1783 class GTEST_API_ ThreadLocalValueHolderBase { 1784 public: 1785 virtual ~ThreadLocalValueHolderBase() = default; 1786 }; 1787 1788 // Called by pthread to delete thread-local data stored by 1789 // pthread_setspecific(). 1790 extern "C" inline void DeleteThreadLocalValue(void* value_holder) { 1791 delete static_cast<ThreadLocalValueHolderBase*>(value_holder); 1792 } 1793 1794 // Implements thread-local storage on pthreads-based systems. 1795 template <typename T> 1796 class GTEST_API_ ThreadLocal { 1797 public: 1798 ThreadLocal() 1799 : key_(CreateKey()), default_factory_(new DefaultValueHolderFactory()) {} 1800 explicit ThreadLocal(const T& value) 1801 : key_(CreateKey()), 1802 default_factory_(new InstanceValueHolderFactory(value)) {} 1803 1804 ~ThreadLocal() { 1805 // Destroys the managed object for the current thread, if any. 1806 DeleteThreadLocalValue(pthread_getspecific(key_)); 1807 1808 // Releases resources associated with the key. This will *not* 1809 // delete managed objects for other threads. 1810 GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_)); 1811 } 1812 1813 T* pointer() { return GetOrCreateValue(); } 1814 const T* pointer() const { return GetOrCreateValue(); } 1815 const T& get() const { return *pointer(); } 1816 void set(const T& value) { *pointer() = value; } 1817 1818 private: 1819 // Holds a value of type T. 1820 class ValueHolder : public ThreadLocalValueHolderBase { 1821 public: 1822 ValueHolder() : value_() {} 1823 explicit ValueHolder(const T& value) : value_(value) {} 1824 1825 T* pointer() { return &value_; } 1826 1827 private: 1828 T value_; 1829 ValueHolder(const ValueHolder&) = delete; 1830 ValueHolder& operator=(const ValueHolder&) = delete; 1831 }; 1832 1833 static pthread_key_t CreateKey() { 1834 pthread_key_t key; 1835 // When a thread exits, DeleteThreadLocalValue() will be called on 1836 // the object managed for that thread. 1837 GTEST_CHECK_POSIX_SUCCESS_( 1838 pthread_key_create(&key, &DeleteThreadLocalValue)); 1839 return key; 1840 } 1841 1842 T* GetOrCreateValue() const { 1843 ThreadLocalValueHolderBase* const holder = 1844 static_cast<ThreadLocalValueHolderBase*>(pthread_getspecific(key_)); 1845 if (holder != nullptr) { 1846 return CheckedDowncastToActualType<ValueHolder>(holder)->pointer(); 1847 } 1848 1849 ValueHolder* const new_holder = default_factory_->MakeNewHolder(); 1850 ThreadLocalValueHolderBase* const holder_base = new_holder; 1851 GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base)); 1852 return new_holder->pointer(); 1853 } 1854 1855 class ValueHolderFactory { 1856 public: 1857 ValueHolderFactory() = default; 1858 virtual ~ValueHolderFactory() = default; 1859 virtual ValueHolder* MakeNewHolder() const = 0; 1860 1861 private: 1862 ValueHolderFactory(const ValueHolderFactory&) = delete; 1863 ValueHolderFactory& operator=(const ValueHolderFactory&) = delete; 1864 }; 1865 1866 class DefaultValueHolderFactory : public ValueHolderFactory { 1867 public: 1868 DefaultValueHolderFactory() = default; 1869 ValueHolder* MakeNewHolder() const override { return new ValueHolder(); } 1870 1871 private: 1872 DefaultValueHolderFactory(const DefaultValueHolderFactory&) = delete; 1873 DefaultValueHolderFactory& operator=(const DefaultValueHolderFactory&) = 1874 delete; 1875 }; 1876 1877 class InstanceValueHolderFactory : public ValueHolderFactory { 1878 public: 1879 explicit InstanceValueHolderFactory(const T& value) : value_(value) {} 1880 ValueHolder* MakeNewHolder() const override { 1881 return new ValueHolder(value_); 1882 } 1883 1884 private: 1885 const T value_; // The value for each thread. 1886 1887 InstanceValueHolderFactory(const InstanceValueHolderFactory&) = delete; 1888 InstanceValueHolderFactory& operator=(const InstanceValueHolderFactory&) = 1889 delete; 1890 }; 1891 1892 // A key pthreads uses for looking up per-thread values. 1893 const pthread_key_t key_; 1894 std::unique_ptr<ValueHolderFactory> default_factory_; 1895 1896 ThreadLocal(const ThreadLocal&) = delete; 1897 ThreadLocal& operator=(const ThreadLocal&) = delete; 1898 }; 1899 1900 #endif // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ 1901 1902 #else // GTEST_IS_THREADSAFE 1903 1904 // A dummy implementation of synchronization primitives (mutex, lock, 1905 // and thread-local variable). Necessary for compiling Google Test where 1906 // mutex is not supported - using Google Test in multiple threads is not 1907 // supported on such platforms. 1908 1909 class Mutex { 1910 public: 1911 Mutex() {} 1912 void Lock() {} 1913 void Unlock() {} 1914 void AssertHeld() const {} 1915 }; 1916 1917 #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ 1918 extern ::testing::internal::Mutex mutex 1919 1920 #define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex 1921 1922 // We cannot name this class MutexLock because the ctor declaration would 1923 // conflict with a macro named MutexLock, which is defined on some 1924 // platforms. That macro is used as a defensive measure to prevent against 1925 // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than 1926 // "MutexLock l(&mu)". Hence the typedef trick below. 1927 class GTestMutexLock { 1928 public: 1929 explicit GTestMutexLock(Mutex*) {} // NOLINT 1930 }; 1931 1932 typedef GTestMutexLock MutexLock; 1933 1934 template <typename T> 1935 class GTEST_API_ ThreadLocal { 1936 public: 1937 ThreadLocal() : value_() {} 1938 explicit ThreadLocal(const T& value) : value_(value) {} 1939 T* pointer() { return &value_; } 1940 const T* pointer() const { return &value_; } 1941 const T& get() const { return value_; } 1942 void set(const T& value) { value_ = value; } 1943 1944 private: 1945 T value_; 1946 }; 1947 1948 #endif // GTEST_IS_THREADSAFE 1949 1950 // Returns the number of threads running in the process, or 0 to indicate that 1951 // we cannot detect it. 1952 GTEST_API_ size_t GetThreadCount(); 1953 1954 #ifdef GTEST_OS_WINDOWS 1955 #define GTEST_PATH_SEP_ "\\" 1956 #define GTEST_HAS_ALT_PATH_SEP_ 1 1957 #else 1958 #define GTEST_PATH_SEP_ "/" 1959 #define GTEST_HAS_ALT_PATH_SEP_ 0 1960 #endif // GTEST_OS_WINDOWS 1961 1962 // Utilities for char. 1963 1964 // isspace(int ch) and friends accept an unsigned char or EOF. char 1965 // may be signed, depending on the compiler (or compiler flags). 1966 // Therefore we need to cast a char to unsigned char before calling 1967 // isspace(), etc. 1968 1969 inline bool IsAlpha(char ch) { 1970 return isalpha(static_cast<unsigned char>(ch)) != 0; 1971 } 1972 inline bool IsAlNum(char ch) { 1973 return isalnum(static_cast<unsigned char>(ch)) != 0; 1974 } 1975 inline bool IsDigit(char ch) { 1976 return isdigit(static_cast<unsigned char>(ch)) != 0; 1977 } 1978 inline bool IsLower(char ch) { 1979 return islower(static_cast<unsigned char>(ch)) != 0; 1980 } 1981 inline bool IsSpace(char ch) { 1982 return isspace(static_cast<unsigned char>(ch)) != 0; 1983 } 1984 inline bool IsUpper(char ch) { 1985 return isupper(static_cast<unsigned char>(ch)) != 0; 1986 } 1987 inline bool IsXDigit(char ch) { 1988 return isxdigit(static_cast<unsigned char>(ch)) != 0; 1989 } 1990 #ifdef __cpp_lib_char8_t 1991 inline bool IsXDigit(char8_t ch) { 1992 return isxdigit(static_cast<unsigned char>(ch)) != 0; 1993 } 1994 #endif 1995 inline bool IsXDigit(char16_t ch) { 1996 const unsigned char low_byte = static_cast<unsigned char>(ch); 1997 return ch == low_byte && isxdigit(low_byte) != 0; 1998 } 1999 inline bool IsXDigit(char32_t ch) { 2000 const unsigned char low_byte = static_cast<unsigned char>(ch); 2001 return ch == low_byte && isxdigit(low_byte) != 0; 2002 } 2003 inline bool IsXDigit(wchar_t ch) { 2004 const unsigned char low_byte = static_cast<unsigned char>(ch); 2005 return ch == low_byte && isxdigit(low_byte) != 0; 2006 } 2007 2008 inline char ToLower(char ch) { 2009 return static_cast<char>(tolower(static_cast<unsigned char>(ch))); 2010 } 2011 inline char ToUpper(char ch) { 2012 return static_cast<char>(toupper(static_cast<unsigned char>(ch))); 2013 } 2014 2015 inline std::string StripTrailingSpaces(std::string str) { 2016 std::string::iterator it = str.end(); 2017 while (it != str.begin() && IsSpace(*--it)) it = str.erase(it); 2018 return str; 2019 } 2020 2021 // The testing::internal::posix namespace holds wrappers for common 2022 // POSIX functions. These wrappers hide the differences between 2023 // Windows/MSVC and POSIX systems. Since some compilers define these 2024 // standard functions as macros, the wrapper cannot have the same name 2025 // as the wrapped function. 2026 2027 namespace posix { 2028 2029 // File system porting. 2030 // Note: Not every I/O-related function is related to file systems, so don't 2031 // just disable all of them here. For example, fileno() and isatty(), etc. must 2032 // always be available in order to detect if a pipe points to a terminal. 2033 #ifdef GTEST_OS_WINDOWS 2034 2035 typedef struct _stat StatStruct; 2036 2037 #ifdef GTEST_OS_WINDOWS_MOBILE 2038 inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); } 2039 // Stat(), RmDir(), and IsDir() are not needed on Windows CE at this 2040 // time and thus not defined there. 2041 #else 2042 inline int FileNo(FILE* file) { return _fileno(file); } 2043 #if GTEST_HAS_FILE_SYSTEM 2044 inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); } 2045 inline int RmDir(const char* dir) { return _rmdir(dir); } 2046 inline bool IsDir(const StatStruct& st) { return (_S_IFDIR & st.st_mode) != 0; } 2047 #endif 2048 #endif // GTEST_OS_WINDOWS_MOBILE 2049 2050 #elif defined(GTEST_OS_ESP8266) 2051 typedef struct stat StatStruct; 2052 2053 inline int FileNo(FILE* file) { return fileno(file); } 2054 #if GTEST_HAS_FILE_SYSTEM 2055 inline int Stat(const char* path, StatStruct* buf) { 2056 // stat function not implemented on ESP8266 2057 return 0; 2058 } 2059 inline int RmDir(const char* dir) { return rmdir(dir); } 2060 inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } 2061 #endif 2062 2063 #else 2064 2065 typedef struct stat StatStruct; 2066 2067 inline int FileNo(FILE* file) { return fileno(file); } 2068 #if GTEST_HAS_FILE_SYSTEM 2069 inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); } 2070 #ifdef GTEST_OS_QURT 2071 // QuRT doesn't support any directory functions, including rmdir 2072 inline int RmDir(const char*) { return 0; } 2073 #else 2074 inline int RmDir(const char* dir) { return rmdir(dir); } 2075 #endif 2076 inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } 2077 #endif 2078 2079 #endif // GTEST_OS_WINDOWS 2080 2081 // Other functions with a different name on Windows. 2082 2083 #ifdef GTEST_OS_WINDOWS 2084 2085 #ifdef __BORLANDC__ 2086 inline int DoIsATTY(int fd) { return isatty(fd); } 2087 inline int StrCaseCmp(const char* s1, const char* s2) { 2088 return stricmp(s1, s2); 2089 } 2090 #else // !__BORLANDC__ 2091 #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_ZOS) || \ 2092 defined(GTEST_OS_IOS) || defined(GTEST_OS_WINDOWS_PHONE) || \ 2093 defined(GTEST_OS_WINDOWS_RT) || defined(ESP_PLATFORM) 2094 inline int DoIsATTY(int /* fd */) { return 0; } 2095 #else 2096 inline int DoIsATTY(int fd) { return _isatty(fd); } 2097 #endif // GTEST_OS_WINDOWS_MOBILE 2098 inline int StrCaseCmp(const char* s1, const char* s2) { 2099 return _stricmp(s1, s2); 2100 } 2101 #endif // __BORLANDC__ 2102 2103 #else 2104 2105 inline int DoIsATTY(int fd) { return isatty(fd); } 2106 inline int StrCaseCmp(const char* s1, const char* s2) { 2107 return strcasecmp(s1, s2); 2108 } 2109 2110 #endif // GTEST_OS_WINDOWS 2111 2112 inline int IsATTY(int fd) { 2113 // DoIsATTY might change errno (for example ENOTTY in case you redirect stdout 2114 // to a file on Linux), which is unexpected, so save the previous value, and 2115 // restore it after the call. 2116 int savedErrno = errno; 2117 int isAttyValue = DoIsATTY(fd); 2118 errno = savedErrno; 2119 2120 return isAttyValue; 2121 } 2122 2123 // Functions deprecated by MSVC 8.0. 2124 2125 GTEST_DISABLE_MSC_DEPRECATED_PUSH_() 2126 2127 // ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and 2128 // StrError() aren't needed on Windows CE at this time and thus not 2129 // defined there. 2130 #if GTEST_HAS_FILE_SYSTEM 2131 #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_WINDOWS_PHONE) && \ 2132 !defined(GTEST_OS_WINDOWS_RT) && !defined(GTEST_OS_WINDOWS_GAMES) && \ 2133 !defined(GTEST_OS_ESP8266) && !defined(GTEST_OS_XTENSA) && \ 2134 !defined(GTEST_OS_QURT) 2135 inline int ChDir(const char* dir) { return chdir(dir); } 2136 #endif 2137 inline FILE* FOpen(const char* path, const char* mode) { 2138 #if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MINGW) 2139 struct wchar_codecvt : public std::codecvt<wchar_t, char, std::mbstate_t> {}; 2140 std::wstring_convert<wchar_codecvt> converter; 2141 std::wstring wide_path = converter.from_bytes(path); 2142 std::wstring wide_mode = converter.from_bytes(mode); 2143 return _wfopen(wide_path.c_str(), wide_mode.c_str()); 2144 #else // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW 2145 return fopen(path, mode); 2146 #endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW 2147 } 2148 #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT) 2149 inline FILE* FReopen(const char* path, const char* mode, FILE* stream) { 2150 return freopen(path, mode, stream); 2151 } 2152 inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); } 2153 #endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT 2154 inline int FClose(FILE* fp) { return fclose(fp); } 2155 #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT) 2156 inline int Read(int fd, void* buf, unsigned int count) { 2157 return static_cast<int>(read(fd, buf, count)); 2158 } 2159 inline int Write(int fd, const void* buf, unsigned int count) { 2160 return static_cast<int>(write(fd, buf, count)); 2161 } 2162 inline int Close(int fd) { return close(fd); } 2163 #endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT 2164 #endif // GTEST_HAS_FILE_SYSTEM 2165 2166 #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT) 2167 inline const char* StrError(int errnum) { return strerror(errnum); } 2168 #endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT 2169 2170 inline const char* GetEnv(const char* name) { 2171 #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_WINDOWS_PHONE) || \ 2172 defined(GTEST_OS_ESP8266) || defined(GTEST_OS_XTENSA) || \ 2173 defined(GTEST_OS_QURT) 2174 // We are on an embedded platform, which has no environment variables. 2175 static_cast<void>(name); // To prevent 'unused argument' warning. 2176 return nullptr; 2177 #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9) 2178 // Environment variables which we programmatically clear will be set to the 2179 // empty string rather than unset (NULL). Handle that case. 2180 const char* const env = getenv(name); 2181 return (env != nullptr && env[0] != '\0') ? env : nullptr; 2182 #else 2183 return getenv(name); 2184 #endif 2185 } 2186 2187 GTEST_DISABLE_MSC_DEPRECATED_POP_() 2188 2189 #ifdef GTEST_OS_WINDOWS_MOBILE 2190 // Windows CE has no C library. The abort() function is used in 2191 // several places in Google Test. This implementation provides a reasonable 2192 // imitation of standard behaviour. 2193 [[noreturn]] void Abort(); 2194 #else 2195 [[noreturn]] inline void Abort() { abort(); } 2196 #endif // GTEST_OS_WINDOWS_MOBILE 2197 2198 } // namespace posix 2199 2200 // MSVC "deprecates" snprintf and issues warnings wherever it is used. In 2201 // order to avoid these warnings, we need to use _snprintf or _snprintf_s on 2202 // MSVC-based platforms. We map the GTEST_SNPRINTF_ macro to the appropriate 2203 // function in order to achieve that. We use macro definition here because 2204 // snprintf is a variadic function. 2205 #if defined(_MSC_VER) && !defined(GTEST_OS_WINDOWS_MOBILE) 2206 // MSVC 2005 and above support variadic macros. 2207 #define GTEST_SNPRINTF_(buffer, size, format, ...) \ 2208 _snprintf_s(buffer, size, size, format, __VA_ARGS__) 2209 #elif defined(_MSC_VER) 2210 // Windows CE does not define _snprintf_s 2211 #define GTEST_SNPRINTF_ _snprintf 2212 #else 2213 #define GTEST_SNPRINTF_ snprintf 2214 #endif 2215 2216 // The biggest signed integer type the compiler supports. 2217 // 2218 // long long is guaranteed to be at least 64-bits in C++11. 2219 using BiggestInt = long long; // NOLINT 2220 2221 // The maximum number a BiggestInt can represent. 2222 constexpr BiggestInt kMaxBiggestInt = (std::numeric_limits<BiggestInt>::max)(); 2223 2224 // This template class serves as a compile-time function from size to 2225 // type. It maps a size in bytes to a primitive type with that 2226 // size. e.g. 2227 // 2228 // TypeWithSize<4>::UInt 2229 // 2230 // is typedef-ed to be unsigned int (unsigned integer made up of 4 2231 // bytes). 2232 // 2233 // Such functionality should belong to STL, but I cannot find it 2234 // there. 2235 // 2236 // Google Test uses this class in the implementation of floating-point 2237 // comparison. 2238 // 2239 // For now it only handles UInt (unsigned int) as that's all Google Test 2240 // needs. Other types can be easily added in the future if need 2241 // arises. 2242 template <size_t size> 2243 class TypeWithSize { 2244 public: 2245 // This prevents the user from using TypeWithSize<N> with incorrect 2246 // values of N. 2247 using UInt = void; 2248 }; 2249 2250 // The specialization for size 4. 2251 template <> 2252 class TypeWithSize<4> { 2253 public: 2254 using Int = std::int32_t; 2255 using UInt = std::uint32_t; 2256 }; 2257 2258 // The specialization for size 8. 2259 template <> 2260 class TypeWithSize<8> { 2261 public: 2262 using Int = std::int64_t; 2263 using UInt = std::uint64_t; 2264 }; 2265 2266 // Integer types of known sizes. 2267 using TimeInMillis = int64_t; // Represents time in milliseconds. 2268 2269 // Utilities for command line flags and environment variables. 2270 2271 // Macro for referencing flags. 2272 #if !defined(GTEST_FLAG) 2273 #define GTEST_FLAG_NAME_(name) gtest_##name 2274 #define GTEST_FLAG(name) FLAGS_gtest_##name 2275 #endif // !defined(GTEST_FLAG) 2276 2277 // Pick a command line flags implementation. 2278 #ifdef GTEST_INTERNAL_HAS_ABSL_FLAGS 2279 2280 // Macros for defining flags. 2281 #define GTEST_DEFINE_bool_(name, default_val, doc) \ 2282 ABSL_FLAG(bool, GTEST_FLAG_NAME_(name), default_val, doc) 2283 #define GTEST_DEFINE_int32_(name, default_val, doc) \ 2284 ABSL_FLAG(int32_t, GTEST_FLAG_NAME_(name), default_val, doc) 2285 #define GTEST_DEFINE_string_(name, default_val, doc) \ 2286 ABSL_FLAG(std::string, GTEST_FLAG_NAME_(name), default_val, doc) 2287 2288 // Macros for declaring flags. 2289 #define GTEST_DECLARE_bool_(name) \ 2290 ABSL_DECLARE_FLAG(bool, GTEST_FLAG_NAME_(name)) 2291 #define GTEST_DECLARE_int32_(name) \ 2292 ABSL_DECLARE_FLAG(int32_t, GTEST_FLAG_NAME_(name)) 2293 #define GTEST_DECLARE_string_(name) \ 2294 ABSL_DECLARE_FLAG(std::string, GTEST_FLAG_NAME_(name)) 2295 2296 #define GTEST_FLAG_SAVER_ ::absl::FlagSaver 2297 2298 #define GTEST_FLAG_GET(name) ::absl::GetFlag(GTEST_FLAG(name)) 2299 #define GTEST_FLAG_SET(name, value) \ 2300 (void)(::absl::SetFlag(>EST_FLAG(name), value)) 2301 #define GTEST_USE_OWN_FLAGFILE_FLAG_ 0 2302 2303 #undef GTEST_INTERNAL_HAS_ABSL_FLAGS 2304 #else // ndef GTEST_INTERNAL_HAS_ABSL_FLAGS 2305 2306 // Macros for defining flags. 2307 #define GTEST_DEFINE_bool_(name, default_val, doc) \ 2308 namespace testing { \ 2309 GTEST_API_ bool GTEST_FLAG(name) = (default_val); \ 2310 } \ 2311 static_assert(true, "no-op to require trailing semicolon") 2312 #define GTEST_DEFINE_int32_(name, default_val, doc) \ 2313 namespace testing { \ 2314 GTEST_API_ std::int32_t GTEST_FLAG(name) = (default_val); \ 2315 } \ 2316 static_assert(true, "no-op to require trailing semicolon") 2317 #define GTEST_DEFINE_string_(name, default_val, doc) \ 2318 namespace testing { \ 2319 GTEST_API_ ::std::string GTEST_FLAG(name) = (default_val); \ 2320 } \ 2321 static_assert(true, "no-op to require trailing semicolon") 2322 2323 // Macros for declaring flags. 2324 #define GTEST_DECLARE_bool_(name) \ 2325 namespace testing { \ 2326 GTEST_API_ extern bool GTEST_FLAG(name); \ 2327 } \ 2328 static_assert(true, "no-op to require trailing semicolon") 2329 #define GTEST_DECLARE_int32_(name) \ 2330 namespace testing { \ 2331 GTEST_API_ extern std::int32_t GTEST_FLAG(name); \ 2332 } \ 2333 static_assert(true, "no-op to require trailing semicolon") 2334 #define GTEST_DECLARE_string_(name) \ 2335 namespace testing { \ 2336 GTEST_API_ extern ::std::string GTEST_FLAG(name); \ 2337 } \ 2338 static_assert(true, "no-op to require trailing semicolon") 2339 2340 #define GTEST_FLAG_SAVER_ ::testing::internal::GTestFlagSaver 2341 2342 #define GTEST_FLAG_GET(name) ::testing::GTEST_FLAG(name) 2343 #define GTEST_FLAG_SET(name, value) (void)(::testing::GTEST_FLAG(name) = value) 2344 #define GTEST_USE_OWN_FLAGFILE_FLAG_ 1 2345 2346 #endif // GTEST_INTERNAL_HAS_ABSL_FLAGS 2347 2348 // Thread annotations 2349 #if !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_) 2350 #define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks) 2351 #define GTEST_LOCK_EXCLUDED_(locks) 2352 #endif // !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_) 2353 2354 // Parses 'str' for a 32-bit signed integer. If successful, writes the result 2355 // to *value and returns true; otherwise leaves *value unchanged and returns 2356 // false. 2357 GTEST_API_ bool ParseInt32(const Message& src_text, const char* str, 2358 int32_t* value); 2359 2360 // Parses a bool/int32_t/string from the environment variable 2361 // corresponding to the given Google Test flag. 2362 bool BoolFromGTestEnv(const char* flag, bool default_val); 2363 GTEST_API_ int32_t Int32FromGTestEnv(const char* flag, int32_t default_val); 2364 std::string OutputFlagAlsoCheckEnvVar(); 2365 const char* StringFromGTestEnv(const char* flag, const char* default_val); 2366 2367 } // namespace internal 2368 } // namespace testing 2369 2370 #if !defined(GTEST_INTERNAL_DEPRECATED) 2371 2372 // Internal Macro to mark an API deprecated, for googletest usage only 2373 // Usage: class GTEST_INTERNAL_DEPRECATED(message) MyClass or 2374 // GTEST_INTERNAL_DEPRECATED(message) <return_type> myFunction(); Every usage of 2375 // a deprecated entity will trigger a warning when compiled with 2376 // `-Wdeprecated-declarations` option (clang, gcc, any __GNUC__ compiler). 2377 // For msvc /W3 option will need to be used 2378 // Note that for 'other' compilers this macro evaluates to nothing to prevent 2379 // compilations errors. 2380 #if defined(_MSC_VER) 2381 #define GTEST_INTERNAL_DEPRECATED(message) __declspec(deprecated(message)) 2382 #elif defined(__GNUC__) 2383 #define GTEST_INTERNAL_DEPRECATED(message) __attribute__((deprecated(message))) 2384 #else 2385 #define GTEST_INTERNAL_DEPRECATED(message) 2386 #endif 2387 2388 #endif // !defined(GTEST_INTERNAL_DEPRECATED) 2389 2390 #ifdef GTEST_HAS_ABSL 2391 // Always use absl::any for UniversalPrinter<> specializations if googletest 2392 // is built with absl support. 2393 #define GTEST_INTERNAL_HAS_ANY 1 2394 #include "absl/types/any.h" 2395 namespace testing { 2396 namespace internal { 2397 using Any = ::absl::any; 2398 } // namespace internal 2399 } // namespace testing 2400 #else 2401 #if defined(__cpp_lib_any) || (GTEST_INTERNAL_HAS_INCLUDE(<any>) && \ 2402 GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L && \ 2403 (!defined(_MSC_VER) || GTEST_HAS_RTTI)) 2404 // Otherwise for C++17 and higher use std::any for UniversalPrinter<> 2405 // specializations. 2406 #define GTEST_INTERNAL_HAS_ANY 1 2407 #include <any> 2408 namespace testing { 2409 namespace internal { 2410 using Any = ::std::any; 2411 } // namespace internal 2412 } // namespace testing 2413 // The case where absl is configured NOT to alias std::any is not 2414 // supported. 2415 #endif // __cpp_lib_any 2416 #endif // GTEST_HAS_ABSL 2417 2418 #ifndef GTEST_INTERNAL_HAS_ANY 2419 #define GTEST_INTERNAL_HAS_ANY 0 2420 #endif 2421 2422 #ifdef GTEST_HAS_ABSL 2423 // Always use absl::optional for UniversalPrinter<> specializations if 2424 // googletest is built with absl support. 2425 #define GTEST_INTERNAL_HAS_OPTIONAL 1 2426 #include "absl/types/optional.h" 2427 namespace testing { 2428 namespace internal { 2429 template <typename T> 2430 using Optional = ::absl::optional<T>; 2431 inline ::absl::nullopt_t Nullopt() { return ::absl::nullopt; } 2432 } // namespace internal 2433 } // namespace testing 2434 #else 2435 #if defined(__cpp_lib_optional) || (GTEST_INTERNAL_HAS_INCLUDE(<optional>) && \ 2436 GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L) 2437 // Otherwise for C++17 and higher use std::optional for UniversalPrinter<> 2438 // specializations. 2439 #define GTEST_INTERNAL_HAS_OPTIONAL 1 2440 #include <optional> 2441 namespace testing { 2442 namespace internal { 2443 template <typename T> 2444 using Optional = ::std::optional<T>; 2445 inline ::std::nullopt_t Nullopt() { return ::std::nullopt; } 2446 } // namespace internal 2447 } // namespace testing 2448 // The case where absl is configured NOT to alias std::optional is not 2449 // supported. 2450 #endif // __cpp_lib_optional 2451 #endif // GTEST_HAS_ABSL 2452 2453 #ifndef GTEST_INTERNAL_HAS_OPTIONAL 2454 #define GTEST_INTERNAL_HAS_OPTIONAL 0 2455 #endif 2456 2457 #if defined(__cpp_lib_span) || (GTEST_INTERNAL_HAS_INCLUDE(<span>) && \ 2458 GTEST_INTERNAL_CPLUSPLUS_LANG >= 202002L) 2459 #define GTEST_INTERNAL_HAS_STD_SPAN 1 2460 #endif // __cpp_lib_span 2461 2462 #ifndef GTEST_INTERNAL_HAS_STD_SPAN 2463 #define GTEST_INTERNAL_HAS_STD_SPAN 0 2464 #endif 2465 2466 #ifdef GTEST_HAS_ABSL 2467 // Always use absl::string_view for Matcher<> specializations if googletest 2468 // is built with absl support. 2469 #define GTEST_INTERNAL_HAS_STRING_VIEW 1 2470 #include "absl/strings/string_view.h" 2471 namespace testing { 2472 namespace internal { 2473 using StringView = ::absl::string_view; 2474 } // namespace internal 2475 } // namespace testing 2476 #else 2477 #if defined(__cpp_lib_string_view) || \ 2478 (GTEST_INTERNAL_HAS_INCLUDE(<string_view>) && \ 2479 GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L) 2480 // Otherwise for C++17 and higher use std::string_view for Matcher<> 2481 // specializations. 2482 #define GTEST_INTERNAL_HAS_STRING_VIEW 1 2483 #include <string_view> 2484 namespace testing { 2485 namespace internal { 2486 using StringView = ::std::string_view; 2487 } // namespace internal 2488 } // namespace testing 2489 // The case where absl is configured NOT to alias std::string_view is not 2490 // supported. 2491 #endif // __cpp_lib_string_view 2492 #endif // GTEST_HAS_ABSL 2493 2494 #ifndef GTEST_INTERNAL_HAS_STRING_VIEW 2495 #define GTEST_INTERNAL_HAS_STRING_VIEW 0 2496 #endif 2497 2498 #ifdef GTEST_HAS_ABSL 2499 // Always use absl::variant for UniversalPrinter<> specializations if googletest 2500 // is built with absl support. 2501 #define GTEST_INTERNAL_HAS_VARIANT 1 2502 #include "absl/types/variant.h" 2503 namespace testing { 2504 namespace internal { 2505 template <typename... T> 2506 using Variant = ::absl::variant<T...>; 2507 } // namespace internal 2508 } // namespace testing 2509 #else 2510 #if defined(__cpp_lib_variant) || (GTEST_INTERNAL_HAS_INCLUDE(<variant>) && \ 2511 GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L) 2512 // Otherwise for C++17 and higher use std::variant for UniversalPrinter<> 2513 // specializations. 2514 #define GTEST_INTERNAL_HAS_VARIANT 1 2515 #include <variant> 2516 namespace testing { 2517 namespace internal { 2518 template <typename... T> 2519 using Variant = ::std::variant<T...>; 2520 } // namespace internal 2521 } // namespace testing 2522 // The case where absl is configured NOT to alias std::variant is not supported. 2523 #endif // __cpp_lib_variant 2524 #endif // GTEST_HAS_ABSL 2525 2526 #ifndef GTEST_INTERNAL_HAS_VARIANT 2527 #define GTEST_INTERNAL_HAS_VARIANT 0 2528 #endif 2529 2530 #if (defined(__cpp_constexpr) && !defined(__cpp_inline_variables)) || \ 2531 (defined(GTEST_INTERNAL_CPLUSPLUS_LANG) && \ 2532 GTEST_INTERNAL_CPLUSPLUS_LANG < 201703L) 2533 #define GTEST_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL 1 2534 #endif 2535 2536 #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 2537