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 // Authors: wan@google.com (Zhanyong Wan)
31 //
32 // Low-level types and utilities for porting Google Test to various
33 // platforms. They are subject to change without notice. DO NOT USE
34 // THEM IN USER CODE.
35
36 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
37 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
38
39 // The user can define the following macros in the build script to
40 // control Google Test's behavior. If the user doesn't define a macro
41 // in this list, Google Test will define it.
42 //
43 // GTEST_HAS_CLONE - Define it to 1/0 to indicate that clone(2)
44 // is/isn't available.
45 // GTEST_HAS_GLOBAL_STRING - Define it to 1/0 to indicate that ::string
46 // is/isn't available (some systems define
47 // ::string, which is different to std::string).
48 // GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::string
49 // is/isn't available (some systems define
50 // ::wstring, which is different to std::wstring).
51 // GTEST_HAS_PTHREAD - Define it to 1/0 to indicate that <pthread.h>
52 // is/isn't available.
53 // GTEST_HAS_RTTI - Define it to 1/0 to indicate that RTTI is/isn't
54 // enabled.
55 // GTEST_HAS_STD_STRING - Define it to 1/0 to indicate that
56 // std::string does/doesn't work (Google Test can
57 // be used where std::string is unavailable).
58 // GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that
59 // std::wstring does/doesn't work (Google Test can
60 // be used where std::wstring is unavailable).
61 // GTEST_HAS_TR1_TUPLE 1 - Define it to 1/0 to indicate tr1::tuple
62 // is/isn't available.
63
64 // This header defines the following utilities:
65 //
66 // Macros indicating the current platform (defined to 1 if compiled on
67 // the given platform; otherwise undefined):
68 // GTEST_OS_CYGWIN - Cygwin
69 // GTEST_OS_LINUX - Linux
70 // GTEST_OS_MAC - Mac OS X
71 // GTEST_OS_SOLARIS - Sun Solaris
72 // GTEST_OS_SYMBIAN - Symbian
73 // GTEST_OS_WINDOWS - Windows
74 // GTEST_OS_ZOS - z/OS
75 //
76 // Among the platforms, Cygwin, Linux, Max OS X, and Windows have the
77 // most stable support. Since core members of the Google Test project
78 // don't have access to other platforms, support for them may be less
79 // stable. If you notice any problems on your platform, please notify
80 // googletestframework@googlegroups.com (patches for fixing them are
81 // even more welcome!).
82 //
83 // Note that it is possible that none of the GTEST_OS_* macros are defined.
84 //
85 // Macros indicating available Google Test features (defined to 1 if
86 // the corresponding feature is supported; otherwise undefined):
87 // GTEST_HAS_COMBINE - the Combine() function (for value-parameterized
88 // tests)
89 // GTEST_HAS_DEATH_TEST - death tests
90 // GTEST_HAS_PARAM_TEST - value-parameterized tests
91 // GTEST_HAS_TYPED_TEST - typed tests
92 // GTEST_HAS_TYPED_TEST_P - type-parameterized tests
93 // GTEST_USES_POSIX_RE - enhanced POSIX regex is used.
94 // GTEST_USES_SIMPLE_RE - our own simple regex is used;
95 // the above two are mutually exclusive.
96 //
97 // Macros for basic C++ coding:
98 // GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.
99 // GTEST_ATTRIBUTE_UNUSED_ - declares that a class' instances don't have to
100 // be used.
101 // GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=.
102 // GTEST_MUST_USE_RESULT_ - declares that a function's result must be used.
103 //
104 // Synchronization:
105 // Mutex, MutexLock, ThreadLocal, GetThreadCount()
106 // - synchronization primitives.
107 // GTEST_IS_THREADSAFE - defined to 1 to indicate that the above
108 // synchronization primitives have real implementations
109 // and Google Test is thread-safe; or 0 otherwise.
110 //
111 // Template meta programming:
112 // is_pointer - as in TR1; needed on Symbian and IBM XL C/C++ only.
113 //
114 // Smart pointers:
115 // scoped_ptr - as in TR2.
116 //
117 // Regular expressions:
118 // RE - a simple regular expression class using the POSIX
119 // Extended Regular Expression syntax. Not available on
120 // Windows.
121 //
122 // Logging:
123 // GTEST_LOG_() - logs messages at the specified severity level.
124 // LogToStderr() - directs all log messages to stderr.
125 // FlushInfoLog() - flushes informational log messages.
126 //
127 // Stderr capturing:
128 // CaptureStderr() - starts capturing stderr.
129 // GetCapturedStderr() - stops capturing stderr and returns the captured
130 // string.
131 //
132 // Integer types:
133 // TypeWithSize - maps an integer to a int type.
134 // Int32, UInt32, Int64, UInt64, TimeInMillis
135 // - integers of known sizes.
136 // BiggestInt - the biggest signed integer type.
137 //
138 // Command-line utilities:
139 // GTEST_FLAG() - references a flag.
140 // GTEST_DECLARE_*() - declares a flag.
141 // GTEST_DEFINE_*() - defines a flag.
142 // GetArgvs() - returns the command line as a vector of strings.
143 //
144 // Environment variable utilities:
145 // GetEnv() - gets the value of an environment variable.
146 // BoolFromGTestEnv() - parses a bool environment variable.
147 // Int32FromGTestEnv() - parses an Int32 environment variable.
148 // StringFromGTestEnv() - parses a string environment variable.
149
150 #include <stdlib.h>
151 #include <stdio.h>
152 #include <iostream> // Used for GTEST_CHECK_
153
154 #define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
155 #define GTEST_FLAG_PREFIX_ "gtest_"
156 #define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
157 #define GTEST_NAME_ "Google Test"
158 #define GTEST_PROJECT_URL_ "http://code.google.com/p/googletest/"
159
160 // Determines the version of gcc that is used to compile this.
161 #ifdef __GNUC__
162 // 40302 means version 4.3.2.
163 #define GTEST_GCC_VER_ \
164 (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
165 #endif // __GNUC__
166
167 // Determines the platform on which Google Test is compiled.
168 #ifdef __CYGWIN__
169 #define GTEST_OS_CYGWIN 1
170 #elif __SYMBIAN32__
171 #define GTEST_OS_SYMBIAN 1
172 #elif defined _MSC_VER
173 // TODO(kenton@google.com): GTEST_OS_WINDOWS is currently used to mean
174 // both "The OS is Windows" and "The compiler is MSVC". These
175 // meanings really should be separated in order to better support
176 // Windows compilers other than MSVC.
177 #define GTEST_OS_WINDOWS 1
178 #elif defined __APPLE__
179 #define GTEST_OS_MAC 1
180 #elif defined __linux__
181 #define GTEST_OS_LINUX 1
182 #elif defined __MVS__
183 #define GTEST_OS_ZOS 1
184 #elif defined(__sun) && defined(__SVR4)
185 #define GTEST_OS_SOLARIS 1
186 #endif // _MSC_VER
187
188 #if GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC
189
190 // On some platforms, <regex.h> needs someone to define size_t, and
191 // won't compile otherwise. We can #include it here as we already
192 // included <stdlib.h>, which is guaranteed to define size_t through
193 // <stddef.h>.
194 #include <regex.h> // NOLINT
195 #define GTEST_USES_POSIX_RE 1
196
197 #else
198
199 // <regex.h> may not be available on this platform. Use our own
200 // simple regex implementation instead.
201 #define GTEST_USES_SIMPLE_RE 1
202
203 #endif // GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC
204
205 // Defines GTEST_HAS_EXCEPTIONS to 1 if exceptions are enabled, or 0
206 // otherwise.
207
208 #ifdef _MSC_VER // Compiled by MSVC?
209 // Assumes that exceptions are enabled by default.
210 #ifndef _HAS_EXCEPTIONS // MSVC uses this macro to enable exceptions.
211 #define _HAS_EXCEPTIONS 1
212 #endif // _HAS_EXCEPTIONS
213 #define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
214 #else // The compiler is not MSVC.
215 // gcc defines __EXCEPTIONS to 1 iff exceptions are enabled. For
216 // other compilers, we assume exceptions are disabled to be
217 // conservative.
218 #if defined(__GNUC__) && __EXCEPTIONS
219 #define GTEST_HAS_EXCEPTIONS 1
220 #else
221 #define GTEST_HAS_EXCEPTIONS 0
222 #endif // defined(__GNUC__) && __EXCEPTIONS
223 #endif // _MSC_VER
224
225 // Determines whether ::std::string and ::string are available.
226
227 #ifndef GTEST_HAS_STD_STRING
228 // The user didn't tell us whether ::std::string is available, so we
229 // need to figure it out. The only environment that we know
230 // ::std::string is not available is MSVC 7.1 or lower with exceptions
231 // disabled.
232 #if defined(_MSC_VER) && (_MSC_VER < 1400) && !GTEST_HAS_EXCEPTIONS
233 #define GTEST_HAS_STD_STRING 0
234 #else
235 #define GTEST_HAS_STD_STRING 1
236 #endif
237 #endif // GTEST_HAS_STD_STRING
238
239 #ifndef GTEST_HAS_GLOBAL_STRING
240 // The user didn't tell us whether ::string is available, so we need
241 // to figure it out.
242
243 #define GTEST_HAS_GLOBAL_STRING 0
244
245 #endif // GTEST_HAS_GLOBAL_STRING
246
247 #ifndef GTEST_HAS_STD_WSTRING
248 // The user didn't tell us whether ::std::wstring is available, so we need
249 // to figure it out.
250 // TODO(wan@google.com): uses autoconf to detect whether ::std::wstring
251 // is available.
252
253 #if GTEST_OS_CYGWIN || GTEST_OS_SOLARIS
254 // Cygwin 1.5 and below doesn't support ::std::wstring.
255 // Cygwin 1.7 might add wstring support; this should be updated when clear.
256 // Solaris' libc++ doesn't support it either.
257 #define GTEST_HAS_STD_WSTRING 0
258 #else
259 #define GTEST_HAS_STD_WSTRING GTEST_HAS_STD_STRING
260 #endif // GTEST_OS_CYGWIN || GTEST_OS_SOLARIS
261
262 #endif // GTEST_HAS_STD_WSTRING
263
264 #ifndef GTEST_HAS_GLOBAL_WSTRING
265 // The user didn't tell us whether ::wstring is available, so we need
266 // to figure it out.
267 #define GTEST_HAS_GLOBAL_WSTRING \
268 (GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING)
269 #endif // GTEST_HAS_GLOBAL_WSTRING
270
271 #if GTEST_HAS_STD_STRING || GTEST_HAS_GLOBAL_STRING || \
272 GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING
273 #include <string> // NOLINT
274 #endif // GTEST_HAS_STD_STRING || GTEST_HAS_GLOBAL_STRING ||
275 // GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING
276
277 #if GTEST_HAS_STD_STRING
278 #include <sstream> // NOLINT
279 #else
280 #include <strstream> // NOLINT
281 #endif // GTEST_HAS_STD_STRING
282
283 // Determines whether RTTI is available.
284 #ifndef GTEST_HAS_RTTI
285 // The user didn't tell us whether RTTI is enabled, so we need to
286 // figure it out.
287
288 #ifdef _MSC_VER
289
290 #ifdef _CPPRTTI // MSVC defines this macro iff RTTI is enabled.
291 #define GTEST_HAS_RTTI 1
292 #else
293 #define GTEST_HAS_RTTI 0
294 #endif // _CPPRTTI
295
296 #elif defined(__GNUC__)
297
298 // Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled.
299 #if GTEST_GCC_VER_ >= 40302
300 #ifdef __GXX_RTTI
301 #define GTEST_HAS_RTTI 1
302 #else
303 #define GTEST_HAS_RTTI 0
304 #endif // __GXX_RTTI
305 #else
306 // For gcc versions smaller than 4.3.2, we assume RTTI is enabled.
307 #define GTEST_HAS_RTTI 1
308 #endif // GTEST_GCC_VER >= 40302
309
310 #else
311
312 // Unknown compiler - assume RTTI is enabled.
313 #define GTEST_HAS_RTTI 1
314
315 #endif // _MSC_VER
316
317 #endif // GTEST_HAS_RTTI
318
319 // Determines whether <pthread.h> is available.
320 #ifndef GTEST_HAS_PTHREAD
321 // The user didn't tell us, so we need to figure it out.
322 #define GTEST_HAS_PTHREAD (GTEST_OS_LINUX || GTEST_OS_MAC)
323 #endif // GTEST_HAS_PTHREAD
324
325 // Determines whether tr1/tuple is available. If you have tr1/tuple
326 // on your platform, define GTEST_HAS_TR1_TUPLE=1 for both the Google
327 // Test project and your tests. If you would like Google Test to detect
328 // tr1/tuple on your platform automatically, please open an issue
329 // ticket at http://code.google.com/p/googletest.
330 #ifndef GTEST_HAS_TR1_TUPLE
331 // The user didn't tell us, so we need to figure it out.
332
333 // GCC provides <tr1/tuple> since 4.0.0.
334 #if defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000)
335 #define GTEST_HAS_TR1_TUPLE 1
336 #else
337 #define GTEST_HAS_TR1_TUPLE 0
338 #endif // __GNUC__
339 #endif // GTEST_HAS_TR1_TUPLE
340
341 // To avoid conditional compilation everywhere, we make it
342 // gtest-port.h's responsibility to #include the header implementing
343 // tr1/tuple.
344 #if GTEST_HAS_TR1_TUPLE
345 #if defined(__GNUC__)
346 // GCC implements tr1/tuple in the <tr1/tuple> header. This does not
347 // conform to the TR1 spec, which requires the header to be <tuple>.
348 #include <tr1/tuple>
349 #else
350 // If the compiler is not GCC, we assume the user is using a
351 // spec-conforming TR1 implementation.
352 #include <tuple>
353 #endif // __GNUC__
354 #endif // GTEST_HAS_TR1_TUPLE
355
356 // Determines whether clone(2) is supported.
357 // Usually it will only be available on Linux, excluding
358 // Linux on the Itanium architecture.
359 // Also see http://linux.die.net/man/2/clone.
360 #ifndef GTEST_HAS_CLONE
361 // The user didn't tell us, so we need to figure it out.
362
363 #if GTEST_OS_LINUX && !defined(__ia64__)
364 #define GTEST_HAS_CLONE 1
365 #else
366 #define GTEST_HAS_CLONE 0
367 #endif // GTEST_OS_LINUX && !defined(__ia64__)
368
369 #endif // GTEST_HAS_CLONE
370
371 // Determines whether to support death tests.
372 // Google Test does not support death tests for VC 7.1 and earlier for
373 // these reasons:
374 // 1. std::vector does not build in VC 7.1 when exceptions are disabled.
375 // 2. std::string does not build in VC 7.1 when exceptions are disabled
376 // (this is covered by GTEST_HAS_STD_STRING guard).
377 // 3. abort() in a VC 7.1 application compiled as GUI in debug config
378 // pops up a dialog window that cannot be suppressed programmatically.
379 #if GTEST_HAS_STD_STRING && (GTEST_OS_LINUX || \
380 GTEST_OS_MAC || \
381 GTEST_OS_CYGWIN || \
382 (GTEST_OS_WINDOWS && _MSC_VER >= 1400))
383 #define GTEST_HAS_DEATH_TEST 1
384 #include <vector>
385 #endif
386
387 // Determines whether to support value-parameterized tests.
388
389 #if defined(__GNUC__) || (_MSC_VER >= 1400)
390 // TODO(vladl@google.com): get the implementation rid of vector and list
391 // to compile on MSVC 7.1.
392 #define GTEST_HAS_PARAM_TEST 1
393 #endif // defined(__GNUC__) || (_MSC_VER >= 1400)
394
395 // Determines whether to support type-driven tests.
396
397 // Typed tests need <typeinfo> and variadic macros, which gcc and VC
398 // 8.0+ support.
399 #if defined(__GNUC__) || (_MSC_VER >= 1400)
400 #define GTEST_HAS_TYPED_TEST 1
401 #define GTEST_HAS_TYPED_TEST_P 1
402 #endif // defined(__GNUC__) || (_MSC_VER >= 1400)
403
404 // Determines whether to support Combine(). This only makes sense when
405 // value-parameterized tests are enabled.
406 #if GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE
407 #define GTEST_HAS_COMBINE 1
408 #endif // GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE
409
410 // Determines whether the system compiler uses UTF-16 for encoding wide strings.
411 #define GTEST_WIDE_STRING_USES_UTF16_ \
412 (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN)
413
414 // Defines some utility macros.
415
416 // The GNU compiler emits a warning if nested "if" statements are followed by
417 // an "else" statement and braces are not used to explicitly disambiguate the
418 // "else" binding. This leads to problems with code like:
419 //
420 // if (gate)
421 // ASSERT_*(condition) << "Some message";
422 //
423 // The "switch (0) case 0:" idiom is used to suppress this.
424 #ifdef __INTEL_COMPILER
425 #define GTEST_AMBIGUOUS_ELSE_BLOCKER_
426 #else
427 #define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: // NOLINT
428 #endif
429
430 // Use this annotation at the end of a struct / class definition to
431 // prevent the compiler from optimizing away instances that are never
432 // used. This is useful when all interesting logic happens inside the
433 // c'tor and / or d'tor. Example:
434 //
435 // struct Foo {
436 // Foo() { ... }
437 // } GTEST_ATTRIBUTE_UNUSED_;
438 #if defined(__GNUC__) && !defined(COMPILER_ICC)
439 #define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
440 #else
441 #define GTEST_ATTRIBUTE_UNUSED_
442 #endif
443
444 // A macro to disallow the evil copy constructor and operator= functions
445 // This should be used in the private: declarations for a class.
446 #define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\
447 type(const type &);\
448 void operator=(const type &)
449
450 // Tell the compiler to warn about unused return values for functions declared
451 // with this macro. The macro should be used on function declarations
452 // following the argument list:
453 //
454 // Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_;
455 #if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC)
456 #define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))
457 #else
458 #define GTEST_MUST_USE_RESULT_
459 #endif // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC
460
461 namespace testing {
462
463 class Message;
464
465 namespace internal {
466
467 class String;
468
469 // std::strstream is deprecated. However, we have to use it on
470 // Windows as std::stringstream won't compile on Windows when
471 // exceptions are disabled. We use std::stringstream on other
472 // platforms to avoid compiler warnings there.
473 #if GTEST_HAS_STD_STRING
474 typedef ::std::stringstream StrStream;
475 #else
476 typedef ::std::strstream StrStream;
477 #endif // GTEST_HAS_STD_STRING
478
479 // Defines scoped_ptr.
480
481 // This implementation of scoped_ptr is PARTIAL - it only contains
482 // enough stuff to satisfy Google Test's need.
483 template <typename T>
484 class scoped_ptr {
485 public:
ptr_(p)486 explicit scoped_ptr(T* p = NULL) : ptr_(p) {}
~scoped_ptr()487 ~scoped_ptr() { reset(); }
488
489 T& operator*() const { return *ptr_; }
490 T* operator->() const { return ptr_; }
get()491 T* get() const { return ptr_; }
492
release()493 T* release() {
494 T* const ptr = ptr_;
495 ptr_ = NULL;
496 return ptr;
497 }
498
499 void reset(T* p = NULL) {
500 if (p != ptr_) {
501 if (sizeof(T) > 0) { // Makes sure T is a complete type.
502 delete ptr_;
503 }
504 ptr_ = p;
505 }
506 }
507 private:
508 T* ptr_;
509
510 GTEST_DISALLOW_COPY_AND_ASSIGN_(scoped_ptr);
511 };
512
513 // Defines RE.
514
515 // A simple C++ wrapper for <regex.h>. It uses the POSIX Enxtended
516 // Regular Expression syntax.
517 class RE {
518 public:
519 // Constructs an RE from a string.
520 #if GTEST_HAS_STD_STRING
RE(const::std::string & regex)521 RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT
522 #endif // GTEST_HAS_STD_STRING
523
524 #if GTEST_HAS_GLOBAL_STRING
RE(const::string & regex)525 RE(const ::string& regex) { Init(regex.c_str()); } // NOLINT
526 #endif // GTEST_HAS_GLOBAL_STRING
527
RE(const char * regex)528 RE(const char* regex) { Init(regex); } // NOLINT
529 ~RE();
530
531 // Returns the string representation of the regex.
pattern()532 const char* pattern() const { return pattern_; }
533
534 // FullMatch(str, re) returns true iff regular expression re matches
535 // the entire str.
536 // PartialMatch(str, re) returns true iff regular expression re
537 // matches a substring of str (including str itself).
538 //
539 // TODO(wan@google.com): make FullMatch() and PartialMatch() work
540 // when str contains NUL characters.
541 #if GTEST_HAS_STD_STRING
FullMatch(const::std::string & str,const RE & re)542 static bool FullMatch(const ::std::string& str, const RE& re) {
543 return FullMatch(str.c_str(), re);
544 }
PartialMatch(const::std::string & str,const RE & re)545 static bool PartialMatch(const ::std::string& str, const RE& re) {
546 return PartialMatch(str.c_str(), re);
547 }
548 #endif // GTEST_HAS_STD_STRING
549
550 #if GTEST_HAS_GLOBAL_STRING
FullMatch(const::string & str,const RE & re)551 static bool FullMatch(const ::string& str, const RE& re) {
552 return FullMatch(str.c_str(), re);
553 }
PartialMatch(const::string & str,const RE & re)554 static bool PartialMatch(const ::string& str, const RE& re) {
555 return PartialMatch(str.c_str(), re);
556 }
557 #endif // GTEST_HAS_GLOBAL_STRING
558
559 static bool FullMatch(const char* str, const RE& re);
560 static bool PartialMatch(const char* str, const RE& re);
561
562 private:
563 void Init(const char* regex);
564
565 // We use a const char* instead of a string, as Google Test may be used
566 // where string is not available. We also do not use Google Test's own
567 // String type here, in order to simplify dependencies between the
568 // files.
569 const char* pattern_;
570 bool is_valid_;
571 #if GTEST_USES_POSIX_RE
572 regex_t full_regex_; // For FullMatch().
573 regex_t partial_regex_; // For PartialMatch().
574 #else // GTEST_USES_SIMPLE_RE
575 const char* full_pattern_; // For FullMatch();
576 #endif
577
578 GTEST_DISALLOW_COPY_AND_ASSIGN_(RE);
579 };
580
581 // Defines logging utilities:
582 // GTEST_LOG_() - logs messages at the specified severity level.
583 // LogToStderr() - directs all log messages to stderr.
584 // FlushInfoLog() - flushes informational log messages.
585
586 enum GTestLogSeverity {
587 GTEST_INFO,
588 GTEST_WARNING,
589 GTEST_ERROR,
590 GTEST_FATAL
591 };
592
593 void GTestLog(GTestLogSeverity severity, const char* file,
594 int line, const char* msg);
595
596 #define GTEST_LOG_(severity, msg)\
597 ::testing::internal::GTestLog(\
598 ::testing::internal::GTEST_##severity, __FILE__, __LINE__, \
599 (::testing::Message() << (msg)).GetString().c_str())
600
LogToStderr()601 inline void LogToStderr() {}
FlushInfoLog()602 inline void FlushInfoLog() { fflush(NULL); }
603
604 // Defines the stderr capturer:
605 // CaptureStderr - starts capturing stderr.
606 // GetCapturedStderr - stops capturing stderr and returns the captured string.
607
608 #if GTEST_HAS_STD_STRING
609 void CaptureStderr();
610 ::std::string GetCapturedStderr();
611 #endif // GTEST_HAS_STD_STRING
612
613 #if GTEST_HAS_DEATH_TEST
614
615 // A copy of all command line arguments. Set by InitGoogleTest().
616 extern ::std::vector<String> g_argvs;
617
618 // GTEST_HAS_DEATH_TEST implies we have ::std::string.
619 const ::std::vector<String>& GetArgvs();
620
621 #endif // GTEST_HAS_DEATH_TEST
622
623 // Defines synchronization primitives.
624
625 // A dummy implementation of synchronization primitives (mutex, lock,
626 // and thread-local variable). Necessary for compiling Google Test where
627 // mutex is not supported - using Google Test in multiple threads is not
628 // supported on such platforms.
629
630 class Mutex {
631 public:
Mutex()632 Mutex() {}
Mutex(int)633 explicit Mutex(int /*unused*/) {}
AssertHeld()634 void AssertHeld() const {}
635 enum { NO_CONSTRUCTOR_NEEDED_FOR_STATIC_MUTEX = 0 };
636 };
637
638 // We cannot call it MutexLock directly as the ctor declaration would
639 // conflict with a macro named MutexLock, which is defined on some
640 // platforms. Hence the typedef trick below.
641 class GTestMutexLock {
642 public:
GTestMutexLock(Mutex *)643 explicit GTestMutexLock(Mutex*) {} // NOLINT
644 };
645
646 typedef GTestMutexLock MutexLock;
647
648 template <typename T>
649 class ThreadLocal {
650 public:
ThreadLocal()651 ThreadLocal() : value_() {}
ThreadLocal(const T & value)652 explicit ThreadLocal(const T& value) : value_(value) {}
pointer()653 T* pointer() { return &value_; }
pointer()654 const T* pointer() const { return &value_; }
get()655 const T& get() const { return value_; }
set(const T & value)656 void set(const T& value) { value_ = value; }
657 private:
658 T value_;
659 };
660
661 // There's no portable way to detect the number of threads, so we just
662 // return 0 to indicate that we cannot detect it.
GetThreadCount()663 inline size_t GetThreadCount() { return 0; }
664
665 // The above synchronization primitives have dummy implementations.
666 // Therefore Google Test is not thread-safe.
667 #define GTEST_IS_THREADSAFE 0
668
669 #if defined(__SYMBIAN32__) || defined(__IBMCPP__)
670
671 // Passing non-POD classes through ellipsis (...) crashes the ARM
672 // compiler. The Nokia Symbian and the IBM XL C/C++ compiler try to
673 // instantiate a copy constructor for objects passed through ellipsis
674 // (...), failing for uncopyable objects. We define this to indicate
675 // the fact.
676 #define GTEST_ELLIPSIS_NEEDS_COPY_ 1
677
678 // The Nokia Symbian and IBM XL C/C++ compilers cannot decide between
679 // const T& and const T* in a function template. These compilers
680 // _can_ decide between class template specializations for T and T*,
681 // so a tr1::type_traits-like is_pointer works.
682 #define GTEST_NEEDS_IS_POINTER_ 1
683
684 #endif // defined(__SYMBIAN32__) || defined(__IBMCPP__)
685
686 template <bool bool_value>
687 struct bool_constant {
688 typedef bool_constant<bool_value> type;
689 static const bool value = bool_value;
690 };
691 template <bool bool_value> const bool bool_constant<bool_value>::value;
692
693 typedef bool_constant<false> false_type;
694 typedef bool_constant<true> true_type;
695
696 template <typename T>
697 struct is_pointer : public false_type {};
698
699 template <typename T>
700 struct is_pointer<T*> : public true_type {};
701
702 #if GTEST_OS_WINDOWS
703 #define GTEST_PATH_SEP_ "\\"
704 #else
705 #define GTEST_PATH_SEP_ "/"
706 #endif // GTEST_OS_WINDOWS
707
708 // Defines BiggestInt as the biggest signed integer type the compiler
709 // supports.
710 #if GTEST_OS_WINDOWS
711 typedef __int64 BiggestInt;
712 #else
713 typedef long long BiggestInt; // NOLINT
714 #endif // GTEST_OS_WINDOWS
715
716 // The maximum number a BiggestInt can represent. This definition
717 // works no matter BiggestInt is represented in one's complement or
718 // two's complement.
719 //
720 // We cannot rely on numeric_limits in STL, as __int64 and long long
721 // are not part of standard C++ and numeric_limits doesn't need to be
722 // defined for them.
723 const BiggestInt kMaxBiggestInt =
724 ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1));
725
726 // This template class serves as a compile-time function from size to
727 // type. It maps a size in bytes to a primitive type with that
728 // size. e.g.
729 //
730 // TypeWithSize<4>::UInt
731 //
732 // is typedef-ed to be unsigned int (unsigned integer made up of 4
733 // bytes).
734 //
735 // Such functionality should belong to STL, but I cannot find it
736 // there.
737 //
738 // Google Test uses this class in the implementation of floating-point
739 // comparison.
740 //
741 // For now it only handles UInt (unsigned int) as that's all Google Test
742 // needs. Other types can be easily added in the future if need
743 // arises.
744 template <size_t size>
745 class TypeWithSize {
746 public:
747 // This prevents the user from using TypeWithSize<N> with incorrect
748 // values of N.
749 typedef void UInt;
750 };
751
752 // The specialization for size 4.
753 template <>
754 class TypeWithSize<4> {
755 public:
756 // unsigned int has size 4 in both gcc and MSVC.
757 //
758 // As base/basictypes.h doesn't compile on Windows, we cannot use
759 // uint32, uint64, and etc here.
760 typedef int Int;
761 typedef unsigned int UInt;
762 };
763
764 // The specialization for size 8.
765 template <>
766 class TypeWithSize<8> {
767 public:
768 #if GTEST_OS_WINDOWS
769 typedef __int64 Int;
770 typedef unsigned __int64 UInt;
771 #else
772 typedef long long Int; // NOLINT
773 typedef unsigned long long UInt; // NOLINT
774 #endif // GTEST_OS_WINDOWS
775 };
776
777 // Integer types of known sizes.
778 typedef TypeWithSize<4>::Int Int32;
779 typedef TypeWithSize<4>::UInt UInt32;
780 typedef TypeWithSize<8>::Int Int64;
781 typedef TypeWithSize<8>::UInt UInt64;
782 typedef TypeWithSize<8>::Int TimeInMillis; // Represents time in milliseconds.
783
784 // Utilities for command line flags and environment variables.
785
786 // A wrapper for getenv() that works on Linux, Windows, and Mac OS.
787 inline const char* GetEnv(const char* name) {
788 #ifdef _WIN32_WCE // We are on Windows CE.
789 // CE has no environment variables.
790 return NULL;
791 #elif GTEST_OS_WINDOWS // We are on Windows proper.
792 // MSVC 8 deprecates getenv(), so we want to suppress warning 4996
793 // (deprecated function) there.
794 #pragma warning(push) // Saves the current warning state.
795 #pragma warning(disable:4996) // Temporarily disables warning 4996.
796 return getenv(name);
797 #pragma warning(pop) // Restores the warning state.
798 #else // We are on Linux or Mac OS.
799 return getenv(name);
800 #endif
801 }
802
803 #ifdef _WIN32_WCE
804 // Windows CE has no C library. The abort() function is used in
805 // several places in Google Test. This implementation provides a reasonable
806 // imitation of standard behaviour.
807 void abort();
808 #else
809 inline void abort() { ::abort(); }
810 #endif // _WIN32_WCE
811
812 // INTERNAL IMPLEMENTATION - DO NOT USE.
813 //
814 // GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition
815 // is not satisfied.
816 // Synopsys:
817 // GTEST_CHECK_(boolean_condition);
818 // or
819 // GTEST_CHECK_(boolean_condition) << "Additional message";
820 //
821 // This checks the condition and if the condition is not satisfied
822 // it prints message about the condition violation, including the
823 // condition itself, plus additional message streamed into it, if any,
824 // and then it aborts the program. It aborts the program irrespective of
825 // whether it is built in the debug mode or not.
826 class GTestCheckProvider {
827 public:
828 GTestCheckProvider(const char* condition, const char* file, int line) {
829 FormatFileLocation(file, line);
830 ::std::cerr << " ERROR: Condition " << condition << " failed. ";
831 }
832 ~GTestCheckProvider() {
833 ::std::cerr << ::std::endl;
834 abort();
835 }
836 void FormatFileLocation(const char* file, int line) {
837 if (file == NULL)
838 file = "unknown file";
839 if (line < 0) {
840 ::std::cerr << file << ":";
841 } else {
842 #if _MSC_VER
843 ::std::cerr << file << "(" << line << "):";
844 #else
845 ::std::cerr << file << ":" << line << ":";
846 #endif
847 }
848 }
849 ::std::ostream& GetStream() { return ::std::cerr; }
850 };
851 #define GTEST_CHECK_(condition) \
852 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
853 if (condition) \
854 ; \
855 else \
856 ::testing::internal::GTestCheckProvider(\
857 #condition, __FILE__, __LINE__).GetStream()
858
859 // Macro for referencing flags.
860 #define GTEST_FLAG(name) FLAGS_gtest_##name
861
862 // Macros for declaring flags.
863 #define GTEST_DECLARE_bool_(name) extern bool GTEST_FLAG(name)
864 #define GTEST_DECLARE_int32_(name) \
865 extern ::testing::internal::Int32 GTEST_FLAG(name)
866 #define GTEST_DECLARE_string_(name) \
867 extern ::testing::internal::String GTEST_FLAG(name)
868
869 // Macros for defining flags.
870 #define GTEST_DEFINE_bool_(name, default_val, doc) \
871 bool GTEST_FLAG(name) = (default_val)
872 #define GTEST_DEFINE_int32_(name, default_val, doc) \
873 ::testing::internal::Int32 GTEST_FLAG(name) = (default_val)
874 #define GTEST_DEFINE_string_(name, default_val, doc) \
875 ::testing::internal::String GTEST_FLAG(name) = (default_val)
876
877 // Parses 'str' for a 32-bit signed integer. If successful, writes the result
878 // to *value and returns true; otherwise leaves *value unchanged and returns
879 // false.
880 // TODO(chandlerc): Find a better way to refactor flag and environment parsing
881 // out of both gtest-port.cc and gtest.cc to avoid exporting this utility
882 // function.
883 bool ParseInt32(const Message& src_text, const char* str, Int32* value);
884
885 // Parses a bool/Int32/string from the environment variable
886 // corresponding to the given Google Test flag.
887 bool BoolFromGTestEnv(const char* flag, bool default_val);
888 Int32 Int32FromGTestEnv(const char* flag, Int32 default_val);
889 const char* StringFromGTestEnv(const char* flag, const char* default_val);
890
891 } // namespace internal
892 } // namespace testing
893
894 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
895