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 | // [email protected] (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 if 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 if 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 if exceptions are enabled before clang 220714, |
418 | // but if 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 if 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 if 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 if 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 if 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 | // Evaluates to the number of elements in 'array'. |
873 | #define GTEST_ARRAY_SIZE_(array) (sizeof(array) / sizeof(array[0])) |
874 | |
875 | // A helper for suppressing warnings on constant condition. It just |
876 | // returns 'condition'. |
877 | GTEST_API_ bool IsTrue(bool condition); |
878 | |
879 | // Defines RE. |
880 | |
881 | #if GTEST_USES_PCRE |
882 | // if used, PCRE is injected by custom/gtest-port.h |
883 | #elif GTEST_USES_POSIX_RE || GTEST_USES_SIMPLE_RE |
884 | |
885 | // A simple C++ wrapper for <regex.h>. It uses the POSIX Extended |
886 | // Regular Expression syntax. |
887 | class GTEST_API_ RE { |
888 | public: |
889 | // A copy constructor is required by the Standard to initialize object |
890 | // references from r-values. |
891 | RE(const RE& other) { Init(other.pattern()); } |
892 | |
893 | // Constructs an RE from a string. |
894 | RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT |
895 | |
896 | RE(const char* regex) { Init(regex); } // NOLINT |
897 | ~RE(); |
898 | |
899 | // Returns the string representation of the regex. |
900 | const char* pattern() const { return pattern_; } |
901 | |
902 | // FullMatch(str, re) returns true if regular expression re matches |
903 | // the entire str. |
904 | // PartialMatch(str, re) returns true if regular expression re |
905 | // matches a substring of str (including str itself). |
906 | static bool FullMatch(const ::std::string& str, const RE& re) { |
907 | return FullMatch(str.c_str(), re); |
908 | } |
909 | static bool PartialMatch(const ::std::string& str, const RE& re) { |
910 | return PartialMatch(str.c_str(), re); |
911 | } |
912 | |
913 | static bool FullMatch(const char* str, const RE& re); |
914 | static bool PartialMatch(const char* str, const RE& re); |
915 | |
916 | private: |
917 | void Init(const char* regex); |
918 | const char* pattern_; |
919 | bool is_valid_; |
920 | |
921 | # if GTEST_USES_POSIX_RE |
922 | |
923 | regex_t full_regex_; // For FullMatch(). |
924 | regex_t partial_regex_; // For PartialMatch(). |
925 | |
926 | # else // GTEST_USES_SIMPLE_RE |
927 | |
928 | const char* full_pattern_; // For FullMatch(); |
929 | |
930 | # endif |
931 | |
932 | GTEST_DISALLOW_ASSIGN_(RE); |
933 | }; |
934 | |
935 | #endif // GTEST_USES_PCRE |
936 | |
937 | // Formats a source file path and a line number as they would appear |
938 | // in an error message from the compiler used to compile this code. |
939 | GTEST_API_ ::std::string FormatFileLocation(const char* file, int line); |
940 | |
941 | // Formats a file location for compiler-independent XML output. |
942 | // Although this function is not platform dependent, we put it next to |
943 | // FormatFileLocation in order to contrast the two functions. |
944 | GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file, |
945 | int line); |
946 | |
947 | // Defines logging utilities: |
948 | // GTEST_LOG_(severity) - logs messages at the specified severity level. The |
949 | // message itself is streamed into the macro. |
950 | // LogToStderr() - directs all log messages to stderr. |
951 | // FlushInfoLog() - flushes informational log messages. |
952 | |
953 | enum GTestLogSeverity { |
954 | GTEST_INFO, |
955 | GTEST_WARNING, |
956 | GTEST_ERROR, |
957 | GTEST_FATAL |
958 | }; |
959 | |
960 | // Formats log entry severity, provides a stream object for streaming the |
961 | // log message, and terminates the message with a newline when going out of |
962 | // scope. |
963 | class GTEST_API_ GTestLog { |
964 | public: |
965 | GTestLog(GTestLogSeverity severity, const char* file, int line); |
966 | |
967 | // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program. |
968 | ~GTestLog(); |
969 | |
970 | ::std::ostream& GetStream() { return ::std::cerr; } |
971 | |
972 | private: |
973 | const GTestLogSeverity severity_; |
974 | |
975 | GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog); |
976 | }; |
977 | |
978 | #if !defined(GTEST_LOG_) |
979 | |
980 | # define GTEST_LOG_(severity) \ |
981 | ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \ |
982 | __FILE__, __LINE__).GetStream() |
983 | |
984 | inline void LogToStderr() {} |
985 | inline void FlushInfoLog() { fflush(nullptr); } |
986 | |
987 | #endif // !defined(GTEST_LOG_) |
988 | |
989 | #if !defined(GTEST_CHECK_) |
990 | // INTERNAL IMPLEMENTATION - DO NOT USE. |
991 | // |
992 | // GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition |
993 | // is not satisfied. |
994 | // Synopsys: |
995 | // GTEST_CHECK_(boolean_condition); |
996 | // or |
997 | // GTEST_CHECK_(boolean_condition) << "Additional message"; |
998 | // |
999 | // This checks the condition and if the condition is not satisfied |
1000 | // it prints message about the condition violation, including the |
1001 | // condition itself, plus additional message streamed into it, if any, |
1002 | // and then it aborts the program. It aborts the program irrespective of |
1003 | // whether it is built in the debug mode or not. |
1004 | # define GTEST_CHECK_(condition) \ |
1005 | GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ |
1006 | if (::testing::internal::IsTrue(condition)) \ |
1007 | ; \ |
1008 | else \ |
1009 | GTEST_LOG_(FATAL) << "Condition " #condition " failed. " |
1010 | #endif // !defined(GTEST_CHECK_) |
1011 | |
1012 | // An all-mode assert to verify that the given POSIX-style function |
1013 | // call returns 0 (indicating success). Known limitation: this |
1014 | // doesn't expand to a balanced 'if' statement, so enclose the macro |
1015 | // in {} if you need to use it as the only statement in an 'if' |
1016 | // branch. |
1017 | #define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \ |
1018 | if (const int gtest_error = (posix_call)) \ |
1019 | GTEST_LOG_(FATAL) << #posix_call << "failed with error " \ |
1020 | << gtest_error |
1021 | |
1022 | // Adds reference to a type if it is not a reference type, |
1023 | // otherwise leaves it unchanged. This is the same as |
1024 | // tr1::add_reference, which is not widely available yet. |
1025 | template <typename T> |
1026 | struct AddReference { typedef T& type; }; // NOLINT |
1027 | template <typename T> |
1028 | struct AddReference<T&> { typedef T& type; }; // NOLINT |
1029 | |
1030 | // A handy wrapper around AddReference that works when the argument T |
1031 | // depends on template parameters. |
1032 | #define GTEST_ADD_REFERENCE_(T) \ |
1033 | typename ::testing::internal::AddReference<T>::type |
1034 | |
1035 | // Transforms "T" into "const T&" according to standard reference collapsing |
1036 | // rules (this is only needed as a backport for C++98 compilers that do not |
1037 | // support reference collapsing). Specifically, it transforms: |
1038 | // |
1039 | // char ==> const char& |
1040 | // const char ==> const char& |
1041 | // char& ==> char& |
1042 | // const char& ==> const char& |
1043 | // |
1044 | // Note that the non-const reference will not have "const" added. This is |
1045 | // standard, and necessary so that "T" can always bind to "const T&". |
1046 | template <typename T> |
1047 | struct ConstRef { typedef const T& type; }; |
1048 | template <typename T> |
1049 | struct ConstRef<T&> { typedef T& type; }; |
1050 | |
1051 | // The argument T must depend on some template parameters. |
1052 | #define GTEST_REFERENCE_TO_CONST_(T) \ |
1053 | typename ::testing::internal::ConstRef<T>::type |
1054 | |
1055 | // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. |
1056 | // |
1057 | // Use ImplicitCast_ as a safe version of static_cast for upcasting in |
1058 | // the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a |
1059 | // const Foo*). When you use ImplicitCast_, the compiler checks that |
1060 | // the cast is safe. Such explicit ImplicitCast_s are necessary in |
1061 | // surprisingly many situations where C++ demands an exact type match |
1062 | // instead of an argument type convertable to a target type. |
1063 | // |
1064 | // The syntax for using ImplicitCast_ is the same as for static_cast: |
1065 | // |
1066 | // ImplicitCast_<ToType>(expr) |
1067 | // |
1068 | // ImplicitCast_ would have been part of the C++ standard library, |
1069 | // but the proposal was submitted too late. It will probably make |
1070 | // its way into the language in the future. |
1071 | // |
1072 | // This relatively ugly name is intentional. It prevents clashes with |
1073 | // similar functions users may have (e.g., implicit_cast). The internal |
1074 | // namespace alone is not enough because the function can be found by ADL. |
1075 | template<typename To> |
1076 | inline To ImplicitCast_(To x) { return x; } |
1077 | |
1078 | // When you upcast (that is, cast a pointer from type Foo to type |
1079 | // SuperclassOfFoo), it's fine to use ImplicitCast_<>, since upcasts |
1080 | // always succeed. When you downcast (that is, cast a pointer from |
1081 | // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because |
1082 | // how do you know the pointer is really of type SubclassOfFoo? It |
1083 | // could be a bare Foo, or of type DifferentSubclassOfFoo. Thus, |
1084 | // when you downcast, you should use this macro. In debug mode, we |
1085 | // use dynamic_cast<> to double-check the downcast is legal (we die |
1086 | // if it's not). In normal mode, we do the efficient static_cast<> |
1087 | // instead. Thus, it's important to test in debug mode to make sure |
1088 | // the cast is legal! |
1089 | // This is the only place in the code we should use dynamic_cast<>. |
1090 | // In particular, you SHOULDN'T be using dynamic_cast<> in order to |
1091 | // do RTTI (eg code like this: |
1092 | // if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo); |
1093 | // if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo); |
1094 | // You should design the code some other way not to need this. |
1095 | // |
1096 | // This relatively ugly name is intentional. It prevents clashes with |
1097 | // similar functions users may have (e.g., down_cast). The internal |
1098 | // namespace alone is not enough because the function can be found by ADL. |
1099 | template<typename To, typename From> // use like this: DownCast_<T*>(foo); |
1100 | inline To DownCast_(From* f) { // so we only accept pointers |
1101 | // Ensures that To is a sub-type of From *. This test is here only |
1102 | // for compile-time type checking, and has no overhead in an |
1103 | // optimized build at run-time, as it will be optimized away |
1104 | // completely. |
1105 | GTEST_INTENTIONAL_CONST_COND_PUSH_() |
1106 | if (false) { |
1107 | GTEST_INTENTIONAL_CONST_COND_POP_() |
1108 | const To to = nullptr; |
1109 | ::testing::internal::ImplicitCast_<From*>(to); |
1110 | } |
1111 | |
1112 | #if GTEST_HAS_RTTI |
1113 | // RTTI: debug mode only! |
1114 | GTEST_CHECK_(f == nullptr || dynamic_cast<To>(f) != nullptr); |
1115 | #endif |
1116 | return static_cast<To>(f); |
1117 | } |
1118 | |
1119 | // Downcasts the pointer of type Base to Derived. |
1120 | // Derived must be a subclass of Base. The parameter MUST |
1121 | // point to a class of type Derived, not any subclass of it. |
1122 | // When RTTI is available, the function performs a runtime |
1123 | // check to enforce this. |
1124 | template <class Derived, class Base> |
1125 | Derived* CheckedDowncastToActualType(Base* base) { |
1126 | #if GTEST_HAS_RTTI |
1127 | GTEST_CHECK_(typeid(*base) == typeid(Derived)); |
1128 | #endif |
1129 | |
1130 | #if GTEST_HAS_DOWNCAST_ |
1131 | return ::down_cast<Derived*>(base); |
1132 | #elif GTEST_HAS_RTTI |
1133 | return dynamic_cast<Derived*>(base); // NOLINT |
1134 | #else |
1135 | return static_cast<Derived*>(base); // Poor man's downcast. |
1136 | #endif |
1137 | } |
1138 | |
1139 | #if GTEST_HAS_STREAM_REDIRECTION |
1140 | |
1141 | // Defines the stderr capturer: |
1142 | // CaptureStdout - starts capturing stdout. |
1143 | // GetCapturedStdout - stops capturing stdout and returns the captured string. |
1144 | // CaptureStderr - starts capturing stderr. |
1145 | // GetCapturedStderr - stops capturing stderr and returns the captured string. |
1146 | // |
1147 | GTEST_API_ void CaptureStdout(); |
1148 | GTEST_API_ std::string GetCapturedStdout(); |
1149 | GTEST_API_ void CaptureStderr(); |
1150 | GTEST_API_ std::string GetCapturedStderr(); |
1151 | |
1152 | #endif // GTEST_HAS_STREAM_REDIRECTION |
1153 | // Returns the size (in bytes) of a file. |
1154 | GTEST_API_ size_t GetFileSize(FILE* file); |
1155 | |
1156 | // Reads the entire content of a file as a string. |
1157 | GTEST_API_ std::string ReadEntireFile(FILE* file); |
1158 | |
1159 | // All command line arguments. |
1160 | GTEST_API_ std::vector<std::string> GetArgvs(); |
1161 | |
1162 | #if GTEST_HAS_DEATH_TEST |
1163 | |
1164 | std::vector<std::string> GetInjectableArgvs(); |
1165 | // Deprecated: pass the args vector by value instead. |
1166 | void SetInjectableArgvs(const std::vector<std::string>* new_argvs); |
1167 | void SetInjectableArgvs(const std::vector<std::string>& new_argvs); |
1168 | void ClearInjectableArgvs(); |
1169 | |
1170 | #endif // GTEST_HAS_DEATH_TEST |
1171 | |
1172 | // Defines synchronization primitives. |
1173 | #if GTEST_IS_THREADSAFE |
1174 | # if GTEST_HAS_PTHREAD |
1175 | // Sleeps for (roughly) n milliseconds. This function is only for testing |
1176 | // Google Test's own constructs. Don't use it in user tests, either |
1177 | // directly or indirectly. |
1178 | inline void SleepMilliseconds(int n) { |
1179 | const timespec time = { |
1180 | 0, // 0 seconds. |
1181 | n * 1000L * 1000L, // And n ms. |
1182 | }; |
1183 | nanosleep(&time, nullptr); |
1184 | } |
1185 | # endif // GTEST_HAS_PTHREAD |
1186 | |
1187 | # if GTEST_HAS_NOTIFICATION_ |
1188 | // Notification has already been imported into the namespace. |
1189 | // Nothing to do here. |
1190 | |
1191 | # elif GTEST_HAS_PTHREAD |
1192 | // Allows a controller thread to pause execution of newly created |
1193 | // threads until notified. Instances of this class must be created |
1194 | // and destroyed in the controller thread. |
1195 | // |
1196 | // This class is only for testing Google Test's own constructs. Do not |
1197 | // use it in user tests, either directly or indirectly. |
1198 | class Notification { |
1199 | public: |
1200 | Notification() : notified_(false) { |
1201 | GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, nullptr)); |
1202 | } |
1203 | ~Notification() { |
1204 | pthread_mutex_destroy(&mutex_); |
1205 | } |
1206 | |
1207 | // Notifies all threads created with this notification to start. Must |
1208 | // be called from the controller thread. |
1209 | void Notify() { |
1210 | pthread_mutex_lock(&mutex_); |
1211 | notified_ = true; |
1212 | pthread_mutex_unlock(&mutex_); |
1213 | } |
1214 | |
1215 | // Blocks until the controller thread notifies. Must be called from a test |
1216 | // thread. |
1217 | void WaitForNotification() { |
1218 | for (;;) { |
1219 | pthread_mutex_lock(&mutex_); |
1220 | const bool notified = notified_; |
1221 | pthread_mutex_unlock(&mutex_); |
1222 | if (notified) |
1223 | break; |
1224 | SleepMilliseconds(10); |
1225 | } |
1226 | } |
1227 | |
1228 | private: |
1229 | pthread_mutex_t mutex_; |
1230 | bool notified_; |
1231 | |
1232 | GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification); |
1233 | }; |
1234 | |
1235 | # elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT |
1236 | |
1237 | GTEST_API_ void SleepMilliseconds(int n); |
1238 | |
1239 | // Provides leak-safe Windows kernel handle ownership. |
1240 | // Used in death tests and in threading support. |
1241 | class GTEST_API_ AutoHandle { |
1242 | public: |
1243 | // Assume that Win32 HANDLE type is equivalent to void*. Doing so allows us to |
1244 | // avoid including <windows.h> in this header file. Including <windows.h> is |
1245 | // undesirable because it defines a lot of symbols and macros that tend to |
1246 | // conflict with client code. This assumption is verified by |
1247 | // WindowsTypesTest.HANDLEIsVoidStar. |
1248 | typedef void* Handle; |
1249 | AutoHandle(); |
1250 | explicit AutoHandle(Handle handle); |
1251 | |
1252 | ~AutoHandle(); |
1253 | |
1254 | Handle Get() const; |
1255 | void Reset(); |
1256 | void Reset(Handle handle); |
1257 | |
1258 | private: |
1259 | // Returns true if the handle is a valid handle object that can be closed. |
1260 | bool IsCloseable() const; |
1261 | |
1262 | Handle handle_; |
1263 | |
1264 | GTEST_DISALLOW_COPY_AND_ASSIGN_(AutoHandle); |
1265 | }; |
1266 | |
1267 | // Allows a controller thread to pause execution of newly created |
1268 | // threads until notified. Instances of this class must be created |
1269 | // and destroyed in the controller thread. |
1270 | // |
1271 | // This class is only for testing Google Test's own constructs. Do not |
1272 | // use it in user tests, either directly or indirectly. |
1273 | class GTEST_API_ Notification { |
1274 | public: |
1275 | Notification(); |
1276 | void Notify(); |
1277 | void WaitForNotification(); |
1278 | |
1279 | private: |
1280 | AutoHandle event_; |
1281 | |
1282 | GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification); |
1283 | }; |
1284 | # endif // GTEST_HAS_NOTIFICATION_ |
1285 | |
1286 | // On MinGW, we can have both GTEST_OS_WINDOWS and GTEST_HAS_PTHREAD |
1287 | // defined, but we don't want to use MinGW's pthreads implementation, which |
1288 | // has conformance problems with some versions of the POSIX standard. |
1289 | # if GTEST_HAS_PTHREAD && !GTEST_OS_WINDOWS_MINGW |
1290 | |
1291 | // As a C-function, ThreadFuncWithCLinkage cannot be templated itself. |
1292 | // Consequently, it cannot select a correct instantiation of ThreadWithParam |
1293 | // in order to call its Run(). Introducing ThreadWithParamBase as a |
1294 | // non-templated base class for ThreadWithParam allows us to bypass this |
1295 | // problem. |
1296 | class ThreadWithParamBase { |
1297 | public: |
1298 | virtual ~ThreadWithParamBase() {} |
1299 | virtual void Run() = 0; |
1300 | }; |
1301 | |
1302 | // pthread_create() accepts a pointer to a function type with the C linkage. |
1303 | // According to the Standard (7.5/1), function types with different linkages |
1304 | // are different even if they are otherwise identical. Some compilers (for |
1305 | // example, SunStudio) treat them as different types. Since class methods |
1306 | // cannot be defined with C-linkage we need to define a free C-function to |
1307 | // pass into pthread_create(). |
1308 | extern "C" inline void* ThreadFuncWithCLinkage(void* thread) { |
1309 | static_cast<ThreadWithParamBase*>(thread)->Run(); |
1310 | return nullptr; |
1311 | } |
1312 | |
1313 | // Helper class for testing Google Test's multi-threading constructs. |
1314 | // To use it, write: |
1315 | // |
1316 | // void ThreadFunc(int param) { /* Do things with param */ } |
1317 | // Notification thread_can_start; |
1318 | // ... |
1319 | // // The thread_can_start parameter is optional; you can supply NULL. |
1320 | // ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start); |
1321 | // thread_can_start.Notify(); |
1322 | // |
1323 | // These classes are only for testing Google Test's own constructs. Do |
1324 | // not use them in user tests, either directly or indirectly. |
1325 | template <typename T> |
1326 | class ThreadWithParam : public ThreadWithParamBase { |
1327 | public: |
1328 | typedef void UserThreadFunc(T); |
1329 | |
1330 | ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start) |
1331 | : func_(func), |
1332 | param_(param), |
1333 | thread_can_start_(thread_can_start), |
1334 | finished_(false) { |
1335 | ThreadWithParamBase* const base = this; |
1336 | // The thread can be created only after all fields except thread_ |
1337 | // have been initialized. |
1338 | GTEST_CHECK_POSIX_SUCCESS_( |
1339 | pthread_create(&thread_, nullptr, &ThreadFuncWithCLinkage, base)); |
1340 | } |
1341 | ~ThreadWithParam() override { Join(); } |
1342 | |
1343 | void Join() { |
1344 | if (!finished_) { |
1345 | GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, nullptr)); |
1346 | finished_ = true; |
1347 | } |
1348 | } |
1349 | |
1350 | void Run() override { |
1351 | if (thread_can_start_ != nullptr) thread_can_start_->WaitForNotification(); |
1352 | func_(param_); |
1353 | } |
1354 | |
1355 | private: |
1356 | UserThreadFunc* const func_; // User-supplied thread function. |
1357 | const T param_; // User-supplied parameter to the thread function. |
1358 | // When non-NULL, used to block execution until the controller thread |
1359 | // notifies. |
1360 | Notification* const thread_can_start_; |
1361 | bool finished_; // true if we know that the thread function has finished. |
1362 | pthread_t thread_; // The native thread object. |
1363 | |
1364 | GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam); |
1365 | }; |
1366 | # endif // !GTEST_OS_WINDOWS && GTEST_HAS_PTHREAD || |
1367 | // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ |
1368 | |
1369 | # if GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ |
1370 | // Mutex and ThreadLocal have already been imported into the namespace. |
1371 | // Nothing to do here. |
1372 | |
1373 | # elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT |
1374 | |
1375 | // Mutex implements mutex on Windows platforms. It is used in conjunction |
1376 | // with class MutexLock: |
1377 | // |
1378 | // Mutex mutex; |
1379 | // ... |
1380 | // MutexLock lock(&mutex); // Acquires the mutex and releases it at the |
1381 | // // end of the current scope. |
1382 | // |
1383 | // A static Mutex *must* be defined or declared using one of the following |
1384 | // macros: |
1385 | // GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex); |
1386 | // GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex); |
1387 | // |
1388 | // (A non-static Mutex is defined/declared in the usual way). |
1389 | class GTEST_API_ Mutex { |
1390 | public: |
1391 | enum MutexType { kStatic = 0, kDynamic = 1 }; |
1392 | // We rely on kStaticMutex being 0 as it is to what the linker initializes |
1393 | // type_ in static mutexes. critical_section_ will be initialized lazily |
1394 | // in ThreadSafeLazyInit(). |
1395 | enum StaticConstructorSelector { kStaticMutex = 0 }; |
1396 | |
1397 | // This constructor intentionally does nothing. It relies on type_ being |
1398 | // statically initialized to 0 (effectively setting it to kStatic) and on |
1399 | // ThreadSafeLazyInit() to lazily initialize the rest of the members. |
1400 | explicit Mutex(StaticConstructorSelector /*dummy*/) {} |
1401 | |
1402 | Mutex(); |
1403 | ~Mutex(); |
1404 | |
1405 | void Lock(); |
1406 | |
1407 | void Unlock(); |
1408 | |
1409 | // Does nothing if the current thread holds the mutex. Otherwise, crashes |
1410 | // with high probability. |
1411 | void AssertHeld(); |
1412 | |
1413 | private: |
1414 | // Initializes owner_thread_id_ and critical_section_ in static mutexes. |
1415 | void ThreadSafeLazyInit(); |
1416 | |
1417 | // Per https://blogs.msdn.microsoft.com/oldnewthing/20040223-00/?p=40503, |
1418 | // we assume that 0 is an invalid value for thread IDs. |
1419 | unsigned int owner_thread_id_; |
1420 | |
1421 | // For static mutexes, we rely on these members being initialized to zeros |
1422 | // by the linker. |
1423 | MutexType type_; |
1424 | long critical_section_init_phase_; // NOLINT |
1425 | GTEST_CRITICAL_SECTION* critical_section_; |
1426 | |
1427 | GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex); |
1428 | }; |
1429 | |
1430 | # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ |
1431 | extern ::testing::internal::Mutex mutex |
1432 | |
1433 | # define GTEST_DEFINE_STATIC_MUTEX_(mutex) \ |
1434 | ::testing::internal::Mutex mutex(::testing::internal::Mutex::kStaticMutex) |
1435 | |
1436 | // We cannot name this class MutexLock because the ctor declaration would |
1437 | // conflict with a macro named MutexLock, which is defined on some |
1438 | // platforms. That macro is used as a defensive measure to prevent against |
1439 | // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than |
1440 | // "MutexLock l(&mu)". Hence the typedef trick below. |
1441 | class GTestMutexLock { |
1442 | public: |
1443 | explicit GTestMutexLock(Mutex* mutex) |
1444 | : mutex_(mutex) { mutex_->Lock(); } |
1445 | |
1446 | ~GTestMutexLock() { mutex_->Unlock(); } |
1447 | |
1448 | private: |
1449 | Mutex* const mutex_; |
1450 | |
1451 | GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock); |
1452 | }; |
1453 | |
1454 | typedef GTestMutexLock MutexLock; |
1455 | |
1456 | // Base class for ValueHolder<T>. Allows a caller to hold and delete a value |
1457 | // without knowing its type. |
1458 | class ThreadLocalValueHolderBase { |
1459 | public: |
1460 | virtual ~ThreadLocalValueHolderBase() {} |
1461 | }; |
1462 | |
1463 | // Provides a way for a thread to send notifications to a ThreadLocal |
1464 | // regardless of its parameter type. |
1465 | class ThreadLocalBase { |
1466 | public: |
1467 | // Creates a new ValueHolder<T> object holding a default value passed to |
1468 | // this ThreadLocal<T>'s constructor and returns it. It is the caller's |
1469 | // responsibility not to call this when the ThreadLocal<T> instance already |
1470 | // has a value on the current thread. |
1471 | virtual ThreadLocalValueHolderBase* NewValueForCurrentThread() const = 0; |
1472 | |
1473 | protected: |
1474 | ThreadLocalBase() {} |
1475 | virtual ~ThreadLocalBase() {} |
1476 | |
1477 | private: |
1478 | GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocalBase); |
1479 | }; |
1480 | |
1481 | // Maps a thread to a set of ThreadLocals that have values instantiated on that |
1482 | // thread and notifies them when the thread exits. A ThreadLocal instance is |
1483 | // expected to persist until all threads it has values on have terminated. |
1484 | class GTEST_API_ ThreadLocalRegistry { |
1485 | public: |
1486 | // Registers thread_local_instance as having value on the current thread. |
1487 | // Returns a value that can be used to identify the thread from other threads. |
1488 | static ThreadLocalValueHolderBase* GetValueOnCurrentThread( |
1489 | const ThreadLocalBase* thread_local_instance); |
1490 | |
1491 | // Invoked when a ThreadLocal instance is destroyed. |
1492 | static void OnThreadLocalDestroyed( |
1493 | const ThreadLocalBase* thread_local_instance); |
1494 | }; |
1495 | |
1496 | class GTEST_API_ ThreadWithParamBase { |
1497 | public: |
1498 | void Join(); |
1499 | |
1500 | protected: |
1501 | class Runnable { |
1502 | public: |
1503 | virtual ~Runnable() {} |
1504 | virtual void Run() = 0; |
1505 | }; |
1506 | |
1507 | ThreadWithParamBase(Runnable *runnable, Notification* thread_can_start); |
1508 | virtual ~ThreadWithParamBase(); |
1509 | |
1510 | private: |
1511 | AutoHandle thread_; |
1512 | }; |
1513 | |
1514 | // Helper class for testing Google Test's multi-threading constructs. |
1515 | template <typename T> |
1516 | class ThreadWithParam : public ThreadWithParamBase { |
1517 | public: |
1518 | typedef void UserThreadFunc(T); |
1519 | |
1520 | ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start) |
1521 | : ThreadWithParamBase(new RunnableImpl(func, param), thread_can_start) { |
1522 | } |
1523 | virtual ~ThreadWithParam() {} |
1524 | |
1525 | private: |
1526 | class RunnableImpl : public Runnable { |
1527 | public: |
1528 | RunnableImpl(UserThreadFunc* func, T param) |
1529 | : func_(func), |
1530 | param_(param) { |
1531 | } |
1532 | virtual ~RunnableImpl() {} |
1533 | virtual void Run() { |
1534 | func_(param_); |
1535 | } |
1536 | |
1537 | private: |
1538 | UserThreadFunc* const func_; |
1539 | const T param_; |
1540 | |
1541 | GTEST_DISALLOW_COPY_AND_ASSIGN_(RunnableImpl); |
1542 | }; |
1543 | |
1544 | GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam); |
1545 | }; |
1546 | |
1547 | // Implements thread-local storage on Windows systems. |
1548 | // |
1549 | // // Thread 1 |
1550 | // ThreadLocal<int> tl(100); // 100 is the default value for each thread. |
1551 | // |
1552 | // // Thread 2 |
1553 | // tl.set(150); // Changes the value for thread 2 only. |
1554 | // EXPECT_EQ(150, tl.get()); |
1555 | // |
1556 | // // Thread 1 |
1557 | // EXPECT_EQ(100, tl.get()); // In thread 1, tl has the original value. |
1558 | // tl.set(200); |
1559 | // EXPECT_EQ(200, tl.get()); |
1560 | // |
1561 | // The template type argument T must have a public copy constructor. |
1562 | // In addition, the default ThreadLocal constructor requires T to have |
1563 | // a public default constructor. |
1564 | // |
1565 | // The users of a TheadLocal instance have to make sure that all but one |
1566 | // threads (including the main one) using that instance have exited before |
1567 | // destroying it. Otherwise, the per-thread objects managed for them by the |
1568 | // ThreadLocal instance are not guaranteed to be destroyed on all platforms. |
1569 | // |
1570 | // Google Test only uses global ThreadLocal objects. That means they |
1571 | // will die after main() has returned. Therefore, no per-thread |
1572 | // object managed by Google Test will be leaked as long as all threads |
1573 | // using Google Test have exited when main() returns. |
1574 | template <typename T> |
1575 | class ThreadLocal : public ThreadLocalBase { |
1576 | public: |
1577 | ThreadLocal() : default_factory_(new DefaultValueHolderFactory()) {} |
1578 | explicit ThreadLocal(const T& value) |
1579 | : default_factory_(new InstanceValueHolderFactory(value)) {} |
1580 | |
1581 | ~ThreadLocal() { ThreadLocalRegistry::OnThreadLocalDestroyed(this); } |
1582 | |
1583 | T* pointer() { return GetOrCreateValue(); } |
1584 | const T* pointer() const { return GetOrCreateValue(); } |
1585 | const T& get() const { return *pointer(); } |
1586 | void set(const T& value) { *pointer() = value; } |
1587 | |
1588 | private: |
1589 | // Holds a value of T. Can be deleted via its base class without the caller |
1590 | // knowing the type of T. |
1591 | class ValueHolder : public ThreadLocalValueHolderBase { |
1592 | public: |
1593 | ValueHolder() : value_() {} |
1594 | explicit ValueHolder(const T& value) : value_(value) {} |
1595 | |
1596 | T* pointer() { return &value_; } |
1597 | |
1598 | private: |
1599 | T value_; |
1600 | GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder); |
1601 | }; |
1602 | |
1603 | |
1604 | T* GetOrCreateValue() const { |
1605 | return static_cast<ValueHolder*>( |
1606 | ThreadLocalRegistry::GetValueOnCurrentThread(this))->pointer(); |
1607 | } |
1608 | |
1609 | virtual ThreadLocalValueHolderBase* NewValueForCurrentThread() const { |
1610 | return default_factory_->MakeNewHolder(); |
1611 | } |
1612 | |
1613 | class ValueHolderFactory { |
1614 | public: |
1615 | ValueHolderFactory() {} |
1616 | virtual ~ValueHolderFactory() {} |
1617 | virtual ValueHolder* MakeNewHolder() const = 0; |
1618 | |
1619 | private: |
1620 | GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolderFactory); |
1621 | }; |
1622 | |
1623 | class DefaultValueHolderFactory : public ValueHolderFactory { |
1624 | public: |
1625 | DefaultValueHolderFactory() {} |
1626 | virtual ValueHolder* MakeNewHolder() const { return new ValueHolder(); } |
1627 | |
1628 | private: |
1629 | GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultValueHolderFactory); |
1630 | }; |
1631 | |
1632 | class InstanceValueHolderFactory : public ValueHolderFactory { |
1633 | public: |
1634 | explicit InstanceValueHolderFactory(const T& value) : value_(value) {} |
1635 | virtual ValueHolder* MakeNewHolder() const { |
1636 | return new ValueHolder(value_); |
1637 | } |
1638 | |
1639 | private: |
1640 | const T value_; // The value for each thread. |
1641 | |
1642 | GTEST_DISALLOW_COPY_AND_ASSIGN_(InstanceValueHolderFactory); |
1643 | }; |
1644 | |
1645 | std::unique_ptr<ValueHolderFactory> default_factory_; |
1646 | |
1647 | GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal); |
1648 | }; |
1649 | |
1650 | # elif GTEST_HAS_PTHREAD |
1651 | |
1652 | // MutexBase and Mutex implement mutex on pthreads-based platforms. |
1653 | class MutexBase { |
1654 | public: |
1655 | // Acquires this mutex. |
1656 | void Lock() { |
1657 | GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_)); |
1658 | owner_ = pthread_self(); |
1659 | has_owner_ = true; |
1660 | } |
1661 | |
1662 | // Releases this mutex. |
1663 | void Unlock() { |
1664 | // Since the lock is being released the owner_ field should no longer be |
1665 | // considered valid. We don't protect writing to has_owner_ here, as it's |
1666 | // the caller's responsibility to ensure that the current thread holds the |
1667 | // mutex when this is called. |
1668 | has_owner_ = false; |
1669 | GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_)); |
1670 | } |
1671 | |
1672 | // Does nothing if the current thread holds the mutex. Otherwise, crashes |
1673 | // with high probability. |
1674 | void AssertHeld() const { |
1675 | GTEST_CHECK_(has_owner_ && pthread_equal(owner_, pthread_self())) |
1676 | << "The current thread is not holding the mutex @" << this; |
1677 | } |
1678 | |
1679 | // A static mutex may be used before main() is entered. It may even |
1680 | // be used before the dynamic initialization stage. Therefore we |
1681 | // must be able to initialize a static mutex object at link time. |
1682 | // This means MutexBase has to be a POD and its member variables |
1683 | // have to be public. |
1684 | public: |
1685 | pthread_mutex_t mutex_; // The underlying pthread mutex. |
1686 | // has_owner_ indicates whether the owner_ field below contains a valid thread |
1687 | // ID and is therefore safe to inspect (e.g., to use in pthread_equal()). All |
1688 | // accesses to the owner_ field should be protected by a check of this field. |
1689 | // An alternative might be to memset() owner_ to all zeros, but there's no |
1690 | // guarantee that a zero'd pthread_t is necessarily invalid or even different |
1691 | // from pthread_self(). |
1692 | bool has_owner_; |
1693 | pthread_t owner_; // The thread holding the mutex. |
1694 | }; |
1695 | |
1696 | // Forward-declares a static mutex. |
1697 | # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ |
1698 | extern ::testing::internal::MutexBase mutex |
1699 | |
1700 | // Defines and statically (i.e. at link time) initializes a static mutex. |
1701 | // The initialization list here does not explicitly initialize each field, |
1702 | // instead relying on default initialization for the unspecified fields. In |
1703 | // particular, the owner_ field (a pthread_t) is not explicitly initialized. |
1704 | // This allows initialization to work whether pthread_t is a scalar or struct. |
1705 | // The flag -Wmissing-field-initializers must not be specified for this to work. |
1706 | #define GTEST_DEFINE_STATIC_MUTEX_(mutex) \ |
1707 | ::testing::internal::MutexBase mutex = {PTHREAD_MUTEX_INITIALIZER, false, 0} |
1708 | |
1709 | // The Mutex class can only be used for mutexes created at runtime. It |
1710 | // shares its API with MutexBase otherwise. |
1711 | class Mutex : public MutexBase { |
1712 | public: |
1713 | Mutex() { |
1714 | GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, nullptr)); |
1715 | has_owner_ = false; |
1716 | } |
1717 | ~Mutex() { |
1718 | GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_)); |
1719 | } |
1720 | |
1721 | private: |
1722 | GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex); |
1723 | }; |
1724 | |
1725 | // We cannot name this class MutexLock because the ctor declaration would |
1726 | // conflict with a macro named MutexLock, which is defined on some |
1727 | // platforms. That macro is used as a defensive measure to prevent against |
1728 | // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than |
1729 | // "MutexLock l(&mu)". Hence the typedef trick below. |
1730 | class GTestMutexLock { |
1731 | public: |
1732 | explicit GTestMutexLock(MutexBase* mutex) |
1733 | : mutex_(mutex) { mutex_->Lock(); } |
1734 | |
1735 | ~GTestMutexLock() { mutex_->Unlock(); } |
1736 | |
1737 | private: |
1738 | MutexBase* const mutex_; |
1739 | |
1740 | GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock); |
1741 | }; |
1742 | |
1743 | typedef GTestMutexLock MutexLock; |
1744 | |
1745 | // Helpers for ThreadLocal. |
1746 | |
1747 | // pthread_key_create() requires DeleteThreadLocalValue() to have |
1748 | // C-linkage. Therefore it cannot be templatized to access |
1749 | // ThreadLocal<T>. Hence the need for class |
1750 | // ThreadLocalValueHolderBase. |
1751 | class ThreadLocalValueHolderBase { |
1752 | public: |
1753 | virtual ~ThreadLocalValueHolderBase() {} |
1754 | }; |
1755 | |
1756 | // Called by pthread to delete thread-local data stored by |
1757 | // pthread_setspecific(). |
1758 | extern "C" inline void DeleteThreadLocalValue(void* value_holder) { |
1759 | delete static_cast<ThreadLocalValueHolderBase*>(value_holder); |
1760 | } |
1761 | |
1762 | // Implements thread-local storage on pthreads-based systems. |
1763 | template <typename T> |
1764 | class GTEST_API_ ThreadLocal { |
1765 | public: |
1766 | ThreadLocal() |
1767 | : key_(CreateKey()), default_factory_(new DefaultValueHolderFactory()) {} |
1768 | explicit ThreadLocal(const T& value) |
1769 | : key_(CreateKey()), |
1770 | default_factory_(new InstanceValueHolderFactory(value)) {} |
1771 | |
1772 | ~ThreadLocal() { |
1773 | // Destroys the managed object for the current thread, if any. |
1774 | DeleteThreadLocalValue(pthread_getspecific(key_)); |
1775 | |
1776 | // Releases resources associated with the key. This will *not* |
1777 | // delete managed objects for other threads. |
1778 | GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_)); |
1779 | } |
1780 | |
1781 | T* pointer() { return GetOrCreateValue(); } |
1782 | const T* pointer() const { return GetOrCreateValue(); } |
1783 | const T& get() const { return *pointer(); } |
1784 | void set(const T& value) { *pointer() = value; } |
1785 | |
1786 | private: |
1787 | // Holds a value of type T. |
1788 | class ValueHolder : public ThreadLocalValueHolderBase { |
1789 | public: |
1790 | ValueHolder() : value_() {} |
1791 | explicit ValueHolder(const T& value) : value_(value) {} |
1792 | |
1793 | T* pointer() { return &value_; } |
1794 | |
1795 | private: |
1796 | T value_; |
1797 | GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder); |
1798 | }; |
1799 | |
1800 | static pthread_key_t CreateKey() { |
1801 | pthread_key_t key; |
1802 | // When a thread exits, DeleteThreadLocalValue() will be called on |
1803 | // the object managed for that thread. |
1804 | GTEST_CHECK_POSIX_SUCCESS_( |
1805 | pthread_key_create(&key, &DeleteThreadLocalValue)); |
1806 | return key; |
1807 | } |
1808 | |
1809 | T* GetOrCreateValue() const { |
1810 | ThreadLocalValueHolderBase* const holder = |
1811 | static_cast<ThreadLocalValueHolderBase*>(pthread_getspecific(key_)); |
1812 | if (holder != nullptr) { |
1813 | return CheckedDowncastToActualType<ValueHolder>(holder)->pointer(); |
1814 | } |
1815 | |
1816 | ValueHolder* const new_holder = default_factory_->MakeNewHolder(); |
1817 | ThreadLocalValueHolderBase* const holder_base = new_holder; |
1818 | GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base)); |
1819 | return new_holder->pointer(); |
1820 | } |
1821 | |
1822 | class ValueHolderFactory { |
1823 | public: |
1824 | ValueHolderFactory() {} |
1825 | virtual ~ValueHolderFactory() {} |
1826 | virtual ValueHolder* MakeNewHolder() const = 0; |
1827 | |
1828 | private: |
1829 | GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolderFactory); |
1830 | }; |
1831 | |
1832 | class DefaultValueHolderFactory : public ValueHolderFactory { |
1833 | public: |
1834 | DefaultValueHolderFactory() {} |
1835 | virtual ValueHolder* MakeNewHolder() const { return new ValueHolder(); } |
1836 | |
1837 | private: |
1838 | GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultValueHolderFactory); |
1839 | }; |
1840 | |
1841 | class InstanceValueHolderFactory : public ValueHolderFactory { |
1842 | public: |
1843 | explicit InstanceValueHolderFactory(const T& value) : value_(value) {} |
1844 | virtual ValueHolder* MakeNewHolder() const { |
1845 | return new ValueHolder(value_); |
1846 | } |
1847 | |
1848 | private: |
1849 | const T value_; // The value for each thread. |
1850 | |
1851 | GTEST_DISALLOW_COPY_AND_ASSIGN_(InstanceValueHolderFactory); |
1852 | }; |
1853 | |
1854 | // A key pthreads uses for looking up per-thread values. |
1855 | const pthread_key_t key_; |
1856 | std::unique_ptr<ValueHolderFactory> default_factory_; |
1857 | |
1858 | GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal); |
1859 | }; |
1860 | |
1861 | # endif // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ |
1862 | |
1863 | #else // GTEST_IS_THREADSAFE |
1864 | |
1865 | // A dummy implementation of synchronization primitives (mutex, lock, |
1866 | // and thread-local variable). Necessary for compiling Google Test where |
1867 | // mutex is not supported - using Google Test in multiple threads is not |
1868 | // supported on such platforms. |
1869 | |
1870 | class Mutex { |
1871 | public: |
1872 | Mutex() {} |
1873 | void Lock() {} |
1874 | void Unlock() {} |
1875 | void AssertHeld() const {} |
1876 | }; |
1877 | |
1878 | # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ |
1879 | extern ::testing::internal::Mutex mutex |
1880 | |
1881 | # define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex |
1882 | |
1883 | // We cannot name this class MutexLock because the ctor declaration would |
1884 | // conflict with a macro named MutexLock, which is defined on some |
1885 | // platforms. That macro is used as a defensive measure to prevent against |
1886 | // inadvertent misuses of MutexLock like "MutexLock(&mu)" rather than |
1887 | // "MutexLock l(&mu)". Hence the typedef trick below. |
1888 | class GTestMutexLock { |
1889 | public: |
1890 | explicit GTestMutexLock(Mutex*) {} // NOLINT |
1891 | }; |
1892 | |
1893 | typedef GTestMutexLock MutexLock; |
1894 | |
1895 | template <typename T> |
1896 | class GTEST_API_ ThreadLocal { |
1897 | public: |
1898 | ThreadLocal() : value_() {} |
1899 | explicit ThreadLocal(const T& value) : value_(value) {} |
1900 | T* pointer() { return &value_; } |
1901 | const T* pointer() const { return &value_; } |
1902 | const T& get() const { return value_; } |
1903 | void set(const T& value) { value_ = value; } |
1904 | private: |
1905 | T value_; |
1906 | }; |
1907 | |
1908 | #endif // GTEST_IS_THREADSAFE |
1909 | |
1910 | // Returns the number of threads running in the process, or 0 to indicate that |
1911 | // we cannot detect it. |
1912 | GTEST_API_ size_t GetThreadCount(); |
1913 | |
1914 | template <bool bool_value> |
1915 | struct bool_constant { |
1916 | typedef bool_constant<bool_value> type; |
1917 | static const bool value = bool_value; |
1918 | }; |
1919 | template <bool bool_value> const bool bool_constant<bool_value>::value; |
1920 | |
1921 | typedef bool_constant<false> false_type; |
1922 | typedef bool_constant<true> true_type; |
1923 | |
1924 | template <typename Iterator> |
1925 | struct IteratorTraits { |
1926 | typedef typename Iterator::value_type value_type; |
1927 | }; |
1928 | |
1929 | |
1930 | template <typename T> |
1931 | struct IteratorTraits<T*> { |
1932 | typedef T value_type; |
1933 | }; |
1934 | |
1935 | template <typename T> |
1936 | struct IteratorTraits<const T*> { |
1937 | typedef T value_type; |
1938 | }; |
1939 | |
1940 | #if GTEST_OS_WINDOWS |
1941 | # define GTEST_PATH_SEP_ "\\" |
1942 | # define GTEST_HAS_ALT_PATH_SEP_ 1 |
1943 | // The biggest signed integer type the compiler supports. |
1944 | typedef __int64 BiggestInt; |
1945 | #else |
1946 | # define GTEST_PATH_SEP_ "/" |
1947 | # define GTEST_HAS_ALT_PATH_SEP_ 0 |
1948 | typedef long long BiggestInt; // NOLINT |
1949 | #endif // GTEST_OS_WINDOWS |
1950 | |
1951 | // Utilities for char. |
1952 | |
1953 | // isspace(int ch) and friends accept an unsigned char or EOF. char |
1954 | // may be signed, depending on the compiler (or compiler flags). |
1955 | // Therefore we need to cast a char to unsigned char before calling |
1956 | // isspace(), etc. |
1957 | |
1958 | inline bool IsAlpha(char ch) { |
1959 | return isalpha(static_cast<unsigned char>(ch)) != 0; |
1960 | } |
1961 | inline bool IsAlNum(char ch) { |
1962 | return isalnum(static_cast<unsigned char>(ch)) != 0; |
1963 | } |
1964 | inline bool IsDigit(char ch) { |
1965 | return isdigit(static_cast<unsigned char>(ch)) != 0; |
1966 | } |
1967 | inline bool IsLower(char ch) { |
1968 | return islower(static_cast<unsigned char>(ch)) != 0; |
1969 | } |
1970 | inline bool IsSpace(char ch) { |
1971 | return isspace(static_cast<unsigned char>(ch)) != 0; |
1972 | } |
1973 | inline bool IsUpper(char ch) { |
1974 | return isupper(static_cast<unsigned char>(ch)) != 0; |
1975 | } |
1976 | inline bool IsXDigit(char ch) { |
1977 | return isxdigit(static_cast<unsigned char>(ch)) != 0; |
1978 | } |
1979 | inline bool IsXDigit(wchar_t ch) { |
1980 | const unsigned char low_byte = static_cast<unsigned char>(ch); |
1981 | return ch == low_byte && isxdigit(low_byte) != 0; |
1982 | } |
1983 | |
1984 | inline char ToLower(char ch) { |
1985 | return static_cast<char>(tolower(static_cast<unsigned char>(ch))); |
1986 | } |
1987 | inline char ToUpper(char ch) { |
1988 | return static_cast<char>(toupper(static_cast<unsigned char>(ch))); |
1989 | } |
1990 | |
1991 | inline std::string StripTrailingSpaces(std::string str) { |
1992 | std::string::iterator it = str.end(); |
1993 | while (it != str.begin() && IsSpace(*--it)) |
1994 | it = str.erase(it); |
1995 | return str; |
1996 | } |
1997 | |
1998 | // The testing::internal::posix namespace holds wrappers for common |
1999 | // POSIX functions. These wrappers hide the differences between |
2000 | // Windows/MSVC and POSIX systems. Since some compilers define these |
2001 | // standard functions as macros, the wrapper cannot have the same name |
2002 | // as the wrapped function. |
2003 | |
2004 | namespace posix { |
2005 | |
2006 | // Functions with a different name on Windows. |
2007 | |
2008 | #if GTEST_OS_WINDOWS |
2009 | |
2010 | typedef struct _stat StatStruct; |
2011 | |
2012 | # ifdef __BORLANDC__ |
2013 | inline int IsATTY(int fd) { return isatty(fd); } |
2014 | inline int StrCaseCmp(const char* s1, const char* s2) { |
2015 | return stricmp(s1, s2); |
2016 | } |
2017 | inline char* StrDup(const char* src) { return strdup(src); } |
2018 | # else // !__BORLANDC__ |
2019 | # if GTEST_OS_WINDOWS_MOBILE |
2020 | inline int IsATTY(int /* fd */) { return 0; } |
2021 | # else |
2022 | inline int IsATTY(int fd) { return _isatty(fd); } |
2023 | # endif // GTEST_OS_WINDOWS_MOBILE |
2024 | inline int StrCaseCmp(const char* s1, const char* s2) { |
2025 | return _stricmp(s1, s2); |
2026 | } |
2027 | inline char* StrDup(const char* src) { return _strdup(src); } |
2028 | # endif // __BORLANDC__ |
2029 | |
2030 | # if GTEST_OS_WINDOWS_MOBILE |
2031 | inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); } |
2032 | // Stat(), RmDir(), and IsDir() are not needed on Windows CE at this |
2033 | // time and thus not defined there. |
2034 | # else |
2035 | inline int FileNo(FILE* file) { return _fileno(file); } |
2036 | inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); } |
2037 | inline int RmDir(const char* dir) { return _rmdir(dir); } |
2038 | inline bool IsDir(const StatStruct& st) { |
2039 | return (_S_IFDIR & st.st_mode) != 0; |
2040 | } |
2041 | # endif // GTEST_OS_WINDOWS_MOBILE |
2042 | |
2043 | #else |
2044 | |
2045 | typedef struct stat StatStruct; |
2046 | |
2047 | inline int FileNo(FILE* file) { return fileno(file); } |
2048 | inline int IsATTY(int fd) { return isatty(fd); } |
2049 | inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); } |
2050 | inline int StrCaseCmp(const char* s1, const char* s2) { |
2051 | return strcasecmp(s1, s2); |
2052 | } |
2053 | inline char* StrDup(const char* src) { return strdup(src); } |
2054 | inline int RmDir(const char* dir) { return rmdir(dir); } |
2055 | inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } |
2056 | |
2057 | #endif // GTEST_OS_WINDOWS |
2058 | |
2059 | // Functions deprecated by MSVC 8.0. |
2060 | |
2061 | GTEST_DISABLE_MSC_DEPRECATED_PUSH_() |
2062 | |
2063 | inline const char* StrNCpy(char* dest, const char* src, size_t n) { |
2064 | return strncpy(dest, src, n); |
2065 | } |
2066 | |
2067 | // ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and |
2068 | // StrError() aren't needed on Windows CE at this time and thus not |
2069 | // defined there. |
2070 | |
2071 | #if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT |
2072 | inline int ChDir(const char* dir) { return chdir(dir); } |
2073 | #endif |
2074 | inline FILE* FOpen(const char* path, const char* mode) { |
2075 | return fopen(path, mode); |
2076 | } |
2077 | #if !GTEST_OS_WINDOWS_MOBILE |
2078 | inline FILE *FReopen(const char* path, const char* mode, FILE* stream) { |
2079 | return freopen(path, mode, stream); |
2080 | } |
2081 | inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); } |
2082 | #endif |
2083 | inline int FClose(FILE* fp) { return fclose(fp); } |
2084 | #if !GTEST_OS_WINDOWS_MOBILE |
2085 | inline int Read(int fd, void* buf, unsigned int count) { |
2086 | return static_cast<int>(read(fd, buf, count)); |
2087 | } |
2088 | inline int Write(int fd, const void* buf, unsigned int count) { |
2089 | return static_cast<int>(write(fd, buf, count)); |
2090 | } |
2091 | inline int Close(int fd) { return close(fd); } |
2092 | inline const char* StrError(int errnum) { return strerror(errnum); } |
2093 | #endif |
2094 | inline const char* GetEnv(const char* name) { |
2095 | #if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT |
2096 | // We are on Windows CE, which has no environment variables. |
2097 | static_cast<void>(name); // To prevent 'unused argument' warning. |
2098 | return nullptr; |
2099 | #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9) |
2100 | // Environment variables which we programmatically clear will be set to the |
2101 | // empty string rather than unset (NULL). Handle that case. |
2102 | const char* const env = getenv(name); |
2103 | return (env != nullptr && env[0] != '\0') ? env : nullptr; |
2104 | #else |
2105 | return getenv(name); |
2106 | #endif |
2107 | } |
2108 | |
2109 | GTEST_DISABLE_MSC_DEPRECATED_POP_() |
2110 | |
2111 | #if GTEST_OS_WINDOWS_MOBILE |
2112 | // Windows CE has no C library. The abort() function is used in |
2113 | // several places in Google Test. This implementation provides a reasonable |
2114 | // imitation of standard behaviour. |
2115 | [[noreturn]] void Abort(); |
2116 | #else |
2117 | [[noreturn]] inline void Abort() { abort(); } |
2118 | #endif // GTEST_OS_WINDOWS_MOBILE |
2119 | |
2120 | } // namespace posix |
2121 | |
2122 | // MSVC "deprecates" snprintf and issues warnings wherever it is used. In |
2123 | // order to avoid these warnings, we need to use _snprintf or _snprintf_s on |
2124 | // MSVC-based platforms. We map the GTEST_SNPRINTF_ macro to the appropriate |
2125 | // function in order to achieve that. We use macro definition here because |
2126 | // snprintf is a variadic function. |
2127 | #if _MSC_VER && !GTEST_OS_WINDOWS_MOBILE |
2128 | // MSVC 2005 and above support variadic macros. |
2129 | # define GTEST_SNPRINTF_(buffer, size, format, ...) \ |
2130 | _snprintf_s(buffer, size, size, format, __VA_ARGS__) |
2131 | #elif defined(_MSC_VER) |
2132 | // Windows CE does not define _snprintf_s |
2133 | # define GTEST_SNPRINTF_ _snprintf |
2134 | #else |
2135 | # define GTEST_SNPRINTF_ snprintf |
2136 | #endif |
2137 | |
2138 | // The maximum number a BiggestInt can represent. This definition |
2139 | // works no matter BiggestInt is represented in one's complement or |
2140 | // two's complement. |
2141 | // |
2142 | // We cannot rely on numeric_limits in STL, as __int64 and long long |
2143 | // are not part of standard C++ and numeric_limits doesn't need to be |
2144 | // defined for them. |
2145 | const BiggestInt kMaxBiggestInt = |
2146 | ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1)); |
2147 | |
2148 | // This template class serves as a compile-time function from size to |
2149 | // type. It maps a size in bytes to a primitive type with that |
2150 | // size. e.g. |
2151 | // |
2152 | // TypeWithSize<4>::UInt |
2153 | // |
2154 | // is typedef-ed to be unsigned int (unsigned integer made up of 4 |
2155 | // bytes). |
2156 | // |
2157 | // Such functionality should belong to STL, but I cannot find it |
2158 | // there. |
2159 | // |
2160 | // Google Test uses this class in the implementation of floating-point |
2161 | // comparison. |
2162 | // |
2163 | // For now it only handles UInt (unsigned int) as that's all Google Test |
2164 | // needs. Other types can be easily added in the future if need |
2165 | // arises. |
2166 | template <size_t size> |
2167 | class TypeWithSize { |
2168 | public: |
2169 | // This prevents the user from using TypeWithSize<N> with incorrect |
2170 | // values of N. |
2171 | typedef void UInt; |
2172 | }; |
2173 | |
2174 | // The specialization for size 4. |
2175 | template <> |
2176 | class TypeWithSize<4> { |
2177 | public: |
2178 | // unsigned int has size 4 in both gcc and MSVC. |
2179 | // |
2180 | // As base/basictypes.h doesn't compile on Windows, we cannot use |
2181 | // uint32, uint64, and etc here. |
2182 | typedef int Int; |
2183 | typedef unsigned int UInt; |
2184 | }; |
2185 | |
2186 | // The specialization for size 8. |
2187 | template <> |
2188 | class TypeWithSize<8> { |
2189 | public: |
2190 | #if GTEST_OS_WINDOWS |
2191 | typedef __int64 Int; |
2192 | typedef unsigned __int64 UInt; |
2193 | #else |
2194 | typedef long long Int; // NOLINT |
2195 | typedef unsigned long long UInt; // NOLINT |
2196 | #endif // GTEST_OS_WINDOWS |
2197 | }; |
2198 | |
2199 | // Integer types of known sizes. |
2200 | typedef TypeWithSize<4>::Int Int32; |
2201 | typedef TypeWithSize<4>::UInt UInt32; |
2202 | typedef TypeWithSize<8>::Int Int64; |
2203 | typedef TypeWithSize<8>::UInt UInt64; |
2204 | typedef TypeWithSize<8>::Int TimeInMillis; // Represents time in milliseconds. |
2205 | |
2206 | // Utilities for command line flags and environment variables. |
2207 | |
2208 | // Macro for referencing flags. |
2209 | #if !defined(GTEST_FLAG) |
2210 | # define GTEST_FLAG(name) FLAGS_gtest_##name |
2211 | #endif // !defined(GTEST_FLAG) |
2212 | |
2213 | #if !defined(GTEST_USE_OWN_FLAGFILE_FLAG_) |
2214 | # define GTEST_USE_OWN_FLAGFILE_FLAG_ 1 |
2215 | #endif // !defined(GTEST_USE_OWN_FLAGFILE_FLAG_) |
2216 | |
2217 | #if !defined(GTEST_DECLARE_bool_) |
2218 | # define GTEST_FLAG_SAVER_ ::testing::internal::GTestFlagSaver |
2219 | |
2220 | // Macros for declaring flags. |
2221 | # define GTEST_DECLARE_bool_(name) GTEST_API_ extern bool GTEST_FLAG(name) |
2222 | # define GTEST_DECLARE_int32_(name) \ |
2223 | GTEST_API_ extern ::testing::internal::Int32 GTEST_FLAG(name) |
2224 | # define GTEST_DECLARE_string_(name) \ |
2225 | GTEST_API_ extern ::std::string GTEST_FLAG(name) |
2226 | |
2227 | // Macros for defining flags. |
2228 | # define GTEST_DEFINE_bool_(name, default_val, doc) \ |
2229 | GTEST_API_ bool GTEST_FLAG(name) = (default_val) |
2230 | # define GTEST_DEFINE_int32_(name, default_val, doc) \ |
2231 | GTEST_API_ ::testing::internal::Int32 GTEST_FLAG(name) = (default_val) |
2232 | # define GTEST_DEFINE_string_(name, default_val, doc) \ |
2233 | GTEST_API_ ::std::string GTEST_FLAG(name) = (default_val) |
2234 | |
2235 | #endif // !defined(GTEST_DECLARE_bool_) |
2236 | |
2237 | // Thread annotations |
2238 | #if !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_) |
2239 | # define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks) |
2240 | # define GTEST_LOCK_EXCLUDED_(locks) |
2241 | #endif // !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_) |
2242 | |
2243 | // Parses 'str' for a 32-bit signed integer. If successful, writes the result |
2244 | // to *value and returns true; otherwise leaves *value unchanged and returns |
2245 | // false. |
2246 | bool ParseInt32(const Message& src_text, const char* str, Int32* value); |
2247 | |
2248 | // Parses a bool/Int32/string from the environment variable |
2249 | // corresponding to the given Google Test flag. |
2250 | bool BoolFromGTestEnv(const char* flag, bool default_val); |
2251 | GTEST_API_ Int32 Int32FromGTestEnv(const char* flag, Int32 default_val); |
2252 | std::string OutputFlagAlsoCheckEnvVar(); |
2253 | const char* StringFromGTestEnv(const char* flag, const char* default_val); |
2254 | |
2255 | } // namespace internal |
2256 | } // namespace testing |
2257 | |
2258 | #if !defined(GTEST_INTERNAL_DEPRECATED) |
2259 | |
2260 | // Internal Macro to mark an API deprecated, for googletest usage only |
2261 | // Usage: class GTEST_INTERNAL_DEPRECATED(message) MyClass or |
2262 | // GTEST_INTERNAL_DEPRECATED(message) <return_type> myFunction(); Every usage of |
2263 | // a deprecated entity will trigger a warning when compiled with |
2264 | // `-Wdeprecated-declarations` option (clang, gcc, any __GNUC__ compiler). |
2265 | // For msvc /W3 option will need to be used |
2266 | // Note that for 'other' compilers this macro evaluates to nothing to prevent |
2267 | // compilations errors. |
2268 | #if defined(_MSC_VER) |
2269 | #define GTEST_INTERNAL_DEPRECATED(message) __declspec(deprecated(message)) |
2270 | #elif defined(__GNUC__) |
2271 | #define GTEST_INTERNAL_DEPRECATED(message) __attribute__((deprecated(message))) |
2272 | #else |
2273 | #define GTEST_INTERNAL_DEPRECATED(message) |
2274 | #endif |
2275 | |
2276 | #endif // !defined(GTEST_INTERNAL_DEPRECATED) |
2277 | |
2278 | #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ |
2279 | |