gtest-port.h 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  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. // This file is fundamental to Google Test. All other Google Test source
  37. // files are expected to #include this. Therefore, it cannot #include
  38. // any other Google Test header.
  39. #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
  40. #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
  41. // The user can define the following macros in the build script to
  42. // control Google Test's behavior. If the user doesn't define a macro
  43. // in this list, Google Test will define it.
  44. //
  45. // GTEST_HAS_CLONE - Define it to 1/0 to indicate that clone(2)
  46. // is/isn't available.
  47. // GTEST_HAS_EXCEPTIONS - Define it to 1/0 to indicate that exceptions
  48. // are enabled.
  49. // GTEST_HAS_GLOBAL_STRING - Define it to 1/0 to indicate that ::string
  50. // is/isn't available (some systems define
  51. // ::string, which is different to std::string).
  52. // GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::string
  53. // is/isn't available (some systems define
  54. // ::wstring, which is different to std::wstring).
  55. // GTEST_HAS_POSIX_RE - Define it to 1/0 to indicate that POSIX regular
  56. // expressions are/aren't available.
  57. // GTEST_HAS_PTHREAD - Define it to 1/0 to indicate that <pthread.h>
  58. // is/isn't available.
  59. // GTEST_HAS_RTTI - Define it to 1/0 to indicate that RTTI is/isn't
  60. // enabled.
  61. // GTEST_HAS_STD_WSTRING - Define it to 1/0 to indicate that
  62. // std::wstring does/doesn't work (Google Test can
  63. // be used where std::wstring is unavailable).
  64. // GTEST_HAS_TR1_TUPLE - Define it to 1/0 to indicate tr1::tuple
  65. // is/isn't available.
  66. // GTEST_HAS_SEH - Define it to 1/0 to indicate whether the
  67. // compiler supports Microsoft's "Structured
  68. // Exception Handling".
  69. // GTEST_HAS_STREAM_REDIRECTION
  70. // - Define it to 1/0 to indicate whether the
  71. // platform supports I/O stream redirection using
  72. // dup() and dup2().
  73. // GTEST_USE_OWN_TR1_TUPLE - Define it to 1/0 to indicate whether Google
  74. // Test's own tr1 tuple implementation should be
  75. // used. Unused when the user sets
  76. // GTEST_HAS_TR1_TUPLE to 0.
  77. // GTEST_LANG_CXX11 - Define it to 1/0 to indicate that Google Test
  78. // is building in C++11/C++98 mode.
  79. // GTEST_LINKED_AS_SHARED_LIBRARY
  80. // - Define to 1 when compiling tests that use
  81. // Google Test as a shared library (known as
  82. // DLL on Windows).
  83. // GTEST_CREATE_SHARED_LIBRARY
  84. // - Define to 1 when compiling Google Test itself
  85. // as a shared library.
  86. // This header defines the following utilities:
  87. //
  88. // Macros indicating the current platform (defined to 1 if compiled on
  89. // the given platform; otherwise undefined):
  90. // GTEST_OS_AIX - IBM AIX
  91. // GTEST_OS_CYGWIN - Cygwin
  92. // GTEST_OS_HPUX - HP-UX
  93. // GTEST_OS_LINUX - Linux
  94. // GTEST_OS_LINUX_ANDROID - Google Android
  95. // GTEST_OS_MAC - Mac OS X
  96. // GTEST_OS_IOS - iOS
  97. // GTEST_OS_IOS_SIMULATOR - iOS simulator
  98. // GTEST_OS_NACL - Google Native Client (NaCl)
  99. // GTEST_OS_OPENBSD - OpenBSD
  100. // GTEST_OS_QNX - QNX
  101. // GTEST_OS_SOLARIS - Sun Solaris
  102. // GTEST_OS_SYMBIAN - Symbian
  103. // GTEST_OS_WINDOWS - Windows (Desktop, MinGW, or Mobile)
  104. // GTEST_OS_WINDOWS_DESKTOP - Windows Desktop
  105. // GTEST_OS_WINDOWS_MINGW - MinGW
  106. // GTEST_OS_WINDOWS_MOBILE - Windows Mobile
  107. // GTEST_OS_ZOS - z/OS
  108. //
  109. // Among the platforms, Cygwin, Linux, Max OS X, and Windows have the
  110. // most stable support. Since core members of the Google Test project
  111. // don't have access to other platforms, support for them may be less
  112. // stable. If you notice any problems on your platform, please notify
  113. // googletestframework@googlegroups.com (patches for fixing them are
  114. // even more welcome!).
  115. //
  116. // Note that it is possible that none of the GTEST_OS_* macros are defined.
  117. //
  118. // Macros indicating available Google Test features (defined to 1 if
  119. // the corresponding feature is supported; otherwise undefined):
  120. // GTEST_HAS_COMBINE - the Combine() function (for value-parameterized
  121. // tests)
  122. // GTEST_HAS_DEATH_TEST - death tests
  123. // GTEST_HAS_PARAM_TEST - value-parameterized tests
  124. // GTEST_HAS_TYPED_TEST - typed tests
  125. // GTEST_HAS_TYPED_TEST_P - type-parameterized tests
  126. // GTEST_USES_POSIX_RE - enhanced POSIX regex is used. Do not confuse with
  127. // GTEST_HAS_POSIX_RE (see above) which users can
  128. // define themselves.
  129. // GTEST_USES_SIMPLE_RE - our own simple regex is used;
  130. // the above two are mutually exclusive.
  131. // GTEST_CAN_COMPARE_NULL - accepts untyped NULL in EXPECT_EQ().
  132. //
  133. // Macros for basic C++ coding:
  134. // GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.
  135. // GTEST_ATTRIBUTE_UNUSED_ - declares that a class' instances or a
  136. // variable don't have to be used.
  137. // GTEST_DISALLOW_ASSIGN_ - disables operator=.
  138. // GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=.
  139. // GTEST_MUST_USE_RESULT_ - declares that a function's result must be used.
  140. //
  141. // Synchronization:
  142. // Mutex, MutexLock, ThreadLocal, GetThreadCount()
  143. // - synchronization primitives.
  144. // GTEST_IS_THREADSAFE - defined to 1 to indicate that the above
  145. // synchronization primitives have real implementations
  146. // and Google Test is thread-safe; or 0 otherwise.
  147. //
  148. // Template meta programming:
  149. // is_pointer - as in TR1; needed on Symbian and IBM XL C/C++ only.
  150. // IteratorTraits - partial implementation of std::iterator_traits, which
  151. // is not available in libCstd when compiled with Sun C++.
  152. //
  153. // Smart pointers:
  154. // scoped_ptr - as in TR2.
  155. //
  156. // Regular expressions:
  157. // RE - a simple regular expression class using the POSIX
  158. // Extended Regular Expression syntax on UNIX-like
  159. // platforms, or a reduced regular exception syntax on
  160. // other platforms, including Windows.
  161. //
  162. // Logging:
  163. // GTEST_LOG_() - logs messages at the specified severity level.
  164. // LogToStderr() - directs all log messages to stderr.
  165. // FlushInfoLog() - flushes informational log messages.
  166. //
  167. // Stdout and stderr capturing:
  168. // CaptureStdout() - starts capturing stdout.
  169. // GetCapturedStdout() - stops capturing stdout and returns the captured
  170. // string.
  171. // CaptureStderr() - starts capturing stderr.
  172. // GetCapturedStderr() - stops capturing stderr and returns the captured
  173. // string.
  174. //
  175. // Integer types:
  176. // TypeWithSize - maps an integer to a int type.
  177. // Int32, UInt32, Int64, UInt64, TimeInMillis
  178. // - integers of known sizes.
  179. // BiggestInt - the biggest signed integer type.
  180. //
  181. // Command-line utilities:
  182. // GTEST_FLAG() - references a flag.
  183. // GTEST_DECLARE_*() - declares a flag.
  184. // GTEST_DEFINE_*() - defines a flag.
  185. // GetInjectableArgvs() - returns the command line as a vector of strings.
  186. //
  187. // Environment variable utilities:
  188. // GetEnv() - gets the value of an environment variable.
  189. // BoolFromGTestEnv() - parses a bool environment variable.
  190. // Int32FromGTestEnv() - parses an Int32 environment variable.
  191. // StringFromGTestEnv() - parses a string environment variable.
  192. #include <ctype.h> // for isspace, etc
  193. #include <stddef.h> // for ptrdiff_t
  194. #include <stdlib.h>
  195. #include <stdio.h>
  196. #include <string.h>
  197. #ifndef _WIN32_WCE
  198. # include <sys/types.h>
  199. # include <sys/stat.h>
  200. #endif // !_WIN32_WCE
  201. #if defined __APPLE__
  202. # include <AvailabilityMacros.h>
  203. # include <TargetConditionals.h>
  204. #endif
  205. #include <iostream> // NOLINT
  206. #include <sstream> // NOLINT
  207. #include <string> // NOLINT
  208. #define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
  209. #define GTEST_FLAG_PREFIX_ "gtest_"
  210. #define GTEST_FLAG_PREFIX_DASH_ "gtest-"
  211. #define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
  212. #define GTEST_NAME_ "Google Test"
  213. #define GTEST_PROJECT_URL_ "http://code.google.com/p/googletest/"
  214. // Determines the version of gcc that is used to compile this.
  215. #ifdef __GNUC__
  216. // 40302 means version 4.3.2.
  217. # define GTEST_GCC_VER_ \
  218. (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)
  219. #endif // __GNUC__
  220. // Determines the platform on which Google Test is compiled.
  221. #ifdef __CYGWIN__
  222. # define GTEST_OS_CYGWIN 1
  223. #elif defined __SYMBIAN32__
  224. # define GTEST_OS_SYMBIAN 1
  225. #elif defined _WIN32
  226. # define GTEST_OS_WINDOWS 1
  227. # ifdef _WIN32_WCE
  228. # define GTEST_OS_WINDOWS_MOBILE 1
  229. # elif defined(__MINGW__) || defined(__MINGW32__)
  230. # define GTEST_OS_WINDOWS_MINGW 1
  231. # else
  232. # define GTEST_OS_WINDOWS_DESKTOP 1
  233. # endif // _WIN32_WCE
  234. #elif defined __APPLE__
  235. # define GTEST_OS_MAC 1
  236. # if TARGET_OS_IPHONE
  237. # define GTEST_OS_IOS 1
  238. # if TARGET_IPHONE_SIMULATOR
  239. # define GTEST_OS_IOS_SIMULATOR 1
  240. # endif
  241. # endif
  242. #elif defined __linux__
  243. # define GTEST_OS_LINUX 1
  244. # if defined __ANDROID__
  245. # define GTEST_OS_LINUX_ANDROID 1
  246. # endif
  247. #elif defined __MVS__
  248. # define GTEST_OS_ZOS 1
  249. #elif defined(__sun) && defined(__SVR4)
  250. # define GTEST_OS_SOLARIS 1
  251. #elif defined(_AIX)
  252. # define GTEST_OS_AIX 1
  253. #elif defined(__hpux)
  254. # define GTEST_OS_HPUX 1
  255. #elif defined __native_client__
  256. # define GTEST_OS_NACL 1
  257. #elif defined __OpenBSD__
  258. # define GTEST_OS_OPENBSD 1
  259. #elif defined __QNX__
  260. # define GTEST_OS_QNX 1
  261. #endif // __CYGWIN__
  262. #ifndef GTEST_LANG_CXX11
  263. // gcc and clang define __GXX_EXPERIMENTAL_CXX0X__ when
  264. // -std={c,gnu}++{0x,11} is passed. The C++11 standard specifies a
  265. // value for __cplusplus, and recent versions of clang, gcc, and
  266. // probably other compilers set that too in C++11 mode.
  267. # if __GXX_EXPERIMENTAL_CXX0X__ || __cplusplus >= 201103L
  268. // Compiling in at least C++11 mode.
  269. # define GTEST_LANG_CXX11 1
  270. # else
  271. # define GTEST_LANG_CXX11 0
  272. # endif
  273. #endif
  274. // Brings in definitions for functions used in the testing::internal::posix
  275. // namespace (read, write, close, chdir, isatty, stat). We do not currently
  276. // use them on Windows Mobile.
  277. #if !GTEST_OS_WINDOWS
  278. // This assumes that non-Windows OSes provide unistd.h. For OSes where this
  279. // is not the case, we need to include headers that provide the functions
  280. // mentioned above.
  281. # include <unistd.h>
  282. # include <strings.h>
  283. #elif !GTEST_OS_WINDOWS_MOBILE
  284. # include <direct.h>
  285. # include <io.h>
  286. #endif
  287. #if GTEST_OS_LINUX_ANDROID
  288. // Used to define __ANDROID_API__ matching the target NDK API level.
  289. # include <android/api-level.h> // NOLINT
  290. #endif
  291. // Defines this to true iff Google Test can use POSIX regular expressions.
  292. #ifndef GTEST_HAS_POSIX_RE
  293. # if GTEST_OS_LINUX_ANDROID
  294. // On Android, <regex.h> is only available starting with Gingerbread.
  295. # define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9)
  296. # else
  297. # define GTEST_HAS_POSIX_RE (!GTEST_OS_WINDOWS)
  298. # endif
  299. #endif
  300. #if GTEST_HAS_POSIX_RE
  301. // On some platforms, <regex.h> needs someone to define size_t, and
  302. // won't compile otherwise. We can #include it here as we already
  303. // included <stdlib.h>, which is guaranteed to define size_t through
  304. // <stddef.h>.
  305. # include <regex.h> // NOLINT
  306. # define GTEST_USES_POSIX_RE 1
  307. #elif GTEST_OS_WINDOWS
  308. // <regex.h> is not available on Windows. Use our own simple regex
  309. // implementation instead.
  310. # define GTEST_USES_SIMPLE_RE 1
  311. #else
  312. // <regex.h> may not be available on this platform. Use our own
  313. // simple regex implementation instead.
  314. # define GTEST_USES_SIMPLE_RE 1
  315. #endif // GTEST_HAS_POSIX_RE
  316. #ifndef GTEST_HAS_EXCEPTIONS
  317. // The user didn't tell us whether exceptions are enabled, so we need
  318. // to figure it out.
  319. # if defined(_MSC_VER) || defined(__BORLANDC__)
  320. // MSVC's and C++Builder's implementations of the STL use the _HAS_EXCEPTIONS
  321. // macro to enable exceptions, so we'll do the same.
  322. // Assumes that exceptions are enabled by default.
  323. # ifndef _HAS_EXCEPTIONS
  324. # define _HAS_EXCEPTIONS 1
  325. # endif // _HAS_EXCEPTIONS
  326. # define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
  327. # elif defined(__GNUC__) && __EXCEPTIONS
  328. // gcc defines __EXCEPTIONS to 1 iff exceptions are enabled.
  329. # define GTEST_HAS_EXCEPTIONS 1
  330. # elif defined(__SUNPRO_CC)
  331. // Sun Pro CC supports exceptions. However, there is no compile-time way of
  332. // detecting whether they are enabled or not. Therefore, we assume that
  333. // they are enabled unless the user tells us otherwise.
  334. # define GTEST_HAS_EXCEPTIONS 1
  335. # elif defined(__IBMCPP__) && __EXCEPTIONS
  336. // xlC defines __EXCEPTIONS to 1 iff exceptions are enabled.
  337. # define GTEST_HAS_EXCEPTIONS 1
  338. # elif defined(__HP_aCC)
  339. // Exception handling is in effect by default in HP aCC compiler. It has to
  340. // be turned of by +noeh compiler option if desired.
  341. # define GTEST_HAS_EXCEPTIONS 1
  342. # else
  343. // For other compilers, we assume exceptions are disabled to be
  344. // conservative.
  345. # define GTEST_HAS_EXCEPTIONS 0
  346. # endif // defined(_MSC_VER) || defined(__BORLANDC__)
  347. #endif // GTEST_HAS_EXCEPTIONS
  348. #if !defined(GTEST_HAS_STD_STRING)
  349. // Even though we don't use this macro any longer, we keep it in case
  350. // some clients still depend on it.
  351. # define GTEST_HAS_STD_STRING 1
  352. #elif !GTEST_HAS_STD_STRING
  353. // The user told us that ::std::string isn't available.
  354. # error "Google Test cannot be used where ::std::string isn't available."
  355. #endif // !defined(GTEST_HAS_STD_STRING)
  356. #ifndef GTEST_HAS_GLOBAL_STRING
  357. // The user didn't tell us whether ::string is available, so we need
  358. // to figure it out.
  359. # define GTEST_HAS_GLOBAL_STRING 0
  360. #endif // GTEST_HAS_GLOBAL_STRING
  361. #ifndef GTEST_HAS_STD_WSTRING
  362. // The user didn't tell us whether ::std::wstring is available, so we need
  363. // to figure it out.
  364. // TODO(wan@google.com): uses autoconf to detect whether ::std::wstring
  365. // is available.
  366. // Cygwin 1.7 and below doesn't support ::std::wstring.
  367. // Solaris' libc++ doesn't support it either. Android has
  368. // no support for it at least as recent as Froyo (2.2).
  369. # define GTEST_HAS_STD_WSTRING \
  370. (!(GTEST_OS_LINUX_ANDROID || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS))
  371. #endif // GTEST_HAS_STD_WSTRING
  372. #ifndef GTEST_HAS_GLOBAL_WSTRING
  373. // The user didn't tell us whether ::wstring is available, so we need
  374. // to figure it out.
  375. # define GTEST_HAS_GLOBAL_WSTRING \
  376. (GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING)
  377. #endif // GTEST_HAS_GLOBAL_WSTRING
  378. // Determines whether RTTI is available.
  379. #ifndef GTEST_HAS_RTTI
  380. // The user didn't tell us whether RTTI is enabled, so we need to
  381. // figure it out.
  382. # ifdef _MSC_VER
  383. # ifdef _CPPRTTI // MSVC defines this macro iff RTTI is enabled.
  384. # define GTEST_HAS_RTTI 1
  385. # else
  386. # define GTEST_HAS_RTTI 0
  387. # endif
  388. // Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled.
  389. # elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40302)
  390. # ifdef __GXX_RTTI
  391. // When building against STLport with the Android NDK and with
  392. // -frtti -fno-exceptions, the build fails at link time with undefined
  393. // references to __cxa_bad_typeid. Note sure if STL or toolchain bug,
  394. // so disable RTTI when detected.
  395. # if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR) && \
  396. !defined(__EXCEPTIONS)
  397. # define GTEST_HAS_RTTI 0
  398. # else
  399. # define GTEST_HAS_RTTI 1
  400. # endif // GTEST_OS_LINUX_ANDROID && __STLPORT_MAJOR && !__EXCEPTIONS
  401. # else
  402. # define GTEST_HAS_RTTI 0
  403. # endif // __GXX_RTTI
  404. // Clang defines __GXX_RTTI starting with version 3.0, but its manual recommends
  405. // using has_feature instead. has_feature(cxx_rtti) is supported since 2.7, the
  406. // first version with C++ support.
  407. # elif defined(__clang__)
  408. # define GTEST_HAS_RTTI __has_feature(cxx_rtti)
  409. // Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if
  410. // both the typeid and dynamic_cast features are present.
  411. # elif defined(__IBMCPP__) && (__IBMCPP__ >= 900)
  412. # ifdef __RTTI_ALL__
  413. # define GTEST_HAS_RTTI 1
  414. # else
  415. # define GTEST_HAS_RTTI 0
  416. # endif
  417. # else
  418. // For all other compilers, we assume RTTI is enabled.
  419. # define GTEST_HAS_RTTI 1
  420. # endif // _MSC_VER
  421. #endif // GTEST_HAS_RTTI
  422. // It's this header's responsibility to #include <typeinfo> when RTTI
  423. // is enabled.
  424. #if GTEST_HAS_RTTI
  425. # include <typeinfo>
  426. #endif
  427. // Determines whether Google Test can use the pthreads library.
  428. #ifndef GTEST_HAS_PTHREAD
  429. // The user didn't tell us explicitly, so we assume pthreads support is
  430. // available on Linux and Mac.
  431. //
  432. // To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0
  433. // to your compiler flags.
  434. # define GTEST_HAS_PTHREAD (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_HPUX \
  435. || GTEST_OS_QNX)
  436. #endif // GTEST_HAS_PTHREAD
  437. #if GTEST_HAS_PTHREAD
  438. // gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is
  439. // true.
  440. # include <pthread.h> // NOLINT
  441. // For timespec and nanosleep, used below.
  442. # include <time.h> // NOLINT
  443. #endif
  444. // Determines whether Google Test can use tr1/tuple. You can define
  445. // this macro to 0 to prevent Google Test from using tuple (any
  446. // feature depending on tuple with be disabled in this mode).
  447. #ifndef GTEST_HAS_TR1_TUPLE
  448. # if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR)
  449. // STLport, provided with the Android NDK, has neither <tr1/tuple> or <tuple>.
  450. # define GTEST_HAS_TR1_TUPLE 0
  451. # else
  452. // The user didn't tell us not to do it, so we assume it's OK.
  453. # define GTEST_HAS_TR1_TUPLE 1
  454. # endif
  455. #endif // GTEST_HAS_TR1_TUPLE
  456. // Determines whether Google Test's own tr1 tuple implementation
  457. // should be used.
  458. #ifndef GTEST_USE_OWN_TR1_TUPLE
  459. // The user didn't tell us, so we need to figure it out.
  460. // We use our own TR1 tuple if we aren't sure the user has an
  461. // implementation of it already. At this time, libstdc++ 4.0.0+ and
  462. // MSVC 2010 are the only mainstream standard libraries that come
  463. // with a TR1 tuple implementation. NVIDIA's CUDA NVCC compiler
  464. // pretends to be GCC by defining __GNUC__ and friends, but cannot
  465. // compile GCC's tuple implementation. MSVC 2008 (9.0) provides TR1
  466. // tuple in a 323 MB Feature Pack download, which we cannot assume the
  467. // user has. QNX's QCC compiler is a modified GCC but it doesn't
  468. // support TR1 tuple. libc++ only provides std::tuple, in C++11 mode,
  469. // and it can be used with some compilers that define __GNUC__.
  470. # if (defined(__GNUC__) && !defined(__CUDACC__) && (GTEST_GCC_VER_ >= 40000) \
  471. && !GTEST_OS_QNX && !defined(_LIBCPP_VERSION)) || _MSC_VER >= 1600
  472. # define GTEST_ENV_HAS_TR1_TUPLE_ 1
  473. # endif
  474. // C++11 specifies that <tuple> provides std::tuple. Use that if gtest is used
  475. // in C++11 mode and libstdc++ isn't very old (binaries targeting OS X 10.6
  476. // can build with clang but need to use gcc4.2's libstdc++).
  477. # if GTEST_LANG_CXX11 && (!defined(__GLIBCXX__) || __GLIBCXX__ > 20110325)
  478. # define GTEST_ENV_HAS_STD_TUPLE_ 1
  479. # endif
  480. # if GTEST_ENV_HAS_TR1_TUPLE_ || GTEST_ENV_HAS_STD_TUPLE_
  481. # define GTEST_USE_OWN_TR1_TUPLE 0
  482. # else
  483. # define GTEST_USE_OWN_TR1_TUPLE 1
  484. # endif
  485. #endif // GTEST_USE_OWN_TR1_TUPLE
  486. // To avoid conditional compilation everywhere, we make it
  487. // gtest-port.h's responsibility to #include the header implementing
  488. // tr1/tuple.
  489. #if GTEST_HAS_TR1_TUPLE
  490. # if GTEST_USE_OWN_TR1_TUPLE
  491. # include "gtest/internal/gtest-tuple.h"
  492. # elif GTEST_ENV_HAS_STD_TUPLE_
  493. # include <tuple>
  494. // C++11 puts its tuple into the ::std namespace rather than
  495. // ::std::tr1. gtest expects tuple to live in ::std::tr1, so put it there.
  496. // This causes undefined behavior, but supported compilers react in
  497. // the way we intend.
  498. namespace std {
  499. namespace tr1 {
  500. using ::std::get;
  501. using ::std::make_tuple;
  502. using ::std::tuple;
  503. using ::std::tuple_element;
  504. using ::std::tuple_size;
  505. }
  506. }
  507. # elif GTEST_OS_SYMBIAN
  508. // On Symbian, BOOST_HAS_TR1_TUPLE causes Boost's TR1 tuple library to
  509. // use STLport's tuple implementation, which unfortunately doesn't
  510. // work as the copy of STLport distributed with Symbian is incomplete.
  511. // By making sure BOOST_HAS_TR1_TUPLE is undefined, we force Boost to
  512. // use its own tuple implementation.
  513. # ifdef BOOST_HAS_TR1_TUPLE
  514. # undef BOOST_HAS_TR1_TUPLE
  515. # endif // BOOST_HAS_TR1_TUPLE
  516. // This prevents <boost/tr1/detail/config.hpp>, which defines
  517. // BOOST_HAS_TR1_TUPLE, from being #included by Boost's <tuple>.
  518. # define BOOST_TR1_DETAIL_CONFIG_HPP_INCLUDED
  519. # include <tuple>
  520. # elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000)
  521. // GCC 4.0+ implements tr1/tuple in the <tr1/tuple> header. This does
  522. // not conform to the TR1 spec, which requires the header to be <tuple>.
  523. # if !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
  524. // Until version 4.3.2, gcc has a bug that causes <tr1/functional>,
  525. // which is #included by <tr1/tuple>, to not compile when RTTI is
  526. // disabled. _TR1_FUNCTIONAL is the header guard for
  527. // <tr1/functional>. Hence the following #define is a hack to prevent
  528. // <tr1/functional> from being included.
  529. # define _TR1_FUNCTIONAL 1
  530. # include <tr1/tuple>
  531. # undef _TR1_FUNCTIONAL // Allows the user to #include
  532. // <tr1/functional> if he chooses to.
  533. # else
  534. # include <tr1/tuple> // NOLINT
  535. # endif // !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302
  536. # else
  537. // If the compiler is not GCC 4.0+, we assume the user is using a
  538. // spec-conforming TR1 implementation.
  539. # include <tuple> // NOLINT
  540. # endif // GTEST_USE_OWN_TR1_TUPLE
  541. #endif // GTEST_HAS_TR1_TUPLE
  542. // Determines whether clone(2) is supported.
  543. // Usually it will only be available on Linux, excluding
  544. // Linux on the Itanium architecture.
  545. // Also see http://linux.die.net/man/2/clone.
  546. #ifndef GTEST_HAS_CLONE
  547. // The user didn't tell us, so we need to figure it out.
  548. # if GTEST_OS_LINUX && !defined(__ia64__)
  549. # if GTEST_OS_LINUX_ANDROID
  550. // On Android, clone() is only available on ARM starting with Gingerbread.
  551. # if defined(__arm__) && __ANDROID_API__ >= 9
  552. # define GTEST_HAS_CLONE 1
  553. # else
  554. # define GTEST_HAS_CLONE 0
  555. # endif
  556. # else
  557. # define GTEST_HAS_CLONE 1
  558. # endif
  559. # else
  560. # define GTEST_HAS_CLONE 0
  561. # endif // GTEST_OS_LINUX && !defined(__ia64__)
  562. #endif // GTEST_HAS_CLONE
  563. // Determines whether to support stream redirection. This is used to test
  564. // output correctness and to implement death tests.
  565. #ifndef GTEST_HAS_STREAM_REDIRECTION
  566. // By default, we assume that stream redirection is supported on all
  567. // platforms except known mobile ones.
  568. # if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN
  569. # define GTEST_HAS_STREAM_REDIRECTION 0
  570. # else
  571. # define GTEST_HAS_STREAM_REDIRECTION 1
  572. # endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN
  573. #endif // GTEST_HAS_STREAM_REDIRECTION
  574. // Determines whether to support death tests.
  575. // Google Test does not support death tests for VC 7.1 and earlier as
  576. // abort() in a VC 7.1 application compiled as GUI in debug config
  577. // pops up a dialog window that cannot be suppressed programmatically.
  578. #if (GTEST_OS_LINUX || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \
  579. (GTEST_OS_MAC && !GTEST_OS_IOS) || GTEST_OS_IOS_SIMULATOR || \
  580. (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER >= 1400) || \
  581. GTEST_OS_WINDOWS_MINGW || GTEST_OS_AIX || GTEST_OS_HPUX || \
  582. GTEST_OS_OPENBSD || GTEST_OS_QNX)
  583. # define GTEST_HAS_DEATH_TEST 1
  584. # include <vector> // NOLINT
  585. #endif
  586. // We don't support MSVC 7.1 with exceptions disabled now. Therefore
  587. // all the compilers we care about are adequate for supporting
  588. // value-parameterized tests.
  589. #define GTEST_HAS_PARAM_TEST 1
  590. // Determines whether to support type-driven tests.
  591. // Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0,
  592. // Sun Pro CC, IBM Visual Age, and HP aCC support.
  593. #if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__SUNPRO_CC) || \
  594. defined(__IBMCPP__) || defined(__HP_aCC)
  595. # define GTEST_HAS_TYPED_TEST 1
  596. # define GTEST_HAS_TYPED_TEST_P 1
  597. #endif
  598. // Determines whether to support Combine(). This only makes sense when
  599. // value-parameterized tests are enabled. The implementation doesn't
  600. // work on Sun Studio since it doesn't understand templated conversion
  601. // operators.
  602. #if GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE && !defined(__SUNPRO_CC)
  603. # define GTEST_HAS_COMBINE 1
  604. #endif
  605. // Determines whether the system compiler uses UTF-16 for encoding wide strings.
  606. #define GTEST_WIDE_STRING_USES_UTF16_ \
  607. (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN || GTEST_OS_AIX)
  608. // Determines whether test results can be streamed to a socket.
  609. #if GTEST_OS_LINUX
  610. # define GTEST_CAN_STREAM_RESULTS_ 1
  611. #endif
  612. // Defines some utility macros.
  613. // The GNU compiler emits a warning if nested "if" statements are followed by
  614. // an "else" statement and braces are not used to explicitly disambiguate the
  615. // "else" binding. This leads to problems with code like:
  616. //
  617. // if (gate)
  618. // ASSERT_*(condition) << "Some message";
  619. //
  620. // The "switch (0) case 0:" idiom is used to suppress this.
  621. #ifdef __INTEL_COMPILER
  622. # define GTEST_AMBIGUOUS_ELSE_BLOCKER_
  623. #else
  624. # define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: default: // NOLINT
  625. #endif
  626. // Use this annotation at the end of a struct/class definition to
  627. // prevent the compiler from optimizing away instances that are never
  628. // used. This is useful when all interesting logic happens inside the
  629. // c'tor and / or d'tor. Example:
  630. //
  631. // struct Foo {
  632. // Foo() { ... }
  633. // } GTEST_ATTRIBUTE_UNUSED_;
  634. //
  635. // Also use it after a variable or parameter declaration to tell the
  636. // compiler the variable/parameter does not have to be used.
  637. #if defined(__GNUC__) && !defined(COMPILER_ICC)
  638. # define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))
  639. #else
  640. # define GTEST_ATTRIBUTE_UNUSED_
  641. #endif
  642. // A macro to disallow operator=
  643. // This should be used in the private: declarations for a class.
  644. #define GTEST_DISALLOW_ASSIGN_(type)\
  645. void operator=(type const &)
  646. // A macro to disallow copy constructor and operator=
  647. // This should be used in the private: declarations for a class.
  648. #define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\
  649. type(type const &);\
  650. GTEST_DISALLOW_ASSIGN_(type)
  651. // Tell the compiler to warn about unused return values for functions declared
  652. // with this macro. The macro should be used on function declarations
  653. // following the argument list:
  654. //
  655. // Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_;
  656. #if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC)
  657. # define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))
  658. #else
  659. # define GTEST_MUST_USE_RESULT_
  660. #endif // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC
  661. // Determine whether the compiler supports Microsoft's Structured Exception
  662. // Handling. This is supported by several Windows compilers but generally
  663. // does not exist on any other system.
  664. #ifndef GTEST_HAS_SEH
  665. // The user didn't tell us, so we need to figure it out.
  666. # if defined(_MSC_VER) || defined(__BORLANDC__)
  667. // These two compilers are known to support SEH.
  668. # define GTEST_HAS_SEH 1
  669. # else
  670. // Assume no SEH.
  671. # define GTEST_HAS_SEH 0
  672. # endif
  673. #endif // GTEST_HAS_SEH
  674. #ifdef _MSC_VER
  675. # if GTEST_LINKED_AS_SHARED_LIBRARY
  676. # define GTEST_API_ __declspec(dllimport)
  677. # elif GTEST_CREATE_SHARED_LIBRARY
  678. # define GTEST_API_ __declspec(dllexport)
  679. # endif
  680. #endif // _MSC_VER
  681. #ifndef GTEST_API_
  682. # define GTEST_API_
  683. #endif
  684. #ifdef __GNUC__
  685. // Ask the compiler to never inline a given function.
  686. # define GTEST_NO_INLINE_ __attribute__((noinline))
  687. #else
  688. # define GTEST_NO_INLINE_
  689. #endif
  690. // _LIBCPP_VERSION is defined by the libc++ library from the LLVM project.
  691. #if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION)
  692. # define GTEST_HAS_CXXABI_H_ 1
  693. #else
  694. # define GTEST_HAS_CXXABI_H_ 0
  695. #endif
  696. namespace testing {
  697. class Message;
  698. namespace internal {
  699. // A secret type that Google Test users don't know about. It has no
  700. // definition on purpose. Therefore it's impossible to create a
  701. // Secret object, which is what we want.
  702. class Secret;
  703. // The GTEST_COMPILE_ASSERT_ macro can be used to verify that a compile time
  704. // expression is true. For example, you could use it to verify the
  705. // size of a static array:
  706. //
  707. // GTEST_COMPILE_ASSERT_(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
  708. // content_type_names_incorrect_size);
  709. //
  710. // or to make sure a struct is smaller than a certain size:
  711. //
  712. // GTEST_COMPILE_ASSERT_(sizeof(foo) < 128, foo_too_large);
  713. //
  714. // The second argument to the macro is the name of the variable. If
  715. // the expression is false, most compilers will issue a warning/error
  716. // containing the name of the variable.
  717. template <bool>
  718. struct CompileAssert {
  719. };
  720. #define GTEST_COMPILE_ASSERT_(expr, msg) \
  721. typedef ::testing::internal::CompileAssert<(static_cast<bool>(expr))> \
  722. msg[static_cast<bool>(expr) ? 1 : -1] GTEST_ATTRIBUTE_UNUSED_
  723. // Implementation details of GTEST_COMPILE_ASSERT_:
  724. //
  725. // - GTEST_COMPILE_ASSERT_ works by defining an array type that has -1
  726. // elements (and thus is invalid) when the expression is false.
  727. //
  728. // - The simpler definition
  729. //
  730. // #define GTEST_COMPILE_ASSERT_(expr, msg) typedef char msg[(expr) ? 1 : -1]
  731. //
  732. // does not work, as gcc supports variable-length arrays whose sizes
  733. // are determined at run-time (this is gcc's extension and not part
  734. // of the C++ standard). As a result, gcc fails to reject the
  735. // following code with the simple definition:
  736. //
  737. // int foo;
  738. // GTEST_COMPILE_ASSERT_(foo, msg); // not supposed to compile as foo is
  739. // // not a compile-time constant.
  740. //
  741. // - By using the type CompileAssert<(bool(expr))>, we ensures that
  742. // expr is a compile-time constant. (Template arguments must be
  743. // determined at compile-time.)
  744. //
  745. // - The outter parentheses in CompileAssert<(bool(expr))> are necessary
  746. // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written
  747. //
  748. // CompileAssert<bool(expr)>
  749. //
  750. // instead, these compilers will refuse to compile
  751. //
  752. // GTEST_COMPILE_ASSERT_(5 > 0, some_message);
  753. //
  754. // (They seem to think the ">" in "5 > 0" marks the end of the
  755. // template argument list.)
  756. //
  757. // - The array size is (bool(expr) ? 1 : -1), instead of simply
  758. //
  759. // ((expr) ? 1 : -1).
  760. //
  761. // This is to avoid running into a bug in MS VC 7.1, which
  762. // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
  763. // StaticAssertTypeEqHelper is used by StaticAssertTypeEq defined in gtest.h.
  764. //
  765. // This template is declared, but intentionally undefined.
  766. template <typename T1, typename T2>
  767. struct StaticAssertTypeEqHelper;
  768. template <typename T>
  769. struct StaticAssertTypeEqHelper<T, T> {};
  770. #if GTEST_HAS_GLOBAL_STRING
  771. typedef ::string string;
  772. #else
  773. typedef ::std::string string;
  774. #endif // GTEST_HAS_GLOBAL_STRING
  775. #if GTEST_HAS_GLOBAL_WSTRING
  776. typedef ::wstring wstring;
  777. #elif GTEST_HAS_STD_WSTRING
  778. typedef ::std::wstring wstring;
  779. #endif // GTEST_HAS_GLOBAL_WSTRING
  780. // A helper for suppressing warnings on constant condition. It just
  781. // returns 'condition'.
  782. GTEST_API_ bool IsTrue(bool condition);
  783. // Defines scoped_ptr.
  784. // This implementation of scoped_ptr is PARTIAL - it only contains
  785. // enough stuff to satisfy Google Test's need.
  786. template <typename T>
  787. class scoped_ptr {
  788. public:
  789. typedef T element_type;
  790. explicit scoped_ptr(T* p = NULL) : ptr_(p) {}
  791. ~scoped_ptr() { reset(); }
  792. T& operator*() const { return *ptr_; }
  793. T* operator->() const { return ptr_; }
  794. T* get() const { return ptr_; }
  795. T* release() {
  796. T* const ptr = ptr_;
  797. ptr_ = NULL;
  798. return ptr;
  799. }
  800. void reset(T* p = NULL) {
  801. if (p != ptr_) {
  802. if (IsTrue(sizeof(T) > 0)) { // Makes sure T is a complete type.
  803. delete ptr_;
  804. }
  805. ptr_ = p;
  806. }
  807. }
  808. private:
  809. T* ptr_;
  810. GTEST_DISALLOW_COPY_AND_ASSIGN_(scoped_ptr);
  811. };
  812. // Defines RE.
  813. // A simple C++ wrapper for <regex.h>. It uses the POSIX Extended
  814. // Regular Expression syntax.
  815. class GTEST_API_ RE {
  816. public:
  817. // A copy constructor is required by the Standard to initialize object
  818. // references from r-values.
  819. RE(const RE& other) { Init(other.pattern()); }
  820. // Constructs an RE from a string.
  821. RE(const ::std::string& regex) { Init(regex.c_str()); } // NOLINT
  822. #if GTEST_HAS_GLOBAL_STRING
  823. RE(const ::string& regex) { Init(regex.c_str()); } // NOLINT
  824. #endif // GTEST_HAS_GLOBAL_STRING
  825. RE(const char* regex) { Init(regex); } // NOLINT
  826. ~RE();
  827. // Returns the string representation of the regex.
  828. const char* pattern() const { return pattern_; }
  829. // FullMatch(str, re) returns true iff regular expression re matches
  830. // the entire str.
  831. // PartialMatch(str, re) returns true iff regular expression re
  832. // matches a substring of str (including str itself).
  833. //
  834. // TODO(wan@google.com): make FullMatch() and PartialMatch() work
  835. // when str contains NUL characters.
  836. static bool FullMatch(const ::std::string& str, const RE& re) {
  837. return FullMatch(str.c_str(), re);
  838. }
  839. static bool PartialMatch(const ::std::string& str, const RE& re) {
  840. return PartialMatch(str.c_str(), re);
  841. }
  842. #if GTEST_HAS_GLOBAL_STRING
  843. static bool FullMatch(const ::string& str, const RE& re) {
  844. return FullMatch(str.c_str(), re);
  845. }
  846. static bool PartialMatch(const ::string& str, const RE& re) {
  847. return PartialMatch(str.c_str(), re);
  848. }
  849. #endif // GTEST_HAS_GLOBAL_STRING
  850. static bool FullMatch(const char* str, const RE& re);
  851. static bool PartialMatch(const char* str, const RE& re);
  852. private:
  853. void Init(const char* regex);
  854. // We use a const char* instead of an std::string, as Google Test used to be
  855. // used where std::string is not available. TODO(wan@google.com): change to
  856. // std::string.
  857. const char* pattern_;
  858. bool is_valid_;
  859. #if GTEST_USES_POSIX_RE
  860. regex_t full_regex_; // For FullMatch().
  861. regex_t partial_regex_; // For PartialMatch().
  862. #else // GTEST_USES_SIMPLE_RE
  863. const char* full_pattern_; // For FullMatch();
  864. #endif
  865. GTEST_DISALLOW_ASSIGN_(RE);
  866. };
  867. // Formats a source file path and a line number as they would appear
  868. // in an error message from the compiler used to compile this code.
  869. GTEST_API_ ::std::string FormatFileLocation(const char* file, int line);
  870. // Formats a file location for compiler-independent XML output.
  871. // Although this function is not platform dependent, we put it next to
  872. // FormatFileLocation in order to contrast the two functions.
  873. GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file,
  874. int line);
  875. // Defines logging utilities:
  876. // GTEST_LOG_(severity) - logs messages at the specified severity level. The
  877. // message itself is streamed into the macro.
  878. // LogToStderr() - directs all log messages to stderr.
  879. // FlushInfoLog() - flushes informational log messages.
  880. enum GTestLogSeverity {
  881. GTEST_INFO,
  882. GTEST_WARNING,
  883. GTEST_ERROR,
  884. GTEST_FATAL
  885. };
  886. // Formats log entry severity, provides a stream object for streaming the
  887. // log message, and terminates the message with a newline when going out of
  888. // scope.
  889. class GTEST_API_ GTestLog {
  890. public:
  891. GTestLog(GTestLogSeverity severity, const char* file, int line);
  892. // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
  893. ~GTestLog();
  894. ::std::ostream& GetStream() { return ::std::cerr; }
  895. private:
  896. const GTestLogSeverity severity_;
  897. GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog);
  898. };
  899. #define GTEST_LOG_(severity) \
  900. ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
  901. __FILE__, __LINE__).GetStream()
  902. inline void LogToStderr() {}
  903. inline void FlushInfoLog() { fflush(NULL); }
  904. // INTERNAL IMPLEMENTATION - DO NOT USE.
  905. //
  906. // GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition
  907. // is not satisfied.
  908. // Synopsys:
  909. // GTEST_CHECK_(boolean_condition);
  910. // or
  911. // GTEST_CHECK_(boolean_condition) << "Additional message";
  912. //
  913. // This checks the condition and if the condition is not satisfied
  914. // it prints message about the condition violation, including the
  915. // condition itself, plus additional message streamed into it, if any,
  916. // and then it aborts the program. It aborts the program irrespective of
  917. // whether it is built in the debug mode or not.
  918. #define GTEST_CHECK_(condition) \
  919. GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
  920. if (::testing::internal::IsTrue(condition)) \
  921. ; \
  922. else \
  923. GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
  924. // An all-mode assert to verify that the given POSIX-style function
  925. // call returns 0 (indicating success). Known limitation: this
  926. // doesn't expand to a balanced 'if' statement, so enclose the macro
  927. // in {} if you need to use it as the only statement in an 'if'
  928. // branch.
  929. #define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \
  930. if (const int gtest_error = (posix_call)) \
  931. GTEST_LOG_(FATAL) << #posix_call << "failed with error " \
  932. << gtest_error
  933. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
  934. //
  935. // Use ImplicitCast_ as a safe version of static_cast for upcasting in
  936. // the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a
  937. // const Foo*). When you use ImplicitCast_, the compiler checks that
  938. // the cast is safe. Such explicit ImplicitCast_s are necessary in
  939. // surprisingly many situations where C++ demands an exact type match
  940. // instead of an argument type convertable to a target type.
  941. //
  942. // The syntax for using ImplicitCast_ is the same as for static_cast:
  943. //
  944. // ImplicitCast_<ToType>(expr)
  945. //
  946. // ImplicitCast_ would have been part of the C++ standard library,
  947. // but the proposal was submitted too late. It will probably make
  948. // its way into the language in the future.
  949. //
  950. // This relatively ugly name is intentional. It prevents clashes with
  951. // similar functions users may have (e.g., implicit_cast). The internal
  952. // namespace alone is not enough because the function can be found by ADL.
  953. template<typename To>
  954. inline To ImplicitCast_(To x) { return x; }
  955. // When you upcast (that is, cast a pointer from type Foo to type
  956. // SuperclassOfFoo), it's fine to use ImplicitCast_<>, since upcasts
  957. // always succeed. When you downcast (that is, cast a pointer from
  958. // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
  959. // how do you know the pointer is really of type SubclassOfFoo? It
  960. // could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
  961. // when you downcast, you should use this macro. In debug mode, we
  962. // use dynamic_cast<> to double-check the downcast is legal (we die
  963. // if it's not). In normal mode, we do the efficient static_cast<>
  964. // instead. Thus, it's important to test in debug mode to make sure
  965. // the cast is legal!
  966. // This is the only place in the code we should use dynamic_cast<>.
  967. // In particular, you SHOULDN'T be using dynamic_cast<> in order to
  968. // do RTTI (eg code like this:
  969. // if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
  970. // if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
  971. // You should design the code some other way not to need this.
  972. //
  973. // This relatively ugly name is intentional. It prevents clashes with
  974. // similar functions users may have (e.g., down_cast). The internal
  975. // namespace alone is not enough because the function can be found by ADL.
  976. template<typename To, typename From> // use like this: DownCast_<T*>(foo);
  977. inline To DownCast_(From* f) { // so we only accept pointers
  978. // Ensures that To is a sub-type of From *. This test is here only
  979. // for compile-time type checking, and has no overhead in an
  980. // optimized build at run-time, as it will be optimized away
  981. // completely.
  982. if (false) {
  983. const To to = NULL;
  984. ::testing::internal::ImplicitCast_<From*>(to);
  985. }
  986. #if GTEST_HAS_RTTI
  987. // RTTI: debug mode only!
  988. GTEST_CHECK_(f == NULL || dynamic_cast<To>(f) != NULL);
  989. #endif
  990. return static_cast<To>(f);
  991. }
  992. // Downcasts the pointer of type Base to Derived.
  993. // Derived must be a subclass of Base. The parameter MUST
  994. // point to a class of type Derived, not any subclass of it.
  995. // When RTTI is available, the function performs a runtime
  996. // check to enforce this.
  997. template <class Derived, class Base>
  998. Derived* CheckedDowncastToActualType(Base* base) {
  999. #if GTEST_HAS_RTTI
  1000. GTEST_CHECK_(typeid(*base) == typeid(Derived));
  1001. return dynamic_cast<Derived*>(base); // NOLINT
  1002. #else
  1003. return static_cast<Derived*>(base); // Poor man's downcast.
  1004. #endif
  1005. }
  1006. #if GTEST_HAS_STREAM_REDIRECTION
  1007. // Defines the stderr capturer:
  1008. // CaptureStdout - starts capturing stdout.
  1009. // GetCapturedStdout - stops capturing stdout and returns the captured string.
  1010. // CaptureStderr - starts capturing stderr.
  1011. // GetCapturedStderr - stops capturing stderr and returns the captured string.
  1012. //
  1013. GTEST_API_ void CaptureStdout();
  1014. GTEST_API_ std::string GetCapturedStdout();
  1015. GTEST_API_ void CaptureStderr();
  1016. GTEST_API_ std::string GetCapturedStderr();
  1017. #endif // GTEST_HAS_STREAM_REDIRECTION
  1018. #if GTEST_HAS_DEATH_TEST
  1019. const ::std::vector<testing::internal::string>& GetInjectableArgvs();
  1020. void SetInjectableArgvs(const ::std::vector<testing::internal::string>*
  1021. new_argvs);
  1022. // A copy of all command line arguments. Set by InitGoogleTest().
  1023. extern ::std::vector<testing::internal::string> g_argvs;
  1024. #endif // GTEST_HAS_DEATH_TEST
  1025. // Defines synchronization primitives.
  1026. #if GTEST_HAS_PTHREAD
  1027. // Sleeps for (roughly) n milli-seconds. This function is only for
  1028. // testing Google Test's own constructs. Don't use it in user tests,
  1029. // either directly or indirectly.
  1030. inline void SleepMilliseconds(int n) {
  1031. const timespec time = {
  1032. 0, // 0 seconds.
  1033. n * 1000L * 1000L, // And n ms.
  1034. };
  1035. nanosleep(&time, NULL);
  1036. }
  1037. // Allows a controller thread to pause execution of newly created
  1038. // threads until notified. Instances of this class must be created
  1039. // and destroyed in the controller thread.
  1040. //
  1041. // This class is only for testing Google Test's own constructs. Do not
  1042. // use it in user tests, either directly or indirectly.
  1043. class Notification {
  1044. public:
  1045. Notification() : notified_(false) {
  1046. GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));
  1047. }
  1048. ~Notification() {
  1049. pthread_mutex_destroy(&mutex_);
  1050. }
  1051. // Notifies all threads created with this notification to start. Must
  1052. // be called from the controller thread.
  1053. void Notify() {
  1054. pthread_mutex_lock(&mutex_);
  1055. notified_ = true;
  1056. pthread_mutex_unlock(&mutex_);
  1057. }
  1058. // Blocks until the controller thread notifies. Must be called from a test
  1059. // thread.
  1060. void WaitForNotification() {
  1061. for (;;) {
  1062. pthread_mutex_lock(&mutex_);
  1063. const bool notified = notified_;
  1064. pthread_mutex_unlock(&mutex_);
  1065. if (notified)
  1066. break;
  1067. SleepMilliseconds(10);
  1068. }
  1069. }
  1070. private:
  1071. pthread_mutex_t mutex_;
  1072. bool notified_;
  1073. GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);
  1074. };
  1075. // As a C-function, ThreadFuncWithCLinkage cannot be templated itself.
  1076. // Consequently, it cannot select a correct instantiation of ThreadWithParam
  1077. // in order to call its Run(). Introducing ThreadWithParamBase as a
  1078. // non-templated base class for ThreadWithParam allows us to bypass this
  1079. // problem.
  1080. class ThreadWithParamBase {
  1081. public:
  1082. virtual ~ThreadWithParamBase() {}
  1083. virtual void Run() = 0;
  1084. };
  1085. // pthread_create() accepts a pointer to a function type with the C linkage.
  1086. // According to the Standard (7.5/1), function types with different linkages
  1087. // are different even if they are otherwise identical. Some compilers (for
  1088. // example, SunStudio) treat them as different types. Since class methods
  1089. // cannot be defined with C-linkage we need to define a free C-function to
  1090. // pass into pthread_create().
  1091. extern "C" inline void* ThreadFuncWithCLinkage(void* thread) {
  1092. static_cast<ThreadWithParamBase*>(thread)->Run();
  1093. return NULL;
  1094. }
  1095. // Helper class for testing Google Test's multi-threading constructs.
  1096. // To use it, write:
  1097. //
  1098. // void ThreadFunc(int param) { /* Do things with param */ }
  1099. // Notification thread_can_start;
  1100. // ...
  1101. // // The thread_can_start parameter is optional; you can supply NULL.
  1102. // ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start);
  1103. // thread_can_start.Notify();
  1104. //
  1105. // These classes are only for testing Google Test's own constructs. Do
  1106. // not use them in user tests, either directly or indirectly.
  1107. template <typename T>
  1108. class ThreadWithParam : public ThreadWithParamBase {
  1109. public:
  1110. typedef void (*UserThreadFunc)(T);
  1111. ThreadWithParam(
  1112. UserThreadFunc func, T param, Notification* thread_can_start)
  1113. : func_(func),
  1114. param_(param),
  1115. thread_can_start_(thread_can_start),
  1116. finished_(false) {
  1117. ThreadWithParamBase* const base = this;
  1118. // The thread can be created only after all fields except thread_
  1119. // have been initialized.
  1120. GTEST_CHECK_POSIX_SUCCESS_(
  1121. pthread_create(&thread_, 0, &ThreadFuncWithCLinkage, base));
  1122. }
  1123. ~ThreadWithParam() { Join(); }
  1124. void Join() {
  1125. if (!finished_) {
  1126. GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, 0));
  1127. finished_ = true;
  1128. }
  1129. }
  1130. virtual void Run() {
  1131. if (thread_can_start_ != NULL)
  1132. thread_can_start_->WaitForNotification();
  1133. func_(param_);
  1134. }
  1135. private:
  1136. const UserThreadFunc func_; // User-supplied thread function.
  1137. const T param_; // User-supplied parameter to the thread function.
  1138. // When non-NULL, used to block execution until the controller thread
  1139. // notifies.
  1140. Notification* const thread_can_start_;
  1141. bool finished_; // true iff we know that the thread function has finished.
  1142. pthread_t thread_; // The native thread object.
  1143. GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam);
  1144. };
  1145. // MutexBase and Mutex implement mutex on pthreads-based platforms. They
  1146. // are used in conjunction with class MutexLock:
  1147. //
  1148. // Mutex mutex;
  1149. // ...
  1150. // MutexLock lock(&mutex); // Acquires the mutex and releases it at the end
  1151. // // of the current scope.
  1152. //
  1153. // MutexBase implements behavior for both statically and dynamically
  1154. // allocated mutexes. Do not use MutexBase directly. Instead, write
  1155. // the following to define a static mutex:
  1156. //
  1157. // GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex);
  1158. //
  1159. // You can forward declare a static mutex like this:
  1160. //
  1161. // GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex);
  1162. //
  1163. // To create a dynamic mutex, just define an object of type Mutex.
  1164. class MutexBase {
  1165. public:
  1166. // Acquires this mutex.
  1167. void Lock() {
  1168. GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_));
  1169. owner_ = pthread_self();
  1170. has_owner_ = true;
  1171. }
  1172. // Releases this mutex.
  1173. void Unlock() {
  1174. // Since the lock is being released the owner_ field should no longer be
  1175. // considered valid. We don't protect writing to has_owner_ here, as it's
  1176. // the caller's responsibility to ensure that the current thread holds the
  1177. // mutex when this is called.
  1178. has_owner_ = false;
  1179. GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_));
  1180. }
  1181. // Does nothing if the current thread holds the mutex. Otherwise, crashes
  1182. // with high probability.
  1183. void AssertHeld() const {
  1184. GTEST_CHECK_(has_owner_ && pthread_equal(owner_, pthread_self()))
  1185. << "The current thread is not holding the mutex @" << this;
  1186. }
  1187. // A static mutex may be used before main() is entered. It may even
  1188. // be used before the dynamic initialization stage. Therefore we
  1189. // must be able to initialize a static mutex object at link time.
  1190. // This means MutexBase has to be a POD and its member variables
  1191. // have to be public.
  1192. public:
  1193. pthread_mutex_t mutex_; // The underlying pthread mutex.
  1194. // has_owner_ indicates whether the owner_ field below contains a valid thread
  1195. // ID and is therefore safe to inspect (e.g., to use in pthread_equal()). All
  1196. // accesses to the owner_ field should be protected by a check of this field.
  1197. // An alternative might be to memset() owner_ to all zeros, but there's no
  1198. // guarantee that a zero'd pthread_t is necessarily invalid or even different
  1199. // from pthread_self().
  1200. bool has_owner_;
  1201. pthread_t owner_; // The thread holding the mutex.
  1202. };
  1203. // Forward-declares a static mutex.
  1204. # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
  1205. extern ::testing::internal::MutexBase mutex
  1206. // Defines and statically (i.e. at link time) initializes a static mutex.
  1207. // The initialization list here does not explicitly initialize each field,
  1208. // instead relying on default initialization for the unspecified fields. In
  1209. // particular, the owner_ field (a pthread_t) is not explicitly initialized.
  1210. // This allows initialization to work whether pthread_t is a scalar or struct.
  1211. // The flag -Wmissing-field-initializers must not be specified for this to work.
  1212. # define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
  1213. ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, false }
  1214. // The Mutex class can only be used for mutexes created at runtime. It
  1215. // shares its API with MutexBase otherwise.
  1216. class Mutex : public MutexBase {
  1217. public:
  1218. Mutex() {
  1219. GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));
  1220. has_owner_ = false;
  1221. }
  1222. ~Mutex() {
  1223. GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_));
  1224. }
  1225. private:
  1226. GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex);
  1227. };
  1228. // We cannot name this class MutexLock as the ctor declaration would
  1229. // conflict with a macro named MutexLock, which is defined on some
  1230. // platforms. Hence the typedef trick below.
  1231. class GTestMutexLock {
  1232. public:
  1233. explicit GTestMutexLock(MutexBase* mutex)
  1234. : mutex_(mutex) { mutex_->Lock(); }
  1235. ~GTestMutexLock() { mutex_->Unlock(); }
  1236. private:
  1237. MutexBase* const mutex_;
  1238. GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock);
  1239. };
  1240. typedef GTestMutexLock MutexLock;
  1241. // Helpers for ThreadLocal.
  1242. // pthread_key_create() requires DeleteThreadLocalValue() to have
  1243. // C-linkage. Therefore it cannot be templatized to access
  1244. // ThreadLocal<T>. Hence the need for class
  1245. // ThreadLocalValueHolderBase.
  1246. class ThreadLocalValueHolderBase {
  1247. public:
  1248. virtual ~ThreadLocalValueHolderBase() {}
  1249. };
  1250. // Called by pthread to delete thread-local data stored by
  1251. // pthread_setspecific().
  1252. extern "C" inline void DeleteThreadLocalValue(void* value_holder) {
  1253. delete static_cast<ThreadLocalValueHolderBase*>(value_holder);
  1254. }
  1255. // Implements thread-local storage on pthreads-based systems.
  1256. //
  1257. // // Thread 1
  1258. // ThreadLocal<int> tl(100); // 100 is the default value for each thread.
  1259. //
  1260. // // Thread 2
  1261. // tl.set(150); // Changes the value for thread 2 only.
  1262. // EXPECT_EQ(150, tl.get());
  1263. //
  1264. // // Thread 1
  1265. // EXPECT_EQ(100, tl.get()); // In thread 1, tl has the original value.
  1266. // tl.set(200);
  1267. // EXPECT_EQ(200, tl.get());
  1268. //
  1269. // The template type argument T must have a public copy constructor.
  1270. // In addition, the default ThreadLocal constructor requires T to have
  1271. // a public default constructor.
  1272. //
  1273. // An object managed for a thread by a ThreadLocal instance is deleted
  1274. // when the thread exits. Or, if the ThreadLocal instance dies in
  1275. // that thread, when the ThreadLocal dies. It's the user's
  1276. // responsibility to ensure that all other threads using a ThreadLocal
  1277. // have exited when it dies, or the per-thread objects for those
  1278. // threads will not be deleted.
  1279. //
  1280. // Google Test only uses global ThreadLocal objects. That means they
  1281. // will die after main() has returned. Therefore, no per-thread
  1282. // object managed by Google Test will be leaked as long as all threads
  1283. // using Google Test have exited when main() returns.
  1284. template <typename T>
  1285. class ThreadLocal {
  1286. public:
  1287. ThreadLocal() : key_(CreateKey()),
  1288. default_() {}
  1289. explicit ThreadLocal(const T& value) : key_(CreateKey()),
  1290. default_(value) {}
  1291. ~ThreadLocal() {
  1292. // Destroys the managed object for the current thread, if any.
  1293. DeleteThreadLocalValue(pthread_getspecific(key_));
  1294. // Releases resources associated with the key. This will *not*
  1295. // delete managed objects for other threads.
  1296. GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_));
  1297. }
  1298. T* pointer() { return GetOrCreateValue(); }
  1299. const T* pointer() const { return GetOrCreateValue(); }
  1300. const T& get() const { return *pointer(); }
  1301. void set(const T& value) { *pointer() = value; }
  1302. private:
  1303. // Holds a value of type T.
  1304. class ValueHolder : public ThreadLocalValueHolderBase {
  1305. public:
  1306. explicit ValueHolder(const T& value) : value_(value) {}
  1307. T* pointer() { return &value_; }
  1308. private:
  1309. T value_;
  1310. GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder);
  1311. };
  1312. static pthread_key_t CreateKey() {
  1313. pthread_key_t key;
  1314. // When a thread exits, DeleteThreadLocalValue() will be called on
  1315. // the object managed for that thread.
  1316. GTEST_CHECK_POSIX_SUCCESS_(
  1317. pthread_key_create(&key, &DeleteThreadLocalValue));
  1318. return key;
  1319. }
  1320. T* GetOrCreateValue() const {
  1321. ThreadLocalValueHolderBase* const holder =
  1322. static_cast<ThreadLocalValueHolderBase*>(pthread_getspecific(key_));
  1323. if (holder != NULL) {
  1324. return CheckedDowncastToActualType<ValueHolder>(holder)->pointer();
  1325. }
  1326. ValueHolder* const new_holder = new ValueHolder(default_);
  1327. ThreadLocalValueHolderBase* const holder_base = new_holder;
  1328. GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base));
  1329. return new_holder->pointer();
  1330. }
  1331. // A key pthreads uses for looking up per-thread values.
  1332. const pthread_key_t key_;
  1333. const T default_; // The default value for each thread.
  1334. GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal);
  1335. };
  1336. # define GTEST_IS_THREADSAFE 1
  1337. #else // GTEST_HAS_PTHREAD
  1338. // A dummy implementation of synchronization primitives (mutex, lock,
  1339. // and thread-local variable). Necessary for compiling Google Test where
  1340. // mutex is not supported - using Google Test in multiple threads is not
  1341. // supported on such platforms.
  1342. class Mutex {
  1343. public:
  1344. Mutex() {}
  1345. void Lock() {}
  1346. void Unlock() {}
  1347. void AssertHeld() const {}
  1348. };
  1349. # define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
  1350. extern ::testing::internal::Mutex mutex
  1351. # define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex
  1352. class GTestMutexLock {
  1353. public:
  1354. explicit GTestMutexLock(Mutex*) {} // NOLINT
  1355. };
  1356. typedef GTestMutexLock MutexLock;
  1357. template <typename T>
  1358. class ThreadLocal {
  1359. public:
  1360. ThreadLocal() : value_() {}
  1361. explicit ThreadLocal(const T& value) : value_(value) {}
  1362. T* pointer() { return &value_; }
  1363. const T* pointer() const { return &value_; }
  1364. const T& get() const { return value_; }
  1365. void set(const T& value) { value_ = value; }
  1366. private:
  1367. T value_;
  1368. };
  1369. // The above synchronization primitives have dummy implementations.
  1370. // Therefore Google Test is not thread-safe.
  1371. # define GTEST_IS_THREADSAFE 0
  1372. #endif // GTEST_HAS_PTHREAD
  1373. // Returns the number of threads running in the process, or 0 to indicate that
  1374. // we cannot detect it.
  1375. GTEST_API_ size_t GetThreadCount();
  1376. // Passing non-POD classes through ellipsis (...) crashes the ARM
  1377. // compiler and generates a warning in Sun Studio. The Nokia Symbian
  1378. // and the IBM XL C/C++ compiler try to instantiate a copy constructor
  1379. // for objects passed through ellipsis (...), failing for uncopyable
  1380. // objects. We define this to ensure that only POD is passed through
  1381. // ellipsis on these systems.
  1382. #if defined(__SYMBIAN32__) || defined(__IBMCPP__) || defined(__SUNPRO_CC)
  1383. // We lose support for NULL detection where the compiler doesn't like
  1384. // passing non-POD classes through ellipsis (...).
  1385. # define GTEST_ELLIPSIS_NEEDS_POD_ 1
  1386. #else
  1387. # define GTEST_CAN_COMPARE_NULL 1
  1388. #endif
  1389. // The Nokia Symbian and IBM XL C/C++ compilers cannot decide between
  1390. // const T& and const T* in a function template. These compilers
  1391. // _can_ decide between class template specializations for T and T*,
  1392. // so a tr1::type_traits-like is_pointer works.
  1393. #if defined(__SYMBIAN32__) || defined(__IBMCPP__)
  1394. # define GTEST_NEEDS_IS_POINTER_ 1
  1395. #endif
  1396. template <bool bool_value>
  1397. struct bool_constant {
  1398. typedef bool_constant<bool_value> type;
  1399. static const bool value = bool_value;
  1400. };
  1401. template <bool bool_value> const bool bool_constant<bool_value>::value;
  1402. typedef bool_constant<false> false_type;
  1403. typedef bool_constant<true> true_type;
  1404. template <typename T>
  1405. struct is_pointer : public false_type {};
  1406. template <typename T>
  1407. struct is_pointer<T*> : public true_type {};
  1408. template <typename Iterator>
  1409. struct IteratorTraits {
  1410. typedef typename Iterator::value_type value_type;
  1411. };
  1412. template <typename T>
  1413. struct IteratorTraits<T*> {
  1414. typedef T value_type;
  1415. };
  1416. template <typename T>
  1417. struct IteratorTraits<const T*> {
  1418. typedef T value_type;
  1419. };
  1420. #if GTEST_OS_WINDOWS
  1421. # define GTEST_PATH_SEP_ "\\"
  1422. # define GTEST_HAS_ALT_PATH_SEP_ 1
  1423. // The biggest signed integer type the compiler supports.
  1424. typedef __int64 BiggestInt;
  1425. #else
  1426. # define GTEST_PATH_SEP_ "/"
  1427. # define GTEST_HAS_ALT_PATH_SEP_ 0
  1428. typedef long long BiggestInt; // NOLINT
  1429. #endif // GTEST_OS_WINDOWS
  1430. // Utilities for char.
  1431. // isspace(int ch) and friends accept an unsigned char or EOF. char
  1432. // may be signed, depending on the compiler (or compiler flags).
  1433. // Therefore we need to cast a char to unsigned char before calling
  1434. // isspace(), etc.
  1435. inline bool IsAlpha(char ch) {
  1436. return isalpha(static_cast<unsigned char>(ch)) != 0;
  1437. }
  1438. inline bool IsAlNum(char ch) {
  1439. return isalnum(static_cast<unsigned char>(ch)) != 0;
  1440. }
  1441. inline bool IsDigit(char ch) {
  1442. return isdigit(static_cast<unsigned char>(ch)) != 0;
  1443. }
  1444. inline bool IsLower(char ch) {
  1445. return islower(static_cast<unsigned char>(ch)) != 0;
  1446. }
  1447. inline bool IsSpace(char ch) {
  1448. return isspace(static_cast<unsigned char>(ch)) != 0;
  1449. }
  1450. inline bool IsUpper(char ch) {
  1451. return isupper(static_cast<unsigned char>(ch)) != 0;
  1452. }
  1453. inline bool IsXDigit(char ch) {
  1454. return isxdigit(static_cast<unsigned char>(ch)) != 0;
  1455. }
  1456. inline bool IsXDigit(wchar_t ch) {
  1457. const unsigned char low_byte = static_cast<unsigned char>(ch);
  1458. return ch == low_byte && isxdigit(low_byte) != 0;
  1459. }
  1460. inline char ToLower(char ch) {
  1461. return static_cast<char>(tolower(static_cast<unsigned char>(ch)));
  1462. }
  1463. inline char ToUpper(char ch) {
  1464. return static_cast<char>(toupper(static_cast<unsigned char>(ch)));
  1465. }
  1466. // The testing::internal::posix namespace holds wrappers for common
  1467. // POSIX functions. These wrappers hide the differences between
  1468. // Windows/MSVC and POSIX systems. Since some compilers define these
  1469. // standard functions as macros, the wrapper cannot have the same name
  1470. // as the wrapped function.
  1471. namespace posix {
  1472. // Functions with a different name on Windows.
  1473. #if GTEST_OS_WINDOWS
  1474. typedef struct _stat StatStruct;
  1475. # ifdef __BORLANDC__
  1476. inline int IsATTY(int fd) { return isatty(fd); }
  1477. inline int StrCaseCmp(const char* s1, const char* s2) {
  1478. return stricmp(s1, s2);
  1479. }
  1480. inline char* StrDup(const char* src) { return strdup(src); }
  1481. # else // !__BORLANDC__
  1482. # if GTEST_OS_WINDOWS_MOBILE
  1483. inline int IsATTY(int /* fd */) { return 0; }
  1484. # else
  1485. inline int IsATTY(int fd) { return _isatty(fd); }
  1486. # endif // GTEST_OS_WINDOWS_MOBILE
  1487. inline int StrCaseCmp(const char* s1, const char* s2) {
  1488. return _stricmp(s1, s2);
  1489. }
  1490. inline char* StrDup(const char* src) { return _strdup(src); }
  1491. # endif // __BORLANDC__
  1492. # if GTEST_OS_WINDOWS_MOBILE
  1493. inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
  1494. // Stat(), RmDir(), and IsDir() are not needed on Windows CE at this
  1495. // time and thus not defined there.
  1496. # else
  1497. inline int FileNo(FILE* file) { return _fileno(file); }
  1498. inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
  1499. inline int RmDir(const char* dir) { return _rmdir(dir); }
  1500. inline bool IsDir(const StatStruct& st) {
  1501. return (_S_IFDIR & st.st_mode) != 0;
  1502. }
  1503. # endif // GTEST_OS_WINDOWS_MOBILE
  1504. #else
  1505. typedef struct stat StatStruct;
  1506. inline int FileNo(FILE* file) { return fileno(file); }
  1507. inline int IsATTY(int fd) { return isatty(fd); }
  1508. inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
  1509. inline int StrCaseCmp(const char* s1, const char* s2) {
  1510. return strcasecmp(s1, s2);
  1511. }
  1512. inline char* StrDup(const char* src) { return strdup(src); }
  1513. inline int RmDir(const char* dir) { return rmdir(dir); }
  1514. inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
  1515. #endif // GTEST_OS_WINDOWS
  1516. // Functions deprecated by MSVC 8.0.
  1517. #ifdef _MSC_VER
  1518. // Temporarily disable warning 4996 (deprecated function).
  1519. # pragma warning(push)
  1520. # pragma warning(disable:4996)
  1521. #endif
  1522. inline const char* StrNCpy(char* dest, const char* src, size_t n) {
  1523. return strncpy(dest, src, n);
  1524. }
  1525. // ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and
  1526. // StrError() aren't needed on Windows CE at this time and thus not
  1527. // defined there.
  1528. #if !GTEST_OS_WINDOWS_MOBILE
  1529. inline int ChDir(const char* dir) { return chdir(dir); }
  1530. #endif
  1531. inline FILE* FOpen(const char* path, const char* mode) {
  1532. return fopen(path, mode);
  1533. }
  1534. #if !GTEST_OS_WINDOWS_MOBILE
  1535. inline FILE *FReopen(const char* path, const char* mode, FILE* stream) {
  1536. return freopen(path, mode, stream);
  1537. }
  1538. inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
  1539. #endif
  1540. inline int FClose(FILE* fp) { return fclose(fp); }
  1541. #if !GTEST_OS_WINDOWS_MOBILE
  1542. inline int Read(int fd, void* buf, unsigned int count) {
  1543. return static_cast<int>(read(fd, buf, count));
  1544. }
  1545. inline int Write(int fd, const void* buf, unsigned int count) {
  1546. return static_cast<int>(write(fd, buf, count));
  1547. }
  1548. inline int Close(int fd) { return close(fd); }
  1549. inline const char* StrError(int errnum) { return strerror(errnum); }
  1550. #endif
  1551. inline const char* GetEnv(const char* name) {
  1552. #if GTEST_OS_WINDOWS_MOBILE
  1553. // We are on Windows CE, which has no environment variables.
  1554. return NULL;
  1555. #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
  1556. // Environment variables which we programmatically clear will be set to the
  1557. // empty string rather than unset (NULL). Handle that case.
  1558. const char* const env = getenv(name);
  1559. return (env != NULL && env[0] != '\0') ? env : NULL;
  1560. #else
  1561. return getenv(name);
  1562. #endif
  1563. }
  1564. #ifdef _MSC_VER
  1565. # pragma warning(pop) // Restores the warning state.
  1566. #endif
  1567. #if GTEST_OS_WINDOWS_MOBILE
  1568. // Windows CE has no C library. The abort() function is used in
  1569. // several places in Google Test. This implementation provides a reasonable
  1570. // imitation of standard behaviour.
  1571. void Abort();
  1572. #else
  1573. inline void Abort() { abort(); }
  1574. #endif // GTEST_OS_WINDOWS_MOBILE
  1575. } // namespace posix
  1576. // MSVC "deprecates" snprintf and issues warnings wherever it is used. In
  1577. // order to avoid these warnings, we need to use _snprintf or _snprintf_s on
  1578. // MSVC-based platforms. We map the GTEST_SNPRINTF_ macro to the appropriate
  1579. // function in order to achieve that. We use macro definition here because
  1580. // snprintf is a variadic function.
  1581. #if _MSC_VER >= 1400 && !GTEST_OS_WINDOWS_MOBILE
  1582. // MSVC 2005 and above support variadic macros.
  1583. # define GTEST_SNPRINTF_(buffer, size, format, ...) \
  1584. _snprintf_s(buffer, size, size, format, __VA_ARGS__)
  1585. #elif defined(_MSC_VER)
  1586. // Windows CE does not define _snprintf_s and MSVC prior to 2005 doesn't
  1587. // complain about _snprintf.
  1588. # define GTEST_SNPRINTF_ _snprintf
  1589. #else
  1590. # define GTEST_SNPRINTF_ snprintf
  1591. #endif
  1592. // The maximum number a BiggestInt can represent. This definition
  1593. // works no matter BiggestInt is represented in one's complement or
  1594. // two's complement.
  1595. //
  1596. // We cannot rely on numeric_limits in STL, as __int64 and long long
  1597. // are not part of standard C++ and numeric_limits doesn't need to be
  1598. // defined for them.
  1599. const BiggestInt kMaxBiggestInt =
  1600. ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1));
  1601. // This template class serves as a compile-time function from size to
  1602. // type. It maps a size in bytes to a primitive type with that
  1603. // size. e.g.
  1604. //
  1605. // TypeWithSize<4>::UInt
  1606. //
  1607. // is typedef-ed to be unsigned int (unsigned integer made up of 4
  1608. // bytes).
  1609. //
  1610. // Such functionality should belong to STL, but I cannot find it
  1611. // there.
  1612. //
  1613. // Google Test uses this class in the implementation of floating-point
  1614. // comparison.
  1615. //
  1616. // For now it only handles UInt (unsigned int) as that's all Google Test
  1617. // needs. Other types can be easily added in the future if need
  1618. // arises.
  1619. template <size_t size>
  1620. class TypeWithSize {
  1621. public:
  1622. // This prevents the user from using TypeWithSize<N> with incorrect
  1623. // values of N.
  1624. typedef void UInt;
  1625. };
  1626. // The specialization for size 4.
  1627. template <>
  1628. class TypeWithSize<4> {
  1629. public:
  1630. // unsigned int has size 4 in both gcc and MSVC.
  1631. //
  1632. // As base/basictypes.h doesn't compile on Windows, we cannot use
  1633. // uint32, uint64, and etc here.
  1634. typedef int Int;
  1635. typedef unsigned int UInt;
  1636. };
  1637. // The specialization for size 8.
  1638. template <>
  1639. class TypeWithSize<8> {
  1640. public:
  1641. #if GTEST_OS_WINDOWS
  1642. typedef __int64 Int;
  1643. typedef unsigned __int64 UInt;
  1644. #else
  1645. typedef long long Int; // NOLINT
  1646. typedef unsigned long long UInt; // NOLINT
  1647. #endif // GTEST_OS_WINDOWS
  1648. };
  1649. // Integer types of known sizes.
  1650. typedef TypeWithSize<4>::Int Int32;
  1651. typedef TypeWithSize<4>::UInt UInt32;
  1652. typedef TypeWithSize<8>::Int Int64;
  1653. typedef TypeWithSize<8>::UInt UInt64;
  1654. typedef TypeWithSize<8>::Int TimeInMillis; // Represents time in milliseconds.
  1655. // Utilities for command line flags and environment variables.
  1656. // Macro for referencing flags.
  1657. #define GTEST_FLAG(name) FLAGS_gtest_##name
  1658. // Macros for declaring flags.
  1659. #define GTEST_DECLARE_bool_(name) GTEST_API_ extern bool GTEST_FLAG(name)
  1660. #define GTEST_DECLARE_int32_(name) \
  1661. GTEST_API_ extern ::testing::internal::Int32 GTEST_FLAG(name)
  1662. #define GTEST_DECLARE_string_(name) \
  1663. GTEST_API_ extern ::std::string GTEST_FLAG(name)
  1664. // Macros for defining flags.
  1665. #define GTEST_DEFINE_bool_(name, default_val, doc) \
  1666. GTEST_API_ bool GTEST_FLAG(name) = (default_val)
  1667. #define GTEST_DEFINE_int32_(name, default_val, doc) \
  1668. GTEST_API_ ::testing::internal::Int32 GTEST_FLAG(name) = (default_val)
  1669. #define GTEST_DEFINE_string_(name, default_val, doc) \
  1670. GTEST_API_ ::std::string GTEST_FLAG(name) = (default_val)
  1671. // Thread annotations
  1672. #define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
  1673. #define GTEST_LOCK_EXCLUDED_(locks)
  1674. // Parses 'str' for a 32-bit signed integer. If successful, writes the result
  1675. // to *value and returns true; otherwise leaves *value unchanged and returns
  1676. // false.
  1677. // TODO(chandlerc): Find a better way to refactor flag and environment parsing
  1678. // out of both gtest-port.cc and gtest.cc to avoid exporting this utility
  1679. // function.
  1680. bool ParseInt32(const Message& src_text, const char* str, Int32* value);
  1681. // Parses a bool/Int32/string from the environment variable
  1682. // corresponding to the given Google Test flag.
  1683. bool BoolFromGTestEnv(const char* flag, bool default_val);
  1684. GTEST_API_ Int32 Int32FromGTestEnv(const char* flag, Int32 default_val);
  1685. const char* StringFromGTestEnv(const char* flag, const char* default_val);
  1686. } // namespace internal
  1687. } // namespace testing
  1688. #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_