From 0686cde779ca39c0180a8ad2558cdd6612f1fea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ma=C5=82ecki?= Date: Fri, 6 Mar 2026 08:09:48 -0800 Subject: [PATCH] Strip `__deprecated_msg` in the preprocessing (#55970) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55970 Strips `__deprecated_msg` annotations due to incorrect parsing when in front of the interfaces (predefining does not help unfortunately). It doesn't fully solve the issue stated in the added test as doxygen also has problem with parsing interfaces for which the base class is in the new line. ``` interface Foo : Bar end ``` This will be handled by the fix in the doxygen. This diff also adds stripping the `__deprecated` label which is again incorrectly parsed by doxygen. Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D95541064 fbshipit-source-id: 08aca2eb5ed8b1897166d385ab513879e7ecbad7 --- .../input_filters/doxygen_strip_comments.py | 23 +++++++++ scripts/cxx-api/parser/__main__.py | 6 ++- .../should_handle_deprecated_msg/snapshot.api | 1 + .../should_handle_deprecated_msg/test.h | 2 + .../snapshot.api | 4 ++ .../test.h | 17 +++++++ scripts/cxx-api/tests/test_input_filters.py | 51 ++++++++++++++++++- scripts/cxx-api/tests/test_snapshots.py | 15 +++--- 8 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 scripts/cxx-api/tests/snapshots/should_strip_deprecated_msg_before_interface/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_strip_deprecated_msg_before_interface/test.h diff --git a/scripts/cxx-api/input_filters/doxygen_strip_comments.py b/scripts/cxx-api/input_filters/doxygen_strip_comments.py index d379f7d1c9d..83a7c06d92e 100755 --- a/scripts/cxx-api/input_filters/doxygen_strip_comments.py +++ b/scripts/cxx-api/input_filters/doxygen_strip_comments.py @@ -36,6 +36,28 @@ def strip_block_comments(content: str) -> str: return comment_pattern.sub(replace_with_newlines, content) +def strip_deprecated_msg(content: str) -> str: + """ + Remove __deprecated_msg(...) macros and standalone __deprecated annotations + from content. + + These macros cause Doxygen to produce malformed XML output when they appear + before @interface declarations, creating __pad0__ artifacts and missing + members. Standalone __deprecated on method declarations causes the annotation + to be parsed as a parameter name. Since the macros are stripped, deprecation + info won't appear in the API snapshot output. + """ + # Pattern to match __deprecated_msg("...") with any content inside quotes + pattern = re.compile(r'__deprecated_msg\s*\(\s*"[^"]*"\s*\)\s*') + content = pattern.sub("", content) + + # Pattern to match standalone __deprecated (not followed by _msg or other suffix) + standalone_pattern = re.compile(r"\b__deprecated\b(?!_)\s*") + content = standalone_pattern.sub("", content) + + return content + + def main(): if len(sys.argv) < 2: print("Usage: doxygen_strip_comments.py ", file=sys.stderr) @@ -48,6 +70,7 @@ def main(): content = f.read() filtered = strip_block_comments(content) + filtered = strip_deprecated_msg(filtered) print(filtered, end="") except Exception as e: # On error, output original content to not break the build diff --git a/scripts/cxx-api/parser/__main__.py b/scripts/cxx-api/parser/__main__.py index 49270bad790..63a05c5fa5b 100644 --- a/scripts/cxx-api/parser/__main__.py +++ b/scripts/cxx-api/parser/__main__.py @@ -60,7 +60,7 @@ def build_doxygen_config( with open(os.path.join(directory, ".doxygen.config.template")) as f: template = f.read() - # replace the placeholder with the actual path + # replace the placeholders with the actual values config = ( template.replace("${INPUTS}", include_directories_str) .replace("${EXCLUDE_PATTERNS}", exclude_patterns_str) @@ -103,9 +103,12 @@ def build_snapshot_for_view( if verbose: print("Running Doxygen") + if input_filter: + print(f" Using input filter: {input_filter}") # Run doxygen with the config file doxygen_bin = os.environ.get("DOXYGEN_BIN", "doxygen") + result = subprocess.run( [doxygen_bin, DOXYGEN_CONFIG_FILE], cwd=react_native_dir, @@ -227,6 +230,7 @@ def main(): definitions=config.definitions, output_dir=output_dir, verbose=verbose, + input_filter=input_filter, ) else: snapshot = build_snapshot_for_view( diff --git a/scripts/cxx-api/tests/snapshots/should_handle_deprecated_msg/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_deprecated_msg/snapshot.api index 3e8df8d033e..4117819c363 100644 --- a/scripts/cxx-api/tests/snapshots/should_handle_deprecated_msg/snapshot.api +++ b/scripts/cxx-api/tests/snapshots/should_handle_deprecated_msg/snapshot.api @@ -7,6 +7,7 @@ interface RCTDeprecatedInterface { interface RCTTestInterface { public virtual NSArray* deprecatedMethod:(id param); + public virtual instancetype initWithURL:launchOptions:(NSURL* url, NSDictionary* launchOptions); public virtual void normalMethod:(NSString* name); } diff --git a/scripts/cxx-api/tests/snapshots/should_handle_deprecated_msg/test.h b/scripts/cxx-api/tests/snapshots/should_handle_deprecated_msg/test.h index b3d47328979..b1e2da33bdf 100644 --- a/scripts/cxx-api/tests/snapshots/should_handle_deprecated_msg/test.h +++ b/scripts/cxx-api/tests/snapshots/should_handle_deprecated_msg/test.h @@ -11,6 +11,8 @@ - (NSArray *)deprecatedMethod:(id)param __deprecated_msg("Use newMethod instead."); +- (instancetype)initWithURL:(NSURL *)url launchOptions:(NSDictionary *)launchOptions __deprecated; + @end @interface RCTDeprecatedInterface diff --git a/scripts/cxx-api/tests/snapshots/should_strip_deprecated_msg_before_interface/snapshot.api b/scripts/cxx-api/tests/snapshots/should_strip_deprecated_msg_before_interface/snapshot.api new file mode 100644 index 00000000000..faa87a7cae6 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_strip_deprecated_msg_before_interface/snapshot.api @@ -0,0 +1,4 @@ +interface RCTSurface { + protected __pad0__; + public @property (assign, readonly) CGSize minimumSize; +} diff --git a/scripts/cxx-api/tests/snapshots/should_strip_deprecated_msg_before_interface/test.h b/scripts/cxx-api/tests/snapshots/should_strip_deprecated_msg_before_interface/test.h new file mode 100644 index 00000000000..4c7de1b9360 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_strip_deprecated_msg_before_interface/test.h @@ -0,0 +1,17 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +__deprecated_msg("This API will be removed along with the legacy architecture.") @interface RCTSurface + : NSObject + +- (instancetype)initWithBridge:(RCTBridge *)bridge + moduleName:(NSString *)moduleName + initialProperties:(NSDictionary *)initialProperties; + +@property (atomic, assign, readonly) CGSize minimumSize; + +@end diff --git a/scripts/cxx-api/tests/test_input_filters.py b/scripts/cxx-api/tests/test_input_filters.py index 2aa3bf50881..821e93e937a 100644 --- a/scripts/cxx-api/tests/test_input_filters.py +++ b/scripts/cxx-api/tests/test_input_filters.py @@ -7,7 +7,10 @@ from __future__ import annotations import unittest -from ..input_filters.doxygen_strip_comments import strip_block_comments +from ..input_filters.doxygen_strip_comments import ( + strip_block_comments, + strip_deprecated_msg, +) class TestDoxygenStripComments(unittest.TestCase): @@ -67,5 +70,51 @@ class TestDoxygenStripComments(unittest.TestCase): self.assertEqual(result, content) +class TestStripDeprecatedMsg(unittest.TestCase): + def test_strips_deprecated_msg(self): + content = '- (void)oldMethod __deprecated_msg("Use newMethod instead.");' + result = strip_deprecated_msg(content) + self.assertEqual(result, "- (void)oldMethod ;") + + def test_strips_standalone_deprecated(self): + content = "- (instancetype)initWithBundleURL:(NSURL *)bundleURL launchOptions:(nullable NSDictionary *)launchOptions __deprecated;" + result = strip_deprecated_msg(content) + self.assertEqual( + result, + "- (instancetype)initWithBundleURL:(NSURL *)bundleURL launchOptions:(nullable NSDictionary *)launchOptions ;", + ) + + def test_standalone_deprecated_does_not_match_deprecated_msg(self): + content = '__deprecated_msg("msg") and __deprecated' + result = strip_deprecated_msg(content) + self.assertEqual(result, "and ") + + def test_preserves_deprecated_in_identifiers(self): + content = ( + "- (void)findComponentViewWithTag_DO_NOT_USE_DEPRECATED:(NSInteger)tag;" + ) + result = strip_deprecated_msg(content) + self.assertEqual(result, content) + + def test_preserves_DEPRECATED_suffix_in_names(self): + content = "@property (weak) RCTViewRegistry *viewRegistry_DEPRECATED;" + result = strip_deprecated_msg(content) + self.assertEqual(result, content) + + def test_handles_no_deprecated(self): + content = "- (void)normalMethod;" + result = strip_deprecated_msg(content) + self.assertEqual(result, content) + + def test_handles_empty_content(self): + result = strip_deprecated_msg("") + self.assertEqual(result, "") + + def test_strips_deprecated_before_interface(self): + content = '__deprecated_msg("This API will be removed.") @interface RCTSurface : NSObject' + result = strip_deprecated_msg(content) + self.assertEqual(result, "@interface RCTSurface : NSObject") + + if __name__ == "__main__": unittest.main() diff --git a/scripts/cxx-api/tests/test_snapshots.py b/scripts/cxx-api/tests/test_snapshots.py index 4b21fcd728f..91a7938e58b 100644 --- a/scripts/cxx-api/tests/test_snapshots.py +++ b/scripts/cxx-api/tests/test_snapshots.py @@ -111,15 +111,16 @@ def _make_case_test(case_dir: Traversable, tests_root: Traversable): ) # Get real filesystem path for filter script if it exists - filter_script_path = None + # IMPORTANT: Keep the context manager active while Doxygen runs, + # otherwise the extracted file may be cleaned up before use if filter_script.is_file(): with ir.as_file(filter_script) as fs_path: - filter_script_path = str(fs_path) - - # Run doxygen to generate the XML - _generate_doxygen_api( - str(case_dir_path), str(doxygen_config_path), filter_script_path - ) + _generate_doxygen_api( + str(case_dir_path), str(doxygen_config_path), str(fs_path) + ) + else: + # No filter script available - run without filter + _generate_doxygen_api(str(case_dir_path), str(doxygen_config_path)) # Parse the generated XML xml_dir = case_dir_path / "api" / "xml"