From 4b83461e9772cce62e84310060fe84172e9bf4ba Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Thu, 29 Jan 2009 06:49:00 +0000 Subject: [PATCH] Fixes some warnings when compiled with MSVC at warning level 4. --- include/gtest/gtest.h | 16 +++++++++++++--- include/gtest/internal/gtest-internal.h | 5 ++++- src/gtest-internal-inl.h | 4 ++++ src/gtest.cc | 22 +++++++++------------- test/gtest-filepath_test.cc | 2 +- test/gtest_unittest.cc | 12 ++++++------ 6 files changed, 37 insertions(+), 24 deletions(-) diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index dfa338b2..f1184dee 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -614,10 +614,20 @@ AssertionResult CmpHelperEQ(const char* expected_expression, const char* actual_expression, const T1& expected, const T2& actual) { +#ifdef _MSC_VER +#pragma warning(push) // Saves the current warning state. +#pragma warning(disable:4389) // Temporarily disables warning on + // signed/unsigned mismatch. +#endif + if (expected == actual) { return AssertionSuccess(); } +#ifdef _MSC_VER +#pragma warning(pop) // Restores the warning state. +#endif + return EqFailure(expected_expression, actual_expression, FormatForComparisonFailureMessage(expected, actual), @@ -688,7 +698,7 @@ class EqHelper { template static AssertionResult Compare(const char* expected_expression, const char* actual_expression, - const T1& expected, + const T1& /* expected */, T2* actual) { // We already know that 'expected' is a null pointer. return CmpHelperEQ(expected_expression, actual_expression, @@ -1315,7 +1325,7 @@ bool StaticAssertTypeEq() { // value, as it always calls GetTypeId<>() from the Google Test // framework. #define TEST(test_case_name, test_name)\ - GTEST_TEST_(test_case_name, test_name,\ + GTEST_TEST_(test_case_name, test_name, \ ::testing::Test, ::testing::internal::GetTestTypeId()) @@ -1346,7 +1356,7 @@ bool StaticAssertTypeEq() { // } #define TEST_F(test_fixture, test_name)\ - GTEST_TEST_(test_fixture, test_name, test_fixture,\ + GTEST_TEST_(test_fixture, test_name, test_fixture, \ ::testing::internal::GetTypeId()) // Use this macro in main() to run all tests. It returns 0 if all diff --git a/include/gtest/internal/gtest-internal.h b/include/gtest/internal/gtest-internal.h index 37faaaeb..541c89b8 100644 --- a/include/gtest/internal/gtest-internal.h +++ b/include/gtest/internal/gtest-internal.h @@ -748,6 +748,9 @@ String GetCurrentOsStackTraceExceptTop(UnitTest* unit_test, int skip_count); // Returns the number of failed test parts in the given test result object. int GetFailedPartCount(const TestResult* result); +// A helper for suppressing warnings on unreachable code in some macros. +inline bool True() { return true; } + } // namespace internal } // namespace testing @@ -835,7 +838,7 @@ int GetFailedPartCount(const TestResult* result); GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ if (const char* gtest_msg = "") { \ ::testing::internal::HasNewFatalFailureHelper gtest_fatal_failure_checker; \ - { statement; } \ + if (::testing::internal::True()) { statement; } \ if (gtest_fatal_failure_checker.has_new_fatal_failure()) { \ gtest_msg = "Expected: " #statement " doesn't generate new fatal " \ "failures in the current thread.\n" \ diff --git a/src/gtest-internal-inl.h b/src/gtest-internal-inl.h index caa0877c..ef3fe98d 100644 --- a/src/gtest-internal-inl.h +++ b/src/gtest-internal-inl.h @@ -901,6 +901,8 @@ class DefaultGlobalTestPartResultReporter private: UnitTestImpl* const unit_test_; + + GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultGlobalTestPartResultReporter); }; // This is the default per thread test part result reporter used in @@ -915,6 +917,8 @@ class DefaultPerThreadTestPartResultReporter private: UnitTestImpl* const unit_test_; + + GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultPerThreadTestPartResultReporter); }; // The private implementation of the UnitTest class. We don't protect diff --git a/src/gtest.cc b/src/gtest.cc index 903dcd94..26077673 100644 --- a/src/gtest.cc +++ b/src/gtest.cc @@ -374,7 +374,7 @@ bool UnitTestOptions::PatternMatchesString(const char *pattern, bool UnitTestOptions::MatchesFilter(const String& name, const char* filter) { const char *cur_pattern = filter; - while (true) { + for (;;) { if (PatternMatchesString(cur_pattern, name.c_str())) { return true; } @@ -1458,23 +1458,19 @@ char* CodePointToUtf8(UInt32 code_point, char* str) { // and thus should be combined into a single Unicode code point // using CreateCodePointFromUtf16SurrogatePair. inline bool IsUtf16SurrogatePair(wchar_t first, wchar_t second) { - if (sizeof(wchar_t) == 2) - return (first & 0xFC00) == 0xD800 && (second & 0xFC00) == 0xDC00; - else - return false; + return sizeof(wchar_t) == 2 && + (first & 0xFC00) == 0xD800 && (second & 0xFC00) == 0xDC00; } // Creates a Unicode code point from UTF16 surrogate pair. inline UInt32 CreateCodePointFromUtf16SurrogatePair(wchar_t first, wchar_t second) { - if (sizeof(wchar_t) == 2) { - const UInt32 mask = (1 << 10) - 1; - return (((first & mask) << 10) | (second & mask)) + 0x10000; - } else { - // This should not be called, but we provide a sensible default - // in case it is. - return static_cast(first); - } + const UInt32 mask = (1 << 10) - 1; + return (sizeof(wchar_t) == 2) ? + (((first & mask) << 10) | (second & mask)) + 0x10000 : + // This function should not be called when the condition is + // false, but we provide a sensible default in case it is. + static_cast(first); } // Converts a wide string to a narrow string in UTF-8 encoding. diff --git a/test/gtest-filepath_test.cc b/test/gtest-filepath_test.cc index 589442fe..ee80f0d9 100644 --- a/test/gtest-filepath_test.cc +++ b/test/gtest-filepath_test.cc @@ -311,7 +311,7 @@ TEST(RemoveTrailingPathSeparatorTest, ShouldReturnUnmodified) { TEST(DirectoryTest, RootDirectoryExists) { #ifdef GTEST_OS_WINDOWS // We are on Windows. char current_drive[_MAX_PATH]; // NOLINT - current_drive[0] = _getdrive() + 'A' - 1; + current_drive[0] = static_cast(_getdrive() + 'A' - 1); current_drive[1] = ':'; current_drive[2] = '\\'; current_drive[3] = '\0'; diff --git a/test/gtest_unittest.cc b/test/gtest_unittest.cc index 135493f6..cf9163be 100644 --- a/test/gtest_unittest.cc +++ b/test/gtest_unittest.cc @@ -1090,7 +1090,7 @@ TEST(TestResultPropertyTest, OverridesValuesForDuplicateKeys) { // property is not recorded. void ExpectNonFatalFailureRecordingPropertyWithReservedKey(const char* key) { TestResult test_result; - TestProperty property("name", "1"); + TestProperty property(key, "1"); EXPECT_NONFATAL_FAILURE(test_result.RecordProperty(property), "Reserved key"); ASSERT_TRUE(test_result.test_properties().IsEmpty()) << "Not recorded"; } @@ -1594,31 +1594,31 @@ TEST(PredicateAssertionTest, AcceptsTemplateFunction) { // Some helper functions for testing using overloaded/template // functions with ASSERT_PRED_FORMATn and EXPECT_PRED_FORMATn. -AssertionResult IsPositiveFormat(const char* expr, int n) { +AssertionResult IsPositiveFormat(const char* /* expr */, int n) { return n > 0 ? AssertionSuccess() : AssertionFailure(Message() << "Failure"); } -AssertionResult IsPositiveFormat(const char* expr, double x) { +AssertionResult IsPositiveFormat(const char* /* expr */, double x) { return x > 0 ? AssertionSuccess() : AssertionFailure(Message() << "Failure"); } template -AssertionResult IsNegativeFormat(const char* expr, T x) { +AssertionResult IsNegativeFormat(const char* /* expr */, T x) { return x < 0 ? AssertionSuccess() : AssertionFailure(Message() << "Failure"); } template -AssertionResult EqualsFormat(const char* expr1, const char* expr2, +AssertionResult EqualsFormat(const char* /* expr1 */, const char* /* expr2 */, const T1& x1, const T2& x2) { return x1 == x2 ? AssertionSuccess() : AssertionFailure(Message() << "Failure"); } // Tests that overloaded functions can be used in *_PRED_FORMAT* -// without explictly specifying their types. +// without explicitly specifying their types. TEST(PredicateFormatAssertionTest, AcceptsOverloadedFunction) { EXPECT_PRED_FORMAT1(IsPositiveFormat, 5); ASSERT_PRED_FORMAT1(IsPositiveFormat, 6.0);