File indexing completed on 2025-09-18 09:18:09
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
0046 #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265 #if defined(_MSVC_LANG)
0266 #define GTEST_INTERNAL_CPLUSPLUS_LANG _MSVC_LANG
0267 #elif defined(__cplusplus)
0268 #define GTEST_INTERNAL_CPLUSPLUS_LANG __cplusplus
0269 #endif
0270
0271 #if !defined(GTEST_INTERNAL_CPLUSPLUS_LANG) || \
0272 GTEST_INTERNAL_CPLUSPLUS_LANG < 201703L
0273 #error C++ versions less than C++17 are not supported.
0274 #endif
0275
0276
0277 #ifdef __has_include
0278 #define GTEST_INTERNAL_HAS_INCLUDE __has_include
0279 #else
0280 #define GTEST_INTERNAL_HAS_INCLUDE(...) 0
0281 #endif
0282
0283
0284
0285
0286
0287
0288
0289 #if GTEST_INTERNAL_CPLUSPLUS_LANG >= 202002L || \
0290 GTEST_INTERNAL_HAS_INCLUDE(<version>)
0291 #include <version> // C++20 or <version> support.
0292 #else
0293 #include <ciso646> // Pre-C++20
0294 #endif
0295
0296 #include <ctype.h> // for isspace, etc
0297 #include <stddef.h> // for ptrdiff_t
0298 #include <stdio.h>
0299 #include <stdlib.h>
0300 #include <string.h>
0301
0302 #include <cerrno>
0303
0304 #include <cstdint>
0305 #include <iostream>
0306 #include <limits>
0307 #include <locale>
0308 #include <memory>
0309 #include <ostream>
0310 #include <string>
0311
0312 #include <tuple>
0313 #include <type_traits>
0314 #include <vector>
0315
0316 #ifndef _WIN32_WCE
0317 #include <sys/stat.h>
0318 #include <sys/types.h>
0319 #endif
0320
0321 #if defined __APPLE__
0322 #include <AvailabilityMacros.h>
0323 #include <TargetConditionals.h>
0324 #endif
0325
0326 #include "gtest/internal/custom/gtest-port.h"
0327 #include "gtest/internal/gtest-port-arch.h"
0328
0329 #ifndef GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
0330 #define GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ 0
0331 #endif
0332
0333 #ifndef GTEST_HAS_NOTIFICATION_
0334 #define GTEST_HAS_NOTIFICATION_ 0
0335 #endif
0336
0337 #if defined(GTEST_HAS_ABSL) && !defined(GTEST_NO_ABSL_FLAGS)
0338 #define GTEST_INTERNAL_HAS_ABSL_FLAGS
0339 #include "absl/flags/declare.h"
0340 #include "absl/flags/flag.h"
0341 #include "absl/flags/reflection.h"
0342 #endif
0343
0344 #if !defined(GTEST_DEV_EMAIL_)
0345 #define GTEST_DEV_EMAIL_ "googletestframework@@googlegroups.com"
0346 #define GTEST_FLAG_PREFIX_ "gtest_"
0347 #define GTEST_FLAG_PREFIX_DASH_ "gtest-"
0348 #define GTEST_FLAG_PREFIX_UPPER_ "GTEST_"
0349 #define GTEST_NAME_ "Google Test"
0350 #define GTEST_PROJECT_URL_ "https://github.com/google/googletest/"
0351 #endif
0352
0353 #if !defined(GTEST_INIT_GOOGLE_TEST_NAME_)
0354 #define GTEST_INIT_GOOGLE_TEST_NAME_ "testing::InitGoogleTest"
0355 #endif
0356
0357
0358 #ifdef __GNUC__
0359
0360 #define GTEST_GCC_VER_ \
0361 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
0362 #endif
0363
0364
0365
0366
0367
0368
0369 #if defined(_MSC_VER)
0370 #define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) \
0371 __pragma(warning(push)) __pragma(warning(disable : warnings))
0372 #define GTEST_DISABLE_MSC_WARNINGS_POP_() __pragma(warning(pop))
0373 #else
0374
0375 #define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
0376 #define GTEST_DISABLE_MSC_WARNINGS_POP_()
0377 #endif
0378
0379
0380
0381 #ifdef __clang__
0382 #define GTEST_DISABLE_MSC_DEPRECATED_PUSH_() \
0383 _Pragma("clang diagnostic push") \
0384 _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") \
0385 _Pragma("clang diagnostic ignored \"-Wdeprecated-implementations\"")
0386 #define GTEST_DISABLE_MSC_DEPRECATED_POP_() _Pragma("clang diagnostic pop")
0387 #else
0388 #define GTEST_DISABLE_MSC_DEPRECATED_PUSH_() \
0389 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996)
0390 #define GTEST_DISABLE_MSC_DEPRECATED_POP_() GTEST_DISABLE_MSC_WARNINGS_POP_()
0391 #endif
0392
0393
0394
0395
0396 #ifdef GTEST_OS_WINDOWS
0397 #ifndef GTEST_OS_WINDOWS_MOBILE
0398 #include <direct.h>
0399 #include <io.h>
0400 #endif
0401
0402 #if defined(GTEST_OS_WINDOWS_MINGW) && !defined(__MINGW64_VERSION_MAJOR)
0403
0404
0405 typedef struct _CRITICAL_SECTION GTEST_CRITICAL_SECTION;
0406 #else
0407
0408
0409
0410 typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
0411 #endif
0412 #elif defined(GTEST_OS_XTENSA)
0413 #include <unistd.h>
0414
0415
0416 #else
0417
0418
0419
0420 #include <strings.h>
0421 #include <unistd.h>
0422 #endif
0423
0424 #ifdef GTEST_OS_LINUX_ANDROID
0425
0426 #include <android/api-level.h> // NOLINT
0427 #endif
0428
0429
0430
0431 #ifndef GTEST_HAS_POSIX_RE
0432 #ifdef GTEST_OS_LINUX_ANDROID
0433
0434 #define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9)
0435 #else
0436 #if !(defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_XTENSA) || \
0437 defined(GTEST_OS_QURT))
0438 #define GTEST_HAS_POSIX_RE 1
0439 #else
0440 #define GTEST_HAS_POSIX_RE 0
0441 #endif
0442 #endif
0443 #endif
0444
0445
0446 #ifdef GTEST_HAS_ABSL
0447
0448 #include "absl/strings/string_view.h"
0449 #include "re2/re2.h"
0450 #define GTEST_USES_RE2 1
0451 #elif GTEST_HAS_POSIX_RE
0452 #include <regex.h> // NOLINT
0453 #define GTEST_USES_POSIX_RE 1
0454 #else
0455
0456 #define GTEST_USES_SIMPLE_RE 1
0457 #endif
0458
0459 #ifndef GTEST_HAS_EXCEPTIONS
0460
0461
0462 #if defined(_MSC_VER) && defined(_CPPUNWIND)
0463
0464 #define GTEST_HAS_EXCEPTIONS 1
0465 #elif defined(__BORLANDC__)
0466
0467
0468
0469 #ifndef _HAS_EXCEPTIONS
0470 #define _HAS_EXCEPTIONS 1
0471 #endif
0472 #define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS
0473 #elif defined(__clang__)
0474
0475
0476
0477
0478
0479
0480
0481
0482 #if defined(__EXCEPTIONS) && __EXCEPTIONS && __has_feature(cxx_exceptions)
0483 #define GTEST_HAS_EXCEPTIONS 1
0484 #else
0485 #define GTEST_HAS_EXCEPTIONS 0
0486 #endif
0487 #elif defined(__GNUC__) && defined(__EXCEPTIONS) && __EXCEPTIONS
0488
0489 #define GTEST_HAS_EXCEPTIONS 1
0490 #elif defined(__SUNPRO_CC)
0491
0492
0493
0494 #define GTEST_HAS_EXCEPTIONS 1
0495 #elif defined(__IBMCPP__) && defined(__EXCEPTIONS) && __EXCEPTIONS
0496
0497 #define GTEST_HAS_EXCEPTIONS 1
0498 #elif defined(__HP_aCC)
0499
0500
0501 #define GTEST_HAS_EXCEPTIONS 1
0502 #else
0503
0504
0505 #define GTEST_HAS_EXCEPTIONS 0
0506 #endif
0507 #endif
0508
0509 #ifndef GTEST_HAS_STD_WSTRING
0510
0511
0512
0513
0514
0515 #if (!(defined(GTEST_OS_LINUX_ANDROID) || defined(GTEST_OS_CYGWIN) || \
0516 defined(GTEST_OS_SOLARIS) || defined(GTEST_OS_HAIKU) || \
0517 defined(GTEST_OS_ESP32) || defined(GTEST_OS_ESP8266) || \
0518 defined(GTEST_OS_XTENSA) || defined(GTEST_OS_QURT) || \
0519 defined(GTEST_OS_NXP_QN9090) || defined(GTEST_OS_NRF52)))
0520 #define GTEST_HAS_STD_WSTRING 1
0521 #else
0522 #define GTEST_HAS_STD_WSTRING 0
0523 #endif
0524 #endif
0525
0526 #ifndef GTEST_HAS_FILE_SYSTEM
0527
0528 #define GTEST_HAS_FILE_SYSTEM 1
0529 #endif
0530
0531
0532 #ifndef GTEST_HAS_RTTI
0533
0534
0535
0536 #ifdef _MSC_VER
0537
0538 #ifdef _CPPRTTI
0539 #define GTEST_HAS_RTTI 1
0540 #else
0541 #define GTEST_HAS_RTTI 0
0542 #endif
0543
0544
0545
0546 #elif defined(__GNUC__)
0547
0548 #ifdef __GXX_RTTI
0549
0550
0551
0552
0553 #if defined(GTEST_OS_LINUX_ANDROID) && defined(_STLPORT_MAJOR) && \
0554 !defined(__EXCEPTIONS)
0555 #define GTEST_HAS_RTTI 0
0556 #else
0557 #define GTEST_HAS_RTTI 1
0558 #endif
0559 #else
0560 #define GTEST_HAS_RTTI 0
0561 #endif
0562
0563
0564
0565
0566 #elif defined(__clang__)
0567
0568 #define GTEST_HAS_RTTI __has_feature(cxx_rtti)
0569
0570
0571
0572 #elif defined(__IBMCPP__) && (__IBMCPP__ >= 900)
0573
0574 #ifdef __RTTI_ALL__
0575 #define GTEST_HAS_RTTI 1
0576 #else
0577 #define GTEST_HAS_RTTI 0
0578 #endif
0579
0580 #else
0581
0582
0583 #define GTEST_HAS_RTTI 1
0584
0585 #endif
0586
0587 #endif
0588
0589
0590
0591 #if GTEST_HAS_RTTI
0592 #include <typeinfo>
0593 #endif
0594
0595
0596 #ifndef GTEST_HAS_PTHREAD
0597
0598
0599
0600
0601
0602 #if (defined(GTEST_OS_LINUX) || defined(GTEST_OS_MAC) || \
0603 defined(GTEST_OS_HPUX) || defined(GTEST_OS_QNX) || \
0604 defined(GTEST_OS_FREEBSD) || defined(GTEST_OS_NACL) || \
0605 defined(GTEST_OS_NETBSD) || defined(GTEST_OS_FUCHSIA) || \
0606 defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_GNU_KFREEBSD) || \
0607 defined(GTEST_OS_OPENBSD) || defined(GTEST_OS_HAIKU) || \
0608 defined(GTEST_OS_GNU_HURD) || defined(GTEST_OS_SOLARIS) || \
0609 defined(GTEST_OS_AIX) || defined(GTEST_OS_ZOS))
0610 #define GTEST_HAS_PTHREAD 1
0611 #else
0612 #define GTEST_HAS_PTHREAD 0
0613 #endif
0614 #endif
0615
0616 #if GTEST_HAS_PTHREAD
0617
0618
0619 #include <pthread.h> // NOLINT
0620
0621
0622 #include <time.h> // NOLINT
0623 #endif
0624
0625
0626
0627
0628
0629 #ifndef GTEST_HAS_CLONE
0630
0631
0632 #if defined(GTEST_OS_LINUX) && !defined(__ia64__)
0633 #if defined(GTEST_OS_LINUX_ANDROID)
0634
0635
0636 #if defined(__LP64__) || (defined(__arm__) && __ANDROID_API__ >= 9) || \
0637 (defined(__mips__) && __ANDROID_API__ >= 12) || \
0638 (defined(__i386__) && __ANDROID_API__ >= 17)
0639 #define GTEST_HAS_CLONE 1
0640 #else
0641 #define GTEST_HAS_CLONE 0
0642 #endif
0643 #else
0644 #define GTEST_HAS_CLONE 1
0645 #endif
0646 #else
0647 #define GTEST_HAS_CLONE 0
0648 #endif
0649
0650 #endif
0651
0652
0653
0654 #ifndef GTEST_HAS_STREAM_REDIRECTION
0655
0656
0657
0658 #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_WINDOWS_PHONE) || \
0659 defined(GTEST_OS_WINDOWS_RT) || defined(GTEST_OS_WINDOWS_GAMES) || \
0660 defined(GTEST_OS_ESP8266) || defined(GTEST_OS_XTENSA) || \
0661 defined(GTEST_OS_QURT) || !GTEST_HAS_FILE_SYSTEM
0662 #define GTEST_HAS_STREAM_REDIRECTION 0
0663 #else
0664 #define GTEST_HAS_STREAM_REDIRECTION 1
0665 #endif
0666 #endif
0667
0668
0669
0670 #if (defined(GTEST_OS_LINUX) || defined(GTEST_OS_CYGWIN) || \
0671 defined(GTEST_OS_SOLARIS) || defined(GTEST_OS_ZOS) || \
0672 (defined(GTEST_OS_MAC) && !defined(GTEST_OS_IOS)) || \
0673 (defined(GTEST_OS_WINDOWS_DESKTOP) && _MSC_VER) || \
0674 defined(GTEST_OS_WINDOWS_MINGW) || defined(GTEST_OS_AIX) || \
0675 defined(GTEST_OS_HPUX) || defined(GTEST_OS_OPENBSD) || \
0676 defined(GTEST_OS_QNX) || defined(GTEST_OS_FREEBSD) || \
0677 defined(GTEST_OS_NETBSD) || defined(GTEST_OS_FUCHSIA) || \
0678 defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_GNU_KFREEBSD) || \
0679 defined(GTEST_OS_HAIKU) || defined(GTEST_OS_GNU_HURD))
0680
0681 #if GTEST_HAS_FILE_SYSTEM
0682 #define GTEST_HAS_DEATH_TEST 1
0683 #endif
0684 #endif
0685
0686
0687
0688
0689
0690 #if defined(__GNUC__) || defined(_MSC_VER) || defined(__SUNPRO_CC) || \
0691 defined(__IBMCPP__) || defined(__HP_aCC)
0692 #define GTEST_HAS_TYPED_TEST 1
0693 #define GTEST_HAS_TYPED_TEST_P 1
0694 #endif
0695
0696
0697 #if defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_CYGWIN) || \
0698 defined(GTEST_OS_AIX) || defined(GTEST_OS_OS2)
0699 #define GTEST_WIDE_STRING_USES_UTF16_ 1
0700 #else
0701 #define GTEST_WIDE_STRING_USES_UTF16_ 0
0702 #endif
0703
0704
0705 #if defined(GTEST_OS_LINUX) || defined(GTEST_OS_GNU_KFREEBSD) || \
0706 defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_FREEBSD) || \
0707 defined(GTEST_OS_NETBSD) || defined(GTEST_OS_OPENBSD) || \
0708 defined(GTEST_OS_GNU_HURD) || defined(GTEST_OS_MAC)
0709 #define GTEST_CAN_STREAM_RESULTS_ 1
0710 #else
0711 #define GTEST_CAN_STREAM_RESULTS_ 0
0712 #endif
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724 #ifdef __INTEL_COMPILER
0725 #define GTEST_AMBIGUOUS_ELSE_BLOCKER_
0726 #else
0727 #define GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
0728 switch (0) \
0729 case 0: \
0730 default:
0731 #endif
0732
0733
0734
0735
0736
0737
0738
0739
0740
0741
0742
0743 #ifdef __has_attribute
0744 #define GTEST_HAVE_ATTRIBUTE_(x) __has_attribute(x)
0745 #else
0746 #define GTEST_HAVE_ATTRIBUTE_(x) 0
0747 #endif
0748
0749
0750
0751
0752
0753
0754
0755 #if defined(__has_cpp_attribute)
0756
0757
0758 #define GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
0759 #else
0760 #define GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(x) 0
0761 #endif
0762
0763
0764
0765
0766
0767 #ifdef __has_feature
0768 #define GTEST_HAVE_FEATURE_(x) __has_feature(x)
0769 #else
0770 #define GTEST_HAVE_FEATURE_(x) 0
0771 #endif
0772
0773
0774 #if GTEST_HAVE_ATTRIBUTE_(format) && defined(__MINGW_PRINTF_FORMAT)
0775
0776
0777
0778 #define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \
0779 __attribute__((format(__MINGW_PRINTF_FORMAT, string_index, first_to_check)))
0780 #elif GTEST_HAVE_ATTRIBUTE_(format)
0781 #define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \
0782 __attribute__((format(printf, string_index, first_to_check)))
0783 #else
0784 #define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check)
0785 #endif
0786
0787
0788
0789
0790
0791
0792
0793
0794
0795 #define GTEST_INTENTIONAL_CONST_COND_PUSH_() \
0796 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127)
0797 #define GTEST_INTENTIONAL_CONST_COND_POP_() GTEST_DISABLE_MSC_WARNINGS_POP_()
0798
0799
0800
0801
0802 #ifndef GTEST_HAS_SEH
0803
0804
0805 #if defined(_MSC_VER) || defined(__BORLANDC__)
0806
0807 #define GTEST_HAS_SEH 1
0808 #else
0809
0810 #define GTEST_HAS_SEH 0
0811 #endif
0812
0813 #endif
0814
0815 #ifndef GTEST_IS_THREADSAFE
0816
0817 #if (GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ || \
0818 (defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_PHONE) && \
0819 !defined(GTEST_OS_WINDOWS_RT)) || \
0820 GTEST_HAS_PTHREAD)
0821 #define GTEST_IS_THREADSAFE 1
0822 #endif
0823
0824 #endif
0825
0826 #ifdef GTEST_IS_THREADSAFE
0827
0828 #include <condition_variable> // NOLINT
0829 #include <mutex> // NOLINT
0830 #endif
0831
0832
0833
0834
0835 #ifndef GTEST_API_
0836
0837 #ifdef _MSC_VER
0838 #if defined(GTEST_LINKED_AS_SHARED_LIBRARY) && GTEST_LINKED_AS_SHARED_LIBRARY
0839 #define GTEST_API_ __declspec(dllimport)
0840 #elif defined(GTEST_CREATE_SHARED_LIBRARY) && GTEST_CREATE_SHARED_LIBRARY
0841 #define GTEST_API_ __declspec(dllexport)
0842 #endif
0843 #elif GTEST_HAVE_ATTRIBUTE_(visibility)
0844 #define GTEST_API_ __attribute__((visibility("default")))
0845 #endif
0846
0847 #endif
0848
0849 #ifndef GTEST_API_
0850 #define GTEST_API_
0851 #endif
0852
0853 #ifndef GTEST_DEFAULT_DEATH_TEST_STYLE
0854 #define GTEST_DEFAULT_DEATH_TEST_STYLE "fast"
0855 #endif
0856
0857 #if GTEST_HAVE_ATTRIBUTE_(noinline)
0858
0859 #define GTEST_NO_INLINE_ __attribute__((noinline))
0860 #else
0861 #define GTEST_NO_INLINE_
0862 #endif
0863
0864 #if GTEST_HAVE_ATTRIBUTE_(disable_tail_calls)
0865
0866
0867 #define GTEST_NO_TAIL_CALL_ __attribute__((disable_tail_calls))
0868 #elif defined(__GNUC__) && !defined(__NVCOMPILER)
0869 #define GTEST_NO_TAIL_CALL_ \
0870 __attribute__((optimize("no-optimize-sibling-calls")))
0871 #else
0872 #define GTEST_NO_TAIL_CALL_
0873 #endif
0874
0875
0876 #if !defined(GTEST_HAS_CXXABI_H_)
0877 #if defined(__GLIBCXX__) || (defined(_LIBCPP_VERSION) && !defined(_MSC_VER))
0878 #define GTEST_HAS_CXXABI_H_ 1
0879 #else
0880 #define GTEST_HAS_CXXABI_H_ 0
0881 #endif
0882 #endif
0883
0884
0885
0886 #if GTEST_HAVE_ATTRIBUTE_(no_sanitize_memory)
0887 #define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ __attribute__((no_sanitize_memory))
0888 #else
0889 #define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
0890 #endif
0891
0892
0893 #if GTEST_HAVE_ATTRIBUTE_(no_sanitize_address)
0894 #define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ \
0895 __attribute__((no_sanitize_address))
0896 #else
0897 #define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
0898 #endif
0899
0900
0901 #if GTEST_HAVE_FEATURE_(hwaddress_sanitizer) && \
0902 GTEST_HAVE_ATTRIBUTE_(no_sanitize)
0903 #define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ \
0904 __attribute__((no_sanitize("hwaddress")))
0905 #else
0906 #define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_
0907 #endif
0908
0909
0910 #if GTEST_HAVE_ATTRIBUTE_(no_sanitize_thread)
0911 #define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ __attribute((no_sanitize_thread))
0912 #else
0913 #define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
0914 #endif
0915
0916 namespace testing {
0917
0918 class Message;
0919
0920
0921
0922 using std::get;
0923 using std::make_tuple;
0924 using std::tuple;
0925 using std::tuple_element;
0926 using std::tuple_size;
0927
0928 namespace internal {
0929
0930
0931
0932
0933 class Secret {
0934 Secret(const Secret&) = delete;
0935 };
0936
0937
0938
0939 GTEST_API_ bool IsTrue(bool condition);
0940
0941
0942
0943 #ifdef GTEST_USES_RE2
0944
0945
0946
0947
0948 class GTEST_API_ RE {
0949 public:
0950 RE(absl::string_view regex) : regex_(regex) {}
0951 RE(const char* regex) : RE(absl::string_view(regex)) {}
0952 RE(const std::string& regex) : RE(absl::string_view(regex)) {}
0953 RE(const RE& other) : RE(other.pattern()) {}
0954
0955 const std::string& pattern() const { return regex_.pattern(); }
0956
0957 static bool FullMatch(absl::string_view str, const RE& re) {
0958 return RE2::FullMatch(str, re.regex_);
0959 }
0960 static bool PartialMatch(absl::string_view str, const RE& re) {
0961 return RE2::PartialMatch(str, re.regex_);
0962 }
0963
0964 private:
0965 RE2 regex_;
0966 };
0967
0968 #elif defined(GTEST_USES_POSIX_RE) || defined(GTEST_USES_SIMPLE_RE)
0969 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
0970 )
0971
0972
0973
0974 class GTEST_API_ RE {
0975 public:
0976
0977
0978 RE(const RE& other) { Init(other.pattern()); }
0979
0980
0981 RE(const ::std::string& regex) { Init(regex.c_str()); }
0982
0983 RE(const char* regex) { Init(regex); }
0984 ~RE();
0985
0986
0987 const char* pattern() const { return pattern_.c_str(); }
0988
0989
0990
0991
0992
0993 static bool FullMatch(const ::std::string& str, const RE& re) {
0994 return FullMatch(str.c_str(), re);
0995 }
0996 static bool PartialMatch(const ::std::string& str, const RE& re) {
0997 return PartialMatch(str.c_str(), re);
0998 }
0999
1000 static bool FullMatch(const char* str, const RE& re);
1001 static bool PartialMatch(const char* str, const RE& re);
1002
1003 private:
1004 void Init(const char* regex);
1005 std::string pattern_;
1006 bool is_valid_;
1007
1008 #ifdef GTEST_USES_POSIX_RE
1009
1010 regex_t full_regex_;
1011 regex_t partial_regex_;
1012
1013 #else
1014
1015 std::string full_pattern_;
1016
1017 #endif
1018 };
1019 GTEST_DISABLE_MSC_WARNINGS_POP_()
1020 #endif
1021
1022
1023
1024 GTEST_API_ ::std::string FormatFileLocation(const char* file, int line);
1025
1026
1027
1028
1029 GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char* file,
1030 int line);
1031
1032
1033
1034
1035
1036
1037
1038 enum GTestLogSeverity { GTEST_INFO, GTEST_WARNING, GTEST_ERROR, GTEST_FATAL };
1039
1040
1041
1042
1043 class GTEST_API_ GTestLog {
1044 public:
1045 GTestLog(GTestLogSeverity severity, const char* file, int line);
1046
1047
1048 ~GTestLog();
1049
1050 ::std::ostream& GetStream() { return ::std::cerr; }
1051
1052 private:
1053 const GTestLogSeverity severity_;
1054
1055 GTestLog(const GTestLog&) = delete;
1056 GTestLog& operator=(const GTestLog&) = delete;
1057 };
1058
1059 #if !defined(GTEST_LOG_)
1060
1061 #define GTEST_LOG_(severity) \
1062 ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
1063 __FILE__, __LINE__) \
1064 .GetStream()
1065
1066 inline void LogToStderr() {}
1067 inline void FlushInfoLog() { fflush(nullptr); }
1068
1069 #endif
1070
1071 #if !defined(GTEST_CHECK_)
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086 #define GTEST_CHECK_(condition) \
1087 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
1088 if (::testing::internal::IsTrue(condition)) \
1089 ; \
1090 else \
1091 GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
1092 #endif
1093
1094
1095
1096
1097
1098
1099 #define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \
1100 if (const int gtest_error = (posix_call)) \
1101 GTEST_LOG_(FATAL) << #posix_call << "failed with error " << gtest_error
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114 template <typename T>
1115 struct ConstRef {
1116 typedef const T& type;
1117 };
1118 template <typename T>
1119 struct ConstRef<T&> {
1120 typedef T& type;
1121 };
1122
1123
1124 #define GTEST_REFERENCE_TO_CONST_(T) \
1125 typename ::testing::internal::ConstRef<T>::type
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147 template <typename To>
1148 inline To ImplicitCast_(To x) {
1149 return x;
1150 }
1151
1152
1153
1154
1155
1156
1157 template <class Derived, class Base>
1158 Derived* CheckedDowncastToActualType(Base* base) {
1159 static_assert(std::is_base_of<Base, Derived>::value,
1160 "target type not derived from source type");
1161 #if GTEST_HAS_RTTI
1162 GTEST_CHECK_(base == nullptr || dynamic_cast<Derived*>(base) != nullptr);
1163 #endif
1164 return static_cast<Derived*>(base);
1165 }
1166
1167 #if GTEST_HAS_STREAM_REDIRECTION
1168
1169
1170
1171
1172
1173
1174
1175 GTEST_API_ void CaptureStdout();
1176 GTEST_API_ std::string GetCapturedStdout();
1177 GTEST_API_ void CaptureStderr();
1178 GTEST_API_ std::string GetCapturedStderr();
1179
1180 #endif
1181
1182 GTEST_API_ size_t GetFileSize(FILE* file);
1183
1184
1185 GTEST_API_ std::string ReadEntireFile(FILE* file);
1186
1187
1188 GTEST_API_ std::vector<std::string> GetArgvs();
1189
1190 #ifdef GTEST_HAS_DEATH_TEST
1191
1192 std::vector<std::string> GetInjectableArgvs();
1193
1194 void SetInjectableArgvs(const std::vector<std::string>* new_argvs);
1195 void SetInjectableArgvs(const std::vector<std::string>& new_argvs);
1196 void ClearInjectableArgvs();
1197
1198 #endif
1199
1200
1201 #ifdef GTEST_IS_THREADSAFE
1202
1203 #ifdef GTEST_OS_WINDOWS
1204
1205
1206 class GTEST_API_ AutoHandle {
1207 public:
1208
1209
1210
1211
1212
1213 typedef void* Handle;
1214 AutoHandle();
1215 explicit AutoHandle(Handle handle);
1216
1217 ~AutoHandle();
1218
1219 Handle Get() const;
1220 void Reset();
1221 void Reset(Handle handle);
1222
1223 private:
1224
1225
1226 bool IsCloseable() const;
1227
1228 Handle handle_;
1229
1230 AutoHandle(const AutoHandle&) = delete;
1231 AutoHandle& operator=(const AutoHandle&) = delete;
1232 };
1233 #endif
1234
1235 #if GTEST_HAS_NOTIFICATION_
1236
1237
1238
1239 #else
1240 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
1241 )
1242
1243
1244
1245
1246
1247
1248
1249
1250 class GTEST_API_ Notification {
1251 public:
1252 Notification() : notified_(false) {}
1253 Notification(const Notification&) = delete;
1254 Notification& operator=(const Notification&) = delete;
1255
1256
1257
1258 void Notify() {
1259 std::lock_guard<std::mutex> lock(mu_);
1260 notified_ = true;
1261 cv_.notify_all();
1262 }
1263
1264
1265
1266 void WaitForNotification() {
1267 std::unique_lock<std::mutex> lock(mu_);
1268 cv_.wait(lock, [this]() { return notified_; });
1269 }
1270
1271 private:
1272 std::mutex mu_;
1273 std::condition_variable cv_;
1274 bool notified_;
1275 };
1276 GTEST_DISABLE_MSC_WARNINGS_POP_()
1277 #endif
1278
1279
1280
1281
1282 #if GTEST_HAS_PTHREAD && !defined(GTEST_OS_WINDOWS_MINGW)
1283
1284
1285
1286
1287
1288
1289 class ThreadWithParamBase {
1290 public:
1291 virtual ~ThreadWithParamBase() = default;
1292 virtual void Run() = 0;
1293 };
1294
1295
1296
1297
1298
1299
1300
1301 extern "C" inline void* ThreadFuncWithCLinkage(void* thread) {
1302 static_cast<ThreadWithParamBase*>(thread)->Run();
1303 return nullptr;
1304 }
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318 template <typename T>
1319 class ThreadWithParam : public ThreadWithParamBase {
1320 public:
1321 typedef void UserThreadFunc(T);
1322
1323 ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start)
1324 : func_(func),
1325 param_(param),
1326 thread_can_start_(thread_can_start),
1327 finished_(false) {
1328 ThreadWithParamBase* const base = this;
1329
1330
1331 GTEST_CHECK_POSIX_SUCCESS_(
1332 pthread_create(&thread_, nullptr, &ThreadFuncWithCLinkage, base));
1333 }
1334 ~ThreadWithParam() override { Join(); }
1335
1336 void Join() {
1337 if (!finished_) {
1338 GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, nullptr));
1339 finished_ = true;
1340 }
1341 }
1342
1343 void Run() override {
1344 if (thread_can_start_ != nullptr) thread_can_start_->WaitForNotification();
1345 func_(param_);
1346 }
1347
1348 private:
1349 UserThreadFunc* const func_;
1350 const T param_;
1351
1352
1353 Notification* const thread_can_start_;
1354 bool finished_;
1355
1356 pthread_t thread_;
1357
1358 ThreadWithParam(const ThreadWithParam&) = delete;
1359 ThreadWithParam& operator=(const ThreadWithParam&) = delete;
1360 };
1361 #endif
1362
1363
1364 #if GTEST_HAS_MUTEX_AND_THREAD_LOCAL_
1365
1366
1367
1368 #elif defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_PHONE) && \
1369 !defined(GTEST_OS_WINDOWS_RT)
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385 class GTEST_API_ Mutex {
1386 public:
1387 enum MutexType { kStatic = 0, kDynamic = 1 };
1388
1389
1390
1391 enum StaticConstructorSelector { kStaticMutex = 0 };
1392
1393
1394
1395
1396 explicit Mutex(StaticConstructorSelector ) {}
1397
1398 Mutex();
1399 ~Mutex();
1400
1401 void Lock();
1402
1403 void Unlock();
1404
1405
1406
1407 void AssertHeld();
1408
1409 private:
1410
1411 void ThreadSafeLazyInit();
1412
1413
1414
1415 unsigned int owner_thread_id_;
1416
1417
1418
1419 MutexType type_;
1420 long critical_section_init_phase_;
1421 GTEST_CRITICAL_SECTION* critical_section_;
1422
1423 Mutex(const Mutex&) = delete;
1424 Mutex& operator=(const Mutex&) = delete;
1425 };
1426
1427 #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1428 extern ::testing::internal::Mutex mutex
1429
1430 #define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1431 ::testing::internal::Mutex mutex(::testing::internal::Mutex::kStaticMutex)
1432
1433
1434
1435
1436
1437
1438 class GTestMutexLock {
1439 public:
1440 explicit GTestMutexLock(Mutex* mutex) : mutex_(mutex) { mutex_->Lock(); }
1441
1442 ~GTestMutexLock() { mutex_->Unlock(); }
1443
1444 private:
1445 Mutex* const mutex_;
1446
1447 GTestMutexLock(const GTestMutexLock&) = delete;
1448 GTestMutexLock& operator=(const GTestMutexLock&) = delete;
1449 };
1450
1451 typedef GTestMutexLock MutexLock;
1452
1453
1454
1455 class ThreadLocalValueHolderBase {
1456 public:
1457 virtual ~ThreadLocalValueHolderBase() {}
1458 };
1459
1460
1461
1462 class ThreadLocalBase {
1463 public:
1464
1465
1466
1467
1468 virtual ThreadLocalValueHolderBase* NewValueForCurrentThread() const = 0;
1469
1470 protected:
1471 ThreadLocalBase() {}
1472 virtual ~ThreadLocalBase() {}
1473
1474 private:
1475 ThreadLocalBase(const ThreadLocalBase&) = delete;
1476 ThreadLocalBase& operator=(const ThreadLocalBase&) = delete;
1477 };
1478
1479
1480
1481
1482 class GTEST_API_ ThreadLocalRegistry {
1483 public:
1484
1485
1486 static ThreadLocalValueHolderBase* GetValueOnCurrentThread(
1487 const ThreadLocalBase* thread_local_instance);
1488
1489
1490 static void OnThreadLocalDestroyed(
1491 const ThreadLocalBase* thread_local_instance);
1492 };
1493
1494 class GTEST_API_ ThreadWithParamBase {
1495 public:
1496 void Join();
1497
1498 protected:
1499 class Runnable {
1500 public:
1501 virtual ~Runnable() {}
1502 virtual void Run() = 0;
1503 };
1504
1505 ThreadWithParamBase(Runnable* runnable, Notification* thread_can_start);
1506 virtual ~ThreadWithParamBase();
1507
1508 private:
1509 AutoHandle thread_;
1510 };
1511
1512
1513 template <typename T>
1514 class ThreadWithParam : public ThreadWithParamBase {
1515 public:
1516 typedef void UserThreadFunc(T);
1517
1518 ThreadWithParam(UserThreadFunc* func, T param, Notification* thread_can_start)
1519 : ThreadWithParamBase(new RunnableImpl(func, param), thread_can_start) {}
1520 virtual ~ThreadWithParam() {}
1521
1522 private:
1523 class RunnableImpl : public Runnable {
1524 public:
1525 RunnableImpl(UserThreadFunc* func, T param) : func_(func), param_(param) {}
1526 virtual ~RunnableImpl() {}
1527 virtual void Run() { func_(param_); }
1528
1529 private:
1530 UserThreadFunc* const func_;
1531 const T param_;
1532
1533 RunnableImpl(const RunnableImpl&) = delete;
1534 RunnableImpl& operator=(const RunnableImpl&) = delete;
1535 };
1536
1537 ThreadWithParam(const ThreadWithParam&) = delete;
1538 ThreadWithParam& operator=(const ThreadWithParam&) = delete;
1539 };
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568 template <typename T>
1569 class ThreadLocal : public ThreadLocalBase {
1570 public:
1571 ThreadLocal() : default_factory_(new DefaultValueHolderFactory()) {}
1572 explicit ThreadLocal(const T& value)
1573 : default_factory_(new InstanceValueHolderFactory(value)) {}
1574
1575 ~ThreadLocal() override { ThreadLocalRegistry::OnThreadLocalDestroyed(this); }
1576
1577 T* pointer() { return GetOrCreateValue(); }
1578 const T* pointer() const { return GetOrCreateValue(); }
1579 const T& get() const { return *pointer(); }
1580 void set(const T& value) { *pointer() = value; }
1581
1582 private:
1583
1584
1585 class ValueHolder : public ThreadLocalValueHolderBase {
1586 public:
1587 ValueHolder() : value_() {}
1588 explicit ValueHolder(const T& value) : value_(value) {}
1589
1590 T* pointer() { return &value_; }
1591
1592 private:
1593 T value_;
1594 ValueHolder(const ValueHolder&) = delete;
1595 ValueHolder& operator=(const ValueHolder&) = delete;
1596 };
1597
1598 T* GetOrCreateValue() const {
1599 return static_cast<ValueHolder*>(
1600 ThreadLocalRegistry::GetValueOnCurrentThread(this))
1601 ->pointer();
1602 }
1603
1604 ThreadLocalValueHolderBase* NewValueForCurrentThread() const override {
1605 return default_factory_->MakeNewHolder();
1606 }
1607
1608 class ValueHolderFactory {
1609 public:
1610 ValueHolderFactory() {}
1611 virtual ~ValueHolderFactory() {}
1612 virtual ValueHolder* MakeNewHolder() const = 0;
1613
1614 private:
1615 ValueHolderFactory(const ValueHolderFactory&) = delete;
1616 ValueHolderFactory& operator=(const ValueHolderFactory&) = delete;
1617 };
1618
1619 class DefaultValueHolderFactory : public ValueHolderFactory {
1620 public:
1621 DefaultValueHolderFactory() {}
1622 ValueHolder* MakeNewHolder() const override { return new ValueHolder(); }
1623
1624 private:
1625 DefaultValueHolderFactory(const DefaultValueHolderFactory&) = delete;
1626 DefaultValueHolderFactory& operator=(const DefaultValueHolderFactory&) =
1627 delete;
1628 };
1629
1630 class InstanceValueHolderFactory : public ValueHolderFactory {
1631 public:
1632 explicit InstanceValueHolderFactory(const T& value) : value_(value) {}
1633 ValueHolder* MakeNewHolder() const override {
1634 return new ValueHolder(value_);
1635 }
1636
1637 private:
1638 const T value_;
1639
1640 InstanceValueHolderFactory(const InstanceValueHolderFactory&) = delete;
1641 InstanceValueHolderFactory& operator=(const InstanceValueHolderFactory&) =
1642 delete;
1643 };
1644
1645 std::unique_ptr<ValueHolderFactory> default_factory_;
1646
1647 ThreadLocal(const ThreadLocal&) = delete;
1648 ThreadLocal& operator=(const ThreadLocal&) = delete;
1649 };
1650
1651 #elif GTEST_HAS_PTHREAD
1652
1653
1654 class MutexBase {
1655 public:
1656
1657 void Lock() {
1658 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_));
1659 owner_ = pthread_self();
1660 has_owner_ = true;
1661 }
1662
1663
1664 void Unlock() {
1665
1666
1667
1668
1669 has_owner_ = false;
1670 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_));
1671 }
1672
1673
1674
1675 void AssertHeld() const {
1676 GTEST_CHECK_(has_owner_ && pthread_equal(owner_, pthread_self()))
1677 << "The current thread is not holding the mutex @" << this;
1678 }
1679
1680
1681
1682
1683
1684
1685 public:
1686 pthread_mutex_t mutex_;
1687
1688
1689
1690
1691
1692
1693 bool has_owner_;
1694 pthread_t owner_;
1695 };
1696
1697
1698 #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1699 extern ::testing::internal::MutexBase mutex
1700
1701
1702
1703
1704
1705
1706
1707 #define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1708 ::testing::internal::MutexBase mutex = {PTHREAD_MUTEX_INITIALIZER, false, 0}
1709
1710
1711
1712 class Mutex : public MutexBase {
1713 public:
1714 Mutex() {
1715 GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, nullptr));
1716 has_owner_ = false;
1717 }
1718 ~Mutex() { GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_)); }
1719
1720 private:
1721 Mutex(const Mutex&) = delete;
1722 Mutex& operator=(const Mutex&) = delete;
1723 };
1724
1725
1726
1727
1728
1729
1730 class GTestMutexLock {
1731 public:
1732 explicit GTestMutexLock(MutexBase* mutex) : mutex_(mutex) { mutex_->Lock(); }
1733
1734 ~GTestMutexLock() { mutex_->Unlock(); }
1735
1736 private:
1737 MutexBase* const mutex_;
1738
1739 GTestMutexLock(const GTestMutexLock&) = delete;
1740 GTestMutexLock& operator=(const GTestMutexLock&) = delete;
1741 };
1742
1743 typedef GTestMutexLock MutexLock;
1744
1745
1746
1747
1748
1749
1750
1751 class GTEST_API_ ThreadLocalValueHolderBase {
1752 public:
1753 virtual ~ThreadLocalValueHolderBase() = default;
1754 };
1755
1756
1757
1758 extern "C" inline void DeleteThreadLocalValue(void* value_holder) {
1759 delete static_cast<ThreadLocalValueHolderBase*>(value_holder);
1760 }
1761
1762
1763 template <typename T>
1764 class GTEST_API_ ThreadLocal {
1765 public:
1766 ThreadLocal()
1767 : key_(CreateKey()), default_factory_(new DefaultValueHolderFactory()) {}
1768 explicit ThreadLocal(const T& value)
1769 : key_(CreateKey()),
1770 default_factory_(new InstanceValueHolderFactory(value)) {}
1771
1772 ~ThreadLocal() {
1773
1774 DeleteThreadLocalValue(pthread_getspecific(key_));
1775
1776
1777
1778 GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_));
1779 }
1780
1781 T* pointer() { return GetOrCreateValue(); }
1782 const T* pointer() const { return GetOrCreateValue(); }
1783 const T& get() const { return *pointer(); }
1784 void set(const T& value) { *pointer() = value; }
1785
1786 private:
1787
1788 class ValueHolder : public ThreadLocalValueHolderBase {
1789 public:
1790 ValueHolder() : value_() {}
1791 explicit ValueHolder(const T& value) : value_(value) {}
1792
1793 T* pointer() { return &value_; }
1794
1795 private:
1796 T value_;
1797 ValueHolder(const ValueHolder&) = delete;
1798 ValueHolder& operator=(const ValueHolder&) = delete;
1799 };
1800
1801 static pthread_key_t CreateKey() {
1802 pthread_key_t key;
1803
1804
1805 GTEST_CHECK_POSIX_SUCCESS_(
1806 pthread_key_create(&key, &DeleteThreadLocalValue));
1807 return key;
1808 }
1809
1810 T* GetOrCreateValue() const {
1811 ThreadLocalValueHolderBase* const holder =
1812 static_cast<ThreadLocalValueHolderBase*>(pthread_getspecific(key_));
1813 if (holder != nullptr) {
1814 return CheckedDowncastToActualType<ValueHolder>(holder)->pointer();
1815 }
1816
1817 ValueHolder* const new_holder = default_factory_->MakeNewHolder();
1818 ThreadLocalValueHolderBase* const holder_base = new_holder;
1819 GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base));
1820 return new_holder->pointer();
1821 }
1822
1823 class ValueHolderFactory {
1824 public:
1825 ValueHolderFactory() = default;
1826 virtual ~ValueHolderFactory() = default;
1827 virtual ValueHolder* MakeNewHolder() const = 0;
1828
1829 private:
1830 ValueHolderFactory(const ValueHolderFactory&) = delete;
1831 ValueHolderFactory& operator=(const ValueHolderFactory&) = delete;
1832 };
1833
1834 class DefaultValueHolderFactory : public ValueHolderFactory {
1835 public:
1836 DefaultValueHolderFactory() = default;
1837 ValueHolder* MakeNewHolder() const override { return new ValueHolder(); }
1838
1839 private:
1840 DefaultValueHolderFactory(const DefaultValueHolderFactory&) = delete;
1841 DefaultValueHolderFactory& operator=(const DefaultValueHolderFactory&) =
1842 delete;
1843 };
1844
1845 class InstanceValueHolderFactory : public ValueHolderFactory {
1846 public:
1847 explicit InstanceValueHolderFactory(const T& value) : value_(value) {}
1848 ValueHolder* MakeNewHolder() const override {
1849 return new ValueHolder(value_);
1850 }
1851
1852 private:
1853 const T value_;
1854
1855 InstanceValueHolderFactory(const InstanceValueHolderFactory&) = delete;
1856 InstanceValueHolderFactory& operator=(const InstanceValueHolderFactory&) =
1857 delete;
1858 };
1859
1860
1861 const pthread_key_t key_;
1862 std::unique_ptr<ValueHolderFactory> default_factory_;
1863
1864 ThreadLocal(const ThreadLocal&) = delete;
1865 ThreadLocal& operator=(const ThreadLocal&) = delete;
1866 };
1867
1868 #endif
1869
1870 #else
1871
1872
1873
1874
1875
1876
1877 class Mutex {
1878 public:
1879 Mutex() {}
1880 void Lock() {}
1881 void Unlock() {}
1882 void AssertHeld() const {}
1883 };
1884
1885 #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
1886 extern ::testing::internal::Mutex mutex
1887
1888 #define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex
1889
1890
1891
1892
1893
1894
1895 class GTestMutexLock {
1896 public:
1897 explicit GTestMutexLock(Mutex*) {}
1898 };
1899
1900 typedef GTestMutexLock MutexLock;
1901
1902 template <typename T>
1903 class GTEST_API_ ThreadLocal {
1904 public:
1905 ThreadLocal() : value_() {}
1906 explicit ThreadLocal(const T& value) : value_(value) {}
1907 T* pointer() { return &value_; }
1908 const T* pointer() const { return &value_; }
1909 const T& get() const { return value_; }
1910 void set(const T& value) { value_ = value; }
1911
1912 private:
1913 T value_;
1914 };
1915
1916 #endif
1917
1918
1919
1920 GTEST_API_ size_t GetThreadCount();
1921
1922 #ifdef GTEST_OS_WINDOWS
1923 #define GTEST_PATH_SEP_ "\\"
1924 #define GTEST_HAS_ALT_PATH_SEP_ 1
1925 #else
1926 #define GTEST_PATH_SEP_ "/"
1927 #define GTEST_HAS_ALT_PATH_SEP_ 0
1928 #endif
1929
1930
1931
1932
1933
1934
1935
1936
1937 inline bool IsAlpha(char ch) {
1938 return isalpha(static_cast<unsigned char>(ch)) != 0;
1939 }
1940 inline bool IsAlNum(char ch) {
1941 return isalnum(static_cast<unsigned char>(ch)) != 0;
1942 }
1943 inline bool IsDigit(char ch) {
1944 return isdigit(static_cast<unsigned char>(ch)) != 0;
1945 }
1946 inline bool IsLower(char ch) {
1947 return islower(static_cast<unsigned char>(ch)) != 0;
1948 }
1949 inline bool IsSpace(char ch) {
1950 return isspace(static_cast<unsigned char>(ch)) != 0;
1951 }
1952 inline bool IsUpper(char ch) {
1953 return isupper(static_cast<unsigned char>(ch)) != 0;
1954 }
1955 inline bool IsXDigit(char ch) {
1956 return isxdigit(static_cast<unsigned char>(ch)) != 0;
1957 }
1958 #ifdef __cpp_lib_char8_t
1959 inline bool IsXDigit(char8_t ch) {
1960 return isxdigit(static_cast<unsigned char>(ch)) != 0;
1961 }
1962 #endif
1963 inline bool IsXDigit(char16_t ch) {
1964 const unsigned char low_byte = static_cast<unsigned char>(ch);
1965 return ch == low_byte && isxdigit(low_byte) != 0;
1966 }
1967 inline bool IsXDigit(char32_t ch) {
1968 const unsigned char low_byte = static_cast<unsigned char>(ch);
1969 return ch == low_byte && isxdigit(low_byte) != 0;
1970 }
1971 inline bool IsXDigit(wchar_t ch) {
1972 const unsigned char low_byte = static_cast<unsigned char>(ch);
1973 return ch == low_byte && isxdigit(low_byte) != 0;
1974 }
1975
1976 inline char ToLower(char ch) {
1977 return static_cast<char>(tolower(static_cast<unsigned char>(ch)));
1978 }
1979 inline char ToUpper(char ch) {
1980 return static_cast<char>(toupper(static_cast<unsigned char>(ch)));
1981 }
1982
1983 inline std::string StripTrailingSpaces(std::string str) {
1984 std::string::iterator it = str.end();
1985 while (it != str.begin() && IsSpace(*--it)) it = str.erase(it);
1986 return str;
1987 }
1988
1989
1990
1991
1992
1993
1994
1995 namespace posix {
1996
1997
1998
1999
2000
2001 #ifdef GTEST_OS_WINDOWS
2002
2003 typedef struct _stat StatStruct;
2004
2005 #ifdef GTEST_OS_WINDOWS_MOBILE
2006 inline int FileNo(FILE* file) { return reinterpret_cast<int>(_fileno(file)); }
2007
2008
2009 #else
2010 inline int FileNo(FILE* file) { return _fileno(file); }
2011 #if GTEST_HAS_FILE_SYSTEM
2012 inline int Stat(const char* path, StatStruct* buf) { return _stat(path, buf); }
2013 inline int RmDir(const char* dir) { return _rmdir(dir); }
2014 inline bool IsDir(const StatStruct& st) { return (_S_IFDIR & st.st_mode) != 0; }
2015 #endif
2016 #endif
2017
2018 #elif defined(GTEST_OS_ESP8266)
2019 typedef struct stat StatStruct;
2020
2021 inline int FileNo(FILE* file) { return fileno(file); }
2022 #if GTEST_HAS_FILE_SYSTEM
2023 inline int Stat(const char* path, StatStruct* buf) {
2024
2025 return 0;
2026 }
2027 inline int RmDir(const char* dir) { return rmdir(dir); }
2028 inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
2029 #endif
2030
2031 #else
2032
2033 typedef struct stat StatStruct;
2034
2035 inline int FileNo(FILE* file) { return fileno(file); }
2036 #if GTEST_HAS_FILE_SYSTEM
2037 inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
2038 #ifdef GTEST_OS_QURT
2039
2040 inline int RmDir(const char*) { return 0; }
2041 #else
2042 inline int RmDir(const char* dir) { return rmdir(dir); }
2043 #endif
2044 inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
2045 #endif
2046
2047 #endif
2048
2049
2050
2051 #ifdef GTEST_OS_WINDOWS
2052
2053 #ifdef __BORLANDC__
2054 inline int DoIsATTY(int fd) { return isatty(fd); }
2055 inline int StrCaseCmp(const char* s1, const char* s2) {
2056 return stricmp(s1, s2);
2057 }
2058 #else
2059 #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_ZOS) || \
2060 defined(GTEST_OS_IOS) || defined(GTEST_OS_WINDOWS_PHONE) || \
2061 defined(GTEST_OS_WINDOWS_RT) || defined(ESP_PLATFORM)
2062 inline int DoIsATTY(int ) { return 0; }
2063 #else
2064 inline int DoIsATTY(int fd) { return _isatty(fd); }
2065 #endif
2066 inline int StrCaseCmp(const char* s1, const char* s2) {
2067 return _stricmp(s1, s2);
2068 }
2069 #endif
2070
2071 #else
2072
2073 inline int DoIsATTY(int fd) { return isatty(fd); }
2074 inline int StrCaseCmp(const char* s1, const char* s2) {
2075 return strcasecmp(s1, s2);
2076 }
2077
2078 #endif
2079
2080 inline int IsATTY(int fd) {
2081
2082
2083
2084 int savedErrno = errno;
2085 int isAttyValue = DoIsATTY(fd);
2086 errno = savedErrno;
2087
2088 return isAttyValue;
2089 }
2090
2091
2092
2093 GTEST_DISABLE_MSC_DEPRECATED_PUSH_()
2094
2095
2096
2097
2098 #if GTEST_HAS_FILE_SYSTEM
2099 #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_WINDOWS_PHONE) && \
2100 !defined(GTEST_OS_WINDOWS_RT) && !defined(GTEST_OS_WINDOWS_GAMES) && \
2101 !defined(GTEST_OS_ESP8266) && !defined(GTEST_OS_XTENSA) && \
2102 !defined(GTEST_OS_QURT)
2103 inline int ChDir(const char* dir) { return chdir(dir); }
2104 #endif
2105 inline FILE* FOpen(const char* path, const char* mode) {
2106 #if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MINGW)
2107 struct wchar_codecvt : public std::codecvt<wchar_t, char, std::mbstate_t> {};
2108 std::wstring_convert<wchar_codecvt> converter;
2109 std::wstring wide_path = converter.from_bytes(path);
2110 std::wstring wide_mode = converter.from_bytes(mode);
2111 return _wfopen(wide_path.c_str(), wide_mode.c_str());
2112 #else
2113 return fopen(path, mode);
2114 #endif
2115 }
2116 #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT)
2117 inline FILE* FReopen(const char* path, const char* mode, FILE* stream) {
2118 return freopen(path, mode, stream);
2119 }
2120 inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
2121 #endif
2122 inline int FClose(FILE* fp) { return fclose(fp); }
2123 #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT)
2124 inline int Read(int fd, void* buf, unsigned int count) {
2125 return static_cast<int>(read(fd, buf, count));
2126 }
2127 inline int Write(int fd, const void* buf, unsigned int count) {
2128 return static_cast<int>(write(fd, buf, count));
2129 }
2130 inline int Close(int fd) { return close(fd); }
2131 #endif
2132 #endif
2133
2134 #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT)
2135 inline const char* StrError(int errnum) { return strerror(errnum); }
2136 #endif
2137
2138 inline const char* GetEnv(const char* name) {
2139 #if defined(GTEST_OS_WINDOWS_MOBILE) || defined(GTEST_OS_WINDOWS_PHONE) || \
2140 defined(GTEST_OS_ESP8266) || defined(GTEST_OS_XTENSA) || \
2141 defined(GTEST_OS_QURT)
2142
2143 static_cast<void>(name);
2144 return nullptr;
2145 #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
2146
2147
2148 const char* const env = getenv(name);
2149 return (env != nullptr && env[0] != '\0') ? env : nullptr;
2150 #else
2151 return getenv(name);
2152 #endif
2153 }
2154
2155 GTEST_DISABLE_MSC_DEPRECATED_POP_()
2156
2157 #ifdef GTEST_OS_WINDOWS_MOBILE
2158
2159
2160
2161 [[noreturn]] void Abort();
2162 #else
2163 [[noreturn]] inline void Abort() { abort(); }
2164 #endif
2165
2166 }
2167
2168
2169
2170
2171
2172
2173 #if defined(_MSC_VER) && !defined(GTEST_OS_WINDOWS_MOBILE)
2174
2175 #define GTEST_SNPRINTF_(buffer, size, format, ...) \
2176 _snprintf_s(buffer, size, size, format, __VA_ARGS__)
2177 #elif defined(_MSC_VER)
2178
2179 #define GTEST_SNPRINTF_ _snprintf
2180 #else
2181 #define GTEST_SNPRINTF_ snprintf
2182 #endif
2183
2184
2185
2186
2187 using BiggestInt = long long;
2188
2189
2190 constexpr BiggestInt kMaxBiggestInt = (std::numeric_limits<BiggestInt>::max)();
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210 template <size_t size>
2211 class TypeWithSize {
2212 public:
2213
2214
2215 using UInt = void;
2216 };
2217
2218
2219 template <>
2220 class TypeWithSize<4> {
2221 public:
2222 using Int = std::int32_t;
2223 using UInt = std::uint32_t;
2224 };
2225
2226
2227 template <>
2228 class TypeWithSize<8> {
2229 public:
2230 using Int = std::int64_t;
2231 using UInt = std::uint64_t;
2232 };
2233
2234
2235 using TimeInMillis = int64_t;
2236
2237
2238
2239
2240 #if !defined(GTEST_FLAG)
2241 #define GTEST_FLAG_NAME_(name) gtest_##name
2242 #define GTEST_FLAG(name) FLAGS_gtest_##name
2243 #endif
2244
2245
2246 #ifdef GTEST_INTERNAL_HAS_ABSL_FLAGS
2247
2248
2249 #define GTEST_DEFINE_bool_(name, default_val, doc) \
2250 ABSL_FLAG(bool, GTEST_FLAG_NAME_(name), default_val, doc)
2251 #define GTEST_DEFINE_int32_(name, default_val, doc) \
2252 ABSL_FLAG(int32_t, GTEST_FLAG_NAME_(name), default_val, doc)
2253 #define GTEST_DEFINE_string_(name, default_val, doc) \
2254 ABSL_FLAG(std::string, GTEST_FLAG_NAME_(name), default_val, doc)
2255
2256
2257 #define GTEST_DECLARE_bool_(name) \
2258 ABSL_DECLARE_FLAG(bool, GTEST_FLAG_NAME_(name))
2259 #define GTEST_DECLARE_int32_(name) \
2260 ABSL_DECLARE_FLAG(int32_t, GTEST_FLAG_NAME_(name))
2261 #define GTEST_DECLARE_string_(name) \
2262 ABSL_DECLARE_FLAG(std::string, GTEST_FLAG_NAME_(name))
2263
2264 #define GTEST_FLAG_SAVER_ ::absl::FlagSaver
2265
2266 #define GTEST_FLAG_GET(name) ::absl::GetFlag(GTEST_FLAG(name))
2267 #define GTEST_FLAG_SET(name, value) \
2268 (void)(::absl::SetFlag(>EST_FLAG(name), value))
2269 #define GTEST_USE_OWN_FLAGFILE_FLAG_ 0
2270
2271 #undef GTEST_INTERNAL_HAS_ABSL_FLAGS
2272 #else
2273
2274
2275 #define GTEST_DEFINE_bool_(name, default_val, doc) \
2276 namespace testing { \
2277 GTEST_API_ bool GTEST_FLAG(name) = (default_val); \
2278 } \
2279 static_assert(true, "no-op to require trailing semicolon")
2280 #define GTEST_DEFINE_int32_(name, default_val, doc) \
2281 namespace testing { \
2282 GTEST_API_ std::int32_t GTEST_FLAG(name) = (default_val); \
2283 } \
2284 static_assert(true, "no-op to require trailing semicolon")
2285 #define GTEST_DEFINE_string_(name, default_val, doc) \
2286 namespace testing { \
2287 GTEST_API_ ::std::string GTEST_FLAG(name) = (default_val); \
2288 } \
2289 static_assert(true, "no-op to require trailing semicolon")
2290
2291
2292 #define GTEST_DECLARE_bool_(name) \
2293 namespace testing { \
2294 GTEST_API_ extern bool GTEST_FLAG(name); \
2295 } \
2296 static_assert(true, "no-op to require trailing semicolon")
2297 #define GTEST_DECLARE_int32_(name) \
2298 namespace testing { \
2299 GTEST_API_ extern std::int32_t GTEST_FLAG(name); \
2300 } \
2301 static_assert(true, "no-op to require trailing semicolon")
2302 #define GTEST_DECLARE_string_(name) \
2303 namespace testing { \
2304 GTEST_API_ extern ::std::string GTEST_FLAG(name); \
2305 } \
2306 static_assert(true, "no-op to require trailing semicolon")
2307
2308 #define GTEST_FLAG_SAVER_ ::testing::internal::GTestFlagSaver
2309
2310 #define GTEST_FLAG_GET(name) ::testing::GTEST_FLAG(name)
2311 #define GTEST_FLAG_SET(name, value) (void)(::testing::GTEST_FLAG(name) = value)
2312 #define GTEST_USE_OWN_FLAGFILE_FLAG_ 1
2313
2314 #endif
2315
2316
2317 #if !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)
2318 #define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)
2319 #define GTEST_LOCK_EXCLUDED_(locks)
2320 #endif
2321
2322
2323
2324
2325 GTEST_API_ bool ParseInt32(const Message& src_text, const char* str,
2326 int32_t* value);
2327
2328
2329
2330 bool BoolFromGTestEnv(const char* flag, bool default_val);
2331 GTEST_API_ int32_t Int32FromGTestEnv(const char* flag, int32_t default_val);
2332 std::string OutputFlagAlsoCheckEnvVar();
2333 const char* StringFromGTestEnv(const char* flag, const char* default_val);
2334
2335 }
2336 }
2337
2338 #ifdef GTEST_HAS_ABSL
2339
2340
2341 #define GTEST_INTERNAL_HAS_ANY 1
2342 #include "absl/types/any.h"
2343 namespace testing {
2344 namespace internal {
2345 using Any = ::absl::any;
2346 }
2347 }
2348 #else
2349 #if defined(__cpp_lib_any) || (GTEST_INTERNAL_HAS_INCLUDE(<any>) && \
2350 GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L && \
2351 (!defined(_MSC_VER) || GTEST_HAS_RTTI))
2352
2353
2354 #define GTEST_INTERNAL_HAS_ANY 1
2355 #include <any>
2356 namespace testing {
2357 namespace internal {
2358 using Any = ::std::any;
2359 }
2360 }
2361
2362
2363 #endif
2364 #endif
2365
2366 #ifndef GTEST_INTERNAL_HAS_ANY
2367 #define GTEST_INTERNAL_HAS_ANY 0
2368 #endif
2369
2370 #ifdef GTEST_HAS_ABSL
2371
2372
2373 #define GTEST_INTERNAL_HAS_OPTIONAL 1
2374 #include "absl/types/optional.h"
2375 namespace testing {
2376 namespace internal {
2377 template <typename T>
2378 using Optional = ::absl::optional<T>;
2379 inline ::absl::nullopt_t Nullopt() { return ::absl::nullopt; }
2380 }
2381 }
2382 #else
2383 #if defined(__cpp_lib_optional) || (GTEST_INTERNAL_HAS_INCLUDE(<optional>) && \
2384 GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L)
2385
2386
2387 #define GTEST_INTERNAL_HAS_OPTIONAL 1
2388 #include <optional>
2389 namespace testing {
2390 namespace internal {
2391 template <typename T>
2392 using Optional = ::std::optional<T>;
2393 inline ::std::nullopt_t Nullopt() { return ::std::nullopt; }
2394 }
2395 }
2396
2397
2398 #endif
2399 #endif
2400
2401 #ifndef GTEST_INTERNAL_HAS_OPTIONAL
2402 #define GTEST_INTERNAL_HAS_OPTIONAL 0
2403 #endif
2404
2405 #if defined(__cpp_lib_span) || (GTEST_INTERNAL_HAS_INCLUDE(<span>) && \
2406 GTEST_INTERNAL_CPLUSPLUS_LANG >= 202002L)
2407 #define GTEST_INTERNAL_HAS_STD_SPAN 1
2408 #endif
2409
2410 #ifndef GTEST_INTERNAL_HAS_STD_SPAN
2411 #define GTEST_INTERNAL_HAS_STD_SPAN 0
2412 #endif
2413
2414 #ifdef GTEST_HAS_ABSL
2415
2416
2417 #define GTEST_INTERNAL_HAS_STRING_VIEW 1
2418 #include "absl/strings/string_view.h"
2419 namespace testing {
2420 namespace internal {
2421 using StringView = ::absl::string_view;
2422 }
2423 }
2424 #else
2425 #if defined(__cpp_lib_string_view) || \
2426 (GTEST_INTERNAL_HAS_INCLUDE(<string_view>) && \
2427 GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L)
2428
2429
2430 #define GTEST_INTERNAL_HAS_STRING_VIEW 1
2431 #include <string_view>
2432 namespace testing {
2433 namespace internal {
2434 using StringView = ::std::string_view;
2435 }
2436 }
2437
2438
2439 #endif
2440 #endif
2441
2442 #ifndef GTEST_INTERNAL_HAS_STRING_VIEW
2443 #define GTEST_INTERNAL_HAS_STRING_VIEW 0
2444 #endif
2445
2446 #ifdef GTEST_HAS_ABSL
2447
2448
2449 #define GTEST_INTERNAL_HAS_VARIANT 1
2450 #include "absl/types/variant.h"
2451 namespace testing {
2452 namespace internal {
2453 template <typename... T>
2454 using Variant = ::absl::variant<T...>;
2455 }
2456 }
2457 #else
2458 #if defined(__cpp_lib_variant) || (GTEST_INTERNAL_HAS_INCLUDE(<variant>) && \
2459 GTEST_INTERNAL_CPLUSPLUS_LANG >= 201703L)
2460
2461
2462 #define GTEST_INTERNAL_HAS_VARIANT 1
2463 #include <variant>
2464 namespace testing {
2465 namespace internal {
2466 template <typename... T>
2467 using Variant = ::std::variant<T...>;
2468 }
2469 }
2470
2471 #endif
2472 #endif
2473
2474 #ifndef GTEST_INTERNAL_HAS_VARIANT
2475 #define GTEST_INTERNAL_HAS_VARIANT 0
2476 #endif
2477
2478 #if (defined(__cpp_lib_three_way_comparison) || \
2479 (GTEST_INTERNAL_HAS_INCLUDE(<compare>) && \
2480 GTEST_INTERNAL_CPLUSPLUS_LANG >= 201907L))
2481 #define GTEST_INTERNAL_HAS_COMPARE_LIB 1
2482 #else
2483 #define GTEST_INTERNAL_HAS_COMPARE_LIB 0
2484 #endif
2485
2486 #endif