Skip to content

Commit d7f4f75

Browse files
Suppress Clang unsafe-buffer warnings in generated C++ TurboModule headers
## Summary: Fixes #60092. The generated C++ TurboModule invoker indexes the raw argument pointer, which Clang rejects under -Wunsafe-buffer-usage. Suppress -Wunsafe-buffer-usage around the generated header body and restore it at the end of the file. The generated invokers keep their existing JSI pointer, count and argument checks. Thanks to @YevheniiKotyrlo for the compiler reproduction. Codex helped implement and verify this fix. ## Changelog: [GENERAL] [FIXED] - Suppress -Wunsafe-buffer-usage in generated C++ TurboModule headers. ## Test Plan: - Generated SampleJSI.h with the repository's Codegen CLI and compiled a concrete TurboModule with Clang 19.1.7 using -std=c++20 -fsyntax-only -Werror -Wunsafe-buffer-usage. The original header failed on args[1]. The patched header compiled with Clang 19.1.7 and GCC 12.2 for required, optional, nullable and zero-argument methods. Clang still flags raw pointer indexing in a source file after it includes the generated header. - yarn test --runInBand packages/react-native-codegen passed all 3,150 tests and 1,279 snapshots. - ESLint and Prettier passed for GenerateModuleH.js. Full Android and iOS app builds were outside this compile regression. Differential Revision: D132939887 Pull Request resolved: #60105
1 parent 845f593 commit d7f4f75

3 files changed

Lines changed: 224 additions & 0 deletions

File tree

‎packages/react-native-codegen/e2e/deep_imports/__tests__/modules/__snapshots__/GenerateModuleH-test.js.snap‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ exports[`GenerateModuleH can generate a header file NativeModule specs 1`] = `
1515
#include <React/Bridging.h>
1616
#include <React/NativeModuleCore.h>
1717

18+
// JSI provides the argument pointer and corresponding count to generated invokers.
19+
#ifdef __clang__
20+
#if __has_warning(\\"-Wunsafe-buffer-usage\\")
21+
#pragma clang diagnostic push
22+
#pragma clang diagnostic ignored \\"-Wunsafe-buffer-usage\\"
23+
#endif
24+
#endif
25+
1826
namespace facebook::react {
1927

2028

@@ -1594,6 +1602,12 @@ private:
15941602
};
15951603

15961604
} // namespace facebook::react
1605+
1606+
#ifdef __clang__
1607+
#if __has_warning(\\"-Wunsafe-buffer-usage\\")
1608+
#pragma clang diagnostic pop
1609+
#endif
1610+
#endif
15971611
"
15981612
`;
15991613

@@ -1612,6 +1626,14 @@ exports[`GenerateModuleH can generate a header file NativeModule specs with assu
16121626
#include <React/Bridging.h>
16131627
#include <React/NativeModuleCore.h>
16141628

1629+
// JSI provides the argument pointer and corresponding count to generated invokers.
1630+
#ifdef __clang__
1631+
#if __has_warning(\\"-Wunsafe-buffer-usage\\")
1632+
#pragma clang diagnostic push
1633+
#pragma clang diagnostic ignored \\"-Wunsafe-buffer-usage\\"
1634+
#endif
1635+
#endif
1636+
16151637
namespace facebook::react {
16161638

16171639

@@ -3191,5 +3213,11 @@ private:
31913213
};
31923214

31933215
} // namespace facebook::react
3216+
3217+
#ifdef __clang__
3218+
#if __has_warning(\\"-Wunsafe-buffer-usage\\")
3219+
#pragma clang diagnostic pop
3220+
#endif
3221+
#endif
31943222
"
31953223
`;

‎packages/react-native-codegen/src/generators/modules/GenerateModuleH.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,25 @@ const FileTemplate = ({
215215
#include <React/Bridging.h>
216216
#include <React/NativeModuleCore.h>
217217
218+
// JSI provides the argument pointer and corresponding count to generated invokers.
219+
#ifdef __clang__
220+
#if __has_warning("-Wunsafe-buffer-usage")
221+
#pragma clang diagnostic push
222+
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
223+
#endif
224+
#endif
225+
218226
namespace facebook::react {
219227
220228
${modules.join('\n\n')}
221229
222230
} // namespace facebook::react
231+
232+
#ifdef __clang__
233+
#if __has_warning("-Wunsafe-buffer-usage")
234+
#pragma clang diagnostic pop
235+
#endif
236+
#endif
223237
`;
224238
};
225239

0 commit comments

Comments
 (0)