From f90612171b99e7036cb822d13b6ef34aad6c5187 Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Fri, 27 Feb 2026 06:45:35 -0800 Subject: [PATCH] Handle constexpr functions (#55801) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55801 Changelog: [Internal] Handle `constexpr` functions in the C++ API snapshot Reviewed By: javache Differential Revision: D94658128 fbshipit-source-id: 9e380de951e46dff72ba40c8726e46c01340a9a8 --- scripts/cxx-api/parser/main.py | 2 ++ scripts/cxx-api/parser/member.py | 5 +++++ .../snapshot.api | 1 + .../test.h | 14 ++++++++++++++ .../snapshot.api | 4 ++++ .../should_handle_constexpr_method/test.h | 18 ++++++++++++++++++ 6 files changed, 44 insertions(+) create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_constexpr_free_function/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_constexpr_free_function/test.h create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_constexpr_method/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_handle_constexpr_method/test.h diff --git a/scripts/cxx-api/parser/main.py b/scripts/cxx-api/parser/main.py index 97c2b31a198..341c1958328 100644 --- a/scripts/cxx-api/parser/main.py +++ b/scripts/cxx-api/parser/main.py @@ -316,6 +316,7 @@ def get_function_member( function_arg_string = function_def.get_argsstring() is_pure_virtual = function_def.get_virt() == "pure-virtual" function_virtual = function_def.get_virt() == "virtual" or is_pure_virtual + is_constexpr = function_def.constexpr == "yes" # Doxygen incorrectly merges "=0" into the return type for pure-virtual # functions using trailing return types (e.g. "auto f() -> T = 0"). @@ -333,6 +334,7 @@ def get_function_member( is_pure_virtual, is_static, doxygen_params, + is_constexpr, ) function.add_template(get_template_params(function_def)) diff --git a/scripts/cxx-api/parser/member.py b/scripts/cxx-api/parser/member.py index c61ac0cf282..6eb98cd5a04 100644 --- a/scripts/cxx-api/parser/member.py +++ b/scripts/cxx-api/parser/member.py @@ -206,11 +206,13 @@ class FunctionMember(Member): is_pure_virtual: bool, is_static: bool, doxygen_params: list[Argument] | None = None, + is_constexpr: bool = False, ) -> None: super().__init__(name, visibility) self.type: str = type self.is_virtual: bool = is_virtual self.is_static: bool = is_static + self.is_constexpr: bool = is_constexpr parsed_arguments, self.modifiers = parse_arg_string(arg_string) self.arguments = ( doxygen_params if doxygen_params is not None else parsed_arguments @@ -258,6 +260,9 @@ class FunctionMember(Member): if self.is_static: result += "static " + if self.is_constexpr: + result += "constexpr " + if self.type: result += f"{self.type} " diff --git a/scripts/cxx-api/tests/snapshots/should_handle_constexpr_free_function/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_constexpr_free_function/snapshot.api new file mode 100644 index 00000000000..c11a40ff8fe --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_constexpr_free_function/snapshot.api @@ -0,0 +1 @@ +constexpr int test::makeValue(); diff --git a/scripts/cxx-api/tests/snapshots/should_handle_constexpr_free_function/test.h b/scripts/cxx-api/tests/snapshots/should_handle_constexpr_free_function/test.h new file mode 100644 index 00000000000..47c46f7f345 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_constexpr_free_function/test.h @@ -0,0 +1,14 @@ +/* + * 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 { + +constexpr int makeValue(); + +} // namespace test diff --git a/scripts/cxx-api/tests/snapshots/should_handle_constexpr_method/snapshot.api b/scripts/cxx-api/tests/snapshots/should_handle_constexpr_method/snapshot.api new file mode 100644 index 00000000000..26c2c415c59 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_constexpr_method/snapshot.api @@ -0,0 +1,4 @@ +class test::Clss { + public constexpr int getValue() const; + public static constexpr int getDefault(); +} diff --git a/scripts/cxx-api/tests/snapshots/should_handle_constexpr_method/test.h b/scripts/cxx-api/tests/snapshots/should_handle_constexpr_method/test.h new file mode 100644 index 00000000000..47220d4cc0a --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_handle_constexpr_method/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 + +namespace test { + +class Clss { + public: + constexpr int getValue() const; + static constexpr int getDefault(); +}; + +} // namespace test