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
This commit is contained in:
Jakub Piasecki authored and meta-codesync[bot] committed 2026-02-25 11:16:36 -08:00
1 parent de87d9989f
commit 6ea92367e7
13 files changed
+191 -17

No files matched your search

+65 -9
View File
@@ -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(
+10 -3
View File
@@ -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 += ";"
@@ -0,0 +1,9 @@
struct test::State {
public test::State::Mode mode;
}
enum test::State::Mode {
Auto,
Disabled,
Manual,
}
@@ -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
@@ -0,0 +1,5 @@
class test::Base {
public bool disabled;
public bool selected;
public int count;
}
@@ -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
@@ -0,0 +1,4 @@
struct test::Config {
public std::optional< bool > expanded;
public std::optional< std::string > label;
}
@@ -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 <optional>
#include <string>
namespace test {
struct Config {
std::optional<bool> expanded{std::nullopt};
std::optional<std::string> label{};
};
} // namespace test
@@ -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;
}
@@ -1,3 +1,3 @@
class test::Base {
public const int value = 1;
public const int value;
}
@@ -1,4 +1,4 @@
enum test::Enum : int {
Value1 = 1,
Value2 = 2,
Value1,
Value2,
}
@@ -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,
}
@@ -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 <optional>
namespace test {
struct AccessibilityState {
bool disabled{false};
bool selected{false};
bool busy{false};
std::optional<bool> expanded{std::nullopt};
enum CheckedState { Unchecked, Checked, Mixed, None };
CheckedState checked{CheckedState::None};
};
} // namespace test