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