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