Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions src/google/protobuf/compiler/cpp/enum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ absl::flat_hash_map<absl::string_view, std::string> EnumVars(
.enum_name_uses_string_view()
? "::absl::string_view"
: "const ::std::string&"},
// TODO: Enable this everywhere.
{"nodiscard", options.opensource_runtime ? "[[nodiscard]]" : ""},
};
}

Expand Down Expand Up @@ -204,11 +206,11 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {

if (has_reflection_) {
p->Emit(R"(
$dllexport_decl $const $pb$::EnumDescriptor* $nonnull$ $Msg_Enum$_descriptor();
$nodiscard $ $dllexport_decl $const $pb$::EnumDescriptor* $nonnull$ $Msg_Enum$_descriptor();
)");
} else {
p->Emit(R"cc(
$return_type$ $Msg_Enum$_Name($Msg_Enum$ value);
$nodiscard $ $return_type$ $Msg_Enum$_Name($Msg_Enum$ value);
)cc");
}

Expand All @@ -230,7 +232,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
if (should_cache_ || !has_reflection_) {
p->Emit({{"static_assert", write_assert}}, R"cc(
template <typename T>
$return_type$ $Msg_Enum$_Name(T value) {
$nodiscard $ $return_type$ $Msg_Enum$_Name(T value) {
$static_assert$;
return $Msg_Enum$_Name(static_cast<$Msg_Enum$>(value));
}
Expand All @@ -242,7 +244,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 $return_type$ $Msg_Enum$_Name($Msg_Enum$ value) {
$nodiscard $ inline $return_type$ $Msg_Enum$_Name($Msg_Enum$ value) {
return $pbi$::NameOfDenseEnum<$Msg_Enum$_descriptor, $kMin$, $kMax$>(
static_cast<int>(value));
}
Expand All @@ -251,7 +253,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
} else {
p->Emit({{"static_assert", write_assert}}, R"cc(
template <typename T>
$return_type$ $Msg_Enum$_Name(T value) {
$nodiscard $ $return_type$ $Msg_Enum$_Name(T value) {
$static_assert$;
return $pbi$::NameOfEnum($Msg_Enum$_descriptor(), value);
}
Expand All @@ -260,7 +262,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {

if (has_reflection_) {
p->Emit(R"cc(
inline bool $Msg_Enum$_Parse(
$nodiscard $ inline bool $Msg_Enum$_Parse(
//~
::absl::string_view name, $Msg_Enum$* $nonnull$ value) {
return $pbi$::ParseNamedEnum<$Msg_Enum$>($Msg_Enum$_descriptor(), name,
Expand All @@ -269,7 +271,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* p) {
)cc");
} else {
p->Emit(R"cc(
bool $Msg_Enum$_Parse(
$nodiscard $ bool $Msg_Enum$_Parse(
//~
::absl::string_view name, $Msg_Enum$* $nonnull$ value);
)cc");
Expand Down Expand Up @@ -323,7 +325,7 @@ void EnumGenerator::GenerateSymbolImports(io::Printer* p) const {
.AnnotatedAs(enum_),
},
R"cc(
static inline bool $Enum$_IsValid(int value) {
$nodiscard $ static inline bool $Enum$_IsValid(int value) {
return $Msg_Enum$_IsValid(value);
}
static constexpr $Enum_$ $Enum_MIN$ = $Msg_Enum$_$Enum$_MIN;
Expand Down Expand Up @@ -351,10 +353,10 @@ void EnumGenerator::GenerateSymbolImports(io::Printer* p) const {

p->Emit(R"cc(
template <typename T>
static inline $return_type$ $Enum$_Name(T value) {
$nodiscard $ static inline $return_type$ $Enum$_Name(T value) {
return $Msg_Enum$_Name(value);
}
static inline bool $Enum$_Parse(
$nodiscard $ static inline bool $Enum$_Parse(
//~
::absl::string_view name, $Enum_$* $nonnull$ value) {
return $Msg_Enum$_Parse(name, value);
Expand All @@ -376,7 +378,7 @@ void EnumGenerator::GenerateIsValid(io::Printer* p) const {
p->Emit({{"min", sorted_unique_values_.front()},
{"max", sorted_unique_values_.back()}},
R"cc(
inline bool $Msg_Enum$_IsValid(int value) {
$nodiscard $ inline bool $Msg_Enum$_IsValid(int value) {
return $min$ <= value && value <= $max$;
}
)cc");
Expand All @@ -389,15 +391,15 @@ void EnumGenerator::GenerateIsValid(io::Printer* p) const {
}
p->Emit({{"bitmap", bitmap}, {"max", sorted_unique_values_.back()}},
R"cc(
inline bool $Msg_Enum$_IsValid(int value) {
$nodiscard $ inline bool $Msg_Enum$_IsValid(int value) {
return 0 <= value && value <= $max$ && (($bitmap$u >> value) & 1) != 0;
}
)cc");
} else {
// More complex struct. Use enum data structure for lookup.
p->Emit(
R"cc(
inline bool $Msg_Enum$_IsValid(int value) {
$nodiscard $ inline bool $Msg_Enum$_IsValid(int value) {
return $pbi$::ValidateEnum(value, $Msg_Enum$_internal_data_);
}
)cc");
Expand All @@ -409,7 +411,8 @@ void EnumGenerator::GenerateMethods(int idx, io::Printer* p) {

if (has_reflection_) {
p->Emit({{"idx", idx}}, R"cc(
const $pb$::EnumDescriptor* $nonnull$ $Msg_Enum$_descriptor() {
$nodiscard $ const $pb$::EnumDescriptor* $nonnull$
$Msg_Enum$_descriptor() {
$pbi$::AssignDescriptors(&$desc_table$);
return $file_level_enum_descriptors$[$idx$];
}
Expand Down Expand Up @@ -541,7 +544,7 @@ void EnumGenerator::GenerateMethods(int idx, io::Printer* p) {
$entries_by_number$,
};

$return_type$ $Msg_Enum$_Name($Msg_Enum$ value) {
$nodiscard $ $return_type$ $Msg_Enum$_Name($Msg_Enum$ value) {
static const bool kDummy = $pbi$::InitializeEnumStrings(
$Msg_Enum$_entries, $Msg_Enum$_entries_by_number, $num_unique$,
$Msg_Enum$_strings);
Expand All @@ -553,7 +556,8 @@ void EnumGenerator::GenerateMethods(int idx, io::Printer* p) {
return idx == -1 ? $pbi$::GetEmptyString() : $Msg_Enum$_strings[idx].get();
}

bool $Msg_Enum$_Parse(::absl::string_view name, $Msg_Enum$* $nonnull$ value) {
$nodiscard $ bool $Msg_Enum$_Parse(::absl::string_view name,
$Msg_Enum$* $nonnull$ value) {
int int_value;
bool success = $pbi$::LookUpEnumValue(
$Msg_Enum$_entries, $num_declared$, name, &int_value);
Expand Down
Loading
Loading