Skip to content

Commit e3fa6aa

Browse files
In edition 2024, Enum_Name(value) functions return absl::string_view by default.
Added enum feature `enum_name_uses_string_view` to turn back to the old behavior returning `const std::string&`. PiperOrigin-RevId: 655637240
1 parent 75b66c2 commit e3fa6aa

File tree

9 files changed

+388
-224
lines changed

9 files changed

+388
-224
lines changed

editions/generated_files_test.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ TEST(Generated, EditionDefaults2023InternalFeatures) {
146146
utf8_validation: VERIFY
147147
message_encoding: LENGTH_PREFIXED
148148
json_format: ALLOW
149-
[pb.cpp] { legacy_closed_enum: false string_type: STRING }
149+
[pb.cpp] {
150+
legacy_closed_enum: false
151+
string_type: STRING
152+
enum_name_uses_string_view: false
153+
}
150154
)pb"));
151155
}
152156

src/google/protobuf/compiler/cpp/enum.cc

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "absl/container/btree_set.h"
2525
#include "absl/container/flat_hash_map.h"
2626
#include "absl/strings/str_cat.h"
27+
#include "google/protobuf/compiler/cpp/generator.h"
2728
#include "google/protobuf/compiler/cpp/helpers.h"
2829
#include "google/protobuf/compiler/cpp/names.h"
2930
#include "google/protobuf/descriptor.h"
@@ -49,6 +50,11 @@ absl::flat_hash_map<absl::string_view, std::string> EnumVars(
4950
enum_->containing_type() == nullptr ? "" : absl::StrCat(classname, "_")},
5051
{"kMin", absl::StrCat(min->number())},
5152
{"kMax", absl::StrCat(max->number())},
53+
{"return_type", CppGenerator::GetResolvedSourceFeatures(*enum_)
54+
.GetExtension(::pb::cpp)
55+
.enum_name_uses_string_view()
56+
? "::absl::string_view"
57+
: "const std::string&"},
5258
};
5359
}
5460

@@ -182,7 +188,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
182188
)cc");
183189
} else {
184190
p->Emit(R"cc(
185-
const std::string& $Msg_Enum$_Name($Msg_Enum$ value);
191+
$return_type$ $Msg_Enum$_Name($Msg_Enum$ value);
186192
)cc");
187193
}
188194

@@ -204,7 +210,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
204210
if (should_cache_ || !has_reflection_) {
205211
p->Emit({{"static_assert", write_assert}}, R"cc(
206212
template <typename T>
207-
const std::string& $Msg_Enum$_Name(T value) {
213+
$return_type$ $Msg_Enum$_Name(T value) {
208214
$static_assert$;
209215
return $Msg_Enum$_Name(static_cast<$Msg_Enum$>(value));
210216
}
@@ -216,7 +222,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
216222
// pointers, so if the enum values are sparse, it's not worth it.
217223
p->Emit(R"cc(
218224
template <>
219-
inline const std::string& $Msg_Enum$_Name($Msg_Enum$ value) {
225+
inline $return_type$ $Msg_Enum$_Name($Msg_Enum$ value) {
220226
return ::$proto_ns$::internal::NameOfDenseEnum<$Msg_Enum$_descriptor,
221227
$kMin$, $kMax$>(
222228
static_cast<int>(value));
@@ -226,7 +232,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
226232
} else {
227233
p->Emit({{"static_assert", write_assert}}, R"cc(
228234
template <typename T>
229-
const std::string& $Msg_Enum$_Name(T value) {
235+
$return_type$ $Msg_Enum$_Name(T value) {
230236
$static_assert$;
231237
return ::$proto_ns$::internal::NameOfEnum($Msg_Enum$_descriptor(), value);
232238
}
@@ -322,7 +328,7 @@ void EnumGenerator::GenerateSymbolImports(io::Printer* p) const {
322328

323329
p->Emit(R"cc(
324330
template <typename T>
325-
static inline const std::string& $Enum$_Name(T value) {
331+
static inline $return_type$ $Enum$_Name(T value) {
326332
return $Msg_Enum$_Name(value);
327333
}
328334
static inline bool $Enum$_Parse(absl::string_view name, $Enum_$* value) {
@@ -514,7 +520,7 @@ void EnumGenerator::GenerateMethods(int idx, io::Printer* p) {
514520
$entries_by_number$,
515521
};
516522
517-
const std::string& $Msg_Enum$_Name($Msg_Enum$ value) {
523+
$return_type$ $Msg_Enum$_Name($Msg_Enum$ value) {
518524
static const bool kDummy =
519525
::$proto_ns$::internal::InitializeEnumStrings(
520526
$Msg_Enum$_entries, $Msg_Enum$_entries_by_number,

src/google/protobuf/cpp_edition_defaults.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// the C++ runtime. This is used for feature resolution under Editions.
66
// NOLINTBEGIN
77
// clang-format off
8-
#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"
8+
#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"
99
// clang-format on
1010
// NOLINTEND
1111

src/google/protobuf/cpp_features.pb.cc

Lines changed: 70 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)