webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
gtest-port.h
Go to the documentation of this file.
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_EXCEPTIONS - Define it to 1/0 to indicate that exceptions
46 // are enabled.
47 // GTEST_HAS_GLOBAL_STRING - Define it to 1/0 to indicate that ::string
48 // is/isn't available (some systems define
49 // ::string, which is different to std::string).
50 // GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::string
51 // is/isn't available (some systems define
52 // ::wstring, which is different to std::wstring).
53 // GTEST_HAS_PTHREAD - Define it to 1/0 to indicate that <pthread.h>
54 // is/isn't available.
55 // GTEST_HAS_RTTI - Define it to 1/0 to indicate that RTTI is/isn't
56 // enabled.
57 // GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that
58 // std::wstring does/doesn't work (Google Test can
59 // be used where std::wstring is unavailable).
60 // GTEST_HAS_TR1_TUPLE - Define it to 1/0 to indicate tr1::tuple
61 // is/isn't available.
62 // GTEST_HAS_SEH - Define it to 1/0 to indicate whether the
63 // compiler supports Microsoft's "Structured
64 // Exception Handling".
65 // GTEST_USE_OWN_TR1_TUPLE - Define it to 1/0 to indicate whether Google
66 // Test's own tr1 tuple implementation should be
67 // used. Unused when the user sets
68 // GTEST_HAS_TR1_TUPLE to 0.
69 // GTEST_LINKED_AS_SHARED_LIBRARY
70 // - Define to 1 when compiling tests that use
71 // Google Test as a shared library (known as
72 // DLL on Windows).
73 // GTEST_CREATE_SHARED_LIBRARY
74 // - Define to 1 when compiling Google Test itself
75 // as a shared library.
76 
77 // This header defines the following utilities:
78 //
79 // Macros indicating the current platform (defined to 1 if compiled on
80 // the given platform; otherwise undefined):
81 // GTEST_OS_AIX - IBM AIX
82 // GTEST_OS_CYGWIN - Cygwin
83 // GTEST_OS_LINUX - Linux
84 // GTEST_OS_FREEBSD - FreeBSD
85 // GTEST_OS_HURD - GNU/Hurd
86 // GTEST_OS_MAC - Mac OS X
87 // GTEST_OS_SOLARIS - Sun Solaris
88 // GTEST_OS_SYMBIAN - Symbian
89 // GTEST_OS_WINDOWS - Windows (Desktop, MinGW, or Mobile)
90 // GTEST_OS_WINDOWS_DESKTOP - Windows Desktop
91 // GTEST_OS_WINDOWS_MINGW - MinGW
92 // GTEST_OS_WINDOWS_MOBILE - Windows Mobile
93 // GTEST_OS_ZOS - z/OS
94 //
95 // Among the platforms, Cygwin, Linux, Max OS X, and Windows have the
96 // most stable support. Since core members of the Google Test project
97 // don't have access to other platforms, support for them may be less
98 // stable. If you notice any problems on your platform, please notify
99 // googletestframework@googlegroups.com (patches for fixing them are
100 // even more welcome!).
101 //
102 // Note that it is possible that none of the GTEST_OS_* macros are defined.
103 //
104 // Macros indicating available Google Test features (defined to 1 if
105 // the corresponding feature is supported; otherwise undefined):
106 // GTEST_HAS_COMBINE - the Combine() function (for value-parameterized
107 // tests)
108 // GTEST_HAS_DEATH_TEST - death tests
109 // GTEST_HAS_PARAM_TEST - value-parameterized tests
110 // GTEST_HAS_TYPED_TEST - typed tests
111 // GTEST_HAS_TYPED_TEST_P - type-parameterized tests
112 // GTEST_USES_POSIX_RE - enhanced POSIX regex is used.
113 // GTEST_USES_SIMPLE_RE - our own simple regex is used;
114 // the above two are mutually exclusive.
115 // GTEST_CAN_COMPARE_NULL - accepts untyped NULL in EXPECT_EQ().
116 //
117 // Macros for basic C++ coding:
118 // GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.
119 // GTEST_ATTRIBUTE_UNUSED_ - declares that a class' instances or a
120 // variable don't have to be used.
121 // GTEST_DISALLOW_ASSIGN_ - disables operator=.
122 // GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=.
123 // GTEST_MUST_USE_RESULT_ - declares that a function's result must be used.
124 //
125 // Synchronization:
126 // Mutex, MutexLock, ThreadLocal, GetThreadCount()
127 // - synchronization primitives.
128 // GTEST_IS_THREADSAFE - defined to 1 to indicate that the above
129 // synchronization primitives have real implementations
130 // and Google Test is thread-safe; or 0 otherwise.
131 //
132 // Template meta programming:
133 // is_pointer - as in TR1; needed on Symbian and IBM XL C/C++ only.
134 //
135 // Smart pointers:
136 // scoped_ptr - as in TR2.
137 //
138 // Regular expressions:
139 // RE - a simple regular expression class using the POSIX
140 // Extended Regular Expression syntax. Not available on
141 // Windows.
142 //
143 // Logging:
144 // GTEST_LOG_() - logs messages at the specified severity level.
145 // LogToStderr() - directs all log messages to stderr.
146 // FlushInfoLog() - flushes informational log messages.
147 //
148 // Stdout and stderr capturing:
149 // CaptureStdout() - starts capturing stdout.
150 // GetCapturedStdout() - stops capturing stdout and returns the captured
151 // string.
152 // CaptureStderr() - starts capturing stderr.
153 // GetCapturedStderr() - stops capturing stderr and returns the captured
154 // string.
155 //
156 // Integer types:
157 // TypeWithSize - maps an integer to a int type.
158 // Int32, UInt32, Int64, UInt64, TimeInMillis
159 // - integers of known sizes.
160 // BiggestInt - the biggest signed integer type.
161 //
162 // Command-line utilities:
163 // GTEST_FLAG() - references a flag.
164 // GTEST_DECLARE_*() - declares a flag.
165 // GTEST_DEFINE_*() - defines a flag.
166 // GetArgvs() - returns the command line as a vector of strings.
167 //
168 // Environment variable utilities:
169 // GetEnv() - gets the value of an environment variable.
170 // BoolFromGTestEnv() - parses a bool environment variable.
171 // Int32FromGTestEnv() - parses an Int32 environment variable.
172 // StringFromGTestEnv() - parses a string environment variable.
173 
174 #include <stddef.h> // For ptrdiff_t
175 #include <stdlib.h>
176 #include <stdio.h>
177 #include <string.h>
178 #ifndef _WIN32_WCE
179 #include <sys/stat.h>
180 #endif // !_WIN32_WCE
181 
182 #include <wtf/Platform.h>
183 #include <wtf/ExportMacros.h>
184 #include <runtime/JSExportMacros.h>
185 #include <wtf/Assertions.h>
186 #include <wtf/FastMalloc.h>
187 
188 #include <iostream> // NOLINT
189 #include <sstream> // NOLINT
190 #include <string> // NOLINT
191 
192 #define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
193 #define GTEST_FLAG_PREFIX_ "gtest_"
194 #define GTEST_FLAG_PREFIX_DASH_ "gtest-"
195 #define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
196 #define GTEST_NAME_ "Google Test"
197 #define GTEST_PROJECT_URL_ "http://code.google.com/p/googletest/"
198 
199 // Determines the version of gcc that is used to compile this.
200 #ifdef __GNUC__
201 // 40302 means version 4.3.2.
202 #define GTEST_GCC_VER_ \
203  (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
204 #endif // __GNUC__
205 
206 // Determines the platform on which Google Test is compiled.
207 #ifdef __CYGWIN__
208 #define GTEST_OS_CYGWIN 1
209 #elif defined __SYMBIAN32__
210 #define GTEST_OS_SYMBIAN 1
211 #elif defined _WIN32
212 #define GTEST_OS_WINDOWS 1
213 #ifdef _WIN32_WCE
214 #define GTEST_OS_WINDOWS_MOBILE 1
215 #elif defined(__MINGW__) || defined(__MINGW32__)
216 #define GTEST_OS_WINDOWS_MINGW 1
217 #else
218 #define GTEST_OS_WINDOWS_DESKTOP 1
219 #endif // _WIN32_WCE
220 #elif defined __APPLE__
221 #define GTEST_OS_MAC 1
222 #elif defined __linux__
223 #define GTEST_OS_LINUX 1
224 #elif defined __GNU__
225 #define GTEST_OS_HURD 1
226 #elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
227 #define GTEST_OS_FREEBSD 1
228 #elif defined __MVS__
229 #define GTEST_OS_ZOS 1
230 #elif defined(__sun) && defined(__SVR4)
231 #define GTEST_OS_SOLARIS 1
232 #elif defined(_AIX)
233 #define GTEST_OS_AIX 1
234 #endif // __CYGWIN__
235 
236 #if GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_SYMBIAN || \
237  GTEST_OS_SOLARIS || GTEST_OS_AIX || GTEST_OS_FREEBSD || GTEST_OS_HURD
238 
239 // On some platforms, <regex.h> needs someone to define size_t, and
240 // won't compile otherwise. We can #include it here as we already
241 // included <stdlib.h>, which is guaranteed to define size_t through
242 // <stddef.h>.
243 #include <regex.h> // NOLINT
244 #include <strings.h> // NOLINT
245 #include <sys/types.h> // NOLINT
246 #include <time.h> // NOLINT
247 #include <unistd.h> // NOLINT
248 
249 #define GTEST_USES_POSIX_RE 1
250 
251 #elif GTEST_OS_WINDOWS
252 
253 #if !GTEST_OS_WINDOWS_MOBILE
254 #include <direct.h> // NOLINT
255 #include <io.h> // NOLINT
256 #endif
257 
258 // <regex.h> is not available on Windows. Use our own simple regex
259 // implementation instead.
260 #define GTEST_USES_SIMPLE_RE 1
261 
262 #else
263 
264 // <regex.h> may not be available on this platform. Use our own
265 // simple regex implementation instead.
266 #define GTEST_USES_SIMPLE_RE 1
267 
268 #endif // GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC ||
269  // GTEST_OS_SYMBIAN || GTEST_OS_SOLARIS || GTEST_OS_AIX ||
270  // GTEST_OS_FREEBSD || GTEST_OS_HURD
271 
272 #ifndef GTEST_HAS_EXCEPTIONS
273 // The user didn't tell us whether exceptions are enabled, so we need
274 // to figure it out.
275 #if defined(_MSC_VER) || defined(__BORLANDC__)
276 // MSVC's and C++Builder's implementations of the STL use the _HAS_EXCEPTIONS
277 // macro to enable exceptions, so we'll do the same.
278 // Assumes that exceptions are enabled by default.
279 #ifndef _HAS_EXCEPTIONS
280 #define _HAS_EXCEPTIONS 1
281 #endif // _HAS_EXCEPTIONS
282 #define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
283 #elif defined(__GNUC__) && __EXCEPTIONS
284 // gcc defines __EXCEPTIONS to 1 iff exceptions are enabled.
285 #define GTEST_HAS_EXCEPTIONS 1
286 #elif defined(__SUNPRO_CC)
287 // Sun Pro CC supports exceptions. However, there is no compile-time way of
288 // detecting whether they are enabled or not. Therefore, we assume that
289 // they are enabled unless the user tells us otherwise.
290 #define GTEST_HAS_EXCEPTIONS 1
291 #elif defined(__IBMCPP__) && __EXCEPTIONS
292 // xlC defines __EXCEPTIONS to 1 iff exceptions are enabled.
293 #define GTEST_HAS_EXCEPTIONS 1
294 #else
295 // For other compilers, we assume exceptions are disabled to be
296 // conservative.
297 #define GTEST_HAS_EXCEPTIONS 0
298 #endif // defined(_MSC_VER) || defined(__BORLANDC__)
299 #endif // GTEST_HAS_EXCEPTIONS
300 
301 #if !defined(GTEST_HAS_STD_STRING)
302 // Even though we don't use this macro any longer, we keep it in case
303 // some clients still depend on it.
304 #define GTEST_HAS_STD_STRING 1
305 #elif !GTEST_HAS_STD_STRING
306 // The user told us that ::std::string isn't available.
307 #error "Google Test cannot be used where ::std::string isn't available."
308 #endif // !defined(GTEST_HAS_STD_STRING)
309 
310 #ifndef GTEST_HAS_GLOBAL_STRING
311 // The user didn't tell us whether ::string is available, so we need
312 // to figure it out.
313 
314 #define GTEST_HAS_GLOBAL_STRING 0
315 
316 #endif // GTEST_HAS_GLOBAL_STRING
317 
318 #ifndef GTEST_HAS_STD_WSTRING
319 // The user didn't tell us whether ::std::wstring is available, so we need
320 // to figure it out.
321 // TODO(wan@google.com): uses autoconf to detect whether ::std::wstring
322 // is available.
323 
324 // Cygwin 1.5 and below doesn't support ::std::wstring.
325 // Cygwin 1.7 might add wstring support; this should be updated when clear.
326 // Solaris' libc++ doesn't support it either.
327 #define GTEST_HAS_STD_WSTRING (!(GTEST_OS_CYGWIN || GTEST_OS_SOLARIS))
328 
329 #endif // GTEST_HAS_STD_WSTRING
330 
331 #ifndef GTEST_HAS_GLOBAL_WSTRING
332 // The user didn't tell us whether ::wstring is available, so we need
333 // to figure it out.
334 #define GTEST_HAS_GLOBAL_WSTRING \
335  (GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING)
336 #endif // GTEST_HAS_GLOBAL_WSTRING
337 
338 // Determines whether RTTI is available.
339 #ifndef GTEST_HAS_RTTI
340 // The user didn't tell us whether RTTI is enabled, so we need to
341 // figure it out.
342 
343 #ifdef _MSC_VER
344 
345 #ifdef _CPPRTTI // MSVC defines this macro iff RTTI is enabled.
346 #define GTEST_HAS_RTTI 1
347 #else
348 #define GTEST_HAS_RTTI 0
349 #endif
350 
351 // Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled.
352 #elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40302)
353 
354 #ifdef __GXX_RTTI
355 #define GTEST_HAS_RTTI 1
356 #else
357 #define GTEST_HAS_RTTI 0
358 #endif // __GXX_RTTI
359 
360 // Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if
361 // both the typeid and dynamic_cast features are present.
362 #elif defined(__IBMCPP__) && (__IBMCPP__ >= 900)
363 
364 #ifdef __RTTI_ALL__
365 #define GTEST_HAS_RTTI 1
366 #else
367 #define GTEST_HAS_RTTI 0
368 #endif
369 
370 #else
371 
372 // For all other compilers, we assume RTTI is enabled.
373 #define GTEST_HAS_RTTI 1
374 
375 #endif // _MSC_VER
376 
377 #endif // GTEST_HAS_RTTI
378 
379 // It's this header's responsibility to #include <typeinfo> when RTTI
380 // is enabled.
381 #if GTEST_HAS_RTTI
382 #include <typeinfo>
383 #endif
384 
385 // Determines whether Google Test can use the pthreads library.
386 #ifndef GTEST_HAS_PTHREAD
387 // The user didn't tell us explicitly, so we assume pthreads support is
388 // available on Linux and Mac.
389 //
390 // To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0
391 // to your compiler flags.
392 #define GTEST_HAS_PTHREAD (GTEST_OS_LINUX || GTEST_OS_FREEBSD || GTEST_OS_HURD || GTEST_OS_MAC)
393 #endif // GTEST_HAS_PTHREAD
394 
395 // Determines whether Google Test can use tr1/tuple. You can define
396 // this macro to 0 to prevent Google Test from using tuple (any
397 // feature depending on tuple with be disabled in this mode).
398 #ifndef GTEST_HAS_TR1_TUPLE
399 // The user didn't tell us not to do it, so we assume it's OK.
400 #define GTEST_HAS_TR1_TUPLE 1
401 #endif // GTEST_HAS_TR1_TUPLE
402 
403 // Determines whether Google Test's own tr1 tuple implementation
404 // should be used.
405 #ifndef GTEST_USE_OWN_TR1_TUPLE
406 // The user didn't tell us, so we need to figure it out.
407 
408 // We use our own TR1 tuple if we aren't sure the user has an
409 // implementation of it already. At this time, GCC 4.0.0+ and MSVC
410 // 2010 are the only mainstream compilers that come with a TR1 tuple
411 // implementation. NVIDIA's CUDA NVCC compiler pretends to be GCC by
412 // defining __GNUC__ and friends, but cannot compile GCC's tuple
413 // implementation. MSVC 2008 (9.0) provides TR1 tuple in a 323 MB
414 // Feature Pack download, which we cannot assume the user has.
415 #if (defined(__GNUC__) && !defined(__CUDACC__) && (GTEST_GCC_VER_ >= 40000)) \
416  || _MSC_VER >= 1600
417 #define GTEST_USE_OWN_TR1_TUPLE 0
418 #else
419 #define GTEST_USE_OWN_TR1_TUPLE 1
420 #endif
421 
422 #endif // GTEST_USE_OWN_TR1_TUPLE
423 
424 // To avoid conditional compilation everywhere, we make it
425 // gtest-port.h's responsibility to #include the header implementing
426 // tr1/tuple.
427 #if GTEST_HAS_TR1_TUPLE
428 
429 #if GTEST_USE_OWN_TR1_TUPLE
431 #elif GTEST_OS_SYMBIAN
432 
433 // On Symbian, BOOST_HAS_TR1_TUPLE causes Boost's TR1 tuple library to
434 // use STLport's tuple implementation, which unfortunately doesn't
435 // work as the copy of STLport distributed with Symbian is incomplete.
436 // By making sure BOOST_HAS_TR1_TUPLE is undefined, we force Boost to
437 // use its own tuple implementation.
438 #ifdef BOOST_HAS_TR1_TUPLE
439 #undef BOOST_HAS_TR1_TUPLE
440 #endif // BOOST_HAS_TR1_TUPLE
441 
442 // This prevents <boost/tr1/detail/config.hpp>, which defines
443 // BOOST_HAS_TR1_TUPLE, from being #included by Boost's <tuple>.
444 #define BOOST_TR1_DETAIL_CONFIG_HPP_INCLUDED
445 #include <tuple>
446 
447 #elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000)
448 // GCC 4.0+ implements tr1/tuple in the <tr1/tuple> header. This does
449 // not conform to the TR1 spec, which requires the header to be <tuple>.
450 
451 #if !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
452 // Until version 4.3.2, gcc has a bug that causes <tr1/functional>,
453 // which is #included by <tr1/tuple>, to not compile when RTTI is
454 // disabled. _TR1_FUNCTIONAL is the header guard for
455 // <tr1/functional>. Hence the following #define is a hack to prevent
456 // <tr1/functional> from being included.
457 #define _TR1_FUNCTIONAL 1
458 #include <tr1/tuple>
459 #undef _TR1_FUNCTIONAL // Allows the user to #include
460  // <tr1/functional> if he chooses to.
461 #else
462 #include <tr1/tuple> // NOLINT
463 #endif // !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
464 
465 #else
466 // If the compiler is not GCC 4.0+, we assume the user is using a
467 // spec-conforming TR1 implementation.
468 #include <tuple> // NOLINT
469 #endif // GTEST_USE_OWN_TR1_TUPLE
470 
471 #endif // GTEST_HAS_TR1_TUPLE
472 
473 // Determines whether clone(2) is supported.
474 // Usually it will only be available on Linux, excluding
475 // Linux on the Itanium architecture.
476 // Also see http://linux.die.net/man/2/clone.
477 #ifndef GTEST_HAS_CLONE
478 // The user didn't tell us, so we need to figure it out.
479 
480 #if GTEST_OS_LINUX && !defined(__ia64__)
481 #define GTEST_HAS_CLONE 1
482 #else
483 #define GTEST_HAS_CLONE 0
484 #endif // GTEST_OS_LINUX && !defined(__ia64__)
485 
486 #endif // GTEST_HAS_CLONE
487 
488 // Determines whether to support stream redirection. This is used to test
489 // output correctness and to implement death tests.
490 #if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN
491 #define GTEST_HAS_STREAM_REDIRECTION_ 1
492 #endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN
493 
494 // Determines whether to support death tests.
495 // Google Test does not support death tests for VC 7.1 and earlier as
496 // abort() in a VC 7.1 application compiled as GUI in debug config
497 // pops up a dialog window that cannot be suppressed programmatically.
498 #if (GTEST_OS_LINUX || GTEST_OS_FREEBSD || GTEST_OS_HURD || \
499  GTEST_OS_MAC || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \
500  (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER >= 1400) || \
501  GTEST_OS_WINDOWS_MINGW || GTEST_OS_AIX)
502 #define GTEST_HAS_DEATH_TEST 1
503 #include <vector> // NOLINT
504 #endif
505 
506 // We don't support MSVC 7.1 with exceptions disabled now. Therefore
507 // all the compilers we care about are adequate for supporting
508 // value-parameterized tests.
509 #define GTEST_HAS_PARAM_TEST 1
510 
511 // Determines whether to support type-driven tests.
512 
513 // Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0,
514 // Sun Pro CC, and IBM Visual Age support.
515 #if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__SUNPRO_CC) || \
516  defined(__IBMCPP__)
517 #define GTEST_HAS_TYPED_TEST 1
518 #define GTEST_HAS_TYPED_TEST_P 1
519 #endif
520 
521 // Determines whether to support Combine(). This only makes sense when
522 // value-parameterized tests are enabled. The implementation doesn't
523 // work on Sun Studio since it doesn't understand templated conversion
524 // operators.
525 #if GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE && !defined(__SUNPRO_CC)
526 #define GTEST_HAS_COMBINE 1
527 #endif
528 
529 // Determines whether the system compiler uses UTF-16 for encoding wide strings.
530 #define GTEST_WIDE_STRING_USES_UTF16_ \
531  (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN || GTEST_OS_AIX)
532 
533 // Defines some utility macros.
534 
535 // The GNU compiler emits a warning if nested "if" statements are followed by
536 // an "else" statement and braces are not used to explicitly disambiguate the
537 // "else" binding. This leads to problems with code like:
538 //
539 // if (gate)
540 // ASSERT_*(condition) << "Some message";
541 //
542 // The "switch (0) case 0:" idiom is used to suppress this.
543 #ifdef __INTEL_COMPILER
544 #define GTEST_AMBIGUOUS_ELSE_BLOCKER_
545 #else
546 #define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: // NOLINT
547 #endif
548 
549 // Use this annotation at the end of a struct/class definition to
550 // prevent the compiler from optimizing away instances that are never
551 // used. This is useful when all interesting logic happens inside the
552 // c'tor and / or d'tor. Example:
553 //
554 // struct Foo {
555 // Foo() { ... }
556 // } GTEST_ATTRIBUTE_UNUSED_;
557 //
558 // Also use it after a variable or parameter declaration to tell the
559 // compiler the variable/parameter does not have to be used.
560 #if defined(__GNUC__) && !defined(COMPILER_ICC)
561 #define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
562 #else
563 #define GTEST_ATTRIBUTE_UNUSED_
564 #endif
565 
566 // A macro to disallow operator=
567 // This should be used in the private: declarations for a class.
568 #define GTEST_DISALLOW_ASSIGN_(type)\
569  void operator=(type const &)
570 
571 // A macro to disallow copy constructor and operator=
572 // This should be used in the private: declarations for a class.
573 #define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\
574  type(type const &);\
575  GTEST_DISALLOW_ASSIGN_(type)
576 
577 // Tell the compiler to warn about unused return values for functions declared
578 // with this macro. The macro should be used on function declarations
579 // following the argument list:
580 //
581 // Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_;
582 #if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC)
583 #define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))
584 #else
585 #define GTEST_MUST_USE_RESULT_
586 #endif // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC
587 
588 // Determine whether the compiler supports Microsoft's Structured Exception
589 // Handling. This is supported by several Windows compilers but generally
590 // does not exist on any other system.
591 #ifndef GTEST_HAS_SEH
592 // The user didn't tell us, so we need to figure it out.
593 
594 #if defined(_MSC_VER) || defined(__BORLANDC__)
595 // These two compilers are known to support SEH.
596 #define GTEST_HAS_SEH 1
597 #else
598 // Assume no SEH.
599 #define GTEST_HAS_SEH 0
600 #endif
601 
602 #endif // GTEST_HAS_SEH
603 
604 #ifdef _MSC_VER
605 
606 #if GTEST_LINKED_AS_SHARED_LIBRARY
607 #define GTEST_API_ __declspec(dllimport)
608 #elif GTEST_CREATE_SHARED_LIBRARY
609 #define GTEST_API_ __declspec(dllexport)
610 #endif
611 
612 #endif // _MSC_VER
613 
614 #ifndef GTEST_API_
615 #define GTEST_API_
616 #endif
617 
618 namespace testing {
619 
620 class Message;
621 
622 namespace internal {
623 
624 class String;
625 
626 class StrStream : public ::std::stringstream {
627  public:
628  void* operator new(size_t, void* p) { return p; }
629  void* operator new[](size_t, void* p) { return p; }
630 
631  void* operator new(size_t size) {
632  return malloc(size);
633  }
634 
635  void operator delete(void* p) {
636  free(p);
637  }
638 
639  void* operator new[](size_t size) {
640  return malloc(size);
641  }
642 
643  void operator delete[](void* p) {
644  free(p);
645  }
646 };
647 
648 
649 
650 // A helper for suppressing warnings on constant condition. It just
651 // returns 'condition'.
652 GTEST_API_ bool IsTrue(bool condition);
653 
654 // Defines scoped_ptr.
655 
656 // This implementation of scoped_ptr is PARTIAL - it only contains
657 // enough stuff to satisfy Google Test's need.
658 template <typename T>
659 class scoped_ptr {
660  public:
661  typedef T element_type;
662 
663  explicit scoped_ptr(T* p = NULL) : ptr_(p) {}
665 
666  T& operator*() const { return *ptr_; }
667  T* operator->() const { return ptr_; }
668  T* get() const { return ptr_; }
669 
670  T* release() {
671  T* const ptr = ptr_;
672  ptr_ = NULL;
673  return ptr;
674  }
675 
676  void reset(T* p = NULL) {
677  if (p != ptr_) {
678  if (IsTrue(sizeof(T) > 0)) { // Makes sure T is a complete type.
679  delete ptr_;
680  }
681  ptr_ = p;
682  }
683  }
684  private:
685  T* ptr_;
686 
688 };
689 
690 // Defines RE.
691 
692 // A simple C++ wrapper for <regex.h>. It uses the POSIX Extended
693 // Regular Expression syntax.
694 class GTEST_API_ RE {
695  public:
696  // A copy constructor is required by the Standard to initialize object
697  // references from r-values.
698  RE(const RE& other) { Init(other.pattern()); }
699 
700  // Constructs an RE from a string.
701  RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT
702 
703 #if GTEST_HAS_GLOBAL_STRING
704  RE(const ::string& regex) { Init(regex.c_str()); } // NOLINT
705 #endif // GTEST_HAS_GLOBAL_STRING
706 
707  RE(const char* regex) { Init(regex); } // NOLINT
708  ~RE();
709 
710  // Returns the string representation of the regex.
711  const char* pattern() const { return pattern_; }
712 
713  // FullMatch(str, re) returns true iff regular expression re matches
714  // the entire str.
715  // PartialMatch(str, re) returns true iff regular expression re
716  // matches a substring of str (including str itself).
717  //
718  // TODO(wan@google.com): make FullMatch() and PartialMatch() work
719  // when str contains NUL characters.
720  static bool FullMatch(const ::std::string& str, const RE& re) {
721  return FullMatch(str.c_str(), re);
722  }
723  static bool PartialMatch(const ::std::string& str, const RE& re) {
724  return PartialMatch(str.c_str(), re);
725  }
726 
727 #if GTEST_HAS_GLOBAL_STRING
728  static bool FullMatch(const ::string& str, const RE& re) {
729  return FullMatch(str.c_str(), re);
730  }
731  static bool PartialMatch(const ::string& str, const RE& re) {
732  return PartialMatch(str.c_str(), re);
733  }
734 #endif // GTEST_HAS_GLOBAL_STRING
735 
736  static bool FullMatch(const char* str, const RE& re);
737  static bool PartialMatch(const char* str, const RE& re);
738 
739  private:
740  void Init(const char* regex);
741 
742  // We use a const char* instead of a string, as Google Test may be used
743  // where string is not available. We also do not use Google Test's own
744  // String type here, in order to simplify dependencies between the
745  // files.
746  const char* pattern_;
747  bool is_valid_;
748 #if GTEST_USES_POSIX_RE
749  regex_t full_regex_; // For FullMatch().
750  regex_t partial_regex_; // For PartialMatch().
751 #else // GTEST_USES_SIMPLE_RE
752  const char* full_pattern_; // For FullMatch();
753 #endif
754 
756 };
757 
758 // Defines logging utilities:
759 // GTEST_LOG_(severity) - logs messages at the specified severity level. The
760 // message itself is streamed into the macro.
761 // LogToStderr() - directs all log messages to stderr.
762 // FlushInfoLog() - flushes informational log messages.
763 
769 };
770 
771 // Formats log entry severity, provides a stream object for streaming the
772 // log message, and terminates the message with a newline when going out of
773 // scope.
775  public:
776  GTestLog(GTestLogSeverity severity, const char* file, int line);
777 
778  // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
779  ~GTestLog();
780 
781  ::std::ostream& GetStream() { return ::std::cerr; }
782 
783  private:
784  const GTestLogSeverity severity_;
785 
787 };
788 
789 #define GTEST_LOG_(severity) \
790  ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
791  __FILE__, __LINE__).GetStream()
792 
793 inline void LogToStderr() {}
794 inline void FlushInfoLog() { fflush(NULL); }
795 
796 // INTERNAL IMPLEMENTATION - DO NOT USE.
797 //
798 // GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition
799 // is not satisfied.
800 // Synopsys:
801 // GTEST_CHECK_(boolean_condition);
802 // or
803 // GTEST_CHECK_(boolean_condition) << "Additional message";
804 //
805 // This checks the condition and if the condition is not satisfied
806 // it prints message about the condition violation, including the
807 // condition itself, plus additional message streamed into it, if any,
808 // and then it aborts the program. It aborts the program irrespective of
809 // whether it is built in the debug mode or not.
810 #define GTEST_CHECK_(condition) \
811  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
812  if (::testing::internal::IsTrue(condition)) \
813  ; \
814  else \
815  GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
816 
817 // An all-mode assert to verify that the given POSIX-style function
818 // call returns 0 (indicating success). Known limitation: this
819 // doesn't expand to a balanced 'if' statement, so enclose the macro
820 // in {} if you need to use it as the only statement in an 'if'
821 // branch.
822 #define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \
823  if (const int gtest_error = (posix_call)) \
824  GTEST_LOG_(FATAL) << #posix_call << "failed with error " \
825  << gtest_error
826 
827 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
828 //
829 // Downcasts the pointer of type Base to Derived.
830 // Derived must be a subclass of Base. The parameter MUST
831 // point to a class of type Derived, not any subclass of it.
832 // When RTTI is available, the function performs a runtime
833 // check to enforce this.
834 template <class Derived, class Base>
836 #if GTEST_HAS_RTTI
837  GTEST_CHECK_(typeid(*base) == typeid(Derived));
838  return dynamic_cast<Derived*>(base); // NOLINT
839 #else
840  return static_cast<Derived*>(base); // Poor man's downcast.
841 #endif
842 }
843 
844 #if GTEST_HAS_STREAM_REDIRECTION_
845 
846 // Defines the stderr capturer:
847 // CaptureStdout - starts capturing stdout.
848 // GetCapturedStdout - stops capturing stdout and returns the captured string.
849 // CaptureStderr - starts capturing stderr.
850 // GetCapturedStderr - stops capturing stderr and returns the captured string.
851 //
856 
857 #endif // GTEST_HAS_STREAM_REDIRECTION_
858 
859 
860 #if GTEST_HAS_DEATH_TEST
861 
862 // A copy of all command line arguments. Set by InitGoogleTest().
863 extern ::std::vector<String> g_argvs;
864 
865 // GTEST_HAS_DEATH_TEST implies we have ::std::string.
866 const ::std::vector<String>& GetArgvs();
867 
868 #endif // GTEST_HAS_DEATH_TEST
869 
870 // Defines synchronization primitives.
871 
872 #if GTEST_HAS_PTHREAD
873 
874 // Sleeps for (roughly) n milli-seconds. This function is only for
875 // testing Google Test's own constructs. Don't use it in user tests,
876 // either directly or indirectly.
877 inline void SleepMilliseconds(int n) {
878  const timespec time = {
879  0, // 0 seconds.
880  n * 1000L * 1000L, // And n ms.
881  };
882  nanosleep(&time, NULL);
883 }
884 
885 // Allows a controller thread to pause execution of newly created
886 // threads until notified. Instances of this class must be created
887 // and destroyed in the controller thread.
888 //
889 // This class is only for testing Google Test's own constructs. Do not
890 // use it in user tests, either directly or indirectly.
891 class Notification {
892  public:
893  Notification() : notified_(false) {}
894 
895  // Notifies all threads created with this notification to start. Must
896  // be called from the controller thread.
897  void Notify() { notified_ = true; }
898 
899  // Blocks until the controller thread notifies. Must be called from a test
900  // thread.
901  void WaitForNotification() {
902  while(!notified_) {
903  SleepMilliseconds(10);
904  }
905  }
906 
907  private:
908  volatile bool notified_;
909 
911 };
912 
913 // As a C-function, ThreadFuncWithCLinkage cannot be templated itself.
914 // Consequently, it cannot select a correct instantiation of ThreadWithParam
915 // in order to call its Run(). Introducing ThreadWithParamBase as a
916 // non-templated base class for ThreadWithParam allows us to bypass this
917 // problem.
918 class ThreadWithParamBase {
919  public:
920  virtual ~ThreadWithParamBase() {}
921  virtual void Run() = 0;
922 };
923 
924 // pthread_create() accepts a pointer to a function type with the C linkage.
925 // According to the Standard (7.5/1), function types with different linkages
926 // are different even if they are otherwise identical. Some compilers (for
927 // example, SunStudio) treat them as different types. Since class methods
928 // cannot be defined with C-linkage we need to define a free C-function to
929 // pass into pthread_create().
930 extern "C" inline void* ThreadFuncWithCLinkage(void* thread) {
931  static_cast<ThreadWithParamBase*>(thread)->Run();
932  return NULL;
933 }
934 
935 // Helper class for testing Google Test's multi-threading constructs.
936 // To use it, write:
937 //
938 // void ThreadFunc(int param) { /* Do things with param */ }
939 // Notification thread_can_start;
940 // ...
941 // // The thread_can_start parameter is optional; you can supply NULL.
942 // ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start);
943 // thread_can_start.Notify();
944 //
945 // These classes are only for testing Google Test's own constructs. Do
946 // not use them in user tests, either directly or indirectly.
947 template <typename T>
948 class ThreadWithParam : public ThreadWithParamBase {
949  public:
950  typedef void (*UserThreadFunc)(T);
951 
952  ThreadWithParam(
953  UserThreadFunc func, T param, Notification* thread_can_start)
954  : func_(func),
955  param_(param),
956  thread_can_start_(thread_can_start),
957  finished_(false) {
958  ThreadWithParamBase* const base = this;
959  // The thread can be created only after all fields except thread_
960  // have been initialized.
962  pthread_create(&thread_, 0, &ThreadFuncWithCLinkage, base));
963  }
964  ~ThreadWithParam() { Join(); }
965 
966  void Join() {
967  if (!finished_) {
968  GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, 0));
969  finished_ = true;
970  }
971  }
972 
973  virtual void Run() {
974  if (thread_can_start_ != NULL)
975  thread_can_start_->WaitForNotification();
976  func_(param_);
977  }
978 
979  private:
980  const UserThreadFunc func_; // User-supplied thread function.
981  const T param_; // User-supplied parameter to the thread function.
982  // When non-NULL, used to block execution until the controller thread
983  // notifies.
984  Notification* const thread_can_start_;
985  bool finished_; // true iff we know that the thread function has finished.
986  pthread_t thread_; // The native thread object.
987 
988  GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam);
989 };
990 
991 // gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is
992 // true.
993 #include <pthread.h>
994 
995 // MutexBase and Mutex implement mutex on pthreads-based platforms. They
996 // are used in conjunction with class MutexLock:
997 //
998 // Mutex mutex;
999 // ...
1000 // MutexLock lock(&mutex); // Acquires the mutex and releases it at the end
1001 // // of the current scope.
1002 //
1003 // MutexBase implements behavior for both statically and dynamically
1004 // allocated mutexes. Do not use MutexBase directly. Instead, write
1005 // the following to define a static mutex:
1006 //
1007 // GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex);
1008 //
1009 // You can forward declare a static mutex like this:
1010 //
1011 // GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex);
1012 //
1013 // To create a dynamic mutex, just define an object of type Mutex.
1014 class MutexBase {
1015  public:
1016  // Acquires this mutex.
1017  void Lock() {
1018  GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_));
1019  owner_ = pthread_self();
1020  }
1021 
1022  // Releases this mutex.
1023  void Unlock() {
1024  // We don't protect writing to owner_ here, as it's the caller's
1025  // responsibility to ensure that the current thread holds the
1026  // mutex when this is called.
1027  owner_ = 0;
1028  GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_));
1029  }
1030 
1031  // Does nothing if the current thread holds the mutex. Otherwise, crashes
1032  // with high probability.
1033  void AssertHeld() const {
1034  GTEST_CHECK_(owner_ == pthread_self())
1035  << "The current thread is not holding the mutex @" << this;
1036  }
1037 
1038  // A static mutex may be used before main() is entered. It may even
1039  // be used before the dynamic initialization stage. Therefore we
1040  // must be able to initialize a static mutex object at link time.
1041  // This means MutexBase has to be a POD and its member variables
1042  // have to be public.
1043  public:
1044  pthread_mutex_t mutex_; // The underlying pthread mutex.
1045  pthread_t owner_; // The thread holding the mutex; 0 means no one holds it.
1046 };
1047 
1048 // Forward-declares a static mutex.
1049 #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1050  extern ::testing::internal::MutexBase mutex
1051 
1052 // Defines and statically (i.e. at link time) initializes a static mutex.
1053 #define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1054  ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, 0 }
1055 
1056 // The Mutex class can only be used for mutexes created at runtime. It
1057 // shares its API with MutexBase otherwise.
1058 class Mutex : public MutexBase {
1059  public:
1060  Mutex() {
1061  GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));
1062  owner_ = 0;
1063  }
1064  ~Mutex() {
1065  GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_));
1066  }
1067 
1068  private:
1070 };
1071 
1072 // We cannot name this class MutexLock as the ctor declaration would
1073 // conflict with a macro named MutexLock, which is defined on some
1074 // platforms. Hence the typedef trick below.
1075 class GTestMutexLock {
1076  public:
1077  explicit GTestMutexLock(MutexBase* mutex)
1078  : mutex_(mutex) { mutex_->Lock(); }
1079 
1080  ~GTestMutexLock() { mutex_->Unlock(); }
1081 
1082  private:
1083  MutexBase* const mutex_;
1084 
1086 };
1087 
1088 typedef GTestMutexLock MutexLock;
1089 
1090 // Helpers for ThreadLocal.
1091 
1092 // pthread_key_create() requires DeleteThreadLocalValue() to have
1093 // C-linkage. Therefore it cannot be templatized to access
1094 // ThreadLocal<T>. Hence the need for class
1095 // ThreadLocalValueHolderBase.
1096 class ThreadLocalValueHolderBase {
1097  public:
1098  virtual ~ThreadLocalValueHolderBase() {}
1099 };
1100 
1101 // Called by pthread to delete thread-local data stored by
1102 // pthread_setspecific().
1103 extern "C" inline void DeleteThreadLocalValue(void* value_holder) {
1104  delete static_cast<ThreadLocalValueHolderBase*>(value_holder);
1105 }
1106 
1107 // Implements thread-local storage on pthreads-based systems.
1108 //
1109 // // Thread 1
1110 // ThreadLocal<int> tl(100); // 100 is the default value for each thread.
1111 //
1112 // // Thread 2
1113 // tl.set(150); // Changes the value for thread 2 only.
1114 // EXPECT_EQ(150, tl.get());
1115 //
1116 // // Thread 1
1117 // EXPECT_EQ(100, tl.get()); // In thread 1, tl has the original value.
1118 // tl.set(200);
1119 // EXPECT_EQ(200, tl.get());
1120 //
1121 // The template type argument T must have a public copy constructor.
1122 // In addition, the default ThreadLocal constructor requires T to have
1123 // a public default constructor.
1124 //
1125 // An object managed for a thread by a ThreadLocal instance is deleted
1126 // when the thread exits. Or, if the ThreadLocal instance dies in
1127 // that thread, when the ThreadLocal dies. It's the user's
1128 // responsibility to ensure that all other threads using a ThreadLocal
1129 // have exited when it dies, or the per-thread objects for those
1130 // threads will not be deleted.
1131 //
1132 // Google Test only uses global ThreadLocal objects. That means they
1133 // will die after main() has returned. Therefore, no per-thread
1134 // object managed by Google Test will be leaked as long as all threads
1135 // using Google Test have exited when main() returns.
1136 template <typename T>
1137 class ThreadLocal {
1138  public:
1139  ThreadLocal() : key_(CreateKey()),
1140  default_() {}
1141  explicit ThreadLocal(const T& value) : key_(CreateKey()),
1142  default_(value) {}
1143 
1144  ~ThreadLocal() {
1145  // Destroys the managed object for the current thread, if any.
1146  DeleteThreadLocalValue(pthread_getspecific(key_));
1147 
1148  // Releases resources associated with the key. This will *not*
1149  // delete managed objects for other threads.
1150  GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_));
1151  }
1152 
1153  T* pointer() { return GetOrCreateValue(); }
1154  const T* pointer() const { return GetOrCreateValue(); }
1155  const T& get() const { return *pointer(); }
1156  void set(const T& value) { *pointer() = value; }
1157 
1158  private:
1159  // Holds a value of type T.
1160  class ValueHolder : public ThreadLocalValueHolderBase {
1161  public:
1162  explicit ValueHolder(const T& value) : value_(value) {}
1163 
1164  T* pointer() { return &value_; }
1165 
1166  private:
1167  T value_;
1168  GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder);
1169  };
1170 
1171  static pthread_key_t CreateKey() {
1172  pthread_key_t key;
1173  // When a thread exits, DeleteThreadLocalValue() will be called on
1174  // the object managed for that thread.
1176  pthread_key_create(&key, &DeleteThreadLocalValue));
1177  return key;
1178  }
1179 
1180  T* GetOrCreateValue() const {
1181  ThreadLocalValueHolderBase* const holder =
1182  static_cast<ThreadLocalValueHolderBase*>(pthread_getspecific(key_));
1183  if (holder != NULL) {
1184  return CheckedDowncastToActualType<ValueHolder>(holder)->pointer();
1185  }
1186 
1187  ValueHolder* const new_holder = new ValueHolder(default_);
1188  ThreadLocalValueHolderBase* const holder_base = new_holder;
1189  GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base));
1190  return new_holder->pointer();
1191  }
1192 
1193  // A key pthreads uses for looking up per-thread values.
1194  const pthread_key_t key_;
1195  const T default_; // The default value for each thread.
1196 
1198 };
1199 
1200 #define GTEST_IS_THREADSAFE 1
1201 
1202 #else // GTEST_HAS_PTHREAD
1203 
1204 // A dummy implementation of synchronization primitives (mutex, lock,
1205 // and thread-local variable). Necessary for compiling Google Test where
1206 // mutex is not supported - using Google Test in multiple threads is not
1207 // supported on such platforms.
1208 
1209 class Mutex {
1210  public:
1211  Mutex() {}
1212  void AssertHeld() const {}
1213 };
1214 
1215 #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1216  extern ::testing::internal::Mutex mutex
1217 
1218 #define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex
1219 
1221  public:
1222  explicit GTestMutexLock(Mutex*) {} // NOLINT
1223 };
1224 
1226 
1227 template <typename T>
1229  public:
1230  ThreadLocal() : value_() {}
1231  explicit ThreadLocal(const T& value) : value_(value) {}
1232  T* pointer() { return &value_; }
1233  const T* pointer() const { return &value_; }
1234  const T& get() const { return value_; }
1235  void set(const T& value) { value_ = value; }
1236  private:
1237  T value_;
1238 };
1239 
1240 // The above synchronization primitives have dummy implementations.
1241 // Therefore Google Test is not thread-safe.
1242 #define GTEST_IS_THREADSAFE 0
1243 
1244 #endif // GTEST_HAS_PTHREAD
1245 
1246 // Returns the number of threads running in the process, or 0 to indicate that
1247 // we cannot detect it.
1248 GTEST_API_ size_t GetThreadCount();
1249 
1250 // Passing non-POD classes through ellipsis (...) crashes the ARM
1251 // compiler and generates a warning in Sun Studio. The Nokia Symbian
1252 // and the IBM XL C/C++ compiler try to instantiate a copy constructor
1253 // for objects passed through ellipsis (...), failing for uncopyable
1254 // objects. We define this to ensure that only POD is passed through
1255 // ellipsis on these systems.
1256 #if defined(__SYMBIAN32__) || defined(__IBMCPP__) || defined(__SUNPRO_CC)
1257 // We lose support for NULL detection where the compiler doesn't like
1258 // passing non-POD classes through ellipsis (...).
1259 #define GTEST_ELLIPSIS_NEEDS_POD_ 1
1260 #else
1261 #define GTEST_CAN_COMPARE_NULL 1
1262 #endif
1263 
1264 // The Nokia Symbian and IBM XL C/C++ compilers cannot decide between
1265 // const T& and const T* in a function template. These compilers
1266 // _can_ decide between class template specializations for T and T*,
1267 // so a tr1::type_traits-like is_pointer works.
1268 #if defined(__SYMBIAN32__) || defined(__IBMCPP__)
1269 #define GTEST_NEEDS_IS_POINTER_ 1
1270 #endif
1271 
1272 template <bool bool_value>
1275  static const bool value = bool_value;
1276 };
1277 template <bool bool_value> const bool bool_constant<bool_value>::value;
1278 
1281 
1282 template <typename T>
1283 struct is_pointer : public false_type {};
1284 
1285 template <typename T>
1286 struct is_pointer<T*> : public true_type {};
1287 
1288 #if GTEST_OS_WINDOWS
1289 #define GTEST_PATH_SEP_ "\\"
1290 #define GTEST_HAS_ALT_PATH_SEP_ 1
1291 // The biggest signed integer type the compiler supports.
1292 typedef __int64 BiggestInt;
1293 #else
1294 #define GTEST_PATH_SEP_ "/"
1295 #define GTEST_HAS_ALT_PATH_SEP_ 0
1296 typedef long long BiggestInt; // NOLINT
1297 #endif // GTEST_OS_WINDOWS
1298 
1299 // The testing::internal::posix namespace holds wrappers for common
1300 // POSIX functions. These wrappers hide the differences between
1301 // Windows/MSVC and POSIX systems. Since some compilers define these
1302 // standard functions as macros, the wrapper cannot have the same name
1303 // as the wrapped function.
1304 
1305 namespace posix {
1306 
1307 // Functions with a different name on Windows.
1308 
1309 #if GTEST_OS_WINDOWS
1310 
1311 typedef struct _stat StatStruct;
1312 
1313 #ifdef __BORLANDC__
1314 inline int IsATTY(int fd) { return isatty(fd); }
1315 inline int StrCaseCmp(const char* s1, const char* s2) {
1316  return stricmp(s1, s2);
1317 }
1318 inline char* StrDup(const char* src) { return strdup(src); }
1319 #else // !__BORLANDC__
1320 #if GTEST_OS_WINDOWS_MOBILE
1321 inline int IsATTY(int /* fd */) { return 0; }
1322 #else
1323 inline int IsATTY(int fd) { return _isatty(fd); }
1324 #endif // GTEST_OS_WINDOWS_MOBILE
1325 inline int StrCaseCmp(const char* s1, const char* s2) {
1326  return _stricmp(s1, s2);
1327 }
1328 inline char* StrDup(const char* src) { return _strdup(src); }
1329 #endif // __BORLANDC__
1330 
1331 #if GTEST_OS_WINDOWS_MOBILE
1332 inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
1333 // Stat(), RmDir(), and IsDir() are not needed on Windows CE at this
1334 // time and thus not defined there.
1335 #else
1336 inline int FileNo(FILE* file) { return _fileno(file); }
1337 inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
1338 inline int RmDir(const char* dir) { return _rmdir(dir); }
1339 inline bool IsDir(const StatStruct& st) {
1340  return (_S_IFDIR & st.st_mode) != 0;
1341 }
1342 #endif // GTEST_OS_WINDOWS_MOBILE
1343 
1344 #else
1345 
1346 typedef struct stat StatStruct;
1347 
1348 inline int FileNo(FILE* file) { return fileno(file); }
1349 inline int IsATTY(int fd) { return isatty(fd); }
1350 inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
1351 inline int StrCaseCmp(const char* s1, const char* s2) {
1352  return strcasecmp(s1, s2);
1353 }
1354 inline char* StrDup(const char* src) { return strdup(src); }
1355 inline int RmDir(const char* dir) { return rmdir(dir); }
1356 inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
1357 
1358 #endif // GTEST_OS_WINDOWS
1359 
1360 // Functions deprecated by MSVC 8.0.
1361 
1362 #ifdef _MSC_VER
1363 // Temporarily disable warning 4996 (deprecated function).
1364 #pragma warning(push)
1365 #pragma warning(disable:4996)
1366 #endif
1367 
1368 inline const char* StrNCpy(char* dest, const char* src, size_t n) {
1369  return strncpy(dest, src, n);
1370 }
1371 
1372 // ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and
1373 // StrError() aren't needed on Windows CE at this time and thus not
1374 // defined there.
1375 
1376 #if !GTEST_OS_WINDOWS_MOBILE
1377 inline int ChDir(const char* dir) { return chdir(dir); }
1378 #endif
1379 inline FILE* FOpen(const char* path, const char* mode) {
1380  return fopen(path, mode);
1381 }
1382 #if !GTEST_OS_WINDOWS_MOBILE
1383 inline FILE *FReopen(const char* path, const char* mode, FILE* stream) {
1384  return freopen(path, mode, stream);
1385 }
1386 inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
1387 #endif
1388 inline int FClose(FILE* fp) { return fclose(fp); }
1389 #if !GTEST_OS_WINDOWS_MOBILE
1390 inline int Read(int fd, void* buf, unsigned int count) {
1391  return static_cast<int>(read(fd, buf, count));
1392 }
1393 inline int Write(int fd, const void* buf, unsigned int count) {
1394  return static_cast<int>(write(fd, buf, count));
1395 }
1396 inline int Close(int fd) { return close(fd); }
1397 inline const char* StrError(int errnum) { return strerror(errnum); }
1398 #endif
1399 inline const char* GetEnv(const char* name) {
1400 #if GTEST_OS_WINDOWS_MOBILE
1401  // We are on Windows CE, which has no environment variables.
1402  return NULL;
1403 #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
1404  // Environment variables which we programmatically clear will be set to the
1405  // empty string rather than unset (NULL). Handle that case.
1406  const char* const env = getenv(name);
1407  return (env != NULL && env[0] != '\0') ? env : NULL;
1408 #else
1409  return getenv(name);
1410 #endif
1411 }
1412 
1413 #ifdef _MSC_VER
1414 #pragma warning(pop) // Restores the warning state.
1415 #endif
1416 
1417 #if GTEST_OS_WINDOWS_MOBILE
1418 // Windows CE has no C library. The abort() function is used in
1419 // several places in Google Test. This implementation provides a reasonable
1420 // imitation of standard behaviour.
1421 void Abort();
1422 #else
1423 inline void Abort() { abort(); }
1424 #endif // GTEST_OS_WINDOWS_MOBILE
1425 
1426 } // namespace posix
1427 
1428 // The maximum number a BiggestInt can represent. This definition
1429 // works no matter BiggestInt is represented in one's complement or
1430 // two's complement.
1431 //
1432 // We cannot rely on numeric_limits in STL, as __int64 and long long
1433 // are not part of standard C++ and numeric_limits doesn't need to be
1434 // defined for them.
1435 const BiggestInt kMaxBiggestInt =
1436  ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1));
1437 
1438 // This template class serves as a compile-time function from size to
1439 // type. It maps a size in bytes to a primitive type with that
1440 // size. e.g.
1441 //
1442 // TypeWithSize<4>::UInt
1443 //
1444 // is typedef-ed to be unsigned int (unsigned integer made up of 4
1445 // bytes).
1446 //
1447 // Such functionality should belong to STL, but I cannot find it
1448 // there.
1449 //
1450 // Google Test uses this class in the implementation of floating-point
1451 // comparison.
1452 //
1453 // For now it only handles UInt (unsigned int) as that's all Google Test
1454 // needs. Other types can be easily added in the future if need
1455 // arises.
1456 template <size_t size>
1458  public:
1459  // This prevents the user from using TypeWithSize<N> with incorrect
1460  // values of N.
1461  typedef void UInt;
1462 };
1463 
1464 // The specialization for size 4.
1465 template <>
1466 class TypeWithSize<4> {
1467  public:
1468  // unsigned int has size 4 in both gcc and MSVC.
1469  //
1470  // As base/basictypes.h doesn't compile on Windows, we cannot use
1471  // uint32, uint64, and etc here.
1472  typedef int Int;
1473  typedef unsigned int UInt;
1474 };
1475 
1476 // The specialization for size 8.
1477 template <>
1478 class TypeWithSize<8> {
1479  public:
1480 #if GTEST_OS_WINDOWS
1481  typedef __int64 Int;
1482  typedef unsigned __int64 UInt;
1483 #else
1484  typedef long long Int; // NOLINT
1485  typedef unsigned long long UInt; // NOLINT
1486 #endif // GTEST_OS_WINDOWS
1487 };
1488 
1489 // Integer types of known sizes.
1494 typedef TypeWithSize<8>::Int TimeInMillis; // Represents time in milliseconds.
1495 
1496 // Utilities for command line flags and environment variables.
1497 
1498 // Macro for referencing flags.
1499 #define GTEST_FLAG(name) FLAGS_gtest_##name
1500 
1501 // Macros for declaring flags.
1502 #define GTEST_DECLARE_bool_(name) GTEST_API_ extern bool GTEST_FLAG(name)
1503 #define GTEST_DECLARE_int32_(name) \
1504  GTEST_API_ extern ::testing::internal::Int32 GTEST_FLAG(name)
1505 #define GTEST_DECLARE_string_(name) \
1506  GTEST_API_ extern ::testing::internal::String GTEST_FLAG(name)
1507 
1508 // Macros for defining flags.
1509 #define GTEST_DEFINE_bool_(name, default_val, doc) \
1510  GTEST_API_ bool GTEST_FLAG(name) = (default_val)
1511 #define GTEST_DEFINE_int32_(name, default_val, doc) \
1512  GTEST_API_ ::testing::internal::Int32 GTEST_FLAG(name) = (default_val)
1513 #define GTEST_DEFINE_string_(name, default_val, doc) \
1514  GTEST_API_ ::testing::internal::String GTEST_FLAG(name) = (default_val)
1515 
1516 // Parses 'str' for a 32-bit signed integer. If successful, writes the result
1517 // to *value and returns true; otherwise leaves *value unchanged and returns
1518 // false.
1519 // TODO(chandlerc): Find a better way to refactor flag and environment parsing
1520 // out of both gtest-port.cc and gtest.cc to avoid exporting this utility
1521 // function.
1522 bool ParseInt32(const Message& src_text, const char* str, Int32* value);
1523 
1524 // Parses a bool/Int32/string from the environment variable
1525 // corresponding to the given Google Test flag.
1526 bool BoolFromGTestEnv(const char* flag, bool default_val);
1527 GTEST_API_ Int32 Int32FromGTestEnv(const char* flag, Int32 default_val);
1528 const char* StringFromGTestEnv(const char* flag, const char* default_val);
1529 
1530 } // namespace internal
1531 } // namespace testing
1532 
1533 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
FILE * FReopen(const char *path, const char *mode, FILE *stream)
Definition: gtest-port.h:1383
void write(TextStream &, const RenderObject &, int indent=0, RenderAsTextBehavior=RenderAsTextBehaviorNormal)
Definition: RenderTreeAsText.cpp:529
scoped_ptr(T *p=NULL)
Definition: gtest-port.h:663
GTEST_API_ Int32 Int32FromGTestEnv(const char *flag, Int32 default_val)
Definition: gtest-port.cc:682
const T * pointer() const
Definition: gtest-port.h:1233
GTEST_API_ bool IsTrue(bool condition)
Definition: gtest.cc:4345
thread
Definition: __init__.py:35
GLint GLsizei count
Definition: gl2.h:421
line
Definition: buildtests.py:37
Definition: gtest-port.h:1220
GTEST_API_ String GetCapturedStdout()
#define size
Definition: float-mm.c:27
EGLStreamKHR EGLint EGLint EGLint size
Definition: eglext.h:984
Definition: testutils.h:40
size_t read
Definition: peerconnection_jni.cc:2388
EGLStreamKHR stream
Definition: eglext.h:340
DOMString p
Definition: WebCryptoAPI.idl:116
void reset(T *p=NULL)
Definition: gtest-port.h:676
uint32_t flag
Definition: ssl_lib.c:2732
int FClose(FILE *fp)
Definition: gtest-port.h:1388
def Run(command)
Definition: gtest_break_on_failure_unittest.py:86
const char * pattern() const
Definition: gtest-port.h:711
static bool PartialMatch(const ::std::string &str, const RE &re)
Definition: gtest-port.h:723
Definition: gtest-port.h:766
const char * StringFromGTestEnv(const char *flag, const char *default_val)
Definition: gtest-port.cc:704
unsigned long long UInt
Definition: gtest-port.h:1485
Definition: gtest-port.h:774
int IsATTY(int fd)
Definition: gtest-port.h:1349
bool_constant< bool_value > type
Definition: gtest-port.h:1274
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: gl2ext.h:134
void UInt
Definition: gtest-port.h:1461
Definition: gtest-port.h:1283
bool ParseInt32(const Message &src_text, const char *str, Int32 *value)
Definition: gtest-port.cc:630
path
Definition: generate.py:74
RE(const char *regex)
Definition: gtest-port.h:707
GTEST_API_ size_t GetThreadCount()
Definition: gtest-port.cc:101
dest
Definition: upload.py:394
Definition: gtest-port.h:768
int Stat(const char *path, StatStruct *buf)
Definition: gtest-port.h:1350
Environment * env
Definition: pseudotcp_parser_fuzzer.cc:44
T * release()
Definition: gtest-port.h:670
int ChDir(const char *dir)
Definition: gtest-port.h:1377
const char * GetEnv(const char *name)
Definition: gtest-port.h:1399
int FILE
Definition: antglob.py:49
#define GTEST_API_
Definition: gtest-port.h:615
Message
Definition: peerconnection_unittest.cc:105
std::integral_constant< std::size_t, V > size_t
Definition: Brigand.h:447
GLenum condition
Definition: gl2ext.h:2537
GLenum GLenum severity
Definition: gl2ext.h:133
GTEST_API_ void CaptureStdout()
GTEST_API_ String GetCapturedStderr()
TypeWithSize< 8 >::Int TimeInMillis
Definition: gtest-port.h:1494
~scoped_ptr()
Definition: gtest-port.h:664
GTestLogSeverity
Definition: gtest-port.h:764
bool_constant< true > true_type
Definition: gtest-port.h:1280
ThreadLocal(const T &value)
Definition: gtest-port.h:1231
Received complete frame timestamp frame type frame size at time
Definition: plotReceiveTrace.m:7
GLenum mode
Definition: gl2.h:461
T & operator*() const
Definition: gtest-port.h:666
GFLAGS_DLL_DECL const std::vector< std::string > & GetArgvs()
Definition: gflags.cc:1490
#define GTEST_DISALLOW_ASSIGN_(type)
Definition: gtest-port.h:568
bool_constant< false > false_type
Definition: gtest-port.h:1279
TypeWithSize< 4 >::UInt UInt32
Definition: gtest-port.h:1491
int Write(int fd, const void *buf, unsigned int count)
Definition: gtest-port.h:1393
Mutex()
Definition: gtest-port.h:1211
void
Definition: AVFoundationCFSoftLinking.h:81
EGLStreamKHR EGLint n
Definition: eglext.h:984
VoEFile * file
Definition: voe_cmd_test.cc:59
TypeWithSize< 4 >::Int Int32
Definition: gtest-port.h:1490
Definition: Notification.idl:40
::std::ostream & GetStream()
Definition: gtest-port.h:781
TestSubObjConstructor T
Definition: TestTypedefs.idl:84
int RmDir(const char *dir)
Definition: gtest-port.h:1355
#define GTEST_CHECK_(condition)
Definition: gtest-port.h:810
static bool FullMatch(const ::std::string &str, const RE &re)
Definition: gtest-port.h:720
fclose(fid)
EGLAttrib * value
Definition: eglext.h:120
void LogToStderr()
Definition: gtest-port.h:793
GLenum func
Definition: gl2.h:481
void Join(Iterator start, Iterator end, const char *delim, string *result)
Definition: strutil.h:756
int Read(int fd, void *buf, unsigned int count)
Definition: gtest-port.h:1390
GTestMutexLock MutexLock
Definition: gtest-port.h:1225
Definition: gtest-port.h:767
Definition: gtest-string.h:81
#define S_ISDIR(mode)
Definition: video_quality_measurement.cc:19
int StrCaseCmp(const char *s1, const char *s2)
Definition: gtest-port.h:1351
EGLImageKHR EGLint * name
Definition: eglext.h:851
long long BiggestInt
Definition: gtest-port.h:1296
Definition: gtest-port.h:694
bool IsDir(const StatStruct &st)
Definition: gtest-port.h:1356
Definition: gtest-port.h:1457
#define GTEST_CHECK_POSIX_SUCCESS_(posix_call)
Definition: gtest-port.h:822
GLenum void ** pointer
Definition: gl2.h:460
#define malloc
Definition: mbmalloc.h:49
Definition: gtest-port.h:1273
unsigned int UInt
Definition: gtest-port.h:1473
int FileNo(FILE *file)
Definition: gtest-port.h:1348
Definition: gtest-port.h:765
Definition: safe_conversions.h:16
bool BoolFromGTestEnv(const char *flag, bool default_val)
Definition: gtest-port.cc:672
FILE * FOpen(const char *path, const char *mode)
Definition: gtest-port.h:1379
str
Definition: make-dist.py:305
GLsizei const GLchar *const * string
Definition: gl2.h:479
Definition: document.h:393
int Close(int fd)
Definition: gtest-port.h:1396
void Init(int s, int n)
Definition: towers.c:162
GLenum src
Definition: gl2ext.h:304
RE(const RE &other)
Definition: gtest-port.h:698
abort
Definition: config.py:261
unsigned int UInt
Definition: config.h:77
GTEST_API_ void CaptureStderr()
void Abort()
Definition: gtest-port.h:1423
Definition: gtest-port.h:1228
GLint param
Definition: gl2.h:471
#define NULL
Definition: common_types.h:41
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Definition: gtest-port.h:573
void FlushInfoLog()
Definition: gtest-port.h:794
const char * StrNCpy(char *dest, const char *src, size_t n)
Definition: gtest-port.h:1368
TypeWithSize< 8 >::UInt UInt64
Definition: gtest-port.h:1493
GTestMutexLock(Mutex *)
Definition: gtest-port.h:1222
T * operator->() const
Definition: gtest-port.h:667
RE(const ::std::string &regex)
Definition: gtest-port.h:701
Definition: gtest-port.h:1209
#define free
Definition: mbmalloc.h:50
TypeWithSize< 8 >::Int Int64
Definition: gtest-port.h:1492
int Int
Definition: gtest-port.h:1472
void AssertHeld() const
Definition: gtest-port.h:1212
Definition: gtest-port.h:659
long long Int
Definition: gtest-port.h:1484
ThreadLocal()
Definition: gtest-port.h:1230
FILE * FDOpen(int fd, const char *mode)
Definition: gtest-port.h:1386
const BiggestInt kMaxBiggestInt
Definition: gtest-port.h:1435
CFArrayRef CFTypeRef key
Definition: AVFoundationCFSoftLinking.h:129
int Int
Definition: config.h:76
char * StrDup(const char *src)
Definition: gtest-port.h:1354
const char * StrError(int errnum)
Definition: gtest-port.h:1397
Definition: gtest-port.h:626
Derived * CheckedDowncastToActualType(Base *base)
Definition: gtest-port.h:835
Definition: gtest-port.h:1305
#define strcasecmp
Definition: windows_port.h:105
Definition: env.py:1
const AtomicString & reset()
Definition: InputTypeNames.cpp:124
#define T(a)
Definition: row_common.cc:1964
T * pointer()
Definition: gtest-port.h:1232
T element_type
Definition: gtest-port.h:661