gtest-port.h 93 KB

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