Skip to content

Commit

Permalink
In edition 2024, Enum_Name(value) functions return `absl::string_vi…
Browse files Browse the repository at this point in the history
…ew` by default.

Added enum feature `enum_name_uses_string_view` to turn back to the old behavior returning `const std::string&`.

PiperOrigin-RevId: 655637240
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jul 24, 2024
1 parent 75b66c2 commit e3fa6aa
Show file tree
Hide file tree
Showing 9 changed files with 388 additions and 224 deletions.
6 changes: 5 additions & 1 deletion editions/generated_files_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ TEST(Generated, EditionDefaults2023InternalFeatures) {
utf8_validation: VERIFY
message_encoding: LENGTH_PREFIXED
json_format: ALLOW
[pb.cpp] { legacy_closed_enum: false string_type: STRING }
[pb.cpp] {
legacy_closed_enum: false
string_type: STRING
enum_name_uses_string_view: false
}
)pb"));
}

Expand Down
18 changes: 12 additions & 6 deletions src/google/protobuf/compiler/cpp/enum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "absl/container/btree_set.h"
#include "absl/container/flat_hash_map.h"
#include "absl/strings/str_cat.h"
#include "google/protobuf/compiler/cpp/generator.h"
#include "google/protobuf/compiler/cpp/helpers.h"
#include "google/protobuf/compiler/cpp/names.h"
#include "google/protobuf/descriptor.h"
Expand All @@ -49,6 +50,11 @@ absl::flat_hash_map<absl::string_view, std::string> EnumVars(
enum_->containing_type() == nullptr ? "" : absl::StrCat(classname, "_")},
{"kMin", absl::StrCat(min->number())},
{"kMax", absl::StrCat(max->number())},
{"return_type", CppGenerator::GetResolvedSourceFeatures(*enum_)
.GetExtension(::pb::cpp)
.enum_name_uses_string_view()
? "::absl::string_view"
: "const std::string&"},
};
}

Expand Down Expand Up @@ -182,7 +188,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
)cc");
} else {
p->Emit(R"cc(
const std::string& $Msg_Enum$_Name($Msg_Enum$ value);
$return_type$ $Msg_Enum$_Name($Msg_Enum$ value);
)cc");
}

Expand All @@ -204,7 +210,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
if (should_cache_ || !has_reflection_) {
p->Emit({{"static_assert", write_assert}}, R"cc(
template <typename T>
const std::string& $Msg_Enum$_Name(T value) {
$return_type$ $Msg_Enum$_Name(T value) {
$static_assert$;
return $Msg_Enum$_Name(static_cast<$Msg_Enum$>(value));
}
Expand All @@ -216,7 +222,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
// pointers, so if the enum values are sparse, it's not worth it.
p->Emit(R"cc(
template <>
inline const std::string& $Msg_Enum$_Name($Msg_Enum$ value) {
inline $return_type$ $Msg_Enum$_Name($Msg_Enum$ value) {
return ::$proto_ns$::internal::NameOfDenseEnum<$Msg_Enum$_descriptor,
$kMin$, $kMax$>(
static_cast<int>(value));
Expand All @@ -226,7 +232,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
} else {
p->Emit({{"static_assert", write_assert}}, R"cc(
template <typename T>
const std::string& $Msg_Enum$_Name(T value) {
$return_type$ $Msg_Enum$_Name(T value) {
$static_assert$;
return ::$proto_ns$::internal::NameOfEnum($Msg_Enum$_descriptor(), value);
}
Expand Down Expand Up @@ -322,7 +328,7 @@ void EnumGenerator::GenerateSymbolImports(io::Printer* p) const {

p->Emit(R"cc(
template <typename T>
static inline const std::string& $Enum$_Name(T value) {
static inline $return_type$ $Enum$_Name(T value) {
return $Msg_Enum$_Name(value);
}
static inline bool $Enum$_Parse(absl::string_view name, $Enum_$* value) {
Expand Down Expand Up @@ -514,7 +520,7 @@ void EnumGenerator::GenerateMethods(int idx, io::Printer* p) {
$entries_by_number$,
};
const std::string& $Msg_Enum$_Name($Msg_Enum$ value) {
$return_type$ $Msg_Enum$_Name($Msg_Enum$ value) {
static const bool kDummy =
::$proto_ns$::internal::InitializeEnumStrings(
$Msg_Enum$_entries, $Msg_Enum$_entries_by_number,
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/cpp_edition_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// the C++ runtime. This is used for feature resolution under Editions.
// NOLINTBEGIN
// clang-format off
#define PROTOBUF_INTERNAL_CPP_EDITION_DEFAULTS "\n\035\030\204\007\"\003\302>\000*\023\010\001\020\002\030\002 \003(\0010\002\302>\004\010\001\020\003\n\035\030\347\007\"\003\302>\000*\023\010\002\020\001\030\001 \002(\0010\001\302>\004\010\000\020\003\n\035\030\350\007\"\023\010\001\020\001\030\001 \002(\0010\001\302>\004\010\000\020\003*\003\302>\000\n\035\030\351\007\"\023\010\001\020\001\030\001 \002(\0010\001\302>\004\010\000\020\001*\003\302>\000 \346\007(\351\007"
#define PROTOBUF_INTERNAL_CPP_EDITION_DEFAULTS "\n\037\030\204\007\"\003\302>\000*\025\010\001\020\002\030\002 \003(\0010\002\302>\006\010\001\020\003\030\000\n\037\030\347\007\"\003\302>\000*\025\010\002\020\001\030\001 \002(\0010\001\302>\006\010\000\020\003\030\000\n\037\030\350\007\"\023\010\001\020\001\030\001 \002(\0010\001\302>\004\010\000\020\003*\005\302>\002\030\000\n\037\030\351\007\"\025\010\001\020\001\030\001 \002(\0010\001\302>\006\010\000\020\001\030\001*\003\302>\000 \346\007(\351\007"
// clang-format on
// NOLINTEND

Expand Down
114 changes: 70 additions & 44 deletions src/google/protobuf/cpp_features.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e3fa6aa

Please sign in to comment.