diff --git a/googlemock/include/gmock/gmock-actions.h b/googlemock/include/gmock/gmock-actions.h index 46e426265..4b2c9cd55 100644 --- a/googlemock/include/gmock/gmock-actions.h +++ b/googlemock/include/gmock/gmock-actions.h @@ -146,6 +146,7 @@ #include "gmock/internal/gmock-internal-utils.h" #include "gmock/internal/gmock-port.h" #include "gmock/internal/gmock-pp.h" +#include "gtest/gtest.h" GTEST_DISABLE_MSC_WARNINGS_PUSH_(4100) @@ -249,7 +250,7 @@ GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(char, '\0'); // There's also no need for a default action for unsigned wchar_t, as // that type is the same as unsigned int for gcc, and invalid for // MSVC. -#if GMOCK_WCHAR_T_IS_NATIVE_ +#if GTEST_HAS_NATIVE_WCHAR GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(wchar_t, 0U); // NOLINT #endif diff --git a/googlemock/include/gmock/internal/gmock-internal-utils.h b/googlemock/include/gmock/internal/gmock-internal-utils.h index 2982c244e..c9df15eae 100644 --- a/googlemock/include/gmock/internal/gmock-internal-utils.h +++ b/googlemock/include/gmock/internal/gmock-internal-utils.h @@ -111,16 +111,6 @@ inline Element* GetRawPointer(Element* p) { #define GMOCK_INTERNAL_WARNING_POP() _Pragma("clang diagnostic pop") #endif -// MSVC treats wchar_t as a native type usually, but treats it as the -// same as unsigned short when the compiler option /Zc:wchar_t- is -// specified. It defines _NATIVE_WCHAR_T_DEFINED symbol when wchar_t -// is a native type. -#if defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED) -// wchar_t is a typedef. -#else -#define GMOCK_WCHAR_T_IS_NATIVE_ 1 -#endif - // In what follows, we use the term "kind" to indicate whether a type // is bool, an integer type (excluding bool), a floating-point type, // or none of them. This categorization is useful for determining @@ -156,7 +146,7 @@ GMOCK_DECLARE_KIND_(unsigned long, kInteger); // NOLINT GMOCK_DECLARE_KIND_(long long, kInteger); // NOLINT GMOCK_DECLARE_KIND_(unsigned long long, kInteger); // NOLINT -#if GMOCK_WCHAR_T_IS_NATIVE_ +#if GTEST_HAS_NATIVE_WCHAR GMOCK_DECLARE_KIND_(wchar_t, kInteger); #endif diff --git a/googlemock/test/gmock-actions_test.cc b/googlemock/test/gmock-actions_test.cc index 1b824884d..a370e570c 100644 --- a/googlemock/test/gmock-actions_test.cc +++ b/googlemock/test/gmock-actions_test.cc @@ -270,7 +270,7 @@ TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) { EXPECT_EQ(0U, BuiltInDefaultValue::Get()); EXPECT_EQ(0, BuiltInDefaultValue::Get()); EXPECT_EQ(0, BuiltInDefaultValue::Get()); -#if GMOCK_WCHAR_T_IS_NATIVE_ +#if GTEST_HAS_NATIVE_WCHAR #if !defined(__WCHAR_UNSIGNED__) EXPECT_EQ(0, BuiltInDefaultValue::Get()); #else @@ -299,7 +299,7 @@ TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) { EXPECT_TRUE(BuiltInDefaultValue::Exists()); EXPECT_TRUE(BuiltInDefaultValue::Exists()); EXPECT_TRUE(BuiltInDefaultValue::Exists()); -#if GMOCK_WCHAR_T_IS_NATIVE_ +#if GTEST_HAS_NATIVE_WCHAR EXPECT_TRUE(BuiltInDefaultValue::Exists()); #endif EXPECT_TRUE(BuiltInDefaultValue::Exists()); // NOLINT diff --git a/googletest/include/gtest/gtest-printers.h b/googletest/include/gtest/gtest-printers.h index 69c9fec3c..64b605290 100644 --- a/googletest/include/gtest/gtest-printers.h +++ b/googletest/include/gtest/gtest-printers.h @@ -674,12 +674,7 @@ inline void PrintTo(char32_t* s, ::std::ostream* os) { PrintTo(ImplicitCast_(s), os); } -// MSVC can be configured to define wchar_t as a typedef of unsigned -// short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native -// type. When wchar_t is a typedef, defining an overload for const -// wchar_t* would cause unsigned short* be printed as a wide string, -// possibly causing invalid memory accesses. -#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) +#if GTEST_HAS_NATIVE_WCHAR // Overloads for wide C strings GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os); inline void PrintTo(wchar_t* s, ::std::ostream* os) { diff --git a/googletest/include/gtest/gtest.h b/googletest/include/gtest/gtest.h index 089b10e9f..d20d9b334 100644 --- a/googletest/include/gtest/gtest.h +++ b/googletest/include/gtest/gtest.h @@ -71,7 +71,7 @@ #include "gtest/gtest-typed-test.h" // IWYU pragma: export #include "gtest/gtest_pred_impl.h" // IWYU pragma: export #include "gtest/gtest_prod.h" // IWYU pragma: export -#include "gtest/internal/gtest-internal.h" +#include "gtest/internal/gtest-internal.h" // IWYU pragma: export #include "gtest/internal/gtest-string.h" GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ diff --git a/googletest/include/gtest/internal/gtest-internal.h b/googletest/include/gtest/internal/gtest-internal.h index a630cc829..f845ffed2 100644 --- a/googletest/include/gtest/internal/gtest-internal.h +++ b/googletest/include/gtest/internal/gtest-internal.h @@ -39,7 +39,7 @@ #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ -#include "gtest/internal/gtest-port.h" +#include "gtest/internal/gtest-port.h" // IWYU pragma: export #ifdef GTEST_OS_LINUX #include diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index 051228553..f13ea49ee 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -500,6 +500,17 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION; #endif // defined(_MSC_VER) || defined(__BORLANDC__) #endif // GTEST_HAS_EXCEPTIONS +// MSVC either defines wchar_t as a typedef of unsigned short, or as a native +// type (in which case, it defines _NATIVE_WCHAR_T_DEFINED). When wchar_t is a +// typedef, defining an overload for const wchar_t* would cause unsigned short* +// be printed as a wide string, possibly causing invalid memory accesses, so we +// omit wchar_t overloads in that case. +#if defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED) +#define GTEST_HAS_NATIVE_WCHAR 0 +#else +#define GTEST_HAS_NATIVE_WCHAR 1 +#endif + // 1. Calculate default GTEST_HAS_STD_WSTRING values based on STL capabilities. #if defined(_MSVC_STL_VERSION) // Microsoft's STL implementation always supports ::std::wstring. diff --git a/googletest/src/gtest-printers.cc b/googletest/src/gtest-printers.cc index 975ebb829..8dceac8a8 100644 --- a/googletest/src/gtest-printers.cc +++ b/googletest/src/gtest-printers.cc @@ -468,16 +468,10 @@ void PrintTo(const char16_t* s, ostream* os) { PrintCStringTo(s, os); } void PrintTo(const char32_t* s, ostream* os) { PrintCStringTo(s, os); } -// MSVC compiler can be configured to define whar_t as a typedef -// of unsigned short. Defining an overload for const wchar_t* in that case -// would cause pointers to unsigned shorts be printed as wide strings, -// possibly accessing more memory than intended and causing invalid -// memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when -// wchar_t is implemented as a native type. -#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) +#if GTEST_HAS_NATIVE_WCHAR // Prints the given wide C string to the ostream. void PrintTo(const wchar_t* s, ostream* os) { PrintCStringTo(s, os); } -#endif // wchar_t is native +#endif // GTEST_HAS_NATIVE_WCHAR namespace { diff --git a/googletest/test/googletest-printers-test.cc b/googletest/test/googletest-printers-test.cc index c051ec709..679c8de52 100644 --- a/googletest/test/googletest-printers-test.cc +++ b/googletest/test/googletest-printers-test.cc @@ -648,13 +648,7 @@ TEST(PrintU32StringTest, EscapesProperly) { Print(p)); } -// MSVC compiler can be configured to define whar_t as a typedef -// of unsigned short. Defining an overload for const wchar_t* in that case -// would cause pointers to unsigned shorts be printed as wide strings, -// possibly accessing more memory than intended and causing invalid -// memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when -// wchar_t is implemented as a native type. -#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) +#if GTEST_HAS_NATIVE_WCHAR // const wchar_t*. TEST(PrintWideCStringTest, Const) { @@ -685,7 +679,7 @@ TEST(PrintWideCStringTest, EscapesProperly) { "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"", Print(static_cast(s))); } -#endif // native wchar_t +#endif // GTEST_HAS_NATIVE_WCHAR // Tests printing pointers to other char types.