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