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