ReactCommon/utils: Migrate uses of NDEBUG to REACT_NATIVE_DEBUG + react_native_assert

Summary:
For better cross-platform consistency, migrate usages of NDEBUG to REACT_NATIVE_DEBUG. See flags.h for explanation.

Changelog: [Internal]

Reviewed By: PeteTheHeat

Differential Revision: D26695275

fbshipit-source-id: 85aae94105a2817d345d25f736386e545dff0a9a
This commit is contained in:
Joshua Gross 2021-02-26 23:26:05 -08:00 коммит произвёл Facebook GitHub Bot
Родитель da73cca5e2
Коммит 9f120efcf4
6 изменённых файлов: 33 добавлений и 18 удалений

Просмотреть файл

@ -11,7 +11,7 @@ LOCAL_MODULE := react_utils
LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)/*.cpp)
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_C_INCLUDES := $(LOCAL_PATH) $(LOCAL_PATH)/../../
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../
LOCAL_CFLAGS := \
@ -20,6 +20,8 @@ LOCAL_CFLAGS := \
LOCAL_CFLAGS += -fexceptions -frtti -std=c++14 -Wall
LOCAL_STATIC_LIBRARIES :=
LOCAL_SHARED_LIBRARIES :=
LOCAL_SHARED_LIBRARIES := libreact_debug
include $(BUILD_SHARED_LIBRARY)
$(call import-module,react/debug)

Просмотреть файл

@ -59,5 +59,6 @@ rn_xplat_cxx_library(
"//xplat/folly:molly",
"//xplat/jsi:jsi",
react_native_xplat_target("better:better"),
react_native_xplat_target("react/debug:debug"),
],
)

Просмотреть файл

@ -7,6 +7,8 @@
#include <functional>
#include <react/debug/react_native_assert.h>
namespace facebook {
namespace react {
@ -31,7 +33,7 @@ class CalledOnceMovableOnlyFunction {
}
~CalledOnceMovableOnlyFunction() {
assert(
react_native_assert(
(wasCalled_ || wasMovedFrom_) &&
"`CalledOnceMovableOnlyFunction` is destroyed before being called.");
}
@ -57,7 +59,7 @@ class CalledOnceMovableOnlyFunction {
CalledOnceMovableOnlyFunction &operator=(
CalledOnceMovableOnlyFunction &&other) noexcept {
assert(
react_native_assert(
(wasCalled_ || wasMovedFrom_) &&
"`CalledOnceMovableOnlyFunction` is re-assigned before being called.");
wasCalled_ = false;
@ -71,10 +73,10 @@ class CalledOnceMovableOnlyFunction {
* Callable.
*/
ReturnT operator()(ArgumentT... args) {
assert(
react_native_assert(
!wasMovedFrom_ &&
"`CalledOnceMovableOnlyFunction` is called after being moved from.");
assert(
react_native_assert(
!wasCalled_ &&
"`CalledOnceMovableOnlyFunction` is called more than once.");

Просмотреть файл

@ -15,6 +15,9 @@
#include <better/mutex.h>
#include <better/optional.h>
#include <react/debug/flags.h>
#include <react/debug/react_native_assert.h>
namespace facebook {
namespace react {
@ -43,7 +46,7 @@ class ContextContainer final {
instances_.insert({key, std::make_shared<T>(instance)});
#ifndef NDEBUG
#ifdef REACT_NATIVE_DEBUG
typeNames_.insert({key, typeid(T).name()});
#endif
}
@ -57,7 +60,7 @@ class ContextContainer final {
instances_.erase(key);
#ifndef NDEBUG
#ifdef REACT_NATIVE_DEBUG
typeNames_.erase(key);
#endif
}
@ -73,7 +76,7 @@ class ContextContainer final {
for (auto const &pair : contextContainer.instances_) {
instances_.erase(pair.first);
instances_.insert(pair);
#ifndef NDEBUG
#ifdef REACT_NATIVE_DEBUG
typeNames_.erase(pair.first);
typeNames_.insert(
{pair.first, contextContainer.typeNames_.at(pair.first)});
@ -90,12 +93,14 @@ class ContextContainer final {
T at(std::string const &key) const {
std::shared_lock<better::shared_mutex> lock(mutex_);
assert(
react_native_assert(
instances_.find(key) != instances_.end() &&
"ContextContainer doesn't have an instance for given key.");
assert(
#ifdef REACT_NATIVE_DEBUG
react_native_assert(
typeNames_.at(key) == typeid(T).name() &&
"ContextContainer stores an instance of different type for given key.");
#endif
return *std::static_pointer_cast<T>(instances_.at(key));
}
@ -113,9 +118,11 @@ class ContextContainer final {
return {};
}
assert(
#ifdef REACT_NATIVE_DEBUG
react_native_assert(
typeNames_.at(key) == typeid(T).name() &&
"ContextContainer stores an instance of different type for given key.");
#endif
return *std::static_pointer_cast<T>(iterator->second);
}
@ -124,7 +131,7 @@ class ContextContainer final {
mutable better::shared_mutex mutex_;
// Protected by mutex_`.
mutable better::map<std::string, std::shared_ptr<void>> instances_;
#ifndef NDEBUG
#ifdef REACT_NATIVE_DEBUG
mutable better::map<std::string, std::string> typeNames_;
#endif
};

Просмотреть файл

@ -7,6 +7,8 @@
#pragma once
#include <react/debug/react_native_assert.h>
#if defined(__OBJC__) && defined(__cplusplus)
#if TARGET_OS_MAC && TARGET_OS_IPHONE
@ -65,7 +67,7 @@ inline std::shared_ptr<void> wrapManagedObjectWeakly(id object) noexcept
inline id unwrapManagedObjectWeakly(std::shared_ptr<void> const &object) noexcept
{
RCTInternalGenericWeakWrapper *weakWrapper = (RCTInternalGenericWeakWrapper *)unwrapManagedObject(object);
assert(weakWrapper && "`RCTInternalGenericWeakWrapper` instance must not be `nil`.");
react_native_assert(weakWrapper && "`RCTInternalGenericWeakWrapper` instance must not be `nil`.");
return weakWrapper.object;
}

Просмотреть файл

@ -7,7 +7,7 @@
#include "RunLoopObserver.h"
#include <cassert>
#include <react/debug/react_native_assert.h>
namespace facebook {
namespace react {
@ -19,8 +19,9 @@ RunLoopObserver::RunLoopObserver(
void RunLoopObserver::setDelegate(Delegate const *delegate) const noexcept {
// We need these constraints to ensure basic thread-safety.
assert(delegate && "A delegate must not be `nullptr`.");
assert(!delegate_ && "`RunLoopObserver::setDelegate` must be called once.");
react_native_assert(delegate && "A delegate must not be `nullptr`.");
react_native_assert(
!delegate_ && "`RunLoopObserver::setDelegate` must be called once.");
delegate_ = delegate;
}
@ -47,7 +48,7 @@ void RunLoopObserver::activityDidChange(Activity activity) const noexcept {
return;
}
assert(
react_native_assert(
!owner_.expired() &&
"`owner_` is null. The caller must `lock` the owner and check it for being not null.");