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