diff options
author | Jisi Liu <jisi.liu@gmail.com> | 2017-07-18 15:38:30 -0700 |
---|---|---|
committer | Jisi Liu <jisi.liu@gmail.com> | 2017-07-18 15:38:30 -0700 |
commit | 09354db1434859a31a3c81abebcc4018d42f2715 (patch) | |
tree | b87c7cdc2255e6c8062ab92b4082665cd698d753 /src/google/protobuf/compiler | |
parent | 9053033a5076f82cf18b823c31f352e95e5bfd8d (diff) | |
download | protobuf-09354db1434859a31a3c81abebcc4018d42f2715.tar.gz protobuf-09354db1434859a31a3c81abebcc4018d42f2715.tar.bz2 protobuf-09354db1434859a31a3c81abebcc4018d42f2715.zip |
Merge from Google internal for 3.4 release
Diffstat (limited to 'src/google/protobuf/compiler')
65 files changed, 3545 insertions, 1602 deletions
diff --git a/src/google/protobuf/compiler/code_generator.h b/src/google/protobuf/compiler/code_generator.h index b917d373..8b9f42e2 100644 --- a/src/google/protobuf/compiler/code_generator.h +++ b/src/google/protobuf/compiler/code_generator.h @@ -50,6 +50,7 @@ namespace io { class ZeroCopyOutputStream; } class FileDescriptor; namespace compiler { +class AccessInfoMap; class Version; // Defined in this file. @@ -112,7 +113,8 @@ class LIBPROTOC_EXPORT CodeGenerator { // runs. class LIBPROTOC_EXPORT GeneratorContext { public: - inline GeneratorContext() {} + inline GeneratorContext() { + } virtual ~GeneratorContext(); // Opens the given file, truncating it if it exists, and returns a @@ -148,6 +150,7 @@ class LIBPROTOC_EXPORT GeneratorContext { // this GeneratorContext. virtual void GetCompilerVersion(Version* version) const; + private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GeneratorContext); }; @@ -162,8 +165,8 @@ typedef GeneratorContext OutputDirectory; // "foo=bar,baz,qux=corge" // parses to the pairs: // ("foo", "bar"), ("baz", ""), ("qux", "corge") -extern void ParseGeneratorParameter(const string&, - std::vector<std::pair<string, string> >*); +LIBPROTOC_EXPORT void ParseGeneratorParameter(const string&, + std::vector<std::pair<string, string> >*); } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/command_line_interface.cc b/src/google/protobuf/compiler/command_line_interface.cc index 7fcd0083..afe63a77 100644 --- a/src/google/protobuf/compiler/command_line_interface.cc +++ b/src/google/protobuf/compiler/command_line_interface.cc @@ -33,6 +33,8 @@ // Sanjay Ghemawat, Jeff Dean, and others. #include <google/protobuf/compiler/command_line_interface.h> + + #include <google/protobuf/stubs/platform_macros.h> #include <stdio.h> @@ -68,19 +70,19 @@ #endif #include <google/protobuf/stubs/common.h> +#include <google/protobuf/stubs/logging.h> #include <google/protobuf/stubs/stringprintf.h> -#include <google/protobuf/compiler/importer.h> -#include <google/protobuf/compiler/code_generator.h> -#include <google/protobuf/compiler/plugin.pb.h> #include <google/protobuf/compiler/subprocess.h> #include <google/protobuf/compiler/zip_writer.h> -#include <google/protobuf/descriptor.h> -#include <google/protobuf/text_format.h> -#include <google/protobuf/dynamic_message.h> +#include <google/protobuf/compiler/plugin.pb.h> +#include <google/protobuf/compiler/code_generator.h> +#include <google/protobuf/compiler/importer.h> #include <google/protobuf/io/coded_stream.h> -#include <google/protobuf/io/zero_copy_stream_impl.h> #include <google/protobuf/io/printer.h> -#include <google/protobuf/stubs/logging.h> +#include <google/protobuf/io/zero_copy_stream_impl.h> +#include <google/protobuf/descriptor.h> +#include <google/protobuf/dynamic_message.h> +#include <google/protobuf/text_format.h> #include <google/protobuf/stubs/strutil.h> #include <google/protobuf/stubs/substitute.h> #include <google/protobuf/stubs/map_util.h> @@ -116,11 +118,6 @@ namespace compiler { #endif namespace { -#if defined(_WIN32) && !defined(__CYGWIN__) -static const char* kPathSeparator = ";"; -#else -static const char* kPathSeparator = ":"; -#endif static const char* kDefaultDirectDependenciesViolationMsg = "File is imported but not declared in --direct_dependencies: %s"; @@ -278,11 +275,14 @@ string PluginName(const string& plugin_prefix, const string& directive) { // strip the "--" and "_out/_opt" and add the plugin prefix. return plugin_prefix + "gen-" + directive.substr(2, directive.size() - 6); } + } // namespace // A MultiFileErrorCollector that prints errors to stderr. -class CommandLineInterface::ErrorPrinter : public MultiFileErrorCollector, - public io::ErrorCollector { +class CommandLineInterface::ErrorPrinter + : public MultiFileErrorCollector, + public io::ErrorCollector, + public DescriptorPool::ErrorCollector { public: ErrorPrinter(ErrorFormat format, DiskSourceTree *tree = NULL) : format_(format), tree_(tree), found_errors_(false) {} @@ -309,6 +309,25 @@ class CommandLineInterface::ErrorPrinter : public MultiFileErrorCollector, AddErrorOrWarning("input", line, column, message, "warning", std::clog); } + // implements DescriptorPool::ErrorCollector------------------------- + void AddError( + const string& filename, + const string& element_name, + const Message* descriptor, + ErrorLocation location, + const string& message) { + AddErrorOrWarning(filename, -1, -1, message, "error", std::cerr); + } + + void AddWarning( + const string& filename, + const string& element_name, + const Message* descriptor, + ErrorLocation location, + const string& message) { + AddErrorOrWarning(filename, -1, -1, message, "warning", std::clog); + } + bool FoundErrors() const { return found_errors_; } private: @@ -410,6 +429,13 @@ class CommandLineInterface::MemoryOutputStream virtual int64 ByteCount() const { return inner_->ByteCount(); } private: + // Checks to see if "filename_.meta" exists in directory_; if so, fixes the + // offsets in that GeneratedCodeInfo record to reflect bytes inserted in + // filename_ at original offset insertion_offset with length insertion_length. + // We assume that insertions will not occur within any given annotated span + // of text. + void UpdateMetadata(size_t insertion_offset, size_t insertion_length); + // Where to insert the string when it's done. GeneratorContextImpl* directory_; string filename_; @@ -430,8 +456,7 @@ class CommandLineInterface::MemoryOutputStream CommandLineInterface::GeneratorContextImpl::GeneratorContextImpl( const std::vector<const FileDescriptor*>& parsed_files) : parsed_files_(parsed_files), - had_error_(false) { -} + had_error_(false) {} CommandLineInterface::GeneratorContextImpl::~GeneratorContextImpl() { STLDeleteValues(&files_); @@ -607,6 +632,44 @@ CommandLineInterface::MemoryOutputStream::MemoryOutputStream( inner_(new io::StringOutputStream(&data_)) { } +void CommandLineInterface::MemoryOutputStream::UpdateMetadata( + size_t insertion_offset, size_t insertion_length) { + std::map<string, string*>::iterator meta_file = + directory_->files_.find(filename_ + ".meta"); + if (meta_file == directory_->files_.end() || !meta_file->second) { + // No metadata was recorded for this file. + return; + } + string* encoded_data = meta_file->second; + GeneratedCodeInfo metadata; + bool is_text_format = false; + if (!metadata.ParseFromString(*encoded_data)) { + if (!TextFormat::ParseFromString(*encoded_data, &metadata)) { + // The metadata is invalid. + std::cerr << filename_ + << ".meta: Could not parse metadata as wire or text format." + << std::endl; + return; + } + // Generators that use the public plugin interface emit text-format + // metadata (because in the public plugin protocol, file content must be + // UTF8-encoded strings). + is_text_format = true; + } + for (int i = 0; i < metadata.annotation_size(); ++i) { + GeneratedCodeInfo::Annotation* annotation = metadata.mutable_annotation(i); + if (annotation->begin() >= insertion_offset) { + annotation->set_begin(annotation->begin() + insertion_length); + annotation->set_end(annotation->end() + insertion_length); + } + } + if (is_text_format) { + TextFormat::PrintToString(metadata, encoded_data); + } else { + metadata.SerializeToString(encoded_data); + } +} + CommandLineInterface::MemoryOutputStream::~MemoryOutputStream() { // Make sure all data has been written. inner_.reset(); @@ -684,6 +747,7 @@ CommandLineInterface::MemoryOutputStream::~MemoryOutputStream() { if (indent_.empty()) { // No indent. This makes things easier. target->insert(pos, data_); + UpdateMetadata(pos, data_.size()); } else { // Calculate how much space we need. int indent_size = 0; @@ -693,6 +757,7 @@ CommandLineInterface::MemoryOutputStream::~MemoryOutputStream() { // Make a hole for it. target->insert(pos, data_.size() + indent_size, '\0'); + UpdateMetadata(pos, data_.size() + indent_size); // Now copy in the data. string::size_type data_pos = 0; @@ -720,6 +785,12 @@ CommandLineInterface::MemoryOutputStream::~MemoryOutputStream() { // =================================================================== +#if defined(_WIN32) && !defined(__CYGWIN__) +const char* const CommandLineInterface::kPathSeparator = ";"; +#else +const char* const CommandLineInterface::kPathSeparator = ":"; +#endif + CommandLineInterface::CommandLineInterface() : mode_(MODE_COMPILE), print_mode_(PRINT_NONE), @@ -729,9 +800,7 @@ CommandLineInterface::CommandLineInterface() kDefaultDirectDependenciesViolationMsg), imports_in_descriptor_set_(false), source_info_in_descriptor_set_(false), - disallow_services_(false), - inputs_are_proto_path_relative_(false) { -} + disallow_services_(false) {} CommandLineInterface::~CommandLineInterface() {} void CommandLineInterface::RegisterGenerator(const string& flag_name, @@ -772,61 +841,39 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) { break; } - AddDefaultProtoPaths(&proto_path_); - - // Set up the source tree. - DiskSourceTree source_tree; - for (int i = 0; i < proto_path_.size(); i++) { - source_tree.MapPath(proto_path_[i].first, proto_path_[i].second); - } - - // Map input files to virtual paths if necessary. - if (!inputs_are_proto_path_relative_) { - if (!MakeInputsBeProtoPathRelative(&source_tree)) { + std::vector<const FileDescriptor*> parsed_files; + // null unless descriptor_set_in_names_.empty() + google::protobuf::scoped_ptr<DiskSourceTree> disk_source_tree; + google::protobuf::scoped_ptr<ErrorPrinter> error_collector; + google::protobuf::scoped_ptr<DescriptorPool> descriptor_pool; + google::protobuf::scoped_ptr<DescriptorDatabase> descriptor_database; + if (descriptor_set_in_names_.empty()) { + disk_source_tree.reset(new DiskSourceTree()); + if (!InitializeDiskSourceTree(disk_source_tree.get())) { return 1; } - } - - // Allocate the Importer. - ErrorPrinter error_collector(error_format_, &source_tree); - Importer importer(&source_tree, &error_collector); - - std::vector<const FileDescriptor*> parsed_files; + error_collector.reset( + new ErrorPrinter(error_format_, disk_source_tree.get())); - // Parse each file. - for (int i = 0; i < input_files_.size(); i++) { - // Import the file. - importer.AddUnusedImportTrackFile(input_files_[i]); - const FileDescriptor* parsed_file = importer.Import(input_files_[i]); - importer.ClearUnusedImportTrackFiles(); - if (parsed_file == NULL) return 1; - parsed_files.push_back(parsed_file); + SourceTreeDescriptorDatabase* database = + new SourceTreeDescriptorDatabase(disk_source_tree.get()); + database->RecordErrorsTo(error_collector.get()); + descriptor_database.reset(database); + descriptor_pool.reset(new DescriptorPool( + descriptor_database.get(), database->GetValidationErrorCollector())); + } else { + error_collector.reset(new ErrorPrinter(error_format_)); - // Enforce --disallow_services. - if (disallow_services_ && parsed_file->service_count() > 0) { - std::cerr << parsed_file->name() << ": This file contains services, but " - "--disallow_services was used." << std::endl; + SimpleDescriptorDatabase* database = new SimpleDescriptorDatabase(); + descriptor_database.reset(database); + if (!PopulateSimpleDescriptorDatabase(database)) { return 1; } - - // Enforce --direct_dependencies - if (direct_dependencies_explicitly_set_) { - bool indirect_imports = false; - for (int i = 0; i < parsed_file->dependency_count(); i++) { - if (direct_dependencies_.find(parsed_file->dependency(i)->name()) == - direct_dependencies_.end()) { - indirect_imports = true; - std::cerr << parsed_file->name() << ": " - << StringReplace(direct_dependencies_violation_msg_, "%s", - parsed_file->dependency(i)->name(), - true /* replace_all */) - << std::endl; - } - } - if (indirect_imports) { - return 1; - } - } + descriptor_pool.reset(new DescriptorPool(database, error_collector.get())); + } + descriptor_pool->EnforceWeakDependencies(true); + if (!ParseInputFiles(descriptor_pool.get(), &parsed_files)) { + return 1; } // We construct a separate GeneratorContext for each output location. Note @@ -879,15 +926,16 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) { } if (!dependency_out_name_.empty()) { + GOOGLE_DCHECK(disk_source_tree.get()); if (!GenerateDependencyManifestFile(parsed_files, output_directories, - &source_tree)) { + disk_source_tree.get())) { return 1; } } STLDeleteValues(&output_directories); - if (!descriptor_set_name_.empty()) { + if (!descriptor_set_out_name_.empty()) { if (!WriteDescriptorSet(parsed_files)) { return 1; } @@ -906,13 +954,13 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) { return 1; } } else { - if (!EncodeOrDecode(importer.pool())) { + if (!EncodeOrDecode(descriptor_pool.get())) { return 1; } } } - if (error_collector.FoundErrors()) { + if (error_collector->FoundErrors()) { return 1; } @@ -928,7 +976,7 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) { break; case PRINT_NONE: GOOGLE_LOG(ERROR) << "If the code reaches here, it usually means a bug of " - "flag parsing in the CommonadLineInterface."; + "flag parsing in the CommandLineInterface."; return 1; // Do not add a default case. @@ -938,6 +986,112 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) { return 0; } +bool CommandLineInterface::InitializeDiskSourceTree( + DiskSourceTree* source_tree) { + AddDefaultProtoPaths(&proto_path_); + + // Set up the source tree. + for (int i = 0; i < proto_path_.size(); i++) { + source_tree->MapPath(proto_path_[i].first, proto_path_[i].second); + } + + // Map input files to virtual paths if possible. + if (!MakeInputsBeProtoPathRelative(source_tree)) { + return false; + } + return true; +} + +bool CommandLineInterface::PopulateSimpleDescriptorDatabase( + SimpleDescriptorDatabase* database) { + for (int i = 0; i < descriptor_set_in_names_.size(); i++) { + int fd; + do { + fd = open(descriptor_set_in_names_[i].c_str(), O_RDONLY | O_BINARY); + } while (fd < 0 && errno == EINTR); + if (fd < 0) { + std::cerr << descriptor_set_in_names_[i] << ": " + << strerror(ENOENT) << std::endl; + return false; + } + + FileDescriptorSet file_descriptor_set; + bool parsed = file_descriptor_set.ParseFromFileDescriptor(fd); + if (close(fd) != 0) { + std::cerr << descriptor_set_in_names_[i] << ": close: " + << strerror(errno); + return false; + } + + if (!parsed) { + std::cerr << descriptor_set_in_names_[i] << ": Unable to parse." + << std::endl; + return false; + } + + for (int j = 0; j < file_descriptor_set.file_size(); j++) { + FileDescriptorProto previously_added_file_descriptor_proto; + if (database->FindFileByName(file_descriptor_set.file(j).name(), + &previously_added_file_descriptor_proto)) { + // already present - skip + continue; + } + if (!database->Add(file_descriptor_set.file(j))) { + return false; + } + } + } + return true; +} + +bool CommandLineInterface::ParseInputFiles( + DescriptorPool* descriptor_pool, + std::vector<const FileDescriptor*>* parsed_files) { + + // Parse each file. + for (int i = 0; i < input_files_.size(); i++) { + // Import the file. + descriptor_pool->AddUnusedImportTrackFile(input_files_[i]); + const FileDescriptor* parsed_file = + descriptor_pool->FindFileByName(input_files_[i]); + descriptor_pool->ClearUnusedImportTrackFiles(); + if (parsed_file == NULL) { + if (!descriptor_set_in_names_.empty()) { + std::cerr << input_files_[i] << ": " << strerror(ENOENT) << std::endl; + } + return false; + } + parsed_files->push_back(parsed_file); + + // Enforce --disallow_services. + if (disallow_services_ && parsed_file->service_count() > 0) { + std::cerr << parsed_file->name() << ": This file contains services, but " + "--disallow_services was used." << std::endl; + return false; + } + + // Enforce --direct_dependencies + if (direct_dependencies_explicitly_set_) { + bool indirect_imports = false; + for (int i = 0; i < parsed_file->dependency_count(); i++) { + if (direct_dependencies_.find(parsed_file->dependency(i)->name()) == + direct_dependencies_.end()) { + indirect_imports = true; + std::cerr << parsed_file->name() << ": " + << StringReplace(direct_dependencies_violation_msg_, "%s", + parsed_file->dependency(i)->name(), + true /* replace_all */) + << std::endl; + } + } + if (indirect_imports) { + return false; + } + } + } + return true; +} + void CommandLineInterface::Clear() { // Clear all members that are set by Run(). Note that we must not clear // members which are set by other methods before Run() is called. @@ -948,7 +1102,8 @@ void CommandLineInterface::Clear() { direct_dependencies_violation_msg_ = kDefaultDirectDependenciesViolationMsg; output_directives_.clear(); codec_type_.clear(); - descriptor_set_name_.clear(); + descriptor_set_in_names_.clear(); + descriptor_set_out_name_.clear(); dependency_out_name_.clear(); mode_ = MODE_COMPILE; @@ -962,6 +1117,17 @@ void CommandLineInterface::Clear() { bool CommandLineInterface::MakeInputsBeProtoPathRelative( DiskSourceTree* source_tree) { for (int i = 0; i < input_files_.size(); i++) { + // If the input file path is not a physical file path, it must be a virtual + // path. + if (access(input_files_[i].c_str(), F_OK) < 0) { + string disk_file; + if (source_tree->VirtualFileToDiskFile(input_files_[i], &disk_file)) { + return true; + } else { + std::cerr << input_files_[i] << ": " << strerror(ENOENT) << std::endl; + return false; + } + } string virtual_file, shadowing_disk_file; switch (source_tree->DiskFileToVirtualFile( input_files_[i], &virtual_file, &shadowing_disk_file)) { @@ -979,12 +1145,14 @@ bool CommandLineInterface::MakeInputsBeProtoPathRelative( case DiskSourceTree::CANNOT_OPEN: std::cerr << input_files_[i] << ": " << strerror(errno) << std::endl; return false; - case DiskSourceTree::NO_MAPPING: - // First check if the file exists at all. - if (access(input_files_[i].c_str(), F_OK) < 0) { - // File does not even exist. - std::cerr << input_files_[i] << ": " << strerror(ENOENT) << std::endl; + case DiskSourceTree::NO_MAPPING: { + // Try to interpret the path as a virtual path. + string disk_file; + if (source_tree->VirtualFileToDiskFile(input_files_[i], &disk_file)) { + return true; } else { + // The input file path can't be mapped to any --proto_path and it also + // can't be interpreted as a virtual path. std::cerr << input_files_[i] << ": File does not reside within any path " @@ -993,9 +1161,11 @@ bool CommandLineInterface::MakeInputsBeProtoPathRelative( "proto_path must be an exact prefix of the .proto file " "names -- protoc is too dumb to figure out when two paths " "(e.g. absolute and relative) are equivalent (it's harder " - "than you think)." << std::endl; + "than you think)." + << std::endl; + return false; } - return false; + } } } @@ -1085,7 +1255,7 @@ CommandLineInterface::ParseArguments(int argc, const char* const argv[]) { return PARSE_ARGUMENT_FAIL; } if (mode_ == MODE_COMPILE && output_directives_.empty() && - descriptor_set_name_.empty()) { + descriptor_set_out_name_.empty()) { std::cerr << "Missing output directives." << std::endl; return PARSE_ARGUMENT_FAIL; } @@ -1100,11 +1270,11 @@ CommandLineInterface::ParseArguments(int argc, const char* const argv[]) { << std::endl; return PARSE_ARGUMENT_FAIL; } - if (imports_in_descriptor_set_ && descriptor_set_name_.empty()) { + if (imports_in_descriptor_set_ && descriptor_set_out_name_.empty()) { std::cerr << "--include_imports only makes sense when combined with " "--descriptor_set_out." << std::endl; } - if (source_info_in_descriptor_set_ && descriptor_set_name_.empty()) { + if (source_info_in_descriptor_set_ && descriptor_set_out_name_.empty()) { std::cerr << "--include_source_info only makes sense when combined with " "--descriptor_set_out." << std::endl; } @@ -1191,11 +1361,20 @@ CommandLineInterface::InterpretArgument(const string& name, input_files_.push_back(value); } else if (name == "-I" || name == "--proto_path") { + if (!descriptor_set_in_names_.empty()) { + std::cerr << "Only one of " << name + << " and --descriptor_set_in can be specified." + << std::endl; + return PARSE_ARGUMENT_FAIL; + } + // Java's -classpath (and some other languages) delimits path components // with colons. Let's accept that syntax too just to make things more // intuitive. std::vector<string> parts = Split( - value, kPathSeparator, true); + value, + CommandLineInterface::kPathSeparator, + true); for (int i = 0; i < parts.size(); i++) { string virtual_path; @@ -1253,8 +1432,38 @@ CommandLineInterface::InterpretArgument(const string& name, } else if (name == "--direct_dependencies_violation_msg") { direct_dependencies_violation_msg_ = value; + } else if (name == "--descriptor_set_in") { + if (!descriptor_set_in_names_.empty()) { + std::cerr << name << " may only be passed once. To specify multiple " + "descriptor sets, pass them all as a single " + "parameter separated by '" + << CommandLineInterface::kPathSeparator << "'." + << std::endl; + return PARSE_ARGUMENT_FAIL; + } + if (value.empty()) { + std::cerr << name << " requires a non-empty value." << std::endl; + return PARSE_ARGUMENT_FAIL; + } + if (!proto_path_.empty()) { + std::cerr << "Only one of " << name + << " and --proto_path can be specified." + << std::endl; + return PARSE_ARGUMENT_FAIL; + } + if (!dependency_out_name_.empty()) { + std::cerr << name << " cannot be used with --dependency_out." + << std::endl; + return PARSE_ARGUMENT_FAIL; + } + + descriptor_set_in_names_ = Split( + value, + CommandLineInterface::kPathSeparator, + true); + } else if (name == "-o" || name == "--descriptor_set_out") { - if (!descriptor_set_name_.empty()) { + if (!descriptor_set_out_name_.empty()) { std::cerr << name << " may only be passed once." << std::endl; return PARSE_ARGUMENT_FAIL; } @@ -1268,7 +1477,7 @@ CommandLineInterface::InterpretArgument(const string& name, "same time." << std::endl; return PARSE_ARGUMENT_FAIL; } - descriptor_set_name_ = value; + descriptor_set_out_name_ = value; } else if (name == "--dependency_out") { if (!dependency_out_name_.empty()) { @@ -1279,6 +1488,11 @@ CommandLineInterface::InterpretArgument(const string& name, std::cerr << name << " requires a non-empty value." << std::endl; return PARSE_ARGUMENT_FAIL; } + if (!descriptor_set_in_names_.empty()) { + std::cerr << name << " cannot be used with --descriptor_set_in." + << std::endl; + return PARSE_ARGUMENT_FAIL; + } dependency_out_name_ = value; } else if (name == "--include_imports") { @@ -1318,7 +1532,7 @@ CommandLineInterface::InterpretArgument(const string& name, << std::endl; return PARSE_ARGUMENT_FAIL; } - if (!output_directives_.empty() || !descriptor_set_name_.empty()) { + if (!output_directives_.empty() || !descriptor_set_out_name_.empty()) { std::cerr << "Cannot use " << name << " and generate code or descriptors at the same time." << std::endl; @@ -1384,7 +1598,7 @@ CommandLineInterface::InterpretArgument(const string& name, << "other info at the same time." << std::endl; return PARSE_ARGUMENT_FAIL; } - if (!output_directives_.empty() || !descriptor_set_name_.empty()) { + if (!output_directives_.empty() || !descriptor_set_out_name_.empty()) { std::cerr << "Cannot use " << name << " and generate code or descriptors at the same time." << std::endl; @@ -1476,6 +1690,14 @@ void CommandLineInterface::PrintHelpText() { " pairs in text format to standard output. No\n" " PROTO_FILES should be given when using this\n" " flag.\n" +" --descriptor_set_in=FILES Specifies a delimited list of FILES\n" +" each containing a FileDescriptorSet (a\n" +" protocol buffer defined in descriptor.proto).\n" +" The FileDescriptor for each of the PROTO_FILES\n" +" provided will be loaded from these\n" +" FileDescriptorSets. If a FileDescriptor\n" +" appears multiple times, the first occurrence\n" +" will be used.\n" " -oFILE, Writes a FileDescriptorSet (a protocol buffer,\n" " --descriptor_set_out=FILE defined in descriptor.proto) containing all of\n" " the input files to FILE.\n" @@ -1655,6 +1877,7 @@ bool CommandLineInterface::GeneratePluginOutput( request.set_parameter(parameter); } + std::set<const FileDescriptor*> already_seen; for (int i = 0; i < parsed_files.size(); i++) { request.add_file_to_generate(parsed_files[i]->name()); @@ -1823,24 +2046,24 @@ bool CommandLineInterface::WriteDescriptorSet( int fd; do { - fd = open(descriptor_set_name_.c_str(), + fd = open(descriptor_set_out_name_.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); } while (fd < 0 && errno == EINTR); if (fd < 0) { - perror(descriptor_set_name_.c_str()); + perror(descriptor_set_out_name_.c_str()); return false; } io::FileOutputStream out(fd); if (!file_set.SerializeToZeroCopyStream(&out)) { - std::cerr << descriptor_set_name_ << ": " << strerror(out.GetErrno()) + std::cerr << descriptor_set_out_name_ << ": " << strerror(out.GetErrno()) << std::endl; out.Close(); return false; } if (!out.Close()) { - std::cerr << descriptor_set_name_ << ": " << strerror(out.GetErrno()) + std::cerr << descriptor_set_out_name_ << ": " << strerror(out.GetErrno()) << std::endl; return false; } diff --git a/src/google/protobuf/compiler/command_line_interface.h b/src/google/protobuf/compiler/command_line_interface.h index 997c1748..e6596575 100644 --- a/src/google/protobuf/compiler/command_line_interface.h +++ b/src/google/protobuf/compiler/command_line_interface.h @@ -52,17 +52,16 @@ namespace protobuf { class Descriptor; // descriptor.h class DescriptorPool; // descriptor.h class FileDescriptor; // descriptor.h +class FileDescriptorSet; // descriptor.h class FileDescriptorProto; // descriptor.pb.h template<typename T> class RepeatedPtrField; // repeated_field.h +class SimpleDescriptorDatabase; // descriptor_database.h -} // namespace protobuf - -namespace protobuf { namespace compiler { -class CodeGenerator; // code_generator.h -class GeneratorContext; // code_generator.h -class DiskSourceTree; // importer.h +class CodeGenerator; // code_generator.h +class GeneratorContext; // code_generator.h +class DiskSourceTree; // importer.h // This class implements the command-line interface to the protocol compiler. // It is designed to make it very easy to create a custom protocol compiler @@ -91,9 +90,21 @@ class DiskSourceTree; // importer.h // The compiler is invoked with syntax like: // protoc --cpp_out=outdir --foo_out=outdir --proto_path=src src/foo.proto // +// The .proto file to compile can be specified on the command line using either +// its physical file path, or a virtual path relative to a diretory specified +// in --proto_path. For example, for src/foo.proto, the following two protoc +// invocations work the same way: +// 1. protoc --proto_path=src src/foo.proto (physical file path) +// 2. protoc --proto_path=src foo.proto (virtual path relative to src) +// +// If a file path can be interpreted both as a physical file path and as a +// relative virtual path, the physical file path takes precendence. +// // For a full description of the command-line syntax, invoke it with --help. class LIBPROTOC_EXPORT CommandLineInterface { public: + static const char* const kPathSeparator; + CommandLineInterface(); ~CommandLineInterface(); @@ -175,17 +186,11 @@ class LIBPROTOC_EXPORT CommandLineInterface { // it calls strerror(). I'm not sure why you'd want to do this anyway. int Run(int argc, const char* const argv[]); - // Call SetInputsAreCwdRelative(true) if the input files given on the command - // line should be interpreted relative to the proto import path specified - // using --proto_path or -I flags. Otherwise, input file names will be - // interpreted relative to the current working directory (or as absolute - // paths if they start with '/'), though they must still reside inside - // a directory given by --proto_path or the compiler will fail. The latter - // mode is generally more intuitive and easier to use, especially e.g. when - // defining implicit rules in Makefiles. - void SetInputsAreProtoPathRelative(bool enable) { - inputs_are_proto_path_relative_ = enable; - } + // DEPRECATED. Calling this method has no effect. Protocol compiler now + // always try to find the .proto file relative to the current directory + // first and if the file is not found, it will then treat the input path + // as a virutal path. + void SetInputsAreProtoPathRelative(bool /* enable */) {} // Provides some text which will be printed when the --version flag is // used. The version of libprotoc will also be printed on the next line @@ -243,6 +248,16 @@ class LIBPROTOC_EXPORT CommandLineInterface { // Print the --help text to stderr. void PrintHelpText(); + // Loads proto_path_ into the provided source_tree. + bool InitializeDiskSourceTree(DiskSourceTree* source_tree); + + // Loads descriptor_set_in into the provided database + bool PopulateSimpleDescriptorDatabase(SimpleDescriptorDatabase* database); + + // Parses input_files_ into parsed_files + bool ParseInputFiles(DescriptorPool* descriptor_pool, + std::vector<const FileDescriptor*>* parsed_files); + // Generate the given output file from the given input. struct OutputDirective; // see below bool GenerateOutput(const std::vector<const FileDescriptor*>& parsed_files, @@ -383,9 +398,13 @@ class LIBPROTOC_EXPORT CommandLineInterface { // decoding. (Empty string indicates --decode_raw.) string codec_type_; + // If --descriptor_set_in was given, these are filenames containing + // parsed FileDescriptorSets to be used for loading protos. Otherwise, empty. + std::vector<string> descriptor_set_in_names_; + // If --descriptor_set_out was given, this is the filename to which the // FileDescriptorSet should be written. Otherwise, empty. - string descriptor_set_name_; + string descriptor_set_out_name_; // If --dependency_out was given, this is the path to the file where the // dependency file will be written. Otherwise, empty. @@ -408,9 +427,6 @@ class LIBPROTOC_EXPORT CommandLineInterface { // Was the --disallow_services flag used? bool disallow_services_; - // See SetInputsAreProtoPathRelative(). - bool inputs_are_proto_path_relative_; - GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CommandLineInterface); }; diff --git a/src/google/protobuf/compiler/command_line_interface_unittest.cc b/src/google/protobuf/compiler/command_line_interface_unittest.cc index 366e623f..c4db7f99 100644 --- a/src/google/protobuf/compiler/command_line_interface_unittest.cc +++ b/src/google/protobuf/compiler/command_line_interface_unittest.cc @@ -35,6 +35,7 @@ #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> + #ifdef _MSC_VER #include <io.h> #else @@ -56,6 +57,7 @@ #include <google/protobuf/unittest.pb.h> #include <google/protobuf/io/printer.h> #include <google/protobuf/io/zero_copy_stream.h> + #include <google/protobuf/descriptor.pb.h> #include <google/protobuf/descriptor.h> #include <google/protobuf/stubs/substitute.h> @@ -86,6 +88,7 @@ namespace compiler { #endif #endif + namespace { bool FileExists(const string& path) { @@ -129,10 +132,6 @@ class CommandLineInterfaceTest : public testing::Test { // google3. #endif // !PROTOBUF_OPENSOURCE - void SetInputsAreProtoPathRelative(bool enable) { - cli_.SetInputsAreProtoPathRelative(enable); - } - // ----------------------------------------------------------------- // Methods to check the test results (called after Run()). @@ -192,12 +191,17 @@ class CommandLineInterfaceTest : public testing::Test { const string& insertions, const string& proto_name, const string& message_name); + void CheckGeneratedAnnotations(const string& name, const string& file); void ExpectNullCodeGeneratorCalled(const string& parameter); + void ReadDescriptorSet(const string& filename, FileDescriptorSet* descriptor_set); + void WriteDescriptorSet(const string& filename, + const FileDescriptorSet* descriptor_set); + void ExpectFileContent(const string& filename, const string& content); @@ -251,11 +255,6 @@ class CommandLineInterfaceTest::NullCodeGenerator : public CodeGenerator { // =================================================================== void CommandLineInterfaceTest::SetUp() { - // Most of these tests were written before this option was added, so we - // run with the option on (which used to be the only way) except in certain - // tests where we turn it off. - cli_.SetInputsAreProtoPathRelative(true); - temp_directory_ = TestTempDir() + "/proto2_cli_test_temp"; // If the temp directory already exists, it must be left over from a @@ -281,6 +280,7 @@ void CommandLineInterfaceTest::SetUp() { mock_generators_to_delete_.push_back(generator); cli_.RegisterGenerator("--null_out", generator, "Null output."); + disallow_plugins_ = false; } @@ -471,12 +471,18 @@ void CommandLineInterfaceTest::ExpectGeneratedWithInsertions( proto_name, temp_directory_); } +void CommandLineInterfaceTest::CheckGeneratedAnnotations(const string& name, + const string& file) { + MockCodeGenerator::CheckGeneratedAnnotations(name, file, temp_directory_); +} + void CommandLineInterfaceTest::ExpectNullCodeGeneratorCalled( const string& parameter) { EXPECT_TRUE(null_generator_->called_); EXPECT_EQ(parameter, null_generator_->parameter_); } + void CommandLineInterfaceTest::ReadDescriptorSet( const string& filename, FileDescriptorSet* descriptor_set) { string path = temp_directory_ + "/" + filename; @@ -488,6 +494,13 @@ void CommandLineInterfaceTest::ReadDescriptorSet( } } +void CommandLineInterfaceTest::WriteDescriptorSet( + const string& filename, const FileDescriptorSet* descriptor_set) { + string binary_proto; + GOOGLE_CHECK(descriptor_set->SerializeToString(&binary_proto)); + CreateTempFile(filename, binary_proto); +} + void CommandLineInterfaceTest::ExpectCapturedStdout( const string& expected_text) { EXPECT_EQ(expected_text, captured_stdout_); @@ -496,7 +509,8 @@ void CommandLineInterfaceTest::ExpectCapturedStdout( void CommandLineInterfaceTest::ExpectCapturedStdoutSubstringWithZeroReturnCode( const string& expected_substring) { EXPECT_EQ(0, return_code_); - EXPECT_PRED_FORMAT2(testing::IsSubstring, expected_substring, captured_stdout_); + EXPECT_PRED_FORMAT2( + testing::IsSubstring, expected_substring, captured_stdout_); } void CommandLineInterfaceTest::ExpectFileContent( @@ -525,6 +539,22 @@ TEST_F(CommandLineInterfaceTest, BasicOutput) { ExpectGenerated("test_generator", "", "foo.proto", "Foo"); } +TEST_F(CommandLineInterfaceTest, BasicOutput_DescriptorSetIn) { + // Test that the common case works. + FileDescriptorSet file_descriptor_set; + FileDescriptorProto* file_descriptor_proto = file_descriptor_set.add_file(); + file_descriptor_proto->set_name("foo.proto"); + file_descriptor_proto->add_message_type()->set_name("Foo"); + + WriteDescriptorSet("foo.bin", &file_descriptor_set); + + Run("protocol_compiler --test_out=$tmpdir " + "--descriptor_set_in=$tmpdir/foo.bin foo.proto"); + + ExpectNoErrors(); + ExpectGenerated("test_generator", "", "foo.proto", "Foo"); +} + TEST_F(CommandLineInterfaceTest, BasicPlugin) { // Test that basic plugins work. @@ -539,6 +569,23 @@ TEST_F(CommandLineInterfaceTest, BasicPlugin) { ExpectGenerated("test_plugin", "", "foo.proto", "Foo"); } +TEST_F(CommandLineInterfaceTest, BasicPlugin_DescriptorSetIn) { + // Test that basic plugins work. + + FileDescriptorSet file_descriptor_set; + FileDescriptorProto* file_descriptor_proto = file_descriptor_set.add_file(); + file_descriptor_proto->set_name("foo.proto"); + file_descriptor_proto->add_message_type()->set_name("Foo"); + + WriteDescriptorSet("foo.bin", &file_descriptor_set); + + Run("protocol_compiler --plug_out=$tmpdir " + "--descriptor_set_in=$tmpdir/foo.bin foo.proto"); + + ExpectNoErrors(); + ExpectGenerated("test_plugin", "", "foo.proto", "Foo"); +} + TEST_F(CommandLineInterfaceTest, GeneratorAndPlugin) { // Invoke a generator and a plugin at the same time. @@ -554,6 +601,24 @@ TEST_F(CommandLineInterfaceTest, GeneratorAndPlugin) { ExpectGenerated("test_plugin", "", "foo.proto", "Foo"); } +TEST_F(CommandLineInterfaceTest, GeneratorAndPlugin_DescriptorSetIn) { + // Invoke a generator and a plugin at the same time. + + FileDescriptorSet file_descriptor_set; + FileDescriptorProto* file_descriptor_proto = file_descriptor_set.add_file(); + file_descriptor_proto->set_name("foo.proto"); + file_descriptor_proto->add_message_type()->set_name("Foo"); + + WriteDescriptorSet("foo.bin", &file_descriptor_set); + + Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir " + "--descriptor_set_in=$tmpdir/foo.bin foo.proto"); + + ExpectNoErrors(); + ExpectGenerated("test_generator", "", "foo.proto", "Foo"); + ExpectGenerated("test_plugin", "", "foo.proto", "Foo"); +} + TEST_F(CommandLineInterfaceTest, MultipleInputs) { // Test parsing multiple input files. @@ -578,6 +643,34 @@ TEST_F(CommandLineInterfaceTest, MultipleInputs) { "bar.proto", "Bar"); } +TEST_F(CommandLineInterfaceTest, MultipleInputs_DescriptorSetIn) { + // Test parsing multiple input files. + FileDescriptorSet file_descriptor_set; + + FileDescriptorProto* file_descriptor_proto = file_descriptor_set.add_file(); + file_descriptor_proto->set_name("foo.proto"); + file_descriptor_proto->add_message_type()->set_name("Foo"); + + file_descriptor_proto = file_descriptor_set.add_file(); + file_descriptor_proto->set_name("bar.proto"); + file_descriptor_proto->add_message_type()->set_name("Bar"); + + WriteDescriptorSet("foo.bin", &file_descriptor_set); + + Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir " + "--descriptor_set_in=$tmpdir/foo.bin foo.proto bar.proto"); + + ExpectNoErrors(); + ExpectGeneratedWithMultipleInputs("test_generator", "foo.proto,bar.proto", + "foo.proto", "Foo"); + ExpectGeneratedWithMultipleInputs("test_generator", "foo.proto,bar.proto", + "bar.proto", "Bar"); + ExpectGeneratedWithMultipleInputs("test_plugin", "foo.proto,bar.proto", + "foo.proto", "Foo"); + ExpectGeneratedWithMultipleInputs("test_plugin", "foo.proto,bar.proto", + "bar.proto", "Bar"); +} + TEST_F(CommandLineInterfaceTest, MultipleInputsWithImport) { // Test parsing multiple input files with an import of a separate file. @@ -608,6 +701,165 @@ TEST_F(CommandLineInterfaceTest, MultipleInputsWithImport) { "bar.proto", "Bar"); } +TEST_F(CommandLineInterfaceTest, MultipleInputsWithImport_DescriptorSetIn) { + // Test parsing multiple input files with an import of a separate file. + FileDescriptorSet file_descriptor_set; + + FileDescriptorProto* file_descriptor_proto = file_descriptor_set.add_file(); + file_descriptor_proto->set_name("foo.proto"); + file_descriptor_proto->add_message_type()->set_name("Foo"); + + file_descriptor_proto = file_descriptor_set.add_file(); + file_descriptor_proto->set_name("bar.proto"); + file_descriptor_proto->add_dependency("baz.proto"); + DescriptorProto* message = file_descriptor_proto->add_message_type(); + message->set_name("Bar"); + FieldDescriptorProto* field = message->add_field(); + field->set_type_name("Baz"); + field->set_name("a"); + field->set_number(1); + + WriteDescriptorSet("foo_and_bar.bin", &file_descriptor_set); + + file_descriptor_set.clear_file(); + file_descriptor_proto = file_descriptor_set.add_file(); + file_descriptor_proto->set_name("baz.proto"); + file_descriptor_proto->add_message_type()->set_name("Baz"); + + file_descriptor_proto = file_descriptor_set.add_file(); + file_descriptor_proto->set_name("bat.proto"); + file_descriptor_proto->add_dependency("baz.proto"); + message = file_descriptor_proto->add_message_type(); + message->set_name("Bat"); + field = message->add_field(); + field->set_type_name("Baz"); + field->set_name("a"); + field->set_number(1); + + WriteDescriptorSet("baz_and_bat.bin", &file_descriptor_set); + Run(strings::Substitute( + "protocol_compiler --test_out=$$tmpdir --plug_out=$$tmpdir " + "--descriptor_set_in=$0 foo.proto bar.proto", + string("$tmpdir/foo_and_bar.bin") + + CommandLineInterface::kPathSeparator + + "$tmpdir/baz_and_bat.bin")); + + ExpectNoErrors(); + ExpectGeneratedWithMultipleInputs("test_generator", "foo.proto,bar.proto", + "foo.proto", "Foo"); + ExpectGeneratedWithMultipleInputs("test_generator", "foo.proto,bar.proto", + "bar.proto", "Bar"); + ExpectGeneratedWithMultipleInputs("test_plugin", "foo.proto,bar.proto", + "foo.proto", "Foo"); + ExpectGeneratedWithMultipleInputs("test_plugin", "foo.proto,bar.proto", + "bar.proto", "Bar"); + + Run(strings::Substitute( + "protocol_compiler --test_out=$$tmpdir --plug_out=$$tmpdir " + "--descriptor_set_in=$0 baz.proto bat.proto", + string("$tmpdir/foo_and_bar.bin") + + CommandLineInterface::kPathSeparator + + "$tmpdir/baz_and_bat.bin")); + + ExpectNoErrors(); + ExpectGeneratedWithMultipleInputs("test_generator", "baz.proto,bat.proto", + "baz.proto", "Baz"); + ExpectGeneratedWithMultipleInputs("test_generator", "baz.proto,bat.proto", + "bat.proto", "Bat"); + ExpectGeneratedWithMultipleInputs("test_plugin", "baz.proto,bat.proto", + "baz.proto", "Baz"); + ExpectGeneratedWithMultipleInputs("test_plugin", "baz.proto,bat.proto", + "bat.proto", "Bat"); +} + +TEST_F(CommandLineInterfaceTest, + MultipleInputsWithImport_DescriptorSetIn_DuplicateFileDescriptor) { + // Test parsing multiple input files with an import of a separate file. + FileDescriptorSet file_descriptor_set; + + FileDescriptorProto foo_file_descriptor_proto; + foo_file_descriptor_proto.set_name("foo.proto"); + foo_file_descriptor_proto.add_message_type()->set_name("Foo"); + + file_descriptor_set.add_file()->CopyFrom(foo_file_descriptor_proto); + + FileDescriptorProto* file_descriptor_proto = file_descriptor_set.add_file(); + file_descriptor_proto->set_name("bar.proto"); + file_descriptor_proto->add_dependency("baz.proto"); + file_descriptor_proto->add_dependency("foo.proto"); + DescriptorProto* message = file_descriptor_proto->add_message_type(); + message->set_name("Bar"); + FieldDescriptorProto* field = message->add_field(); + field->set_type_name("Baz"); + field->set_name("a"); + field->set_number(1); + field = message->add_field(); + field->set_type_name("Foo"); + field->set_name("f"); + field->set_number(2); + WriteDescriptorSet("foo_and_bar.bin", &file_descriptor_set); + + file_descriptor_set.clear_file(); + file_descriptor_set.add_file()->CopyFrom(foo_file_descriptor_proto); + + file_descriptor_proto = file_descriptor_set.add_file(); + file_descriptor_proto->set_name("baz.proto"); + file_descriptor_proto->add_dependency("foo.proto"); + message = file_descriptor_proto->add_message_type(); + message->set_name("Baz"); + field = message->add_field(); + field->set_type_name("Foo"); + field->set_name("f"); + field->set_number(1); + WriteDescriptorSet("foo_and_baz.bin", &file_descriptor_set); + + Run(strings::Substitute( + "protocol_compiler --test_out=$$tmpdir --plug_out=$$tmpdir " + "--descriptor_set_in=$0 bar.proto", + string("$tmpdir/foo_and_bar.bin") + + CommandLineInterface::kPathSeparator + + "$tmpdir/foo_and_baz.bin")); + + ExpectNoErrors(); + ExpectGenerated("test_generator", "", "bar.proto", "Bar"); + ExpectGenerated("test_plugin", "", "bar.proto", "Bar"); +} + +TEST_F(CommandLineInterfaceTest, + MultipleInputsWithImport_DescriptorSetIn_MissingImport) { + // Test parsing multiple input files with an import of a separate file. + FileDescriptorSet file_descriptor_set; + + FileDescriptorProto* file_descriptor_proto = file_descriptor_set.add_file(); + file_descriptor_proto->set_name("foo.proto"); + file_descriptor_proto->add_message_type()->set_name("Foo"); + + file_descriptor_proto = file_descriptor_set.add_file(); + file_descriptor_proto->set_name("bar.proto"); + file_descriptor_proto->add_dependency("baz.proto"); + DescriptorProto* message = file_descriptor_proto->add_message_type(); + message->set_name("Bar"); + FieldDescriptorProto* field = message->add_field(); + field->set_type_name("Baz"); + field->set_name("a"); + field->set_number(1); + + WriteDescriptorSet("foo_and_bar.bin", &file_descriptor_set); + + file_descriptor_set.clear_file(); + file_descriptor_proto = file_descriptor_set.add_file(); + file_descriptor_proto->set_name("baz.proto"); + file_descriptor_proto->add_message_type()->set_name("Baz"); + + WriteDescriptorSet("baz.bin", &file_descriptor_set); + Run("protocol_compiler --test_out=$tmpdir --plug_out=$tmpdir " + "--descriptor_set_in=$tmpdir/foo_and_bar.bin " + "foo.proto bar.proto"); + ExpectErrorSubstring( + "bar.proto: Import \"baz.proto\" was not found or had errors."); + ExpectErrorSubstring("bar.proto: \"Baz\" is not defined."); +} + TEST_F(CommandLineInterfaceTest, CreateDirectory) { // Test that when we output to a sub-directory, it is created. @@ -754,6 +1006,25 @@ TEST_F(CommandLineInterfaceTest, Insert) { "foo.proto", "Foo"); } +TEST_F(CommandLineInterfaceTest, InsertWithAnnotationFixup) { + // Check that annotation spans are updated after insertions. + + CreateTempFile("foo.proto", + "syntax = \"proto2\";\n" + "message MockCodeGenerator_Annotate {}\n"); + + Run("protocol_compiler " + "--test_out=TestParameter:$tmpdir " + "--plug_out=TestPluginParameter:$tmpdir " + "--test_out=insert=test_generator,test_plugin:$tmpdir " + "--plug_out=insert=test_generator,test_plugin:$tmpdir " + "--proto_path=$tmpdir foo.proto"); + + ExpectNoErrors(); + CheckGeneratedAnnotations("test_generator", "foo.proto"); + CheckGeneratedAnnotations("test_plugin", "foo.proto"); +} + #if defined(_WIN32) TEST_F(CommandLineInterfaceTest, WindowsOutputPath) { @@ -839,17 +1110,11 @@ TEST_F(CommandLineInterfaceTest, ColonDelimitedPath) { "}\n"); CreateTempFile("b/foo.proto", "this should not be parsed\n"); -#undef PATH_SEPARATOR -#if defined(_WIN32) -#define PATH_SEPARATOR ";" -#else -#define PATH_SEPARATOR ":" -#endif - - Run("protocol_compiler --test_out=$tmpdir " - "--proto_path=$tmpdir/a" PATH_SEPARATOR "$tmpdir/b foo.proto"); - -#undef PATH_SEPARATOR + Run(strings::Substitute( + "protocol_compiler --test_out=$$tmpdir --proto_path=$0 foo.proto", + string("$tmpdir/a") + + CommandLineInterface::kPathSeparator + + "$tmpdir/b")); ExpectNoErrors(); ExpectGenerated("test_generator", "", "foo.proto", "Foo"); @@ -1059,8 +1324,6 @@ TEST_F(CommandLineInterfaceTest, DirectDependencies_CustomErrorMessage) { TEST_F(CommandLineInterfaceTest, CwdRelativeInputs) { // Test that we can accept working-directory-relative input files. - SetInputsAreProtoPathRelative(false); - CreateTempFile("foo.proto", "syntax = \"proto2\";\n" "message Foo {}\n"); @@ -1318,6 +1581,17 @@ TEST_F(CommandLineInterfaceTest, ParseErrors) { "foo.proto:2:1: Expected top-level statement (e.g. \"message\").\n"); } +TEST_F(CommandLineInterfaceTest, ParseErrors_DescriptorSetIn) { + // Test that parse errors are reported. + CreateTempFile("foo.bin", "not a FileDescriptorSet"); + + Run("protocol_compiler --test_out=$tmpdir " + "--descriptor_set_in=$tmpdir/foo.bin foo.proto"); + + ExpectErrorText( + "$tmpdir/foo.bin: Unable to parse.\n"); +} + TEST_F(CommandLineInterfaceTest, ParseErrorsMultipleFiles) { // Test that parse errors are reported from multiple files. @@ -1364,16 +1638,23 @@ TEST_F(CommandLineInterfaceTest, InputNotFoundError) { Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir foo.proto"); + ExpectErrorText("foo.proto: No such file or directory\n"); +} + +TEST_F(CommandLineInterfaceTest, InputNotFoundError_DescriptorSetIn) { + // Test what happens if the input file is not found. + + Run("protocol_compiler --test_out=$tmpdir " + "--descriptor_set_in=$tmpdir/foo.bin foo.proto"); + ExpectErrorText( - "foo.proto: File not found.\n"); + "$tmpdir/foo.bin: No such file or directory\n"); } TEST_F(CommandLineInterfaceTest, CwdRelativeInputNotFoundError) { // Test what happens when a working-directory-relative input file is not // found. - SetInputsAreProtoPathRelative(false); - Run("protocol_compiler --test_out=$tmpdir " "--proto_path=$tmpdir $tmpdir/foo.proto"); @@ -1385,8 +1666,6 @@ TEST_F(CommandLineInterfaceTest, CwdRelativeInputNotMappedError) { // Test what happens when a working-directory-relative input file is not // mapped to a virtual path. - SetInputsAreProtoPathRelative(false); - CreateTempFile("foo.proto", "syntax = \"proto2\";\n" "message Foo {}\n"); @@ -1411,8 +1690,6 @@ TEST_F(CommandLineInterfaceTest, CwdRelativeInputNotFoundAndNotMappedError) { // Check what happens if the input file is not found *and* is not mapped // in the proto_path. - SetInputsAreProtoPathRelative(false); - // Create a directory called "bar" so that we can point --proto_path at it. CreateTempFile("bar/dummy", ""); @@ -1427,8 +1704,6 @@ TEST_F(CommandLineInterfaceTest, CwdRelativeInputShadowedError) { // Test what happens when a working-directory-relative input file is shadowed // by another file in the virtual path. - SetInputsAreProtoPathRelative(false); - CreateTempFile("foo/foo.proto", "syntax = \"proto2\";\n" "message Foo {}\n"); @@ -1454,8 +1729,34 @@ TEST_F(CommandLineInterfaceTest, ProtoPathNotFoundError) { "--proto_path=$tmpdir/foo foo.proto"); ExpectErrorText( - "$tmpdir/foo: warning: directory does not exist.\n" - "foo.proto: File not found.\n"); + "$tmpdir/foo: warning: directory does not exist.\n" + "foo.proto: No such file or directory\n"); +} + +TEST_F(CommandLineInterfaceTest, ProtoPathAndDescriptorSetIn) { + Run("protocol_compiler --test_out=$tmpdir " + "--proto_path=$tmpdir --descriptor_set_in=$tmpdir/foo.bin foo.proto"); + ExpectErrorText( + "Only one of --descriptor_set_in and --proto_path can be specified.\n"); + + Run("protocol_compiler --test_out=$tmpdir " + "--descriptor_set_in=$tmpdir/foo.bin --proto_path=$tmpdir foo.proto"); + ExpectErrorText( + "Only one of --proto_path and --descriptor_set_in can be specified.\n"); +} + +TEST_F(CommandLineInterfaceTest, ProtoPathAndDependencyOut) { + Run("protocol_compiler --test_out=$tmpdir " + "--dependency_out=$tmpdir/manifest " + "--descriptor_set_in=$tmpdir/foo.bin foo.proto"); + ExpectErrorText( + "--descriptor_set_in cannot be used with --dependency_out.\n"); + + Run("protocol_compiler --test_out=$tmpdir " + "--descriptor_set_in=$tmpdir/foo.bin " + "--dependency_out=$tmpdir/manifest foo.proto"); + ExpectErrorText( + "--dependency_out cannot be used with --descriptor_set_in.\n"); } TEST_F(CommandLineInterfaceTest, MissingInputError) { @@ -1905,9 +2206,16 @@ TEST_F(CommandLineInterfaceTest, PrintFreeFieldNumbers) { // test as a shell script, but we'd like to be able to run the test on // platforms that don't have a Bourne-compatible shell available (especially // Windows/MSVC). -class EncodeDecodeTest : public testing::Test { + +enum EncodeDecodeTestMode { + PROTO_PATH, + DESCRIPTOR_SET_IN +}; + +class EncodeDecodeTest : public testing::TestWithParam<EncodeDecodeTestMode> { protected: virtual void SetUp() { + WriteUnittestProtoDescriptorSet(); duped_stdin_ = dup(STDIN_FILENO); } @@ -1950,7 +2258,18 @@ class EncodeDecodeTest : public testing::Test { std::vector<string> args; args.push_back("protoc"); SplitStringUsing(command, " ", &args); - args.push_back("--proto_path=" + TestSourceDir()); + switch (GetParam()) { + case PROTO_PATH: + args.push_back("--proto_path=" + TestSourceDir()); + break; + case DESCRIPTOR_SET_IN: + args.push_back(StrCat( + "--descriptor_set_in=", + unittest_proto_descriptor_set_filename_)); + break; + default: + ADD_FAILURE() << "unexpected EncodeDecodeTestMode: " << GetParam(); + } google::protobuf::scoped_array<const char * > argv(new const char* [args.size()]); for (int i = 0; i < args.size(); i++) { @@ -1958,7 +2277,6 @@ class EncodeDecodeTest : public testing::Test { } CommandLineInterface cli; - cli.SetInputsAreProtoPathRelative(true); CaptureTestStdout(); CaptureTestStderr(); @@ -1996,12 +2314,37 @@ class EncodeDecodeTest : public testing::Test { } private: + void WriteUnittestProtoDescriptorSet() { + unittest_proto_descriptor_set_filename_ = + TestTempDir() + "/unittest_proto_descriptor_set.bin"; + FileDescriptorSet file_descriptor_set; + protobuf_unittest::TestAllTypes test_all_types; + test_all_types.descriptor()->file()->CopyTo(file_descriptor_set.add_file()); + + protobuf_unittest_import::ImportMessage import_message; + import_message.descriptor()->file()->CopyTo(file_descriptor_set.add_file()); + + + protobuf_unittest_import::PublicImportMessage public_import_message; + public_import_message.descriptor()->file()->CopyTo( + file_descriptor_set.add_file()); + GOOGLE_DCHECK(file_descriptor_set.IsInitialized()); + + string binary_proto; + GOOGLE_CHECK(file_descriptor_set.SerializeToString(&binary_proto)); + GOOGLE_CHECK_OK(File::SetContents( + unittest_proto_descriptor_set_filename_, + binary_proto, + true)); + } + int duped_stdin_; string captured_stdout_; string captured_stderr_; + string unittest_proto_descriptor_set_filename_; }; -TEST_F(EncodeDecodeTest, Encode) { +TEST_P(EncodeDecodeTest, Encode) { RedirectStdinFromFile(TestSourceDir() + "/google/protobuf/" "testdata/text_format_unittest_data_oneof_implemented.txt"); EXPECT_TRUE(Run("google/protobuf/unittest.proto " @@ -2011,7 +2354,7 @@ TEST_F(EncodeDecodeTest, Encode) { ExpectStderrMatchesText(""); } -TEST_F(EncodeDecodeTest, Decode) { +TEST_P(EncodeDecodeTest, Decode) { RedirectStdinFromFile(TestSourceDir() + "/google/protobuf/testdata/golden_message_oneof_implemented"); EXPECT_TRUE(Run("google/protobuf/unittest.proto " @@ -2022,7 +2365,7 @@ TEST_F(EncodeDecodeTest, Decode) { ExpectStderrMatchesText(""); } -TEST_F(EncodeDecodeTest, Partial) { +TEST_P(EncodeDecodeTest, Partial) { RedirectStdinFromText(""); EXPECT_TRUE(Run("google/protobuf/unittest.proto " "--encode=protobuf_unittest.TestRequired")); @@ -2031,7 +2374,7 @@ TEST_F(EncodeDecodeTest, Partial) { "warning: Input message is missing required fields: a, b, c\n"); } -TEST_F(EncodeDecodeTest, DecodeRaw) { +TEST_P(EncodeDecodeTest, DecodeRaw) { protobuf_unittest::TestAllTypes message; message.set_optional_int32(123); message.set_optional_string("foo"); @@ -2045,21 +2388,24 @@ TEST_F(EncodeDecodeTest, DecodeRaw) { ExpectStderrMatchesText(""); } -TEST_F(EncodeDecodeTest, UnknownType) { +TEST_P(EncodeDecodeTest, UnknownType) { EXPECT_FALSE(Run("google/protobuf/unittest.proto " "--encode=NoSuchType")); ExpectStdoutMatchesText(""); ExpectStderrMatchesText("Type not defined: NoSuchType\n"); } -TEST_F(EncodeDecodeTest, ProtoParseError) { +TEST_P(EncodeDecodeTest, ProtoParseError) { EXPECT_FALSE(Run("google/protobuf/no_such_file.proto " "--encode=NoSuchType")); ExpectStdoutMatchesText(""); ExpectStderrMatchesText( - "google/protobuf/no_such_file.proto: File not found.\n"); + "google/protobuf/no_such_file.proto: No such file or directory\n"); } +INSTANTIATE_TEST_CASE_P(FileDescriptorSetSource, + EncodeDecodeTest, + testing::Values(PROTO_PATH, DESCRIPTOR_SET_IN)); } // anonymous namespace #endif // !GOOGLE_PROTOBUF_HEAP_CHECK_DRACONIAN diff --git a/src/google/protobuf/compiler/cpp/cpp_bootstrap_unittest.cc b/src/google/protobuf/compiler/cpp/cpp_bootstrap_unittest.cc index fce58c02..f99159f5 100644 --- a/src/google/protobuf/compiler/cpp/cpp_bootstrap_unittest.cc +++ b/src/google/protobuf/compiler/cpp/cpp_bootstrap_unittest.cc @@ -125,12 +125,9 @@ TEST(BootstrapTest, GeneratedDescriptorMatches) { importer.Import("google/protobuf/descriptor.proto"); const FileDescriptor* plugin_proto_file = importer.Import("google/protobuf/compiler/plugin.proto"); - const FileDescriptor* profile_proto_file = - importer.Import("google/protobuf/compiler/profile.proto"); EXPECT_EQ("", error_collector.text_); ASSERT_TRUE(proto_file != NULL); ASSERT_TRUE(plugin_proto_file != NULL); - ASSERT_TRUE(profile_proto_file != NULL); CppGenerator generator; MockGeneratorContext context; @@ -141,8 +138,6 @@ TEST(BootstrapTest, GeneratedDescriptorMatches) { parameter = "dllexport_decl=LIBPROTOC_EXPORT"; ASSERT_TRUE(generator.Generate(plugin_proto_file, parameter, &context, &error)); - ASSERT_TRUE(generator.Generate(profile_proto_file, parameter, - &context, &error)); context.ExpectFileMatches("google/protobuf/descriptor.pb.h", "google/protobuf/descriptor.pb.h"); @@ -152,10 +147,6 @@ TEST(BootstrapTest, GeneratedDescriptorMatches) { "google/protobuf/compiler/plugin.pb.h"); context.ExpectFileMatches("google/protobuf/compiler/plugin.pb.cc", "google/protobuf/compiler/plugin.pb.cc"); - context.ExpectFileMatches("google/protobuf/compiler/profile.pb.h", - "google/protobuf/compiler/profile.pb.h"); - context.ExpectFileMatches("google/protobuf/compiler/profile.pb.cc", - "google/protobuf/compiler/profile.pb.cc"); } } // namespace diff --git a/src/google/protobuf/compiler/cpp/cpp_enum.cc b/src/google/protobuf/compiler/cpp/cpp_enum.cc index 6a8a83d1..3b4b97e6 100644 --- a/src/google/protobuf/compiler/cpp/cpp_enum.cc +++ b/src/google/protobuf/compiler/cpp/cpp_enum.cc @@ -81,10 +81,16 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) { std::map<string, string> vars; vars["classname"] = classname_; vars["short_name"] = descriptor_->name(); - vars["enumbase"] = classname_ + (options_.proto_h ? " : int" : ""); - - printer->Print(vars, "enum $enumbase$ {\n"); - printer->Annotate("enumbase", descriptor_); + vars["enumbase"] = options_.proto_h ? " : int" : ""; + // These variables are placeholders to pick out the beginning and ends of + // identifiers for annotations (when doing so with existing variables would + // be ambiguous or impossible). They should never be set to anything but the + // empty string. + vars["{"] = ""; + vars["}"] = ""; + + printer->Print(vars, "enum $classname$$enumbase$ {\n"); + printer->Annotate("classname", descriptor_); printer->Indent(); const EnumValueDescriptor* min_value = descriptor_->value(0); @@ -102,7 +108,8 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) { " PROTOBUF_DEPRECATED" : ""; if (i > 0) printer->Print(",\n"); - printer->Print(vars, "$prefix$$name$$deprecation$ = $number$"); + printer->Print(vars, "${$$prefix$$name$$}$$deprecation$ = $number$"); + printer->Annotate("{", "}", descriptor_->value(i)); if (descriptor_->value(i)->number() < min_value->number()) { min_value = descriptor_->value(i); @@ -134,14 +141,20 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) { } printer->Print(vars, - "$dllexport$bool $classname$_IsValid(int value);\n" - "const $classname$ $prefix$$short_name$_MIN = $prefix$$min_name$;\n" - "const $classname$ $prefix$$short_name$_MAX = $prefix$$max_name$;\n"); + "$dllexport$bool $classname$_IsValid(int value);\n" + "const $classname$ ${$$prefix$$short_name$_MIN$}$ = " + "$prefix$$min_name$;\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print(vars, + "const $classname$ ${$$prefix$$short_name$_MAX$}$ = " + "$prefix$$max_name$;\n"); + printer->Annotate("{", "}", descriptor_); if (generate_array_size_) { printer->Print(vars, - "const int $prefix$$short_name$_ARRAYSIZE = " - "$prefix$$short_name$_MAX + 1;\n\n"); + "const int ${$$prefix$$short_name$_ARRAYSIZE$}$ = " + "$prefix$$short_name$_MAX + 1;\n\n"); + printer->Annotate("{", "}", descriptor_); } if (HasDescriptorMethods(descriptor_->file(), options_)) { diff --git a/src/google/protobuf/compiler/cpp/cpp_enum_field.cc b/src/google/protobuf/compiler/cpp/cpp_enum_field.cc index c15be942..e99e7e55 100644 --- a/src/google/protobuf/compiler/cpp/cpp_enum_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_enum_field.cc @@ -74,9 +74,11 @@ GeneratePrivateMembers(io::Printer* printer) const { void EnumFieldGenerator:: GenerateAccessorDeclarations(io::Printer* printer) const { + printer->Print(variables_, "$deprecated_attr$$type$ $name$() const;\n"); + printer->Annotate("name", descriptor_); printer->Print(variables_, - "$deprecated_attr$$type$ $name$() const;\n" - "$deprecated_attr$void set_$name$($type$ value);\n"); + "$deprecated_attr$void ${$set_$name$$}$($type$ value);\n"); + printer->Annotate("{", "}", descriptor_); } void EnumFieldGenerator:: @@ -113,7 +115,7 @@ GenerateMergingCode(io::Printer* printer) const { void EnumFieldGenerator:: GenerateSwappingCode(io::Printer* printer) const { - printer->Print(variables_, "std::swap($name$_, other->$name$_);\n"); + printer->Print(variables_, "swap($name$_, other->$name$_);\n"); } void EnumFieldGenerator:: @@ -257,12 +259,23 @@ GeneratePrivateMembers(io::Printer* printer) const { void RepeatedEnumFieldGenerator:: GenerateAccessorDeclarations(io::Printer* printer) const { printer->Print(variables_, - "$deprecated_attr$$type$ $name$(int index) const;\n" - "$deprecated_attr$void set_$name$(int index, $type$ value);\n" - "$deprecated_attr$void add_$name$($type$ value);\n"); + "$deprecated_attr$$type$ $name$(int index) const;\n"); + printer->Annotate("name", descriptor_); + printer->Print( + variables_, + "$deprecated_attr$void ${$set_$name$$}$(int index, $type$ value);\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print(variables_, + "$deprecated_attr$void ${$add_$name$$}$($type$ value);\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print( + variables_, + "$deprecated_attr$const ::google::protobuf::RepeatedField<int>& $name$() const;\n"); + printer->Annotate("name", descriptor_); printer->Print(variables_, - "$deprecated_attr$const ::google::protobuf::RepeatedField<int>& $name$() const;\n" - "$deprecated_attr$::google::protobuf::RepeatedField<int>* mutable_$name$();\n"); + "$deprecated_attr$::google::protobuf::RepeatedField<int>* " + "${$mutable_$name$$}$();\n"); + printer->Annotate("{", "}", descriptor_); } void RepeatedEnumFieldGenerator:: @@ -292,8 +305,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, printer->Print(variables, " $name$_.Add(value);\n" " // @@protoc_insertion_point(field_add:$full_name$)\n" - "}\n"); - printer->Print(variables, + "}\n" "$inline$const ::google::protobuf::RepeatedField<int>&\n" "$classname$::$name$() const {\n" " // @@protoc_insertion_point(field_list:$full_name$)\n" diff --git a/src/google/protobuf/compiler/cpp/cpp_extension.cc b/src/google/protobuf/compiler/cpp/cpp_extension.cc index e4fce461..6b1673b2 100644 --- a/src/google/protobuf/compiler/cpp/cpp_extension.cc +++ b/src/google/protobuf/compiler/cpp/cpp_extension.cc @@ -35,9 +35,10 @@ #include <google/protobuf/compiler/cpp/cpp_extension.h> #include <map> #include <google/protobuf/compiler/cpp/cpp_helpers.h> -#include <google/protobuf/stubs/strutil.h> #include <google/protobuf/io/printer.h> #include <google/protobuf/descriptor.pb.h> +#include <google/protobuf/stubs/strutil.h> + namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/cpp/cpp_field.cc b/src/google/protobuf/compiler/cpp/cpp_field.cc index 4480a9d2..dce9617c 100644 --- a/src/google/protobuf/compiler/cpp/cpp_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_field.cc @@ -95,6 +95,13 @@ void SetCommonFieldVariables(const FieldDescriptor* descriptor, // By default, empty string, so that generic code used for both oneofs and // singular fields can be written. (*variables)["oneof_prefix"] = ""; + + // These variables are placeholders to pick out the beginning and ends of + // identifiers for annotations (when doing so with existing variables would + // be ambiguous or impossible). They should never be set to anything but the + // empty string. + (*variables)["{"] = ""; + (*variables)["}"] = ""; } void SetCommonOneofFieldVariables(const FieldDescriptor* descriptor, @@ -194,7 +201,6 @@ const FieldGenerator& FieldGeneratorMap::get( return *field_generators_[field->index()]; } - } // namespace cpp } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/cpp/cpp_field.h b/src/google/protobuf/compiler/cpp/cpp_field.h index 00dc25d4..d9dd3850 100644 --- a/src/google/protobuf/compiler/cpp/cpp_field.h +++ b/src/google/protobuf/compiler/cpp/cpp_field.h @@ -180,10 +180,6 @@ class FieldGenerator { virtual void GenerateDefaultInstanceAllocator(io::Printer* /*printer*/) const {} - // Generate code that should be run when ShutdownProtobufLibrary() is called, - // to delete all dynamically-allocated objects. - virtual void GenerateShutdownCode(io::Printer* /*printer*/) const {} - // Generate lines to decode this field, which will be placed inside the // message's MergeFromCodedStream() method. virtual void GenerateMergeFromCodedStream(io::Printer* printer) const = 0; @@ -233,7 +229,6 @@ class FieldGeneratorMap { GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap); }; - } // namespace cpp } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/cpp/cpp_file.cc b/src/google/protobuf/compiler/cpp/cpp_file.cc index e0542ae8..c56e26f7 100644 --- a/src/google/protobuf/compiler/cpp/cpp_file.cc +++ b/src/google/protobuf/compiler/cpp/cpp_file.cc @@ -126,12 +126,20 @@ FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options) new EnumGenerator(file->enum_type(i), options)); enum_generators_.push_back(enum_generators_owner_[i].get()); } + for (int i = 0; i < enum_generators_.size(); i++) { + enum_generators_[i]->index_in_metadata_ = i; + } for (int i = 0; i < file->service_count(); i++) { service_generators_owner_[i].reset( new ServiceGenerator(file->service(i), options)); service_generators_.push_back(service_generators_owner_[i].get()); } + if (HasGenericServices(file_, options_)) { + for (int i = 0; i < service_generators_.size(); i++) { + service_generators_[i]->index_in_metadata_ = i; + } + } for (int i = 0; i < file->extension_count(); i++) { extension_generators_owner_[i].reset( @@ -526,11 +534,10 @@ class FileGenerator::ForwardDeclarations { void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) { // AddDescriptors() is a file-level procedure which adds the encoded // FileDescriptorProto for this .proto file to the global DescriptorPool for - // generated files (DescriptorPool::generated_pool()). It either runs at - // static initialization time (by default) or when default_instance() is - // called for the first time (in LITE_RUNTIME mode with - // GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER flag enabled). This procedure also - // constructs default instances and registers extensions. + // generated files (DescriptorPool::generated_pool()). It ordinarily runs at + // static initialization time, but is not used at all in LITE_RUNTIME mode + // except when extensions are used. This procedure also constructs default + // instances and registers extensions. // // Its sibling, AssignDescriptors(), actually pulls the compiled // FileDescriptor from the DescriptorPool and uses it to populate all of @@ -546,7 +553,8 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) { // table-driven parsing. printer->Print("PROTOBUF_CONSTEXPR_VAR ::google::protobuf::internal::ParseTableField\n" - " const TableStruct::entries[] = {\n"); + " const TableStruct::entries[] " + "GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n"); printer->Indent(); std::vector<size_t> entries; @@ -567,7 +575,8 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) { "};\n" "\n" "PROTOBUF_CONSTEXPR_VAR ::google::protobuf::internal::AuxillaryParseTableField\n" - " const TableStruct::aux[] = {\n"); + " const TableStruct::aux[] " + "GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n"); printer->Indent(); std::vector<size_t> aux_entries; @@ -586,7 +595,8 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) { printer->Print( "};\n" "PROTOBUF_CONSTEXPR_VAR ::google::protobuf::internal::ParseTable const\n" - " TableStruct::schema[] = {\n"); + " TableStruct::schema[] " + "GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n"); printer->Indent(); size_t offset = 0; @@ -606,9 +616,47 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) { "};\n" "\n"); + if (!message_generators_.empty() && options_.table_driven_serialization) { + printer->Print( + "const ::google::protobuf::internal::FieldMetadata TableStruct::field_metadata[] " + "= {\n"); + printer->Indent(); + std::vector<int> field_metadata_offsets; + int idx = 0; + for (int i = 0; i < message_generators_.size(); i++) { + field_metadata_offsets.push_back(idx); + idx += message_generators_[i]->GenerateFieldMetadata(printer); + } + field_metadata_offsets.push_back(idx); + printer->Outdent(); + printer->Print( + "};\n" + "const ::google::protobuf::internal::SerializationTable " + "TableStruct::serialization_table[] = {\n"); + printer->Indent(); + // We rely on the order we layout the tables to match the order we + // calculate them with FlattenMessagesInFile, so we check here that + // these match exactly. + std::vector<const Descriptor*> calculated_order = + FlattenMessagesInFile(file_); + GOOGLE_CHECK_EQ(calculated_order.size(), message_generators_.size()); + for (int i = 0; i < message_generators_.size(); i++) { + GOOGLE_CHECK_EQ(calculated_order[i], message_generators_[i]->descriptor_); + printer->Print( + "{$num_fields$, TableStruct::field_metadata + $index$},\n", + "classname", message_generators_[i]->classname_, "num_fields", + SimpleItoa(field_metadata_offsets[i + 1] - field_metadata_offsets[i]), + "index", SimpleItoa(field_metadata_offsets[i])); + } + printer->Outdent(); + printer->Print( + "};\n" + "\n"); + } if (HasDescriptorMethods(file_, options_)) { if (!message_generators_.empty()) { - printer->Print("const ::google::protobuf::uint32 TableStruct::offsets[] = {\n"); + printer->Print("const ::google::protobuf::uint32 TableStruct::offsets[] " + "GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n"); printer->Indent(); std::vector<std::pair<size_t, size_t> > pairs; for (int i = 0; i < message_generators_.size(); i++) { @@ -617,8 +665,8 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) { printer->Outdent(); printer->Print( "};\n" - "\n" - "static const ::google::protobuf::internal::MigrationSchema schemas[] = {\n"); + "static const ::google::protobuf::internal::MigrationSchema schemas[] " + "GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n"); printer->Indent(); { int offset = 0; @@ -649,7 +697,7 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) { // we still need these symbols to exist printer->Print( // MSVC doesn't like empty arrays, so we add a dummy. - "const ::google::protobuf::uint32 TableStruct::offsets[] = { ~0u };\n" + "const ::google::protobuf::uint32 TableStruct::offsets[1] = {};\n" "static const ::google::protobuf::internal::MigrationSchema* schemas = NULL;\n" "static const ::google::protobuf::Message* const* " "file_default_instances = NULL;\n"); @@ -739,33 +787,6 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) { "} // namespace\n"); } - // ----------------------------------------------------------------- - - // ShutdownFile(): Deletes descriptors, default instances, etc. on shutdown. - printer->Print( - "\n" - "void TableStruct::Shutdown() {\n"); - printer->Indent(); - - for (int i = 0; i < message_generators_.size(); i++) { - message_generators_[i]->GenerateShutdownCode(printer); - } - - if (HasDescriptorMethods(file_, options_)) { - for (int i = 0; i < message_generators_.size(); i++) { - if (!IsMapEntryMessage(message_generators_[i]->descriptor_)) continue; - printer->Print( - "delete file_level_metadata[$index$].reflection;\n", - "index", SimpleItoa(i)); - } - } - - printer->Outdent(); - printer->Print( - "}\n\n"); - - // ----------------------------------------------------------------- - // Now generate the InitDefaultsImpl() function. printer->Print( "void TableStruct::InitDefaultsImpl() {\n" @@ -826,7 +847,8 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) { string file_data; file_proto.SerializeToString(&file_data); - printer->Print("static const char descriptor[] = {\n"); + printer->Print("static const char descriptor[] " + "GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n"); printer->Indent(); if (file_data.size() > 66535) { @@ -877,9 +899,6 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) { file_namespace); } - printer->Print( - "::google::protobuf::internal::OnShutdown(&TableStruct::Shutdown);\n"); - printer->Outdent(); printer->Print( "}\n" @@ -889,19 +908,15 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) { " ::google::protobuf::GoogleOnceInit(&once, &AddDescriptorsImpl);\n" "}\n"); - if (!StaticInitializersForced(file_, options_)) { - printer->Print("#ifdef GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER\n"); - } - printer->Print( - // With static initializers. - "// Force AddDescriptors() to be called at static initialization time.\n" - "struct StaticDescriptorInitializer {\n" - " StaticDescriptorInitializer() {\n" - " AddDescriptors();\n" - " }\n" - "} static_descriptor_initializer;\n"); - if (!StaticInitializersForced(file_, options_)) { - printer->Print("#endif // GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER\n"); + if (StaticInitializersForced(file_, options_)) { + printer->Print( + "// Force AddDescriptors() to be called at dynamic initialization " + "time.\n" + "struct StaticDescriptorInitializer {\n" + " StaticDescriptorInitializer() {\n" + " AddDescriptors();\n" + " }\n" + "} static_descriptor_initializer;\n"); } } @@ -925,19 +940,11 @@ void FileGenerator::GenerateNamespaceClosers(io::Printer* printer) { void FileGenerator::GenerateForwardDeclarations(io::Printer* printer) { ForwardDeclarations decls; - for (int i = 0; i < file_->dependency_count(); i++) { - FileGenerator dependency(file_->dependency(i), options_); - dependency.FillForwardDeclarations(&decls); - } FillForwardDeclarations(&decls); decls.Print(printer, options_); } void FileGenerator::FillForwardDeclarations(ForwardDeclarations* decls) { - for (int i = 0; i < file_->public_dependency_count(); i++) { - FileGenerator dependency(file_->public_dependency(i), options_); - dependency.FillForwardDeclarations(decls); - } for (int i = 0; i < package_parts_.size(); i++) { decls = decls->AddOrGetNamespace(package_parts_[i]); } @@ -1033,11 +1040,11 @@ void FileGenerator::GenerateLibraryIncludes(io::Printer* printer) { "#include <google/protobuf/map.h>" " // IWYU pragma: export\n"); if (HasDescriptorMethods(file_, options_)) { - printer->Print( - "#include <google/protobuf/map_field_inl.h>\n"); + printer->Print("#include <google/protobuf/map_entry.h>\n"); + printer->Print("#include <google/protobuf/map_field_inl.h>\n"); } else { - printer->Print( - "#include <google/protobuf/map_field_lite.h>\n"); + printer->Print("#include <google/protobuf/map_entry_lite.h>\n"); + printer->Print("#include <google/protobuf/map_field_lite.h>\n"); } } @@ -1104,7 +1111,7 @@ void FileGenerator::GenerateDependencyIncludes(io::Printer* printer) { void FileGenerator::GenerateGlobalStateFunctionDeclarations( io::Printer* printer) { - // Forward-declare the AddDescriptors, AssignDescriptors, and ShutdownFile + // Forward-declare the AddDescriptors, AssignDescriptors // functions, so that we can declare them to be friends of each class. printer->Print( "\n" @@ -1115,12 +1122,14 @@ void FileGenerator::GenerateGlobalStateFunctionDeclarations( " static const ::google::protobuf::internal::AuxillaryParseTableField aux[];\n" " static const ::google::protobuf::internal::ParseTable schema[];\n" " static const ::google::protobuf::uint32 offsets[];\n" + " static const ::google::protobuf::internal::FieldMetadata field_metadata[];\n" + " static const ::google::protobuf::internal::SerializationTable " + "serialization_table[];\n" // The following function(s) need to be able to access private members of // the messages defined in the file. So we make them static members. // This is the internal implementation of InitDefaults. It should only // be called by InitDefaults which makes sure it will be called only once. " static void InitDefaultsImpl();\n" - " static void Shutdown();\n" "};\n" "void $dllexport_decl$AddDescriptors();\n" "void $dllexport_decl$InitDefaults();\n" @@ -1210,6 +1219,13 @@ void FileGenerator::GenerateInlineFunctionDefinitions(io::Printer* printer) { // dependent. printer->Print("#if !PROTOBUF_INLINE_NOT_IN_HEADERS\n"); + // TODO(gerbens) remove pragmas when gcc is no longer used. Current version + // of gcc fires a bogus error when compiled with strict-aliasing. + printer->Print( + "#ifdef __GNUC__\n" + " #pragma GCC diagnostic push\n" + " #pragma GCC diagnostic ignored \"-Wstrict-aliasing\"\n" + "#endif // __GNUC__\n"); // Generate class inline methods. for (int i = 0; i < message_generators_.size(); i++) { if (i > 0) { @@ -1219,6 +1235,10 @@ void FileGenerator::GenerateInlineFunctionDefinitions(io::Printer* printer) { message_generators_[i]->GenerateInlineMethods(printer, /* is_inline = */ true); } + printer->Print( + "#ifdef __GNUC__\n" + " #pragma GCC diagnostic pop\n" + "#endif // __GNUC__\n"); printer->Print("#endif // !PROTOBUF_INLINE_NOT_IN_HEADERS\n"); for (int i = 0; i < message_generators_.size(); i++) { @@ -1235,13 +1255,8 @@ void FileGenerator::GenerateProto2NamespaceEnumSpecializations( io::Printer* printer) { // Emit GetEnumDescriptor specializations into google::protobuf namespace: if (HasEnumDefinitions(file_)) { - // The SWIG conditional is to avoid a null-pointer dereference - // (bug 1984964) in swig-1.3.21 resulting from the following syntax: - // namespace X { void Y<Z::W>(); } - // which appears in GetEnumDescriptor() specializations. printer->Print( "\n" - "#ifndef SWIG\n" "namespace google {\nnamespace protobuf {\n" "\n"); for (int i = 0; i < enum_generators_.size(); i++) { @@ -1249,8 +1264,7 @@ void FileGenerator::GenerateProto2NamespaceEnumSpecializations( } printer->Print( "\n" - "} // namespace protobuf\n} // namespace google\n" - "#endif // SWIG\n"); + "} // namespace protobuf\n} // namespace google\n"); } } diff --git a/src/google/protobuf/compiler/cpp/cpp_generator.cc b/src/google/protobuf/compiler/cpp/cpp_generator.cc index cee31224..68abd0ef 100644 --- a/src/google/protobuf/compiler/cpp/cpp_generator.cc +++ b/src/google/protobuf/compiler/cpp/cpp_generator.cc @@ -100,6 +100,8 @@ bool CppGenerator::Generate(const FileDescriptor* file, file_options.enforce_lite = true; } else if (options[i].first == "table_driven_parsing") { file_options.table_driven_parsing = true; + } else if (options[i].first == "table_driven_serialization") { + file_options.table_driven_serialization = true; } else { *error = "Unknown generator option: " + options[i].first; return false; diff --git a/src/google/protobuf/compiler/cpp/cpp_helpers.cc b/src/google/protobuf/compiler/cpp/cpp_helpers.cc index 9cddba5c..00959796 100644 --- a/src/google/protobuf/compiler/cpp/cpp_helpers.cc +++ b/src/google/protobuf/compiler/cpp/cpp_helpers.cc @@ -37,14 +37,16 @@ #include <vector> #include <google/protobuf/stubs/hash.h> -#include <google/protobuf/compiler/cpp/cpp_helpers.h> -#include <google/protobuf/io/printer.h> #include <google/protobuf/stubs/logging.h> #include <google/protobuf/stubs/common.h> +#include <google/protobuf/compiler/cpp/cpp_helpers.h> +#include <google/protobuf/io/printer.h> #include <google/protobuf/stubs/strutil.h> #include <google/protobuf/stubs/substitute.h> + + namespace google { namespace protobuf { namespace compiler { @@ -167,6 +169,11 @@ string ClassName(const EnumDescriptor* enum_descriptor, bool qualified) { } } +string DefaultInstanceName(const Descriptor* descriptor) { + string prefix = descriptor->file()->package().empty() ? "" : "::"; + return prefix + DotsToColons(descriptor->file()->package()) + "::_" + + ClassName(descriptor, false) + "_default_instance_"; +} string DependentBaseClassTemplateName(const Descriptor* descriptor) { return ClassName(descriptor, false) + "_InternalBase"; @@ -654,6 +661,26 @@ void GenerateUtf8CheckCodeForCord(const FieldDescriptor* field, "VerifyUtf8Cord", "VerifyUTF8CordNamedField", printer); } +namespace { + +void Flatten(const Descriptor* descriptor, + std::vector<const Descriptor*>* flatten) { + for (int i = 0; i < descriptor->nested_type_count(); i++) + Flatten(descriptor->nested_type(i), flatten); + flatten->push_back(descriptor); +} + +} // namespace + +std::vector<const Descriptor*> FlattenMessagesInFile( + const FileDescriptor* file) { + std::vector<const Descriptor*> result; + for (int i = 0; i < file->message_type_count(); i++) { + Flatten(file->message_type(i), &result); + } + return result; +} + bool HasWeakFields(const Descriptor* descriptor) { return false; } diff --git a/src/google/protobuf/compiler/cpp/cpp_helpers.h b/src/google/protobuf/compiler/cpp/cpp_helpers.h index a744a865..6ae68591 100644 --- a/src/google/protobuf/compiler/cpp/cpp_helpers.h +++ b/src/google/protobuf/compiler/cpp/cpp_helpers.h @@ -67,6 +67,9 @@ extern const char kThinSeparator[]; string ClassName(const Descriptor* descriptor, bool qualified); string ClassName(const EnumDescriptor* enum_descriptor, bool qualified); +// Fully qualified name of the default_instance of this message. +string DefaultInstanceName(const Descriptor* descriptor); + // Name of the CRTP class template (for use with proto_h). // This is a class name, like "ProtoName_InternalBase". string DependentBaseClassTemplateName(const Descriptor* descriptor); @@ -159,8 +162,18 @@ string SafeFunctionName(const Descriptor* descriptor, const FieldDescriptor* field, const string& prefix); -// Returns true if unknown fields are preseved after parsing. -inline bool PreserveUnknownFields(const Descriptor* message) { +// Returns true if unknown fields are always preserved after parsing. +inline bool AlwaysPreserveUnknownFields(const FileDescriptor* file) { + return file->syntax() != FileDescriptor::SYNTAX_PROTO3; +} + +// Returns true if unknown fields are preserved after parsing. +inline bool AlwaysPreserveUnknownFields(const Descriptor* message) { + return AlwaysPreserveUnknownFields(message->file()); +} + +// Returns true if generated messages have public unknown fields accessors +inline bool PublicUnknownFieldsAccessors(const Descriptor* message) { return message->file()->syntax() != FileDescriptor::SYNTAX_PROTO3; } @@ -168,10 +181,8 @@ inline bool PreserveUnknownFields(const Descriptor* message) { ::google::protobuf::FileOptions_OptimizeMode GetOptimizeFor( const FileDescriptor* file, const Options& options); -// If PreserveUnknownFields() is true, determines whether unknown -// fields will be stored in an UnknownFieldSet or a string. -// If PreserveUnknownFields() is false, this method will not be -// used. +// Determines whether unknown fields will be stored in an UnknownFieldSet or +// a string. inline bool UseUnknownFieldSet(const FileDescriptor* file, const Options& options) { return GetOptimizeFor(file, options) != FileOptions::LITE_RUNTIME; @@ -277,6 +288,10 @@ inline ::google::protobuf::FileOptions_OptimizeMode GetOptimizeFor( : file->options().optimize_for(); } +// This orders the messages in a .pb.cc as it's outputted by file.cc +std::vector<const Descriptor*> FlattenMessagesInFile( + const FileDescriptor* file); + bool HasWeakFields(const Descriptor* desc); bool HasWeakFields(const FileDescriptor* desc); diff --git a/src/google/protobuf/compiler/cpp/cpp_map_field.cc b/src/google/protobuf/compiler/cpp/cpp_map_field.cc index 52a3b8b0..5d461401 100644 --- a/src/google/protobuf/compiler/cpp/cpp_map_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_map_field.cc @@ -150,6 +150,7 @@ GeneratePrivateMembers(io::Printer* printer) const { " $map_classname$;\n"); } printer->Print(variables_, + "private:\n" "::google::protobuf::internal::MapField$lite$<\n" " $map_classname$,\n" " $key_cpp$, $val_cpp$,\n" @@ -161,11 +162,15 @@ GeneratePrivateMembers(io::Printer* printer) const { void MapFieldGenerator:: GenerateAccessorDeclarations(io::Printer* printer) const { - printer->Print(variables_, + printer->Print( + variables_, "$deprecated_attr$const ::google::protobuf::Map< $key_cpp$, $val_cpp$ >&\n" - " $name$() const;\n" - "$deprecated_attr$::google::protobuf::Map< $key_cpp$, $val_cpp$ >*\n" - " mutable_$name$();\n"); + " $name$() const;\n"); + printer->Annotate("name", descriptor_); + printer->Print(variables_, + "$deprecated_attr$::google::protobuf::Map< $key_cpp$, $val_cpp$ >*\n" + " ${$mutable_$name$$}$();\n"); + printer->Annotate("{", "}", descriptor_); } void MapFieldGenerator:: diff --git a/src/google/protobuf/compiler/cpp/cpp_message.cc b/src/google/protobuf/compiler/cpp/cpp_message.cc index d9524f64..752673d0 100644 --- a/src/google/protobuf/compiler/cpp/cpp_message.cc +++ b/src/google/protobuf/compiler/cpp/cpp_message.cc @@ -52,6 +52,8 @@ #include <google/protobuf/io/printer.h> #include <google/protobuf/descriptor.pb.h> #include <google/protobuf/generated_message_table_driven.h> +#include <google/protobuf/generated_message_util.h> +#include <google/protobuf/map_entry_lite.h> #include <google/protobuf/wire_format.h> #include <google/protobuf/stubs/strutil.h> #include <google/protobuf/stubs/substitute.h> @@ -280,7 +282,7 @@ void OptimizePadding(std::vector<const FieldDescriptor*>* fields, enum Family { REPEATED = 0, STRING = 1, - MESSAGE = 2, + MESSAGE = 3, ZERO_INITIALIZABLE = 4, OTHER = 5, kMaxFamily @@ -452,22 +454,13 @@ bool HasPrivateHasMethod(const FieldDescriptor* field) { } -bool TableDrivenEnabled(const Descriptor* descriptor, const Options& options) { +bool TableDrivenParsingEnabled( + const Descriptor* descriptor, const Options& options) { if (!options.table_driven_parsing) { return false; } // Consider table-driven parsing. We only do this if: - // - There are no extensions - if (descriptor->extension_range_count() != 0) { - return false; - } - - // - We are not using UnknownFieldSet (part of the non-lite library). - if (UseUnknownFieldSet(descriptor->file(), options)) { - return false; - } - // - We have has_bits for fields. This avoids a check on every field we set // when are present (the common case). if (!HasFieldPresence(descriptor->file())) { @@ -482,16 +475,6 @@ bool TableDrivenEnabled(const Descriptor* descriptor, const Options& options) { max_field_number = field->number(); } - // - There are no map fields. - if (field->is_map()) { - return false; - } - - // - There are no oneof fields. - if (field->containing_oneof()) { - return false; - } - // - There are no weak fields. if (field->options().weak()) { return false; @@ -503,8 +486,10 @@ bool TableDrivenEnabled(const Descriptor* descriptor, const Options& options) { return false; } - // - Field numbers are relatively dense within the actual number of fields - if (max_field_number * table_sparseness >= descriptor->field_count()) { + // - Field numbers are relatively dense within the actual number of fields. + // We check for strictly greater than in the case where there are no fields + // (only extensions) so max_field_number == descriptor->field_count() == 0. + if (max_field_number * table_sparseness > descriptor->field_count()) { return false; } @@ -516,6 +501,31 @@ bool TableDrivenEnabled(const Descriptor* descriptor, const Options& options) { return true; } +void SetUnknkownFieldsVariable(const Descriptor* descriptor, + const Options& options, + std::map<string, string>* variables) { + if (UseUnknownFieldSet(descriptor->file(), options)) { + (*variables)["unknown_fields_type"] = "::google::protobuf::UnknownFieldSet"; + } else { + (*variables)["unknown_fields_type"] = "::std::string"; + } + if (AlwaysPreserveUnknownFields(descriptor)) { + (*variables)["have_unknown_fields"] = + "_internal_metadata_.have_unknown_fields()"; + (*variables)["unknown_fields"] = "_internal_metadata_.unknown_fields()"; + } else { + (*variables)["have_unknown_fields"] = + "(_internal_metadata_.have_unknown_fields() && " + " ::google::protobuf::internal::GetProto3PreserveUnknownsDefault())"; + (*variables)["unknown_fields"] = + "(::google::protobuf::internal::GetProto3PreserveUnknownsDefault()" + " ? _internal_metadata_.unknown_fields()" + " : _internal_metadata_.default_instance())"; + } + (*variables)["mutable_unknown_fields"] = + "_internal_metadata_.mutable_unknown_fields()"; +} + } // anonymous namespace // =================================================================== @@ -592,7 +602,7 @@ MessageGenerator::MessageGenerator(const Descriptor* descriptor, use_dependent_base_ = true; } - table_driven_ = TableDrivenEnabled(descriptor_, options_); + table_driven_ = TableDrivenParsingEnabled(descriptor_, options_); } MessageGenerator::~MessageGenerator() {} @@ -703,24 +713,29 @@ GenerateFieldAccessorDeclarations(io::Printer* printer) { } if (field->is_repeated()) { - printer->Print(vars, "$deprecated_attr$int $name$_size() const;\n"); + printer->Print(vars, "$deprecated_attr$int ${$$name$_size$}$() const;\n"); + printer->Annotate("{", "}", field); } else if (HasHasMethod(field)) { - printer->Print(vars, "$deprecated_attr$bool has_$name$() const;\n"); + printer->Print(vars, "$deprecated_attr$bool ${$has_$name$$}$() const;\n"); + printer->Annotate("{", "}", field); } else if (HasPrivateHasMethod(field)) { printer->Print(vars, - "private:\n" - "bool has_$name$() const;\n" - "public:\n"); + "private:\n" + "bool ${$has_$name$$}$() const;\n" + "public:\n"); + 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->Print(vars, "$deprecated_attr$void ${$clear_$name$$}$();\n"); + printer->Annotate("{", "}", field); } printer->Print(vars, "$deprecated_attr$static const int $constant_name$ = " "$number$;\n"); + printer->Annotate("constant_name", field); // Generate type-specific accessor declarations. field_generators_.get(field).GenerateAccessorDeclarations(printer); @@ -1074,32 +1089,39 @@ GenerateClassDefinition(io::Printer* printer) { printer->Print(" public:\n"); printer->Indent(); - printer->Print(vars, - "$classname$();\n" - "virtual ~$classname$();\n" - "\n" - "$classname$(const $classname$& from);\n" - "\n" - "inline $classname$& operator=(const $classname$& from) {\n" - " CopyFrom(from);\n" - " return *this;\n" - "}\n" - "\n"); + printer->Print( + vars, + "$classname$();\n" + "virtual ~$classname$();\n" + "\n" + "$classname$(const $classname$& from);\n" + "\n" + "inline $classname$& operator=(const $classname$& from) {\n" + " CopyFrom(from);\n" + " return *this;\n" + "}\n"); + + if (options_.table_driven_serialization) { + printer->Print( + "private:\n" + "const void* InternalGetTable() const;\n" + "public:\n" + "\n"); + } // Generate move constructor and move assignment operator for types other than // Any. - #ifdef PROTO_EXPERIMENTAL_ENABLE_MOVE if (!IsAnyMessage(descriptor_)) { printer->Print(vars, "#if LANG_CXX11\n" - "$classname$($classname$&& from)\n" + "$classname$($classname$&& from) noexcept\n" " : $classname$() {\n" " *this = ::std::move(from);\n" "}\n" "\n" - "inline $classname$& operator=($classname$&& from) {\n" + "inline $classname$& operator=($classname$&& from) noexcept {\n" " if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) {\n" - " InternalSwap(&from);\n" + " if (this != &from) InternalSwap(&from);\n" " } else {\n" " CopyFrom(from);\n" " }\n" @@ -1107,22 +1129,17 @@ GenerateClassDefinition(io::Printer* printer) { "}\n" "#endif\n"); } - #endif - if (PreserveUnknownFields(descriptor_)) { - string type = UseUnknownFieldSet(descriptor_->file(), options_) - ? "::google::protobuf::UnknownFieldSet" - : "::std::string"; - printer->Print( - "inline const $type$& unknown_fields() const {\n" - " return _internal_metadata_.unknown_fields();\n" - "}\n" - "\n" - "inline $type$* mutable_unknown_fields() {\n" - " return _internal_metadata_.mutable_unknown_fields();\n" - "}\n" - "\n", - "type", type ); + SetUnknkownFieldsVariable(descriptor_, options_, &vars); + if (PublicUnknownFieldsAccessors(descriptor_)) { + printer->Print(vars, + "inline const $unknown_fields_type$& unknown_fields() const {\n" + " return $unknown_fields$;\n" + "}\n" + "inline $unknown_fields_type$* mutable_unknown_fields() {\n" + " return $mutable_unknown_fields$;\n" + "}\n" + "\n"); } // N.B.: We exclude GetArena() when arena support is disabled, falling back on @@ -1190,7 +1207,6 @@ GenerateClassDefinition(io::Printer* printer) { " $message_index$;\n" "\n"); - if (SupportsArenas(descriptor_)) { printer->Print(vars, "void UnsafeArenaSwap($classname$* other);\n"); @@ -1214,6 +1230,9 @@ GenerateClassDefinition(io::Printer* printer) { printer->Print(vars, "void Swap($classname$* other);\n" + "friend void swap($classname$& a, $classname$& b) {\n" + " a.Swap(&b);\n" + "}\n" "\n" "// implements Message ----------------------------------------------\n" "\n" @@ -1250,9 +1269,14 @@ GenerateClassDefinition(io::Printer* printer) { "\n" "size_t ByteSizeLong() const PROTOBUF_FINAL;\n" "bool MergePartialFromCodedStream(\n" - " ::google::protobuf::io::CodedInputStream* input)$merge_partial_final$;\n" - "void SerializeWithCachedSizes(\n" - " ::google::protobuf::io::CodedOutputStream* output) const PROTOBUF_FINAL;\n"); + " ::google::protobuf::io::CodedInputStream* input)$merge_partial_final$;\n"); + if (!options_.table_driven_serialization || + descriptor_->options().message_set_wire_format()) { + printer->Print( + "void SerializeWithCachedSizes(\n" + " ::google::protobuf::io::CodedOutputStream* output) const " + "PROTOBUF_FINAL;\n"); + } // DiscardUnknownFields() is implemented in message.cc using reflections. We // need to implement this function in generated code for messages. if (!UseUnknownFieldSet(descriptor_->file(), options_)) { @@ -1278,6 +1302,9 @@ GenerateClassDefinition(io::Printer* printer) { "final", use_final); if (SupportsArenas(descriptor_)) { printer->Print( + // TODO(gerbens) Make this private! Currently people are deriving from + // protos to give access to this constructor, breaking the invariants + // we rely on. "protected:\n" "explicit $classname$(::google::protobuf::Arena* arena);\n" "private:\n" @@ -1443,7 +1470,7 @@ GenerateClassDefinition(io::Printer* printer) { if (SupportsArenas(descriptor_)) { printer->Print( - "friend class ::google::protobuf::Arena;\n" + "template <typename T> friend class ::google::protobuf::Arena::InternalHelper;\n" "typedef void InternalArenaConstructable_;\n" "typedef void DestructorSkippable_;\n"); } @@ -1639,9 +1666,44 @@ bool MessageGenerator::GenerateParseTable(io::Printer* printer, size_t offset, " $classname$, _has_bits_),\n"); } + if (descriptor_->oneof_decl_count() > 0) { + printer->Print(vars, + "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(\n" + " $classname$, _oneof_case_),\n"); + } else { + printer->Print("-1, // no _oneof_case_\n"); + } + + if (descriptor_->extension_range_count() > 0) { + printer->Print(vars, + "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, " + "_extensions_),\n"); + } else { + printer->Print("-1, // no _extensions_\n"); + } + + // TODO(ckennelly): Consolidate this with the calculation for + // AuxillaryParseTableField. + std::vector<string> package_parts; + + const Descriptor* outer = descriptor_; + while (outer->containing_type() != NULL) { + outer = outer->containing_type(); + } + + package_parts = Split( + outer->full_name(), ".", true); + // outer->full_name() contains the class itself. Remove it as it is + // used in the name of the default instance variable. + GOOGLE_DCHECK_NE(package_parts.size(), 0); + package_parts.back().clear(); + + vars["ns"] = Join(package_parts, "::"); + printer->Print(vars, "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(\n" - " $classname$, _internal_metadata_),\n"); + " $classname$, _internal_metadata_),\n" + "&::$ns$_$classname$_default_instance_,\n"); if (UseUnknownFieldSet(descriptor_->file(), options_)) { printer->Print(vars, "true,\n"); @@ -1670,6 +1732,239 @@ void MessageGenerator::GenerateSchema(io::Printer* printer, int offset, "{ $offset$, $has_bits_offsets$, sizeof($classname$)},\n"); } +namespace { + +// TODO(gerbens) remove this after the next sync with GitHub code base. +// Then the opensource testing has gained the functionality to compile +// the CalcFieldNum given the symbols defined in generated-message-util. +#ifdef OPENSOURCE_PROTOBUF_CPP_BOOTSTRAP +// We need a clean version of CalcFieldNum that doesn't use new functionality +// in the runtime, because this functionality is not yet in the opensource +// runtime + +uint32 CalculateType(uint32 type, uint32 type_class) { + return (type - 1) + type_class * 20; +} + +uint32 CalcFieldNum(const FieldDescriptor* field, const Options& options) { + bool is_a_map = IsMapEntryMessage(field->containing_type()); + int type = field->type(); + if (field->containing_oneof()) { + return CalculateType(type, 4); + } + if (field->is_packed()) { + return CalculateType(type, 3); + } else if (field->is_repeated()) { + return CalculateType(type, 2); + } else if (!HasFieldPresence(field->file()) && + field->containing_oneof() == NULL && !is_a_map) { + return CalculateType(type, 1); + } else { + return CalculateType(type, 0); + } +} + +#else +// We need to calculate for each field what function the table driven code +// should use to serialize it. This returns the index in a lookup table. +uint32 CalcFieldNum(const FieldDescriptor* field, const Options& options) { + bool is_a_map = IsMapEntryMessage(field->containing_type()); + int type = field->type(); + if (field->containing_oneof()) { + return internal::FieldMetadata::CalculateType( + type, internal::FieldMetadata::kOneOf); + } + if (field->is_packed()) { + return internal::FieldMetadata::CalculateType( + type, internal::FieldMetadata::kPacked); + } else if (field->is_repeated()) { + return internal::FieldMetadata::CalculateType( + type, internal::FieldMetadata::kRepeated); + } else if (!HasFieldPresence(field->file()) && + field->containing_oneof() == NULL && !is_a_map) { + return internal::FieldMetadata::CalculateType( + type, internal::FieldMetadata::kNoPresence); + } else { + return internal::FieldMetadata::CalculateType( + type, internal::FieldMetadata::kPresence); + } +} +#endif + +int FindMessageIndexInFile(const Descriptor* descriptor) { + std::vector<const Descriptor*> flatten = + FlattenMessagesInFile(descriptor->file()); + return std::find(flatten.begin(), flatten.end(), descriptor) - + flatten.begin(); +} + +} // namespace + +int MessageGenerator::GenerateFieldMetadata(io::Printer* printer) { + if (!options_.table_driven_serialization) { + return 0; + } + + std::vector<const FieldDescriptor*> sorted = SortFieldsByNumber(descriptor_); + if (IsMapEntryMessage(descriptor_)) { + for (int i = 0; i < 2; i++) { + const FieldDescriptor* field = sorted[i]; + uint32 tag = internal::WireFormatLite::MakeTag( + field->number(), WireFormat::WireTypeForFieldType(field->type())); + + std::map<string, string> vars; + vars["classname"] = classname_; + vars["parent_classname"] = + ClassName(descriptor_->containing_type(), false); + vars["field_name"] = FieldName(field); + vars["tag"] = SimpleItoa(tag); + vars["hasbit"] = SimpleItoa(i); + vars["type"] = SimpleItoa(CalcFieldNum(field, options_)); + vars["ptr"] = "NULL"; + if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { + GOOGLE_CHECK(!IsMapEntryMessage(field->message_type())); + { + vars["ptr"] = + QualifiedFileLevelSymbol( + field->message_type()->file()->package(), + FileLevelNamespace(field->message_type()->file()->name())) + + "::TableStruct::serialization_table + " + + SimpleItoa(FindMessageIndexInFile(field->message_type())); + } + } + vars["extra"] = HasDescriptorMethods(descriptor_->file(), options_) + ? "::SuperType" + : ""; + printer->Print(vars, + "{GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(" + "::google::protobuf::internal::MapEntryHelper<$parent_classname$::$" + "classname$$extra$>, $field_name$_), $tag$," + "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(" + "::google::protobuf::internal::MapEntryHelper<$parent_classname$::$" + "classname$$extra$>, _has_bits_) * 8 + $hasbit$, $type$, " + "$ptr$},\n"); + } + return 2; + } + printer->Print( + "{GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, " + "_cached_size_), 0, 0, 0, NULL},\n", + "classname", classname_); + std::vector<const Descriptor::ExtensionRange*> sorted_extensions; + for (int i = 0; i < descriptor_->extension_range_count(); ++i) { + sorted_extensions.push_back(descriptor_->extension_range(i)); + } + std::sort(sorted_extensions.begin(), sorted_extensions.end(), + ExtensionRangeSorter()); + for (int i = 0, extension_idx = 0; /* no range */; i++) { + for (; extension_idx < sorted_extensions.size() && + (i == sorted.size() || + sorted_extensions[extension_idx]->start < sorted[i]->number()); + extension_idx++) { + const Descriptor::ExtensionRange* range = + sorted_extensions[extension_idx]; + printer->Print( + "{GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, " + "_extensions_), $start$, $end$, " + "::google::protobuf::internal::FieldMetadata::kSpecial, " + "reinterpret_cast<const " + "void*>(::google::protobuf::internal::ExtensionSerializer)},\n", + "classname", classname_, "start", SimpleItoa(range->start), "end", + SimpleItoa(range->end)); + } + if (i == sorted.size()) break; + const FieldDescriptor* field = sorted[i]; + + uint32 tag = internal::WireFormatLite::MakeTag( + field->number(), WireFormat::WireTypeForFieldType(field->type())); + if (field->is_packed()) { + tag = internal::WireFormatLite::MakeTag( + field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED); + } + + string classfieldname = FieldName(field); + if (field->containing_oneof()) { + classfieldname = field->containing_oneof()->name(); + } + std::map<string, string> vars; + vars["classname"] = classname_; + vars["field_name"] = classfieldname; + vars["tag"] = SimpleItoa(tag); + vars["ptr"] = "NULL"; + if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { + if (IsMapEntryMessage(field->message_type())) { + vars["idx"] = SimpleItoa(FindMessageIndexInFile(field->message_type())); + vars["fieldclassname"] = ClassName(field->message_type(), false); + printer->Print(vars, + "{GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($" + "classname$, $field_name$_), $tag$, $idx$, " + "::google::protobuf::internal::FieldMetadata::kSpecial, " + "reinterpret_cast<const void*>(static_cast< " + "::google::protobuf::internal::SpecialSerializer>(" + "::google::protobuf::internal::MapFieldSerializer< " + "::google::protobuf::internal::MapEntryToMapField<$classname$::$" + "fieldclassname$>::MapFieldType, " + "TableStruct::serialization_table>))},\n"); + continue; + } else { + vars["ptr"] = + QualifiedFileLevelSymbol( + field->message_type()->file()->package(), + FileLevelNamespace(field->message_type()->file()->name())) + + "::TableStruct::serialization_table + " + + SimpleItoa(FindMessageIndexInFile(field->message_type())); + } + } + vars["type"] = SimpleItoa(CalcFieldNum(field, options_)); + + + if (field->options().weak()) { + // TODO(gerbens) merge weak fields into ranges + printer->Print(vars, + "{GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($" + "classname$, _weak_field_map_), $tag$, $tag$, " + "::google::protobuf::internal::FieldMetadata::kSpecial, " + "reinterpret_cast<const " + "void*>(::google::protobuf::internal::WeakFieldSerializer)},\n"); + } else if (field->containing_oneof()) { + vars["oneofoffset"] = + SimpleItoa(sizeof(uint32) * field->containing_oneof()->index()); + printer->Print(vars, + "{GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($" + "classname$, $field_name$_), $tag$, " + "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($" + "classname$, _oneof_case_) + $oneofoffset$, " + "$type$, $ptr$},\n"); + } else if (HasFieldPresence(descriptor_->file()) && + has_bit_indices_[field->index()] != -1) { + vars["hasbitsoffset"] = SimpleItoa(has_bit_indices_[field->index()]); + printer->Print(vars, + "{GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($" + "classname$, $field_name$_), $tag$, " + "GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($" + "classname$, _has_bits_) * 8 + $hasbitsoffset$, $type$, " + "$ptr$},\n"); + } else { + printer->Print(vars, + "{GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($" + "classname$, $field_name$_), $tag$, ~0u, $type$, " + "$ptr$},\n"); + } + } + int num_field_metadata = 1 + sorted.size() + sorted_extensions.size(); + num_field_metadata++; + string serializer = UseUnknownFieldSet(descriptor_->file(), options_) + ? "::google::protobuf::internal::UnknownFieldSetSerializer" + : "::google::protobuf::internal::UnknownFieldSerializerLite"; + printer->Print( + "{GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET($classname$, " + "_internal_metadata_), 0, ~0u, " + "::google::protobuf::internal::FieldMetadata::kSpecial, reinterpret_cast<const " + "void*>($serializer$)},\n", + "classname", classname_, "serializer", serializer); + return num_field_metadata; +} + void MessageGenerator:: GenerateDefaultInstanceAllocator(io::Printer* printer) { // Construct the default instances of all fields, as they will be used @@ -1732,25 +2027,6 @@ GenerateDefaultInstanceInitializer(io::Printer* printer) { } void MessageGenerator:: -GenerateShutdownCode(io::Printer* printer) { - if (IsMapEntryMessage(descriptor_)) return; - - printer->Print("_$classname$_default_instance_.Shutdown();\n", "classname", - classname_); - - if (HasDescriptorMethods(descriptor_->file(), options_)) { - printer->Print("delete file_level_metadata[$index$].reflection;\n", "index", - SimpleItoa(index_in_file_messages_)); - } - - // Handle default instances of fields. - for (int i = 0; i < descriptor_->field_count(); i++) { - field_generators_.get(descriptor_->field(i)) - .GenerateShutdownCode(printer); - } -} - -void MessageGenerator:: GenerateClassMethods(io::Printer* printer) { if (IsMapEntryMessage(descriptor_)) { if (HasDescriptorMethods(descriptor_->file(), options_)) { @@ -1854,6 +2130,15 @@ GenerateClassMethods(io::Printer* printer) { GenerateSwap(printer); printer->Print("\n"); + if (options_.table_driven_serialization) { + printer->Print( + "const void* $classname$::InternalGetTable() const {\n" + " return $file_namespace$::TableStruct::serialization_table + $index$;\n" + "}\n" + "\n", + "classname", classname_, "index", SimpleItoa(index_in_file_messages_), + "file_namespace", FileLevelNamespace(descriptor_->file()->name())); + } if (HasDescriptorMethods(descriptor_->file(), options_)) { printer->Print( "::google::protobuf::Metadata $classname$::GetMetadata() const {\n" @@ -1926,13 +2211,25 @@ size_t MessageGenerator::GenerateParseOffsets(io::Printer* printer) { processing_type |= static_cast<unsigned>( field->is_repeated() ? internal::kRepeatedMask : 0); + processing_type |= static_cast<unsigned>( + field->containing_oneof() ? internal::kOneofMask : 0); + + if (field->is_map()) { + processing_type = internal::TYPE_MAP; + } + const unsigned char tag_size = WireFormat::TagSize(field->number(), field->type()); std::map<string, string> vars; vars["classname"] = classname_; - vars["name"] = FieldName(field); - vars["has"] = SimpleItoa(has_bit_indices_[field->index()]); + if (field->containing_oneof() != NULL) { + vars["name"] = field->containing_oneof()->name(); + vars["presence"] = SimpleItoa(field->containing_oneof()->index()); + } else { + vars["name"] = FieldName(field); + vars["presence"] = SimpleItoa(has_bit_indices_[field->index()]); + } vars["nwtype"] = SimpleItoa(normal_wiretype); vars["pwtype"] = SimpleItoa(packed_wiretype); vars["ptype"] = SimpleItoa(processing_type); @@ -1942,7 +2239,7 @@ size_t MessageGenerator::GenerateParseOffsets(io::Printer* printer) { "{\n" " GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(\n" " $classname$, $name$_),\n" - " static_cast< ::google::protobuf::uint32>($has$),\n" + " static_cast< ::google::protobuf::uint32>($presence$),\n" " $nwtype$, $pwtype$, $ptype$, $tag_size$\n" "},\n"); } @@ -1977,7 +2274,7 @@ size_t MessageGenerator::GenerateParseAuxTable(io::Printer* printer) { printer->Print( vars, "{::google::protobuf::internal::AuxillaryParseTableField::enum_aux{" - "$type$_IsValid, \"$type$\" }},\n"); + "$type$_IsValid}},\n"); last_field_number++; break; case FieldDescriptor::CPPTYPE_MESSAGE: { @@ -1995,7 +2292,17 @@ size_t MessageGenerator::GenerateParseAuxTable(io::Printer* printer) { GOOGLE_DCHECK_NE(package_parts.size(), 0); package_parts.back().clear(); - vars["classname"] = ClassName(field->message_type(), false); + if (field->is_map()) { + vars["classname"] = ClassName(field->containing_type(), false) + + "::" + ClassName(field->message_type(), false); + printer->Print(vars, + "{::google::protobuf::internal::AuxillaryParseTableField::map_" + "aux{&::google::protobuf::internal::ParseMap<$classname$>}},\n"); + last_field_number++; + break; + } else { + vars["classname"] = ClassName(field->message_type(), false); + } vars["ns"] = Join(package_parts, "::"); vars["type"] = FieldMessageTypeName(field); vars["file_namespace"] = FileLevelNamespace(outer->file()->name()); @@ -2005,7 +2312,7 @@ size_t MessageGenerator::GenerateParseAuxTable(io::Printer* printer) { " &::$ns$_$classname$_default_instance_,\n"); bool dont_emit_table = - !TableDrivenEnabled(field->message_type(), options_); + !TableDrivenParsingEnabled(field->message_type(), options_); if (dont_emit_table) { printer->Print(" NULL,\n"); @@ -2034,16 +2341,10 @@ size_t MessageGenerator::GenerateParseAuxTable(io::Printer* printer) { break; } vars["full_name"] = field->full_name(); - vars["strict"] = - field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3 - ? "true" : "false"; - vars["type"] = field->full_name(); printer->Print(vars, "{::google::protobuf::internal::AuxillaryParseTableField::string_aux{\n" " $default$,\n" - " \"$full_name$\",\n" - " $strict$,\n" - " \"$type$\"\n" + " \"$full_name$\"\n" "}},\n"); last_field_number++; break; @@ -2103,8 +2404,7 @@ std::pair<size_t, size_t> MessageGenerator::GenerateOffsets( const FieldDescriptor* field = descriptor_->field(i); if (field->containing_oneof() || field->options().weak()) { printer->Print( - "GOOGLE_PROTOBUF_GENERATED_DEFAULT_ONEOF_FIELD_OFFSET(" - "(&_$classname$_default_instance_), $name$_),\n", + "offsetof($classname$DefaultTypeInternal, $name$_),\n", "classname", classname_, "name", FieldName(field)); } else { printer->Print( @@ -2178,6 +2478,7 @@ GenerateSharedDestructorCode(io::Printer* printer) { // 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" @@ -2384,8 +2685,7 @@ GenerateStructors(io::Printer* printer) { initializer_with_arena += ", _weak_field_map_(arena)"; } - string initializer_null; - initializer_null = ", _internal_metadata_(NULL)"; + string initializer_null = superclass + "(), _internal_metadata_(NULL)"; if (IsAnyMessage(descriptor_)) { initializer_null += ", _any_metadata_(&type_url_, &value_)"; } @@ -2395,26 +2695,22 @@ GenerateStructors(io::Printer* printer) { printer->Print( "$classname$::$classname$()\n" - " : $superclass$()$initializer$ {\n" + " : $initializer$ {\n" " if (GOOGLE_PREDICT_TRUE(this != internal_default_instance())) {\n" " $file_namespace$::InitDefaults();\n" " }\n" " SharedCtor();\n" " // @@protoc_insertion_point(constructor:$full_name$)\n" "}\n", - "classname", classname_, "superclass", superclass, "full_name", - descriptor_->full_name(), "initializer", initializer_null, - "file_namespace", FileLevelNamespace(descriptor_->file()->name())); + "classname", classname_, "full_name", descriptor_->full_name(), + "initializer", initializer_null, "file_namespace", + FileLevelNamespace(descriptor_->file()->name())); if (SupportsArenas(descriptor_)) { printer->Print( "$classname$::$classname$(::google::protobuf::Arena* arena)\n" " : $initializer$ {\n" - // When arenas are used it's safe to assume we have finished - // static init time (protos with arenas are unsafe during static init) - "#ifdef GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER\n" " $file_namespace$::InitDefaults();\n" - "#endif // GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER\n" " SharedCtor();\n" " RegisterArenaDtor(arena);\n" " // @@protoc_insertion_point(arena_constructor:$full_name$)\n" @@ -2601,7 +2897,6 @@ GenerateStructors(io::Printer* printer) { "}\n", "classname", classname_); } - } // Return the number of bits set in n, a non-negative integer. @@ -2614,6 +2909,22 @@ static int popcnt(uint32 n) { return result; } +bool MessageGenerator::MaybeGenerateOptionalFieldCondition( + io::Printer* printer, const FieldDescriptor* field, + int expected_has_bits_index) { + int has_bit_index = has_bit_indices_[field->index()]; + if (!field->options().weak() && + expected_has_bits_index == has_bit_index / 32) { + const string mask = + StrCat(strings::Hex(1u << (has_bit_index % 32), strings::ZERO_PAD_8)); + printer->Print( + "if (cached_has_bits & 0x$mask$u) {\n", + "mask", mask); + return true; + } + return false; +} + void MessageGenerator:: GenerateClear(io::Printer* printer) { printer->Print( @@ -2622,6 +2933,15 @@ GenerateClear(io::Printer* printer) { "classname", classname_, "full_name", descriptor_->full_name()); printer->Indent(); + printer->Print( + // TODO(jwb): It would be better to avoid emitting this if it is not used, + // rather than emitting a workaround for the resulting warning. + "::google::protobuf::uint32 cached_has_bits = 0;\n" + "// Prevent compiler warnings about cached_has_bits being unused\n" + "(void) cached_has_bits;\n\n"); + + int cached_has_bit_index = -1; + // Step 1: Extensions if (descriptor_->extension_range_count() > 0) { printer->Print("_extensions_.Clear();\n"); @@ -2730,9 +3050,14 @@ GenerateClear(io::Printer* printer) { GOOGLE_DCHECK_LE(2, count); GOOGLE_DCHECK_GE(8, count); + if (cached_has_bit_index != last_chunk / 4) { + cached_has_bit_index = last_chunk / 4; + printer->Print( + "cached_has_bits = _has_bits_[$idx$];\n", + "idx", SimpleItoa(cached_has_bit_index)); + } printer->Print( - "if (_has_bits_[$index$ / 32] & $mask$u) {\n", - "index", SimpleItoa(last_chunk * 8), + "if (cached_has_bits & $mask$u) {\n", "mask", SimpleItoa(last_chunk_mask)); printer->Indent(); } @@ -2778,7 +3103,12 @@ GenerateClear(io::Printer* printer) { if (should_check_bit && // If no field presence, then always clear strings/messages as well. HasFieldPresence(descriptor_->file())) { - printer->Print("if (has_$name$()) {\n", "name", fieldname); + if (!MaybeGenerateOptionalFieldCondition(printer, field, + cached_has_bit_index)) { + printer->Print( + "if (has_$name$()) {\n", + "name", fieldname); + } printer->Indent(); have_enclosing_if = true; } @@ -2814,9 +3144,7 @@ GenerateClear(io::Printer* printer) { printer->Print("_has_bits_.Clear();\n"); } - if (PreserveUnknownFields(descriptor_)) { - printer->Print("_internal_metadata_.Clear();\n"); - } + printer->Print("_internal_metadata_.Clear();\n"); printer->Outdent(); printer->Print("}\n"); @@ -2920,6 +3248,7 @@ GenerateSwap(io::Printer* printer) { printer->Print("void $classname$::InternalSwap($classname$* other) {\n", "classname", classname_); printer->Indent(); + printer->Print("using std::swap;\n"); if (HasGeneratedMethods(descriptor_->file(), options_)) { for (int i = 0; i < optimized_order_.size(); i++) { @@ -2931,24 +3260,22 @@ GenerateSwap(io::Printer* printer) { for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { printer->Print( - "std::swap($oneof_name$_, other->$oneof_name$_);\n" - "std::swap(_oneof_case_[$i$], other->_oneof_case_[$i$]);\n", + "swap($oneof_name$_, other->$oneof_name$_);\n" + "swap(_oneof_case_[$i$], other->_oneof_case_[$i$]);\n", "oneof_name", descriptor_->oneof_decl(i)->name(), "i", SimpleItoa(i)); } if (HasFieldPresence(descriptor_->file())) { for (int i = 0; i < HasBitsSize() / 4; ++i) { - printer->Print("std::swap(_has_bits_[$i$], other->_has_bits_[$i$]);\n", + printer->Print("swap(_has_bits_[$i$], other->_has_bits_[$i$]);\n", "i", SimpleItoa(i)); } } - if (PreserveUnknownFields(descriptor_)) { - printer->Print("_internal_metadata_.Swap(&other->_internal_metadata_);\n"); - } + printer->Print("_internal_metadata_.Swap(&other->_internal_metadata_);\n"); - printer->Print("std::swap(_cached_size_, other->_cached_size_);\n"); + printer->Print("swap(_cached_size_, other->_cached_size_);\n"); if (descriptor_->extension_range_count() > 0) { printer->Print("_extensions_.Swap(&other->_extensions_);\n"); } @@ -3251,21 +3578,16 @@ GenerateCopyFrom(io::Printer* printer) { void MessageGenerator:: GenerateMergeFromCodedStream(io::Printer* printer) { + std::map<string, string> vars; + SetUnknkownFieldsVariable(descriptor_, options_, &vars); if (descriptor_->options().message_set_wire_format()) { // Special-case MessageSet. - printer->Print( + vars["classname"] = classname_; + printer->Print(vars, "bool $classname$::MergePartialFromCodedStream(\n" - " ::google::protobuf::io::CodedInputStream* input) {\n", - "classname", classname_); - - printer->Print( - " return _extensions_.ParseMessageSet(input, " - "internal_default_instance(),\n" - " mutable_unknown_fields());\n", - // Vars. - "classname", classname_); - - printer->Print( + " ::google::protobuf::io::CodedInputStream* input) {\n" + " return _extensions_.ParseMessageSet(input,\n" + " internal_default_instance(), $mutable_unknown_fields$);\n" "}\n"); return; } @@ -3281,14 +3603,18 @@ GenerateMergeFromCodedStream(io::Printer* printer) { if (table_driven_) { printer->Indent(); + const string lite = UseUnknownFieldSet(descriptor_->file(), options_) ? + "" : "Lite"; + printer->Print( - "return ::google::protobuf::internal::MergePartialFromCodedStream(\n" + "return ::google::protobuf::internal::MergePartialFromCodedStream$lite$(\n" " this,\n" " $file_namespace$::TableStruct::schema[\n" " $classname$::kIndexInFileMessages],\n" " input);\n", "classname", classname_, - "file_namespace", FileLevelNamespace(descriptor_->file()->name())); + "file_namespace", FileLevelNamespace(descriptor_->file()->name()), + "lite", lite); printer->Outdent(); @@ -3300,8 +3626,7 @@ GenerateMergeFromCodedStream(io::Printer* printer) { "#define DO_(EXPRESSION) if (!GOOGLE_PREDICT_TRUE(EXPRESSION)) goto failure\n" " ::google::protobuf::uint32 tag;\n"); - if (PreserveUnknownFields(descriptor_) && - !UseUnknownFieldSet(descriptor_->file(), options_)) { + if (!UseUnknownFieldSet(descriptor_->file(), options_)) { // Use LazyStringOutputString to avoid initializing unknown fields string // unless it is actually needed. For the same reason, disable eager refresh // on the CodedOutputStream. @@ -3354,13 +3679,12 @@ GenerateMergeFromCodedStream(io::Printer* printer) { } printer->Print("::std::pair< ::google::protobuf::uint32, bool> p = " - "input->ReadTagWithCutoff$lasttag$($max$u);\n" + "input->ReadTagWithCutoffNoLastTag($max$u);\n" "tag = p.first;\n" "if (!p.second) goto handle_unusual;\n", "max", SimpleItoa(maxtag <= kCutoff0 ? kCutoff0 : (maxtag <= kCutoff1 ? kCutoff1 : - maxtag)), - "lasttag", !capture_last_tag ? "NoLastTag" : ""); + maxtag))); if (descriptor_->field_count() > 0) { // We don't even want to print the switch() if we have no fields because @@ -3395,9 +3719,11 @@ GenerateMergeFromCodedStream(io::Printer* printer) { const FieldGenerator& field_generator = field_generators_.get(field); // Emit code to parse the common, expected case. - printer->Print("if (static_cast< ::google::protobuf::uint8>(tag) ==\n" - " static_cast< ::google::protobuf::uint8>($commontag$u)) {\n", - "commontag", SimpleItoa(WireFormat::MakeTag(field))); + printer->Print( + "if (static_cast< ::google::protobuf::uint8>(tag) ==\n" + " static_cast< ::google::protobuf::uint8>($truncated$u /* $full$ & 0xFF */)) {\n", + "truncated", SimpleItoa(WireFormat::MakeTag(field) & 0xFF), + "full", SimpleItoa(WireFormat::MakeTag(field))); printer->Indent(); if (field->is_packed()) { @@ -3411,22 +3737,30 @@ GenerateMergeFromCodedStream(io::Printer* printer) { if (field->is_packed()) { internal::WireFormatLite::WireType wiretype = WireFormat::WireTypeForFieldType(field->type()); - 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))); + const uint32 tag = internal::WireFormatLite::MakeTag( + field->number(), wiretype); + printer->Print( + "} else if (\n" + " static_cast< ::google::protobuf::uint8>(tag) ==\n" + " static_cast< ::google::protobuf::uint8>($truncated$u /* $full$ & 0xFF */)) {\n", + "truncated", SimpleItoa(tag & 0xFF), + "full", SimpleItoa(tag)); + printer->Indent(); field_generator.GenerateMergeFromCodedStream(printer); printer->Outdent(); } else if (field->is_packable() && !field->is_packed()) { internal::WireFormatLite::WireType wiretype = internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED; - 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))); + const uint32 tag = internal::WireFormatLite::MakeTag( + field->number(), wiretype); + + printer->Print( + "} else if (\n" + " static_cast< ::google::protobuf::uint8>(tag) ==\n" + " static_cast< ::google::protobuf::uint8>($truncated$u /* $full$ & 0xFF */)) {\n", + "truncated", SimpleItoa(tag & 0xFF), + "full", SimpleItoa(tag)); printer->Indent(); field_generator.GenerateMergeFromCodedStreamWithPacking(printer); printer->Outdent(); @@ -3452,12 +3786,20 @@ GenerateMergeFromCodedStream(io::Printer* printer) { printer->Print("handle_unusual:\n"); printer->Indent(); // If tag is 0 or an end-group tag then this must be the end of the message. - printer->Print( - "if (tag == 0 ||\n" - " ::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==\n" - " ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) {\n" - " goto success;\n" - "}\n"); + if (capture_last_tag) { + printer->Print( + "if (tag == 0 ||\n" + " ::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==\n" + " ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) {\n" + " input->SetLastTag(tag);\n" + " goto success;\n" + "}\n"); + } else { + printer->Print( + "if (tag == 0) {\n" + " goto success;\n" + "}\n"); + } // Handle extension ranges. if (descriptor_->extension_range_count() > 0) { @@ -3485,23 +3827,16 @@ GenerateMergeFromCodedStream(io::Printer* printer) { } } printer->Print(") {\n"); - if (PreserveUnknownFields(descriptor_)) { - if (UseUnknownFieldSet(descriptor_->file(), options_)) { - printer->Print( - " DO_(_extensions_.ParseField(tag, input, " - "internal_default_instance(),\n" - " mutable_unknown_fields()));\n"); - } else { - printer->Print( - " DO_(_extensions_.ParseField(tag, input, " - "internal_default_instance(),\n" - " &unknown_fields_stream));\n"); - } + if (UseUnknownFieldSet(descriptor_->file(), options_)) { + printer->Print(vars, + " DO_(_extensions_.ParseField(tag, input,\n" + " internal_default_instance(),\n" + " $mutable_unknown_fields$));\n"); } else { printer->Print( - // With static initializers. - " DO_(_extensions_.ParseField(tag, input, " - "internal_default_instance());\n"); + " DO_(_extensions_.ParseField(tag, input,\n" + " internal_default_instance(),\n" + " &unknown_fields_stream));\n"); } printer->Print( " continue;\n" @@ -3509,19 +3844,14 @@ GenerateMergeFromCodedStream(io::Printer* printer) { } // We really don't recognize this tag. Skip it. - if (PreserveUnknownFields(descriptor_)) { - if (UseUnknownFieldSet(descriptor_->file(), options_)) { - printer->Print( + if (UseUnknownFieldSet(descriptor_->file(), options_)) { + printer->Print(vars, "DO_(::google::protobuf::internal::WireFormat::SkipField(\n" - " input, tag, mutable_unknown_fields()));\n"); - } else { - printer->Print( - "DO_(::google::protobuf::internal::WireFormatLite::SkipField(\n" - " input, tag, &unknown_fields_stream));\n"); - } + " input, tag, $mutable_unknown_fields$));\n"); } else { printer->Print( - "DO_(::google::protobuf::internal::WireFormatLite::SkipField(input, tag));\n"); + "DO_(::google::protobuf::internal::WireFormatLite::SkipField(\n" + " input, tag, &unknown_fields_stream));\n"); } if (descriptor_->field_count() > 0) { @@ -3658,13 +3988,16 @@ GenerateSerializeWithCachedSizes(io::Printer* printer) { " _extensions_.SerializeMessageSetWithCachedSizes(output);\n", "classname", classname_); GOOGLE_CHECK(UseUnknownFieldSet(descriptor_->file(), options_)); - printer->Print( + std::map<string, string> vars; + SetUnknkownFieldsVariable(descriptor_, options_, &vars); + printer->Print(vars, " ::google::protobuf::internal::WireFormat::SerializeUnknownMessageSetItems(\n" - " unknown_fields(), output);\n"); + " $unknown_fields$, output);\n"); printer->Print( "}\n"); return; } + if (options_.table_driven_serialization) return; printer->Print( "void $classname$::SerializeWithCachedSizes(\n" @@ -3699,10 +4032,12 @@ GenerateSerializeWithCachedSizesToArray(io::Printer* printer) { " deterministic, target);\n", "classname", classname_); GOOGLE_CHECK(UseUnknownFieldSet(descriptor_->file(), options_)); - printer->Print( + std::map<string, string> vars; + SetUnknkownFieldsVariable(descriptor_, options_, &vars); + printer->Print(vars, " target = ::google::protobuf::internal::WireFormat::\n" " SerializeUnknownMessageSetItemsToArray(\n" - " unknown_fields(), target);\n"); + " $unknown_fields$, target);\n"); printer->Print( " return target;\n" "}\n"); @@ -3866,29 +4201,29 @@ GenerateSerializeWithCachedSizesBody(io::Printer* printer, bool to_array) { } } - if (PreserveUnknownFields(descriptor_)) { - if (UseUnknownFieldSet(descriptor_->file(), options_)) { - printer->Print("if (_internal_metadata_.have_unknown_fields()) {\n"); - printer->Indent(); - if (to_array) { - printer->Print( - "target = " - "::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(\n" - " unknown_fields(), target);\n"); - } else { - printer->Print( - "::google::protobuf::internal::WireFormat::SerializeUnknownFields(\n" - " unknown_fields(), output);\n"); - } - printer->Outdent(); - - printer->Print( - "}\n"); + std::map<string, string> vars; + SetUnknkownFieldsVariable(descriptor_, options_, &vars); + if (UseUnknownFieldSet(descriptor_->file(), options_)) { + printer->Print(vars, + "if ($have_unknown_fields$) {\n"); + printer->Indent(); + if (to_array) { + printer->Print(vars, + "target = " + "::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(\n" + " $unknown_fields$, target);\n"); } else { - printer->Print( - "output->WriteRaw(unknown_fields().data(),\n" - " static_cast<int>(unknown_fields().size()));\n"); + printer->Print(vars, + "::google::protobuf::internal::WireFormat::SerializeUnknownFields(\n" + " $unknown_fields$, output);\n"); } + printer->Outdent(); + + printer->Print("}\n"); + } else { + printer->Print(vars, + "output->WriteRaw($unknown_fields$.data(),\n" + " static_cast<int>($unknown_fields$.size()));\n"); } } @@ -3932,18 +4267,19 @@ void MessageGenerator:: GenerateByteSize(io::Printer* printer) { if (descriptor_->options().message_set_wire_format()) { // Special-case MessageSet. - printer->Print( - "size_t $classname$::ByteSizeLong() const {\n" - "// @@protoc_insertion_point(message_set_byte_size_start:$full_name$)\n" - " size_t total_size = _extensions_.MessageSetByteSize();\n", - "classname", classname_, "full_name", descriptor_->full_name()); GOOGLE_CHECK(UseUnknownFieldSet(descriptor_->file(), options_)); - printer->Print( - "if (_internal_metadata_.have_unknown_fields()) {\n" - " total_size += ::google::protobuf::internal::WireFormat::\n" - " ComputeUnknownMessageSetItemsSize(unknown_fields());\n" - "}\n"); - printer->Print( + std::map<string, string> vars; + SetUnknkownFieldsVariable(descriptor_, options_, &vars); + vars["classname"] = classname_; + vars["full_name"] = descriptor_->full_name(); + printer->Print(vars, + "size_t $classname$::ByteSizeLong() const {\n" + "// @@protoc_insertion_point(message_set_byte_size_start:$full_name$)\n" + " size_t total_size = _extensions_.MessageSetByteSize();\n" + " if ($have_unknown_fields$) {\n" + " total_size += ::google::protobuf::internal::WireFormat::\n" + " ComputeUnknownMessageSetItemsSize($unknown_fields$);\n" + " }\n" " int cached_size = ::google::protobuf::internal::ToCachedSize(total_size);\n" " GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();\n" " _cached_size_ = cached_size;\n" @@ -3997,19 +4333,19 @@ GenerateByteSize(io::Printer* printer) { "\n"); } - if (PreserveUnknownFields(descriptor_)) { - if (UseUnknownFieldSet(descriptor_->file(), options_)) { - printer->Print( - "if (_internal_metadata_.have_unknown_fields()) {\n" - " total_size +=\n" - " ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(\n" - " unknown_fields());\n" - "}\n"); - } else { - printer->Print( - "total_size += unknown_fields().size();\n" - "\n"); - } + std::map<string, string> vars; + SetUnknkownFieldsVariable(descriptor_, options_, &vars); + if (UseUnknownFieldSet(descriptor_->file(), options_)) { + printer->Print(vars, + "if ($have_unknown_fields$) {\n" + " total_size +=\n" + " ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(\n" + " $unknown_fields$);\n" + "}\n"); + } else { + printer->Print(vars, + "total_size += $unknown_fields$.size();\n" + "\n"); } // Handle required fields (if any). We expect all of them to be @@ -4358,7 +4694,6 @@ GenerateIsInitialized(io::Printer* printer) { "}\n"); } - } // namespace cpp } // namespace compiler } // namespace protobuf diff --git a/src/google/protobuf/compiler/cpp/cpp_message.h b/src/google/protobuf/compiler/cpp/cpp_message.h index 23aaeeb0..352069eb 100644 --- a/src/google/protobuf/compiler/cpp/cpp_message.h +++ b/src/google/protobuf/compiler/cpp/cpp_message.h @@ -104,10 +104,6 @@ class MessageGenerator { // allocated before any can be initialized. void GenerateDefaultInstanceInitializer(io::Printer* printer); - // Generates code that should be run when ShutdownProtobufLibrary() is called, - // to delete all dynamically-allocated objects. - void GenerateShutdownCode(io::Printer* printer); - // Generate all non-inline methods for this class. void GenerateClassMethods(io::Printer* printer); @@ -132,6 +128,9 @@ class MessageGenerator { // of entries generated and the index of the first has_bit entry. std::pair<size_t, size_t> GenerateOffsets(io::Printer* printer); void GenerateSchema(io::Printer* printer, int offset, int has_offset); + // For each field generates a table entry describing the field for the + // table driven serializer. + int GenerateFieldMetadata(io::Printer* printer); // Generate constructors and destructor. void GenerateStructors(io::Printer* printer); @@ -147,6 +146,13 @@ class MessageGenerator { // Generate the arena-specific destructor code. void GenerateArenaDestructorCode(io::Printer* printer); + // Helper for GenerateClear and others. Optionally emits a condition that + // assumes the existence of the cached_has_bits variable, and returns true if + // the condition was printed. + bool MaybeGenerateOptionalFieldCondition(io::Printer* printer, + const FieldDescriptor* field, + int expected_has_bits_index); + // Generate standard Message methods. void GenerateClear(io::Printer* printer); void GenerateOneofClear(io::Printer* printer); @@ -179,7 +185,6 @@ class MessageGenerator { io::Printer* printer, const Descriptor::ExtensionRange* range, bool unbounded); - // Generates has_foo() functions and variables for singular field has-bits. void GenerateSingularFieldHasBits(const FieldDescriptor* field, std::map<string, string> vars, diff --git a/src/google/protobuf/compiler/cpp/cpp_message_field.cc b/src/google/protobuf/compiler/cpp/cpp_message_field.cc index fc3c4564..e45470fb 100644 --- a/src/google/protobuf/compiler/cpp/cpp_message_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_message_field.cc @@ -50,6 +50,8 @@ void SetMessageVariables(const FieldDescriptor* descriptor, const Options& options) { SetCommonFieldVariables(descriptor, variables, options); (*variables)["type"] = FieldMessageTypeName(descriptor); + (*variables)["type_default_instance"] = + DefaultInstanceName(descriptor->message_type()); if (descriptor->options().weak() || !descriptor->containing_oneof()) { (*variables)["non_null_ptr_to_name"] = StrCat("this->", (*variables)["name"], "_"); @@ -98,6 +100,7 @@ void MessageFieldGenerator:: GenerateGetterDeclaration(io::Printer* printer) const { printer->Print(variables_, "$deprecated_attr$const $type$& $name$() const;\n"); + printer->Annotate("name", descriptor_); } void MessageFieldGenerator:: @@ -107,9 +110,14 @@ GenerateDependentAccessorDeclarations(io::Printer* printer) const { } // Arena manipulation code is out-of-line in the derived message class. printer->Print(variables_, - "$deprecated_attr$$type$* mutable_$name$();\n" - "$deprecated_attr$$type$* $release_name$();\n" - "$deprecated_attr$void set_allocated_$name$($type$* $name$);\n"); + "$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_); } void MessageFieldGenerator:: @@ -130,15 +138,25 @@ GenerateAccessorDeclarations(io::Printer* printer) const { GenerateGetterDeclaration(printer); if (!dependent_field_) { printer->Print(variables_, - "$deprecated_attr$$type$* mutable_$name$();\n" - "$deprecated_attr$$type$* $release_name$();\n" - "$deprecated_attr$void set_allocated_$name$($type$* $name$);\n"); + "$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$$type$* unsafe_arena_release_$name$();\n" - "$deprecated_attr$void unsafe_arena_set_allocated_$name$(\n" - " $type$* $name$);\n"); + "$deprecated_attr$void " + "${$unsafe_arena_set_allocated_$name$$}$(\n" + " $type$* $name$);\n"); + printer->Annotate("{", "}", descriptor_); } } @@ -346,30 +364,18 @@ GenerateDependentInlineAccessorDefinitions(io::Printer* printer) const { void MessageFieldGenerator:: GenerateInlineAccessorDefinitions(io::Printer* printer, bool is_inline) const { - if (dependent_field_) { - // for dependent fields we cannot access its internal_default_instance, - // because the type is incomplete. - // TODO(gerbens) deprecate dependent base class. - std::map<string, string> variables(variables_); - variables["inline"] = is_inline ? "inline " : ""; - printer->Print(variables, - "$inline$const $type$& $classname$::$name$() const {\n" - " // @@protoc_insertion_point(field_get:$full_name$)\n" - " return $name$_ != NULL ? *$name$_\n" - " : *internal_default_instance()->$name$_;\n" - "}\n"); - return; - } - std::map<string, string> variables(variables_); variables["inline"] = is_inline ? "inline " : ""; printer->Print(variables, "$inline$const $type$& $classname$::$name$() const {\n" + " const $type$* p = $name$_;\n" " // @@protoc_insertion_point(field_get:$full_name$)\n" - " return $name$_ != NULL ? *$name$_\n" - " : *$type$::internal_default_instance();\n" + " return p != NULL ? *p : *reinterpret_cast<const $type$*>(\n" + " &$type_default_instance$);\n" "}\n"); + if (dependent_field_) return; + if (SupportsArenas(descriptor_)) { printer->Print(variables, "$inline$" @@ -511,18 +517,12 @@ GenerateMergingCode(io::Printer* printer) const { void MessageFieldGenerator:: GenerateSwappingCode(io::Printer* printer) const { - printer->Print(variables_, "std::swap($name$_, other->$name$_);\n"); + printer->Print(variables_, "swap($name$_, other->$name$_);\n"); } void MessageFieldGenerator:: GenerateDestructorCode(io::Printer* printer) const { - // In google3 a default instance will never get deleted so we don't need to - // worry about that but in opensource protobuf default instances are deleted - // in shutdown process and we need to take special care when handling them. - printer->Print(variables_, - "if (this != internal_default_instance()) {\n" - " delete $name$_;\n" - "}\n"); + printer->Print(variables_, "delete $name$_;\n"); } void MessageFieldGenerator:: @@ -899,16 +899,20 @@ GeneratePrivateMembers(io::Printer* printer) const { void RepeatedMessageFieldGenerator:: InternalGenerateTypeDependentAccessorDeclarations(io::Printer* printer) const { printer->Print(variables_, - "$deprecated_attr$$type$* mutable_$name$(int index);\n" - "$deprecated_attr$$type$* add_$name$();\n"); + "$deprecated_attr$$type$* ${$mutable_$name$$}$(int index);\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print(variables_, "$deprecated_attr$$type$* ${$add_$name$$}$();\n"); + printer->Annotate("{", "}", descriptor_); if (dependent_getter_) { printer->Print(variables_, "$deprecated_attr$const ::google::protobuf::RepeatedPtrField< $type$ >&\n" " $name$() const;\n"); + printer->Annotate("name", descriptor_); } printer->Print(variables_, - "$deprecated_attr$::google::protobuf::RepeatedPtrField< $type$ >*\n" - " mutable_$name$();\n"); + "$deprecated_attr$::google::protobuf::RepeatedPtrField< $type$ >*\n" + " ${$mutable_$name$$}$();\n"); + printer->Annotate("{", "}", descriptor_); } void RepeatedMessageFieldGenerator:: @@ -916,6 +920,7 @@ GenerateDependentAccessorDeclarations(io::Printer* printer) const { if (dependent_getter_) { printer->Print(variables_, "$deprecated_attr$const $type$& $name$(int index) const;\n"); + printer->Annotate("name", descriptor_); } if (dependent_field_) { InternalGenerateTypeDependentAccessorDeclarations(printer); @@ -927,6 +932,7 @@ GenerateAccessorDeclarations(io::Printer* printer) const { if (!dependent_getter_) { printer->Print(variables_, "$deprecated_attr$const $type$& $name$(int index) const;\n"); + printer->Annotate("name", descriptor_); } if (!dependent_field_) { InternalGenerateTypeDependentAccessorDeclarations(printer); @@ -935,6 +941,7 @@ GenerateAccessorDeclarations(io::Printer* printer) const { printer->Print(variables_, "$deprecated_attr$const ::google::protobuf::RepeatedPtrField< $type$ >&\n" " $name$() const;\n"); + printer->Annotate("name", descriptor_); } } diff --git a/src/google/protobuf/compiler/cpp/cpp_move_unittest.cc b/src/google/protobuf/compiler/cpp/cpp_move_unittest.cc new file mode 100644 index 00000000..f72a7d60 --- /dev/null +++ b/src/google/protobuf/compiler/cpp/cpp_move_unittest.cc @@ -0,0 +1,169 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include <google/protobuf/stubs/common.h> +#include <google/protobuf/test_util.h> +#include <google/protobuf/unittest.pb.h> +#include <gtest/gtest.h> + +#if LANG_CXX11 +#include <google/protobuf/stubs/type_traits.h> +#endif + +namespace google { +namespace protobuf { +namespace compiler { +namespace cpp { + +// Can't use an anonymous namespace here due to brokenness of Tru64 compiler. +namespace cpp_unittest { + +// Moves are enabled only when compiling with a C++11 compiler or newer. +#if LANG_CXX11 + +TEST(MovableMessageTest, MoveConstructor) { + protobuf_unittest::TestAllTypes message1; + TestUtil::SetAllFields(&message1); + const auto* nested = &message1.optional_nested_message(); + + protobuf_unittest::TestAllTypes message2(std::move(message1)); + TestUtil::ExpectAllFieldsSet(message2); + + // Check if the optional_nested_message was actually moved (and not just + // copied). + EXPECT_EQ(nested, &message2.optional_nested_message()); + EXPECT_NE(nested, &message1.optional_nested_message()); +} + +TEST(MovableMessageTest, MoveAssignmentOperator) { + protobuf_unittest::TestAllTypes message1; + TestUtil::SetAllFields(&message1); + const auto* nested = &message1.optional_nested_message(); + + protobuf_unittest::TestAllTypes message2; + message2 = std::move(message1); + TestUtil::ExpectAllFieldsSet(message2); + + // Check if the optional_nested_message was actually moved (and not just + // copied). + EXPECT_EQ(nested, &message2.optional_nested_message()); + EXPECT_NE(nested, &message1.optional_nested_message()); +} + +TEST(MovableMessageTest, SelfMoveAssignment) { + // The `self` reference is necessary to defeat -Wself-move. + protobuf_unittest::TestAllTypes message, &self = message; + TestUtil::SetAllFields(&message); + message = std::move(self); + TestUtil::ExpectAllFieldsSet(message); +} + +TEST(MovableMessageTest, MoveSameArena) { + Arena arena; + + auto* message1_on_arena = + Arena::CreateMessage<protobuf_unittest::TestAllTypes>(&arena); + TestUtil::SetAllFields(message1_on_arena); + const auto* nested = &message1_on_arena->optional_nested_message(); + + auto* message2_on_arena = + Arena::CreateMessage<protobuf_unittest::TestAllTypes>(&arena); + + // Moving messages on the same arena should lead to swapped pointers. + *message2_on_arena = std::move(*message1_on_arena); + EXPECT_EQ(nested, &message2_on_arena->optional_nested_message()); +} + +TEST(MovableMessageTest, MoveDifferentArenas) { + Arena arena1, arena2; + + auto* message1_on_arena = + Arena::CreateMessage<protobuf_unittest::TestAllTypes>(&arena1); + TestUtil::SetAllFields(message1_on_arena); + const auto* nested = &message1_on_arena->optional_nested_message(); + + auto* message2_on_arena = + Arena::CreateMessage<protobuf_unittest::TestAllTypes>(&arena2); + + // Moving messages on two different arenas should lead to a copy. + *message2_on_arena = std::move(*message1_on_arena); + EXPECT_NE(nested, &message2_on_arena->optional_nested_message()); + TestUtil::ExpectAllFieldsSet(*message1_on_arena); + TestUtil::ExpectAllFieldsSet(*message2_on_arena); +} + +TEST(MovableMessageTest, MoveFromArena) { + Arena arena; + + auto* message1_on_arena = + Arena::CreateMessage<protobuf_unittest::TestAllTypes>(&arena); + TestUtil::SetAllFields(message1_on_arena); + const auto* nested = &message1_on_arena->optional_nested_message(); + + protobuf_unittest::TestAllTypes message2; + + // Moving from a message on the arena should lead to a copy. + message2 = std::move(*message1_on_arena); + EXPECT_NE(nested, &message2.optional_nested_message()); + TestUtil::ExpectAllFieldsSet(*message1_on_arena); + TestUtil::ExpectAllFieldsSet(message2); +} + +TEST(MovableMessageTest, MoveToArena) { + Arena arena; + + protobuf_unittest::TestAllTypes message1; + TestUtil::SetAllFields(&message1); + const auto* nested = &message1.optional_nested_message(); + + auto* message2_on_arena = + Arena::CreateMessage<protobuf_unittest::TestAllTypes>(&arena); + + // Moving to a message on the arena should lead to a copy. + *message2_on_arena = std::move(message1); + EXPECT_NE(nested, &message2_on_arena->optional_nested_message()); + TestUtil::ExpectAllFieldsSet(message1); + TestUtil::ExpectAllFieldsSet(*message2_on_arena); +} + +TEST(MovableMessageTest, Noexcept) { + EXPECT_TRUE( + std::is_nothrow_move_constructible<protobuf_unittest::TestAllTypes>()); + EXPECT_TRUE(std::is_nothrow_move_assignable<protobuf_unittest::TestAllTypes>()); +} + +#endif // LANG_CXX11 + +} // namespace cpp_unittest + +} // namespace cpp +} // namespace compiler +} // namespace protobuf +} // namespace google diff --git a/src/google/protobuf/compiler/cpp/cpp_options.h b/src/google/protobuf/compiler/cpp/cpp_options.h index bdaa12a5..04338083 100644 --- a/src/google/protobuf/compiler/cpp/cpp_options.h +++ b/src/google/protobuf/compiler/cpp/cpp_options.h @@ -49,7 +49,8 @@ struct Options { transitive_pb_h(true), annotate_headers(false), enforce_lite(false), - table_driven_parsing(false) {} + table_driven_parsing(false), + table_driven_serialization(false) {} string dllexport_decl; bool safe_boundary_check; @@ -58,6 +59,7 @@ struct Options { bool annotate_headers; bool enforce_lite; bool table_driven_parsing; + bool table_driven_serialization; string annotation_pragma_name; string annotation_guard_name; }; diff --git a/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc b/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc index 020c1941..07ac0bb4 100644 --- a/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc @@ -115,9 +115,11 @@ GeneratePrivateMembers(io::Printer* printer) const { void PrimitiveFieldGenerator:: GenerateAccessorDeclarations(io::Printer* printer) const { + printer->Print(variables_, "$deprecated_attr$$type$ $name$() const;\n"); + printer->Annotate("name", descriptor_); printer->Print(variables_, - "$deprecated_attr$$type$ $name$() const;\n" - "$deprecated_attr$void set_$name$($type$ value);\n"); + "$deprecated_attr$void ${$set_$name$$}$($type$ value);\n"); + printer->Annotate("{", "}", descriptor_); } void PrimitiveFieldGenerator:: @@ -148,7 +150,7 @@ GenerateMergingCode(io::Printer* printer) const { void PrimitiveFieldGenerator:: GenerateSwappingCode(io::Printer* printer) const { - printer->Print(variables_, "std::swap($name$_, other->$name$_);\n"); + printer->Print(variables_, "swap($name$_, other->$name$_);\n"); } void PrimitiveFieldGenerator:: @@ -290,14 +292,23 @@ GeneratePrivateMembers(io::Printer* printer) const { void RepeatedPrimitiveFieldGenerator:: GenerateAccessorDeclarations(io::Printer* printer) const { printer->Print(variables_, - "$deprecated_attr$$type$ $name$(int index) const;\n" - "$deprecated_attr$void set_$name$(int index, $type$ value);\n" - "$deprecated_attr$void add_$name$($type$ value);\n"); + "$deprecated_attr$$type$ $name$(int index) const;\n"); + printer->Annotate("name", descriptor_); + printer->Print( + variables_, + "$deprecated_attr$void ${$set_$name$$}$(int index, $type$ value);\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print(variables_, + "$deprecated_attr$void ${$add_$name$$}$($type$ value);\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print(variables_, + "$deprecated_attr$const ::google::protobuf::RepeatedField< $type$ >&\n" + " $name$() const;\n"); + printer->Annotate("name", descriptor_); printer->Print(variables_, - "$deprecated_attr$const ::google::protobuf::RepeatedField< $type$ >&\n" - " $name$() const;\n" - "$deprecated_attr$::google::protobuf::RepeatedField< $type$ >*\n" - " mutable_$name$();\n"); + "$deprecated_attr$::google::protobuf::RepeatedField< $type$ >*\n" + " ${$mutable_$name$$}$();\n"); + printer->Annotate("{", "}", descriptor_); } void RepeatedPrimitiveFieldGenerator:: @@ -316,8 +327,7 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, bool is_inline) const { "$inline$void $classname$::add_$name$($type$ value) {\n" " $name$_.Add(value);\n" " // @@protoc_insertion_point(field_add:$full_name$)\n" - "}\n"); - printer->Print(variables, + "}\n" "$inline$const ::google::protobuf::RepeatedField< $type$ >&\n" "$classname$::$name$() const {\n" " // @@protoc_insertion_point(field_list:$full_name$)\n" diff --git a/src/google/protobuf/compiler/cpp/cpp_string_field.cc b/src/google/protobuf/compiler/cpp/cpp_string_field.cc index 7a849e2e..c23dd6fd 100644 --- a/src/google/protobuf/compiler/cpp/cpp_string_field.cc +++ b/src/google/protobuf/compiler/cpp/cpp_string_field.cc @@ -142,28 +142,47 @@ GenerateAccessorDeclarations(io::Printer* printer) const { } printer->Print(variables_, - "$deprecated_attr$const ::std::string& $name$() const;\n" - "$deprecated_attr$void set_$name$(const ::std::string& value);\n"); + "$deprecated_attr$const ::std::string& $name$() const;\n"); + printer->Annotate("name", descriptor_); + printer->Print( + variables_, + "$deprecated_attr$void ${$set_$name$$}$(const ::std::string& value);\n"); + printer->Annotate("{", "}", descriptor_); - if (!SupportsArenas(descriptor_)) { - printer->Print(variables_, - "#if LANG_CXX11\n" - "$deprecated_attr$void set_$name$(::std::string&& value);\n" - "#endif\n"); - } + printer->Print(variables_, + "#if LANG_CXX11\n" + "$deprecated_attr$void ${$set_$name$$}$(::std::string&& value);\n" + "#endif\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print( + variables_, + "$deprecated_attr$void ${$set_$name$$}$(const char* value);\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print(variables_, + "$deprecated_attr$void ${$set_$name$$}$(const $pointer_type$* " + "value, size_t size)" + ";\n"); + printer->Annotate("{", "}", descriptor_); printer->Print(variables_, - "$deprecated_attr$void set_$name$(const char* value);\n" - "$deprecated_attr$void set_$name$(const $pointer_type$* value, size_t size)" - ";\n" - "$deprecated_attr$::std::string* mutable_$name$();\n" - "$deprecated_attr$::std::string* $release_name$();\n" - "$deprecated_attr$void set_allocated_$name$(::std::string* $name$);\n"); + "$deprecated_attr$::std::string* ${$mutable_$name$$}$();\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print(variables_, "$deprecated_attr$::std::string* $release_name$();\n"); + printer->Annotate("release_name", descriptor_); + printer->Print( + variables_, + "$deprecated_attr$void ${$set_allocated_$name$$}$(::std::string* $name$);\n"); + printer->Annotate("{", "}", descriptor_); if (SupportsArenas(descriptor_)) { - printer->Print(variables_, - "$deprecated_attr$::std::string* unsafe_arena_release_$name$();\n" - "$deprecated_attr$void unsafe_arena_set_allocated_$name$(\n" - " ::std::string* $name$);\n"); + printer->Print( + variables_, + "$deprecated_attr$::std::string* ${$unsafe_arena_release_$name$$}$();\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print( + variables_, + "$deprecated_attr$void ${$unsafe_arena_set_allocated_$name$$}$(\n" + " ::std::string* $name$);\n"); + printer->Annotate("{", "}", descriptor_); } @@ -191,6 +210,14 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " $name$_.Set($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" + " $set_hasbit$\n" + " $name$_.Set(\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" " $null_check$" " $set_hasbit$\n" @@ -456,15 +483,10 @@ GenerateDefaultInstanceAllocator(io::Printer* printer) const { printer->Print(variables_, "$classname$::$default_variable_name$.DefaultConstruct();\n" "*$classname$::$default_variable_name$.get_mutable() = " - "::std::string($default$, $default_length$);\n"); - } -} - -void StringFieldGenerator:: -GenerateShutdownCode(io::Printer* printer) const { - if (!descriptor_->default_value_string().empty()) { - printer->Print(variables_, - "$classname$::$default_variable_name$.Shutdown();\n"); + "::std::string($default$, $default_length$);\n" + "::google::protobuf::internal::OnShutdownDestroyString(\n" + " $classname$::$default_variable_name$.get_mutable());\n" + ); } } @@ -551,6 +573,19 @@ GenerateInlineAccessorDefinitions(io::Printer* printer, " GetArenaNoVirtual());\n" " // @@protoc_insertion_point(field_set:$full_name$)\n" "}\n" + "#if LANG_CXX11\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" + " set_has_$name$();\n" + " $oneof_prefix$$name$_.UnsafeSetDefault($default_variable$);\n" + " }\n" + " $oneof_prefix$$name$_.Set(\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" " $null_check$" " if (!has_$name$()) {\n" @@ -833,28 +868,62 @@ GenerateAccessorDeclarations(io::Printer* printer) const { } printer->Print(variables_, - "$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" - "$deprecated_attr$const ::google::protobuf::RepeatedPtrField< ::std::string>& $name$() " - "const;\n" - "$deprecated_attr$::google::protobuf::RepeatedPtrField< ::std::string>* mutable_$name$()" + "$deprecated_attr$const ::std::string& $name$(int index) const;\n"); + printer->Annotate("name", descriptor_); + printer->Print( + variables_, + "$deprecated_attr$::std::string* ${$mutable_$name$$}$(int index);\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print(variables_, + "$deprecated_attr$void ${$set_$name$$}$(int index, const " + "::std::string& value);\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print( + variables_, + "#if LANG_CXX11\n" + "$deprecated_attr$void ${$set_$name$$}$(int index, ::std::string&& value);\n" + "#endif\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print(variables_, + "$deprecated_attr$void ${$set_$name$$}$(int index, const " + "char* value);\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print(variables_, + "" + "$deprecated_attr$void ${$set_$name$$}$(" + "int index, const $pointer_type$* value, size_t size);\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print(variables_, + "$deprecated_attr$::std::string* ${$add_$name$$}$();\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print( + variables_, + "$deprecated_attr$void ${$add_$name$$}$(const ::std::string& value);\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print(variables_, + "#if LANG_CXX11\n" + "$deprecated_attr$void ${$add_$name$$}$(::std::string&& value);\n" + "#endif\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print( + variables_, + "$deprecated_attr$void ${$add_$name$$}$(const char* value);\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print(variables_, + "$deprecated_attr$void ${$add_$name$$}$(const $pointer_type$* " + "value, size_t size)" + ";\n"); + printer->Annotate("{", "}", descriptor_); + printer->Print( + variables_, + "$deprecated_attr$const ::google::protobuf::RepeatedPtrField< ::std::string>& $name$() " + "const;\n"); + printer->Annotate("name", descriptor_); + printer->Print(variables_, + "$deprecated_attr$::google::protobuf::RepeatedPtrField< ::std::string>* " + "${$mutable_$name$$}$()" ";\n"); + printer->Annotate("{", "}", descriptor_); if (unknown_ctype) { printer->Outdent(); diff --git a/src/google/protobuf/compiler/cpp/cpp_string_field.h b/src/google/protobuf/compiler/cpp/cpp_string_field.h index af263c1a..531252b0 100644 --- a/src/google/protobuf/compiler/cpp/cpp_string_field.h +++ b/src/google/protobuf/compiler/cpp/cpp_string_field.h @@ -65,7 +65,6 @@ class StringFieldGenerator : public FieldGenerator { void GenerateCopyConstructorCode(io::Printer* printer) const; void GenerateDestructorCode(io::Printer* printer) const; void GenerateDefaultInstanceAllocator(io::Printer* printer) const; - void GenerateShutdownCode(io::Printer* printer) const; void GenerateMergeFromCodedStream(io::Printer* printer) const; void GenerateSerializeWithCachedSizes(io::Printer* printer) const; void GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const; diff --git a/src/google/protobuf/compiler/cpp/cpp_unittest.cc b/src/google/protobuf/compiler/cpp/cpp_unittest.cc index e56964c7..fdde771b 100644 --- a/src/google/protobuf/compiler/cpp/cpp_unittest.cc +++ b/src/google/protobuf/compiler/cpp/cpp_unittest.cc @@ -576,12 +576,73 @@ TEST(GeneratedMessageTest, SwapWithOther) { EXPECT_EQ(unittest::TestAllTypes::BAR, message2.repeated_nested_enum(1)); } -TEST(GeneratedMessageTest, CopyConstructor) { - unittest::TestAllTypes message1; +TEST(GeneratedMessageTest, ADLSwap) { + unittest::TestAllTypes message1, message2; TestUtil::SetAllFields(&message1); - unittest::TestAllTypes message2(message1); + // Note the address of one of the repeated fields, to verify it was swapped + // rather than copied. + const int32* addr = &message1.repeated_int32().Get(0); + + using std::swap; + swap(message1, message2); + TestUtil::ExpectAllFieldsSet(message2); + TestUtil::ExpectClear(message1); + + EXPECT_EQ(addr, &message2.repeated_int32().Get(0)); +} + +TEST(GeneratedMessageTest, CopyConstructor) { + // All set. + { + unittest::TestAllTypes message1; + TestUtil::SetAllFields(&message1); + + unittest::TestAllTypes message2(message1); + TestUtil::ExpectAllFieldsSet(message2); + } + + // None set. + { + unittest::TestAllTypes message1; + unittest::TestAllTypes message2(message1); + + EXPECT_FALSE(message1.has_optional_string()); + EXPECT_FALSE(message2.has_optional_string()); + EXPECT_EQ(&message1.optional_string(), + &message2.optional_string()); + + EXPECT_FALSE(message1.has_optional_bytes()); + EXPECT_FALSE(message2.has_optional_bytes()); + EXPECT_EQ(&message1.optional_bytes(), + &message2.optional_bytes()); + + EXPECT_FALSE(message1.has_optional_nested_message()); + EXPECT_FALSE(message2.has_optional_nested_message()); + EXPECT_EQ(&message1.optional_nested_message(), + &message2.optional_nested_message()); + + EXPECT_FALSE(message1.has_optional_foreign_message()); + EXPECT_FALSE(message2.has_optional_foreign_message()); + EXPECT_EQ(&message1.optional_foreign_message(), + &message2.optional_foreign_message()); + + EXPECT_FALSE(message1.has_optional_import_message()); + EXPECT_FALSE(message2.has_optional_import_message()); + EXPECT_EQ(&message1.optional_import_message(), + &message2.optional_import_message()); + + EXPECT_FALSE(message1.has_optional_public_import_message()); + EXPECT_FALSE(message2.has_optional_public_import_message()); + EXPECT_EQ(&message1.optional_public_import_message(), + &message2.optional_public_import_message()); + + EXPECT_FALSE(message1.has_optional_lazy_message()); + EXPECT_FALSE(message2.has_optional_lazy_message()); + EXPECT_EQ(&message1.optional_lazy_message(), + &message2.optional_lazy_message()); + } } TEST(GeneratedMessageTest, CopyConstructorWithArenas) { @@ -1340,7 +1401,7 @@ class GeneratedServiceTest : public testing::Test { foo_(descriptor_->FindMethodByName("Foo")), bar_(descriptor_->FindMethodByName("Bar")), stub_(&mock_channel_), - done_(NewPermanentCallback(&DoNothing)) {} + done_(::google::protobuf::NewPermanentCallback(&DoNothing)) {} virtual void SetUp() { ASSERT_TRUE(foo_ != NULL); diff --git a/src/google/protobuf/compiler/importer.cc b/src/google/protobuf/compiler/importer.cc index 844edc1c..a27562f7 100644 --- a/src/google/protobuf/compiler/importer.cc +++ b/src/google/protobuf/compiler/importer.cc @@ -32,6 +32,7 @@ // Based on original Protocol Buffers design by // Sanjay Ghemawat, Jeff Dean, and others. + #ifdef _MSC_VER #include <io.h> #else @@ -295,10 +296,8 @@ static string CanonicalizePath(string path) { } static inline bool ContainsParentReference(const string& path) { - return path == ".." || - HasPrefixString(path, "../") || - HasSuffixString(path, "/..") || - path.find("/../") != string::npos; + return path == ".." || HasPrefixString(path, "../") || + HasSuffixString(path, "/..") || path.find("/../") != string::npos; } // Maps a file from an old location to a new one. Typically, old_prefix is @@ -328,8 +327,7 @@ static bool ApplyMapping(const string& filename, // We do not allow the file name to use "..". return false; } - if (HasPrefixString(filename, "/") || - IsWindowsAbsolutePath(filename)) { + if (HasPrefixString(filename, "/") || IsWindowsAbsolutePath(filename)) { // This is an absolute path, so it isn't matched by the empty string. return false; } diff --git a/src/google/protobuf/compiler/java/java_context.cc b/src/google/protobuf/compiler/java/java_context.cc index b82fb3dd..0771d5e1 100644 --- a/src/google/protobuf/compiler/java/java_context.cc +++ b/src/google/protobuf/compiler/java/java_context.cc @@ -50,7 +50,7 @@ Context::Context(const FileDescriptor* file, const Options& options) Context::~Context() { } -ClassNameResolver* Context::GetNameResolver() { +ClassNameResolver* Context::GetNameResolver() const { return name_resolver_.get(); } @@ -154,7 +154,7 @@ void Context::InitializeFieldGeneratorInfoForFields( for (int i = 0; i < fields.size(); ++i) { const FieldDescriptor* field = fields[i]; FieldGeneratorInfo info; - info.name = UnderscoresToCamelCase(field); + info.name = CamelCaseFieldName(field); info.capitalized_name = UnderscoresToCapitalizedCamelCase(field); // For fields conflicting with some other fields, we append the field // number to their field names in generated code to avoid conflicts. diff --git a/src/google/protobuf/compiler/java/java_context.h b/src/google/protobuf/compiler/java/java_context.h index b22e7e3a..9a74c430 100644 --- a/src/google/protobuf/compiler/java/java_context.h +++ b/src/google/protobuf/compiler/java/java_context.h @@ -70,7 +70,7 @@ class Context { // Get the name resolver associated with this context. The resolver // can be used to map descriptors to Java class names. - ClassNameResolver* GetNameResolver(); + ClassNameResolver* GetNameResolver() const; // Get the FieldGeneratorInfo for a given field. const FieldGeneratorInfo* GetFieldGeneratorInfo( diff --git a/src/google/protobuf/compiler/java/java_enum.cc b/src/google/protobuf/compiler/java/java_enum.cc index b9ee00ff..d125ebe5 100644 --- a/src/google/protobuf/compiler/java/java_enum.cc +++ b/src/google/protobuf/compiler/java/java_enum.cc @@ -109,14 +109,16 @@ void EnumGenerator::Generate(io::Printer* printer) { printer->Print(vars, "$name$($index$, $number$),\n"); } + printer->Annotate("name", canonical_values_[i]); } if (SupportUnknownEnumValue(descriptor_->file())) { if (ordinal_is_index) { - printer->Print("UNRECOGNIZED(-1),\n"); + printer->Print("${$UNRECOGNIZED$}$(-1),\n", "{", "", "}", ""); } else { - printer->Print("UNRECOGNIZED(-1, -1),\n"); + printer->Print("${$UNRECOGNIZED$}$(-1, -1),\n", "{", "", "}", ""); } + printer->Annotate("{", "}", descriptor_); } printer->Print( @@ -133,15 +135,19 @@ void EnumGenerator::Generate(io::Printer* printer) { WriteEnumValueDocComment(printer, aliases_[i].value); printer->Print(vars, "public static final $classname$ $name$ = $canonical_name$;\n"); + printer->Annotate("name", aliases_[i].value); } for (int i = 0; i < descriptor_->value_count(); i++) { std::map<string, string> vars; vars["name"] = descriptor_->value(i)->name(); vars["number"] = SimpleItoa(descriptor_->value(i)->number()); + vars["{"] = ""; + vars["}"] = ""; WriteEnumValueDocComment(printer, descriptor_->value(i)); printer->Print(vars, - "public static final int $name$_VALUE = $number$;\n"); + "public static final int ${$$name$_VALUE$}$ = $number$;\n"); + printer->Annotate("{", "}", descriptor_->value(i)); } printer->Print("\n"); diff --git a/src/google/protobuf/compiler/java/java_enum_field.cc b/src/google/protobuf/compiler/java/java_enum_field.cc index 279b9da4..9f7bb349 100644 --- a/src/google/protobuf/compiler/java/java_enum_field.cc +++ b/src/google/protobuf/compiler/java/java_enum_field.cc @@ -183,23 +183,26 @@ GenerateMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $get_has_field_bit_message$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Value() {\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" " return $name$_;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " $type$ result = $type$.$for_number$($name$_);\n" " return result == null ? $unknown$ : result;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void ImmutableEnumFieldGenerator:: @@ -209,33 +212,38 @@ GenerateBuilderMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $get_has_field_bit_builder$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Value() {\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" " return $name$_;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$Value(int value) {\n" + "$deprecation$public Builder " + "${$set$capitalized_name$Value$}$(int value) {\n" " $name$_ = value;\n" " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " $type$ result = $type$.$for_number$($name$_);\n" " return result == null ? $unknown$ : result;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$($type$ value) {\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" " if (value == null) {\n" " throw new NullPointerException();\n" " }\n" @@ -244,14 +252,16 @@ GenerateBuilderMembers(io::Printer* printer) const { " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " $clear_has_field_bit_builder$\n" " $name$_ = $default_number$;\n" " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void ImmutableEnumFieldGenerator:: @@ -311,12 +321,8 @@ GenerateParsingCode(io::Printer* printer) const { printer->Print(variables_, "int rawValue = input.readEnum();\n" "$type$ value = $type$.$for_number$(rawValue);\n" - "if (value == null) {\n"); - if (PreserveUnknownFields(descriptor_->containing_type())) { - printer->Print(variables_, - " unknownFields.mergeVarintField($number$, rawValue);\n"); - } - printer->Print(variables_, + "if (value == null) {\n" + " unknownFields.mergeVarintField($number$, rawValue);\n" "} else {\n" " $set_has_field_bit_message$\n" " $name$_ = rawValue;\n" @@ -386,23 +392,25 @@ GenerateMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $has_oneof_case_message$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Value() {\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" " if ($has_oneof_case_message$) {\n" " return (java.lang.Integer) $oneof_name$_;\n" " }\n" " return $default_number$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " if ($has_oneof_case_message$) {\n" " $type$ result = $type$.$for_number$(\n" " (java.lang.Integer) $oneof_name$_);\n" @@ -410,6 +418,7 @@ GenerateMembers(io::Printer* printer) const { " }\n" " return $default$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void ImmutableEnumOneofFieldGenerator:: @@ -417,31 +426,35 @@ GenerateBuilderMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $has_oneof_case_message$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Value() {\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" " if ($has_oneof_case_message$) {\n" " return ((java.lang.Integer) $oneof_name$_).intValue();\n" " }\n" " return $default_number$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$Value(int value) {\n" + "$deprecation$public Builder " + "${$set$capitalized_name$Value$}$(int value) {\n" " $set_oneof_case_message$;\n" " $oneof_name$_ = value;\n" " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " if ($has_oneof_case_message$) {\n" " $type$ result = $type$.$for_number$(\n" " (java.lang.Integer) $oneof_name$_);\n" @@ -449,9 +462,10 @@ GenerateBuilderMembers(io::Printer* printer) const { " }\n" " return $default$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$($type$ value) {\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" " if (value == null) {\n" " throw new NullPointerException();\n" " }\n" @@ -460,9 +474,10 @@ GenerateBuilderMembers(io::Printer* printer) const { " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " if ($has_oneof_case_message$) {\n" " $clear_oneof_case_message$;\n" " $oneof_name$_ = null;\n" @@ -470,6 +485,7 @@ GenerateBuilderMembers(io::Printer* printer) const { " }\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void ImmutableEnumOneofFieldGenerator:: @@ -502,12 +518,8 @@ GenerateParsingCode(io::Printer* printer) const { printer->Print(variables_, "int rawValue = input.readEnum();\n" "$type$ value = $type$.$for_number$(rawValue);\n" - "if (value == null) {\n"); - if (PreserveUnknownFields(descriptor_->containing_type())) { - printer->Print(variables_, - " unknownFields.mergeVarintField($number$, rawValue);\n"); - } - printer->Print(variables_, + "if (value == null) {\n" + " unknownFields.mergeVarintField($number$, rawValue);\n" "} else {\n" " $set_oneof_case_message$;\n" " $oneof_name$_ = rawValue;\n" @@ -621,32 +633,38 @@ GenerateMembers(io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.util.List<$type$> get$capitalized_name$List() {\n" + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" " return new com.google.protobuf.Internal.ListAdapter<\n" " java.lang.Integer, $type$>($name$_, $name$_converter_);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Count() {\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" " return $name$_.size();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$(int index) {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" " return $name$_converter_.convert($name$_.get(index));\n" "}\n"); + printer->Annotate("{", "}", descriptor_); if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public java.util.List<java.lang.Integer>\n" - "get$capitalized_name$ValueList() {\n" + "${$get$capitalized_name$ValueList$}$() {\n" " return $name$_;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Value(int index) {\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$(int index) {\n" " return $name$_.get(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } if (descriptor_->is_packed() && @@ -684,23 +702,27 @@ GenerateBuilderMembers(io::Printer* printer) const { // could hold on to the returned list and modify it after the message // has been built, thus mutating the message which is supposed to be // immutable. - "$deprecation$public java.util.List<$type$> get$capitalized_name$List() {\n" + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" " return new com.google.protobuf.Internal.ListAdapter<\n" " java.lang.Integer, $type$>($name$_, $name$_converter_);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Count() {\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" " return $name$_.size();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$(int index) {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" " return $name$_converter_.convert($name$_.get(index));\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$(\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" " int index, $type$ value) {\n" " if (value == null) {\n" " throw new NullPointerException();\n" @@ -710,9 +732,10 @@ GenerateBuilderMembers(io::Printer* printer) const { " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder add$capitalized_name$($type$ value) {\n" + "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value) {\n" " if (value == null) {\n" " throw new NullPointerException();\n" " }\n" @@ -721,9 +744,10 @@ GenerateBuilderMembers(io::Printer* printer) const { " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder addAll$capitalized_name$(\n" + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" " java.lang.Iterable<? extends $type$> values) {\n" " ensure$capitalized_name$IsMutable();\n" " for ($type$ value : values) {\n" @@ -732,47 +756,54 @@ GenerateBuilderMembers(io::Printer* printer) const { " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " $name$_ = java.util.Collections.emptyList();\n" " $clear_mutable_bit_builder$;\n" " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public java.util.List<java.lang.Integer>\n" - "get$capitalized_name$ValueList() {\n" + "${$get$capitalized_name$ValueList$}$() {\n" " return java.util.Collections.unmodifiableList($name$_);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Value(int index) {\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$(int index) {\n" " return $name$_.get(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$Value(\n" + "$deprecation$public Builder ${$set$capitalized_name$Value$}$(\n" " int index, int value) {\n" " ensure$capitalized_name$IsMutable();\n" " $name$_.set(index, value);\n" " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder add$capitalized_name$Value(int value) {\n" + "$deprecation$public Builder " + "${$add$capitalized_name$Value$}$(int value) {\n" " ensure$capitalized_name$IsMutable();\n" " $name$_.add(value);\n" " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder addAll$capitalized_name$Value(\n" + "$deprecation$public Builder ${$addAll$capitalized_name$Value$}$(\n" " java.lang.Iterable<java.lang.Integer> values) {\n" " ensure$capitalized_name$IsMutable();\n" " for (int value : values) {\n" @@ -781,6 +812,7 @@ GenerateBuilderMembers(io::Printer* printer) const { " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } } @@ -848,12 +880,8 @@ GenerateParsingCode(io::Printer* printer) const { printer->Print(variables_, "int rawValue = input.readEnum();\n" "$type$ value = $type$.$for_number$(rawValue);\n" - "if (value == null) {\n"); - if (PreserveUnknownFields(descriptor_->containing_type())) { - printer->Print(variables_, - " unknownFields.mergeVarintField($number$, rawValue);\n"); - } - printer->Print(variables_, + "if (value == null) {\n" + " unknownFields.mergeVarintField($number$, rawValue);\n" "} else {\n" " if (!$get_mutable_bit_parser$) {\n" " $name$_ = new java.util.ArrayList<java.lang.Integer>();\n" diff --git a/src/google/protobuf/compiler/java/java_enum_field_lite.cc b/src/google/protobuf/compiler/java/java_enum_field_lite.cc index 1eefdcfc..a4de1e23 100644 --- a/src/google/protobuf/compiler/java/java_enum_field_lite.cc +++ b/src/google/protobuf/compiler/java/java_enum_field_lite.cc @@ -165,23 +165,26 @@ GenerateMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $get_has_field_bit_message$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Value() {\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" " return $name$_;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " $type$ result = $type$.forNumber($name$_);\n" " return result == null ? $unknown$ : result;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Generate private setters for the builder to proxy into. if (SupportUnknownEnumValue(descriptor_->file())) { @@ -214,43 +217,50 @@ GenerateBuilderMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return instance.has$capitalized_name$();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Value() {\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" " return instance.get$capitalized_name$Value();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$Value(int value) {\n" + "$deprecation$public Builder " + "${$set$capitalized_name$Value$}$(int value) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$Value(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " return instance.get$capitalized_name$();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$($type$ value) {\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " copyOnWrite();\n" " instance.clear$capitalized_name$();\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void ImmutableEnumFieldLiteGenerator:: @@ -296,12 +306,8 @@ GenerateParsingCode(io::Printer* printer) const { printer->Print(variables_, "int rawValue = input.readEnum();\n" "$type$ value = $type$.forNumber(rawValue);\n" - "if (value == null) {\n"); - if (PreserveUnknownFields(descriptor_->containing_type())) { - printer->Print(variables_, - " super.mergeVarintField($number$, rawValue);\n"); - } - printer->Print(variables_, + "if (value == null) {\n" + " super.mergeVarintField($number$, rawValue);\n" "} else {\n" " $set_has_field_bit_message$\n" " $name$_ = rawValue;\n" @@ -372,29 +378,32 @@ GenerateMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $has_oneof_case_message$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Value() {\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" " if ($has_oneof_case_message$) {\n" " return (java.lang.Integer) $oneof_name$_;\n" " }\n" " return $default_number$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " if ($has_oneof_case_message$) {\n" " $type$ result = $type$.forNumber((java.lang.Integer) $oneof_name$_);\n" " return result == null ? $unknown$ : result;\n" " }\n" " return $default$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Generate private setters for the builder to proxy into. if (SupportUnknownEnumValue(descriptor_->file())) { @@ -430,43 +439,50 @@ GenerateBuilderMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return instance.has$capitalized_name$();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Value() {\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$() {\n" " return instance.get$capitalized_name$Value();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$Value(int value) {\n" + "$deprecation$public Builder " + "${$set$capitalized_name$Value$}$(int value) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$Value(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " return instance.get$capitalized_name$();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$($type$ value) {\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " copyOnWrite();\n" " instance.clear$capitalized_name$();\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void ImmutableEnumOneofFieldLiteGenerator:: @@ -487,12 +503,8 @@ GenerateParsingCode(io::Printer* printer) const { printer->Print(variables_, "int rawValue = input.readEnum();\n" "$type$ value = $type$.forNumber(rawValue);\n" - "if (value == null) {\n"); - if (PreserveUnknownFields(descriptor_->containing_type())) { - printer->Print(variables_, - " super.mergeVarintField($number$, rawValue);\n"); - } - printer->Print(variables_, + "if (value == null) {\n" + " super.mergeVarintField($number$, rawValue);\n" "} else {\n" " $set_oneof_case_message$;\n" " $oneof_name$_ = rawValue;\n" @@ -607,35 +619,41 @@ GenerateMembers(io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.util.List<$type$> get$capitalized_name$List() {\n" + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" " return new com.google.protobuf.Internal.ListAdapter<\n" " java.lang.Integer, $type$>($name$_, $name$_converter_);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Count() {\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" " return $name$_.size();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$(int index) {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" " return $name$_converter_.convert($name$_.getInt(index));\n" "}\n"); + printer->Annotate("{", "}", descriptor_); if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public java.util.List<java.lang.Integer>\n" - "get$capitalized_name$ValueList() {\n" + "${$get$capitalized_name$ValueList$}$() {\n" " return $name$_;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Value(int index) {\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$(int index) {\n" " return $name$_.getInt(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } - if (descriptor_->options().packed() && + if (descriptor_->is_packed() && context_->HasGeneratedMethods(descriptor_->containing_type())) { printer->Print(variables_, "private int $name$MemoizedSerializedSize;\n"); @@ -714,85 +732,99 @@ void RepeatedImmutableEnumFieldLiteGenerator:: GenerateBuilderMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.util.List<$type$> get$capitalized_name$List() {\n" + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" " return instance.get$capitalized_name$List();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Count() {\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" " return instance.get$capitalized_name$Count();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$(int index) {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" " return instance.get$capitalized_name$(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$(\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" " int index, $type$ value) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$(index, value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder add$capitalized_name$($type$ value) {\n" + "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value) {\n" " copyOnWrite();\n" " instance.add$capitalized_name$(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder addAll$capitalized_name$(\n" + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" " java.lang.Iterable<? extends $type$> values) {\n" " copyOnWrite();\n" " instance.addAll$capitalized_name$(values);" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " copyOnWrite();\n" " instance.clear$capitalized_name$();\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); if (SupportUnknownEnumValue(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public java.util.List<java.lang.Integer>\n" - "get$capitalized_name$ValueList() {\n" + "${$get$capitalized_name$ValueList$}$() {\n" " return java.util.Collections.unmodifiableList(\n" " instance.get$capitalized_name$ValueList());\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Value(int index) {\n" + "$deprecation$public int ${$get$capitalized_name$Value$}$(int index) {\n" " return instance.get$capitalized_name$Value(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$Value(\n" + "$deprecation$public Builder ${$set$capitalized_name$Value$}$(\n" " int index, int value) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$Value(index, value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder add$capitalized_name$Value(int value) {\n" + "$deprecation$public Builder " + "${$add$capitalized_name$Value$}$(int value) {\n" " instance.add$capitalized_name$Value(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder addAll$capitalized_name$Value(\n" + "$deprecation$public Builder ${$addAll$capitalized_name$Value$}$(\n" " java.lang.Iterable<java.lang.Integer> values) {\n" " copyOnWrite();\n" " instance.addAll$capitalized_name$Value(values);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } } @@ -889,7 +921,7 @@ GenerateParsingDoneCode(io::Printer* printer) const { void RepeatedImmutableEnumFieldLiteGenerator:: GenerateSerializationCode(io::Printer* printer) const { - if (descriptor_->options().packed()) { + if (descriptor_->is_packed()) { printer->Print(variables_, "if (get$capitalized_name$List().size() > 0) {\n" " output.writeUInt32NoTag($tag$);\n" @@ -920,7 +952,7 @@ GenerateSerializedSizeCode(io::Printer* printer) const { "}\n"); printer->Print( "size += dataSize;\n"); - if (descriptor_->options().packed()) { + if (descriptor_->is_packed()) { printer->Print(variables_, "if (!get$capitalized_name$List().isEmpty()) {" " size += $tag_size$;\n" @@ -933,7 +965,7 @@ GenerateSerializedSizeCode(io::Printer* printer) const { } // cache the data size for packed fields. - if (descriptor_->options().packed()) { + if (descriptor_->is_packed()) { printer->Print(variables_, "$name$MemoizedSerializedSize = dataSize;\n"); } diff --git a/src/google/protobuf/compiler/java/java_enum_lite.cc b/src/google/protobuf/compiler/java/java_enum_lite.cc index 38b054e7..ab3b3323 100644 --- a/src/google/protobuf/compiler/java/java_enum_lite.cc +++ b/src/google/protobuf/compiler/java/java_enum_lite.cc @@ -93,10 +93,12 @@ void EnumLiteGenerator::Generate(io::Printer* printer) { } printer->Print(vars, "$name$($number$),\n"); + printer->Annotate("name", canonical_values_[i]); } if (SupportUnknownEnumValue(descriptor_->file())) { - printer->Print("UNRECOGNIZED(-1),\n"); + printer->Print("${$UNRECOGNIZED$}$(-1),\n", "{", "", "}", ""); + printer->Annotate("{", "}", descriptor_); } printer->Print( @@ -113,15 +115,19 @@ void EnumLiteGenerator::Generate(io::Printer* printer) { WriteEnumValueDocComment(printer, aliases_[i].value); printer->Print(vars, "public static final $classname$ $name$ = $canonical_name$;\n"); + printer->Annotate("name", aliases_[i].value); } for (int i = 0; i < descriptor_->value_count(); i++) { std::map<string, string> vars; vars["name"] = descriptor_->value(i)->name(); vars["number"] = SimpleItoa(descriptor_->value(i)->number()); + vars["{"] = ""; + vars["}"] = ""; WriteEnumValueDocComment(printer, descriptor_->value(i)); printer->Print(vars, - "public static final int $name$_VALUE = $number$;\n"); + "public static final int ${$$name$_VALUE$}$ = $number$;\n"); + printer->Annotate("{", "}", descriptor_->value(i)); } printer->Print("\n"); diff --git a/src/google/protobuf/compiler/java/java_extension.cc b/src/google/protobuf/compiler/java/java_extension.cc index cb237bf6..9b9be55b 100644 --- a/src/google/protobuf/compiler/java/java_extension.cc +++ b/src/google/protobuf/compiler/java/java_extension.cc @@ -75,7 +75,7 @@ void ExtensionGenerator::InitTemplateVars( vars["default"] = descriptor->is_repeated() ? "" : DefaultValue(descriptor, immutable, name_resolver); vars["type_constant"] = FieldTypeName(GetType(descriptor)); - vars["packed"] = descriptor->options().packed() ? "true" : "false"; + vars["packed"] = descriptor->is_packed() ? "true" : "false"; vars["enum_map"] = "null"; vars["prototype"] = "null"; diff --git a/src/google/protobuf/compiler/java/java_field.cc b/src/google/protobuf/compiler/java/java_field.cc index 04917296..1ab18629 100644 --- a/src/google/protobuf/compiler/java/java_field.cc +++ b/src/google/protobuf/compiler/java/java_field.cc @@ -45,8 +45,6 @@ #include <google/protobuf/compiler/java/java_enum_field.h> #include <google/protobuf/compiler/java/java_enum_field_lite.h> #include <google/protobuf/compiler/java/java_helpers.h> -#include <google/protobuf/compiler/java/java_lazy_message_field.h> -#include <google/protobuf/compiler/java/java_lazy_message_field_lite.h> #include <google/protobuf/compiler/java/java_map_field.h> #include <google/protobuf/compiler/java/java_map_field_lite.h> #include <google/protobuf/compiler/java/java_message_field.h> @@ -77,13 +75,8 @@ ImmutableFieldGenerator* MakeImmutableGenerator( return new ImmutableMapFieldGenerator( field, messageBitIndex, builderBitIndex, context); } else { - if (IsLazy(field, context->EnforceLite())) { - return new RepeatedImmutableLazyMessageFieldGenerator( - field, messageBitIndex, builderBitIndex, context); - } else { - return new RepeatedImmutableMessageFieldGenerator( - field, messageBitIndex, builderBitIndex, context); - } + return new RepeatedImmutableMessageFieldGenerator( + field, messageBitIndex, builderBitIndex, context); } case JAVATYPE_ENUM: return new RepeatedImmutableEnumFieldGenerator( @@ -99,13 +92,8 @@ ImmutableFieldGenerator* MakeImmutableGenerator( if (field->containing_oneof()) { switch (GetJavaType(field)) { case JAVATYPE_MESSAGE: - if (IsLazy(field, context->EnforceLite())) { - return new ImmutableLazyMessageOneofFieldGenerator( - field, messageBitIndex, builderBitIndex, context); - } else { - return new ImmutableMessageOneofFieldGenerator( - field, messageBitIndex, builderBitIndex, context); - } + return new ImmutableMessageOneofFieldGenerator( + field, messageBitIndex, builderBitIndex, context); case JAVATYPE_ENUM: return new ImmutableEnumOneofFieldGenerator( field, messageBitIndex, builderBitIndex, context); @@ -119,13 +107,8 @@ ImmutableFieldGenerator* MakeImmutableGenerator( } else { switch (GetJavaType(field)) { case JAVATYPE_MESSAGE: - if (IsLazy(field, context->EnforceLite())) { - return new ImmutableLazyMessageFieldGenerator( - field, messageBitIndex, builderBitIndex, context); - } else { - return new ImmutableMessageFieldGenerator( - field, messageBitIndex, builderBitIndex, context); - } + return new ImmutableMessageFieldGenerator( + field, messageBitIndex, builderBitIndex, context); case JAVATYPE_ENUM: return new ImmutableEnumFieldGenerator( field, messageBitIndex, builderBitIndex, context); @@ -150,13 +133,8 @@ ImmutableFieldLiteGenerator* MakeImmutableLiteGenerator( return new ImmutableMapFieldLiteGenerator( field, messageBitIndex, builderBitIndex, context); } else { - if (IsLazy(field, context->EnforceLite())) { - return new RepeatedImmutableLazyMessageFieldLiteGenerator( - field, messageBitIndex, builderBitIndex, context); - } else { - return new RepeatedImmutableMessageFieldLiteGenerator( - field, messageBitIndex, builderBitIndex, context); - } + return new RepeatedImmutableMessageFieldLiteGenerator( + field, messageBitIndex, builderBitIndex, context); } case JAVATYPE_ENUM: return new RepeatedImmutableEnumFieldLiteGenerator( @@ -172,13 +150,8 @@ ImmutableFieldLiteGenerator* MakeImmutableLiteGenerator( if (field->containing_oneof()) { switch (GetJavaType(field)) { case JAVATYPE_MESSAGE: - if (IsLazy(field, context->EnforceLite())) { - return new ImmutableLazyMessageOneofFieldLiteGenerator( - field, messageBitIndex, builderBitIndex, context); - } else { - return new ImmutableMessageOneofFieldLiteGenerator( - field, messageBitIndex, builderBitIndex, context); - } + return new ImmutableMessageOneofFieldLiteGenerator( + field, messageBitIndex, builderBitIndex, context); case JAVATYPE_ENUM: return new ImmutableEnumOneofFieldLiteGenerator( field, messageBitIndex, builderBitIndex, context); @@ -192,13 +165,8 @@ ImmutableFieldLiteGenerator* MakeImmutableLiteGenerator( } else { switch (GetJavaType(field)) { case JAVATYPE_MESSAGE: - if (IsLazy(field, context->EnforceLite())) { - return new ImmutableLazyMessageFieldLiteGenerator( - field, messageBitIndex, builderBitIndex, context); - } else { - return new ImmutableMessageFieldLiteGenerator( - field, messageBitIndex, builderBitIndex, context); - } + return new ImmutableMessageFieldLiteGenerator( + field, messageBitIndex, builderBitIndex, context); case JAVATYPE_ENUM: return new ImmutableEnumFieldLiteGenerator( field, messageBitIndex, builderBitIndex, context); @@ -293,10 +261,17 @@ void SetCommonFieldVariables(const FieldDescriptor* descriptor, std::map<string, string>* variables) { (*variables)["field_name"] = descriptor->name(); (*variables)["name"] = info->name; + (*variables)["classname"] = descriptor->containing_type()->name(); (*variables)["capitalized_name"] = info->capitalized_name; (*variables)["disambiguated_reason"] = info->disambiguated_reason; (*variables)["constant_name"] = FieldConstantName(descriptor); (*variables)["number"] = SimpleItoa(descriptor->number()); + // These variables are placeholders to pick out the beginning and ends of + // identifiers for annotations (when doing so with existing variables would + // be ambiguous or impossible). They should never be set to anything but the + // empty string. + (*variables)["{"] = ""; + (*variables)["}"] = ""; } void SetCommonOneofVariables(const FieldDescriptor* descriptor, diff --git a/src/google/protobuf/compiler/java/java_file.cc b/src/google/protobuf/compiler/java/java_file.cc index 0fda166d..2d5465ba 100644 --- a/src/google/protobuf/compiler/java/java_file.cc +++ b/src/google/protobuf/compiler/java/java_file.cc @@ -499,19 +499,57 @@ void FileGenerator::GenerateDescriptorInitializationCodeForMutable(io::Printer* // Try to load immutable messages' outer class. Its initialization code // will take care of interpreting custom options. printer->Print( - "try {\n" - // Note that we have to load the immutable class dynamically here as - // we want the mutable code to be independent from the immutable code - // at compile time. It is required to implement dual-compile for - // mutable and immutable API in blaze. - " java.lang.Class immutableClass = java.lang.Class.forName(\n" - " \"$immutable_classname$\");\n" - "} catch (java.lang.ClassNotFoundException e) {\n" - // The immutable class can not be found. Custom options are left - // as unknown fields. - // TODO(xiaofeng): inform the user with a warning? - "}\n", - "immutable_classname", name_resolver_->GetImmutableClassName(file_)); + "try {\n" + // Note that we have to load the immutable class dynamically here as + // we want the mutable code to be independent from the immutable code + // at compile time. It is required to implement dual-compile for + // mutable and immutable API in blaze. + " java.lang.Class immutableClass = java.lang.Class.forName(\n" + " \"$immutable_classname$\");\n" + "} catch (java.lang.ClassNotFoundException e) {\n", + "immutable_classname", name_resolver_->GetImmutableClassName(file_)); + printer->Indent(); + + // The immutable class can not be found. We try our best to collect all + // custom option extensions to interpret the custom options. + printer->Print( + "com.google.protobuf.ExtensionRegistry registry =\n" + " com.google.protobuf.ExtensionRegistry.newInstance();\n" + "com.google.protobuf.MessageLite defaultExtensionInstance = null;\n"); + FieldDescriptorSet::iterator it; + for (it = extensions.begin(); it != extensions.end(); it++) { + const FieldDescriptor* field = *it; + string scope; + if (field->extension_scope() != NULL) { + scope = name_resolver_->GetMutableClassName(field->extension_scope()) + + ".getDescriptor()"; + } else { + scope = FileJavaPackage(field->file(), true) + "." + + name_resolver_->GetDescriptorClassName(field->file()) + + ".descriptor"; + } + if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { + printer->Print( + "defaultExtensionInstance = com.google.protobuf.Internal\n" + " .getDefaultInstance(\"$class$\");\n" + "if (defaultExtensionInstance != null) {\n" + " registry.add(\n" + " $scope$.getExtensions().get($index$),\n" + " (com.google.protobuf.Message) defaultExtensionInstance);\n" + "}\n", + "scope", scope, "index", SimpleItoa(field->index()), "class", + name_resolver_->GetImmutableClassName(field->message_type())); + } else { + printer->Print("registry.add($scope$.getExtensions().get($index$));\n", + "scope", scope, "index", SimpleItoa(field->index())); + } + } + printer->Print( + "com.google.protobuf.Descriptors.FileDescriptor\n" + " .internalUpdateFileDescriptor(descriptor, registry);\n"); + + printer->Outdent(); + printer->Print("}\n"); } // Force descriptor initialization of all dependencies. diff --git a/src/google/protobuf/compiler/java/java_generator.cc b/src/google/protobuf/compiler/java/java_generator.cc index 02141210..84a3b90d 100644 --- a/src/google/protobuf/compiler/java/java_generator.cc +++ b/src/google/protobuf/compiler/java/java_generator.cc @@ -42,6 +42,7 @@ #include <google/protobuf/compiler/java/java_file.h> #include <google/protobuf/compiler/java/java_generator_factory.h> #include <google/protobuf/compiler/java/java_helpers.h> +#include <google/protobuf/compiler/java/java_name_resolver.h> #include <google/protobuf/compiler/java/java_options.h> #include <google/protobuf/compiler/java/java_shared_code_generator.h> #include <google/protobuf/io/printer.h> diff --git a/src/google/protobuf/compiler/java/java_helpers.cc b/src/google/protobuf/compiler/java/java_helpers.cc index 8469740f..d8ac2db3 100644 --- a/src/google/protobuf/compiler/java/java_helpers.cc +++ b/src/google/protobuf/compiler/java/java_helpers.cc @@ -43,6 +43,8 @@ #include <google/protobuf/wire_format.h> #include <google/protobuf/stubs/strutil.h> #include <google/protobuf/stubs/substitute.h> + + #include <google/protobuf/stubs/hash.h> // for hash<T *> namespace google { @@ -167,6 +169,14 @@ string UniqueFileScopeIdentifier(const Descriptor* descriptor) { return "static_" + StringReplace(descriptor->full_name(), ".", "_", true); } +string CamelCaseFieldName(const FieldDescriptor* field) { + string fieldName = UnderscoresToCamelCase(field); + if ('0' <= fieldName[0] && fieldName[0] <= '9') { + return '_' + fieldName; + } + return fieldName; +} + string StripProto(const string& filename) { if (HasSuffixString(filename, ".protodevel")) { return StripSuffixString(filename, ".protodevel"); diff --git a/src/google/protobuf/compiler/java/java_helpers.h b/src/google/protobuf/compiler/java/java_helpers.h index 829ec3d7..bd565ced 100644 --- a/src/google/protobuf/compiler/java/java_helpers.h +++ b/src/google/protobuf/compiler/java/java_helpers.h @@ -74,6 +74,10 @@ string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field); // of lower-casing the first letter of the name.) string UnderscoresToCamelCase(const MethodDescriptor* method); +// Similar to UnderscoresToCamelCase, but guarentees that the result is a +// complete Java identifier by adding a _ if needed. +string CamelCaseFieldName(const FieldDescriptor* field); + // Get an identifier that uniquely identifies this type within the file. // This is used to declare static variables related to this type at the // outermost file scope. @@ -242,15 +246,6 @@ inline bool HasGenericServices(const FileDescriptor *file, bool enforce_lite) { file->options().java_generic_services(); } -inline bool IsLazy(const FieldDescriptor* descriptor, bool enforce_lite) { - // Currently, the proto-lite version supports lazy field. - // TODO(niwasaki): Support lazy fields also for other proto runtimes. - if (HasDescriptorMethods(descriptor->file(), enforce_lite)) { - return false; - } - return descriptor->options().lazy(); -} - // Methods for shared bitfields. // Gets the name of the shared bitfield for the given index. @@ -372,10 +367,6 @@ inline bool IsMapField(const FieldDescriptor* descriptor) { return descriptor->is_map(); } -inline bool PreserveUnknownFields(const Descriptor* descriptor) { - return descriptor->file()->syntax() != FileDescriptor::SYNTAX_PROTO3; -} - inline bool IsAnyMessage(const Descriptor* descriptor) { return descriptor->full_name() == "google.protobuf.Any"; } diff --git a/src/google/protobuf/compiler/java/java_map_field.cc b/src/google/protobuf/compiler/java/java_map_field.cc index 608b5378..b22a2199 100644 --- a/src/google/protobuf/compiler/java/java_map_field.cc +++ b/src/google/protobuf/compiler/java/java_map_field.cc @@ -183,12 +183,14 @@ GenerateInterfaceMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, - "$deprecation$int get$capitalized_name$Count();\n"); + "$deprecation$int ${$get$capitalized_name$Count$}$();\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, - "$deprecation$boolean contains$capitalized_name$(\n" + "$deprecation$boolean ${$contains$capitalized_name$$}$(\n" " $key_type$ key);\n"); + printer->Annotate("{", "}", descriptor_); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { printer->Print( variables_, @@ -197,23 +199,27 @@ GenerateInterfaceMembers(io::Printer* printer) const { " */\n" "@java.lang.Deprecated\n" "java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "get$capitalized_name$();\n"); + "${$get$capitalized_name$$}$();\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "get$capitalized_name$Map();\n"); + "${$get$capitalized_name$Map$}$();\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, - "$deprecation$$value_enum_type$ get$capitalized_name$OrDefault(\n" + "$deprecation$$value_enum_type$ ${$get$capitalized_name$OrDefault$}$(\n" " $key_type$ key,\n" " $value_enum_type$ defaultValue);\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, - "$deprecation$$value_enum_type$ get$capitalized_name$OrThrow(\n" + "$deprecation$$value_enum_type$ ${$get$capitalized_name$OrThrow$}$(\n" " $key_type$ key);\n"); + printer->Annotate("{", "}", descriptor_); if (SupportUnknownEnumValue(descriptor_->file())) { printer->Print( variables_, @@ -222,25 +228,29 @@ GenerateInterfaceMembers(io::Printer* printer) const { " */\n" "@java.lang.Deprecated\n" "java.util.Map<$type_parameters$>\n" - "get$capitalized_name$Value();\n"); + "${$get$capitalized_name$Value$}$();\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$java.util.Map<$type_parameters$>\n" - "get$capitalized_name$ValueMap();\n"); + "${$get$capitalized_name$ValueMap$}$();\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "$value_type$ get$capitalized_name$ValueOrDefault(\n" + "$value_type$ ${$get$capitalized_name$ValueOrDefault$}$(\n" " $key_type$ key,\n" " $value_type$ defaultValue);\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "$value_type$ get$capitalized_name$ValueOrThrow(\n" + "$value_type$ ${$get$capitalized_name$ValueOrThrow$}$(\n" " $key_type$ key);\n"); + printer->Annotate("{", "}", descriptor_); } } else { printer->Print( @@ -250,25 +260,29 @@ GenerateInterfaceMembers(io::Printer* printer) const { " */\n" "@java.lang.Deprecated\n" "java.util.Map<$type_parameters$>\n" - "get$capitalized_name$();\n"); + "${$get$capitalized_name$$}$();\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$java.util.Map<$type_parameters$>\n" - "get$capitalized_name$Map();\n"); + "${$get$capitalized_name$Map$}$();\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "$value_type$ get$capitalized_name$OrDefault(\n" + "$value_type$ ${$get$capitalized_name$OrDefault$}$(\n" " $key_type$ key,\n" " $value_type$ defaultValue);\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "$value_type$ get$capitalized_name$OrThrow(\n" + "$value_type$ ${$get$capitalized_name$OrThrow$}$(\n" " $key_type$ key);\n"); + printer->Annotate("{", "}", descriptor_); } } @@ -349,23 +363,27 @@ GenerateBuilderMembers(io::Printer* printer) const { " return $name$_;\n" "}\n"); GenerateMapGetters(printer); - printer->Print(variables_, - "$deprecation$\n" - "public Builder clear$capitalized_name$() {\n" - " internalGetMutable$capitalized_name$().getMutableMap()\n" - " .clear();\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$\n" + "public Builder ${$clear$capitalized_name$$}$() {\n" + " internalGetMutable$capitalized_name$().getMutableMap()\n" + " .clear();\n" + " return this;\n" + "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$\n" - "public Builder remove$capitalized_name$(\n" - " $key_type$ key) {\n" - " $key_null_check$\n" - " internalGetMutable$capitalized_name$().getMutableMap()\n" - " .remove(key);\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$\n" + "public Builder ${$remove$capitalized_name$$}$(\n" + " $key_type$ key) {\n" + " $key_null_check$\n" + " internalGetMutable$capitalized_name$().getMutableMap()\n" + " .remove(key);\n" + " return this;\n" + "}\n"); + printer->Annotate("{", "}", descriptor_); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { printer->Print( variables_, @@ -374,13 +392,14 @@ GenerateBuilderMembers(io::Printer* printer) const { " */\n" "@java.lang.Deprecated\n" "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "getMutable$capitalized_name$() {\n" + "${$getMutable$capitalized_name$$}$() {\n" " return internalGetAdapted$capitalized_name$Map(\n" " internalGetMutable$capitalized_name$().getMutableMap());\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder put$capitalized_name$(\n" + "$deprecation$public Builder ${$put$capitalized_name$$}$(\n" " $key_type$ key,\n" " $value_enum_type$ value) {\n" " $key_null_check$\n" @@ -389,16 +408,18 @@ GenerateBuilderMembers(io::Printer* printer) const { " .put(key, $name$ValueConverter.doBackward(value));\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, - "$deprecation$public Builder putAll$capitalized_name$(\n" + "$deprecation$public Builder ${$putAll$capitalized_name$$}$(\n" " java.util.Map<$boxed_key_type$, $value_enum_type$> values) {\n" " internalGetAdapted$capitalized_name$Map(\n" " internalGetMutable$capitalized_name$().getMutableMap())\n" " .putAll(values);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); if (SupportUnknownEnumValue(descriptor_->file())) { printer->Print( variables_, @@ -407,13 +428,14 @@ GenerateBuilderMembers(io::Printer* printer) const { " */\n" "@java.lang.Deprecated\n" "public java.util.Map<$boxed_key_type$, $boxed_value_type$>\n" - "getMutable$capitalized_name$Value() {\n" + "${$getMutable$capitalized_name$Value$}$() {\n" " return internalGetMutable$capitalized_name$().getMutableMap();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, - "$deprecation$public Builder put$capitalized_name$Value(\n" + "$deprecation$public Builder ${$put$capitalized_name$Value$}$(\n" " $key_type$ key,\n" " $value_type$ value) {\n" " $key_null_check$\n" @@ -421,15 +443,17 @@ GenerateBuilderMembers(io::Printer* printer) const { " .put(key, value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, - "$deprecation$public Builder putAll$capitalized_name$Value(\n" + "$deprecation$public Builder ${$putAll$capitalized_name$Value$}$(\n" " java.util.Map<$boxed_key_type$, $boxed_value_type$> values) {\n" " internalGetMutable$capitalized_name$().getMutableMap()\n" " .putAll(values);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } } else { printer->Print( @@ -439,30 +463,35 @@ GenerateBuilderMembers(io::Printer* printer) const { " */\n" "@java.lang.Deprecated\n" "public java.util.Map<$type_parameters$>\n" - "getMutable$capitalized_name$() {\n" + "${$getMutable$capitalized_name$$}$() {\n" " return internalGetMutable$capitalized_name$().getMutableMap();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$" - "public Builder put$capitalized_name$(\n" - " $key_type$ key,\n" - " $value_type$ value) {\n" - " $key_null_check$\n" - " $value_null_check$\n" - " internalGetMutable$capitalized_name$().getMutableMap()\n" - " .put(key, value);\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$" + "public Builder ${$put$capitalized_name$$}$(\n" + " $key_type$ key,\n" + " $value_type$ value) {\n" + " $key_null_check$\n" + " $value_null_check$\n" + " internalGetMutable$capitalized_name$().getMutableMap()\n" + " .put(key, value);\n" + " return this;\n" + "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$\n" - "public Builder putAll$capitalized_name$(\n" - " java.util.Map<$type_parameters$> values) {\n" - " internalGetMutable$capitalized_name$().getMutableMap()\n" - " .putAll(values);\n" - " return this;\n" - "}\n"); + printer->Print( + variables_, + "$deprecation$\n" + "public Builder ${$putAll$capitalized_name$$}$(\n" + " java.util.Map<$type_parameters$> values) {\n" + " internalGetMutable$capitalized_name$().getMutableMap()\n" + " .putAll(values);\n" + " return this;\n" + "}\n"); + printer->Annotate("{", "}", descriptor_); } } @@ -471,18 +500,20 @@ GenerateMapGetters(io::Printer* printer) const { printer->Print( variables_, "$deprecation$\n" - "public int get$capitalized_name$Count() {\n" + "public int ${$get$capitalized_name$Count$}$() {\n" " return internalGet$capitalized_name$().getMap().size();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public boolean contains$capitalized_name$(\n" + "public boolean ${$contains$capitalized_name$$}$(\n" " $key_type$ key) {\n" " $key_null_check$\n" " return internalGet$capitalized_name$().getMap().containsKey(key);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { printer->Print( variables_, @@ -491,22 +522,25 @@ GenerateMapGetters(io::Printer* printer) const { " */\n" "@java.lang.Deprecated\n" "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "get$capitalized_name$() {\n" + "${$get$capitalized_name$$}$() {\n" " return get$capitalized_name$Map();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$\n" - "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "get$capitalized_name$Map() {\n" - " return internalGetAdapted$capitalized_name$Map(\n" - " internalGet$capitalized_name$().getMap());" - "}\n"); + printer->Print( + variables_, + "$deprecation$\n" + "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" + "${$get$capitalized_name$Map$}$() {\n" + " return internalGetAdapted$capitalized_name$Map(\n" + " internalGet$capitalized_name$().getMap());" + "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public $value_enum_type$ get$capitalized_name$OrDefault(\n" + "public $value_enum_type$ ${$get$capitalized_name$OrDefault$}$(\n" " $key_type$ key,\n" " $value_enum_type$ defaultValue) {\n" " $key_null_check$\n" @@ -516,11 +550,12 @@ GenerateMapGetters(io::Printer* printer) const { " ? $name$ValueConverter.doForward(map.get(key))\n" " : defaultValue;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public $value_enum_type$ get$capitalized_name$OrThrow(\n" + "public $value_enum_type$ ${$get$capitalized_name$OrThrow$}$(\n" " $key_type$ key) {\n" " $key_null_check$\n" " java.util.Map<$boxed_key_type$, $boxed_value_type$> map =\n" @@ -530,6 +565,7 @@ GenerateMapGetters(io::Printer* printer) const { " }\n" " return $name$ValueConverter.doForward(map.get(key));\n" "}\n"); + printer->Annotate("{", "}", descriptor_); if (SupportUnknownEnumValue(descriptor_->file())) { printer->Print( variables_, @@ -538,22 +574,24 @@ GenerateMapGetters(io::Printer* printer) const { " */\n" "@java.lang.Deprecated\n" "public java.util.Map<$boxed_key_type$, $boxed_value_type$>\n" - "get$capitalized_name$Value() {\n" + "${$get$capitalized_name$Value$}$() {\n" " return get$capitalized_name$ValueMap();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" "public java.util.Map<$boxed_key_type$, $boxed_value_type$>\n" - "get$capitalized_name$ValueMap() {\n" + "${$get$capitalized_name$ValueMap$}$() {\n" " return internalGet$capitalized_name$().getMap();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public $value_type$ get$capitalized_name$ValueOrDefault(\n" + "public $value_type$ ${$get$capitalized_name$ValueOrDefault$}$(\n" " $key_type$ key,\n" " $value_type$ defaultValue) {\n" " $key_null_check$\n" @@ -561,11 +599,12 @@ GenerateMapGetters(io::Printer* printer) const { " internalGet$capitalized_name$().getMap();\n" " return map.containsKey(key) ? map.get(key) : defaultValue;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public $value_type$ get$capitalized_name$ValueOrThrow(\n" + "public $value_type$ ${$get$capitalized_name$ValueOrThrow$}$(\n" " $key_type$ key) {\n" " $key_null_check$\n" " java.util.Map<$boxed_key_type$, $boxed_value_type$> map =\n" @@ -575,6 +614,7 @@ GenerateMapGetters(io::Printer* printer) const { " }\n" " return map.get(key);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } } else { printer->Print( @@ -583,21 +623,25 @@ GenerateMapGetters(io::Printer* printer) const { " * Use {@link #get$capitalized_name$Map()} instead.\n" " */\n" "@java.lang.Deprecated\n" - "public java.util.Map<$type_parameters$> get$capitalized_name$() {\n" + "public java.util.Map<$type_parameters$> " + "${$get$capitalized_name$$}$() {\n" " return get$capitalized_name$Map();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public java.util.Map<$type_parameters$> get$capitalized_name$Map() {\n" + "public java.util.Map<$type_parameters$> " + "${$get$capitalized_name$Map$}$() {\n" " return internalGet$capitalized_name$().getMap();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public $value_type$ get$capitalized_name$OrDefault(\n" + "public $value_type$ ${$get$capitalized_name$OrDefault$}$(\n" " $key_type$ key,\n" " $value_type$ defaultValue) {\n" " $key_null_check$\n" @@ -605,11 +649,12 @@ GenerateMapGetters(io::Printer* printer) const { " internalGet$capitalized_name$().getMap();\n" " return map.containsKey(key) ? map.get(key) : defaultValue;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public $value_type$ get$capitalized_name$OrThrow(\n" + "public $value_type$ ${$get$capitalized_name$OrThrow$}$(\n" " $key_type$ key) {\n" " $key_null_check$\n" " java.util.Map<$type_parameters$> map =\n" @@ -619,6 +664,7 @@ GenerateMapGetters(io::Printer* printer) const { " }\n" " return map.get(key);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } } diff --git a/src/google/protobuf/compiler/java/java_map_field_lite.cc b/src/google/protobuf/compiler/java/java_map_field_lite.cc index 073d1cfc..f19ec271 100644 --- a/src/google/protobuf/compiler/java/java_map_field_lite.cc +++ b/src/google/protobuf/compiler/java/java_map_field_lite.cc @@ -166,12 +166,14 @@ GenerateInterfaceMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, - "$deprecation$int get$capitalized_name$Count();\n"); + "$deprecation$int ${$get$capitalized_name$Count$}$();\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, - "$deprecation$boolean contains$capitalized_name$(\n" + "$deprecation$boolean ${$contains$capitalized_name$$}$(\n" " $key_type$ key);\n"); + printer->Annotate("{", "}", descriptor_); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { printer->Print( variables_, @@ -180,23 +182,27 @@ GenerateInterfaceMembers(io::Printer* printer) const { " */\n" "@java.lang.Deprecated\n" "java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "get$capitalized_name$();\n"); + "${$get$capitalized_name$$}$();\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "get$capitalized_name$Map();\n"); + "${$get$capitalized_name$Map$}$();\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, - "$deprecation$$value_enum_type$ get$capitalized_name$OrDefault(\n" + "$deprecation$$value_enum_type$ ${$get$capitalized_name$OrDefault$}$(\n" " $key_type$ key,\n" " $value_enum_type$ defaultValue);\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, - "$deprecation$$value_enum_type$ get$capitalized_name$OrThrow(\n" + "$deprecation$$value_enum_type$ ${$get$capitalized_name$OrThrow$}$(\n" " $key_type$ key);\n"); + printer->Annotate("{", "}", descriptor_); if (SupportUnknownEnumValue(descriptor_->file())) { printer->Print( variables_, @@ -205,25 +211,29 @@ GenerateInterfaceMembers(io::Printer* printer) const { " */\n" "@java.lang.Deprecated\n" "java.util.Map<$type_parameters$>\n" - "get$capitalized_name$Value();\n"); + "${$get$capitalized_name$Value$}$();\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$java.util.Map<$type_parameters$>\n" - "get$capitalized_name$ValueMap();\n"); + "${$get$capitalized_name$ValueMap$}$();\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "$value_type$ get$capitalized_name$ValueOrDefault(\n" + "$value_type$ ${$get$capitalized_name$ValueOrDefault$}$(\n" " $key_type$ key,\n" " $value_type$ defaultValue);\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "$value_type$ get$capitalized_name$ValueOrThrow(\n" + "$value_type$ ${$get$capitalized_name$ValueOrThrow$}$(\n" " $key_type$ key);\n"); + printer->Annotate("{", "}", descriptor_); } } else { printer->Print( @@ -233,25 +243,29 @@ GenerateInterfaceMembers(io::Printer* printer) const { " */\n" "@java.lang.Deprecated\n" "java.util.Map<$type_parameters$>\n" - "get$capitalized_name$();\n"); + "${$get$capitalized_name$$}$();\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$java.util.Map<$type_parameters$>\n" - "get$capitalized_name$Map();\n"); + "${$get$capitalized_name$Map$}$();\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "$value_type$ get$capitalized_name$OrDefault(\n" + "$value_type$ ${$get$capitalized_name$OrDefault$}$(\n" " $key_type$ key,\n" " $value_type$ defaultValue);\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "$value_type$ get$capitalized_name$OrThrow(\n" + "$value_type$ ${$get$capitalized_name$OrThrow$}$(\n" " $key_type$ key);\n"); + printer->Annotate("{", "}", descriptor_); } } @@ -288,18 +302,20 @@ GenerateMembers(io::Printer* printer) const { printer->Print( variables_, "$deprecation$\n" - "public int get$capitalized_name$Count() {\n" + "public int ${$get$capitalized_name$Count$}$() {\n" " return internalGet$capitalized_name$().size();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public boolean contains$capitalized_name$(\n" + "public boolean ${$contains$capitalized_name$$}$(\n" " $key_type$ key) {\n" " $key_null_check$\n" " return internalGet$capitalized_name$().containsKey(key);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { printer->Print( variables_, @@ -316,26 +332,28 @@ GenerateMembers(io::Printer* printer) const { " */\n" "@java.lang.Deprecated\n" "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "get$capitalized_name$() {\n" + "${$get$capitalized_name$$}$() {\n" " return get$capitalized_name$Map();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "get$capitalized_name$Map() {\n" + "${$get$capitalized_name$Map$}$() {\n" " return java.util.Collections.unmodifiableMap(\n" " new com.google.protobuf.Internal.MapAdapter<\n" " $boxed_key_type$, $value_enum_type$, java.lang.Integer>(\n" " internalGet$capitalized_name$(),\n" " $name$ValueConverter));\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public $value_enum_type$ get$capitalized_name$OrDefault(\n" + "public $value_enum_type$ ${$get$capitalized_name$OrDefault$}$(\n" " $key_type$ key,\n" " $value_enum_type$ defaultValue) {\n" " $key_null_check$\n" @@ -345,11 +363,12 @@ GenerateMembers(io::Printer* printer) const { " ? $name$ValueConverter.doForward(map.get(key))\n" " : defaultValue;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public $value_enum_type$ get$capitalized_name$OrThrow(\n" + "public $value_enum_type$ ${$get$capitalized_name$OrThrow$}$(\n" " $key_type$ key) {\n" " $key_null_check$\n" " java.util.Map<$boxed_key_type$, $boxed_value_type$> map =\n" @@ -359,6 +378,7 @@ GenerateMembers(io::Printer* printer) const { " }\n" " return $name$ValueConverter.doForward(map.get(key));\n" "}\n"); + printer->Annotate("{", "}", descriptor_); if (SupportUnknownEnumValue(descriptor_->file())) { printer->Print( variables_, @@ -367,23 +387,25 @@ GenerateMembers(io::Printer* printer) const { " */\n" "@java.lang.Deprecated\n" "public java.util.Map<$boxed_key_type$, $boxed_value_type$>\n" - "get$capitalized_name$Value() {\n" + "${$get$capitalized_name$Value$}$() {\n" " return get$capitalized_name$ValueMap();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" "public java.util.Map<$boxed_key_type$, $boxed_value_type$>\n" - "get$capitalized_name$ValueMap() {\n" + "${$get$capitalized_name$ValueMap$}$() {\n" " return java.util.Collections.unmodifiableMap(\n" " internalGet$capitalized_name$());\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public $value_type$ get$capitalized_name$ValueOrDefault(\n" + "public $value_type$ ${$get$capitalized_name$ValueOrDefault$}$(\n" " $key_type$ key,\n" " $value_type$ defaultValue) {\n" " $key_null_check$\n" @@ -391,11 +413,12 @@ GenerateMembers(io::Printer* printer) const { " internalGet$capitalized_name$();\n" " return map.containsKey(key) ? map.get(key) : defaultValue;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public $value_type$ get$capitalized_name$ValueOrThrow(\n" + "public $value_type$ ${$get$capitalized_name$ValueOrThrow$}$(\n" " $key_type$ key) {\n" " $key_null_check$\n" " java.util.Map<$boxed_key_type$, $boxed_value_type$> map =\n" @@ -405,6 +428,7 @@ GenerateMembers(io::Printer* printer) const { " }\n" " return map.get(key);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } } else { printer->Print( @@ -413,22 +437,26 @@ GenerateMembers(io::Printer* printer) const { " * Use {@link #get$capitalized_name$Map()} instead.\n" " */\n" "@java.lang.Deprecated\n" - "public java.util.Map<$type_parameters$> get$capitalized_name$() {\n" + "public java.util.Map<$type_parameters$> " + "${$get$capitalized_name$$}$() {\n" " return get$capitalized_name$Map();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public java.util.Map<$type_parameters$> get$capitalized_name$Map() {\n" + "public java.util.Map<$type_parameters$> " + "${$get$capitalized_name$Map$}$() {\n" " return java.util.Collections.unmodifiableMap(\n" " internalGet$capitalized_name$());\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public $value_type$ get$capitalized_name$OrDefault(\n" + "public $value_type$ ${$get$capitalized_name$OrDefault$}$(\n" " $key_type$ key,\n" " $value_type$ defaultValue) {\n" " $key_null_check$\n" @@ -436,11 +464,12 @@ GenerateMembers(io::Printer* printer) const { " internalGet$capitalized_name$();\n" " return map.containsKey(key) ? map.get(key) : defaultValue;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public $value_type$ get$capitalized_name$OrThrow(\n" + "public $value_type$ ${$get$capitalized_name$OrThrow$}$(\n" " $key_type$ key) {\n" " $key_null_check$\n" " java.util.Map<$type_parameters$> map =\n" @@ -450,6 +479,7 @@ GenerateMembers(io::Printer* printer) const { " }\n" " return map.get(key);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } // Generate private setters for the builder to proxy into. @@ -490,37 +520,41 @@ GenerateBuilderMembers(io::Printer* printer) const { printer->Print( variables_, "$deprecation$\n" - "public int get$capitalized_name$Count() {\n" + "public int ${$get$capitalized_name$Count$}$() {\n" " return instance.get$capitalized_name$Map().size();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public boolean contains$capitalized_name$(\n" + "public boolean ${$contains$capitalized_name$$}$(\n" " $key_type$ key) {\n" " $key_null_check$\n" " return instance.get$capitalized_name$Map().containsKey(key);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); printer->Print( variables_, "$deprecation$\n" - "public Builder clear$capitalized_name$() {\n" + "public Builder ${$clear$capitalized_name$$}$() {\n" " copyOnWrite();\n" " instance.getMutable$capitalized_name$Map().clear();\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public Builder remove$capitalized_name$(\n" + "public Builder ${$remove$capitalized_name$$}$(\n" " $key_type$ key) {\n" " $key_null_check$\n" " copyOnWrite();\n" " instance.getMutable$capitalized_name$Map().remove(key);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); if (GetJavaType(ValueField(descriptor_)) == JAVATYPE_ENUM) { printer->Print( variables_, @@ -529,23 +563,25 @@ GenerateBuilderMembers(io::Printer* printer) const { " */\n" "@java.lang.Deprecated\n" "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "get$capitalized_name$() {\n" + "${$get$capitalized_name$$}$() {\n" " return get$capitalized_name$Map();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" "public java.util.Map<$boxed_key_type$, $value_enum_type$>\n" - "get$capitalized_name$Map() {\n" + "${$get$capitalized_name$Map$}$() {\n" " return java.util.Collections.unmodifiableMap(\n" " instance.get$capitalized_name$Map());\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public $value_enum_type$ get$capitalized_name$OrDefault(\n" + "public $value_enum_type$ ${$get$capitalized_name$OrDefault$}$(\n" " $key_type$ key,\n" " $value_enum_type$ defaultValue) {\n" " $key_null_check$\n" @@ -555,11 +591,12 @@ GenerateBuilderMembers(io::Printer* printer) const { " ? map.get(key)\n" " : defaultValue;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public $value_enum_type$ get$capitalized_name$OrThrow(\n" + "public $value_enum_type$ ${$get$capitalized_name$OrThrow$}$(\n" " $key_type$ key) {\n" " $key_null_check$\n" " java.util.Map<$boxed_key_type$, $value_enum_type$> map =\n" @@ -569,10 +606,11 @@ GenerateBuilderMembers(io::Printer* printer) const { " }\n" " return map.get(key);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, - "$deprecation$public Builder put$capitalized_name$(\n" + "$deprecation$public Builder ${$put$capitalized_name$$}$(\n" " $key_type$ key,\n" " $value_enum_type$ value) {\n" " $key_null_check$\n" @@ -581,15 +619,17 @@ GenerateBuilderMembers(io::Printer* printer) const { " instance.getMutable$capitalized_name$Map().put(key, value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, - "$deprecation$public Builder putAll$capitalized_name$(\n" + "$deprecation$public Builder ${$putAll$capitalized_name$$}$(\n" " java.util.Map<$boxed_key_type$, $value_enum_type$> values) {\n" " copyOnWrite();\n" " instance.getMutable$capitalized_name$Map().putAll(values);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); if (SupportUnknownEnumValue(descriptor_->file())) { printer->Print( variables_, @@ -598,23 +638,25 @@ GenerateBuilderMembers(io::Printer* printer) const { " */\n" "@java.lang.Deprecated\n" "public java.util.Map<$boxed_key_type$, $boxed_value_type$>\n" - "get$capitalized_name$Value() {\n" + "${$get$capitalized_name$Value$}$() {\n" " return get$capitalized_name$ValueMap();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" "public java.util.Map<$boxed_key_type$, $boxed_value_type$>\n" - "get$capitalized_name$ValueMap() {\n" + "${$get$capitalized_name$ValueMap$}$() {\n" " return java.util.Collections.unmodifiableMap(\n" " instance.get$capitalized_name$ValueMap());\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public $value_type$ get$capitalized_name$ValueOrDefault(\n" + "public $value_type$ ${$get$capitalized_name$ValueOrDefault$}$(\n" " $key_type$ key,\n" " $value_type$ defaultValue) {\n" " $key_null_check$\n" @@ -622,11 +664,12 @@ GenerateBuilderMembers(io::Printer* printer) const { " instance.get$capitalized_name$ValueMap();\n" " return map.containsKey(key) ? map.get(key) : defaultValue;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public $value_type$ get$capitalized_name$ValueOrThrow(\n" + "public $value_type$ ${$get$capitalized_name$ValueOrThrow$}$(\n" " $key_type$ key) {\n" " $key_null_check$\n" " java.util.Map<$boxed_key_type$, $boxed_value_type$> map =\n" @@ -636,10 +679,11 @@ GenerateBuilderMembers(io::Printer* printer) const { " }\n" " return map.get(key);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, - "$deprecation$public Builder put$capitalized_name$Value(\n" + "$deprecation$public Builder ${$put$capitalized_name$Value$}$(\n" " $key_type$ key,\n" " $value_type$ value) {\n" " $key_null_check$\n" @@ -647,15 +691,17 @@ GenerateBuilderMembers(io::Printer* printer) const { " instance.getMutable$capitalized_name$ValueMap().put(key, value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, - "$deprecation$public Builder putAll$capitalized_name$Value(\n" + "$deprecation$public Builder ${$putAll$capitalized_name$Value$}$(\n" " java.util.Map<$boxed_key_type$, $boxed_value_type$> values) {\n" " copyOnWrite();\n" " instance.getMutable$capitalized_name$ValueMap().putAll(values);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } } else { printer->Print( @@ -664,22 +710,26 @@ GenerateBuilderMembers(io::Printer* printer) const { " * Use {@link #get$capitalized_name$Map()} instead.\n" " */\n" "@java.lang.Deprecated\n" - "public java.util.Map<$type_parameters$> get$capitalized_name$() {\n" + "public java.util.Map<$type_parameters$> " + "${$get$capitalized_name$$}$() {\n" " return get$capitalized_name$Map();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$" - "public java.util.Map<$type_parameters$> get$capitalized_name$Map() {\n" + "public java.util.Map<$type_parameters$> " + "${$get$capitalized_name$Map$}$() {\n" " return java.util.Collections.unmodifiableMap(\n" " instance.get$capitalized_name$Map());\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public $value_type$ get$capitalized_name$OrDefault(\n" + "public $value_type$ ${$get$capitalized_name$OrDefault$}$(\n" " $key_type$ key,\n" " $value_type$ defaultValue) {\n" " $key_null_check$\n" @@ -687,11 +737,12 @@ GenerateBuilderMembers(io::Printer* printer) const { " instance.get$capitalized_name$Map();\n" " return map.containsKey(key) ? map.get(key) : defaultValue;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$\n" - "public $value_type$ get$capitalized_name$OrThrow(\n" + "public $value_type$ ${$get$capitalized_name$OrThrow$}$(\n" " $key_type$ key) {\n" " $key_null_check$\n" " java.util.Map<$type_parameters$> map =\n" @@ -701,11 +752,12 @@ GenerateBuilderMembers(io::Printer* printer) const { " }\n" " return map.get(key);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$" - "public Builder put$capitalized_name$(\n" + "public Builder ${$put$capitalized_name$$}$(\n" " $key_type$ key,\n" " $value_type$ value) {\n" " $key_null_check$\n" @@ -714,16 +766,18 @@ GenerateBuilderMembers(io::Printer* printer) const { " instance.getMutable$capitalized_name$Map().put(key, value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print( variables_, "$deprecation$" - "public Builder putAll$capitalized_name$(\n" + "public Builder ${$putAll$capitalized_name$$}$(\n" " java.util.Map<$type_parameters$> values) {\n" " copyOnWrite();\n" " instance.getMutable$capitalized_name$Map().putAll(values);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } } diff --git a/src/google/protobuf/compiler/java/java_message.cc b/src/google/protobuf/compiler/java/java_message.cc index 6e3b4a75..ecc67575 100644 --- a/src/google/protobuf/compiler/java/java_message.cc +++ b/src/google/protobuf/compiler/java/java_message.cc @@ -56,8 +56,9 @@ #include <google/protobuf/io/printer.h> #include <google/protobuf/descriptor.pb.h> #include <google/protobuf/wire_format.h> -#include <google/protobuf/stubs/strutil.h> #include <google/protobuf/stubs/substitute.h> +#include <google/protobuf/stubs/strutil.h> + namespace google { namespace protobuf { @@ -253,7 +254,7 @@ void ImmutableMessageGenerator::GenerateInterface(io::Printer* printer) { /* immutable = */ true, "OrBuilder"); if (descriptor_->extension_range_count() > 0) { printer->Print( - "$deprecation$public interface $classname$OrBuilder$idend$ extends\n" + "$deprecation$public interface ${$$classname$OrBuilder$}$ extends\n" " $extra_interfaces$\n" " com.google.protobuf.GeneratedMessage$ver$.\n" " ExtendableMessageOrBuilder<$classname$> {\n", @@ -261,19 +262,19 @@ void ImmutableMessageGenerator::GenerateInterface(io::Printer* printer) { "@java.lang.Deprecated " : "", "extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_), "classname", descriptor_->name(), - "idend", "", "ver", GeneratedCodeVersionSuffix()); + "{", "", "}", "", "ver", GeneratedCodeVersionSuffix()); } else { printer->Print( - "$deprecation$public interface $classname$OrBuilder$idend$ extends\n" + "$deprecation$public interface ${$$classname$OrBuilder$}$ extends\n" " $extra_interfaces$\n" " com.google.protobuf.MessageOrBuilder {\n", "deprecation", descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "", "extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_), "classname", descriptor_->name(), - "idend", ""); + "{", "", "}", ""); } - printer->Annotate("classname", "idend", descriptor_); + printer->Annotate("{", "}", descriptor_); printer->Indent(); for (int i = 0; i < descriptor_->field_count(); i++) { @@ -345,6 +346,9 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { "com.google.protobuf.GeneratedMessage$0.Builder<?>", GeneratedCodeVersionSuffix()); } + printer->Print( + "private static final long serialVersionUID = 0L;\n"); + printer->Indent(); // Using builder_type, instead of Builder, prevents the Builder class from // being loaded into PermGen space when the default instance is created. @@ -370,15 +374,8 @@ void ImmutableMessageGenerator::Generate(io::Printer* printer) { printer->Print( "@java.lang.Override\n" "public final com.google.protobuf.UnknownFieldSet\n" - "getUnknownFields() {\n"); - if (PreserveUnknownFields(descriptor_)) { - printer->Print( - " return this.unknownFields;\n"); - } else { - printer->Print( - " return com.google.protobuf.UnknownFieldSet.getDefaultInstance();\n"); - } - printer->Print( + "getUnknownFields() {\n" + " return this.unknownFields;\n" "}\n"); if (context_->HasGeneratedMethods(descriptor_)) { @@ -623,14 +620,12 @@ GenerateMessageSerializationMethods(io::Printer* printer) { } } - if (PreserveUnknownFields(descriptor_)) { - if (descriptor_->options().message_set_wire_format()) { - printer->Print( + if (descriptor_->options().message_set_wire_format()) { + printer->Print( "unknownFields.writeAsMessageSetTo(output);\n"); - } else { - printer->Print( + } else { + printer->Print( "unknownFields.writeTo(output);\n"); - } } printer->Outdent(); @@ -658,14 +653,12 @@ GenerateMessageSerializationMethods(io::Printer* printer) { } } - if (PreserveUnknownFields(descriptor_)) { - if (descriptor_->options().message_set_wire_format()) { - printer->Print( + if (descriptor_->options().message_set_wire_format()) { + printer->Print( "size += unknownFields.getSerializedSizeAsMessageSet();\n"); - } else { - printer->Print( + } else { + printer->Print( "size += unknownFields.getSerializedSize();\n"); - } } printer->Outdent(); @@ -674,9 +667,6 @@ GenerateMessageSerializationMethods(io::Printer* printer) { " return size;\n" "}\n" "\n"); - - printer->Print( - "private static final long serialVersionUID = 0L;\n"); } void ImmutableMessageGenerator:: @@ -1078,13 +1068,11 @@ GenerateEqualsAndHashCode(io::Printer* printer) { printer->Print("}\n"); } - if (PreserveUnknownFields(descriptor_)) { - // Always consider unknown fields for equality. This will sometimes return - // false for non-canonical ordering when running in LITE_RUNTIME but it's - // the best we can do. - printer->Print( + // Always consider unknown fields for equality. This will sometimes return + // false for non-canonical ordering when running in LITE_RUNTIME but it's + // the best we can do. + printer->Print( "result = result && unknownFields.equals(other.unknownFields);\n"); - } if (descriptor_->extension_range_count() > 0) { printer->Print( "result = result &&\n" @@ -1226,11 +1214,9 @@ GenerateParsingConstructor(io::Printer* printer) { "bit_field_name", GetBitFieldName(i)); } - if (PreserveUnknownFields(descriptor_)) { - printer->Print( + printer->Print( "com.google.protobuf.UnknownFieldSet.Builder unknownFields =\n" " com.google.protobuf.UnknownFieldSet.newBuilder();\n"); - } printer->Print( "try {\n"); @@ -1247,28 +1233,19 @@ GenerateParsingConstructor(io::Printer* printer) { printer->Indent(); printer->Print( - "case 0:\n" // zero signals EOF / limit reached + "case 0:\n" // zero signals EOF / limit reached " done = true;\n" - " break;\n"); - - if (PreserveUnknownFields(descriptor_)) { - printer->Print( - "default: {\n" - " if (!parseUnknownField(input, unknownFields,\n" - " extensionRegistry, tag)) {\n" - " done = true;\n" // it's an endgroup tag - " }\n" - " break;\n" - "}\n"); - } else { - printer->Print( - "default: {\n" - " if (!input.skipField(tag)) {\n" - " done = true;\n" // it's an endgroup tag - " }\n" - " break;\n" - "}\n"); - } + " break;\n" + "default: {\n" + " if (!parseUnknownField$suffix$(\n" + " input, unknownFields, extensionRegistry, tag)) {\n" + " done = true;\n" // it's an endgroup tag + " }\n" + " break;\n" + "}\n", + "suffix", + descriptor_->file()->syntax() == FileDescriptor::SYNTAX_PROTO3 ? "Proto3" + : ""); for (int i = 0; i < descriptor_->field_count(); i++) { const FieldDescriptor* field = sorted_fields[i]; @@ -1328,10 +1305,8 @@ GenerateParsingConstructor(io::Printer* printer) { field_generators_.get(field).GenerateParsingDoneCode(printer); } - if (PreserveUnknownFields(descriptor_)) { - // Make unknown fields immutable. - printer->Print("this.unknownFields = unknownFields.build();\n"); - } + // Make unknown fields immutable. + printer->Print("this.unknownFields = unknownFields.build();\n"); // Make extensions immutable. printer->Print( diff --git a/src/google/protobuf/compiler/java/java_message_builder.cc b/src/google/protobuf/compiler/java/java_message_builder.cc index f5643abc..5368c1df 100644 --- a/src/google/protobuf/compiler/java/java_message_builder.cc +++ b/src/google/protobuf/compiler/java/java_message_builder.cc @@ -54,8 +54,9 @@ #include <google/protobuf/io/printer.h> #include <google/protobuf/descriptor.pb.h> #include <google/protobuf/wire_format.h> -#include <google/protobuf/stubs/strutil.h> #include <google/protobuf/stubs/substitute.h> +#include <google/protobuf/stubs/strutil.h> + namespace google { namespace protobuf { @@ -171,36 +172,25 @@ Generate(io::Printer* printer) { .GenerateBuilderMembers(printer); } - if (!PreserveUnknownFields(descriptor_)) { - printer->Print( - "public final Builder setUnknownFields(\n" - " final com.google.protobuf.UnknownFieldSet unknownFields) {\n" - " return this;\n" - "}\n" - "\n" - "public final Builder mergeUnknownFields(\n" - " final com.google.protobuf.UnknownFieldSet unknownFields) {\n" - " return this;\n" - "}\n" - "\n"); - } else { + bool is_proto3 = + descriptor_->file()->syntax() == FileDescriptor::SYNTAX_PROTO3; // Override methods declared in GeneratedMessage to return the concrete // generated type so callsites won't depend on GeneratedMessage. This // is needed to keep binary compatibility when we change generated code // to subclass a different GeneratedMessage class (e.g., in v3.0.0 release // we changed all generated code to subclass GeneratedMessageV3). - printer->Print( - "public final Builder setUnknownFields(\n" - " final com.google.protobuf.UnknownFieldSet unknownFields) {\n" - " return super.setUnknownFields(unknownFields);\n" - "}\n" - "\n" - "public final Builder mergeUnknownFields(\n" - " final com.google.protobuf.UnknownFieldSet unknownFields) {\n" - " return super.mergeUnknownFields(unknownFields);\n" - "}\n" - "\n"); - } + printer->Print( + "public final Builder setUnknownFields(\n" + " final com.google.protobuf.UnknownFieldSet unknownFields) {\n" + " return super.setUnknownFields$suffix$(unknownFields);\n" + "}\n" + "\n" + "public final Builder mergeUnknownFields(\n" + " final com.google.protobuf.UnknownFieldSet unknownFields) {\n" + " return super.mergeUnknownFields(unknownFields);\n" + "}\n" + "\n", + "suffix", is_proto3 ? "Proto3" : ""); printer->Print( "\n" @@ -594,10 +584,8 @@ GenerateCommonBuilderMethods(io::Printer* printer) { " this.mergeExtensionFields(other);\n"); } - if (PreserveUnknownFields(descriptor_)) { - printer->Print( - " this.mergeUnknownFields(other.unknownFields);\n"); - } + printer->Print( + " this.mergeUnknownFields(other.unknownFields);\n"); printer->Print( " onChanged();\n"); diff --git a/src/google/protobuf/compiler/java/java_message_field.cc b/src/google/protobuf/compiler/java/java_message_field.cc index ae84db1c..baa7f872 100644 --- a/src/google/protobuf/compiler/java/java_message_field.cc +++ b/src/google/protobuf/compiler/java/java_message_field.cc @@ -150,12 +150,9 @@ GenerateInterfaceMembers(io::Printer* printer) const { // interface so that builders can choose dynamically to either return a // message or a nested builder, so that asking for the interface doesn't // cause a message to ever be built. - if (SupportFieldPresence(descriptor_->file()) || - descriptor_->containing_oneof() == NULL) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$boolean has$capitalized_name$();\n"); - } + WriteFieldDocComment(printer, descriptor_); + printer->Print(variables_, + "$deprecation$boolean has$capitalized_name$();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$$type$ get$capitalized_name$();\n"); @@ -174,39 +171,45 @@ GenerateMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $get_has_field_bit_message$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public $type$OrBuilder " - "get$capitalized_name$OrBuilder() {\n" + "${$get$capitalized_name$OrBuilder$}$() {\n" " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } else { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $name$_ != null;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public $type$OrBuilder " - "get$capitalized_name$OrBuilder() {\n" + "${$get$capitalized_name$OrBuilder$}$() {\n" " return get$capitalized_name$();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } } @@ -232,6 +235,7 @@ void ImmutableMessageFieldGenerator::PrintNestedBuilderFunction( const char* nested_builder_case, const char* trailing_code) const { printer->Print(variables_, method_prototype); + printer->Annotate("{", "}", descriptor_); printer->Print(" {\n"); printer->Indent(); PrintNestedBuilderCondition(printer, regular_case, nested_builder_case); @@ -267,20 +271,22 @@ GenerateBuilderMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); if (support_field_presence) { printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $get_has_field_bit_builder$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } else { printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $name$Builder_ != null || $name$_ != null;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } // Field getField() WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public $type$ get$capitalized_name$()", + "$deprecation$public $type$ ${$get$capitalized_name$$}$()", "return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n", "return $name$Builder_.getMessage();\n", NULL); @@ -288,7 +294,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // Field.Builder setField(Field value) WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public Builder set$capitalized_name$($type$ value)", + "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value)", "if (value == null) {\n" " throw new NullPointerException();\n" @@ -304,7 +310,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // Field.Builder setField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public Builder set$capitalized_name$(\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" " $type$.Builder builderForValue)", "$name$_ = builderForValue.build();\n" @@ -318,7 +324,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // Field.Builder mergeField(Field value) WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public Builder merge$capitalized_name$($type$ value)", + "$deprecation$public Builder ${$merge$capitalized_name$$}$($type$ value)", support_field_presence ? "if ($get_has_field_bit_builder$ &&\n" @@ -346,7 +352,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // Field.Builder clearField() WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public Builder clear$capitalized_name$()", + "$deprecation$public Builder ${$clear$capitalized_name$$}$()", "$name$_ = null;\n" "$on_changed$\n", @@ -361,14 +367,17 @@ GenerateBuilderMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$.Builder get$capitalized_name$Builder() {\n" + "$deprecation$public $type$.Builder " + "${$get$capitalized_name$Builder$}$() {\n" " $set_has_field_bit_builder$\n" " $on_changed$\n" " return get$capitalized_name$FieldBuilder().getBuilder();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$OrBuilder get$capitalized_name$OrBuilder() {\n" + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$() {\n" " if ($name$Builder_ != null) {\n" " return $name$Builder_.getMessageOrBuilder();\n" " } else {\n" @@ -376,6 +385,7 @@ GenerateBuilderMembers(io::Printer* printer) const { " $type$.getDefaultInstance() : $name$_;\n" " }\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "private com.google.protobuf.SingleFieldBuilder$ver$<\n" @@ -530,30 +540,32 @@ ImmutableMessageOneofFieldGenerator:: void ImmutableMessageOneofFieldGenerator:: GenerateMembers(io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); - if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); - } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); + printer->Annotate("{", "}", descriptor_); + WriteFieldDocComment(printer, descriptor_); + printer->Print(variables_, + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " if ($has_oneof_case_message$) {\n" " return ($type$) $oneof_name$_;\n" " }\n" " return $type$.getDefaultInstance();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$OrBuilder get$capitalized_name$OrBuilder() {\n" + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$() {\n" " if ($has_oneof_case_message$) {\n" " return ($type$) $oneof_name$_;\n" " }\n" " return $type$.getDefaultInstance();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void ImmutableMessageOneofFieldGenerator:: @@ -571,19 +583,18 @@ GenerateBuilderMembers(io::Printer* printer) const { // The comments above the methods below are based on a hypothetical // field of type "Field" called "Field". - if (SupportFieldPresence(descriptor_->file())) { - // boolean hasField() - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); - } + // boolean hasField() + WriteFieldDocComment(printer, descriptor_); + printer->Print(variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); + printer->Annotate("{", "}", descriptor_); // Field getField() WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public $type$ get$capitalized_name$()", + "$deprecation$public $type$ ${$get$capitalized_name$$}$()", "if ($has_oneof_case_message$) {\n" " return ($type$) $oneof_name$_;\n" @@ -600,7 +611,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // Field.Builder setField(Field value) WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public Builder set$capitalized_name$($type$ value)", + "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value)", "if (value == null) {\n" " throw new NullPointerException();\n" @@ -616,7 +627,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // Field.Builder setField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public Builder set$capitalized_name$(\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" " $type$.Builder builderForValue)", "$oneof_name$_ = builderForValue.build();\n" @@ -630,7 +641,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // Field.Builder mergeField(Field value) WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public Builder merge$capitalized_name$($type$ value)", + "$deprecation$public Builder ${$merge$capitalized_name$$}$($type$ value)", "if ($has_oneof_case_message$ &&\n" " $oneof_name$_ != $type$.getDefaultInstance()) {\n" @@ -652,7 +663,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // Field.Builder clearField() WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public Builder clear$capitalized_name$()", + "$deprecation$public Builder ${$clear$capitalized_name$$}$()", "if ($has_oneof_case_message$) {\n" " $clear_oneof_case_message$;\n" @@ -670,12 +681,15 @@ GenerateBuilderMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$.Builder get$capitalized_name$Builder() {\n" + "$deprecation$public $type$.Builder " + "${$get$capitalized_name$Builder$}$() {\n" " return get$capitalized_name$FieldBuilder().getBuilder();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$OrBuilder get$capitalized_name$OrBuilder() {\n" + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$() {\n" " if (($has_oneof_case_message$) && ($name$Builder_ != null)) {\n" " return $name$Builder_.getMessageOrBuilder();\n" " } else {\n" @@ -685,11 +699,12 @@ GenerateBuilderMembers(io::Printer* printer) const { " return $type$.getDefaultInstance();\n" " }\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "private com.google.protobuf.SingleFieldBuilder$ver$<\n" " $type$, $type$.Builder, $type$OrBuilder> \n" - " get$capitalized_name$FieldBuilder() {\n" + " ${$get$capitalized_name$FieldBuilder$}$() {\n" " if ($name$Builder_ == null) {\n" " if (!($has_oneof_case_message$)) {\n" " $oneof_name$_ = $type$.getDefaultInstance();\n" @@ -705,6 +720,7 @@ GenerateBuilderMembers(io::Printer* printer) const { " $on_changed$;\n" " return $name$Builder_;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void ImmutableMessageOneofFieldGenerator:: @@ -833,31 +849,38 @@ GenerateMembers(io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.util.List<$type$> get$capitalized_name$List() {\n" + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" " return $name$_;\n" // note: unmodifiable list "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public java.util.List<? extends $type$OrBuilder> \n" - " get$capitalized_name$OrBuilderList() {\n" + " ${$get$capitalized_name$OrBuilderList$}$() {\n" " return $name$_;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Count() {\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" " return $name$_.size();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$(int index) {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" " return $name$_.get(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$OrBuilder get$capitalized_name$OrBuilder(\n" + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$(\n" " int index) {\n" " return $name$_.get(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } @@ -883,6 +906,7 @@ void RepeatedImmutableMessageFieldGenerator::PrintNestedBuilderFunction( const char* nested_builder_case, const char* trailing_code) const { printer->Print(variables_, method_prototype); + printer->Annotate("{", "}", descriptor_); printer->Print(" {\n"); printer->Indent(); PrintNestedBuilderCondition(printer, regular_case, nested_builder_case); @@ -934,7 +958,8 @@ GenerateBuilderMembers(io::Printer* printer) const { // List<Field> getRepeatedFieldList() WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public java.util.List<$type$> get$capitalized_name$List()", + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$()", "return java.util.Collections.unmodifiableList($name$_);\n", "return $name$Builder_.getMessageList();\n", @@ -944,7 +969,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // int getRepeatedFieldCount() WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public int get$capitalized_name$Count()", + "$deprecation$public int ${$get$capitalized_name$Count$}$()", "return $name$_.size();\n", "return $name$Builder_.getCount();\n", @@ -954,7 +979,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // Field getRepeatedField(int index) WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public $type$ get$capitalized_name$(int index)", + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index)", "return $name$_.get(index);\n", @@ -965,7 +990,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // Builder setRepeatedField(int index, Field value) WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public Builder set$capitalized_name$(\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" " int index, $type$ value)", "if (value == null) {\n" " throw new NullPointerException();\n" @@ -979,7 +1004,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // Builder setRepeatedField(int index, Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public Builder set$capitalized_name$(\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" " int index, $type$.Builder builderForValue)", "ensure$capitalized_name$IsMutable();\n" @@ -993,7 +1018,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // Builder addRepeatedField(Field value) WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public Builder add$capitalized_name$($type$ value)", + "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value)", "if (value == null) {\n" " throw new NullPointerException();\n" @@ -1010,7 +1035,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // Builder addRepeatedField(int index, Field value) WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public Builder add$capitalized_name$(\n" + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" " int index, $type$ value)", "if (value == null) {\n" @@ -1027,7 +1052,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // Builder addRepeatedField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public Builder add$capitalized_name$(\n" + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" " $type$.Builder builderForValue)", "ensure$capitalized_name$IsMutable();\n" @@ -1041,7 +1066,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // Builder addRepeatedField(int index, Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public Builder add$capitalized_name$(\n" + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" " int index, $type$.Builder builderForValue)", "ensure$capitalized_name$IsMutable();\n" @@ -1055,7 +1080,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // Builder addAllRepeatedField(Iterable<Field> values) WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public Builder addAll$capitalized_name$(\n" + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" " java.lang.Iterable<? extends $type$> values)", "ensure$capitalized_name$IsMutable();\n" @@ -1070,7 +1095,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // Builder clearAllRepeatedField() WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public Builder clear$capitalized_name$()", + "$deprecation$public Builder ${$clear$capitalized_name$$}$()", "$name$_ = java.util.Collections.emptyList();\n" "$clear_mutable_bit_builder$;\n" @@ -1083,7 +1108,7 @@ GenerateBuilderMembers(io::Printer* printer) const { // Builder removeRepeatedField(int index) WriteFieldDocComment(printer, descriptor_); PrintNestedBuilderFunction(printer, - "$deprecation$public Builder remove$capitalized_name$(int index)", + "$deprecation$public Builder ${$remove$capitalized_name$$}$(int index)", "ensure$capitalized_name$IsMutable();\n" "$name$_.remove(index);\n" @@ -1095,14 +1120,16 @@ GenerateBuilderMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$.Builder get$capitalized_name$Builder(\n" + "$deprecation$public $type$.Builder ${$get$capitalized_name$Builder$}$(\n" " int index) {\n" " return get$capitalized_name$FieldBuilder().getBuilder(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$OrBuilder get$capitalized_name$OrBuilder(\n" + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$(\n" " int index) {\n" " if ($name$Builder_ == null) {\n" " return $name$_.get(index);" @@ -1110,35 +1137,40 @@ GenerateBuilderMembers(io::Printer* printer) const { " return $name$Builder_.getMessageOrBuilder(index);\n" " }\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public java.util.List<? extends $type$OrBuilder> \n" - " get$capitalized_name$OrBuilderList() {\n" + " ${$get$capitalized_name$OrBuilderList$}$() {\n" " if ($name$Builder_ != null) {\n" " return $name$Builder_.getMessageOrBuilderList();\n" " } else {\n" " return java.util.Collections.unmodifiableList($name$_);\n" " }\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$.Builder add$capitalized_name$Builder() {\n" + "$deprecation$public $type$.Builder " + "${$add$capitalized_name$Builder$}$() {\n" " return get$capitalized_name$FieldBuilder().addBuilder(\n" " $type$.getDefaultInstance());\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$.Builder add$capitalized_name$Builder(\n" + "$deprecation$public $type$.Builder ${$add$capitalized_name$Builder$}$(\n" " int index) {\n" " return get$capitalized_name$FieldBuilder().addBuilder(\n" " index, $type$.getDefaultInstance());\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public java.util.List<$type$.Builder> \n" - " get$capitalized_name$BuilderList() {\n" + " ${$get$capitalized_name$BuilderList$}$() {\n" " return get$capitalized_name$FieldBuilder().getBuilderList();\n" "}\n" "private com.google.protobuf.RepeatedFieldBuilder$ver$<\n" @@ -1155,6 +1187,7 @@ GenerateBuilderMembers(io::Printer* printer) const { " }\n" " return $name$Builder_;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void RepeatedImmutableMessageFieldGenerator:: diff --git a/src/google/protobuf/compiler/java/java_message_field_lite.cc b/src/google/protobuf/compiler/java/java_message_field_lite.cc index c71e9101..df3e80d4 100644 --- a/src/google/protobuf/compiler/java/java_message_field_lite.cc +++ b/src/google/protobuf/compiler/java/java_message_field_lite.cc @@ -129,16 +129,9 @@ int ImmutableMessageFieldLiteGenerator::GetNumBitsForBuilder() const { void ImmutableMessageFieldLiteGenerator:: GenerateInterfaceMembers(io::Printer* printer) const { - // TODO(jonp): In the future, consider having a method specific to the - // interface so that builders can choose dynamically to either return a - // message or a nested builder, so that asking for the interface doesn't - // cause a message to ever be built. - if (SupportFieldPresence(descriptor_->file()) || - descriptor_->containing_oneof() == NULL) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$boolean has$capitalized_name$();\n"); - } + WriteFieldDocComment(printer, descriptor_); + printer->Print(variables_, + "$deprecation$boolean has$capitalized_name$();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$$type$ get$capitalized_name$();\n"); @@ -154,25 +147,29 @@ GenerateMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $get_has_field_bit_message$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } else { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $name$_ != null;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " return $name$_ == null ? $type$.getDefaultInstance() : $name$_;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } // Field.Builder setField(Field value) @@ -226,53 +223,60 @@ GenerateBuilderMembers(io::Printer* printer) const { // boolean hasField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return instance.has$capitalized_name$();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Field getField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " return instance.get$capitalized_name$();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Field.Builder setField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$($type$ value) {\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$(value);\n" " return this;\n" " }\n"); + printer->Annotate("{", "}", descriptor_); // Field.Builder setField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$(\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" " $type$.Builder builderForValue) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$(builderForValue);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Field.Builder mergeField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder merge$capitalized_name$($type$ value) {\n" + "$deprecation$public Builder " + "${$merge$capitalized_name$$}$($type$ value) {\n" " copyOnWrite();\n" " instance.merge$capitalized_name$(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Field.Builder clearField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {" " copyOnWrite();\n" " instance.clear$capitalized_name$();\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void ImmutableMessageFieldLiteGenerator:: @@ -390,21 +394,21 @@ ImmutableMessageOneofFieldLiteGenerator:: void ImmutableMessageOneofFieldLiteGenerator:: GenerateMembers(io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); - if (SupportFieldPresence(descriptor_->file())) { - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" - " return $has_oneof_case_message$;\n" - "}\n"); - } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return $has_oneof_case_message$;\n" + "}\n"); + printer->Annotate("{", "}", descriptor_); + WriteFieldDocComment(printer, descriptor_); + printer->Print(variables_, + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " if ($has_oneof_case_message$) {\n" " return ($type$) $oneof_name$_;\n" " }\n" " return $type$.getDefaultInstance();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Field.Builder setField(Field value) WriteFieldDocComment(printer, descriptor_); @@ -457,58 +461,63 @@ GenerateBuilderMembers(io::Printer* printer) const { // The comments above the methods below are based on a hypothetical // field of type "Field" called "Field". - if (SupportFieldPresence(descriptor_->file())) { - // boolean hasField() - WriteFieldDocComment(printer, descriptor_); - printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" - " return instance.has$capitalized_name$();\n" - "}\n"); - } + // boolean hasField() + WriteFieldDocComment(printer, descriptor_); + printer->Print(variables_, + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" + " return instance.has$capitalized_name$();\n" + "}\n"); + printer->Annotate("{", "}", descriptor_); // Field getField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " return instance.get$capitalized_name$();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Field.Builder setField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$($type$ value) {\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Field.Builder setField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$(\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" " $type$.Builder builderForValue) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$(builderForValue);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Field.Builder mergeField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder merge$capitalized_name$($type$ value) {\n" + "$deprecation$public Builder " + "${$merge$capitalized_name$$}$($type$ value) {\n" " copyOnWrite();\n" " instance.merge$capitalized_name$(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Field.Builder clearField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " copyOnWrite();\n" " instance.clear$capitalized_name$();\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void ImmutableMessageOneofFieldLiteGenerator:: @@ -615,31 +624,38 @@ GenerateMembers(io::Printer* printer) const { PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.util.List<$type$> get$capitalized_name$List() {\n" + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" " return $name$_;\n" // note: unmodifiable list "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public java.util.List<? extends $type$OrBuilder> \n" - " get$capitalized_name$OrBuilderList() {\n" + " ${$get$capitalized_name$OrBuilderList$}$() {\n" " return $name$_;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Count() {\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" " return $name$_.size();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$(int index) {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" " return $name$_.get(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$OrBuilder get$capitalized_name$OrBuilder(\n" + "$deprecation$public $type$OrBuilder " + "${$get$capitalized_name$OrBuilder$}$(\n" " int index) {\n" " return $name$_.get(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); printer->Print(variables_, "private void ensure$capitalized_name$IsMutable() {\n" @@ -745,110 +761,123 @@ GenerateBuilderMembers(io::Printer* printer) const { // List<Field> getRepeatedFieldList() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.util.List<$type$> get$capitalized_name$List() {\n" + "$deprecation$public java.util.List<$type$> " + "${$get$capitalized_name$List$}$() {\n" " return java.util.Collections.unmodifiableList(\n" " instance.get$capitalized_name$List());\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // int getRepeatedFieldCount() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Count() {\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" " return instance.get$capitalized_name$Count();\n" "}"); + printer->Annotate("{", "}", descriptor_); // Field getRepeatedField(int index) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$(int index) {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" " return instance.get$capitalized_name$(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Builder setRepeatedField(int index, Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$(\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" " int index, $type$ value) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$(index, value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Builder setRepeatedField(int index, Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$(\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" " int index, $type$.Builder builderForValue) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$(index, builderForValue);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Builder addRepeatedField(Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder add$capitalized_name$($type$ value) {\n" + "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value) {\n" " copyOnWrite();\n" " instance.add$capitalized_name$(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Builder addRepeatedField(int index, Field value) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder add$capitalized_name$(\n" + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" " int index, $type$ value) {\n" " copyOnWrite();\n" " instance.add$capitalized_name$(index, value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Builder addRepeatedField(Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder add$capitalized_name$(\n" + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" " $type$.Builder builderForValue) {\n" " copyOnWrite();\n" " instance.add$capitalized_name$(builderForValue);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Builder addRepeatedField(int index, Field.Builder builderForValue) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder add$capitalized_name$(\n" + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" " int index, $type$.Builder builderForValue) {\n" " copyOnWrite();\n" " instance.add$capitalized_name$(index, builderForValue);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Builder addAllRepeatedField(Iterable<Field> values) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder addAll$capitalized_name$(\n" + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" " java.lang.Iterable<? extends $type$> values) {\n" " copyOnWrite();\n" " instance.addAll$capitalized_name$(values);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Builder clearAllRepeatedField() WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " copyOnWrite();\n" " instance.clear$capitalized_name$();\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); // Builder removeRepeatedField(int index) WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder remove$capitalized_name$(int index) {\n" + "$deprecation$public Builder ${$remove$capitalized_name$$}$(int index) {\n" " copyOnWrite();\n" " instance.remove$capitalized_name$(index);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void RepeatedImmutableMessageFieldLiteGenerator:: diff --git a/src/google/protobuf/compiler/java/java_message_lite.cc b/src/google/protobuf/compiler/java/java_message_lite.cc index 5007ecee..26f16439 100644 --- a/src/google/protobuf/compiler/java/java_message_lite.cc +++ b/src/google/protobuf/compiler/java/java_message_lite.cc @@ -56,8 +56,9 @@ #include <google/protobuf/io/printer.h> #include <google/protobuf/descriptor.pb.h> #include <google/protobuf/wire_format.h> -#include <google/protobuf/stubs/strutil.h> #include <google/protobuf/stubs/substitute.h> +#include <google/protobuf/stubs/strutil.h> + namespace google { namespace protobuf { @@ -124,7 +125,7 @@ void ImmutableMessageLiteGenerator::GenerateInterface(io::Printer* printer) { /* immutable = */ true, "OrBuilder"); if (descriptor_->extension_range_count() > 0) { printer->Print( - "$deprecation$public interface $classname$OrBuilder$idend$ extends \n" + "$deprecation$public interface ${$$classname$OrBuilder$}$ extends \n" " $extra_interfaces$\n" " com.google.protobuf.GeneratedMessageLite.\n" " ExtendableMessageOrBuilder<\n" @@ -133,19 +134,19 @@ void ImmutableMessageLiteGenerator::GenerateInterface(io::Printer* printer) { "@java.lang.Deprecated " : "", "extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_), "classname", descriptor_->name(), - "idend", ""); + "{", "", "}", ""); } else { printer->Print( - "$deprecation$public interface $classname$OrBuilder$idend$ extends\n" + "$deprecation$public interface ${$$classname$OrBuilder$}$ extends\n" " $extra_interfaces$\n" " com.google.protobuf.MessageLiteOrBuilder {\n", "deprecation", descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "", "extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_), "classname", descriptor_->name(), - "idend", ""); + "{", "", "}", ""); } - printer->Annotate("classname", "idend", descriptor_); + printer->Annotate("{", "}", descriptor_); printer->Indent(); for (int i = 0; i < descriptor_->field_count(); i++) { @@ -345,15 +346,15 @@ void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) { } printer->Print( - "@java.lang.SuppressWarnings({\"unchecked\", \"fallthrough\"})\n" - "protected final Object dynamicMethod(\n" - " com.google.protobuf.GeneratedMessageLite.MethodToInvoke method,\n" - " Object arg0, Object arg1) {\n" - " switch (method) {\n" - " case NEW_MUTABLE_INSTANCE: {\n" - " return new $classname$();\n" - " }\n", - "classname", name_resolver_->GetImmutableClassName(descriptor_)); + "@java.lang.SuppressWarnings({\"unchecked\", \"fallthrough\"})\n" + "protected final java.lang.Object dynamicMethod(\n" + " com.google.protobuf.GeneratedMessageLite.MethodToInvoke method,\n" + " java.lang.Object arg0, java.lang.Object arg1) {\n" + " switch (method) {\n" + " case NEW_MUTABLE_INSTANCE: {\n" + " return new $classname$();\n" + " }\n", + "classname", name_resolver_->GetImmutableClassName(descriptor_)); printer->Indent(); printer->Indent(); @@ -530,14 +531,12 @@ GenerateMessageSerializationMethods(io::Printer* printer) { } } - if (PreserveUnknownFields(descriptor_)) { - if (descriptor_->options().message_set_wire_format()) { - printer->Print( - "unknownFields.writeAsMessageSetTo(output);\n"); - } else { - printer->Print( - "unknownFields.writeTo(output);\n"); - } + if (descriptor_->options().message_set_wire_format()) { + printer->Print( + "unknownFields.writeAsMessageSetTo(output);\n"); + } else { + printer->Print( + "unknownFields.writeTo(output);\n"); } printer->Outdent(); @@ -565,14 +564,12 @@ GenerateMessageSerializationMethods(io::Printer* printer) { } } - if (PreserveUnknownFields(descriptor_)) { - if (descriptor_->options().message_set_wire_format()) { - printer->Print( - "size += unknownFields.getSerializedSizeAsMessageSet();\n"); - } else { - printer->Print( - "size += unknownFields.getSerializedSize();\n"); - } + if (descriptor_->options().message_set_wire_format()) { + printer->Print( + "size += unknownFields.getSerializedSizeAsMessageSet();\n"); + } else { + printer->Print( + "size += unknownFields.getSerializedSize();\n"); } printer->Outdent(); @@ -968,41 +965,31 @@ void ImmutableMessageLiteGenerator::GenerateDynamicMethodMergeFromStream( " done = true;\n" " break;\n"); - if (PreserveUnknownFields(descriptor_)) { - if (descriptor_->extension_range_count() > 0) { - if (descriptor_->options().message_set_wire_format()) { - printer->Print( - "default: {\n" - " if (!parseUnknownFieldAsMessageSet(\n" - " getDefaultInstanceForType(), input, extensionRegistry,\n" - " tag)) {\n" - " done = true;\n" // it's an endgroup tag - " }\n" - " break;\n" - "}\n"); - } else { - printer->Print( - "default: {\n" - " if (!parseUnknownField(getDefaultInstanceForType(),\n" - " input, extensionRegistry, tag)) {\n" - " done = true;\n" // it's an endgroup tag - " }\n" - " break;\n" - "}\n"); - } + if (descriptor_->extension_range_count() > 0) { + if (descriptor_->options().message_set_wire_format()) { + printer->Print( + "default: {\n" + " if (!parseUnknownFieldAsMessageSet(\n" + " getDefaultInstanceForType(), input, extensionRegistry,\n" + " tag)) {\n" + " done = true;\n" // it's an endgroup tag + " }\n" + " break;\n" + "}\n"); } else { printer->Print( - "default: {\n" - " if (!parseUnknownField(tag, input)) {\n" - " done = true;\n" // it's an endgroup tag - " }\n" - " break;\n" - "}\n"); + "default: {\n" + " if (!parseUnknownField(getDefaultInstanceForType(),\n" + " input, extensionRegistry, tag)) {\n" + " done = true;\n" // it's an endgroup tag + " }\n" + " break;\n" + "}\n"); } } else { printer->Print( "default: {\n" - " if (!input.skipField(tag)) {\n" + " if (!parseUnknownField(tag, input)) {\n" " done = true;\n" // it's an endgroup tag " }\n" " break;\n" diff --git a/src/google/protobuf/compiler/java/java_name_resolver.cc b/src/google/protobuf/compiler/java/java_name_resolver.cc index bffe4f16..1673b4ee 100644 --- a/src/google/protobuf/compiler/java/java_name_resolver.cc +++ b/src/google/protobuf/compiler/java/java_name_resolver.cc @@ -33,6 +33,7 @@ #include <map> #include <string> + #include <google/protobuf/compiler/java/java_helpers.h> #include <google/protobuf/stubs/substitute.h> diff --git a/src/google/protobuf/compiler/java/java_primitive_field.cc b/src/google/protobuf/compiler/java/java_primitive_field.cc index 840252e7..074a6be8 100644 --- a/src/google/protobuf/compiler/java/java_primitive_field.cc +++ b/src/google/protobuf/compiler/java/java_primitive_field.cc @@ -190,16 +190,18 @@ GenerateMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $get_has_field_bit_message$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " return $name$_;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void ImmutablePrimitiveFieldGenerator:: @@ -210,31 +212,35 @@ GenerateBuilderMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $get_has_field_bit_builder$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " return $name$_;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$($type$ value) {\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" "$null_check$" " $set_has_field_bit_builder$\n" " $name$_ = value;\n" " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " $clear_has_field_bit_builder$\n"); + printer->Annotate("{", "}", descriptor_); JavaType type = GetJavaType(descriptor_); if (type == JAVATYPE_STRING || type == JAVATYPE_BYTES) { // The default value is not a simple literal so we want to avoid executing @@ -441,19 +447,21 @@ GenerateMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $has_oneof_case_message$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " if ($has_oneof_case_message$) {\n" " return ($boxed_type$) $oneof_name$_;\n" " }\n" " return $default$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } @@ -462,33 +470,36 @@ GenerateBuilderMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $has_oneof_case_message$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " if ($has_oneof_case_message$) {\n" " return ($boxed_type$) $oneof_name$_;\n" " }\n" " return $default$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$($type$ value) {\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" "$null_check$" " $set_oneof_case_message$;\n" " $oneof_name$_ = value;\n" " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " if ($has_oneof_case_message$) {\n" " $clear_oneof_case_message$;\n" " $oneof_name$_ = null;\n" @@ -496,6 +507,7 @@ GenerateBuilderMembers(io::Printer* printer) const { " }\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void ImmutablePrimitiveOneofFieldGenerator:: @@ -604,19 +616,22 @@ GenerateMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public java.util.List<$boxed_type$>\n" - " get$capitalized_name$List() {\n" + " ${$get$capitalized_name$List$}$() {\n" " return $name$_;\n" // note: unmodifiable list "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Count() {\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" " return $name$_.size();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$(int index) {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" " return $name$_.get(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); if (descriptor_->is_packed() && context_->HasGeneratedMethods(descriptor_->containing_type())) { @@ -654,22 +669,25 @@ GenerateBuilderMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public java.util.List<$boxed_type$>\n" - " get$capitalized_name$List() {\n" + " ${$get$capitalized_name$List$}$() {\n" " return java.util.Collections.unmodifiableList($name$_);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Count() {\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" " return $name$_.size();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$(int index) {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" " return $name$_.get(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$(\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" " int index, $type$ value) {\n" "$null_check$" " ensure$capitalized_name$IsMutable();\n" @@ -677,18 +695,20 @@ GenerateBuilderMembers(io::Printer* printer) const { " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder add$capitalized_name$($type$ value) {\n" + "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value) {\n" "$null_check$" " ensure$capitalized_name$IsMutable();\n" " $name$_.add(value);\n" " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder addAll$capitalized_name$(\n" + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" " java.lang.Iterable<? extends $boxed_type$> values) {\n" " ensure$capitalized_name$IsMutable();\n" " com.google.protobuf.AbstractMessageLite.Builder.addAll(\n" @@ -696,14 +716,16 @@ GenerateBuilderMembers(io::Printer* printer) const { " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " $name$_ = $empty_list$;\n" " $clear_mutable_bit_builder$;\n" " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void RepeatedImmutablePrimitiveFieldGenerator:: diff --git a/src/google/protobuf/compiler/java/java_primitive_field_lite.cc b/src/google/protobuf/compiler/java/java_primitive_field_lite.cc index 0e8e492f..f9293171 100644 --- a/src/google/protobuf/compiler/java/java_primitive_field_lite.cc +++ b/src/google/protobuf/compiler/java/java_primitive_field_lite.cc @@ -222,16 +222,18 @@ GenerateMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $get_has_field_bit_message$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " return $name$_;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, @@ -264,32 +266,36 @@ GenerateBuilderMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return instance.has$capitalized_name$();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " return instance.get$capitalized_name$();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$($type$ value) {\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " copyOnWrite();\n" " instance.clear$capitalized_name$();\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void ImmutablePrimitiveFieldLiteGenerator:: @@ -482,19 +488,21 @@ GenerateMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $has_oneof_case_message$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " if ($has_oneof_case_message$) {\n" " return ($boxed_type$) $oneof_name$_;\n" " }\n" " return $default$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, @@ -520,32 +528,36 @@ GenerateBuilderMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return instance.has$capitalized_name$();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$() {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$() {\n" " return instance.get$capitalized_name$();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$($type$ value) {\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$($type$ value) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " copyOnWrite();\n" " instance.clear$capitalized_name$();\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void ImmutablePrimitiveOneofFieldLiteGenerator:: @@ -634,21 +646,24 @@ GenerateMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public java.util.List<$boxed_type$>\n" - " get$capitalized_name$List() {\n" + " ${$get$capitalized_name$List$}$() {\n" " return $name$_;\n" // note: unmodifiable list "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Count() {\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" " return $name$_.size();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$(int index) {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" " return $repeated_get$(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); - if (descriptor_->options().packed() && + if (descriptor_->is_packed() && context_->HasGeneratedMethods(descriptor_->containing_type())) { printer->Print(variables_, "private int $name$MemoizedSerializedSize = -1;\n"); @@ -697,50 +712,57 @@ GenerateBuilderMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public java.util.List<$boxed_type$>\n" - " get$capitalized_name$List() {\n" + " ${$get$capitalized_name$List$}$() {\n" " return java.util.Collections.unmodifiableList(\n" " instance.get$capitalized_name$List());\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Count() {\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" " return instance.get$capitalized_name$Count();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public $type$ get$capitalized_name$(int index) {\n" + "$deprecation$public $type$ ${$get$capitalized_name$$}$(int index) {\n" " return instance.get$capitalized_name$(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$(\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" " int index, $type$ value) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$(index, value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder add$capitalized_name$($type$ value) {\n" + "$deprecation$public Builder ${$add$capitalized_name$$}$($type$ value) {\n" " copyOnWrite();\n" " instance.add$capitalized_name$(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder addAll$capitalized_name$(\n" + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" " java.lang.Iterable<? extends $boxed_type$> values) {\n" " copyOnWrite();\n" " instance.addAll$capitalized_name$(values);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " copyOnWrite();\n" " instance.clear$capitalized_name$();\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void RepeatedImmutablePrimitiveFieldLiteGenerator:: @@ -829,7 +851,7 @@ GenerateParsingDoneCode(io::Printer* printer) const { void RepeatedImmutablePrimitiveFieldLiteGenerator:: GenerateSerializationCode(io::Printer* printer) const { - if (descriptor_->options().packed()) { + if (descriptor_->is_packed()) { // We invoke getSerializedSize in writeTo for messages that have packed // fields in ImmutableMessageGenerator::GenerateMessageSerializationMethods. // That makes it safe to rely on the memoized size here. @@ -870,7 +892,7 @@ GenerateSerializedSizeCode(io::Printer* printer) const { printer->Print( "size += dataSize;\n"); - if (descriptor_->options().packed()) { + if (descriptor_->is_packed()) { printer->Print(variables_, "if (!get$capitalized_name$List().isEmpty()) {\n" " size += $tag_size$;\n" @@ -883,7 +905,7 @@ GenerateSerializedSizeCode(io::Printer* printer) const { } // cache the data size for packed fields. - if (descriptor_->options().packed()) { + if (descriptor_->is_packed()) { printer->Print(variables_, "$name$MemoizedSerializedSize = dataSize;\n"); } diff --git a/src/google/protobuf/compiler/java/java_string_field.cc b/src/google/protobuf/compiler/java/java_string_field.cc index 5c2900ce..2b6e9381 100644 --- a/src/google/protobuf/compiler/java/java_string_field.cc +++ b/src/google/protobuf/compiler/java/java_string_field.cc @@ -217,14 +217,15 @@ GenerateMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $get_has_field_bit_message$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.lang.String get$capitalized_name$() {\n" + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" " java.lang.Object ref = $name$_;\n" " if (ref instanceof java.lang.String) {\n" " return (java.lang.String) ref;\n" @@ -232,6 +233,7 @@ GenerateMembers(io::Printer* printer) const { " com.google.protobuf.ByteString bs = \n" " (com.google.protobuf.ByteString) ref;\n" " java.lang.String s = bs.toStringUtf8();\n"); + printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { printer->Print(variables_, " $name$_ = s;\n"); @@ -248,7 +250,7 @@ GenerateMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes() {\n" + " ${$get$capitalized_name$Bytes$}$() {\n" " java.lang.Object ref = $name$_;\n" " if (ref instanceof java.lang.String) {\n" " com.google.protobuf.ByteString b = \n" @@ -260,6 +262,7 @@ GenerateMembers(io::Printer* printer) const { " return (com.google.protobuf.ByteString) ref;\n" " }\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void ImmutableStringFieldGenerator:: @@ -269,19 +272,21 @@ GenerateBuilderMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $get_has_field_bit_builder$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.lang.String get$capitalized_name$() {\n" + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" " java.lang.Object ref = $name$_;\n" " if (!(ref instanceof java.lang.String)) {\n" " com.google.protobuf.ByteString bs =\n" " (com.google.protobuf.ByteString) ref;\n" " java.lang.String s = bs.toStringUtf8();\n"); + printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { printer->Print(variables_, " $name$_ = s;\n"); @@ -301,7 +306,7 @@ GenerateBuilderMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes() {\n" + " ${$get$capitalized_name$Bytes$}$() {\n" " java.lang.Object ref = $name$_;\n" " if (ref instanceof String) {\n" " com.google.protobuf.ByteString b = \n" @@ -313,10 +318,11 @@ GenerateBuilderMembers(io::Printer* printer) const { " return (com.google.protobuf.ByteString) ref;\n" " }\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$(\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" " java.lang.String value) {\n" "$null_check$" " $set_has_field_bit_builder$\n" @@ -324,10 +330,12 @@ GenerateBuilderMembers(io::Printer* printer) const { " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " $clear_has_field_bit_builder$\n"); + printer->Annotate("{", "}", descriptor_); // The default value is not a simple literal so we want to avoid executing // it multiple times. Instead, get the default out of the default instance. printer->Print(variables_, @@ -339,9 +347,10 @@ GenerateBuilderMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$Bytes(\n" + "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" " com.google.protobuf.ByteString value) {\n" "$null_check$"); + printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { printer->Print(variables_, " checkByteStringIsUtf8(value);\n"); @@ -482,14 +491,15 @@ GenerateMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $has_oneof_case_message$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.lang.String get$capitalized_name$() {\n" + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" " java.lang.Object ref $default_init$;\n" " if ($has_oneof_case_message$) {\n" " ref = $oneof_name$_;\n" @@ -500,6 +510,7 @@ GenerateMembers(io::Printer* printer) const { " com.google.protobuf.ByteString bs = \n" " (com.google.protobuf.ByteString) ref;\n" " java.lang.String s = bs.toStringUtf8();\n"); + printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { printer->Print(variables_, " if ($has_oneof_case_message$) {\n" @@ -519,7 +530,7 @@ GenerateMembers(io::Printer* printer) const { printer->Print(variables_, "$deprecation$public com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes() {\n" + " ${$get$capitalized_name$Bytes$}$() {\n" " java.lang.Object ref $default_init$;\n" " if ($has_oneof_case_message$) {\n" " ref = $oneof_name$_;\n" @@ -536,6 +547,7 @@ GenerateMembers(io::Printer* printer) const { " return (com.google.protobuf.ByteString) ref;\n" " }\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void ImmutableStringOneofFieldGenerator:: @@ -543,14 +555,15 @@ GenerateBuilderMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $has_oneof_case_message$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.lang.String get$capitalized_name$() {\n" + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" " java.lang.Object ref $default_init$;\n" " if ($has_oneof_case_message$) {\n" " ref = $oneof_name$_;\n" @@ -560,6 +573,7 @@ GenerateBuilderMembers(io::Printer* printer) const { " (com.google.protobuf.ByteString) ref;\n" " java.lang.String s = bs.toStringUtf8();\n" " if ($has_oneof_case_message$) {\n"); + printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { printer->Print(variables_, " $oneof_name$_ = s;\n"); @@ -580,7 +594,7 @@ GenerateBuilderMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes() {\n" + " ${$get$capitalized_name$Bytes$}$() {\n" " java.lang.Object ref $default_init$;\n" " if ($has_oneof_case_message$) {\n" " ref = $oneof_name$_;\n" @@ -597,10 +611,11 @@ GenerateBuilderMembers(io::Printer* printer) const { " return (com.google.protobuf.ByteString) ref;\n" " }\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$(\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" " java.lang.String value) {\n" "$null_check$" " $set_oneof_case_message$;\n" @@ -608,9 +623,10 @@ GenerateBuilderMembers(io::Printer* printer) const { " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " if ($has_oneof_case_message$) {\n" " $clear_oneof_case_message$;\n" " $oneof_name$_ = null;\n" @@ -618,12 +634,14 @@ GenerateBuilderMembers(io::Printer* printer) const { " }\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$Bytes(\n" + "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" " com.google.protobuf.ByteString value) {\n" "$null_check$"); + printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { printer->Print(variables_, " checkByteStringIsUtf8(value);\n"); @@ -744,25 +762,30 @@ GenerateMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public com.google.protobuf.ProtocolStringList\n" - " get$capitalized_name$List() {\n" + " ${$get$capitalized_name$List$}$() {\n" " return $name$_;\n" // note: unmodifiable list "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Count() {\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" " return $name$_.size();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.lang.String get$capitalized_name$(int index) {\n" + "$deprecation$public java.lang.String " + "${$get$capitalized_name$$}$(int index) {\n" " return $name$_.get(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes(int index) {\n" + " ${$get$capitalized_name$Bytes$}$(int index) {\n" " return $name$_.getByteString(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void RepeatedImmutableStringFieldGenerator:: @@ -794,28 +817,33 @@ GenerateBuilderMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public com.google.protobuf.ProtocolStringList\n" - " get$capitalized_name$List() {\n" + " ${$get$capitalized_name$List$}$() {\n" " return $name$_.getUnmodifiableView();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Count() {\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" " return $name$_.size();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.lang.String get$capitalized_name$(int index) {\n" + "$deprecation$public java.lang.String " + "${$get$capitalized_name$$}$(int index) {\n" " return $name$_.get(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes(int index) {\n" + " ${$get$capitalized_name$Bytes$}$(int index) {\n" " return $name$_.getByteString(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$(\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" " int index, java.lang.String value) {\n" "$null_check$" " ensure$capitalized_name$IsMutable();\n" @@ -823,9 +851,10 @@ GenerateBuilderMembers(io::Printer* printer) const { " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder add$capitalized_name$(\n" + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" " java.lang.String value) {\n" "$null_check$" " ensure$capitalized_name$IsMutable();\n" @@ -833,9 +862,10 @@ GenerateBuilderMembers(io::Printer* printer) const { " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder addAll$capitalized_name$(\n" + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" " java.lang.Iterable<java.lang.String> values) {\n" " ensure$capitalized_name$IsMutable();\n" " com.google.protobuf.AbstractMessageLite.Builder.addAll(\n" @@ -843,20 +873,23 @@ GenerateBuilderMembers(io::Printer* printer) const { " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " $name$_ = $empty_list$;\n" " $clear_mutable_bit_builder$;\n" " $on_changed$\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder add$capitalized_name$Bytes(\n" + "$deprecation$public Builder ${$add$capitalized_name$Bytes$}$(\n" " com.google.protobuf.ByteString value) {\n" "$null_check$"); + printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { printer->Print(variables_, " checkByteStringIsUtf8(value);\n"); diff --git a/src/google/protobuf/compiler/java/java_string_field_lite.cc b/src/google/protobuf/compiler/java/java_string_field_lite.cc index 7e3ad1d0..adda307c 100644 --- a/src/google/protobuf/compiler/java/java_string_field_lite.cc +++ b/src/google/protobuf/compiler/java/java_string_field_lite.cc @@ -71,7 +71,7 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor, (*variables)["default"] = ImmutableDefaultValue(descriptor, name_resolver); (*variables)["default_init"] = "= " + ImmutableDefaultValue(descriptor, name_resolver); - (*variables)["capitalized_type"] = "String"; + (*variables)["capitalized_type"] = "java.lang.String"; (*variables)["tag"] = SimpleItoa(static_cast<int32>(WireFormat::MakeTag(descriptor))); (*variables)["tag_size"] = SimpleItoa( @@ -192,22 +192,25 @@ GenerateMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $get_has_field_bit_message$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.lang.String get$capitalized_name$() {\n" + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" " return $name$_;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes() {\n" + " ${$get$capitalized_name$Bytes$}$() {\n" " return com.google.protobuf.ByteString.copyFromUtf8($name$_);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, @@ -246,48 +249,54 @@ GenerateBuilderMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return instance.has$capitalized_name$();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.lang.String get$capitalized_name$() {\n" + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" " return instance.get$capitalized_name$();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes() {\n" + " ${$get$capitalized_name$Bytes$}$() {\n" " return instance.get$capitalized_name$Bytes();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$(\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" " java.lang.String value) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " copyOnWrite();\n" " instance.clear$capitalized_name$();\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$Bytes(\n" + "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" " com.google.protobuf.ByteString value) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$Bytes(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void ImmutableStringFieldLiteGenerator:: @@ -324,7 +333,7 @@ void ImmutableStringFieldLiteGenerator:: GenerateParsingCode(io::Printer* printer) const { if (CheckUtf8(descriptor_)) { printer->Print(variables_, - "String s = input.readStringRequireUtf8();\n" + "java.lang.String s = input.readStringRequireUtf8();\n" "$set_has_field_bit_message$\n" "$name$_ = s;\n"); } else { @@ -333,7 +342,7 @@ GenerateParsingCode(io::Printer* printer) const { // spurious intermediary ByteString allocations, cutting overall allocations // in half. printer->Print(variables_, - "String s = input.readString();\n" + "java.lang.String s = input.readString();\n" "$set_has_field_bit_message$\n" "$name$_ = s;\n"); } @@ -410,54 +419,60 @@ GenerateMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return $has_oneof_case_message$;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.lang.String get$capitalized_name$() {\n" + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" " java.lang.String ref $default_init$;\n" " if ($has_oneof_case_message$) {\n" " ref = (java.lang.String) $oneof_name$_;\n" " }\n" " return ref;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes() {\n" + " ${$get$capitalized_name$Bytes$}$() {\n" " java.lang.String ref $default_init$;\n" " if ($has_oneof_case_message$) {\n" " ref = (java.lang.String) $oneof_name$_;\n" " }\n" " return com.google.protobuf.ByteString.copyFromUtf8(ref);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$(\n" + "private void ${$set$capitalized_name$$}$(\n" " java.lang.String value) {\n" "$null_check$" " $set_oneof_case_message$;\n" " $oneof_name$_ = value;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void clear$capitalized_name$() {\n" + "private void ${$clear$capitalized_name$$}$() {\n" " if ($has_oneof_case_message$) {\n" " $clear_oneof_case_message$;\n" " $oneof_name$_ = null;\n" " }\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "private void set$capitalized_name$Bytes(\n" + "private void ${$set$capitalized_name$Bytes$}$(\n" " com.google.protobuf.ByteString value) {\n" "$null_check$"); + printer->Annotate("{", "}", descriptor_); if (CheckUtf8(descriptor_)) { printer->Print(variables_, " checkByteStringIsUtf8(value);\n"); @@ -474,48 +489,54 @@ GenerateBuilderMembers(io::Printer* printer) const { if (SupportFieldPresence(descriptor_->file())) { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public boolean has$capitalized_name$() {\n" + "$deprecation$public boolean ${$has$capitalized_name$$}$() {\n" " return instance.has$capitalized_name$();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.lang.String get$capitalized_name$() {\n" + "$deprecation$public java.lang.String ${$get$capitalized_name$$}$() {\n" " return instance.get$capitalized_name$();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes() {\n" + " ${$get$capitalized_name$Bytes$}$() {\n" " return instance.get$capitalized_name$Bytes();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$(\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" " java.lang.String value) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " copyOnWrite();\n" " instance.clear$capitalized_name$();\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$Bytes(\n" + "$deprecation$public Builder ${$set$capitalized_name$Bytes$}$(\n" " com.google.protobuf.ByteString value) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$Bytes(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void ImmutableStringOneofFieldLiteGenerator:: @@ -529,7 +550,7 @@ void ImmutableStringOneofFieldLiteGenerator:: GenerateParsingCode(io::Printer* printer) const { if (CheckUtf8(descriptor_)) { printer->Print(variables_, - "String s = input.readStringRequireUtf8();\n" + "java.lang.String s = input.readStringRequireUtf8();\n" "$set_oneof_case_message$;\n" "$oneof_name$_ = s;\n"); } else { @@ -538,7 +559,7 @@ GenerateParsingCode(io::Printer* printer) const { // spurious intermediary ByteString allocations, cutting overall allocations // in half. printer->Print(variables_, - "String s = input.readString();\n" + "java.lang.String s = input.readString();\n" "$set_oneof_case_message$;\n" "$oneof_name$_ = s;\n"); } @@ -597,7 +618,7 @@ void RepeatedImmutableStringFieldLiteGenerator:: GenerateInterfaceMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$java.util.List<String>\n" + "$deprecation$java.util.List<java.lang.String>\n" " get$capitalized_name$List();\n"); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, @@ -615,30 +636,37 @@ GenerateInterfaceMembers(io::Printer* printer) const { void RepeatedImmutableStringFieldLiteGenerator:: GenerateMembers(io::Printer* printer) const { printer->Print(variables_, - "private com.google.protobuf.Internal.ProtobufList<String> $name$_;\n"); + "private com.google.protobuf.Internal.ProtobufList<java.lang.String> " + "$name$_;\n"); PrintExtraFieldInfo(variables_, printer); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.util.List<String> get$capitalized_name$List() {\n" - " return $name$_;\n" // note: unmodifiable list + "$deprecation$public java.util.List<java.lang.String> " + "${$get$capitalized_name$List$}$() {\n" + " return $name$_;\n" // note: unmodifiable list "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Count() {\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" " return $name$_.size();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.lang.String get$capitalized_name$(int index) {\n" + "$deprecation$public java.lang.String " + "${$get$capitalized_name$$}$(int index) {\n" " return $name$_.get(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes(int index) {\n" + " ${$get$capitalized_name$Bytes$}$(int index) {\n" " return com.google.protobuf.ByteString.copyFromUtf8(\n" " $name$_.get(index));\n" "}\n"); + printer->Annotate("{", "}", descriptor_); printer->Print(variables_, "private void ensure$capitalized_name$IsMutable() {\n" @@ -697,67 +725,77 @@ void RepeatedImmutableStringFieldLiteGenerator:: GenerateBuilderMembers(io::Printer* printer) const { WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.util.List<String>\n" - " get$capitalized_name$List() {\n" + "$deprecation$public java.util.List<java.lang.String>\n" + " ${$get$capitalized_name$List$}$() {\n" " return java.util.Collections.unmodifiableList(\n" " instance.get$capitalized_name$List());\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public int get$capitalized_name$Count() {\n" + "$deprecation$public int ${$get$capitalized_name$Count$}$() {\n" " return instance.get$capitalized_name$Count();\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public java.lang.String get$capitalized_name$(int index) {\n" + "$deprecation$public java.lang.String " + "${$get$capitalized_name$$}$(int index) {\n" " return instance.get$capitalized_name$(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, "$deprecation$public com.google.protobuf.ByteString\n" - " get$capitalized_name$Bytes(int index) {\n" + " ${$get$capitalized_name$Bytes$}$(int index) {\n" " return instance.get$capitalized_name$Bytes(index);\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder set$capitalized_name$(\n" + "$deprecation$public Builder ${$set$capitalized_name$$}$(\n" " int index, java.lang.String value) {\n" " copyOnWrite();\n" " instance.set$capitalized_name$(index, value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder add$capitalized_name$(\n" + "$deprecation$public Builder ${$add$capitalized_name$$}$(\n" " java.lang.String value) {\n" " copyOnWrite();\n" " instance.add$capitalized_name$(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder addAll$capitalized_name$(\n" + "$deprecation$public Builder ${$addAll$capitalized_name$$}$(\n" " java.lang.Iterable<java.lang.String> values) {\n" " copyOnWrite();\n" " instance.addAll$capitalized_name$(values);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder clear$capitalized_name$() {\n" + "$deprecation$public Builder ${$clear$capitalized_name$$}$() {\n" " copyOnWrite();\n" " instance.clear$capitalized_name$();\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); WriteFieldDocComment(printer, descriptor_); printer->Print(variables_, - "$deprecation$public Builder add$capitalized_name$Bytes(\n" + "$deprecation$public Builder ${$add$capitalized_name$Bytes$}$(\n" " com.google.protobuf.ByteString value) {\n" " copyOnWrite();\n" " instance.add$capitalized_name$Bytes(value);\n" " return this;\n" "}\n"); + printer->Annotate("{", "}", descriptor_); } void RepeatedImmutableStringFieldLiteGenerator:: @@ -787,14 +825,14 @@ void RepeatedImmutableStringFieldLiteGenerator:: GenerateParsingCode(io::Printer* printer) const { if (CheckUtf8(descriptor_)) { printer->Print(variables_, - "String s = input.readStringRequireUtf8();\n"); + "java.lang.String s = input.readStringRequireUtf8();\n"); } else { // Lite runtime should attempt to reduce allocations by attempting to // construct the string directly from the input stream buffer. This avoids // spurious intermediary ByteString allocations, cutting overall allocations // in half. printer->Print(variables_, - "String s = input.readString();\n"); + "java.lang.String s = input.readString();\n"); } printer->Print(variables_, "if (!$is_mutable$) {\n" @@ -868,7 +906,7 @@ GenerateHashCode(io::Printer* printer) const { } string RepeatedImmutableStringFieldLiteGenerator::GetBoxedType() const { - return "String"; + return "java.lang.String"; } } // namespace java diff --git a/src/google/protobuf/compiler/js/js_generator.cc b/src/google/protobuf/compiler/js/js_generator.cc index 7c63e58f..ba078703 100755 --- a/src/google/protobuf/compiler/js/js_generator.cc +++ b/src/google/protobuf/compiler/js/js_generator.cc @@ -143,12 +143,16 @@ bool IsReserved(const string& ident) { return false; } +bool StrEndsWith(StringPiece sp, StringPiece x) { + return sp.size() >= x.size() && sp.substr(sp.size() - x.size()) == x; +} + // Returns a copy of |filename| with any trailing ".protodevel" or ".proto // suffix stripped. // TODO(haberman): Unify with copy in compiler/cpp/internal/helpers.cc. string StripProto(const string& filename) { - const char* suffix = HasSuffixString(filename, ".protodevel") - ? ".protodevel" : ".proto"; + const char* suffix = + StrEndsWith(filename, ".protodevel") ? ".protodevel" : ".proto"; return StripSuffixString(filename, suffix); } @@ -756,8 +760,22 @@ string DoubleToString(double value) { return PostProcessFloat(result); } +// Return true if this is an integral field that should be represented as string +// in JS. +bool IsIntegralFieldWithStringJSType(const FieldDescriptor* field) { + switch (field->cpp_type()) { + case FieldDescriptor::CPPTYPE_INT64: + case FieldDescriptor::CPPTYPE_UINT64: + // The default value of JSType is JS_NORMAL, which behaves the same as + // JS_NUMBER. + return field->options().jstype() == google::protobuf::FieldOptions::JS_STRING; + default: + return false; + } +} + string MaybeNumberString(const FieldDescriptor* field, const string& orig) { - return orig; + return IsIntegralFieldWithStringJSType(field) ? ("\"" + orig + "\"") : orig; } string JSFieldDefault(const FieldDescriptor* field) { @@ -856,7 +874,7 @@ string ProtoTypeName(const GeneratorOptions& options, } string JSIntegerTypeName(const FieldDescriptor* field) { - return "number"; + return IsIntegralFieldWithStringJSType(field) ? "string" : "number"; } string JSStringTypeName(const GeneratorOptions& options, @@ -1033,8 +1051,7 @@ string JSBinaryReaderMethodType(const FieldDescriptor* field) { if (name[0] >= 'a' && name[0] <= 'z') { name[0] = (name[0] - 'a') + 'A'; } - - return name; + return IsIntegralFieldWithStringJSType(field) ? (name + "String") : name; } string JSBinaryReadWriteMethodName(const FieldDescriptor* field, @@ -1244,13 +1261,6 @@ string FieldComments(const FieldDescriptor* field, BytesMode bytes_mode) { " * You should avoid comparisons like {@code val === true/false} in " "those cases.\n"; } - if (field->is_repeated()) { - comments += - " * If you change this array by adding, removing or replacing " - "elements, or if you\n" - " * replace the array itself, then you must call the setter to " - "update it.\n"; - } if (field->type() == FieldDescriptor::TYPE_BYTES && bytes_mode == BYTES_U8) { comments += " * Note that Uint8Array is not supported on all browsers.\n" @@ -1323,7 +1333,7 @@ bool IsExtendable(const Descriptor* desc) { // Returns the max index in the underlying data storage array beyond which the // extension object is used. string GetPivot(const Descriptor* desc) { - static const int kDefaultPivot = (1 << 29); // max field number (29 bits) + static const int kDefaultPivot = 500; // Find the max field number int max_field_number = 0; @@ -1335,7 +1345,7 @@ string GetPivot(const Descriptor* desc) { } int pivot = -1; - if (IsExtendable(desc)) { + if (IsExtendable(desc) || (max_field_number >= kDefaultPivot)) { pivot = ((max_field_number + 1) < kDefaultPivot) ? (max_field_number + 1) : kDefaultPivot; } @@ -1507,6 +1517,10 @@ void Generator::GenerateHeader(const GeneratorOptions& options, printer->Print("/**\n" " * @fileoverview\n" " * @enhanceable\n" + " * @suppress {messageConventions} JS Compiler reports an " + "error if a variable or\n" + " * field starts with 'MSG_' and isn't a translatable " + "message.\n" " * @public\n" " */\n" "// GENERATED CODE -- DO NOT EDIT!\n" @@ -1698,18 +1712,16 @@ void Generator::GenerateRequiresImpl(const GeneratorOptions& options, bool require_jspb, bool require_extension, bool require_map) const { if (require_jspb) { - printer->Print( - "goog.require('jspb.Message');\n" - "goog.require('jspb.BinaryReader');\n" - "goog.require('jspb.BinaryWriter');\n"); + required->insert("jspb.Message"); + required->insert("jspb.BinaryReader"); + required->insert("jspb.BinaryWriter"); } if (require_extension) { - printer->Print("goog.require('jspb.ExtensionFieldBinaryInfo');\n"); - printer->Print( - "goog.require('jspb.ExtensionFieldInfo');\n"); + required->insert("jspb.ExtensionFieldBinaryInfo"); + required->insert("jspb.ExtensionFieldInfo"); } if (require_map) { - printer->Print("goog.require('jspb.Map');\n"); + required->insert("jspb.Map"); } std::set<string>::iterator it; @@ -2037,6 +2049,7 @@ void Generator::GenerateClassToObject(const GeneratorOptions& options, " * http://goto/soy-param-migration\n" " * @param {!$classname$} msg The msg instance to transform.\n" " * @return {!Object}\n" + " * @suppress {unusedLocalVariables} f is only used for nested messages\n" " */\n" "$classname$.toObject = function(includeInstance, msg) {\n" " var f, obj = {", @@ -2125,7 +2138,8 @@ void Generator::GenerateFieldValueExpression(io::Printer* printer, "obj", obj_reference); } } else { - printer->Print("jspb.Message.getField($obj$, $index$)", + printer->Print("jspb.Message.get$cardinality$Field($obj$, $index$)", + "cardinality", field->is_repeated() ? "Repeated" : "", "index", JSFieldIndex(field), "obj", obj_reference); } @@ -2333,7 +2347,6 @@ void GenerateBytesWrapper(const GeneratorOptions& options, "defname", JSGetterName(options, field, BYTES_DEFAULT)); } - void Generator::GenerateClassField(const GeneratorOptions& options, io::Printer* printer, const FieldDescriptor* field) const { @@ -2463,7 +2476,7 @@ void Generator::GenerateClassField(const GeneratorOptions& options, // Simple (primitive) field, either singular or repeated. // TODO(b/26173701): Always use BYTES_DEFAULT for the getter return type; - // at this point we "lie" to non-binary users and tell the the return + // at this point we "lie" to non-binary users and tell the return // type is always base64 string, pending a LSC to migrate to typed getters. BytesMode bytes_mode = field->type() == FieldDescriptor::TYPE_BYTES && !options.binary ? @@ -2910,6 +2923,7 @@ void Generator::GenerateClassSerializeBinary(const GeneratorOptions& options, " * format), writing to the given BinaryWriter.\n" " * @param {!$class$} message\n" " * @param {!jspb.BinaryWriter} writer\n" + " * @suppress {unusedLocalVariables} f is only used for nested messages\n" " */\n" "$class$.serializeBinaryToWriter = function(message, " "writer) {\n" @@ -2982,7 +2996,13 @@ void Generator::GenerateClassSerializeBinaryField( case FieldDescriptor::CPPTYPE_INT64: case FieldDescriptor::CPPTYPE_UINT32: case FieldDescriptor::CPPTYPE_UINT64: { - { + if (IsIntegralFieldWithStringJSType(field)) { + // We can use `parseInt` here even though it will not be precise for + // 64-bit quantities because we are only testing for zero/nonzero, + // and JS numbers (64-bit floating point values, i.e., doubles) are + // integer-precise in the range that includes zero. + printer->Print(" if (parseInt(f, 10) !== 0) {\n"); + } else { printer->Print(" if (f !== 0) {\n"); } break; @@ -3306,7 +3326,8 @@ void Generator::GenerateFile(const GeneratorOptions& options, printer->Print( "var $alias$ = require('$file$');\n", "alias", ModuleAlias(name), - "file", GetRootPath(file->name(), name) + GetJSFilename(options, name)); + "file", + GetRootPath(file->name(), name) + GetJSFilename(options, name)); } } diff --git a/src/google/protobuf/compiler/js/well_known_types/any.js b/src/google/protobuf/compiler/js/well_known_types/any.js index 22f18919..d7ca6e3a 100644 --- a/src/google/protobuf/compiler/js/well_known_types/any.js +++ b/src/google/protobuf/compiler/js/well_known_types/any.js @@ -69,7 +69,7 @@ proto.google.protobuf.Any.prototype.pack = function(serialized, name, * the binary data properly. * @param {string} name The expected type name of this message object. * @return {?T} If the name matched the expected name, returns the deserialized - * object, otherwise returns undefined. + * object, otherwise returns null. */ proto.google.protobuf.Any.prototype.unpack = function(deserialize, name) { if (this.getTypeName() == name) { diff --git a/src/google/protobuf/compiler/mock_code_generator.cc b/src/google/protobuf/compiler/mock_code_generator.cc index 0ddb99e5..dfd28330 100644 --- a/src/google/protobuf/compiler/mock_code_generator.cc +++ b/src/google/protobuf/compiler/mock_code_generator.cc @@ -40,6 +40,7 @@ #endif #include <vector> + #include <google/protobuf/stubs/strutil.h> #include <google/protobuf/stubs/logging.h> @@ -52,6 +53,7 @@ #include <google/protobuf/io/zero_copy_stream.h> #include <google/protobuf/descriptor.pb.h> #include <google/protobuf/descriptor.h> +#include <google/protobuf/text_format.h> #include <google/protobuf/stubs/substitute.h> #include <gtest/gtest.h> @@ -129,11 +131,48 @@ void MockCodeGenerator::ExpectGenerated( } } +namespace { +void CheckSingleAnnotation(const string& expected_file, + const string& expected_text, + const string& file_content, + const GeneratedCodeInfo::Annotation& annotation) { + EXPECT_EQ(expected_file, annotation.source_file()); + ASSERT_GE(file_content.size(), annotation.begin()); + ASSERT_GE(file_content.size(), annotation.end()); + ASSERT_LE(annotation.begin(), annotation.end()); + EXPECT_EQ(expected_text.size(), annotation.end() - annotation.begin()); + EXPECT_EQ(expected_text, + file_content.substr(annotation.begin(), expected_text.size())); +} +} // anonymous namespace + +void MockCodeGenerator::CheckGeneratedAnnotations( + const string& name, const string& file, const string& output_directory) { + string file_content; + GOOGLE_CHECK_OK( + File::GetContents(output_directory + "/" + GetOutputFileName(name, file), + &file_content, true)); + string meta_content; + GOOGLE_CHECK_OK(File::GetContents( + output_directory + "/" + GetOutputFileName(name, file) + ".meta", + &meta_content, true)); + GeneratedCodeInfo annotations; + GOOGLE_CHECK(TextFormat::ParseFromString(meta_content, &annotations)); + ASSERT_EQ(3, annotations.annotation_size()); + CheckSingleAnnotation("first_annotation", "first", file_content, + annotations.annotation(0)); + CheckSingleAnnotation("second_annotation", "second", file_content, + annotations.annotation(1)); + CheckSingleAnnotation("third_annotation", "third", file_content, + annotations.annotation(2)); +} + bool MockCodeGenerator::Generate( const FileDescriptor* file, const string& parameter, GeneratorContext* context, string* error) const { + bool annotate = false; for (int i = 0; i < file->message_type_count(); i++) { if (HasPrefixString(file->message_type(i)->name(), "MockCodeGenerator_")) { string command = StripPrefixString(file->message_type(i)->name(), @@ -162,6 +201,8 @@ bool MockCodeGenerator::Generate( std::cerr << "Saw json_name: " << field_descriptor_proto.has_json_name() << std::endl; abort(); + } else if (command == "Annotate") { + annotate = true; } else if (command == "ShowVersionNumber") { Version compiler_version; context->GetCompilerVersion(&compiler_version); @@ -212,16 +253,40 @@ bool MockCodeGenerator::Generate( google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output( context->Open(GetOutputFileName(name_, file))); - io::Printer printer(output.get(), '$'); - printer.PrintRaw(GetOutputFileContent(name_, parameter, - file, context)); + GeneratedCodeInfo annotations; + io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector( + &annotations); + io::Printer printer(output.get(), '$', + annotate ? &annotation_collector : NULL); + printer.PrintRaw(GetOutputFileContent(name_, parameter, file, context)); + string annotate_suffix = "_annotation"; + if (annotate) { + printer.Print("$p$", "p", "first"); + printer.Annotate("p", "first" + annotate_suffix); + } printer.PrintRaw(kFirstInsertionPoint); + if (annotate) { + printer.Print("$p$", "p", "second"); + printer.Annotate("p", "second" + annotate_suffix); + } printer.PrintRaw(kSecondInsertionPoint); + if (annotate) { + printer.Print("$p$", "p", "third"); + printer.Annotate("p", "third" + annotate_suffix); + } if (printer.failed()) { *error = "MockCodeGenerator detected write error."; return false; } + if (annotate) { + google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> meta_output( + context->Open(GetOutputFileName(name_, file) + ".meta")); + if (!TextFormat::Print(annotations, meta_output.get())) { + *error = "MockCodeGenerator couldn't write .meta"; + return false; + } + } } return true; diff --git a/src/google/protobuf/compiler/mock_code_generator.h b/src/google/protobuf/compiler/mock_code_generator.h index e1665f88..cdd9138c 100644 --- a/src/google/protobuf/compiler/mock_code_generator.h +++ b/src/google/protobuf/compiler/mock_code_generator.h @@ -68,6 +68,8 @@ namespace compiler { // printing "Saw message type MockCodeGenerator_HasSourceCodeInfo: FOO." to // stderr, where FOO is "1" if the supplied FileDescriptorProto has source // code info, and "0" otherwise. +// MockCodeGenerator_Annotate: Generate() will add annotations to its output +// that can later be verified with CheckGeneratedAnnotations. class MockCodeGenerator : public CodeGenerator { public: MockCodeGenerator(const string& name); @@ -88,6 +90,12 @@ class MockCodeGenerator : public CodeGenerator { const string& parsed_file_list, const string& output_directory); + // Checks that the correct text ranges were annotated by the + // MockCodeGenerator_Annotate directive. + static void CheckGeneratedAnnotations(const string& name, + const string& file, + const string& output_directory); + // Get the name of the file which would be written by the given generator. static string GetOutputFileName(const string& generator_name, const FileDescriptor* file); diff --git a/src/google/protobuf/compiler/parser.cc b/src/google/protobuf/compiler/parser.cc index 7a03d42b..23e9e62b 100644 --- a/src/google/protobuf/compiler/parser.cc +++ b/src/google/protobuf/compiler/parser.cc @@ -664,7 +664,7 @@ bool Parser::ParseMessageDefinition( namespace { -const int kMaxExtensionRangeSentinel = -1; +const int kMaxRangeSentinel = -1; bool IsMessageSetWireFormatMessage(const DescriptorProto& message) { const MessageOptions& options = message.options(); @@ -688,12 +688,27 @@ void AdjustExtensionRangesWithMaxEndNumber(DescriptorProto* message) { kint32max : FieldDescriptor::kMaxNumber + 1; for (int i = 0; i < message->extension_range_size(); ++i) { - if (message->extension_range(i).end() == kMaxExtensionRangeSentinel) { + if (message->extension_range(i).end() == kMaxRangeSentinel) { message->mutable_extension_range(i)->set_end(max_extension_number); } } } +// Modifies any reserved ranges that specified 'max' as the end of the +// reserved range, and sets them to the type-specific maximum. The actual max +// tag number can only be determined after all options have been parsed. +void AdjustReservedRangesWithMaxEndNumber(DescriptorProto* message) { + const bool is_message_set = IsMessageSetWireFormatMessage(*message); + const int max_field_number = is_message_set ? + kint32max : + FieldDescriptor::kMaxNumber + 1; + for (int i = 0; i < message->reserved_range_size(); ++i) { + if (message->reserved_range(i).end() == kMaxRangeSentinel) { + message->mutable_reserved_range(i)->set_end(max_field_number); + } + } +} + } // namespace bool Parser::ParseMessageBlock(DescriptorProto* message, @@ -717,6 +732,9 @@ bool Parser::ParseMessageBlock(DescriptorProto* message, if (message->extension_range_size() > 0) { AdjustExtensionRangesWithMaxEndNumber(message); } + if (message->reserved_range_size() > 0) { + AdjustReservedRangesWithMaxEndNumber(message); + } return true; } @@ -1429,6 +1447,8 @@ bool Parser::ParseExtensions(DescriptorProto* message, // Parse the declaration. DO(Consume("extensions")); + int old_range_size = message->extension_range_size(); + do { // Note that kExtensionRangeFieldNumber was already pushed by the parent. LocationRecorder location(extensions_location, @@ -1455,7 +1475,7 @@ bool Parser::ParseExtensions(DescriptorProto* message, // Set to the sentinel value - 1 since we increment the value below. // The actual value of the end of the range should be set with // AdjustExtensionRangesWithMaxEndNumber. - end = kMaxExtensionRangeSentinel - 1; + end = kMaxRangeSentinel - 1; } else { DO(ConsumeInteger(&end, "Expected integer.")); } @@ -1475,12 +1495,36 @@ bool Parser::ParseExtensions(DescriptorProto* message, range->set_end(end); } while (TryConsume(",")); + if (LookingAt("[")) { + LocationRecorder location( + extensions_location, + DescriptorProto::ExtensionRange::kOptionsFieldNumber); + + DO(Consume("[")); + + // Parse extension range options in the first range. + ExtensionRangeOptions* options = + message->mutable_extension_range(old_range_size)->mutable_options(); + do { + DO(ParseOption(options, location, containing_file, OPTION_ASSIGNMENT)); + } while (TryConsume(",")); + + DO(Consume("]")); + + // Then copy the extension range options to all of the other ranges we've + // parsed. + for (int i = old_range_size + 1; i < message->extension_range_size(); i++) { + message->mutable_extension_range(i)->mutable_options() + ->CopyFrom(*options); + } + } + DO(ConsumeEndOfDeclaration(";", &extensions_location)); return true; } -// This is similar to extension range parsing, except that "max" is not -// supported, and accepts field name literals. +// This is similar to extension range parsing, except that it accepts field +// name literals. bool Parser::ParseReserved(DescriptorProto* message, const LocationRecorder& message_location) { // Parse the declaration. @@ -1528,7 +1572,14 @@ bool Parser::ParseReservedNumbers(DescriptorProto* message, if (TryConsume("to")) { LocationRecorder end_location( location, DescriptorProto::ReservedRange::kEndFieldNumber); - DO(ConsumeInteger(&end, "Expected integer.")); + if (TryConsume("max")) { + // Set to the sentinel value - 1 since we increment the value below. + // The actual value of the end of the range should be set with + // AdjustExtensionRangesWithMaxEndNumber. + end = kMaxRangeSentinel - 1; + } else { + DO(ConsumeInteger(&end, "Expected integer.")); + } } else { LocationRecorder end_location( location, DescriptorProto::ReservedRange::kEndFieldNumber); diff --git a/src/google/protobuf/compiler/parser_unittest.cc b/src/google/protobuf/compiler/parser_unittest.cc index d5e31325..97831f71 100644 --- a/src/google/protobuf/compiler/parser_unittest.cc +++ b/src/google/protobuf/compiler/parser_unittest.cc @@ -663,7 +663,7 @@ TEST_F(ParseMessageTest, ReservedRange) { ExpectParsesTo( "message TestMessage {\n" " required int32 foo = 1;\n" - " reserved 2, 15, 9 to 11, 3;\n" + " reserved 2, 15, 9 to 11, 3, 20 to max;\n" "}\n", "message_type {" @@ -673,6 +673,29 @@ TEST_F(ParseMessageTest, ReservedRange) { " reserved_range { start:15 end:16 }" " reserved_range { start:9 end:12 }" " reserved_range { start:3 end:4 }" + " reserved_range { start:20 end:536870912 }" + "}"); +} + +TEST_F(ParseMessageTest, ReservedRangeOnMessageSet) { + ExpectParsesTo( + "message TestMessage {\n" + " option message_set_wire_format = true;\n" + " reserved 20 to max;\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"message_set_wire_format\"" + " is_extension: false" + " }" + " identifier_value: \"true\"" + " }" + " }" + " reserved_range { start:20 end:2147483647 }" "}"); } @@ -703,6 +726,30 @@ TEST_F(ParseMessageTest, ExtensionRange) { "}"); } +TEST_F(ParseMessageTest, ExtensionRangeWithOptions) { + ExpectParsesTo( + "message TestMessage {\n" + " extensions 10 to 19 [(i) = 5];\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " extension_range {" + " start:10" + " end:20" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"i\"" + " is_extension: true" + " }" + " positive_int_value: 5" + " }" + " }" + " }" + "}"); +} + TEST_F(ParseMessageTest, CompoundExtensionRange) { ExpectParsesTo( "message TestMessage {\n" @@ -719,6 +766,82 @@ TEST_F(ParseMessageTest, CompoundExtensionRange) { "}"); } +TEST_F(ParseMessageTest, CompoundExtensionRangeWithOptions) { + ExpectParsesTo( + "message TestMessage {\n" + " extensions 2, 15, 9 to 11, 100 to max, 3 [(i) = 5];\n" + "}\n", + + "message_type {" + " name: \"TestMessage\"" + " extension_range {" + " start:2" + " end:3" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"i\"" + " is_extension: true" + " }" + " positive_int_value: 5" + " }" + " }" + " }" + " extension_range {" + " start:15" + " end:16" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"i\"" + " is_extension: true" + " }" + " positive_int_value: 5" + " }" + " }" + " }" + " extension_range {" + " start:9" + " end:12" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"i\"" + " is_extension: true" + " }" + " positive_int_value: 5" + " }" + " }" + " }" + " extension_range {" + " start:100" + " end:536870912" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"i\"" + " is_extension: true" + " }" + " positive_int_value: 5" + " }" + " }" + " }" + " extension_range {" + " start:3" + " end:4" + " options {" + " uninterpreted_option {" + " name {" + " name_part: \"i\"" + " is_extension: true" + " }" + " positive_int_value: 5" + " }" + " }" + " }" + "}"); +} + TEST_F(ParseMessageTest, LargerMaxForMessageSetWireFormatMessages) { // Messages using the message_set_wire_format option can accept larger // extension numbers, as the numbers are not encoded as int32 field values @@ -1368,12 +1491,12 @@ TEST_F(ParseErrorTest, EnumValueMissingNumber) { // ------------------------------------------------------------------- // Reserved field number errors -TEST_F(ParseErrorTest, ReservedMaxNotAllowed) { +TEST_F(ParseErrorTest, ReservedStandaloneMaxNotAllowed) { ExpectHasErrors( "message Foo {\n" - " reserved 10 to max;\n" + " reserved max;\n" "}\n", - "1:17: Expected integer.\n"); + "1:11: Expected field name or number range.\n"); } TEST_F(ParseErrorTest, ReservedMixNameAndNumber) { diff --git a/src/google/protobuf/compiler/plugin.cc b/src/google/protobuf/compiler/plugin.cc index 3848101d..534b2a73 100644 --- a/src/google/protobuf/compiler/plugin.cc +++ b/src/google/protobuf/compiler/plugin.cc @@ -127,6 +127,7 @@ bool GenerateCode(const CodeGeneratorRequest& request, GeneratorResponseContext context( request.compiler_version(), response, parsed_files); + string error; bool succeeded = generator.GenerateAll( parsed_files, request.parameter(), &context, &error); diff --git a/src/google/protobuf/compiler/plugin.pb.cc b/src/google/protobuf/compiler/plugin.pb.cc index f7dc1b70..6cc96b30 100644 --- a/src/google/protobuf/compiler/plugin.pb.cc +++ b/src/google/protobuf/compiler/plugin.pb.cc @@ -39,23 +39,23 @@ namespace { } // namespace PROTOBUF_CONSTEXPR_VAR ::google::protobuf::internal::ParseTableField - const TableStruct::entries[] = { + const TableStruct::entries[] GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { {0, 0, 0, ::google::protobuf::internal::kInvalidMask, 0, 0}, }; PROTOBUF_CONSTEXPR_VAR ::google::protobuf::internal::AuxillaryParseTableField - const TableStruct::aux[] = { + const TableStruct::aux[] GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { ::google::protobuf::internal::AuxillaryParseTableField(), }; PROTOBUF_CONSTEXPR_VAR ::google::protobuf::internal::ParseTable const - TableStruct::schema[] = { + TableStruct::schema[] GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { { NULL, NULL, 0, -1, -1, false }, { NULL, NULL, 0, -1, -1, false }, { NULL, NULL, 0, -1, -1, false }, { NULL, NULL, 0, -1, -1, false }, }; -const ::google::protobuf::uint32 TableStruct::offsets[] = { +const ::google::protobuf::uint32 TableStruct::offsets[] GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Version, _has_bits_), GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(Version, _internal_metadata_), ~0u, // no _extensions_ @@ -103,8 +103,7 @@ const ::google::protobuf::uint32 TableStruct::offsets[] = { 0, ~0u, }; - -static const ::google::protobuf::internal::MigrationSchema schemas[] = { +static const ::google::protobuf::internal::MigrationSchema schemas[] GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { { 0, 9, sizeof(Version)}, { 13, 22, sizeof(CodeGeneratorRequest)}, { 26, 34, sizeof(CodeGeneratorResponse_File)}, @@ -140,18 +139,6 @@ void protobuf_RegisterTypes(const ::std::string&) { } } // namespace - -void TableStruct::Shutdown() { - _Version_default_instance_.Shutdown(); - delete file_level_metadata[0].reflection; - _CodeGeneratorRequest_default_instance_.Shutdown(); - delete file_level_metadata[1].reflection; - _CodeGeneratorResponse_File_default_instance_.Shutdown(); - delete file_level_metadata[2].reflection; - _CodeGeneratorResponse_default_instance_.Shutdown(); - delete file_level_metadata[3].reflection; -} - void TableStruct::InitDefaultsImpl() { GOOGLE_PROTOBUF_VERIFY_VERSION; @@ -171,7 +158,7 @@ void InitDefaults() { } void AddDescriptorsImpl() { InitDefaults(); - static const char descriptor[] = { + static const char descriptor[] GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = { "\n%google/protobuf/compiler/plugin.proto\022" "\030google.protobuf.compiler\032 google/protob" "uf/descriptor.proto\"F\n\007Version\022\r\n\005major\030" @@ -194,14 +181,13 @@ void AddDescriptorsImpl() { ::google::protobuf::MessageFactory::InternalRegisterGeneratedFile( "google/protobuf/compiler/plugin.proto", &protobuf_RegisterTypes); ::google::protobuf::protobuf_google_2fprotobuf_2fdescriptor_2eproto::AddDescriptors(); - ::google::protobuf::internal::OnShutdown(&TableStruct::Shutdown); } void AddDescriptors() { static GOOGLE_PROTOBUF_DECLARE_ONCE(once); ::google::protobuf::GoogleOnceInit(&once, &AddDescriptorsImpl); } -// Force AddDescriptors() to be called at static initialization time. +// Force AddDescriptors() to be called at dynamic initialization time. struct StaticDescriptorInitializer { StaticDescriptorInitializer() { AddDescriptors(); @@ -285,11 +271,16 @@ Version* Version::New(::google::protobuf::Arena* arena) const { void Version::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.compiler.Version) + ::google::protobuf::uint32 cached_has_bits = 0; + // Prevent compiler warnings about cached_has_bits being unused + (void) cached_has_bits; + if (has_suffix()) { GOOGLE_DCHECK(!suffix_.IsDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited())); (*suffix_.UnsafeRawStringPointer())->clear(); } - if (_has_bits_[0 / 32] & 14u) { + cached_has_bits = _has_bits_[0]; + if (cached_has_bits & 14u) { ::memset(&major_, 0, reinterpret_cast<char*>(&patch_) - reinterpret_cast<char*>(&major_) + sizeof(patch_)); } @@ -310,7 +301,7 @@ bool Version::MergePartialFromCodedStream( // optional int32 major = 1; case 1: { if (static_cast< ::google::protobuf::uint8>(tag) == - static_cast< ::google::protobuf::uint8>(8u)) { + static_cast< ::google::protobuf::uint8>(8u /* 8 & 0xFF */)) { set_has_major(); DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( @@ -324,7 +315,7 @@ bool Version::MergePartialFromCodedStream( // optional int32 minor = 2; case 2: { if (static_cast< ::google::protobuf::uint8>(tag) == - static_cast< ::google::protobuf::uint8>(16u)) { + static_cast< ::google::protobuf::uint8>(16u /* 16 & 0xFF */)) { set_has_minor(); DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( @@ -338,7 +329,7 @@ bool Version::MergePartialFromCodedStream( // optional int32 patch = 3; case 3: { if (static_cast< ::google::protobuf::uint8>(tag) == - static_cast< ::google::protobuf::uint8>(24u)) { + static_cast< ::google::protobuf::uint8>(24u /* 24 & 0xFF */)) { set_has_patch(); DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( @@ -352,7 +343,7 @@ bool Version::MergePartialFromCodedStream( // optional string suffix = 4; case 4: { if (static_cast< ::google::protobuf::uint8>(tag) == - static_cast< ::google::protobuf::uint8>(34u)) { + static_cast< ::google::protobuf::uint8>(34u /* 34 & 0xFF */)) { DO_(::google::protobuf::internal::WireFormatLite::ReadString( input, this->mutable_suffix())); ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( @@ -367,13 +358,11 @@ bool Version::MergePartialFromCodedStream( default: { handle_unusual: - if (tag == 0 || - ::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { + if (tag == 0) { goto success; } DO_(::google::protobuf::internal::WireFormat::SkipField( - input, tag, mutable_unknown_fields())); + input, tag, _internal_metadata_.mutable_unknown_fields())); break; } } @@ -421,7 +410,7 @@ void Version::SerializeWithCachedSizes( if (_internal_metadata_.have_unknown_fields()) { ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - unknown_fields(), output); + _internal_metadata_.unknown_fields(), output); } // @@protoc_insertion_point(serialize_end:google.protobuf.compiler.Version) } @@ -461,7 +450,7 @@ void Version::SerializeWithCachedSizes( if (_internal_metadata_.have_unknown_fields()) { target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - unknown_fields(), target); + _internal_metadata_.unknown_fields(), target); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.compiler.Version) return target; @@ -474,7 +463,7 @@ size_t Version::ByteSizeLong() const { if (_internal_metadata_.have_unknown_fields()) { total_size += ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - unknown_fields()); + _internal_metadata_.unknown_fields()); } if (_has_bits_[0 / 32] & 15u) { // optional string suffix = 4; @@ -577,13 +566,14 @@ void Version::Swap(Version* other) { InternalSwap(other); } void Version::InternalSwap(Version* other) { + using std::swap; suffix_.Swap(&other->suffix_); - std::swap(major_, other->major_); - std::swap(minor_, other->minor_); - std::swap(patch_, other->patch_); - std::swap(_has_bits_[0], other->_has_bits_[0]); + swap(major_, other->major_); + swap(minor_, other->minor_); + swap(patch_, other->patch_); + swap(_has_bits_[0], other->_has_bits_[0]); _internal_metadata_.Swap(&other->_internal_metadata_); - std::swap(_cached_size_, other->_cached_size_); + swap(_cached_size_, other->_cached_size_); } ::google::protobuf::Metadata Version::GetMetadata() const { @@ -781,9 +771,7 @@ CodeGeneratorRequest::~CodeGeneratorRequest() { void CodeGeneratorRequest::SharedDtor() { parameter_.DestroyNoArena(&::google::protobuf::internal::GetEmptyStringAlreadyInited()); - if (this != internal_default_instance()) { - delete compiler_version_; - } + delete compiler_version_; } void CodeGeneratorRequest::SetCachedSize(int size) const { @@ -811,14 +799,19 @@ CodeGeneratorRequest* CodeGeneratorRequest::New(::google::protobuf::Arena* arena void CodeGeneratorRequest::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.compiler.CodeGeneratorRequest) + ::google::protobuf::uint32 cached_has_bits = 0; + // Prevent compiler warnings about cached_has_bits being unused + (void) cached_has_bits; + file_to_generate_.Clear(); proto_file_.Clear(); - if (_has_bits_[0 / 32] & 3u) { - if (has_parameter()) { + cached_has_bits = _has_bits_[0]; + if (cached_has_bits & 3u) { + if (cached_has_bits & 0x00000001u) { GOOGLE_DCHECK(!parameter_.IsDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited())); (*parameter_.UnsafeRawStringPointer())->clear(); } - if (has_compiler_version()) { + if (cached_has_bits & 0x00000002u) { GOOGLE_DCHECK(compiler_version_ != NULL); compiler_version_->::google::protobuf::compiler::Version::Clear(); } @@ -840,7 +833,7 @@ bool CodeGeneratorRequest::MergePartialFromCodedStream( // repeated string file_to_generate = 1; case 1: { if (static_cast< ::google::protobuf::uint8>(tag) == - static_cast< ::google::protobuf::uint8>(10u)) { + static_cast< ::google::protobuf::uint8>(10u /* 10 & 0xFF */)) { DO_(::google::protobuf::internal::WireFormatLite::ReadString( input, this->add_file_to_generate())); ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( @@ -857,7 +850,7 @@ bool CodeGeneratorRequest::MergePartialFromCodedStream( // optional string parameter = 2; case 2: { if (static_cast< ::google::protobuf::uint8>(tag) == - static_cast< ::google::protobuf::uint8>(18u)) { + static_cast< ::google::protobuf::uint8>(18u /* 18 & 0xFF */)) { DO_(::google::protobuf::internal::WireFormatLite::ReadString( input, this->mutable_parameter())); ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( @@ -873,7 +866,7 @@ bool CodeGeneratorRequest::MergePartialFromCodedStream( // optional .google.protobuf.compiler.Version compiler_version = 3; case 3: { if (static_cast< ::google::protobuf::uint8>(tag) == - static_cast< ::google::protobuf::uint8>(26u)) { + static_cast< ::google::protobuf::uint8>(26u /* 26 & 0xFF */)) { DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( input, mutable_compiler_version())); } else { @@ -885,7 +878,7 @@ bool CodeGeneratorRequest::MergePartialFromCodedStream( // repeated .google.protobuf.FileDescriptorProto proto_file = 15; case 15: { if (static_cast< ::google::protobuf::uint8>(tag) == - static_cast< ::google::protobuf::uint8>(122u)) { + static_cast< ::google::protobuf::uint8>(122u /* 122 & 0xFF */)) { DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( input, add_proto_file())); } else { @@ -896,13 +889,11 @@ bool CodeGeneratorRequest::MergePartialFromCodedStream( default: { handle_unusual: - if (tag == 0 || - ::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { + if (tag == 0) { goto success; } DO_(::google::protobuf::internal::WireFormat::SkipField( - input, tag, mutable_unknown_fields())); + input, tag, _internal_metadata_.mutable_unknown_fields())); break; } } @@ -957,7 +948,7 @@ void CodeGeneratorRequest::SerializeWithCachedSizes( if (_internal_metadata_.have_unknown_fields()) { ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - unknown_fields(), output); + _internal_metadata_.unknown_fields(), output); } // @@protoc_insertion_point(serialize_end:google.protobuf.compiler.CodeGeneratorRequest) } @@ -1006,7 +997,7 @@ void CodeGeneratorRequest::SerializeWithCachedSizes( if (_internal_metadata_.have_unknown_fields()) { target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - unknown_fields(), target); + _internal_metadata_.unknown_fields(), target); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.compiler.CodeGeneratorRequest) return target; @@ -1019,7 +1010,7 @@ size_t CodeGeneratorRequest::ByteSizeLong() const { if (_internal_metadata_.have_unknown_fields()) { total_size += ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - unknown_fields()); + _internal_metadata_.unknown_fields()); } // repeated string file_to_generate = 1; total_size += 1 * @@ -1123,13 +1114,14 @@ void CodeGeneratorRequest::Swap(CodeGeneratorRequest* other) { InternalSwap(other); } void CodeGeneratorRequest::InternalSwap(CodeGeneratorRequest* other) { + using std::swap; file_to_generate_.InternalSwap(&other->file_to_generate_); proto_file_.InternalSwap(&other->proto_file_); parameter_.Swap(&other->parameter_); - std::swap(compiler_version_, other->compiler_version_); - std::swap(_has_bits_[0], other->_has_bits_[0]); + swap(compiler_version_, other->compiler_version_); + swap(_has_bits_[0], other->_has_bits_[0]); _internal_metadata_.Swap(&other->_internal_metadata_); - std::swap(_cached_size_, other->_cached_size_); + swap(_cached_size_, other->_cached_size_); } ::google::protobuf::Metadata CodeGeneratorRequest::GetMetadata() const { @@ -1317,9 +1309,10 @@ void CodeGeneratorRequest::clear_compiler_version() { clear_has_compiler_version(); } const ::google::protobuf::compiler::Version& CodeGeneratorRequest::compiler_version() const { + const ::google::protobuf::compiler::Version* p = compiler_version_; // @@protoc_insertion_point(field_get:google.protobuf.compiler.CodeGeneratorRequest.compiler_version) - return compiler_version_ != NULL ? *compiler_version_ - : *::google::protobuf::compiler::Version::internal_default_instance(); + return p != NULL ? *p : *reinterpret_cast<const ::google::protobuf::compiler::Version*>( + &::google::protobuf::compiler::_Version_default_instance_); } ::google::protobuf::compiler::Version* CodeGeneratorRequest::mutable_compiler_version() { set_has_compiler_version(); @@ -1429,16 +1422,21 @@ CodeGeneratorResponse_File* CodeGeneratorResponse_File::New(::google::protobuf:: void CodeGeneratorResponse_File::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.compiler.CodeGeneratorResponse.File) - if (_has_bits_[0 / 32] & 7u) { - if (has_name()) { + ::google::protobuf::uint32 cached_has_bits = 0; + // Prevent compiler warnings about cached_has_bits being unused + (void) cached_has_bits; + + cached_has_bits = _has_bits_[0]; + if (cached_has_bits & 7u) { + if (cached_has_bits & 0x00000001u) { GOOGLE_DCHECK(!name_.IsDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited())); (*name_.UnsafeRawStringPointer())->clear(); } - if (has_insertion_point()) { + if (cached_has_bits & 0x00000002u) { GOOGLE_DCHECK(!insertion_point_.IsDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited())); (*insertion_point_.UnsafeRawStringPointer())->clear(); } - if (has_content()) { + if (cached_has_bits & 0x00000004u) { GOOGLE_DCHECK(!content_.IsDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited())); (*content_.UnsafeRawStringPointer())->clear(); } @@ -1460,7 +1458,7 @@ bool CodeGeneratorResponse_File::MergePartialFromCodedStream( // optional string name = 1; case 1: { if (static_cast< ::google::protobuf::uint8>(tag) == - static_cast< ::google::protobuf::uint8>(10u)) { + static_cast< ::google::protobuf::uint8>(10u /* 10 & 0xFF */)) { DO_(::google::protobuf::internal::WireFormatLite::ReadString( input, this->mutable_name())); ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( @@ -1476,7 +1474,7 @@ bool CodeGeneratorResponse_File::MergePartialFromCodedStream( // optional string insertion_point = 2; case 2: { if (static_cast< ::google::protobuf::uint8>(tag) == - static_cast< ::google::protobuf::uint8>(18u)) { + static_cast< ::google::protobuf::uint8>(18u /* 18 & 0xFF */)) { DO_(::google::protobuf::internal::WireFormatLite::ReadString( input, this->mutable_insertion_point())); ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( @@ -1492,7 +1490,7 @@ bool CodeGeneratorResponse_File::MergePartialFromCodedStream( // optional string content = 15; case 15: { if (static_cast< ::google::protobuf::uint8>(tag) == - static_cast< ::google::protobuf::uint8>(122u)) { + static_cast< ::google::protobuf::uint8>(122u /* 122 & 0xFF */)) { DO_(::google::protobuf::internal::WireFormatLite::ReadString( input, this->mutable_content())); ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( @@ -1507,13 +1505,11 @@ bool CodeGeneratorResponse_File::MergePartialFromCodedStream( default: { handle_unusual: - if (tag == 0 || - ::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { + if (tag == 0) { goto success; } DO_(::google::protobuf::internal::WireFormat::SkipField( - input, tag, mutable_unknown_fields())); + input, tag, _internal_metadata_.mutable_unknown_fields())); break; } } @@ -1566,7 +1562,7 @@ void CodeGeneratorResponse_File::SerializeWithCachedSizes( if (_internal_metadata_.have_unknown_fields()) { ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - unknown_fields(), output); + _internal_metadata_.unknown_fields(), output); } // @@protoc_insertion_point(serialize_end:google.protobuf.compiler.CodeGeneratorResponse.File) } @@ -1613,7 +1609,7 @@ void CodeGeneratorResponse_File::SerializeWithCachedSizes( if (_internal_metadata_.have_unknown_fields()) { target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - unknown_fields(), target); + _internal_metadata_.unknown_fields(), target); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.compiler.CodeGeneratorResponse.File) return target; @@ -1626,7 +1622,7 @@ size_t CodeGeneratorResponse_File::ByteSizeLong() const { if (_internal_metadata_.have_unknown_fields()) { total_size += ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - unknown_fields()); + _internal_metadata_.unknown_fields()); } if (_has_bits_[0 / 32] & 7u) { // optional string name = 1; @@ -1720,12 +1716,13 @@ void CodeGeneratorResponse_File::Swap(CodeGeneratorResponse_File* other) { InternalSwap(other); } void CodeGeneratorResponse_File::InternalSwap(CodeGeneratorResponse_File* other) { + using std::swap; name_.Swap(&other->name_); insertion_point_.Swap(&other->insertion_point_); content_.Swap(&other->content_); - std::swap(_has_bits_[0], other->_has_bits_[0]); + swap(_has_bits_[0], other->_has_bits_[0]); _internal_metadata_.Swap(&other->_internal_metadata_); - std::swap(_cached_size_, other->_cached_size_); + swap(_cached_size_, other->_cached_size_); } ::google::protobuf::Metadata CodeGeneratorResponse_File::GetMetadata() const { @@ -1995,6 +1992,10 @@ CodeGeneratorResponse* CodeGeneratorResponse::New(::google::protobuf::Arena* are void CodeGeneratorResponse::Clear() { // @@protoc_insertion_point(message_clear_start:google.protobuf.compiler.CodeGeneratorResponse) + ::google::protobuf::uint32 cached_has_bits = 0; + // Prevent compiler warnings about cached_has_bits being unused + (void) cached_has_bits; + file_.Clear(); if (has_error()) { GOOGLE_DCHECK(!error_.IsDefault(&::google::protobuf::internal::GetEmptyStringAlreadyInited())); @@ -2017,7 +2018,7 @@ bool CodeGeneratorResponse::MergePartialFromCodedStream( // optional string error = 1; case 1: { if (static_cast< ::google::protobuf::uint8>(tag) == - static_cast< ::google::protobuf::uint8>(10u)) { + static_cast< ::google::protobuf::uint8>(10u /* 10 & 0xFF */)) { DO_(::google::protobuf::internal::WireFormatLite::ReadString( input, this->mutable_error())); ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField( @@ -2033,7 +2034,7 @@ bool CodeGeneratorResponse::MergePartialFromCodedStream( // repeated .google.protobuf.compiler.CodeGeneratorResponse.File file = 15; case 15: { if (static_cast< ::google::protobuf::uint8>(tag) == - static_cast< ::google::protobuf::uint8>(122u)) { + static_cast< ::google::protobuf::uint8>(122u /* 122 & 0xFF */)) { DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual( input, add_file())); } else { @@ -2044,13 +2045,11 @@ bool CodeGeneratorResponse::MergePartialFromCodedStream( default: { handle_unusual: - if (tag == 0 || - ::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == - ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { + if (tag == 0) { goto success; } DO_(::google::protobuf::internal::WireFormat::SkipField( - input, tag, mutable_unknown_fields())); + input, tag, _internal_metadata_.mutable_unknown_fields())); break; } } @@ -2089,7 +2088,7 @@ void CodeGeneratorResponse::SerializeWithCachedSizes( if (_internal_metadata_.have_unknown_fields()) { ::google::protobuf::internal::WireFormat::SerializeUnknownFields( - unknown_fields(), output); + _internal_metadata_.unknown_fields(), output); } // @@protoc_insertion_point(serialize_end:google.protobuf.compiler.CodeGeneratorResponse) } @@ -2121,7 +2120,7 @@ void CodeGeneratorResponse::SerializeWithCachedSizes( if (_internal_metadata_.have_unknown_fields()) { target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( - unknown_fields(), target); + _internal_metadata_.unknown_fields(), target); } // @@protoc_insertion_point(serialize_to_array_end:google.protobuf.compiler.CodeGeneratorResponse) return target; @@ -2134,7 +2133,7 @@ size_t CodeGeneratorResponse::ByteSizeLong() const { if (_internal_metadata_.have_unknown_fields()) { total_size += ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( - unknown_fields()); + _internal_metadata_.unknown_fields()); } // repeated .google.protobuf.compiler.CodeGeneratorResponse.File file = 15; { @@ -2213,11 +2212,12 @@ void CodeGeneratorResponse::Swap(CodeGeneratorResponse* other) { InternalSwap(other); } void CodeGeneratorResponse::InternalSwap(CodeGeneratorResponse* other) { + using std::swap; file_.InternalSwap(&other->file_); error_.Swap(&other->error_); - std::swap(_has_bits_[0], other->_has_bits_[0]); + swap(_has_bits_[0], other->_has_bits_[0]); _internal_metadata_.Swap(&other->_internal_metadata_); - std::swap(_cached_size_, other->_cached_size_); + swap(_cached_size_, other->_cached_size_); } ::google::protobuf::Metadata CodeGeneratorResponse::GetMetadata() const { diff --git a/src/google/protobuf/compiler/plugin.pb.h b/src/google/protobuf/compiler/plugin.pb.h index 4f8befb6..cce28936 100644 --- a/src/google/protobuf/compiler/plugin.pb.h +++ b/src/google/protobuf/compiler/plugin.pb.h @@ -39,81 +39,6 @@ #endif namespace google { namespace protobuf { -class DescriptorProto; -class DescriptorProtoDefaultTypeInternal; -LIBPROTOC_EXPORT extern DescriptorProtoDefaultTypeInternal _DescriptorProto_default_instance_; -class DescriptorProto_ExtensionRange; -class DescriptorProto_ExtensionRangeDefaultTypeInternal; -LIBPROTOC_EXPORT extern DescriptorProto_ExtensionRangeDefaultTypeInternal _DescriptorProto_ExtensionRange_default_instance_; -class DescriptorProto_ReservedRange; -class DescriptorProto_ReservedRangeDefaultTypeInternal; -LIBPROTOC_EXPORT extern DescriptorProto_ReservedRangeDefaultTypeInternal _DescriptorProto_ReservedRange_default_instance_; -class EnumDescriptorProto; -class EnumDescriptorProtoDefaultTypeInternal; -LIBPROTOC_EXPORT extern EnumDescriptorProtoDefaultTypeInternal _EnumDescriptorProto_default_instance_; -class EnumOptions; -class EnumOptionsDefaultTypeInternal; -LIBPROTOC_EXPORT extern EnumOptionsDefaultTypeInternal _EnumOptions_default_instance_; -class EnumValueDescriptorProto; -class EnumValueDescriptorProtoDefaultTypeInternal; -LIBPROTOC_EXPORT extern EnumValueDescriptorProtoDefaultTypeInternal _EnumValueDescriptorProto_default_instance_; -class EnumValueOptions; -class EnumValueOptionsDefaultTypeInternal; -LIBPROTOC_EXPORT extern EnumValueOptionsDefaultTypeInternal _EnumValueOptions_default_instance_; -class FieldDescriptorProto; -class FieldDescriptorProtoDefaultTypeInternal; -LIBPROTOC_EXPORT extern FieldDescriptorProtoDefaultTypeInternal _FieldDescriptorProto_default_instance_; -class FieldOptions; -class FieldOptionsDefaultTypeInternal; -LIBPROTOC_EXPORT extern FieldOptionsDefaultTypeInternal _FieldOptions_default_instance_; -class FileDescriptorProto; -class FileDescriptorProtoDefaultTypeInternal; -LIBPROTOC_EXPORT extern FileDescriptorProtoDefaultTypeInternal _FileDescriptorProto_default_instance_; -class FileDescriptorSet; -class FileDescriptorSetDefaultTypeInternal; -LIBPROTOC_EXPORT extern FileDescriptorSetDefaultTypeInternal _FileDescriptorSet_default_instance_; -class FileOptions; -class FileOptionsDefaultTypeInternal; -LIBPROTOC_EXPORT extern FileOptionsDefaultTypeInternal _FileOptions_default_instance_; -class GeneratedCodeInfo; -class GeneratedCodeInfoDefaultTypeInternal; -LIBPROTOC_EXPORT extern GeneratedCodeInfoDefaultTypeInternal _GeneratedCodeInfo_default_instance_; -class GeneratedCodeInfo_Annotation; -class GeneratedCodeInfo_AnnotationDefaultTypeInternal; -LIBPROTOC_EXPORT extern GeneratedCodeInfo_AnnotationDefaultTypeInternal _GeneratedCodeInfo_Annotation_default_instance_; -class MessageOptions; -class MessageOptionsDefaultTypeInternal; -LIBPROTOC_EXPORT extern MessageOptionsDefaultTypeInternal _MessageOptions_default_instance_; -class MethodDescriptorProto; -class MethodDescriptorProtoDefaultTypeInternal; -LIBPROTOC_EXPORT extern MethodDescriptorProtoDefaultTypeInternal _MethodDescriptorProto_default_instance_; -class MethodOptions; -class MethodOptionsDefaultTypeInternal; -LIBPROTOC_EXPORT extern MethodOptionsDefaultTypeInternal _MethodOptions_default_instance_; -class OneofDescriptorProto; -class OneofDescriptorProtoDefaultTypeInternal; -LIBPROTOC_EXPORT extern OneofDescriptorProtoDefaultTypeInternal _OneofDescriptorProto_default_instance_; -class OneofOptions; -class OneofOptionsDefaultTypeInternal; -LIBPROTOC_EXPORT extern OneofOptionsDefaultTypeInternal _OneofOptions_default_instance_; -class ServiceDescriptorProto; -class ServiceDescriptorProtoDefaultTypeInternal; -LIBPROTOC_EXPORT extern ServiceDescriptorProtoDefaultTypeInternal _ServiceDescriptorProto_default_instance_; -class ServiceOptions; -class ServiceOptionsDefaultTypeInternal; -LIBPROTOC_EXPORT extern ServiceOptionsDefaultTypeInternal _ServiceOptions_default_instance_; -class SourceCodeInfo; -class SourceCodeInfoDefaultTypeInternal; -LIBPROTOC_EXPORT extern SourceCodeInfoDefaultTypeInternal _SourceCodeInfo_default_instance_; -class SourceCodeInfo_Location; -class SourceCodeInfo_LocationDefaultTypeInternal; -LIBPROTOC_EXPORT extern SourceCodeInfo_LocationDefaultTypeInternal _SourceCodeInfo_Location_default_instance_; -class UninterpretedOption; -class UninterpretedOptionDefaultTypeInternal; -LIBPROTOC_EXPORT extern UninterpretedOptionDefaultTypeInternal _UninterpretedOption_default_instance_; -class UninterpretedOption_NamePart; -class UninterpretedOption_NamePartDefaultTypeInternal; -LIBPROTOC_EXPORT extern UninterpretedOption_NamePartDefaultTypeInternal _UninterpretedOption_NamePart_default_instance_; namespace compiler { class CodeGeneratorRequest; class CodeGeneratorRequestDefaultTypeInternal; @@ -142,8 +67,9 @@ struct LIBPROTOC_EXPORT TableStruct { static const ::google::protobuf::internal::AuxillaryParseTableField aux[]; static const ::google::protobuf::internal::ParseTable schema[]; static const ::google::protobuf::uint32 offsets[]; + static const ::google::protobuf::internal::FieldMetadata field_metadata[]; + static const ::google::protobuf::internal::SerializationTable serialization_table[]; static void InitDefaultsImpl(); - static void Shutdown(); }; void LIBPROTOC_EXPORT AddDescriptors(); void LIBPROTOC_EXPORT InitDefaults(); @@ -162,11 +88,24 @@ class LIBPROTOC_EXPORT Version : public ::google::protobuf::Message /* @@protoc_ CopyFrom(from); return *this; } + #if LANG_CXX11 + Version(Version&& from) noexcept + : Version() { + *this = ::std::move(from); + } + inline Version& operator=(Version&& from) noexcept { + if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { + if (this != &from) InternalSwap(&from); + } else { + CopyFrom(from); + } + return *this; + } + #endif inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } @@ -182,6 +121,9 @@ class LIBPROTOC_EXPORT Version : public ::google::protobuf::Message /* @@protoc_ 0; void Swap(Version* other); + friend void swap(Version& a, Version& b) { + a.Swap(&b); + } // implements Message ---------------------------------------------- @@ -292,11 +234,24 @@ class LIBPROTOC_EXPORT CodeGeneratorRequest : public ::google::protobuf::Message CopyFrom(from); return *this; } + #if LANG_CXX11 + CodeGeneratorRequest(CodeGeneratorRequest&& from) noexcept + : CodeGeneratorRequest() { + *this = ::std::move(from); + } + inline CodeGeneratorRequest& operator=(CodeGeneratorRequest&& from) noexcept { + if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { + if (this != &from) InternalSwap(&from); + } else { + CopyFrom(from); + } + return *this; + } + #endif inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } @@ -312,6 +267,9 @@ class LIBPROTOC_EXPORT CodeGeneratorRequest : public ::google::protobuf::Message 1; void Swap(CodeGeneratorRequest* other); + friend void swap(CodeGeneratorRequest& a, CodeGeneratorRequest& b) { + a.Swap(&b); + } // implements Message ---------------------------------------------- @@ -440,11 +398,24 @@ class LIBPROTOC_EXPORT CodeGeneratorResponse_File : public ::google::protobuf::M CopyFrom(from); return *this; } + #if LANG_CXX11 + CodeGeneratorResponse_File(CodeGeneratorResponse_File&& from) noexcept + : CodeGeneratorResponse_File() { + *this = ::std::move(from); + } + inline CodeGeneratorResponse_File& operator=(CodeGeneratorResponse_File&& from) noexcept { + if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { + if (this != &from) InternalSwap(&from); + } else { + CopyFrom(from); + } + return *this; + } + #endif inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } @@ -460,6 +431,9 @@ class LIBPROTOC_EXPORT CodeGeneratorResponse_File : public ::google::protobuf::M 2; void Swap(CodeGeneratorResponse_File* other); + friend void swap(CodeGeneratorResponse_File& a, CodeGeneratorResponse_File& b) { + a.Swap(&b); + } // implements Message ---------------------------------------------- @@ -576,11 +550,24 @@ class LIBPROTOC_EXPORT CodeGeneratorResponse : public ::google::protobuf::Messag CopyFrom(from); return *this; } + #if LANG_CXX11 + CodeGeneratorResponse(CodeGeneratorResponse&& from) noexcept + : CodeGeneratorResponse() { + *this = ::std::move(from); + } + inline CodeGeneratorResponse& operator=(CodeGeneratorResponse&& from) noexcept { + if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { + if (this != &from) InternalSwap(&from); + } else { + CopyFrom(from); + } + return *this; + } + #endif inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { return _internal_metadata_.unknown_fields(); } - inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { return _internal_metadata_.mutable_unknown_fields(); } @@ -596,6 +583,9 @@ class LIBPROTOC_EXPORT CodeGeneratorResponse : public ::google::protobuf::Messag 3; void Swap(CodeGeneratorResponse* other); + friend void swap(CodeGeneratorResponse& a, CodeGeneratorResponse& b) { + a.Swap(&b); + } // implements Message ---------------------------------------------- @@ -684,6 +674,10 @@ class LIBPROTOC_EXPORT CodeGeneratorResponse : public ::google::protobuf::Messag // =================================================================== #if !PROTOBUF_INLINE_NOT_IN_HEADERS +#ifdef __GNUC__ + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wstrict-aliasing" +#endif // __GNUC__ // Version // optional int32 major = 1; @@ -1002,9 +996,10 @@ inline void CodeGeneratorRequest::clear_compiler_version() { clear_has_compiler_version(); } inline const ::google::protobuf::compiler::Version& CodeGeneratorRequest::compiler_version() const { + const ::google::protobuf::compiler::Version* p = compiler_version_; // @@protoc_insertion_point(field_get:google.protobuf.compiler.CodeGeneratorRequest.compiler_version) - return compiler_version_ != NULL ? *compiler_version_ - : *::google::protobuf::compiler::Version::internal_default_instance(); + return p != NULL ? *p : *reinterpret_cast<const ::google::protobuf::compiler::Version*>( + &::google::protobuf::compiler::_Version_default_instance_); } inline ::google::protobuf::compiler::Version* CodeGeneratorRequest::mutable_compiler_version() { set_has_compiler_version(); @@ -1322,6 +1317,9 @@ CodeGeneratorResponse::file() const { return file_; } +#ifdef __GNUC__ + #pragma GCC diagnostic pop +#endif // __GNUC__ #endif // !PROTOBUF_INLINE_NOT_IN_HEADERS // ------------------------------------------------------------------- diff --git a/src/google/protobuf/compiler/plugin.proto b/src/google/protobuf/compiler/plugin.proto index f04dc73c..5b557452 100644 --- a/src/google/protobuf/compiler/plugin.proto +++ b/src/google/protobuf/compiler/plugin.proto @@ -91,6 +91,7 @@ message CodeGeneratorRequest { // The version number of protocol compiler. optional Version compiler_version = 3; + } // The plugin writes an encoded CodeGeneratorResponse to stdout. diff --git a/src/google/protobuf/compiler/python/python_generator.cc b/src/google/protobuf/compiler/python/python_generator.cc index 21a7e158..97769835 100644 --- a/src/google/protobuf/compiler/python/python_generator.cc +++ b/src/google/protobuf/compiler/python/python_generator.cc @@ -63,11 +63,12 @@ #include <google/protobuf/stubs/common.h> #include <google/protobuf/stubs/stringprintf.h> #include <google/protobuf/io/printer.h> -#include <google/protobuf/descriptor.h> #include <google/protobuf/io/zero_copy_stream.h> +#include <google/protobuf/descriptor.h> #include <google/protobuf/stubs/strutil.h> #include <google/protobuf/stubs/substitute.h> + namespace google { namespace protobuf { namespace compiler { @@ -75,12 +76,21 @@ namespace python { namespace { +// Reimplemented here because we can't bring in +// absl/strings/string_view_utils.h because it needs C++11. +bool StrStartsWith(StringPiece sp, StringPiece x) { + return sp.size() >= x.size() && sp.substr(0, x.size()) == x; +} +bool StrEndsWith(StringPiece sp, StringPiece x) { + return sp.size() >= x.size() && sp.substr(sp.size() - x.size()) == x; +} + // Returns a copy of |filename| with any trailing ".protodevel" or ".proto // suffix stripped. // TODO(robinson): Unify with copy in compiler/cpp/internal/helpers.cc. string StripProto(const string& filename) { - const char* suffix = HasSuffixString(filename, ".protodevel") - ? ".protodevel" : ".proto"; + const char* suffix = + StrEndsWith(filename, ".protodevel") ? ".protodevel" : ".proto"; return StripSuffixString(filename, suffix); } @@ -350,7 +360,9 @@ bool Generator::Generate(const FileDescriptor* file, // can only be successfully parsed after we register corresponding // extensions. Therefore we parse all options again here to recognize // custom options that may be unknown when we define the descriptors. + // This does not apply to services because they are not used by extensions. FixAllDescriptorOptions(); + PrintServiceDescriptors(); if (HasGenericServices(file)) { PrintServices(); } @@ -562,9 +574,16 @@ void Generator::PrintMessageDescriptors() const { } } -void Generator::PrintServices() const { +void Generator::PrintServiceDescriptors() const { for (int i = 0; i < file_->service_count(); ++i) { PrintServiceDescriptor(*file_->service(i)); + AddServiceToFileDescriptor(*file_->service(i)); + printer_->Print("\n"); + } +} + +void Generator::PrintServices() const { + for (int i = 0; i < file_->service_count(); ++i) { PrintServiceClass(*file_->service(i)); PrintServiceStub(*file_->service(i)); printer_->Print("\n"); @@ -628,7 +647,10 @@ void Generator::PrintServiceDescriptor( } printer_->Outdent(); - printer_->Print("])\n\n"); + printer_->Print("])\n"); + printer_->Print("_sym_db.RegisterServiceDescriptor($name$)\n", "name", + service_name); + printer_->Print("\n"); } @@ -887,6 +909,18 @@ void Generator::AddMessageToFileDescriptor(const Descriptor& descriptor) const { printer_->Print(m, file_descriptor_template); } +void Generator::AddServiceToFileDescriptor( + const ServiceDescriptor& descriptor) const { + std::map<string, string> m; + m["descriptor_name"] = kDescriptorKey; + m["service_name"] = descriptor.name(); + m["service_descriptor_name"] = ModuleLevelServiceDescriptorName(descriptor); + const char file_descriptor_template[] = + "$descriptor_name$.services_by_name['$service_name$'] = " + "$service_descriptor_name$\n"; + printer_->Print(m, file_descriptor_template); +} + void Generator::AddEnumToFileDescriptor( const EnumDescriptor& descriptor) const { std::map<string, string> m; diff --git a/src/google/protobuf/compiler/python/python_generator.h b/src/google/protobuf/compiler/python/python_generator.h index 594260af..2b5a028b 100644 --- a/src/google/protobuf/compiler/python/python_generator.h +++ b/src/google/protobuf/compiler/python/python_generator.h @@ -112,6 +112,7 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator { void AddMessageToFileDescriptor(const Descriptor& descriptor) const; void AddEnumToFileDescriptor(const EnumDescriptor& descriptor) const; void AddExtensionToFileDescriptor(const FieldDescriptor& descriptor) const; + void AddServiceToFileDescriptor(const ServiceDescriptor& descriptor) const; string FieldReferencingExpression(const Descriptor* containing_type, const FieldDescriptor& field, const string& python_dict_name) const; @@ -126,11 +127,12 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator { void FixForeignFieldsInNestedExtensions(const Descriptor& descriptor) const; void PrintServices() const; + void PrintServiceDescriptors() const; void PrintServiceDescriptor(const ServiceDescriptor& descriptor) const; void PrintServiceClass(const ServiceDescriptor& descriptor) const; void PrintServiceStub(const ServiceDescriptor& descriptor) const; void PrintDescriptorKeyAndModuleName( - const ServiceDescriptor& descriptor) const ; + const ServiceDescriptor& descriptor) const; void PrintEnumValueDescriptor(const EnumValueDescriptor& descriptor) const; string OptionsValue(const string& class_name, diff --git a/src/google/protobuf/compiler/subprocess.cc b/src/google/protobuf/compiler/subprocess.cc index 933450fa..afe48774 100644 --- a/src/google/protobuf/compiler/subprocess.cc +++ b/src/google/protobuf/compiler/subprocess.cc @@ -47,7 +47,6 @@ #include <google/protobuf/message.h> #include <google/protobuf/stubs/substitute.h> - namespace google { namespace protobuf { namespace compiler { @@ -347,7 +346,6 @@ void Subprocess::Start(const string& program, SearchMode search_mode) { bool Subprocess::Communicate(const Message& input, Message* output, string* error) { - GOOGLE_CHECK_NE(child_stdin_, -1) << "Must call Start() first."; // The "sighandler_t" typedef is GNU-specific, so define our own. diff --git a/src/google/protobuf/compiler/subprocess.h b/src/google/protobuf/compiler/subprocess.h index 25138631..9d980b06 100644 --- a/src/google/protobuf/compiler/subprocess.h +++ b/src/google/protobuf/compiler/subprocess.h @@ -44,7 +44,6 @@ #include <string> - namespace google { namespace protobuf { diff --git a/src/google/protobuf/compiler/zip_output_unittest.sh b/src/google/protobuf/compiler/zip_output_unittest.sh index 6fc7136d..8cd80e38 100755 --- a/src/google/protobuf/compiler/zip_output_unittest.sh +++ b/src/google/protobuf/compiler/zip_output_unittest.sh @@ -41,6 +41,8 @@ fail() { TEST_TMPDIR=. PROTOC=./protoc +JAR=jar +UNZIP=unzip echo ' syntax = "proto2"; @@ -57,35 +59,32 @@ $PROTOC \ || fail 'protoc failed.' echo "Testing output to zip..." -if unzip -h > /dev/null; then - unzip -t $TEST_TMPDIR/testzip.zip > $TEST_TMPDIR/testzip.list || fail 'unzip failed.' +$UNZIP -t $TEST_TMPDIR/testzip.zip > $TEST_TMPDIR/testzip.list || fail 'unzip failed.' - grep 'testing: testzip\.pb\.cc *OK$' $TEST_TMPDIR/testzip.list > /dev/null \ - || fail 'testzip.pb.cc not found in output zip.' - grep 'testing: testzip\.pb\.h *OK$' $TEST_TMPDIR/testzip.list > /dev/null \ - || fail 'testzip.pb.h not found in output zip.' - grep 'testing: testzip_pb2\.py *OK$' $TEST_TMPDIR/testzip.list > /dev/null \ - || fail 'testzip_pb2.py not found in output zip.' - grep -i 'manifest' $TEST_TMPDIR/testzip.list > /dev/null \ - && fail 'Zip file contained manifest.' -else - echo "Warning: 'unzip' command not available. Skipping test." -fi +grep 'testing: testzip\.pb\.cc *OK$' $TEST_TMPDIR/testzip.list > /dev/null \ + || fail 'testzip.pb.cc not found in output zip.' +grep 'testing: testzip\.pb\.h *OK$' $TEST_TMPDIR/testzip.list > /dev/null \ + || fail 'testzip.pb.h not found in output zip.' +grep 'testing: testzip_pb2\.py *OK$' $TEST_TMPDIR/testzip.list > /dev/null \ + || fail 'testzip_pb2.py not found in output zip.' +grep -i 'manifest' $TEST_TMPDIR/testzip.list > /dev/null \ + && fail 'Zip file contained manifest.' echo "Testing output to jar..." -if jar c $TEST_TMPDIR/testzip.proto > /dev/null; then - jar tf $TEST_TMPDIR/testzip.jar > $TEST_TMPDIR/testzip.list || fail 'jar failed.' +$JAR tf $TEST_TMPDIR/testzip.jar > $TEST_TMPDIR/testzip.list || fail 'jar failed.' - grep '^test/jar/Foo\.java$' $TEST_TMPDIR/testzip.list > /dev/null \ - || fail 'Foo.java not found in output jar.' - grep '^test/jar/Bar\.java$' $TEST_TMPDIR/testzip.list > /dev/null \ - || fail 'Bar.java not found in output jar.' - grep '^test/jar/Outer\.java$' $TEST_TMPDIR/testzip.list > /dev/null \ - || fail 'Outer.java not found in output jar.' - grep '^META-INF/MANIFEST\.MF$' $TEST_TMPDIR/testzip.list > /dev/null \ - || fail 'Manifest not found in output jar.' -else - echo "Warning: 'jar' command not available. Skipping test." +# Check that -interface.jar timestamps are normalized: +if [[ "$(TZ=UTC $JAR tvf $TEST_TMPDIR/testzip.jar)" != *'Tue Jan 01 00:00:00 UTC 1980'* ]]; then + fail 'Zip did not contain normalized timestamps' fi +grep '^test/jar/Foo\.java$' $TEST_TMPDIR/testzip.list > /dev/null \ + || fail 'Foo.java not found in output jar.' +grep '^test/jar/Bar\.java$' $TEST_TMPDIR/testzip.list > /dev/null \ + || fail 'Bar.java not found in output jar.' +grep '^test/jar/Outer\.java$' $TEST_TMPDIR/testzip.list > /dev/null \ + || fail 'Outer.java not found in output jar.' +grep '^META-INF/MANIFEST\.MF$' $TEST_TMPDIR/testzip.list > /dev/null \ + || fail 'Manifest not found in output jar.' + echo PASS diff --git a/src/google/protobuf/compiler/zip_writer.cc b/src/google/protobuf/compiler/zip_writer.cc index 458cced2..1799af6a 100644 --- a/src/google/protobuf/compiler/zip_writer.cc +++ b/src/google/protobuf/compiler/zip_writer.cc @@ -70,6 +70,10 @@ namespace google { namespace protobuf { namespace compiler { +// January 1, 1980 as a DOS date. +// see https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx +static const uint16 kDosEpoch = 1 << 5 | 1; + static const uint32 kCRC32Table[256] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, @@ -154,7 +158,7 @@ bool ZipWriter::Write(const string& filename, const string& contents) { WriteShort(&output, 0); // flags WriteShort(&output, 0); // compression method: stored WriteShort(&output, 0); // last modified time - WriteShort(&output, 0); // last modified date + WriteShort(&output, kDosEpoch); // last modified date output.WriteLittleEndian32(info.crc32); // crc-32 output.WriteLittleEndian32(info.size); // compressed size output.WriteLittleEndian32(info.size); // uncompressed size @@ -185,7 +189,7 @@ bool ZipWriter::WriteDirectory() { WriteShort(&output, 0); // flags WriteShort(&output, 0); // compression method: stored WriteShort(&output, 0); // last modified time - WriteShort(&output, 0); // last modified date + WriteShort(&output, kDosEpoch); // last modified date output.WriteLittleEndian32(crc32); // crc-32 output.WriteLittleEndian32(size); // compressed size output.WriteLittleEndian32(size); // uncompressed size |