mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Explicitly handle friend clasess (#55843)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55843 Changelog: [Internal] Adds explicit support for friend classes in the C++ API snapshot. Doxygen treats friends as variables of type "friend". For the snapshot use case, we cover that special case and store them as explicit `FriendMember`. This allows for correctly ignoring friend definitions when qualifying identifiers. Reviewed By: cipolleschi Differential Revision: D94901510 fbshipit-source-id: fd2f8d7798eee2bfb5778a0969cf77d20d9978a7
This commit is contained in:
1 parent
bdaf176a21
commit
4683e006f5
8 files changed
+119
-6
No files matched your search
@@ -15,6 +15,7 @@ from doxmlparser import compound, index
|
||||
from .member import (
|
||||
ConceptMember,
|
||||
EnumMember,
|
||||
FriendMember,
|
||||
FunctionMember,
|
||||
TypedefMember,
|
||||
VariableMember,
|
||||
@@ -474,11 +475,22 @@ def build_snapshot(xml_dir: str) -> Snapshot:
|
||||
if member_type == "attrib":
|
||||
for member_def in section_def.memberdef:
|
||||
if member_def.kind == "variable":
|
||||
class_scope.add_member(
|
||||
get_variable_member(
|
||||
member_def, visibility, is_static
|
||||
)
|
||||
(var_type, _) = resolve_linked_text_name(
|
||||
member_def.get_type()
|
||||
)
|
||||
|
||||
if var_type == "friend":
|
||||
class_scope.add_member(
|
||||
FriendMember(
|
||||
member_def.get_name(), visibility
|
||||
)
|
||||
)
|
||||
else:
|
||||
class_scope.add_member(
|
||||
get_variable_member(
|
||||
member_def, visibility, is_static
|
||||
)
|
||||
)
|
||||
elif member_type == "func":
|
||||
for function_def in section_def.memberdef:
|
||||
class_scope.add_member(
|
||||
|
||||
@@ -40,6 +40,7 @@ class MemberKind(IntEnum):
|
||||
FUNCTION = 3
|
||||
OPERATOR = 4
|
||||
VARIABLE = 5
|
||||
FRIEND = 6
|
||||
|
||||
|
||||
class Member(ABC):
|
||||
@@ -358,6 +359,28 @@ class TypedefMember(Member):
|
||||
return result
|
||||
|
||||
|
||||
class FriendMember(Member):
|
||||
def __init__(self, name: str, visibility: str = "public") -> None:
|
||||
super().__init__(name, visibility)
|
||||
|
||||
@property
|
||||
def member_kind(self) -> MemberKind:
|
||||
return MemberKind.FRIEND
|
||||
|
||||
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 += f"friend {name};"
|
||||
return result
|
||||
|
||||
|
||||
class ConceptMember(Member):
|
||||
def __init__(
|
||||
self,
|
||||
|
||||
@@ -11,7 +11,7 @@ from typing import Generic, TypeVar
|
||||
|
||||
from natsort import natsort_keygen, natsorted
|
||||
|
||||
from .member import Member, MemberKind
|
||||
from .member import FriendMember, Member, MemberKind
|
||||
from .template import Template, TemplateList
|
||||
from .utils import parse_qualified_path
|
||||
|
||||
@@ -226,7 +226,10 @@ class Scope(Generic[ScopeKindT]):
|
||||
if base_name in current_scope.inner_scopes:
|
||||
matched_segments.append(path_segment)
|
||||
current_scope = current_scope.inner_scopes[base_name]
|
||||
elif any(m.name == base_name for m in current_scope._members):
|
||||
elif any(
|
||||
m.name == base_name and not isinstance(m, FriendMember)
|
||||
for m in current_scope._members
|
||||
):
|
||||
# Found as a member, assume following segments exist in the scope
|
||||
prefix = "::".join(matched_segments)
|
||||
suffix = "::".join(path[i:])
|
||||
@@ -239,6 +242,15 @@ class Scope(Generic[ScopeKindT]):
|
||||
if anchor_prefix:
|
||||
return f"{anchor_prefix}::{suffix}"
|
||||
return suffix
|
||||
elif any(
|
||||
isinstance(m, FriendMember) and m.name == base_name
|
||||
for m in current_scope._members
|
||||
):
|
||||
# The name matches a friend declaration, not a real member.
|
||||
# Re-qualify from the remaining segments so the type resolves
|
||||
# to its actual definition rather than the friend's owning class.
|
||||
remaining = "::".join(path[i:])
|
||||
return self.qualify_name(remaining)
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
@@ -53,6 +53,16 @@ def qualify_type_str(type_str: str, scope: Scope) -> str:
|
||||
|
||||
return qualified_prefix + qualified_template + qualified_suffix
|
||||
|
||||
# Handle leading qualifiers (const, volatile) that prevent qualify_name
|
||||
# from matching. Strip them, qualify the rest, and prepend back.
|
||||
for qualifier in ("const ", "volatile "):
|
||||
if type_str.startswith(qualifier):
|
||||
inner = type_str[len(qualifier) :]
|
||||
qualified_inner = qualify_type_str(inner, scope)
|
||||
if qualified_inner != inner:
|
||||
return qualifier + qualified_inner
|
||||
break
|
||||
|
||||
# Try qualifying the entire string (handles simple cases without templates)
|
||||
qualified = scope.qualify_name(type_str)
|
||||
if qualified is not None:
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
struct test::ComponentDescriptor {
|
||||
public friend ShadowNode;
|
||||
public virtual void adopt(test::ShadowNode& shadowNode) const = 0;
|
||||
}
|
||||
|
||||
struct test::ShadowNode {
|
||||
}
|
||||
@@ -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
|
||||
|
||||
namespace test {
|
||||
|
||||
struct ShadowNode {};
|
||||
|
||||
struct ComponentDescriptor {
|
||||
friend ShadowNode;
|
||||
|
||||
virtual void adopt(ShadowNode &shadowNode) const = 0;
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
struct test::ComponentDescriptor {
|
||||
public friend ShadowNode;
|
||||
public virtual std::shared_ptr< const test::ShadowNode > clone() const = 0;
|
||||
}
|
||||
|
||||
struct test::ShadowNode {
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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 <memory>
|
||||
|
||||
namespace test {
|
||||
|
||||
struct ShadowNode {};
|
||||
|
||||
struct ComponentDescriptor {
|
||||
friend ShadowNode;
|
||||
|
||||
virtual std::shared_ptr<const ShadowNode> clone() const = 0;
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
Reference in new issue
Block a user