diff options
author | Jisi Liu <jisi.liu@gmail.com> | 2017-10-18 12:22:18 -0700 |
---|---|---|
committer | Jisi Liu <jisi.liu@gmail.com> | 2017-10-18 12:22:18 -0700 |
commit | 1a7a7fca804afa1cf67f8be5e71092898ba40334 (patch) | |
tree | 04b3da27c71c607510f34a12cf7856a1b94181ae /src/google/protobuf/compiler/cpp | |
parent | c4f59dcc5c13debc572154c8f636b8a9361aacde (diff) | |
download | protobuf-1a7a7fca804afa1cf67f8be5e71092898ba40334.tar.gz protobuf-1a7a7fca804afa1cf67f8be5e71092898ba40334.tar.bz2 protobuf-1a7a7fca804afa1cf67f8be5e71092898ba40334.zip |
Merge from google internal
Diffstat (limited to 'src/google/protobuf/compiler/cpp')
18 files changed, 528 insertions, 974 deletions
diff --git a/src/google/protobuf/compiler/cpp/cpp_enum_field.cc b/src/google/protobuf/compiler/cpp/cpp_enum_field.cc index 008490ed..3d5b5b8d 100644 --- a/src/google/protobuf/compiler/cpp/cpp_enum_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_enum_field.cc @@ -82,21 +82,18 @@ GenerateAccessorDeclarations(io::Printer* printer) const { } void EnumFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const { - std::map<string, string> variables(variables_); - variables["inline"] = is_inline ? "inline " : ""; - printer->Print(variables, - "$inline$$type$ $classname$::$name$() const {\n" +GenerateInlineAccessorDefinitions(io::Printer* printer) const { + printer->Print(variables_, + "inline $type$ $classname$::$name$() const {\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return static_cast< $type$ >($name$_);\n" "}\n" - "$inline$void $classname$::set_$name$($type$ value) {\n"); + "inline void $classname$::set_$name$($type$ value) {\n"); if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { - printer->Print(variables, + printer->Print(variables_, " assert($type$_IsValid(value));\n"); } - printer->Print(variables, + printer->Print(variables_, " $set_hasbit$\n" " $name$_ = value;\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" @@ -193,24 +190,21 @@ EnumOneofFieldGenerator(const FieldDescriptor* descriptor, EnumOneofFieldGenerator::~EnumOneofFieldGenerator() {} void EnumOneofFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const { - std::map<string, string> variables(variables_); - variables["inline"] = is_inline ? "inline " : ""; - printer->Print(variables, - "$inline$$type$ $classname$::$name$() const {\n" +GenerateInlineAccessorDefinitions(io::Printer* printer) const { + printer->Print(variables_, + "inline $type$ $classname$::$name$() const {\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" " if (has_$name$()) {\n" " return static_cast< $type$ >($oneof_prefix$$name$_);\n" " }\n" " return static_cast< $type$ >($default$);\n" "}\n" - "$inline$void $classname$::set_$name$($type$ value) {\n"); + "inline void $classname$::set_$name$($type$ value) {\n"); if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { - printer->Print(variables, + printer->Print(variables_, " assert($type$_IsValid(value));\n"); } - printer->Print(variables, + printer->Print(variables_, " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" " set_has_$name$();\n" @@ -280,39 +274,36 @@ GenerateAccessorDeclarations(io::Printer* printer) const { } void RepeatedEnumFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const { - std::map<string, string> variables(variables_); - variables["inline"] = is_inline ? "inline " : ""; - printer->Print(variables, - "$inline$$type$ $classname$::$name$(int index) const {\n" +GenerateInlineAccessorDefinitions(io::Printer* printer) const { + printer->Print(variables_, + "inline $type$ $classname$::$name$(int index) const {\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return static_cast< $type$ >($name$_.Get(index));\n" "}\n" - "$inline$void $classname$::set_$name$(int index, $type$ value) {\n"); + "inline void $classname$::set_$name$(int index, $type$ value) {\n"); if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { - printer->Print(variables, + printer->Print(variables_, " assert($type$_IsValid(value));\n"); } - printer->Print(variables, + printer->Print(variables_, " $name$_.Set(index, value);\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" "}\n" - "$inline$void $classname$::add_$name$($type$ value) {\n"); + "inline void $classname$::add_$name$($type$ value) {\n"); if (!HasPreservingUnknownEnumSemantics(descriptor_->file())) { - printer->Print(variables, + printer->Print(variables_, " assert($type$_IsValid(value));\n"); } - printer->Print(variables, + printer->Print(variables_, " $name$_.Add(value);\n" " // @@protoc_insertion_point(field_add:$full_name$)\n" "}\n" - "$inline$const ::google::protobuf::RepeatedField<int>&\n" + "inline const ::google::protobuf::RepeatedField<int>&\n" "$classname$::$name$() const {\n" " // @@protoc_insertion_point(field_list:$full_name$)\n" " return $name$_;\n" "}\n" - "$inline$::google::protobuf::RepeatedField<int>*\n" + "inline ::google::protobuf::RepeatedField<int>*\n" "$classname$::mutable_$name$() {\n" " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" " return &$name$_;\n" diff --git a/src/google/protobuf/compiler/cpp/cpp_enum_field.h b/src/google/protobuf/compiler/cpp/cpp_enum_field.h index 3ecd7ba8..d0e87b79 100644 --- a/src/google/protobuf/compiler/cpp/cpp_enum_field.h +++ b/src/google/protobuf/compiler/cpp/cpp_enum_field.h @@ -52,8 +52,7 @@ class EnumFieldGenerator : public FieldGenerator { // implements FieldGenerator --------------------------------------- void GeneratePrivateMembers(io::Printer* printer) const; void GenerateAccessorDeclarations(io::Printer* printer) const; - void GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const; + void GenerateInlineAccessorDefinitions(io::Printer* printer) const; void GenerateClearingCode(io::Printer* printer) const; void GenerateMergingCode(io::Printer* printer) const; void GenerateSwappingCode(io::Printer* printer) const; @@ -79,8 +78,7 @@ class EnumOneofFieldGenerator : public EnumFieldGenerator { ~EnumOneofFieldGenerator(); // implements FieldGenerator --------------------------------------- - void GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const; + void GenerateInlineAccessorDefinitions(io::Printer* printer) const; void GenerateClearingCode(io::Printer* printer) const; void GenerateSwappingCode(io::Printer* printer) const; void GenerateConstructorCode(io::Printer* printer) const; @@ -98,8 +96,7 @@ class RepeatedEnumFieldGenerator : public FieldGenerator { // implements FieldGenerator --------------------------------------- void GeneratePrivateMembers(io::Printer* printer) const; void GenerateAccessorDeclarations(io::Printer* printer) const; - void GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const; + void GenerateInlineAccessorDefinitions(io::Printer* printer) const; void GenerateClearingCode(io::Printer* printer) const; void GenerateMergingCode(io::Printer* printer) const; void GenerateSwappingCode(io::Printer* printer) const; diff --git a/src/google/protobuf/compiler/cpp/cpp_field.h b/src/google/protobuf/compiler/cpp/cpp_field.h index d9dd3850..891e30f6 100644 --- a/src/google/protobuf/compiler/cpp/cpp_field.h +++ b/src/google/protobuf/compiler/cpp/cpp_field.h @@ -109,19 +109,19 @@ class FieldGenerator { // Generate inline definitions of depenent accessor functions for this field. // These are placed inside the header after all class definitions. virtual void GenerateDependentInlineAccessorDefinitions( - io::Printer* printer) const {} + io::Printer* printer) const {} // Generate inline definitions of accessor functions for this field. // These are placed inside the header after all class definitions. // In non-.proto.h mode, this generates dependent accessor functions as well. virtual void GenerateInlineAccessorDefinitions( - io::Printer* printer, bool is_inline) const = 0; + io::Printer* printer) const = 0; // Generate definitions of accessors that aren't inlined. These are // placed somewhere in the .cc file. // Most field types don't need this, so the default implementation is empty. virtual void GenerateNonInlineAccessorDefinitions( - io::Printer* /*printer*/) const {} + io::Printer* /*printer*/) const {} // Generate lines of code (statements, not declarations) which clear the // field. This is used to define the clear_$name$() method diff --git a/src/google/protobuf/compiler/cpp/cpp_file.cc b/src/google/protobuf/compiler/cpp/cpp_file.cc index 0e74f215..52a16835 100644 --- a/src/google/protobuf/compiler/cpp/cpp_file.cc +++ b/src/google/protobuf/compiler/cpp/cpp_file.cc @@ -303,6 +303,18 @@ void FileGenerator::GenerateSourceIncludes(io::Printer* printer) { } } + // TODO(gerbens) Remove this when all code in google is using the same + // proto library. This is a temporary hack to force build errors if + // the proto library is compiled with GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS + // and is also linking internal proto2. This is to prevent regressions while + // we work cleaning up the code base. After this is completed and we have + // one proto lib all code uses this should be removed. + printer->Print( + "// This is a temporary google only hack\n" + "#ifdef GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS\n" + "#include \"third_party/protobuf/version.h\"\n" + "#endif\n"); + printer->Print( "// @@protoc_insertion_point(includes)\n"); } @@ -385,6 +397,10 @@ void FileGenerator::GenerateSourceForMessage(int idx, io::Printer* printer) { // Define default instances GenerateSourceDefaultInstance(idx, printer); + if (UsingImplicitWeakFields(file_, options_)) { + printer->Print("void $classname$_ReferenceStrong() {}\n", "classname", + message_generators_[idx]->classname_); + } // Generate classes. printer->Print("\n"); @@ -452,7 +468,7 @@ void FileGenerator::GenerateSource(io::Printer* printer) { for (int i = 0; i < message_generators_.size(); i++) { GenerateSourceDefaultInstance(i, printer); if (UsingImplicitWeakFields(file_, options_)) { - printer->Print("void $classname$_Reference() {}\n", "classname", + printer->Print("void $classname$_ReferenceStrong() {}\n", "classname", message_generators_[i]->classname_); } } @@ -564,7 +580,7 @@ class FileGenerator::ForwardDeclarations { "classname", it->first); if (options.lite_implicit_weak_fields) { - printer->Print("void $classname$_Reference();\n", + printer->Print("void $classname$_ReferenceStrong();\n", "classname", it->first); } } @@ -827,8 +843,12 @@ void FileGenerator::GenerateInitForSCC(const SCC* scc, io::Printer* printer) { printer->Print( "void InitDefaults$scc_name$Impl() {\n" " GOOGLE_PROTOBUF_VERIFY_VERSION;\n\n" + "#ifdef GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS\n" + " ::google::protobuf::internal::InitProtobufDefaultsForceUnique();\n" + "#else\n" + " ::google::protobuf::internal::InitProtobufDefaults();\n" + "#endif // GOOGLE_PROTOBUF_ENFORCE_UNIQUENESS\n", // Force initialization of primitive values we depend on. - " ::google::protobuf::internal::InitProtobufDefaults();\n", "scc_name", scc_name); printer->Indent(); @@ -1317,8 +1337,7 @@ void FileGenerator::GenerateInlineFunctionDefinitions(io::Printer* printer) { printer->Print(kThinSeparator); printer->Print("\n"); } - message_generators_[i]->GenerateInlineMethods(printer, - /* is_inline = */ true); + message_generators_[i]->GenerateInlineMethods(printer); } printer->Print( "#ifdef __GNUC__\n" diff --git a/src/google/protobuf/compiler/cpp/cpp_helpers.cc b/src/google/protobuf/compiler/cpp/cpp_helpers.cc index 4aa77d06..96950e52 100644 --- a/src/google/protobuf/compiler/cpp/cpp_helpers.cc +++ b/src/google/protobuf/compiler/cpp/cpp_helpers.cc @@ -216,7 +216,7 @@ string DefaultInstanceName(const Descriptor* descriptor) { } string ReferenceFunctionName(const Descriptor* descriptor) { - return QualifiedClassName(descriptor) + "_Reference"; + return QualifiedClassName(descriptor) + "_ReferenceStrong"; } string DependentBaseClassTemplateName(const Descriptor* descriptor) { @@ -753,8 +753,7 @@ bool IsImplicitWeakField(const FieldDescriptor* field, const Options& options) { return UsingImplicitWeakFields(field->file(), options) && field->type() == FieldDescriptor::TYPE_MESSAGE && !field->is_required() && !field->is_repeated() && !field->is_map() && - field->containing_oneof() == NULL && - field->message_type()->file() != field->file(); + field->containing_oneof() == NULL; } struct CompareDescriptors { diff --git a/src/google/protobuf/compiler/cpp/cpp_helpers.h b/src/google/protobuf/compiler/cpp/cpp_helpers.h index 550438dd..e0f809c9 100644 --- a/src/google/protobuf/compiler/cpp/cpp_helpers.h +++ b/src/google/protobuf/compiler/cpp/cpp_helpers.h @@ -305,6 +305,11 @@ inline bool SupportsArenas(const FieldDescriptor* field) { return SupportsArenas(field->file()); } +inline bool IsCrossFileMessage(const FieldDescriptor* field) { + return field->type() == FieldDescriptor::TYPE_MESSAGE && + field->message_type()->file() != field->file(); +} + bool IsAnyMessage(const FileDescriptor* descriptor); bool IsAnyMessage(const Descriptor* descriptor); diff --git a/src/google/protobuf/compiler/cpp/cpp_map_field.cc b/src/google/protobuf/compiler/cpp/cpp_map_field.cc index d06a1d39..b22c0754 100644 --- a/src/google/protobuf/compiler/cpp/cpp_map_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_map_field.cc @@ -137,17 +137,14 @@ GenerateAccessorDeclarations(io::Printer* printer) const { } void MapFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const { - std::map<string, string> variables(variables_); - variables["inline"] = is_inline ? "inline" : ""; - printer->Print(variables, - "$inline$ const ::google::protobuf::Map< $key_cpp$, $val_cpp$ >&\n" +GenerateInlineAccessorDefinitions(io::Printer* printer) const { + printer->Print(variables_, + "inline const ::google::protobuf::Map< $key_cpp$, $val_cpp$ >&\n" "$classname$::$name$() const {\n" " // @@protoc_insertion_point(field_map:$full_name$)\n" " return $name$_.GetMap();\n" "}\n" - "$inline$ ::google::protobuf::Map< $key_cpp$, $val_cpp$ >*\n" + "inline ::google::protobuf::Map< $key_cpp$, $val_cpp$ >*\n" "$classname$::mutable_$name$() {\n" " // @@protoc_insertion_point(field_mutable_map:$full_name$)\n" " return $name$_.MutableMap();\n" @@ -156,9 +153,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, void MapFieldGenerator:: GenerateClearingCode(io::Printer* printer) const { - std::map<string, string> variables(variables_); - variables["this_message"] = dependent_field_ ? DependentBaseDownCast() : ""; - printer->Print(variables, "$this_message$$name$_.Clear();\n"); + printer->Print(variables_, "$name$_.Clear();\n"); } void MapFieldGenerator:: diff --git a/src/google/protobuf/compiler/cpp/cpp_map_field.h b/src/google/protobuf/compiler/cpp/cpp_map_field.h index 02e66497..88e3b464 100644 --- a/src/google/protobuf/compiler/cpp/cpp_map_field.h +++ b/src/google/protobuf/compiler/cpp/cpp_map_field.h @@ -49,8 +49,7 @@ class MapFieldGenerator : public FieldGenerator { // implements FieldGenerator --------------------------------------- void GeneratePrivateMembers(io::Printer* printer) const; void GenerateAccessorDeclarations(io::Printer* printer) const; - void GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const; + void GenerateInlineAccessorDefinitions(io::Printer* printer) const; void GenerateClearingCode(io::Printer* printer) const; void GenerateMergingCode(io::Printer* printer) const; void GenerateSwappingCode(io::Printer* printer) const; diff --git a/src/google/protobuf/compiler/cpp/cpp_message.cc b/src/google/protobuf/compiler/cpp/cpp_message.cc index cf9c1233..60467598 100644 --- a/src/google/protobuf/compiler/cpp/cpp_message.cc +++ b/src/google/protobuf/compiler/cpp/cpp_message.cc @@ -306,6 +306,25 @@ void SetUnknkownFieldsVariable(const Descriptor* descriptor, "_internal_metadata_.mutable_unknown_fields()"; } +bool IsCrossFileMapField(const FieldDescriptor* field) { + if (!field->is_map()) { + return false; + } + + const Descriptor* d = field->message_type(); + const FieldDescriptor* value = d->FindFieldByNumber(2); + + return IsCrossFileMessage(value); +} + +bool IsCrossFileMaybeMap(const FieldDescriptor* field) { + if (IsCrossFileMapField(field)) { + return true; + } + + return IsCrossFileMessage(field); +} + } // anonymous namespace // =================================================================== @@ -426,12 +445,6 @@ GenerateDependentFieldAccessorDeclarations(io::Printer* printer) { std::map<string, string> vars; SetCommonFieldVariables(field, &vars, options_); - if (use_dependent_base_ && IsFieldDependent(field)) { - // If the message is dependent, the inline clear_*() method will need - // to delete the message type, so it must be in the dependent base - // class. (See also GenerateFieldAccessorDeclarations.) - printer->Print(vars, "$deprecated_attr$void clear_$name$();\n"); - } // Generate type-specific accessor declarations. field_generators_.get(field).GenerateDependentAccessorDeclarations(printer); printer->Print("\n"); @@ -498,12 +511,8 @@ GenerateFieldAccessorDeclarations(io::Printer* printer) { printer->Annotate("{", "}", field); } - if (!dependent_field) { - // If this field is dependent, then its clear_() method is in the - // depenent base class. (See also GenerateDependentAccessorDeclarations.) - printer->Print(vars, "$deprecated_attr$void ${$clear_$name$$}$();\n"); - printer->Annotate("{", "}", field); - } + printer->Print(vars, "$deprecated_attr$void ${$clear_$name$$}$();\n"); + printer->Annotate("{", "}", field); printer->Print(vars, "$deprecated_attr$static const int $constant_name$ = " "$number$;\n"); @@ -545,36 +554,6 @@ GenerateDependentFieldAccessorDefinitions(io::Printer* printer) { if (field->options().weak()) continue; PrintFieldComment(printer, field); - - // These functions are not really dependent: they are part of the - // (non-dependent) derived class. However, they need to live outside - // any #ifdef guards, so we treat them as if they were dependent. - // - // See the comment in FileGenerator::GenerateInlineFunctionDefinitions - // for a more complete explanation. - if (use_dependent_base_ && IsFieldDependent(field)) { - std::map<string, string> vars; - SetCommonFieldVariables(field, &vars, options_); - vars["inline"] = "inline "; - if (field->containing_oneof()) { - vars["field_name"] = UnderscoresToCamelCase(field->name(), true); - vars["oneof_name"] = field->containing_oneof()->name(); - vars["oneof_index"] = SimpleItoa(field->containing_oneof()->index()); - GenerateOneofMemberHasBits(field, vars, printer); - } else if (!field->is_repeated()) { - // There will be no header guard, so this always has to be inline. - GenerateSingularFieldHasBits(field, vars, printer); - } - // vars needed for clear_(), which is in the dependent base: - // (See also GenerateDependentFieldAccessorDeclarations.) - vars["tmpl"] = "template<class T>\n"; - vars["dependent_classname"] = - DependentBaseClassTemplateName(descriptor_) + "<T>"; - vars["this_message"] = DependentBaseDownCast(); - vars["this_const_message"] = DependentBaseConstDownCast(); - GenerateFieldClear(field, vars, printer); - } - // Generate type-specific accessors. field_generators_.get(field) .GenerateDependentInlineAccessorDefinitions(printer); @@ -585,7 +564,7 @@ GenerateDependentFieldAccessorDefinitions(io::Printer* printer) { // Generate has_$name$() and clear_has_$name$() functions for oneofs // Similar to other has-bits, these must always be in the header if we // are using a dependent base class. - GenerateOneofHasBits(printer, true /* is_inline */); + GenerateOneofHasBits(printer); } void MessageGenerator:: @@ -595,8 +574,7 @@ GenerateSingularFieldHasBits(const FieldDescriptor* field, if (field->options().weak()) { printer->Print( vars, - "$inline$" - "bool $classname$::has_$name$() const {\n" + "inline bool $classname$::has_$name$() const {\n" " return _weak_field_map_.Has($number$);\n" "}\n"); return; @@ -611,16 +589,13 @@ GenerateSingularFieldHasBits(const FieldDescriptor* field, vars["has_mask"] = StrCat(strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8)); printer->Print(vars, - "$inline$" - "bool $classname$::has_$name$() const {\n" + "inline bool $classname$::has_$name$() const {\n" " return (_has_bits_[$has_array_index$] & 0x$has_mask$u) != 0;\n" "}\n" - "$inline$" - "void $classname$::set_has_$name$() {\n" + "inline void $classname$::set_has_$name$() {\n" " _has_bits_[$has_array_index$] |= 0x$has_mask$u;\n" "}\n" - "$inline$" - "void $classname$::clear_has_$name$() {\n" + "inline void $classname$::clear_has_$name$() {\n" " _has_bits_[$has_array_index$] &= ~0x$has_mask$u;\n" "}\n"); } else { @@ -629,15 +604,13 @@ GenerateSingularFieldHasBits(const FieldDescriptor* field, bool is_lazy = false; if (is_lazy) { printer->Print(vars, - "$inline$" - "bool $classname$::has_$name$() const {\n" + "inline bool $classname$::has_$name$() const {\n" " return !$name$_.IsCleared();\n" "}\n"); } else { printer->Print( vars, - "$inline$" - "bool $classname$::has_$name$() const {\n" + "inline bool $classname$::has_$name$() const {\n" " return this != internal_default_instance() && $name$_ != NULL;\n" "}\n"); } @@ -646,7 +619,7 @@ GenerateSingularFieldHasBits(const FieldDescriptor* field, } void MessageGenerator:: -GenerateOneofHasBits(io::Printer* printer, bool is_inline) { +GenerateOneofHasBits(io::Printer* printer) { for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { std::map<string, string> vars; vars["oneof_name"] = descriptor_->oneof_decl(i)->name(); @@ -654,15 +627,12 @@ GenerateOneofHasBits(io::Printer* printer, bool is_inline) { vars["cap_oneof_name"] = ToUpper(descriptor_->oneof_decl(i)->name()); vars["classname"] = classname_; - vars["inline"] = (is_inline ? "inline " : ""); printer->Print( vars, - "$inline$" - "bool $classname$::has_$oneof_name$() const {\n" + "inline bool $classname$::has_$oneof_name$() const {\n" " return $oneof_name$_case() != $cap_oneof_name$_NOT_SET;\n" "}\n" - "$inline$" - "void $classname$::clear_has_$oneof_name$() {\n" + "inline void $classname$::clear_has_$oneof_name$() {\n" " _oneof_case_[$oneof_index$] = $cap_oneof_name$_NOT_SET;\n" "}\n"); } @@ -679,13 +649,11 @@ GenerateOneofMemberHasBits(const FieldDescriptor* field, // method, so that generated code is slightly cleaner (vs. comparing // _oneof_case_[index] against a constant everywhere). printer->Print(vars, - "$inline$" - "bool $classname$::has_$name$() const {\n" + "inline bool $classname$::has_$name$() const {\n" " return $oneof_name$_case() == k$field_name$;\n" "}\n"); printer->Print(vars, - "$inline$" - "void $classname$::set_has_$name$() {\n" + "inline void $classname$::set_has_$name$() {\n" " _oneof_case_[$oneof_index$] = k$field_name$;\n" "}\n"); } @@ -693,14 +661,14 @@ GenerateOneofMemberHasBits(const FieldDescriptor* field, void MessageGenerator:: GenerateFieldClear(const FieldDescriptor* field, const std::map<string, string>& vars, + bool is_inline, io::Printer* printer) { - // Generate clear_$name$() (See GenerateFieldAccessorDeclarations and - // GenerateDependentFieldAccessorDeclarations, $dependent_classname$ is - // set by the Generate*Definitions functions.) + // Generate clear_$name$(). + if (is_inline) { + printer->Print("inline "); + } printer->Print(vars, - "$tmpl$" - "$inline$" - "void $dependent_classname$::clear_$name$() {\n"); + "void $classname$::clear_$name$() {\n"); printer->Indent(); @@ -708,12 +676,12 @@ GenerateFieldClear(const FieldDescriptor* field, // Clear this field only if it is the active field in this oneof, // otherwise ignore printer->Print(vars, - "if ($this_message$has_$name$()) {\n"); + "if (has_$name$()) {\n"); printer->Indent(); field_generators_.get(field) .GenerateClearingCode(printer); printer->Print(vars, - "$this_message$clear_has_$oneof_name$();\n"); + "clear_has_$oneof_name$();\n"); printer->Outdent(); printer->Print("}\n"); } else { @@ -721,8 +689,7 @@ GenerateFieldClear(const FieldDescriptor* field, .GenerateClearingCode(printer); if (HasFieldPresence(descriptor_->file())) { if (!field->is_repeated() && !field->options().weak()) { - printer->Print(vars, - "$this_message$clear_has_$name$();\n"); + printer->Print(vars, "clear_has_$name$();\n"); } } } @@ -732,7 +699,7 @@ GenerateFieldClear(const FieldDescriptor* field, } void MessageGenerator:: -GenerateFieldAccessorDefinitions(io::Printer* printer, bool is_inline) { +GenerateFieldAccessorDefinitions(io::Printer* printer) { printer->Print("// $classname$\n\n", "classname", classname_); for (int i = 0; i < descriptor_->field_count(); i++) { @@ -742,7 +709,6 @@ GenerateFieldAccessorDefinitions(io::Printer* printer, bool is_inline) { std::map<string, string> vars; SetCommonFieldVariables(field, &vars, options_); - vars["inline"] = is_inline ? "inline " : ""; if (use_dependent_base_ && IsFieldDependent(field)) { vars["tmpl"] = "template<class T>\n"; vars["dependent_classname"] = @@ -759,31 +725,25 @@ GenerateFieldAccessorDefinitions(io::Printer* printer, bool is_inline) { // Generate has_$name$() or $name$_size(). if (field->is_repeated()) { printer->Print(vars, - "$inline$" - "int $classname$::$name$_size() const {\n" + "inline int $classname$::$name$_size() const {\n" " return $name$_.size();\n" "}\n"); } else if (field->containing_oneof()) { vars["field_name"] = UnderscoresToCamelCase(field->name(), true); vars["oneof_name"] = field->containing_oneof()->name(); vars["oneof_index"] = SimpleItoa(field->containing_oneof()->index()); - if (!use_dependent_base_ || !IsFieldDependent(field)) { - GenerateOneofMemberHasBits(field, vars, printer); - } + GenerateOneofMemberHasBits(field, vars, printer); } else { // Singular field. - if (!use_dependent_base_ || !IsFieldDependent(field)) { - GenerateSingularFieldHasBits(field, vars, printer); - } + GenerateSingularFieldHasBits(field, vars, printer); } - if (!use_dependent_base_ || !IsFieldDependent(field)) { - GenerateFieldClear(field, vars, printer); + if (!IsCrossFileMaybeMap(field)) { + GenerateFieldClear(field, vars, true, printer); } // Generate type-specific accessors. - field_generators_.get(field).GenerateInlineAccessorDefinitions( - printer, /* is_inline = */ true); + field_generators_.get(field).GenerateInlineAccessorDefinitions(printer); printer->Print("\n"); } @@ -792,7 +752,7 @@ GenerateFieldAccessorDefinitions(io::Printer* printer, bool is_inline) { // Generate has_$name$() and clear_has_$name$() functions for oneofs // If we aren't using a dependent base, they can be with the other functions // that are #ifdef-guarded. - GenerateOneofHasBits(printer, is_inline); + GenerateOneofHasBits(printer); } } @@ -1381,9 +1341,9 @@ GenerateDependentInlineMethods(io::Printer* printer) { } void MessageGenerator:: -GenerateInlineMethods(io::Printer* printer, bool is_inline) { +GenerateInlineMethods(io::Printer* printer) { if (IsMapEntryMessage(descriptor_)) return; - GenerateFieldAccessorDefinitions(printer, /* is_inline = */ true); + GenerateFieldAccessorDefinitions(printer); // Generate oneof_case() functions. for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { @@ -1393,11 +1353,9 @@ GenerateInlineMethods(io::Printer* printer, bool is_inline) { descriptor_->oneof_decl(i)->name(), true); vars["oneof_name"] = descriptor_->oneof_decl(i)->name(); vars["oneof_index"] = SimpleItoa(descriptor_->oneof_decl(i)->index()); - vars["inline"] = is_inline ? "inline " : ""; printer->Print( vars, - "$inline$" - "$class_name$::$camel_oneof_name$Case $class_name$::" + "inline $class_name$::$camel_oneof_name$Case $class_name$::" "$oneof_name$_case() const {\n" " return $class_name$::$camel_oneof_name$Case(" "_oneof_case_[$oneof_index$]);\n" @@ -1852,8 +1810,17 @@ GenerateClassMethods(io::Printer* printer) { // Generate non-inline field definitions. for (int i = 0; i < descriptor_->field_count(); i++) { - field_generators_.get(descriptor_->field(i)) + const FieldDescriptor* field = descriptor_->field(i); + field_generators_.get(field) .GenerateNonInlineAccessorDefinitions(printer); + if (IsCrossFileMaybeMap(field)) { + std::map<string, string> vars; + SetCommonFieldVariables(field, &vars, options_); + if (field->containing_oneof()) { + SetCommonOneofFieldVariables(field, &vars); + } + GenerateFieldClear(field, vars, false, printer); + } } // Generate field number constants. @@ -2244,16 +2211,9 @@ GenerateSharedDestructorCode(io::Printer* printer) { "classname", classname_); printer->Indent(); if (SupportsArenas(descriptor_)) { - // Do nothing when the message is allocated in an arena. printer->Print( - "::google::protobuf::Arena* arena = GetArenaNoVirtual();\n" - "GOOGLE_DCHECK(arena == NULL);\n" - "if (arena != NULL) {\n" - " return;\n" - "}\n" - "\n"); + "GOOGLE_DCHECK(GetArenaNoVirtual() == NULL);\n"); } - // Write the destructors for each field except oneof members. // optimized_order_ does not contain oneof fields. for (int i = 0; i < optimized_order_.size(); i++) { @@ -2748,11 +2708,7 @@ GenerateClear(io::Printer* printer) { break; } - if (use_dependent_base_ && IsFieldDependent(field)) { - printer->Print("clear_$name$();\n", "name", FieldName(field)); - } else { - generator.GenerateMessageClearingCode(printer); - } + generator.GenerateMessageClearingCode(printer); } // Step 3: Greedily seek runs of fields that can be cleared by @@ -2780,8 +2736,7 @@ GenerateClear(io::Printer* printer) { if (last_chunk == -1) { last_chunk = chunk; last_chunk_start = i; - } else if ((memset_run_start == -1 || unconditional_budget < 0) && - chunk != last_chunk) { + } else if (chunk != last_chunk) { // Emit the fields for this chunk so far. break; } @@ -2900,6 +2855,12 @@ flush: if (should_check_bit && // If no field presence, then always clear strings/messages as well. HasFieldPresence(descriptor_->file())) { + if (!field->options().weak() && + cached_has_bit_index != (has_bit_indices_[field->index()] / 32)) { + cached_has_bit_index = (has_bit_indices_[field->index()] / 32); + printer->Print("cached_has_bits = _has_bits_[$new_index$];\n", + "new_index", SimpleItoa(cached_has_bit_index)); + } if (!MaybeGenerateOptionalFieldCondition(printer, field, cached_has_bit_index)) { printer->Print( diff --git a/src/google/protobuf/compiler/cpp/cpp_message.h b/src/google/protobuf/compiler/cpp/cpp_message.h index cf64f483..0387f0ca 100644 --- a/src/google/protobuf/compiler/cpp/cpp_message.h +++ b/src/google/protobuf/compiler/cpp/cpp_message.h @@ -85,7 +85,7 @@ class MessageGenerator { // Generate definitions of inline methods (placed at the end of the header // file). - void GenerateInlineMethods(io::Printer* printer, bool is_inline); + void GenerateInlineMethods(io::Printer* printer); // Dependent methods are always inline. void GenerateDependentInlineMethods(io::Printer* printer); @@ -112,7 +112,7 @@ class MessageGenerator { void GenerateDependentFieldAccessorDeclarations(io::Printer* printer); void GenerateFieldAccessorDeclarations(io::Printer* printer); void GenerateDependentFieldAccessorDefinitions(io::Printer* printer); - void GenerateFieldAccessorDefinitions(io::Printer* printer, bool is_inline); + void GenerateFieldAccessorDefinitions(io::Printer* printer); // Generate the table-driven parsing array. Returns the number of entries // generated. @@ -189,7 +189,7 @@ class MessageGenerator { std::map<string, string> vars, io::Printer* printer); // Generates has_foo() functions and variables for oneof field has-bits. - void GenerateOneofHasBits(io::Printer* printer, bool is_inline); + void GenerateOneofHasBits(io::Printer* printer); // Generates has_foo_bar() functions for oneof members. void GenerateOneofMemberHasBits(const FieldDescriptor* field, const std::map<string, string>& vars, @@ -197,6 +197,7 @@ class MessageGenerator { // Generates the clear_foo() method for a field. void GenerateFieldClear(const FieldDescriptor* field, const std::map<string, string>& vars, + bool is_inline, io::Printer* printer); void GenerateConstructorBody(io::Printer* printer, diff --git a/src/google/protobuf/compiler/cpp/cpp_message_field.cc b/src/google/protobuf/compiler/cpp/cpp_message_field.cc index 5888f51a..fe60a283 100644 --- a/src/google/protobuf/compiler/cpp/cpp_message_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_message_field.cc @@ -75,7 +75,7 @@ void SetMessageVariables(const FieldDescriptor* descriptor, SetCommonFieldVariables(descriptor, variables, options); (*variables)["type"] = FieldMessageTypeName(descriptor); (*variables)["casted_member"] = - StaticCast((*variables)["type"] + "*", (*variables)["name"] + "_", + ReinterpretCast((*variables)["type"] + "*", (*variables)["name"] + "_", IsImplicitWeakField(descriptor, options)); (*variables)["type_default_instance"] = DefaultInstanceName(descriptor->message_type()); @@ -131,54 +131,21 @@ GeneratePrivateMembers(io::Printer* printer) const { } void MessageFieldGenerator:: -GenerateGetterDeclaration(io::Printer* printer) const { - printer->Print(variables_, - "$deprecated_attr$const $type$& $name$() const;\n"); - printer->Annotate("name", descriptor_); -} - -void MessageFieldGenerator:: GenerateDependentAccessorDeclarations(io::Printer* printer) const { if (!dependent_field_) { return; } - // Arena manipulation code is out-of-line in the derived message class. The - // one exception is unsafe_arena_release_; this method has to be inline so - // that when the implicit weak field optimization is enabled, the method does - // not introduce a strong dependency on the submessage type unless the - // accessor actually gets called somewhere. printer->Print(variables_, "$deprecated_attr$$type$* ${$mutable_$name$$}$();\n"); printer->Annotate("{", "}", descriptor_); - printer->Print(variables_, "$deprecated_attr$$type$* $release_name$();\n"); - printer->Annotate("release_name", descriptor_); - printer->Print(variables_, - "$deprecated_attr$void ${$set_allocated_$name$$}$" - "($type$* $name$);\n"); - printer->Annotate("{", "}", descriptor_); - if (SupportsArenas(descriptor_)) { - printer->Print( - variables_, - "$deprecated_attr$$type$* ${$unsafe_arena_release_$name$$}$();\n"); - printer->Annotate("{", "}", descriptor_); - } } void MessageFieldGenerator:: GenerateAccessorDeclarations(io::Printer* printer) const { - if (SupportsArenas(descriptor_)) { + if (SupportsArenas(descriptor_) && !implicit_weak_field_) { printer->Print(variables_, - "private:\n"); - if (!implicit_weak_field_) { - printer->Print(variables_, "void _slow_mutable_$name$();\n"); - } - if (SupportsArenas(descriptor_->message_type())) { - printer->Print(variables_, - "void _slow_set_allocated_$name$(\n" - " ::google::protobuf::Arena* message_arena, $type$** $name$);\n"); - } - printer->Print(variables_, - "$type$* _slow_$release_name$();\n" + "private:\n" + "void _slow_mutable_$name$();\n" "public:\n"); } if (implicit_weak_field_) { @@ -191,30 +158,30 @@ GenerateAccessorDeclarations(io::Printer* printer) const { "google::protobuf::MessageLite* _internal_mutable_$name$();\n" "public:\n"); } - GenerateGetterDeclaration(printer); + printer->Print(variables_, + "$deprecated_attr$const $type$& $name$() const;\n"); + printer->Annotate("name", descriptor_); + printer->Print(variables_, "$deprecated_attr$$type$* $release_name$();\n"); + printer->Annotate("release_name", descriptor_); if (!dependent_field_) { printer->Print(variables_, "$deprecated_attr$$type$* ${$mutable_$name$$}$();\n"); printer->Annotate("{", "}", descriptor_); - printer->Print(variables_, "$deprecated_attr$$type$* $release_name$();\n"); - printer->Annotate("release_name", descriptor_); - printer->Print(variables_, - "$deprecated_attr$void ${$set_allocated_$name$$}$" - "($type$* $name$);\n"); - printer->Annotate("{", "}", descriptor_); - if (SupportsArenas(descriptor_)) { - printer->Print( - variables_, - "$deprecated_attr$$type$* ${$unsafe_arena_release_$name$$}$();\n"); - printer->Annotate("{", "}", descriptor_); - } } + printer->Print(variables_, + "$deprecated_attr$void ${$set_allocated_$name$$}$" + "($type$* $name$);\n"); + printer->Annotate("{", "}", descriptor_); if (SupportsArenas(descriptor_)) { printer->Print(variables_, "$deprecated_attr$void " "${$unsafe_arena_set_allocated_$name$$}$(\n" " $type$* $name$);\n"); printer->Annotate("{", "}", descriptor_); + printer->Print( + variables_, + "$deprecated_attr$$type$* ${$unsafe_arena_release_$name$$}$();\n"); + printer->Annotate("{", "}", descriptor_); } } @@ -264,56 +231,8 @@ void MessageFieldGenerator::GenerateNonInlineAccessorDefinitions( } } printer->Print(variables_, - "}\n" - "$type$* $classname$::_slow_$release_name$() {\n" - " if ($name$_ == NULL) {\n" - " return NULL;\n" - " } else {\n"); - if (implicit_weak_field_) { - printer->Print(variables_, - " google::protobuf::MessageLite* temp = $name$_->New();\n" - " temp->CheckTypeAndMergeFrom(*$name$_);\n"); - } else { - printer->Print(variables_, - " $type$* temp = new $type$(*$name$_);\n"); - } - printer->Print(variables_, " $name$_ = NULL;\n"); - printer->Print( - " return $result$;\n", "result", - StaticCast(variables_.at("type") + "*", "temp", implicit_weak_field_)); - printer->Print(variables_, - " }\n" "}\n"); - if (SupportsArenas(descriptor_->message_type())) { - // NOTE: the same logic is mirrored in weak_message_field.cc. Any - // arena-related semantics changes should be made in both places. - printer->Print(variables_, - "void $classname$::_slow_set_allocated_$name$(\n" - " ::google::protobuf::Arena* message_arena, $type$** $name$) {\n" - " if (message_arena != NULL && \n" - " ::google::protobuf::Arena::GetArena(*$name$) == NULL) {\n" - " message_arena->Own(*$name$);\n" - " } else if (message_arena !=\n" - " ::google::protobuf::Arena::GetArena(*$name$)) {\n"); - if (implicit_weak_field_) { - printer->Print(variables_, - " google::protobuf::MessageLite* new_$name$ =\n" - " reinterpret_cast<const google::protobuf::MessageLite*>(\n" - " &$type_default_instance$)->New(GetArenaNoVirtual());\n" - " new_$name$->CheckTypeAndMergeFrom(**$name$);\n" - " *$name$ = static_cast< $type$* >(new_$name$);\n"); - } else { - printer->Print(variables_, - " $type$* new_$name$ =\n" - " ::google::protobuf::Arena::CreateMessage< $type$ >(\n" - " message_arena);\n" - " new_$name$->CopyFrom(**$name$);\n" - " *$name$ = new_$name$;\n"); - } - printer->Print(variables_, - " }\n" - "}\n"); - } + printer->Print(variables_, "void $classname$::unsafe_arena_set_allocated_$name$(\n" " $type$* $name$) {\n" @@ -385,13 +304,11 @@ GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const { " $dependent_typename$*& $name$_ = $casted_reference$;\n" " if ($name$_ == NULL) {\n"); if (implicit_weak_field_) { - if (SupportsArenas(descriptor_->message_type())) { - printer->Print(variables, - " $name$_ = reinterpret_cast<$dependent_typename$*>(\n" - " reinterpret_cast<const google::protobuf::MessageLite*>(\n" - " &$type_default_instance$)->New(\n" - " $this_message$GetArenaNoVirtual()));\n"); - } + printer->Print(variables, + " $name$_ = reinterpret_cast<$dependent_typename$*>(\n" + " reinterpret_cast<const google::protobuf::MessageLite*>(\n" + " &$type_default_instance$)->New(\n" + " $this_message$GetArenaNoVirtual()));\n"); } else { printer->Print(variables, " $this_message$_slow_mutable_$name$();\n"); @@ -400,72 +317,6 @@ GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const { " }\n" " // @@protoc_insertion_point(field_mutable:$full_name$)\n" " return $name$_;\n" - "}\n" - "template <class T>\n" - "inline $type$* $dependent_classname$::$release_name$() {\n" - " // @@protoc_insertion_point(field_release:$full_name$)\n"); - if (implicit_weak_field_) { - printer->Print(variables, " $type_reference_function$();\n"); - } - printer->Print(variables, - " $dependent_typename$*& $name$_ = $casted_reference$;\n" - " $clear_hasbit$\n" - " if ($this_message$GetArenaNoVirtual() != NULL) {\n" - " return $this_message$_slow_$release_name$();\n" - " } else {\n" - " $dependent_typename$* temp = $name$_;\n" - " $name$_ = NULL;\n" - " return temp;\n" - " }\n" - "}\n" - "template <class T>\n" - "inline void $dependent_classname$::" - "set_allocated_$name$($type$* $name$) {\n" - " ::google::protobuf::Arena* message_arena = $this_message$GetArenaNoVirtual();\n" - " $dependent_typename$*& $name$_ = $casted_reference$;\n" - " if (message_arena == NULL) {\n" - " delete $name$_;\n" - " }\n" - " if ($name$ != NULL) {\n"); - if (SupportsArenas(descriptor_->message_type())) { - // If we're on an arena and the incoming message is not, simply Own() it - // rather than copy to the arena -- either way we need a heap dealloc, - // so we might as well defer it. Otherwise, if incoming message is on a - // different ownership domain (specific arena, or the heap) than we are, - // copy to our arena (or heap, as the case may be). - printer->Print(variables, - " $this_message$_slow_set_allocated_$name$(message_arena, " - "&$name$);\n"); - } else { - printer->Print(variables, - " if (message_arena != NULL) {\n" - " message_arena->Own($name$);\n" - " }\n"); - } - printer->Print(variables, - " }\n" - " $name$_ = $name$;\n" - " if ($name$) {\n" - " $set_hasbit$\n" - " } else {\n" - " $clear_hasbit$\n" - " }\n" - // TODO(dlj): move insertion points to message class. - " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" - "}\n" - "template <class T>\n" - "inline $type$* $dependent_classname$::unsafe_arena_release_$name$() {\n" - " // @@protoc_insertion_point(" - "field_unsafe_arena_release:$full_name$)\n"); - if (implicit_weak_field_) { - printer->Print(variables, " $type_reference_function$();\n"); - } - printer->Print(variables, - " $clear_hasbit$\n" - " $dependent_typename$*& $name$_ = $casted_reference$;\n" - " $dependent_typename$* temp = $name$_;\n" - " $name$_ = NULL;\n" - " return temp;\n" "}\n"); } else { printer->Print(variables, @@ -478,58 +329,18 @@ GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const { " }\n" " // @@protoc_insertion_point(field_mutable:$full_name$)\n" " return $name$_;\n" - "}\n" - "template <class T>\n" - "inline $type$* $dependent_classname$::$release_name$() {\n" - " // @@protoc_insertion_point(field_release:$full_name$)\n"); - if (implicit_weak_field_) { - printer->Print(variables, " $type_reference_function$();\n"); - } - printer->Print(variables, - " $clear_hasbit$\n" - " $dependent_typename$*& $name$_ = $casted_reference$;\n" - " $dependent_typename$* temp = $name$_;\n" - " $name$_ = NULL;\n" - " return temp;\n" - "}\n" - "template <class T>\n" - "inline void $dependent_classname$::" - "set_allocated_$name$($type$* $name$) {\n" - " $dependent_typename$*& $name$_ = $casted_reference$;\n" - " delete $name$_;\n"); - - if (SupportsArenas(descriptor_->message_type())) { - printer->Print(variables, - " if ($name$ != NULL && static_cast< $dependent_typename$* >($name$)" - "->GetArena() != NULL) {\n" - " $dependent_typename$* new_$name$ = new $dependent_typename$;\n" - " new_$name$->CopyFrom(*$name$);\n" - " $name$ = new_$name$;\n" - " }\n"); - } - - printer->Print(variables, - " $name$_ = $name$;\n" - " if ($name$) {\n" - " $set_hasbit$\n" - " } else {\n" - " $clear_hasbit$\n" - " }\n" - " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" "}\n"); } } void MessageFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const { +GenerateInlineAccessorDefinitions(io::Printer* printer) const { std::map<string, string> variables(variables_); - variables["inline"] = is_inline ? "inline " : ""; variables["const_member"] = ReinterpretCast( "const " + variables["type"] + "*", variables["name"] + "_", implicit_weak_field_); printer->Print(variables, - "$inline$const $type$& $classname$::$name$() const {\n"); + "inline const $type$& $classname$::$name$() const {\n"); if (implicit_weak_field_) { printer->Print(variables, " $type_reference_function$();\n"); } @@ -540,71 +351,29 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " &$type_default_instance$);\n" "}\n"); - if (dependent_field_) return; - + printer->Print(variables, + "inline $type$* $classname$::$release_name$() {\n" + " // @@protoc_insertion_point(field_release:$full_name$)\n"); + if (implicit_weak_field_) { + printer->Print(variables, " $type_reference_function$();\n"); + } + printer->Print(variables, + " $clear_hasbit$\n" + " $type$* temp = $casted_member$;\n"); if (SupportsArenas(descriptor_)) { printer->Print(variables, - "$inline$" - "$type$* $classname$::mutable_$name$() {\n" - " $set_hasbit$\n" - " if ($name$_ == NULL) {\n"); - if (implicit_weak_field_) { - printer->Print(variables, - " _internal_mutable_$name$();\n"); - } else { - printer->Print(variables, - " _slow_mutable_$name$();\n"); - } - printer->Print(variables, - " }\n" - " // @@protoc_insertion_point(field_mutable:$full_name$)\n" - " return $casted_member$;\n" - "}\n" - "$inline$" - "$type$* $classname$::$release_name$() {\n" - " // @@protoc_insertion_point(field_release:$full_name$)\n" - " $clear_hasbit$\n" " if (GetArenaNoVirtual() != NULL) {\n" - " return _slow_$release_name$();\n" - " } else {\n" - " $type$* temp = $casted_member$;\n" - " $name$_ = NULL;\n" - " return temp;\n" - " }\n" - "}\n" - "$inline$ " - "void $classname$::set_allocated_$name$($type$* $name$) {\n" - " ::google::protobuf::Arena* message_arena = GetArenaNoVirtual();\n" - " if (message_arena == NULL) {\n" - " delete $name$_;\n" - " }\n" - " if ($name$ != NULL) {\n"); - if (SupportsArenas(descriptor_->message_type())) { - // If we're on an arena and the incoming message is not, simply Own() it - // rather than copy to the arena -- either way we need a heap dealloc, - // so we might as well defer it. Otherwise, if incoming message is on a - // different ownership domain (specific arena, or the heap) than we are, - // copy to our arena (or heap, as the case may be). - printer->Print(variables, - " _slow_set_allocated_$name$(message_arena, &$name$);\n"); - } else { - printer->Print(variables, - " if (message_arena != NULL) {\n" - " message_arena->Own($name$);\n" - " }\n"); - } + " temp = ::google::protobuf::internal::DuplicateIfNonNull(temp, NULL);\n" + " }\n"); + } + printer->Print(variables, + " $name$_ = NULL;\n" + " return temp;\n" + "}\n"); + + if (SupportsArenas(descriptor_)) { printer->Print(variables, - " }\n" - " $name$_ = $name$;\n" - " if ($name$) {\n" - " $set_hasbit$\n" - " } else {\n" - " $clear_hasbit$\n" - " }\n" - " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" - "}\n" - "$inline$" - "$type$* $classname$::unsafe_arena_release_$name$() {\n" + "inline $type$* $classname$::unsafe_arena_release_$name$() {\n" " // @@protoc_insertion_point(" "field_unsafe_arena_release:$full_name$)\n"); if (implicit_weak_field_) { @@ -616,68 +385,106 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " $name$_ = NULL;\n" " return temp;\n" "}\n"); - } else { - printer->Print(variables, - "$inline$" - "$type$* $classname$::mutable_$name$() {\n" - " $set_hasbit$\n" - " if ($name$_ == NULL) {\n" - " $name$_ = new $type$;\n" - " }\n" - " // @@protoc_insertion_point(field_mutable:$full_name$)\n" - " return $casted_member$;\n" - "}\n" - "$inline$" - "$type$* $classname$::$release_name$() {\n" - " // @@protoc_insertion_point(field_release:$full_name$)\n" - " $clear_hasbit$\n" - " $type$* temp = $casted_member$;\n" - " $name$_ = NULL;\n" - " return temp;\n" - "}\n" - "$inline$" - "void $classname$::set_allocated_$name$($type$* $name$) {\n" - " delete $name$_;\n"); + } - if (SupportsArenas(descriptor_->message_type())) { + if (!dependent_field_) { + if (SupportsArenas(descriptor_)) { printer->Print(variables, - " if ($name$ != NULL && $name$->GetArena() != NULL) {\n" - " $type$* new_$name$ = new $type$;\n" - " new_$name$->CopyFrom(*$name$);\n" - " $name$ = new_$name$;\n" - " }\n"); + "inline $type$* $classname$::mutable_$name$() {\n" + " $set_hasbit$\n" + " if ($name$_ == NULL) {\n"); + if (implicit_weak_field_) { + printer->Print(variables, + " _internal_mutable_$name$();\n"); + } else { + printer->Print(variables, + " _slow_mutable_$name$();\n"); + } + printer->Print(variables, + " }\n" + " // @@protoc_insertion_point(field_mutable:$full_name$)\n" + " return $casted_member$;\n" + "}\n"); + } else { + printer->Print(variables, + "inline $type$* $classname$::mutable_$name$() {\n" + " $set_hasbit$\n" + " if ($name$_ == NULL) {\n" + " $name$_ = new $type$;\n" + " }\n" + " // @@protoc_insertion_point(field_mutable:$full_name$)\n" + " return $casted_member$;\n" + "}\n"); } + } + + // We handle the most common case inline, and delegate less common cases to + // the slow fallback function. + printer->Print(variables, + "inline void $classname$::set_allocated_$name$($type$* $name$) {\n" + " ::google::protobuf::Arena* message_arena = GetArenaNoVirtual();\n"); + printer->Print(variables, + " if (message_arena == NULL) {\n"); + if (IsCrossFileMessage(descriptor_)) { printer->Print(variables, - " $name$_ = $name$;\n" - " if ($name$) {\n" - " $set_hasbit$\n" - " } else {\n" - " $clear_hasbit$\n" - " }\n" - " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" - "}\n"); + " delete reinterpret_cast< ::google::protobuf::MessageLite*>($name$_);\n"); + } else { + printer->Print(variables, + " delete $name$_;\n"); + } + printer->Print(variables, + " }\n" + " if ($name$) {\n"); + if (SupportsArenas(descriptor_->message_type()) && + IsCrossFileMessage(descriptor_)) { + // We have to read the arena through the virtual method, because the type + // isn't defined in this file. + printer->Print(variables, + " ::google::protobuf::Arena* submessage_arena =\n" + " reinterpret_cast< ::google::protobuf::MessageLite*>($name$)->GetArena();\n"); + } else if (!SupportsArenas(descriptor_->message_type())) { + printer->Print(variables, + " ::google::protobuf::Arena* submessage_arena = NULL;\n"); + } else { + printer->Print(variables, + " ::google::protobuf::Arena* submessage_arena =\n" + " ::google::protobuf::Arena::GetArena($name$);\n"); } + printer->Print(variables, + " if (message_arena != submessage_arena) {\n" + " $name$ = ::google::protobuf::internal::GetOwnedMessage(\n" + " message_arena, $name$, submessage_arena);\n" + " }\n" + " $set_hasbit$\n" + " } else {\n" + " $clear_hasbit$\n" + " }\n"); + if (implicit_weak_field_) { + printer->Print(variables, + " $name$_ = reinterpret_cast<MessageLite*>($name$);\n"); + } else { + printer->Print(variables, + " $name$_ = $name$;\n"); + } + printer->Print(variables, + " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" + "}\n"); } void MessageFieldGenerator:: GenerateClearingCode(io::Printer* printer) const { - std::map<string, string> variables(variables_); - variables["this_message"] = dependent_field_ ? DependentBaseDownCast() : ""; if (!HasFieldPresence(descriptor_->file())) { // If we don't have has-bits, message presence is indicated only by ptr != // NULL. Thus on clear, we need to delete the object. - printer->Print(variables, - "if ($this_message$GetArenaNoVirtual() == NULL && " - "$this_message$$name$_ != NULL) delete $this_message$$name$_;\n" - "$this_message$$name$_ = NULL;\n"); - } else if (implicit_weak_field_) { - printer->Print(variables, - "if ($this_message$$name$_ != NULL) $this_message$$name$_->Clear();\n"); + printer->Print(variables_, + "if (GetArenaNoVirtual() == NULL && $name$_ != NULL) {\n" + " delete $name$_;\n" + "}\n" + "$name$_ = NULL;\n"); } else { - printer->Print(variables, - "if ($this_message$$name$_ != NULL) $this_message$$name$_->" - "$dependent_type$::Clear();\n"); + printer->Print(variables_, + "if ($name$_ != NULL) $name$_->Clear();\n"); } } @@ -691,14 +498,10 @@ GenerateMessageClearingCode(io::Printer* printer) const { " delete $name$_;\n" "}\n" "$name$_ = NULL;\n"); - } else if (implicit_weak_field_) { - printer->Print(variables_, - "GOOGLE_DCHECK($name$_ != NULL);\n" - "$name$_->Clear();\n"); } else { printer->Print(variables_, "GOOGLE_DCHECK($name$_ != NULL);\n" - "$name$_->$type$::Clear();\n"); + "$name$_->Clear();\n"); } } @@ -775,11 +578,11 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { " input, _internal_mutable_$name$()));\n"); } else if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE) { printer->Print(variables_, - "DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual(\n" + "DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(\n" " input, mutable_$name$()));\n"); } else { printer->Print(variables_, - "DO_(::google::protobuf::internal::WireFormatLite::ReadGroupNoVirtual(\n" + "DO_(::google::protobuf::internal::WireFormatLite::ReadGroup(\n" " $number$, input, mutable_$name$()));\n"); } } @@ -795,17 +598,15 @@ void MessageFieldGenerator:: GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { printer->Print(variables_, "target = ::google::protobuf::internal::WireFormatLite::\n" - " InternalWrite$declared_type$NoVirtualToArray(\n" + " InternalWrite$declared_type$ToArray(\n" " $number$, *$non_null_ptr_to_name$, deterministic, target);\n"); } void MessageFieldGenerator:: GenerateByteSize(io::Printer* printer) const { - std::map<string, string> variables = variables_; - variables["no_virtual"] = (implicit_weak_field_ ? "" : "NoVirtual"); - printer->Print(variables, + printer->Print(variables_, "total_size += $tag_size$ +\n" - " ::google::protobuf::internal::WireFormatLite::$declared_type$Size$no_virtual$(\n" + " ::google::protobuf::internal::WireFormatLite::$declared_type$Size(\n" " *$non_null_ptr_to_name$);\n"); } @@ -821,29 +622,38 @@ MessageOneofFieldGenerator(const FieldDescriptor* descriptor, MessageOneofFieldGenerator::~MessageOneofFieldGenerator() {} - -void MessageOneofFieldGenerator:: -GenerateDependentAccessorDeclarations(io::Printer* printer) const { - // Oneof field getters must be dependent as they call default_instance(). - // Otherwise, the logic is the same as MessageFields. - if (!dependent_field_) { - return; - } +void MessageOneofFieldGenerator::GenerateNonInlineAccessorDefinitions( + io::Printer* printer) const { printer->Print(variables_, - "$deprecated_attr$const $type$& $name$() const;\n"); - MessageFieldGenerator::GenerateDependentAccessorDeclarations(printer); -} - -void MessageOneofFieldGenerator:: -GenerateGetterDeclaration(io::Printer* printer) const { - // Oneof field getters must be dependent as they call default_instance(). - // Unlike MessageField, this means there is no (non-dependent) getter to - // generate. - if (dependent_field_) { - return; + "void $classname$::set_allocated_$name$($type$* $name$) {\n" + " ::google::protobuf::Arena* message_arena = GetArenaNoVirtual();\n" + " clear_$oneof_name$();\n" + " if ($name$) {\n"); + if (SupportsArenas(descriptor_->message_type()) && + descriptor_->file() != descriptor_->message_type()->file()) { + // We have to read the arena through the virtual method, because the type + // isn't defined in this file. + printer->Print(variables_, + " ::google::protobuf::Arena* submessage_arena =\n" + " reinterpret_cast< ::google::protobuf::MessageLite*>($name$)->GetArena();\n"); + } else if (!SupportsArenas(descriptor_->message_type())) { + printer->Print(variables_, + " ::google::protobuf::Arena* submessage_arena = NULL;\n"); + } else { + printer->Print(variables_, + " ::google::protobuf::Arena* submessage_arena =\n" + " ::google::protobuf::Arena::GetArena($name$);\n"); } printer->Print(variables_, - "$deprecated_attr$const $type$& $name$() const;\n"); + " if (message_arena != submessage_arena) {\n" + " $name$ = ::google::protobuf::internal::GetOwnedMessage(\n" + " message_arena, $name$, submessage_arena);\n" + " }\n" + " set_has_$name$();\n" + " $oneof_prefix$$name$_ = $name$;\n" + " }\n" + " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" + "}\n"); } void MessageOneofFieldGenerator:: @@ -854,7 +664,6 @@ GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const { return; } std::map<string, string> variables(variables_); - variables["inline"] = "inline "; variables["dependent_classname"] = DependentBaseClassTemplateName(descriptor_->containing_type()) + "<T>"; variables["this_message"] = "reinterpret_cast<T*>(this)->"; @@ -868,13 +677,9 @@ GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const { } void MessageOneofFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const { - if (dependent_base_) { - return; - } +GenerateInlineAccessorDefinitions(io::Printer* printer) const { + std::map<string, string> variables(variables_); - variables["inline"] = is_inline ? "inline " : ""; variables["dependent_classname"] = variables["classname"]; variables["this_message"] = ""; variables["this_const_message"] = ""; @@ -882,118 +687,50 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, variables["field_member"] = variables["oneof_prefix"] + variables["name"] + "_"; variables["dependent_type"] = variables["type"]; - InternalGenerateInlineAccessorDefinitions(variables, printer); -} -void MessageOneofFieldGenerator::InternalGenerateInlineAccessorDefinitions( - const std::map<string, string>& variables, io::Printer* printer) const { printer->Print(variables, - "$tmpl$" - "$inline$ " - "const $type$& $dependent_classname$::$name$() const {\n" + "inline $type$* $classname$::$release_name$() {\n" + " // @@protoc_insertion_point(field_release:$full_name$)\n" + " if ($this_message$has_$name$()) {\n" + " $this_message$clear_has_$oneof_name$();\n" + " $type$* temp = $field_member$;\n"); + if (SupportsArenas(descriptor_)) { + printer->Print(variables, + " if ($this_message$GetArenaNoVirtual() != NULL) {\n" + " temp = ::google::protobuf::internal::DuplicateIfNonNull(temp, NULL);\n" + " }\n"); + } + printer->Print(variables, + " $field_member$ = NULL;\n" + " return temp;\n" + " } else {\n" + " return NULL;\n" + " }\n" + "}\n"); + + printer->Print(variables, + "inline const $type$& $classname$::$name$() const {\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return $this_const_message$has_$name$()\n" " ? *$this_const_message$$oneof_prefix$$name$_\n" - " : $dependent_type$::default_instance();\n" + " : *reinterpret_cast< $type$*>(&$type_default_instance$);\n" "}\n"); if (SupportsArenas(descriptor_)) { printer->Print(variables, - "$tmpl$" - "$inline$" - "$type$* $dependent_classname$::mutable_$name$() {\n" - " if (!$this_message$has_$name$()) {\n" - " $this_message$clear_$oneof_name$();\n" - " $this_message$set_has_$name$();\n"); - if (SupportsArenas(descriptor_->message_type())) { - printer->Print(variables, - " $field_member$ = \n" - " ::google::protobuf::Arena::CreateMessage< $dependent_typename$ >(\n" - " $this_message$GetArenaNoVirtual());\n"); - } else { - printer->Print(variables, - " $this_message$$oneof_prefix$$name$_ = \n" - " ::google::protobuf::Arena::Create< $dependent_typename$ >(\n" - " $this_message$GetArenaNoVirtual());\n"); - } - printer->Print(variables, - " }\n" - " // @@protoc_insertion_point(field_mutable:$full_name$)\n" - " return $field_member$;\n" - "}\n" - "$tmpl$" - "$inline$" - "$type$* $dependent_classname$::$release_name$() {\n" - " // @@protoc_insertion_point(field_release:$full_name$)\n" - " if ($this_message$has_$name$()) {\n" - " $this_message$clear_has_$oneof_name$();\n" - " if ($this_message$GetArenaNoVirtual() != NULL) {\n" - // N.B.: safe to use the underlying field pointer here because we are sure - // that it is non-NULL (because has_$name$() returned true). - " $dependent_typename$* temp = " - "new $dependent_typename$(*$field_member$);\n" - " $field_member$ = NULL;\n" - " return temp;\n" - " } else {\n" - " $dependent_typename$* temp = $field_member$;\n" - " $field_member$ = NULL;\n" - " return temp;\n" - " }\n" - " } else {\n" - " return NULL;\n" - " }\n" - "}\n" - "$tmpl$" - "$inline$" - "void $dependent_classname$::" - "set_allocated_$name$($type$* $name$) {\n" - " $this_message$clear_$oneof_name$();\n" - " if ($name$) {\n"); - - if (SupportsArenas(descriptor_->message_type())) { - printer->Print(variables, - // If incoming message is on the heap and we are on an arena, just Own() - // it (see above). If it's on a different arena than we are or one of us - // is on the heap, we make a copy to our arena/heap. - " if ($this_message$GetArenaNoVirtual() != NULL &&\n" - " ::google::protobuf::Arena::GetArena($name$) == NULL) {\n" - " $this_message$GetArenaNoVirtual()->Own($name$);\n" - " } else if ($this_message$GetArenaNoVirtual() !=\n" - " ::google::protobuf::Arena::GetArena($name$)) {\n" - " $dependent_typename$* new_$name$ = \n" - " ::google::protobuf::Arena::CreateMessage< $dependent_typename$ >(\n" - " $this_message$GetArenaNoVirtual());\n" - " new_$name$->CopyFrom(*$name$);\n" - " $name$ = new_$name$;\n" - " }\n"); - } else { - printer->Print(variables, - " if ($this_message$GetArenaNoVirtual() != NULL) {\n" - " $this_message$GetArenaNoVirtual()->Own($name$);\n" - " }\n"); - } - - printer->Print(variables, - " $this_message$set_has_$name$();\n" - " $field_member$ = $name$;\n" - " }\n" - " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" - "}\n" - "$tmpl$" - "$inline$" - "$type$* $dependent_classname$::unsafe_arena_release_$name$() {\n" + "inline $type$* $dependent_classname$::unsafe_arena_release_$name$() {\n" " // @@protoc_insertion_point(field_unsafe_arena_release" ":$full_name$)\n" " if ($this_message$has_$name$()) {\n" " $this_message$clear_has_$oneof_name$();\n" - " $dependent_typename$* temp = $this_message$$oneof_prefix$$name$_;\n" + " $type$* temp = $this_message$$oneof_prefix$$name$_;\n" " $this_message$$oneof_prefix$$name$_ = NULL;\n" " return temp;\n" " } else {\n" " return NULL;\n" " }\n" "}\n" - "$inline$ void $classname$::unsafe_arena_set_allocated_$name$" + "inline void $classname$::unsafe_arena_set_allocated_$name$" "($type$* $name$) {\n" // We rely on the oneof clear method to free the earlier contents of this // oneof. We can directly use the pointer we're given to set the new @@ -1006,68 +743,65 @@ void MessageOneofFieldGenerator::InternalGenerateInlineAccessorDefinitions( " // @@protoc_insertion_point(field_unsafe_arena_set_allocated:" "$full_name$)\n" "}\n"); - } else { + } + + if (dependent_base_) { + return; + } + + InternalGenerateInlineAccessorDefinitions(variables, printer); +} + +void MessageOneofFieldGenerator::InternalGenerateInlineAccessorDefinitions( + const std::map<string, string>& variables, io::Printer* printer) const { + if (SupportsArenas(descriptor_)) { printer->Print(variables, "$tmpl$" - "$inline$" - "$type$* $dependent_classname$::mutable_$name$() {\n" + "inline $type$* $dependent_classname$::mutable_$name$() {\n" " if (!$this_message$has_$name$()) {\n" " $this_message$clear_$oneof_name$();\n" - " $this_message$set_has_$name$();\n" - " $field_member$ = new $dependent_typename$;\n" - " }\n" - " // @@protoc_insertion_point(field_mutable:$full_name$)\n" - " return $field_member$;\n" - "}\n" - "$tmpl$" - "$inline$" - "$type$* $dependent_classname$::$release_name$() {\n" - " // @@protoc_insertion_point(field_release:$full_name$)\n" - " if ($this_message$has_$name$()) {\n" - " $this_message$clear_has_$oneof_name$();\n" - " $dependent_typename$* temp = $field_member$;\n" - " $field_member$ = NULL;\n" - " return temp;\n" - " } else {\n" - " return NULL;\n" - " }\n" - "}\n" - "$tmpl$" - "$inline$" - "void $dependent_classname$::" - "set_allocated_$name$($type$* $name$) {\n" - " $this_message$clear_$oneof_name$();\n" - " if ($name$) {\n"); + " $this_message$set_has_$name$();\n"); if (SupportsArenas(descriptor_->message_type())) { printer->Print(variables, - " if (static_cast< $dependent_typename$*>($name$)->" - "GetArena() != NULL) {\n" - " $dependent_typename$* new_$name$ = new $dependent_typename$;\n" - " new_$name$->CopyFrom(*$name$);\n" - " $name$ = new_$name$;\n" - " }\n"); + " $field_member$ = \n" + " ::google::protobuf::Arena::CreateMessage< $dependent_typename$ >(\n" + " $this_message$GetArenaNoVirtual());\n"); + } else { + printer->Print(variables, + " $this_message$$oneof_prefix$$name$_ = \n" + " ::google::protobuf::Arena::Create< $dependent_typename$ >(\n" + " $this_message$GetArenaNoVirtual());\n"); } printer->Print(variables, + " }\n" + " // @@protoc_insertion_point(field_mutable:$full_name$)\n" + " return $field_member$;\n" + "}\n"); + } else { + printer->Print(variables, + "$tmpl$" + "inline $type$* $dependent_classname$::mutable_$name$() {\n" + " if (!$this_message$has_$name$()) {\n" + " $this_message$clear_$oneof_name$();\n" " $this_message$set_has_$name$();\n" - " $field_member$ = $name$;\n" + " $field_member$ = new $dependent_typename$;\n" " }\n" - " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" + " // @@protoc_insertion_point(field_mutable:$full_name$)\n" + " return $field_member$;\n" "}\n"); } } void MessageOneofFieldGenerator:: GenerateClearingCode(io::Printer* printer) const { - std::map<string, string> variables(variables_); - variables["this_message"] = dependent_field_ ? DependentBaseDownCast() : ""; if (SupportsArenas(descriptor_)) { - printer->Print(variables, - "if ($this_message$GetArenaNoVirtual() == NULL) {\n" - " delete $this_message$$oneof_prefix$$name$_;\n" + printer->Print(variables_, + "if (GetArenaNoVirtual() == NULL) {\n" + " delete $oneof_prefix$$name$_;\n" "}\n"); } else { - printer->Print(variables, - "delete $this_message$$oneof_prefix$$name$_;\n"); + printer->Print(variables_, + "delete $oneof_prefix$$name$_;\n"); } } @@ -1218,48 +952,39 @@ GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const { } void RepeatedMessageFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const { - std::map<string, string> variables(variables_); - variables["inline"] = is_inline ? "inline " : ""; - +GenerateInlineAccessorDefinitions(io::Printer* printer) const { if (!dependent_getter_) { - printer->Print(variables, - "$inline$" - "const $type$& $classname$::$name$(int index) const {\n" + printer->Print(variables_, + "inline const $type$& $classname$::$name$(int index) const {\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return $name$_.$cppget$(index);\n" "}\n"); } if (!dependent_field_) { - printer->Print(variables, - "$inline$" - "$type$* $classname$::mutable_$name$(int index) {\n" + printer->Print(variables_, + "inline $type$* $classname$::mutable_$name$(int index) {\n" // TODO(dlj): move insertion points " // @@protoc_insertion_point(field_mutable:$full_name$)\n" " return $name$_.Mutable(index);\n" "}\n" - "$inline$" - "$type$* $classname$::add_$name$() {\n" + "inline $type$* $classname$::add_$name$() {\n" " // @@protoc_insertion_point(field_add:$full_name$)\n" " return $name$_.Add();\n" "}\n"); } if (!dependent_field_) { - printer->Print(variables, - "$inline$" - "::google::protobuf::RepeatedPtrField< $type$ >*\n" + printer->Print(variables_, + "inline ::google::protobuf::RepeatedPtrField< $type$ >*\n" "$classname$::mutable_$name$() {\n" " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" " return &$name$_;\n" "}\n"); } if (!dependent_getter_) { - printer->Print(variables, - "$inline$" - "const ::google::protobuf::RepeatedPtrField< $type$ >&\n" + printer->Print(variables_, + "inline const ::google::protobuf::RepeatedPtrField< $type$ >&\n" "$classname$::$name$() const {\n" " // @@protoc_insertion_point(field_list:$full_name$)\n" " return $name$_;\n" @@ -1269,9 +994,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, void RepeatedMessageFieldGenerator:: GenerateClearingCode(io::Printer* printer) const { - std::map<string, string> variables(variables_); - variables["this_message"] = dependent_field_ ? DependentBaseDownCast() : ""; - printer->Print(variables, "$this_message$$name$_.Clear();\n"); + printer->Print(variables_, "$name$_.Clear();\n"); } void RepeatedMessageFieldGenerator:: @@ -1294,13 +1017,11 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE) { printer->Print(variables_, "DO_(::google::protobuf::internal::WireFormatLite::" - "ReadMessageNoVirtual(\n" - " input, add_$name$()));\n"); + "ReadMessage(input, add_$name$()));\n"); } else { printer->Print(variables_, "DO_(::google::protobuf::internal::WireFormatLite::" - "ReadGroupNoVirtual(\n" - " $number$, input, add_$name$()));\n"); + "ReadGroup($number$, input, add_$name$()));\n"); } } @@ -1320,7 +1041,7 @@ GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { "for (unsigned int i = 0,\n" " n = static_cast<unsigned int>(this->$name$_size()); i < n; i++) {\n" " target = ::google::protobuf::internal::WireFormatLite::\n" - " InternalWrite$declared_type$NoVirtualToArray(\n" + " InternalWrite$declared_type$ToArray(\n" " $number$, this->$name$(static_cast<int>(i)), deterministic, target);\n" "}\n"); } @@ -1335,7 +1056,7 @@ GenerateByteSize(io::Printer* printer) const { "total_size += $tag_size$UL * count;\n" "for (unsigned int i = 0; i < count; i++) {\n" " total_size +=\n" - " ::google::protobuf::internal::WireFormatLite::$declared_type$SizeNoVirtual(\n" + " ::google::protobuf::internal::WireFormatLite::$declared_type$Size(\n" " this->$name$(static_cast<int>(i)));\n" "}\n"); printer->Outdent(); diff --git a/src/google/protobuf/compiler/cpp/cpp_message_field.h b/src/google/protobuf/compiler/cpp/cpp_message_field.h index 14698992..3be505e3 100644 --- a/src/google/protobuf/compiler/cpp/cpp_message_field.h +++ b/src/google/protobuf/compiler/cpp/cpp_message_field.h @@ -57,8 +57,7 @@ class MessageFieldGenerator : public FieldGenerator { void GenerateDependentAccessorDeclarations(io::Printer* printer) const; void GenerateAccessorDeclarations(io::Printer* printer) const; void GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const; - void GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const; + void GenerateInlineAccessorDefinitions(io::Printer* printer) const; void GenerateNonInlineAccessorDefinitions(io::Printer* printer) const; void GenerateClearingCode(io::Printer* printer) const; void GenerateMessageClearingCode(io::Printer* printer) const; @@ -73,11 +72,6 @@ class MessageFieldGenerator : public FieldGenerator { void GenerateByteSize(io::Printer* printer) const; protected: - void GenerateArenaManipulationCode(const std::map<string, string>& variables, - io::Printer* printer) const; - - virtual void GenerateGetterDeclaration(io::Printer* printer) const; - const FieldDescriptor* descriptor_; const bool dependent_field_; const bool implicit_weak_field_; @@ -94,11 +88,9 @@ class MessageOneofFieldGenerator : public MessageFieldGenerator { ~MessageOneofFieldGenerator(); // implements FieldGenerator --------------------------------------- - void GenerateDependentAccessorDeclarations(io::Printer* printer) const; void GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const; - void GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const; - void GenerateNonInlineAccessorDefinitions(io::Printer* printer) const { } + void GenerateInlineAccessorDefinitions(io::Printer* printer) const; + void GenerateNonInlineAccessorDefinitions(io::Printer* printer) const; void GenerateClearingCode(io::Printer* printer) const; // MessageFieldGenerator, from which we inherit, overrides this so we need to @@ -108,9 +100,6 @@ class MessageOneofFieldGenerator : public MessageFieldGenerator { void GenerateDestructorCode(io::Printer* printer) const; void GenerateConstructorCode(io::Printer* printer) const; - protected: - void GenerateGetterDeclaration(io::Printer* printer) const; - private: void InternalGenerateInlineAccessorDefinitions( const std::map<string, string>& variables, io::Printer* printer) const; @@ -130,8 +119,7 @@ class RepeatedMessageFieldGenerator : public FieldGenerator { void GenerateDependentAccessorDeclarations(io::Printer* printer) const; void GenerateAccessorDeclarations(io::Printer* printer) const; void GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const; - void GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const; + void GenerateInlineAccessorDefinitions(io::Printer* printer) const; void GenerateClearingCode(io::Printer* printer) const; void GenerateMergingCode(io::Printer* printer) const; void GenerateSwappingCode(io::Printer* printer) const; diff --git a/src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc b/src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc index 34a41d82..ceb2270e 100644 --- a/src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc +++ b/src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc @@ -132,7 +132,7 @@ class TestGenerator : public CodeGenerator { // Check field accessors for a message inside oneof{}: TryInsert("test.pb.h", "field_get:foo.Bar.oneOfMessage", context); TryInsert("test.pb.h", "field_mutable:foo.Bar.oneOfMessage", context); - TryInsert("test.pb.h", "field_set_allocated:foo.Bar.oneOfMessage", context); + TryInsert("test.pb.cc", "field_set_allocated:foo.Bar.oneOfMessage", context); // Check field accessors for an optional enum: TryInsert("test.pb.h", "field_get:foo.Bar.optEnum", context); diff --git a/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc b/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc index 67cfc405..bc2d02ea 100644 --- a/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc @@ -123,15 +123,13 @@ GenerateAccessorDeclarations(io::Printer* printer) const { } void PrimitiveFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer, bool is_inline) const { - std::map<string, string> variables(variables_); - variables["inline"] = is_inline ? "inline " : ""; - printer->Print(variables, - "$inline$$type$ $classname$::$name$() const {\n" +GenerateInlineAccessorDefinitions(io::Printer* printer) const { + printer->Print(variables_, + "inline $type$ $classname$::$name$() const {\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return $name$_;\n" "}\n" - "$inline$void $classname$::set_$name$($type$ value) {\n" + "inline void $classname$::set_$name$($type$ value) {\n" " $set_hasbit$\n" " $name$_ = value;\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" @@ -212,18 +210,16 @@ PrimitiveOneofFieldGenerator(const FieldDescriptor* descriptor, PrimitiveOneofFieldGenerator::~PrimitiveOneofFieldGenerator() {} void PrimitiveOneofFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer, bool is_inline) const { - std::map<string, string> variables(variables_); - variables["inline"] = is_inline ? "inline " : ""; - printer->Print(variables, - "$inline$$type$ $classname$::$name$() const {\n" +GenerateInlineAccessorDefinitions(io::Printer* printer) const { + printer->Print(variables_, + "inline $type$ $classname$::$name$() const {\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" " if (has_$name$()) {\n" " return $oneof_prefix$$name$_;\n" " }\n" " return $default$;\n" "}\n" - "$inline$void $classname$::set_$name$($type$ value) {\n" + "inline void $classname$::set_$name$($type$ value) {\n" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" " set_has_$name$();\n" @@ -311,28 +307,26 @@ GenerateAccessorDeclarations(io::Printer* printer) const { } void RepeatedPrimitiveFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer, bool is_inline) const { - std::map<string, string> variables(variables_); - variables["inline"] = is_inline ? "inline " : ""; - printer->Print(variables, - "$inline$$type$ $classname$::$name$(int index) const {\n" +GenerateInlineAccessorDefinitions(io::Printer* printer) const { + printer->Print(variables_, + "inline $type$ $classname$::$name$(int index) const {\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return $name$_.Get(index);\n" "}\n" - "$inline$void $classname$::set_$name$(int index, $type$ value) {\n" + "inline void $classname$::set_$name$(int index, $type$ value) {\n" " $name$_.Set(index, value);\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" "}\n" - "$inline$void $classname$::add_$name$($type$ value) {\n" + "inline void $classname$::add_$name$($type$ value) {\n" " $name$_.Add(value);\n" " // @@protoc_insertion_point(field_add:$full_name$)\n" "}\n" - "$inline$const ::google::protobuf::RepeatedField< $type$ >&\n" + "inline const ::google::protobuf::RepeatedField< $type$ >&\n" "$classname$::$name$() const {\n" " // @@protoc_insertion_point(field_list:$full_name$)\n" " return $name$_;\n" "}\n" - "$inline$::google::protobuf::RepeatedField< $type$ >*\n" + "inline ::google::protobuf::RepeatedField< $type$ >*\n" "$classname$::mutable_$name$() {\n" " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" " return &$name$_;\n" diff --git a/src/google/protobuf/compiler/cpp/cpp_primitive_field.h b/src/google/protobuf/compiler/cpp/cpp_primitive_field.h index 44c9ff3e..d52228e9 100644 --- a/src/google/protobuf/compiler/cpp/cpp_primitive_field.h +++ b/src/google/protobuf/compiler/cpp/cpp_primitive_field.h @@ -53,8 +53,7 @@ class PrimitiveFieldGenerator : public FieldGenerator { // implements FieldGenerator --------------------------------------- void GeneratePrivateMembers(io::Printer* printer) const; void GenerateAccessorDeclarations(io::Printer* printer) const; - void GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const; + void GenerateInlineAccessorDefinitions(io::Printer* printer) const; void GenerateClearingCode(io::Printer* printer) const; void GenerateMergingCode(io::Printer* printer) const; void GenerateSwappingCode(io::Printer* printer) const; @@ -80,8 +79,7 @@ class PrimitiveOneofFieldGenerator : public PrimitiveFieldGenerator { ~PrimitiveOneofFieldGenerator(); // implements FieldGenerator --------------------------------------- - void GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const; + void GenerateInlineAccessorDefinitions(io::Printer* printer) const; void GenerateClearingCode(io::Printer* printer) const; void GenerateSwappingCode(io::Printer* printer) const; void GenerateConstructorCode(io::Printer* printer) const; @@ -100,8 +98,7 @@ class RepeatedPrimitiveFieldGenerator : public FieldGenerator { // implements FieldGenerator --------------------------------------- void GeneratePrivateMembers(io::Printer* printer) const; void GenerateAccessorDeclarations(io::Printer* printer) const; - void GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const; + void GenerateInlineAccessorDefinitions(io::Printer* printer) const; void GenerateClearingCode(io::Printer* printer) const; void GenerateMergingCode(io::Printer* printer) const; void GenerateSwappingCode(io::Printer* printer) const; diff --git a/src/google/protobuf/compiler/cpp/cpp_string_field.cc b/src/google/protobuf/compiler/cpp/cpp_string_field.cc index 8e675751..264f6124 100644 --- a/src/google/protobuf/compiler/cpp/cpp_string_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_string_field.cc @@ -180,16 +180,21 @@ GenerateAccessorDeclarations(io::Printer* printer) const { if (SupportsArenas(descriptor_)) { printer->Print( variables_, - "$deprecated_attr$::std::string* ${$unsafe_arena_release_$name$$}$();\n"); + "PROTOBUF_RUNTIME_DEPRECATED(\"The unsafe_arena_ accessors for\"\n" + "\" string fields are deprecated and will be removed in a\"\n" + "\" future release.\")\n" + "::std::string* ${$unsafe_arena_release_$name$$}$();\n"); printer->Annotate("{", "}", descriptor_); printer->Print( variables_, - "$deprecated_attr$void ${$unsafe_arena_set_allocated_$name$$}$(\n" + "PROTOBUF_RUNTIME_DEPRECATED(\"The unsafe_arena_ accessors for\"\n" + "\" string fields are deprecated and will be removed in a\"\n" + "\" future release.\")\n" + "void ${$unsafe_arena_set_allocated_$name$$}$(\n" " ::std::string* $name$);\n"); printer->Annotate("{", "}", descriptor_); } - if (unknown_ctype) { printer->Outdent(); printer->Print(" public:\n"); @@ -198,38 +203,35 @@ GenerateAccessorDeclarations(io::Printer* printer) const { } void StringFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const { - std::map<string, string> variables(variables_); - variables["inline"] = is_inline ? "inline " : ""; +GenerateInlineAccessorDefinitions(io::Printer* printer) const { if (SupportsArenas(descriptor_)) { printer->Print( - variables, - "$inline$const ::std::string& $classname$::$name$() const {\n" + variables_, + "inline const ::std::string& $classname$::$name$() const {\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return $name$_.Get();\n" "}\n" - "$inline$void $classname$::set_$name$(const ::std::string& value) {\n" + "inline void $classname$::set_$name$(const ::std::string& value) {\n" " $set_hasbit$\n" " $name$_.Set$lite$($default_variable$, value, GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" "}\n" "#if LANG_CXX11\n" - "$inline$void $classname$::set_$name$(::std::string&& value) {\n" + "inline void $classname$::set_$name$(::std::string&& value) {\n" " $set_hasbit$\n" " $name$_.Set$lite$(\n" " $default_variable$, ::std::move(value), GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set_rvalue:$full_name$)\n" "}\n" "#endif\n" - "$inline$void $classname$::set_$name$(const char* value) {\n" + "inline void $classname$::set_$name$(const char* value) {\n" " $null_check$" " $set_hasbit$\n" " $name$_.Set$lite$($default_variable$, $string_piece$(value),\n" " GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set_char:$full_name$)\n" "}\n" - "$inline$" + "inline " "void $classname$::set_$name$(const $pointer_type$* value,\n" " size_t size) {\n" " $set_hasbit$\n" @@ -238,25 +240,17 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, "GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" "}\n" - "$inline$::std::string* $classname$::mutable_$name$() {\n" + "inline ::std::string* $classname$::mutable_$name$() {\n" " $set_hasbit$\n" " // @@protoc_insertion_point(field_mutable:$full_name$)\n" " return $name$_.Mutable($default_variable$, GetArenaNoVirtual());\n" "}\n" - "$inline$::std::string* $classname$::$release_name$() {\n" + "inline ::std::string* $classname$::$release_name$() {\n" " // @@protoc_insertion_point(field_release:$full_name$)\n" " $clear_hasbit$\n" " return $name$_.Release($default_variable$, GetArenaNoVirtual());\n" "}\n" - "$inline$::std::string* $classname$::unsafe_arena_release_$name$() {\n" - " // " - "@@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n" - " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" - " $clear_hasbit$\n" - " return $name$_.UnsafeArenaRelease($default_variable$,\n" - " GetArenaNoVirtual());\n" - "}\n" - "$inline$void $classname$::set_allocated_$name$(::std::string* $name$) {\n" + "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n" " if ($name$ != NULL) {\n" " $set_hasbit$\n" " } else {\n" @@ -266,7 +260,15 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" "}\n" - "$inline$void $classname$::unsafe_arena_set_allocated_$name$(\n" + "inline ::std::string* $classname$::unsafe_arena_release_$name$() {\n" + " // " + "@@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n" + " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" + " $clear_hasbit$\n" + " return $name$_.UnsafeArenaRelease($default_variable$,\n" + " GetArenaNoVirtual());\n" + "}\n" + "inline void $classname$::unsafe_arena_set_allocated_$name$(\n" " ::std::string* $name$) {\n" " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" " if ($name$ != NULL) {\n" @@ -282,31 +284,31 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, } else { // No-arena case. printer->Print( - variables, - "$inline$const ::std::string& $classname$::$name$() const {\n" + variables_, + "inline const ::std::string& $classname$::$name$() const {\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return $name$_.GetNoArena();\n" "}\n" - "$inline$void $classname$::set_$name$(const ::std::string& value) {\n" + "inline void $classname$::set_$name$(const ::std::string& value) {\n" " $set_hasbit$\n" " $name$_.SetNoArena($default_variable$, value);\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" "}\n" "#if LANG_CXX11\n" - "$inline$void $classname$::set_$name$(::std::string&& value) {\n" + "inline void $classname$::set_$name$(::std::string&& value) {\n" " $set_hasbit$\n" " $name$_.SetNoArena(\n" " $default_variable$, ::std::move(value));\n" " // @@protoc_insertion_point(field_set_rvalue:$full_name$)\n" "}\n" "#endif\n" - "$inline$void $classname$::set_$name$(const char* value) {\n" + "inline void $classname$::set_$name$(const char* value) {\n" " $null_check$" " $set_hasbit$\n" " $name$_.SetNoArena($default_variable$, $string_piece$(value));\n" " // @@protoc_insertion_point(field_set_char:$full_name$)\n" "}\n" - "$inline$" + "inline " "void $classname$::set_$name$(const $pointer_type$* value, " "size_t size) {\n" " $set_hasbit$\n" @@ -314,17 +316,17 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " $string_piece$(reinterpret_cast<const char*>(value), size));\n" " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" "}\n" - "$inline$::std::string* $classname$::mutable_$name$() {\n" + "inline ::std::string* $classname$::mutable_$name$() {\n" " $set_hasbit$\n" " // @@protoc_insertion_point(field_mutable:$full_name$)\n" " return $name$_.MutableNoArena($default_variable$);\n" "}\n" - "$inline$::std::string* $classname$::$release_name$() {\n" + "inline ::std::string* $classname$::$release_name$() {\n" " // @@protoc_insertion_point(field_release:$full_name$)\n" " $clear_hasbit$\n" " return $name$_.ReleaseNoArena($default_variable$);\n" "}\n" - "$inline$void $classname$::set_allocated_$name$(::std::string* $name$) {\n" + "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n" " if ($name$ != NULL) {\n" " $set_hasbit$\n" " } else {\n" @@ -470,15 +472,8 @@ GenerateCopyConstructorCode(io::Printer* printer) const { void StringFieldGenerator:: GenerateDestructorCode(io::Printer* printer) const { - if (SupportsArenas(descriptor_)) { - // The variable |arena| is defined by the enclosing code. - // See MessageGenerator::GenerateSharedDestructorCode. - printer->Print(variables_, - "$name$_.Destroy($default_variable$, arena);\n"); - } else { - printer->Print(variables_, - "$name$_.DestroyNoArena($default_variable$);\n"); - } + printer->Print(variables_, + "$name$_.DestroyNoArena($default_variable$);\n"); } void StringFieldGenerator:: @@ -557,21 +552,18 @@ StringOneofFieldGenerator(const FieldDescriptor* descriptor, StringOneofFieldGenerator::~StringOneofFieldGenerator() {} void StringOneofFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const { - std::map<string, string> variables(variables_); - variables["inline"] = is_inline ? "inline " : ""; +GenerateInlineAccessorDefinitions(io::Printer* printer) const { if (SupportsArenas(descriptor_)) { printer->Print( - variables, - "$inline$const ::std::string& $classname$::$name$() const {\n" + variables_, + "inline const ::std::string& $classname$::$name$() const {\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" " if (has_$name$()) {\n" " return $oneof_prefix$$name$_.Get();\n" " }\n" " return *$default_variable$;\n" "}\n" - "$inline$void $classname$::set_$name$(const ::std::string& value) {\n" + "inline void $classname$::set_$name$(const ::std::string& value) {\n" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" " set_has_$name$();\n" @@ -582,7 +574,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " // @@protoc_insertion_point(field_set:$full_name$)\n" "}\n" "#if LANG_CXX11\n" - "$inline$void $classname$::set_$name$(::std::string&& value) {\n" + "inline void $classname$::set_$name$(::std::string&& value) {\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" @@ -594,7 +586,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " // @@protoc_insertion_point(field_set_rvalue:$full_name$)\n" "}\n" "#endif\n" - "$inline$void $classname$::set_$name$(const char* value) {\n" + "inline void $classname$::set_$name$(const char* value) {\n" " $null_check$" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" @@ -605,7 +597,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " $string_piece$(value), GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set_char:$full_name$)\n" "}\n" - "$inline$" + "inline " "void $classname$::set_$name$(const $pointer_type$* value,\n" " size_t size) {\n" " if (!has_$name$()) {\n" @@ -619,7 +611,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" "}\n" - "$inline$::std::string* $classname$::mutable_$name$() {\n" + "inline ::std::string* $classname$::mutable_$name$() {\n" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" " set_has_$name$();\n" @@ -629,7 +621,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_mutable:$full_name$)\n" "}\n" - "$inline$::std::string* $classname$::$release_name$() {\n" + "inline ::std::string* $classname$::$release_name$() {\n" " // @@protoc_insertion_point(field_release:$full_name$)\n" " if (has_$name$()) {\n" " clear_has_$oneof_name$();\n" @@ -639,19 +631,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " return NULL;\n" " }\n" "}\n" - "$inline$::std::string* $classname$::unsafe_arena_release_$name$() {\n" - " // " - "@@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n" - " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" - " if (has_$name$()) {\n" - " clear_has_$oneof_name$();\n" - " return $oneof_prefix$$name$_.UnsafeArenaRelease(\n" - " $default_variable$, GetArenaNoVirtual());\n" - " } else {\n" - " return NULL;\n" - " }\n" - "}\n" - "$inline$void $classname$::set_allocated_$name$(::std::string* $name$) {\n" + "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n" " if (!has_$name$()) {\n" " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" " }\n" @@ -663,7 +643,19 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " }\n" " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n" "}\n" - "$inline$void $classname$::unsafe_arena_set_allocated_$name$(" + "inline ::std::string* $classname$::unsafe_arena_release_$name$() {\n" + " // " + "@@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n" + " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" + " if (has_$name$()) {\n" + " clear_has_$oneof_name$();\n" + " return $oneof_prefix$$name$_.UnsafeArenaRelease(\n" + " $default_variable$, GetArenaNoVirtual());\n" + " } else {\n" + " return NULL;\n" + " }\n" + "}\n" + "inline void $classname$::unsafe_arena_set_allocated_$name$(" "::std::string* $name$) {\n" " GOOGLE_DCHECK(GetArenaNoVirtual() != NULL);\n" " if (!has_$name$()) {\n" @@ -681,15 +673,15 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, } else { // No-arena case. printer->Print( - variables, - "$inline$const ::std::string& $classname$::$name$() const {\n" + variables_, + "inline const ::std::string& $classname$::$name$() const {\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" " if (has_$name$()) {\n" " return $oneof_prefix$$name$_.GetNoArena();\n" " }\n" " return *$default_variable$;\n" "}\n" - "$inline$void $classname$::set_$name$(const ::std::string& value) {\n" + "inline void $classname$::set_$name$(const ::std::string& value) {\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" @@ -700,7 +692,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " // @@protoc_insertion_point(field_set:$full_name$)\n" "}\n" "#if LANG_CXX11\n" - "$inline$void $classname$::set_$name$(::std::string&& value) {\n" + "inline void $classname$::set_$name$(::std::string&& value) {\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" @@ -712,7 +704,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " // @@protoc_insertion_point(field_set_rvalue:$full_name$)\n" "}\n" "#endif\n" - "$inline$void $classname$::set_$name$(const char* value) {\n" + "inline void $classname$::set_$name$(const char* value) {\n" " $null_check$" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" @@ -723,7 +715,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " $string_piece$(value));\n" " // @@protoc_insertion_point(field_set_char:$full_name$)\n" "}\n" - "$inline$" + "inline " "void $classname$::set_$name$(const $pointer_type$* value, size_t " "size) {\n" " if (!has_$name$()) {\n" @@ -736,7 +728,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " reinterpret_cast<const char*>(value), size));\n" " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" "}\n" - "$inline$::std::string* $classname$::mutable_$name$() {\n" + "inline ::std::string* $classname$::mutable_$name$() {\n" " if (!has_$name$()) {\n" " clear_$oneof_name$();\n" " set_has_$name$();\n" @@ -745,7 +737,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " // @@protoc_insertion_point(field_mutable:$full_name$)\n" " return $oneof_prefix$$name$_.MutableNoArena($default_variable$);\n" "}\n" - "$inline$::std::string* $classname$::$release_name$() {\n" + "inline ::std::string* $classname$::$release_name$() {\n" " // @@protoc_insertion_point(field_release:$full_name$)\n" " if (has_$name$()) {\n" " clear_has_$oneof_name$();\n" @@ -754,7 +746,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " return NULL;\n" " }\n" "}\n" - "$inline$void $classname$::set_allocated_$name$(::std::string* $name$) {\n" + "inline void $classname$::set_allocated_$name$(::std::string* $name$) {\n" " if (!has_$name$()) {\n" " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" " }\n" @@ -771,29 +763,13 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, void StringOneofFieldGenerator:: GenerateClearingCode(io::Printer* printer) const { - std::map<string, string> variables(variables_); - if (dependent_field_) { - variables["this_message"] = DependentBaseDownCast(); - // This clearing code may be in the dependent base class. If the default - // value is an empty string, then the $default_variable$ is a global - // singleton. If the default is not empty, we need to down-cast to get the - // default value's global singleton instance. See SetStringVariables() for - // possible values of default_variable. - if (!descriptor_->default_value_string().empty()) { - variables["default_variable"] = "&" + DependentBaseDownCast() + - variables["default_variable_name"] + - ".get()"; - } - } else { - variables["this_message"] = ""; - } if (SupportsArenas(descriptor_)) { - printer->Print(variables, - "$this_message$$oneof_prefix$$name$_.Destroy($default_variable$,\n" - " $this_message$GetArenaNoVirtual());\n"); + printer->Print(variables_, + "$oneof_prefix$$name$_.Destroy($default_variable$,\n" + " GetArenaNoVirtual());\n"); } else { - printer->Print(variables, - "$this_message$$oneof_prefix$$name$_." + printer->Print(variables_, + "$oneof_prefix$$name$_." "DestroyNoArena($default_variable$);\n"); } } @@ -818,18 +794,10 @@ GenerateConstructorCode(io::Printer* printer) const { void StringOneofFieldGenerator:: GenerateDestructorCode(io::Printer* printer) const { - if (SupportsArenas(descriptor_)) { - printer->Print(variables_, - "if (has_$name$()) {\n" - " $oneof_prefix$$name$_.Destroy($default_variable$,\n" - " GetArenaNoVirtual());\n" - "}\n"); - } else { - printer->Print(variables_, - "if (has_$name$()) {\n" - " $oneof_prefix$$name$_.DestroyNoArena($default_variable$);\n" - "}\n"); - } + printer->Print(variables_, + "if (has_$name$()) {\n" + " $oneof_prefix$$name$_.DestroyNoArena($default_variable$);\n" + "}\n"); } void StringOneofFieldGenerator:: @@ -943,71 +911,68 @@ GenerateAccessorDeclarations(io::Printer* printer) const { } void RepeatedStringFieldGenerator:: -GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const { - std::map<string, string> variables(variables_); - variables["inline"] = is_inline ? "inline " : ""; - printer->Print(variables, - "$inline$const ::std::string& $classname$::$name$(int index) const {\n" +GenerateInlineAccessorDefinitions(io::Printer* printer) const { + printer->Print(variables_, + "inline const ::std::string& $classname$::$name$(int index) const {\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" " return $name$_.$cppget$(index);\n" "}\n" - "$inline$::std::string* $classname$::mutable_$name$(int index) {\n" + "inline ::std::string* $classname$::mutable_$name$(int index) {\n" " // @@protoc_insertion_point(field_mutable:$full_name$)\n" " return $name$_.Mutable(index);\n" "}\n" - "$inline$void $classname$::set_$name$(int index, const ::std::string& value) {\n" + "inline void $classname$::set_$name$(int index, const ::std::string& value) {\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" " $name$_.Mutable(index)->assign(value);\n" "}\n" "#if LANG_CXX11\n" - "$inline$void $classname$::set_$name$(int index, ::std::string&& value) {\n" + "inline void $classname$::set_$name$(int index, ::std::string&& value) {\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" " $name$_.Mutable(index)->assign(std::move(value));\n" "}\n" "#endif\n" - "$inline$void $classname$::set_$name$(int index, const char* value) {\n" + "inline void $classname$::set_$name$(int index, const char* value) {\n" " $null_check$" " $name$_.Mutable(index)->assign(value);\n" " // @@protoc_insertion_point(field_set_char:$full_name$)\n" "}\n" - "$inline$void " + "inline void " "$classname$::set_$name$" "(int index, const $pointer_type$* value, size_t size) {\n" " $name$_.Mutable(index)->assign(\n" " reinterpret_cast<const char*>(value), size);\n" " // @@protoc_insertion_point(field_set_pointer:$full_name$)\n" "}\n" - "$inline$::std::string* $classname$::add_$name$() {\n" + "inline ::std::string* $classname$::add_$name$() {\n" " // @@protoc_insertion_point(field_add_mutable:$full_name$)\n" " return $name$_.Add();\n" "}\n" - "$inline$void $classname$::add_$name$(const ::std::string& value) {\n" + "inline void $classname$::add_$name$(const ::std::string& value) {\n" " $name$_.Add()->assign(value);\n" " // @@protoc_insertion_point(field_add:$full_name$)\n" "}\n" "#if LANG_CXX11\n" - "$inline$void $classname$::add_$name$(::std::string&& value) {\n" + "inline void $classname$::add_$name$(::std::string&& value) {\n" " $name$_.Add(std::move(value));\n" " // @@protoc_insertion_point(field_add:$full_name$)\n" "}\n" "#endif\n" - "$inline$void $classname$::add_$name$(const char* value) {\n" + "inline void $classname$::add_$name$(const char* value) {\n" " $null_check$" " $name$_.Add()->assign(value);\n" " // @@protoc_insertion_point(field_add_char:$full_name$)\n" "}\n" - "$inline$void " + "inline void " "$classname$::add_$name$(const $pointer_type$* value, size_t size) {\n" " $name$_.Add()->assign(reinterpret_cast<const char*>(value), size);\n" " // @@protoc_insertion_point(field_add_pointer:$full_name$)\n" "}\n" - "$inline$const ::google::protobuf::RepeatedPtrField< ::std::string>&\n" + "inline const ::google::protobuf::RepeatedPtrField< ::std::string>&\n" "$classname$::$name$() const {\n" " // @@protoc_insertion_point(field_list:$full_name$)\n" " return $name$_;\n" "}\n" - "$inline$::google::protobuf::RepeatedPtrField< ::std::string>*\n" + "inline ::google::protobuf::RepeatedPtrField< ::std::string>*\n" "$classname$::mutable_$name$() {\n" " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n" " return &$name$_;\n" diff --git a/src/google/protobuf/compiler/cpp/cpp_string_field.h b/src/google/protobuf/compiler/cpp/cpp_string_field.h index 933f3c6b..f56f0721 100644 --- a/src/google/protobuf/compiler/cpp/cpp_string_field.h +++ b/src/google/protobuf/compiler/cpp/cpp_string_field.h @@ -54,8 +54,7 @@ class StringFieldGenerator : public FieldGenerator { void GeneratePrivateMembers(io::Printer* printer) const; void GenerateStaticMembers(io::Printer* printer) const; void GenerateAccessorDeclarations(io::Printer* printer) const; - void GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const; + void GenerateInlineAccessorDefinitions(io::Printer* printer) const; void GenerateNonInlineAccessorDefinitions(io::Printer* printer) const; void GenerateClearingCode(io::Printer* printer) const; void GenerateMessageClearingCode(io::Printer* printer) const; @@ -86,8 +85,7 @@ class StringOneofFieldGenerator : public StringFieldGenerator { ~StringOneofFieldGenerator(); // implements FieldGenerator --------------------------------------- - void GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const; + void GenerateInlineAccessorDefinitions(io::Printer* printer) const; void GenerateClearingCode(io::Printer* printer) const; // StringFieldGenerator, from which we inherit, overrides this so we need to @@ -112,8 +110,7 @@ class RepeatedStringFieldGenerator : public FieldGenerator { // implements FieldGenerator --------------------------------------- void GeneratePrivateMembers(io::Printer* printer) const; void GenerateAccessorDeclarations(io::Printer* printer) const; - void GenerateInlineAccessorDefinitions(io::Printer* printer, - bool is_inline) const; + void GenerateInlineAccessorDefinitions(io::Printer* printer) const; void GenerateClearingCode(io::Printer* printer) const; void GenerateMergingCode(io::Printer* printer) const; void GenerateSwappingCode(io::Printer* printer) const; diff --git a/src/google/protobuf/compiler/cpp/metadata_test.cc b/src/google/protobuf/compiler/cpp/metadata_test.cc index 03f6b12b..d1bb3194 100644 --- a/src/google/protobuf/compiler/cpp/metadata_test.cc +++ b/src/google/protobuf/compiler/cpp/metadata_test.cc @@ -35,10 +35,8 @@ #include <google/protobuf/compiler/cpp/cpp_helpers.h> #include <google/protobuf/compiler/cpp/cpp_generator.h> +#include <google/protobuf/compiler/annotation_test_util.h> #include <google/protobuf/compiler/command_line_interface.h> -#include <google/protobuf/io/zero_copy_stream.h> -#include <google/protobuf/io/zero_copy_stream_impl_lite.h> -#include <google/protobuf/io/printer.h> #include <google/protobuf/descriptor.pb.h> #include <google/protobuf/testing/file.h> @@ -47,37 +45,15 @@ #include <gtest/gtest.h> namespace google { +namespace atu = ::google::protobuf::compiler::annotation_test_util; + namespace protobuf { namespace compiler { namespace cpp { namespace { -// A CodeGenerator that captures the FileDescriptor it's passed as a -// FileDescriptorProto. -class DescriptorCapturingGenerator : public CodeGenerator { - public: - // Does not own file; file must outlive the Generator. - explicit DescriptorCapturingGenerator(FileDescriptorProto* file) - : file_(file) {} - - virtual bool Generate(const FileDescriptor* file, const string& parameter, - GeneratorContext* context, string* error) const { - file->CopyTo(file_); - return true; - } - - private: - FileDescriptorProto* file_; -}; - class CppMetadataTest : public ::testing::Test { public: - // Adds a file with name `filename` and content `data`. - void AddFile(const string& filename, const string& data) { - GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/" + filename, data, - true)); - } - // Tries to capture a FileDescriptorProto, GeneratedCodeInfo, and output // code from the previously added file with name `filename`. Returns true on // success. If pb_h is non-null, expects a .pb.h and a .pb.h.meta (copied to @@ -87,26 +63,21 @@ class CppMetadataTest : public ::testing::Test { string* proto_h, GeneratedCodeInfo* proto_h_info, string* pb_cc) { google::protobuf::compiler::CommandLineInterface cli; - cli.SetInputsAreProtoPathRelative(true); - CppGenerator cpp_generator; - DescriptorCapturingGenerator capturing_generator(file); cli.RegisterGenerator("--cpp_out", &cpp_generator, ""); - cli.RegisterGenerator("--capture_out", &capturing_generator, ""); - - string proto_path = "-I" + TestTempDir(); string cpp_out = "--cpp_out=annotate_headers=true," "annotation_pragma_name=pragma_name," "annotation_guard_name=guard_name:" + TestTempDir(); - string capture_out = "--capture_out=" + TestTempDir(); - const char* argv[] = {"protoc", proto_path.c_str(), cpp_out.c_str(), - capture_out.c_str(), filename.c_str()}; + const bool result = + atu::CaptureMetadata(filename, cpp_out, + /* meta_file_suffix */ "", &cli, file, + /* outputs */ NULL); - if (cli.Run(5, argv) != 0) { - return false; + if (!result) { + return result; } string output_base = TestTempDir() + "/" + StripProto(filename); @@ -119,7 +90,7 @@ class CppMetadataTest : public ::testing::Test { if (pb_h != NULL && pb_h_info != NULL) { GOOGLE_CHECK_OK( File::GetContents(output_base + ".pb.h", pb_h, true)); - if (!DecodeMetadata(output_base + ".pb.h.meta", pb_h_info)) { + if (!atu::DecodeMetadata(output_base + ".pb.h.meta", pb_h_info)) { return false; } } @@ -127,23 +98,13 @@ class CppMetadataTest : public ::testing::Test { if (proto_h != NULL && proto_h_info != NULL) { GOOGLE_CHECK_OK(File::GetContents(output_base + ".proto.h", proto_h, true)); - if (!DecodeMetadata(output_base + ".proto.h.meta", proto_h_info)) { + if (!atu::DecodeMetadata(output_base + ".proto.h.meta", proto_h_info)) { return false; } } return true; } - - private: - // Decodes GeneratedCodeInfo stored in path and copies it to info. - // Returns true on success. - bool DecodeMetadata(const string& path, GeneratedCodeInfo* info) { - string data; - GOOGLE_CHECK_OK(File::GetContents(path, &data, true)); - io::ArrayInputStream input(data.data(), data.size()); - return info->ParseFromZeroCopyStream(&input); - } }; const char kSmallTestFile[] = @@ -152,48 +113,11 @@ const char kSmallTestFile[] = "enum Enum { VALUE = 0; }\n" "message Message { }\n"; -// Finds the Annotation for a given source file and path (or returns null if it -// couldn't). -const GeneratedCodeInfo::Annotation* FindAnnotationOnPath( - const GeneratedCodeInfo& info, const string& source_file, - const std::vector<int>& path) { - for (int i = 0; i < info.annotation_size(); ++i) { - const GeneratedCodeInfo::Annotation* annotation = &info.annotation(i); - if (annotation->source_file() != source_file || - annotation->path_size() != path.size()) { - continue; - } - int node = 0; - for (; node < path.size(); ++node) { - if (annotation->path(node) != path[node]) { - break; - } - } - if (node == path.size()) { - return annotation; - } - } - return NULL; -} - -// Returns true if the provided annotation covers a given substring in -// file_content. -bool AnnotationMatchesSubstring(const string& file_content, - const GeneratedCodeInfo::Annotation* annotation, - const string& expected_text) { - uint32 begin = annotation->begin(); - uint32 end = annotation->end(); - if (end < begin || end > file_content.size()) { - return false; - } - return file_content.substr(begin, end - begin) == expected_text; -} - TEST_F(CppMetadataTest, CapturesEnumNames) { FileDescriptorProto file; GeneratedCodeInfo info; string pb_h; - AddFile("test.proto", kSmallTestFile); + atu::AddFile("test.proto", kSmallTestFile); EXPECT_TRUE( CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL)); EXPECT_EQ("Enum", file.enum_type(0).name()); @@ -201,16 +125,16 @@ TEST_F(CppMetadataTest, CapturesEnumNames) { enum_path.push_back(FileDescriptorProto::kEnumTypeFieldNumber); enum_path.push_back(0); const GeneratedCodeInfo::Annotation* enum_annotation = - FindAnnotationOnPath(info, "test.proto", enum_path); + atu::FindAnnotationOnPath(info, "test.proto", enum_path); EXPECT_TRUE(NULL != enum_annotation); - EXPECT_TRUE(AnnotationMatchesSubstring(pb_h, enum_annotation, "Enum")); + EXPECT_TRUE(atu::AnnotationMatchesSubstring(pb_h, enum_annotation, "Enum")); } TEST_F(CppMetadataTest, AddsPragma) { FileDescriptorProto file; GeneratedCodeInfo info; string pb_h; - AddFile("test.proto", kSmallTestFile); + atu::AddFile("test.proto", kSmallTestFile); EXPECT_TRUE( CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL)); EXPECT_TRUE(pb_h.find("#ifdef guard_name") != string::npos); @@ -222,7 +146,7 @@ TEST_F(CppMetadataTest, CapturesMessageNames) { FileDescriptorProto file; GeneratedCodeInfo info; string pb_h; - AddFile("test.proto", kSmallTestFile); + atu::AddFile("test.proto", kSmallTestFile); EXPECT_TRUE( CaptureMetadata("test.proto", &file, &pb_h, &info, NULL, NULL, NULL)); EXPECT_EQ("Message", file.message_type(0).name()); @@ -230,9 +154,10 @@ TEST_F(CppMetadataTest, CapturesMessageNames) { message_path.push_back(FileDescriptorProto::kMessageTypeFieldNumber); message_path.push_back(0); const GeneratedCodeInfo::Annotation* message_annotation = - FindAnnotationOnPath(info, "test.proto", message_path); + atu::FindAnnotationOnPath(info, "test.proto", message_path); EXPECT_TRUE(NULL != message_annotation); - EXPECT_TRUE(AnnotationMatchesSubstring(pb_h, message_annotation, "Message")); + EXPECT_TRUE( + atu::AnnotationMatchesSubstring(pb_h, message_annotation, "Message")); } } // namespace |