From 22464570659ca5b9a55ef681057be8ea7b22b9c6 Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Wed, 25 Feb 2026 01:12:30 -0800 Subject: [PATCH] Handle type aliases in C++ API snapshot (#55694) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55694 Changelog: [Internal] Adds support for handling type aliases in the c++ API snapshot Reviewed By: cipolleschi Differential Revision: D94074032 fbshipit-source-id: 8af8bc88e884f4b1b7f7394d1316776cc9d50d6b --- scripts/cxx-api/parser/main.py | 34 +++++++++++++- scripts/cxx-api/parser/member.py | 46 +++++++++++++++++++ .../snapshot.api | 3 ++ .../test.h | 17 +++++++ .../snapshot.api | 3 ++ .../should_handle_class_with_typedef/test.h | 17 +++++++ .../snapshot.api | 3 ++ .../test.h | 17 +++++++ .../snapshot.api | 4 ++ .../test.h | 18 ++++++++ .../should_handle_free_typedef/snapshot.api | 1 + .../should_handle_free_typedef/test.h | 14 ++++++ .../snapshot.api | 1 + .../test.h | 14 ++++++ .../snapshot.api | 1 + .../should_handle_free_using_typedef/test.h | 14 ++++++ .../snapshot.api | 6 +++ .../should_handle_union_with_typedef/test.h | 23 ++++++++++ 18 files changed, 235 insertions(+), 1 deletion(-) create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_class_protected_typedef/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_class_protected_typedef/test.h create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_class_with_typedef/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_class_with_typedef/test.h create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_class_with_using_typedef/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_class_with_using_typedef/test.h create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_class_with_using_typedef_ref/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_class_with_using_typedef_ref/test.h create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_free_typedef/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_free_typedef/test.h create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_free_typedef_function/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_free_typedef_function/test.h create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_free_using_typedef/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_free_using_typedef/test.h create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_union_with_typedef/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_union_with_typedef/test.h diff --git a/scripts/cxx-api/parser/main.py b/scripts/cxx-api/parser/main.py index 048d6573da0..3b07e502526 100644 --- a/scripts/cxx-api/parser/main.py +++ b/scripts/cxx-api/parser/main.py @@ -10,7 +10,7 @@ from pprint import pprint from doxmlparser import compound, index -from .member import EnumMember, FunctionMember, VariableMember +from .member import EnumMember, FunctionMember, TypedefMember, VariableMember from .scope import StructLikeScopeKind from .snapshot import Snapshot @@ -166,6 +166,29 @@ def get_function_member( return function +def get_typedef_member( + typedef_def: compound.memberdefType, visibility: str +) -> TypedefMember: + typedef_name = typedef_def.get_name() + typedef_type = resolve_ref_text_name(typedef_def.get_type()) + typedef_argstring = typedef_def.get_argsstring() + typedef_definition = typedef_def.definition + + typedef_keyword = "using" + if typedef_definition.startswith("typedef"): + typedef_keyword = "typedef" + + typedef = TypedefMember( + typedef_name, + typedef_type, + typedef_argstring, + visibility, + typedef_keyword, + ) + + return typedef + + def create_enum_scope(snapshot: Snapshot, enum_def: compound.EnumdefType): """ Create an enum scope in the snapshot. @@ -261,6 +284,10 @@ def build_snapshot(xml_dir: str) -> Snapshot: for member_def in section_def.memberdef: if member_def.kind == "enum": create_enum_scope(snapshot, member_def) + elif member_def.kind == "typedef": + class_scope.add_member( + get_typedef_member(member_def, visibility) + ) else: print( f"Unknown section member kind: {member_def.kind} in {compound_object.location.file}" @@ -302,6 +329,11 @@ def build_snapshot(xml_dir: str) -> Snapshot: namespace_scope.add_member( get_function_member(function_def, "public") ) + elif section_def.kind == "typedef": + for typedef_def in section_def.memberdef: + namespace_scope.add_member( + get_typedef_member(typedef_def, "public") + ) elif section_def.kind == "enum": for enum_def in section_def.memberdef: create_enum_scope(snapshot, enum_def) diff --git a/scripts/cxx-api/parser/member.py b/scripts/cxx-api/parser/member.py index 196cae3eb5b..568183feaf2 100644 --- a/scripts/cxx-api/parser/member.py +++ b/scripts/cxx-api/parser/member.py @@ -549,3 +549,49 @@ class FunctionMember(Member): modifiers = FunctionMember._parse_modifiers(modifiers_str) return (arguments, modifiers) + + +class TypedefMember(Member): + def __init__( + self, name: str, type: str, argstring: str | None, visibility: str, keyword: str + ) -> None: + super().__init__(name, visibility) + self.type: str = type + self.keyword: str = keyword + self.argstring: str | None = argstring + + def close(self, scope: Scope): + # TODO: handle unqualified references + pass + + def _is_function_pointer(self) -> bool: + """Check if this typedef is a function pointer type.""" + return self.argstring is not None and self.argstring.startswith(")(") + + def to_string( + self, + indent: int = 0, + qualification: str | None = None, + hide_visibility: bool = False, + ) -> str: + name = self._get_qualified_name(qualification) + result = " " * indent + + if not hide_visibility: + result += self.visibility + " " + + result += self.keyword + + if self.keyword == "using": + result += f" {name} = {self.type};" + elif self._is_function_pointer(): + # Function pointer typedef: "typedef return_type (*name)(args);" + # type is e.g. "void(*", argstring is ")(args...)" + if "(*" in self.type: + result += f" {self.type}{name}{self.argstring};" + else: + result += f" {self.type}(*{name}{self.argstring};" + else: + result += f" {self.type} {name};" + + return result diff --git a/scripts/cxx-api/tests/snapshots/should_handle_class_protected_typedef/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_class_protected_typedef/snapshot.api new file mode 100644 index 00000000000..e4e1a20d571 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_class_protected_typedef/snapshot.api @@ -0,0 +1,3 @@ +class test::Clss { + protected typedef int32_t MyInt32; +} diff --git a/scripts/cxx-api/tests/snapshots/should_handle_class_protected_typedef/test.h b/scripts/cxx-api/tests/snapshots/should_handle_class_protected_typedef/test.h new file mode 100644 index 00000000000..9fb2427708a --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_class_protected_typedef/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. + */ + +#pragma once + +namespace test { + +class Clss { + protected: + typedef int32_t MyInt32; +}; + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_class_with_typedef/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_class_with_typedef/snapshot.api new file mode 100644 index 00000000000..43d945a2e6a --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_class_with_typedef/snapshot.api @@ -0,0 +1,3 @@ +class test::Clss { + public typedef int32_t MyInt32; +} diff --git a/scripts/cxx-api/tests/snapshots/should_handle_class_with_typedef/test.h b/scripts/cxx-api/tests/snapshots/should_handle_class_with_typedef/test.h new file mode 100644 index 00000000000..febfb198211 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_class_with_typedef/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. + */ + +#pragma once + +namespace test { + +class Clss { + public: + typedef int32_t MyInt32; +}; + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_class_with_using_typedef/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_class_with_using_typedef/snapshot.api new file mode 100644 index 00000000000..41423e074b6 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_class_with_using_typedef/snapshot.api @@ -0,0 +1,3 @@ +class test::Clss { + public using MyInt32 = int32_t; +} diff --git a/scripts/cxx-api/tests/snapshots/should_handle_class_with_using_typedef/test.h b/scripts/cxx-api/tests/snapshots/should_handle_class_with_using_typedef/test.h new file mode 100644 index 00000000000..b9de8b85505 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_class_with_using_typedef/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. + */ + +#pragma once + +namespace test { + +class Clss { + public: + using MyInt32 = int32_t; +}; + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_class_with_using_typedef_ref/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_class_with_using_typedef_ref/snapshot.api new file mode 100644 index 00000000000..8eea6806966 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_class_with_using_typedef_ref/snapshot.api @@ -0,0 +1,4 @@ +class test::Clss { + public typedef MyInt32 int32_t; + public using OtherInt32 = MyInt32; +} diff --git a/scripts/cxx-api/tests/snapshots/should_handle_class_with_using_typedef_ref/test.h b/scripts/cxx-api/tests/snapshots/should_handle_class_with_using_typedef_ref/test.h new file mode 100644 index 00000000000..aec59cc3171 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_class_with_using_typedef_ref/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: + typedef MyInt32 int32_t; + using OtherInt32 = MyInt32; +}; + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_free_typedef/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_free_typedef/snapshot.api new file mode 100644 index 00000000000..b510389a699 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_free_typedef/snapshot.api @@ -0,0 +1 @@ +typedef int32_t test::MyInt32; diff --git a/scripts/cxx-api/tests/snapshots/should_handle_free_typedef/test.h b/scripts/cxx-api/tests/snapshots/should_handle_free_typedef/test.h new file mode 100644 index 00000000000..849db4f6186 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_free_typedef/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 { + +typedef int32_t MyInt32; + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_free_typedef_function/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_free_typedef_function/snapshot.api new file mode 100644 index 00000000000..a2871e35854 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_free_typedef_function/snapshot.api @@ -0,0 +1 @@ +typedef void(*test::FnPtr)(int32_t); diff --git a/scripts/cxx-api/tests/snapshots/should_handle_free_typedef_function/test.h b/scripts/cxx-api/tests/snapshots/should_handle_free_typedef_function/test.h new file mode 100644 index 00000000000..de686a5106f --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_free_typedef_function/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 { + +typedef void (*FnPtr)(int32_t); + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_free_using_typedef/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_free_using_typedef/snapshot.api new file mode 100644 index 00000000000..b959915c3ef --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_free_using_typedef/snapshot.api @@ -0,0 +1 @@ +using test::MyInt32 = int32_t; diff --git a/scripts/cxx-api/tests/snapshots/should_handle_free_using_typedef/test.h b/scripts/cxx-api/tests/snapshots/should_handle_free_using_typedef/test.h new file mode 100644 index 00000000000..ababc93aa6c --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_free_using_typedef/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 { + +using MyInt32 = int32_t; + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_union_with_typedef/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_union_with_typedef/snapshot.api new file mode 100644 index 00000000000..2a53d5d9cf9 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_union_with_typedef/snapshot.api @@ -0,0 +1,6 @@ +union test::TypedefUnion { + public FloatType floatValue; + public ValueType intValue; + public typedef float FloatType; + public using ValueType = int32_t; +} diff --git a/scripts/cxx-api/tests/snapshots/should_handle_union_with_typedef/test.h b/scripts/cxx-api/tests/snapshots/should_handle_union_with_typedef/test.h new file mode 100644 index 00000000000..ac45b267e66 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_union_with_typedef/test.h @@ -0,0 +1,23 @@ +/* + * 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 + +#include + +namespace test { + +union TypedefUnion { + public: + using ValueType = int32_t; + typedef float FloatType; + + ValueType intValue; + FloatType floatValue; +}; + +} // namespace test