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