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