gtest-port.h 91 KB

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