/Unittests/googletest/include/gtest/internal/gtest-port.h

http://unladen-swallow.googlecode.com/ · C++ Header · 1084 lines · 500 code · 158 blank · 426 comment · 46 complexity · ad17d570d68a1a18b2752989f9c3bb85 MD5 · raw file

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