Switch C++ extension identifier to constexpr.

Add static_assert test for ExtensionNumber API.

PiperOrigin-RevId: 654129635
pull/17547/head
Protobuf Team Bot 8 months ago committed by Copybara-Service
parent b698355957
commit 74f6da4d26
  1. 9
      hpb/hpb.h
  2. 50
      hpb_generator/gen_extensions.cc
  3. 8
      hpb_generator/gen_extensions.h
  4. 21
      hpb_generator/gen_messages.cc
  5. 5
      hpb_generator/protoc-gen-upb-protos.cc
  6. 2
      hpb_generator/tests/test_generated.cc

@ -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;
};

@ -7,9 +7,14 @@
#include "google/protobuf/compiler/hpb/gen_extensions.h"
#include <cassert>
#include <string>
#include <vector>
#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<const protobuf::FieldDescriptor*>& 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<const protobuf::FieldDescriptor*>& extensions,
Output& output) {
for (const auto* ext : extensions) {
if (!ext->extension_scope()) {
WriteExtensionIdentifier(ext, output);
WriteExtensionIdentifierHeader(ext, output);
}
}
}

@ -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<const protobuf::FieldDescriptor*>& extensions,
Output& output);
void WriteExtensionIdentifier(const protobuf::FieldDescriptor* ext,
Output& output);
} // namespace protobuf
} // namespace google::hpb_generator

@ -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<const protobuf::FieldDescriptor*>& file_exts,
Output& output);
void WriteUsingEnumsInHeader(
const protobuf::Descriptor* message,
const std::vector<const protobuf::EnumDescriptor*>& 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<const protobuf::FieldDescriptor*>& 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<const protobuf::EnumDescriptor*>& file_enums,

@ -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<const protobuf::FieldDescriptor*> this_file_exts =
SortedExtensions(file);
WriteExtensionIdentifiers(this_file_exts, output);
WriteEndNamespace(file, output);
output("#include \"upb/port/undef.inc\"\n\n");

@ -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));
}

Loading…
Cancel
Save