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_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_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../
LOCAL_CFLAGS := \ LOCAL_CFLAGS := \
@ -20,6 +20,8 @@ LOCAL_CFLAGS := \
LOCAL_CFLAGS += -fexceptions -frtti -std=c++14 -Wall LOCAL_CFLAGS += -fexceptions -frtti -std=c++14 -Wall
LOCAL_STATIC_LIBRARIES := LOCAL_STATIC_LIBRARIES :=
LOCAL_SHARED_LIBRARIES := LOCAL_SHARED_LIBRARIES := libreact_debug
include $(BUILD_SHARED_LIBRARY) include $(BUILD_SHARED_LIBRARY)
$(call import-module,react/debug)

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

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

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

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

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

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

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

@ -7,6 +7,8 @@
#pragma once #pragma once
#include <react/debug/react_native_assert.h>
#if defined(__OBJC__) && defined(__cplusplus) #if defined(__OBJC__) && defined(__cplusplus)
#if TARGET_OS_MAC && TARGET_OS_IPHONE #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 inline id unwrapManagedObjectWeakly(std::shared_ptr<void> const &object) noexcept
{ {
RCTInternalGenericWeakWrapper *weakWrapper = (RCTInternalGenericWeakWrapper *)unwrapManagedObject(object); 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; return weakWrapper.object;
} }

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

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