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