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