Simplifies the implementation of GTEST_LOG_ & GTEST_LOG_; renames

GTEST_HIDE_UNREACHABLE_CODE_ to
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_ (by Vlad Losev).
This commit is contained in:
zhanyong.wan 2009-09-16 06:59:17 +00:00
parent b2ee82ebf9
commit 866f4a9446
6 changed files with 63 additions and 74 deletions

View File

@ -172,7 +172,7 @@ bool ExitedUnsuccessfully(int exit_status);
case ::testing::internal::DeathTest::EXECUTE_TEST: { \ case ::testing::internal::DeathTest::EXECUTE_TEST: { \
::testing::internal::DeathTest::ReturnSentinel \ ::testing::internal::DeathTest::ReturnSentinel \
gtest_sentinel(gtest_dt); \ gtest_sentinel(gtest_dt); \
GTEST_HIDE_UNREACHABLE_CODE_(statement); \ GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \ gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \
break; \ break; \
} \ } \
@ -253,17 +253,15 @@ InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag();
// statement unconditionally returns or throws. The Message constructor at // statement unconditionally returns or throws. The Message constructor at
// the end allows the syntax of streaming additional messages into the // the end allows the syntax of streaming additional messages into the
// macro, for compilational compatibility with EXPECT_DEATH/ASSERT_DEATH. // macro, for compilational compatibility with EXPECT_DEATH/ASSERT_DEATH.
// TODO(vladl@google.com): rename the GTEST_HIDE_UNREACHABLE_CODE_ macro to
// GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_.
#define GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, terminator) \ #define GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, terminator) \
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
if (::testing::internal::AlwaysTrue()) { \ if (::testing::internal::AlwaysTrue()) { \
GTEST_LOG_(WARNING, \ GTEST_LOG_(WARNING) \
"Death tests are not supported on this platform.\n" \ << "Death tests are not supported on this platform.\n" \
"Statement '" #statement "' cannot be verified."); \ << "Statement '" #statement "' cannot be verified."; \
} else if (!::testing::internal::AlwaysTrue()) { \ } else if (!::testing::internal::AlwaysTrue()) { \
::testing::internal::RE::PartialMatch(".*", (regex)); \ ::testing::internal::RE::PartialMatch(".*", (regex)); \
GTEST_HIDE_UNREACHABLE_CODE_(statement); \ GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
terminator; \ terminator; \
} else \ } else \
::testing::Message() ::testing::Message()

View File

@ -767,7 +767,7 @@ bool AlwaysTrue();
// Suppresses MSVC warnings 4072 (unreachable code) for the code following // Suppresses MSVC warnings 4072 (unreachable code) for the code following
// statement if it returns or throws (or doesn't return or throw in some // statement if it returns or throws (or doesn't return or throw in some
// situations). // situations).
#define GTEST_HIDE_UNREACHABLE_CODE_(statement) \ #define GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) \
if (::testing::internal::AlwaysTrue()) { statement; } if (::testing::internal::AlwaysTrue()) { statement; }
#define GTEST_TEST_THROW_(statement, expected_exception, fail) \ #define GTEST_TEST_THROW_(statement, expected_exception, fail) \
@ -775,7 +775,7 @@ bool AlwaysTrue();
if (const char* gtest_msg = "") { \ if (const char* gtest_msg = "") { \
bool gtest_caught_expected = false; \ bool gtest_caught_expected = false; \
try { \ try { \
GTEST_HIDE_UNREACHABLE_CODE_(statement); \ GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
} \ } \
catch (expected_exception const&) { \ catch (expected_exception const&) { \
gtest_caught_expected = true; \ gtest_caught_expected = true; \
@ -799,7 +799,7 @@ bool AlwaysTrue();
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
if (const char* gtest_msg = "") { \ if (const char* gtest_msg = "") { \
try { \ try { \
GTEST_HIDE_UNREACHABLE_CODE_(statement); \ GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
} \ } \
catch (...) { \ catch (...) { \
gtest_msg = "Expected: " #statement " doesn't throw an exception.\n" \ gtest_msg = "Expected: " #statement " doesn't throw an exception.\n" \
@ -815,7 +815,7 @@ bool AlwaysTrue();
if (const char* gtest_msg = "") { \ if (const char* gtest_msg = "") { \
bool gtest_caught_any = false; \ bool gtest_caught_any = false; \
try { \ try { \
GTEST_HIDE_UNREACHABLE_CODE_(statement); \ GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
} \ } \
catch (...) { \ catch (...) { \
gtest_caught_any = true; \ gtest_caught_any = true; \
@ -841,7 +841,7 @@ bool AlwaysTrue();
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
if (const char* gtest_msg = "") { \ if (const char* gtest_msg = "") { \
::testing::internal::HasNewFatalFailureHelper gtest_fatal_failure_checker; \ ::testing::internal::HasNewFatalFailureHelper gtest_fatal_failure_checker; \
GTEST_HIDE_UNREACHABLE_CODE_(statement); \ GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
if (gtest_fatal_failure_checker.has_new_fatal_failure()) { \ if (gtest_fatal_failure_checker.has_new_fatal_failure()) { \
gtest_msg = "Expected: " #statement " doesn't generate new fatal " \ gtest_msg = "Expected: " #statement " doesn't generate new fatal " \
"failures in the current thread.\n" \ "failures in the current thread.\n" \

View File

@ -672,7 +672,8 @@ class RE {
}; };
// Defines logging utilities: // Defines logging utilities:
// GTEST_LOG_() - logs messages at the specified severity level. // GTEST_LOG_(severity) - logs messages at the specified severity level. The
// message itself is streamed into the macro.
// LogToStderr() - directs all log messages to stderr. // LogToStderr() - directs all log messages to stderr.
// FlushInfoLog() - flushes informational log messages. // FlushInfoLog() - flushes informational log messages.
@ -683,13 +684,27 @@ enum GTestLogSeverity {
GTEST_FATAL GTEST_FATAL
}; };
void GTestLog(GTestLogSeverity severity, const char* file, // Formats log entry severity, provides a stream object for streaming the
int line, const char* msg); // log message, and terminates the message with a newline when going out of
// scope.
class GTestLog {
public:
GTestLog(GTestLogSeverity severity, const char* file, int line);
#define GTEST_LOG_(severity, msg)\ // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
::testing::internal::GTestLog(\ ~GTestLog();
::testing::internal::GTEST_##severity, __FILE__, __LINE__, \
(::testing::Message() << (msg)).GetString().c_str()) ::std::ostream& GetStream() { return ::std::cerr; }
private:
const GTestLogSeverity severity_;
GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog);
};
#define GTEST_LOG_(severity) \
::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \
__FILE__, __LINE__).GetStream()
inline void LogToStderr() {} inline void LogToStderr() {}
inline void FlushInfoLog() { fflush(NULL); } inline void FlushInfoLog() { fflush(NULL); }
@ -1011,38 +1026,12 @@ typedef TypeWithSize<8>::Int TimeInMillis; // Represents time in milliseconds.
// condition itself, plus additional message streamed into it, if any, // condition itself, plus additional message streamed into it, if any,
// and then it aborts the program. It aborts the program irrespective of // and then it aborts the program. It aborts the program irrespective of
// whether it is built in the debug mode or not. // whether it is built in the debug mode or not.
class GTestCheckProvider {
public:
GTestCheckProvider(const char* condition, const char* file, int line) {
FormatFileLocation(file, line);
::std::cerr << " ERROR: Condition " << condition << " failed. ";
}
~GTestCheckProvider() {
::std::cerr << ::std::endl;
posix::Abort();
}
void FormatFileLocation(const char* file, int line) {
if (file == NULL)
file = "unknown file";
if (line < 0) {
::std::cerr << file << ":";
} else {
#if _MSC_VER
::std::cerr << file << "(" << line << "):";
#else
::std::cerr << file << ":" << line << ":";
#endif
}
}
::std::ostream& GetStream() { return ::std::cerr; }
};
#define GTEST_CHECK_(condition) \ #define GTEST_CHECK_(condition) \
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
if (condition) \ if (condition) \
; \ ; \
else \ else \
::testing::internal::GTestCheckProvider(\ GTEST_LOG_(FATAL) << "Condition " #condition " failed. "
#condition, __FILE__, __LINE__).GetStream()
// Macro for referencing flags. // Macro for referencing flags.
#define GTEST_FLAG(name) FLAGS_gtest_##name #define GTEST_FLAG(name) FLAGS_gtest_##name

View File

@ -269,13 +269,12 @@ static void FailFromInternalError(int fd) {
} while (num_read == -1 && errno == EINTR); } while (num_read == -1 && errno == EINTR);
if (num_read == 0) { if (num_read == 0) {
GTEST_LOG_(FATAL, error); GTEST_LOG_(FATAL) << error.GetString().c_str();
} else { } else {
const int last_error = errno; const int last_error = errno;
const String message = GetLastErrnoDescription(); const String message = GetLastErrnoDescription();
GTEST_LOG_(FATAL, GTEST_LOG_(FATAL) << "Error while reading death test internal: "
Message() << "Error while reading death test internal: " << message.c_str() << " [" << last_error << "]";
<< message << " [" << last_error << "]");
} }
} }
@ -397,15 +396,13 @@ void DeathTestImpl::ReadAndInterpretStatusByte() {
FailFromInternalError(read_fd()); // Does not return. FailFromInternalError(read_fd()); // Does not return.
break; break;
default: default:
GTEST_LOG_(FATAL, GTEST_LOG_(FATAL) << "Death test child process reported "
Message() << "Death test child process reported " << "unexpected status byte ("
<< "unexpected status byte (" << static_cast<unsigned int>(flag) << ")";
<< static_cast<unsigned int>(flag) << ")");
} }
} else { } else {
GTEST_LOG_(FATAL, GTEST_LOG_(FATAL) << "Read from death test child process failed: "
Message() << "Read from death test child process failed: " << GetLastErrnoDescription().c_str();
<< GetLastErrnoDescription());
} }
GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Close(read_fd())); GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Close(read_fd()));
set_read_fd(-1); set_read_fd(-1);
@ -484,8 +481,8 @@ bool DeathTestImpl::Passed(bool status_ok) {
break; break;
case IN_PROGRESS: case IN_PROGRESS:
default: default:
GTEST_LOG_(FATAL, GTEST_LOG_(FATAL)
"DeathTest::Passed somehow called before conclusion of test"); << "DeathTest::Passed somehow called before conclusion of test";
} }
DeathTest::set_last_death_test_message(buffer.GetString()); DeathTest::set_last_death_test_message(buffer.GetString());
@ -741,7 +738,7 @@ class NoExecDeathTest : public ForkingDeathTest {
DeathTest::TestRole NoExecDeathTest::AssumeRole() { DeathTest::TestRole NoExecDeathTest::AssumeRole() {
const size_t thread_count = GetThreadCount(); const size_t thread_count = GetThreadCount();
if (thread_count != 1) { if (thread_count != 1) {
GTEST_LOG_(WARNING, DeathTestThreadWarning(thread_count)); GTEST_LOG_(WARNING) << DeathTestThreadWarning(thread_count);
} }
int pipe_fd[2]; int pipe_fd[2];

View File

@ -417,20 +417,25 @@ void RE::Init(const char* regex) {
#endif // GTEST_USES_POSIX_RE #endif // GTEST_USES_POSIX_RE
// Logs a message at the given severity level.
void GTestLog(GTestLogSeverity severity, const char* file, GTestLog::GTestLog(GTestLogSeverity severity, const char* file, int line)
int line, const char* msg) { : severity_(severity) {
const char* const marker = const char* const marker =
severity == GTEST_INFO ? "[ INFO ]" : severity == GTEST_INFO ? "[ INFO ]" :
severity == GTEST_WARNING ? "[WARNING]" : severity == GTEST_WARNING ? "[WARNING]" :
severity == GTEST_ERROR ? "[ ERROR ]" : "[ FATAL ]"; severity == GTEST_ERROR ? "[ ERROR ]" : "[ FATAL ]";
fprintf(stderr, "\n%s %s:%d: %s\n", marker, file, line, msg); GetStream() << ::std::endl << marker << " "
if (severity == GTEST_FATAL) { << FormatFileLocation(file, line).c_str() << ": ";
fflush(NULL); // abort() is not guaranteed to flush open file streams. }
// Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
GTestLog::~GTestLog() {
GetStream() << ::std::endl;
if (severity_ == GTEST_FATAL) {
fflush(stderr);
posix::Abort(); posix::Abort();
} }
} }
// Disable Microsoft deprecation warnings for POSIX functions called from // Disable Microsoft deprecation warnings for POSIX functions called from
// this class (creat, dup, dup2, and close) // this class (creat, dup, dup2, and close)
#ifdef _MSC_VER #ifdef _MSC_VER
@ -537,7 +542,7 @@ static String ReadEntireFile(FILE * file) {
// Starts capturing stderr. // Starts capturing stderr.
void CaptureStderr() { void CaptureStderr() {
if (g_captured_stderr != NULL) { if (g_captured_stderr != NULL) {
GTEST_LOG_(FATAL, "Only one stderr capturer can exist at one time."); GTEST_LOG_(FATAL) << "Only one stderr capturer can exist at one time.";
} }
g_captured_stderr = new CapturedStderr; g_captured_stderr = new CapturedStderr;
} }

View File

@ -143,7 +143,7 @@ class MayDie {
// A member function that may die. // A member function that may die.
void MemberFunction() const { void MemberFunction() const {
if (should_die_) { if (should_die_) {
GTEST_LOG_(FATAL, "death inside MayDie::MemberFunction()."); GTEST_LOG_(FATAL) << "death inside MayDie::MemberFunction().";
} }
} }
@ -154,26 +154,26 @@ class MayDie {
// A global function that's expected to die. // A global function that's expected to die.
void GlobalFunction() { void GlobalFunction() {
GTEST_LOG_(FATAL, "death inside GlobalFunction()."); GTEST_LOG_(FATAL) << "death inside GlobalFunction().";
} }
// A non-void function that's expected to die. // A non-void function that's expected to die.
int NonVoidFunction() { int NonVoidFunction() {
GTEST_LOG_(FATAL, "death inside NonVoidFunction()."); GTEST_LOG_(FATAL) << "death inside NonVoidFunction().";
return 1; return 1;
} }
// A unary function that may die. // A unary function that may die.
void DieIf(bool should_die) { void DieIf(bool should_die) {
if (should_die) { if (should_die) {
GTEST_LOG_(FATAL, "death inside DieIf()."); GTEST_LOG_(FATAL) << "death inside DieIf().";
} }
} }
// A binary function that may die. // A binary function that may die.
bool DieIfLessThan(int x, int y) { bool DieIfLessThan(int x, int y) {
if (x < y) { if (x < y) {
GTEST_LOG_(FATAL, "death inside DieIfLessThan()."); GTEST_LOG_(FATAL) << "death inside DieIfLessThan().";
} }
return true; return true;
} }
@ -188,7 +188,7 @@ void DeathTestSubroutine() {
int DieInDebugElse12(int* sideeffect) { int DieInDebugElse12(int* sideeffect) {
if (sideeffect) *sideeffect = 12; if (sideeffect) *sideeffect = 12;
#ifndef NDEBUG #ifndef NDEBUG
GTEST_LOG_(FATAL, "debug death inside DieInDebugElse12()"); GTEST_LOG_(FATAL) << "debug death inside DieInDebugElse12()";
#endif // NDEBUG #endif // NDEBUG
return 12; return 12;
} }