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