mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Handle enums in C++ API snapshot (#55691)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55691 Changelog: [Internal] Adds support for handling enums in the C++ API parser. Reviewed By: cipolleschi Differential Revision: D94073960 fbshipit-source-id: 2c726f5ac5c33c66950463760e2592064d812400
This commit is contained in:
1 parent
25e402587a
commit
e69c08bf04
14 files changed
+229
-2
No files matched your search
@@ -10,7 +10,7 @@ from pprint import pprint
|
||||
|
||||
from doxmlparser import compound, index
|
||||
|
||||
from .member import VariableMember
|
||||
from .member import EnumMember, VariableMember
|
||||
from .scope import StructLikeScopeKind
|
||||
from .snapshot import Snapshot
|
||||
|
||||
@@ -138,6 +138,29 @@ def get_variable_member(
|
||||
)
|
||||
|
||||
|
||||
def create_enum_scope(snapshot: Snapshot, enum_def: compound.EnumdefType):
|
||||
"""
|
||||
Create an enum scope in the snapshot.
|
||||
"""
|
||||
scope = snapshot.create_enum(enum_def.qualifiedname)
|
||||
scope.kind.type = resolve_ref_text_name(enum_def.get_type())
|
||||
scope.location = enum_def.location.file
|
||||
|
||||
for enum_value_def in enum_def.enumvalue:
|
||||
value_name = enum_value_def.get_name()
|
||||
value_value = None
|
||||
|
||||
if enum_value_def.initializer is not None:
|
||||
value_value = resolve_linked_text_name(enum_value_def.initializer)
|
||||
|
||||
scope.add_member(
|
||||
EnumMember(
|
||||
value_name,
|
||||
value_value,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def build_snapshot(xml_dir: str) -> Snapshot:
|
||||
"""
|
||||
Reads the Doxygen XML output and builds a snapshot of the C++ API.
|
||||
@@ -199,6 +222,14 @@ def build_snapshot(xml_dir: str) -> Snapshot:
|
||||
member_def, visibility, is_static
|
||||
)
|
||||
)
|
||||
elif member_type == "type":
|
||||
for member_def in section_def.memberdef:
|
||||
if member_def.kind == "enum":
|
||||
create_enum_scope(snapshot, member_def)
|
||||
else:
|
||||
print(
|
||||
f"Unknown section member kind: {member_def.kind} in {compound_object.location.file}"
|
||||
)
|
||||
else:
|
||||
print(
|
||||
f"Unknown class section kind: {kind} in {compound_object.location.file}"
|
||||
@@ -228,6 +259,9 @@ def build_snapshot(xml_dir: str) -> Snapshot:
|
||||
namespace_scope.add_member(
|
||||
get_variable_member(variable_def, "public", is_static)
|
||||
)
|
||||
elif section_def.kind == "enum":
|
||||
for enum_def in section_def.memberdef:
|
||||
create_enum_scope(snapshot, enum_def)
|
||||
else:
|
||||
print(
|
||||
f"Unknown section kind: {section_def.kind} in {compound_object.location.file}"
|
||||
|
||||
@@ -35,6 +35,25 @@ class Member(ABC):
|
||||
return f"{qualification}::{self.name}" if qualification else self.name
|
||||
|
||||
|
||||
class EnumMember(Member):
|
||||
def __init__(self, name: str, value: str | None) -> None:
|
||||
super().__init__(name, "public")
|
||||
self.value: str | None = value
|
||||
|
||||
def to_string(
|
||||
self,
|
||||
indent: int = 0,
|
||||
qualification: str | None = None,
|
||||
hide_visibility: bool = False,
|
||||
) -> str:
|
||||
name = self._get_qualified_name(qualification)
|
||||
|
||||
if self.value is None:
|
||||
return " " * indent + f"{name}"
|
||||
|
||||
return " " * indent + f"{name} = {self.value}"
|
||||
|
||||
|
||||
class VariableMember(Member):
|
||||
def __init__(
|
||||
self,
|
||||
|
||||
@@ -99,6 +99,33 @@ class NamespaceScopeKind(ScopeKind):
|
||||
return "\n".join(result)
|
||||
|
||||
|
||||
class EnumScopeKind(ScopeKind):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("enum")
|
||||
self.type: str | None = None
|
||||
|
||||
def to_string(self, scope: Scope) -> str:
|
||||
result = ""
|
||||
inheritance_string = f" : {self.type}" if self.type else ""
|
||||
|
||||
result += (
|
||||
"\n" + f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{"
|
||||
)
|
||||
|
||||
stringified_members = []
|
||||
for member in scope.get_members():
|
||||
stringified_members.append(member.to_string(2) + ",")
|
||||
|
||||
stringified_members = natsorted(stringified_members)
|
||||
result += ("\n" if len(stringified_members) > 0 else "") + "\n".join(
|
||||
stringified_members
|
||||
)
|
||||
|
||||
result += "\n}"
|
||||
|
||||
return result
|
||||
|
||||
|
||||
class TemporaryScopeKind(ScopeKind):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("temporary")
|
||||
|
||||
@@ -5,7 +5,13 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from .scope import NamespaceScopeKind, Scope, StructLikeScopeKind, TemporaryScopeKind
|
||||
from .scope import (
|
||||
EnumScopeKind,
|
||||
NamespaceScopeKind,
|
||||
Scope,
|
||||
StructLikeScopeKind,
|
||||
TemporaryScopeKind,
|
||||
)
|
||||
from .utils import parse_qualified_path
|
||||
|
||||
|
||||
@@ -78,6 +84,30 @@ class Snapshot:
|
||||
current_scope.inner_scopes[namespace_name] = new_scope
|
||||
return new_scope
|
||||
|
||||
def create_enum(self, qualified_name: str) -> Scope[EnumScopeKind]:
|
||||
"""
|
||||
Create an enum in the snapshot.
|
||||
"""
|
||||
path = parse_qualified_path(qualified_name)
|
||||
scope_path = path[0:-1]
|
||||
enum_name = path[-1]
|
||||
current_scope = self.ensure_scope(scope_path)
|
||||
|
||||
if enum_name in current_scope.inner_scopes:
|
||||
scope = current_scope.inner_scopes[enum_name]
|
||||
if scope.kind.name == "temporary":
|
||||
scope.kind = EnumScopeKind()
|
||||
else:
|
||||
raise RuntimeError(
|
||||
f"Identifier {enum_name} already exists in scope {current_scope.name}"
|
||||
)
|
||||
return scope
|
||||
else:
|
||||
new_scope = Scope(EnumScopeKind(), enum_name)
|
||||
new_scope.parent_scope = current_scope
|
||||
current_scope.inner_scopes[enum_name] = new_scope
|
||||
return new_scope
|
||||
|
||||
def _ensure_scope_is_defined(self, scope: Scope) -> None:
|
||||
"""
|
||||
Ensure that a scope is defined in the snapshot.
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
class test::Outer {
|
||||
}
|
||||
|
||||
enum test::Outer::InnerEnum {
|
||||
Value1,
|
||||
Value2,
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
class Outer {
|
||||
public:
|
||||
enum class InnerEnum {
|
||||
Value1,
|
||||
Value2,
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
@@ -0,0 +1,4 @@
|
||||
enum test::Enum {
|
||||
Value1,
|
||||
Value2,
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
enum class Enum {
|
||||
Value1,
|
||||
Value2,
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
@@ -0,0 +1,4 @@
|
||||
enum test::Enum : int {
|
||||
Value1 = 1,
|
||||
Value2 = 2,
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
enum class Enum : int {
|
||||
Value1 = 1,
|
||||
Value2 = 2,
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
@@ -0,0 +1,7 @@
|
||||
enum test::TestStruct::Enum {
|
||||
Value1,
|
||||
Value2,
|
||||
}
|
||||
|
||||
struct test::TestStruct {
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
struct TestStruct {
|
||||
enum class Enum {
|
||||
Value1,
|
||||
Value2,
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
@@ -0,0 +1,2 @@
|
||||
struct test::TestStruct {
|
||||
}
|
||||
@@ -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 TestStruct {
|
||||
private:
|
||||
enum class PrivateEnum {
|
||||
Value1,
|
||||
Value2,
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
Reference in new issue
Block a user