From 5e4da8410ec960442f8666ecbf0e6a4582aaca04 Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Wed, 25 Feb 2026 11:16:36 -0800 Subject: [PATCH] Fix handling of unnamed template arguments (#55646) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55646 Changelog: [Internal] Updates the C++ API snapshot generator script to correctly handle unnamed template arguments. Reviewed By: cipolleschi Differential Revision: D93407624 fbshipit-source-id: 50928b596fe64fa3549cde5d69da7146b452d784 --- scripts/cxx-api/parser/main.py | 12 ++++++-- scripts/cxx-api/parser/template.py | 20 ++++++++++++- .../snapshot.api | 3 ++ .../test.h | 18 +++++++++++ .../snapshot.api | 16 ++++++++++ .../test.h | 30 +++++++++++++++++++ 6 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_unnamed_template_param/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_unnamed_template_param/test.h create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_unnamed_template_param_with_default_value/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_unnamed_template_param_with_default_value/test.h diff --git a/scripts/cxx-api/parser/main.py b/scripts/cxx-api/parser/main.py index 511e1dec79c..1eb33690966 100644 --- a/scripts/cxx-api/parser/main.py +++ b/scripts/cxx-api/parser/main.py @@ -152,8 +152,16 @@ def get_template_params( template_type = " ".join(parts[:-1]) template_name = parts[-1] elif len(parts) == 1: - # Just a name like "T" with no type keyword - template_name = parts[0] + # Check if this is an unnamed template parameter + # (just "typename" or "class" with or without a default value) + # In this case, we leave name as None/empty + if parts[0] in ("typename", "class"): + # Unnamed template parameter + # e.g., "typename" or "typename = std::enable_if_t<...>" + template_name = None + else: + # Just a name like "T" with no type keyword + template_name = parts[0] template_params.append( Template(template_type, template_name, template_value) diff --git a/scripts/cxx-api/parser/template.py b/scripts/cxx-api/parser/template.py index b05f1bb8048..910f4963d11 100644 --- a/scripts/cxx-api/parser/template.py +++ b/scripts/cxx-api/parser/template.py @@ -3,14 +3,32 @@ # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. +import re + + +def _normalize_whitespace(text: str) -> str: + """ + Normalize whitespace by collapsing multiple consecutive whitespace + characters into a single space. + """ + return re.sub(r"\s+", " ", text).strip() + class Template: def __init__(self, type: str, name: str, value: str | None) -> None: self.type: str = type self.name: str = name - self.value: str | None = value + self.value: str | None = _normalize_whitespace(value) if value else value def to_string(self) -> str: + # Handle unnamed template parameters (e.g., "typename = std::enable_if_t<...>") + # When the name is empty or None, we just output "type = value" or "type" + if not self.name: + if self.value is None: + return self.type + else: + return f"{self.type} = {self.value}" + if self.value is None: return f"{self.type} {self.name}" else: diff --git a/scripts/cxx-api/tests/snapshots/should_handle_unnamed_template_param/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_unnamed_template_param/snapshot.api new file mode 100644 index 00000000000..ae63c8679cb --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_unnamed_template_param/snapshot.api @@ -0,0 +1,3 @@ +template +struct test::is_optional : public std::false_type { +} diff --git a/scripts/cxx-api/tests/snapshots/should_handle_unnamed_template_param/test.h b/scripts/cxx-api/tests/snapshots/should_handle_unnamed_template_param/test.h new file mode 100644 index 00000000000..ab231f41540 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_unnamed_template_param/test.h @@ -0,0 +1,18 @@ +/* + * 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 + +namespace test { + +// Unnamed template parameter without default value +template +struct is_optional : std::false_type {}; + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_unnamed_template_param_with_default_value/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_unnamed_template_param_with_default_value/snapshot.api new file mode 100644 index 00000000000..df4d0840cb8 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_unnamed_template_param_with_default_value/snapshot.api @@ -0,0 +1,16 @@ +class test::BigInt { +} + +class test::Object { +} + +class test::String { +} + +class test::Symbol { +} + +struct test::Value { + template ::value || std::is_base_of::value || std::is_base_of::value || std::is_base_of::value>> + public Value(T && other); +} diff --git a/scripts/cxx-api/tests/snapshots/should_handle_unnamed_template_param_with_default_value/test.h b/scripts/cxx-api/tests/snapshots/should_handle_unnamed_template_param_with_default_value/test.h new file mode 100644 index 00000000000..c11aa19d391 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_unnamed_template_param_with_default_value/test.h @@ -0,0 +1,30 @@ +/* + * 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 + +namespace test { + +class Symbol {}; +class BigInt {}; +class String {}; +class Object {}; + +struct Value { + template < + typename T, + typename = std::enable_if_t< + std::is_base_of::value || std::is_base_of::value || std::is_base_of::value || + std::is_base_of::value>> + /* implicit */ Value(T &&other) + { + } +}; + +} // namespace test