From 74f6da4d26173088de178365678891105393799e Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Fri, 19 Jul 2024 14:42:20 -0700 Subject: [PATCH] Switch C++ extension identifier to constexpr. Add static_assert test for ExtensionNumber API. PiperOrigin-RevId: 654129635 --- hpb/hpb.h | 9 +++-- hpb_generator/gen_extensions.cc | 50 ++++++++------------------ hpb_generator/gen_extensions.h | 8 +---- hpb_generator/gen_messages.cc | 21 +---------- hpb_generator/protoc-gen-upb-protos.cc | 5 ++- hpb_generator/tests/test_generated.cc | 2 ++ 6 files changed, 24 insertions(+), 71 deletions(-) diff --git a/hpb/hpb.h b/hpb/hpb.h index 7ae1b605e8..9e18057638 100644 --- a/hpb/hpb.h +++ b/hpb/hpb.h @@ -204,13 +204,12 @@ class ExtensionIdentifier : public ExtensionMiniTableProvider { using Extendee = ExtendeeType; constexpr explicit ExtensionIdentifier( - const upb_MiniTableExtension* mini_table_ext) - : ExtensionMiniTableProvider(mini_table_ext) {} + uint32_t number, const upb_MiniTableExtension* mini_table_ext) + : ExtensionMiniTableProvider(mini_table_ext), number_(number) {} private: - constexpr uint32_t number() const { - return upb_MiniTableExtension_Number(mini_table_ext()); - } + uint32_t number_; + constexpr uint32_t number() const { return number_; } friend class PrivateAccess; }; diff --git a/hpb_generator/gen_extensions.cc b/hpb_generator/gen_extensions.cc index d0c54eb36d..1bfe74b555 100644 --- a/hpb_generator/gen_extensions.cc +++ b/hpb_generator/gen_extensions.cc @@ -7,9 +7,14 @@ #include "google/protobuf/compiler/hpb/gen_extensions.h" +#include +#include +#include + #include "absl/strings/str_cat.h" -#include "google/protobuf/compiler/hpb/gen_utils.h" #include "google/protobuf/compiler/hpb/names.h" +#include "google/protobuf/compiler/hpb/output.h" +#include "google/protobuf/descriptor.h" namespace google::protobuf::hpb_generator { @@ -38,55 +43,28 @@ void WriteExtensionIdentifierHeader(const protobuf::FieldDescriptor* ext, if (ext->extension_scope()) { output( R"cc( - static const ::protos::internal::ExtensionIdentifier<$0, $1> $2; - )cc", - ContainingTypeName(ext), CppTypeParameterName(ext), ext->name()); - } else { - output( - R"cc( - extern const ::protos::internal::ExtensionIdentifier<$0, $1> $2; - )cc", - ContainingTypeName(ext), CppTypeParameterName(ext), ext->name()); - } -} - -void WriteExtensionIdentifiersHeader( - const std::vector& extensions, - Output& output) { - for (const auto* ext : extensions) { - if (!ext->extension_scope()) { - WriteExtensionIdentifierHeader(ext, output); - } - } -} - -void WriteExtensionIdentifier(const protobuf::FieldDescriptor* ext, - Output& output) { - std::string mini_table_name = - absl::StrCat(ExtensionIdentifierBase(ext), "_", ext->name(), "_ext"); - if (ext->extension_scope()) { - output( - R"cc( - const ::protos::internal::ExtensionIdentifier<$0, $3> $4::$2(&$1); + static constexpr ::protos::internal::ExtensionIdentifier<$0, $3> $2{ + $4, &$1}; )cc", ContainingTypeName(ext), mini_table_name, ext->name(), - CppTypeParameterName(ext), ClassName(ext->extension_scope())); + CppTypeParameterName(ext), ext->number()); } else { output( R"cc( - const ::protos::internal::ExtensionIdentifier<$0, $3> $2(&$1); + inline constexpr ::protos::internal::ExtensionIdentifier<$0, $3> $2{ + $4, &$1}; )cc", ContainingTypeName(ext), mini_table_name, ext->name(), - CppTypeParameterName(ext)); + CppTypeParameterName(ext), ext->number()); } } -void WriteExtensionIdentifiers( +void WriteExtensionIdentifiersHeader( const std::vector& extensions, Output& output) { for (const auto* ext : extensions) { if (!ext->extension_scope()) { - WriteExtensionIdentifier(ext, output); + WriteExtensionIdentifierHeader(ext, output); } } } diff --git a/hpb_generator/gen_extensions.h b/hpb_generator/gen_extensions.h index e48f657e8d..1d2e932c43 100644 --- a/hpb_generator/gen_extensions.h +++ b/hpb_generator/gen_extensions.h @@ -8,8 +8,8 @@ #ifndef PROTOBUF_COMPILER_HBP_GEN_EXTENSIONS_H_ #define PROTOBUF_COMPILER_HBP_GEN_EXTENSIONS_H_ -#include "google/protobuf/descriptor.h" #include "google/protobuf/compiler/hpb/output.h" +#include "google/protobuf/descriptor.h" namespace google::protobuf::hpb_generator { @@ -20,12 +20,6 @@ void WriteExtensionIdentifiersHeader( Output& output); void WriteExtensionIdentifierHeader(const protobuf::FieldDescriptor* ext, Output& output); -void WriteExtensionIdentifiers( - const std::vector& extensions, - Output& output); -void WriteExtensionIdentifier(const protobuf::FieldDescriptor* ext, - Output& output); - } // namespace protobuf } // namespace google::hpb_generator diff --git a/hpb_generator/gen_messages.cc b/hpb_generator/gen_messages.cc index 75e486ed24..d2576743c4 100644 --- a/hpb_generator/gen_messages.cc +++ b/hpb_generator/gen_messages.cc @@ -15,15 +15,14 @@ #include "absl/strings/ascii.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" -#include "google/protobuf/descriptor.h" #include "google/protobuf/compiler/hpb/gen_accessors.h" #include "google/protobuf/compiler/hpb/gen_enums.h" #include "google/protobuf/compiler/hpb/gen_extensions.h" #include "google/protobuf/compiler/hpb/gen_utils.h" #include "google/protobuf/compiler/hpb/names.h" #include "google/protobuf/compiler/hpb/output.h" +#include "google/protobuf/descriptor.h" #include "upb_generator/common.h" -#include "upb_generator/file_layout.h" namespace google::protobuf::hpb_generator { @@ -48,10 +47,6 @@ void WriteInternalForwardDeclarationsInHeader( const protobuf::Descriptor* message, Output& output); void WriteDefaultInstanceHeader(const protobuf::Descriptor* message, Output& output); -void WriteExtensionIdentifiersImplementation( - const protobuf::Descriptor* message, - const std::vector& file_exts, - Output& output); void WriteUsingEnumsInHeader( const protobuf::Descriptor* message, const std::vector& file_enums, @@ -457,8 +452,6 @@ void WriteMessageImplementation( } )cc", ClassName(descriptor)); - - WriteExtensionIdentifiersImplementation(descriptor, file_exts, output); } } @@ -486,18 +479,6 @@ void WriteExtensionIdentifiersInClassHeader( } } -void WriteExtensionIdentifiersImplementation( - const protobuf::Descriptor* message, - const std::vector& file_exts, - Output& output) { - for (auto* ext : file_exts) { - if (ext->extension_scope() && - ext->extension_scope()->full_name() == message->full_name()) { - WriteExtensionIdentifier(ext, output); - } - } -} - void WriteUsingEnumsInHeader( const protobuf::Descriptor* message, const std::vector& file_enums, diff --git a/hpb_generator/protoc-gen-upb-protos.cc b/hpb_generator/protoc-gen-upb-protos.cc index d946fb260a..5bf9b8f381 100644 --- a/hpb_generator/protoc-gen-upb-protos.cc +++ b/hpb_generator/protoc-gen-upb-protos.cc @@ -13,14 +13,14 @@ #include "google/protobuf/descriptor.pb.h" #include "google/protobuf/compiler/code_generator.h" -#include "google/protobuf/compiler/plugin.h" -#include "google/protobuf/descriptor.h" #include "google/protobuf/compiler/hpb/gen_enums.h" #include "google/protobuf/compiler/hpb/gen_extensions.h" #include "google/protobuf/compiler/hpb/gen_messages.h" #include "google/protobuf/compiler/hpb/gen_utils.h" #include "google/protobuf/compiler/hpb/names.h" #include "google/protobuf/compiler/hpb/output.h" +#include "google/protobuf/compiler/plugin.h" +#include "google/protobuf/descriptor.h" namespace google::protobuf::hpb_generator { namespace { @@ -211,7 +211,6 @@ void WriteSource(const protobuf::FileDescriptor* file, Output& output, WriteMessageImplementations(file, output); const std::vector this_file_exts = SortedExtensions(file); - WriteExtensionIdentifiers(this_file_exts, output); WriteEndNamespace(file, output); output("#include \"upb/port/undef.inc\"\n\n"); diff --git a/hpb_generator/tests/test_generated.cc b/hpb_generator/tests/test_generated.cc index c007d31d7c..578f2d8b8d 100644 --- a/hpb_generator/tests/test_generated.cc +++ b/hpb_generator/tests/test_generated.cc @@ -1242,6 +1242,8 @@ TEST(CppGeneratedCode, FieldNumberConstants) { } TEST(CppGeneratedCode, ExtensionFieldNumberConstant) { + static_assert(::protos::ExtensionNumber(ThemeExtension::theme_extension) == + 12003); EXPECT_EQ(12003, ::protos::ExtensionNumber(ThemeExtension::theme_extension)); }