From 78a7718d6cb5b4d590b5a450a1f2d98dfcc08df9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ma=C5=82ecki?= Date: Wed, 18 Mar 2026 09:29:02 -0700 Subject: [PATCH] Extract member specializations from name (#56076) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56076 Changelog: [Internal] Updates the C++ API snapshot generator to extract specializations from name when a member is created, instead of relying on the one stored in the name string. It also makes out-of-class definitions of members to be skipped in the snapshot as those were essentially duplicated between their declaration inside the class, and the initialization outside of it. Reviewed By: cipolleschi Differential Revision: D96303740 fbshipit-source-id: d649cb04bd7370af55ce707d607a372d7b4115b4 --- scripts/cxx-api/parser/main.py | 12 ++++++++++- .../cxx-api/parser/member/function_member.py | 20 ++++++++++++------ .../cxx-api/parser/member/variable_member.py | 21 +++++++++++++------ scripts/cxx-api/parser/utils/__init__.py | 10 +++------ .../cxx-api/parser/utils/argument_parsing.py | 21 +++++++++++++++++++ .../parser/utils/type_qualification.py | 10 --------- .../should_handle_free_variable/snapshot.api | 1 + .../should_handle_free_variable/test.h | 14 +++++++++++++ .../snapshot.api | 3 --- 9 files changed, 79 insertions(+), 33 deletions(-) create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_free_variable/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_free_variable/test.h diff --git a/scripts/cxx-api/parser/main.py b/scripts/cxx-api/parser/main.py index c1f8b6ad1f0..ddc055bb0cc 100644 --- a/scripts/cxx-api/parser/main.py +++ b/scripts/cxx-api/parser/main.py @@ -25,7 +25,7 @@ from .builders import ( get_variable_member, ) from .snapshot import Snapshot -from .utils import parse_qualified_path +from .utils import has_scope_resolution_outside_angles, parse_qualified_path def build_snapshot(xml_dir: str) -> Snapshot: @@ -84,12 +84,22 @@ def build_snapshot(xml_dir: str) -> Snapshot: for section_def in compound_object.sectiondef: if section_def.kind == "var": for variable_def in section_def.memberdef: + # Skip out-of-class definitions (e.g. "Strct::VALUE") + if has_scope_resolution_outside_angles( + variable_def.get_name() + ): + continue is_static = variable_def.static == "yes" namespace_scope.add_member( get_variable_member(variable_def, "public", is_static) ) elif section_def.kind == "func": for function_def in section_def.memberdef: + # Skip out-of-class definitions (e.g. "Strct::convert") + if has_scope_resolution_outside_angles( + function_def.get_name() + ): + continue function_static = function_def.static == "yes" if not function_static: diff --git a/scripts/cxx-api/parser/member/function_member.py b/scripts/cxx-api/parser/member/function_member.py index 6f7c9077887..17650c24372 100644 --- a/scripts/cxx-api/parser/member/function_member.py +++ b/scripts/cxx-api/parser/member/function_member.py @@ -12,8 +12,8 @@ from ..utils import ( format_arguments, parse_arg_string, qualify_arguments, - qualify_template_args_only, qualify_type_str, + split_specialization, ) from .base import Member, MemberKind @@ -34,7 +34,9 @@ class FunctionMember(Member): doxygen_params: list[Argument] | None = None, is_constexpr: bool = False, ) -> None: - super().__init__(name, visibility) + base_name, specialization_args = split_specialization(name) + super().__init__(base_name, visibility) + self.specialization_args: list[str] | None = specialization_args self.type: str = type self.is_virtual: bool = is_virtual self.is_static: bool = is_static @@ -62,10 +64,16 @@ class FunctionMember(Member): def close(self, scope: Scope): self.type = qualify_type_str(self.type, scope) self.arguments = qualify_arguments(self.arguments, scope) - # Qualify template arguments in function name for explicit specializations - # e.g., "convert" -> "convert" - if "<" in self.name: - self.name = qualify_template_args_only(self.name, scope) + if self.specialization_args is not None: + self.specialization_args = [ + qualify_type_str(arg, scope) for arg in self.specialization_args + ] + + def _get_qualified_name(self, qualification: str | None) -> str: + name = self.name + if self.specialization_args is not None: + name = f"{name}<{', '.join(self.specialization_args)}>" + return f"{qualification}::{name}" if qualification else name def to_string( self, diff --git a/scripts/cxx-api/parser/member/variable_member.py b/scripts/cxx-api/parser/member/variable_member.py index 69c18509caa..c69378eb835 100644 --- a/scripts/cxx-api/parser/member/variable_member.py +++ b/scripts/cxx-api/parser/member/variable_member.py @@ -15,7 +15,8 @@ from ..utils import ( parse_type_with_argstrings, qualify_arguments, qualify_parsed_type, - qualify_template_args_only, + qualify_type_str, + split_specialization, ) from .base import Member, MemberKind, STORE_INITIALIZERS_IN_SNAPSHOT @@ -38,7 +39,9 @@ class VariableMember(Member): argstring: str | None = None, is_brace_initializer: bool = False, ) -> None: - super().__init__(name, visibility) + base_name, specialization_args = split_specialization(name) + super().__init__(base_name, visibility) + self.specialization_args: list[str] | None = specialization_args self.type: str = type self.value: str | None = value self.is_const: bool = is_const @@ -62,15 +65,21 @@ class VariableMember(Member): def close(self, scope: Scope): self._fp_arguments = qualify_arguments(self._fp_arguments, scope) self._parsed_type = qualify_parsed_type(self._parsed_type, scope) - # Qualify template arguments in variable name for explicit specializations - # e.g., "default_value" -> "default_value" - if "<" in self.name: - self.name = qualify_template_args_only(self.name, scope) + if self.specialization_args is not None: + self.specialization_args = [ + qualify_type_str(arg, scope) for arg in self.specialization_args + ] def _is_function_pointer(self) -> bool: """Check if this variable is a function pointer type.""" return self.argstring is not None and self.argstring.startswith(")(") + def _get_qualified_name(self, qualification: str | None) -> str: + name = self.name + if self.specialization_args is not None: + name = f"{name}<{', '.join(self.specialization_args)}>" + return f"{qualification}::{name}" if qualification else name + def to_string( self, indent: int = 0, diff --git a/scripts/cxx-api/parser/utils/__init__.py b/scripts/cxx-api/parser/utils/__init__.py index bf8858fce2b..bf2ce030127 100644 --- a/scripts/cxx-api/parser/utils/__init__.py +++ b/scripts/cxx-api/parser/utils/__init__.py @@ -9,6 +9,7 @@ from .argument_parsing import ( format_arguments, format_parsed_type, FunctionModifiers, + has_scope_resolution_outside_angles, parse_arg_string, parse_function_pointer_argstring, parse_type_with_argstrings, @@ -22,12 +23,7 @@ from .text_resolution import ( normalize_pointer_spacing, resolve_linked_text_name, ) -from .type_qualification import ( - qualify_arguments, - qualify_parsed_type, - qualify_template_args_only, - qualify_type_str, -) +from .type_qualification import qualify_arguments, qualify_parsed_type, qualify_type_str __all__ = [ "Argument", @@ -36,6 +32,7 @@ __all__ = [ "format_arguments", "format_parsed_type", "FunctionModifiers", + "has_scope_resolution_outside_angles", "InitializerType", "normalize_angle_brackets", "normalize_pointer_spacing", @@ -45,7 +42,6 @@ __all__ = [ "parse_type_with_argstrings", "qualify_arguments", "qualify_parsed_type", - "qualify_template_args_only", "qualify_type_str", "resolve_linked_text_name", "split_specialization", diff --git a/scripts/cxx-api/parser/utils/argument_parsing.py b/scripts/cxx-api/parser/utils/argument_parsing.py index 01f4018a2fa..e9bcfb010ee 100644 --- a/scripts/cxx-api/parser/utils/argument_parsing.py +++ b/scripts/cxx-api/parser/utils/argument_parsing.py @@ -116,6 +116,27 @@ def _find_matching_angle(s: str, start: int = 0) -> int: return _find_matching_bracket(s, start, "<", ">", ignore_inside="(") +def has_scope_resolution_outside_angles(name: str) -> bool: + """Check if '::' appears outside angle brackets in a name. + + Returns True for class-prefixed out-of-class definitions + (e.g. 'Strct< T >::VALUE') but False when '::' only appears inside + template arguments (e.g. 'func'). + """ + depth = 0 + i = 0 + while i < len(name): + ch = name[i] + if ch == "<": + depth += 1 + elif ch == ">": + depth -= 1 + elif ch == ":" and depth == 0 and i + 1 < len(name) and name[i + 1] == ":": + return True + i += 1 + return False + + def _iter_at_depth_zero(s: str): """Iterate over string, yielding (index, char, at_depth_zero) tuples. diff --git a/scripts/cxx-api/parser/utils/type_qualification.py b/scripts/cxx-api/parser/utils/type_qualification.py index 24b16605075..ded0978d56d 100644 --- a/scripts/cxx-api/parser/utils/type_qualification.py +++ b/scripts/cxx-api/parser/utils/type_qualification.py @@ -31,16 +31,6 @@ def qualify_type_str(type_str: str, scope: Scope) -> str: return _qualify_type_str_impl(type_str, scope, qualify_base=True) -def qualify_template_args_only(type_str: str, scope: Scope) -> str: - """Qualify only template arguments in a type string, leaving the base type unchanged. - - This is useful for class names in template specializations where the base type - is already positioned in the correct scope but the template arguments need - qualification (e.g., "MyVector< Test >" -> "MyVector< ns::Test >"). - """ - return _qualify_type_str_impl(type_str, scope, qualify_base=False) - - def _qualify_prefix_with_decorators(prefix: str, scope: Scope) -> str: """Qualify a template prefix that may have leading const/volatile qualifiers.""" stripped = prefix.lstrip() diff --git a/scripts/cxx-api/tests/snapshots/should_handle_free_variable/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_free_variable/snapshot.api new file mode 100644 index 00000000000..98c7eb48b9e --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_free_variable/snapshot.api @@ -0,0 +1 @@ +int test::helloWorld; diff --git a/scripts/cxx-api/tests/snapshots/should_handle_free_variable/test.h b/scripts/cxx-api/tests/snapshots/should_handle_free_variable/test.h new file mode 100644 index 00000000000..1e0f3698376 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_free_variable/test.h @@ -0,0 +1,14 @@ +/* + * 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. + */ + +#pragma once + +namespace test { + +extern int helloWorld; + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_template_variable/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_template_variable/snapshot.api index 52711af8da3..a24a25f6de4 100644 --- a/scripts/cxx-api/tests/snapshots/should_handle_template_variable/snapshot.api +++ b/scripts/cxx-api/tests/snapshots/should_handle_template_variable/snapshot.api @@ -1,6 +1,3 @@ -template -const test::Strct test::Strct::VALUE; - template struct test::Strct { public static const test::Strct VALUE;