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: 41// 42// GTEST_HAS_STD_STRING - Define it to 1/0 to indicate that 43// std::string does/doesn't work (Google Test can 44// be used where std::string is unavailable). 45// Leave it undefined to let Google Test define it. 46// GTEST_HAS_GLOBAL_STRING - Define it to 1/0 to indicate that ::string 47// is/isn't available (some systems define 48// ::string, which is different to std::string). 49// Leave it undefined to let Google Test define it. 50// GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that 51// std::wstring does/doesn't work (Google Test can 52// be used where std::wstring is unavailable). 53// Leave it undefined to let Google Test define it. 54// GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::string 55// is/isn't available (some systems define 56// ::wstring, which is different to std::wstring). 57// Leave it undefined to let Google Test define it. 58 59// This header defines the following utilities: 60// 61// Macros indicating the name of the Google C++ Testing Framework project: 62// GTEST_NAME - a string literal of the project name. 63// GTEST_FLAG_PREFIX - a string literal of the prefix all Google 64// Test flag names share. 65// GTEST_FLAG_PREFIX_UPPER - a string literal of the prefix all Google 66// Test flag names share, in upper case. 67// 68// Macros indicating the current platform: 69// GTEST_OS_CYGWIN - defined iff compiled on Cygwin. 70// GTEST_OS_LINUX - defined iff compiled on Linux. 71// GTEST_OS_MAC - defined iff compiled on Mac OS X. 72// GTEST_OS_WINDOWS - defined iff compiled on Windows. 73// Note that it is possible that none of the GTEST_OS_ macros are defined. 74// 75// Macros indicating available Google Test features: 76// GTEST_HAS_DEATH_TEST - defined iff death tests are supported. 77// 78// Macros for basic C++ coding: 79// GTEST_AMBIGUOUS_ELSE_BLOCKER - for disabling a gcc warning. 80// GTEST_ATTRIBUTE_UNUSED - declares that a class' instances don't have to 81// be used. 82// GTEST_DISALLOW_COPY_AND_ASSIGN() - disables copy ctor and operator=. 83// GTEST_MUST_USE_RESULT - declares that a function's result must be used. 84// 85// Synchronization: 86// Mutex, MutexLock, ThreadLocal, GetThreadCount() 87// - synchronization primitives. 88// 89// Template meta programming: 90// is_pointer - as in TR1; needed on Symbian only. 91// 92// Smart pointers: 93// scoped_ptr - as in TR2. 94// 95// Regular expressions: 96// RE - a simple regular expression class using the POSIX 97// Extended Regular Expression syntax. Not available on 98// Windows. 99// 100// Logging: 101// GTEST_LOG() - logs messages at the specified severity level. 102// LogToStderr() - directs all log messages to stderr. 103// FlushInfoLog() - flushes informational log messages. 104// 105// Stderr capturing: 106// CaptureStderr() - starts capturing stderr. 107// GetCapturedStderr() - stops capturing stderr and returns the captured 108// string. 109// 110// Integer types: 111// TypeWithSize - maps an integer to a int type. 112// Int32, UInt32, Int64, UInt64, TimeInMillis 113// - integers of known sizes. 114// BiggestInt - the biggest signed integer type. 115// 116// Command-line utilities: 117// GTEST_FLAG() - references a flag. 118// GTEST_DECLARE_*() - declares a flag. 119// GTEST_DEFINE_*() - defines a flag. 120// GetArgvs() - returns the command line as a vector of strings. 121// 122// Environment variable utilities: 123// GetEnv() - gets the value of an environment variable. 124// BoolFromGTestEnv() - parses a bool environment variable. 125// Int32FromGTestEnv() - parses an Int32 environment variable. 126// StringFromGTestEnv() - parses a string environment variable. 127 128#include <stdlib.h> 129#include <stdio.h> 130 131#define GTEST_NAME "Google Test" 132#define GTEST_FLAG_PREFIX "gtest_" 133#define GTEST_FLAG_PREFIX_UPPER "GTEST_" 134 135// Determines the platform on which Google Test is compiled. 136#ifdef __CYGWIN__ 137#define GTEST_OS_CYGWIN 138#elif defined _MSC_VER 139// TODO(kenton@google.com): GTEST_OS_WINDOWS is currently used to mean 140// both "The OS is Windows" and "The compiler is MSVC". These 141// meanings really should be separated in order to better support 142// Windows compilers other than MSVC. 143#define GTEST_OS_WINDOWS 144#elif defined __APPLE__ 145#define GTEST_OS_MAC 146#elif defined __linux__ 147#define GTEST_OS_LINUX 148#endif // _MSC_VER 149 150// Determines whether ::std::string and ::string are available. 151 152#ifndef GTEST_HAS_STD_STRING 153// The user didn't tell us whether ::std::string is available, so we 154// need to figure it out. 155 156#ifdef GTEST_OS_WINDOWS 157// Assumes that exceptions are enabled by default. 158#ifndef _HAS_EXCEPTIONS 159#define _HAS_EXCEPTIONS 1 160#endif // _HAS_EXCEPTIONS 161// GTEST_HAS_EXCEPTIONS is non-zero iff exceptions are enabled. It is 162// always defined, while _HAS_EXCEPTIONS is defined only on Windows. 163#define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS 164// On Windows, we can use ::std::string if the compiler version is VS 165// 2005 or above, or if exceptions are enabled. 166#define GTEST_HAS_STD_STRING ((_MSC_VER >= 1400) || GTEST_HAS_EXCEPTIONS) 167#else // We are on Linux or Mac OS. 168#define GTEST_HAS_EXCEPTIONS 0 169#define GTEST_HAS_STD_STRING 1 170#endif // GTEST_OS_WINDOWS 171 172#endif // GTEST_HAS_STD_STRING 173 174#ifndef GTEST_HAS_GLOBAL_STRING 175// The user didn't tell us whether ::string is available, so we need 176// to figure it out. 177 178#define GTEST_HAS_GLOBAL_STRING 0 179 180#endif // GTEST_HAS_GLOBAL_STRING 181 182#ifndef GTEST_HAS_STD_WSTRING 183// The user didn't tell us whether ::std::wstring is available, so we need 184// to figure it out. 185// TODO(wan@google.com): uses autoconf to detect whether ::std::wstring 186// is available. 187 188#ifdef GTEST_OS_CYGWIN 189// At least some versions of cygwin doesn't support ::std::wstring. 190#define GTEST_HAS_STD_WSTRING 0 191#else 192#define GTEST_HAS_STD_WSTRING GTEST_HAS_STD_STRING 193#endif // GTEST_OS_CYGWIN 194 195#endif // GTEST_HAS_STD_WSTRING 196 197#ifndef GTEST_HAS_GLOBAL_WSTRING 198// The user didn't tell us whether ::wstring is available, so we need 199// to figure it out. 200#define GTEST_HAS_GLOBAL_WSTRING GTEST_HAS_GLOBAL_STRING 201#endif // GTEST_HAS_GLOBAL_WSTRING 202 203#if GTEST_HAS_STD_STRING || GTEST_HAS_GLOBAL_STRING || \ 204 GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING 205#include <string> // NOLINT 206#endif // GTEST_HAS_STD_STRING || GTEST_HAS_GLOBAL_STRING || 207 // GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING 208 209#if GTEST_HAS_STD_STRING 210#include <sstream> // NOLINT 211#else 212#include <strstream> // NOLINT 213#endif // GTEST_HAS_STD_STRING 214 215// Determines whether to support death tests. 216#if GTEST_HAS_STD_STRING && defined(GTEST_OS_LINUX) 217#define GTEST_HAS_DEATH_TEST 218// On some platforms, <regex.h> needs someone to define size_t, and 219// won't compile otherwise. We can #include it here as we already 220// included <stdlib.h>, which is guaranteed to define size_t through 221// <stddef.h>. 222#include <regex.h> 223#include <vector> 224#include <fcntl.h> 225#include <sys/mman.h> 226#endif // GTEST_HAS_STD_STRING && defined(GTEST_OS_LINUX) 227 228// Defines some utility macros. 229 230// The GNU compiler emits a warning if nested "if" statements are followed by 231// an "else" statement and braces are not used to explicitly disambiguate the 232// "else" binding. This leads to problems with code like: 233// 234// if (gate) 235// ASSERT_*(condition) << "Some message"; 236// 237// The "switch (0) case 0:" idiom is used to suppress this. 238#ifdef __INTEL_COMPILER 239#define GTEST_AMBIGUOUS_ELSE_BLOCKER 240#else 241#define GTEST_AMBIGUOUS_ELSE_BLOCKER switch (0) case 0: // NOLINT 242#endif 243 244// Use this annotation at the end of a struct / class definition to 245// prevent the compiler from optimizing away instances that are never 246// used. This is useful when all interesting logic happens inside the 247// c'tor and / or d'tor. Example: 248// 249// struct Foo { 250// Foo() { ... } 251// } GTEST_ATTRIBUTE_UNUSED; 252#if defined(GTEST_OS_WINDOWS) || (defined(GTEST_OS_LINUX) && defined(SWIG)) 253#define GTEST_ATTRIBUTE_UNUSED 254#else 255#define GTEST_ATTRIBUTE_UNUSED __attribute__ ((unused)) 256#endif // GTEST_OS_WINDOWS || (GTEST_OS_LINUX && SWIG) 257 258// A macro to disallow the evil copy constructor and operator= functions 259// This should be used in the private: declarations for a class. 260#define GTEST_DISALLOW_COPY_AND_ASSIGN(type)\ 261 type(const type &);\ 262 void operator=(const type &) 263 264// Tell the compiler to warn about unused return values for functions declared 265// with this macro. The macro should be used on function declarations 266// following the argument list: 267// 268// Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT; 269#if defined(__GNUC__) \ 270 && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) \ 271 && !defined(COMPILER_ICC) 272#define GTEST_MUST_USE_RESULT __attribute__ ((warn_unused_result)) 273#else 274#define GTEST_MUST_USE_RESULT 275#endif // (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4) 276 277namespace testing { 278 279class Message; 280 281namespace internal { 282 283class String; 284 285// std::strstream is deprecated. However, we have to use it on 286// Windows as std::stringstream won't compile on Windows when 287// exceptions are disabled. We use std::stringstream on other 288// platforms to avoid compiler warnings there. 289#if GTEST_HAS_STD_STRING 290typedef ::std::stringstream StrStream; 291#else 292typedef ::std::strstream StrStream; 293#endif // GTEST_HAS_STD_STRING 294 295// Defines scoped_ptr. 296 297// This implementation of scoped_ptr is PARTIAL - it only contains 298// enough stuff to satisfy Google Test's need. 299template <typename T> 300class scoped_ptr { 301 public: 302 explicit scoped_ptr(T* p = NULL) : ptr_(p) {} 303 ~scoped_ptr() { reset(); } 304 305 T& operator*() const { return *ptr_; } 306 T* operator->() const { return ptr_; } 307 T* get() const { return ptr_; } 308 309 T* release() { 310 T* const ptr = ptr_; 311 ptr_ = NULL; 312 return ptr; 313 } 314 315 void reset(T* p = NULL) { 316 if (p != ptr_) { 317 if (sizeof(T) > 0) { // Makes sure T is a complete type. 318 delete ptr_; 319 } 320 ptr_ = p; 321 } 322 } 323 private: 324 T* ptr_; 325 326 GTEST_DISALLOW_COPY_AND_ASSIGN(scoped_ptr); 327}; 328 329#ifdef GTEST_HAS_DEATH_TEST 330 331// Defines RE. Currently only needed for death tests. 332 333// A simple C++ wrapper for <regex.h>. It uses the POSIX Enxtended 334// Regular Expression syntax. 335class RE { 336 public: 337 // Constructs an RE from a string. 338#if GTEST_HAS_STD_STRING 339 RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT 340#endif // GTEST_HAS_STD_STRING 341 342#if GTEST_HAS_GLOBAL_STRING 343 RE(const ::string& regex) { Init(regex.c_str()); } // NOLINT 344#endif // GTEST_HAS_GLOBAL_STRING 345 346 RE(const char* regex) { Init(regex); } // NOLINT 347 ~RE(); 348 349 // Returns the string representation of the regex. 350 const char* pattern() const { return pattern_; } 351 352 // Returns true iff str contains regular expression re. 353 354 // TODO(wan): make PartialMatch() work when str contains NUL 355 // characters. 356#if GTEST_HAS_STD_STRING 357 static bool PartialMatch(const ::std::string& str, const RE& re) { 358 return PartialMatch(str.c_str(), re); 359 } 360#endif // GTEST_HAS_STD_STRING 361 362#if GTEST_HAS_GLOBAL_STRING 363 static bool PartialMatch(const ::string& str, const RE& re) { 364 return PartialMatch(str.c_str(), re); 365 } 366#endif // GTEST_HAS_GLOBAL_STRING 367 368 static bool PartialMatch(const char* str, const RE& re); 369 370 private: 371 void Init(const char* regex); 372 373 // We use a const char* instead of a string, as Google Test may be used 374 // where string is not available. We also do not use Google Test's own 375 // String type here, in order to simplify dependencies between the 376 // files. 377 const char* pattern_; 378 regex_t regex_; 379 bool is_valid_; 380}; 381 382#endif // GTEST_HAS_DEATH_TEST 383 384// Defines logging utilities: 385// GTEST_LOG() - logs messages at the specified severity level. 386// LogToStderr() - directs all log messages to stderr. 387// FlushInfoLog() - flushes informational log messages. 388 389enum GTestLogSeverity { 390 GTEST_INFO, 391 GTEST_WARNING, 392 GTEST_ERROR, 393 GTEST_FATAL 394}; 395 396void GTestLog(GTestLogSeverity severity, const char* file, 397 int line, const char* msg); 398 399#define GTEST_LOG(severity, msg)\ 400 ::testing::internal::GTestLog(\ 401 ::testing::internal::GTEST_##severity, __FILE__, __LINE__, \ 402 (::testing::Message() << (msg)).GetString().c_str()) 403 404inline void LogToStderr() {} 405inline void FlushInfoLog() { fflush(NULL); } 406 407// Defines the stderr capturer: 408// CaptureStderr - starts capturing stderr. 409// GetCapturedStderr - stops capturing stderr and returns the captured string. 410 411#ifdef GTEST_HAS_DEATH_TEST 412 413// A copy of all command line arguments. Set by InitGoogleTest(). 414extern ::std::vector<String> g_argvs; 415 416void CaptureStderr(); 417// GTEST_HAS_DEATH_TEST implies we have ::std::string. 418::std::string GetCapturedStderr(); 419const ::std::vector<String>& GetArgvs(); 420 421#endif // GTEST_HAS_DEATH_TEST 422 423// Defines synchronization primitives. 424 425// A dummy implementation of synchronization primitives (mutex, lock, 426// and thread-local variable). Necessary for compiling Google Test where 427// mutex is not supported - using Google Test in multiple threads is not 428// supported on such platforms. 429 430class Mutex { 431 public: 432 Mutex() {} 433 explicit Mutex(int /*unused*/) {} 434 void AssertHeld() const {} 435 enum { NO_CONSTRUCTOR_NEEDED_FOR_STATIC_MUTEX = 0 }; 436}; 437 438// We cannot call it MutexLock directly as the ctor declaration would 439// conflict with a macro named MutexLock, which is defined on some 440// platforms. Hence the typedef trick below. 441class GTestMutexLock { 442 public: 443 explicit GTestMutexLock(Mutex*) {} // NOLINT 444}; 445 446typedef GTestMutexLock MutexLock; 447 448template <typename T> 449class ThreadLocal { 450 public: 451 T* pointer() { return &value_; } 452 const T* pointer() const { return &value_; } 453 const T& get() const { return value_; } 454 void set(const T& value) { value_ = value; } 455 private: 456 T value_; 457}; 458 459// There's no portable way to detect the number of threads, so we just 460// return 0 to indicate that we cannot detect it. 461inline size_t GetThreadCount() { return 0; } 462 463// Defines tr1::is_pointer (only needed for Symbian). 464 465#ifdef __SYMBIAN32__ 466 467// Symbian does not have tr1::type_traits, so we define our own is_pointer 468// These are needed as the Nokia Symbian Compiler cannot decide between 469// const T& and const T* in a function template. 470 471template <bool bool_value> 472struct bool_constant { 473 typedef bool_constant<bool_value> type; 474 static const bool value = bool_value; 475}; 476template <bool bool_value> const bool bool_constant<bool_value>::value; 477 478typedef bool_constant<false> false_type; 479typedef bool_constant<true> true_type; 480 481template <typename T> 482struct is_pointer : public false_type {}; 483 484template <typename T> 485struct is_pointer<T*> : public true_type {}; 486 487#endif // __SYMBIAN32__ 488 489// Defines BiggestInt as the biggest signed integer type the compiler 490// supports. 491 492#ifdef GTEST_OS_WINDOWS 493typedef __int64 BiggestInt; 494#else 495typedef long long BiggestInt; // NOLINT 496#endif // GTEST_OS_WINDOWS 497 498// The maximum number a BiggestInt can represent. This definition 499// works no matter BiggestInt is represented in one's complement or 500// two's complement. 501// 502// We cannot rely on numeric_limits in STL, as __int64 and long long 503// are not part of standard C++ and numeric_limits doesn't need to be 504// defined for them. 505const BiggestInt kMaxBiggestInt = 506 ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1)); 507 508// This template class serves as a compile-time function from size to 509// type. It maps a size in bytes to a primitive type with that 510// size. e.g. 511// 512// TypeWithSize<4>::UInt 513// 514// is typedef-ed to be unsigned int (unsigned integer made up of 4 515// bytes). 516// 517// Such functionality should belong to STL, but I cannot find it 518// there. 519// 520// Google Test uses this class in the implementation of floating-point 521// comparison. 522// 523// For now it only handles UInt (unsigned int) as that's all Google Test 524// needs. Other types can be easily added in the future if need 525// arises. 526template <size_t size> 527class TypeWithSize { 528 public: 529 // This prevents the user from using TypeWithSize<N> with incorrect 530 // values of N. 531 typedef void UInt; 532}; 533 534// The specialization for size 4. 535template <> 536class TypeWithSize<4> { 537 public: 538 // unsigned int has size 4 in both gcc and MSVC. 539 // 540 // As base/basictypes.h doesn't compile on Windows, we cannot use 541 // uint32, uint64, and etc here. 542 typedef int Int; 543 typedef unsigned int UInt; 544}; 545 546// The specialization for size 8. 547template <> 548class TypeWithSize<8> { 549 public: 550#ifdef GTEST_OS_WINDOWS 551 typedef __int64 Int; 552 typedef unsigned __int64 UInt; 553#else 554 typedef long long Int; // NOLINT 555 typedef unsigned long long UInt; // NOLINT 556#endif // GTEST_OS_WINDOWS 557}; 558 559// Integer types of known sizes. 560typedef TypeWithSize<4>::Int Int32; 561typedef TypeWithSize<4>::UInt UInt32; 562typedef TypeWithSize<8>::Int Int64; 563typedef TypeWithSize<8>::UInt UInt64; 564typedef TypeWithSize<8>::Int TimeInMillis; // Represents time in milliseconds. 565 566// Utilities for command line flags and environment variables. 567 568// A wrapper for getenv() that works on Linux, Windows, and Mac OS. 569inline const char* GetEnv(const char* name) { 570#ifdef _WIN32_WCE // We are on Windows CE. 571 // CE has no environment variables. 572 return NULL; 573#elif defined(GTEST_OS_WINDOWS) // We are on Windows proper. 574 // MSVC 8 deprecates getenv(), so we want to suppress warning 4996 575 // (deprecated function) there. 576#pragma warning(push) // Saves the current warning state. 577#pragma warning(disable:4996) // Temporarily disables warning 4996. 578 return getenv(name); 579#pragma warning(pop) // Restores the warning state. 580#else // We are on Linux or Mac OS. 581 return getenv(name); 582#endif 583} 584 585// Macro for referencing flags. 586#define GTEST_FLAG(name) FLAGS_gtest_##name 587 588// Macros for declaring flags. 589#define GTEST_DECLARE_bool(name) extern bool GTEST_FLAG(name) 590#define GTEST_DECLARE_int32(name) \ 591 extern ::testing::internal::Int32 GTEST_FLAG(name) 592#define GTEST_DECLARE_string(name) \ 593 extern ::testing::internal::String GTEST_FLAG(name) 594 595// Macros for defining flags. 596#define GTEST_DEFINE_bool(name, default_val, doc) \ 597 bool GTEST_FLAG(name) = (default_val) 598#define GTEST_DEFINE_int32(name, default_val, doc) \ 599 ::testing::internal::Int32 GTEST_FLAG(name) = (default_val) 600#define GTEST_DEFINE_string(name, default_val, doc) \ 601 ::testing::internal::String GTEST_FLAG(name) = (default_val) 602 603// Parses 'str' for a 32-bit signed integer. If successful, writes the result 604// to *value and returns true; otherwise leaves *value unchanged and returns 605// false. 606// TODO(chandlerc): Find a better way to refactor flag and environment parsing 607// out of both gtest-port.cc and gtest.cc to avoid exporting this utility 608// function. 609bool ParseInt32(const Message& src_text, const char* str, Int32* value); 610 611// Parses a bool/Int32/string from the environment variable 612// corresponding to the given Google Test flag. 613bool BoolFromGTestEnv(const char* flag, bool default_val); 614Int32 Int32FromGTestEnv(const char* flag, Int32 default_val); 615const char* StringFromGTestEnv(const char* flag, const char* default_val); 616 617} // namespace internal 618} // namespace testing 619 620#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ 621