1 // Copyright 2005, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Low-level types and utilities for porting Google Test to various
31 // platforms. All macros ending with _ and symbols defined in an
32 // internal namespace are subject to change without notice. Code
33 // outside Google Test MUST NOT USE THEM DIRECTLY. Macros that don't
34 // end with _ are part of Google Test's public API and can be used by
35 // code outside Google Test.
37 // This file is fundamental to Google Test. All other Google Test source
38 // files are expected to #include this. Therefore, it cannot #include
39 // any other Google Test header.
41 // GOOGLETEST_CM0001 DO NOT DELETE
43 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
44 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
46 // Environment-describing macros
47 // -----------------------------
49 // Google Test can be used in many different environments. Macros in
50 // this section tell Google Test what kind of environment it is being
51 // used in, such that Google Test can provide environment-specific
52 // features and implementations.
54 // Google Test tries to automatically detect the properties of its
55 // environment, so users usually don't need to worry about these
56 // macros. However, the automatic detection is not perfect.
57 // Sometimes it's necessary for a user to define some of the following
58 // macros in the build script to override Google Test's decisions.
60 // If the user doesn't define a macro in the list, Google Test will
61 // provide a default definition. After this header is #included, all
62 // macros in this list will be defined to either 1 or 0.
64 // Notes to maintainers:
65 // - Each macro here is a user-tweakable knob; do not grow the list
67 // - Use #if to key off these macros. Don't use #ifdef or "#if
68 // defined(...)", which will not work as these macros are ALWAYS
71 // GTEST_HAS_CLONE - Define it to 1/0 to indicate that clone(2)
72 // is/isn't available.
73 // GTEST_HAS_EXCEPTIONS - Define it to 1/0 to indicate that exceptions
75 // GTEST_HAS_POSIX_RE - Define it to 1/0 to indicate that POSIX regular
76 // expressions are/aren't available.
77 // GTEST_HAS_PTHREAD - Define it to 1/0 to indicate that <pthread.h>
78 // is/isn't available.
79 // GTEST_HAS_RTTI - Define it to 1/0 to indicate that RTTI is/isn't
81 // GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that
82 // std::wstring does/doesn't work (Google Test can
83 // be used where std::wstring is unavailable).
84 // GTEST_HAS_SEH - Define it to 1/0 to indicate whether the
85 // compiler supports Microsoft's "Structured
86 // Exception Handling".
87 // GTEST_HAS_STREAM_REDIRECTION
88 // - Define it to 1/0 to indicate whether the
89 // platform supports I/O stream redirection using
91 // GTEST_LINKED_AS_SHARED_LIBRARY
92 // - Define to 1 when compiling tests that use
93 // Google Test as a shared library (known as
95 // GTEST_CREATE_SHARED_LIBRARY
96 // - Define to 1 when compiling Google Test itself
97 // as a shared library.
98 // GTEST_DEFAULT_DEATH_TEST_STYLE
99 // - The default value of --gtest_death_test_style.
100 // The legacy default has been "fast" in the open
101 // source version since 2008. The recommended value
102 // is "threadsafe", and can be set in
103 // custom/gtest-port.h.
105 // Platform-indicating macros
106 // --------------------------
108 // Macros indicating the platform on which Google Test is being used
109 // (a macro is defined to 1 if compiled on the given platform;
110 // otherwise UNDEFINED -- it's never defined to 0.). Google Test
111 // defines these macros automatically. Code outside Google Test MUST
114 // GTEST_OS_AIX - IBM AIX
115 // GTEST_OS_CYGWIN - Cygwin
116 // GTEST_OS_DRAGONFLY - DragonFlyBSD
117 // GTEST_OS_FREEBSD - FreeBSD
118 // GTEST_OS_FUCHSIA - Fuchsia
119 // GTEST_OS_GNU_KFREEBSD - GNU/kFreeBSD
120 // GTEST_OS_HAIKU - Haiku
121 // GTEST_OS_HPUX - HP-UX
122 // GTEST_OS_LINUX - Linux
123 // GTEST_OS_LINUX_ANDROID - Google Android
124 // GTEST_OS_MAC - Mac OS X
125 // GTEST_OS_IOS - iOS
126 // GTEST_OS_NACL - Google Native Client (NaCl)
127 // GTEST_OS_NETBSD - NetBSD
128 // GTEST_OS_OPENBSD - OpenBSD
129 // GTEST_OS_OS2 - OS/2
130 // GTEST_OS_QNX - QNX
131 // GTEST_OS_SOLARIS - Sun Solaris
132 // GTEST_OS_WINDOWS - Windows (Desktop, MinGW, or Mobile)
133 // GTEST_OS_WINDOWS_DESKTOP - Windows Desktop
134 // GTEST_OS_WINDOWS_MINGW - MinGW
135 // GTEST_OS_WINDOWS_MOBILE - Windows Mobile
136 // GTEST_OS_WINDOWS_PHONE - Windows Phone
137 // GTEST_OS_WINDOWS_RT - Windows Store App/WinRT
138 // GTEST_OS_ZOS - z/OS
140 // Among the platforms, Cygwin, Linux, Mac OS X, and Windows have the
141 // most stable support. Since core members of the Google Test project
142 // don't have access to other platforms, support for them may be less
143 // stable. If you notice any problems on your platform, please notify
144 // googletestframework@googlegroups.com (patches for fixing them are
145 // even more welcome!).
147 // It is possible that none of the GTEST_OS_* macros are defined.
149 // Feature-indicating macros
150 // -------------------------
152 // Macros indicating which Google Test features are available (a macro
153 // is defined to 1 if the corresponding feature is supported;
154 // otherwise UNDEFINED -- it's never defined to 0.). Google Test
155 // defines these macros automatically. Code outside Google Test MUST
158 // These macros are public so that portable tests can be written.
159 // Such tests typically surround code using a feature with an #if
160 // which controls that code. For example:
162 // #if GTEST_HAS_DEATH_TEST
163 // EXPECT_DEATH(DoSomethingDeadly());
166 // GTEST_HAS_DEATH_TEST - death tests
167 // GTEST_HAS_TYPED_TEST - typed tests
168 // GTEST_HAS_TYPED_TEST_P - type-parameterized tests
169 // GTEST_IS_THREADSAFE - Google Test is thread-safe.
170 // GOOGLETEST_CM0007 DO NOT DELETE
171 // GTEST_USES_POSIX_RE - enhanced POSIX regex is used. Do not confuse with
172 // GTEST_HAS_POSIX_RE (see above) which users can
173 // define themselves.
174 // GTEST_USES_SIMPLE_RE - our own simple regex is used;
175 // the above RE\b(s) are mutually exclusive.
177 // Misc public macros
178 // ------------------
180 // GTEST_FLAG(flag_name) - references the variable corresponding to
181 // the given Google Test flag.
183 // Internal utilities
184 // ------------------
186 // The following macros and utilities are for Google Test's INTERNAL
187 // use only. Code outside Google Test MUST NOT USE THEM DIRECTLY.
189 // Macros for basic C++ coding:
190 // GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.
191 // GTEST_ATTRIBUTE_UNUSED_ - declares that a class' instances or a
192 // variable don't have to be used.
193 // GTEST_DISALLOW_ASSIGN_ - disables operator=.
194 // GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=.
195 // GTEST_MUST_USE_RESULT_ - declares that a function's result must be used.
196 // GTEST_INTENTIONAL_CONST_COND_PUSH_ - start code section where MSVC C4127 is
197 // suppressed (constant conditional).
198 // GTEST_INTENTIONAL_CONST_COND_POP_ - finish code section where MSVC C4127
202 // Mutex, MutexLock, ThreadLocal, GetThreadCount()
203 // - synchronization primitives.
205 // Regular expressions:
206 // RE - a simple regular expression class using the POSIX
207 // Extended Regular Expression syntax on UNIX-like platforms
208 // GOOGLETEST_CM0008 DO NOT DELETE
209 // or a reduced regular exception syntax on other
210 // platforms, including Windows.
212 // GTEST_LOG_() - logs messages at the specified severity level.
213 // LogToStderr() - directs all log messages to stderr.
214 // FlushInfoLog() - flushes informational log messages.
216 // Stdout and stderr capturing:
217 // CaptureStdout() - starts capturing stdout.
218 // GetCapturedStdout() - stops capturing stdout and returns the captured
220 // CaptureStderr() - starts capturing stderr.
221 // GetCapturedStderr() - stops capturing stderr and returns the captured
225 // TypeWithSize - maps an integer to a int type.
226 // Int32, UInt32, Int64, UInt64, TimeInMillis
227 // - integers of known sizes.
228 // BiggestInt - the biggest signed integer type.
230 // Command-line utilities:
231 // GTEST_DECLARE_*() - declares a flag.
232 // GTEST_DEFINE_*() - defines a flag.
233 // GetInjectableArgvs() - returns the command line as a vector of strings.
235 // Environment variable utilities:
236 // GetEnv() - gets the value of an environment variable.
237 // BoolFromGTestEnv() - parses a bool environment variable.
238 // Int32FromGTestEnv() - parses an Int32 environment variable.
239 // StringFromGTestEnv() - parses a string environment variable.
241 // Deprecation warnings:
242 // GTEST_INTERNAL_DEPRECATED(message) - attribute marking a function as
243 // deprecated; calling a marked function
244 // should generate a compiler warning
246 #include <ctype.h> // for isspace, etc
247 #include <stddef.h> // for ptrdiff_t
252 #include <type_traits>
255 # include <sys/types.h>
256 # include <sys/stat.h>
257 #endif // !_WIN32_WCE
259 #if defined __APPLE__
260 # include <AvailabilityMacros.h>
261 # include <TargetConditionals.h>
264 #include <algorithm> // NOLINT
265 #include <iostream> // NOLINT
266 #include <sstream> // NOLINT
267 #include <string> // NOLINT
270 #include <vector> // NOLINT
272 #include "gtest/internal/gtest-port-arch.h"
273 #include "gtest/internal/custom/gtest-port.h"
275 #if !defined(GTEST_DEV_EMAIL_)
276 # define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
277 # define GTEST_FLAG_PREFIX_ "gtest_"
278 # define GTEST_FLAG_PREFIX_DASH_ "gtest-"
279 # define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
280 # define GTEST_NAME_ "Google Test"
281 # define GTEST_PROJECT_URL_ "https://github.com/google/googletest/"
282 #endif // !defined(GTEST_DEV_EMAIL_)
284 #if !defined(GTEST_INIT_GOOGLE_TEST_NAME_)
285 # define GTEST_INIT_GOOGLE_TEST_NAME_ "testing::InitGoogleTest"
286 #endif // !defined(GTEST_INIT_GOOGLE_TEST_NAME_)
288 // Determines the version of gcc that is used to compile this.
290 // 40302 means version 4.3.2.
291 # define GTEST_GCC_VER_ \
292 (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
295 // Macros for disabling Microsoft Visual C++ warnings.
297 // GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 4385)
298 // /* code that triggers warnings C4800 and C4385 */
299 // GTEST_DISABLE_MSC_WARNINGS_POP_()
300 #if defined(_MSC_VER)
301 # define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) \
302 __pragma(warning(push)) \
303 __pragma(warning(disable: warnings))
304 # define GTEST_DISABLE_MSC_WARNINGS_POP_() \
305 __pragma(warning(pop))
307 // Not all compilers are MSVC
308 # define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
309 # define GTEST_DISABLE_MSC_WARNINGS_POP_()
312 // Clang on Windows does not understand MSVC's pragma warning.
313 // We need clang-specific way to disable function deprecation warning.
315 # define GTEST_DISABLE_MSC_DEPRECATED_PUSH_() \
316 _Pragma("clang diagnostic push") \
317 _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") \
318 _Pragma("clang diagnostic ignored \"-Wdeprecated-implementations\"")
319 #define GTEST_DISABLE_MSC_DEPRECATED_POP_() \
320 _Pragma("clang diagnostic pop")
322 # define GTEST_DISABLE_MSC_DEPRECATED_PUSH_() \
323 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996)
324 # define GTEST_DISABLE_MSC_DEPRECATED_POP_() \
325 GTEST_DISABLE_MSC_WARNINGS_POP_()
328 // Brings in definitions for functions used in the testing::internal::posix
329 // namespace (read, write, close, chdir, isatty, stat). We do not currently
330 // use them on Windows Mobile.
332 # if !GTEST_OS_WINDOWS_MOBILE
336 // In order to avoid having to include <windows.h>, use forward declaration
337 #if GTEST_OS_WINDOWS_MINGW && !defined(__MINGW64_VERSION_MAJOR)
338 // MinGW defined _CRITICAL_SECTION and _RTL_CRITICAL_SECTION as two
339 // separate (equivalent) structs, instead of using typedef
340 typedef struct _CRITICAL_SECTION GTEST_CRITICAL_SECTION
;
342 // Assume CRITICAL_SECTION is a typedef of _RTL_CRITICAL_SECTION.
343 // This assumption is verified by
344 // WindowsTypesTest.CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION.
345 typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION
;
348 // This assumes that non-Windows OSes provide unistd.h. For OSes where this
349 // is not the case, we need to include headers that provide the functions
352 # include <strings.h>
353 #endif // GTEST_OS_WINDOWS
355 #if GTEST_OS_LINUX_ANDROID
356 // Used to define __ANDROID_API__ matching the target NDK API level.
357 # include <android/api-level.h> // NOLINT
360 // Defines this to true if and only if Google Test can use POSIX regular
362 #ifndef GTEST_HAS_POSIX_RE
363 # if GTEST_OS_LINUX_ANDROID
364 // On Android, <regex.h> is only available starting with Gingerbread.
365 # define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9)
367 # define GTEST_HAS_POSIX_RE (!GTEST_OS_WINDOWS)
372 // The appropriate headers have already been included.
374 #elif GTEST_HAS_POSIX_RE
376 // On some platforms, <regex.h> needs someone to define size_t, and
377 // won't compile otherwise. We can #include it here as we already
378 // included <stdlib.h>, which is guaranteed to define size_t through
380 # include <regex.h> // NOLINT
382 # define GTEST_USES_POSIX_RE 1
384 #elif GTEST_OS_WINDOWS
386 // <regex.h> is not available on Windows. Use our own simple regex
387 // implementation instead.
388 # define GTEST_USES_SIMPLE_RE 1
392 // <regex.h> may not be available on this platform. Use our own
393 // simple regex implementation instead.
394 # define GTEST_USES_SIMPLE_RE 1
396 #endif // GTEST_USES_PCRE
398 #ifndef GTEST_HAS_EXCEPTIONS
399 // The user didn't tell us whether exceptions are enabled, so we need
401 # if defined(_MSC_VER) && defined(_CPPUNWIND)
402 // MSVC defines _CPPUNWIND to 1 if and only if exceptions are enabled.
403 # define GTEST_HAS_EXCEPTIONS 1
404 # elif defined(__BORLANDC__)
405 // C++Builder's implementation of the STL uses the _HAS_EXCEPTIONS
406 // macro to enable exceptions, so we'll do the same.
407 // Assumes that exceptions are enabled by default.
408 # ifndef _HAS_EXCEPTIONS
409 # define _HAS_EXCEPTIONS 1
410 # endif // _HAS_EXCEPTIONS
411 # define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
412 # elif defined(__clang__)
413 // clang defines __EXCEPTIONS if and only if exceptions are enabled before clang
414 // 220714, but if and only if cleanups are enabled after that. In Obj-C++ files,
415 // there can be cleanups for ObjC exceptions which also need cleanups, even if
416 // C++ exceptions are disabled. clang has __has_feature(cxx_exceptions) which
417 // checks for C++ exceptions starting at clang r206352, but which checked for
418 // cleanups prior to that. To reliably check for C++ exception availability with
420 // __EXCEPTIONS && __has_feature(cxx_exceptions).
421 # define GTEST_HAS_EXCEPTIONS (__EXCEPTIONS && __has_feature(cxx_exceptions))
422 # elif defined(__GNUC__) && __EXCEPTIONS
423 // gcc defines __EXCEPTIONS to 1 if and only if exceptions are enabled.
424 # define GTEST_HAS_EXCEPTIONS 1
425 # elif defined(__SUNPRO_CC)
426 // Sun Pro CC supports exceptions. However, there is no compile-time way of
427 // detecting whether they are enabled or not. Therefore, we assume that
428 // they are enabled unless the user tells us otherwise.
429 # define GTEST_HAS_EXCEPTIONS 1
430 # elif defined(__IBMCPP__) && __EXCEPTIONS
431 // xlC defines __EXCEPTIONS to 1 if and only if exceptions are enabled.
432 # define GTEST_HAS_EXCEPTIONS 1
433 # elif defined(__HP_aCC)
434 // Exception handling is in effect by default in HP aCC compiler. It has to
435 // be turned of by +noeh compiler option if desired.
436 # define GTEST_HAS_EXCEPTIONS 1
438 // For other compilers, we assume exceptions are disabled to be
440 # define GTEST_HAS_EXCEPTIONS 0
441 # endif // defined(_MSC_VER) || defined(__BORLANDC__)
442 #endif // GTEST_HAS_EXCEPTIONS
444 #if !defined(GTEST_HAS_STD_STRING)
445 // Even though we don't use this macro any longer, we keep it in case
446 // some clients still depend on it.
447 # define GTEST_HAS_STD_STRING 1
448 #elif !GTEST_HAS_STD_STRING
449 // The user told us that ::std::string isn't available.
450 # error "::std::string isn't available."
451 #endif // !defined(GTEST_HAS_STD_STRING)
453 #ifndef GTEST_HAS_STD_WSTRING
454 // The user didn't tell us whether ::std::wstring is available, so we need
456 // Cygwin 1.7 and below doesn't support ::std::wstring.
457 // Solaris' libc++ doesn't support it either. Android has
458 // no support for it at least as recent as Froyo (2.2).
459 #define GTEST_HAS_STD_WSTRING \
460 (!(GTEST_OS_LINUX_ANDROID || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \
463 #endif // GTEST_HAS_STD_WSTRING
465 // Determines whether RTTI is available.
466 #ifndef GTEST_HAS_RTTI
467 // The user didn't tell us whether RTTI is enabled, so we need to
472 #ifdef _CPPRTTI // MSVC defines this macro if and only if RTTI is enabled.
473 # define GTEST_HAS_RTTI 1
475 # define GTEST_HAS_RTTI 0
478 // Starting with version 4.3.2, gcc defines __GXX_RTTI if and only if RTTI is
480 # elif defined(__GNUC__)
483 // When building against STLport with the Android NDK and with
484 // -frtti -fno-exceptions, the build fails at link time with undefined
485 // references to __cxa_bad_typeid. Note sure if STL or toolchain bug,
486 // so disable RTTI when detected.
487 # if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR) && \
488 !defined(__EXCEPTIONS)
489 # define GTEST_HAS_RTTI 0
491 # define GTEST_HAS_RTTI 1
492 # endif // GTEST_OS_LINUX_ANDROID && __STLPORT_MAJOR && !__EXCEPTIONS
494 # define GTEST_HAS_RTTI 0
495 # endif // __GXX_RTTI
497 // Clang defines __GXX_RTTI starting with version 3.0, but its manual recommends
498 // using has_feature instead. has_feature(cxx_rtti) is supported since 2.7, the
499 // first version with C++ support.
500 # elif defined(__clang__)
502 # define GTEST_HAS_RTTI __has_feature(cxx_rtti)
504 // Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if
505 // both the typeid and dynamic_cast features are present.
506 # elif defined(__IBMCPP__) && (__IBMCPP__ >= 900)
509 # define GTEST_HAS_RTTI 1
511 # define GTEST_HAS_RTTI 0
516 // For all other compilers, we assume RTTI is enabled.
517 # define GTEST_HAS_RTTI 1
521 #endif // GTEST_HAS_RTTI
523 // It's this header's responsibility to #include <typeinfo> when RTTI
529 // Determines whether Google Test can use the pthreads library.
530 #ifndef GTEST_HAS_PTHREAD
531 // The user didn't tell us explicitly, so we make reasonable assumptions about
532 // which platforms have pthreads support.
534 // To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0
535 // to your compiler flags.
536 #define GTEST_HAS_PTHREAD \
537 (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_HPUX || GTEST_OS_QNX || \
538 GTEST_OS_FREEBSD || GTEST_OS_NACL || GTEST_OS_NETBSD || GTEST_OS_FUCHSIA || \
539 GTEST_OS_DRAGONFLY || GTEST_OS_GNU_KFREEBSD || GTEST_OS_OPENBSD || \
541 #endif // GTEST_HAS_PTHREAD
543 #if GTEST_HAS_PTHREAD
544 // gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is
546 # include <pthread.h> // NOLINT
548 // For timespec and nanosleep, used below.
549 # include <time.h> // NOLINT
552 // Determines whether clone(2) is supported.
553 // Usually it will only be available on Linux, excluding
554 // Linux on the Itanium architecture.
555 // Also see http://linux.die.net/man/2/clone.
556 #ifndef GTEST_HAS_CLONE
557 // The user didn't tell us, so we need to figure it out.
559 # if GTEST_OS_LINUX && !defined(__ia64__)
560 # if GTEST_OS_LINUX_ANDROID
561 // On Android, clone() became available at different API levels for each 32-bit
563 # if defined(__LP64__) || \
564 (defined(__arm__) && __ANDROID_API__ >= 9) || \
565 (defined(__mips__) && __ANDROID_API__ >= 12) || \
566 (defined(__i386__) && __ANDROID_API__ >= 17)
567 # define GTEST_HAS_CLONE 1
569 # define GTEST_HAS_CLONE 0
572 # define GTEST_HAS_CLONE 1
575 # define GTEST_HAS_CLONE 0
576 # endif // GTEST_OS_LINUX && !defined(__ia64__)
578 #endif // GTEST_HAS_CLONE
580 // Determines whether to support stream redirection. This is used to test
581 // output correctness and to implement death tests.
582 #ifndef GTEST_HAS_STREAM_REDIRECTION
583 // By default, we assume that stream redirection is supported on all
584 // platforms except known mobile ones.
585 # if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT
586 # define GTEST_HAS_STREAM_REDIRECTION 0
588 # define GTEST_HAS_STREAM_REDIRECTION 1
589 # endif // !GTEST_OS_WINDOWS_MOBILE
590 #endif // GTEST_HAS_STREAM_REDIRECTION
592 // Determines whether to support death tests.
593 // pops up a dialog window that cannot be suppressed programmatically.
594 #if (GTEST_OS_LINUX || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \
595 (GTEST_OS_MAC && !GTEST_OS_IOS) || \
596 (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER) || GTEST_OS_WINDOWS_MINGW || \
597 GTEST_OS_AIX || GTEST_OS_HPUX || GTEST_OS_OPENBSD || GTEST_OS_QNX || \
598 GTEST_OS_FREEBSD || GTEST_OS_NETBSD || GTEST_OS_FUCHSIA || \
599 GTEST_OS_DRAGONFLY || GTEST_OS_GNU_KFREEBSD || GTEST_OS_HAIKU)
600 # define GTEST_HAS_DEATH_TEST 1
603 // Determines whether to support type-driven tests.
605 // Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0,
606 // Sun Pro CC, IBM Visual Age, and HP aCC support.
607 #if defined(__GNUC__) || defined(_MSC_VER) || defined(__SUNPRO_CC) || \
608 defined(__IBMCPP__) || defined(__HP_aCC)
609 # define GTEST_HAS_TYPED_TEST 1
610 # define GTEST_HAS_TYPED_TEST_P 1
613 // Determines whether the system compiler uses UTF-16 for encoding wide strings.
614 #define GTEST_WIDE_STRING_USES_UTF16_ \
615 (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_AIX || GTEST_OS_OS2)
617 // Determines whether test results can be streamed to a socket.
618 #if GTEST_OS_LINUX || GTEST_OS_GNU_KFREEBSD || GTEST_OS_DRAGONFLY || \
619 GTEST_OS_FREEBSD || GTEST_OS_NETBSD || GTEST_OS_OPENBSD
620 # define GTEST_CAN_STREAM_RESULTS_ 1
623 // Defines some utility macros.
625 // The GNU compiler emits a warning if nested "if" statements are followed by
626 // an "else" statement and braces are not used to explicitly disambiguate the
627 // "else" binding. This leads to problems with code like:
630 // ASSERT_*(condition) << "Some message";
632 // The "switch (0) case 0:" idiom is used to suppress this.
633 #ifdef __INTEL_COMPILER
634 # define GTEST_AMBIGUOUS_ELSE_BLOCKER_
636 # define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: default: // NOLINT
639 // Use this annotation at the end of a struct/class definition to
640 // prevent the compiler from optimizing away instances that are never
641 // used. This is useful when all interesting logic happens inside the
642 // c'tor and / or d'tor. Example:
646 // } GTEST_ATTRIBUTE_UNUSED_;
648 // Also use it after a variable or parameter declaration to tell the
649 // compiler the variable/parameter does not have to be used.
650 #if defined(__GNUC__) && !defined(COMPILER_ICC)
651 # define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
652 #elif defined(__clang__)
653 # if __has_attribute(unused)
654 # define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
657 #ifndef GTEST_ATTRIBUTE_UNUSED_
658 # define GTEST_ATTRIBUTE_UNUSED_
661 // Use this annotation before a function that takes a printf format string.
662 #if (defined(__GNUC__) || defined(__clang__)) && !defined(COMPILER_ICC)
663 # if defined(__MINGW_PRINTF_FORMAT)
664 // MinGW has two different printf implementations. Ensure the format macro
665 // matches the selected implementation. See
666 // https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/.
667 # define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \
668 __attribute__((__format__(__MINGW_PRINTF_FORMAT, string_index, \
671 # define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \
672 __attribute__((__format__(__printf__, string_index, first_to_check)))
675 # define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check)
679 // A macro to disallow operator=
680 // This should be used in the private: declarations for a class.
681 #define GTEST_DISALLOW_ASSIGN_(type) \
682 void operator=(type const &) = delete
684 // A macro to disallow copy constructor and operator=
685 // This should be used in the private: declarations for a class.
686 #define GTEST_DISALLOW_COPY_AND_ASSIGN_(type) \
687 type(type const &) = delete; \
688 GTEST_DISALLOW_ASSIGN_(type)
690 // Tell the compiler to warn about unused return values for functions declared
691 // with this macro. The macro should be used on function declarations
692 // following the argument list:
694 // Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_;
695 #if defined(__GNUC__) && !defined(COMPILER_ICC)
696 # define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))
698 # define GTEST_MUST_USE_RESULT_
699 #endif // __GNUC__ && !COMPILER_ICC
701 // MS C++ compiler emits warning when a conditional expression is compile time
702 // constant. In some contexts this warning is false positive and needs to be
703 // suppressed. Use the following two macros in such cases:
705 // GTEST_INTENTIONAL_CONST_COND_PUSH_()
707 // GTEST_INTENTIONAL_CONST_COND_POP_()
709 # define GTEST_INTENTIONAL_CONST_COND_PUSH_() \
710 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127)
711 # define GTEST_INTENTIONAL_CONST_COND_POP_() \
712 GTEST_DISABLE_MSC_WARNINGS_POP_()
714 // Determine whether the compiler supports Microsoft's Structured Exception
715 // Handling. This is supported by several Windows compilers but generally
716 // does not exist on any other system.
717 #ifndef GTEST_HAS_SEH
718 // The user didn't tell us, so we need to figure it out.
720 # if defined(_MSC_VER) || defined(__BORLANDC__)
721 // These two compilers are known to support SEH.
722 # define GTEST_HAS_SEH 1
725 # define GTEST_HAS_SEH 0
728 #endif // GTEST_HAS_SEH
730 #ifndef GTEST_IS_THREADSAFE
732 #define GTEST_IS_THREADSAFE \
733 (GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ || \
734 (GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT) || \
737 #endif // GTEST_IS_THREADSAFE
739 // GTEST_API_ qualifies all symbols that must be exported. The definitions below
740 // are guarded by #ifndef to give embedders a chance to define GTEST_API_ in
741 // gtest/internal/custom/gtest-port.h
745 # if GTEST_LINKED_AS_SHARED_LIBRARY
746 # define GTEST_API_ __declspec(dllimport)
747 # elif GTEST_CREATE_SHARED_LIBRARY
748 # define GTEST_API_ __declspec(dllexport)
750 #elif __GNUC__ >= 4 || defined(__clang__)
751 # define GTEST_API_ __attribute__((visibility ("default")))
760 #ifndef GTEST_DEFAULT_DEATH_TEST_STYLE
761 # define GTEST_DEFAULT_DEATH_TEST_STYLE "fast"
762 #endif // GTEST_DEFAULT_DEATH_TEST_STYLE
765 // Ask the compiler to never inline a given function.
766 # define GTEST_NO_INLINE_ __attribute__((noinline))
768 # define GTEST_NO_INLINE_
771 // _LIBCPP_VERSION is defined by the libc++ library from the LLVM project.
772 #if !defined(GTEST_HAS_CXXABI_H_)
773 # if defined(__GLIBCXX__) || (defined(_LIBCPP_VERSION) && !defined(_MSC_VER))
774 # define GTEST_HAS_CXXABI_H_ 1
776 # define GTEST_HAS_CXXABI_H_ 0
780 // A function level attribute to disable checking for use of uninitialized
781 // memory when built with MemorySanitizer.
782 #if defined(__clang__)
783 # if __has_feature(memory_sanitizer)
784 # define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ \
785 __attribute__((no_sanitize_memory))
787 # define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
788 # endif // __has_feature(memory_sanitizer)
790 # define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
793 // A function level attribute to disable AddressSanitizer instrumentation.
794 #if defined(__clang__)
795 # if __has_feature(address_sanitizer)
796 # define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ \
797 __attribute__((no_sanitize_address))
799 # define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
800 # endif // __has_feature(address_sanitizer)
802 # define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
805 // A function level attribute to disable HWAddressSanitizer instrumentation.
806 #if defined(__clang__)
807 # if __has_feature(hwaddress_sanitizer)
808 # define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ \
809 __attribute__((no_sanitize("hwaddress")))
811 # define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_
812 # endif // __has_feature(hwaddress_sanitizer)
814 # define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_
817 // A function level attribute to disable ThreadSanitizer instrumentation.
818 #if defined(__clang__)
819 # if __has_feature(thread_sanitizer)
820 # define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ \
821 __attribute__((no_sanitize_thread))
823 # define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
824 # endif // __has_feature(thread_sanitizer)
826 # define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
833 // Legacy imports for backwards compatibility.
834 // New code should use std:: names directly.
836 using std::make_tuple
;
838 using std::tuple_element
;
839 using std::tuple_size
;
843 // A secret type that Google Test users don't know about. It has no
844 // definition on purpose. Therefore it's impossible to create a
845 // Secret object, which is what we want.
848 // The GTEST_COMPILE_ASSERT_ is a legacy macro used to verify that a compile
849 // time expression is true (in new code, use static_assert instead). For
850 // example, you could use it to verify the size of a static array:
852 // GTEST_COMPILE_ASSERT_(GTEST_ARRAY_SIZE_(names) == NUM_NAMES,
853 // names_incorrect_size);
855 // The second argument to the macro must be a valid C++ identifier. If the
856 // expression is false, compiler will issue an error containing this identifier.
857 #define GTEST_COMPILE_ASSERT_(expr, msg) static_assert(expr, #msg)
859 // Evaluates to the number of elements in 'array'.
860 #define GTEST_ARRAY_SIZE_(array) (sizeof(array) / sizeof(array[0]))
862 // A helper for suppressing warnings on constant condition. It just
863 // returns 'condition'.
864 GTEST_API_
bool IsTrue(bool condition
);
869 // if used, PCRE is injected by custom/gtest-port.h
870 #elif GTEST_USES_POSIX_RE || GTEST_USES_SIMPLE_RE
872 // A simple C++ wrapper for <regex.h>. It uses the POSIX Extended
873 // Regular Expression syntax.
874 class GTEST_API_ RE
{
876 // A copy constructor is required by the Standard to initialize object
877 // references from r-values.
878 RE(const RE
& other
) { Init(other
.pattern()); }
880 // Constructs an RE from a string.
881 RE(const ::std::string
& regex
) { Init(regex
.c_str()); } // NOLINT
883 RE(const char* regex
) { Init(regex
); } // NOLINT
886 // Returns the string representation of the regex.
887 const char* pattern() const { return pattern_
; }
889 // FullMatch(str, re) returns true if and only if regular expression re
890 // matches the entire str.
891 // PartialMatch(str, re) returns true if and only if regular expression re
892 // matches a substring of str (including str itself).
893 static bool FullMatch(const ::std::string
& str
, const RE
& re
) {
894 return FullMatch(str
.c_str(), re
);
896 static bool PartialMatch(const ::std::string
& str
, const RE
& re
) {
897 return PartialMatch(str
.c_str(), re
);
900 static bool FullMatch(const char* str
, const RE
& re
);
901 static bool PartialMatch(const char* str
, const RE
& re
);
904 void Init(const char* regex
);
905 const char* pattern_
;
908 # if GTEST_USES_POSIX_RE
910 regex_t full_regex_
; // For FullMatch().
911 regex_t partial_regex_
; // For PartialMatch().
913 # else // GTEST_USES_SIMPLE_RE
915 const char* full_pattern_
; // For FullMatch();
919 GTEST_DISALLOW_ASSIGN_(RE
);
922 #endif // GTEST_USES_PCRE
924 // Formats a source file path and a line number as they would appear
925 // in an error message from the compiler used to compile this code.
926 GTEST_API_ ::std::string
FormatFileLocation(const char* file
, int line
);
928 // Formats a file location for compiler-independent XML output.
929 // Although this function is not platform dependent, we put it next to
930 // FormatFileLocation in order to contrast the two functions.
931 GTEST_API_ ::std::string
FormatCompilerIndependentFileLocation(const char* file
,
934 // Defines logging utilities:
935 // GTEST_LOG_(severity) - logs messages at the specified severity level. The
936 // message itself is streamed into the macro.
937 // LogToStderr() - directs all log messages to stderr.
938 // FlushInfoLog() - flushes informational log messages.
940 enum GTestLogSeverity
{
947 // Formats log entry severity, provides a stream object for streaming the
948 // log message, and terminates the message with a newline when going out of
950 class GTEST_API_ GTestLog
{
952 GTestLog(GTestLogSeverity severity
, const char* file
, int line
);
954 // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
957 ::std::ostream
& GetStream() { return ::std::cerr
; }
960 const GTestLogSeverity severity_
;
962 GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog
);
965 #if !defined(GTEST_LOG_)
967 # define GTEST_LOG_(severity) \
968 ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
969 __FILE__, __LINE__).GetStream()
971 inline void LogToStderr() {}
972 inline void FlushInfoLog() { fflush(nullptr); }
974 #endif // !defined(GTEST_LOG_)
976 #if !defined(GTEST_CHECK_)
977 // INTERNAL IMPLEMENTATION - DO NOT USE.
979 // GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition
982 // GTEST_CHECK_(boolean_condition);
984 // GTEST_CHECK_(boolean_condition) << "Additional message";
986 // This checks the condition and if the condition is not satisfied
987 // it prints message about the condition violation, including the
988 // condition itself, plus additional message streamed into it, if any,
989 // and then it aborts the program. It aborts the program irrespective of
990 // whether it is built in the debug mode or not.
991 # define GTEST_CHECK_(condition) \
992 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
993 if (::testing::internal::IsTrue(condition)) \
996 GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
997 #endif // !defined(GTEST_CHECK_)
999 // An all-mode assert to verify that the given POSIX-style function
1000 // call returns 0 (indicating success). Known limitation: this
1001 // doesn't expand to a balanced 'if' statement, so enclose the macro
1002 // in {} if you need to use it as the only statement in an 'if'
1004 #define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \
1005 if (const int gtest_error = (posix_call)) \
1006 GTEST_LOG_(FATAL) << #posix_call << "failed with error " \
1009 // Transforms "T" into "const T&" according to standard reference collapsing
1010 // rules (this is only needed as a backport for C++98 compilers that do not
1011 // support reference collapsing). Specifically, it transforms:
1013 // char ==> const char&
1014 // const char ==> const char&
1016 // const char& ==> const char&
1018 // Note that the non-const reference will not have "const" added. This is
1019 // standard, and necessary so that "T" can always bind to "const T&".
1020 template <typename T
>
1021 struct ConstRef
{ typedef const T
& type
; };
1022 template <typename T
>
1023 struct ConstRef
<T
&> { typedef T
& type
; };
1025 // The argument T must depend on some template parameters.
1026 #define GTEST_REFERENCE_TO_CONST_(T) \
1027 typename ::testing::internal::ConstRef<T>::type
1029 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
1031 // Use ImplicitCast_ as a safe version of static_cast for upcasting in
1032 // the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a
1033 // const Foo*). When you use ImplicitCast_, the compiler checks that
1034 // the cast is safe. Such explicit ImplicitCast_s are necessary in
1035 // surprisingly many situations where C++ demands an exact type match
1036 // instead of an argument type convertable to a target type.
1038 // The syntax for using ImplicitCast_ is the same as for static_cast:
1040 // ImplicitCast_<ToType>(expr)
1042 // ImplicitCast_ would have been part of the C++ standard library,
1043 // but the proposal was submitted too late. It will probably make
1044 // its way into the language in the future.
1046 // This relatively ugly name is intentional. It prevents clashes with
1047 // similar functions users may have (e.g., implicit_cast). The internal
1048 // namespace alone is not enough because the function can be found by ADL.
1049 template<typename To
>
1050 inline To
ImplicitCast_(To x
) { return x
; }
1052 // When you upcast (that is, cast a pointer from type Foo to type
1053 // SuperclassOfFoo), it's fine to use ImplicitCast_<>, since upcasts
1054 // always succeed. When you downcast (that is, cast a pointer from
1055 // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
1056 // how do you know the pointer is really of type SubclassOfFoo? It
1057 // could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
1058 // when you downcast, you should use this macro. In debug mode, we
1059 // use dynamic_cast<> to double-check the downcast is legal (we die
1060 // if it's not). In normal mode, we do the efficient static_cast<>
1061 // instead. Thus, it's important to test in debug mode to make sure
1062 // the cast is legal!
1063 // This is the only place in the code we should use dynamic_cast<>.
1064 // In particular, you SHOULDN'T be using dynamic_cast<> in order to
1065 // do RTTI (eg code like this:
1066 // if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
1067 // if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
1068 // You should design the code some other way not to need this.
1070 // This relatively ugly name is intentional. It prevents clashes with
1071 // similar functions users may have (e.g., down_cast). The internal
1072 // namespace alone is not enough because the function can be found by ADL.
1073 template<typename To
, typename From
> // use like this: DownCast_<T*>(foo);
1074 inline To
DownCast_(From
* f
) { // so we only accept pointers
1075 // Ensures that To is a sub-type of From *. This test is here only
1076 // for compile-time type checking, and has no overhead in an
1077 // optimized build at run-time, as it will be optimized away
1079 GTEST_INTENTIONAL_CONST_COND_PUSH_()
1081 GTEST_INTENTIONAL_CONST_COND_POP_()
1082 const To to
= nullptr;
1083 ::testing::internal::ImplicitCast_
<From
*>(to
);
1087 // RTTI: debug mode only!
1088 GTEST_CHECK_(f
== nullptr || dynamic_cast<To
>(f
) != nullptr);
1090 return static_cast<To
>(f
);
1093 // Downcasts the pointer of type Base to Derived.
1094 // Derived must be a subclass of Base. The parameter MUST
1095 // point to a class of type Derived, not any subclass of it.
1096 // When RTTI is available, the function performs a runtime
1097 // check to enforce this.
1098 template <class Derived
, class Base
>
1099 Derived
* CheckedDowncastToActualType(Base
* base
) {
1101 GTEST_CHECK_(typeid(*base
) == typeid(Derived
));
1104 #if GTEST_HAS_DOWNCAST_
1105 return ::down_cast
<Derived
*>(base
);
1106 #elif GTEST_HAS_RTTI
1107 return dynamic_cast<Derived
*>(base
); // NOLINT
1109 return static_cast<Derived
*>(base
); // Poor man's downcast.
1113 #if GTEST_HAS_STREAM_REDIRECTION
1115 // Defines the stderr capturer:
1116 // CaptureStdout - starts capturing stdout.
1117 // GetCapturedStdout - stops capturing stdout and returns the captured string.
1118 // CaptureStderr - starts capturing stderr.
1119 // GetCapturedStderr - stops capturing stderr and returns the captured string.
1121 GTEST_API_
void CaptureStdout();
1122 GTEST_API_
std::string
GetCapturedStdout();
1123 GTEST_API_
void CaptureStderr();
1124 GTEST_API_
std::string
GetCapturedStderr();
1126 #endif // GTEST_HAS_STREAM_REDIRECTION
1127 // Returns the size (in bytes) of a file.
1128 GTEST_API_
size_t GetFileSize(FILE* file
);
1130 // Reads the entire content of a file as a string.
1131 GTEST_API_
std::string
ReadEntireFile(FILE* file
);
1133 // All command line arguments.
1134 GTEST_API_
std::vector
<std::string
> GetArgvs();
1136 #if GTEST_HAS_DEATH_TEST
1138 std::vector
<std::string
> GetInjectableArgvs();
1139 // Deprecated: pass the args vector by value instead.
1140 void SetInjectableArgvs(const std::vector
<std::string
>* new_argvs
);
1141 void SetInjectableArgvs(const std::vector
<std::string
>& new_argvs
);
1142 void ClearInjectableArgvs();
1144 #endif // GTEST_HAS_DEATH_TEST
1146 // Defines synchronization primitives.
1147 #if GTEST_IS_THREADSAFE
1148 # if GTEST_HAS_PTHREAD
1149 // Sleeps for (roughly) n milliseconds. This function is only for testing
1150 // Google Test's own constructs. Don't use it in user tests, either
1151 // directly or indirectly.
1152 inline void SleepMilliseconds(int n
) {
1153 const timespec time
= {
1155 n
* 1000L * 1000L, // And n ms.
1157 nanosleep(&time
, nullptr);
1159 # endif // GTEST_HAS_PTHREAD
1161 # if GTEST_HAS_NOTIFICATION_
1162 // Notification has already been imported into the namespace.
1163 // Nothing to do here.
1165 # elif GTEST_HAS_PTHREAD
1166 // Allows a controller thread to pause execution of newly created
1167 // threads until notified. Instances of this class must be created
1168 // and destroyed in the controller thread.
1170 // This class is only for testing Google Test's own constructs. Do not
1171 // use it in user tests, either directly or indirectly.
1172 class Notification
{
1174 Notification() : notified_(false) {
1175 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_
, nullptr));
1178 pthread_mutex_destroy(&mutex_
);
1181 // Notifies all threads created with this notification to start. Must
1182 // be called from the controller thread.
1184 pthread_mutex_lock(&mutex_
);
1186 pthread_mutex_unlock(&mutex_
);
1189 // Blocks until the controller thread notifies. Must be called from a test
1191 void WaitForNotification() {
1193 pthread_mutex_lock(&mutex_
);
1194 const bool notified
= notified_
;
1195 pthread_mutex_unlock(&mutex_
);
1198 SleepMilliseconds(10);
1203 pthread_mutex_t mutex_
;
1206 GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification
);
1209 # elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
1211 GTEST_API_
void SleepMilliseconds(int n
);
1213 // Provides leak-safe Windows kernel handle ownership.
1214 // Used in death tests and in threading support.
1215 class GTEST_API_ AutoHandle
{
1217 // Assume that Win32 HANDLE type is equivalent to void*. Doing so allows us to
1218 // avoid including <windows.h> in this header file. Including <windows.h> is
1219 // undesirable because it defines a lot of symbols and macros that tend to
1220 // conflict with client code. This assumption is verified by
1221 // WindowsTypesTest.HANDLEIsVoidStar.
1222 typedef void* Handle
;
1224 explicit AutoHandle(Handle handle
);
1230 void Reset(Handle handle
);
1233 // Returns true if and only if the handle is a valid handle object that can be
1235 bool IsCloseable() const;
1239 GTEST_DISALLOW_COPY_AND_ASSIGN_(AutoHandle
);
1242 // Allows a controller thread to pause execution of newly created
1243 // threads until notified. Instances of this class must be created
1244 // and destroyed in the controller thread.
1246 // This class is only for testing Google Test's own constructs. Do not
1247 // use it in user tests, either directly or indirectly.
1248 class GTEST_API_ Notification
{
1252 void WaitForNotification();
1257 GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification
);
1259 # endif // GTEST_HAS_NOTIFICATION_
1261 // On MinGW, we can have both GTEST_OS_WINDOWS and GTEST_HAS_PTHREAD
1262 // defined, but we don't want to use MinGW's pthreads implementation, which
1263 // has conformance problems with some versions of the POSIX standard.
1264 # if GTEST_HAS_PTHREAD && !GTEST_OS_WINDOWS_MINGW
1266 // As a C-function, ThreadFuncWithCLinkage cannot be templated itself.
1267 // Consequently, it cannot select a correct instantiation of ThreadWithParam
1268 // in order to call its Run(). Introducing ThreadWithParamBase as a
1269 // non-templated base class for ThreadWithParam allows us to bypass this
1271 class ThreadWithParamBase
{
1273 virtual ~ThreadWithParamBase() {}
1274 virtual void Run() = 0;
1277 // pthread_create() accepts a pointer to a function type with the C linkage.
1278 // According to the Standard (7.5/1), function types with different linkages
1279 // are different even if they are otherwise identical. Some compilers (for
1280 // example, SunStudio) treat them as different types. Since class methods
1281 // cannot be defined with C-linkage we need to define a free C-function to
1282 // pass into pthread_create().
1283 extern "C" inline void* ThreadFuncWithCLinkage(void* thread
) {
1284 static_cast<ThreadWithParamBase
*>(thread
)->Run();
1288 // Helper class for testing Google Test's multi-threading constructs.
1289 // To use it, write:
1291 // void ThreadFunc(int param) { /* Do things with param */ }
1292 // Notification thread_can_start;
1294 // // The thread_can_start parameter is optional; you can supply NULL.
1295 // ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start);
1296 // thread_can_start.Notify();
1298 // These classes are only for testing Google Test's own constructs. Do
1299 // not use them in user tests, either directly or indirectly.
1300 template <typename T
>
1301 class ThreadWithParam
: public ThreadWithParamBase
{
1303 typedef void UserThreadFunc(T
);
1305 ThreadWithParam(UserThreadFunc
* func
, T param
, Notification
* thread_can_start
)
1308 thread_can_start_(thread_can_start
),
1310 ThreadWithParamBase
* const base
= this;
1311 // The thread can be created only after all fields except thread_
1312 // have been initialized.
1313 GTEST_CHECK_POSIX_SUCCESS_(
1314 pthread_create(&thread_
, nullptr, &ThreadFuncWithCLinkage
, base
));
1316 ~ThreadWithParam() override
{ Join(); }
1320 GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_
, nullptr));
1325 void Run() override
{
1326 if (thread_can_start_
!= nullptr) thread_can_start_
->WaitForNotification();
1331 UserThreadFunc
* const func_
; // User-supplied thread function.
1332 const T param_
; // User-supplied parameter to the thread function.
1333 // When non-NULL, used to block execution until the controller thread
1335 Notification
* const thread_can_start_
;
1336 bool finished_
; // true if and only if we know that the thread function has
1338 pthread_t thread_
; // The native thread object.
1340 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam
);
1342 # endif // !GTEST_OS_WINDOWS && GTEST_HAS_PTHREAD ||
1343 // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
1345 # if GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
1346 // Mutex and ThreadLocal have already been imported into the namespace.
1347 // Nothing to do here.
1349 # elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
1351 // Mutex implements mutex on Windows platforms. It is used in conjunction
1352 // with class MutexLock:
1356 // MutexLock lock(&mutex); // Acquires the mutex and releases it at the
1357 // // end of the current scope.
1359 // A static Mutex *must* be defined or declared using one of the following
1361 // GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex);
1362 // GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex);
1364 // (A non-static Mutex is defined/declared in the usual way).
1365 class GTEST_API_ Mutex
{
1367 enum MutexType
{ kStatic
= 0, kDynamic
= 1 };
1368 // We rely on kStaticMutex being 0 as it is to what the linker initializes
1369 // type_ in static mutexes. critical_section_ will be initialized lazily
1370 // in ThreadSafeLazyInit().
1371 enum StaticConstructorSelector
{ kStaticMutex
= 0 };
1373 // This constructor intentionally does nothing. It relies on type_ being
1374 // statically initialized to 0 (effectively setting it to kStatic) and on
1375 // ThreadSafeLazyInit() to lazily initialize the rest of the members.
1376 explicit Mutex(StaticConstructorSelector
/*dummy*/) {}
1385 // Does nothing if the current thread holds the mutex. Otherwise, crashes
1386 // with high probability.
1390 // Initializes owner_thread_id_ and critical_section_ in static mutexes.
1391 void ThreadSafeLazyInit();
1393 // Per https://blogs.msdn.microsoft.com/oldnewthing/20040223-00/?p=40503,
1394 // we assume that 0 is an invalid value for thread IDs.
1395 unsigned int owner_thread_id_
;
1397 // For static mutexes, we rely on these members being initialized to zeros
1400 long critical_section_init_phase_
; // NOLINT
1401 GTEST_CRITICAL_SECTION
* critical_section_
;
1403 GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex
);
1406 # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1407 extern ::testing::internal::Mutex mutex
1409 # define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1410 ::testing::internal::Mutex mutex(::testing::internal::Mutex::kStaticMutex)
1412 // We cannot name this class MutexLock because the ctor declaration would
1413 // conflict with a macro named MutexLock, which is defined on some
1414 // platforms. That macro is used as a defensive measure to prevent against
1415 // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than
1416 // "MutexLock l(&mu)". Hence the typedef trick below.
1417 class GTestMutexLock
{
1419 explicit GTestMutexLock(Mutex
* mutex
)
1420 : mutex_(mutex
) { mutex_
->Lock(); }
1422 ~GTestMutexLock() { mutex_
->Unlock(); }
1425 Mutex
* const mutex_
;
1427 GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock
);
1430 typedef GTestMutexLock MutexLock
;
1432 // Base class for ValueHolder<T>. Allows a caller to hold and delete a value
1433 // without knowing its type.
1434 class ThreadLocalValueHolderBase
{
1436 virtual ~ThreadLocalValueHolderBase() {}
1439 // Provides a way for a thread to send notifications to a ThreadLocal
1440 // regardless of its parameter type.
1441 class ThreadLocalBase
{
1443 // Creates a new ValueHolder<T> object holding a default value passed to
1444 // this ThreadLocal<T>'s constructor and returns it. It is the caller's
1445 // responsibility not to call this when the ThreadLocal<T> instance already
1446 // has a value on the current thread.
1447 virtual ThreadLocalValueHolderBase
* NewValueForCurrentThread() const = 0;
1450 ThreadLocalBase() {}
1451 virtual ~ThreadLocalBase() {}
1454 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocalBase
);
1457 // Maps a thread to a set of ThreadLocals that have values instantiated on that
1458 // thread and notifies them when the thread exits. A ThreadLocal instance is
1459 // expected to persist until all threads it has values on have terminated.
1460 class GTEST_API_ ThreadLocalRegistry
{
1462 // Registers thread_local_instance as having value on the current thread.
1463 // Returns a value that can be used to identify the thread from other threads.
1464 static ThreadLocalValueHolderBase
* GetValueOnCurrentThread(
1465 const ThreadLocalBase
* thread_local_instance
);
1467 // Invoked when a ThreadLocal instance is destroyed.
1468 static void OnThreadLocalDestroyed(
1469 const ThreadLocalBase
* thread_local_instance
);
1472 class GTEST_API_ ThreadWithParamBase
{
1479 virtual ~Runnable() {}
1480 virtual void Run() = 0;
1483 ThreadWithParamBase(Runnable
*runnable
, Notification
* thread_can_start
);
1484 virtual ~ThreadWithParamBase();
1490 // Helper class for testing Google Test's multi-threading constructs.
1491 template <typename T
>
1492 class ThreadWithParam
: public ThreadWithParamBase
{
1494 typedef void UserThreadFunc(T
);
1496 ThreadWithParam(UserThreadFunc
* func
, T param
, Notification
* thread_can_start
)
1497 : ThreadWithParamBase(new RunnableImpl(func
, param
), thread_can_start
) {
1499 virtual ~ThreadWithParam() {}
1502 class RunnableImpl
: public Runnable
{
1504 RunnableImpl(UserThreadFunc
* func
, T param
)
1508 virtual ~RunnableImpl() {}
1509 virtual void Run() {
1514 UserThreadFunc
* const func_
;
1517 GTEST_DISALLOW_COPY_AND_ASSIGN_(RunnableImpl
);
1520 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam
);
1523 // Implements thread-local storage on Windows systems.
1526 // ThreadLocal<int> tl(100); // 100 is the default value for each thread.
1529 // tl.set(150); // Changes the value for thread 2 only.
1530 // EXPECT_EQ(150, tl.get());
1533 // EXPECT_EQ(100, tl.get()); // In thread 1, tl has the original value.
1535 // EXPECT_EQ(200, tl.get());
1537 // The template type argument T must have a public copy constructor.
1538 // In addition, the default ThreadLocal constructor requires T to have
1539 // a public default constructor.
1541 // The users of a TheadLocal instance have to make sure that all but one
1542 // threads (including the main one) using that instance have exited before
1543 // destroying it. Otherwise, the per-thread objects managed for them by the
1544 // ThreadLocal instance are not guaranteed to be destroyed on all platforms.
1546 // Google Test only uses global ThreadLocal objects. That means they
1547 // will die after main() has returned. Therefore, no per-thread
1548 // object managed by Google Test will be leaked as long as all threads
1549 // using Google Test have exited when main() returns.
1550 template <typename T
>
1551 class ThreadLocal
: public ThreadLocalBase
{
1553 ThreadLocal() : default_factory_(new DefaultValueHolderFactory()) {}
1554 explicit ThreadLocal(const T
& value
)
1555 : default_factory_(new InstanceValueHolderFactory(value
)) {}
1557 ~ThreadLocal() { ThreadLocalRegistry::OnThreadLocalDestroyed(this); }
1559 T
* pointer() { return GetOrCreateValue(); }
1560 const T
* pointer() const { return GetOrCreateValue(); }
1561 const T
& get() const { return *pointer(); }
1562 void set(const T
& value
) { *pointer() = value
; }
1565 // Holds a value of T. Can be deleted via its base class without the caller
1566 // knowing the type of T.
1567 class ValueHolder
: public ThreadLocalValueHolderBase
{
1569 ValueHolder() : value_() {}
1570 explicit ValueHolder(const T
& value
) : value_(value
) {}
1572 T
* pointer() { return &value_
; }
1576 GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder
);
1580 T
* GetOrCreateValue() const {
1581 return static_cast<ValueHolder
*>(
1582 ThreadLocalRegistry::GetValueOnCurrentThread(this))->pointer();
1585 virtual ThreadLocalValueHolderBase
* NewValueForCurrentThread() const {
1586 return default_factory_
->MakeNewHolder();
1589 class ValueHolderFactory
{
1591 ValueHolderFactory() {}
1592 virtual ~ValueHolderFactory() {}
1593 virtual ValueHolder
* MakeNewHolder() const = 0;
1596 GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolderFactory
);
1599 class DefaultValueHolderFactory
: public ValueHolderFactory
{
1601 DefaultValueHolderFactory() {}
1602 virtual ValueHolder
* MakeNewHolder() const { return new ValueHolder(); }
1605 GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultValueHolderFactory
);
1608 class InstanceValueHolderFactory
: public ValueHolderFactory
{
1610 explicit InstanceValueHolderFactory(const T
& value
) : value_(value
) {}
1611 virtual ValueHolder
* MakeNewHolder() const {
1612 return new ValueHolder(value_
);
1616 const T value_
; // The value for each thread.
1618 GTEST_DISALLOW_COPY_AND_ASSIGN_(InstanceValueHolderFactory
);
1621 std::unique_ptr
<ValueHolderFactory
> default_factory_
;
1623 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal
);
1626 # elif GTEST_HAS_PTHREAD
1628 // MutexBase and Mutex implement mutex on pthreads-based platforms.
1631 // Acquires this mutex.
1633 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_
));
1634 owner_
= pthread_self();
1638 // Releases this mutex.
1640 // Since the lock is being released the owner_ field should no longer be
1641 // considered valid. We don't protect writing to has_owner_ here, as it's
1642 // the caller's responsibility to ensure that the current thread holds the
1643 // mutex when this is called.
1645 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_
));
1648 // Does nothing if the current thread holds the mutex. Otherwise, crashes
1649 // with high probability.
1650 void AssertHeld() const {
1651 GTEST_CHECK_(has_owner_
&& pthread_equal(owner_
, pthread_self()))
1652 << "The current thread is not holding the mutex @" << this;
1655 // A static mutex may be used before main() is entered. It may even
1656 // be used before the dynamic initialization stage. Therefore we
1657 // must be able to initialize a static mutex object at link time.
1658 // This means MutexBase has to be a POD and its member variables
1659 // have to be public.
1661 pthread_mutex_t mutex_
; // The underlying pthread mutex.
1662 // has_owner_ indicates whether the owner_ field below contains a valid thread
1663 // ID and is therefore safe to inspect (e.g., to use in pthread_equal()). All
1664 // accesses to the owner_ field should be protected by a check of this field.
1665 // An alternative might be to memset() owner_ to all zeros, but there's no
1666 // guarantee that a zero'd pthread_t is necessarily invalid or even different
1667 // from pthread_self().
1669 pthread_t owner_
; // The thread holding the mutex.
1672 // Forward-declares a static mutex.
1673 # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1674 extern ::testing::internal::MutexBase mutex
1676 // Defines and statically (i.e. at link time) initializes a static mutex.
1677 // The initialization list here does not explicitly initialize each field,
1678 // instead relying on default initialization for the unspecified fields. In
1679 // particular, the owner_ field (a pthread_t) is not explicitly initialized.
1680 // This allows initialization to work whether pthread_t is a scalar or struct.
1681 // The flag -Wmissing-field-initializers must not be specified for this to work.
1682 #define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1683 ::testing::internal::MutexBase mutex = {PTHREAD_MUTEX_INITIALIZER, false, 0}
1685 // The Mutex class can only be used for mutexes created at runtime. It
1686 // shares its API with MutexBase otherwise.
1687 class Mutex
: public MutexBase
{
1690 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_
, nullptr));
1694 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_
));
1698 GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex
);
1701 // We cannot name this class MutexLock because the ctor declaration would
1702 // conflict with a macro named MutexLock, which is defined on some
1703 // platforms. That macro is used as a defensive measure to prevent against
1704 // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than
1705 // "MutexLock l(&mu)". Hence the typedef trick below.
1706 class GTestMutexLock
{
1708 explicit GTestMutexLock(MutexBase
* mutex
)
1709 : mutex_(mutex
) { mutex_
->Lock(); }
1711 ~GTestMutexLock() { mutex_
->Unlock(); }
1714 MutexBase
* const mutex_
;
1716 GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock
);
1719 typedef GTestMutexLock MutexLock
;
1721 // Helpers for ThreadLocal.
1723 // pthread_key_create() requires DeleteThreadLocalValue() to have
1724 // C-linkage. Therefore it cannot be templatized to access
1725 // ThreadLocal<T>. Hence the need for class
1726 // ThreadLocalValueHolderBase.
1727 class ThreadLocalValueHolderBase
{
1729 virtual ~ThreadLocalValueHolderBase() {}
1732 // Called by pthread to delete thread-local data stored by
1733 // pthread_setspecific().
1734 extern "C" inline void DeleteThreadLocalValue(void* value_holder
) {
1735 delete static_cast<ThreadLocalValueHolderBase
*>(value_holder
);
1738 // Implements thread-local storage on pthreads-based systems.
1739 template <typename T
>
1740 class GTEST_API_ ThreadLocal
{
1743 : key_(CreateKey()), default_factory_(new DefaultValueHolderFactory()) {}
1744 explicit ThreadLocal(const T
& value
)
1745 : key_(CreateKey()),
1746 default_factory_(new InstanceValueHolderFactory(value
)) {}
1749 // Destroys the managed object for the current thread, if any.
1750 DeleteThreadLocalValue(pthread_getspecific(key_
));
1752 // Releases resources associated with the key. This will *not*
1753 // delete managed objects for other threads.
1754 GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_
));
1757 T
* pointer() { return GetOrCreateValue(); }
1758 const T
* pointer() const { return GetOrCreateValue(); }
1759 const T
& get() const { return *pointer(); }
1760 void set(const T
& value
) { *pointer() = value
; }
1763 // Holds a value of type T.
1764 class ValueHolder
: public ThreadLocalValueHolderBase
{
1766 ValueHolder() : value_() {}
1767 explicit ValueHolder(const T
& value
) : value_(value
) {}
1769 T
* pointer() { return &value_
; }
1773 GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder
);
1776 static pthread_key_t
CreateKey() {
1778 // When a thread exits, DeleteThreadLocalValue() will be called on
1779 // the object managed for that thread.
1780 GTEST_CHECK_POSIX_SUCCESS_(
1781 pthread_key_create(&key
, &DeleteThreadLocalValue
));
1785 T
* GetOrCreateValue() const {
1786 ThreadLocalValueHolderBase
* const holder
=
1787 static_cast<ThreadLocalValueHolderBase
*>(pthread_getspecific(key_
));
1788 if (holder
!= nullptr) {
1789 return CheckedDowncastToActualType
<ValueHolder
>(holder
)->pointer();
1792 ValueHolder
* const new_holder
= default_factory_
->MakeNewHolder();
1793 ThreadLocalValueHolderBase
* const holder_base
= new_holder
;
1794 GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_
, holder_base
));
1795 return new_holder
->pointer();
1798 class ValueHolderFactory
{
1800 ValueHolderFactory() {}
1801 virtual ~ValueHolderFactory() {}
1802 virtual ValueHolder
* MakeNewHolder() const = 0;
1805 GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolderFactory
);
1808 class DefaultValueHolderFactory
: public ValueHolderFactory
{
1810 DefaultValueHolderFactory() {}
1811 virtual ValueHolder
* MakeNewHolder() const { return new ValueHolder(); }
1814 GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultValueHolderFactory
);
1817 class InstanceValueHolderFactory
: public ValueHolderFactory
{
1819 explicit InstanceValueHolderFactory(const T
& value
) : value_(value
) {}
1820 virtual ValueHolder
* MakeNewHolder() const {
1821 return new ValueHolder(value_
);
1825 const T value_
; // The value for each thread.
1827 GTEST_DISALLOW_COPY_AND_ASSIGN_(InstanceValueHolderFactory
);
1830 // A key pthreads uses for looking up per-thread values.
1831 const pthread_key_t key_
;
1832 std::unique_ptr
<ValueHolderFactory
> default_factory_
;
1834 GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal
);
1837 # endif // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
1839 #else // GTEST_IS_THREADSAFE
1841 // A dummy implementation of synchronization primitives (mutex, lock,
1842 // and thread-local variable). Necessary for compiling Google Test where
1843 // mutex is not supported - using Google Test in multiple threads is not
1844 // supported on such platforms.
1851 void AssertHeld() const {}
1854 # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1855 extern ::testing::internal::Mutex mutex
1857 # define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex
1859 // We cannot name this class MutexLock because the ctor declaration would
1860 // conflict with a macro named MutexLock, which is defined on some
1861 // platforms. That macro is used as a defensive measure to prevent against
1862 // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than
1863 // "MutexLock l(&mu)". Hence the typedef trick below.
1864 class GTestMutexLock
{
1866 explicit GTestMutexLock(Mutex
*) {} // NOLINT
1869 typedef GTestMutexLock MutexLock
;
1871 template <typename T
>
1872 class GTEST_API_ ThreadLocal
{
1874 ThreadLocal() : value_() {}
1875 explicit ThreadLocal(const T
& value
) : value_(value
) {}
1876 T
* pointer() { return &value_
; }
1877 const T
* pointer() const { return &value_
; }
1878 const T
& get() const { return value_
; }
1879 void set(const T
& value
) { value_
= value
; }
1884 #endif // GTEST_IS_THREADSAFE
1886 // Returns the number of threads running in the process, or 0 to indicate that
1887 // we cannot detect it.
1888 GTEST_API_
size_t GetThreadCount();
1891 using bool_constant
= std::integral_constant
<bool, B
>;
1893 #if GTEST_OS_WINDOWS
1894 # define GTEST_PATH_SEP_ "\\"
1895 # define GTEST_HAS_ALT_PATH_SEP_ 1
1896 // The biggest signed integer type the compiler supports.
1897 typedef __int64 BiggestInt
;
1899 # define GTEST_PATH_SEP_ "/"
1900 # define GTEST_HAS_ALT_PATH_SEP_ 0
1901 typedef long long BiggestInt
; // NOLINT
1902 #endif // GTEST_OS_WINDOWS
1904 // Utilities for char.
1906 // isspace(int ch) and friends accept an unsigned char or EOF. char
1907 // may be signed, depending on the compiler (or compiler flags).
1908 // Therefore we need to cast a char to unsigned char before calling
1911 inline bool IsAlpha(char ch
) {
1912 return isalpha(static_cast<unsigned char>(ch
)) != 0;
1914 inline bool IsAlNum(char ch
) {
1915 return isalnum(static_cast<unsigned char>(ch
)) != 0;
1917 inline bool IsDigit(char ch
) {
1918 return isdigit(static_cast<unsigned char>(ch
)) != 0;
1920 inline bool IsLower(char ch
) {
1921 return islower(static_cast<unsigned char>(ch
)) != 0;
1923 inline bool IsSpace(char ch
) {
1924 return isspace(static_cast<unsigned char>(ch
)) != 0;
1926 inline bool IsUpper(char ch
) {
1927 return isupper(static_cast<unsigned char>(ch
)) != 0;
1929 inline bool IsXDigit(char ch
) {
1930 return isxdigit(static_cast<unsigned char>(ch
)) != 0;
1932 inline bool IsXDigit(wchar_t ch
) {
1933 const unsigned char low_byte
= static_cast<unsigned char>(ch
);
1934 return ch
== low_byte
&& isxdigit(low_byte
) != 0;
1937 inline char ToLower(char ch
) {
1938 return static_cast<char>(tolower(static_cast<unsigned char>(ch
)));
1940 inline char ToUpper(char ch
) {
1941 return static_cast<char>(toupper(static_cast<unsigned char>(ch
)));
1944 inline std::string
StripTrailingSpaces(std::string str
) {
1945 std::string::iterator it
= str
.end();
1946 while (it
!= str
.begin() && IsSpace(*--it
))
1951 // The testing::internal::posix namespace holds wrappers for common
1952 // POSIX functions. These wrappers hide the differences between
1953 // Windows/MSVC and POSIX systems. Since some compilers define these
1954 // standard functions as macros, the wrapper cannot have the same name
1955 // as the wrapped function.
1959 // Functions with a different name on Windows.
1961 #if GTEST_OS_WINDOWS
1963 typedef struct _stat StatStruct
;
1965 # ifdef __BORLANDC__
1966 inline int IsATTY(int fd
) { return isatty(fd
); }
1967 inline int StrCaseCmp(const char* s1
, const char* s2
) {
1968 return stricmp(s1
, s2
);
1970 inline char* StrDup(const char* src
) { return strdup(src
); }
1971 # else // !__BORLANDC__
1972 # if GTEST_OS_WINDOWS_MOBILE
1973 inline int IsATTY(int /* fd */) { return 0; }
1975 inline int IsATTY(int fd
) { return _isatty(fd
); }
1976 # endif // GTEST_OS_WINDOWS_MOBILE
1977 inline int StrCaseCmp(const char* s1
, const char* s2
) {
1978 return _stricmp(s1
, s2
);
1980 inline char* StrDup(const char* src
) { return _strdup(src
); }
1981 # endif // __BORLANDC__
1983 # if GTEST_OS_WINDOWS_MOBILE
1984 inline int FileNo(FILE* file
) { return reinterpret_cast<int>(_fileno(file
)); }
1985 // Stat(), RmDir(), and IsDir() are not needed on Windows CE at this
1986 // time and thus not defined there.
1988 inline int FileNo(FILE* file
) { return _fileno(file
); }
1989 inline int Stat(const char* path
, StatStruct
* buf
) { return _stat(path
, buf
); }
1990 inline int RmDir(const char* dir
) { return _rmdir(dir
); }
1991 inline bool IsDir(const StatStruct
& st
) {
1992 return (_S_IFDIR
& st
.st_mode
) != 0;
1994 # endif // GTEST_OS_WINDOWS_MOBILE
1998 typedef struct stat StatStruct
;
2000 inline int FileNo(FILE* file
) { return fileno(file
); }
2001 inline int IsATTY(int fd
) { return isatty(fd
); }
2002 inline int Stat(const char* path
, StatStruct
* buf
) { return stat(path
, buf
); }
2003 inline int StrCaseCmp(const char* s1
, const char* s2
) {
2004 return strcasecmp(s1
, s2
);
2006 inline char* StrDup(const char* src
) { return strdup(src
); }
2007 inline int RmDir(const char* dir
) { return rmdir(dir
); }
2008 inline bool IsDir(const StatStruct
& st
) { return S_ISDIR(st
.st_mode
); }
2010 #endif // GTEST_OS_WINDOWS
2012 // Functions deprecated by MSVC 8.0.
2014 GTEST_DISABLE_MSC_DEPRECATED_PUSH_()
2016 inline const char* StrNCpy(char* dest
, const char* src
, size_t n
) {
2017 return strncpy(dest
, src
, n
);
2020 // ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and
2021 // StrError() aren't needed on Windows CE at this time and thus not
2024 #if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
2025 inline int ChDir(const char* dir
) { return chdir(dir
); }
2027 inline FILE* FOpen(const char* path
, const char* mode
) {
2028 return fopen(path
, mode
);
2030 #if !GTEST_OS_WINDOWS_MOBILE
2031 inline FILE *FReopen(const char* path
, const char* mode
, FILE* stream
) {
2032 return freopen(path
, mode
, stream
);
2034 inline FILE* FDOpen(int fd
, const char* mode
) { return fdopen(fd
, mode
); }
2036 inline int FClose(FILE* fp
) { return fclose(fp
); }
2037 #if !GTEST_OS_WINDOWS_MOBILE
2038 inline int Read(int fd
, void* buf
, unsigned int count
) {
2039 return static_cast<int>(read(fd
, buf
, count
));
2041 inline int Write(int fd
, const void* buf
, unsigned int count
) {
2042 return static_cast<int>(write(fd
, buf
, count
));
2044 inline int Close(int fd
) { return close(fd
); }
2045 inline const char* StrError(int errnum
) { return strerror(errnum
); }
2047 inline const char* GetEnv(const char* name
) {
2048 #if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT
2049 // We are on Windows CE, which has no environment variables.
2050 static_cast<void>(name
); // To prevent 'unused argument' warning.
2052 #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
2053 // Environment variables which we programmatically clear will be set to the
2054 // empty string rather than unset (NULL). Handle that case.
2055 const char* const env
= getenv(name
);
2056 return (env
!= nullptr && env
[0] != '\0') ? env
: nullptr;
2058 return getenv(name
);
2062 GTEST_DISABLE_MSC_DEPRECATED_POP_()
2064 #if GTEST_OS_WINDOWS_MOBILE
2065 // Windows CE has no C library. The abort() function is used in
2066 // several places in Google Test. This implementation provides a reasonable
2067 // imitation of standard behaviour.
2068 [[noreturn
]] void Abort();
2070 [[noreturn
]] inline void Abort() { abort(); }
2071 #endif // GTEST_OS_WINDOWS_MOBILE
2073 } // namespace posix
2075 // MSVC "deprecates" snprintf and issues warnings wherever it is used. In
2076 // order to avoid these warnings, we need to use _snprintf or _snprintf_s on
2077 // MSVC-based platforms. We map the GTEST_SNPRINTF_ macro to the appropriate
2078 // function in order to achieve that. We use macro definition here because
2079 // snprintf is a variadic function.
2080 #if _MSC_VER && !GTEST_OS_WINDOWS_MOBILE
2081 // MSVC 2005 and above support variadic macros.
2082 # define GTEST_SNPRINTF_(buffer, size, format, ...) \
2083 _snprintf_s(buffer, size, size, format, __VA_ARGS__)
2084 #elif defined(_MSC_VER)
2085 // Windows CE does not define _snprintf_s
2086 # define GTEST_SNPRINTF_ _snprintf
2088 # define GTEST_SNPRINTF_ snprintf
2091 // The maximum number a BiggestInt can represent. This definition
2092 // works no matter BiggestInt is represented in one's complement or
2093 // two's complement.
2095 // We cannot rely on numeric_limits in STL, as __int64 and long long
2096 // are not part of standard C++ and numeric_limits doesn't need to be
2097 // defined for them.
2098 const BiggestInt kMaxBiggestInt
=
2099 ~(static_cast<BiggestInt
>(1) << (8*sizeof(BiggestInt
) - 1));
2101 // This template class serves as a compile-time function from size to
2102 // type. It maps a size in bytes to a primitive type with that
2105 // TypeWithSize<4>::UInt
2107 // is typedef-ed to be unsigned int (unsigned integer made up of 4
2110 // Such functionality should belong to STL, but I cannot find it
2113 // Google Test uses this class in the implementation of floating-point
2116 // For now it only handles UInt (unsigned int) as that's all Google Test
2117 // needs. Other types can be easily added in the future if need
2119 template <size_t size
>
2120 class TypeWithSize
{
2122 // This prevents the user from using TypeWithSize<N> with incorrect
2127 // The specialization for size 4.
2129 class TypeWithSize
<4> {
2131 // unsigned int has size 4 in both gcc and MSVC.
2133 // As base/basictypes.h doesn't compile on Windows, we cannot use
2134 // uint32, uint64, and etc here.
2136 typedef unsigned int UInt
;
2139 // The specialization for size 8.
2141 class TypeWithSize
<8> {
2143 #if GTEST_OS_WINDOWS
2144 typedef __int64 Int
;
2145 typedef unsigned __int64 UInt
;
2147 typedef long long Int
; // NOLINT
2148 typedef unsigned long long UInt
; // NOLINT
2149 #endif // GTEST_OS_WINDOWS
2152 // Integer types of known sizes.
2153 typedef TypeWithSize
<4>::Int Int32
;
2154 typedef TypeWithSize
<4>::UInt UInt32
;
2155 typedef TypeWithSize
<8>::Int Int64
;
2156 typedef TypeWithSize
<8>::UInt UInt64
;
2157 typedef TypeWithSize
<8>::Int TimeInMillis
; // Represents time in milliseconds.
2159 // Utilities for command line flags and environment variables.
2161 // Macro for referencing flags.
2162 #if !defined(GTEST_FLAG)
2163 # define GTEST_FLAG(name) FLAGS_gtest_##name
2164 #endif // !defined(GTEST_FLAG)
2166 #if !defined(GTEST_USE_OWN_FLAGFILE_FLAG_)
2167 # define GTEST_USE_OWN_FLAGFILE_FLAG_ 1
2168 #endif // !defined(GTEST_USE_OWN_FLAGFILE_FLAG_)
2170 #if !defined(GTEST_DECLARE_bool_)
2171 # define GTEST_FLAG_SAVER_ ::testing::internal::GTestFlagSaver
2173 // Macros for declaring flags.
2174 # define GTEST_DECLARE_bool_(name) GTEST_API_ extern bool GTEST_FLAG(name)
2175 # define GTEST_DECLARE_int32_(name) \
2176 GTEST_API_ extern ::testing::internal::Int32 GTEST_FLAG(name)
2177 # define GTEST_DECLARE_string_(name) \
2178 GTEST_API_ extern ::std::string GTEST_FLAG(name)
2180 // Macros for defining flags.
2181 # define GTEST_DEFINE_bool_(name, default_val, doc) \
2182 GTEST_API_ bool GTEST_FLAG(name) = (default_val)
2183 # define GTEST_DEFINE_int32_(name, default_val, doc) \
2184 GTEST_API_ ::testing::internal::Int32 GTEST_FLAG(name) = (default_val)
2185 # define GTEST_DEFINE_string_(name, default_val, doc) \
2186 GTEST_API_ ::std::string GTEST_FLAG(name) = (default_val)
2188 #endif // !defined(GTEST_DECLARE_bool_)
2190 // Thread annotations
2191 #if !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)
2192 # define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
2193 # define GTEST_LOCK_EXCLUDED_(locks)
2194 #endif // !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)
2196 // Parses 'str' for a 32-bit signed integer. If successful, writes the result
2197 // to *value and returns true; otherwise leaves *value unchanged and returns
2199 bool ParseInt32(const Message
& src_text
, const char* str
, Int32
* value
);
2201 // Parses a bool/Int32/string from the environment variable
2202 // corresponding to the given Google Test flag.
2203 bool BoolFromGTestEnv(const char* flag
, bool default_val
);
2204 GTEST_API_ Int32
Int32FromGTestEnv(const char* flag
, Int32 default_val
);
2205 std::string
OutputFlagAlsoCheckEnvVar();
2206 const char* StringFromGTestEnv(const char* flag
, const char* default_val
);
2208 } // namespace internal
2209 } // namespace testing
2211 #if !defined(GTEST_INTERNAL_DEPRECATED)
2213 // Internal Macro to mark an API deprecated, for googletest usage only
2214 // Usage: class GTEST_INTERNAL_DEPRECATED(message) MyClass or
2215 // GTEST_INTERNAL_DEPRECATED(message) <return_type> myFunction(); Every usage of
2216 // a deprecated entity will trigger a warning when compiled with
2217 // `-Wdeprecated-declarations` option (clang, gcc, any __GNUC__ compiler).
2218 // For msvc /W3 option will need to be used
2219 // Note that for 'other' compilers this macro evaluates to nothing to prevent
2220 // compilations errors.
2221 #if defined(_MSC_VER)
2222 #define GTEST_INTERNAL_DEPRECATED(message) __declspec(deprecated(message))
2223 #elif defined(__GNUC__)
2224 #define GTEST_INTERNAL_DEPRECATED(message) __attribute__((deprecated(message)))
2226 #define GTEST_INTERNAL_DEPRECATED(message)
2229 #endif // !defined(GTEST_INTERNAL_DEPRECATED)
2231 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_