From e4ded3b9c0805cc90580296a2610318477c7e950 Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Wed, 25 Feb 2026 01:12:30 -0800 Subject: [PATCH] Handle templates in the C++ API snapshot (#55693) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55693 Changelog: [Internal] Adds support for handling template params in the C++ API parser. Reviewed By: cipolleschi Differential Revision: D94074045 fbshipit-source-id: b758a8ceb7f95e8a1acf0983afa0c65aeec6b756 --- scripts/cxx-api/parser/main.py | 38 +++++++++++++++++++ scripts/cxx-api/parser/member.py | 19 ++++++++++ scripts/cxx-api/parser/scope.py | 14 +++++++ scripts/cxx-api/parser/template.py | 28 ++++++++++++++ .../snapshot.api | 4 ++ .../test.h | 18 +++++++++ .../snapshot.api | 4 ++ .../test.h | 18 +++++++++ .../snapshot.api | 2 + .../test.h | 15 ++++++++ .../snapshot.api | 2 + .../test.h | 15 ++++++++ .../should_handle_template_class/snapshot.api | 4 ++ .../should_handle_template_class/test.h | 18 +++++++++ .../snapshot.api | 4 ++ .../test.h | 18 +++++++++ 16 files changed, 221 insertions(+) create mode 100644 scripts/cxx-api/parser/template.py create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_class_public_template_method_simple/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_class_public_template_method_simple/test.h create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_class_with_template_using_typedef/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_class_with_template_using_typedef/test.h create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_free_template_function/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_free_template_function/test.h create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_free_template_using_typedef/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_free_template_using_typedef/test.h create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_template_class/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_template_class/test.h create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_template_class_with_default_param/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_template_class_with_default_param/test.h diff --git a/scripts/cxx-api/parser/main.py b/scripts/cxx-api/parser/main.py index 3b07e502526..3fc3aaabec4 100644 --- a/scripts/cxx-api/parser/main.py +++ b/scripts/cxx-api/parser/main.py @@ -13,6 +13,7 @@ from doxmlparser import compound, index from .member import EnumMember, FunctionMember, TypedefMember, VariableMember from .scope import StructLikeScopeKind from .snapshot import Snapshot +from .template import Template def resolve_ref_text_name(type_def: compound.refTextType) -> str: @@ -92,6 +93,38 @@ def get_base_classes( return base_classes +def get_template_params( + compound_object: compound.CompounddefType, +) -> [Template]: + """ + Get the template parameters of a compound object. + """ + template_params = [] + if compound_object.templateparamlist is not None: + for param in compound_object.templateparamlist.param: + template_value = ( + resolve_ref_text_name(param.defval) if param.defval else None + ) + template_name = param.defname + template_type = resolve_ref_text_name(param.get_type()) + + if template_name is None: + # Split type string and extract name from the end + # Handles: "typename T", "class T", "int N", etc. + parts = template_type.strip().split() + if len(parts) >= 2: + template_type = " ".join(parts[:-1]) + template_name = parts[-1] + elif len(parts) == 1: + # Just a name like "T" with no type keyword + template_name = parts[0] + + template_params.append( + Template(template_type, template_name, template_value) + ) + return template_params + + def get_variable_member( member_def: compound.MemberdefType, visibility: str, @@ -163,6 +196,8 @@ def get_function_member( is_static, ) + function.add_template(get_template_params(function_def)) + return function @@ -186,6 +221,8 @@ def get_typedef_member( typedef_keyword, ) + typedef.add_template(get_template_params(typedef_def)) + return typedef @@ -253,6 +290,7 @@ def build_snapshot(xml_dir: str) -> Snapshot: ) class_scope.kind.add_base(get_base_classes(compound_object)) + class_scope.kind.add_template(get_template_params(compound_object)) class_scope.location = compound_object.location.file for section_def in compound_object.sectiondef: diff --git a/scripts/cxx-api/parser/member.py b/scripts/cxx-api/parser/member.py index 568183feaf2..d7f4b1917c5 100644 --- a/scripts/cxx-api/parser/member.py +++ b/scripts/cxx-api/parser/member.py @@ -10,6 +10,8 @@ from abc import ABC, abstractmethod from dataclasses import dataclass from typing import TYPE_CHECKING +from .template import Template, TemplateList + if TYPE_CHECKING: from .scope import Scope @@ -18,6 +20,7 @@ class Member(ABC): def __init__(self, name: str, visibility: str) -> None: self.name: str = name self.visibility: str = visibility + self.template_list: TemplateList | None = None @abstractmethod def to_string( @@ -34,6 +37,16 @@ class Member(ABC): def _get_qualified_name(self, qualification: str | None): return f"{qualification}::{self.name}" if qualification else self.name + def add_template(self, template: Template | [Template]) -> None: + if template and self.template_list is None: + self.template_list = TemplateList() + + if isinstance(template, list): + for t in template: + self.template_list.add(t) + else: + self.template_list.add(template) + class EnumMember(Member): def __init__(self, name: str, value: str | None) -> None: @@ -188,6 +201,9 @@ class FunctionMember(Member): name = self._get_qualified_name(qualification) result = "" + if self.template_list is not None: + result += " " * indent + self.template_list.to_string() + "\n" + result += " " * indent if not hide_visibility: @@ -577,6 +593,9 @@ class TypedefMember(Member): name = self._get_qualified_name(qualification) result = " " * indent + if self.keyword == "using" and self.template_list is not None: + result += self.template_list.to_string() + "\n" + " " * indent + if not hide_visibility: result += self.visibility + " " diff --git a/scripts/cxx-api/parser/scope.py b/scripts/cxx-api/parser/scope.py index 1cf6490b1b1..c68803c9230 100644 --- a/scripts/cxx-api/parser/scope.py +++ b/scripts/cxx-api/parser/scope.py @@ -12,6 +12,7 @@ from typing import Generic, TypeVar from natsort import natsorted from .member import Member +from .template import Template, TemplateList from .utils import parse_qualified_path @@ -46,6 +47,7 @@ class StructLikeScopeKind(ScopeKind): super().__init__(type.value) self.base_classes: [StructLikeScopeKind.Base] = [] + self.template_list: TemplateList | None = None def add_base( self, base: StructLikeScopeKind.Base | [StructLikeScopeKind.Base] @@ -56,6 +58,16 @@ class StructLikeScopeKind(ScopeKind): else: self.base_classes.append(base) + def add_template(self, template: Template | [Template]) -> None: + if template and self.template_list is None: + self.template_list = TemplateList() + + if isinstance(template, list): + for t in template: + self.template_list.add(t) + else: + self.template_list.add(template) + def to_string(self, scope: Scope) -> str: result = "" @@ -69,6 +81,8 @@ class StructLikeScopeKind(ScopeKind): inheritance_string = " : " + ", ".join(bases) if bases else "" + if self.template_list is not None: + result += "\n" + self.template_list.to_string() + "\n" result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{" stringified_members = [] diff --git a/scripts/cxx-api/parser/template.py b/scripts/cxx-api/parser/template.py new file mode 100644 index 00000000000..b05f1bb8048 --- /dev/null +++ b/scripts/cxx-api/parser/template.py @@ -0,0 +1,28 @@ +# 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. + + +class Template: + def __init__(self, type: str, name: str, value: str | None) -> None: + self.type: str = type + self.name: str = name + self.value: str | None = value + + def to_string(self) -> str: + if self.value is None: + return f"{self.type} {self.name}" + else: + return f"{self.type} {self.name} = {self.value}" + + +class TemplateList: + def __init__(self) -> None: + self.templates: [Template] = [] + + def add(self, template: Template) -> None: + self.templates.append(template) + + def to_string(self) -> str: + return f"template <{', '.join([template.to_string() for template in self.templates])}>" diff --git a/scripts/cxx-api/tests/snapshots/should_handle_class_public_template_method_simple/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_class_public_template_method_simple/snapshot.api new file mode 100644 index 00000000000..0d8589adb81 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_class_public_template_method_simple/snapshot.api @@ -0,0 +1,4 @@ +class test::Base { + template + public T hello(); +} diff --git a/scripts/cxx-api/tests/snapshots/should_handle_class_public_template_method_simple/test.h b/scripts/cxx-api/tests/snapshots/should_handle_class_public_template_method_simple/test.h new file mode 100644 index 00000000000..f2abcd7ddcc --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_class_public_template_method_simple/test.h @@ -0,0 +1,18 @@ +/* + * 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 { + +class Base { + public: + template + T hello(); +}; + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_class_with_template_using_typedef/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_class_with_template_using_typedef/snapshot.api new file mode 100644 index 00000000000..c7e6fcb16a0 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_class_with_template_using_typedef/snapshot.api @@ -0,0 +1,4 @@ +class test::Clss { + template + public using MyT = T; +} diff --git a/scripts/cxx-api/tests/snapshots/should_handle_class_with_template_using_typedef/test.h b/scripts/cxx-api/tests/snapshots/should_handle_class_with_template_using_typedef/test.h new file mode 100644 index 00000000000..64d05733fc1 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_class_with_template_using_typedef/test.h @@ -0,0 +1,18 @@ +/* + * 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 { + +class Clss { + public: + template + using MyT = T; +}; + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_free_template_function/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_free_template_function/snapshot.api new file mode 100644 index 00000000000..79b42db1a88 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_free_template_function/snapshot.api @@ -0,0 +1,2 @@ +template +T test::helloWorld(); diff --git a/scripts/cxx-api/tests/snapshots/should_handle_free_template_function/test.h b/scripts/cxx-api/tests/snapshots/should_handle_free_template_function/test.h new file mode 100644 index 00000000000..f35330dc169 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_free_template_function/test.h @@ -0,0 +1,15 @@ +/* + * 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 { + +template +T helloWorld(); + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_free_template_using_typedef/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_free_template_using_typedef/snapshot.api new file mode 100644 index 00000000000..bfef8803063 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_free_template_using_typedef/snapshot.api @@ -0,0 +1,2 @@ +template +using test::MyT = T; diff --git a/scripts/cxx-api/tests/snapshots/should_handle_free_template_using_typedef/test.h b/scripts/cxx-api/tests/snapshots/should_handle_free_template_using_typedef/test.h new file mode 100644 index 00000000000..063a9f61417 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_free_template_using_typedef/test.h @@ -0,0 +1,15 @@ +/* + * 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 { + +template +using MyT = T; + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_template_class/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_template_class/snapshot.api new file mode 100644 index 00000000000..f67741c5409 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_template_class/snapshot.api @@ -0,0 +1,4 @@ +template +class test::Tmp { + public T field; +} diff --git a/scripts/cxx-api/tests/snapshots/should_handle_template_class/test.h b/scripts/cxx-api/tests/snapshots/should_handle_template_class/test.h new file mode 100644 index 00000000000..140efd6b9c2 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_template_class/test.h @@ -0,0 +1,18 @@ +/* + * 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 { + +template +class Tmp { + public: + T field; +}; + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_template_class_with_default_param/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_template_class_with_default_param/snapshot.api new file mode 100644 index 00000000000..92e30e273ff --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_template_class_with_default_param/snapshot.api @@ -0,0 +1,4 @@ +template +class test::Tmp { + public T field; +} diff --git a/scripts/cxx-api/tests/snapshots/should_handle_template_class_with_default_param/test.h b/scripts/cxx-api/tests/snapshots/should_handle_template_class_with_default_param/test.h new file mode 100644 index 00000000000..16b619adb8a --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_template_class_with_default_param/test.h @@ -0,0 +1,18 @@ +/* + * 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 { + +template +class Tmp { + public: + T field; +}; + +} // namespace test