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. They are subject to change without notice. DO NOT USE 34 // THEM IN USER CODE. 35 36 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 37 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 38 39 // The user can define the following macros in the build script to 40 // control Google Test's behavior. If the user doesn't define a macro 41 // in this list, Google Test will define it. 42 // 43 // GTEST_HAS_CLONE - Define it to 1/0 to indicate that clone(2) 44 // is/isn't available. 45 // GTEST_HAS_GLOBAL_STRING - Define it to 1/0 to indicate that ::string 46 // is/isn't available (some systems define 47 // ::string, which is different to std::string). 48 // GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::string 49 // is/isn't available (some systems define 50 // ::wstring, which is different to std::wstring). 51 // GTEST_HAS_PTHREAD - Define it to 1/0 to indicate that <pthread.h> 52 // is/isn't available. 53 // GTEST_HAS_RTTI - Define it to 1/0 to indicate that RTTI is/isn't 54 // enabled. 55 // GTEST_HAS_STD_STRING - Define it to 1/0 to indicate that 56 // std::string does/doesn't work (Google Test can 57 // be used where std::string is unavailable). 58 // GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that 59 // std::wstring does/doesn't work (Google Test can 60 // be used where std::wstring is unavailable). 61 // GTEST_HAS_TR1_TUPLE 1 - Define it to 1/0 to indicate tr1::tuple 62 // is/isn't available. 63 64 // This header defines the following utilities: 65 // 66 // Macros indicating the current platform (defined to 1 if compiled on 67 // the given platform; otherwise undefined): 68 // GTEST_OS_ANDROID - Android 69 // GTEST_OS_CYGWIN - Cygwin 70 // GTEST_OS_LINUX - Linux 71 // GTEST_OS_MAC - Mac OS X 72 // GTEST_OS_SOLARIS - Sun Solaris 73 // GTEST_OS_SYMBIAN - Symbian 74 // GTEST_OS_WINDOWS - Windows 75 // GTEST_OS_ZOS - z/OS 76 // 77 // Among the platforms, Cygwin, Linux, Max OS X, and Windows have the 78 // most stable support. Since core members of the Google Test project 79 // don't have access to other platforms, support for them may be less 80 // stable. If you notice any problems on your platform, please notify 81 // googletestframework (at) googlegroups.com (patches for fixing them are 82 // even more welcome!). 83 // 84 // Note that it is possible that none of the GTEST_OS_* macros are defined. 85 // 86 // Macros indicating available Google Test features (defined to 1 if 87 // the corresponding feature is supported; otherwise undefined): 88 // GTEST_HAS_COMBINE - the Combine() function (for value-parameterized 89 // tests) 90 // GTEST_HAS_DEATH_TEST - death tests 91 // GTEST_HAS_PARAM_TEST - value-parameterized tests 92 // GTEST_HAS_TYPED_TEST - typed tests 93 // GTEST_HAS_TYPED_TEST_P - type-parameterized tests 94 // GTEST_USES_POSIX_RE - enhanced POSIX regex is used. 95 // GTEST_USES_SIMPLE_RE - our own simple regex is used; 96 // the above two are mutually exclusive. 97 // 98 // Macros for basic C++ coding: 99 // GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning. 100 // GTEST_ATTRIBUTE_UNUSED_ - declares that a class' instances don't have to 101 // be used. 102 // GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=. 103 // GTEST_MUST_USE_RESULT_ - declares that a function's result must be used. 104 // 105 // Synchronization: 106 // Mutex, MutexLock, ThreadLocal, GetThreadCount() 107 // - synchronization primitives. 108 // GTEST_IS_THREADSAFE - defined to 1 to indicate that the above 109 // synchronization primitives have real implementations 110 // and Google Test is thread-safe; or 0 otherwise. 111 // 112 // Template meta programming: 113 // is_pointer - as in TR1; needed on Symbian and IBM XL C/C++ only. 114 // 115 // Smart pointers: 116 // scoped_ptr - as in TR2. 117 // 118 // Regular expressions: 119 // RE - a simple regular expression class using the POSIX 120 // Extended Regular Expression syntax. Not available on 121 // Windows. 122 // 123 // Logging: 124 // GTEST_LOG_() - logs messages at the specified severity level. 125 // LogToStderr() - directs all log messages to stderr. 126 // FlushInfoLog() - flushes informational log messages. 127 // 128 // Stderr capturing: 129 // CaptureStderr() - starts capturing stderr. 130 // GetCapturedStderr() - stops capturing stderr and returns the captured 131 // string. 132 // 133 // Integer types: 134 // TypeWithSize - maps an integer to a int type. 135 // Int32, UInt32, Int64, UInt64, TimeInMillis 136 // - integers of known sizes. 137 // BiggestInt - the biggest signed integer type. 138 // 139 // Command-line utilities: 140 // GTEST_FLAG() - references a flag. 141 // GTEST_DECLARE_*() - declares a flag. 142 // GTEST_DEFINE_*() - defines a flag. 143 // GetArgvs() - returns the command line as a vector of strings. 144 // 145 // Environment variable utilities: 146 // GetEnv() - gets the value of an environment variable. 147 // BoolFromGTestEnv() - parses a bool environment variable. 148 // Int32FromGTestEnv() - parses an Int32 environment variable. 149 // StringFromGTestEnv() - parses a string environment variable. 150 151 #include <stdlib.h> 152 #include <stdio.h> 153 #include <iostream> // Used for GTEST_CHECK_ 154 155 #define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com" 156 #define GTEST_FLAG_PREFIX_ "gtest_" 157 #define GTEST_FLAG_PREFIX_UPPER_ "GTEST_" 158 #define GTEST_NAME_ "Google Test" 159 #define GTEST_PROJECT_URL_ "http://code.google.com/p/googletest/" 160 161 // Determines the version of gcc that is used to compile this. 162 #ifdef __GNUC__ 163 // 40302 means version 4.3.2. 164 #define GTEST_GCC_VER_ \ 165 (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__) 166 #endif // __GNUC__ 167 168 // Determines the platform on which Google Test is compiled. 169 #ifdef __CYGWIN__ 170 #define GTEST_OS_CYGWIN 1 171 #elif __SYMBIAN32__ 172 #define GTEST_OS_SYMBIAN 1 173 #elif defined _MSC_VER 174 // TODO(kenton (at) google.com): GTEST_OS_WINDOWS is currently used to mean 175 // both "The OS is Windows" and "The compiler is MSVC". These 176 // meanings really should be separated in order to better support 177 // Windows compilers other than MSVC. 178 #define GTEST_OS_WINDOWS 1 179 #elif defined __APPLE__ 180 #define GTEST_OS_MAC 1 181 #elif defined ANDROID 182 #define GTEST_OS_ANDROID 1 183 #elif defined __linux__ 184 #define GTEST_OS_LINUX 1 185 #elif defined __MVS__ 186 #define GTEST_OS_ZOS 1 187 #elif defined(__sun) && defined(__SVR4) 188 #define GTEST_OS_SOLARIS 1 189 #endif // _MSC_VER 190 191 #if GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC 192 193 // On some platforms, <regex.h> needs someone to define size_t, and 194 // won't compile otherwise. We can #include it here as we already 195 // included <stdlib.h>, which is guaranteed to define size_t through 196 // <stddef.h>. 197 #include <regex.h> // NOLINT 198 #define GTEST_USES_POSIX_RE 1 199 200 #else 201 202 // <regex.h> may not be available on this platform. Use our own 203 // simple regex implementation instead. 204 #define GTEST_USES_SIMPLE_RE 1 205 206 #endif // GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC 207 208 // Defines GTEST_HAS_EXCEPTIONS to 1 if exceptions are enabled, or 0 209 // otherwise. 210 211 #ifdef _MSC_VER // Compiled by MSVC? 212 // Assumes that exceptions are enabled by default. 213 #ifndef _HAS_EXCEPTIONS // MSVC uses this macro to enable exceptions. 214 #define _HAS_EXCEPTIONS 1 215 #endif // _HAS_EXCEPTIONS 216 #define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS 217 #else // The compiler is not MSVC. 218 // gcc defines __EXCEPTIONS to 1 iff exceptions are enabled. For 219 // other compilers, we assume exceptions are disabled to be 220 // conservative. 221 #if defined(__GNUC__) && __EXCEPTIONS 222 #define GTEST_HAS_EXCEPTIONS 1 223 #else 224 #define GTEST_HAS_EXCEPTIONS 0 225 #endif // defined(__GNUC__) && __EXCEPTIONS 226 #endif // _MSC_VER 227 228 // Determines whether ::std::string and ::string are available. 229 230 #ifndef GTEST_HAS_STD_STRING 231 // The user didn't tell us whether ::std::string is available, so we 232 // need to figure it out. The only environment that we know 233 // ::std::string is not available is MSVC 7.1 or lower with exceptions 234 // disabled. 235 #if defined(_MSC_VER) && (_MSC_VER < 1400) && !GTEST_HAS_EXCEPTIONS 236 #define GTEST_HAS_STD_STRING 0 237 #else 238 #define GTEST_HAS_STD_STRING 1 239 #endif 240 #endif // GTEST_HAS_STD_STRING 241 242 #ifndef GTEST_HAS_GLOBAL_STRING 243 // The user didn't tell us whether ::string is available, so we need 244 // to figure it out. 245 246 #define GTEST_HAS_GLOBAL_STRING 0 247 248 #endif // GTEST_HAS_GLOBAL_STRING 249 250 #ifndef GTEST_HAS_STD_WSTRING 251 // The user didn't tell us whether ::std::wstring is available, so we need 252 // to figure it out. 253 // TODO(wan (at) google.com): uses autoconf to detect whether ::std::wstring 254 // is available. 255 256 #if GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || GTEST_OS_ANDROID 257 // Cygwin 1.5 and below doesn't support ::std::wstring. 258 // Cygwin 1.7 might add wstring support; this should be updated when clear. 259 // Solaris' libc++ doesn't support it either. 260 // Android does not support wstring and never will. 261 #define GTEST_HAS_STD_WSTRING 0 262 #else 263 #define GTEST_HAS_STD_WSTRING GTEST_HAS_STD_STRING 264 #endif // GTEST_OS_CYGWIN || GTEST_OS_SOLARIS 265 266 #endif // GTEST_HAS_STD_WSTRING 267 268 #ifndef GTEST_HAS_GLOBAL_WSTRING 269 // The user didn't tell us whether ::wstring is available, so we need 270 // to figure it out. 271 #define GTEST_HAS_GLOBAL_WSTRING \ 272 (GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING) 273 #endif // GTEST_HAS_GLOBAL_WSTRING 274 275 #if GTEST_HAS_STD_STRING || GTEST_HAS_GLOBAL_STRING || \ 276 GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING 277 #include <string> // NOLINT 278 #endif // GTEST_HAS_STD_STRING || GTEST_HAS_GLOBAL_STRING || 279 // GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING 280 281 #if GTEST_HAS_STD_STRING 282 #include <sstream> // NOLINT 283 #else 284 #include <strstream> // NOLINT 285 #endif // GTEST_HAS_STD_STRING 286 287 // Determines whether RTTI is available. 288 #ifndef GTEST_HAS_RTTI 289 // The user didn't tell us whether RTTI is enabled, so we need to 290 // figure it out. 291 292 #ifdef _MSC_VER 293 294 #ifdef _CPPRTTI // MSVC defines this macro iff RTTI is enabled. 295 #define GTEST_HAS_RTTI 1 296 #else 297 #define GTEST_HAS_RTTI 0 298 #endif // _CPPRTTI 299 300 #elif defined(__GNUC__) 301 302 // Android uses GCC but does not support RTTI 303 #if GTEST_OS_ANDROID 304 #define GTEST_HAS_RTTI 0 305 #else 306 // Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled. 307 #if GTEST_GCC_VER_ >= 40302 308 #ifdef __GXX_RTTI 309 #define GTEST_HAS_RTTI 1 310 #else 311 #define GTEST_HAS_RTTI 0 312 #endif // __GXX_RTTI 313 #else 314 // For gcc versions smaller than 4.3.2, we assume RTTI is enabled. 315 #define GTEST_HAS_RTTI 1 316 #endif // GTEST_GCC_VER >= 40302 317 #endif // GTEST_OS_ANDROID 318 319 #else 320 321 // Unknown compiler - assume RTTI is enabled. 322 #define GTEST_HAS_RTTI 1 323 324 #endif // _MSC_VER 325 326 #endif // GTEST_HAS_RTTI 327 328 // Determines whether <pthread.h> is available. 329 #ifndef GTEST_HAS_PTHREAD 330 // The user didn't tell us, so we need to figure it out. 331 #define GTEST_HAS_PTHREAD (GTEST_OS_LINUX || GTEST_OS_MAC) 332 #endif // GTEST_HAS_PTHREAD 333 334 // Determines whether tr1/tuple is available. If you have tr1/tuple 335 // on your platform, define GTEST_HAS_TR1_TUPLE=1 for both the Google 336 // Test project and your tests. If you would like Google Test to detect 337 // tr1/tuple on your platform automatically, please open an issue 338 // ticket at http://code.google.com/p/googletest. 339 #ifndef GTEST_HAS_TR1_TUPLE 340 // The user didn't tell us, so we need to figure it out. 341 342 // GCC provides <tr1/tuple> since 4.0.0. 343 #if defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000) && !GTEST_OS_ANDROID 344 #define GTEST_HAS_TR1_TUPLE 1 345 #else 346 #define GTEST_HAS_TR1_TUPLE 0 347 #endif // __GNUC__ 348 #endif // GTEST_HAS_TR1_TUPLE 349 350 // To avoid conditional compilation everywhere, we make it 351 // gtest-port.h's responsibility to #include the header implementing 352 // tr1/tuple. 353 #if GTEST_HAS_TR1_TUPLE 354 #if defined(__GNUC__) 355 // GCC implements tr1/tuple in the <tr1/tuple> header. This does not 356 // conform to the TR1 spec, which requires the header to be <tuple>. 357 #include <tr1/tuple> 358 #else 359 // If the compiler is not GCC, we assume the user is using a 360 // spec-conforming TR1 implementation. 361 #include <tuple> 362 #endif // __GNUC__ 363 #endif // GTEST_HAS_TR1_TUPLE 364 365 // Determines whether clone(2) is supported. 366 // Usually it will only be available on Linux, excluding 367 // Linux on the Itanium architecture. 368 // Also see http://linux.die.net/man/2/clone. 369 #ifndef GTEST_HAS_CLONE 370 // The user didn't tell us, so we need to figure it out. 371 372 #if GTEST_OS_LINUX && !defined(__ia64__) 373 #define GTEST_HAS_CLONE 1 374 #else 375 #define GTEST_HAS_CLONE 0 376 #endif // GTEST_OS_LINUX && !defined(__ia64__) 377 378 #endif // GTEST_HAS_CLONE 379 380 // Determines whether to support death tests. 381 // Google Test does not support death tests for VC 7.1 and earlier for 382 // these reasons: 383 // 1. std::vector does not build in VC 7.1 when exceptions are disabled. 384 // 2. std::string does not build in VC 7.1 when exceptions are disabled 385 // (this is covered by GTEST_HAS_STD_STRING guard). 386 // 3. abort() in a VC 7.1 application compiled as GUI in debug config 387 // pops up a dialog window that cannot be suppressed programmatically. 388 #if GTEST_HAS_STD_STRING && (GTEST_OS_LINUX || \ 389 GTEST_OS_MAC || \ 390 GTEST_OS_CYGWIN || \ 391 (GTEST_OS_WINDOWS && _MSC_VER >= 1400)) 392 #define GTEST_HAS_DEATH_TEST 1 393 #include <vector> 394 #endif 395 396 // Determines whether to support value-parameterized tests. 397 398 #if defined(__GNUC__) || (_MSC_VER >= 1400) 399 // TODO(vladl (at) google.com): get the implementation rid of vector and list 400 // to compile on MSVC 7.1. 401 #define GTEST_HAS_PARAM_TEST 1 402 #endif // defined(__GNUC__) || (_MSC_VER >= 1400) 403 404 // Determines whether to support type-driven tests. 405 406 // Typed tests need <typeinfo> and variadic macros, which gcc and VC 407 // 8.0+ support. 408 // TODO: We should be able to support these on Android but we don't 409 // have cxxabi.h when building for the target but we have it for the host. 410 #if ( defined(__GNUC__) || (_MSC_VER >= 1400) ) && !defined(GTEST_OS_ANDROID) 411 #define GTEST_HAS_TYPED_TEST 1 412 #define GTEST_HAS_TYPED_TEST_P 1 413 #endif // defined(__GNUC__) || (_MSC_VER >= 1400) && !defined(GTEST_OS_ANDROID) 414 415 // Determines whether to support Combine(). This only makes sense when 416 // value-parameterized tests are enabled. 417 #if GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE 418 #define GTEST_HAS_COMBINE 1 419 #endif // GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE 420 421 // Determines whether the system compiler uses UTF-16 for encoding wide strings. 422 #define GTEST_WIDE_STRING_USES_UTF16_ \ 423 (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN) 424 425 // Defines some utility macros. 426 427 // The GNU compiler emits a warning if nested "if" statements are followed by 428 // an "else" statement and braces are not used to explicitly disambiguate the 429 // "else" binding. This leads to problems with code like: 430 // 431 // if (gate) 432 // ASSERT_*(condition) << "Some message"; 433 // 434 // The "switch (0) case 0:" idiom is used to suppress this. 435 #ifdef __INTEL_COMPILER 436 #define GTEST_AMBIGUOUS_ELSE_BLOCKER_ 437 #else 438 #define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: // NOLINT 439 #endif 440 441 // Use this annotation at the end of a struct / class definition to 442 // prevent the compiler from optimizing away instances that are never 443 // used. This is useful when all interesting logic happens inside the 444 // c'tor and / or d'tor. Example: 445 // 446 // struct Foo { 447 // Foo() { ... } 448 // } GTEST_ATTRIBUTE_UNUSED_; 449 #if defined(__GNUC__) && !defined(COMPILER_ICC) 450 #define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused)) 451 #else 452 #define GTEST_ATTRIBUTE_UNUSED_ 453 #endif 454 455 // A macro to disallow the evil copy constructor and operator= functions 456 // This should be used in the private: declarations for a class. 457 #define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\ 458 type(const type &);\ 459 void operator=(const type &) 460 461 // Tell the compiler to warn about unused return values for functions declared 462 // with this macro. The macro should be used on function declarations 463 // following the argument list: 464 // 465 // Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_; 466 #if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC) 467 #define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result)) 468 #else 469 #define GTEST_MUST_USE_RESULT_ 470 #endif // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC 471 472 namespace testing { 473 474 class Message; 475 476 namespace internal { 477 478 class String; 479 480 // std::strstream is deprecated. However, we have to use it on 481 // Windows as std::stringstream won't compile on Windows when 482 // exceptions are disabled. We use std::stringstream on other 483 // platforms to avoid compiler warnings there. 484 #if GTEST_HAS_STD_STRING 485 typedef ::std::stringstream StrStream; 486 #else 487 typedef ::std::strstream StrStream; 488 #endif // GTEST_HAS_STD_STRING 489 490 // Defines scoped_ptr. 491 492 // This implementation of scoped_ptr is PARTIAL - it only contains 493 // enough stuff to satisfy Google Test's need. 494 template <typename T> 495 class scoped_ptr { 496 public: 497 explicit scoped_ptr(T* p = NULL) : ptr_(p) {} 498 ~scoped_ptr() { reset(); } 499 500 T& operator*() const { return *ptr_; } 501 T* operator->() const { return ptr_; } 502 T* get() const { return ptr_; } 503 504 T* release() { 505 T* const ptr = ptr_; 506 ptr_ = NULL; 507 return ptr; 508 } 509 510 void reset(T* p = NULL) { 511 if (p != ptr_) { 512 if (sizeof(T) > 0) { // Makes sure T is a complete type. 513 delete ptr_; 514 } 515 ptr_ = p; 516 } 517 } 518 private: 519 T* ptr_; 520 521 GTEST_DISALLOW_COPY_AND_ASSIGN_(scoped_ptr); 522 }; 523 524 // Defines RE. 525 526 // A simple C++ wrapper for <regex.h>. It uses the POSIX Enxtended 527 // Regular Expression syntax. 528 class RE { 529 public: 530 // Constructs an RE from a string. 531 #if GTEST_HAS_STD_STRING 532 RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT 533 #endif // GTEST_HAS_STD_STRING 534 535 #if GTEST_HAS_GLOBAL_STRING 536 RE(const ::string& regex) { Init(regex.c_str()); } // NOLINT 537 #endif // GTEST_HAS_GLOBAL_STRING 538 539 RE(const char* regex) { Init(regex); } // NOLINT 540 ~RE(); 541 542 // Returns the string representation of the regex. 543 const char* pattern() const { return pattern_; } 544 545 // FullMatch(str, re) returns true iff regular expression re matches 546 // the entire str. 547 // PartialMatch(str, re) returns true iff regular expression re 548 // matches a substring of str (including str itself). 549 // 550 // TODO(wan (at) google.com): make FullMatch() and PartialMatch() work 551 // when str contains NUL characters. 552 #if GTEST_HAS_STD_STRING 553 static bool FullMatch(const ::std::string& str, const RE& re) { 554 return FullMatch(str.c_str(), re); 555 } 556 static bool PartialMatch(const ::std::string& str, const RE& re) { 557 return PartialMatch(str.c_str(), re); 558 } 559 #endif // GTEST_HAS_STD_STRING 560 561 #if GTEST_HAS_GLOBAL_STRING 562 static bool FullMatch(const ::string& str, const RE& re) { 563 return FullMatch(str.c_str(), re); 564 } 565 static bool PartialMatch(const ::string& str, const RE& re) { 566 return PartialMatch(str.c_str(), re); 567 } 568 #endif // GTEST_HAS_GLOBAL_STRING 569 570 static bool FullMatch(const char* str, const RE& re); 571 static bool PartialMatch(const char* str, const RE& re); 572 573 private: 574 void Init(const char* regex); 575 576 // We use a const char* instead of a string, as Google Test may be used 577 // where string is not available. We also do not use Google Test's own 578 // String type here, in order to simplify dependencies between the 579 // files. 580 const char* pattern_; 581 bool is_valid_; 582 #if GTEST_USES_POSIX_RE 583 regex_t full_regex_; // For FullMatch(). 584 regex_t partial_regex_; // For PartialMatch(). 585 #else // GTEST_USES_SIMPLE_RE 586 const char* full_pattern_; // For FullMatch(); 587 #endif 588 589 GTEST_DISALLOW_COPY_AND_ASSIGN_(RE); 590 }; 591 592 // Defines logging utilities: 593 // GTEST_LOG_() - logs messages at the specified severity level. 594 // LogToStderr() - directs all log messages to stderr. 595 // FlushInfoLog() - flushes informational log messages. 596 597 enum GTestLogSeverity { 598 GTEST_INFO, 599 GTEST_WARNING, 600 GTEST_ERROR, 601 GTEST_FATAL 602 }; 603 604 void GTestLog(GTestLogSeverity severity, const char* file, 605 int line, const char* msg); 606 607 #define GTEST_LOG_(severity, msg)\ 608 ::testing::internal::GTestLog(\ 609 ::testing::internal::GTEST_##severity, __FILE__, __LINE__, \ 610 (::testing::Message() << (msg)).GetString().c_str()) 611 612 inline void LogToStderr() {} 613 inline void FlushInfoLog() { fflush(NULL); } 614 615 // Defines the stderr capturer: 616 // CaptureStderr - starts capturing stderr. 617 // GetCapturedStderr - stops capturing stderr and returns the captured string. 618 619 #if GTEST_HAS_STD_STRING 620 void CaptureStderr(); 621 ::std::string GetCapturedStderr(); 622 #endif // GTEST_HAS_STD_STRING 623 624 #if GTEST_HAS_DEATH_TEST 625 626 // A copy of all command line arguments. Set by InitGoogleTest(). 627 extern ::std::vector<String> g_argvs; 628 629 // GTEST_HAS_DEATH_TEST implies we have ::std::string. 630 const ::std::vector<String>& GetArgvs(); 631 632 #endif // GTEST_HAS_DEATH_TEST 633 634 // Defines synchronization primitives. 635 636 // A dummy implementation of synchronization primitives (mutex, lock, 637 // and thread-local variable). Necessary for compiling Google Test where 638 // mutex is not supported - using Google Test in multiple threads is not 639 // supported on such platforms. 640 641 class Mutex { 642 public: 643 Mutex() {} 644 explicit Mutex(int /*unused*/) {} 645 void AssertHeld() const {} 646 enum { NO_CONSTRUCTOR_NEEDED_FOR_STATIC_MUTEX = 0 }; 647 }; 648 649 // We cannot call it MutexLock directly as the ctor declaration would 650 // conflict with a macro named MutexLock, which is defined on some 651 // platforms. Hence the typedef trick below. 652 class GTestMutexLock { 653 public: 654 explicit GTestMutexLock(Mutex*) {} // NOLINT 655 }; 656 657 typedef GTestMutexLock MutexLock; 658 659 template <typename T> 660 class ThreadLocal { 661 public: 662 ThreadLocal() : value_() {} 663 explicit ThreadLocal(const T& value) : value_(value) {} 664 T* pointer() { return &value_; } 665 const T* pointer() const { return &value_; } 666 const T& get() const { return value_; } 667 void set(const T& value) { value_ = value; } 668 private: 669 T value_; 670 }; 671 672 // There's no portable way to detect the number of threads, so we just 673 // return 0 to indicate that we cannot detect it. 674 inline size_t GetThreadCount() { return 0; } 675 676 // The above synchronization primitives have dummy implementations. 677 // Therefore Google Test is not thread-safe. 678 #define GTEST_IS_THREADSAFE 0 679 680 #if defined(__SYMBIAN32__) || defined(__IBMCPP__) 681 682 // Passing non-POD classes through ellipsis (...) crashes the ARM 683 // compiler. The Nokia Symbian and the IBM XL C/C++ compiler try to 684 // instantiate a copy constructor for objects passed through ellipsis 685 // (...), failing for uncopyable objects. We define this to indicate 686 // the fact. 687 #define GTEST_ELLIPSIS_NEEDS_COPY_ 1 688 689 // The Nokia Symbian and IBM XL C/C++ compilers cannot decide between 690 // const T& and const T* in a function template. These compilers 691 // _can_ decide between class template specializations for T and T*, 692 // so a tr1::type_traits-like is_pointer works. 693 #define GTEST_NEEDS_IS_POINTER_ 1 694 695 #endif // defined(__SYMBIAN32__) || defined(__IBMCPP__) 696 697 template <bool bool_value> 698 struct bool_constant { 699 typedef bool_constant<bool_value> type; 700 static const bool value = bool_value; 701 }; 702 template <bool bool_value> const bool bool_constant<bool_value>::value; 703 704 typedef bool_constant<false> false_type; 705 typedef bool_constant<true> true_type; 706 707 template <typename T> 708 struct is_pointer : public false_type {}; 709 710 template <typename T> 711 struct is_pointer<T*> : public true_type {}; 712 713 #if GTEST_OS_WINDOWS 714 #define GTEST_PATH_SEP_ "\\" 715 #else 716 #define GTEST_PATH_SEP_ "/" 717 #endif // GTEST_OS_WINDOWS 718 719 // Defines BiggestInt as the biggest signed integer type the compiler 720 // supports. 721 #if GTEST_OS_WINDOWS 722 typedef __int64 BiggestInt; 723 #else 724 typedef long long BiggestInt; // NOLINT 725 #endif // GTEST_OS_WINDOWS 726 727 // The maximum number a BiggestInt can represent. This definition 728 // works no matter BiggestInt is represented in one's complement or 729 // two's complement. 730 // 731 // We cannot rely on numeric_limits in STL, as __int64 and long long 732 // are not part of standard C++ and numeric_limits doesn't need to be 733 // defined for them. 734 const BiggestInt kMaxBiggestInt = 735 ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1)); 736 737 // This template class serves as a compile-time function from size to 738 // type. It maps a size in bytes to a primitive type with that 739 // size. e.g. 740 // 741 // TypeWithSize<4>::UInt 742 // 743 // is typedef-ed to be unsigned int (unsigned integer made up of 4 744 // bytes). 745 // 746 // Such functionality should belong to STL, but I cannot find it 747 // there. 748 // 749 // Google Test uses this class in the implementation of floating-point 750 // comparison. 751 // 752 // For now it only handles UInt (unsigned int) as that's all Google Test 753 // needs. Other types can be easily added in the future if need 754 // arises. 755 template <size_t size> 756 class TypeWithSize { 757 public: 758 // This prevents the user from using TypeWithSize<N> with incorrect 759 // values of N. 760 typedef void UInt; 761 }; 762 763 // The specialization for size 4. 764 template <> 765 class TypeWithSize<4> { 766 public: 767 // unsigned int has size 4 in both gcc and MSVC. 768 // 769 // As base/basictypes.h doesn't compile on Windows, we cannot use 770 // uint32, uint64, and etc here. 771 typedef int Int; 772 typedef unsigned int UInt; 773 }; 774 775 // The specialization for size 8. 776 template <> 777 class TypeWithSize<8> { 778 public: 779 #if GTEST_OS_WINDOWS 780 typedef __int64 Int; 781 typedef unsigned __int64 UInt; 782 #else 783 typedef long long Int; // NOLINT 784 typedef unsigned long long UInt; // NOLINT 785 #endif // GTEST_OS_WINDOWS 786 }; 787 788 // Integer types of known sizes. 789 typedef TypeWithSize<4>::Int Int32; 790 typedef TypeWithSize<4>::UInt UInt32; 791 typedef TypeWithSize<8>::Int Int64; 792 typedef TypeWithSize<8>::UInt UInt64; 793 typedef TypeWithSize<8>::Int TimeInMillis; // Represents time in milliseconds. 794 795 // Utilities for command line flags and environment variables. 796 797 // A wrapper for getenv() that works on Linux, Windows, and Mac OS. 798 inline const char* GetEnv(const char* name) { 799 #ifdef _WIN32_WCE // We are on Windows CE. 800 // CE has no environment variables. 801 return NULL; 802 #elif GTEST_OS_WINDOWS // We are on Windows proper. 803 // MSVC 8 deprecates getenv(), so we want to suppress warning 4996 804 // (deprecated function) there. 805 #pragma warning(push) // Saves the current warning state. 806 #pragma warning(disable:4996) // Temporarily disables warning 4996. 807 return getenv(name); 808 #pragma warning(pop) // Restores the warning state. 809 #else // We are on Linux or Mac OS. 810 return getenv(name); 811 #endif 812 } 813 814 #ifdef _WIN32_WCE 815 // Windows CE has no C library. The abort() function is used in 816 // several places in Google Test. This implementation provides a reasonable 817 // imitation of standard behaviour. 818 void abort(); 819 #else 820 inline void abort() { ::abort(); } 821 #endif // _WIN32_WCE 822 823 // INTERNAL IMPLEMENTATION - DO NOT USE. 824 // 825 // GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition 826 // is not satisfied. 827 // Synopsys: 828 // GTEST_CHECK_(boolean_condition); 829 // or 830 // GTEST_CHECK_(boolean_condition) << "Additional message"; 831 // 832 // This checks the condition and if the condition is not satisfied 833 // it prints message about the condition violation, including the 834 // condition itself, plus additional message streamed into it, if any, 835 // and then it aborts the program. It aborts the program irrespective of 836 // whether it is built in the debug mode or not. 837 class GTestCheckProvider { 838 public: 839 GTestCheckProvider(const char* condition, const char* file, int line) { 840 FormatFileLocation(file, line); 841 ::std::cerr << " ERROR: Condition " << condition << " failed. "; 842 } 843 ~GTestCheckProvider() { 844 ::std::cerr << ::std::endl; 845 abort(); 846 } 847 void FormatFileLocation(const char* file, int line) { 848 if (file == NULL) 849 file = "unknown file"; 850 if (line < 0) { 851 ::std::cerr << file << ":"; 852 } else { 853 #if _MSC_VER 854 ::std::cerr << file << "(" << line << "):"; 855 #else 856 ::std::cerr << file << ":" << line << ":"; 857 #endif 858 } 859 } 860 ::std::ostream& GetStream() { return ::std::cerr; } 861 }; 862 #define GTEST_CHECK_(condition) \ 863 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 864 if (condition) \ 865 ; \ 866 else \ 867 ::testing::internal::GTestCheckProvider(\ 868 #condition, __FILE__, __LINE__).GetStream() 869 870 // Macro for referencing flags. 871 #define GTEST_FLAG(name) FLAGS_gtest_##name 872 873 // Macros for declaring flags. 874 #define GTEST_DECLARE_bool_(name) extern bool GTEST_FLAG(name) 875 #define GTEST_DECLARE_int32_(name) \ 876 extern ::testing::internal::Int32 GTEST_FLAG(name) 877 #define GTEST_DECLARE_string_(name) \ 878 extern ::testing::internal::String GTEST_FLAG(name) 879 880 // Macros for defining flags. 881 #define GTEST_DEFINE_bool_(name, default_val, doc) \ 882 bool GTEST_FLAG(name) = (default_val) 883 #define GTEST_DEFINE_int32_(name, default_val, doc) \ 884 ::testing::internal::Int32 GTEST_FLAG(name) = (default_val) 885 #define GTEST_DEFINE_string_(name, default_val, doc) \ 886 ::testing::internal::String GTEST_FLAG(name) = (default_val) 887 888 // Parses 'str' for a 32-bit signed integer. If successful, writes the result 889 // to *value and returns true; otherwise leaves *value unchanged and returns 890 // false. 891 // TODO(chandlerc): Find a better way to refactor flag and environment parsing 892 // out of both gtest-port.cc and gtest.cc to avoid exporting this utility 893 // function. 894 bool ParseInt32(const Message& src_text, const char* str, Int32* value); 895 896 // Parses a bool/Int32/string from the environment variable 897 // corresponding to the given Google Test flag. 898 bool BoolFromGTestEnv(const char* flag, bool default_val); 899 Int32 Int32FromGTestEnv(const char* flag, Int32 default_val); 900 const char* StringFromGTestEnv(const char* flag, const char* default_val); 901 902 } // namespace internal 903 } // namespace testing 904 905 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 906