Skip to content

Commit c5bc3f1

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Use NSCAssert() in react_native_assert instead of C assert() (#36177)
Summary: Pull Request resolved: #36177 react_native_assert calls C `assert()`, where XCode does not have a built-in breakpoint navigator to hook to assertion failures (though you can add a symbolic breakpoint to "abort()" to get the effect). This changes the Apple implemented of `react_native_assert()` to use `NSCAssert` under the hood. This is safe to use in C functions, but will be trapped by the default XCode exceptions breakpoint navigator. Changelog: [iOS][Fixed] - Use NSCAssert() in react_native_assert instead of C assert() Reviewed By: cipolleschi Differential Revision: D43275024 fbshipit-source-id: 43c4e4f1ae6b99f32634d4b1880bce712c3ae8f6
1 parent 96b2ca4 commit c5bc3f1

File tree

6 files changed

+37
-42
lines changed

6 files changed

+37
-42
lines changed

ReactCommon/React-Fabric.podspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ Pod::Spec.new do |s|
235235
s.subspec "debug_core" do |ss|
236236
ss.dependency folly_dep_name, folly_version
237237
ss.compiler_flags = folly_compiler_flags
238-
ss.source_files = "react/debug/**/*.{m,mm,cpp,h}"
238+
ss.source_files = "react/debug/*.h",
239+
"react/debug/ios/**/*.{m,mm,cpp,h}"
239240
ss.exclude_files = "react/debug/tests"
240241
ss.header_dir = "react/debug"
241242
end

ReactCommon/react/debug/BUCK

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ APPLE_COMPILER_FLAGS = get_apple_compiler_flags()
1414

1515
rn_xplat_cxx_library(
1616
name = "debug",
17-
srcs = glob(
18-
["**/*.cpp"],
19-
exclude = glob(["tests/**/*.cpp"]),
20-
),
2117
headers = glob(
2218
["**/*.h"],
2319
exclude = glob(["tests/**/*.h"]),
@@ -40,8 +36,10 @@ rn_xplat_cxx_library(
4036
# for android react_native_assert
4137
"-llog",
4238
],
39+
fbandroid_srcs = glob(["android/**/*.cpp"]),
4340
fbobjc_compiler_flags = APPLE_COMPILER_FLAGS,
4441
fbobjc_preprocessor_flags = get_preprocessor_flags_for_build_mode() + get_apple_inspector_flags(),
42+
fbobjc_srcs = glob(["ios/**/*.mm"]),
4543
force_static = True,
4644
labels = [
4745
"pfh:ReactNative_CommonInfrastructurePlaceholder",

ReactCommon/react/debug/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ add_compile_options(
1616
-DLOG_TAG=\"Fabric\")
1717

1818

19-
file(GLOB react_debug_SRC CONFIGURE_DEPENDS *.cpp)
19+
file(GLOB react_debug_SRC CONFIGURE_DEPENDS android/*.cpp)
2020
add_library(react_debug SHARED ${react_debug_SRC})
2121

2222
target_include_directories(react_debug PUBLIC ${REACT_COMMON_DIR})

ReactCommon/react/debug/react_native_assert.cpp renamed to ReactCommon/react/debug/android/react_native_assert.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,10 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
#ifdef __ANDROID__
9-
108
#include <android/log.h>
9+
#include <react/debug/react_native_assert.h>
1110

12-
// Provide a prototype to silence missing prototype warning in release
13-
// mode.
14-
extern "C" void react_native_assert_fail(
15-
const char *func,
16-
const char *file,
17-
int line,
18-
const char *expr);
11+
#ifdef REACT_NATIVE_DEBUG
1912

2013
extern "C" void react_native_assert_fail(
2114
const char *func,
@@ -42,4 +35,4 @@ extern "C" void react_native_assert_fail(
4235
expr);
4336
}
4437

45-
#endif // __ANDROID__
38+
#endif
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <Foundation/NSException.h>
9+
#import <glog/logging.h>
10+
#import <react/debug/react_native_assert.h>
11+
12+
#ifdef REACT_NATIVE_DEBUG
13+
14+
extern "C" void react_native_assert_fail(const char *func, const char *file, int line, const char *expr)
15+
{
16+
// flush logs because some might be lost on iOS if an assert is hit right after
17+
// this. If you are trying to debug something actively and have added lots of
18+
// LOG statements to track down an issue, there is race between flushing the
19+
// final logs and stopping execution when the assert hits. Thus, if we know an
20+
// assert will fail, we force flushing to happen right before the assert.
21+
LOG(ERROR) << "react_native_assert failure: " << expr;
22+
google::FlushLogFiles(google::GLOG_INFO /*min_severity*/);
23+
24+
NSCAssert(false, @"%s:%d: function %s: assertion failed (%s)", file, line, func, expr);
25+
}
26+
27+
#endif

ReactCommon/react/debug/react_native_assert.h

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@
2828

2929
#else // REACT_NATIVE_DEBUG
3030

31-
#ifdef __ANDROID__
32-
33-
#include <android/log.h>
31+
#define react_native_assert(e) \
32+
((e) ? (void)0 : react_native_assert_fail(__func__, __FILE__, __LINE__, #e))
3433

3534
#ifdef __cplusplus
3635
extern "C" {
@@ -44,27 +43,4 @@ void react_native_assert_fail(
4443
}
4544
#endif // __cpusplus
4645

47-
#define react_native_assert(e) \
48-
((e) ? (void)0 : react_native_assert_fail(__func__, __FILE__, __LINE__, #e))
49-
50-
#else // __ANDROID__
51-
52-
#include <glog/logging.h>
53-
#include <cassert>
54-
55-
// For all platforms, but iOS+Xcode especially: flush logs because some might be
56-
// lost on iOS if an assert is hit right after this. If you are trying to debug
57-
// something actively and have added lots of LOG statements to track down an
58-
// issue, there is race between flushing the final logs and stopping execution
59-
// when the assert hits. Thus, if we know an assert will fail, we force flushing
60-
// to happen right before the assert.
61-
#define react_native_assert(cond) \
62-
if (!(cond)) { \
63-
LOG(ERROR) << "react_native_assert failure: " << #cond; \
64-
google::FlushLogFiles(google::GLOG_INFO); \
65-
assert(cond); \
66-
}
67-
68-
#endif // platforms besides __ANDROID__
69-
7046
#endif // REACT_NATIVE_DEBUG

0 commit comments

Comments
 (0)