From 5e69e1a51e7acb7f0755a58794e26ae9adf293a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Sun, 20 May 2018 17:05:25 +0200 Subject: [PATCH] src: add CHECK_IMPLIES macro This change introduces the CHECK_IMPLIES macro similar to its definition in v8 and replaces instances of CHECK with CHECK_IMPLIES where it seems appropriate. PR-URL: https://github.com/nodejs/node/pull/20914 Reviewed-By: Anna Henningsen Reviewed-By: Ben Noordhuis Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- src/callback_scope.cc | 4 +--- src/inspector_io.cc | 2 +- src/tls_wrap.cc | 2 +- src/util-inl.h | 6 +++--- src/util.h | 1 + 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/callback_scope.cc b/src/callback_scope.cc index 5e7a27cf01fc20..9eac7beb038a26 100644 --- a/src/callback_scope.cc +++ b/src/callback_scope.cc @@ -44,9 +44,7 @@ InternalCallbackScope::InternalCallbackScope(Environment* env, async_context_(asyncContext), object_(object), callback_scope_(env) { - if (expect == kRequireResource) { - CHECK(!object.IsEmpty()); - } + CHECK_IMPLIES(expect == kRequireResource, !object.IsEmpty()); if (!env->can_call_into_js()) { failed_ = true; diff --git a/src/inspector_io.cc b/src/inspector_io.cc index ce18e989737b5a..2934512478468c 100644 --- a/src/inspector_io.cc +++ b/src/inspector_io.cc @@ -197,7 +197,7 @@ bool InspectorIo::Start() { } void InspectorIo::Stop() { - CHECK(state_ == State::kAccepting || !sessions_.empty()); + CHECK_IMPLIES(sessions_.empty(), state_ == State::kAccepting); Write(TransportAction::kKill, 0, StringView()); int err = uv_thread_join(&thread_); CHECK_EQ(err, 0); diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc index 5d84a10da2e0b1..8ecb4880d30dab 100644 --- a/src/tls_wrap.cc +++ b/src/tls_wrap.cc @@ -676,7 +676,7 @@ void TLSWrap::OnStreamRead(ssize_t nread, const uv_buf_t& buf) { if (!hello_parser_.IsEnded()) { size_t avail = 0; uint8_t* data = reinterpret_cast(enc_in->Peek(&avail)); - CHECK(avail == 0 || data != nullptr); + CHECK_IMPLIES(data == nullptr, avail == 0); return hello_parser_.Parse(data, avail); } diff --git a/src/util-inl.h b/src/util-inl.h index 41a22c97efd9c0..ff0d47c078815a 100644 --- a/src/util-inl.h +++ b/src/util-inl.h @@ -365,21 +365,21 @@ inline T* UncheckedCalloc(size_t n) { template inline T* Realloc(T* pointer, size_t n) { T* ret = UncheckedRealloc(pointer, n); - if (n > 0) CHECK_NE(ret, nullptr); + CHECK_IMPLIES(n > 0, ret != nullptr); return ret; } template inline T* Malloc(size_t n) { T* ret = UncheckedMalloc(n); - if (n > 0) CHECK_NE(ret, nullptr); + CHECK_IMPLIES(n > 0, ret != nullptr); return ret; } template inline T* Calloc(size_t n) { T* ret = UncheckedCalloc(n); - if (n > 0) CHECK_NE(ret, nullptr); + CHECK_IMPLIES(n > 0, ret != nullptr); return ret; } diff --git a/src/util.h b/src/util.h index 1f43ba3f7b90f3..ff77ccc0b8c20d 100644 --- a/src/util.h +++ b/src/util.h @@ -129,6 +129,7 @@ void DumpBacktrace(FILE* fp); #define CHECK_LE(a, b) CHECK((a) <= (b)) #define CHECK_LT(a, b) CHECK((a) < (b)) #define CHECK_NE(a, b) CHECK((a) != (b)) +#define CHECK_IMPLIES(a, b) CHECK(!(a) || (b)) #define UNREACHABLE() ABORT()