Skip to content

Commit 7f6790b

Browse files
j-piaseckimeta-codesync[bot]
authored andcommitted
Inline private type aliases in the API snapshot (facebook#56030)
Summary: Pull Request resolved: facebook#56030 Changelog: [Internal] Currently, the parser ignores all private sections, which causes private type aliases definitions not to be present in the snapshot. The refereces to those types can still appear in the public API, if they are resolvable to public types. This diff updates the parser to track private type definitions, and inline them when referenced from the public API. Reviewed By: cortinico Differential Revision: D95920125 fbshipit-source-id: 054786987c266ac1062c0fa3ba1631cb179fb6ef
1 parent 63800af commit 7f6790b

5 files changed

Lines changed: 72 additions & 4 deletions

File tree

scripts/cxx-api/parser/builders.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,11 @@ def _process_objc_sections(
465465
member_type = parts[-1]
466466

467467
if visibility == "private":
468-
pass
468+
if member_type == "type":
469+
for member_def in section_def.memberdef:
470+
if member_def.kind == "typedef":
471+
typedef_member = get_typedef_member(member_def, visibility)
472+
scope.add_private_typedef(typedef_member)
469473
elif visibility in ("public", "protected"):
470474
if member_type == "attrib":
471475
for member_def in section_def.memberdef:
@@ -611,7 +615,11 @@ def create_class_scope(
611615
member_type = parts[-1]
612616

613617
if visibility == "private":
614-
pass
618+
if member_type == "type":
619+
for member_def in section_def.memberdef:
620+
if member_def.kind == "typedef":
621+
typedef_member = get_typedef_member(member_def, visibility)
622+
class_scope.add_private_typedef(typedef_member)
615623
elif visibility in ("public", "protected"):
616624
if member_type == "attrib":
617625
for member_def in section_def.memberdef:

scripts/cxx-api/parser/member.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,19 @@ def _is_function_pointer(self) -> bool:
334334
"""Check if this typedef is a function pointer type."""
335335
return self.argstring is not None and self.argstring.startswith(")(")
336336

337+
def get_value(self) -> str:
338+
if self.keyword == "using":
339+
return format_parsed_type(self._parsed_type)
340+
elif self._is_function_pointer():
341+
formatted_args = format_arguments(self._fp_arguments)
342+
qualified_type = format_parsed_type(self._parsed_type)
343+
if "(*" in qualified_type:
344+
return f"{qualified_type})({formatted_args})"
345+
else:
346+
return f"{qualified_type}(*)({formatted_args})"
347+
else:
348+
return self.type
349+
337350
def to_string(
338351
self,
339352
indent: int = 0,

scripts/cxx-api/parser/scope.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from natsort import natsort_keygen, natsorted
1313

14-
from .member import FriendMember, Member, MemberKind
14+
from .member import FriendMember, Member, MemberKind, TypedefMember
1515
from .template import Template, TemplateList
1616
from .utils import parse_qualified_path, qualify_template_args_only, qualify_type_str
1717

@@ -311,8 +311,9 @@ def __init__(self, kind: ScopeKindT, name: str | None = None) -> None:
311311
self.kind: ScopeKindT = kind
312312
self.parent_scope: Scope | None = None
313313
self.inner_scopes: dict[str, Scope] = {}
314-
self._members: list[Member] = []
315314
self.location: str | None = None
315+
self._members: list[Member] = []
316+
self._private_typedefs: dict[str, TypedefMember] = {}
316317

317318
def get_qualified_name(self) -> str:
318319
"""
@@ -372,6 +373,10 @@ def qualify_name(self, name: str | None) -> str | None:
372373
prefix = current_scope.get_qualified_name()
373374
return f"{prefix}::{name}" if prefix else name
374375

376+
# Check private typedefs: substitute with the expanded definition
377+
if len(path) == 1 and base_first in current_scope._private_typedefs:
378+
return current_scope._private_typedefs[base_first].get_value()
379+
375380
current_scope = current_scope.parent_scope
376381

377382
if current_scope is None:
@@ -423,6 +428,15 @@ def qualify_name(self, name: str | None) -> str | None:
423428
else:
424429
return "::".join(matched_segments)
425430

431+
def add_private_typedef(self, member: TypedefMember) -> None:
432+
"""
433+
Store a private typedef for use during type resolution.
434+
435+
Private typedefs are not included in the snapshot output, but their
436+
definitions are substituted for references to them in public members.
437+
"""
438+
self._private_typedefs[member.name] = member
439+
426440
def add_member(self, member: Member | None) -> None:
427441
"""
428442
Add a member to the scope.
@@ -441,6 +455,9 @@ def close(self) -> None:
441455
"""
442456
Close the scope by setting the kind of all temporary scopes.
443457
"""
458+
for typedef in self._private_typedefs.values():
459+
typedef.close(self)
460+
444461
for member in self.get_members():
445462
member.close(self)
446463

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
template <typename... ArgumentT>
2+
class test::Clss {
3+
public void test1(std::function<void(ArgumentT...)>&& function);
4+
public void test2(int val) const;
5+
public void test3(void(*)(int) val) const;
6+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
namespace test {
11+
12+
template <typename... ArgumentT>
13+
class Clss {
14+
using T = void(ArgumentT...);
15+
typedef int MyType;
16+
typedef void (*MyFunction)(MyType);
17+
18+
public:
19+
void test1(std::function<T> &&function);
20+
void test2(MyType val) const;
21+
void test3(MyFunction val) const;
22+
};
23+
24+
} // namespace test

0 commit comments

Comments
 (0)