From f9db851a8dad16032210edf92fa8be8b659e9faa Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Wed, 25 Feb 2026 01:12:30 -0800 Subject: [PATCH] Handle concepts in the C++ API snapshot (#55695) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55695 Changelog: [Internal] Adds support for handling concepts in the C++ API parser. Reviewed By: NickGerleman Differential Revision: D94074054 fbshipit-source-id: 5e8ef0b0d4d1eeb93554558bb7f80cd67d622a72 --- scripts/cxx-api/parser/main.py | 76 ++++++++++++++++- scripts/cxx-api/parser/member.py | 85 +++++++++++++++++++ .../should_handle_concept/snapshot.api | 4 + .../snapshots/should_handle_concept/test.h | 22 +++++ .../should_handle_concept_indent/snapshot.api | 9 ++ .../should_handle_concept_indent/test.h | 31 +++++++ 6 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_concept/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_concept/test.h create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_concept_indent/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_concept_indent/test.h diff --git a/scripts/cxx-api/parser/main.py b/scripts/cxx-api/parser/main.py index 3fc3aaabec4..40fce52d7d6 100644 --- a/scripts/cxx-api/parser/main.py +++ b/scripts/cxx-api/parser/main.py @@ -10,10 +10,17 @@ from pprint import pprint from doxmlparser import compound, index -from .member import EnumMember, FunctionMember, TypedefMember, VariableMember +from .member import ( + ConceptMember, + EnumMember, + FunctionMember, + TypedefMember, + VariableMember, +) from .scope import StructLikeScopeKind from .snapshot import Snapshot from .template import Template +from .utils import parse_qualified_path def resolve_ref_text_name(type_def: compound.refTextType) -> str: @@ -59,6 +66,34 @@ def resolve_linked_text_name(type_def: compound.linkedTextType) -> str: return name.strip() +def resolve_linked_text_full(type_def: compound.linkedTextType) -> str: + """ + Resolve the full text content of a linkedTextType, including all text + fragments and ref elements. + + Unlike resolve_linked_text_name which only gets the first ref or value, + this function concatenates all content_ items to reconstruct the full text. + """ + if not type_def.content_: + # Fall back to valueOf_ if no content_ list + return type_def.get_valueOf_() or "" + + result = [] + for item in type_def.content_: + if item.category == 1: # MixedContainer.CategoryText + result.append(item.value) + elif item.category == 3: # MixedContainer.CategoryComplex (ref element) + # For ref elements, get the text content + if hasattr(item.value, "get_valueOf_"): + result.append(item.value.get_valueOf_()) + elif hasattr(item.value, "valueOf_"): + result.append(item.value.valueOf_) + else: + result.append(str(item.value)) + + return "".join(result) + + def get_base_classes( compound_object: compound.CompounddefType, ) -> [StructLikeScopeKind.Base]: @@ -226,6 +261,33 @@ def get_typedef_member( return typedef +def get_concept_member( + concept_def: compound.CompounddefType, +) -> ConceptMember: + """ + Get the concept member from a compound definition. + """ + concept_name = concept_def.compoundname + concept_path = parse_qualified_path(concept_name) + unqualified_name = concept_path[-1] + + initializer = concept_def.initializer + constraint = "" + + if initializer: + # The initializer contains the entire constraind definition. + # We want to extract the constraint part after "=" + initializer_text = resolve_linked_text_full(initializer) + eq_pos = initializer_text.find("=") + if eq_pos != -1: + constraint = initializer_text[eq_pos + 1 :].strip() + + concept = ConceptMember(unqualified_name, constraint) + concept.add_template(get_template_params(concept_def)) + + return concept + + def create_enum_scope(snapshot: Snapshot, enum_def: compound.EnumdefType): """ Create an enum scope in the snapshot. @@ -379,6 +441,18 @@ def build_snapshot(xml_dir: str) -> Snapshot: print( f"Unknown section kind: {section_def.kind} in {compound_object.location.file}" ) + elif compound_object.kind == "concept": + # Concepts belong to a namespace, so we need to find or create the parent namespace + concept_name = compound_object.compoundname + concept_path = parse_qualified_path(concept_name) + namespace_path = "::".join(concept_path[:-1]) if concept_path else "" + + if namespace_path: + namespace_scope = snapshot.create_or_get_namespace(namespace_path) + else: + namespace_scope = snapshot.root_scope + + namespace_scope.add_member(get_concept_member(compound_object)) elif compound_object.kind == "file": pass elif compound_object.kind == "dir": diff --git a/scripts/cxx-api/parser/member.py b/scripts/cxx-api/parser/member.py index d7f4b1917c5..7bfddf8062e 100644 --- a/scripts/cxx-api/parser/member.py +++ b/scripts/cxx-api/parser/member.py @@ -614,3 +614,88 @@ class TypedefMember(Member): result += f" {self.type} {name};" return result + + +class ConceptMember(Member): + def __init__( + self, + name: str, + constraint: str, + ) -> None: + super().__init__(name, "public") + self.constraint: str = self._normalize_constraint(constraint) + + @staticmethod + def _normalize_constraint(constraint: str) -> str: + """ + Normalize the whitespace in a concept constraint expression. + + Doxygen preserves original source indentation, which becomes + inconsistent when we flatten namespaces and use qualified names. + This method normalizes the indentation by dedenting all lines + to the minimum non-empty indentation level. + """ + if not constraint: + return constraint + + lines = constraint.split("\n") + if len(lines) <= 1: + return constraint.strip() + + # Find minimum indentation (excluding the first line and empty lines) + min_indent = float("inf") + for line in lines[1:]: + stripped = line.lstrip() + if stripped: # Skip empty lines + indent = len(line) - len(stripped) + min_indent = min(min_indent, indent) + + if min_indent == float("inf"): + min_indent = 0 + + # Dedent all lines by the minimum indentation + result_lines = [lines[0].strip()] + for line in lines[1:]: + if line.strip(): # Non-empty line + # Remove the minimum indentation to normalize + dedented = ( + line[int(min_indent) :] + if len(line) >= min_indent + else line.lstrip() + ) + result_lines.append(dedented.rstrip()) + else: + result_lines.append("") + + # Check if no line is indented + if all(not line.startswith(" ") for line in result_lines): + # Re-indent all lines but the first by 2 spaces + not_indented = result_lines + result_lines = [not_indented[0]] + for line in not_indented[1:]: + if line.strip(): # Non-empty line + result_lines.append(" " + line) + else: + result_lines.append("") + + return "\n".join(result_lines) + + def close(self, scope: Scope): + # TODO: handle unqualified references + pass + + def to_string( + self, + indent: int = 0, + qualification: str | None = None, + hide_visibility: bool = False, + ) -> str: + name = self._get_qualified_name(qualification) + result = "" + + if self.template_list is not None: + result += " " * indent + self.template_list.to_string() + "\n" + + result += " " * indent + f"concept {name} = {self.constraint};" + + return result diff --git a/scripts/cxx-api/tests/snapshots/should_handle_concept/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_concept/snapshot.api new file mode 100644 index 00000000000..1b3a474c3a0 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_concept/snapshot.api @@ -0,0 +1,4 @@ +template +concept test::CSSParserSink = requires(CSSSyntaxParser &parser) { + { T::consume(parser) } -> std::convertible_to; +}; diff --git a/scripts/cxx-api/tests/snapshots/should_handle_concept/test.h b/scripts/cxx-api/tests/snapshots/should_handle_concept/test.h new file mode 100644 index 00000000000..e63f8a450ba --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_concept/test.h @@ -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 +#include + +namespace test { + +class CSSSyntaxParser; + +template +concept CSSParserSink = requires(CSSSyntaxParser &parser) { + { T::consume(parser) } -> std::convertible_to; +}; + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_concept_indent/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_concept_indent/snapshot.api new file mode 100644 index 00000000000..8ed0e695f88 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_concept_indent/snapshot.api @@ -0,0 +1,9 @@ +template +concept test::CSSUniqueComponentValueVisitors = CSSSyntaxVisitorReturn && (CSSComponentValueVisitor && ...) && + ((CSSFunctionVisitor ? 1 : 0) + ... + 0) <= 1 && + ((CSSPreservedTokenVisitor ? 1 : 0) + ... + 0) <= 1 && + ((CSSSimpleBlockVisitor ? 1 : 0) + ... + 0) <= 1; +template +concept test::CSSParserSink = requires(CSSSyntaxParser &parser) { + { T::consume(parser) } -> std::convertible_to; +}; diff --git a/scripts/cxx-api/tests/snapshots/should_handle_concept_indent/test.h b/scripts/cxx-api/tests/snapshots/should_handle_concept_indent/test.h new file mode 100644 index 00000000000..738b9408491 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_concept_indent/test.h @@ -0,0 +1,31 @@ +/* + * 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 { + +class CSSSyntaxParser; + +template +// clang-format off +concept CSSParserSink = requires(CSSSyntaxParser &parser) { + { T::consume(parser) } -> std::convertible_to; +}; +// clang-format on + +template +concept CSSUniqueComponentValueVisitors = + CSSSyntaxVisitorReturn && (CSSComponentValueVisitor && ...) && + ((CSSFunctionVisitor ? 1 : 0) + ... + 0) <= 1 && + ((CSSPreservedTokenVisitor ? 1 : 0) + ... + 0) <= 1 && + ((CSSSimpleBlockVisitor ? 1 : 0) + ... + 0) <= 1; + +} // namespace test