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
14 changes: 5 additions & 9 deletions src/google/protobuf/compiler/cpp/field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,8 @@ std::vector<Sub> FieldVars(const FieldDescriptor* field, const Options& opts) {
"::internal::TSanRead(&_impl_)")},

// Old-style names.
{"field", FieldMemberName(field, split)},
{"declared_type", DeclaredTypeMethodName(field->type())},
{"classname", ClassName(FieldScope(field), false)},
{"ns", Namespace(field)},
{"tag_size", WireFormat::TagSize(field->number(), field->type())},
{"deprecated_attr", DeprecatedAttribute(opts, field)},
Sub("WeakDescriptorSelfPin",
UsingImplicitWeakDescriptor(field->file(), opts)
? absl::StrCat(
Expand Down Expand Up @@ -202,7 +198,7 @@ void FieldGeneratorBase::GenerateOneofCopyConstruct(io::Printer* p) const {
ABSL_CHECK(!field_->is_extension()) << "Not supported";
ABSL_CHECK(!field_->is_repeated()) << "Not supported";
ABSL_CHECK(!field_->is_map()) << "Not supported";
p->Emit("$field$ = from.$field$;\n");
p->Emit("$field_$ = from.$field_$;\n");
}

void FieldGeneratorBase::GenerateAggregateInitializer(io::Printer* p) const {
Expand All @@ -212,22 +208,22 @@ void FieldGeneratorBase::GenerateAggregateInitializer(io::Printer* p) const {
)cc");
} else {
p->Emit(R"cc(
decltype($field$){arena},
decltype($field_$){arena},
)cc");
}
}

void FieldGeneratorBase::GenerateConstexprAggregateInitializer(
io::Printer* p) const {
p->Emit(R"cc(
/*decltype($field$)*/ {},
/*decltype($field_$)*/ {},
)cc");
}

void FieldGeneratorBase::GenerateCopyAggregateInitializer(
io::Printer* p) const {
p->Emit(R"cc(
decltype($field$){from.$field$},
decltype($field_$){from.$field_$},
)cc");
}

Expand All @@ -236,7 +232,7 @@ void FieldGeneratorBase::GenerateCopyConstructorCode(io::Printer* p) const {
// There is no copy constructor for the `Split` struct, so we need to copy
// the value here.
Formatter format(p, variables_);
format("$field$ = from.$field$;\n");
format("$field_$ = from.$field_$;\n");
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/google/protobuf/compiler/cpp/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,11 @@ class FieldGenerator {
// GeneratePrivateMembers().
//
// These go into the SharedCtor's aggregate initialization of the _impl_
// struct and must follow the syntax `decltype($field$){$default$}`.
// struct and must follow the syntax `decltype($field_$){$default$}`.
// Does not include `:` or `,` separators. Default values should be specified
// here when possible.
//
// NOTE: We use `decltype($field$)` for both explicit construction and the
// NOTE: We use `decltype($field_$)` for both explicit construction and the
// fact that it's self-documenting. Pre-C++17, copy elision isn't guaranteed
// in aggregate initialization so a valid copy/move constructor must exist
// (even though it's not used). Because of this, we need to comment out the
Expand All @@ -433,7 +433,7 @@ class FieldGenerator {
// GeneratePrivateMembers().
//
// These go into the constexpr constructor's aggregate initialization of the
// _impl_ struct and must follow the syntax `/*decltype($field$)*/{}` (see
// _impl_ struct and must follow the syntax `/*decltype($field_$)*/{}` (see
// above). Does not include `:` or `,` separators.
void GenerateConstexprAggregateInitializer(io::Printer* p) const {
auto vars = PushVarsForCall(p);
Expand All @@ -444,7 +444,7 @@ class FieldGenerator {
// GeneratePrivateMembers().
//
// These go into the copy constructor's aggregate initialization of the _impl_
// struct and must follow the syntax `decltype($field$){from.$field$}` (see
// struct and must follow the syntax `decltype($field_$){from.$field_$}` (see
// above). Does not include `:` or `,` separators.
void GenerateCopyAggregateInitializer(io::Printer* p) const {
auto vars = PushVarsForCall(p);
Expand Down
58 changes: 29 additions & 29 deletions src/google/protobuf/compiler/cpp/field_generators/cord_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class CordFieldGenerator : public FieldGeneratorBase {
void GenerateOneofCopyConstruct(io::Printer* p) const override {
auto vars = p->WithVars(variables_);
p->Emit(R"cc(
$field$ = $pb$::Arena::Create<absl::Cord>(arena, *from.$field$);
$field_$ = $pb$::Arena::Create<absl::Cord>(arena, *from.$field_$);
)cc");
}
};
Expand Down Expand Up @@ -170,9 +170,9 @@ void CordFieldGenerator::GenerateAccessorDeclarations(io::Printer* p) const {
p->WithVars(AnnotatedAccessors(field_, {"mutable_"}, Semantic::kAlias));

p->Emit(R"cc(
[[nodiscard]] $deprecated_attr$const ::absl::Cord& $name$() const;
$deprecated_attr$void $set_name$(const ::absl::Cord& value);
$deprecated_attr$void $set_name$(::absl::string_view value);
[[nodiscard]] $DEPRECATED$ const ::absl::Cord& $name$() const;
$DEPRECATED$void $set_name$(const ::absl::Cord& value);
$DEPRECATED$void $set_name$(::absl::string_view value);

private:
const ::absl::Cord& $_internal_name$() const;
Expand All @@ -188,7 +188,7 @@ void CordFieldGenerator::GenerateInlineAccessorDefinitions(
auto v = p->WithVars(variables_);
p->Emit(R"cc(
inline const ::absl::Cord& $classname$::_internal_$name_internal$() const {
return $field$;
return $field_$;
}
)cc");
p->Emit(R"cc(
Expand All @@ -204,7 +204,7 @@ void CordFieldGenerator::GenerateInlineAccessorDefinitions(
inline void $classname$::_internal_set_$name_internal$(
const ::absl::Cord& value) {
$set_hasbit$;
$field$ = value;
$field_$ = value;
}
)cc");
p->Emit(R"cc(
Expand All @@ -221,7 +221,7 @@ void CordFieldGenerator::GenerateInlineAccessorDefinitions(
$WeakDescriptorSelfPin$;
$PrepareSplitMessageForWrite$;
$set_hasbit$;
$field$ = value;
$field_$ = value;
$annotate_set$;
// @@protoc_insertion_point(field_set_string_piece:$full_name$)
}
Expand All @@ -230,7 +230,7 @@ void CordFieldGenerator::GenerateInlineAccessorDefinitions(
inline ::absl::Cord* $nonnull$
$classname$::_internal_mutable_$name_internal$() {
$set_hasbit$;
return &$field$;
return &$field_$;
}
)cc");
}
Expand All @@ -239,11 +239,11 @@ void CordFieldGenerator::GenerateClearingCode(io::Printer* p) const {
auto v = p->WithVars(variables_);
if (field_->default_value_string().empty()) {
p->Emit(R"cc(
$field$.Clear();
$field_$.Clear();
)cc");
} else {
p->Emit(R"cc(
$field$ = ::absl::string_view($default$, $default_length$);
$field_$ = ::absl::string_view($default$, $default_length$);
)cc");
}
}
Expand All @@ -258,15 +258,15 @@ void CordFieldGenerator::GenerateMergingCode(io::Printer* p) const {
void CordFieldGenerator::GenerateSwappingCode(io::Printer* p) const {
auto v = p->WithVars(variables_);
p->Emit(R"cc(
$field$.swap(other->$field$);
$field_$.swap(other->$field_$);
)cc");
}

void CordFieldGenerator::GenerateArenaDestructorCode(io::Printer* p) const {
auto v = p->WithVars(variables_);
// _this is the object being destructed (we are inside a static method here).
p->Emit(R"cc(
_this->$field$.::absl::Cord::~Cord();
_this->$field_$.::absl::Cord::~Cord();
)cc");
}

Expand All @@ -279,30 +279,30 @@ void CordFieldGenerator::GenerateSerializeWithCachedSizesToArray(
absl::Substitute("this_._internal_$0(), ", p->LookupVar("name")));
}
p->Emit(R"cc(
target = stream->Write$declared_type$($number$, this_._internal_$name$(),
target);
target =
stream->Write$DeclaredType$($number$, this_._internal_$name$(), target);
)cc");
}

void CordFieldGenerator::GenerateByteSize(io::Printer* p) const {
auto v = p->WithVars(variables_);
p->Emit(R"cc(
total_size += $tag_size$ + $pbi$::WireFormatLite::$declared_type$Size(
this_._internal_$name$());
total_size += $kTagBytes$ + $pbi$::WireFormatLite::$DeclaredType$Size(
this_._internal_$name$());
)cc");
}

void CordFieldGenerator::GenerateConstexprAggregateInitializer(
io::Printer* p) const {
if (field_->default_value_string().empty()) {
p->Emit(R"cc(
/*decltype($field$)*/ {},
/*decltype($field_$)*/ {},
)cc");
} else {
p->Emit(
{{"Split", should_split() ? "Split::" : ""}},
R"cc(
/*decltype($field$)*/ {::absl::strings_internal::MakeStringConstant(
/*decltype($field_$)*/ {::absl::strings_internal::MakeStringConstant(
$classname$::Impl_::$Split$_default_$name$_func_{})},
)cc");
}
Expand All @@ -315,7 +315,7 @@ void CordFieldGenerator::GenerateAggregateInitializer(io::Printer* p) const {
)cc");
} else {
p->Emit(R"cc(
decltype($field$){},
decltype($field_$){},
)cc");
}
}
Expand Down Expand Up @@ -353,7 +353,7 @@ void CordOneofFieldGenerator::GenerateInlineAccessorDefinitions(
p->Emit(R"cc(
inline const ::absl::Cord& $classname$::_internal_$name_internal$() const {
if ($has_field$) {
return *$field$;
return *$field_$;
}
return $default_variable$;
}
Expand All @@ -373,9 +373,9 @@ void CordOneofFieldGenerator::GenerateInlineAccessorDefinitions(
if ($not_has_field$) {
clear_$oneof_name$();
set_has_$name_internal$();
$field$ = $pb$::Arena::Create<::absl::Cord>(GetArena());
$field_$ = $pb$::Arena::Create<::absl::Cord>(GetArena());
}
*$field$ = value;
*$field_$ = value;
$annotate_set$;
// @@protoc_insertion_point(field_set:$full_name$)
}
Expand All @@ -386,9 +386,9 @@ void CordOneofFieldGenerator::GenerateInlineAccessorDefinitions(
if ($not_has_field$) {
clear_$oneof_name$();
set_has_$name_internal$();
$field$ = $pb$::Arena::Create<::absl::Cord>(GetArena());
$field_$ = $pb$::Arena::Create<::absl::Cord>(GetArena());
}
*$field$ = value;
*$field_$ = value;
$annotate_set$;
// @@protoc_insertion_point(field_set_string_piece:$full_name$)
}
Expand All @@ -399,9 +399,9 @@ void CordOneofFieldGenerator::GenerateInlineAccessorDefinitions(
if ($not_has_field$) {
clear_$oneof_name$();
set_has_$name_internal$();
$field$ = $pb$::Arena::Create<::absl::Cord>(GetArena());
$field_$ = $pb$::Arena::Create<::absl::Cord>(GetArena());
}
return $field$;
return $field_$;
}
)cc");
}
Expand Down Expand Up @@ -431,7 +431,7 @@ void CordOneofFieldGenerator::GenerateClearingCode(io::Printer* p) const {
auto v = p->WithVars(variables_);
p->Emit(R"cc(
if (GetArena() == nullptr) {
delete $field$;
delete $field_$;
}
)cc");
}
Expand All @@ -449,9 +449,9 @@ void CordOneofFieldGenerator::GenerateArenaDestructorCode(
void CordOneofFieldGenerator::GenerateMergingCode(io::Printer* p) const {
p->Emit(R"cc(
if (oneof_needs_init) {
_this->$field$ = $pb$::Arena::Create<absl::Cord>(arena);
_this->$field_$ = $pb$::Arena::Create<absl::Cord>(arena);
}
*_this->$field$ = *from.$field$;
*_this->$field_$ = *from.$field_$;
)cc");
}

Expand Down
Loading
Loading