From 9911e280430ace382bef85b1c8e463c17fbe0522 Mon Sep 17 00:00:00 2001 From: Chih-Hung Hsieh Date: Wed, 29 Jan 2020 21:30:48 -0800 Subject: [PATCH] Allow LOG_ALWAYS_FATAL_IF in constexpr functions * When __FAKE_USE_VA_ARGS has args that are not constexpr, reaching it in a constexpr function will be make the constexpr function invalid. To allow LOG_ALWAYS_FATAL_IF in a constexpr functions when the tested condition is false, it should call __FAKE_USE_VA_ARGS only when the tested condition is true. * Other ALOG*_IF macros are also changed to call __FAKE_USE_VA_ARGS only when the tested condition is true. Bug: 148548418 Test: WITH_TIDY=1 make Change-Id: Ie8a444dffbf9cbef78e5e0b49b44f4092bcf9982 --- liblog/include/log/log_main.h | 45 +++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/liblog/include/log/log_main.h b/liblog/include/log/log_main.h index 64791c2cc..1bd1c8aec 100644 --- a/liblog/include/log/log_main.h +++ b/liblog/include/log/log_main.h @@ -56,7 +56,7 @@ __BEGIN_DECLS /* * Use __VA_ARGS__ if running a static analyzer, * to avoid warnings of unused variables in __VA_ARGS__. - * Use contexpr function in C++ mode, so these macros can be used + * Use constexpr function in C++ mode, so these macros can be used * in other constexpr functions without warning. */ #ifdef __clang_analyzer__ @@ -131,10 +131,10 @@ extern int __fake_use_va_args(int, ...); * is -inverted- from the normal assert() semantics. */ #ifndef LOG_ALWAYS_FATAL_IF -#define LOG_ALWAYS_FATAL_IF(cond, ...) \ - ((__predict_false(cond)) \ - ? ((void)android_printAssert(#cond, LOG_TAG, ##__VA_ARGS__)) \ - : __FAKE_USE_VA_ARGS(__VA_ARGS__)) +#define LOG_ALWAYS_FATAL_IF(cond, ...) \ + ((__predict_false(cond)) ? (__FAKE_USE_VA_ARGS(__VA_ARGS__), \ + ((void)android_printAssert(#cond, LOG_TAG, ##__VA_ARGS__))) \ + : ((void)0)) #endif #ifndef LOG_ALWAYS_FATAL @@ -213,9 +213,10 @@ extern int __fake_use_va_args(int, ...); #if LOG_NDEBUG #define ALOGV_IF(cond, ...) __FAKE_USE_VA_ARGS(__VA_ARGS__) #else -#define ALOGV_IF(cond, ...) \ - ((__predict_false(cond)) ? ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \ - : __FAKE_USE_VA_ARGS(__VA_ARGS__)) +#define ALOGV_IF(cond, ...) \ + ((__predict_false(cond)) \ + ? (__FAKE_USE_VA_ARGS(__VA_ARGS__), (void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \ + : ((void)0)) #endif #endif @@ -227,9 +228,10 @@ extern int __fake_use_va_args(int, ...); #endif #ifndef ALOGD_IF -#define ALOGD_IF(cond, ...) \ - ((__predict_false(cond)) ? ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \ - : __FAKE_USE_VA_ARGS(__VA_ARGS__)) +#define ALOGD_IF(cond, ...) \ + ((__predict_false(cond)) \ + ? (__FAKE_USE_VA_ARGS(__VA_ARGS__), (void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \ + : ((void)0)) #endif /* @@ -240,9 +242,10 @@ extern int __fake_use_va_args(int, ...); #endif #ifndef ALOGI_IF -#define ALOGI_IF(cond, ...) \ - ((__predict_false(cond)) ? ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \ - : __FAKE_USE_VA_ARGS(__VA_ARGS__)) +#define ALOGI_IF(cond, ...) \ + ((__predict_false(cond)) \ + ? (__FAKE_USE_VA_ARGS(__VA_ARGS__), (void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \ + : ((void)0)) #endif /* @@ -253,9 +256,10 @@ extern int __fake_use_va_args(int, ...); #endif #ifndef ALOGW_IF -#define ALOGW_IF(cond, ...) \ - ((__predict_false(cond)) ? ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \ - : __FAKE_USE_VA_ARGS(__VA_ARGS__)) +#define ALOGW_IF(cond, ...) \ + ((__predict_false(cond)) \ + ? (__FAKE_USE_VA_ARGS(__VA_ARGS__), (void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \ + : ((void)0)) #endif /* @@ -266,9 +270,10 @@ extern int __fake_use_va_args(int, ...); #endif #ifndef ALOGE_IF -#define ALOGE_IF(cond, ...) \ - ((__predict_false(cond)) ? ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \ - : __FAKE_USE_VA_ARGS(__VA_ARGS__)) +#define ALOGE_IF(cond, ...) \ + ((__predict_false(cond)) \ + ? (__FAKE_USE_VA_ARGS(__VA_ARGS__), (void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \ + : ((void)0)) #endif /* --------------------------------------------------------------------- */