1b89a7cc2SEnji Cooper // Copyright 2005, Google Inc.
2b89a7cc2SEnji Cooper // All rights reserved.
3b89a7cc2SEnji Cooper //
4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6b89a7cc2SEnji Cooper // met:
7b89a7cc2SEnji Cooper //
8b89a7cc2SEnji Cooper //     * Redistributions of source code must retain the above copyright
9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10b89a7cc2SEnji Cooper //     * Redistributions in binary form must reproduce the above
11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13b89a7cc2SEnji Cooper // distribution.
14b89a7cc2SEnji Cooper //     * Neither the name of Google Inc. nor the names of its
15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16b89a7cc2SEnji Cooper // this software without specific prior written permission.
17b89a7cc2SEnji Cooper //
18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2928f6c2f2SEnji Cooper 
30b89a7cc2SEnji Cooper // Low-level types and utilities for porting Google Test to various
31b89a7cc2SEnji Cooper // platforms.  All macros ending with _ and symbols defined in an
32b89a7cc2SEnji Cooper // internal namespace are subject to change without notice.  Code
33b89a7cc2SEnji Cooper // outside Google Test MUST NOT USE THEM DIRECTLY.  Macros that don't
34b89a7cc2SEnji Cooper // end with _ are part of Google Test's public API and can be used by
35b89a7cc2SEnji Cooper // code outside Google Test.
36b89a7cc2SEnji Cooper //
37b89a7cc2SEnji Cooper // This file is fundamental to Google Test.  All other Google Test source
38b89a7cc2SEnji Cooper // files are expected to #include this.  Therefore, it cannot #include
39b89a7cc2SEnji Cooper // any other Google Test header.
40b89a7cc2SEnji Cooper 
4128f6c2f2SEnji Cooper // IWYU pragma: private, include "gtest/gtest.h"
4228f6c2f2SEnji Cooper // IWYU pragma: friend gtest/.*
4328f6c2f2SEnji Cooper // IWYU pragma: friend gmock/.*
44b89a7cc2SEnji Cooper 
4528f6c2f2SEnji Cooper #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
4628f6c2f2SEnji Cooper #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
47b89a7cc2SEnji Cooper 
48b89a7cc2SEnji Cooper // Environment-describing macros
49b89a7cc2SEnji Cooper // -----------------------------
50b89a7cc2SEnji Cooper //
51b89a7cc2SEnji Cooper // Google Test can be used in many different environments.  Macros in
52b89a7cc2SEnji Cooper // this section tell Google Test what kind of environment it is being
53b89a7cc2SEnji Cooper // used in, such that Google Test can provide environment-specific
54b89a7cc2SEnji Cooper // features and implementations.
55b89a7cc2SEnji Cooper //
56b89a7cc2SEnji Cooper // Google Test tries to automatically detect the properties of its
57b89a7cc2SEnji Cooper // environment, so users usually don't need to worry about these
58b89a7cc2SEnji Cooper // macros.  However, the automatic detection is not perfect.
59b89a7cc2SEnji Cooper // Sometimes it's necessary for a user to define some of the following
60b89a7cc2SEnji Cooper // macros in the build script to override Google Test's decisions.
61b89a7cc2SEnji Cooper //
62b89a7cc2SEnji Cooper // If the user doesn't define a macro in the list, Google Test will
63b89a7cc2SEnji Cooper // provide a default definition.  After this header is #included, all
64b89a7cc2SEnji Cooper // macros in this list will be defined to either 1 or 0.
65b89a7cc2SEnji Cooper //
66b89a7cc2SEnji Cooper // Notes to maintainers:
67b89a7cc2SEnji Cooper //   - Each macro here is a user-tweakable knob; do not grow the list
68b89a7cc2SEnji Cooper //     lightly.
69b89a7cc2SEnji Cooper //   - Use #if to key off these macros.  Don't use #ifdef or "#if
70b89a7cc2SEnji Cooper //     defined(...)", which will not work as these macros are ALWAYS
71b89a7cc2SEnji Cooper //     defined.
72b89a7cc2SEnji Cooper //
73b89a7cc2SEnji Cooper //   GTEST_HAS_CLONE          - Define it to 1/0 to indicate that clone(2)
74b89a7cc2SEnji Cooper //                              is/isn't available.
75b89a7cc2SEnji Cooper //   GTEST_HAS_EXCEPTIONS     - Define it to 1/0 to indicate that exceptions
76b89a7cc2SEnji Cooper //                              are enabled.
77b89a7cc2SEnji Cooper //   GTEST_HAS_POSIX_RE       - Define it to 1/0 to indicate that POSIX regular
78b89a7cc2SEnji Cooper //                              expressions are/aren't available.
79b89a7cc2SEnji Cooper //   GTEST_HAS_PTHREAD        - Define it to 1/0 to indicate that <pthread.h>
80b89a7cc2SEnji Cooper //                              is/isn't available.
81b89a7cc2SEnji Cooper //   GTEST_HAS_RTTI           - Define it to 1/0 to indicate that RTTI is/isn't
82b89a7cc2SEnji Cooper //                              enabled.
83b89a7cc2SEnji Cooper //   GTEST_HAS_STD_WSTRING    - Define it to 1/0 to indicate that
84b89a7cc2SEnji Cooper //                              std::wstring does/doesn't work (Google Test can
85b89a7cc2SEnji Cooper //                              be used where std::wstring is unavailable).
8628f6c2f2SEnji Cooper //   GTEST_HAS_FILE_SYSTEM    - Define it to 1/0 to indicate whether or not a
8728f6c2f2SEnji Cooper //                              file system is/isn't available.
88b89a7cc2SEnji Cooper //   GTEST_HAS_SEH            - Define it to 1/0 to indicate whether the
89b89a7cc2SEnji Cooper //                              compiler supports Microsoft's "Structured
90b89a7cc2SEnji Cooper //                              Exception Handling".
91b89a7cc2SEnji Cooper //   GTEST_HAS_STREAM_REDIRECTION
92b89a7cc2SEnji Cooper //                            - Define it to 1/0 to indicate whether the
93b89a7cc2SEnji Cooper //                              platform supports I/O stream redirection using
94b89a7cc2SEnji Cooper //                              dup() and dup2().
95b89a7cc2SEnji Cooper //   GTEST_LINKED_AS_SHARED_LIBRARY
96b89a7cc2SEnji Cooper //                            - Define to 1 when compiling tests that use
97b89a7cc2SEnji Cooper //                              Google Test as a shared library (known as
98b89a7cc2SEnji Cooper //                              DLL on Windows).
99b89a7cc2SEnji Cooper //   GTEST_CREATE_SHARED_LIBRARY
100b89a7cc2SEnji Cooper //                            - Define to 1 when compiling Google Test itself
101b89a7cc2SEnji Cooper //                              as a shared library.
102b89a7cc2SEnji Cooper //   GTEST_DEFAULT_DEATH_TEST_STYLE
103b89a7cc2SEnji Cooper //                            - The default value of --gtest_death_test_style.
104b89a7cc2SEnji Cooper //                              The legacy default has been "fast" in the open
105b89a7cc2SEnji Cooper //                              source version since 2008. The recommended value
106b89a7cc2SEnji Cooper //                              is "threadsafe", and can be set in
107b89a7cc2SEnji Cooper //                              custom/gtest-port.h.
108b89a7cc2SEnji Cooper 
109b89a7cc2SEnji Cooper // Platform-indicating macros
110b89a7cc2SEnji Cooper // --------------------------
111b89a7cc2SEnji Cooper //
112b89a7cc2SEnji Cooper // Macros indicating the platform on which Google Test is being used
113b89a7cc2SEnji Cooper // (a macro is defined to 1 if compiled on the given platform;
114b89a7cc2SEnji Cooper // otherwise UNDEFINED -- it's never defined to 0.).  Google Test
115b89a7cc2SEnji Cooper // defines these macros automatically.  Code outside Google Test MUST
116b89a7cc2SEnji Cooper // NOT define them.
117b89a7cc2SEnji Cooper //
118b89a7cc2SEnji Cooper //   GTEST_OS_AIX      - IBM AIX
119b89a7cc2SEnji Cooper //   GTEST_OS_CYGWIN   - Cygwin
12028f6c2f2SEnji Cooper //   GTEST_OS_DRAGONFLY - DragonFlyBSD
121b89a7cc2SEnji Cooper //   GTEST_OS_FREEBSD  - FreeBSD
122b89a7cc2SEnji Cooper //   GTEST_OS_FUCHSIA  - Fuchsia
12328f6c2f2SEnji Cooper //   GTEST_OS_GNU_HURD - GNU/Hurd
12428f6c2f2SEnji Cooper //   GTEST_OS_GNU_KFREEBSD - GNU/kFreeBSD
12528f6c2f2SEnji Cooper //   GTEST_OS_HAIKU    - Haiku
126b89a7cc2SEnji Cooper //   GTEST_OS_HPUX     - HP-UX
127b89a7cc2SEnji Cooper //   GTEST_OS_LINUX    - Linux
128b89a7cc2SEnji Cooper //     GTEST_OS_LINUX_ANDROID - Google Android
129b89a7cc2SEnji Cooper //   GTEST_OS_MAC      - Mac OS X
130b89a7cc2SEnji Cooper //     GTEST_OS_IOS    - iOS
131b89a7cc2SEnji Cooper //   GTEST_OS_NACL     - Google Native Client (NaCl)
132b89a7cc2SEnji Cooper //   GTEST_OS_NETBSD   - NetBSD
133b89a7cc2SEnji Cooper //   GTEST_OS_OPENBSD  - OpenBSD
13428f6c2f2SEnji Cooper //   GTEST_OS_OS2      - OS/2
135b89a7cc2SEnji Cooper //   GTEST_OS_QNX      - QNX
136b89a7cc2SEnji Cooper //   GTEST_OS_SOLARIS  - Sun Solaris
137b89a7cc2SEnji Cooper //   GTEST_OS_WINDOWS  - Windows (Desktop, MinGW, or Mobile)
138b89a7cc2SEnji Cooper //     GTEST_OS_WINDOWS_DESKTOP  - Windows Desktop
139b89a7cc2SEnji Cooper //     GTEST_OS_WINDOWS_MINGW    - MinGW
140b89a7cc2SEnji Cooper //     GTEST_OS_WINDOWS_MOBILE   - Windows Mobile
141b89a7cc2SEnji Cooper //     GTEST_OS_WINDOWS_PHONE    - Windows Phone
142b89a7cc2SEnji Cooper //     GTEST_OS_WINDOWS_RT       - Windows Store App/WinRT
143b89a7cc2SEnji Cooper //   GTEST_OS_ZOS      - z/OS
144b89a7cc2SEnji Cooper //
14528f6c2f2SEnji Cooper // Among the platforms, Cygwin, Linux, Mac OS X, and Windows have the
146b89a7cc2SEnji Cooper // most stable support.  Since core members of the Google Test project
147b89a7cc2SEnji Cooper // don't have access to other platforms, support for them may be less
148b89a7cc2SEnji Cooper // stable.  If you notice any problems on your platform, please notify
149b89a7cc2SEnji Cooper // googletestframework@googlegroups.com (patches for fixing them are
150b89a7cc2SEnji Cooper // even more welcome!).
151b89a7cc2SEnji Cooper //
152b89a7cc2SEnji Cooper // It is possible that none of the GTEST_OS_* macros are defined.
153b89a7cc2SEnji Cooper 
154b89a7cc2SEnji Cooper // Feature-indicating macros
155b89a7cc2SEnji Cooper // -------------------------
156b89a7cc2SEnji Cooper //
157b89a7cc2SEnji Cooper // Macros indicating which Google Test features are available (a macro
158b89a7cc2SEnji Cooper // is defined to 1 if the corresponding feature is supported;
159b89a7cc2SEnji Cooper // otherwise UNDEFINED -- it's never defined to 0.).  Google Test
160b89a7cc2SEnji Cooper // defines these macros automatically.  Code outside Google Test MUST
161b89a7cc2SEnji Cooper // NOT define them.
162b89a7cc2SEnji Cooper //
163b89a7cc2SEnji Cooper // These macros are public so that portable tests can be written.
16428f6c2f2SEnji Cooper // Such tests typically surround code using a feature with an #ifdef
165b89a7cc2SEnji Cooper // which controls that code.  For example:
166b89a7cc2SEnji Cooper //
16728f6c2f2SEnji Cooper // #ifdef GTEST_HAS_DEATH_TEST
168b89a7cc2SEnji Cooper //   EXPECT_DEATH(DoSomethingDeadly());
169b89a7cc2SEnji Cooper // #endif
170b89a7cc2SEnji Cooper //
171b89a7cc2SEnji Cooper //   GTEST_HAS_DEATH_TEST   - death tests
172b89a7cc2SEnji Cooper //   GTEST_HAS_TYPED_TEST   - typed tests
173b89a7cc2SEnji Cooper //   GTEST_HAS_TYPED_TEST_P - type-parameterized tests
174b89a7cc2SEnji Cooper //   GTEST_IS_THREADSAFE    - Google Test is thread-safe.
17528f6c2f2SEnji Cooper //   GTEST_USES_RE2         - the RE2 regular expression library is used
176b89a7cc2SEnji Cooper //   GTEST_USES_POSIX_RE    - enhanced POSIX regex is used. Do not confuse with
177b89a7cc2SEnji Cooper //                            GTEST_HAS_POSIX_RE (see above) which users can
178b89a7cc2SEnji Cooper //                            define themselves.
179b89a7cc2SEnji Cooper //   GTEST_USES_SIMPLE_RE   - our own simple regex is used;
180b89a7cc2SEnji Cooper //                            the above RE\b(s) are mutually exclusive.
18128f6c2f2SEnji Cooper //   GTEST_HAS_ABSL         - Google Test is compiled with Abseil.
182b89a7cc2SEnji Cooper 
183b89a7cc2SEnji Cooper // Misc public macros
184b89a7cc2SEnji Cooper // ------------------
185b89a7cc2SEnji Cooper //
186b89a7cc2SEnji Cooper //   GTEST_FLAG(flag_name)  - references the variable corresponding to
187b89a7cc2SEnji Cooper //                            the given Google Test flag.
188b89a7cc2SEnji Cooper 
189b89a7cc2SEnji Cooper // Internal utilities
190b89a7cc2SEnji Cooper // ------------------
191b89a7cc2SEnji Cooper //
192b89a7cc2SEnji Cooper // The following macros and utilities are for Google Test's INTERNAL
193b89a7cc2SEnji Cooper // use only.  Code outside Google Test MUST NOT USE THEM DIRECTLY.
194b89a7cc2SEnji Cooper //
195b89a7cc2SEnji Cooper // Macros for basic C++ coding:
196b89a7cc2SEnji Cooper //   GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.
197b89a7cc2SEnji Cooper //   GTEST_ATTRIBUTE_UNUSED_  - declares that a class' instances or a
198b89a7cc2SEnji Cooper //                              variable don't have to be used.
199b89a7cc2SEnji Cooper //   GTEST_MUST_USE_RESULT_   - declares that a function's result must be used.
200b89a7cc2SEnji Cooper //   GTEST_INTENTIONAL_CONST_COND_PUSH_ - start code section where MSVC C4127 is
201b89a7cc2SEnji Cooper //                                        suppressed (constant conditional).
202b89a7cc2SEnji Cooper //   GTEST_INTENTIONAL_CONST_COND_POP_  - finish code section where MSVC C4127
203b89a7cc2SEnji Cooper //                                        is suppressed.
20428f6c2f2SEnji Cooper //   GTEST_INTERNAL_HAS_ANY - for enabling UniversalPrinter<std::any> or
20528f6c2f2SEnji Cooper //                            UniversalPrinter<absl::any> specializations.
20628f6c2f2SEnji Cooper //                            Always defined to 0 or 1.
20728f6c2f2SEnji Cooper //   GTEST_INTERNAL_HAS_OPTIONAL - for enabling UniversalPrinter<std::optional>
20828f6c2f2SEnji Cooper //   or
20928f6c2f2SEnji Cooper //                                 UniversalPrinter<absl::optional>
21028f6c2f2SEnji Cooper //                                 specializations. Always defined to 0 or 1.
21128f6c2f2SEnji Cooper //   GTEST_INTERNAL_HAS_STRING_VIEW - for enabling Matcher<std::string_view> or
21228f6c2f2SEnji Cooper //                                    Matcher<absl::string_view>
21328f6c2f2SEnji Cooper //                                    specializations. Always defined to 0 or 1.
21428f6c2f2SEnji Cooper //   GTEST_INTERNAL_HAS_VARIANT - for enabling UniversalPrinter<std::variant> or
21528f6c2f2SEnji Cooper //                                UniversalPrinter<absl::variant>
21628f6c2f2SEnji Cooper //                                specializations. Always defined to 0 or 1.
21728f6c2f2SEnji Cooper //   GTEST_USE_OWN_FLAGFILE_FLAG_ - Always defined to 0 or 1.
21828f6c2f2SEnji Cooper //   GTEST_HAS_CXXABI_H_ - Always defined to 0 or 1.
21928f6c2f2SEnji Cooper //   GTEST_CAN_STREAM_RESULTS_ - Always defined to 0 or 1.
22028f6c2f2SEnji Cooper //   GTEST_HAS_ALT_PATH_SEP_ - Always defined to 0 or 1.
22128f6c2f2SEnji Cooper //   GTEST_WIDE_STRING_USES_UTF16_ - Always defined to 0 or 1.
22228f6c2f2SEnji Cooper //   GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ - Always defined to 0 or 1.
22328f6c2f2SEnji Cooper //   GTEST_HAS_NOTIFICATION_- Always defined to 0 or 1.
224b89a7cc2SEnji Cooper //
225b89a7cc2SEnji Cooper // Synchronization:
226b89a7cc2SEnji Cooper //   Mutex, MutexLock, ThreadLocal, GetThreadCount()
227b89a7cc2SEnji Cooper //                            - synchronization primitives.
228b89a7cc2SEnji Cooper //
229b89a7cc2SEnji Cooper // Regular expressions:
23028f6c2f2SEnji Cooper //   RE             - a simple regular expression class using
23128f6c2f2SEnji Cooper //                     1) the RE2 syntax on all platforms when built with RE2
23228f6c2f2SEnji Cooper //                        and Abseil as dependencies
23328f6c2f2SEnji Cooper //                     2) the POSIX Extended Regular Expression syntax on
23428f6c2f2SEnji Cooper //                        UNIX-like platforms,
23528f6c2f2SEnji Cooper //                     3) A reduced regular exception syntax on other platforms,
23628f6c2f2SEnji Cooper //                        including Windows.
237b89a7cc2SEnji Cooper // Logging:
238b89a7cc2SEnji Cooper //   GTEST_LOG_()   - logs messages at the specified severity level.
239b89a7cc2SEnji Cooper //   LogToStderr()  - directs all log messages to stderr.
240b89a7cc2SEnji Cooper //   FlushInfoLog() - flushes informational log messages.
241b89a7cc2SEnji Cooper //
242b89a7cc2SEnji Cooper // Stdout and stderr capturing:
243b89a7cc2SEnji Cooper //   CaptureStdout()     - starts capturing stdout.
244b89a7cc2SEnji Cooper //   GetCapturedStdout() - stops capturing stdout and returns the captured
245b89a7cc2SEnji Cooper //                         string.
246b89a7cc2SEnji Cooper //   CaptureStderr()     - starts capturing stderr.
247b89a7cc2SEnji Cooper //   GetCapturedStderr() - stops capturing stderr and returns the captured
248b89a7cc2SEnji Cooper //                         string.
249b89a7cc2SEnji Cooper //
250b89a7cc2SEnji Cooper // Integer types:
251b89a7cc2SEnji Cooper //   TypeWithSize   - maps an integer to a int type.
25228f6c2f2SEnji Cooper //   TimeInMillis   - integers of known sizes.
253b89a7cc2SEnji Cooper //   BiggestInt     - the biggest signed integer type.
254b89a7cc2SEnji Cooper //
255b89a7cc2SEnji Cooper // Command-line utilities:
256b89a7cc2SEnji Cooper //   GetInjectableArgvs() - returns the command line as a vector of strings.
257b89a7cc2SEnji Cooper //
258b89a7cc2SEnji Cooper // Environment variable utilities:
259b89a7cc2SEnji Cooper //   GetEnv()             - gets the value of an environment variable.
260b89a7cc2SEnji Cooper //   BoolFromGTestEnv()   - parses a bool environment variable.
26128f6c2f2SEnji Cooper //   Int32FromGTestEnv()  - parses an int32_t environment variable.
262b89a7cc2SEnji Cooper //   StringFromGTestEnv() - parses a string environment variable.
26328f6c2f2SEnji Cooper //
26428f6c2f2SEnji Cooper // Deprecation warnings:
26528f6c2f2SEnji Cooper //   GTEST_INTERNAL_DEPRECATED(message) - attribute marking a function as
26628f6c2f2SEnji Cooper //                                        deprecated; calling a marked function
26728f6c2f2SEnji Cooper //                                        should generate a compiler warning
26828f6c2f2SEnji Cooper 
26928f6c2f2SEnji Cooper // The definition of GTEST_INTERNAL_CPLUSPLUS_LANG comes first because it can
27028f6c2f2SEnji Cooper // potentially be used as an #include guard.
27128f6c2f2SEnji Cooper #if defined(_MSVC_LANG)
27228f6c2f2SEnji Cooper #define GTEST_INTERNAL_CPLUSPLUS_LANG _MSVC_LANG
27328f6c2f2SEnji Cooper #elif defined(__cplusplus)
27428f6c2f2SEnji Cooper #define GTEST_INTERNAL_CPLUSPLUS_LANG __cplusplus
27528f6c2f2SEnji Cooper #endif
27628f6c2f2SEnji Cooper 
27728f6c2f2SEnji Cooper #if !defined(GTEST_INTERNAL_CPLUSPLUS_LANG) || \
27828f6c2f2SEnji Cooper     GTEST_INTERNAL_CPLUSPLUS_LANG < 201402L
27928f6c2f2SEnji Cooper #error C++ versions less than C++14 are not supported.
28028f6c2f2SEnji Cooper #endif
281b89a7cc2SEnji Cooper 
282b89a7cc2SEnji Cooper #include <ctype.h>   // for isspace, etc
283b89a7cc2SEnji Cooper #include <stddef.h>  // for ptrdiff_t
284b89a7cc2SEnji Cooper #include <stdio.h>
28528f6c2f2SEnji Cooper #include <stdlib.h>
286b89a7cc2SEnji Cooper #include <string.h>
28728f6c2f2SEnji Cooper 
28828f6c2f2SEnji Cooper #include <cerrno>
28928f6c2f2SEnji Cooper // #include <condition_variable>  // Guarded by GTEST_IS_THREADSAFE below
29028f6c2f2SEnji Cooper #include <cstdint>
29128f6c2f2SEnji Cooper #include <iostream>
29228f6c2f2SEnji Cooper #include <limits>
29328f6c2f2SEnji Cooper #include <locale>
29428f6c2f2SEnji Cooper #include <memory>
29528f6c2f2SEnji Cooper #include <ostream>
29628f6c2f2SEnji Cooper #include <string>
29728f6c2f2SEnji Cooper // #include <mutex>  // Guarded by GTEST_IS_THREADSAFE below
29828f6c2f2SEnji Cooper #include <tuple>
29928f6c2f2SEnji Cooper #include <type_traits>
30028f6c2f2SEnji Cooper #include <vector>
30128f6c2f2SEnji Cooper 
302b89a7cc2SEnji Cooper #ifndef _WIN32_WCE
303b89a7cc2SEnji Cooper #include <sys/stat.h>
30428f6c2f2SEnji Cooper #include <sys/types.h>
305b89a7cc2SEnji Cooper #endif  // !_WIN32_WCE
306b89a7cc2SEnji Cooper 
307b89a7cc2SEnji Cooper #if defined __APPLE__
308b89a7cc2SEnji Cooper #include <AvailabilityMacros.h>
309b89a7cc2SEnji Cooper #include <TargetConditionals.h>
310b89a7cc2SEnji Cooper #endif
311b89a7cc2SEnji Cooper 
312b89a7cc2SEnji Cooper #include "gtest/internal/custom/gtest-port.h"
31328f6c2f2SEnji Cooper #include "gtest/internal/gtest-port-arch.h"
31428f6c2f2SEnji Cooper 
31528f6c2f2SEnji Cooper #ifndef GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
31628f6c2f2SEnji Cooper #define GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ 0
31728f6c2f2SEnji Cooper #endif
31828f6c2f2SEnji Cooper 
31928f6c2f2SEnji Cooper #ifndef GTEST_HAS_NOTIFICATION_
32028f6c2f2SEnji Cooper #define GTEST_HAS_NOTIFICATION_ 0
32128f6c2f2SEnji Cooper #endif
32228f6c2f2SEnji Cooper 
32328f6c2f2SEnji Cooper #ifdef GTEST_HAS_ABSL
32428f6c2f2SEnji Cooper #include "absl/flags/declare.h"
32528f6c2f2SEnji Cooper #include "absl/flags/flag.h"
32628f6c2f2SEnji Cooper #include "absl/flags/reflection.h"
32728f6c2f2SEnji Cooper #endif
328b89a7cc2SEnji Cooper 
329b89a7cc2SEnji Cooper #if !defined(GTEST_DEV_EMAIL_)
330b89a7cc2SEnji Cooper #define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
331b89a7cc2SEnji Cooper #define GTEST_FLAG_PREFIX_ "gtest_"
332b89a7cc2SEnji Cooper #define GTEST_FLAG_PREFIX_DASH_ "gtest-"
333b89a7cc2SEnji Cooper #define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
334b89a7cc2SEnji Cooper #define GTEST_NAME_ "Google Test"
335b89a7cc2SEnji Cooper #define GTEST_PROJECT_URL_ "https://github.com/google/googletest/"
336b89a7cc2SEnji Cooper #endif  // !defined(GTEST_DEV_EMAIL_)
337b89a7cc2SEnji Cooper 
338b89a7cc2SEnji Cooper #if !defined(GTEST_INIT_GOOGLE_TEST_NAME_)
339b89a7cc2SEnji Cooper #define GTEST_INIT_GOOGLE_TEST_NAME_ "testing::InitGoogleTest"
340b89a7cc2SEnji Cooper #endif  // !defined(GTEST_INIT_GOOGLE_TEST_NAME_)
341b89a7cc2SEnji Cooper 
342b89a7cc2SEnji Cooper // Determines the version of gcc that is used to compile this.
343b89a7cc2SEnji Cooper #ifdef __GNUC__
344b89a7cc2SEnji Cooper // 40302 means version 4.3.2.
345b89a7cc2SEnji Cooper #define GTEST_GCC_VER_ \
346b89a7cc2SEnji Cooper   (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
347b89a7cc2SEnji Cooper #endif  // __GNUC__
348b89a7cc2SEnji Cooper 
349b89a7cc2SEnji Cooper // Macros for disabling Microsoft Visual C++ warnings.
350b89a7cc2SEnji Cooper //
351b89a7cc2SEnji Cooper //   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 4385)
352b89a7cc2SEnji Cooper //   /* code that triggers warnings C4800 and C4385 */
353b89a7cc2SEnji Cooper //   GTEST_DISABLE_MSC_WARNINGS_POP_()
35428f6c2f2SEnji Cooper #if defined(_MSC_VER)
355b89a7cc2SEnji Cooper #define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) \
35628f6c2f2SEnji Cooper   __pragma(warning(push)) __pragma(warning(disable : warnings))
35728f6c2f2SEnji Cooper #define GTEST_DISABLE_MSC_WARNINGS_POP_() __pragma(warning(pop))
358b89a7cc2SEnji Cooper #else
35928f6c2f2SEnji Cooper // Not all compilers are MSVC
360b89a7cc2SEnji Cooper #define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
361b89a7cc2SEnji Cooper #define GTEST_DISABLE_MSC_WARNINGS_POP_()
362b89a7cc2SEnji Cooper #endif
363b89a7cc2SEnji Cooper 
364b89a7cc2SEnji Cooper // Clang on Windows does not understand MSVC's pragma warning.
365b89a7cc2SEnji Cooper // We need clang-specific way to disable function deprecation warning.
366b89a7cc2SEnji Cooper #ifdef __clang__
367b89a7cc2SEnji Cooper #define GTEST_DISABLE_MSC_DEPRECATED_PUSH_()                            \
368b89a7cc2SEnji Cooper   _Pragma("clang diagnostic push")                                      \
369b89a7cc2SEnji Cooper       _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") \
370b89a7cc2SEnji Cooper           _Pragma("clang diagnostic ignored \"-Wdeprecated-implementations\"")
37128f6c2f2SEnji Cooper #define GTEST_DISABLE_MSC_DEPRECATED_POP_() _Pragma("clang diagnostic pop")
372b89a7cc2SEnji Cooper #else
373b89a7cc2SEnji Cooper #define GTEST_DISABLE_MSC_DEPRECATED_PUSH_() \
374b89a7cc2SEnji Cooper   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996)
37528f6c2f2SEnji Cooper #define GTEST_DISABLE_MSC_DEPRECATED_POP_() GTEST_DISABLE_MSC_WARNINGS_POP_()
376b89a7cc2SEnji Cooper #endif
377b89a7cc2SEnji Cooper 
378b89a7cc2SEnji Cooper // Brings in definitions for functions used in the testing::internal::posix
379b89a7cc2SEnji Cooper // namespace (read, write, close, chdir, isatty, stat). We do not currently
380b89a7cc2SEnji Cooper // use them on Windows Mobile.
38128f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
38228f6c2f2SEnji Cooper #ifndef GTEST_OS_WINDOWS_MOBILE
383b89a7cc2SEnji Cooper #include <direct.h>
384b89a7cc2SEnji Cooper #include <io.h>
385b89a7cc2SEnji Cooper #endif
386b89a7cc2SEnji Cooper // In order to avoid having to include <windows.h>, use forward declaration
38728f6c2f2SEnji Cooper #if defined(GTEST_OS_WINDOWS_MINGW) && !defined(__MINGW64_VERSION_MAJOR)
388b89a7cc2SEnji Cooper // MinGW defined _CRITICAL_SECTION and _RTL_CRITICAL_SECTION as two
389b89a7cc2SEnji Cooper // separate (equivalent) structs, instead of using typedef
390b89a7cc2SEnji Cooper typedef struct _CRITICAL_SECTION GTEST_CRITICAL_SECTION;
391b89a7cc2SEnji Cooper #else
392b89a7cc2SEnji Cooper // Assume CRITICAL_SECTION is a typedef of _RTL_CRITICAL_SECTION.
393b89a7cc2SEnji Cooper // This assumption is verified by
394b89a7cc2SEnji Cooper // WindowsTypesTest.CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION.
395b89a7cc2SEnji Cooper typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
396b89a7cc2SEnji Cooper #endif
39728f6c2f2SEnji Cooper #elif defined(GTEST_OS_XTENSA)
39828f6c2f2SEnji Cooper #include <unistd.h>
39928f6c2f2SEnji Cooper // Xtensa toolchains define strcasecmp in the string.h header instead of
40028f6c2f2SEnji Cooper // strings.h. string.h is already included.
401b89a7cc2SEnji Cooper #else
402b89a7cc2SEnji Cooper // This assumes that non-Windows OSes provide unistd.h. For OSes where this
403b89a7cc2SEnji Cooper // is not the case, we need to include headers that provide the functions
404b89a7cc2SEnji Cooper // mentioned above.
405b89a7cc2SEnji Cooper #include <strings.h>
40628f6c2f2SEnji Cooper #include <unistd.h>
407b89a7cc2SEnji Cooper #endif  // GTEST_OS_WINDOWS
408b89a7cc2SEnji Cooper 
40928f6c2f2SEnji Cooper #ifdef GTEST_OS_LINUX_ANDROID
410b89a7cc2SEnji Cooper // Used to define __ANDROID_API__ matching the target NDK API level.
411b89a7cc2SEnji Cooper #include <android/api-level.h>  // NOLINT
412b89a7cc2SEnji Cooper #endif
413b89a7cc2SEnji Cooper 
41428f6c2f2SEnji Cooper // Defines this to true if and only if Google Test can use POSIX regular
41528f6c2f2SEnji Cooper // expressions.
416b89a7cc2SEnji Cooper #ifndef GTEST_HAS_POSIX_RE
41728f6c2f2SEnji Cooper #ifdef GTEST_OS_LINUX_ANDROID
418b89a7cc2SEnji Cooper // On Android, <regex.h> is only available starting with Gingerbread.
419b89a7cc2SEnji Cooper #define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9)
420b89a7cc2SEnji Cooper #else
42128f6c2f2SEnji Cooper #if !(defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_XTENSA) || \
42228f6c2f2SEnji Cooper       defined(GTEST_OS_QURT))
42328f6c2f2SEnji Cooper #define GTEST_HAS_POSIX_RE 1
424b89a7cc2SEnji Cooper #else
42528f6c2f2SEnji Cooper #define GTEST_HAS_POSIX_RE 0
42628f6c2f2SEnji Cooper #endif
42728f6c2f2SEnji Cooper #endif  // GTEST_OS_LINUX_ANDROID
42828f6c2f2SEnji Cooper #endif
429b89a7cc2SEnji Cooper 
43028f6c2f2SEnji Cooper // Select the regular expression implementation.
43128f6c2f2SEnji Cooper #ifdef GTEST_HAS_ABSL
43228f6c2f2SEnji Cooper // When using Abseil, RE2 is required.
43328f6c2f2SEnji Cooper #include "absl/strings/string_view.h"
43428f6c2f2SEnji Cooper #include "re2/re2.h"
43528f6c2f2SEnji Cooper #define GTEST_USES_RE2 1
43628f6c2f2SEnji Cooper #elif GTEST_HAS_POSIX_RE
43728f6c2f2SEnji Cooper #include <regex.h>  // NOLINT
43828f6c2f2SEnji Cooper #define GTEST_USES_POSIX_RE 1
43928f6c2f2SEnji Cooper #else
44028f6c2f2SEnji Cooper // Use our own simple regex implementation.
441b89a7cc2SEnji Cooper #define GTEST_USES_SIMPLE_RE 1
44228f6c2f2SEnji Cooper #endif
443b89a7cc2SEnji Cooper 
444b89a7cc2SEnji Cooper #ifndef GTEST_HAS_EXCEPTIONS
445b89a7cc2SEnji Cooper // The user didn't tell us whether exceptions are enabled, so we need
446b89a7cc2SEnji Cooper // to figure it out.
447b89a7cc2SEnji Cooper #if defined(_MSC_VER) && defined(_CPPUNWIND)
44828f6c2f2SEnji Cooper // MSVC defines _CPPUNWIND to 1 if and only if exceptions are enabled.
449b89a7cc2SEnji Cooper #define GTEST_HAS_EXCEPTIONS 1
450b89a7cc2SEnji Cooper #elif defined(__BORLANDC__)
451b89a7cc2SEnji Cooper // C++Builder's implementation of the STL uses the _HAS_EXCEPTIONS
452b89a7cc2SEnji Cooper // macro to enable exceptions, so we'll do the same.
453b89a7cc2SEnji Cooper // Assumes that exceptions are enabled by default.
454b89a7cc2SEnji Cooper #ifndef _HAS_EXCEPTIONS
455b89a7cc2SEnji Cooper #define _HAS_EXCEPTIONS 1
456b89a7cc2SEnji Cooper #endif  // _HAS_EXCEPTIONS
457b89a7cc2SEnji Cooper #define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
458b89a7cc2SEnji Cooper #elif defined(__clang__)
45928f6c2f2SEnji Cooper // clang defines __EXCEPTIONS if and only if exceptions are enabled before clang
46028f6c2f2SEnji Cooper // 220714, but if and only if cleanups are enabled after that. In Obj-C++ files,
46128f6c2f2SEnji Cooper // there can be cleanups for ObjC exceptions which also need cleanups, even if
46228f6c2f2SEnji Cooper // C++ exceptions are disabled. clang has __has_feature(cxx_exceptions) which
46328f6c2f2SEnji Cooper // checks for C++ exceptions starting at clang r206352, but which checked for
46428f6c2f2SEnji Cooper // cleanups prior to that. To reliably check for C++ exception availability with
46528f6c2f2SEnji Cooper // clang, check for
466b89a7cc2SEnji Cooper // __EXCEPTIONS && __has_feature(cxx_exceptions).
46728f6c2f2SEnji Cooper #if defined(__EXCEPTIONS) && __EXCEPTIONS && __has_feature(cxx_exceptions)
46828f6c2f2SEnji Cooper #define GTEST_HAS_EXCEPTIONS 1
46928f6c2f2SEnji Cooper #else
47028f6c2f2SEnji Cooper #define GTEST_HAS_EXCEPTIONS 0
47128f6c2f2SEnji Cooper #endif
47228f6c2f2SEnji Cooper #elif defined(__GNUC__) && defined(__EXCEPTIONS) && __EXCEPTIONS
47328f6c2f2SEnji Cooper // gcc defines __EXCEPTIONS to 1 if and only if exceptions are enabled.
474b89a7cc2SEnji Cooper #define GTEST_HAS_EXCEPTIONS 1
475b89a7cc2SEnji Cooper #elif defined(__SUNPRO_CC)
476b89a7cc2SEnji Cooper // Sun Pro CC supports exceptions.  However, there is no compile-time way of
477b89a7cc2SEnji Cooper // detecting whether they are enabled or not.  Therefore, we assume that
478b89a7cc2SEnji Cooper // they are enabled unless the user tells us otherwise.
479b89a7cc2SEnji Cooper #define GTEST_HAS_EXCEPTIONS 1
48028f6c2f2SEnji Cooper #elif defined(__IBMCPP__) && defined(__EXCEPTIONS) && __EXCEPTIONS
48128f6c2f2SEnji Cooper // xlC defines __EXCEPTIONS to 1 if and only if exceptions are enabled.
482b89a7cc2SEnji Cooper #define GTEST_HAS_EXCEPTIONS 1
483b89a7cc2SEnji Cooper #elif defined(__HP_aCC)
484b89a7cc2SEnji Cooper // Exception handling is in effect by default in HP aCC compiler. It has to
485b89a7cc2SEnji Cooper // be turned of by +noeh compiler option if desired.
486b89a7cc2SEnji Cooper #define GTEST_HAS_EXCEPTIONS 1
487b89a7cc2SEnji Cooper #else
488b89a7cc2SEnji Cooper // For other compilers, we assume exceptions are disabled to be
489b89a7cc2SEnji Cooper // conservative.
490b89a7cc2SEnji Cooper #define GTEST_HAS_EXCEPTIONS 0
491b89a7cc2SEnji Cooper #endif  // defined(_MSC_VER) || defined(__BORLANDC__)
492b89a7cc2SEnji Cooper #endif  // GTEST_HAS_EXCEPTIONS
493b89a7cc2SEnji Cooper 
494b89a7cc2SEnji Cooper #ifndef GTEST_HAS_STD_WSTRING
495b89a7cc2SEnji Cooper // The user didn't tell us whether ::std::wstring is available, so we need
496b89a7cc2SEnji Cooper // to figure it out.
497b89a7cc2SEnji Cooper // Cygwin 1.7 and below doesn't support ::std::wstring.
498b89a7cc2SEnji Cooper // Solaris' libc++ doesn't support it either.  Android has
499b89a7cc2SEnji Cooper // no support for it at least as recent as Froyo (2.2).
50028f6c2f2SEnji Cooper #if (!(defined(GTEST_OS_LINUX_ANDROID) || defined(GTEST_OS_CYGWIN) || \
50128f6c2f2SEnji Cooper        defined(GTEST_OS_SOLARIS) || defined(GTEST_OS_HAIKU) ||        \
50228f6c2f2SEnji Cooper        defined(GTEST_OS_ESP32) || defined(GTEST_OS_ESP8266) ||        \
50328f6c2f2SEnji Cooper        defined(GTEST_OS_XTENSA) || defined(GTEST_OS_QURT) ||          \
50428f6c2f2SEnji Cooper        defined(GTEST_OS_NXP_QN9090) || defined(GTEST_OS_NRF52)))
50528f6c2f2SEnji Cooper #define GTEST_HAS_STD_WSTRING 1
50628f6c2f2SEnji Cooper #else
50728f6c2f2SEnji Cooper #define GTEST_HAS_STD_WSTRING 0
50828f6c2f2SEnji Cooper #endif
509b89a7cc2SEnji Cooper #endif  // GTEST_HAS_STD_WSTRING
510b89a7cc2SEnji Cooper 
51128f6c2f2SEnji Cooper #ifndef GTEST_HAS_FILE_SYSTEM
51228f6c2f2SEnji Cooper // Most platforms support a file system.
51328f6c2f2SEnji Cooper #define GTEST_HAS_FILE_SYSTEM 1
51428f6c2f2SEnji Cooper #endif  // GTEST_HAS_FILE_SYSTEM
515b89a7cc2SEnji Cooper 
516b89a7cc2SEnji Cooper // Determines whether RTTI is available.
517b89a7cc2SEnji Cooper #ifndef GTEST_HAS_RTTI
518b89a7cc2SEnji Cooper // The user didn't tell us whether RTTI is enabled, so we need to
519b89a7cc2SEnji Cooper // figure it out.
520b89a7cc2SEnji Cooper 
521b89a7cc2SEnji Cooper #ifdef _MSC_VER
522b89a7cc2SEnji Cooper 
52328f6c2f2SEnji Cooper #ifdef _CPPRTTI  // MSVC defines this macro if and only if RTTI is enabled.
524b89a7cc2SEnji Cooper #define GTEST_HAS_RTTI 1
525b89a7cc2SEnji Cooper #else
526b89a7cc2SEnji Cooper #define GTEST_HAS_RTTI 0
527b89a7cc2SEnji Cooper #endif
528b89a7cc2SEnji Cooper 
52928f6c2f2SEnji Cooper // Starting with version 4.3.2, gcc defines __GXX_RTTI if and only if RTTI is
53028f6c2f2SEnji Cooper // enabled.
53128f6c2f2SEnji Cooper #elif defined(__GNUC__)
532b89a7cc2SEnji Cooper 
533b89a7cc2SEnji Cooper #ifdef __GXX_RTTI
534b89a7cc2SEnji Cooper // When building against STLport with the Android NDK and with
535b89a7cc2SEnji Cooper // -frtti -fno-exceptions, the build fails at link time with undefined
536b89a7cc2SEnji Cooper // references to __cxa_bad_typeid. Note sure if STL or toolchain bug,
537b89a7cc2SEnji Cooper // so disable RTTI when detected.
53828f6c2f2SEnji Cooper #if defined(GTEST_OS_LINUX_ANDROID) && defined(_STLPORT_MAJOR) && \
539b89a7cc2SEnji Cooper     !defined(__EXCEPTIONS)
540b89a7cc2SEnji Cooper #define GTEST_HAS_RTTI 0
541b89a7cc2SEnji Cooper #else
542b89a7cc2SEnji Cooper #define GTEST_HAS_RTTI 1
543b89a7cc2SEnji Cooper #endif  // GTEST_OS_LINUX_ANDROID && __STLPORT_MAJOR && !__EXCEPTIONS
544b89a7cc2SEnji Cooper #else
545b89a7cc2SEnji Cooper #define GTEST_HAS_RTTI 0
546b89a7cc2SEnji Cooper #endif  // __GXX_RTTI
547b89a7cc2SEnji Cooper 
548b89a7cc2SEnji Cooper // Clang defines __GXX_RTTI starting with version 3.0, but its manual recommends
549b89a7cc2SEnji Cooper // using has_feature instead. has_feature(cxx_rtti) is supported since 2.7, the
550b89a7cc2SEnji Cooper // first version with C++ support.
551b89a7cc2SEnji Cooper #elif defined(__clang__)
552b89a7cc2SEnji Cooper 
553b89a7cc2SEnji Cooper #define GTEST_HAS_RTTI __has_feature(cxx_rtti)
554b89a7cc2SEnji Cooper 
555b89a7cc2SEnji Cooper // Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if
556b89a7cc2SEnji Cooper // both the typeid and dynamic_cast features are present.
557b89a7cc2SEnji Cooper #elif defined(__IBMCPP__) && (__IBMCPP__ >= 900)
558b89a7cc2SEnji Cooper 
559b89a7cc2SEnji Cooper #ifdef __RTTI_ALL__
560b89a7cc2SEnji Cooper #define GTEST_HAS_RTTI 1
561b89a7cc2SEnji Cooper #else
562b89a7cc2SEnji Cooper #define GTEST_HAS_RTTI 0
563b89a7cc2SEnji Cooper #endif
564b89a7cc2SEnji Cooper 
565b89a7cc2SEnji Cooper #else
566b89a7cc2SEnji Cooper 
567b89a7cc2SEnji Cooper // For all other compilers, we assume RTTI is enabled.
568b89a7cc2SEnji Cooper #define GTEST_HAS_RTTI 1
569b89a7cc2SEnji Cooper 
570b89a7cc2SEnji Cooper #endif  // _MSC_VER
571b89a7cc2SEnji Cooper 
572b89a7cc2SEnji Cooper #endif  // GTEST_HAS_RTTI
573b89a7cc2SEnji Cooper 
574b89a7cc2SEnji Cooper // It's this header's responsibility to #include <typeinfo> when RTTI
575b89a7cc2SEnji Cooper // is enabled.
576b89a7cc2SEnji Cooper #if GTEST_HAS_RTTI
577b89a7cc2SEnji Cooper #include <typeinfo>
578b89a7cc2SEnji Cooper #endif
579b89a7cc2SEnji Cooper 
580b89a7cc2SEnji Cooper // Determines whether Google Test can use the pthreads library.
581b89a7cc2SEnji Cooper #ifndef GTEST_HAS_PTHREAD
582b89a7cc2SEnji Cooper // The user didn't tell us explicitly, so we make reasonable assumptions about
583b89a7cc2SEnji Cooper // which platforms have pthreads support.
584b89a7cc2SEnji Cooper //
585b89a7cc2SEnji Cooper // To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0
586b89a7cc2SEnji Cooper // to your compiler flags.
58728f6c2f2SEnji Cooper #if (defined(GTEST_OS_LINUX) || defined(GTEST_OS_MAC) ||              \
58828f6c2f2SEnji Cooper      defined(GTEST_OS_HPUX) || defined(GTEST_OS_QNX) ||               \
58928f6c2f2SEnji Cooper      defined(GTEST_OS_FREEBSD) || defined(GTEST_OS_NACL) ||           \
59028f6c2f2SEnji Cooper      defined(GTEST_OS_NETBSD) || defined(GTEST_OS_FUCHSIA) ||         \
59128f6c2f2SEnji Cooper      defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_GNU_KFREEBSD) || \
59228f6c2f2SEnji Cooper      defined(GTEST_OS_OPENBSD) || defined(GTEST_OS_HAIKU) ||          \
59328f6c2f2SEnji Cooper      defined(GTEST_OS_GNU_HURD))
59428f6c2f2SEnji Cooper #define GTEST_HAS_PTHREAD 1
59528f6c2f2SEnji Cooper #else
59628f6c2f2SEnji Cooper #define GTEST_HAS_PTHREAD 0
59728f6c2f2SEnji Cooper #endif
598b89a7cc2SEnji Cooper #endif  // GTEST_HAS_PTHREAD
599b89a7cc2SEnji Cooper 
600b89a7cc2SEnji Cooper #if GTEST_HAS_PTHREAD
601b89a7cc2SEnji Cooper // gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is
602b89a7cc2SEnji Cooper // true.
603b89a7cc2SEnji Cooper #include <pthread.h>  // NOLINT
604b89a7cc2SEnji Cooper 
605b89a7cc2SEnji Cooper // For timespec and nanosleep, used below.
606b89a7cc2SEnji Cooper #include <time.h>  // NOLINT
607b89a7cc2SEnji Cooper #endif
608b89a7cc2SEnji Cooper 
609b89a7cc2SEnji Cooper // Determines whether clone(2) is supported.
610b89a7cc2SEnji Cooper // Usually it will only be available on Linux, excluding
611b89a7cc2SEnji Cooper // Linux on the Itanium architecture.
612b89a7cc2SEnji Cooper // Also see http://linux.die.net/man/2/clone.
613b89a7cc2SEnji Cooper #ifndef GTEST_HAS_CLONE
614b89a7cc2SEnji Cooper // The user didn't tell us, so we need to figure it out.
615b89a7cc2SEnji Cooper 
61628f6c2f2SEnji Cooper #if defined(GTEST_OS_LINUX) && !defined(__ia64__)
61728f6c2f2SEnji Cooper #if defined(GTEST_OS_LINUX_ANDROID)
618b89a7cc2SEnji Cooper // On Android, clone() became available at different API levels for each 32-bit
619b89a7cc2SEnji Cooper // architecture.
62028f6c2f2SEnji Cooper #if defined(__LP64__) || (defined(__arm__) && __ANDROID_API__ >= 9) || \
621b89a7cc2SEnji Cooper     (defined(__mips__) && __ANDROID_API__ >= 12) ||                    \
622b89a7cc2SEnji Cooper     (defined(__i386__) && __ANDROID_API__ >= 17)
623b89a7cc2SEnji Cooper #define GTEST_HAS_CLONE 1
624b89a7cc2SEnji Cooper #else
625b89a7cc2SEnji Cooper #define GTEST_HAS_CLONE 0
626b89a7cc2SEnji Cooper #endif
627b89a7cc2SEnji Cooper #else
628b89a7cc2SEnji Cooper #define GTEST_HAS_CLONE 1
629b89a7cc2SEnji Cooper #endif
630b89a7cc2SEnji Cooper #else
631b89a7cc2SEnji Cooper #define GTEST_HAS_CLONE 0
632b89a7cc2SEnji Cooper #endif  // GTEST_OS_LINUX && !defined(__ia64__)
633b89a7cc2SEnji Cooper 
634b89a7cc2SEnji Cooper #endif  // GTEST_HAS_CLONE
635b89a7cc2SEnji Cooper 
636b89a7cc2SEnji Cooper // Determines whether to support stream redirection. This is used to test
637b89a7cc2SEnji Cooper // output correctness and to implement death tests.
638b89a7cc2SEnji Cooper #ifndef GTEST_HAS_STREAM_REDIRECTION
639b89a7cc2SEnji Cooper // By default, we assume that stream redirection is supported on all
64028f6c2f2SEnji Cooper // platforms except known mobile / embedded ones. Also, if the port doesn't have
64128f6c2f2SEnji Cooper // a file system, stream redirection is not supported.
64228f6c2f2SEnji Cooper #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_WINDOWS_PHONE) || \
64328f6c2f2SEnji Cooper     defined(GTEST_OS_WINDOWS_RT) || defined(GTEST_OS_ESP8266) ||           \
64428f6c2f2SEnji Cooper     defined(GTEST_OS_XTENSA) || defined(GTEST_OS_QURT) ||                  \
64528f6c2f2SEnji Cooper     !GTEST_HAS_FILE_SYSTEM
646b89a7cc2SEnji Cooper #define GTEST_HAS_STREAM_REDIRECTION 0
647b89a7cc2SEnji Cooper #else
648b89a7cc2SEnji Cooper #define GTEST_HAS_STREAM_REDIRECTION 1
64928f6c2f2SEnji Cooper #endif  // !GTEST_OS_WINDOWS_MOBILE
650b89a7cc2SEnji Cooper #endif  // GTEST_HAS_STREAM_REDIRECTION
651b89a7cc2SEnji Cooper 
652b89a7cc2SEnji Cooper // Determines whether to support death tests.
653b89a7cc2SEnji Cooper // pops up a dialog window that cannot be suppressed programmatically.
65428f6c2f2SEnji Cooper #if (defined(GTEST_OS_LINUX) || defined(GTEST_OS_CYGWIN) ||           \
65528f6c2f2SEnji Cooper      defined(GTEST_OS_SOLARIS) ||                                     \
65628f6c2f2SEnji Cooper      (defined(GTEST_OS_MAC) && !defined(GTEST_OS_IOS)) ||             \
65728f6c2f2SEnji Cooper      (defined(GTEST_OS_WINDOWS_DESKTOP) && _MSC_VER) ||               \
65828f6c2f2SEnji Cooper      defined(GTEST_OS_WINDOWS_MINGW) || defined(GTEST_OS_AIX) ||      \
65928f6c2f2SEnji Cooper      defined(GTEST_OS_HPUX) || defined(GTEST_OS_OPENBSD) ||           \
66028f6c2f2SEnji Cooper      defined(GTEST_OS_QNX) || defined(GTEST_OS_FREEBSD) ||            \
66128f6c2f2SEnji Cooper      defined(GTEST_OS_NETBSD) || defined(GTEST_OS_FUCHSIA) ||         \
66228f6c2f2SEnji Cooper      defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_GNU_KFREEBSD) || \
66328f6c2f2SEnji Cooper      defined(GTEST_OS_HAIKU) || defined(GTEST_OS_GNU_HURD))
66428f6c2f2SEnji Cooper // Death tests require a file system to work properly.
66528f6c2f2SEnji Cooper #if GTEST_HAS_FILE_SYSTEM
666b89a7cc2SEnji Cooper #define GTEST_HAS_DEATH_TEST 1
66728f6c2f2SEnji Cooper #endif  // GTEST_HAS_FILE_SYSTEM
668b89a7cc2SEnji Cooper #endif
669b89a7cc2SEnji Cooper 
670b89a7cc2SEnji Cooper // Determines whether to support type-driven tests.
671b89a7cc2SEnji Cooper 
672b89a7cc2SEnji Cooper // Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0,
673b89a7cc2SEnji Cooper // Sun Pro CC, IBM Visual Age, and HP aCC support.
67428f6c2f2SEnji Cooper #if defined(__GNUC__) || defined(_MSC_VER) || defined(__SUNPRO_CC) || \
675b89a7cc2SEnji Cooper     defined(__IBMCPP__) || defined(__HP_aCC)
676b89a7cc2SEnji Cooper #define GTEST_HAS_TYPED_TEST 1
677b89a7cc2SEnji Cooper #define GTEST_HAS_TYPED_TEST_P 1
678b89a7cc2SEnji Cooper #endif
679b89a7cc2SEnji Cooper 
68028f6c2f2SEnji Cooper // Determines whether the system compiler uses UTF-16 for encoding wide strings.
68128f6c2f2SEnji Cooper #if defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_CYGWIN) || \
68228f6c2f2SEnji Cooper     defined(GTEST_OS_AIX) || defined(GTEST_OS_OS2)
68328f6c2f2SEnji Cooper #define GTEST_WIDE_STRING_USES_UTF16_ 1
68428f6c2f2SEnji Cooper #else
68528f6c2f2SEnji Cooper #define GTEST_WIDE_STRING_USES_UTF16_ 0
686b89a7cc2SEnji Cooper #endif
687b89a7cc2SEnji Cooper 
688b89a7cc2SEnji Cooper // Determines whether test results can be streamed to a socket.
68928f6c2f2SEnji Cooper #if defined(GTEST_OS_LINUX) || defined(GTEST_OS_GNU_KFREEBSD) || \
69028f6c2f2SEnji Cooper     defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_FREEBSD) ||  \
69128f6c2f2SEnji Cooper     defined(GTEST_OS_NETBSD) || defined(GTEST_OS_OPENBSD) ||     \
69228f6c2f2SEnji Cooper     defined(GTEST_OS_GNU_HURD) || defined(GTEST_OS_MAC)
693b89a7cc2SEnji Cooper #define GTEST_CAN_STREAM_RESULTS_ 1
69428f6c2f2SEnji Cooper #else
69528f6c2f2SEnji Cooper #define GTEST_CAN_STREAM_RESULTS_ 0
696b89a7cc2SEnji Cooper #endif
697b89a7cc2SEnji Cooper 
698b89a7cc2SEnji Cooper // Defines some utility macros.
699b89a7cc2SEnji Cooper 
700b89a7cc2SEnji Cooper // The GNU compiler emits a warning if nested "if" statements are followed by
701b89a7cc2SEnji Cooper // an "else" statement and braces are not used to explicitly disambiguate the
702b89a7cc2SEnji Cooper // "else" binding.  This leads to problems with code like:
703b89a7cc2SEnji Cooper //
704b89a7cc2SEnji Cooper //   if (gate)
705b89a7cc2SEnji Cooper //     ASSERT_*(condition) << "Some message";
706b89a7cc2SEnji Cooper //
707b89a7cc2SEnji Cooper // The "switch (0) case 0:" idiom is used to suppress this.
708b89a7cc2SEnji Cooper #ifdef __INTEL_COMPILER
709b89a7cc2SEnji Cooper #define GTEST_AMBIGUOUS_ELSE_BLOCKER_
710b89a7cc2SEnji Cooper #else
71128f6c2f2SEnji Cooper #define GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
71228f6c2f2SEnji Cooper   switch (0)                          \
71328f6c2f2SEnji Cooper   case 0:                             \
71428f6c2f2SEnji Cooper   default:  // NOLINT
715b89a7cc2SEnji Cooper #endif
716b89a7cc2SEnji Cooper 
71728f6c2f2SEnji Cooper // GTEST_HAVE_ATTRIBUTE_
718b89a7cc2SEnji Cooper //
71928f6c2f2SEnji Cooper // A function-like feature checking macro that is a wrapper around
72028f6c2f2SEnji Cooper // `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates to a
72128f6c2f2SEnji Cooper // nonzero constant integer if the attribute is supported or 0 if not.
722b89a7cc2SEnji Cooper //
72328f6c2f2SEnji Cooper // It evaluates to zero if `__has_attribute` is not defined by the compiler.
72428f6c2f2SEnji Cooper //
72528f6c2f2SEnji Cooper // GCC: https://gcc.gnu.org/gcc-5/changes.html
72628f6c2f2SEnji Cooper // Clang: https://clang.llvm.org/docs/LanguageExtensions.html
72728f6c2f2SEnji Cooper #ifdef __has_attribute
72828f6c2f2SEnji Cooper #define GTEST_HAVE_ATTRIBUTE_(x) __has_attribute(x)
72928f6c2f2SEnji Cooper #else
73028f6c2f2SEnji Cooper #define GTEST_HAVE_ATTRIBUTE_(x) 0
73128f6c2f2SEnji Cooper #endif
73228f6c2f2SEnji Cooper 
73328f6c2f2SEnji Cooper // GTEST_HAVE_FEATURE_
73428f6c2f2SEnji Cooper //
73528f6c2f2SEnji Cooper // A function-like feature checking macro that is a wrapper around
73628f6c2f2SEnji Cooper // `__has_feature`.
73728f6c2f2SEnji Cooper #ifdef __has_feature
73828f6c2f2SEnji Cooper #define GTEST_HAVE_FEATURE_(x) __has_feature(x)
73928f6c2f2SEnji Cooper #else
74028f6c2f2SEnji Cooper #define GTEST_HAVE_FEATURE_(x) 0
74128f6c2f2SEnji Cooper #endif
74228f6c2f2SEnji Cooper 
74328f6c2f2SEnji Cooper // Use this annotation after a variable or parameter declaration to tell the
744b89a7cc2SEnji Cooper // compiler the variable/parameter does not have to be used.
74528f6c2f2SEnji Cooper // Example:
74628f6c2f2SEnji Cooper //
74728f6c2f2SEnji Cooper //   GTEST_ATTRIBUTE_UNUSED_ int foo = bar();
74828f6c2f2SEnji Cooper #if GTEST_HAVE_ATTRIBUTE_(unused)
749b89a7cc2SEnji Cooper #define GTEST_ATTRIBUTE_UNUSED_ __attribute__((unused))
75028f6c2f2SEnji Cooper #else
751b89a7cc2SEnji Cooper #define GTEST_ATTRIBUTE_UNUSED_
752b89a7cc2SEnji Cooper #endif
753b89a7cc2SEnji Cooper 
754b89a7cc2SEnji Cooper // Use this annotation before a function that takes a printf format string.
75528f6c2f2SEnji Cooper #if GTEST_HAVE_ATTRIBUTE_(format) && defined(__MINGW_PRINTF_FORMAT)
756b89a7cc2SEnji Cooper // MinGW has two different printf implementations. Ensure the format macro
757b89a7cc2SEnji Cooper // matches the selected implementation. See
758b89a7cc2SEnji Cooper // https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/.
759b89a7cc2SEnji Cooper #define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \
76028f6c2f2SEnji Cooper   __attribute__((format(__MINGW_PRINTF_FORMAT, string_index, first_to_check)))
76128f6c2f2SEnji Cooper #elif GTEST_HAVE_ATTRIBUTE_(format)
762b89a7cc2SEnji Cooper #define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \
76328f6c2f2SEnji Cooper   __attribute__((format(printf, string_index, first_to_check)))
764b89a7cc2SEnji Cooper #else
765b89a7cc2SEnji Cooper #define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check)
766b89a7cc2SEnji Cooper #endif
767b89a7cc2SEnji Cooper 
768b89a7cc2SEnji Cooper // Tell the compiler to warn about unused return values for functions declared
769b89a7cc2SEnji Cooper // with this macro.  The macro should be used on function declarations
770b89a7cc2SEnji Cooper // following the argument list:
771b89a7cc2SEnji Cooper //
772b89a7cc2SEnji Cooper //   Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_;
77328f6c2f2SEnji Cooper #if GTEST_HAVE_ATTRIBUTE_(warn_unused_result)
774b89a7cc2SEnji Cooper #define GTEST_MUST_USE_RESULT_ __attribute__((warn_unused_result))
775b89a7cc2SEnji Cooper #else
776b89a7cc2SEnji Cooper #define GTEST_MUST_USE_RESULT_
77728f6c2f2SEnji Cooper #endif
778b89a7cc2SEnji Cooper 
779b89a7cc2SEnji Cooper // MS C++ compiler emits warning when a conditional expression is compile time
780b89a7cc2SEnji Cooper // constant. In some contexts this warning is false positive and needs to be
781b89a7cc2SEnji Cooper // suppressed. Use the following two macros in such cases:
782b89a7cc2SEnji Cooper //
783b89a7cc2SEnji Cooper // GTEST_INTENTIONAL_CONST_COND_PUSH_()
784b89a7cc2SEnji Cooper // while (true) {
785b89a7cc2SEnji Cooper // GTEST_INTENTIONAL_CONST_COND_POP_()
786b89a7cc2SEnji Cooper // }
787b89a7cc2SEnji Cooper #define GTEST_INTENTIONAL_CONST_COND_PUSH_() \
788b89a7cc2SEnji Cooper   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127)
78928f6c2f2SEnji Cooper #define GTEST_INTENTIONAL_CONST_COND_POP_() GTEST_DISABLE_MSC_WARNINGS_POP_()
790b89a7cc2SEnji Cooper 
791b89a7cc2SEnji Cooper // Determine whether the compiler supports Microsoft's Structured Exception
792b89a7cc2SEnji Cooper // Handling.  This is supported by several Windows compilers but generally
793b89a7cc2SEnji Cooper // does not exist on any other system.
794b89a7cc2SEnji Cooper #ifndef GTEST_HAS_SEH
795b89a7cc2SEnji Cooper // The user didn't tell us, so we need to figure it out.
796b89a7cc2SEnji Cooper 
797b89a7cc2SEnji Cooper #if defined(_MSC_VER) || defined(__BORLANDC__)
798b89a7cc2SEnji Cooper // These two compilers are known to support SEH.
799b89a7cc2SEnji Cooper #define GTEST_HAS_SEH 1
800b89a7cc2SEnji Cooper #else
801b89a7cc2SEnji Cooper // Assume no SEH.
802b89a7cc2SEnji Cooper #define GTEST_HAS_SEH 0
803b89a7cc2SEnji Cooper #endif
804b89a7cc2SEnji Cooper 
805b89a7cc2SEnji Cooper #endif  // GTEST_HAS_SEH
806b89a7cc2SEnji Cooper 
80728f6c2f2SEnji Cooper #ifndef GTEST_IS_THREADSAFE
80828f6c2f2SEnji Cooper 
80928f6c2f2SEnji Cooper #if (GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ ||                              \
81028f6c2f2SEnji Cooper      (defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_PHONE) && \
81128f6c2f2SEnji Cooper       !defined(GTEST_OS_WINDOWS_RT)) ||                                \
81228f6c2f2SEnji Cooper      GTEST_HAS_PTHREAD)
81328f6c2f2SEnji Cooper #define GTEST_IS_THREADSAFE 1
81428f6c2f2SEnji Cooper #endif
81528f6c2f2SEnji Cooper 
81628f6c2f2SEnji Cooper #endif  // GTEST_IS_THREADSAFE
81728f6c2f2SEnji Cooper 
81828f6c2f2SEnji Cooper #ifdef GTEST_IS_THREADSAFE
81928f6c2f2SEnji Cooper // Some platforms don't support including these threading related headers.
82028f6c2f2SEnji Cooper #include <condition_variable>  // NOLINT
82128f6c2f2SEnji Cooper #include <mutex>               // NOLINT
82228f6c2f2SEnji Cooper #endif                         // GTEST_IS_THREADSAFE
82328f6c2f2SEnji Cooper 
824b89a7cc2SEnji Cooper // GTEST_API_ qualifies all symbols that must be exported. The definitions below
825b89a7cc2SEnji Cooper // are guarded by #ifndef to give embedders a chance to define GTEST_API_ in
826b89a7cc2SEnji Cooper // gtest/internal/custom/gtest-port.h
827b89a7cc2SEnji Cooper #ifndef GTEST_API_
828b89a7cc2SEnji Cooper 
829b89a7cc2SEnji Cooper #ifdef _MSC_VER
830b89a7cc2SEnji Cooper #if GTEST_LINKED_AS_SHARED_LIBRARY
831b89a7cc2SEnji Cooper #define GTEST_API_ __declspec(dllimport)
832b89a7cc2SEnji Cooper #elif GTEST_CREATE_SHARED_LIBRARY
833b89a7cc2SEnji Cooper #define GTEST_API_ __declspec(dllexport)
834b89a7cc2SEnji Cooper #endif
83528f6c2f2SEnji Cooper #elif GTEST_HAVE_ATTRIBUTE_(visibility)
836b89a7cc2SEnji Cooper #define GTEST_API_ __attribute__((visibility("default")))
837b89a7cc2SEnji Cooper #endif  // _MSC_VER
838b89a7cc2SEnji Cooper 
839b89a7cc2SEnji Cooper #endif  // GTEST_API_
840b89a7cc2SEnji Cooper 
841b89a7cc2SEnji Cooper #ifndef GTEST_API_
842b89a7cc2SEnji Cooper #define GTEST_API_
843b89a7cc2SEnji Cooper #endif  // GTEST_API_
844b89a7cc2SEnji Cooper 
845b89a7cc2SEnji Cooper #ifndef GTEST_DEFAULT_DEATH_TEST_STYLE
846b89a7cc2SEnji Cooper #define GTEST_DEFAULT_DEATH_TEST_STYLE "fast"
847b89a7cc2SEnji Cooper #endif  // GTEST_DEFAULT_DEATH_TEST_STYLE
848b89a7cc2SEnji Cooper 
84928f6c2f2SEnji Cooper #if GTEST_HAVE_ATTRIBUTE_(noinline)
850b89a7cc2SEnji Cooper // Ask the compiler to never inline a given function.
851b89a7cc2SEnji Cooper #define GTEST_NO_INLINE_ __attribute__((noinline))
852b89a7cc2SEnji Cooper #else
853b89a7cc2SEnji Cooper #define GTEST_NO_INLINE_
854b89a7cc2SEnji Cooper #endif
855b89a7cc2SEnji Cooper 
85628f6c2f2SEnji Cooper #if GTEST_HAVE_ATTRIBUTE_(disable_tail_calls)
85728f6c2f2SEnji Cooper // Ask the compiler not to perform tail call optimization inside
85828f6c2f2SEnji Cooper // the marked function.
85928f6c2f2SEnji Cooper #define GTEST_NO_TAIL_CALL_ __attribute__((disable_tail_calls))
86028f6c2f2SEnji Cooper #elif defined(__GNUC__) && !defined(__NVCOMPILER)
86128f6c2f2SEnji Cooper #define GTEST_NO_TAIL_CALL_ \
86228f6c2f2SEnji Cooper   __attribute__((optimize("no-optimize-sibling-calls")))
86328f6c2f2SEnji Cooper #else
86428f6c2f2SEnji Cooper #define GTEST_NO_TAIL_CALL_
86528f6c2f2SEnji Cooper #endif
86628f6c2f2SEnji Cooper 
867b89a7cc2SEnji Cooper // _LIBCPP_VERSION is defined by the libc++ library from the LLVM project.
868b89a7cc2SEnji Cooper #if !defined(GTEST_HAS_CXXABI_H_)
869b89a7cc2SEnji Cooper #if defined(__GLIBCXX__) || (defined(_LIBCPP_VERSION) && !defined(_MSC_VER))
870b89a7cc2SEnji Cooper #define GTEST_HAS_CXXABI_H_ 1
871b89a7cc2SEnji Cooper #else
872b89a7cc2SEnji Cooper #define GTEST_HAS_CXXABI_H_ 0
873b89a7cc2SEnji Cooper #endif
874b89a7cc2SEnji Cooper #endif
875b89a7cc2SEnji Cooper 
876b89a7cc2SEnji Cooper // A function level attribute to disable checking for use of uninitialized
877b89a7cc2SEnji Cooper // memory when built with MemorySanitizer.
87828f6c2f2SEnji Cooper #if GTEST_HAVE_ATTRIBUTE_(no_sanitize_memory)
87928f6c2f2SEnji Cooper #define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ __attribute__((no_sanitize_memory))
880b89a7cc2SEnji Cooper #else
881b89a7cc2SEnji Cooper #define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
88228f6c2f2SEnji Cooper #endif
883b89a7cc2SEnji Cooper 
884b89a7cc2SEnji Cooper // A function level attribute to disable AddressSanitizer instrumentation.
88528f6c2f2SEnji Cooper #if GTEST_HAVE_ATTRIBUTE_(no_sanitize_address)
886b89a7cc2SEnji Cooper #define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ \
887b89a7cc2SEnji Cooper   __attribute__((no_sanitize_address))
888b89a7cc2SEnji Cooper #else
889b89a7cc2SEnji Cooper #define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
89028f6c2f2SEnji Cooper #endif
89128f6c2f2SEnji Cooper 
89228f6c2f2SEnji Cooper // A function level attribute to disable HWAddressSanitizer instrumentation.
89328f6c2f2SEnji Cooper #if GTEST_HAVE_FEATURE_(hwaddress_sanitizer) && \
89428f6c2f2SEnji Cooper     GTEST_HAVE_ATTRIBUTE_(no_sanitize)
89528f6c2f2SEnji Cooper #define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ \
89628f6c2f2SEnji Cooper   __attribute__((no_sanitize("hwaddress")))
897b89a7cc2SEnji Cooper #else
89828f6c2f2SEnji Cooper #define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_
89928f6c2f2SEnji Cooper #endif
900b89a7cc2SEnji Cooper 
901b89a7cc2SEnji Cooper // A function level attribute to disable ThreadSanitizer instrumentation.
90228f6c2f2SEnji Cooper #if GTEST_HAVE_ATTRIBUTE_(no_sanitize_thread)
90328f6c2f2SEnji Cooper #define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ __attribute((no_sanitize_thread))
904b89a7cc2SEnji Cooper #else
905b89a7cc2SEnji Cooper #define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
90628f6c2f2SEnji Cooper #endif
907b89a7cc2SEnji Cooper 
908b89a7cc2SEnji Cooper namespace testing {
909b89a7cc2SEnji Cooper 
910b89a7cc2SEnji Cooper class Message;
911b89a7cc2SEnji Cooper 
91228f6c2f2SEnji Cooper // Legacy imports for backwards compatibility.
91328f6c2f2SEnji Cooper // New code should use std:: names directly.
91428f6c2f2SEnji Cooper using std::get;
91528f6c2f2SEnji Cooper using std::make_tuple;
91628f6c2f2SEnji Cooper using std::tuple;
91728f6c2f2SEnji Cooper using std::tuple_element;
91828f6c2f2SEnji Cooper using std::tuple_size;
919b89a7cc2SEnji Cooper 
920b89a7cc2SEnji Cooper namespace internal {
921b89a7cc2SEnji Cooper 
922b89a7cc2SEnji Cooper // A secret type that Google Test users don't know about.  It has no
92328f6c2f2SEnji Cooper // accessible constructors on purpose.  Therefore it's impossible to create a
924b89a7cc2SEnji Cooper // Secret object, which is what we want.
92528f6c2f2SEnji Cooper class Secret {
92628f6c2f2SEnji Cooper   Secret(const Secret&) = delete;
927b89a7cc2SEnji Cooper };
928b89a7cc2SEnji Cooper 
929b89a7cc2SEnji Cooper // A helper for suppressing warnings on constant condition.  It just
930b89a7cc2SEnji Cooper // returns 'condition'.
931b89a7cc2SEnji Cooper GTEST_API_ bool IsTrue(bool condition);
932b89a7cc2SEnji Cooper 
93328f6c2f2SEnji Cooper // Defines RE.
934b89a7cc2SEnji Cooper 
93528f6c2f2SEnji Cooper #ifdef GTEST_USES_RE2
93628f6c2f2SEnji Cooper 
93728f6c2f2SEnji Cooper // This is almost `using RE = ::RE2`, except it is copy-constructible, and it
93828f6c2f2SEnji Cooper // needs to disambiguate the `std::string`, `absl::string_view`, and `const
93928f6c2f2SEnji Cooper // char*` constructors.
94028f6c2f2SEnji Cooper class GTEST_API_ RE {
941b89a7cc2SEnji Cooper  public:
RE(absl::string_view regex)94228f6c2f2SEnji Cooper   RE(absl::string_view regex) : regex_(regex) {}                  // NOLINT
RE(const char * regex)94328f6c2f2SEnji Cooper   RE(const char* regex) : RE(absl::string_view(regex)) {}         // NOLINT
RE(const std::string & regex)94428f6c2f2SEnji Cooper   RE(const std::string& regex) : RE(absl::string_view(regex)) {}  // NOLINT
RE(const RE & other)94528f6c2f2SEnji Cooper   RE(const RE& other) : RE(other.pattern()) {}
946b89a7cc2SEnji Cooper 
pattern()94728f6c2f2SEnji Cooper   const std::string& pattern() const { return regex_.pattern(); }
948b89a7cc2SEnji Cooper 
FullMatch(absl::string_view str,const RE & re)94928f6c2f2SEnji Cooper   static bool FullMatch(absl::string_view str, const RE& re) {
95028f6c2f2SEnji Cooper     return RE2::FullMatch(str, re.regex_);
951b89a7cc2SEnji Cooper   }
PartialMatch(absl::string_view str,const RE & re)95228f6c2f2SEnji Cooper   static bool PartialMatch(absl::string_view str, const RE& re) {
95328f6c2f2SEnji Cooper     return RE2::PartialMatch(str, re.regex_);
954b89a7cc2SEnji Cooper   }
955b89a7cc2SEnji Cooper 
956b89a7cc2SEnji Cooper  private:
95728f6c2f2SEnji Cooper   RE2 regex_;
958b89a7cc2SEnji Cooper };
959b89a7cc2SEnji Cooper 
96028f6c2f2SEnji Cooper #elif defined(GTEST_USES_POSIX_RE) || defined(GTEST_USES_SIMPLE_RE)
96128f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
96228f6c2f2SEnji Cooper /* class A needs to have dll-interface to be used by clients of class B */)
963b89a7cc2SEnji Cooper 
964b89a7cc2SEnji Cooper // A simple C++ wrapper for <regex.h>.  It uses the POSIX Extended
965b89a7cc2SEnji Cooper // Regular Expression syntax.
966b89a7cc2SEnji Cooper class GTEST_API_ RE {
967b89a7cc2SEnji Cooper  public:
968b89a7cc2SEnji Cooper   // A copy constructor is required by the Standard to initialize object
969b89a7cc2SEnji Cooper   // references from r-values.
RE(const RE & other)970b89a7cc2SEnji Cooper   RE(const RE& other) { Init(other.pattern()); }
971b89a7cc2SEnji Cooper 
972b89a7cc2SEnji Cooper   // Constructs an RE from a string.
RE(const::std::string & regex)973b89a7cc2SEnji Cooper   RE(const ::std::string& regex) { Init(regex.c_str()); }  // NOLINT
974b89a7cc2SEnji Cooper 
RE(const char * regex)975b89a7cc2SEnji Cooper   RE(const char* regex) { Init(regex); }  // NOLINT
976b89a7cc2SEnji Cooper   ~RE();
977b89a7cc2SEnji Cooper 
978b89a7cc2SEnji Cooper   // Returns the string representation of the regex.
pattern()97928f6c2f2SEnji Cooper   const char* pattern() const { return pattern_.c_str(); }
980b89a7cc2SEnji Cooper 
98128f6c2f2SEnji Cooper   // FullMatch(str, re) returns true if and only if regular expression re
98228f6c2f2SEnji Cooper   // matches the entire str.
98328f6c2f2SEnji Cooper   // PartialMatch(str, re) returns true if and only if regular expression re
984b89a7cc2SEnji Cooper   // matches a substring of str (including str itself).
FullMatch(const::std::string & str,const RE & re)985b89a7cc2SEnji Cooper   static bool FullMatch(const ::std::string& str, const RE& re) {
986b89a7cc2SEnji Cooper     return FullMatch(str.c_str(), re);
987b89a7cc2SEnji Cooper   }
PartialMatch(const::std::string & str,const RE & re)988b89a7cc2SEnji Cooper   static bool PartialMatch(const ::std::string& str, const RE& re) {
989b89a7cc2SEnji Cooper     return PartialMatch(str.c_str(), re);
990b89a7cc2SEnji Cooper   }
991b89a7cc2SEnji Cooper 
992b89a7cc2SEnji Cooper   static bool FullMatch(const char* str, const RE& re);
993b89a7cc2SEnji Cooper   static bool PartialMatch(const char* str, const RE& re);
994b89a7cc2SEnji Cooper 
995b89a7cc2SEnji Cooper  private:
996b89a7cc2SEnji Cooper   void Init(const char* regex);
99728f6c2f2SEnji Cooper   std::string pattern_;
998b89a7cc2SEnji Cooper   bool is_valid_;
999b89a7cc2SEnji Cooper 
100028f6c2f2SEnji Cooper #ifdef GTEST_USES_POSIX_RE
1001b89a7cc2SEnji Cooper 
1002b89a7cc2SEnji Cooper   regex_t full_regex_;     // For FullMatch().
1003b89a7cc2SEnji Cooper   regex_t partial_regex_;  // For PartialMatch().
1004b89a7cc2SEnji Cooper 
1005b89a7cc2SEnji Cooper #else  // GTEST_USES_SIMPLE_RE
1006b89a7cc2SEnji Cooper 
100728f6c2f2SEnji Cooper   std::string full_pattern_;  // For FullMatch();
1008b89a7cc2SEnji Cooper 
1009b89a7cc2SEnji Cooper #endif
1010b89a7cc2SEnji Cooper };
101128f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_()  // 4251
101228f6c2f2SEnji Cooper #endif  // ::testing::internal::RE implementation
1013b89a7cc2SEnji Cooper 
1014b89a7cc2SEnji Cooper // Formats a source file path and a line number as they would appear
1015b89a7cc2SEnji Cooper // in an error message from the compiler used to compile this code.
1016b89a7cc2SEnji Cooper GTEST_API_ ::std::string FormatFileLocation(const char* file, int line);
1017b89a7cc2SEnji Cooper 
1018b89a7cc2SEnji Cooper // Formats a file location for compiler-independent XML output.
1019b89a7cc2SEnji Cooper // Although this function is not platform dependent, we put it next to
1020b89a7cc2SEnji Cooper // FormatFileLocation in order to contrast the two functions.
1021b89a7cc2SEnji Cooper GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file,
1022b89a7cc2SEnji Cooper                                                                int line);
1023b89a7cc2SEnji Cooper 
1024b89a7cc2SEnji Cooper // Defines logging utilities:
1025b89a7cc2SEnji Cooper //   GTEST_LOG_(severity) - logs messages at the specified severity level. The
1026b89a7cc2SEnji Cooper //                          message itself is streamed into the macro.
1027b89a7cc2SEnji Cooper //   LogToStderr()  - directs all log messages to stderr.
1028b89a7cc2SEnji Cooper //   FlushInfoLog() - flushes informational log messages.
1029b89a7cc2SEnji Cooper 
103028f6c2f2SEnji Cooper enum GTestLogSeverity { GTEST_INFO, GTEST_WARNING, GTEST_ERROR, GTEST_FATAL };
1031b89a7cc2SEnji Cooper 
1032b89a7cc2SEnji Cooper // Formats log entry severity, provides a stream object for streaming the
1033b89a7cc2SEnji Cooper // log message, and terminates the message with a newline when going out of
1034b89a7cc2SEnji Cooper // scope.
1035b89a7cc2SEnji Cooper class GTEST_API_ GTestLog {
1036b89a7cc2SEnji Cooper  public:
1037b89a7cc2SEnji Cooper   GTestLog(GTestLogSeverity severity, const char* file, int line);
1038b89a7cc2SEnji Cooper 
1039b89a7cc2SEnji Cooper   // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
1040b89a7cc2SEnji Cooper   ~GTestLog();
1041b89a7cc2SEnji Cooper 
GetStream()1042b89a7cc2SEnji Cooper   ::std::ostream& GetStream() { return ::std::cerr; }
1043b89a7cc2SEnji Cooper 
1044b89a7cc2SEnji Cooper  private:
1045b89a7cc2SEnji Cooper   const GTestLogSeverity severity_;
1046b89a7cc2SEnji Cooper 
104728f6c2f2SEnji Cooper   GTestLog(const GTestLog&) = delete;
104828f6c2f2SEnji Cooper   GTestLog& operator=(const GTestLog&) = delete;
1049b89a7cc2SEnji Cooper };
1050b89a7cc2SEnji Cooper 
1051b89a7cc2SEnji Cooper #if !defined(GTEST_LOG_)
1052b89a7cc2SEnji Cooper 
1053b89a7cc2SEnji Cooper #define GTEST_LOG_(severity)                                           \
1054b89a7cc2SEnji Cooper   ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
105528f6c2f2SEnji Cooper                                 __FILE__, __LINE__)                    \
105628f6c2f2SEnji Cooper       .GetStream()
1057b89a7cc2SEnji Cooper 
LogToStderr()1058b89a7cc2SEnji Cooper inline void LogToStderr() {}
FlushInfoLog()105928f6c2f2SEnji Cooper inline void FlushInfoLog() { fflush(nullptr); }
1060b89a7cc2SEnji Cooper 
1061b89a7cc2SEnji Cooper #endif  // !defined(GTEST_LOG_)
1062b89a7cc2SEnji Cooper 
1063b89a7cc2SEnji Cooper #if !defined(GTEST_CHECK_)
1064b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE.
1065b89a7cc2SEnji Cooper //
1066b89a7cc2SEnji Cooper // GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition
1067b89a7cc2SEnji Cooper // is not satisfied.
106828f6c2f2SEnji Cooper //  Synopsis:
1069b89a7cc2SEnji Cooper //    GTEST_CHECK_(boolean_condition);
1070b89a7cc2SEnji Cooper //     or
1071b89a7cc2SEnji Cooper //    GTEST_CHECK_(boolean_condition) << "Additional message";
1072b89a7cc2SEnji Cooper //
1073b89a7cc2SEnji Cooper //    This checks the condition and if the condition is not satisfied
1074b89a7cc2SEnji Cooper //    it prints message about the condition violation, including the
1075b89a7cc2SEnji Cooper //    condition itself, plus additional message streamed into it, if any,
1076b89a7cc2SEnji Cooper //    and then it aborts the program. It aborts the program irrespective of
1077b89a7cc2SEnji Cooper //    whether it is built in the debug mode or not.
1078b89a7cc2SEnji Cooper #define GTEST_CHECK_(condition)               \
1079b89a7cc2SEnji Cooper   GTEST_AMBIGUOUS_ELSE_BLOCKER_               \
1080b89a7cc2SEnji Cooper   if (::testing::internal::IsTrue(condition)) \
1081b89a7cc2SEnji Cooper     ;                                         \
1082b89a7cc2SEnji Cooper   else                                        \
1083b89a7cc2SEnji Cooper     GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
1084b89a7cc2SEnji Cooper #endif  // !defined(GTEST_CHECK_)
1085b89a7cc2SEnji Cooper 
1086b89a7cc2SEnji Cooper // An all-mode assert to verify that the given POSIX-style function
1087b89a7cc2SEnji Cooper // call returns 0 (indicating success).  Known limitation: this
1088b89a7cc2SEnji Cooper // doesn't expand to a balanced 'if' statement, so enclose the macro
1089b89a7cc2SEnji Cooper // in {} if you need to use it as the only statement in an 'if'
1090b89a7cc2SEnji Cooper // branch.
1091b89a7cc2SEnji Cooper #define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \
1092b89a7cc2SEnji Cooper   if (const int gtest_error = (posix_call))    \
109328f6c2f2SEnji Cooper   GTEST_LOG_(FATAL) << #posix_call << "failed with error " << gtest_error
1094b89a7cc2SEnji Cooper 
1095b89a7cc2SEnji Cooper // Transforms "T" into "const T&" according to standard reference collapsing
1096b89a7cc2SEnji Cooper // rules (this is only needed as a backport for C++98 compilers that do not
1097b89a7cc2SEnji Cooper // support reference collapsing). Specifically, it transforms:
1098b89a7cc2SEnji Cooper //
1099b89a7cc2SEnji Cooper //   char         ==> const char&
1100b89a7cc2SEnji Cooper //   const char   ==> const char&
1101b89a7cc2SEnji Cooper //   char&        ==> char&
1102b89a7cc2SEnji Cooper //   const char&  ==> const char&
1103b89a7cc2SEnji Cooper //
1104b89a7cc2SEnji Cooper // Note that the non-const reference will not have "const" added. This is
1105b89a7cc2SEnji Cooper // standard, and necessary so that "T" can always bind to "const T&".
1106b89a7cc2SEnji Cooper template <typename T>
110728f6c2f2SEnji Cooper struct ConstRef {
110828f6c2f2SEnji Cooper   typedef const T& type;
110928f6c2f2SEnji Cooper };
1110b89a7cc2SEnji Cooper template <typename T>
111128f6c2f2SEnji Cooper struct ConstRef<T&> {
111228f6c2f2SEnji Cooper   typedef T& type;
111328f6c2f2SEnji Cooper };
1114b89a7cc2SEnji Cooper 
1115b89a7cc2SEnji Cooper // The argument T must depend on some template parameters.
1116b89a7cc2SEnji Cooper #define GTEST_REFERENCE_TO_CONST_(T) \
1117b89a7cc2SEnji Cooper   typename ::testing::internal::ConstRef<T>::type
1118b89a7cc2SEnji Cooper 
1119b89a7cc2SEnji Cooper // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
1120b89a7cc2SEnji Cooper //
1121b89a7cc2SEnji Cooper // Use ImplicitCast_ as a safe version of static_cast for upcasting in
1122b89a7cc2SEnji Cooper // the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a
1123b89a7cc2SEnji Cooper // const Foo*).  When you use ImplicitCast_, the compiler checks that
1124b89a7cc2SEnji Cooper // the cast is safe.  Such explicit ImplicitCast_s are necessary in
1125b89a7cc2SEnji Cooper // surprisingly many situations where C++ demands an exact type match
112628f6c2f2SEnji Cooper // instead of an argument type convertible to a target type.
1127b89a7cc2SEnji Cooper //
1128b89a7cc2SEnji Cooper // The syntax for using ImplicitCast_ is the same as for static_cast:
1129b89a7cc2SEnji Cooper //
1130b89a7cc2SEnji Cooper //   ImplicitCast_<ToType>(expr)
1131b89a7cc2SEnji Cooper //
1132b89a7cc2SEnji Cooper // ImplicitCast_ would have been part of the C++ standard library,
1133b89a7cc2SEnji Cooper // but the proposal was submitted too late.  It will probably make
1134b89a7cc2SEnji Cooper // its way into the language in the future.
1135b89a7cc2SEnji Cooper //
1136b89a7cc2SEnji Cooper // This relatively ugly name is intentional. It prevents clashes with
1137b89a7cc2SEnji Cooper // similar functions users may have (e.g., implicit_cast). The internal
1138b89a7cc2SEnji Cooper // namespace alone is not enough because the function can be found by ADL.
1139b89a7cc2SEnji Cooper template <typename To>
114028f6c2f2SEnji Cooper inline To ImplicitCast_(To x) {
114128f6c2f2SEnji Cooper   return x;
1142b89a7cc2SEnji Cooper }
1143b89a7cc2SEnji Cooper 
1144b89a7cc2SEnji Cooper // Downcasts the pointer of type Base to Derived.
1145b89a7cc2SEnji Cooper // Derived must be a subclass of Base. The parameter MUST
1146b89a7cc2SEnji Cooper // point to a class of type Derived, not any subclass of it.
1147b89a7cc2SEnji Cooper // When RTTI is available, the function performs a runtime
1148b89a7cc2SEnji Cooper // check to enforce this.
1149b89a7cc2SEnji Cooper template <class Derived, class Base>
1150b89a7cc2SEnji Cooper Derived* CheckedDowncastToActualType(Base* base) {
115128f6c2f2SEnji Cooper   static_assert(std::is_base_of<Base, Derived>::value,
115228f6c2f2SEnji Cooper                 "target type not derived from source type");
1153b89a7cc2SEnji Cooper #if GTEST_HAS_RTTI
115428f6c2f2SEnji Cooper   GTEST_CHECK_(base == nullptr || dynamic_cast<Derived*>(base) != nullptr);
1155b89a7cc2SEnji Cooper #endif
115628f6c2f2SEnji Cooper   return static_cast<Derived*>(base);
1157b89a7cc2SEnji Cooper }
1158b89a7cc2SEnji Cooper 
1159b89a7cc2SEnji Cooper #if GTEST_HAS_STREAM_REDIRECTION
1160b89a7cc2SEnji Cooper 
1161b89a7cc2SEnji Cooper // Defines the stderr capturer:
1162b89a7cc2SEnji Cooper //   CaptureStdout     - starts capturing stdout.
1163b89a7cc2SEnji Cooper //   GetCapturedStdout - stops capturing stdout and returns the captured string.
1164b89a7cc2SEnji Cooper //   CaptureStderr     - starts capturing stderr.
1165b89a7cc2SEnji Cooper //   GetCapturedStderr - stops capturing stderr and returns the captured string.
1166b89a7cc2SEnji Cooper //
1167b89a7cc2SEnji Cooper GTEST_API_ void CaptureStdout();
1168b89a7cc2SEnji Cooper GTEST_API_ std::string GetCapturedStdout();
1169b89a7cc2SEnji Cooper GTEST_API_ void CaptureStderr();
1170b89a7cc2SEnji Cooper GTEST_API_ std::string GetCapturedStderr();
1171b89a7cc2SEnji Cooper 
1172b89a7cc2SEnji Cooper #endif  // GTEST_HAS_STREAM_REDIRECTION
1173b89a7cc2SEnji Cooper // Returns the size (in bytes) of a file.
1174b89a7cc2SEnji Cooper GTEST_API_ size_t GetFileSize(FILE* file);
1175b89a7cc2SEnji Cooper 
1176b89a7cc2SEnji Cooper // Reads the entire content of a file as a string.
1177b89a7cc2SEnji Cooper GTEST_API_ std::string ReadEntireFile(FILE* file);
1178b89a7cc2SEnji Cooper 
1179b89a7cc2SEnji Cooper // All command line arguments.
1180b89a7cc2SEnji Cooper GTEST_API_ std::vector<std::string> GetArgvs();
1181b89a7cc2SEnji Cooper 
118228f6c2f2SEnji Cooper #ifdef GTEST_HAS_DEATH_TEST
1183b89a7cc2SEnji Cooper 
1184b89a7cc2SEnji Cooper std::vector<std::string> GetInjectableArgvs();
1185b89a7cc2SEnji Cooper // Deprecated: pass the args vector by value instead.
1186b89a7cc2SEnji Cooper void SetInjectableArgvs(const std::vector<std::string>* new_argvs);
1187b89a7cc2SEnji Cooper void SetInjectableArgvs(const std::vector<std::string>& new_argvs);
1188b89a7cc2SEnji Cooper void ClearInjectableArgvs();
1189b89a7cc2SEnji Cooper 
1190b89a7cc2SEnji Cooper #endif  // GTEST_HAS_DEATH_TEST
1191b89a7cc2SEnji Cooper 
1192b89a7cc2SEnji Cooper // Defines synchronization primitives.
119328f6c2f2SEnji Cooper #ifdef GTEST_IS_THREADSAFE
1194b89a7cc2SEnji Cooper 
119528f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
1196b89a7cc2SEnji Cooper // Provides leak-safe Windows kernel handle ownership.
1197b89a7cc2SEnji Cooper // Used in death tests and in threading support.
1198b89a7cc2SEnji Cooper class GTEST_API_ AutoHandle {
1199b89a7cc2SEnji Cooper  public:
1200b89a7cc2SEnji Cooper   // Assume that Win32 HANDLE type is equivalent to void*. Doing so allows us to
1201b89a7cc2SEnji Cooper   // avoid including <windows.h> in this header file. Including <windows.h> is
1202b89a7cc2SEnji Cooper   // undesirable because it defines a lot of symbols and macros that tend to
1203b89a7cc2SEnji Cooper   // conflict with client code. This assumption is verified by
1204b89a7cc2SEnji Cooper   // WindowsTypesTest.HANDLEIsVoidStar.
1205b89a7cc2SEnji Cooper   typedef void* Handle;
1206b89a7cc2SEnji Cooper   AutoHandle();
1207b89a7cc2SEnji Cooper   explicit AutoHandle(Handle handle);
1208b89a7cc2SEnji Cooper 
1209b89a7cc2SEnji Cooper   ~AutoHandle();
1210b89a7cc2SEnji Cooper 
1211b89a7cc2SEnji Cooper   Handle Get() const;
1212b89a7cc2SEnji Cooper   void Reset();
1213b89a7cc2SEnji Cooper   void Reset(Handle handle);
1214b89a7cc2SEnji Cooper 
1215b89a7cc2SEnji Cooper  private:
121628f6c2f2SEnji Cooper   // Returns true if and only if the handle is a valid handle object that can be
121728f6c2f2SEnji Cooper   // closed.
1218b89a7cc2SEnji Cooper   bool IsCloseable() const;
1219b89a7cc2SEnji Cooper 
1220b89a7cc2SEnji Cooper   Handle handle_;
1221b89a7cc2SEnji Cooper 
122228f6c2f2SEnji Cooper   AutoHandle(const AutoHandle&) = delete;
122328f6c2f2SEnji Cooper   AutoHandle& operator=(const AutoHandle&) = delete;
1224b89a7cc2SEnji Cooper };
122528f6c2f2SEnji Cooper #endif
122628f6c2f2SEnji Cooper 
122728f6c2f2SEnji Cooper #if GTEST_HAS_NOTIFICATION_
122828f6c2f2SEnji Cooper // Notification has already been imported into the namespace.
122928f6c2f2SEnji Cooper // Nothing to do here.
123028f6c2f2SEnji Cooper 
123128f6c2f2SEnji Cooper #else
123228f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
123328f6c2f2SEnji Cooper /* class A needs to have dll-interface to be used by clients of class B */)
1234b89a7cc2SEnji Cooper 
1235b89a7cc2SEnji Cooper // Allows a controller thread to pause execution of newly created
1236b89a7cc2SEnji Cooper // threads until notified.  Instances of this class must be created
1237b89a7cc2SEnji Cooper // and destroyed in the controller thread.
1238b89a7cc2SEnji Cooper //
1239b89a7cc2SEnji Cooper // This class is only for testing Google Test's own constructs. Do not
1240b89a7cc2SEnji Cooper // use it in user tests, either directly or indirectly.
124128f6c2f2SEnji Cooper // TODO(b/203539622): Replace unconditionally with absl::Notification.
1242b89a7cc2SEnji Cooper class GTEST_API_ Notification {
1243b89a7cc2SEnji Cooper  public:
124428f6c2f2SEnji Cooper   Notification() : notified_(false) {}
124528f6c2f2SEnji Cooper   Notification(const Notification&) = delete;
124628f6c2f2SEnji Cooper   Notification& operator=(const Notification&) = delete;
124728f6c2f2SEnji Cooper 
124828f6c2f2SEnji Cooper   // Notifies all threads created with this notification to start. Must
124928f6c2f2SEnji Cooper   // be called from the controller thread.
125028f6c2f2SEnji Cooper   void Notify() {
125128f6c2f2SEnji Cooper     std::lock_guard<std::mutex> lock(mu_);
125228f6c2f2SEnji Cooper     notified_ = true;
125328f6c2f2SEnji Cooper     cv_.notify_all();
125428f6c2f2SEnji Cooper   }
125528f6c2f2SEnji Cooper 
125628f6c2f2SEnji Cooper   // Blocks until the controller thread notifies. Must be called from a test
125728f6c2f2SEnji Cooper   // thread.
125828f6c2f2SEnji Cooper   void WaitForNotification() {
125928f6c2f2SEnji Cooper     std::unique_lock<std::mutex> lock(mu_);
126028f6c2f2SEnji Cooper     cv_.wait(lock, [this]() { return notified_; });
126128f6c2f2SEnji Cooper   }
1262b89a7cc2SEnji Cooper 
1263b89a7cc2SEnji Cooper  private:
126428f6c2f2SEnji Cooper   std::mutex mu_;
126528f6c2f2SEnji Cooper   std::condition_variable cv_;
126628f6c2f2SEnji Cooper   bool notified_;
1267b89a7cc2SEnji Cooper };
126828f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_()  // 4251
1269b89a7cc2SEnji Cooper #endif  // GTEST_HAS_NOTIFICATION_
1270b89a7cc2SEnji Cooper 
1271b89a7cc2SEnji Cooper // On MinGW, we can have both GTEST_OS_WINDOWS and GTEST_HAS_PTHREAD
1272b89a7cc2SEnji Cooper // defined, but we don't want to use MinGW's pthreads implementation, which
1273b89a7cc2SEnji Cooper // has conformance problems with some versions of the POSIX standard.
127428f6c2f2SEnji Cooper #if GTEST_HAS_PTHREAD && !defined(GTEST_OS_WINDOWS_MINGW)
1275b89a7cc2SEnji Cooper 
1276b89a7cc2SEnji Cooper // As a C-function, ThreadFuncWithCLinkage cannot be templated itself.
1277b89a7cc2SEnji Cooper // Consequently, it cannot select a correct instantiation of ThreadWithParam
1278b89a7cc2SEnji Cooper // in order to call its Run(). Introducing ThreadWithParamBase as a
1279b89a7cc2SEnji Cooper // non-templated base class for ThreadWithParam allows us to bypass this
1280b89a7cc2SEnji Cooper // problem.
1281b89a7cc2SEnji Cooper class ThreadWithParamBase {
1282b89a7cc2SEnji Cooper  public:
128328f6c2f2SEnji Cooper   virtual ~ThreadWithParamBase() = default;
1284b89a7cc2SEnji Cooper   virtual void Run() = 0;
1285b89a7cc2SEnji Cooper };
1286b89a7cc2SEnji Cooper 
1287b89a7cc2SEnji Cooper // pthread_create() accepts a pointer to a function type with the C linkage.
1288b89a7cc2SEnji Cooper // According to the Standard (7.5/1), function types with different linkages
1289b89a7cc2SEnji Cooper // are different even if they are otherwise identical.  Some compilers (for
1290b89a7cc2SEnji Cooper // example, SunStudio) treat them as different types.  Since class methods
1291b89a7cc2SEnji Cooper // cannot be defined with C-linkage we need to define a free C-function to
1292b89a7cc2SEnji Cooper // pass into pthread_create().
1293b89a7cc2SEnji Cooper extern "C" inline void* ThreadFuncWithCLinkage(void* thread) {
1294b89a7cc2SEnji Cooper   static_cast<ThreadWithParamBase*>(thread)->Run();
129528f6c2f2SEnji Cooper   return nullptr;
1296b89a7cc2SEnji Cooper }
1297b89a7cc2SEnji Cooper 
1298b89a7cc2SEnji Cooper // Helper class for testing Google Test's multi-threading constructs.
1299b89a7cc2SEnji Cooper // To use it, write:
1300b89a7cc2SEnji Cooper //
1301b89a7cc2SEnji Cooper //   void ThreadFunc(int param) { /* Do things with param */ }
1302b89a7cc2SEnji Cooper //   Notification thread_can_start;
1303b89a7cc2SEnji Cooper //   ...
1304b89a7cc2SEnji Cooper //   // The thread_can_start parameter is optional; you can supply NULL.
1305b89a7cc2SEnji Cooper //   ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start);
1306b89a7cc2SEnji Cooper //   thread_can_start.Notify();
1307b89a7cc2SEnji Cooper //
1308b89a7cc2SEnji Cooper // These classes are only for testing Google Test's own constructs. Do
1309b89a7cc2SEnji Cooper // not use them in user tests, either directly or indirectly.
1310b89a7cc2SEnji Cooper template <typename T>
1311b89a7cc2SEnji Cooper class ThreadWithParam : public ThreadWithParamBase {
1312b89a7cc2SEnji Cooper  public:
1313b89a7cc2SEnji Cooper   typedef void UserThreadFunc(T);
1314b89a7cc2SEnji Cooper 
1315b89a7cc2SEnji Cooper   ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start)
1316b89a7cc2SEnji Cooper       : func_(func),
1317b89a7cc2SEnji Cooper         param_(param),
1318b89a7cc2SEnji Cooper         thread_can_start_(thread_can_start),
1319b89a7cc2SEnji Cooper         finished_(false) {
1320b89a7cc2SEnji Cooper     ThreadWithParamBase* const base = this;
1321b89a7cc2SEnji Cooper     // The thread can be created only after all fields except thread_
1322b89a7cc2SEnji Cooper     // have been initialized.
1323b89a7cc2SEnji Cooper     GTEST_CHECK_POSIX_SUCCESS_(
132428f6c2f2SEnji Cooper         pthread_create(&thread_, nullptr, &ThreadFuncWithCLinkage, base));
1325b89a7cc2SEnji Cooper   }
132628f6c2f2SEnji Cooper   ~ThreadWithParam() override { Join(); }
1327b89a7cc2SEnji Cooper 
1328b89a7cc2SEnji Cooper   void Join() {
1329b89a7cc2SEnji Cooper     if (!finished_) {
133028f6c2f2SEnji Cooper       GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, nullptr));
1331b89a7cc2SEnji Cooper       finished_ = true;
1332b89a7cc2SEnji Cooper     }
1333b89a7cc2SEnji Cooper   }
1334b89a7cc2SEnji Cooper 
133528f6c2f2SEnji Cooper   void Run() override {
133628f6c2f2SEnji Cooper     if (thread_can_start_ != nullptr) thread_can_start_->WaitForNotification();
1337b89a7cc2SEnji Cooper     func_(param_);
1338b89a7cc2SEnji Cooper   }
1339b89a7cc2SEnji Cooper 
1340b89a7cc2SEnji Cooper  private:
1341b89a7cc2SEnji Cooper   UserThreadFunc* const func_;  // User-supplied thread function.
1342b89a7cc2SEnji Cooper   const T param_;  // User-supplied parameter to the thread function.
1343b89a7cc2SEnji Cooper   // When non-NULL, used to block execution until the controller thread
1344b89a7cc2SEnji Cooper   // notifies.
1345b89a7cc2SEnji Cooper   Notification* const thread_can_start_;
134628f6c2f2SEnji Cooper   bool finished_;  // true if and only if we know that the thread function has
134728f6c2f2SEnji Cooper                    // finished.
1348b89a7cc2SEnji Cooper   pthread_t thread_;  // The native thread object.
1349b89a7cc2SEnji Cooper 
135028f6c2f2SEnji Cooper   ThreadWithParam(const ThreadWithParam&) = delete;
135128f6c2f2SEnji Cooper   ThreadWithParam& operator=(const ThreadWithParam&) = delete;
1352b89a7cc2SEnji Cooper };
1353b89a7cc2SEnji Cooper #endif  // !GTEST_OS_WINDOWS && GTEST_HAS_PTHREAD ||
1354b89a7cc2SEnji Cooper         // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
1355b89a7cc2SEnji Cooper 
1356b89a7cc2SEnji Cooper #if GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
1357b89a7cc2SEnji Cooper // Mutex and ThreadLocal have already been imported into the namespace.
1358b89a7cc2SEnji Cooper // Nothing to do here.
1359b89a7cc2SEnji Cooper 
136028f6c2f2SEnji Cooper #elif defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_PHONE) && \
136128f6c2f2SEnji Cooper     !defined(GTEST_OS_WINDOWS_RT)
1362b89a7cc2SEnji Cooper 
1363b89a7cc2SEnji Cooper // Mutex implements mutex on Windows platforms.  It is used in conjunction
1364b89a7cc2SEnji Cooper // with class MutexLock:
1365b89a7cc2SEnji Cooper //
1366b89a7cc2SEnji Cooper //   Mutex mutex;
1367b89a7cc2SEnji Cooper //   ...
1368b89a7cc2SEnji Cooper //   MutexLock lock(&mutex);  // Acquires the mutex and releases it at the
1369b89a7cc2SEnji Cooper //                            // end of the current scope.
1370b89a7cc2SEnji Cooper //
1371b89a7cc2SEnji Cooper // A static Mutex *must* be defined or declared using one of the following
1372b89a7cc2SEnji Cooper // macros:
1373b89a7cc2SEnji Cooper //   GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex);
1374b89a7cc2SEnji Cooper //   GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex);
1375b89a7cc2SEnji Cooper //
1376b89a7cc2SEnji Cooper // (A non-static Mutex is defined/declared in the usual way).
1377b89a7cc2SEnji Cooper class GTEST_API_ Mutex {
1378b89a7cc2SEnji Cooper  public:
1379b89a7cc2SEnji Cooper   enum MutexType { kStatic = 0, kDynamic = 1 };
1380b89a7cc2SEnji Cooper   // We rely on kStaticMutex being 0 as it is to what the linker initializes
1381b89a7cc2SEnji Cooper   // type_ in static mutexes.  critical_section_ will be initialized lazily
1382b89a7cc2SEnji Cooper   // in ThreadSafeLazyInit().
1383b89a7cc2SEnji Cooper   enum StaticConstructorSelector { kStaticMutex = 0 };
1384b89a7cc2SEnji Cooper 
1385b89a7cc2SEnji Cooper   // This constructor intentionally does nothing.  It relies on type_ being
1386b89a7cc2SEnji Cooper   // statically initialized to 0 (effectively setting it to kStatic) and on
1387b89a7cc2SEnji Cooper   // ThreadSafeLazyInit() to lazily initialize the rest of the members.
1388b89a7cc2SEnji Cooper   explicit Mutex(StaticConstructorSelector /*dummy*/) {}
1389b89a7cc2SEnji Cooper 
1390b89a7cc2SEnji Cooper   Mutex();
1391b89a7cc2SEnji Cooper   ~Mutex();
1392b89a7cc2SEnji Cooper 
1393b89a7cc2SEnji Cooper   void Lock();
1394b89a7cc2SEnji Cooper 
1395b89a7cc2SEnji Cooper   void Unlock();
1396b89a7cc2SEnji Cooper 
1397b89a7cc2SEnji Cooper   // Does nothing if the current thread holds the mutex. Otherwise, crashes
1398b89a7cc2SEnji Cooper   // with high probability.
1399b89a7cc2SEnji Cooper   void AssertHeld();
1400b89a7cc2SEnji Cooper 
1401b89a7cc2SEnji Cooper  private:
1402b89a7cc2SEnji Cooper   // Initializes owner_thread_id_ and critical_section_ in static mutexes.
1403b89a7cc2SEnji Cooper   void ThreadSafeLazyInit();
1404b89a7cc2SEnji Cooper 
1405b89a7cc2SEnji Cooper   // Per https://blogs.msdn.microsoft.com/oldnewthing/20040223-00/?p=40503,
1406b89a7cc2SEnji Cooper   // we assume that 0 is an invalid value for thread IDs.
1407b89a7cc2SEnji Cooper   unsigned int owner_thread_id_;
1408b89a7cc2SEnji Cooper 
1409b89a7cc2SEnji Cooper   // For static mutexes, we rely on these members being initialized to zeros
1410b89a7cc2SEnji Cooper   // by the linker.
1411b89a7cc2SEnji Cooper   MutexType type_;
1412b89a7cc2SEnji Cooper   long critical_section_init_phase_;  // NOLINT
1413b89a7cc2SEnji Cooper   GTEST_CRITICAL_SECTION* critical_section_;
1414b89a7cc2SEnji Cooper 
141528f6c2f2SEnji Cooper   Mutex(const Mutex&) = delete;
141628f6c2f2SEnji Cooper   Mutex& operator=(const Mutex&) = delete;
1417b89a7cc2SEnji Cooper };
1418b89a7cc2SEnji Cooper 
1419b89a7cc2SEnji Cooper #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1420b89a7cc2SEnji Cooper   extern ::testing::internal::Mutex mutex
1421b89a7cc2SEnji Cooper 
1422b89a7cc2SEnji Cooper #define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1423b89a7cc2SEnji Cooper   ::testing::internal::Mutex mutex(::testing::internal::Mutex::kStaticMutex)
1424b89a7cc2SEnji Cooper 
1425b89a7cc2SEnji Cooper // We cannot name this class MutexLock because the ctor declaration would
1426b89a7cc2SEnji Cooper // conflict with a macro named MutexLock, which is defined on some
1427b89a7cc2SEnji Cooper // platforms. That macro is used as a defensive measure to prevent against
1428b89a7cc2SEnji Cooper // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than
1429b89a7cc2SEnji Cooper // "MutexLock l(&mu)".  Hence the typedef trick below.
1430b89a7cc2SEnji Cooper class GTestMutexLock {
1431b89a7cc2SEnji Cooper  public:
143228f6c2f2SEnji Cooper   explicit GTestMutexLock(Mutex* mutex) : mutex_(mutex) { mutex_->Lock(); }
1433b89a7cc2SEnji Cooper 
1434b89a7cc2SEnji Cooper   ~GTestMutexLock() { mutex_->Unlock(); }
1435b89a7cc2SEnji Cooper 
1436b89a7cc2SEnji Cooper  private:
1437b89a7cc2SEnji Cooper   Mutex* const mutex_;
1438b89a7cc2SEnji Cooper 
143928f6c2f2SEnji Cooper   GTestMutexLock(const GTestMutexLock&) = delete;
144028f6c2f2SEnji Cooper   GTestMutexLock& operator=(const GTestMutexLock&) = delete;
1441b89a7cc2SEnji Cooper };
1442b89a7cc2SEnji Cooper 
1443b89a7cc2SEnji Cooper typedef GTestMutexLock MutexLock;
1444b89a7cc2SEnji Cooper 
1445b89a7cc2SEnji Cooper // Base class for ValueHolder<T>.  Allows a caller to hold and delete a value
1446b89a7cc2SEnji Cooper // without knowing its type.
1447b89a7cc2SEnji Cooper class ThreadLocalValueHolderBase {
1448b89a7cc2SEnji Cooper  public:
1449b89a7cc2SEnji Cooper   virtual ~ThreadLocalValueHolderBase() {}
1450b89a7cc2SEnji Cooper };
1451b89a7cc2SEnji Cooper 
1452b89a7cc2SEnji Cooper // Provides a way for a thread to send notifications to a ThreadLocal
1453b89a7cc2SEnji Cooper // regardless of its parameter type.
1454b89a7cc2SEnji Cooper class ThreadLocalBase {
1455b89a7cc2SEnji Cooper  public:
1456b89a7cc2SEnji Cooper   // Creates a new ValueHolder<T> object holding a default value passed to
1457b89a7cc2SEnji Cooper   // this ThreadLocal<T>'s constructor and returns it.  It is the caller's
1458b89a7cc2SEnji Cooper   // responsibility not to call this when the ThreadLocal<T> instance already
1459b89a7cc2SEnji Cooper   // has a value on the current thread.
1460b89a7cc2SEnji Cooper   virtual ThreadLocalValueHolderBase* NewValueForCurrentThread() const = 0;
1461b89a7cc2SEnji Cooper 
1462b89a7cc2SEnji Cooper  protected:
1463b89a7cc2SEnji Cooper   ThreadLocalBase() {}
1464b89a7cc2SEnji Cooper   virtual ~ThreadLocalBase() {}
1465b89a7cc2SEnji Cooper 
1466b89a7cc2SEnji Cooper  private:
146728f6c2f2SEnji Cooper   ThreadLocalBase(const ThreadLocalBase&) = delete;
146828f6c2f2SEnji Cooper   ThreadLocalBase& operator=(const ThreadLocalBase&) = delete;
1469b89a7cc2SEnji Cooper };
1470b89a7cc2SEnji Cooper 
1471b89a7cc2SEnji Cooper // Maps a thread to a set of ThreadLocals that have values instantiated on that
1472b89a7cc2SEnji Cooper // thread and notifies them when the thread exits.  A ThreadLocal instance is
1473b89a7cc2SEnji Cooper // expected to persist until all threads it has values on have terminated.
1474b89a7cc2SEnji Cooper class GTEST_API_ ThreadLocalRegistry {
1475b89a7cc2SEnji Cooper  public:
1476b89a7cc2SEnji Cooper   // Registers thread_local_instance as having value on the current thread.
1477b89a7cc2SEnji Cooper   // Returns a value that can be used to identify the thread from other threads.
1478b89a7cc2SEnji Cooper   static ThreadLocalValueHolderBase* GetValueOnCurrentThread(
1479b89a7cc2SEnji Cooper       const ThreadLocalBase* thread_local_instance);
1480b89a7cc2SEnji Cooper 
1481b89a7cc2SEnji Cooper   // Invoked when a ThreadLocal instance is destroyed.
1482b89a7cc2SEnji Cooper   static void OnThreadLocalDestroyed(
1483b89a7cc2SEnji Cooper       const ThreadLocalBase* thread_local_instance);
1484b89a7cc2SEnji Cooper };
1485b89a7cc2SEnji Cooper 
1486b89a7cc2SEnji Cooper class GTEST_API_ ThreadWithParamBase {
1487b89a7cc2SEnji Cooper  public:
1488b89a7cc2SEnji Cooper   void Join();
1489b89a7cc2SEnji Cooper 
1490b89a7cc2SEnji Cooper  protected:
1491b89a7cc2SEnji Cooper   class Runnable {
1492b89a7cc2SEnji Cooper    public:
1493b89a7cc2SEnji Cooper     virtual ~Runnable() {}
1494b89a7cc2SEnji Cooper     virtual void Run() = 0;
1495b89a7cc2SEnji Cooper   };
1496b89a7cc2SEnji Cooper 
1497b89a7cc2SEnji Cooper   ThreadWithParamBase(Runnable* runnable, Notification* thread_can_start);
1498b89a7cc2SEnji Cooper   virtual ~ThreadWithParamBase();
1499b89a7cc2SEnji Cooper 
1500b89a7cc2SEnji Cooper  private:
1501b89a7cc2SEnji Cooper   AutoHandle thread_;
1502b89a7cc2SEnji Cooper };
1503b89a7cc2SEnji Cooper 
1504b89a7cc2SEnji Cooper // Helper class for testing Google Test's multi-threading constructs.
1505b89a7cc2SEnji Cooper template <typename T>
1506b89a7cc2SEnji Cooper class ThreadWithParam : public ThreadWithParamBase {
1507b89a7cc2SEnji Cooper  public:
1508b89a7cc2SEnji Cooper   typedef void UserThreadFunc(T);
1509b89a7cc2SEnji Cooper 
1510b89a7cc2SEnji Cooper   ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start)
151128f6c2f2SEnji Cooper       : ThreadWithParamBase(new RunnableImpl(func, param), thread_can_start) {}
1512b89a7cc2SEnji Cooper   virtual ~ThreadWithParam() {}
1513b89a7cc2SEnji Cooper 
1514b89a7cc2SEnji Cooper  private:
1515b89a7cc2SEnji Cooper   class RunnableImpl : public Runnable {
1516b89a7cc2SEnji Cooper    public:
151728f6c2f2SEnji Cooper     RunnableImpl(UserThreadFunc* func, T param) : func_(func), param_(param) {}
1518b89a7cc2SEnji Cooper     virtual ~RunnableImpl() {}
151928f6c2f2SEnji Cooper     virtual void Run() { func_(param_); }
1520b89a7cc2SEnji Cooper 
1521b89a7cc2SEnji Cooper    private:
1522b89a7cc2SEnji Cooper     UserThreadFunc* const func_;
1523b89a7cc2SEnji Cooper     const T param_;
1524b89a7cc2SEnji Cooper 
152528f6c2f2SEnji Cooper     RunnableImpl(const RunnableImpl&) = delete;
152628f6c2f2SEnji Cooper     RunnableImpl& operator=(const RunnableImpl&) = delete;
1527b89a7cc2SEnji Cooper   };
1528b89a7cc2SEnji Cooper 
152928f6c2f2SEnji Cooper   ThreadWithParam(const ThreadWithParam&) = delete;
153028f6c2f2SEnji Cooper   ThreadWithParam& operator=(const ThreadWithParam&) = delete;
1531b89a7cc2SEnji Cooper };
1532b89a7cc2SEnji Cooper 
1533b89a7cc2SEnji Cooper // Implements thread-local storage on Windows systems.
1534b89a7cc2SEnji Cooper //
1535b89a7cc2SEnji Cooper //   // Thread 1
1536b89a7cc2SEnji Cooper //   ThreadLocal<int> tl(100);  // 100 is the default value for each thread.
1537b89a7cc2SEnji Cooper //
1538b89a7cc2SEnji Cooper //   // Thread 2
1539b89a7cc2SEnji Cooper //   tl.set(150);  // Changes the value for thread 2 only.
1540b89a7cc2SEnji Cooper //   EXPECT_EQ(150, tl.get());
1541b89a7cc2SEnji Cooper //
1542b89a7cc2SEnji Cooper //   // Thread 1
1543b89a7cc2SEnji Cooper //   EXPECT_EQ(100, tl.get());  // In thread 1, tl has the original value.
1544b89a7cc2SEnji Cooper //   tl.set(200);
1545b89a7cc2SEnji Cooper //   EXPECT_EQ(200, tl.get());
1546b89a7cc2SEnji Cooper //
1547b89a7cc2SEnji Cooper // The template type argument T must have a public copy constructor.
1548b89a7cc2SEnji Cooper // In addition, the default ThreadLocal constructor requires T to have
1549b89a7cc2SEnji Cooper // a public default constructor.
1550b89a7cc2SEnji Cooper //
1551b89a7cc2SEnji Cooper // The users of a TheadLocal instance have to make sure that all but one
1552b89a7cc2SEnji Cooper // threads (including the main one) using that instance have exited before
1553b89a7cc2SEnji Cooper // destroying it. Otherwise, the per-thread objects managed for them by the
1554b89a7cc2SEnji Cooper // ThreadLocal instance are not guaranteed to be destroyed on all platforms.
1555b89a7cc2SEnji Cooper //
1556b89a7cc2SEnji Cooper // Google Test only uses global ThreadLocal objects.  That means they
1557b89a7cc2SEnji Cooper // will die after main() has returned.  Therefore, no per-thread
1558b89a7cc2SEnji Cooper // object managed by Google Test will be leaked as long as all threads
1559b89a7cc2SEnji Cooper // using Google Test have exited when main() returns.
1560b89a7cc2SEnji Cooper template <typename T>
1561b89a7cc2SEnji Cooper class ThreadLocal : public ThreadLocalBase {
1562b89a7cc2SEnji Cooper  public:
1563b89a7cc2SEnji Cooper   ThreadLocal() : default_factory_(new DefaultValueHolderFactory()) {}
1564b89a7cc2SEnji Cooper   explicit ThreadLocal(const T& value)
1565b89a7cc2SEnji Cooper       : default_factory_(new InstanceValueHolderFactory(value)) {}
1566b89a7cc2SEnji Cooper 
156728f6c2f2SEnji Cooper   ~ThreadLocal() override { ThreadLocalRegistry::OnThreadLocalDestroyed(this); }
1568b89a7cc2SEnji Cooper 
1569b89a7cc2SEnji Cooper   T* pointer() { return GetOrCreateValue(); }
1570b89a7cc2SEnji Cooper   const T* pointer() const { return GetOrCreateValue(); }
1571b89a7cc2SEnji Cooper   const T& get() const { return *pointer(); }
1572b89a7cc2SEnji Cooper   void set(const T& value) { *pointer() = value; }
1573b89a7cc2SEnji Cooper 
1574b89a7cc2SEnji Cooper  private:
1575b89a7cc2SEnji Cooper   // Holds a value of T.  Can be deleted via its base class without the caller
1576b89a7cc2SEnji Cooper   // knowing the type of T.
1577b89a7cc2SEnji Cooper   class ValueHolder : public ThreadLocalValueHolderBase {
1578b89a7cc2SEnji Cooper    public:
1579b89a7cc2SEnji Cooper     ValueHolder() : value_() {}
1580b89a7cc2SEnji Cooper     explicit ValueHolder(const T& value) : value_(value) {}
1581b89a7cc2SEnji Cooper 
1582b89a7cc2SEnji Cooper     T* pointer() { return &value_; }
1583b89a7cc2SEnji Cooper 
1584b89a7cc2SEnji Cooper    private:
1585b89a7cc2SEnji Cooper     T value_;
158628f6c2f2SEnji Cooper     ValueHolder(const ValueHolder&) = delete;
158728f6c2f2SEnji Cooper     ValueHolder& operator=(const ValueHolder&) = delete;
1588b89a7cc2SEnji Cooper   };
1589b89a7cc2SEnji Cooper 
1590b89a7cc2SEnji Cooper   T* GetOrCreateValue() const {
1591b89a7cc2SEnji Cooper     return static_cast<ValueHolder*>(
159228f6c2f2SEnji Cooper                ThreadLocalRegistry::GetValueOnCurrentThread(this))
159328f6c2f2SEnji Cooper         ->pointer();
1594b89a7cc2SEnji Cooper   }
1595b89a7cc2SEnji Cooper 
159628f6c2f2SEnji Cooper   ThreadLocalValueHolderBase* NewValueForCurrentThread() const override {
1597b89a7cc2SEnji Cooper     return default_factory_->MakeNewHolder();
1598b89a7cc2SEnji Cooper   }
1599b89a7cc2SEnji Cooper 
1600b89a7cc2SEnji Cooper   class ValueHolderFactory {
1601b89a7cc2SEnji Cooper    public:
1602b89a7cc2SEnji Cooper     ValueHolderFactory() {}
1603b89a7cc2SEnji Cooper     virtual ~ValueHolderFactory() {}
1604b89a7cc2SEnji Cooper     virtual ValueHolder* MakeNewHolder() const = 0;
1605b89a7cc2SEnji Cooper 
1606b89a7cc2SEnji Cooper    private:
160728f6c2f2SEnji Cooper     ValueHolderFactory(const ValueHolderFactory&) = delete;
160828f6c2f2SEnji Cooper     ValueHolderFactory& operator=(const ValueHolderFactory&) = delete;
1609b89a7cc2SEnji Cooper   };
1610b89a7cc2SEnji Cooper 
1611b89a7cc2SEnji Cooper   class DefaultValueHolderFactory : public ValueHolderFactory {
1612b89a7cc2SEnji Cooper    public:
1613b89a7cc2SEnji Cooper     DefaultValueHolderFactory() {}
161428f6c2f2SEnji Cooper     ValueHolder* MakeNewHolder() const override { return new ValueHolder(); }
1615b89a7cc2SEnji Cooper 
1616b89a7cc2SEnji Cooper    private:
161728f6c2f2SEnji Cooper     DefaultValueHolderFactory(const DefaultValueHolderFactory&) = delete;
161828f6c2f2SEnji Cooper     DefaultValueHolderFactory& operator=(const DefaultValueHolderFactory&) =
161928f6c2f2SEnji Cooper         delete;
1620b89a7cc2SEnji Cooper   };
1621b89a7cc2SEnji Cooper 
1622b89a7cc2SEnji Cooper   class InstanceValueHolderFactory : public ValueHolderFactory {
1623b89a7cc2SEnji Cooper    public:
1624b89a7cc2SEnji Cooper     explicit InstanceValueHolderFactory(const T& value) : value_(value) {}
162528f6c2f2SEnji Cooper     ValueHolder* MakeNewHolder() const override {
1626b89a7cc2SEnji Cooper       return new ValueHolder(value_);
1627b89a7cc2SEnji Cooper     }
1628b89a7cc2SEnji Cooper 
1629b89a7cc2SEnji Cooper    private:
1630b89a7cc2SEnji Cooper     const T value_;  // The value for each thread.
1631b89a7cc2SEnji Cooper 
163228f6c2f2SEnji Cooper     InstanceValueHolderFactory(const InstanceValueHolderFactory&) = delete;
163328f6c2f2SEnji Cooper     InstanceValueHolderFactory& operator=(const InstanceValueHolderFactory&) =
163428f6c2f2SEnji Cooper         delete;
1635b89a7cc2SEnji Cooper   };
1636b89a7cc2SEnji Cooper 
163728f6c2f2SEnji Cooper   std::unique_ptr<ValueHolderFactory> default_factory_;
1638b89a7cc2SEnji Cooper 
163928f6c2f2SEnji Cooper   ThreadLocal(const ThreadLocal&) = delete;
164028f6c2f2SEnji Cooper   ThreadLocal& operator=(const ThreadLocal&) = delete;
1641b89a7cc2SEnji Cooper };
1642b89a7cc2SEnji Cooper 
1643b89a7cc2SEnji Cooper #elif GTEST_HAS_PTHREAD
1644b89a7cc2SEnji Cooper 
1645b89a7cc2SEnji Cooper // MutexBase and Mutex implement mutex on pthreads-based platforms.
1646b89a7cc2SEnji Cooper class MutexBase {
1647b89a7cc2SEnji Cooper  public:
1648b89a7cc2SEnji Cooper   // Acquires this mutex.
1649b89a7cc2SEnji Cooper   void Lock() {
1650b89a7cc2SEnji Cooper     GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_));
1651b89a7cc2SEnji Cooper     owner_ = pthread_self();
1652b89a7cc2SEnji Cooper     has_owner_ = true;
1653b89a7cc2SEnji Cooper   }
1654b89a7cc2SEnji Cooper 
1655b89a7cc2SEnji Cooper   // Releases this mutex.
1656b89a7cc2SEnji Cooper   void Unlock() {
1657b89a7cc2SEnji Cooper     // Since the lock is being released the owner_ field should no longer be
1658b89a7cc2SEnji Cooper     // considered valid. We don't protect writing to has_owner_ here, as it's
1659b89a7cc2SEnji Cooper     // the caller's responsibility to ensure that the current thread holds the
1660b89a7cc2SEnji Cooper     // mutex when this is called.
1661b89a7cc2SEnji Cooper     has_owner_ = false;
1662b89a7cc2SEnji Cooper     GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_));
1663b89a7cc2SEnji Cooper   }
1664b89a7cc2SEnji Cooper 
1665b89a7cc2SEnji Cooper   // Does nothing if the current thread holds the mutex. Otherwise, crashes
1666b89a7cc2SEnji Cooper   // with high probability.
1667b89a7cc2SEnji Cooper   void AssertHeld() const {
1668b89a7cc2SEnji Cooper     GTEST_CHECK_(has_owner_ && pthread_equal(owner_, pthread_self()))
1669b89a7cc2SEnji Cooper         << "The current thread is not holding the mutex @" << this;
1670b89a7cc2SEnji Cooper   }
1671b89a7cc2SEnji Cooper 
1672b89a7cc2SEnji Cooper   // A static mutex may be used before main() is entered.  It may even
1673b89a7cc2SEnji Cooper   // be used before the dynamic initialization stage.  Therefore we
1674b89a7cc2SEnji Cooper   // must be able to initialize a static mutex object at link time.
1675b89a7cc2SEnji Cooper   // This means MutexBase has to be a POD and its member variables
1676b89a7cc2SEnji Cooper   // have to be public.
1677b89a7cc2SEnji Cooper  public:
1678b89a7cc2SEnji Cooper   pthread_mutex_t mutex_;  // The underlying pthread mutex.
1679b89a7cc2SEnji Cooper   // has_owner_ indicates whether the owner_ field below contains a valid thread
1680b89a7cc2SEnji Cooper   // ID and is therefore safe to inspect (e.g., to use in pthread_equal()). All
1681b89a7cc2SEnji Cooper   // accesses to the owner_ field should be protected by a check of this field.
1682b89a7cc2SEnji Cooper   // An alternative might be to memset() owner_ to all zeros, but there's no
1683b89a7cc2SEnji Cooper   // guarantee that a zero'd pthread_t is necessarily invalid or even different
1684b89a7cc2SEnji Cooper   // from pthread_self().
1685b89a7cc2SEnji Cooper   bool has_owner_;
1686b89a7cc2SEnji Cooper   pthread_t owner_;  // The thread holding the mutex.
1687b89a7cc2SEnji Cooper };
1688b89a7cc2SEnji Cooper 
1689b89a7cc2SEnji Cooper // Forward-declares a static mutex.
1690b89a7cc2SEnji Cooper #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1691b89a7cc2SEnji Cooper   extern ::testing::internal::MutexBase mutex
1692b89a7cc2SEnji Cooper 
1693b89a7cc2SEnji Cooper // Defines and statically (i.e. at link time) initializes a static mutex.
1694b89a7cc2SEnji Cooper // The initialization list here does not explicitly initialize each field,
1695b89a7cc2SEnji Cooper // instead relying on default initialization for the unspecified fields. In
1696b89a7cc2SEnji Cooper // particular, the owner_ field (a pthread_t) is not explicitly initialized.
1697b89a7cc2SEnji Cooper // This allows initialization to work whether pthread_t is a scalar or struct.
1698b89a7cc2SEnji Cooper // The flag -Wmissing-field-initializers must not be specified for this to work.
1699b89a7cc2SEnji Cooper #define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1700b89a7cc2SEnji Cooper   ::testing::internal::MutexBase mutex = {PTHREAD_MUTEX_INITIALIZER, false, 0}
1701b89a7cc2SEnji Cooper 
1702b89a7cc2SEnji Cooper // The Mutex class can only be used for mutexes created at runtime. It
1703b89a7cc2SEnji Cooper // shares its API with MutexBase otherwise.
1704b89a7cc2SEnji Cooper class Mutex : public MutexBase {
1705b89a7cc2SEnji Cooper  public:
1706b89a7cc2SEnji Cooper   Mutex() {
170728f6c2f2SEnji Cooper     GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, nullptr));
1708b89a7cc2SEnji Cooper     has_owner_ = false;
1709b89a7cc2SEnji Cooper   }
171028f6c2f2SEnji Cooper   ~Mutex() { GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_)); }
1711b89a7cc2SEnji Cooper 
1712b89a7cc2SEnji Cooper  private:
171328f6c2f2SEnji Cooper   Mutex(const Mutex&) = delete;
171428f6c2f2SEnji Cooper   Mutex& operator=(const Mutex&) = delete;
1715b89a7cc2SEnji Cooper };
1716b89a7cc2SEnji Cooper 
1717b89a7cc2SEnji Cooper // We cannot name this class MutexLock because the ctor declaration would
1718b89a7cc2SEnji Cooper // conflict with a macro named MutexLock, which is defined on some
1719b89a7cc2SEnji Cooper // platforms. That macro is used as a defensive measure to prevent against
1720b89a7cc2SEnji Cooper // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than
1721b89a7cc2SEnji Cooper // "MutexLock l(&mu)".  Hence the typedef trick below.
1722b89a7cc2SEnji Cooper class GTestMutexLock {
1723b89a7cc2SEnji Cooper  public:
172428f6c2f2SEnji Cooper   explicit GTestMutexLock(MutexBase* mutex) : mutex_(mutex) { mutex_->Lock(); }
1725b89a7cc2SEnji Cooper 
1726b89a7cc2SEnji Cooper   ~GTestMutexLock() { mutex_->Unlock(); }
1727b89a7cc2SEnji Cooper 
1728b89a7cc2SEnji Cooper  private:
1729b89a7cc2SEnji Cooper   MutexBase* const mutex_;
1730b89a7cc2SEnji Cooper 
173128f6c2f2SEnji Cooper   GTestMutexLock(const GTestMutexLock&) = delete;
173228f6c2f2SEnji Cooper   GTestMutexLock& operator=(const GTestMutexLock&) = delete;
1733b89a7cc2SEnji Cooper };
1734b89a7cc2SEnji Cooper 
1735b89a7cc2SEnji Cooper typedef GTestMutexLock MutexLock;
1736b89a7cc2SEnji Cooper 
1737b89a7cc2SEnji Cooper // Helpers for ThreadLocal.
1738b89a7cc2SEnji Cooper 
1739b89a7cc2SEnji Cooper // pthread_key_create() requires DeleteThreadLocalValue() to have
1740b89a7cc2SEnji Cooper // C-linkage.  Therefore it cannot be templatized to access
1741b89a7cc2SEnji Cooper // ThreadLocal<T>.  Hence the need for class
1742b89a7cc2SEnji Cooper // ThreadLocalValueHolderBase.
174328f6c2f2SEnji Cooper class GTEST_API_ ThreadLocalValueHolderBase {
1744b89a7cc2SEnji Cooper  public:
174528f6c2f2SEnji Cooper   virtual ~ThreadLocalValueHolderBase() = default;
1746b89a7cc2SEnji Cooper };
1747b89a7cc2SEnji Cooper 
1748b89a7cc2SEnji Cooper // Called by pthread to delete thread-local data stored by
1749b89a7cc2SEnji Cooper // pthread_setspecific().
1750b89a7cc2SEnji Cooper extern "C" inline void DeleteThreadLocalValue(void* value_holder) {
1751b89a7cc2SEnji Cooper   delete static_cast<ThreadLocalValueHolderBase*>(value_holder);
1752b89a7cc2SEnji Cooper }
1753b89a7cc2SEnji Cooper 
1754b89a7cc2SEnji Cooper // Implements thread-local storage on pthreads-based systems.
1755b89a7cc2SEnji Cooper template <typename T>
1756b89a7cc2SEnji Cooper class GTEST_API_ ThreadLocal {
1757b89a7cc2SEnji Cooper  public:
1758b89a7cc2SEnji Cooper   ThreadLocal()
1759b89a7cc2SEnji Cooper       : key_(CreateKey()), default_factory_(new DefaultValueHolderFactory()) {}
1760b89a7cc2SEnji Cooper   explicit ThreadLocal(const T& value)
1761b89a7cc2SEnji Cooper       : key_(CreateKey()),
1762b89a7cc2SEnji Cooper         default_factory_(new InstanceValueHolderFactory(value)) {}
1763b89a7cc2SEnji Cooper 
1764b89a7cc2SEnji Cooper   ~ThreadLocal() {
1765b89a7cc2SEnji Cooper     // Destroys the managed object for the current thread, if any.
1766b89a7cc2SEnji Cooper     DeleteThreadLocalValue(pthread_getspecific(key_));
1767b89a7cc2SEnji Cooper 
1768b89a7cc2SEnji Cooper     // Releases resources associated with the key.  This will *not*
1769b89a7cc2SEnji Cooper     // delete managed objects for other threads.
1770b89a7cc2SEnji Cooper     GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_));
1771b89a7cc2SEnji Cooper   }
1772b89a7cc2SEnji Cooper 
1773b89a7cc2SEnji Cooper   T* pointer() { return GetOrCreateValue(); }
1774b89a7cc2SEnji Cooper   const T* pointer() const { return GetOrCreateValue(); }
1775b89a7cc2SEnji Cooper   const T& get() const { return *pointer(); }
1776b89a7cc2SEnji Cooper   void set(const T& value) { *pointer() = value; }
1777b89a7cc2SEnji Cooper 
1778b89a7cc2SEnji Cooper  private:
1779b89a7cc2SEnji Cooper   // Holds a value of type T.
1780b89a7cc2SEnji Cooper   class ValueHolder : public ThreadLocalValueHolderBase {
1781b89a7cc2SEnji Cooper    public:
1782b89a7cc2SEnji Cooper     ValueHolder() : value_() {}
1783b89a7cc2SEnji Cooper     explicit ValueHolder(const T& value) : value_(value) {}
1784b89a7cc2SEnji Cooper 
1785b89a7cc2SEnji Cooper     T* pointer() { return &value_; }
1786b89a7cc2SEnji Cooper 
1787b89a7cc2SEnji Cooper    private:
1788b89a7cc2SEnji Cooper     T value_;
178928f6c2f2SEnji Cooper     ValueHolder(const ValueHolder&) = delete;
179028f6c2f2SEnji Cooper     ValueHolder& operator=(const ValueHolder&) = delete;
1791b89a7cc2SEnji Cooper   };
1792b89a7cc2SEnji Cooper 
1793b89a7cc2SEnji Cooper   static pthread_key_t CreateKey() {
1794b89a7cc2SEnji Cooper     pthread_key_t key;
1795b89a7cc2SEnji Cooper     // When a thread exits, DeleteThreadLocalValue() will be called on
1796b89a7cc2SEnji Cooper     // the object managed for that thread.
1797b89a7cc2SEnji Cooper     GTEST_CHECK_POSIX_SUCCESS_(
1798b89a7cc2SEnji Cooper         pthread_key_create(&key, &DeleteThreadLocalValue));
1799b89a7cc2SEnji Cooper     return key;
1800b89a7cc2SEnji Cooper   }
1801b89a7cc2SEnji Cooper 
1802b89a7cc2SEnji Cooper   T* GetOrCreateValue() const {
1803b89a7cc2SEnji Cooper     ThreadLocalValueHolderBase* const holder =
1804b89a7cc2SEnji Cooper         static_cast<ThreadLocalValueHolderBase*>(pthread_getspecific(key_));
180528f6c2f2SEnji Cooper     if (holder != nullptr) {
1806b89a7cc2SEnji Cooper       return CheckedDowncastToActualType<ValueHolder>(holder)->pointer();
1807b89a7cc2SEnji Cooper     }
1808b89a7cc2SEnji Cooper 
1809b89a7cc2SEnji Cooper     ValueHolder* const new_holder = default_factory_->MakeNewHolder();
1810b89a7cc2SEnji Cooper     ThreadLocalValueHolderBase* const holder_base = new_holder;
1811b89a7cc2SEnji Cooper     GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base));
1812b89a7cc2SEnji Cooper     return new_holder->pointer();
1813b89a7cc2SEnji Cooper   }
1814b89a7cc2SEnji Cooper 
1815b89a7cc2SEnji Cooper   class ValueHolderFactory {
1816b89a7cc2SEnji Cooper    public:
181728f6c2f2SEnji Cooper     ValueHolderFactory() = default;
181828f6c2f2SEnji Cooper     virtual ~ValueHolderFactory() = default;
1819b89a7cc2SEnji Cooper     virtual ValueHolder* MakeNewHolder() const = 0;
1820b89a7cc2SEnji Cooper 
1821b89a7cc2SEnji Cooper    private:
182228f6c2f2SEnji Cooper     ValueHolderFactory(const ValueHolderFactory&) = delete;
182328f6c2f2SEnji Cooper     ValueHolderFactory& operator=(const ValueHolderFactory&) = delete;
1824b89a7cc2SEnji Cooper   };
1825b89a7cc2SEnji Cooper 
1826b89a7cc2SEnji Cooper   class DefaultValueHolderFactory : public ValueHolderFactory {
1827b89a7cc2SEnji Cooper    public:
182828f6c2f2SEnji Cooper     DefaultValueHolderFactory() = default;
182928f6c2f2SEnji Cooper     ValueHolder* MakeNewHolder() const override { return new ValueHolder(); }
1830b89a7cc2SEnji Cooper 
1831b89a7cc2SEnji Cooper    private:
183228f6c2f2SEnji Cooper     DefaultValueHolderFactory(const DefaultValueHolderFactory&) = delete;
183328f6c2f2SEnji Cooper     DefaultValueHolderFactory& operator=(const DefaultValueHolderFactory&) =
183428f6c2f2SEnji Cooper         delete;
1835b89a7cc2SEnji Cooper   };
1836b89a7cc2SEnji Cooper 
1837b89a7cc2SEnji Cooper   class InstanceValueHolderFactory : public ValueHolderFactory {
1838b89a7cc2SEnji Cooper    public:
1839b89a7cc2SEnji Cooper     explicit InstanceValueHolderFactory(const T& value) : value_(value) {}
184028f6c2f2SEnji Cooper     ValueHolder* MakeNewHolder() const override {
1841b89a7cc2SEnji Cooper       return new ValueHolder(value_);
1842b89a7cc2SEnji Cooper     }
1843b89a7cc2SEnji Cooper 
1844b89a7cc2SEnji Cooper    private:
1845b89a7cc2SEnji Cooper     const T value_;  // The value for each thread.
1846b89a7cc2SEnji Cooper 
184728f6c2f2SEnji Cooper     InstanceValueHolderFactory(const InstanceValueHolderFactory&) = delete;
184828f6c2f2SEnji Cooper     InstanceValueHolderFactory& operator=(const InstanceValueHolderFactory&) =
184928f6c2f2SEnji Cooper         delete;
1850b89a7cc2SEnji Cooper   };
1851b89a7cc2SEnji Cooper 
1852b89a7cc2SEnji Cooper   // A key pthreads uses for looking up per-thread values.
1853b89a7cc2SEnji Cooper   const pthread_key_t key_;
185428f6c2f2SEnji Cooper   std::unique_ptr<ValueHolderFactory> default_factory_;
1855b89a7cc2SEnji Cooper 
185628f6c2f2SEnji Cooper   ThreadLocal(const ThreadLocal&) = delete;
185728f6c2f2SEnji Cooper   ThreadLocal& operator=(const ThreadLocal&) = delete;
1858b89a7cc2SEnji Cooper };
1859b89a7cc2SEnji Cooper 
1860b89a7cc2SEnji Cooper #endif  // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
1861b89a7cc2SEnji Cooper 
1862b89a7cc2SEnji Cooper #else  // GTEST_IS_THREADSAFE
1863b89a7cc2SEnji Cooper 
1864b89a7cc2SEnji Cooper // A dummy implementation of synchronization primitives (mutex, lock,
1865b89a7cc2SEnji Cooper // and thread-local variable).  Necessary for compiling Google Test where
1866b89a7cc2SEnji Cooper // mutex is not supported - using Google Test in multiple threads is not
1867b89a7cc2SEnji Cooper // supported on such platforms.
1868b89a7cc2SEnji Cooper 
1869b89a7cc2SEnji Cooper class Mutex {
1870b89a7cc2SEnji Cooper  public:
1871b89a7cc2SEnji Cooper   Mutex() {}
1872b89a7cc2SEnji Cooper   void Lock() {}
1873b89a7cc2SEnji Cooper   void Unlock() {}
1874b89a7cc2SEnji Cooper   void AssertHeld() const {}
1875b89a7cc2SEnji Cooper };
1876b89a7cc2SEnji Cooper 
1877b89a7cc2SEnji Cooper #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1878b89a7cc2SEnji Cooper   extern ::testing::internal::Mutex mutex
1879b89a7cc2SEnji Cooper 
1880b89a7cc2SEnji Cooper #define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex
1881b89a7cc2SEnji Cooper 
1882b89a7cc2SEnji Cooper // We cannot name this class MutexLock because the ctor declaration would
1883b89a7cc2SEnji Cooper // conflict with a macro named MutexLock, which is defined on some
1884b89a7cc2SEnji Cooper // platforms. That macro is used as a defensive measure to prevent against
1885b89a7cc2SEnji Cooper // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than
1886b89a7cc2SEnji Cooper // "MutexLock l(&mu)".  Hence the typedef trick below.
1887b89a7cc2SEnji Cooper class GTestMutexLock {
1888b89a7cc2SEnji Cooper  public:
1889b89a7cc2SEnji Cooper   explicit GTestMutexLock(Mutex*) {}  // NOLINT
1890b89a7cc2SEnji Cooper };
1891b89a7cc2SEnji Cooper 
1892b89a7cc2SEnji Cooper typedef GTestMutexLock MutexLock;
1893b89a7cc2SEnji Cooper 
1894b89a7cc2SEnji Cooper template <typename T>
1895b89a7cc2SEnji Cooper class GTEST_API_ ThreadLocal {
1896b89a7cc2SEnji Cooper  public:
1897b89a7cc2SEnji Cooper   ThreadLocal() : value_() {}
1898b89a7cc2SEnji Cooper   explicit ThreadLocal(const T& value) : value_(value) {}
1899b89a7cc2SEnji Cooper   T* pointer() { return &value_; }
1900b89a7cc2SEnji Cooper   const T* pointer() const { return &value_; }
1901b89a7cc2SEnji Cooper   const T& get() const { return value_; }
1902b89a7cc2SEnji Cooper   void set(const T& value) { value_ = value; }
190328f6c2f2SEnji Cooper 
1904b89a7cc2SEnji Cooper  private:
1905b89a7cc2SEnji Cooper   T value_;
1906b89a7cc2SEnji Cooper };
1907b89a7cc2SEnji Cooper 
1908b89a7cc2SEnji Cooper #endif  // GTEST_IS_THREADSAFE
1909b89a7cc2SEnji Cooper 
1910b89a7cc2SEnji Cooper // Returns the number of threads running in the process, or 0 to indicate that
1911b89a7cc2SEnji Cooper // we cannot detect it.
1912b89a7cc2SEnji Cooper GTEST_API_ size_t GetThreadCount();
1913b89a7cc2SEnji Cooper 
191428f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
1915b89a7cc2SEnji Cooper #define GTEST_PATH_SEP_ "\\"
1916b89a7cc2SEnji Cooper #define GTEST_HAS_ALT_PATH_SEP_ 1
1917b89a7cc2SEnji Cooper #else
1918b89a7cc2SEnji Cooper #define GTEST_PATH_SEP_ "/"
1919b89a7cc2SEnji Cooper #define GTEST_HAS_ALT_PATH_SEP_ 0
1920b89a7cc2SEnji Cooper #endif  // GTEST_OS_WINDOWS
1921b89a7cc2SEnji Cooper 
1922b89a7cc2SEnji Cooper // Utilities for char.
1923b89a7cc2SEnji Cooper 
1924b89a7cc2SEnji Cooper // isspace(int ch) and friends accept an unsigned char or EOF.  char
1925b89a7cc2SEnji Cooper // may be signed, depending on the compiler (or compiler flags).
1926b89a7cc2SEnji Cooper // Therefore we need to cast a char to unsigned char before calling
1927b89a7cc2SEnji Cooper // isspace(), etc.
1928b89a7cc2SEnji Cooper 
1929b89a7cc2SEnji Cooper inline bool IsAlpha(char ch) {
1930b89a7cc2SEnji Cooper   return isalpha(static_cast<unsigned char>(ch)) != 0;
1931b89a7cc2SEnji Cooper }
1932b89a7cc2SEnji Cooper inline bool IsAlNum(char ch) {
1933b89a7cc2SEnji Cooper   return isalnum(static_cast<unsigned char>(ch)) != 0;
1934b89a7cc2SEnji Cooper }
1935b89a7cc2SEnji Cooper inline bool IsDigit(char ch) {
1936b89a7cc2SEnji Cooper   return isdigit(static_cast<unsigned char>(ch)) != 0;
1937b89a7cc2SEnji Cooper }
1938b89a7cc2SEnji Cooper inline bool IsLower(char ch) {
1939b89a7cc2SEnji Cooper   return islower(static_cast<unsigned char>(ch)) != 0;
1940b89a7cc2SEnji Cooper }
1941b89a7cc2SEnji Cooper inline bool IsSpace(char ch) {
1942b89a7cc2SEnji Cooper   return isspace(static_cast<unsigned char>(ch)) != 0;
1943b89a7cc2SEnji Cooper }
1944b89a7cc2SEnji Cooper inline bool IsUpper(char ch) {
1945b89a7cc2SEnji Cooper   return isupper(static_cast<unsigned char>(ch)) != 0;
1946b89a7cc2SEnji Cooper }
1947b89a7cc2SEnji Cooper inline bool IsXDigit(char ch) {
1948b89a7cc2SEnji Cooper   return isxdigit(static_cast<unsigned char>(ch)) != 0;
1949b89a7cc2SEnji Cooper }
195028f6c2f2SEnji Cooper #ifdef __cpp_lib_char8_t
195128f6c2f2SEnji Cooper inline bool IsXDigit(char8_t ch) {
195228f6c2f2SEnji Cooper   return isxdigit(static_cast<unsigned char>(ch)) != 0;
195328f6c2f2SEnji Cooper }
195428f6c2f2SEnji Cooper #endif
195528f6c2f2SEnji Cooper inline bool IsXDigit(char16_t ch) {
195628f6c2f2SEnji Cooper   const unsigned char low_byte = static_cast<unsigned char>(ch);
195728f6c2f2SEnji Cooper   return ch == low_byte && isxdigit(low_byte) != 0;
195828f6c2f2SEnji Cooper }
195928f6c2f2SEnji Cooper inline bool IsXDigit(char32_t ch) {
196028f6c2f2SEnji Cooper   const unsigned char low_byte = static_cast<unsigned char>(ch);
196128f6c2f2SEnji Cooper   return ch == low_byte && isxdigit(low_byte) != 0;
196228f6c2f2SEnji Cooper }
1963b89a7cc2SEnji Cooper inline bool IsXDigit(wchar_t ch) {
1964b89a7cc2SEnji Cooper   const unsigned char low_byte = static_cast<unsigned char>(ch);
1965b89a7cc2SEnji Cooper   return ch == low_byte && isxdigit(low_byte) != 0;
1966b89a7cc2SEnji Cooper }
1967b89a7cc2SEnji Cooper 
1968b89a7cc2SEnji Cooper inline char ToLower(char ch) {
1969b89a7cc2SEnji Cooper   return static_cast<char>(tolower(static_cast<unsigned char>(ch)));
1970b89a7cc2SEnji Cooper }
1971b89a7cc2SEnji Cooper inline char ToUpper(char ch) {
1972b89a7cc2SEnji Cooper   return static_cast<char>(toupper(static_cast<unsigned char>(ch)));
1973b89a7cc2SEnji Cooper }
1974b89a7cc2SEnji Cooper 
1975b89a7cc2SEnji Cooper inline std::string StripTrailingSpaces(std::string str) {
1976b89a7cc2SEnji Cooper   std::string::iterator it = str.end();
197728f6c2f2SEnji Cooper   while (it != str.begin() && IsSpace(*--it)) it = str.erase(it);
1978b89a7cc2SEnji Cooper   return str;
1979b89a7cc2SEnji Cooper }
1980b89a7cc2SEnji Cooper 
1981b89a7cc2SEnji Cooper // The testing::internal::posix namespace holds wrappers for common
1982b89a7cc2SEnji Cooper // POSIX functions.  These wrappers hide the differences between
1983b89a7cc2SEnji Cooper // Windows/MSVC and POSIX systems.  Since some compilers define these
1984b89a7cc2SEnji Cooper // standard functions as macros, the wrapper cannot have the same name
1985b89a7cc2SEnji Cooper // as the wrapped function.
1986b89a7cc2SEnji Cooper 
1987b89a7cc2SEnji Cooper namespace posix {
1988b89a7cc2SEnji Cooper 
198928f6c2f2SEnji Cooper // File system porting.
199028f6c2f2SEnji Cooper #if GTEST_HAS_FILE_SYSTEM
199128f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
1992b89a7cc2SEnji Cooper 
1993b89a7cc2SEnji Cooper typedef struct _stat StatStruct;
1994b89a7cc2SEnji Cooper 
199528f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS_MOBILE
1996b89a7cc2SEnji Cooper inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
1997b89a7cc2SEnji Cooper // Stat(), RmDir(), and IsDir() are not needed on Windows CE at this
1998b89a7cc2SEnji Cooper // time and thus not defined there.
1999b89a7cc2SEnji Cooper #else
2000b89a7cc2SEnji Cooper inline int FileNo(FILE* file) { return _fileno(file); }
2001b89a7cc2SEnji Cooper inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
2002b89a7cc2SEnji Cooper inline int RmDir(const char* dir) { return _rmdir(dir); }
200328f6c2f2SEnji Cooper inline bool IsDir(const StatStruct& st) { return (_S_IFDIR & st.st_mode) != 0; }
2004b89a7cc2SEnji Cooper #endif  // GTEST_OS_WINDOWS_MOBILE
2005b89a7cc2SEnji Cooper 
200628f6c2f2SEnji Cooper #elif defined(GTEST_OS_ESP8266)
200728f6c2f2SEnji Cooper typedef struct stat StatStruct;
200828f6c2f2SEnji Cooper 
200928f6c2f2SEnji Cooper inline int FileNo(FILE* file) { return fileno(file); }
201028f6c2f2SEnji Cooper inline int Stat(const char* path, StatStruct* buf) {
201128f6c2f2SEnji Cooper   // stat function not implemented on ESP8266
201228f6c2f2SEnji Cooper   return 0;
201328f6c2f2SEnji Cooper }
201428f6c2f2SEnji Cooper inline int RmDir(const char* dir) { return rmdir(dir); }
201528f6c2f2SEnji Cooper inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
201628f6c2f2SEnji Cooper 
2017b89a7cc2SEnji Cooper #else
2018b89a7cc2SEnji Cooper 
2019b89a7cc2SEnji Cooper typedef struct stat StatStruct;
2020b89a7cc2SEnji Cooper 
2021b89a7cc2SEnji Cooper inline int FileNo(FILE* file) { return fileno(file); }
2022b89a7cc2SEnji Cooper inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
202328f6c2f2SEnji Cooper #ifdef GTEST_OS_QURT
202428f6c2f2SEnji Cooper // QuRT doesn't support any directory functions, including rmdir
202528f6c2f2SEnji Cooper inline int RmDir(const char*) { return 0; }
202628f6c2f2SEnji Cooper #else
2027b89a7cc2SEnji Cooper inline int RmDir(const char* dir) { return rmdir(dir); }
202828f6c2f2SEnji Cooper #endif
2029b89a7cc2SEnji Cooper inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
2030b89a7cc2SEnji Cooper 
2031b89a7cc2SEnji Cooper #endif  // GTEST_OS_WINDOWS
203228f6c2f2SEnji Cooper #endif  // GTEST_HAS_FILE_SYSTEM
203328f6c2f2SEnji Cooper 
203428f6c2f2SEnji Cooper // Other functions with a different name on Windows.
203528f6c2f2SEnji Cooper 
203628f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
203728f6c2f2SEnji Cooper 
203828f6c2f2SEnji Cooper #ifdef __BORLANDC__
203928f6c2f2SEnji Cooper inline int DoIsATTY(int fd) { return isatty(fd); }
204028f6c2f2SEnji Cooper inline int StrCaseCmp(const char* s1, const char* s2) {
204128f6c2f2SEnji Cooper   return stricmp(s1, s2);
204228f6c2f2SEnji Cooper }
204328f6c2f2SEnji Cooper #else  // !__BORLANDC__
204428f6c2f2SEnji Cooper #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_ZOS) || \
204528f6c2f2SEnji Cooper     defined(GTEST_OS_IOS) || defined(GTEST_OS_WINDOWS_PHONE) ||  \
204628f6c2f2SEnji Cooper     defined(GTEST_OS_WINDOWS_RT) || defined(ESP_PLATFORM)
204728f6c2f2SEnji Cooper inline int DoIsATTY(int /* fd */) { return 0; }
204828f6c2f2SEnji Cooper #else
204928f6c2f2SEnji Cooper inline int DoIsATTY(int fd) { return _isatty(fd); }
205028f6c2f2SEnji Cooper #endif  // GTEST_OS_WINDOWS_MOBILE
205128f6c2f2SEnji Cooper inline int StrCaseCmp(const char* s1, const char* s2) {
205228f6c2f2SEnji Cooper   return _stricmp(s1, s2);
205328f6c2f2SEnji Cooper }
205428f6c2f2SEnji Cooper #endif  // __BORLANDC__
205528f6c2f2SEnji Cooper 
205628f6c2f2SEnji Cooper #else
205728f6c2f2SEnji Cooper 
205828f6c2f2SEnji Cooper inline int DoIsATTY(int fd) { return isatty(fd); }
205928f6c2f2SEnji Cooper inline int StrCaseCmp(const char* s1, const char* s2) {
206028f6c2f2SEnji Cooper   return strcasecmp(s1, s2);
206128f6c2f2SEnji Cooper }
206228f6c2f2SEnji Cooper 
206328f6c2f2SEnji Cooper #endif  // GTEST_OS_WINDOWS
206428f6c2f2SEnji Cooper 
206528f6c2f2SEnji Cooper inline int IsATTY(int fd) {
206628f6c2f2SEnji Cooper   // DoIsATTY might change errno (for example ENOTTY in case you redirect stdout
206728f6c2f2SEnji Cooper   // to a file on Linux), which is unexpected, so save the previous value, and
206828f6c2f2SEnji Cooper   // restore it after the call.
206928f6c2f2SEnji Cooper   int savedErrno = errno;
207028f6c2f2SEnji Cooper   int isAttyValue = DoIsATTY(fd);
207128f6c2f2SEnji Cooper   errno = savedErrno;
207228f6c2f2SEnji Cooper 
207328f6c2f2SEnji Cooper   return isAttyValue;
207428f6c2f2SEnji Cooper }
2075b89a7cc2SEnji Cooper 
2076b89a7cc2SEnji Cooper // Functions deprecated by MSVC 8.0.
2077b89a7cc2SEnji Cooper 
2078b89a7cc2SEnji Cooper GTEST_DISABLE_MSC_DEPRECATED_PUSH_()
2079b89a7cc2SEnji Cooper 
2080b89a7cc2SEnji Cooper // ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and
2081b89a7cc2SEnji Cooper // StrError() aren't needed on Windows CE at this time and thus not
2082b89a7cc2SEnji Cooper // defined there.
208328f6c2f2SEnji Cooper #if GTEST_HAS_FILE_SYSTEM
208428f6c2f2SEnji Cooper #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_WINDOWS_PHONE) && \
208528f6c2f2SEnji Cooper     !defined(GTEST_OS_WINDOWS_RT) && !defined(GTEST_OS_ESP8266) &&           \
208628f6c2f2SEnji Cooper     !defined(GTEST_OS_XTENSA) && !defined(GTEST_OS_QURT)
2087b89a7cc2SEnji Cooper inline int ChDir(const char* dir) { return chdir(dir); }
2088b89a7cc2SEnji Cooper #endif
2089b89a7cc2SEnji Cooper inline FILE* FOpen(const char* path, const char* mode) {
209028f6c2f2SEnji Cooper #if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MINGW)
209128f6c2f2SEnji Cooper   struct wchar_codecvt : public std::codecvt<wchar_t, char, std::mbstate_t> {};
209228f6c2f2SEnji Cooper   std::wstring_convert<wchar_codecvt> converter;
209328f6c2f2SEnji Cooper   std::wstring wide_path = converter.from_bytes(path);
209428f6c2f2SEnji Cooper   std::wstring wide_mode = converter.from_bytes(mode);
209528f6c2f2SEnji Cooper   return _wfopen(wide_path.c_str(), wide_mode.c_str());
209628f6c2f2SEnji Cooper #else   // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW
2097b89a7cc2SEnji Cooper   return fopen(path, mode);
209828f6c2f2SEnji Cooper #endif  // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW
2099b89a7cc2SEnji Cooper }
210028f6c2f2SEnji Cooper #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT)
2101b89a7cc2SEnji Cooper inline FILE* FReopen(const char* path, const char* mode, FILE* stream) {
2102b89a7cc2SEnji Cooper   return freopen(path, mode, stream);
2103b89a7cc2SEnji Cooper }
2104b89a7cc2SEnji Cooper inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
210528f6c2f2SEnji Cooper #endif  // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
2106b89a7cc2SEnji Cooper inline int FClose(FILE* fp) { return fclose(fp); }
210728f6c2f2SEnji Cooper #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT)
2108b89a7cc2SEnji Cooper inline int Read(int fd, void* buf, unsigned int count) {
2109b89a7cc2SEnji Cooper   return static_cast<int>(read(fd, buf, count));
2110b89a7cc2SEnji Cooper }
2111b89a7cc2SEnji Cooper inline int Write(int fd, const void* buf, unsigned int count) {
2112b89a7cc2SEnji Cooper   return static_cast<int>(write(fd, buf, count));
2113b89a7cc2SEnji Cooper }
2114b89a7cc2SEnji Cooper inline int Close(int fd) { return close(fd); }
211528f6c2f2SEnji Cooper #endif  // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
211628f6c2f2SEnji Cooper #endif  // GTEST_HAS_FILE_SYSTEM
211728f6c2f2SEnji Cooper 
211828f6c2f2SEnji Cooper #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT)
2119b89a7cc2SEnji Cooper inline const char* StrError(int errnum) { return strerror(errnum); }
212028f6c2f2SEnji Cooper #endif  // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
212128f6c2f2SEnji Cooper 
2122b89a7cc2SEnji Cooper inline const char* GetEnv(const char* name) {
212328f6c2f2SEnji Cooper #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_WINDOWS_PHONE) || \
212428f6c2f2SEnji Cooper     defined(GTEST_OS_ESP8266) || defined(GTEST_OS_XTENSA) ||               \
212528f6c2f2SEnji Cooper     defined(GTEST_OS_QURT)
212628f6c2f2SEnji Cooper   // We are on an embedded platform, which has no environment variables.
2127b89a7cc2SEnji Cooper   static_cast<void>(name);  // To prevent 'unused argument' warning.
212828f6c2f2SEnji Cooper   return nullptr;
2129b89a7cc2SEnji Cooper #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
2130b89a7cc2SEnji Cooper   // Environment variables which we programmatically clear will be set to the
2131b89a7cc2SEnji Cooper   // empty string rather than unset (NULL).  Handle that case.
2132b89a7cc2SEnji Cooper   const char* const env = getenv(name);
213328f6c2f2SEnji Cooper   return (env != nullptr && env[0] != '\0') ? env : nullptr;
2134b89a7cc2SEnji Cooper #else
2135b89a7cc2SEnji Cooper   return getenv(name);
2136b89a7cc2SEnji Cooper #endif
2137b89a7cc2SEnji Cooper }
2138b89a7cc2SEnji Cooper 
2139b89a7cc2SEnji Cooper GTEST_DISABLE_MSC_DEPRECATED_POP_()
2140b89a7cc2SEnji Cooper 
214128f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS_MOBILE
2142b89a7cc2SEnji Cooper // Windows CE has no C library. The abort() function is used in
2143b89a7cc2SEnji Cooper // several places in Google Test. This implementation provides a reasonable
2144b89a7cc2SEnji Cooper // imitation of standard behaviour.
214528f6c2f2SEnji Cooper [[noreturn]] void Abort();
2146b89a7cc2SEnji Cooper #else
214728f6c2f2SEnji Cooper [[noreturn]] inline void Abort() { abort(); }
2148b89a7cc2SEnji Cooper #endif  // GTEST_OS_WINDOWS_MOBILE
2149b89a7cc2SEnji Cooper 
2150b89a7cc2SEnji Cooper }  // namespace posix
2151b89a7cc2SEnji Cooper 
2152b89a7cc2SEnji Cooper // MSVC "deprecates" snprintf and issues warnings wherever it is used.  In
2153b89a7cc2SEnji Cooper // order to avoid these warnings, we need to use _snprintf or _snprintf_s on
2154b89a7cc2SEnji Cooper // MSVC-based platforms.  We map the GTEST_SNPRINTF_ macro to the appropriate
2155b89a7cc2SEnji Cooper // function in order to achieve that.  We use macro definition here because
2156b89a7cc2SEnji Cooper // snprintf is a variadic function.
215728f6c2f2SEnji Cooper #if defined(_MSC_VER) && !defined(GTEST_OS_WINDOWS_MOBILE)
2158b89a7cc2SEnji Cooper // MSVC 2005 and above support variadic macros.
2159b89a7cc2SEnji Cooper #define GTEST_SNPRINTF_(buffer, size, format, ...) \
2160b89a7cc2SEnji Cooper   _snprintf_s(buffer, size, size, format, __VA_ARGS__)
2161b89a7cc2SEnji Cooper #elif defined(_MSC_VER)
216228f6c2f2SEnji Cooper // Windows CE does not define _snprintf_s
2163b89a7cc2SEnji Cooper #define GTEST_SNPRINTF_ _snprintf
2164b89a7cc2SEnji Cooper #else
2165b89a7cc2SEnji Cooper #define GTEST_SNPRINTF_ snprintf
2166b89a7cc2SEnji Cooper #endif
2167b89a7cc2SEnji Cooper 
216828f6c2f2SEnji Cooper // The biggest signed integer type the compiler supports.
2169b89a7cc2SEnji Cooper //
217028f6c2f2SEnji Cooper // long long is guaranteed to be at least 64-bits in C++11.
217128f6c2f2SEnji Cooper using BiggestInt = long long;  // NOLINT
217228f6c2f2SEnji Cooper 
217328f6c2f2SEnji Cooper // The maximum number a BiggestInt can represent.
217428f6c2f2SEnji Cooper constexpr BiggestInt kMaxBiggestInt = (std::numeric_limits<BiggestInt>::max)();
2175b89a7cc2SEnji Cooper 
2176b89a7cc2SEnji Cooper // This template class serves as a compile-time function from size to
2177b89a7cc2SEnji Cooper // type.  It maps a size in bytes to a primitive type with that
2178b89a7cc2SEnji Cooper // size. e.g.
2179b89a7cc2SEnji Cooper //
2180b89a7cc2SEnji Cooper //   TypeWithSize<4>::UInt
2181b89a7cc2SEnji Cooper //
2182b89a7cc2SEnji Cooper // is typedef-ed to be unsigned int (unsigned integer made up of 4
2183b89a7cc2SEnji Cooper // bytes).
2184b89a7cc2SEnji Cooper //
2185b89a7cc2SEnji Cooper // Such functionality should belong to STL, but I cannot find it
2186b89a7cc2SEnji Cooper // there.
2187b89a7cc2SEnji Cooper //
2188b89a7cc2SEnji Cooper // Google Test uses this class in the implementation of floating-point
2189b89a7cc2SEnji Cooper // comparison.
2190b89a7cc2SEnji Cooper //
2191b89a7cc2SEnji Cooper // For now it only handles UInt (unsigned int) as that's all Google Test
2192b89a7cc2SEnji Cooper // needs.  Other types can be easily added in the future if need
2193b89a7cc2SEnji Cooper // arises.
2194b89a7cc2SEnji Cooper template <size_t size>
2195b89a7cc2SEnji Cooper class TypeWithSize {
2196b89a7cc2SEnji Cooper  public:
2197b89a7cc2SEnji Cooper   // This prevents the user from using TypeWithSize<N> with incorrect
2198b89a7cc2SEnji Cooper   // values of N.
219928f6c2f2SEnji Cooper   using UInt = void;
2200b89a7cc2SEnji Cooper };
2201b89a7cc2SEnji Cooper 
2202b89a7cc2SEnji Cooper // The specialization for size 4.
2203b89a7cc2SEnji Cooper template <>
2204b89a7cc2SEnji Cooper class TypeWithSize<4> {
2205b89a7cc2SEnji Cooper  public:
220628f6c2f2SEnji Cooper   using Int = std::int32_t;
220728f6c2f2SEnji Cooper   using UInt = std::uint32_t;
2208b89a7cc2SEnji Cooper };
2209b89a7cc2SEnji Cooper 
2210b89a7cc2SEnji Cooper // The specialization for size 8.
2211b89a7cc2SEnji Cooper template <>
2212b89a7cc2SEnji Cooper class TypeWithSize<8> {
2213b89a7cc2SEnji Cooper  public:
221428f6c2f2SEnji Cooper   using Int = std::int64_t;
221528f6c2f2SEnji Cooper   using UInt = std::uint64_t;
2216b89a7cc2SEnji Cooper };
2217b89a7cc2SEnji Cooper 
2218b89a7cc2SEnji Cooper // Integer types of known sizes.
221928f6c2f2SEnji Cooper using TimeInMillis = int64_t;  // Represents time in milliseconds.
2220b89a7cc2SEnji Cooper 
2221b89a7cc2SEnji Cooper // Utilities for command line flags and environment variables.
2222b89a7cc2SEnji Cooper 
2223b89a7cc2SEnji Cooper // Macro for referencing flags.
2224b89a7cc2SEnji Cooper #if !defined(GTEST_FLAG)
222528f6c2f2SEnji Cooper #define GTEST_FLAG_NAME_(name) gtest_##name
2226b89a7cc2SEnji Cooper #define GTEST_FLAG(name) FLAGS_gtest_##name
2227b89a7cc2SEnji Cooper #endif  // !defined(GTEST_FLAG)
2228b89a7cc2SEnji Cooper 
222928f6c2f2SEnji Cooper // Pick a command line flags implementation.
223028f6c2f2SEnji Cooper #ifdef GTEST_HAS_ABSL
2231b89a7cc2SEnji Cooper 
2232b89a7cc2SEnji Cooper // Macros for defining flags.
2233b89a7cc2SEnji Cooper #define GTEST_DEFINE_bool_(name, default_val, doc) \
223428f6c2f2SEnji Cooper   ABSL_FLAG(bool, GTEST_FLAG_NAME_(name), default_val, doc)
2235b89a7cc2SEnji Cooper #define GTEST_DEFINE_int32_(name, default_val, doc) \
223628f6c2f2SEnji Cooper   ABSL_FLAG(int32_t, GTEST_FLAG_NAME_(name), default_val, doc)
2237b89a7cc2SEnji Cooper #define GTEST_DEFINE_string_(name, default_val, doc) \
223828f6c2f2SEnji Cooper   ABSL_FLAG(std::string, GTEST_FLAG_NAME_(name), default_val, doc)
2239b89a7cc2SEnji Cooper 
224028f6c2f2SEnji Cooper // Macros for declaring flags.
224128f6c2f2SEnji Cooper #define GTEST_DECLARE_bool_(name) \
224228f6c2f2SEnji Cooper   ABSL_DECLARE_FLAG(bool, GTEST_FLAG_NAME_(name))
224328f6c2f2SEnji Cooper #define GTEST_DECLARE_int32_(name) \
224428f6c2f2SEnji Cooper   ABSL_DECLARE_FLAG(int32_t, GTEST_FLAG_NAME_(name))
224528f6c2f2SEnji Cooper #define GTEST_DECLARE_string_(name) \
224628f6c2f2SEnji Cooper   ABSL_DECLARE_FLAG(std::string, GTEST_FLAG_NAME_(name))
224728f6c2f2SEnji Cooper 
224828f6c2f2SEnji Cooper #define GTEST_FLAG_SAVER_ ::absl::FlagSaver
224928f6c2f2SEnji Cooper 
225028f6c2f2SEnji Cooper #define GTEST_FLAG_GET(name) ::absl::GetFlag(GTEST_FLAG(name))
225128f6c2f2SEnji Cooper #define GTEST_FLAG_SET(name, value) \
225228f6c2f2SEnji Cooper   (void)(::absl::SetFlag(&GTEST_FLAG(name), value))
225328f6c2f2SEnji Cooper #define GTEST_USE_OWN_FLAGFILE_FLAG_ 0
225428f6c2f2SEnji Cooper 
225528f6c2f2SEnji Cooper #else  // GTEST_HAS_ABSL
225628f6c2f2SEnji Cooper 
225728f6c2f2SEnji Cooper // Macros for defining flags.
225828f6c2f2SEnji Cooper #define GTEST_DEFINE_bool_(name, default_val, doc)  \
225928f6c2f2SEnji Cooper   namespace testing {                               \
226028f6c2f2SEnji Cooper   GTEST_API_ bool GTEST_FLAG(name) = (default_val); \
226128f6c2f2SEnji Cooper   }                                                 \
226228f6c2f2SEnji Cooper   static_assert(true, "no-op to require trailing semicolon")
226328f6c2f2SEnji Cooper #define GTEST_DEFINE_int32_(name, default_val, doc)         \
226428f6c2f2SEnji Cooper   namespace testing {                                       \
226528f6c2f2SEnji Cooper   GTEST_API_ std::int32_t GTEST_FLAG(name) = (default_val); \
226628f6c2f2SEnji Cooper   }                                                         \
226728f6c2f2SEnji Cooper   static_assert(true, "no-op to require trailing semicolon")
226828f6c2f2SEnji Cooper #define GTEST_DEFINE_string_(name, default_val, doc)         \
226928f6c2f2SEnji Cooper   namespace testing {                                        \
227028f6c2f2SEnji Cooper   GTEST_API_ ::std::string GTEST_FLAG(name) = (default_val); \
227128f6c2f2SEnji Cooper   }                                                          \
227228f6c2f2SEnji Cooper   static_assert(true, "no-op to require trailing semicolon")
227328f6c2f2SEnji Cooper 
227428f6c2f2SEnji Cooper // Macros for declaring flags.
227528f6c2f2SEnji Cooper #define GTEST_DECLARE_bool_(name)          \
227628f6c2f2SEnji Cooper   namespace testing {                      \
227728f6c2f2SEnji Cooper   GTEST_API_ extern bool GTEST_FLAG(name); \
227828f6c2f2SEnji Cooper   }                                        \
227928f6c2f2SEnji Cooper   static_assert(true, "no-op to require trailing semicolon")
228028f6c2f2SEnji Cooper #define GTEST_DECLARE_int32_(name)                 \
228128f6c2f2SEnji Cooper   namespace testing {                              \
228228f6c2f2SEnji Cooper   GTEST_API_ extern std::int32_t GTEST_FLAG(name); \
228328f6c2f2SEnji Cooper   }                                                \
228428f6c2f2SEnji Cooper   static_assert(true, "no-op to require trailing semicolon")
228528f6c2f2SEnji Cooper #define GTEST_DECLARE_string_(name)                 \
228628f6c2f2SEnji Cooper   namespace testing {                               \
228728f6c2f2SEnji Cooper   GTEST_API_ extern ::std::string GTEST_FLAG(name); \
228828f6c2f2SEnji Cooper   }                                                 \
228928f6c2f2SEnji Cooper   static_assert(true, "no-op to require trailing semicolon")
229028f6c2f2SEnji Cooper 
229128f6c2f2SEnji Cooper #define GTEST_FLAG_SAVER_ ::testing::internal::GTestFlagSaver
229228f6c2f2SEnji Cooper 
229328f6c2f2SEnji Cooper #define GTEST_FLAG_GET(name) ::testing::GTEST_FLAG(name)
229428f6c2f2SEnji Cooper #define GTEST_FLAG_SET(name, value) (void)(::testing::GTEST_FLAG(name) = value)
229528f6c2f2SEnji Cooper #define GTEST_USE_OWN_FLAGFILE_FLAG_ 1
229628f6c2f2SEnji Cooper 
229728f6c2f2SEnji Cooper #endif  // GTEST_HAS_ABSL
2298b89a7cc2SEnji Cooper 
2299b89a7cc2SEnji Cooper // Thread annotations
2300b89a7cc2SEnji Cooper #if !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)
2301b89a7cc2SEnji Cooper #define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
2302b89a7cc2SEnji Cooper #define GTEST_LOCK_EXCLUDED_(locks)
2303b89a7cc2SEnji Cooper #endif  // !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)
2304b89a7cc2SEnji Cooper 
2305b89a7cc2SEnji Cooper // Parses 'str' for a 32-bit signed integer.  If successful, writes the result
2306b89a7cc2SEnji Cooper // to *value and returns true; otherwise leaves *value unchanged and returns
2307b89a7cc2SEnji Cooper // false.
230828f6c2f2SEnji Cooper GTEST_API_ bool ParseInt32(const Message& src_text, const char* str,
230928f6c2f2SEnji Cooper                            int32_t* value);
2310b89a7cc2SEnji Cooper 
231128f6c2f2SEnji Cooper // Parses a bool/int32_t/string from the environment variable
2312b89a7cc2SEnji Cooper // corresponding to the given Google Test flag.
2313b89a7cc2SEnji Cooper bool BoolFromGTestEnv(const char* flag, bool default_val);
231428f6c2f2SEnji Cooper GTEST_API_ int32_t Int32FromGTestEnv(const char* flag, int32_t default_val);
2315b89a7cc2SEnji Cooper std::string OutputFlagAlsoCheckEnvVar();
2316b89a7cc2SEnji Cooper const char* StringFromGTestEnv(const char* flag, const char* default_val);
2317b89a7cc2SEnji Cooper 
2318b89a7cc2SEnji Cooper }  // namespace internal
2319b89a7cc2SEnji Cooper }  // namespace testing
2320b89a7cc2SEnji Cooper 
232128f6c2f2SEnji Cooper #if !defined(GTEST_INTERNAL_DEPRECATED)
232228f6c2f2SEnji Cooper 
232328f6c2f2SEnji Cooper // Internal Macro to mark an API deprecated, for googletest usage only
232428f6c2f2SEnji Cooper // Usage: class GTEST_INTERNAL_DEPRECATED(message) MyClass or
232528f6c2f2SEnji Cooper // GTEST_INTERNAL_DEPRECATED(message) <return_type> myFunction(); Every usage of
232628f6c2f2SEnji Cooper // a deprecated entity will trigger a warning when compiled with
232728f6c2f2SEnji Cooper // `-Wdeprecated-declarations` option (clang, gcc, any __GNUC__ compiler).
232828f6c2f2SEnji Cooper // For msvc /W3 option will need to be used
232928f6c2f2SEnji Cooper // Note that for 'other' compilers this macro evaluates to nothing to prevent
233028f6c2f2SEnji Cooper // compilations errors.
233128f6c2f2SEnji Cooper #if defined(_MSC_VER)
233228f6c2f2SEnji Cooper #define GTEST_INTERNAL_DEPRECATED(message) __declspec(deprecated(message))
233328f6c2f2SEnji Cooper #elif defined(__GNUC__)
233428f6c2f2SEnji Cooper #define GTEST_INTERNAL_DEPRECATED(message) __attribute__((deprecated(message)))
233528f6c2f2SEnji Cooper #else
233628f6c2f2SEnji Cooper #define GTEST_INTERNAL_DEPRECATED(message)
233728f6c2f2SEnji Cooper #endif
233828f6c2f2SEnji Cooper 
233928f6c2f2SEnji Cooper #endif  // !defined(GTEST_INTERNAL_DEPRECATED)
234028f6c2f2SEnji Cooper 
234128f6c2f2SEnji Cooper #ifdef GTEST_HAS_ABSL
234228f6c2f2SEnji Cooper // Always use absl::any for UniversalPrinter<> specializations if googletest
234328f6c2f2SEnji Cooper // is built with absl support.
234428f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_ANY 1
234528f6c2f2SEnji Cooper #include "absl/types/any.h"
234628f6c2f2SEnji Cooper namespace testing {
234728f6c2f2SEnji Cooper namespace internal {
234828f6c2f2SEnji Cooper using Any = ::absl::any;
234928f6c2f2SEnji Cooper }  // namespace internal
235028f6c2f2SEnji Cooper }  // namespace testing
235128f6c2f2SEnji Cooper #else
235228f6c2f2SEnji Cooper #ifdef __has_include
235328f6c2f2SEnji Cooper #if __has_include(<any>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L && \
235428f6c2f2SEnji Cooper     (!defined(_MSC_VER) || GTEST_HAS_RTTI)
235528f6c2f2SEnji Cooper // Otherwise for C++17 and higher use std::any for UniversalPrinter<>
235628f6c2f2SEnji Cooper // specializations.
235728f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_ANY 1
235828f6c2f2SEnji Cooper #include <any>
235928f6c2f2SEnji Cooper namespace testing {
236028f6c2f2SEnji Cooper namespace internal {
236128f6c2f2SEnji Cooper using Any = ::std::any;
236228f6c2f2SEnji Cooper }  // namespace internal
236328f6c2f2SEnji Cooper }  // namespace testing
236428f6c2f2SEnji Cooper // The case where absl is configured NOT to alias std::any is not
236528f6c2f2SEnji Cooper // supported.
236628f6c2f2SEnji Cooper #endif  // __has_include(<any>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
236728f6c2f2SEnji Cooper #endif  // __has_include
236828f6c2f2SEnji Cooper #endif  // GTEST_HAS_ABSL
236928f6c2f2SEnji Cooper 
237028f6c2f2SEnji Cooper #ifndef GTEST_INTERNAL_HAS_ANY
237128f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_ANY 0
237228f6c2f2SEnji Cooper #endif
237328f6c2f2SEnji Cooper 
237428f6c2f2SEnji Cooper #ifdef GTEST_HAS_ABSL
237528f6c2f2SEnji Cooper // Always use absl::optional for UniversalPrinter<> specializations if
237628f6c2f2SEnji Cooper // googletest is built with absl support.
237728f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_OPTIONAL 1
237828f6c2f2SEnji Cooper #include "absl/types/optional.h"
237928f6c2f2SEnji Cooper namespace testing {
238028f6c2f2SEnji Cooper namespace internal {
238128f6c2f2SEnji Cooper template <typename T>
238228f6c2f2SEnji Cooper using Optional = ::absl::optional<T>;
238328f6c2f2SEnji Cooper inline ::absl::nullopt_t Nullopt() { return ::absl::nullopt; }
238428f6c2f2SEnji Cooper }  // namespace internal
238528f6c2f2SEnji Cooper }  // namespace testing
238628f6c2f2SEnji Cooper #else
238728f6c2f2SEnji Cooper #ifdef __has_include
238828f6c2f2SEnji Cooper #if __has_include(<optional>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
238928f6c2f2SEnji Cooper // Otherwise for C++17 and higher use std::optional for UniversalPrinter<>
239028f6c2f2SEnji Cooper // specializations.
239128f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_OPTIONAL 1
239228f6c2f2SEnji Cooper #include <optional>
239328f6c2f2SEnji Cooper namespace testing {
239428f6c2f2SEnji Cooper namespace internal {
239528f6c2f2SEnji Cooper template <typename T>
239628f6c2f2SEnji Cooper using Optional = ::std::optional<T>;
239728f6c2f2SEnji Cooper inline ::std::nullopt_t Nullopt() { return ::std::nullopt; }
239828f6c2f2SEnji Cooper }  // namespace internal
239928f6c2f2SEnji Cooper }  // namespace testing
240028f6c2f2SEnji Cooper // The case where absl is configured NOT to alias std::optional is not
240128f6c2f2SEnji Cooper // supported.
240228f6c2f2SEnji Cooper #endif  // __has_include(<optional>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
240328f6c2f2SEnji Cooper #endif  // __has_include
240428f6c2f2SEnji Cooper #endif  // GTEST_HAS_ABSL
240528f6c2f2SEnji Cooper 
240628f6c2f2SEnji Cooper #ifndef GTEST_INTERNAL_HAS_OPTIONAL
240728f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_OPTIONAL 0
240828f6c2f2SEnji Cooper #endif
240928f6c2f2SEnji Cooper 
241028f6c2f2SEnji Cooper #ifdef GTEST_HAS_ABSL
241128f6c2f2SEnji Cooper // Always use absl::string_view for Matcher<> specializations if googletest
241228f6c2f2SEnji Cooper // is built with absl support.
241328f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_STRING_VIEW 1
241428f6c2f2SEnji Cooper #include "absl/strings/string_view.h"
241528f6c2f2SEnji Cooper namespace testing {
241628f6c2f2SEnji Cooper namespace internal {
241728f6c2f2SEnji Cooper using StringView = ::absl::string_view;
241828f6c2f2SEnji Cooper }  // namespace internal
241928f6c2f2SEnji Cooper }  // namespace testing
242028f6c2f2SEnji Cooper #else
242128f6c2f2SEnji Cooper #ifdef __has_include
242228f6c2f2SEnji Cooper #if __has_include(<string_view>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
242328f6c2f2SEnji Cooper // Otherwise for C++17 and higher use std::string_view for Matcher<>
242428f6c2f2SEnji Cooper // specializations.
242528f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_STRING_VIEW 1
242628f6c2f2SEnji Cooper #include <string_view>
242728f6c2f2SEnji Cooper namespace testing {
242828f6c2f2SEnji Cooper namespace internal {
242928f6c2f2SEnji Cooper using StringView = ::std::string_view;
243028f6c2f2SEnji Cooper }  // namespace internal
243128f6c2f2SEnji Cooper }  // namespace testing
243228f6c2f2SEnji Cooper // The case where absl is configured NOT to alias std::string_view is not
243328f6c2f2SEnji Cooper // supported.
243428f6c2f2SEnji Cooper #endif  // __has_include(<string_view>) && GTEST_INTERNAL_CPLUSPLUS_LANG >=
243528f6c2f2SEnji Cooper         // 201703L
243628f6c2f2SEnji Cooper #endif  // __has_include
243728f6c2f2SEnji Cooper #endif  // GTEST_HAS_ABSL
243828f6c2f2SEnji Cooper 
243928f6c2f2SEnji Cooper #ifndef GTEST_INTERNAL_HAS_STRING_VIEW
244028f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_STRING_VIEW 0
244128f6c2f2SEnji Cooper #endif
244228f6c2f2SEnji Cooper 
244328f6c2f2SEnji Cooper #ifdef GTEST_HAS_ABSL
244428f6c2f2SEnji Cooper // Always use absl::variant for UniversalPrinter<> specializations if googletest
244528f6c2f2SEnji Cooper // is built with absl support.
244628f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_VARIANT 1
244728f6c2f2SEnji Cooper #include "absl/types/variant.h"
244828f6c2f2SEnji Cooper namespace testing {
244928f6c2f2SEnji Cooper namespace internal {
245028f6c2f2SEnji Cooper template <typename... T>
245128f6c2f2SEnji Cooper using Variant = ::absl::variant<T...>;
245228f6c2f2SEnji Cooper }  // namespace internal
245328f6c2f2SEnji Cooper }  // namespace testing
245428f6c2f2SEnji Cooper #else
245528f6c2f2SEnji Cooper #ifdef __has_include
245628f6c2f2SEnji Cooper #if __has_include(<variant>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
245728f6c2f2SEnji Cooper // Otherwise for C++17 and higher use std::variant for UniversalPrinter<>
245828f6c2f2SEnji Cooper // specializations.
245928f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_VARIANT 1
246028f6c2f2SEnji Cooper #include <variant>
246128f6c2f2SEnji Cooper namespace testing {
246228f6c2f2SEnji Cooper namespace internal {
246328f6c2f2SEnji Cooper template <typename... T>
246428f6c2f2SEnji Cooper using Variant = ::std::variant<T...>;
246528f6c2f2SEnji Cooper }  // namespace internal
246628f6c2f2SEnji Cooper }  // namespace testing
246728f6c2f2SEnji Cooper // The case where absl is configured NOT to alias std::variant is not supported.
246828f6c2f2SEnji Cooper #endif  // __has_include(<variant>) && GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L
246928f6c2f2SEnji Cooper #endif  // __has_include
247028f6c2f2SEnji Cooper #endif  // GTEST_HAS_ABSL
247128f6c2f2SEnji Cooper 
247228f6c2f2SEnji Cooper #ifndef GTEST_INTERNAL_HAS_VARIANT
247328f6c2f2SEnji Cooper #define GTEST_INTERNAL_HAS_VARIANT 0
247428f6c2f2SEnji Cooper #endif
247528f6c2f2SEnji Cooper 
247628f6c2f2SEnji Cooper #if defined(GTEST_INTERNAL_CPLUSPLUS_LANG) && \
247728f6c2f2SEnji Cooper     GTEST_INTERNAL_CPLUSPLUS_LANG < 201703L
247828f6c2f2SEnji Cooper #define GTEST_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL 1
247928f6c2f2SEnji Cooper #endif
248028f6c2f2SEnji Cooper 
248128f6c2f2SEnji Cooper #endif  // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
2482