ci_base
gtest-port.h
1 // Copyright 2005, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Low-level types and utilities for porting Google Test to various
31 // platforms. All macros ending with _ and symbols defined in an
32 // internal namespace are subject to change without notice. Code
33 // outside Google Test MUST NOT USE THEM DIRECTLY. Macros that don't
34 // end with _ are part of Google Test's public API and can be used by
35 // code outside Google Test.
36 //
37 // This file is fundamental to Google Test. All other Google Test source
38 // files are expected to #include this. Therefore, it cannot #include
39 // any other Google Test header.
40 
41 // GOOGLETEST_CM0001 DO NOT DELETE
42 
43 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
44 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
45 
46 // Environment-describing macros
47 // -----------------------------
48 //
49 // Google Test can be used in many different environments. Macros in
50 // this section tell Google Test what kind of environment it is being
51 // used in, such that Google Test can provide environment-specific
52 // features and implementations.
53 //
54 // Google Test tries to automatically detect the properties of its
55 // environment, so users usually don't need to worry about these
56 // macros. However, the automatic detection is not perfect.
57 // Sometimes it's necessary for a user to define some of the following
58 // macros in the build script to override Google Test's decisions.
59 //
60 // If the user doesn't define a macro in the list, Google Test will
61 // provide a default definition. After this header is #included, all
62 // macros in this list will be defined to either 1 or 0.
63 //
64 // Notes to maintainers:
65 // - Each macro here is a user-tweakable knob; do not grow the list
66 // lightly.
67 // - Use #if to key off these macros. Don't use #ifdef or "#if
68 // defined(...)", which will not work as these macros are ALWAYS
69 // defined.
70 //
71 // GTEST_HAS_CLONE - Define it to 1/0 to indicate that clone(2)
72 // is/isn't available.
73 // GTEST_HAS_EXCEPTIONS - Define it to 1/0 to indicate that exceptions
74 // are enabled.
75 // GTEST_HAS_GLOBAL_STRING - Define it to 1/0 to indicate that ::string
76 // is/isn't available
77 // GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::wstring
78 // is/isn't available
79 // GTEST_HAS_POSIX_RE - Define it to 1/0 to indicate that POSIX regular
80 // expressions are/aren't available.
81 // GTEST_HAS_PTHREAD - Define it to 1/0 to indicate that <pthread.h>
82 // is/isn't available.
83 // GTEST_HAS_RTTI - Define it to 1/0 to indicate that RTTI is/isn't
84 // enabled.
85 // GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that
86 // std::wstring does/doesn't work (Google Test can
87 // be used where std::wstring is unavailable).
88 // GTEST_HAS_SEH - Define it to 1/0 to indicate whether the
89 // compiler supports Microsoft's "Structured
90 // Exception Handling".
91 // GTEST_HAS_STREAM_REDIRECTION
92 // - Define it to 1/0 to indicate whether the
93 // platform supports I/O stream redirection using
94 // dup() and dup2().
95 // GTEST_LINKED_AS_SHARED_LIBRARY
96 // - Define to 1 when compiling tests that use
97 // Google Test as a shared library (known as
98 // DLL on Windows).
99 // GTEST_CREATE_SHARED_LIBRARY
100 // - Define to 1 when compiling Google Test itself
101 // as a shared library.
102 // GTEST_DEFAULT_DEATH_TEST_STYLE
103 // - The default value of --gtest_death_test_style.
104 // The legacy default has been "fast" in the open
105 // source version since 2008. The recommended value
106 // is "threadsafe", and can be set in
107 // custom/gtest-port.h.
108 
109 // Platform-indicating macros
110 // --------------------------
111 //
112 // Macros indicating the platform on which Google Test is being used
113 // (a macro is defined to 1 if compiled on the given platform;
114 // otherwise UNDEFINED -- it's never defined to 0.). Google Test
115 // defines these macros automatically. Code outside Google Test MUST
116 // NOT define them.
117 //
118 // GTEST_OS_AIX - IBM AIX
119 // GTEST_OS_CYGWIN - Cygwin
120 // GTEST_OS_FREEBSD - FreeBSD
121 // GTEST_OS_FUCHSIA - Fuchsia
122 // GTEST_OS_HPUX - HP-UX
123 // GTEST_OS_LINUX - Linux
124 // GTEST_OS_LINUX_ANDROID - Google Android
125 // GTEST_OS_MAC - Mac OS X
126 // GTEST_OS_IOS - iOS
127 // GTEST_OS_NACL - Google Native Client (NaCl)
128 // GTEST_OS_NETBSD - NetBSD
129 // GTEST_OS_OPENBSD - OpenBSD
130 // GTEST_OS_OS2 - OS/2
131 // GTEST_OS_QNX - QNX
132 // GTEST_OS_SOLARIS - Sun Solaris
133 // GTEST_OS_WINDOWS - Windows (Desktop, MinGW, or Mobile)
134 // GTEST_OS_WINDOWS_DESKTOP - Windows Desktop
135 // GTEST_OS_WINDOWS_MINGW - MinGW
136 // GTEST_OS_WINDOWS_MOBILE - Windows Mobile
137 // GTEST_OS_WINDOWS_PHONE - Windows Phone
138 // GTEST_OS_WINDOWS_RT - Windows Store App/WinRT
139 // GTEST_OS_ZOS - z/OS
140 //
141 // Among the platforms, Cygwin, Linux, Max OS X, and Windows have the
142 // most stable support. Since core members of the Google Test project
143 // don't have access to other platforms, support for them may be less
144 // stable. If you notice any problems on your platform, please notify
145 // googletestframework@googlegroups.com (patches for fixing them are
146 // even more welcome!).
147 //
148 // It is possible that none of the GTEST_OS_* macros are defined.
149 
150 // Feature-indicating macros
151 // -------------------------
152 //
153 // Macros indicating which Google Test features are available (a macro
154 // is defined to 1 if the corresponding feature is supported;
155 // otherwise UNDEFINED -- it's never defined to 0.). Google Test
156 // defines these macros automatically. Code outside Google Test MUST
157 // NOT define them.
158 //
159 // These macros are public so that portable tests can be written.
160 // Such tests typically surround code using a feature with an #if
161 // which controls that code. For example:
162 //
163 // #if GTEST_HAS_DEATH_TEST
164 // EXPECT_DEATH(DoSomethingDeadly());
165 // #endif
166 //
167 // GTEST_HAS_DEATH_TEST - death tests
168 // GTEST_HAS_TYPED_TEST - typed tests
169 // GTEST_HAS_TYPED_TEST_P - type-parameterized tests
170 // GTEST_IS_THREADSAFE - Google Test is thread-safe.
171 // GOOGLETEST_CM0007 DO NOT DELETE
172 // GTEST_USES_POSIX_RE - enhanced POSIX regex is used. Do not confuse with
173 // GTEST_HAS_POSIX_RE (see above) which users can
174 // define themselves.
175 // GTEST_USES_SIMPLE_RE - our own simple regex is used;
176 // the above RE\b(s) are mutually exclusive.
177 
178 // Misc public macros
179 // ------------------
180 //
181 // GTEST_FLAG(flag_name) - references the variable corresponding to
182 // the given Google Test flag.
183 
184 // Internal utilities
185 // ------------------
186 //
187 // The following macros and utilities are for Google Test's INTERNAL
188 // use only. Code outside Google Test MUST NOT USE THEM DIRECTLY.
189 //
190 // Macros for basic C++ coding:
191 // GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.
192 // GTEST_ATTRIBUTE_UNUSED_ - declares that a class' instances or a
193 // variable don't have to be used.
194 // GTEST_DISALLOW_ASSIGN_ - disables operator=.
195 // GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=.
196 // GTEST_MUST_USE_RESULT_ - declares that a function's result must be used.
197 // GTEST_INTENTIONAL_CONST_COND_PUSH_ - start code section where MSVC C4127 is
198 // suppressed (constant conditional).
199 // GTEST_INTENTIONAL_CONST_COND_POP_ - finish code section where MSVC C4127
200 // is suppressed.
201 //
202 // Synchronization:
203 // Mutex, MutexLock, ThreadLocal, GetThreadCount()
204 // - synchronization primitives.
205 //
206 // Template meta programming:
207 // IteratorTraits - partial implementation of std::iterator_traits, which
208 // is not available in libCstd when compiled with Sun C++.
209 //
210 //
211 // Regular expressions:
212 // RE - a simple regular expression class using the POSIX
213 // Extended Regular Expression syntax on UNIX-like platforms
214 // GOOGLETEST_CM0008 DO NOT DELETE
215 // or a reduced regular exception syntax on other
216 // platforms, including Windows.
217 // Logging:
218 // GTEST_LOG_() - logs messages at the specified severity level.
219 // LogToStderr() - directs all log messages to stderr.
220 // FlushInfoLog() - flushes informational log messages.
221 //
222 // Stdout and stderr capturing:
223 // CaptureStdout() - starts capturing stdout.
224 // GetCapturedStdout() - stops capturing stdout and returns the captured
225 // string.
226 // CaptureStderr() - starts capturing stderr.
227 // GetCapturedStderr() - stops capturing stderr and returns the captured
228 // string.
229 //
230 // Integer types:
231 // TypeWithSize - maps an integer to a int type.
232 // Int32, UInt32, Int64, UInt64, TimeInMillis
233 // - integers of known sizes.
234 // BiggestInt - the biggest signed integer type.
235 //
236 // Command-line utilities:
237 // GTEST_DECLARE_*() - declares a flag.
238 // GTEST_DEFINE_*() - defines a flag.
239 // GetInjectableArgvs() - returns the command line as a vector of strings.
240 //
241 // Environment variable utilities:
242 // GetEnv() - gets the value of an environment variable.
243 // BoolFromGTestEnv() - parses a bool environment variable.
244 // Int32FromGTestEnv() - parses an Int32 environment variable.
245 // StringFromGTestEnv() - parses a string environment variable.
246 
247 #include <ctype.h> // for isspace, etc
248 #include <stddef.h> // for ptrdiff_t
249 #include <stdio.h>
250 #include <stdlib.h>
251 #include <string.h>
252 #include <memory>
253 #include <type_traits>
254 
255 #ifndef _WIN32_WCE
256 # include <sys/types.h>
257 # include <sys/stat.h>
258 #endif // !_WIN32_WCE
259 
260 #if defined __APPLE__
261 # include <AvailabilityMacros.h>
262 # include <TargetConditionals.h>
263 #endif
264 
265 // Brings in the definition of HAS_GLOBAL_STRING. This must be done
266 // BEFORE we test HAS_GLOBAL_STRING.
267 #include <string> // NOLINT
268 #include <algorithm> // NOLINT
269 #include <iostream> // NOLINT
270 #include <sstream> // NOLINT
271 #include <tuple>
272 #include <utility>
273 #include <vector> // NOLINT
274 
275 #include "gtest/internal/gtest-port-arch.h"
276 #include "gtest/internal/custom/gtest-port.h"
277 
278 #if !defined(GTEST_DEV_EMAIL_)
279 # define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
280 # define GTEST_FLAG_PREFIX_ "gtest_"
281 # define GTEST_FLAG_PREFIX_DASH_ "gtest-"
282 # define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
283 # define GTEST_NAME_ "Google Test"
284 # define GTEST_PROJECT_URL_ "https://github.com/google/googletest/"
285 #endif // !defined(GTEST_DEV_EMAIL_)
286 
287 #if !defined(GTEST_INIT_GOOGLE_TEST_NAME_)
288 # define GTEST_INIT_GOOGLE_TEST_NAME_ "testing::InitGoogleTest"
289 #endif // !defined(GTEST_INIT_GOOGLE_TEST_NAME_)
290 
291 // Determines the version of gcc that is used to compile this.
292 #ifdef __GNUC__
293 // 40302 means version 4.3.2.
294 # define GTEST_GCC_VER_ \
295  (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
296 #endif // __GNUC__
297 
298 // Macros for disabling Microsoft Visual C++ warnings.
299 //
300 // GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 4385)
301 // /* code that triggers warnings C4800 and C4385 */
302 // GTEST_DISABLE_MSC_WARNINGS_POP_()
303 #if defined(_MSC_VER)
304 # define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) \
305  __pragma(warning(push)) \
306  __pragma(warning(disable: warnings))
307 # define GTEST_DISABLE_MSC_WARNINGS_POP_() \
308  __pragma(warning(pop))
309 #else
310 // Not all compilers are MSVC
311 # define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
312 # define GTEST_DISABLE_MSC_WARNINGS_POP_()
313 #endif
314 
315 // Clang on Windows does not understand MSVC's pragma warning.
316 // We need clang-specific way to disable function deprecation warning.
317 #ifdef __clang__
318 # define GTEST_DISABLE_MSC_DEPRECATED_PUSH_() \
319  _Pragma("clang diagnostic push") \
320  _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") \
321  _Pragma("clang diagnostic ignored \"-Wdeprecated-implementations\"")
322 #define GTEST_DISABLE_MSC_DEPRECATED_POP_() \
323  _Pragma("clang diagnostic pop")
324 #else
325 # define GTEST_DISABLE_MSC_DEPRECATED_PUSH_() \
326  GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996)
327 # define GTEST_DISABLE_MSC_DEPRECATED_POP_() \
328  GTEST_DISABLE_MSC_WARNINGS_POP_()
329 #endif
330 
331 // Brings in definitions for functions used in the testing::internal::posix
332 // namespace (read, write, close, chdir, isatty, stat). We do not currently
333 // use them on Windows Mobile.
334 #if GTEST_OS_WINDOWS
335 # if !GTEST_OS_WINDOWS_MOBILE
336 # include <direct.h>
337 # include <io.h>
338 # endif
339 // In order to avoid having to include <windows.h>, use forward declaration
340 #if GTEST_OS_WINDOWS_MINGW && !defined(__MINGW64_VERSION_MAJOR)
341 // MinGW defined _CRITICAL_SECTION and _RTL_CRITICAL_SECTION as two
342 // separate (equivalent) structs, instead of using typedef
343 typedef struct _CRITICAL_SECTION GTEST_CRITICAL_SECTION;
344 #else
345 // Assume CRITICAL_SECTION is a typedef of _RTL_CRITICAL_SECTION.
346 // This assumption is verified by
347 // WindowsTypesTest.CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION.
348 typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
349 #endif
350 #else
351 // This assumes that non-Windows OSes provide unistd.h. For OSes where this
352 // is not the case, we need to include headers that provide the functions
353 // mentioned above.
354 # include <unistd.h>
355 # include <strings.h>
356 #endif // GTEST_OS_WINDOWS
357 
358 #if GTEST_OS_LINUX_ANDROID
359 // Used to define __ANDROID_API__ matching the target NDK API level.
360 # include <android/api-level.h> // NOLINT
361 #endif
362 
363 // Defines this to true iff Google Test can use POSIX regular expressions.
364 #ifndef GTEST_HAS_POSIX_RE
365 # if GTEST_OS_LINUX_ANDROID
366 // On Android, <regex.h> is only available starting with Gingerbread.
367 # define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9)
368 # else
369 # define GTEST_HAS_POSIX_RE (!GTEST_OS_WINDOWS)
370 # endif
371 #endif
372 
373 #if GTEST_USES_PCRE
374 // The appropriate headers have already been included.
375 
376 #elif GTEST_HAS_POSIX_RE
377 
378 // On some platforms, <regex.h> needs someone to define size_t, and
379 // won't compile otherwise. We can #include it here as we already
380 // included <stdlib.h>, which is guaranteed to define size_t through
381 // <stddef.h>.
382 # include <regex.h> // NOLINT
383 
384 # define GTEST_USES_POSIX_RE 1
385 
386 #elif GTEST_OS_WINDOWS
387 
388 // <regex.h> is not available on Windows. Use our own simple regex
389 // implementation instead.
390 # define GTEST_USES_SIMPLE_RE 1
391 
392 #else
393 
394 // <regex.h> may not be available on this platform. Use our own
395 // simple regex implementation instead.
396 # define GTEST_USES_SIMPLE_RE 1
397 
398 #endif // GTEST_USES_PCRE
399 
400 #ifndef GTEST_HAS_EXCEPTIONS
401 // The user didn't tell us whether exceptions are enabled, so we need
402 // to figure it out.
403 # if defined(_MSC_VER) && defined(_CPPUNWIND)
404 // MSVC defines _CPPUNWIND to 1 iff exceptions are enabled.
405 # define GTEST_HAS_EXCEPTIONS 1
406 # elif defined(__BORLANDC__)
407 // C++Builder's implementation of the STL uses the _HAS_EXCEPTIONS
408 // macro to enable exceptions, so we'll do the same.
409 // Assumes that exceptions are enabled by default.
410 # ifndef _HAS_EXCEPTIONS
411 # define _HAS_EXCEPTIONS 1
412 # endif // _HAS_EXCEPTIONS
413 # define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
414 # elif defined(__clang__)
415 // clang defines __EXCEPTIONS iff exceptions are enabled before clang 220714,
416 // but iff cleanups are enabled after that. In Obj-C++ files, there can be
417 // cleanups for ObjC exceptions which also need cleanups, even if C++ exceptions
418 // are disabled. clang has __has_feature(cxx_exceptions) which checks for C++
419 // exceptions starting at clang r206352, but which checked for cleanups prior to
420 // that. To reliably check for C++ exception availability with clang, check for
421 // __EXCEPTIONS && __has_feature(cxx_exceptions).
422 # define GTEST_HAS_EXCEPTIONS (__EXCEPTIONS && __has_feature(cxx_exceptions))
423 # elif defined(__GNUC__) && __EXCEPTIONS
424 // gcc defines __EXCEPTIONS to 1 iff exceptions are enabled.
425 # define GTEST_HAS_EXCEPTIONS 1
426 # elif defined(__SUNPRO_CC)
427 // Sun Pro CC supports exceptions. However, there is no compile-time way of
428 // detecting whether they are enabled or not. Therefore, we assume that
429 // they are enabled unless the user tells us otherwise.
430 # define GTEST_HAS_EXCEPTIONS 1
431 # elif defined(__IBMCPP__) && __EXCEPTIONS
432 // xlC defines __EXCEPTIONS to 1 iff exceptions are enabled.
433 # define GTEST_HAS_EXCEPTIONS 1
434 # elif defined(__HP_aCC)
435 // Exception handling is in effect by default in HP aCC compiler. It has to
436 // be turned of by +noeh compiler option if desired.
437 # define GTEST_HAS_EXCEPTIONS 1
438 # else
439 // For other compilers, we assume exceptions are disabled to be
440 // conservative.
441 # define GTEST_HAS_EXCEPTIONS 0
442 # endif // defined(_MSC_VER) || defined(__BORLANDC__)
443 #endif // GTEST_HAS_EXCEPTIONS
444 
445 #if !defined(GTEST_HAS_STD_STRING)
446 // Even though we don't use this macro any longer, we keep it in case
447 // some clients still depend on it.
448 # define GTEST_HAS_STD_STRING 1
449 #elif !GTEST_HAS_STD_STRING
450 // The user told us that ::std::string isn't available.
451 # error "::std::string isn't available."
452 #endif // !defined(GTEST_HAS_STD_STRING)
453 
454 #ifndef GTEST_HAS_GLOBAL_STRING
455 # define GTEST_HAS_GLOBAL_STRING 0
456 #endif // GTEST_HAS_GLOBAL_STRING
457 
458 #ifndef GTEST_HAS_STD_WSTRING
459 // The user didn't tell us whether ::std::wstring is available, so we need
460 // to figure it out.
461 // Cygwin 1.7 and below doesn't support ::std::wstring.
462 // Solaris' libc++ doesn't support it either. Android has
463 // no support for it at least as recent as Froyo (2.2).
464 # define GTEST_HAS_STD_WSTRING \
465  (!(GTEST_OS_LINUX_ANDROID || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS))
466 
467 #endif // GTEST_HAS_STD_WSTRING
468 
469 #ifndef GTEST_HAS_GLOBAL_WSTRING
470 // The user didn't tell us whether ::wstring is available, so we need
471 // to figure it out.
472 # define GTEST_HAS_GLOBAL_WSTRING \
473  (GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING)
474 #endif // GTEST_HAS_GLOBAL_WSTRING
475 
476 // Determines whether RTTI is available.
477 #ifndef GTEST_HAS_RTTI
478 // The user didn't tell us whether RTTI is enabled, so we need to
479 // figure it out.
480 
481 # ifdef _MSC_VER
482 
483 # ifdef _CPPRTTI // MSVC defines this macro iff RTTI is enabled.
484 # define GTEST_HAS_RTTI 1
485 # else
486 # define GTEST_HAS_RTTI 0
487 # endif
488 
489 // Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled.
490 # elif defined(__GNUC__)
491 
492 # ifdef __GXX_RTTI
493 // When building against STLport with the Android NDK and with
494 // -frtti -fno-exceptions, the build fails at link time with undefined
495 // references to __cxa_bad_typeid. Note sure if STL or toolchain bug,
496 // so disable RTTI when detected.
497 # if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR) && \
498  !defined(__EXCEPTIONS)
499 # define GTEST_HAS_RTTI 0
500 # else
501 # define GTEST_HAS_RTTI 1
502 # endif // GTEST_OS_LINUX_ANDROID && __STLPORT_MAJOR && !__EXCEPTIONS
503 # else
504 # define GTEST_HAS_RTTI 0
505 # endif // __GXX_RTTI
506 
507 // Clang defines __GXX_RTTI starting with version 3.0, but its manual recommends
508 // using has_feature instead. has_feature(cxx_rtti) is supported since 2.7, the
509 // first version with C++ support.
510 # elif defined(__clang__)
511 
512 # define GTEST_HAS_RTTI __has_feature(cxx_rtti)
513 
514 // Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if
515 // both the typeid and dynamic_cast features are present.
516 # elif defined(__IBMCPP__) && (__IBMCPP__ >= 900)
517 
518 # ifdef __RTTI_ALL__
519 # define GTEST_HAS_RTTI 1
520 # else
521 # define GTEST_HAS_RTTI 0
522 # endif
523 
524 # else
525 
526 // For all other compilers, we assume RTTI is enabled.
527 # define GTEST_HAS_RTTI 1
528 
529 # endif // _MSC_VER
530 
531 #endif // GTEST_HAS_RTTI
532 
533 // It's this header's responsibility to #include <typeinfo> when RTTI
534 // is enabled.
535 #if GTEST_HAS_RTTI
536 # include <typeinfo>
537 #endif
538 
539 // Determines whether Google Test can use the pthreads library.
540 #ifndef GTEST_HAS_PTHREAD
541 // The user didn't tell us explicitly, so we make reasonable assumptions about
542 // which platforms have pthreads support.
543 //
544 // To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0
545 // to your compiler flags.
546 #define GTEST_HAS_PTHREAD \
547  (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_HPUX || GTEST_OS_QNX || \
548  GTEST_OS_FREEBSD || GTEST_OS_NACL || GTEST_OS_NETBSD || GTEST_OS_FUCHSIA)
549 #endif // GTEST_HAS_PTHREAD
550 
551 #if GTEST_HAS_PTHREAD
552 // gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is
553 // true.
554 # include <pthread.h> // NOLINT
555 
556 // For timespec and nanosleep, used below.
557 # include <time.h> // NOLINT
558 #endif
559 
560 // Determines whether clone(2) is supported.
561 // Usually it will only be available on Linux, excluding
562 // Linux on the Itanium architecture.
563 // Also see http://linux.die.net/man/2/clone.
564 #ifndef GTEST_HAS_CLONE
565 // The user didn't tell us, so we need to figure it out.
566 
567 # if GTEST_OS_LINUX && !defined(__ia64__)
568 # if GTEST_OS_LINUX_ANDROID
569 // On Android, clone() became available at different API levels for each 32-bit
570 // architecture.
571 # if defined(__LP64__) || \
572  (defined(__arm__) && __ANDROID_API__ >= 9) || \
573  (defined(__mips__) && __ANDROID_API__ >= 12) || \
574  (defined(__i386__) && __ANDROID_API__ >= 17)
575 # define GTEST_HAS_CLONE 1
576 # else
577 # define GTEST_HAS_CLONE 0
578 # endif
579 # else
580 # define GTEST_HAS_CLONE 1
581 # endif
582 # else
583 # define GTEST_HAS_CLONE 0
584 # endif // GTEST_OS_LINUX && !defined(__ia64__)
585 
586 #endif // GTEST_HAS_CLONE
587 
588 // Determines whether to support stream redirection. This is used to test
589 // output correctness and to implement death tests.
590 #ifndef GTEST_HAS_STREAM_REDIRECTION
591 // By default, we assume that stream redirection is supported on all
592 // platforms except known mobile ones.
593 # if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT
594 # define GTEST_HAS_STREAM_REDIRECTION 0
595 # else
596 # define GTEST_HAS_STREAM_REDIRECTION 1
597 # endif // !GTEST_OS_WINDOWS_MOBILE
598 #endif // GTEST_HAS_STREAM_REDIRECTION
599 
600 // Determines whether to support death tests.
601 // pops up a dialog window that cannot be suppressed programmatically.
602 #if (GTEST_OS_LINUX || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \
603  (GTEST_OS_MAC && !GTEST_OS_IOS) || \
604  (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER) || \
605  GTEST_OS_WINDOWS_MINGW || GTEST_OS_AIX || GTEST_OS_HPUX || \
606  GTEST_OS_OPENBSD || GTEST_OS_QNX || GTEST_OS_FREEBSD || \
607  GTEST_OS_NETBSD || GTEST_OS_FUCHSIA)
608 # define GTEST_HAS_DEATH_TEST 1
609 #endif
610 
611 // Determines whether to support type-driven tests.
612 
613 // Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0,
614 // Sun Pro CC, IBM Visual Age, and HP aCC support.
615 #if defined(__GNUC__) || defined(_MSC_VER) || defined(__SUNPRO_CC) || \
616  defined(__IBMCPP__) || defined(__HP_aCC)
617 # define GTEST_HAS_TYPED_TEST 1
618 # define GTEST_HAS_TYPED_TEST_P 1
619 #endif
620 
621 // Determines whether the system compiler uses UTF-16 for encoding wide strings.
622 #define GTEST_WIDE_STRING_USES_UTF16_ \
623  (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_AIX || GTEST_OS_OS2)
624 
625 // Determines whether test results can be streamed to a socket.
626 #if GTEST_OS_LINUX
627 # define GTEST_CAN_STREAM_RESULTS_ 1
628 #endif
629 
630 // Defines some utility macros.
631 
632 // The GNU compiler emits a warning if nested "if" statements are followed by
633 // an "else" statement and braces are not used to explicitly disambiguate the
634 // "else" binding. This leads to problems with code like:
635 //
636 // if (gate)
637 // ASSERT_*(condition) << "Some message";
638 //
639 // The "switch (0) case 0:" idiom is used to suppress this.
640 #ifdef __INTEL_COMPILER
641 # define GTEST_AMBIGUOUS_ELSE_BLOCKER_
642 #else
643 # define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: default: // NOLINT
644 #endif
645 
646 // Use this annotation at the end of a struct/class definition to
647 // prevent the compiler from optimizing away instances that are never
648 // used. This is useful when all interesting logic happens inside the
649 // c'tor and / or d'tor. Example:
650 //
651 // struct Foo {
652 // Foo() { ... }
653 // } GTEST_ATTRIBUTE_UNUSED_;
654 //
655 // Also use it after a variable or parameter declaration to tell the
656 // compiler the variable/parameter does not have to be used.
657 #if defined(__GNUC__) && !defined(COMPILER_ICC)
658 # define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
659 #elif defined(__clang__)
660 # if __has_attribute(unused)
661 # define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
662 # endif
663 #endif
664 #ifndef GTEST_ATTRIBUTE_UNUSED_
665 # define GTEST_ATTRIBUTE_UNUSED_
666 #endif
667 
668 // Use this annotation before a function that takes a printf format string.
669 #if (defined(__GNUC__) || defined(__clang__)) && !defined(COMPILER_ICC)
670 # if defined(__MINGW_PRINTF_FORMAT)
671 // MinGW has two different printf implementations. Ensure the format macro
672 // matches the selected implementation. See
673 // https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/.
674 # define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \
675  __attribute__((__format__(__MINGW_PRINTF_FORMAT, string_index, \
676  first_to_check)))
677 # else
678 # define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \
679  __attribute__((__format__(__printf__, string_index, first_to_check)))
680 # endif
681 #else
682 # define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check)
683 #endif
684 
685 
686 // A macro to disallow operator=
687 // This should be used in the private: declarations for a class.
688 #define GTEST_DISALLOW_ASSIGN_(type) \
689  void operator=(type const &) = delete
690 
691 // A macro to disallow copy constructor and operator=
692 // This should be used in the private: declarations for a class.
693 #define GTEST_DISALLOW_COPY_AND_ASSIGN_(type) \
694  type(type const &) = delete; \
695  GTEST_DISALLOW_ASSIGN_(type)
696 
697 // Tell the compiler to warn about unused return values for functions declared
698 // with this macro. The macro should be used on function declarations
699 // following the argument list:
700 //
701 // Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_;
702 #if defined(__GNUC__) && !defined(COMPILER_ICC)
703 # define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))
704 #else
705 # define GTEST_MUST_USE_RESULT_
706 #endif // __GNUC__ && !COMPILER_ICC
707 
708 // MS C++ compiler emits warning when a conditional expression is compile time
709 // constant. In some contexts this warning is false positive and needs to be
710 // suppressed. Use the following two macros in such cases:
711 //
712 // GTEST_INTENTIONAL_CONST_COND_PUSH_()
713 // while (true) {
714 // GTEST_INTENTIONAL_CONST_COND_POP_()
715 // }
716 # define GTEST_INTENTIONAL_CONST_COND_PUSH_() \
717  GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127)
718 # define GTEST_INTENTIONAL_CONST_COND_POP_() \
719  GTEST_DISABLE_MSC_WARNINGS_POP_()
720 
721 // Determine whether the compiler supports Microsoft's Structured Exception
722 // Handling. This is supported by several Windows compilers but generally
723 // does not exist on any other system.
724 #ifndef GTEST_HAS_SEH
725 // The user didn't tell us, so we need to figure it out.
726 
727 # if defined(_MSC_VER) || defined(__BORLANDC__)
728 // These two compilers are known to support SEH.
729 # define GTEST_HAS_SEH 1
730 # else
731 // Assume no SEH.
732 # define GTEST_HAS_SEH 0
733 # endif
734 
735 #endif // GTEST_HAS_SEH
736 
737 #ifndef GTEST_IS_THREADSAFE
738 
739 #define GTEST_IS_THREADSAFE \
740  (GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ || \
741  (GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT) || \
742  GTEST_HAS_PTHREAD)
743 
744 #endif // GTEST_IS_THREADSAFE
745 
746 // GTEST_API_ qualifies all symbols that must be exported. The definitions below
747 // are guarded by #ifndef to give embedders a chance to define GTEST_API_ in
748 // gtest/internal/custom/gtest-port.h
749 #ifndef GTEST_API_
750 
751 #ifdef _MSC_VER
752 # if GTEST_LINKED_AS_SHARED_LIBRARY
753 # define GTEST_API_ __declspec(dllimport)
754 # elif GTEST_CREATE_SHARED_LIBRARY
755 # define GTEST_API_ __declspec(dllexport)
756 # endif
757 #elif __GNUC__ >= 4 || defined(__clang__)
758 # define GTEST_API_ __attribute__((visibility ("default")))
759 #endif // _MSC_VER
760 
761 #endif // GTEST_API_
762 
763 #ifndef GTEST_API_
764 # define GTEST_API_
765 #endif // GTEST_API_
766 
767 #ifndef GTEST_DEFAULT_DEATH_TEST_STYLE
768 # define GTEST_DEFAULT_DEATH_TEST_STYLE "fast"
769 #endif // GTEST_DEFAULT_DEATH_TEST_STYLE
770 
771 #ifdef __GNUC__
772 // Ask the compiler to never inline a given function.
773 # define GTEST_NO_INLINE_ __attribute__((noinline))
774 #else
775 # define GTEST_NO_INLINE_
776 #endif
777 
778 // _LIBCPP_VERSION is defined by the libc++ library from the LLVM project.
779 #if !defined(GTEST_HAS_CXXABI_H_)
780 # if defined(__GLIBCXX__) || (defined(_LIBCPP_VERSION) && !defined(_MSC_VER))
781 # define GTEST_HAS_CXXABI_H_ 1
782 # else
783 # define GTEST_HAS_CXXABI_H_ 0
784 # endif
785 #endif
786 
787 // A function level attribute to disable checking for use of uninitialized
788 // memory when built with MemorySanitizer.
789 #if defined(__clang__)
790 # if __has_feature(memory_sanitizer)
791 # define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ \
792  __attribute__((no_sanitize_memory))
793 # else
794 # define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
795 # endif // __has_feature(memory_sanitizer)
796 #else
797 # define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
798 #endif // __clang__
799 
800 // A function level attribute to disable AddressSanitizer instrumentation.
801 #if defined(__clang__)
802 # if __has_feature(address_sanitizer)
803 # define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ \
804  __attribute__((no_sanitize_address))
805 # else
806 # define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
807 # endif // __has_feature(address_sanitizer)
808 #else
809 # define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
810 #endif // __clang__
811 
812 // A function level attribute to disable ThreadSanitizer instrumentation.
813 #if defined(__clang__)
814 # if __has_feature(thread_sanitizer)
815 # define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ \
816  __attribute__((no_sanitize_thread))
817 # else
818 # define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
819 # endif // __has_feature(thread_sanitizer)
820 #else
821 # define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
822 #endif // __clang__
823 
824 namespace testing {
825 
826 class Message;
827 
828 // Legacy imports for backwards compatibility.
829 // New code should use std:: names directly.
830 using std::get;
831 using std::make_tuple;
832 using std::tuple;
833 using std::tuple_element;
834 using std::tuple_size;
835 
836 namespace internal {
837 
838 // A secret type that Google Test users don't know about. It has no
839 // definition on purpose. Therefore it's impossible to create a
840 // Secret object, which is what we want.
841 class Secret;
842 
843 // The GTEST_COMPILE_ASSERT_ is a legacy macro used to verify that a compile
844 // time expression is true (in new code, use static_assert instead). For
845 // example, you could use it to verify the size of a static array:
846 //
847 // GTEST_COMPILE_ASSERT_(GTEST_ARRAY_SIZE_(names) == NUM_NAMES,
848 // names_incorrect_size);
849 //
850 // The second argument to the macro must be a valid C++ identifier. If the
851 // expression is false, compiler will issue an error containing this identifier.
852 #define GTEST_COMPILE_ASSERT_(expr, msg) static_assert(expr, #msg)
853 
854 // StaticAssertTypeEqHelper is used by StaticAssertTypeEq defined in gtest.h.
855 //
856 // This template is declared, but intentionally undefined.
857 template <typename T1, typename T2>
859 
860 template <typename T>
862  enum { value = true };
863 };
864 
865 // Same as std::is_same<>.
866 template <typename T, typename U>
867 struct IsSame {
868  enum { value = false };
869 };
870 template <typename T>
871 struct IsSame<T, T> {
872  enum { value = true };
873 };
874 
875 // Evaluates to the number of elements in 'array'.
876 #define GTEST_ARRAY_SIZE_(array) (sizeof(array) / sizeof(array[0]))
877 
878 #if GTEST_HAS_GLOBAL_STRING
879 typedef ::string string;
880 #else
881 typedef ::std::string string;
882 #endif // GTEST_HAS_GLOBAL_STRING
883 
884 #if GTEST_HAS_GLOBAL_WSTRING
885 typedef ::wstring wstring;
886 #elif GTEST_HAS_STD_WSTRING
887 typedef ::std::wstring wstring;
888 #endif // GTEST_HAS_GLOBAL_WSTRING
889 
890 // A helper for suppressing warnings on constant condition. It just
891 // returns 'condition'.
892 GTEST_API_ bool IsTrue(bool condition);
893 
894 // Defines RE.
895 
896 #if GTEST_USES_PCRE
897 // if used, PCRE is injected by custom/gtest-port.h
898 #elif GTEST_USES_POSIX_RE || GTEST_USES_SIMPLE_RE
899 
900 // A simple C++ wrapper for <regex.h>. It uses the POSIX Extended
901 // Regular Expression syntax.
902 class GTEST_API_ RE {
903  public:
904  // A copy constructor is required by the Standard to initialize object
905  // references from r-values.
906  RE(const RE& other) { Init(other.pattern()); }
907 
908  // Constructs an RE from a string.
909  RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT
910 
911 # if GTEST_HAS_GLOBAL_STRING
912 
913  RE(const ::string& regex) { Init(regex.c_str()); } // NOLINT
914 
915 # endif // GTEST_HAS_GLOBAL_STRING
916 
917  RE(const char* regex) { Init(regex); } // NOLINT
918  ~RE();
919 
920  // Returns the string representation of the regex.
921  const char* pattern() const { return pattern_; }
922 
923  // FullMatch(str, re) returns true iff regular expression re matches
924  // the entire str.
925  // PartialMatch(str, re) returns true iff regular expression re
926  // matches a substring of str (including str itself).
927  static bool FullMatch(const ::std::string& str, const RE& re) {
928  return FullMatch(str.c_str(), re);
929  }
930  static bool PartialMatch(const ::std::string& str, const RE& re) {
931  return PartialMatch(str.c_str(), re);
932  }
933 
934 # if GTEST_HAS_GLOBAL_STRING
935 
936  static bool FullMatch(const ::string& str, const RE& re) {
937  return FullMatch(str.c_str(), re);
938  }
939  static bool PartialMatch(const ::string& str, const RE& re) {
940  return PartialMatch(str.c_str(), re);
941  }
942 
943 # endif // GTEST_HAS_GLOBAL_STRING
944 
945  static bool FullMatch(const char* str, const RE& re);
946  static bool PartialMatch(const char* str, const RE& re);
947 
948  private:
949  void Init(const char* regex);
950  const char* pattern_;
951  bool is_valid_;
952 
953 # if GTEST_USES_POSIX_RE
954 
955  regex_t full_regex_; // For FullMatch().
956  regex_t partial_regex_; // For PartialMatch().
957 
958 # else // GTEST_USES_SIMPLE_RE
959 
960  const char* full_pattern_; // For FullMatch();
961 
962 # endif
963 
964  GTEST_DISALLOW_ASSIGN_(RE);
965 };
966 
967 #endif // GTEST_USES_PCRE
968 
969 // Formats a source file path and a line number as they would appear
970 // in an error message from the compiler used to compile this code.
971 GTEST_API_ ::std::string FormatFileLocation(const char* file, int line);
972 
973 // Formats a file location for compiler-independent XML output.
974 // Although this function is not platform dependent, we put it next to
975 // FormatFileLocation in order to contrast the two functions.
976 GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file,
977  int line);
978 
979 // Defines logging utilities:
980 // GTEST_LOG_(severity) - logs messages at the specified severity level. The
981 // message itself is streamed into the macro.
982 // LogToStderr() - directs all log messages to stderr.
983 // FlushInfoLog() - flushes informational log messages.
984 
985 enum GTestLogSeverity {
986  GTEST_INFO,
987  GTEST_WARNING,
988  GTEST_ERROR,
989  GTEST_FATAL
990 };
991 
992 // Formats log entry severity, provides a stream object for streaming the
993 // log message, and terminates the message with a newline when going out of
994 // scope.
995 class GTEST_API_ GTestLog {
996  public:
997  GTestLog(GTestLogSeverity severity, const char* file, int line);
998 
999  // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
1000  ~GTestLog();
1001 
1002  ::std::ostream& GetStream() { return ::std::cerr; }
1003 
1004  private:
1005  const GTestLogSeverity severity_;
1006 
1007  GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog);
1008 };
1009 
1010 #if !defined(GTEST_LOG_)
1011 
1012 # define GTEST_LOG_(severity) \
1013  ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
1014  __FILE__, __LINE__).GetStream()
1015 
1016 inline void LogToStderr() {}
1017 inline void FlushInfoLog() { fflush(nullptr); }
1018 
1019 #endif // !defined(GTEST_LOG_)
1020 
1021 #if !defined(GTEST_CHECK_)
1022 // INTERNAL IMPLEMENTATION - DO NOT USE.
1023 //
1024 // GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition
1025 // is not satisfied.
1026 // Synopsys:
1027 // GTEST_CHECK_(boolean_condition);
1028 // or
1029 // GTEST_CHECK_(boolean_condition) << "Additional message";
1030 //
1031 // This checks the condition and if the condition is not satisfied
1032 // it prints message about the condition violation, including the
1033 // condition itself, plus additional message streamed into it, if any,
1034 // and then it aborts the program. It aborts the program irrespective of
1035 // whether it is built in the debug mode or not.
1036 # define GTEST_CHECK_(condition) \
1037  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
1038  if (::testing::internal::IsTrue(condition)) \
1039  ; \
1040  else \
1041  GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
1042 #endif // !defined(GTEST_CHECK_)
1043 
1044 // An all-mode assert to verify that the given POSIX-style function
1045 // call returns 0 (indicating success). Known limitation: this
1046 // doesn't expand to a balanced 'if' statement, so enclose the macro
1047 // in {} if you need to use it as the only statement in an 'if'
1048 // branch.
1049 #define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \
1050  if (const int gtest_error = (posix_call)) \
1051  GTEST_LOG_(FATAL) << #posix_call << "failed with error " \
1052  << gtest_error
1053 
1054 // Adds reference to a type if it is not a reference type,
1055 // otherwise leaves it unchanged. This is the same as
1056 // tr1::add_reference, which is not widely available yet.
1057 template <typename T>
1058 struct AddReference { typedef T& type; }; // NOLINT
1059 template <typename T>
1060 struct AddReference<T&> { typedef T& type; }; // NOLINT
1061 
1062 // A handy wrapper around AddReference that works when the argument T
1063 // depends on template parameters.
1064 #define GTEST_ADD_REFERENCE_(T) \
1065  typename ::testing::internal::AddReference<T>::type
1066 
1067 // Transforms "T" into "const T&" according to standard reference collapsing
1068 // rules (this is only needed as a backport for C++98 compilers that do not
1069 // support reference collapsing). Specifically, it transforms:
1070 //
1071 // char ==> const char&
1072 // const char ==> const char&
1073 // char& ==> char&
1074 // const char& ==> const char&
1075 //
1076 // Note that the non-const reference will not have "const" added. This is
1077 // standard, and necessary so that "T" can always bind to "const T&".
1078 template <typename T>
1079 struct ConstRef { typedef const T& type; };
1080 template <typename T>
1081 struct ConstRef<T&> { typedef T& type; };
1082 
1083 // The argument T must depend on some template parameters.
1084 #define GTEST_REFERENCE_TO_CONST_(T) \
1085  typename ::testing::internal::ConstRef<T>::type
1086 
1087 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
1088 //
1089 // Use ImplicitCast_ as a safe version of static_cast for upcasting in
1090 // the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a
1091 // const Foo*). When you use ImplicitCast_, the compiler checks that
1092 // the cast is safe. Such explicit ImplicitCast_s are necessary in
1093 // surprisingly many situations where C++ demands an exact type match
1094 // instead of an argument type convertable to a target type.
1095 //
1096 // The syntax for using ImplicitCast_ is the same as for static_cast:
1097 //
1098 // ImplicitCast_<ToType>(expr)
1099 //
1100 // ImplicitCast_ would have been part of the C++ standard library,
1101 // but the proposal was submitted too late. It will probably make
1102 // its way into the language in the future.
1103 //
1104 // This relatively ugly name is intentional. It prevents clashes with
1105 // similar functions users may have (e.g., implicit_cast). The internal
1106 // namespace alone is not enough because the function can be found by ADL.
1107 template<typename To>
1108 inline To ImplicitCast_(To x) { return x; }
1109 
1110 // When you upcast (that is, cast a pointer from type Foo to type
1111 // SuperclassOfFoo), it's fine to use ImplicitCast_<>, since upcasts
1112 // always succeed. When you downcast (that is, cast a pointer from
1113 // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
1114 // how do you know the pointer is really of type SubclassOfFoo? It
1115 // could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
1116 // when you downcast, you should use this macro. In debug mode, we
1117 // use dynamic_cast<> to double-check the downcast is legal (we die
1118 // if it's not). In normal mode, we do the efficient static_cast<>
1119 // instead. Thus, it's important to test in debug mode to make sure
1120 // the cast is legal!
1121 // This is the only place in the code we should use dynamic_cast<>.
1122 // In particular, you SHOULDN'T be using dynamic_cast<> in order to
1123 // do RTTI (eg code like this:
1124 // if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
1125 // if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
1126 // You should design the code some other way not to need this.
1127 //
1128 // This relatively ugly name is intentional. It prevents clashes with
1129 // similar functions users may have (e.g., down_cast). The internal
1130 // namespace alone is not enough because the function can be found by ADL.
1131 template<typename To, typename From> // use like this: DownCast_<T*>(foo);
1132 inline To DownCast_(From* f) { // so we only accept pointers
1133  // Ensures that To is a sub-type of From *. This test is here only
1134  // for compile-time type checking, and has no overhead in an
1135  // optimized build at run-time, as it will be optimized away
1136  // completely.
1137  GTEST_INTENTIONAL_CONST_COND_PUSH_()
1138  if (false) {
1139  GTEST_INTENTIONAL_CONST_COND_POP_()
1140  const To to = nullptr;
1141  ::testing::internal::ImplicitCast_<From*>(to);
1142  }
1143 
1144 #if GTEST_HAS_RTTI
1145  // RTTI: debug mode only!
1146  GTEST_CHECK_(f == nullptr || dynamic_cast<To>(f) != nullptr);
1147 #endif
1148  return static_cast<To>(f);
1149 }
1150 
1151 // Downcasts the pointer of type Base to Derived.
1152 // Derived must be a subclass of Base. The parameter MUST
1153 // point to a class of type Derived, not any subclass of it.
1154 // When RTTI is available, the function performs a runtime
1155 // check to enforce this.
1156 template <class Derived, class Base>
1157 Derived* CheckedDowncastToActualType(Base* base) {
1158 #if GTEST_HAS_RTTI
1159  GTEST_CHECK_(typeid(*base) == typeid(Derived));
1160 #endif
1161 
1162 #if GTEST_HAS_DOWNCAST_
1163  return ::down_cast<Derived*>(base);
1164 #elif GTEST_HAS_RTTI
1165  return dynamic_cast<Derived*>(base); // NOLINT
1166 #else
1167  return static_cast<Derived*>(base); // Poor man's downcast.
1168 #endif
1169 }
1170 
1171 #if GTEST_HAS_STREAM_REDIRECTION
1172 
1173 // Defines the stderr capturer:
1174 // CaptureStdout - starts capturing stdout.
1175 // GetCapturedStdout - stops capturing stdout and returns the captured string.
1176 // CaptureStderr - starts capturing stderr.
1177 // GetCapturedStderr - stops capturing stderr and returns the captured string.
1178 //
1179 GTEST_API_ void CaptureStdout();
1180 GTEST_API_ std::string GetCapturedStdout();
1181 GTEST_API_ void CaptureStderr();
1182 GTEST_API_ std::string GetCapturedStderr();
1183 
1184 #endif // GTEST_HAS_STREAM_REDIRECTION
1185 // Returns the size (in bytes) of a file.
1186 GTEST_API_ size_t GetFileSize(FILE* file);
1187 
1188 // Reads the entire content of a file as a string.
1189 GTEST_API_ std::string ReadEntireFile(FILE* file);
1190 
1191 // All command line arguments.
1192 GTEST_API_ std::vector<std::string> GetArgvs();
1193 
1194 #if GTEST_HAS_DEATH_TEST
1195 
1196 std::vector<std::string> GetInjectableArgvs();
1197 // Deprecated: pass the args vector by value instead.
1198 void SetInjectableArgvs(const std::vector<std::string>* new_argvs);
1199 void SetInjectableArgvs(const std::vector<std::string>& new_argvs);
1200 #if GTEST_HAS_GLOBAL_STRING
1201 void SetInjectableArgvs(const std::vector< ::string>& new_argvs);
1202 #endif // GTEST_HAS_GLOBAL_STRING
1203 void ClearInjectableArgvs();
1204 
1205 #endif // GTEST_HAS_DEATH_TEST
1206 
1207 // Defines synchronization primitives.
1208 #if GTEST_IS_THREADSAFE
1209 # if GTEST_HAS_PTHREAD
1210 // Sleeps for (roughly) n milliseconds. This function is only for testing
1211 // Google Test's own constructs. Don't use it in user tests, either
1212 // directly or indirectly.
1213 inline void SleepMilliseconds(int n) {
1214  const timespec time = {
1215  0, // 0 seconds.
1216  n * 1000L * 1000L, // And n ms.
1217  };
1218  nanosleep(&time, nullptr);
1219 }
1220 # endif // GTEST_HAS_PTHREAD
1221 
1222 # if GTEST_HAS_NOTIFICATION_
1223 // Notification has already been imported into the namespace.
1224 // Nothing to do here.
1225 
1226 # elif GTEST_HAS_PTHREAD
1227 // Allows a controller thread to pause execution of newly created
1228 // threads until notified. Instances of this class must be created
1229 // and destroyed in the controller thread.
1230 //
1231 // This class is only for testing Google Test's own constructs. Do not
1232 // use it in user tests, either directly or indirectly.
1233 class Notification {
1234  public:
1235  Notification() : notified_(false) {
1236  GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, nullptr));
1237  }
1238  ~Notification() {
1239  pthread_mutex_destroy(&mutex_);
1240  }
1241 
1242  // Notifies all threads created with this notification to start. Must
1243  // be called from the controller thread.
1244  void Notify() {
1245  pthread_mutex_lock(&mutex_);
1246  notified_ = true;
1247  pthread_mutex_unlock(&mutex_);
1248  }
1249 
1250  // Blocks until the controller thread notifies. Must be called from a test
1251  // thread.
1252  void WaitForNotification() {
1253  for (;;) {
1254  pthread_mutex_lock(&mutex_);
1255  const bool notified = notified_;
1256  pthread_mutex_unlock(&mutex_);
1257  if (notified)
1258  break;
1259  SleepMilliseconds(10);
1260  }
1261  }
1262 
1263  private:
1264  pthread_mutex_t mutex_;
1265  bool notified_;
1266 
1267  GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
1268 };
1269 
1270 # elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
1271 
1272 GTEST_API_ void SleepMilliseconds(int n);
1273 
1274 // Provides leak-safe Windows kernel handle ownership.
1275 // Used in death tests and in threading support.
1276 class GTEST_API_ AutoHandle {
1277  public:
1278  // Assume that Win32 HANDLE type is equivalent to void*. Doing so allows us to
1279  // avoid including <windows.h> in this header file. Including <windows.h> is
1280  // undesirable because it defines a lot of symbols and macros that tend to
1281  // conflict with client code. This assumption is verified by
1282  // WindowsTypesTest.HANDLEIsVoidStar.
1283  typedef void* Handle;
1284  AutoHandle();
1285  explicit AutoHandle(Handle handle);
1286 
1287  ~AutoHandle();
1288 
1289  Handle Get() const;
1290  void Reset();
1291  void Reset(Handle handle);
1292 
1293  private:
1294  // Returns true iff the handle is a valid handle object that can be closed.
1295  bool IsCloseable() const;
1296 
1297  Handle handle_;
1298 
1299  GTEST_DISALLOW_COPY_AND_ASSIGN_(AutoHandle);
1300 };
1301 
1302 // Allows a controller thread to pause execution of newly created
1303 // threads until notified. Instances of this class must be created
1304 // and destroyed in the controller thread.
1305 //
1306 // This class is only for testing Google Test's own constructs. Do not
1307 // use it in user tests, either directly or indirectly.
1308 class GTEST_API_ Notification {
1309  public:
1310  Notification();
1311  void Notify();
1312  void WaitForNotification();
1313 
1314  private:
1315  AutoHandle event_;
1316 
1317  GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
1318 };
1319 # endif // GTEST_HAS_NOTIFICATION_
1320 
1321 // On MinGW, we can have both GTEST_OS_WINDOWS and GTEST_HAS_PTHREAD
1322 // defined, but we don't want to use MinGW's pthreads implementation, which
1323 // has conformance problems with some versions of the POSIX standard.
1324 # if GTEST_HAS_PTHREAD && !GTEST_OS_WINDOWS_MINGW
1325 
1326 // As a C-function, ThreadFuncWithCLinkage cannot be templated itself.
1327 // Consequently, it cannot select a correct instantiation of ThreadWithParam
1328 // in order to call its Run(). Introducing ThreadWithParamBase as a
1329 // non-templated base class for ThreadWithParam allows us to bypass this
1330 // problem.
1331 class ThreadWithParamBase {
1332  public:
1333  virtual ~ThreadWithParamBase() {}
1334  virtual void Run() = 0;
1335 };
1336 
1337 // pthread_create() accepts a pointer to a function type with the C linkage.
1338 // According to the Standard (7.5/1), function types with different linkages
1339 // are different even if they are otherwise identical. Some compilers (for
1340 // example, SunStudio) treat them as different types. Since class methods
1341 // cannot be defined with C-linkage we need to define a free C-function to
1342 // pass into pthread_create().
1343 extern "C" inline void* ThreadFuncWithCLinkage(void* thread) {
1344  static_cast<ThreadWithParamBase*>(thread)->Run();
1345  return nullptr;
1346 }
1347 
1348 // Helper class for testing Google Test's multi-threading constructs.
1349 // To use it, write:
1350 //
1351 // void ThreadFunc(int param) { /* Do things with param */ }
1352 // Notification thread_can_start;
1353 // ...
1354 // // The thread_can_start parameter is optional; you can supply NULL.
1355 // ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start);
1356 // thread_can_start.Notify();
1357 //
1358 // These classes are only for testing Google Test's own constructs. Do
1359 // not use them in user tests, either directly or indirectly.
1360 template <typename T>
1361 class ThreadWithParam : public ThreadWithParamBase {
1362  public:
1363  typedef void UserThreadFunc(T);
1364 
1365  ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start)
1366  : func_(func),
1367  param_(param),
1368  thread_can_start_(thread_can_start),
1369  finished_(false) {
1370  ThreadWithParamBase* const base = this;
1371  // The thread can be created only after all fields except thread_
1372  // have been initialized.
1373  GTEST_CHECK_POSIX_SUCCESS_(
1374  pthread_create(&thread_, nullptr, &ThreadFuncWithCLinkage, base));
1375  }
1376  ~ThreadWithParam() override { Join(); }
1377 
1378  void Join() {
1379  if (!finished_) {
1380  GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, nullptr));
1381  finished_ = true;
1382  }
1383  }
1384 
1385  void Run() override {
1386  if (thread_can_start_ != nullptr) thread_can_start_->WaitForNotification();
1387  func_(param_);
1388  }
1389 
1390  private:
1391  UserThreadFunc* const func_; // User-supplied thread function.
1392  const T param_; // User-supplied parameter to the thread function.
1393  // When non-NULL, used to block execution until the controller thread
1394  // notifies.
1395  Notification* const thread_can_start_;
1396  bool finished_; // true iff we know that the thread function has finished.
1397  pthread_t thread_; // The native thread object.
1398 
1399  GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam);
1400 };
1401 # endif // !GTEST_OS_WINDOWS && GTEST_HAS_PTHREAD ||
1402  // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
1403 
1404 # if GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
1405 // Mutex and ThreadLocal have already been imported into the namespace.
1406 // Nothing to do here.
1407 
1408 # elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
1409 
1410 // Mutex implements mutex on Windows platforms. It is used in conjunction
1411 // with class MutexLock:
1412 //
1413 // Mutex mutex;
1414 // ...
1415 // MutexLock lock(&mutex); // Acquires the mutex and releases it at the
1416 // // end of the current scope.
1417 //
1418 // A static Mutex *must* be defined or declared using one of the following
1419 // macros:
1420 // GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex);
1421 // GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex);
1422 //
1423 // (A non-static Mutex is defined/declared in the usual way).
1424 class GTEST_API_ Mutex {
1425  public:
1426  enum MutexType { kStatic = 0, kDynamic = 1 };
1427  // We rely on kStaticMutex being 0 as it is to what the linker initializes
1428  // type_ in static mutexes. critical_section_ will be initialized lazily
1429  // in ThreadSafeLazyInit().
1430  enum StaticConstructorSelector { kStaticMutex = 0 };
1431 
1432  // This constructor intentionally does nothing. It relies on type_ being
1433  // statically initialized to 0 (effectively setting it to kStatic) and on
1434  // ThreadSafeLazyInit() to lazily initialize the rest of the members.
1435  explicit Mutex(StaticConstructorSelector /*dummy*/) {}
1436 
1437  Mutex();
1438  ~Mutex();
1439 
1440  void Lock();
1441 
1442  void Unlock();
1443 
1444  // Does nothing if the current thread holds the mutex. Otherwise, crashes
1445  // with high probability.
1446  void AssertHeld();
1447 
1448  private:
1449  // Initializes owner_thread_id_ and critical_section_ in static mutexes.
1450  void ThreadSafeLazyInit();
1451 
1452  // Per https://blogs.msdn.microsoft.com/oldnewthing/20040223-00/?p=40503,
1453  // we assume that 0 is an invalid value for thread IDs.
1454  unsigned int owner_thread_id_;
1455 
1456  // For static mutexes, we rely on these members being initialized to zeros
1457  // by the linker.
1458  MutexType type_;
1459  long critical_section_init_phase_; // NOLINT
1460  GTEST_CRITICAL_SECTION* critical_section_;
1461 
1462  GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex);
1463 };
1464 
1465 # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1466  extern ::testing::internal::Mutex mutex
1467 
1468 # define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1469  ::testing::internal::Mutex mutex(::testing::internal::Mutex::kStaticMutex)
1470 
1471 // We cannot name this class MutexLock because the ctor declaration would
1472 // conflict with a macro named MutexLock, which is defined on some
1473 // platforms. That macro is used as a defensive measure to prevent against
1474 // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than
1475 // "MutexLock l(&mu)". Hence the typedef trick below.
1476 class GTestMutexLock {
1477  public:
1478  explicit GTestMutexLock(Mutex* mutex)
1479  : mutex_(mutex) { mutex_->Lock(); }
1480 
1481  ~GTestMutexLock() { mutex_->Unlock(); }
1482 
1483  private:
1484  Mutex* const mutex_;
1485 
1486  GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock);
1487 };
1488 
1489 typedef GTestMutexLock MutexLock;
1490 
1491 // Base class for ValueHolder<T>. Allows a caller to hold and delete a value
1492 // without knowing its type.
1493 class ThreadLocalValueHolderBase {
1494  public:
1495  virtual ~ThreadLocalValueHolderBase() {}
1496 };
1497 
1498 // Provides a way for a thread to send notifications to a ThreadLocal
1499 // regardless of its parameter type.
1500 class ThreadLocalBase {
1501  public:
1502  // Creates a new ValueHolder<T> object holding a default value passed to
1503  // this ThreadLocal<T>'s constructor and returns it. It is the caller's
1504  // responsibility not to call this when the ThreadLocal<T> instance already
1505  // has a value on the current thread.
1506  virtual ThreadLocalValueHolderBase* NewValueForCurrentThread() const = 0;
1507 
1508  protected:
1509  ThreadLocalBase() {}
1510  virtual ~ThreadLocalBase() {}
1511 
1512  private:
1513  GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocalBase);
1514 };
1515 
1516 // Maps a thread to a set of ThreadLocals that have values instantiated on that
1517 // thread and notifies them when the thread exits. A ThreadLocal instance is
1518 // expected to persist until all threads it has values on have terminated.
1519 class GTEST_API_ ThreadLocalRegistry {
1520  public:
1521  // Registers thread_local_instance as having value on the current thread.
1522  // Returns a value that can be used to identify the thread from other threads.
1523  static ThreadLocalValueHolderBase* GetValueOnCurrentThread(
1524  const ThreadLocalBase* thread_local_instance);
1525 
1526  // Invoked when a ThreadLocal instance is destroyed.
1527  static void OnThreadLocalDestroyed(
1528  const ThreadLocalBase* thread_local_instance);
1529 };
1530 
1531 class GTEST_API_ ThreadWithParamBase {
1532  public:
1533  void Join();
1534 
1535  protected:
1536  class Runnable {
1537  public:
1538  virtual ~Runnable() {}
1539  virtual void Run() = 0;
1540  };
1541 
1542  ThreadWithParamBase(Runnable *runnable, Notification* thread_can_start);
1543  virtual ~ThreadWithParamBase();
1544 
1545  private:
1546  AutoHandle thread_;
1547 };
1548 
1549 // Helper class for testing Google Test's multi-threading constructs.
1550 template <typename T>
1551 class ThreadWithParam : public ThreadWithParamBase {
1552  public:
1553  typedef void UserThreadFunc(T);
1554 
1555  ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start)
1556  : ThreadWithParamBase(new RunnableImpl(func, param), thread_can_start) {
1557  }
1558  virtual ~ThreadWithParam() {}
1559 
1560  private:
1561  class RunnableImpl : public Runnable {
1562  public:
1563  RunnableImpl(UserThreadFunc* func, T param)
1564  : func_(func),
1565  param_(param) {
1566  }
1567  virtual ~RunnableImpl() {}
1568  virtual void Run() {
1569  func_(param_);
1570  }
1571 
1572  private:
1573  UserThreadFunc* const func_;
1574  const T param_;
1575 
1576  GTEST_DISALLOW_COPY_AND_ASSIGN_(RunnableImpl);
1577  };
1578 
1579  GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam);
1580 };
1581 
1582 // Implements thread-local storage on Windows systems.
1583 //
1584 // // Thread 1
1585 // ThreadLocal<int> tl(100); // 100 is the default value for each thread.
1586 //
1587 // // Thread 2
1588 // tl.set(150); // Changes the value for thread 2 only.
1589 // EXPECT_EQ(150, tl.get());
1590 //
1591 // // Thread 1
1592 // EXPECT_EQ(100, tl.get()); // In thread 1, tl has the original value.
1593 // tl.set(200);
1594 // EXPECT_EQ(200, tl.get());
1595 //
1596 // The template type argument T must have a public copy constructor.
1597 // In addition, the default ThreadLocal constructor requires T to have
1598 // a public default constructor.
1599 //
1600 // The users of a TheadLocal instance have to make sure that all but one
1601 // threads (including the main one) using that instance have exited before
1602 // destroying it. Otherwise, the per-thread objects managed for them by the
1603 // ThreadLocal instance are not guaranteed to be destroyed on all platforms.
1604 //
1605 // Google Test only uses global ThreadLocal objects. That means they
1606 // will die after main() has returned. Therefore, no per-thread
1607 // object managed by Google Test will be leaked as long as all threads
1608 // using Google Test have exited when main() returns.
1609 template <typename T>
1610 class ThreadLocal : public ThreadLocalBase {
1611  public:
1612  ThreadLocal() : default_factory_(new DefaultValueHolderFactory()) {}
1613  explicit ThreadLocal(const T& value)
1614  : default_factory_(new InstanceValueHolderFactory(value)) {}
1615 
1616  ~ThreadLocal() { ThreadLocalRegistry::OnThreadLocalDestroyed(this); }
1617 
1618  T* pointer() { return GetOrCreateValue(); }
1619  const T* pointer() const { return GetOrCreateValue(); }
1620  const T& get() const { return *pointer(); }
1621  void set(const T& value) { *pointer() = value; }
1622 
1623  private:
1624  // Holds a value of T. Can be deleted via its base class without the caller
1625  // knowing the type of T.
1626  class ValueHolder : public ThreadLocalValueHolderBase {
1627  public:
1628  ValueHolder() : value_() {}
1629  explicit ValueHolder(const T& value) : value_(value) {}
1630 
1631  T* pointer() { return &value_; }
1632 
1633  private:
1634  T value_;
1635  GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder);
1636  };
1637 
1638 
1639  T* GetOrCreateValue() const {
1640  return static_cast<ValueHolder*>(
1641  ThreadLocalRegistry::GetValueOnCurrentThread(this))->pointer();
1642  }
1643 
1644  virtual ThreadLocalValueHolderBase* NewValueForCurrentThread() const {
1645  return default_factory_->MakeNewHolder();
1646  }
1647 
1648  class ValueHolderFactory {
1649  public:
1650  ValueHolderFactory() {}
1651  virtual ~ValueHolderFactory() {}
1652  virtual ValueHolder* MakeNewHolder() const = 0;
1653 
1654  private:
1655  GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolderFactory);
1656  };
1657 
1658  class DefaultValueHolderFactory : public ValueHolderFactory {
1659  public:
1660  DefaultValueHolderFactory() {}
1661  virtual ValueHolder* MakeNewHolder() const { return new ValueHolder(); }
1662 
1663  private:
1664  GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultValueHolderFactory);
1665  };
1666 
1667  class InstanceValueHolderFactory : public ValueHolderFactory {
1668  public:
1669  explicit InstanceValueHolderFactory(const T& value) : value_(value) {}
1670  virtual ValueHolder* MakeNewHolder() const {
1671  return new ValueHolder(value_);
1672  }
1673 
1674  private:
1675  const T value_; // The value for each thread.
1676 
1677  GTEST_DISALLOW_COPY_AND_ASSIGN_(InstanceValueHolderFactory);
1678  };
1679 
1680  std::unique_ptr<ValueHolderFactory> default_factory_;
1681 
1682  GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal);
1683 };
1684 
1685 # elif GTEST_HAS_PTHREAD
1686 
1687 // MutexBase and Mutex implement mutex on pthreads-based platforms.
1688 class MutexBase {
1689  public:
1690  // Acquires this mutex.
1691  void Lock() {
1692  GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_));
1693  owner_ = pthread_self();
1694  has_owner_ = true;
1695  }
1696 
1697  // Releases this mutex.
1698  void Unlock() {
1699  // Since the lock is being released the owner_ field should no longer be
1700  // considered valid. We don't protect writing to has_owner_ here, as it's
1701  // the caller's responsibility to ensure that the current thread holds the
1702  // mutex when this is called.
1703  has_owner_ = false;
1704  GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_));
1705  }
1706 
1707  // Does nothing if the current thread holds the mutex. Otherwise, crashes
1708  // with high probability.
1709  void AssertHeld() const {
1710  GTEST_CHECK_(has_owner_ && pthread_equal(owner_, pthread_self()))
1711  << "The current thread is not holding the mutex @" << this;
1712  }
1713 
1714  // A static mutex may be used before main() is entered. It may even
1715  // be used before the dynamic initialization stage. Therefore we
1716  // must be able to initialize a static mutex object at link time.
1717  // This means MutexBase has to be a POD and its member variables
1718  // have to be public.
1719  public:
1720  pthread_mutex_t mutex_; // The underlying pthread mutex.
1721  // has_owner_ indicates whether the owner_ field below contains a valid thread
1722  // ID and is therefore safe to inspect (e.g., to use in pthread_equal()). All
1723  // accesses to the owner_ field should be protected by a check of this field.
1724  // An alternative might be to memset() owner_ to all zeros, but there's no
1725  // guarantee that a zero'd pthread_t is necessarily invalid or even different
1726  // from pthread_self().
1727  bool has_owner_;
1728  pthread_t owner_; // The thread holding the mutex.
1729 };
1730 
1731 // Forward-declares a static mutex.
1732 # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1733  extern ::testing::internal::MutexBase mutex
1734 
1735 // Defines and statically (i.e. at link time) initializes a static mutex.
1736 // The initialization list here does not explicitly initialize each field,
1737 // instead relying on default initialization for the unspecified fields. In
1738 // particular, the owner_ field (a pthread_t) is not explicitly initialized.
1739 // This allows initialization to work whether pthread_t is a scalar or struct.
1740 // The flag -Wmissing-field-initializers must not be specified for this to work.
1741 #define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1742  ::testing::internal::MutexBase mutex = {PTHREAD_MUTEX_INITIALIZER, false, 0}
1743 
1744 // The Mutex class can only be used for mutexes created at runtime. It
1745 // shares its API with MutexBase otherwise.
1746 class Mutex : public MutexBase {
1747  public:
1748  Mutex() {
1749  GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, nullptr));
1750  has_owner_ = false;
1751  }
1752  ~Mutex() {
1753  GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_));
1754  }
1755 
1756  private:
1757  GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex);
1758 };
1759 
1760 // We cannot name this class MutexLock because the ctor declaration would
1761 // conflict with a macro named MutexLock, which is defined on some
1762 // platforms. That macro is used as a defensive measure to prevent against
1763 // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than
1764 // "MutexLock l(&mu)". Hence the typedef trick below.
1765 class GTestMutexLock {
1766  public:
1767  explicit GTestMutexLock(MutexBase* mutex)
1768  : mutex_(mutex) { mutex_->Lock(); }
1769 
1770  ~GTestMutexLock() { mutex_->Unlock(); }
1771 
1772  private:
1773  MutexBase* const mutex_;
1774 
1775  GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock);
1776 };
1777 
1778 typedef GTestMutexLock MutexLock;
1779 
1780 // Helpers for ThreadLocal.
1781 
1782 // pthread_key_create() requires DeleteThreadLocalValue() to have
1783 // C-linkage. Therefore it cannot be templatized to access
1784 // ThreadLocal<T>. Hence the need for class
1785 // ThreadLocalValueHolderBase.
1786 class ThreadLocalValueHolderBase {
1787  public:
1788  virtual ~ThreadLocalValueHolderBase() {}
1789 };
1790 
1791 // Called by pthread to delete thread-local data stored by
1792 // pthread_setspecific().
1793 extern "C" inline void DeleteThreadLocalValue(void* value_holder) {
1794  delete static_cast<ThreadLocalValueHolderBase*>(value_holder);
1795 }
1796 
1797 // Implements thread-local storage on pthreads-based systems.
1798 template <typename T>
1799 class GTEST_API_ ThreadLocal {
1800  public:
1801  ThreadLocal()
1802  : key_(CreateKey()), default_factory_(new DefaultValueHolderFactory()) {}
1803  explicit ThreadLocal(const T& value)
1804  : key_(CreateKey()),
1805  default_factory_(new InstanceValueHolderFactory(value)) {}
1806 
1807  ~ThreadLocal() {
1808  // Destroys the managed object for the current thread, if any.
1809  DeleteThreadLocalValue(pthread_getspecific(key_));
1810 
1811  // Releases resources associated with the key. This will *not*
1812  // delete managed objects for other threads.
1813  GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_));
1814  }
1815 
1816  T* pointer() { return GetOrCreateValue(); }
1817  const T* pointer() const { return GetOrCreateValue(); }
1818  const T& get() const { return *pointer(); }
1819  void set(const T& value) { *pointer() = value; }
1820 
1821  private:
1822  // Holds a value of type T.
1823  class ValueHolder : public ThreadLocalValueHolderBase {
1824  public:
1825  ValueHolder() : value_() {}
1826  explicit ValueHolder(const T& value) : value_(value) {}
1827 
1828  T* pointer() { return &value_; }
1829 
1830  private:
1831  T value_;
1832  GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder);
1833  };
1834 
1835  static pthread_key_t CreateKey() {
1836  pthread_key_t key;
1837  // When a thread exits, DeleteThreadLocalValue() will be called on
1838  // the object managed for that thread.
1839  GTEST_CHECK_POSIX_SUCCESS_(
1840  pthread_key_create(&key, &DeleteThreadLocalValue));
1841  return key;
1842  }
1843 
1844  T* GetOrCreateValue() const {
1845  ThreadLocalValueHolderBase* const holder =
1846  static_cast<ThreadLocalValueHolderBase*>(pthread_getspecific(key_));
1847  if (holder != nullptr) {
1848  return CheckedDowncastToActualType<ValueHolder>(holder)->pointer();
1849  }
1850 
1851  ValueHolder* const new_holder = default_factory_->MakeNewHolder();
1852  ThreadLocalValueHolderBase* const holder_base = new_holder;
1853  GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base));
1854  return new_holder->pointer();
1855  }
1856 
1857  class ValueHolderFactory {
1858  public:
1859  ValueHolderFactory() {}
1860  virtual ~ValueHolderFactory() {}
1861  virtual ValueHolder* MakeNewHolder() const = 0;
1862 
1863  private:
1864  GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolderFactory);
1865  };
1866 
1867  class DefaultValueHolderFactory : public ValueHolderFactory {
1868  public:
1869  DefaultValueHolderFactory() {}
1870  virtual ValueHolder* MakeNewHolder() const { return new ValueHolder(); }
1871 
1872  private:
1873  GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultValueHolderFactory);
1874  };
1875 
1876  class InstanceValueHolderFactory : public ValueHolderFactory {
1877  public:
1878  explicit InstanceValueHolderFactory(const T& value) : value_(value) {}
1879  virtual ValueHolder* MakeNewHolder() const {
1880  return new ValueHolder(value_);
1881  }
1882 
1883  private:
1884  const T value_; // The value for each thread.
1885 
1886  GTEST_DISALLOW_COPY_AND_ASSIGN_(InstanceValueHolderFactory);
1887  };
1888 
1889  // A key pthreads uses for looking up per-thread values.
1890  const pthread_key_t key_;
1891  std::unique_ptr<ValueHolderFactory> default_factory_;
1892 
1893  GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal);
1894 };
1895 
1896 # endif // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
1897 
1898 #else // GTEST_IS_THREADSAFE
1899 
1900 // A dummy implementation of synchronization primitives (mutex, lock,
1901 // and thread-local variable). Necessary for compiling Google Test where
1902 // mutex is not supported - using Google Test in multiple threads is not
1903 // supported on such platforms.
1904 
1905 class Mutex {
1906  public:
1907  Mutex() {}
1908  void Lock() {}
1909  void Unlock() {}
1910  void AssertHeld() const {}
1911 };
1912 
1913 # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1914  extern ::testing::internal::Mutex mutex
1915 
1916 # define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex
1917 
1918 // We cannot name this class MutexLock because the ctor declaration would
1919 // conflict with a macro named MutexLock, which is defined on some
1920 // platforms. That macro is used as a defensive measure to prevent against
1921 // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than
1922 // "MutexLock l(&mu)". Hence the typedef trick below.
1924  public:
1925  explicit GTestMutexLock(Mutex*) {} // NOLINT
1926 };
1927 
1928 typedef GTestMutexLock MutexLock;
1929 
1930 template <typename T>
1931 class GTEST_API_ ThreadLocal {
1932  public:
1933  ThreadLocal() : value_() {}
1934  explicit ThreadLocal(const T& value) : value_(value) {}
1935  T* pointer() { return &value_; }
1936  const T* pointer() const { return &value_; }
1937  const T& get() const { return value_; }
1938  void set(const T& value) { value_ = value; }
1939  private:
1940  T value_;
1941 };
1942 
1943 #endif // GTEST_IS_THREADSAFE
1944 
1945 // Returns the number of threads running in the process, or 0 to indicate that
1946 // we cannot detect it.
1947 GTEST_API_ size_t GetThreadCount();
1948 
1949 template <bool bool_value>
1952  static const bool value = bool_value;
1953 };
1954 template <bool bool_value> const bool bool_constant<bool_value>::value;
1955 
1958 
1959 template <typename T, typename U>
1960 struct is_same : public false_type {};
1961 
1962 template <typename T>
1963 struct is_same<T, T> : public true_type {};
1964 
1965 template <typename Iterator>
1967  typedef typename Iterator::value_type value_type;
1968 };
1969 
1970 
1971 template <typename T>
1972 struct IteratorTraits<T*> {
1973  typedef T value_type;
1974 };
1975 
1976 template <typename T>
1977 struct IteratorTraits<const T*> {
1978  typedef T value_type;
1979 };
1980 
1981 #if GTEST_OS_WINDOWS
1982 # define GTEST_PATH_SEP_ "\\"
1983 # define GTEST_HAS_ALT_PATH_SEP_ 1
1984 // The biggest signed integer type the compiler supports.
1985 typedef __int64 BiggestInt;
1986 #else
1987 # define GTEST_PATH_SEP_ "/"
1988 # define GTEST_HAS_ALT_PATH_SEP_ 0
1989 typedef long long BiggestInt; // NOLINT
1990 #endif // GTEST_OS_WINDOWS
1991 
1992 // Utilities for char.
1993 
1994 // isspace(int ch) and friends accept an unsigned char or EOF. char
1995 // may be signed, depending on the compiler (or compiler flags).
1996 // Therefore we need to cast a char to unsigned char before calling
1997 // isspace(), etc.
1998 
1999 inline bool IsAlpha(char ch) {
2000  return isalpha(static_cast<unsigned char>(ch)) != 0;
2001 }
2002 inline bool IsAlNum(char ch) {
2003  return isalnum(static_cast<unsigned char>(ch)) != 0;
2004 }
2005 inline bool IsDigit(char ch) {
2006  return isdigit(static_cast<unsigned char>(ch)) != 0;
2007 }
2008 inline bool IsLower(char ch) {
2009  return islower(static_cast<unsigned char>(ch)) != 0;
2010 }
2011 inline bool IsSpace(char ch) {
2012  return isspace(static_cast<unsigned char>(ch)) != 0;
2013 }
2014 inline bool IsUpper(char ch) {
2015  return isupper(static_cast<unsigned char>(ch)) != 0;
2016 }
2017 inline bool IsXDigit(char ch) {
2018  return isxdigit(static_cast<unsigned char>(ch)) != 0;
2019 }
2020 inline bool IsXDigit(wchar_t ch) {
2021  const unsigned char low_byte = static_cast<unsigned char>(ch);
2022  return ch == low_byte && isxdigit(low_byte) != 0;
2023 }
2024 
2025 inline char ToLower(char ch) {
2026  return static_cast<char>(tolower(static_cast<unsigned char>(ch)));
2027 }
2028 inline char ToUpper(char ch) {
2029  return static_cast<char>(toupper(static_cast<unsigned char>(ch)));
2030 }
2031 
2032 inline std::string StripTrailingSpaces(std::string str) {
2033  std::string::iterator it = str.end();
2034  while (it != str.begin() && IsSpace(*--it))
2035  it = str.erase(it);
2036  return str;
2037 }
2038 
2039 // The testing::internal::posix namespace holds wrappers for common
2040 // POSIX functions. These wrappers hide the differences between
2041 // Windows/MSVC and POSIX systems. Since some compilers define these
2042 // standard functions as macros, the wrapper cannot have the same name
2043 // as the wrapped function.
2044 
2045 namespace posix {
2046 
2047 // Functions with a different name on Windows.
2048 
2049 #if GTEST_OS_WINDOWS
2050 
2051 typedef struct _stat StatStruct;
2052 
2053 # ifdef __BORLANDC__
2054 inline int IsATTY(int fd) { return isatty(fd); }
2055 inline int StrCaseCmp(const char* s1, const char* s2) {
2056  return stricmp(s1, s2);
2057 }
2058 inline char* StrDup(const char* src) { return strdup(src); }
2059 # else // !__BORLANDC__
2060 # if GTEST_OS_WINDOWS_MOBILE
2061 inline int IsATTY(int /* fd */) { return 0; }
2062 # else
2063 inline int IsATTY(int fd) { return _isatty(fd); }
2064 # endif // GTEST_OS_WINDOWS_MOBILE
2065 inline int StrCaseCmp(const char* s1, const char* s2) {
2066  return _stricmp(s1, s2);
2067 }
2068 inline char* StrDup(const char* src) { return _strdup(src); }
2069 # endif // __BORLANDC__
2070 
2071 # if GTEST_OS_WINDOWS_MOBILE
2072 inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
2073 // Stat(), RmDir(), and IsDir() are not needed on Windows CE at this
2074 // time and thus not defined there.
2075 # else
2076 inline int FileNo(FILE* file) { return _fileno(file); }
2077 inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
2078 inline int RmDir(const char* dir) { return _rmdir(dir); }
2079 inline bool IsDir(const StatStruct& st) {
2080  return (_S_IFDIR & st.st_mode) != 0;
2081 }
2082 # endif // GTEST_OS_WINDOWS_MOBILE
2083 
2084 #else
2085 
2086 typedef struct stat StatStruct;
2087 
2088 inline int FileNo(FILE* file) { return fileno(file); }
2089 inline int IsATTY(int fd) { return isatty(fd); }
2090 inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
2091 inline int StrCaseCmp(const char* s1, const char* s2) {
2092  return strcasecmp(s1, s2);
2093 }
2094 inline char* StrDup(const char* src) { return strdup(src); }
2095 inline int RmDir(const char* dir) { return rmdir(dir); }
2096 inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
2097 
2098 #endif // GTEST_OS_WINDOWS
2099 
2100 // Functions deprecated by MSVC 8.0.
2101 
2102 GTEST_DISABLE_MSC_DEPRECATED_PUSH_()
2103 
2104 inline const char* StrNCpy(char* dest, const char* src, size_t n) {
2105  return strncpy(dest, src, n);
2106 }
2107 
2108 // ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and
2109 // StrError() aren't needed on Windows CE at this time and thus not
2110 // defined there.
2111 
2112 #if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT
2113 inline int ChDir(const char* dir) { return chdir(dir); }
2114 #endif
2115 inline FILE* FOpen(const char* path, const char* mode) {
2116  return fopen(path, mode);
2117 }
2118 #if !GTEST_OS_WINDOWS_MOBILE
2119 inline FILE *FReopen(const char* path, const char* mode, FILE* stream) {
2120  return freopen(path, mode, stream);
2121 }
2122 inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
2123 #endif
2124 inline int FClose(FILE* fp) { return fclose(fp); }
2125 #if !GTEST_OS_WINDOWS_MOBILE
2126 inline int Read(int fd, void* buf, unsigned int count) {
2127  return static_cast<int>(read(fd, buf, count));
2128 }
2129 inline int Write(int fd, const void* buf, unsigned int count) {
2130  return static_cast<int>(write(fd, buf, count));
2131 }
2132 inline int Close(int fd) { return close(fd); }
2133 inline const char* StrError(int errnum) { return strerror(errnum); }
2134 #endif
2135 inline const char* GetEnv(const char* name) {
2136 #if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT
2137  // We are on Windows CE, which has no environment variables.
2138  static_cast<void>(name); // To prevent 'unused argument' warning.
2139  return nullptr;
2140 #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
2141  // Environment variables which we programmatically clear will be set to the
2142  // empty string rather than unset (NULL). Handle that case.
2143  const char* const env = getenv(name);
2144  return (env != nullptr && env[0] != '\0') ? env : nullptr;
2145 #else
2146  return getenv(name);
2147 #endif
2148 }
2149 
2150 GTEST_DISABLE_MSC_DEPRECATED_POP_()
2151 
2152 #if GTEST_OS_WINDOWS_MOBILE
2153 // Windows CE has no C library. The abort() function is used in
2154 // several places in Google Test. This implementation provides a reasonable
2155 // imitation of standard behaviour.
2156 [[noreturn]] void Abort();
2157 #else
2158 [[noreturn]] inline void Abort() { abort(); }
2159 #endif // GTEST_OS_WINDOWS_MOBILE
2160 
2161 } // namespace posix
2162 
2163 // MSVC "deprecates" snprintf and issues warnings wherever it is used. In
2164 // order to avoid these warnings, we need to use _snprintf or _snprintf_s on
2165 // MSVC-based platforms. We map the GTEST_SNPRINTF_ macro to the appropriate
2166 // function in order to achieve that. We use macro definition here because
2167 // snprintf is a variadic function.
2168 #if _MSC_VER && !GTEST_OS_WINDOWS_MOBILE
2169 // MSVC 2005 and above support variadic macros.
2170 # define GTEST_SNPRINTF_(buffer, size, format, ...) \
2171  _snprintf_s(buffer, size, size, format, __VA_ARGS__)
2172 #elif defined(_MSC_VER)
2173 // Windows CE does not define _snprintf_s
2174 # define GTEST_SNPRINTF_ _snprintf
2175 #else
2176 # define GTEST_SNPRINTF_ snprintf
2177 #endif
2178 
2179 // The maximum number a BiggestInt can represent. This definition
2180 // works no matter BiggestInt is represented in one's complement or
2181 // two's complement.
2182 //
2183 // We cannot rely on numeric_limits in STL, as __int64 and long long
2184 // are not part of standard C++ and numeric_limits doesn't need to be
2185 // defined for them.
2186 const BiggestInt kMaxBiggestInt =
2187  ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1));
2188 
2189 // This template class serves as a compile-time function from size to
2190 // type. It maps a size in bytes to a primitive type with that
2191 // size. e.g.
2192 //
2193 // TypeWithSize<4>::UInt
2194 //
2195 // is typedef-ed to be unsigned int (unsigned integer made up of 4
2196 // bytes).
2197 //
2198 // Such functionality should belong to STL, but I cannot find it
2199 // there.
2200 //
2201 // Google Test uses this class in the implementation of floating-point
2202 // comparison.
2203 //
2204 // For now it only handles UInt (unsigned int) as that's all Google Test
2205 // needs. Other types can be easily added in the future if need
2206 // arises.
2207 template <size_t size>
2209  public:
2210  // This prevents the user from using TypeWithSize<N> with incorrect
2211  // values of N.
2212  typedef void UInt;
2213 };
2214 
2215 // The specialization for size 4.
2216 template <>
2217 class TypeWithSize<4> {
2218  public:
2219  // unsigned int has size 4 in both gcc and MSVC.
2220  //
2221  // As base/basictypes.h doesn't compile on Windows, we cannot use
2222  // uint32, uint64, and etc here.
2223  typedef int Int;
2224  typedef unsigned int UInt;
2225 };
2226 
2227 // The specialization for size 8.
2228 template <>
2229 class TypeWithSize<8> {
2230  public:
2231 #if GTEST_OS_WINDOWS
2232  typedef __int64 Int;
2233  typedef unsigned __int64 UInt;
2234 #else
2235  typedef long long Int; // NOLINT
2236  typedef unsigned long long UInt; // NOLINT
2237 #endif // GTEST_OS_WINDOWS
2238 };
2239 
2240 // Integer types of known sizes.
2241 typedef TypeWithSize<4>::Int Int32;
2242 typedef TypeWithSize<4>::UInt UInt32;
2243 typedef TypeWithSize<8>::Int Int64;
2244 typedef TypeWithSize<8>::UInt UInt64;
2245 typedef TypeWithSize<8>::Int TimeInMillis; // Represents time in milliseconds.
2246 
2247 // Utilities for command line flags and environment variables.
2248 
2249 // Macro for referencing flags.
2250 #if !defined(GTEST_FLAG)
2251 # define GTEST_FLAG(name) FLAGS_gtest_##name
2252 #endif // !defined(GTEST_FLAG)
2253 
2254 #if !defined(GTEST_USE_OWN_FLAGFILE_FLAG_)
2255 # define GTEST_USE_OWN_FLAGFILE_FLAG_ 1
2256 #endif // !defined(GTEST_USE_OWN_FLAGFILE_FLAG_)
2257 
2258 #if !defined(GTEST_DECLARE_bool_)
2259 # define GTEST_FLAG_SAVER_ ::testing::internal::GTestFlagSaver
2260 
2261 // Macros for declaring flags.
2262 # define GTEST_DECLARE_bool_(name) GTEST_API_ extern bool GTEST_FLAG(name)
2263 # define GTEST_DECLARE_int32_(name) \
2264  GTEST_API_ extern ::testing::internal::Int32 GTEST_FLAG(name)
2265 # define GTEST_DECLARE_string_(name) \
2266  GTEST_API_ extern ::std::string GTEST_FLAG(name)
2267 
2268 // Macros for defining flags.
2269 # define GTEST_DEFINE_bool_(name, default_val, doc) \
2270  GTEST_API_ bool GTEST_FLAG(name) = (default_val)
2271 # define GTEST_DEFINE_int32_(name, default_val, doc) \
2272  GTEST_API_ ::testing::internal::Int32 GTEST_FLAG(name) = (default_val)
2273 # define GTEST_DEFINE_string_(name, default_val, doc) \
2274  GTEST_API_ ::std::string GTEST_FLAG(name) = (default_val)
2275 
2276 #endif // !defined(GTEST_DECLARE_bool_)
2277 
2278 // Thread annotations
2279 #if !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)
2280 # define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
2281 # define GTEST_LOCK_EXCLUDED_(locks)
2282 #endif // !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)
2283 
2284 // Parses 'str' for a 32-bit signed integer. If successful, writes the result
2285 // to *value and returns true; otherwise leaves *value unchanged and returns
2286 // false.
2287 bool ParseInt32(const Message& src_text, const char* str, Int32* value);
2288 
2289 // Parses a bool/Int32/string from the environment variable
2290 // corresponding to the given Google Test flag.
2291 bool BoolFromGTestEnv(const char* flag, bool default_val);
2292 GTEST_API_ Int32 Int32FromGTestEnv(const char* flag, Int32 default_val);
2293 std::string OutputFlagAlsoCheckEnvVar();
2294 const char* StringFromGTestEnv(const char* flag, const char* default_val);
2295 
2296 } // namespace internal
2297 } // namespace testing
2298 
2299 // Internal Macro to mark an API deprecated, for googletest usage only
2300 // Usage: class GTEST_INTERNAL_DEPRECATED(message) MyClass or
2301 // GTEST_INTERNAL_DEPRECATED(message) <return_type> myFunction(); Every usage of
2302 // a deprecated entity will trigger a warning when compiled with
2303 // `-Wdeprecated-declarations` option (clang, gcc, any __GNUC__ compiler).
2304 // For msvc /W3 option will need to be used
2305 // Note that for 'other' compilers this macro evaluates to nothing to prevent
2306 // compilations errors.
2307 #if defined(_MSC_VER)
2308 #define GTEST_INTERNAL_DEPRECATED(message) __declspec(deprecated(message))
2309 #elif defined(__GNUC__)
2310 #define GTEST_INTERNAL_DEPRECATED(message) __attribute__((deprecated(message)))
2311 #else
2312 #define GTEST_INTERNAL_DEPRECATED(message)
2313 #endif
2314 
2315 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
Definition: gmock-actions.h:59
Definition: gtest-port.h:1931
Definition: googletest-port-test.cc:192
Definition: gtest-port.h:1079
Definition: gtest-port.h:2208
Definition: gtest-message.h:90
Definition: gtest-port.h:867
Definition: gtest-port.h:995
Definition: gtest-port.h:1923
Definition: gtest-port.h:1950
Definition: gtest-port.h:1058
Definition: gtest-port.h:1960
Definition: gtest-port.h:902
Definition: gtest-port.h:2045
Definition: gtest-port.h:1966