From 2527628edae5651cb28e72b2c98f24e72dcdb384 Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Mon, 21 Mar 2016 17:46:21 -0700 Subject: [PATCH] base: Avoid compilation error when compiled with -Wdangling-else. As logging macros uses `if xxx else yyy` style, it is reported as an error when DCHECK() is compiled with -Wdangling-else option. Because after preprocess, DCHECK(x) becomes: if (EnableDChecks) if (x) ; else LogMessage(FATAL) << yyy; This CL avoids compilation error by replacing `if xxx else yyy` with `xxx && yyy` or `!(xxx) || yyy`. Bug: 26962895 Change-Id: Ib0bf242cc04a238ec31a1ab66b53fc8a5b5ed28f --- base/include/android-base/logging.h | 32 ++++++++++++----------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/base/include/android-base/logging.h b/base/include/android-base/logging.h index e5babedf3..41fe4b32b 100644 --- a/base/include/android-base/logging.h +++ b/base/include/android-base/logging.h @@ -100,9 +100,9 @@ class ErrnoRestorer { errno = saved_errno_; } - // Allow this object to evaluate to false which is useful in macros. + // Allow this object to be used as part of && operation. operator bool() const { - return false; + return true; } private: @@ -123,13 +123,11 @@ class ErrnoRestorer { // else statement after LOG() macro, it won't bind to the if statement in the macro. // do-while(0) statement doesn't work here. Because we need to support << operator // following the macro, like "LOG(DEBUG) << xxx;". -#define LOG_TO(dest, severity) \ - if (LIKELY(::android::base::severity < ::android::base::GetMinimumLogSeverity())) \ - ; \ - else \ - ::android::base::ErrnoRestorer() ? *(std::ostream*)nullptr : \ - ::android::base::LogMessage(__FILE__, __LINE__, \ - ::android::base::dest, \ +#define LOG_TO(dest, severity) \ + UNLIKELY(::android::base::severity >= ::android::base::GetMinimumLogSeverity()) && \ + ::android::base::ErrnoRestorer() && \ + ::android::base::LogMessage(__FILE__, __LINE__, \ + ::android::base::dest, \ ::android::base::severity, -1).stream() // A variant of LOG that also logs the current errno value. To be used when @@ -137,13 +135,11 @@ class ErrnoRestorer { #define PLOG(severity) PLOG_TO(DEFAULT, severity) // Behaves like PLOG, but logs to the specified log ID. -#define PLOG_TO(dest, severity) \ - if (LIKELY(::android::base::severity < ::android::base::GetMinimumLogSeverity())) \ - ; \ - else \ - ::android::base::ErrnoRestorer() ? *(std::ostream*)nullptr : \ - ::android::base::LogMessage(__FILE__, __LINE__, \ - ::android::base::dest, \ +#define PLOG_TO(dest, severity) \ + UNLIKELY(::android::base::severity >= ::android::base::GetMinimumLogSeverity()) && \ + ::android::base::ErrnoRestorer() && \ + ::android::base::LogMessage(__FILE__, __LINE__, \ + ::android::base::dest, \ ::android::base::severity, errno).stream() // Marker that code is yet to be implemented. @@ -165,9 +161,7 @@ class ErrnoRestorer { // CHECK(false == true) results in a log message of // "Check failed: false == true". #define CHECK(x) \ - if (LIKELY((x))) \ - ; \ - else \ + LIKELY((x)) || \ ABORT_AFTER_LOG_FATAL \ ::android::base::LogMessage(__FILE__, __LINE__, ::android::base::DEFAULT, \ ::android::base::FATAL, -1).stream() \