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