Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Unified Diff: third_party/protobuf/src/google/protobuf/compiler/command_line_interface.cc

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Make chrome settings proto generated file a component Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/protobuf/src/google/protobuf/compiler/command_line_interface.cc
diff --git a/third_party/protobuf/src/google/protobuf/compiler/command_line_interface.cc b/third_party/protobuf/src/google/protobuf/compiler/command_line_interface.cc
index fcad6b61652b5e69ca388063a3eb6d7293425d98..bef0fe5a9d86042cc843908c6c762e3b66a8d9cc 100644
--- a/third_party/protobuf/src/google/protobuf/compiler/command_line_interface.cc
+++ b/third_party/protobuf/src/google/protobuf/compiler/command_line_interface.cc
@@ -50,9 +50,7 @@
#include <iostream>
#include <ctype.h>
-#ifdef GOOGLE_PROTOBUF_ARCH_SPARC
#include <limits.h> //For PATH_MAX
-#endif
#include <memory>
#ifndef _SHARED_PTR_H
@@ -175,7 +173,8 @@ bool VerifyDirectoryExists(const string& path) {
// directories listed in |filename|.
bool TryCreateParentDirectory(const string& prefix, const string& filename) {
// Recursively create parent directories to the output file.
- vector<string> parts = Split(filename, "/", true);
+ std::vector<string> parts =
+ Split(filename, "/", true);
string path_so_far = prefix;
for (int i = 0; i < parts.size() - 1; i++) {
path_so_far += parts[i];
@@ -264,6 +263,12 @@ void AddDefaultProtoPaths(vector<pair<string, string> >* paths) {
return;
}
}
+
+string PluginName(const string& plugin_prefix, const string& directive) {
+ // Assuming the directive starts with "--" and ends with "_out" or "_opt",
+ // 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.
@@ -340,7 +345,7 @@ class CommandLineInterface::ErrorPrinter : public MultiFileErrorCollector,
// them all to disk on demand.
class CommandLineInterface::GeneratorContextImpl : public GeneratorContext {
public:
- GeneratorContextImpl(const vector<const FileDescriptor*>& parsed_files);
+ GeneratorContextImpl(const std::vector<const FileDescriptor*>& parsed_files);
~GeneratorContextImpl();
// Write all files in the directory to disk at the given output location,
@@ -356,14 +361,14 @@ class CommandLineInterface::GeneratorContextImpl : public GeneratorContext {
void AddJarManifest();
// Get name of all output files.
- void GetOutputFilenames(vector<string>* output_filenames);
+ void GetOutputFilenames(std::vector<string>* output_filenames);
// implements GeneratorContext --------------------------------------
io::ZeroCopyOutputStream* Open(const string& filename);
io::ZeroCopyOutputStream* OpenForAppend(const string& filename);
io::ZeroCopyOutputStream* OpenForInsert(
const string& filename, const string& insertion_point);
- void ListParsedFiles(vector<const FileDescriptor*>* output) {
+ void ListParsedFiles(std::vector<const FileDescriptor*>* output) {
*output = parsed_files_;
}
@@ -372,8 +377,8 @@ class CommandLineInterface::GeneratorContextImpl : public GeneratorContext {
// map instead of hash_map so that files are written in order (good when
// writing zips).
- map<string, string*> files_;
- const vector<const FileDescriptor*>& parsed_files_;
+ std::map<string, string*> files_;
+ const std::vector<const FileDescriptor*>& parsed_files_;
bool had_error_;
};
@@ -410,7 +415,7 @@ class CommandLineInterface::MemoryOutputStream
// -------------------------------------------------------------------
CommandLineInterface::GeneratorContextImpl::GeneratorContextImpl(
- const vector<const FileDescriptor*>& parsed_files)
+ const std::vector<const FileDescriptor*>& parsed_files)
: parsed_files_(parsed_files),
had_error_(false) {
}
@@ -429,7 +434,7 @@ bool CommandLineInterface::GeneratorContextImpl::WriteAllToDisk(
return false;
}
- for (map<string, string*>::const_iterator iter = files_.begin();
+ for (std::map<string, string*>::const_iterator iter = files_.begin();
iter != files_.end(); ++iter) {
const string& relative_filename = iter->first;
const char* data = iter->second->data();
@@ -517,7 +522,7 @@ bool CommandLineInterface::GeneratorContextImpl::WriteAllToZip(
io::FileOutputStream stream(file_descriptor);
ZipWriter zip_writer(&stream);
- for (map<string, string*>::const_iterator iter = files_.begin();
+ for (std::map<string, string*>::const_iterator iter = files_.begin();
iter != files_.end(); ++iter) {
zip_writer.Write(iter->first, *iter->second);
}
@@ -546,8 +551,8 @@ void CommandLineInterface::GeneratorContextImpl::AddJarManifest() {
}
void CommandLineInterface::GeneratorContextImpl::GetOutputFilenames(
- vector<string>* output_filenames) {
- for (map<string, string*>::iterator iter = files_.begin();
+ std::vector<string>* output_filenames) {
+ for (std::map<string, string*>::iterator iter = files_.begin();
iter != files_.end(); ++iter) {
output_filenames->push_back(iter->first);
}
@@ -614,7 +619,7 @@ CommandLineInterface::MemoryOutputStream::~MemoryOutputStream() {
} else {
// This was an OpenForInsert().
- // If the data doens't end with a clean line break, add one.
+ // If the data doesn't end with a clean line break, add one.
if (!data_.empty() && data_[data_.size() - 1] != '\n') {
data_.push_back('\n');
}
@@ -641,18 +646,23 @@ CommandLineInterface::MemoryOutputStream::~MemoryOutputStream() {
return;
}
- // Seek backwards to the beginning of the line, which is where we will
- // insert the data. Note that this has the effect of pushing the insertion
- // point down, so the data is inserted before it. This is intentional
- // because it means that multiple insertions at the same point will end
- // up in the expected order in the final output.
- pos = target->find_last_of('\n', pos);
- if (pos == string::npos) {
- // Insertion point is on the first line.
- pos = 0;
+ if ((pos > 3) && (target->substr(pos - 3, 2) == "/*")) {
+ // Support for inline "/* @@protoc_insertion_point() */"
+ pos = pos - 3;
} else {
- // Advance to character after '\n'.
- ++pos;
+ // Seek backwards to the beginning of the line, which is where we will
+ // insert the data. Note that this has the effect of pushing the
+ // insertion point down, so the data is inserted before it. This is
+ // intentional because it means that multiple insertions at the same point
+ // will end up in the expected order in the final output.
+ pos = target->find_last_of('\n', pos);
+ if (pos == string::npos) {
+ // Insertion point is on the first line.
+ pos = 0;
+ } else {
+ // Advance to character after '\n'.
+ ++pos;
+ }
}
// Extract indent.
@@ -701,6 +711,7 @@ CommandLineInterface::CommandLineInterface()
: mode_(MODE_COMPILE),
print_mode_(PRINT_NONE),
error_format_(ERROR_FORMAT_GCC),
+ direct_dependencies_explicitly_set_(false),
imports_in_descriptor_set_(false),
source_info_in_descriptor_set_(false),
disallow_services_(false),
@@ -764,7 +775,7 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) {
ErrorPrinter error_collector(error_format_, &source_tree);
Importer importer(&source_tree, &error_collector);
- vector<const FileDescriptor*> parsed_files;
+ std::vector<const FileDescriptor*> parsed_files;
// Parse each file.
for (int i = 0; i < input_files_.size(); i++) {
@@ -781,6 +792,24 @@ int CommandLineInterface::Run(int argc, const char* const argv[]) {
"--disallow_services was used." << endl;
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;
+ cerr << parsed_file->name()
+ << ": File is imported but not declared in "
+ << "--direct_dependencies: "
+ << parsed_file->dependency(i)->name() << std::endl;
+ }
+ }
+ if (indirect_imports) {
+ return 1;
+ }
+ }
}
// We construct a separate GeneratorContext for each output location. Note
@@ -894,6 +923,7 @@ void CommandLineInterface::Clear() {
executable_name_.clear();
proto_path_.clear();
input_files_.clear();
+ direct_dependencies_.clear();
output_directives_.clear();
codec_type_.clear();
descriptor_set_name_.clear();
@@ -904,6 +934,7 @@ void CommandLineInterface::Clear() {
imports_in_descriptor_set_ = false;
source_info_in_descriptor_set_ = false;
disallow_services_ = false;
+ direct_dependencies_explicitly_set_ = false;
}
bool CommandLineInterface::MakeInputsBeProtoPathRelative(
@@ -954,7 +985,7 @@ CommandLineInterface::ParseArgumentStatus
CommandLineInterface::ParseArguments(int argc, const char* const argv[]) {
executable_name_ = argv[0];
- vector<string> arguments;
+ std::vector<string> arguments;
for (int i = 1; i < argc; ++i) {
arguments.push_back(argv[i]);
}
@@ -983,12 +1014,24 @@ CommandLineInterface::ParseArguments(int argc, const char* const argv[]) {
return status;
}
+ // Make sure each plugin option has a matching plugin output.
+ for (map<string, string>::const_iterator i = plugin_parameters_.begin();
+ i != plugin_parameters_.end(); ++i) {
+ if (plugins_.find(i->first) == plugins_.end()) {
+ std::cerr << "Unknown flag: "
+ // strip prefix + "gen-" and add back "_opt"
+ << "--" + i->first.substr(plugin_prefix_.size() + 4) + "_opt"
+ << std::endl;
+ return PARSE_ARGUMENT_FAIL;
+ }
+ }
+
// If no --proto_path was given, use the current working directory.
if (proto_path_.empty()) {
// Don't use make_pair as the old/default standard library on Solaris
// doesn't support it without explicit template parameters, which are
// incompatible with C++0x's make_pair.
- proto_path_.push_back(pair<string, string>("", "."));
+ proto_path_.push_back(std::pair<string, string>("", "."));
}
// Check some errror cases.
@@ -1111,7 +1154,7 @@ CommandLineInterface::InterpretArgument(const string& name,
// Java's -classpath (and some other languages) delimits path components
// with colons. Let's accept that syntax too just to make things more
// intuitive.
- vector<string> parts = Split(
+ std::vector<string> parts = Split(
value, kPathSeparator, true);
for (int i = 0; i < parts.size(); i++) {
@@ -1136,16 +1179,36 @@ CommandLineInterface::InterpretArgument(const string& name,
// Make sure disk path exists, warn otherwise.
if (access(disk_path.c_str(), F_OK) < 0) {
- std::cerr << disk_path << ": warning: directory does not exist."
- << std::endl;
+ // Try the original path; it may have just happed to have a '=' in it.
+ if (access(parts[i].c_str(), F_OK) < 0) {
+ cerr << disk_path << ": warning: directory does not exist." << endl;
+ } else {
+ virtual_path = "";
+ disk_path = parts[i];
+ }
}
// Don't use make_pair as the old/default standard library on Solaris
// doesn't support it without explicit template parameters, which are
// incompatible with C++0x's make_pair.
- proto_path_.push_back(pair<string, string>(virtual_path, disk_path));
+ proto_path_.push_back(std::pair<string, string>(virtual_path, disk_path));
+ }
+
+ } else if (name == "--direct_dependencies") {
+ if (direct_dependencies_explicitly_set_) {
+ std::cerr << name << " may only be passed once. To specify multiple "
+ "direct dependencies, pass them all as a single "
+ "parameter separated by ':'."
+ << std::endl;
+ return PARSE_ARGUMENT_FAIL;
}
+ direct_dependencies_explicitly_set_ = true;
+ std::vector<string> direct = Split(
+ value, ":", true);
+ GOOGLE_DCHECK(direct_dependencies_.empty());
+ direct_dependencies_.insert(direct.begin(), direct.end());
+
} else if (name == "-o" || name == "--descriptor_set_out") {
if (!descriptor_set_name_.empty()) {
std::cerr << name << " may only be passed once." << std::endl;
@@ -1293,15 +1356,22 @@ CommandLineInterface::InterpretArgument(const string& name,
(plugin_prefix_.empty() || !HasSuffixString(name, "_out"))) {
// Check if it's a generator option flag.
generator_info = FindOrNull(generators_by_option_name_, name);
- if (generator_info == NULL) {
- std::cerr << "Unknown flag: " << name << std::endl;
- return PARSE_ARGUMENT_FAIL;
- } else {
+ if (generator_info != NULL) {
string* parameters = &generator_parameters_[generator_info->flag_name];
if (!parameters->empty()) {
parameters->append(",");
}
parameters->append(value);
+ } else if (HasPrefixString(name, "--") && HasSuffixString(name, "_opt")) {
+ string* parameters =
+ &plugin_parameters_[PluginName(plugin_prefix_, name)];
+ if (!parameters->empty()) {
+ parameters->append(",");
+ }
+ parameters->append(value);
+ } else {
+ std::cerr << "Unknown flag: " << name << std::endl;
+ return PARSE_ARGUMENT_FAIL;
}
} else {
// It's an output flag. Add it to the output directives.
@@ -1409,7 +1479,7 @@ void CommandLineInterface::PrintHelpText() {
}
bool CommandLineInterface::GenerateOutput(
- const vector<const FileDescriptor*>& parsed_files,
+ const std::vector<const FileDescriptor*>& parsed_files,
const OutputDirective& output_directive,
GeneratorContext* generator_context) {
// Call the generator.
@@ -1420,12 +1490,16 @@ bool CommandLineInterface::GenerateOutput(
HasSuffixString(output_directive.name, "_out"))
<< "Bad name for plugin generator: " << output_directive.name;
- // Strip the "--" and "_out" and add the plugin prefix.
- string plugin_name = plugin_prefix_ + "gen-" +
- output_directive.name.substr(2, output_directive.name.size() - 6);
-
+ string plugin_name = PluginName(plugin_prefix_ , output_directive.name);
+ string parameters = output_directive.parameter;
+ if (!plugin_parameters_[plugin_name].empty()) {
+ if (!parameters.empty()) {
+ parameters.append(",");
+ }
+ parameters.append(plugin_parameters_[plugin_name]);
+ }
if (!GeneratePluginOutput(parsed_files, plugin_name,
- output_directive.parameter,
+ parameters,
generator_context, &error)) {
std::cerr << output_directive.name << ": " << error << std::endl;
return false;
@@ -1439,24 +1513,11 @@ bool CommandLineInterface::GenerateOutput(
}
parameters.append(generator_parameters_[output_directive.name]);
}
- if (output_directive.generator->HasGenerateAll()) {
- if (!output_directive.generator->GenerateAll(
- parsed_files, parameters, generator_context, &error)) {
- // Generator returned an error.
- std::cerr << output_directive.name << ": "
- << ": " << error << std::endl;
- return false;
- }
- } else {
- for (int i = 0; i < parsed_files.size(); i++) {
- if (!output_directive.generator->Generate(parsed_files[i], parameters,
- generator_context, &error)) {
- // Generator returned an error.
- std::cerr << output_directive.name << ": " << parsed_files[i]->name()
- << ": " << error << std::endl;
- return false;
- }
- }
+ if (!output_directive.generator->GenerateAll(
+ parsed_files, parameters, generator_context, &error)) {
+ // Generator returned an error.
+ std::cerr << output_directive.name << ": " << error << std::endl;
+ return false;
}
}
@@ -1464,12 +1525,12 @@ bool CommandLineInterface::GenerateOutput(
}
bool CommandLineInterface::GenerateDependencyManifestFile(
- const vector<const FileDescriptor*>& parsed_files,
+ const std::vector<const FileDescriptor*>& parsed_files,
const GeneratorContextMap& output_directories,
DiskSourceTree* source_tree) {
FileDescriptorSet file_set;
- set<const FileDescriptor*> already_seen;
+ std::set<const FileDescriptor*> already_seen;
for (int i = 0; i < parsed_files.size(); i++) {
GetTransitiveDependencies(parsed_files[i],
false,
@@ -1478,12 +1539,12 @@ bool CommandLineInterface::GenerateDependencyManifestFile(
file_set.mutable_file());
}
- vector<string> output_filenames;
+ std::vector<string> output_filenames;
for (GeneratorContextMap::const_iterator iter = output_directories.begin();
iter != output_directories.end(); ++iter) {
const string& location = iter->first;
GeneratorContextImpl* directory = iter->second;
- vector<string> relative_output_filenames;
+ std::vector<string> relative_output_filenames;
directory->GetOutputFilenames(&relative_output_filenames);
for (int i = 0; i < relative_output_filenames.size(); i++) {
string output_filename = location + relative_output_filenames[i];
@@ -1536,7 +1597,7 @@ bool CommandLineInterface::GenerateDependencyManifestFile(
}
bool CommandLineInterface::GeneratePluginOutput(
- const vector<const FileDescriptor*>& parsed_files,
+ const std::vector<const FileDescriptor*>& parsed_files,
const string& plugin_name,
const string& parameter,
GeneratorContext* generator_context,
@@ -1549,7 +1610,7 @@ bool CommandLineInterface::GeneratePluginOutput(
request.set_parameter(parameter);
}
- set<const FileDescriptor*> already_seen;
+ std::set<const FileDescriptor*> already_seen;
for (int i = 0; i < parsed_files.size(); i++) {
request.add_file_to_generate(parsed_files[i]->name());
GetTransitiveDependencies(parsed_files[i],
@@ -1558,6 +1619,13 @@ bool CommandLineInterface::GeneratePluginOutput(
&already_seen, request.mutable_proto_file());
}
+ google::protobuf::compiler::Version* version =
+ request.mutable_compiler_version();
+ version->set_major(GOOGLE_PROTOBUF_VERSION / 1000000);
+ version->set_minor(GOOGLE_PROTOBUF_VERSION / 1000 % 1000);
+ version->set_patch(GOOGLE_PROTOBUF_VERSION % 1000);
+ version->set_suffix(GOOGLE_PROTOBUF_VERSION_SUFFIX);
+
// Invoke the plugin.
Subprocess subprocess;
@@ -1679,11 +1747,11 @@ bool CommandLineInterface::EncodeOrDecode(const DescriptorPool* pool) {
}
bool CommandLineInterface::WriteDescriptorSet(
- const vector<const FileDescriptor*> parsed_files) {
+ const std::vector<const FileDescriptor*> parsed_files) {
FileDescriptorSet file_set;
if (imports_in_descriptor_set_) {
- set<const FileDescriptor*> already_seen;
+ std::set<const FileDescriptor*> already_seen;
for (int i = 0; i < parsed_files.size(); i++) {
GetTransitiveDependencies(parsed_files[i],
true, // Include json_name
@@ -1691,7 +1759,7 @@ bool CommandLineInterface::WriteDescriptorSet(
&already_seen, file_set.mutable_file());
}
} else {
- set<const FileDescriptor*> already_seen;
+ std::set<const FileDescriptor*> already_seen;
for (int i = 0; i < parsed_files.size(); i++) {
if (!already_seen.insert(parsed_files[i]).second) {
continue;
@@ -1736,7 +1804,7 @@ void CommandLineInterface::GetTransitiveDependencies(
const FileDescriptor* file,
bool include_json_name,
bool include_source_code_info,
- set<const FileDescriptor*>* already_seen,
+ std::set<const FileDescriptor*>* already_seen,
RepeatedPtrField<FileDescriptorProto>* output) {
if (!already_seen->insert(file).second) {
// Already saw this file. Skip.
@@ -1795,11 +1863,11 @@ namespace {
// parameter will contain the direct children (when groups are ignored in the
// tree) of the given descriptor for the caller to traverse. The declaration
// order of the nested messages is also preserved.
-typedef pair<int, int> FieldRange;
-void GatherOccupiedFieldRanges(const Descriptor* descriptor,
- set<FieldRange>* ranges,
- vector<const Descriptor*>* nested_messages) {
- set<const Descriptor*> groups;
+typedef std::pair<int, int> FieldRange;
+void GatherOccupiedFieldRanges(
+ const Descriptor* descriptor, std::set<FieldRange>* ranges,
+ std::vector<const Descriptor*>* nested_messages) {
+ std::set<const Descriptor*> groups;
for (int i = 0; i < descriptor->field_count(); ++i) {
const FieldDescriptor* fd = descriptor->field(i);
ranges->insert(FieldRange(fd->number(), fd->number() + 1));
@@ -1831,11 +1899,11 @@ void GatherOccupiedFieldRanges(const Descriptor* descriptor,
// Actually prints the formatted free field numbers for given message name and
// occupied ranges.
void FormatFreeFieldNumbers(const string& name,
- const set<FieldRange>& ranges) {
+ const std::set<FieldRange>& ranges) {
string output;
StringAppendF(&output, "%-35s free:", name.c_str());
int next_free_number = 1;
- for (set<FieldRange>::const_iterator i = ranges.begin();
+ for (std::set<FieldRange>::const_iterator i = ranges.begin();
i != ranges.end(); ++i) {
// This happens when groups re-use parent field numbers, in which
// case we skip the FieldRange entirely.
@@ -1862,8 +1930,8 @@ void FormatFreeFieldNumbers(const string& name,
void CommandLineInterface::PrintFreeFieldNumbers(
const Descriptor* descriptor) {
- set<FieldRange> ranges;
- vector<const Descriptor*> nested_messages;
+ std::set<FieldRange> ranges;
+ std::vector<const Descriptor*> nested_messages;
GatherOccupiedFieldRanges(descriptor, &ranges, &nested_messages);
for (int i = 0; i < nested_messages.size(); ++i) {

Powered by Google App Engine
This is Rietveld 408576698