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