From d920d109cfb1f4259c448a91860caf46d6ea8c00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ma=C5=82ecki?= Date: Tue, 31 Mar 2026 08:32:57 -0700 Subject: [PATCH] Fix Doxygen merging base classes from primary templates into specializations (#56239) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56239 Doxygen incorrectly merges base classes from primary templates into their partial specializations. In C++, a specialization's inheritance list completely replaces the primary template's, but Doxygen combines both into a single basecompoundref list. - **Contradictory type traits** — is_optional> and is_variant_of_data_types> showed inheritance from both std::false_type (primary) and std::true_type (specialization) - **Duplicate base classes** — Converter listed ConverterBase twice (once from the primary template after substitution, once from the specialization) This diff fixes both issues with two complementary mechanisms: - **Dedup-by-name (Extendable._deduplicate_base_classes)**: removes exact duplicate base classes, keeping the last occurrence. Handles cases where Doxygen's template argument substitution produces identical names. - **Primary template base subtraction (StructLikeScopeKind._remove_merged_primary_bases)**: for partial specializations, looks up the primary template among sibling scopes and performs count-based subtraction of its bases. Count-based (rather than set-based) subtraction correctly preserves bases that a specialization explicitly re-inherits from the same class as the primary. Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D98291360 fbshipit-source-id: 82b34969b13c7df8babf2c5efc222b29979d6361 --- .../api-snapshots/ReactAndroidDebugCxx.api | 6 +-- .../api-snapshots/ReactAndroidReleaseCxx.api | 6 +-- .../api-snapshots/ReactAppleDebugCxx.api | 6 +-- .../api-snapshots/ReactAppleReleaseCxx.api | 6 +-- .../api-snapshots/ReactCommonDebugCxx.api | 6 +-- .../api-snapshots/ReactCommonReleaseCxx.api | 6 +-- scripts/cxx-api/parser/scope/extendable.py | 14 +++++++ .../parser/scope/struct_like_scope_kind.py | 40 +++++++++++++++++++ .../snapshot.api | 19 +++++++++ .../should_deduplicate_base_classes/test.h | 32 +++++++++++++++ 10 files changed, 123 insertions(+), 18 deletions(-) create mode 100644 scripts/cxx-api/tests/snapshots/should_deduplicate_base_classes/snapshot.api create mode 100644 scripts/cxx-api/tests/snapshots/should_deduplicate_base_classes/test.h diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api index ef349fa201f..b6c191e12db 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api @@ -10876,7 +10876,7 @@ struct facebook::react::detail::merge_data_types> } template -struct facebook::react::detail::is_variant_of_data_types> : public std::false_type, public std::true_type { +struct facebook::react::detail::is_variant_of_data_types> : public std::true_type { } template @@ -12473,13 +12473,13 @@ struct facebook::react::bridging::is_optional : public std::false_type { } template -struct facebook::react::bridging::Converter> : public facebook::react::bridging::ConverterBase, public facebook::react::bridging::ConverterBase { +struct facebook::react::bridging::Converter> : public facebook::react::bridging::ConverterBase { public Converter(facebook::jsi::Runtime& rt, std::optional value); public operator std::optional(); } template -struct facebook::react::bridging::is_optional> : public std::false_type, public std::true_type { +struct facebook::react::bridging::is_optional> : public std::true_type { } struct facebook::react::bridging::Converter : public facebook::react::bridging::ConverterBase { diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api index d9f9e1883b0..d645401c94d 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api @@ -10703,7 +10703,7 @@ struct facebook::react::detail::merge_data_types> } template -struct facebook::react::detail::is_variant_of_data_types> : public std::false_type, public std::true_type { +struct facebook::react::detail::is_variant_of_data_types> : public std::true_type { } template @@ -12300,13 +12300,13 @@ struct facebook::react::bridging::is_optional : public std::false_type { } template -struct facebook::react::bridging::Converter> : public facebook::react::bridging::ConverterBase, public facebook::react::bridging::ConverterBase { +struct facebook::react::bridging::Converter> : public facebook::react::bridging::ConverterBase { public Converter(facebook::jsi::Runtime& rt, std::optional value); public operator std::optional(); } template -struct facebook::react::bridging::is_optional> : public std::false_type, public std::true_type { +struct facebook::react::bridging::is_optional> : public std::true_type { } struct facebook::react::bridging::Converter : public facebook::react::bridging::ConverterBase { diff --git a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api index f8a39daaf14..18dfe51e5af 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api @@ -13329,7 +13329,7 @@ struct facebook::react::detail::merge_data_types> } template -struct facebook::react::detail::is_variant_of_data_types> : public std::false_type, public std::true_type { +struct facebook::react::detail::is_variant_of_data_types> : public std::true_type { } template @@ -14861,13 +14861,13 @@ struct facebook::react::bridging::is_optional : public std::false_type { } template -struct facebook::react::bridging::Converter> : public facebook::react::bridging::ConverterBase, public facebook::react::bridging::ConverterBase { +struct facebook::react::bridging::Converter> : public facebook::react::bridging::ConverterBase { public Converter(facebook::jsi::Runtime& rt, std::optional value); public operator std::optional(); } template -struct facebook::react::bridging::is_optional> : public std::false_type, public std::true_type { +struct facebook::react::bridging::is_optional> : public std::true_type { } struct facebook::react::bridging::Converter : public facebook::react::bridging::ConverterBase { diff --git a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api index 5892be0aaa8..ea0a658ab90 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api @@ -13156,7 +13156,7 @@ struct facebook::react::detail::merge_data_types> } template -struct facebook::react::detail::is_variant_of_data_types> : public std::false_type, public std::true_type { +struct facebook::react::detail::is_variant_of_data_types> : public std::true_type { } template @@ -14688,13 +14688,13 @@ struct facebook::react::bridging::is_optional : public std::false_type { } template -struct facebook::react::bridging::Converter> : public facebook::react::bridging::ConverterBase, public facebook::react::bridging::ConverterBase { +struct facebook::react::bridging::Converter> : public facebook::react::bridging::ConverterBase { public Converter(facebook::jsi::Runtime& rt, std::optional value); public operator std::optional(); } template -struct facebook::react::bridging::is_optional> : public std::false_type, public std::true_type { +struct facebook::react::bridging::is_optional> : public std::true_type { } struct facebook::react::bridging::Converter : public facebook::react::bridging::ConverterBase { diff --git a/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api index 551fb33b907..ba6cbfd7ffd 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api @@ -7629,7 +7629,7 @@ struct facebook::react::detail::merge_data_types> } template -struct facebook::react::detail::is_variant_of_data_types> : public std::false_type, public std::true_type { +struct facebook::react::detail::is_variant_of_data_types> : public std::true_type { } template @@ -9154,13 +9154,13 @@ struct facebook::react::bridging::is_optional : public std::false_type { } template -struct facebook::react::bridging::Converter> : public facebook::react::bridging::ConverterBase, public facebook::react::bridging::ConverterBase { +struct facebook::react::bridging::Converter> : public facebook::react::bridging::ConverterBase { public Converter(facebook::jsi::Runtime& rt, std::optional value); public operator std::optional(); } template -struct facebook::react::bridging::is_optional> : public std::false_type, public std::true_type { +struct facebook::react::bridging::is_optional> : public std::true_type { } struct facebook::react::bridging::Converter : public facebook::react::bridging::ConverterBase { diff --git a/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api index f4ccb0d3a79..b05c2e09a06 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api @@ -7620,7 +7620,7 @@ struct facebook::react::detail::merge_data_types> } template -struct facebook::react::detail::is_variant_of_data_types> : public std::false_type, public std::true_type { +struct facebook::react::detail::is_variant_of_data_types> : public std::true_type { } template @@ -9145,13 +9145,13 @@ struct facebook::react::bridging::is_optional : public std::false_type { } template -struct facebook::react::bridging::Converter> : public facebook::react::bridging::ConverterBase, public facebook::react::bridging::ConverterBase { +struct facebook::react::bridging::Converter> : public facebook::react::bridging::ConverterBase { public Converter(facebook::jsi::Runtime& rt, std::optional value); public operator std::optional(); } template -struct facebook::react::bridging::is_optional> : public std::false_type, public std::true_type { +struct facebook::react::bridging::is_optional> : public std::true_type { } struct facebook::react::bridging::Converter : public facebook::react::bridging::ConverterBase { diff --git a/scripts/cxx-api/parser/scope/extendable.py b/scripts/cxx-api/parser/scope/extendable.py index 523b55357e7..5926bb73d2f 100644 --- a/scripts/cxx-api/parser/scope/extendable.py +++ b/scripts/cxx-api/parser/scope/extendable.py @@ -25,6 +25,20 @@ class Extendable: self.base_classes.append(b) else: self.base_classes.append(base) + self._deduplicate_base_classes() + + def _deduplicate_base_classes(self) -> None: + """Remove duplicate base classes. + + Doxygen sometimes reports the same base class multiple times (e.g. + when template argument substitution produces identical names for + a primary template and its specialization). This keeps only the + last occurrence of each name. + """ + seen: dict[str, int] = {} + for i, base in enumerate(self.base_classes): + seen[base.name] = i + self.base_classes = [self.base_classes[i] for i in sorted(seen.values())] def qualify_base_classes(self, scope) -> None: """Qualify base class names and their template arguments.""" diff --git a/scripts/cxx-api/parser/scope/struct_like_scope_kind.py b/scripts/cxx-api/parser/scope/struct_like_scope_kind.py index 3050cf4414b..5ef42a0db4f 100644 --- a/scripts/cxx-api/parser/scope/struct_like_scope_kind.py +++ b/scripts/cxx-api/parser/scope/struct_like_scope_kind.py @@ -42,8 +42,48 @@ class StructLikeScopeKind(ScopeKind, Extendable): self.template_list.add(template) def close(self, scope: Scope) -> None: + self._remove_merged_primary_bases(scope) self.qualify_base_classes(scope) + def _remove_merged_primary_bases(self, scope: Scope) -> None: + """Remove base classes that Doxygen incorrectly merged from the + primary template into a partial specialization. + + In C++ a partial specialization's inheritance list completely + replaces the primary template's, but Doxygen merges both lists + into the specialization's ``basecompoundref`` elements. + + This method performs count-based subtraction: for each base in + the primary template, one matching occurrence (by name) is removed + from this specialization's base list. Count-based subtraction + correctly preserves bases that the specialization explicitly + re-inherits from the same class as the primary template. + """ + if self.specialization_args is None: + return + if scope.parent_scope is None: + return + + for sibling in scope.parent_scope.inner_scopes.values(): + if ( + sibling is not scope + and sibling.name == scope.name + and isinstance(sibling.kind, StructLikeScopeKind) + and sibling.kind.specialization_args is None + ): + primary_base_counts: dict[str, int] = {} + for b in sibling.kind.base_classes: + primary_base_counts[b.name] = primary_base_counts.get(b.name, 0) + 1 + + result = [] + for b in self.base_classes: + if primary_base_counts.get(b.name, 0) > 0: + primary_base_counts[b.name] -= 1 + else: + result.append(b) + self.base_classes = result + break + def to_string(self, scope: Scope) -> str: result = "" diff --git a/scripts/cxx-api/tests/snapshots/should_deduplicate_base_classes/snapshot.api b/scripts/cxx-api/tests/snapshots/should_deduplicate_base_classes/snapshot.api new file mode 100644 index 00000000000..93ede796655 --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_deduplicate_base_classes/snapshot.api @@ -0,0 +1,19 @@ +struct test::BaseA { +} + +struct test::Derived : public test::BaseA { +} + +struct test::false_type { +} + +struct test::true_type { +} + +template +struct test::is_special : public test::true_type { +} + +template +struct test::is_special : public test::false_type { +} diff --git a/scripts/cxx-api/tests/snapshots/should_deduplicate_base_classes/test.h b/scripts/cxx-api/tests/snapshots/should_deduplicate_base_classes/test.h new file mode 100644 index 00000000000..ce614aa70fe --- /dev/null +++ b/scripts/cxx-api/tests/snapshots/should_deduplicate_base_classes/test.h @@ -0,0 +1,32 @@ +/* + * 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 false_type {}; +struct true_type {}; + +// Primary template inherits from false_type. +// Partial specialization inherits from true_type. +// Doxygen may incorrectly merge both base class lists into the +// specialization, producing : false_type, true_type. The parser +// should subtract the primary template's bases and keep only true_type. + +template +struct is_special : public false_type {}; + +template +struct is_special : public true_type {}; + +// Duplicate base class via Doxygen merging template-substituted bases. +struct BaseA {}; + +struct Derived : public BaseA, public BaseA {}; + +} // namespace test