diff options
Diffstat (limited to 'src/google/protobuf/compiler')
-rw-r--r-- | src/google/protobuf/compiler/cpp/cpp_enum_field.cc | 4 | ||||
-rw-r--r-- | src/google/protobuf/compiler/cpp/cpp_message.cc | 9 | ||||
-rw-r--r-- | src/google/protobuf/compiler/cpp/cpp_primitive_field.cc | 4 | ||||
-rw-r--r-- | src/google/protobuf/compiler/cpp/cpp_string_field.cc | 24 | ||||
-rw-r--r-- | src/google/protobuf/compiler/cpp/cpp_unittest.cc | 20 | ||||
-rw-r--r-- | src/google/protobuf/compiler/plugin.pb.cc | 57 | ||||
-rw-r--r-- | src/google/protobuf/compiler/plugin.pb.h | 18 |
7 files changed, 110 insertions, 26 deletions
diff --git a/src/google/protobuf/compiler/cpp/cpp_enum_field.cc b/src/google/protobuf/compiler/cpp/cpp_enum_field.cc index 44a86fc8..e4b17a98 100644 --- a/src/google/protobuf/compiler/cpp/cpp_enum_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_enum_field.cc @@ -436,7 +436,7 @@ GenerateSerializeWithCachedSizes(io::Printer* printer) const { "}\n"); } printer->Print(variables_, - "for (int i = 0; i < this->$name$_size(); i++) {\n"); + "for (int i = 0, n = this->$name$_size(); i < n; i++) {\n"); if (descriptor_->is_packed()) { printer->Print(variables_, " ::google::protobuf::internal::WireFormatLite::WriteEnumNoTag(\n" @@ -464,7 +464,7 @@ GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { "}\n"); } printer->Print(variables_, - "for (int i = 0; i < this->$name$_size(); i++) {\n"); + "for (int i = 0, n = this->$name$_size(); i < n; i++) {\n"); if (descriptor_->is_packed()) { printer->Print(variables_, " target = ::google::protobuf::internal::WireFormatLite::WriteEnumNoTagToArray(\n" diff --git a/src/google/protobuf/compiler/cpp/cpp_message.cc b/src/google/protobuf/compiler/cpp/cpp_message.cc index 32f05f25..b4f1adfd 100644 --- a/src/google/protobuf/compiler/cpp/cpp_message.cc +++ b/src/google/protobuf/compiler/cpp/cpp_message.cc @@ -3028,7 +3028,8 @@ GenerateMergeFromCodedStream(io::Printer* printer) { const FieldGenerator& field_generator = field_generators_.get(field); // Emit code to parse the common, expected case. - printer->Print("if (tag == $commontag$u) {\n", + printer->Print("if (static_cast<::google::protobuf::uint8>(tag) ==\n" + " static_cast<::google::protobuf::uint8>($commontag$u)) {\n", "commontag", SimpleItoa(WireFormat::MakeTag(field))); if (loops) { @@ -3047,7 +3048,8 @@ GenerateMergeFromCodedStream(io::Printer* printer) { if (field->is_packed()) { internal::WireFormatLite::WireType wiretype = WireFormat::WireTypeForFieldType(field->type()); - printer->Print("} else if (tag == $uncommontag$u) {\n", + printer->Print("} else if (static_cast<::google::protobuf::uint8>(tag) ==\n" + " static_cast<::google::protobuf::uint8>($uncommontag$u)) {\n", "uncommontag", SimpleItoa( internal::WireFormatLite::MakeTag( field->number(), wiretype))); @@ -3057,7 +3059,8 @@ GenerateMergeFromCodedStream(io::Printer* printer) { } else if (field->is_packable() && !field->is_packed()) { internal::WireFormatLite::WireType wiretype = internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED; - printer->Print("} else if (tag == $uncommontag$u) {\n", + printer->Print("} else if (static_cast<::google::protobuf::uint8>(tag) ==\n" + " static_cast<::google::protobuf::uint8>($uncommontag$u)) {\n", "uncommontag", SimpleItoa( internal::WireFormatLite::MakeTag( field->number(), wiretype))); diff --git a/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc b/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc index a68891a3..4a0a23f6 100644 --- a/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc @@ -394,7 +394,7 @@ GenerateSerializeWithCachedSizes(io::Printer* printer) const { } if (!array_written) { printer->Print(variables_, - "for (int i = 0; i < this->$name$_size(); i++) {\n"); + "for (int i = 0, n = this->$name$_size(); i < n; i++) {\n"); if (descriptor_->is_packed()) { printer->Print(variables_, " ::google::protobuf::internal::WireFormatLite::Write$declared_type$NoTag(\n" @@ -423,7 +423,7 @@ GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { "}\n"); } printer->Print(variables_, - "for (int i = 0; i < this->$name$_size(); i++) {\n"); + "for (int i = 0, n = this->$name$_size(); i < n; i++) {\n"); if (descriptor_->is_packed()) { printer->Print(variables_, " target = ::google::protobuf::internal::WireFormatLite::\n" diff --git a/src/google/protobuf/compiler/cpp/cpp_string_field.cc b/src/google/protobuf/compiler/cpp/cpp_string_field.cc index 359e5e26..664a2779 100644 --- a/src/google/protobuf/compiler/cpp/cpp_string_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_string_field.cc @@ -830,12 +830,18 @@ GenerateAccessorDeclarations(io::Printer* printer) const { "$deprecated_attr$const ::std::string& $name$(int index) const;\n" "$deprecated_attr$::std::string* mutable_$name$(int index);\n" "$deprecated_attr$void set_$name$(int index, const ::std::string& value);\n" + "#if LANG_CXX11\n" + "$deprecated_attr$void set_$name$(int index, ::std::string&& value);\n" + "#endif\n" "$deprecated_attr$void set_$name$(int index, const char* value);\n" "" "$deprecated_attr$void set_$name$(" "int index, const $pointer_type$* value, size_t size);\n" "$deprecated_attr$::std::string* add_$name$();\n" "$deprecated_attr$void add_$name$(const ::std::string& value);\n" + "#if LANG_CXX11\n" + "$deprecated_attr$void add_$name$(::std::string&& value);\n" + "#endif\n" "$deprecated_attr$void add_$name$(const char* value);\n" "$deprecated_attr$void add_$name$(const $pointer_type$* value, size_t size)" ";\n" @@ -869,6 +875,12 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " // @@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" + " // @@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" " $name$_.Mutable(index)->assign(value);\n" " // @@protoc_insertion_point(field_set_char:$full_name$)\n" @@ -888,6 +900,12 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " $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" + " $name$_.Add()->assign(std::move(value));\n" + " // @@protoc_insertion_point(field_add:$full_name$)\n" + "}\n" + "#endif\n" "$inline$void $classname$::add_$name$(const char* value) {\n" " $name$_.Add()->assign(value);\n" " // @@protoc_insertion_point(field_add_char:$full_name$)\n" @@ -951,7 +969,7 @@ GenerateMergeFromCodedStream(io::Printer* printer) const { void RepeatedStringFieldGenerator:: GenerateSerializeWithCachedSizes(io::Printer* printer) const { printer->Print(variables_, - "for (int i = 0; i < this->$name$_size(); i++) {\n"); + "for (int i = 0, n = this->$name$_size(); i < n; i++) {\n"); printer->Indent(); if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { GenerateUtf8CheckCodeForString( @@ -968,7 +986,7 @@ GenerateSerializeWithCachedSizes(io::Printer* printer) const { void RepeatedStringFieldGenerator:: GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const { printer->Print(variables_, - "for (int i = 0; i < this->$name$_size(); i++) {\n"); + "for (int i = 0, n = this->$name$_size(); i < n; i++) {\n"); printer->Indent(); if (descriptor_->type() == FieldDescriptor::TYPE_STRING) { GenerateUtf8CheckCodeForString( @@ -987,7 +1005,7 @@ GenerateByteSize(io::Printer* printer) const { printer->Print(variables_, "total_size += $tag_size$ *\n" " ::google::protobuf::internal::FromIntSize(this->$name$_size());\n" - "for (int i = 0; i < this->$name$_size(); i++) {\n" + "for (int i = 0, n = this->$name$_size(); i < n; i++) {\n" " total_size += ::google::protobuf::internal::WireFormatLite::$declared_type$Size(\n" " this->$name$(i));\n" "}\n"); diff --git a/src/google/protobuf/compiler/cpp/cpp_unittest.cc b/src/google/protobuf/compiler/cpp/cpp_unittest.cc index 686c70a9..6d68ec35 100644 --- a/src/google/protobuf/compiler/cpp/cpp_unittest.cc +++ b/src/google/protobuf/compiler/cpp/cpp_unittest.cc @@ -463,6 +463,26 @@ TEST(GeneratedMessageTest, StringMove) { EXPECT_EQ(old_data, new_data); EXPECT_EQ(string(32, 'b'), message.oneof_string()); } + + // Verify that we trigger the move behavior on a repeated setter. + { + string tmp(32, 'a'); + + const char* old_data = tmp.data(); + message.add_repeated_string(std::move(tmp)); + const char* new_data = message.repeated_string(0).data(); + + EXPECT_EQ(old_data, new_data); + EXPECT_EQ(string(32, 'a'), message.repeated_string(0)); + + string tmp2(32, 'b'); + old_data = tmp2.data(); + message.set_repeated_string(0, std::move(tmp2)); + new_data = message.repeated_string(0).data(); + + EXPECT_EQ(old_data, new_data); + EXPECT_EQ(string(32, 'b'), message.repeated_string(0)); + } } #endif diff --git a/src/google/protobuf/compiler/plugin.pb.cc b/src/google/protobuf/compiler/plugin.pb.cc index ac27c412..686ee477 100644 --- a/src/google/protobuf/compiler/plugin.pb.cc +++ b/src/google/protobuf/compiler/plugin.pb.cc @@ -287,7 +287,8 @@ bool Version::MergePartialFromCodedStream( switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional int32 major = 1; case 1: { - if (tag == 8u) { + if (static_cast<::google::protobuf::uint8>(tag) == + static_cast<::google::protobuf::uint8>(8u)) { set_has_major(); DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( @@ -300,7 +301,8 @@ bool Version::MergePartialFromCodedStream( // optional int32 minor = 2; case 2: { - if (tag == 16u) { + if (static_cast<::google::protobuf::uint8>(tag) == + static_cast<::google::protobuf::uint8>(16u)) { set_has_minor(); DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( @@ -313,7 +315,8 @@ bool Version::MergePartialFromCodedStream( // optional int32 patch = 3; case 3: { - if (tag == 24u) { + if (static_cast<::google::protobuf::uint8>(tag) == + static_cast<::google::protobuf::uint8>(24u)) { set_has_patch(); DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( @@ -326,7 +329,8 @@ bool Version::MergePartialFromCodedStream( // optional string suffix = 4; case 4: { - if (tag == 34u) { + if (static_cast<::google::protobuf::uint8>(tag) == + static_cast<::google::protobuf::uint8>(34u)) { DO_(::google::protobuf::internal::WireFormatLite::ReadString( input, this->mutable_suffix())); ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( @@ -800,7 +804,8 @@ bool CodeGeneratorRequest::MergePartialFromCodedStream( switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { // repeated string file_to_generate = 1; case 1: { - if (tag == 10u) { + if (static_cast<::google::protobuf::uint8>(tag) == + static_cast<::google::protobuf::uint8>(10u)) { DO_(::google::protobuf::internal::WireFormatLite::ReadString( input, this->add_file_to_generate())); ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( @@ -816,7 +821,8 @@ bool CodeGeneratorRequest::MergePartialFromCodedStream( // optional string parameter = 2; case 2: { - if (tag == 18u) { + if (static_cast<::google::protobuf::uint8>(tag) == + static_cast<::google::protobuf::uint8>(18u)) { DO_(::google::protobuf::internal::WireFormatLite::ReadString( input, this->mutable_parameter())); ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( @@ -831,7 +837,8 @@ bool CodeGeneratorRequest::MergePartialFromCodedStream( // optional .google.protobuf.compiler.Version compiler_version = 3; case 3: { - if (tag == 26u) { + if (static_cast<::google::protobuf::uint8>(tag) == + static_cast<::google::protobuf::uint8>(26u)) { DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( input, mutable_compiler_version())); } else { @@ -842,7 +849,8 @@ bool CodeGeneratorRequest::MergePartialFromCodedStream( // repeated .google.protobuf.FileDescriptorProto proto_file = 15; case 15: { - if (tag == 122u) { + if (static_cast<::google::protobuf::uint8>(tag) == + static_cast<::google::protobuf::uint8>(122u)) { DO_(input->IncrementRecursionDepth()); DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtualNoRecursionDepth( input, add_proto_file())); @@ -879,7 +887,7 @@ void CodeGeneratorRequest::SerializeWithCachedSizes( ::google::protobuf::io::CodedOutputStream* output) const { // @@protoc_insertion_point(serialize_start:google.protobuf.compiler.CodeGeneratorRequest) // repeated string file_to_generate = 1; - for (int i = 0; i < this->file_to_generate_size(); i++) { + for (int i = 0, n = this->file_to_generate_size(); i < n; i++) { ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( this->file_to_generate(i).data(), this->file_to_generate(i).length(), ::google::protobuf::internal::WireFormat::SERIALIZE, @@ -922,7 +930,7 @@ void CodeGeneratorRequest::SerializeWithCachedSizes( (void)deterministic; // Unused // @@protoc_insertion_point(serialize_to_array_start:google.protobuf.compiler.CodeGeneratorRequest) // repeated string file_to_generate = 1; - for (int i = 0; i < this->file_to_generate_size(); i++) { + for (int i = 0, n = this->file_to_generate_size(); i < n; i++) { ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( this->file_to_generate(i).data(), this->file_to_generate(i).length(), ::google::protobuf::internal::WireFormat::SERIALIZE, @@ -976,7 +984,7 @@ size_t CodeGeneratorRequest::ByteSizeLong() const { // repeated string file_to_generate = 1; total_size += 1 * ::google::protobuf::internal::FromIntSize(this->file_to_generate_size()); - for (int i = 0; i < this->file_to_generate_size(); i++) { + for (int i = 0, n = this->file_to_generate_size(); i < n; i++) { total_size += ::google::protobuf::internal::WireFormatLite::StringSize( this->file_to_generate(i)); } @@ -1107,6 +1115,12 @@ void CodeGeneratorRequest::set_file_to_generate(int index, const ::std::string& // @@protoc_insertion_point(field_set:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) file_to_generate_.Mutable(index)->assign(value); } +#if LANG_CXX11 +void CodeGeneratorRequest::set_file_to_generate(int index, ::std::string&& value) { + // @@protoc_insertion_point(field_set:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) + file_to_generate_.Mutable(index)->assign(std::move(value)); +} +#endif void CodeGeneratorRequest::set_file_to_generate(int index, const char* value) { file_to_generate_.Mutable(index)->assign(value); // @@protoc_insertion_point(field_set_char:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) @@ -1124,6 +1138,12 @@ void CodeGeneratorRequest::add_file_to_generate(const ::std::string& value) { file_to_generate_.Add()->assign(value); // @@protoc_insertion_point(field_add:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) } +#if LANG_CXX11 +void CodeGeneratorRequest::add_file_to_generate(::std::string&& value) { + file_to_generate_.Add()->assign(std::move(value)); + // @@protoc_insertion_point(field_add:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) +} +#endif void CodeGeneratorRequest::add_file_to_generate(const char* value) { file_to_generate_.Add()->assign(value); // @@protoc_insertion_point(field_add_char:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) @@ -1392,7 +1412,8 @@ bool CodeGeneratorResponse_File::MergePartialFromCodedStream( switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional string name = 1; case 1: { - if (tag == 10u) { + if (static_cast<::google::protobuf::uint8>(tag) == + static_cast<::google::protobuf::uint8>(10u)) { DO_(::google::protobuf::internal::WireFormatLite::ReadString( input, this->mutable_name())); ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( @@ -1407,7 +1428,8 @@ bool CodeGeneratorResponse_File::MergePartialFromCodedStream( // optional string insertion_point = 2; case 2: { - if (tag == 18u) { + if (static_cast<::google::protobuf::uint8>(tag) == + static_cast<::google::protobuf::uint8>(18u)) { DO_(::google::protobuf::internal::WireFormatLite::ReadString( input, this->mutable_insertion_point())); ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( @@ -1422,7 +1444,8 @@ bool CodeGeneratorResponse_File::MergePartialFromCodedStream( // optional string content = 15; case 15: { - if (tag == 122u) { + if (static_cast<::google::protobuf::uint8>(tag) == + static_cast<::google::protobuf::uint8>(122u)) { DO_(::google::protobuf::internal::WireFormatLite::ReadString( input, this->mutable_content())); ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( @@ -1932,7 +1955,8 @@ bool CodeGeneratorResponse::MergePartialFromCodedStream( switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { // optional string error = 1; case 1: { - if (tag == 10u) { + if (static_cast<::google::protobuf::uint8>(tag) == + static_cast<::google::protobuf::uint8>(10u)) { DO_(::google::protobuf::internal::WireFormatLite::ReadString( input, this->mutable_error())); ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( @@ -1947,7 +1971,8 @@ bool CodeGeneratorResponse::MergePartialFromCodedStream( // repeated .google.protobuf.compiler.CodeGeneratorResponse.File file = 15; case 15: { - if (tag == 122u) { + if (static_cast<::google::protobuf::uint8>(tag) == + static_cast<::google::protobuf::uint8>(122u)) { DO_(input->IncrementRecursionDepth()); DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtualNoRecursionDepth( input, add_file())); diff --git a/src/google/protobuf/compiler/plugin.pb.h b/src/google/protobuf/compiler/plugin.pb.h index 8465f0bd..1b91daca 100644 --- a/src/google/protobuf/compiler/plugin.pb.h +++ b/src/google/protobuf/compiler/plugin.pb.h @@ -356,10 +356,16 @@ class LIBPROTOC_EXPORT CodeGeneratorRequest : public ::google::protobuf::Message const ::std::string& file_to_generate(int index) const; ::std::string* mutable_file_to_generate(int index); void set_file_to_generate(int index, const ::std::string& value); + #if LANG_CXX11 + void set_file_to_generate(int index, ::std::string&& value); + #endif void set_file_to_generate(int index, const char* value); void set_file_to_generate(int index, const char* value, size_t size); ::std::string* add_file_to_generate(); void add_file_to_generate(const ::std::string& value); + #if LANG_CXX11 + void add_file_to_generate(::std::string&& value); + #endif void add_file_to_generate(const char* value); void add_file_to_generate(const char* value, size_t size); const ::google::protobuf::RepeatedPtrField< ::std::string>& file_to_generate() const; @@ -839,6 +845,12 @@ inline void CodeGeneratorRequest::set_file_to_generate(int index, const ::std::s // @@protoc_insertion_point(field_set:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) file_to_generate_.Mutable(index)->assign(value); } +#if LANG_CXX11 +inline void CodeGeneratorRequest::set_file_to_generate(int index, ::std::string&& value) { + // @@protoc_insertion_point(field_set:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) + file_to_generate_.Mutable(index)->assign(std::move(value)); +} +#endif inline void CodeGeneratorRequest::set_file_to_generate(int index, const char* value) { file_to_generate_.Mutable(index)->assign(value); // @@protoc_insertion_point(field_set_char:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) @@ -856,6 +868,12 @@ inline void CodeGeneratorRequest::add_file_to_generate(const ::std::string& valu file_to_generate_.Add()->assign(value); // @@protoc_insertion_point(field_add:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) } +#if LANG_CXX11 +inline void CodeGeneratorRequest::add_file_to_generate(::std::string&& value) { + file_to_generate_.Add()->assign(std::move(value)); + // @@protoc_insertion_point(field_add:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) +} +#endif inline void CodeGeneratorRequest::add_file_to_generate(const char* value) { file_to_generate_.Add()->assign(value); // @@protoc_insertion_point(field_add_char:google.protobuf.compiler.CodeGeneratorRequest.file_to_generate) |