gtest-port.h revision 3f9bef5994073d5f7e91e638ab000821ea0f7628
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@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@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@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@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// Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled. 303#if GTEST_GCC_VER_ >= 40302 304#ifdef __GXX_RTTI 305#define GTEST_HAS_RTTI 1 306#else 307#define GTEST_HAS_RTTI 0 308#endif // __GXX_RTTI 309#else 310// For gcc versions smaller than 4.3.2, we assume RTTI is enabled. 311#define GTEST_HAS_RTTI 1 312#endif // GTEST_GCC_VER >= 40302 313 314#else 315 316// Unknown compiler - assume RTTI is enabled. 317#define GTEST_HAS_RTTI 1 318 319#endif // _MSC_VER 320 321#endif // GTEST_HAS_RTTI 322 323// Determines whether <pthread.h> is available. 324#ifndef GTEST_HAS_PTHREAD 325// The user didn't tell us, so we need to figure it out. 326#define GTEST_HAS_PTHREAD (GTEST_OS_LINUX || GTEST_OS_MAC) 327#endif // GTEST_HAS_PTHREAD 328 329// Determines whether tr1/tuple is available. If you have tr1/tuple 330// on your platform, define GTEST_HAS_TR1_TUPLE=1 for both the Google 331// Test project and your tests. If you would like Google Test to detect 332// tr1/tuple on your platform automatically, please open an issue 333// ticket at http://code.google.com/p/googletest. 334#ifndef GTEST_HAS_TR1_TUPLE 335// The user didn't tell us, so we need to figure it out. 336 337// GCC provides <tr1/tuple> since 4.0.0. 338#if defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000) && !GTEST_OS_ANDROID 339#define GTEST_HAS_TR1_TUPLE 1 340#else 341#define GTEST_HAS_TR1_TUPLE 0 342#endif // __GNUC__ 343#endif // GTEST_HAS_TR1_TUPLE 344 345// To avoid conditional compilation everywhere, we make it 346// gtest-port.h's responsibility to #include the header implementing 347// tr1/tuple. 348#if GTEST_HAS_TR1_TUPLE 349#if defined(__GNUC__) 350// GCC implements tr1/tuple in the <tr1/tuple> header. This does not 351// conform to the TR1 spec, which requires the header to be <tuple>. 352#include <tr1/tuple> 353#else 354// If the compiler is not GCC, we assume the user is using a 355// spec-conforming TR1 implementation. 356#include <tuple> 357#endif // __GNUC__ 358#endif // GTEST_HAS_TR1_TUPLE 359 360// Determines whether clone(2) is supported. 361// Usually it will only be available on Linux, excluding 362// Linux on the Itanium architecture. 363// Also see http://linux.die.net/man/2/clone. 364#ifndef GTEST_HAS_CLONE 365// The user didn't tell us, so we need to figure it out. 366 367#if GTEST_OS_LINUX && !defined(__ia64__) 368#define GTEST_HAS_CLONE 1 369#else 370#define GTEST_HAS_CLONE 0 371#endif // GTEST_OS_LINUX && !defined(__ia64__) 372 373#endif // GTEST_HAS_CLONE 374 375// Determines whether to support death tests. 376// Google Test does not support death tests for VC 7.1 and earlier for 377// these reasons: 378// 1. std::vector does not build in VC 7.1 when exceptions are disabled. 379// 2. std::string does not build in VC 7.1 when exceptions are disabled 380// (this is covered by GTEST_HAS_STD_STRING guard). 381// 3. abort() in a VC 7.1 application compiled as GUI in debug config 382// pops up a dialog window that cannot be suppressed programmatically. 383#if GTEST_HAS_STD_STRING && (GTEST_OS_LINUX || \ 384 GTEST_OS_MAC || \ 385 GTEST_OS_CYGWIN || \ 386 (GTEST_OS_WINDOWS && _MSC_VER >= 1400)) && !GTEST_OS_ANDROID 387#define GTEST_HAS_DEATH_TEST 1 388#include <vector> 389#endif 390 391// Determines whether to support value-parameterized tests. 392 393#if defined(__GNUC__) || (_MSC_VER >= 1400) 394// TODO(vladl@google.com): get the implementation rid of vector and list 395// to compile on MSVC 7.1. 396#define GTEST_HAS_PARAM_TEST 1 397#endif // defined(__GNUC__) || (_MSC_VER >= 1400) 398 399// Determines whether to support type-driven tests. 400 401// Typed tests need <typeinfo> and variadic macros, which gcc and VC 402// 8.0+ support. 403#if defined(__GNUC__) || (_MSC_VER >= 1400) 404#define GTEST_HAS_TYPED_TEST 1 405#define GTEST_HAS_TYPED_TEST_P 1 406#endif // defined(__GNUC__) || (_MSC_VER >= 1400) 407 408// Determines whether to support Combine(). This only makes sense when 409// value-parameterized tests are enabled. 410#if GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE 411#define GTEST_HAS_COMBINE 1 412#endif // GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE 413 414// Determines whether the system compiler uses UTF-16 for encoding wide strings. 415#define GTEST_WIDE_STRING_USES_UTF16_ \ 416 (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN) 417 418// Defines some utility macros. 419 420// The GNU compiler emits a warning if nested "if" statements are followed by 421// an "else" statement and braces are not used to explicitly disambiguate the 422// "else" binding. This leads to problems with code like: 423// 424// if (gate) 425// ASSERT_*(condition) << "Some message"; 426// 427// The "switch (0) case 0:" idiom is used to suppress this. 428#ifdef __INTEL_COMPILER 429#define GTEST_AMBIGUOUS_ELSE_BLOCKER_ 430#else 431#define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: // NOLINT 432#endif 433 434// Use this annotation at the end of a struct / class definition to 435// prevent the compiler from optimizing away instances that are never 436// used. This is useful when all interesting logic happens inside the 437// c'tor and / or d'tor. Example: 438// 439// struct Foo { 440// Foo() { ... } 441// } GTEST_ATTRIBUTE_UNUSED_; 442#if defined(__GNUC__) && !defined(COMPILER_ICC) 443#define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused)) 444#else 445#define GTEST_ATTRIBUTE_UNUSED_ 446#endif 447 448// A macro to disallow the evil copy constructor and operator= functions 449// This should be used in the private: declarations for a class. 450#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\ 451 type(const type &);\ 452 void operator=(const type &) 453 454// Tell the compiler to warn about unused return values for functions declared 455// with this macro. The macro should be used on function declarations 456// following the argument list: 457// 458// Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_; 459#if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC) 460#define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result)) 461#else 462#define GTEST_MUST_USE_RESULT_ 463#endif // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC 464 465namespace testing { 466 467class Message; 468 469namespace internal { 470 471class String; 472 473// std::strstream is deprecated. However, we have to use it on 474// Windows as std::stringstream won't compile on Windows when 475// exceptions are disabled. We use std::stringstream on other 476// platforms to avoid compiler warnings there. 477#if GTEST_HAS_STD_STRING 478typedef ::std::stringstream StrStream; 479#else 480typedef ::std::strstream StrStream; 481#endif // GTEST_HAS_STD_STRING 482 483// Defines scoped_ptr. 484 485// This implementation of scoped_ptr is PARTIAL - it only contains 486// enough stuff to satisfy Google Test's need. 487template <typename T> 488class scoped_ptr { 489 public: 490 explicit scoped_ptr(T* p = NULL) : ptr_(p) {} 491 ~scoped_ptr() { reset(); } 492 493 T& operator*() const { return *ptr_; } 494 T* operator->() const { return ptr_; } 495 T* get() const { return ptr_; } 496 497 T* release() { 498 T* const ptr = ptr_; 499 ptr_ = NULL; 500 return ptr; 501 } 502 503 void reset(T* p = NULL) { 504 if (p != ptr_) { 505 if (sizeof(T) > 0) { // Makes sure T is a complete type. 506 delete ptr_; 507 } 508 ptr_ = p; 509 } 510 } 511 private: 512 T* ptr_; 513 514 GTEST_DISALLOW_COPY_AND_ASSIGN_(scoped_ptr); 515}; 516 517// Defines RE. 518 519// A simple C++ wrapper for <regex.h>. It uses the POSIX Enxtended 520// Regular Expression syntax. 521class RE { 522 public: 523 // Constructs an RE from a string. 524#if GTEST_HAS_STD_STRING 525 RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT 526#endif // GTEST_HAS_STD_STRING 527 528#if GTEST_HAS_GLOBAL_STRING 529 RE(const ::string& regex) { Init(regex.c_str()); } // NOLINT 530#endif // GTEST_HAS_GLOBAL_STRING 531 532 RE(const char* regex) { Init(regex); } // NOLINT 533 ~RE(); 534 535 // Returns the string representation of the regex. 536 const char* pattern() const { return pattern_; } 537 538 // FullMatch(str, re) returns true iff regular expression re matches 539 // the entire str. 540 // PartialMatch(str, re) returns true iff regular expression re 541 // matches a substring of str (including str itself). 542 // 543 // TODO(wan@google.com): make FullMatch() and PartialMatch() work 544 // when str contains NUL characters. 545#if GTEST_HAS_STD_STRING 546 static bool FullMatch(const ::std::string& str, const RE& re) { 547 return FullMatch(str.c_str(), re); 548 } 549 static bool PartialMatch(const ::std::string& str, const RE& re) { 550 return PartialMatch(str.c_str(), re); 551 } 552#endif // GTEST_HAS_STD_STRING 553 554#if GTEST_HAS_GLOBAL_STRING 555 static bool FullMatch(const ::string& str, const RE& re) { 556 return FullMatch(str.c_str(), re); 557 } 558 static bool PartialMatch(const ::string& str, const RE& re) { 559 return PartialMatch(str.c_str(), re); 560 } 561#endif // GTEST_HAS_GLOBAL_STRING 562 563 static bool FullMatch(const char* str, const RE& re); 564 static bool PartialMatch(const char* str, const RE& re); 565 566 private: 567 void Init(const char* regex); 568 569 // We use a const char* instead of a string, as Google Test may be used 570 // where string is not available. We also do not use Google Test's own 571 // String type here, in order to simplify dependencies between the 572 // files. 573 const char* pattern_; 574 bool is_valid_; 575#if GTEST_USES_POSIX_RE 576 regex_t full_regex_; // For FullMatch(). 577 regex_t partial_regex_; // For PartialMatch(). 578#else // GTEST_USES_SIMPLE_RE 579 const char* full_pattern_; // For FullMatch(); 580#endif 581 582 GTEST_DISALLOW_COPY_AND_ASSIGN_(RE); 583}; 584 585// Defines logging utilities: 586// GTEST_LOG_() - logs messages at the specified severity level. 587// LogToStderr() - directs all log messages to stderr. 588// FlushInfoLog() - flushes informational log messages. 589 590enum GTestLogSeverity { 591 GTEST_INFO, 592 GTEST_WARNING, 593 GTEST_ERROR, 594 GTEST_FATAL 595}; 596 597void GTestLog(GTestLogSeverity severity, const char* file, 598 int line, const char* msg); 599 600#define GTEST_LOG_(severity, msg)\ 601 ::testing::internal::GTestLog(\ 602 ::testing::internal::GTEST_##severity, __FILE__, __LINE__, \ 603 (::testing::Message() << (msg)).GetString().c_str()) 604 605inline void LogToStderr() {} 606inline void FlushInfoLog() { fflush(NULL); } 607 608// Defines the stderr capturer: 609// CaptureStderr - starts capturing stderr. 610// GetCapturedStderr - stops capturing stderr and returns the captured string. 611 612#if GTEST_HAS_STD_STRING 613void CaptureStderr(); 614::std::string GetCapturedStderr(); 615#endif // GTEST_HAS_STD_STRING 616 617#if GTEST_HAS_DEATH_TEST 618 619// A copy of all command line arguments. Set by InitGoogleTest(). 620extern ::std::vector<String> g_argvs; 621 622// GTEST_HAS_DEATH_TEST implies we have ::std::string. 623const ::std::vector<String>& GetArgvs(); 624 625#endif // GTEST_HAS_DEATH_TEST 626 627// Defines synchronization primitives. 628 629// A dummy implementation of synchronization primitives (mutex, lock, 630// and thread-local variable). Necessary for compiling Google Test where 631// mutex is not supported - using Google Test in multiple threads is not 632// supported on such platforms. 633 634class Mutex { 635 public: 636 Mutex() {} 637 explicit Mutex(int /*unused*/) {} 638 void AssertHeld() const {} 639 enum { NO_CONSTRUCTOR_NEEDED_FOR_STATIC_MUTEX = 0 }; 640}; 641 642// We cannot call it MutexLock directly as the ctor declaration would 643// conflict with a macro named MutexLock, which is defined on some 644// platforms. Hence the typedef trick below. 645class GTestMutexLock { 646 public: 647 explicit GTestMutexLock(Mutex*) {} // NOLINT 648}; 649 650typedef GTestMutexLock MutexLock; 651 652template <typename T> 653class ThreadLocal { 654 public: 655 ThreadLocal() : value_() {} 656 explicit ThreadLocal(const T& value) : value_(value) {} 657 T* pointer() { return &value_; } 658 const T* pointer() const { return &value_; } 659 const T& get() const { return value_; } 660 void set(const T& value) { value_ = value; } 661 private: 662 T value_; 663}; 664 665// There's no portable way to detect the number of threads, so we just 666// return 0 to indicate that we cannot detect it. 667inline size_t GetThreadCount() { return 0; } 668 669// The above synchronization primitives have dummy implementations. 670// Therefore Google Test is not thread-safe. 671#define GTEST_IS_THREADSAFE 0 672 673#if defined(__SYMBIAN32__) || defined(__IBMCPP__) 674 675// Passing non-POD classes through ellipsis (...) crashes the ARM 676// compiler. The Nokia Symbian and the IBM XL C/C++ compiler try to 677// instantiate a copy constructor for objects passed through ellipsis 678// (...), failing for uncopyable objects. We define this to indicate 679// the fact. 680#define GTEST_ELLIPSIS_NEEDS_COPY_ 1 681 682// The Nokia Symbian and IBM XL C/C++ compilers cannot decide between 683// const T& and const T* in a function template. These compilers 684// _can_ decide between class template specializations for T and T*, 685// so a tr1::type_traits-like is_pointer works. 686#define GTEST_NEEDS_IS_POINTER_ 1 687 688#endif // defined(__SYMBIAN32__) || defined(__IBMCPP__) 689 690template <bool bool_value> 691struct bool_constant { 692 typedef bool_constant<bool_value> type; 693 static const bool value = bool_value; 694}; 695template <bool bool_value> const bool bool_constant<bool_value>::value; 696 697typedef bool_constant<false> false_type; 698typedef bool_constant<true> true_type; 699 700template <typename T> 701struct is_pointer : public false_type {}; 702 703template <typename T> 704struct is_pointer<T*> : public true_type {}; 705 706#if GTEST_OS_WINDOWS 707#define GTEST_PATH_SEP_ "\\" 708#else 709#define GTEST_PATH_SEP_ "/" 710#endif // GTEST_OS_WINDOWS 711 712// Defines BiggestInt as the biggest signed integer type the compiler 713// supports. 714#if GTEST_OS_WINDOWS 715typedef __int64 BiggestInt; 716#else 717typedef long long BiggestInt; // NOLINT 718#endif // GTEST_OS_WINDOWS 719 720// The maximum number a BiggestInt can represent. This definition 721// works no matter BiggestInt is represented in one's complement or 722// two's complement. 723// 724// We cannot rely on numeric_limits in STL, as __int64 and long long 725// are not part of standard C++ and numeric_limits doesn't need to be 726// defined for them. 727const BiggestInt kMaxBiggestInt = 728 ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1)); 729 730// This template class serves as a compile-time function from size to 731// type. It maps a size in bytes to a primitive type with that 732// size. e.g. 733// 734// TypeWithSize<4>::UInt 735// 736// is typedef-ed to be unsigned int (unsigned integer made up of 4 737// bytes). 738// 739// Such functionality should belong to STL, but I cannot find it 740// there. 741// 742// Google Test uses this class in the implementation of floating-point 743// comparison. 744// 745// For now it only handles UInt (unsigned int) as that's all Google Test 746// needs. Other types can be easily added in the future if need 747// arises. 748template <size_t size> 749class TypeWithSize { 750 public: 751 // This prevents the user from using TypeWithSize<N> with incorrect 752 // values of N. 753 typedef void UInt; 754}; 755 756// The specialization for size 4. 757template <> 758class TypeWithSize<4> { 759 public: 760 // unsigned int has size 4 in both gcc and MSVC. 761 // 762 // As base/basictypes.h doesn't compile on Windows, we cannot use 763 // uint32, uint64, and etc here. 764 typedef int Int; 765 typedef unsigned int UInt; 766}; 767 768// The specialization for size 8. 769template <> 770class TypeWithSize<8> { 771 public: 772#if GTEST_OS_WINDOWS 773 typedef __int64 Int; 774 typedef unsigned __int64 UInt; 775#else 776 typedef long long Int; // NOLINT 777 typedef unsigned long long UInt; // NOLINT 778#endif // GTEST_OS_WINDOWS 779}; 780 781// Integer types of known sizes. 782typedef TypeWithSize<4>::Int Int32; 783typedef TypeWithSize<4>::UInt UInt32; 784typedef TypeWithSize<8>::Int Int64; 785typedef TypeWithSize<8>::UInt UInt64; 786typedef TypeWithSize<8>::Int TimeInMillis; // Represents time in milliseconds. 787 788// Utilities for command line flags and environment variables. 789 790// A wrapper for getenv() that works on Linux, Windows, and Mac OS. 791inline const char* GetEnv(const char* name) { 792#ifdef _WIN32_WCE // We are on Windows CE. 793 // CE has no environment variables. 794 return NULL; 795#elif GTEST_OS_WINDOWS // We are on Windows proper. 796 // MSVC 8 deprecates getenv(), so we want to suppress warning 4996 797 // (deprecated function) there. 798#pragma warning(push) // Saves the current warning state. 799#pragma warning(disable:4996) // Temporarily disables warning 4996. 800 return getenv(name); 801#pragma warning(pop) // Restores the warning state. 802#else // We are on Linux or Mac OS. 803 return getenv(name); 804#endif 805} 806 807#ifdef _WIN32_WCE 808// Windows CE has no C library. The abort() function is used in 809// several places in Google Test. This implementation provides a reasonable 810// imitation of standard behaviour. 811void abort(); 812#else 813inline void abort() { ::abort(); } 814#endif // _WIN32_WCE 815 816// INTERNAL IMPLEMENTATION - DO NOT USE. 817// 818// GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition 819// is not satisfied. 820// Synopsys: 821// GTEST_CHECK_(boolean_condition); 822// or 823// GTEST_CHECK_(boolean_condition) << "Additional message"; 824// 825// This checks the condition and if the condition is not satisfied 826// it prints message about the condition violation, including the 827// condition itself, plus additional message streamed into it, if any, 828// and then it aborts the program. It aborts the program irrespective of 829// whether it is built in the debug mode or not. 830class GTestCheckProvider { 831 public: 832 GTestCheckProvider(const char* condition, const char* file, int line) { 833 FormatFileLocation(file, line); 834 ::std::cerr << " ERROR: Condition " << condition << " failed. "; 835 } 836 ~GTestCheckProvider() { 837 ::std::cerr << ::std::endl; 838 abort(); 839 } 840 void FormatFileLocation(const char* file, int line) { 841 if (file == NULL) 842 file = "unknown file"; 843 if (line < 0) { 844 ::std::cerr << file << ":"; 845 } else { 846#if _MSC_VER 847 ::std::cerr << file << "(" << line << "):"; 848#else 849 ::std::cerr << file << ":" << line << ":"; 850#endif 851 } 852 } 853 ::std::ostream& GetStream() { return ::std::cerr; } 854}; 855#define GTEST_CHECK_(condition) \ 856 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 857 if (condition) \ 858 ; \ 859 else \ 860 ::testing::internal::GTestCheckProvider(\ 861 #condition, __FILE__, __LINE__).GetStream() 862 863// Macro for referencing flags. 864#define GTEST_FLAG(name) FLAGS_gtest_##name 865 866// Macros for declaring flags. 867#define GTEST_DECLARE_bool_(name) extern bool GTEST_FLAG(name) 868#define GTEST_DECLARE_int32_(name) \ 869 extern ::testing::internal::Int32 GTEST_FLAG(name) 870#define GTEST_DECLARE_string_(name) \ 871 extern ::testing::internal::String GTEST_FLAG(name) 872 873// Macros for defining flags. 874#define GTEST_DEFINE_bool_(name, default_val, doc) \ 875 bool GTEST_FLAG(name) = (default_val) 876#define GTEST_DEFINE_int32_(name, default_val, doc) \ 877 ::testing::internal::Int32 GTEST_FLAG(name) = (default_val) 878#define GTEST_DEFINE_string_(name, default_val, doc) \ 879 ::testing::internal::String GTEST_FLAG(name) = (default_val) 880 881// Parses 'str' for a 32-bit signed integer. If successful, writes the result 882// to *value and returns true; otherwise leaves *value unchanged and returns 883// false. 884// TODO(chandlerc): Find a better way to refactor flag and environment parsing 885// out of both gtest-port.cc and gtest.cc to avoid exporting this utility 886// function. 887bool ParseInt32(const Message& src_text, const char* str, Int32* value); 888 889// Parses a bool/Int32/string from the environment variable 890// corresponding to the given Google Test flag. 891bool BoolFromGTestEnv(const char* flag, bool default_val); 892Int32 Int32FromGTestEnv(const char* flag, Int32 default_val); 893const char* StringFromGTestEnv(const char* flag, const char* default_val); 894 895} // namespace internal 896} // namespace testing 897 898#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 899