From 6ea92367e76543495feb65b03dc7564b9179bc30 Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Wed, 25 Feb 2026 11:16:36 -0800 Subject: [PATCH] Handle brace initializers (#55650) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55650 Changelog: [Internal] Updates the C++ snapshot generation script to correctly handle brace-initialized values. It also adds `STORE_INITIALIZERS_IN_SNAPSHOT` which can be used to enable/disable presence of initializers in the API snapshot. Reviewed By: cortinico Differential Revision: D93845131 fbshipit-source-id: 06872e705305ced0c46779f4a97c674f26417ff5 --- scripts/cxx-api/parser/main.py | 74 ++++++++++++++++--- scripts/cxx-api/parser/member.py | 13 +++- .../snapshot.api | 9 +++ .../test.h | 17 +++++ .../snapshot.api | 5 ++ .../test.h | 19 +++++ .../snapshot.api | 4 + .../test.h | 20 +++++ .../snapshot.api | 4 +- .../snapshot.api | 2 +- .../snapshot.api | 4 +- .../snapshot.api | 14 ++++ .../test.h | 23 ++++++ 13 files changed, 191 insertions(+), 17 deletions(-) create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_enum_field/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_enum_field/test.h create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_field/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_field/test.h create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_optional_field/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_optional_field/test.h create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_struct_inline_enum_brace_init/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_struct_inline_enum_brace_init/test.h diff --git a/scripts/cxx-api/parser/main.py b/scripts/cxx-api/parser/main.py index 1eb33690966..2dd529b5b55 100644 --- a/scripts/cxx-api/parser/main.py +++ b/scripts/cxx-api/parser/main.py @@ -25,6 +25,10 @@ from .utils import Argument, extract_qualifiers, parse_qualified_path def resolve_ref_text_name(type_def: compound.refTextType) -> str: + """ + Resolve the full text content of a refTextType, including all text + fragments and ref elements. + """ if hasattr(type_def, "content_") and type_def.content_: name = "" for part in type_def.content_: @@ -45,26 +49,74 @@ def resolve_ref_text_name(type_def: compound.refTextType) -> str: return type_def.get_valueOf_() -def resolve_linked_text_name(type_def: compound.linkedTextType) -> str: +def extract_namespace_from_refid(refid: str) -> str: + """Extract the namespace prefix from a doxygen refid. + e.g. 'namespacefacebook_1_1yoga_1a...' -> 'facebook::yoga' + """ + for prefix in ("namespace", "struct", "class", "union"): + if refid.startswith(prefix): + compound_part = refid[len(prefix) :] + idx = compound_part.find("_1a") + if idx != -1: + compound_part = compound_part[:idx] + return compound_part.replace("_1_1", "::") + return "" + + +def resolve_linked_text_name(type_def: compound.linkedTextType) -> (str, bool): + """ + Resolve the full text content of a linkedTextType, including all text + fragments and ref elements. + """ name = "" + in_string = False for part in type_def.content_: if part.category == 1: # MixedContainer.CategoryText + in_string = part.value.count('"') % 2 != in_string name += part.value elif part.category == 3: # MixedContainer.CategoryComplex (ref element) - # For ref elements, get the text content + # For ref elements, get the text content and fully qualify using refid + text = "" if hasattr(part.value, "get_valueOf_"): - name += part.value.get_valueOf_() + text = part.value.get_valueOf_() elif hasattr(part.value, "valueOf_"): - name += part.value.valueOf_ + text = part.value.valueOf_ else: - name += str(part.value) + text = str(part.value) - # literal initializers keep "=" sign + # Don't resolve refs inside string literals - doxygen may + # incorrectly treat symbols in strings as references + refid = getattr(part.value, "refid", None) + if refid and not in_string: + ns = extract_namespace_from_refid(refid) + if ns and not text.startswith(ns): + # The text may already start with a trailing portion of + # the namespace. For example ns="facebook::react::HighResDuration" + # and text="HighResDuration::zero". We need to find the + # longest suffix of ns that is a prefix of text (on a "::" + # boundary) and only prepend the missing part. + ns_parts = ns.split("::") + prepend = ns + for i in range(1, len(ns_parts)): + suffix = "::".join(ns_parts[i:]) + if text.startswith(suffix + "::") or text == suffix: + prepend = "::".join(ns_parts[:i]) + break + text = prepend + "::" + text + + name += text + + is_brace_initializer = False if name.startswith("="): + # Detect assignment initializers: = value name = name[1:] + elif name.startswith("{") and name.endswith("}"): + # Detect brace initializers: {value} + is_brace_initializer = True + name = name[1:-1].strip() - return name.strip() + return (name.strip(), is_brace_initializer) def resolve_linked_text_full(type_def: compound.linkedTextType) -> str: @@ -198,8 +250,11 @@ def get_variable_member( if is_const: variable_type = variable_type[5:].strip() + is_brace_initializer = False if member_def.initializer is not None: - variable_value = resolve_linked_text_name(member_def.initializer) + (variable_value, is_brace_initializer) = resolve_linked_text_name( + member_def.initializer + ) return VariableMember( variable_name, @@ -212,6 +267,7 @@ def get_variable_member( variable_value, variable_definition, variable_argstring, + is_brace_initializer, ) @@ -370,7 +426,7 @@ def create_enum_scope(snapshot: Snapshot, enum_def: compound.EnumdefType): value_value = None if enum_value_def.initializer is not None: - value_value = resolve_linked_text_name(enum_value_def.initializer) + (value_value, _) = resolve_linked_text_name(enum_value_def.initializer) scope.add_member( EnumMember( diff --git a/scripts/cxx-api/parser/member.py b/scripts/cxx-api/parser/member.py index d5fe3cb2639..c61ac0cf282 100644 --- a/scripts/cxx-api/parser/member.py +++ b/scripts/cxx-api/parser/member.py @@ -25,6 +25,8 @@ from .utils import ( if TYPE_CHECKING: from .scope import Scope +STORE_INITIALIZERS_IN_SNAPSHOT = False + class MemberKind(IntEnum): """ @@ -94,7 +96,7 @@ class EnumMember(Member): ) -> str: name = self._get_qualified_name(qualification) - if self.value is None: + if not STORE_INITIALIZERS_IN_SNAPSHOT or self.value is None: return " " * indent + f"{name}" return " " * indent + f"{name} = {self.value}" @@ -113,6 +115,7 @@ class VariableMember(Member): value: str | None, definition: str, argstring: str | None = None, + is_brace_initializer: bool = False, ) -> None: super().__init__(name, visibility) self.type: str = type @@ -121,6 +124,7 @@ class VariableMember(Member): self.is_static: bool = is_static self.is_constexpr: bool = is_constexpr self.is_mutable: bool = is_mutable + self.is_brace_initializer: bool = is_brace_initializer self.definition: str = definition self.argstring: str | None = argstring self._fp_arguments: list[Argument] = ( @@ -180,8 +184,11 @@ class VariableMember(Member): else: result += f"{format_parsed_type(self._parsed_type)} {name}" - if self.value is not None and (self.is_const or self.is_constexpr): - result += f" = {self.value}" + if STORE_INITIALIZERS_IN_SNAPSHOT and self.value is not None: + if self.is_brace_initializer: + result += f"{{{self.value}}}" + else: + result += f" = {self.value}" result += ";" diff --git a/scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_enum_field/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_enum_field/snapshot.api new file mode 100644 index 00000000000..d563647ce7a --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_enum_field/snapshot.api @@ -0,0 +1,9 @@ +struct test::State { + public test::State::Mode mode; +} + +enum test::State::Mode { + Auto, + Disabled, + Manual, +} diff --git a/scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_enum_field/test.h b/scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_enum_field/test.h new file mode 100644 index 00000000000..9691e2feb4e --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_enum_field/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 { + +struct State { + enum class Mode { Auto, Manual, Disabled }; + Mode mode{Mode::Auto}; +}; + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_field/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_field/snapshot.api new file mode 100644 index 00000000000..92354771148 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_field/snapshot.api @@ -0,0 +1,5 @@ +class test::Base { + public bool disabled; + public bool selected; + public int count; +} diff --git a/scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_field/test.h b/scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_field/test.h new file mode 100644 index 00000000000..8cb61f280cc --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_field/test.h @@ -0,0 +1,19 @@ +/* + * 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: + bool disabled{false}; + bool selected{true}; + int count{0}; +}; + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_optional_field/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_optional_field/snapshot.api new file mode 100644 index 00000000000..ef41a739820 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_optional_field/snapshot.api @@ -0,0 +1,4 @@ +struct test::Config { + public std::optional< bool > expanded; + public std::optional< std::string > label; +} diff --git a/scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_optional_field/test.h b/scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_optional_field/test.h new file mode 100644 index 00000000000..ba632ef1d73 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_brace_initialized_optional_field/test.h @@ -0,0 +1,20 @@ +/* + * 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 +#include + +namespace test { + +struct Config { + std::optional expanded{std::nullopt}; + std::optional label{}; +}; + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_class_public_constexpr_field/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_class_public_constexpr_field/snapshot.api index e2092f2ab5c..a5274a6cf6a 100644 --- a/scripts/cxx-api/tests/snapshots/should_handle_class_public_constexpr_field/snapshot.api +++ b/scripts/cxx-api/tests/snapshots/should_handle_class_public_constexpr_field/snapshot.api @@ -1,4 +1,4 @@ class test::Clss { - public constexpr int value1 = 1; - public constexpr int value2 = 1; + public constexpr int value1; + public constexpr int value2; } diff --git a/scripts/cxx-api/tests/snapshots/should_handle_class_public_initialized_const_field/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_class_public_initialized_const_field/snapshot.api index 48a9d17f1b3..d89a0c837e0 100644 --- a/scripts/cxx-api/tests/snapshots/should_handle_class_public_initialized_const_field/snapshot.api +++ b/scripts/cxx-api/tests/snapshots/should_handle_class_public_initialized_const_field/snapshot.api @@ -1,3 +1,3 @@ class test::Base { - public const int value = 1; + public const int value; } diff --git a/scripts/cxx-api/tests/snapshots/should_handle_enum_class_with_type/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_enum_class_with_type/snapshot.api index 88290d89962..1d88c250ee0 100644 --- a/scripts/cxx-api/tests/snapshots/should_handle_enum_class_with_type/snapshot.api +++ b/scripts/cxx-api/tests/snapshots/should_handle_enum_class_with_type/snapshot.api @@ -1,4 +1,4 @@ enum test::Enum : int { - Value1 = 1, - Value2 = 2, + Value1, + Value2, } diff --git a/scripts/cxx-api/tests/snapshots/should_handle_struct_inline_enum_brace_init/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_struct_inline_enum_brace_init/snapshot.api new file mode 100644 index 00000000000..ec3c672d418 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_struct_inline_enum_brace_init/snapshot.api @@ -0,0 +1,14 @@ +struct test::AccessibilityState { + public bool busy; + public bool disabled; + public bool selected; + public std::optional< bool > expanded; + public test::AccessibilityState::CheckedState checked; +} + +enum test::AccessibilityState::CheckedState { + Checked, + Mixed, + None, + Unchecked, +} diff --git a/scripts/cxx-api/tests/snapshots/should_handle_struct_inline_enum_brace_init/test.h b/scripts/cxx-api/tests/snapshots/should_handle_struct_inline_enum_brace_init/test.h new file mode 100644 index 00000000000..003afebba44 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_struct_inline_enum_brace_init/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 { + +struct AccessibilityState { + bool disabled{false}; + bool selected{false}; + bool busy{false}; + std::optional expanded{std::nullopt}; + enum CheckedState { Unchecked, Checked, Mixed, None }; + CheckedState checked{CheckedState::None}; +}; + +} // namespace test