• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Author: kenton@google.com (Kenton Varda)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 
35 #include <google/protobuf/compiler/command_line_interface.h>
36 
37 #include <google/protobuf/stubs/platform_macros.h>
38 
39 #include <stdio.h>
40 #include <sys/types.h>
41 #ifdef major
42 #undef major
43 #endif
44 #ifdef minor
45 #undef minor
46 #endif
47 #include <fcntl.h>
48 #include <sys/stat.h>
49 #ifndef _MSC_VER
50 #include <unistd.h>
51 #endif
52 #include <ctype.h>
53 #include <errno.h>
54 #include <fstream>
55 #include <iostream>
56 
57 #include <limits.h>  //For PATH_MAX
58 
59 #include <memory>
60 
61 #ifdef __APPLE__
62 #include <mach-o/dyld.h>
63 #endif
64 
65 #include <google/protobuf/stubs/common.h>
66 #include <google/protobuf/stubs/logging.h>
67 #include <google/protobuf/stubs/stringprintf.h>
68 #include <google/protobuf/compiler/subprocess.h>
69 #include <google/protobuf/compiler/zip_writer.h>
70 #include <google/protobuf/compiler/plugin.pb.h>
71 #include <google/protobuf/compiler/code_generator.h>
72 #include <google/protobuf/compiler/importer.h>
73 #include <google/protobuf/io/coded_stream.h>
74 #include <google/protobuf/io/printer.h>
75 #include <google/protobuf/io/zero_copy_stream_impl.h>
76 #include <google/protobuf/descriptor.h>
77 #include <google/protobuf/dynamic_message.h>
78 #include <google/protobuf/text_format.h>
79 #include <google/protobuf/stubs/strutil.h>
80 #include <google/protobuf/stubs/substitute.h>
81 #include <google/protobuf/io/io_win32.h>
82 #include <google/protobuf/stubs/map_util.h>
83 #include <google/protobuf/stubs/stl_util.h>
84 
85 
86 #include <google/protobuf/port_def.inc>
87 
88 namespace google {
89 namespace protobuf {
90 namespace compiler {
91 
92 #ifndef O_BINARY
93 #ifdef _O_BINARY
94 #define O_BINARY _O_BINARY
95 #else
96 #define O_BINARY 0  // If this isn't defined, the platform doesn't need it.
97 #endif
98 #endif
99 
100 namespace {
101 #if defined(_WIN32)
102 // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import
103 // them like we do below.
104 using google::protobuf::io::win32::access;
105 using google::protobuf::io::win32::close;
106 using google::protobuf::io::win32::mkdir;
107 using google::protobuf::io::win32::open;
108 using google::protobuf::io::win32::setmode;
109 using google::protobuf::io::win32::write;
110 #endif
111 
112 static const char* kDefaultDirectDependenciesViolationMsg =
113     "File is imported but not declared in --direct_dependencies: %s";
114 
115 // Returns true if the text looks like a Windows-style absolute path, starting
116 // with a drive letter.  Example:  "C:\foo".  TODO(kenton):  Share this with
117 // copy in importer.cc?
IsWindowsAbsolutePath(const std::string & text)118 static bool IsWindowsAbsolutePath(const std::string& text) {
119 #if defined(_WIN32) || defined(__CYGWIN__)
120   return text.size() >= 3 && text[1] == ':' && isalpha(text[0]) &&
121          (text[2] == '/' || text[2] == '\\') && text.find_last_of(':') == 1;
122 #else
123   return false;
124 #endif
125 }
126 
SetFdToTextMode(int fd)127 void SetFdToTextMode(int fd) {
128 #ifdef _WIN32
129   if (setmode(fd, _O_TEXT) == -1) {
130     // This should never happen, I think.
131     GOOGLE_LOG(WARNING) << "setmode(" << fd << ", _O_TEXT): " << strerror(errno);
132   }
133 #endif
134   // (Text and binary are the same on non-Windows platforms.)
135 }
136 
SetFdToBinaryMode(int fd)137 void SetFdToBinaryMode(int fd) {
138 #ifdef _WIN32
139   if (setmode(fd, _O_BINARY) == -1) {
140     // This should never happen, I think.
141     GOOGLE_LOG(WARNING) << "setmode(" << fd << ", _O_BINARY): " << strerror(errno);
142   }
143 #endif
144   // (Text and binary are the same on non-Windows platforms.)
145 }
146 
AddTrailingSlash(std::string * path)147 void AddTrailingSlash(std::string* path) {
148   if (!path->empty() && path->at(path->size() - 1) != '/') {
149     path->push_back('/');
150   }
151 }
152 
VerifyDirectoryExists(const std::string & path)153 bool VerifyDirectoryExists(const std::string& path) {
154   if (path.empty()) return true;
155 
156   if (access(path.c_str(), F_OK) == -1) {
157     std::cerr << path << ": " << strerror(errno) << std::endl;
158     return false;
159   } else {
160     return true;
161   }
162 }
163 
164 // Try to create the parent directory of the given file, creating the parent's
165 // parent if necessary, and so on.  The full file name is actually
166 // (prefix + filename), but we assume |prefix| already exists and only create
167 // directories listed in |filename|.
TryCreateParentDirectory(const std::string & prefix,const std::string & filename)168 bool TryCreateParentDirectory(const std::string& prefix,
169                               const std::string& filename) {
170   // Recursively create parent directories to the output file.
171   // On Windows, both '/' and '\' are valid path separators.
172   std::vector<std::string> parts =
173       Split(filename, "/\\", true);
174   std::string path_so_far = prefix;
175   for (int i = 0; i < parts.size() - 1; i++) {
176     path_so_far += parts[i];
177     if (mkdir(path_so_far.c_str(), 0777) != 0) {
178       if (errno != EEXIST) {
179         std::cerr << filename << ": while trying to create directory "
180                   << path_so_far << ": " << strerror(errno) << std::endl;
181         return false;
182       }
183     }
184     path_so_far += '/';
185   }
186 
187   return true;
188 }
189 
190 // Get the absolute path of this protoc binary.
GetProtocAbsolutePath(std::string * path)191 bool GetProtocAbsolutePath(std::string* path) {
192 #ifdef _WIN32
193   char buffer[MAX_PATH];
194   int len = GetModuleFileNameA(NULL, buffer, MAX_PATH);
195 #elif defined(__APPLE__)
196   char buffer[PATH_MAX];
197   int len = 0;
198 
199   char dirtybuffer[PATH_MAX];
200   uint32_t size = sizeof(dirtybuffer);
201   if (_NSGetExecutablePath(dirtybuffer, &size) == 0) {
202     realpath(dirtybuffer, buffer);
203     len = strlen(buffer);
204   }
205 #else
206   char buffer[PATH_MAX];
207   int len = readlink("/proc/self/exe", buffer, PATH_MAX);
208 #endif
209   if (len > 0) {
210     path->assign(buffer, len);
211     return true;
212   } else {
213     return false;
214   }
215 }
216 
217 // Whether a path is where google/protobuf/descriptor.proto and other well-known
218 // type protos are installed.
IsInstalledProtoPath(const std::string & path)219 bool IsInstalledProtoPath(const std::string& path) {
220   // Checking the descriptor.proto file should be good enough.
221   std::string file_path = path + "/google/protobuf/descriptor.proto";
222   return access(file_path.c_str(), F_OK) != -1;
223 }
224 
225 // Add the paths where google/protobuf/descriptor.proto and other well-known
226 // type protos are installed.
AddDefaultProtoPaths(std::vector<std::pair<std::string,std::string>> * paths)227 void AddDefaultProtoPaths(
228     std::vector<std::pair<std::string, std::string> >* paths) {
229   // TODO(xiaofeng): The code currently only checks relative paths of where
230   // the protoc binary is installed. We probably should make it handle more
231   // cases than that.
232   std::string path;
233   if (!GetProtocAbsolutePath(&path)) {
234     return;
235   }
236   // Strip the binary name.
237   size_t pos = path.find_last_of("/\\");
238   if (pos == std::string::npos || pos == 0) {
239     return;
240   }
241   path = path.substr(0, pos);
242   // Check the binary's directory.
243   if (IsInstalledProtoPath(path)) {
244     paths->push_back(std::pair<std::string, std::string>("", path));
245     return;
246   }
247   // Check if there is an include subdirectory.
248   if (IsInstalledProtoPath(path + "/include")) {
249     paths->push_back(
250         std::pair<std::string, std::string>("", path + "/include"));
251     return;
252   }
253   // Check if the upper level directory has an "include" subdirectory.
254   pos = path.find_last_of("/\\");
255   if (pos == std::string::npos || pos == 0) {
256     return;
257   }
258   path = path.substr(0, pos);
259   if (IsInstalledProtoPath(path + "/include")) {
260     paths->push_back(
261         std::pair<std::string, std::string>("", path + "/include"));
262     return;
263   }
264 }
265 
PluginName(const std::string & plugin_prefix,const std::string & directive)266 std::string PluginName(const std::string& plugin_prefix,
267                        const std::string& directive) {
268   // Assuming the directive starts with "--" and ends with "_out" or "_opt",
269   // strip the "--" and "_out/_opt" and add the plugin prefix.
270   return plugin_prefix + "gen-" + directive.substr(2, directive.size() - 6);
271 }
272 
273 }  // namespace
274 
275 // A MultiFileErrorCollector that prints errors to stderr.
276 class CommandLineInterface::ErrorPrinter
277     : public MultiFileErrorCollector,
278       public io::ErrorCollector,
279       public DescriptorPool::ErrorCollector {
280  public:
ErrorPrinter(ErrorFormat format,DiskSourceTree * tree=NULL)281   ErrorPrinter(ErrorFormat format, DiskSourceTree* tree = NULL)
282       : format_(format), tree_(tree), found_errors_(false) {}
~ErrorPrinter()283   ~ErrorPrinter() {}
284 
285   // implements MultiFileErrorCollector ------------------------------
AddError(const std::string & filename,int line,int column,const std::string & message)286   void AddError(const std::string& filename, int line, int column,
287                 const std::string& message) {
288     found_errors_ = true;
289     AddErrorOrWarning(filename, line, column, message, "error", std::cerr);
290   }
291 
AddWarning(const std::string & filename,int line,int column,const std::string & message)292   void AddWarning(const std::string& filename, int line, int column,
293                   const std::string& message) {
294     AddErrorOrWarning(filename, line, column, message, "warning", std::clog);
295   }
296 
297   // implements io::ErrorCollector -----------------------------------
AddError(int line,int column,const std::string & message)298   void AddError(int line, int column, const std::string& message) {
299     AddError("input", line, column, message);
300   }
301 
AddWarning(int line,int column,const std::string & message)302   void AddWarning(int line, int column, const std::string& message) {
303     AddErrorOrWarning("input", line, column, message, "warning", std::clog);
304   }
305 
306   // implements DescriptorPool::ErrorCollector-------------------------
AddError(const std::string & filename,const std::string & element_name,const Message * descriptor,ErrorLocation location,const std::string & message)307   void AddError(const std::string& filename, const std::string& element_name,
308                 const Message* descriptor, ErrorLocation location,
309                 const std::string& message) {
310     AddErrorOrWarning(filename, -1, -1, message, "error", std::cerr);
311   }
312 
AddWarning(const std::string & filename,const std::string & element_name,const Message * descriptor,ErrorLocation location,const std::string & message)313   void AddWarning(const std::string& filename, const std::string& element_name,
314                   const Message* descriptor, ErrorLocation location,
315                   const std::string& message) {
316     AddErrorOrWarning(filename, -1, -1, message, "warning", std::clog);
317   }
318 
FoundErrors() const319   bool FoundErrors() const { return found_errors_; }
320 
321  private:
AddErrorOrWarning(const std::string & filename,int line,int column,const std::string & message,const std::string & type,std::ostream & out)322   void AddErrorOrWarning(const std::string& filename, int line, int column,
323                          const std::string& message, const std::string& type,
324                          std::ostream& out) {
325     // Print full path when running under MSVS
326     std::string dfile;
327     if (format_ == CommandLineInterface::ERROR_FORMAT_MSVS && tree_ != NULL &&
328         tree_->VirtualFileToDiskFile(filename, &dfile)) {
329       out << dfile;
330     } else {
331       out << filename;
332     }
333 
334     // Users typically expect 1-based line/column numbers, so we add 1
335     // to each here.
336     if (line != -1) {
337       // Allow for both GCC- and Visual-Studio-compatible output.
338       switch (format_) {
339         case CommandLineInterface::ERROR_FORMAT_GCC:
340           out << ":" << (line + 1) << ":" << (column + 1);
341           break;
342         case CommandLineInterface::ERROR_FORMAT_MSVS:
343           out << "(" << (line + 1) << ") : " << type
344               << " in column=" << (column + 1);
345           break;
346       }
347     }
348 
349     if (type == "warning") {
350       out << ": warning: " << message << std::endl;
351     } else {
352       out << ": " << message << std::endl;
353     }
354   }
355 
356   const ErrorFormat format_;
357   DiskSourceTree* tree_;
358   bool found_errors_;
359 };
360 
361 // -------------------------------------------------------------------
362 
363 // A GeneratorContext implementation that buffers files in memory, then dumps
364 // them all to disk on demand.
365 class CommandLineInterface::GeneratorContextImpl : public GeneratorContext {
366  public:
367   GeneratorContextImpl(const std::vector<const FileDescriptor*>& parsed_files);
368 
369   // Write all files in the directory to disk at the given output location,
370   // which must end in a '/'.
371   bool WriteAllToDisk(const std::string& prefix);
372 
373   // Write the contents of this directory to a ZIP-format archive with the
374   // given name.
375   bool WriteAllToZip(const std::string& filename);
376 
377   // Add a boilerplate META-INF/MANIFEST.MF file as required by the Java JAR
378   // format, unless one has already been written.
379   void AddJarManifest();
380 
381   // Get name of all output files.
382   void GetOutputFilenames(std::vector<std::string>* output_filenames);
383 
384   // implements GeneratorContext --------------------------------------
385   io::ZeroCopyOutputStream* Open(const std::string& filename);
386   io::ZeroCopyOutputStream* OpenForAppend(const std::string& filename);
387   io::ZeroCopyOutputStream* OpenForInsert(const std::string& filename,
388                                           const std::string& insertion_point);
ListParsedFiles(std::vector<const FileDescriptor * > * output)389   void ListParsedFiles(std::vector<const FileDescriptor*>* output) {
390     *output = parsed_files_;
391   }
392 
393  private:
394   friend class MemoryOutputStream;
395 
396   // map instead of unordered_map so that files are written in order (good when
397   // writing zips).
398   std::map<std::string, std::string> files_;
399   const std::vector<const FileDescriptor*>& parsed_files_;
400   bool had_error_;
401 };
402 
403 class CommandLineInterface::MemoryOutputStream
404     : public io::ZeroCopyOutputStream {
405  public:
406   MemoryOutputStream(GeneratorContextImpl* directory,
407                      const std::string& filename, bool append_mode);
408   MemoryOutputStream(GeneratorContextImpl* directory,
409                      const std::string& filename,
410                      const std::string& insertion_point);
411   virtual ~MemoryOutputStream();
412 
413   // implements ZeroCopyOutputStream ---------------------------------
Next(void ** data,int * size)414   bool Next(void** data, int* size) override {
415     return inner_->Next(data, size);
416   }
BackUp(int count)417   void BackUp(int count) override { inner_->BackUp(count); }
ByteCount() const418   int64_t ByteCount() const override { return inner_->ByteCount(); }
419 
420  private:
421   // Checks to see if "filename_.meta" exists in directory_; if so, fixes the
422   // offsets in that GeneratedCodeInfo record to reflect bytes inserted in
423   // filename_ at original offset insertion_offset with length insertion_length.
424   // We assume that insertions will not occur within any given annotated span
425   // of text.
426   void UpdateMetadata(size_t insertion_offset, size_t insertion_length);
427 
428   // Where to insert the string when it's done.
429   GeneratorContextImpl* directory_;
430   std::string filename_;
431   std::string insertion_point_;
432 
433   // The string we're building.
434   std::string data_;
435 
436   // Whether we should append the output stream to the existing file.
437   bool append_mode_;
438 
439   // StringOutputStream writing to data_.
440   std::unique_ptr<io::StringOutputStream> inner_;
441 };
442 
443 // -------------------------------------------------------------------
444 
GeneratorContextImpl(const std::vector<const FileDescriptor * > & parsed_files)445 CommandLineInterface::GeneratorContextImpl::GeneratorContextImpl(
446     const std::vector<const FileDescriptor*>& parsed_files)
447     : parsed_files_(parsed_files), had_error_(false) {}
448 
WriteAllToDisk(const std::string & prefix)449 bool CommandLineInterface::GeneratorContextImpl::WriteAllToDisk(
450     const std::string& prefix) {
451   if (had_error_) {
452     return false;
453   }
454 
455   if (!VerifyDirectoryExists(prefix)) {
456     return false;
457   }
458 
459   for (const auto& pair : files_) {
460     const std::string& relative_filename = pair.first;
461     const char* data = pair.second.data();
462     int size = pair.second.size();
463 
464     if (!TryCreateParentDirectory(prefix, relative_filename)) {
465       return false;
466     }
467     std::string filename = prefix + relative_filename;
468 
469     // Create the output file.
470     int file_descriptor;
471     do {
472       file_descriptor =
473           open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
474     } while (file_descriptor < 0 && errno == EINTR);
475 
476     if (file_descriptor < 0) {
477       int error = errno;
478       std::cerr << filename << ": " << strerror(error);
479       return false;
480     }
481 
482     // Write the file.
483     while (size > 0) {
484       int write_result;
485       do {
486         write_result = write(file_descriptor, data, size);
487       } while (write_result < 0 && errno == EINTR);
488 
489       if (write_result <= 0) {
490         // Write error.
491 
492         // FIXME(kenton):  According to the man page, if write() returns zero,
493         //   there was no error; write() simply did not write anything.  It's
494         //   unclear under what circumstances this might happen, but presumably
495         //   errno won't be set in this case.  I am confused as to how such an
496         //   event should be handled.  For now I'm treating it as an error,
497         //   since retrying seems like it could lead to an infinite loop.  I
498         //   suspect this never actually happens anyway.
499 
500         if (write_result < 0) {
501           int error = errno;
502           std::cerr << filename << ": write: " << strerror(error);
503         } else {
504           std::cerr << filename << ": write() returned zero?" << std::endl;
505         }
506         return false;
507       }
508 
509       data += write_result;
510       size -= write_result;
511     }
512 
513     if (close(file_descriptor) != 0) {
514       int error = errno;
515       std::cerr << filename << ": close: " << strerror(error);
516       return false;
517     }
518   }
519 
520   return true;
521 }
522 
WriteAllToZip(const std::string & filename)523 bool CommandLineInterface::GeneratorContextImpl::WriteAllToZip(
524     const std::string& filename) {
525   if (had_error_) {
526     return false;
527   }
528 
529   // Create the output file.
530   int file_descriptor;
531   do {
532     file_descriptor =
533         open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
534   } while (file_descriptor < 0 && errno == EINTR);
535 
536   if (file_descriptor < 0) {
537     int error = errno;
538     std::cerr << filename << ": " << strerror(error);
539     return false;
540   }
541 
542   // Create the ZipWriter
543   io::FileOutputStream stream(file_descriptor);
544   ZipWriter zip_writer(&stream);
545 
546   for (const auto& pair : files_) {
547     zip_writer.Write(pair.first, pair.second);
548   }
549 
550   zip_writer.WriteDirectory();
551 
552   if (stream.GetErrno() != 0) {
553     std::cerr << filename << ": " << strerror(stream.GetErrno()) << std::endl;
554   }
555 
556   if (!stream.Close()) {
557     std::cerr << filename << ": " << strerror(stream.GetErrno()) << std::endl;
558   }
559 
560   return true;
561 }
562 
AddJarManifest()563 void CommandLineInterface::GeneratorContextImpl::AddJarManifest() {
564   auto pair = files_.insert({"META-INF/MANIFEST.MF", ""});
565   if (pair.second) {
566     pair.first->second =
567         "Manifest-Version: 1.0\n"
568         "Created-By: 1.6.0 (protoc)\n"
569         "\n";
570   }
571 }
572 
GetOutputFilenames(std::vector<std::string> * output_filenames)573 void CommandLineInterface::GeneratorContextImpl::GetOutputFilenames(
574     std::vector<std::string>* output_filenames) {
575   for (const auto& pair : files_) {
576     output_filenames->push_back(pair.first);
577   }
578 }
579 
Open(const std::string & filename)580 io::ZeroCopyOutputStream* CommandLineInterface::GeneratorContextImpl::Open(
581     const std::string& filename) {
582   return new MemoryOutputStream(this, filename, false);
583 }
584 
585 io::ZeroCopyOutputStream*
OpenForAppend(const std::string & filename)586 CommandLineInterface::GeneratorContextImpl::OpenForAppend(
587     const std::string& filename) {
588   return new MemoryOutputStream(this, filename, true);
589 }
590 
591 io::ZeroCopyOutputStream*
OpenForInsert(const std::string & filename,const std::string & insertion_point)592 CommandLineInterface::GeneratorContextImpl::OpenForInsert(
593     const std::string& filename, const std::string& insertion_point) {
594   return new MemoryOutputStream(this, filename, insertion_point);
595 }
596 
597 // -------------------------------------------------------------------
598 
MemoryOutputStream(GeneratorContextImpl * directory,const std::string & filename,bool append_mode)599 CommandLineInterface::MemoryOutputStream::MemoryOutputStream(
600     GeneratorContextImpl* directory, const std::string& filename,
601     bool append_mode)
602     : directory_(directory),
603       filename_(filename),
604       append_mode_(append_mode),
605       inner_(new io::StringOutputStream(&data_)) {}
606 
MemoryOutputStream(GeneratorContextImpl * directory,const std::string & filename,const std::string & insertion_point)607 CommandLineInterface::MemoryOutputStream::MemoryOutputStream(
608     GeneratorContextImpl* directory, const std::string& filename,
609     const std::string& insertion_point)
610     : directory_(directory),
611       filename_(filename),
612       insertion_point_(insertion_point),
613       inner_(new io::StringOutputStream(&data_)) {}
614 
UpdateMetadata(size_t insertion_offset,size_t insertion_length)615 void CommandLineInterface::MemoryOutputStream::UpdateMetadata(
616     size_t insertion_offset, size_t insertion_length) {
617   auto it = directory_->files_.find(filename_ + ".meta");
618   if (it == directory_->files_.end()) {
619     // No metadata was recorded for this file.
620     return;
621   }
622   std::string& encoded_data = it->second;
623   GeneratedCodeInfo metadata;
624   bool is_text_format = false;
625   if (!metadata.ParseFromString(encoded_data)) {
626     if (!TextFormat::ParseFromString(encoded_data, &metadata)) {
627       // The metadata is invalid.
628       std::cerr << filename_
629                 << ".meta: Could not parse metadata as wire or text format."
630                 << std::endl;
631       return;
632     }
633     // Generators that use the public plugin interface emit text-format
634     // metadata (because in the public plugin protocol, file content must be
635     // UTF8-encoded strings).
636     is_text_format = true;
637   }
638   for (int i = 0; i < metadata.annotation_size(); ++i) {
639     GeneratedCodeInfo::Annotation* annotation = metadata.mutable_annotation(i);
640     if (annotation->begin() >= insertion_offset) {
641       annotation->set_begin(annotation->begin() + insertion_length);
642       annotation->set_end(annotation->end() + insertion_length);
643     }
644   }
645   if (is_text_format) {
646     TextFormat::PrintToString(metadata, &encoded_data);
647   } else {
648     metadata.SerializeToString(&encoded_data);
649   }
650 }
651 
~MemoryOutputStream()652 CommandLineInterface::MemoryOutputStream::~MemoryOutputStream() {
653   // Make sure all data has been written.
654   inner_.reset();
655 
656   // Insert into the directory.
657   auto pair = directory_->files_.insert({filename_, ""});
658   auto it = pair.first;
659   bool already_present = !pair.second;
660 
661   if (insertion_point_.empty()) {
662     // This was just a regular Open().
663     if (already_present) {
664       if (append_mode_) {
665         it->second.append(data_);
666       } else {
667         std::cerr << filename_ << ": Tried to write the same file twice."
668                   << std::endl;
669         directory_->had_error_ = true;
670       }
671       return;
672     }
673 
674     it->second.swap(data_);
675   } else {
676     // This was an OpenForInsert().
677 
678     // If the data doesn't end with a clean line break, add one.
679     if (!data_.empty() && data_[data_.size() - 1] != '\n') {
680       data_.push_back('\n');
681     }
682 
683     // Find the file we are going to insert into.
684     if (!already_present) {
685       std::cerr << filename_
686                 << ": Tried to insert into file that doesn't exist."
687                 << std::endl;
688       directory_->had_error_ = true;
689       return;
690     }
691     std::string* target = &it->second;
692 
693     // Find the insertion point.
694     std::string magic_string =
695         strings::Substitute("@@protoc_insertion_point($0)", insertion_point_);
696     std::string::size_type pos = target->find(magic_string);
697 
698     if (pos == std::string::npos) {
699       std::cerr << filename_ << ": insertion point \"" << insertion_point_
700                 << "\" not found." << std::endl;
701       directory_->had_error_ = true;
702       return;
703     }
704 
705     if ((pos > 3) && (target->substr(pos - 3, 2) == "/*")) {
706       // Support for inline "/* @@protoc_insertion_point() */"
707       pos = pos - 3;
708     } else {
709       // Seek backwards to the beginning of the line, which is where we will
710       // insert the data.  Note that this has the effect of pushing the
711       // insertion point down, so the data is inserted before it.  This is
712       // intentional because it means that multiple insertions at the same point
713       // will end up in the expected order in the final output.
714       pos = target->find_last_of('\n', pos);
715       if (pos == std::string::npos) {
716         // Insertion point is on the first line.
717         pos = 0;
718       } else {
719         // Advance to character after '\n'.
720         ++pos;
721       }
722     }
723 
724     // Extract indent.
725     std::string indent_(*target, pos,
726                         target->find_first_not_of(" \t", pos) - pos);
727 
728     if (indent_.empty()) {
729       // No indent.  This makes things easier.
730       target->insert(pos, data_);
731       UpdateMetadata(pos, data_.size());
732     } else {
733       // Calculate how much space we need.
734       int indent_size = 0;
735       for (int i = 0; i < data_.size(); i++) {
736         if (data_[i] == '\n') indent_size += indent_.size();
737       }
738 
739       // Make a hole for it.
740       target->insert(pos, data_.size() + indent_size, '\0');
741       UpdateMetadata(pos, data_.size() + indent_size);
742 
743       // Now copy in the data.
744       std::string::size_type data_pos = 0;
745       char* target_ptr = ::google::protobuf::string_as_array(target) + pos;
746       while (data_pos < data_.size()) {
747         // Copy indent.
748         memcpy(target_ptr, indent_.data(), indent_.size());
749         target_ptr += indent_.size();
750 
751         // Copy line from data_.
752         // We already guaranteed that data_ ends with a newline (above), so this
753         // search can't fail.
754         std::string::size_type line_length =
755             data_.find_first_of('\n', data_pos) + 1 - data_pos;
756         memcpy(target_ptr, data_.data() + data_pos, line_length);
757         target_ptr += line_length;
758         data_pos += line_length;
759       }
760 
761       GOOGLE_CHECK_EQ(target_ptr,
762                ::google::protobuf::string_as_array(target) + pos + data_.size() + indent_size);
763     }
764   }
765 }
766 
767 // ===================================================================
768 
769 #if defined(_WIN32) && !defined(__CYGWIN__)
770 const char* const CommandLineInterface::kPathSeparator = ";";
771 #else
772 const char* const CommandLineInterface::kPathSeparator = ":";
773 #endif
774 
CommandLineInterface()775 CommandLineInterface::CommandLineInterface()
776     : direct_dependencies_violation_msg_(
777           kDefaultDirectDependenciesViolationMsg) {}
778 
~CommandLineInterface()779 CommandLineInterface::~CommandLineInterface() {}
780 
RegisterGenerator(const std::string & flag_name,CodeGenerator * generator,const std::string & help_text)781 void CommandLineInterface::RegisterGenerator(const std::string& flag_name,
782                                              CodeGenerator* generator,
783                                              const std::string& help_text) {
784   GeneratorInfo info;
785   info.flag_name = flag_name;
786   info.generator = generator;
787   info.help_text = help_text;
788   generators_by_flag_name_[flag_name] = info;
789 }
790 
RegisterGenerator(const std::string & flag_name,const std::string & option_flag_name,CodeGenerator * generator,const std::string & help_text)791 void CommandLineInterface::RegisterGenerator(
792     const std::string& flag_name, const std::string& option_flag_name,
793     CodeGenerator* generator, const std::string& help_text) {
794   GeneratorInfo info;
795   info.flag_name = flag_name;
796   info.option_flag_name = option_flag_name;
797   info.generator = generator;
798   info.help_text = help_text;
799   generators_by_flag_name_[flag_name] = info;
800   generators_by_option_name_[option_flag_name] = info;
801 }
802 
AllowPlugins(const std::string & exe_name_prefix)803 void CommandLineInterface::AllowPlugins(const std::string& exe_name_prefix) {
804   plugin_prefix_ = exe_name_prefix;
805 }
806 
807 namespace {
808 
ContainsProto3Optional(const Descriptor * desc)809 bool ContainsProto3Optional(const Descriptor* desc) {
810   for (int i = 0; i < desc->field_count(); i++) {
811     if (desc->field(i)->has_optional_keyword()) {
812       return true;
813     }
814   }
815   for (int i = 0; i < desc->nested_type_count(); i++) {
816     if (ContainsProto3Optional(desc->nested_type(i))) {
817       return true;
818     }
819   }
820   return false;
821 }
822 
ContainsProto3Optional(const FileDescriptor * file)823 bool ContainsProto3Optional(const FileDescriptor* file) {
824   if (file->syntax() == FileDescriptor::SYNTAX_PROTO3) {
825     for (int i = 0; i < file->message_type_count(); i++) {
826       if (ContainsProto3Optional(file->message_type(i))) {
827         return true;
828       }
829     }
830   }
831   return false;
832 }
833 
834 }  // namespace
835 
836 namespace {
837 std::unique_ptr<SimpleDescriptorDatabase>
838 PopulateSingleSimpleDescriptorDatabase(const std::string& descriptor_set_name);
839 }
840 
Run(int argc,const char * const argv[])841 int CommandLineInterface::Run(int argc, const char* const argv[]) {
842   Clear();
843   switch (ParseArguments(argc, argv)) {
844     case PARSE_ARGUMENT_DONE_AND_EXIT:
845       return 0;
846     case PARSE_ARGUMENT_FAIL:
847       return 1;
848     case PARSE_ARGUMENT_DONE_AND_CONTINUE:
849       break;
850   }
851 
852   std::vector<const FileDescriptor*> parsed_files;
853   std::unique_ptr<DiskSourceTree> disk_source_tree;
854   std::unique_ptr<ErrorPrinter> error_collector;
855   std::unique_ptr<DescriptorPool> descriptor_pool;
856 
857   // The SimpleDescriptorDatabases here are the constituents of the
858   // MergedDescriptorDatabase descriptor_set_in_database, so this vector is for
859   // managing their lifetimes. Its scope should match descriptor_set_in_database
860   std::vector<std::unique_ptr<SimpleDescriptorDatabase>>
861       databases_per_descriptor_set;
862   std::unique_ptr<MergedDescriptorDatabase> descriptor_set_in_database;
863 
864   std::unique_ptr<SourceTreeDescriptorDatabase> source_tree_database;
865 
866   // Any --descriptor_set_in FileDescriptorSet objects will be used as a
867   // fallback to input_files on command line, so create that db first.
868   if (!descriptor_set_in_names_.empty()) {
869     for (const std::string& name : descriptor_set_in_names_) {
870       std::unique_ptr<SimpleDescriptorDatabase> database_for_descriptor_set =
871           PopulateSingleSimpleDescriptorDatabase(name);
872       if (!database_for_descriptor_set) {
873         return EXIT_FAILURE;
874       }
875       databases_per_descriptor_set.push_back(
876           std::move(database_for_descriptor_set));
877     }
878 
879     std::vector<DescriptorDatabase*> raw_databases_per_descriptor_set;
880     raw_databases_per_descriptor_set.reserve(
881         databases_per_descriptor_set.size());
882     for (const std::unique_ptr<SimpleDescriptorDatabase>& db :
883          databases_per_descriptor_set) {
884       raw_databases_per_descriptor_set.push_back(db.get());
885     }
886     descriptor_set_in_database.reset(
887         new MergedDescriptorDatabase(raw_databases_per_descriptor_set));
888   }
889 
890   if (proto_path_.empty()) {
891     // If there are no --proto_path flags, then just look in the specified
892     // --descriptor_set_in files.  But first, verify that the input files are
893     // there.
894     if (!VerifyInputFilesInDescriptors(descriptor_set_in_database.get())) {
895       return 1;
896     }
897 
898     error_collector.reset(new ErrorPrinter(error_format_));
899     descriptor_pool.reset(new DescriptorPool(descriptor_set_in_database.get(),
900                                              error_collector.get()));
901   } else {
902     disk_source_tree.reset(new DiskSourceTree());
903     if (!InitializeDiskSourceTree(disk_source_tree.get(),
904                                   descriptor_set_in_database.get())) {
905       return 1;
906     }
907 
908     error_collector.reset(
909         new ErrorPrinter(error_format_, disk_source_tree.get()));
910 
911     source_tree_database.reset(new SourceTreeDescriptorDatabase(
912         disk_source_tree.get(), descriptor_set_in_database.get()));
913     source_tree_database->RecordErrorsTo(error_collector.get());
914 
915     descriptor_pool.reset(new DescriptorPool(
916         source_tree_database.get(),
917         source_tree_database->GetValidationErrorCollector()));
918   }
919 
920   descriptor_pool->EnforceWeakDependencies(true);
921   if (!ParseInputFiles(descriptor_pool.get(), disk_source_tree.get(),
922                        &parsed_files)) {
923     return 1;
924   }
925 
926 
927   for (auto fd : parsed_files) {
928     if (!AllowProto3Optional(*fd) && ContainsProto3Optional(fd)) {
929       std::cerr << fd->name()
930                 << ": This file contains proto3 optional fields, but "
931                    "--experimental_allow_proto3_optional was not set."
932                 << std::endl;
933       return 1;
934     }
935   }
936 
937   // We construct a separate GeneratorContext for each output location.  Note
938   // that two code generators may output to the same location, in which case
939   // they should share a single GeneratorContext so that OpenForInsert() works.
940   GeneratorContextMap output_directories;
941 
942   // Generate output.
943   if (mode_ == MODE_COMPILE) {
944     for (int i = 0; i < output_directives_.size(); i++) {
945       std::string output_location = output_directives_[i].output_location;
946       if (!HasSuffixString(output_location, ".zip") &&
947           !HasSuffixString(output_location, ".jar") &&
948           !HasSuffixString(output_location, ".srcjar")) {
949         AddTrailingSlash(&output_location);
950       }
951 
952       auto& generator = output_directories[output_location];
953 
954       if (!generator) {
955         // First time we've seen this output location.
956         generator.reset(new GeneratorContextImpl(parsed_files));
957       }
958 
959       if (!GenerateOutput(parsed_files, output_directives_[i],
960                           generator.get())) {
961         return 1;
962       }
963     }
964   }
965 
966   // Write all output to disk.
967   for (const auto& pair : output_directories) {
968     const std::string& location = pair.first;
969     GeneratorContextImpl* directory = pair.second.get();
970     if (HasSuffixString(location, "/")) {
971       if (!directory->WriteAllToDisk(location)) {
972         return 1;
973       }
974     } else {
975       if (HasSuffixString(location, ".jar")) {
976         directory->AddJarManifest();
977       }
978 
979       if (!directory->WriteAllToZip(location)) {
980         return 1;
981       }
982     }
983   }
984 
985   if (!dependency_out_name_.empty()) {
986     GOOGLE_DCHECK(disk_source_tree.get());
987     if (!GenerateDependencyManifestFile(parsed_files, output_directories,
988                                         disk_source_tree.get())) {
989       return 1;
990     }
991   }
992 
993   if (!descriptor_set_out_name_.empty()) {
994     if (!WriteDescriptorSet(parsed_files)) {
995       return 1;
996     }
997   }
998 
999   if (mode_ == MODE_ENCODE || mode_ == MODE_DECODE) {
1000     if (codec_type_.empty()) {
1001       // HACK:  Define an EmptyMessage type to use for decoding.
1002       DescriptorPool pool;
1003       FileDescriptorProto file;
1004       file.set_name("empty_message.proto");
1005       file.add_message_type()->set_name("EmptyMessage");
1006       GOOGLE_CHECK(pool.BuildFile(file) != NULL);
1007       codec_type_ = "EmptyMessage";
1008       if (!EncodeOrDecode(&pool)) {
1009         return 1;
1010       }
1011     } else {
1012       if (!EncodeOrDecode(descriptor_pool.get())) {
1013         return 1;
1014       }
1015     }
1016   }
1017 
1018   if (error_collector->FoundErrors()) {
1019     return 1;
1020   }
1021 
1022   if (mode_ == MODE_PRINT) {
1023     switch (print_mode_) {
1024       case PRINT_FREE_FIELDS:
1025         for (int i = 0; i < parsed_files.size(); ++i) {
1026           const FileDescriptor* fd = parsed_files[i];
1027           for (int j = 0; j < fd->message_type_count(); ++j) {
1028             PrintFreeFieldNumbers(fd->message_type(j));
1029           }
1030         }
1031         break;
1032       case PRINT_NONE:
1033         GOOGLE_LOG(ERROR) << "If the code reaches here, it usually means a bug of "
1034                       "flag parsing in the CommandLineInterface.";
1035         return 1;
1036 
1037         // Do not add a default case.
1038     }
1039   }
1040 
1041   return 0;
1042 }
1043 
InitializeDiskSourceTree(DiskSourceTree * source_tree,DescriptorDatabase * fallback_database)1044 bool CommandLineInterface::InitializeDiskSourceTree(
1045     DiskSourceTree* source_tree, DescriptorDatabase* fallback_database) {
1046   AddDefaultProtoPaths(&proto_path_);
1047 
1048   // Set up the source tree.
1049   for (int i = 0; i < proto_path_.size(); i++) {
1050     source_tree->MapPath(proto_path_[i].first, proto_path_[i].second);
1051   }
1052 
1053   // Map input files to virtual paths if possible.
1054   if (!MakeInputsBeProtoPathRelative(source_tree, fallback_database)) {
1055     return false;
1056   }
1057 
1058   return true;
1059 }
1060 
1061 namespace {
1062 std::unique_ptr<SimpleDescriptorDatabase>
PopulateSingleSimpleDescriptorDatabase(const std::string & descriptor_set_name)1063 PopulateSingleSimpleDescriptorDatabase(const std::string& descriptor_set_name) {
1064   int fd;
1065   do {
1066     fd = open(descriptor_set_name.c_str(), O_RDONLY | O_BINARY);
1067   } while (fd < 0 && errno == EINTR);
1068   if (fd < 0) {
1069     std::cerr << descriptor_set_name << ": " << strerror(ENOENT) << std::endl;
1070     return nullptr;
1071   }
1072 
1073   FileDescriptorSet file_descriptor_set;
1074   bool parsed = file_descriptor_set.ParseFromFileDescriptor(fd);
1075   if (close(fd) != 0) {
1076     std::cerr << descriptor_set_name << ": close: " << strerror(errno)
1077               << std::endl;
1078     return nullptr;
1079   }
1080 
1081   if (!parsed) {
1082     std::cerr << descriptor_set_name << ": Unable to parse." << std::endl;
1083     return nullptr;
1084   }
1085 
1086   std::unique_ptr<SimpleDescriptorDatabase> database{
1087       new SimpleDescriptorDatabase()};
1088 
1089   for (int j = 0; j < file_descriptor_set.file_size(); j++) {
1090     FileDescriptorProto previously_added_file_descriptor_proto;
1091     if (database->FindFileByName(file_descriptor_set.file(j).name(),
1092                                  &previously_added_file_descriptor_proto)) {
1093       // already present - skip
1094       continue;
1095     }
1096     if (!database->Add(file_descriptor_set.file(j))) {
1097       return nullptr;
1098     }
1099   }
1100   return database;
1101 }
1102 
1103 }  // namespace
1104 
AllowProto3Optional(const FileDescriptor & file) const1105 bool CommandLineInterface::AllowProto3Optional(
1106     const FileDescriptor& file) const {
1107   // If the --experimental_allow_proto3_optional flag was set, we allow.
1108   if (allow_proto3_optional_) return true;
1109 
1110   // Whitelist all ads protos. Ads is an early adopter of this feature.
1111   if (file.name().find("google/ads/googleads") != std::string::npos) {
1112     return true;
1113   }
1114 
1115   // Whitelist all protos testing proto3 optional.
1116   if (file.name().find("test_proto3_optional") != std::string::npos) {
1117     return true;
1118   }
1119 
1120 
1121   return false;
1122 }
1123 
VerifyInputFilesInDescriptors(DescriptorDatabase * database)1124 bool CommandLineInterface::VerifyInputFilesInDescriptors(
1125     DescriptorDatabase* database) {
1126   for (const auto& input_file : input_files_) {
1127     FileDescriptorProto file_descriptor;
1128     if (!database->FindFileByName(input_file, &file_descriptor)) {
1129       std::cerr << "Could not find file in descriptor database: " << input_file
1130                 << ": " << strerror(ENOENT) << std::endl;
1131       return false;
1132     }
1133 
1134     // Enforce --disallow_services.
1135     if (disallow_services_ && file_descriptor.service_size() > 0) {
1136       std::cerr << file_descriptor.name()
1137                 << ": This file contains services, but "
1138                    "--disallow_services was used."
1139                 << std::endl;
1140       return false;
1141     }
1142   }
1143   return true;
1144 }
1145 
ParseInputFiles(DescriptorPool * descriptor_pool,DiskSourceTree * source_tree,std::vector<const FileDescriptor * > * parsed_files)1146 bool CommandLineInterface::ParseInputFiles(
1147     DescriptorPool* descriptor_pool, DiskSourceTree* source_tree,
1148     std::vector<const FileDescriptor*>* parsed_files) {
1149 
1150   if (!proto_path_.empty()) {
1151     // Track unused imports in all source files that were loaded from the
1152     // filesystem. We do not track unused imports for files loaded from
1153     // descriptor sets as they may be programmatically generated in which case
1154     // exerting this level of rigor is less desirable. We're also making the
1155     // assumption that the initial parse of the proto from the filesystem
1156     // was rigorous in checking unused imports and that the descriptor set
1157     // being parsed was produced then and that it was subsequent mutations
1158     // of that descriptor set that left unused imports.
1159     //
1160     // Note that relying on proto_path exclusively is limited in that we may
1161     // be loading descriptors from both the filesystem and descriptor sets
1162     // depending on the invocation. At least for invocations that are
1163     // exclusively reading from descriptor sets, we can eliminate this failure
1164     // condition.
1165     for (const auto& input_file : input_files_) {
1166       descriptor_pool->AddUnusedImportTrackFile(input_file);
1167     }
1168   }
1169 
1170   bool result = true;
1171   // Parse each file.
1172   for (const auto& input_file : input_files_) {
1173     // Import the file.
1174     const FileDescriptor* parsed_file =
1175         descriptor_pool->FindFileByName(input_file);
1176     if (parsed_file == NULL) {
1177       result = false;
1178       break;
1179     }
1180     parsed_files->push_back(parsed_file);
1181 
1182     // Enforce --disallow_services.
1183     if (disallow_services_ && parsed_file->service_count() > 0) {
1184       std::cerr << parsed_file->name()
1185                 << ": This file contains services, but "
1186                    "--disallow_services was used."
1187                 << std::endl;
1188       result = false;
1189       break;
1190     }
1191 
1192     // Enforce --direct_dependencies
1193     if (direct_dependencies_explicitly_set_) {
1194       bool indirect_imports = false;
1195       for (int i = 0; i < parsed_file->dependency_count(); i++) {
1196         if (direct_dependencies_.find(parsed_file->dependency(i)->name()) ==
1197             direct_dependencies_.end()) {
1198           indirect_imports = true;
1199           std::cerr << parsed_file->name() << ": "
1200                     << StringReplace(direct_dependencies_violation_msg_, "%s",
1201                                      parsed_file->dependency(i)->name(),
1202                                      true /* replace_all */)
1203                     << std::endl;
1204         }
1205       }
1206       if (indirect_imports) {
1207         result = false;
1208         break;
1209       }
1210     }
1211   }
1212   descriptor_pool->ClearUnusedImportTrackFiles();
1213   return result;
1214 }
1215 
Clear()1216 void CommandLineInterface::Clear() {
1217   // Clear all members that are set by Run().  Note that we must not clear
1218   // members which are set by other methods before Run() is called.
1219   executable_name_.clear();
1220   proto_path_.clear();
1221   input_files_.clear();
1222   direct_dependencies_.clear();
1223   direct_dependencies_violation_msg_ = kDefaultDirectDependenciesViolationMsg;
1224   output_directives_.clear();
1225   codec_type_.clear();
1226   descriptor_set_in_names_.clear();
1227   descriptor_set_out_name_.clear();
1228   dependency_out_name_.clear();
1229 
1230 
1231   mode_ = MODE_COMPILE;
1232   print_mode_ = PRINT_NONE;
1233   imports_in_descriptor_set_ = false;
1234   source_info_in_descriptor_set_ = false;
1235   disallow_services_ = false;
1236   direct_dependencies_explicitly_set_ = false;
1237   allow_proto3_optional_ = false;
1238 }
1239 
MakeProtoProtoPathRelative(DiskSourceTree * source_tree,std::string * proto,DescriptorDatabase * fallback_database)1240 bool CommandLineInterface::MakeProtoProtoPathRelative(
1241     DiskSourceTree* source_tree, std::string* proto,
1242     DescriptorDatabase* fallback_database) {
1243   // If it's in the fallback db, don't report non-existent file errors.
1244   FileDescriptorProto fallback_file;
1245   bool in_fallback_database =
1246       fallback_database != nullptr &&
1247       fallback_database->FindFileByName(*proto, &fallback_file);
1248 
1249   // If the input file path is not a physical file path, it must be a virtual
1250   // path.
1251   if (access(proto->c_str(), F_OK) < 0) {
1252     std::string disk_file;
1253     if (source_tree->VirtualFileToDiskFile(*proto, &disk_file) ||
1254         in_fallback_database) {
1255       return true;
1256     } else {
1257       std::cerr << "Could not make proto path relative: " << *proto << ": "
1258                 << strerror(ENOENT) << std::endl;
1259       return false;
1260     }
1261   }
1262 
1263   std::string virtual_file, shadowing_disk_file;
1264   switch (source_tree->DiskFileToVirtualFile(*proto, &virtual_file,
1265                                              &shadowing_disk_file)) {
1266     case DiskSourceTree::SUCCESS:
1267       *proto = virtual_file;
1268       break;
1269     case DiskSourceTree::SHADOWED:
1270       std::cerr << *proto << ": Input is shadowed in the --proto_path by \""
1271                 << shadowing_disk_file
1272                 << "\".  Either use the latter file as your input or reorder "
1273                    "the --proto_path so that the former file's location "
1274                    "comes first."
1275                 << std::endl;
1276       return false;
1277     case DiskSourceTree::CANNOT_OPEN: {
1278       if (in_fallback_database) {
1279         return true;
1280       }
1281       std::string error_str = source_tree->GetLastErrorMessage().empty() ?
1282         strerror(errno) : source_tree->GetLastErrorMessage();
1283       std::cerr << "Could not map to virtual file: " << *proto << ": "
1284                 << error_str << std::endl;
1285       return false;
1286     }
1287     case DiskSourceTree::NO_MAPPING: {
1288       // Try to interpret the path as a virtual path.
1289       std::string disk_file;
1290       if (source_tree->VirtualFileToDiskFile(*proto, &disk_file) ||
1291           in_fallback_database) {
1292         return true;
1293       } else {
1294         // The input file path can't be mapped to any --proto_path and it also
1295         // can't be interpreted as a virtual path.
1296         std::cerr
1297             << *proto
1298             << ": File does not reside within any path "
1299                "specified using --proto_path (or -I).  You must specify a "
1300                "--proto_path which encompasses this file.  Note that the "
1301                "proto_path must be an exact prefix of the .proto file "
1302                "names -- protoc is too dumb to figure out when two paths "
1303                "(e.g. absolute and relative) are equivalent (it's harder "
1304                "than you think)."
1305             << std::endl;
1306         return false;
1307       }
1308     }
1309   }
1310   return true;
1311 }
1312 
MakeInputsBeProtoPathRelative(DiskSourceTree * source_tree,DescriptorDatabase * fallback_database)1313 bool CommandLineInterface::MakeInputsBeProtoPathRelative(
1314     DiskSourceTree* source_tree, DescriptorDatabase* fallback_database) {
1315   for (auto& input_file : input_files_) {
1316     if (!MakeProtoProtoPathRelative(source_tree, &input_file,
1317                                     fallback_database)) {
1318       return false;
1319     }
1320   }
1321 
1322   return true;
1323 }
1324 
1325 
ExpandArgumentFile(const std::string & file,std::vector<std::string> * arguments)1326 bool CommandLineInterface::ExpandArgumentFile(
1327     const std::string& file, std::vector<std::string>* arguments) {
1328   // The argument file is searched in the working directory only. We don't
1329   // use the proto import path here.
1330   std::ifstream file_stream(file.c_str());
1331   if (!file_stream.is_open()) {
1332     return false;
1333   }
1334   std::string argument;
1335   // We don't support any kind of shell expansion right now.
1336   while (std::getline(file_stream, argument)) {
1337     arguments->push_back(argument);
1338   }
1339   return true;
1340 }
1341 
ParseArguments(int argc,const char * const argv[])1342 CommandLineInterface::ParseArgumentStatus CommandLineInterface::ParseArguments(
1343     int argc, const char* const argv[]) {
1344   executable_name_ = argv[0];
1345 
1346   std::vector<std::string> arguments;
1347   for (int i = 1; i < argc; ++i) {
1348     if (argv[i][0] == '@') {
1349       if (!ExpandArgumentFile(argv[i] + 1, &arguments)) {
1350         std::cerr << "Failed to open argument file: " << (argv[i] + 1)
1351                   << std::endl;
1352         return PARSE_ARGUMENT_FAIL;
1353       }
1354       continue;
1355     }
1356     arguments.push_back(argv[i]);
1357   }
1358 
1359   // if no arguments are given, show help
1360   if (arguments.empty()) {
1361     PrintHelpText();
1362     return PARSE_ARGUMENT_DONE_AND_EXIT;  // Exit without running compiler.
1363   }
1364 
1365   // Iterate through all arguments and parse them.
1366   for (int i = 0; i < arguments.size(); ++i) {
1367     std::string name, value;
1368 
1369     if (ParseArgument(arguments[i].c_str(), &name, &value)) {
1370       // Returned true => Use the next argument as the flag value.
1371       if (i + 1 == arguments.size() || arguments[i + 1][0] == '-') {
1372         std::cerr << "Missing value for flag: " << name << std::endl;
1373         if (name == "--decode") {
1374           std::cerr << "To decode an unknown message, use --decode_raw."
1375                     << std::endl;
1376         }
1377         return PARSE_ARGUMENT_FAIL;
1378       } else {
1379         ++i;
1380         value = arguments[i];
1381       }
1382     }
1383 
1384     ParseArgumentStatus status = InterpretArgument(name, value);
1385     if (status != PARSE_ARGUMENT_DONE_AND_CONTINUE) return status;
1386   }
1387 
1388   // Make sure each plugin option has a matching plugin output.
1389   bool foundUnknownPluginOption = false;
1390   for (std::map<std::string, std::string>::const_iterator i =
1391            plugin_parameters_.begin();
1392        i != plugin_parameters_.end(); ++i) {
1393     if (plugins_.find(i->first) != plugins_.end()) {
1394       continue;
1395     }
1396     bool foundImplicitPlugin = false;
1397     for (std::vector<OutputDirective>::const_iterator j =
1398              output_directives_.begin();
1399          j != output_directives_.end(); ++j) {
1400       if (j->generator == NULL) {
1401         std::string plugin_name = PluginName(plugin_prefix_, j->name);
1402         if (plugin_name == i->first) {
1403           foundImplicitPlugin = true;
1404           break;
1405         }
1406       }
1407     }
1408     if (!foundImplicitPlugin) {
1409       std::cerr << "Unknown flag: "
1410                 // strip prefix + "gen-" and add back "_opt"
1411                 << "--" + i->first.substr(plugin_prefix_.size() + 4) + "_opt"
1412                 << std::endl;
1413       foundUnknownPluginOption = true;
1414     }
1415   }
1416   if (foundUnknownPluginOption) {
1417     return PARSE_ARGUMENT_FAIL;
1418   }
1419 
1420   // The --proto_path & --descriptor_set_in flags both specify places to look
1421   // for proto files. If neither were given, use the current working directory.
1422   if (proto_path_.empty() && descriptor_set_in_names_.empty()) {
1423     // Don't use make_pair as the old/default standard library on Solaris
1424     // doesn't support it without explicit template parameters, which are
1425     // incompatible with C++0x's make_pair.
1426     proto_path_.push_back(std::pair<std::string, std::string>("", "."));
1427   }
1428 
1429   // Check some error cases.
1430   bool decoding_raw = (mode_ == MODE_DECODE) && codec_type_.empty();
1431   if (decoding_raw && !input_files_.empty()) {
1432     std::cerr << "When using --decode_raw, no input files should be given."
1433               << std::endl;
1434     return PARSE_ARGUMENT_FAIL;
1435   } else if (!decoding_raw && input_files_.empty()) {
1436     std::cerr << "Missing input file." << std::endl;
1437     return PARSE_ARGUMENT_FAIL;
1438   }
1439   if (mode_ == MODE_COMPILE && output_directives_.empty() &&
1440       descriptor_set_out_name_.empty()) {
1441     std::cerr << "Missing output directives." << std::endl;
1442     return PARSE_ARGUMENT_FAIL;
1443   }
1444   if (mode_ != MODE_COMPILE && !dependency_out_name_.empty()) {
1445     std::cerr << "Can only use --dependency_out=FILE when generating code."
1446               << std::endl;
1447     return PARSE_ARGUMENT_FAIL;
1448   }
1449   if (!dependency_out_name_.empty() && input_files_.size() > 1) {
1450     std::cerr
1451         << "Can only process one input file when using --dependency_out=FILE."
1452         << std::endl;
1453     return PARSE_ARGUMENT_FAIL;
1454   }
1455   if (imports_in_descriptor_set_ && descriptor_set_out_name_.empty()) {
1456     std::cerr << "--include_imports only makes sense when combined with "
1457                  "--descriptor_set_out."
1458               << std::endl;
1459   }
1460   if (source_info_in_descriptor_set_ && descriptor_set_out_name_.empty()) {
1461     std::cerr << "--include_source_info only makes sense when combined with "
1462                  "--descriptor_set_out."
1463               << std::endl;
1464   }
1465 
1466   return PARSE_ARGUMENT_DONE_AND_CONTINUE;
1467 }
1468 
ParseArgument(const char * arg,std::string * name,std::string * value)1469 bool CommandLineInterface::ParseArgument(const char* arg, std::string* name,
1470                                          std::string* value) {
1471   bool parsed_value = false;
1472 
1473   if (arg[0] != '-') {
1474     // Not a flag.
1475     name->clear();
1476     parsed_value = true;
1477     *value = arg;
1478   } else if (arg[1] == '-') {
1479     // Two dashes:  Multi-character name, with '=' separating name and
1480     //   value.
1481     const char* equals_pos = strchr(arg, '=');
1482     if (equals_pos != NULL) {
1483       *name = std::string(arg, equals_pos - arg);
1484       *value = equals_pos + 1;
1485       parsed_value = true;
1486     } else {
1487       *name = arg;
1488     }
1489   } else {
1490     // One dash:  One-character name, all subsequent characters are the
1491     //   value.
1492     if (arg[1] == '\0') {
1493       // arg is just "-".  We treat this as an input file, except that at
1494       // present this will just lead to a "file not found" error.
1495       name->clear();
1496       *value = arg;
1497       parsed_value = true;
1498     } else {
1499       *name = std::string(arg, 2);
1500       *value = arg + 2;
1501       parsed_value = !value->empty();
1502     }
1503   }
1504 
1505   // Need to return true iff the next arg should be used as the value for this
1506   // one, false otherwise.
1507 
1508   if (parsed_value) {
1509     // We already parsed a value for this flag.
1510     return false;
1511   }
1512 
1513   if (*name == "-h" || *name == "--help" || *name == "--disallow_services" ||
1514       *name == "--include_imports" || *name == "--include_source_info" ||
1515       *name == "--version" || *name == "--decode_raw" ||
1516       *name == "--print_free_field_numbers" ||
1517       *name == "--experimental_allow_proto3_optional") {
1518     // HACK:  These are the only flags that don't take a value.
1519     //   They probably should not be hard-coded like this but for now it's
1520     //   not worth doing better.
1521     return false;
1522   }
1523 
1524   // Next argument is the flag value.
1525   return true;
1526 }
1527 
1528 CommandLineInterface::ParseArgumentStatus
InterpretArgument(const std::string & name,const std::string & value)1529 CommandLineInterface::InterpretArgument(const std::string& name,
1530                                         const std::string& value) {
1531   if (name.empty()) {
1532     // Not a flag.  Just a filename.
1533     if (value.empty()) {
1534       std::cerr
1535           << "You seem to have passed an empty string as one of the "
1536              "arguments to "
1537           << executable_name_
1538           << ".  This is actually "
1539              "sort of hard to do.  Congrats.  Unfortunately it is not valid "
1540              "input so the program is going to die now."
1541           << std::endl;
1542       return PARSE_ARGUMENT_FAIL;
1543     }
1544 
1545 #if defined(_WIN32)
1546     // On Windows, the shell (typically cmd.exe) does not expand wildcards in
1547     // file names (e.g. foo\*.proto), so we do it ourselves.
1548     switch (google::protobuf::io::win32::ExpandWildcards(
1549         value,
1550         [this](const string& path) { this->input_files_.push_back(path); })) {
1551       case google::protobuf::io::win32::ExpandWildcardsResult::kSuccess:
1552         break;
1553       case google::protobuf::io::win32::ExpandWildcardsResult::
1554           kErrorNoMatchingFile:
1555         // Path does not exist, is not a file, or it's longer than MAX_PATH and
1556         // long path handling is disabled.
1557         std::cerr << "Invalid file name pattern or missing input file \""
1558                   << value << "\"" << std::endl;
1559         return PARSE_ARGUMENT_FAIL;
1560       default:
1561         std::cerr << "Cannot convert path \"" << value
1562                   << "\" to or from Windows style" << std::endl;
1563         return PARSE_ARGUMENT_FAIL;
1564     }
1565 #else   // not _WIN32
1566     // On other platforms than Windows (e.g. Linux, Mac OS) the shell (typically
1567     // Bash) expands wildcards.
1568     input_files_.push_back(value);
1569 #endif  // _WIN32
1570 
1571   } else if (name == "-I" || name == "--proto_path") {
1572     // Java's -classpath (and some other languages) delimits path components
1573     // with colons.  Let's accept that syntax too just to make things more
1574     // intuitive.
1575     std::vector<std::string> parts = Split(
1576         value, CommandLineInterface::kPathSeparator,
1577         true);
1578 
1579     for (int i = 0; i < parts.size(); i++) {
1580       std::string virtual_path;
1581       std::string disk_path;
1582 
1583       std::string::size_type equals_pos = parts[i].find_first_of('=');
1584       if (equals_pos == std::string::npos) {
1585         virtual_path = "";
1586         disk_path = parts[i];
1587       } else {
1588         virtual_path = parts[i].substr(0, equals_pos);
1589         disk_path = parts[i].substr(equals_pos + 1);
1590       }
1591 
1592       if (disk_path.empty()) {
1593         std::cerr
1594             << "--proto_path passed empty directory name.  (Use \".\" for "
1595                "current directory.)"
1596             << std::endl;
1597         return PARSE_ARGUMENT_FAIL;
1598       }
1599 
1600       // Make sure disk path exists, warn otherwise.
1601       if (access(disk_path.c_str(), F_OK) < 0) {
1602         // Try the original path; it may have just happened to have a '=' in it.
1603         if (access(parts[i].c_str(), F_OK) < 0) {
1604           std::cerr << disk_path << ": warning: directory does not exist."
1605                     << std::endl;
1606         } else {
1607           virtual_path = "";
1608           disk_path = parts[i];
1609         }
1610       }
1611 
1612       // Don't use make_pair as the old/default standard library on Solaris
1613       // doesn't support it without explicit template parameters, which are
1614       // incompatible with C++0x's make_pair.
1615       proto_path_.push_back(
1616           std::pair<std::string, std::string>(virtual_path, disk_path));
1617     }
1618 
1619   } else if (name == "--direct_dependencies") {
1620     if (direct_dependencies_explicitly_set_) {
1621       std::cerr << name
1622                 << " may only be passed once. To specify multiple "
1623                    "direct dependencies, pass them all as a single "
1624                    "parameter separated by ':'."
1625                 << std::endl;
1626       return PARSE_ARGUMENT_FAIL;
1627     }
1628 
1629     direct_dependencies_explicitly_set_ = true;
1630     std::vector<std::string> direct =
1631         Split(value, ":", true);
1632     GOOGLE_DCHECK(direct_dependencies_.empty());
1633     direct_dependencies_.insert(direct.begin(), direct.end());
1634 
1635   } else if (name == "--direct_dependencies_violation_msg") {
1636     direct_dependencies_violation_msg_ = value;
1637 
1638   } else if (name == "--descriptor_set_in") {
1639     if (!descriptor_set_in_names_.empty()) {
1640       std::cerr << name
1641                 << " may only be passed once. To specify multiple "
1642                    "descriptor sets, pass them all as a single "
1643                    "parameter separated by '"
1644                 << CommandLineInterface::kPathSeparator << "'." << std::endl;
1645       return PARSE_ARGUMENT_FAIL;
1646     }
1647     if (value.empty()) {
1648       std::cerr << name << " requires a non-empty value." << std::endl;
1649       return PARSE_ARGUMENT_FAIL;
1650     }
1651     if (!dependency_out_name_.empty()) {
1652       std::cerr << name << " cannot be used with --dependency_out."
1653                 << std::endl;
1654       return PARSE_ARGUMENT_FAIL;
1655     }
1656 
1657     descriptor_set_in_names_ = Split(
1658         value, CommandLineInterface::kPathSeparator,
1659         true);
1660 
1661   } else if (name == "-o" || name == "--descriptor_set_out") {
1662     if (!descriptor_set_out_name_.empty()) {
1663       std::cerr << name << " may only be passed once." << std::endl;
1664       return PARSE_ARGUMENT_FAIL;
1665     }
1666     if (value.empty()) {
1667       std::cerr << name << " requires a non-empty value." << std::endl;
1668       return PARSE_ARGUMENT_FAIL;
1669     }
1670     if (mode_ != MODE_COMPILE) {
1671       std::cerr
1672           << "Cannot use --encode or --decode and generate descriptors at the "
1673              "same time."
1674           << std::endl;
1675       return PARSE_ARGUMENT_FAIL;
1676     }
1677     descriptor_set_out_name_ = value;
1678 
1679   } else if (name == "--dependency_out") {
1680     if (!dependency_out_name_.empty()) {
1681       std::cerr << name << " may only be passed once." << std::endl;
1682       return PARSE_ARGUMENT_FAIL;
1683     }
1684     if (value.empty()) {
1685       std::cerr << name << " requires a non-empty value." << std::endl;
1686       return PARSE_ARGUMENT_FAIL;
1687     }
1688     if (!descriptor_set_in_names_.empty()) {
1689       std::cerr << name << " cannot be used with --descriptor_set_in."
1690                 << std::endl;
1691       return PARSE_ARGUMENT_FAIL;
1692     }
1693     dependency_out_name_ = value;
1694 
1695   } else if (name == "--include_imports") {
1696     if (imports_in_descriptor_set_) {
1697       std::cerr << name << " may only be passed once." << std::endl;
1698       return PARSE_ARGUMENT_FAIL;
1699     }
1700     imports_in_descriptor_set_ = true;
1701 
1702   } else if (name == "--include_source_info") {
1703     if (source_info_in_descriptor_set_) {
1704       std::cerr << name << " may only be passed once." << std::endl;
1705       return PARSE_ARGUMENT_FAIL;
1706     }
1707     source_info_in_descriptor_set_ = true;
1708 
1709   } else if (name == "-h" || name == "--help") {
1710     PrintHelpText();
1711     return PARSE_ARGUMENT_DONE_AND_EXIT;  // Exit without running compiler.
1712 
1713   } else if (name == "--version") {
1714     if (!version_info_.empty()) {
1715       std::cout << version_info_ << std::endl;
1716     }
1717     std::cout << "libprotoc " << internal::VersionString(PROTOBUF_VERSION)
1718               << std::endl;
1719     return PARSE_ARGUMENT_DONE_AND_EXIT;  // Exit without running compiler.
1720 
1721   } else if (name == "--disallow_services") {
1722     disallow_services_ = true;
1723 
1724   } else if (name == "--experimental_allow_proto3_optional") {
1725     allow_proto3_optional_ = true;
1726 
1727   } else if (name == "--encode" || name == "--decode" ||
1728              name == "--decode_raw") {
1729     if (mode_ != MODE_COMPILE) {
1730       std::cerr << "Only one of --encode and --decode can be specified."
1731                 << std::endl;
1732       return PARSE_ARGUMENT_FAIL;
1733     }
1734     if (!output_directives_.empty() || !descriptor_set_out_name_.empty()) {
1735       std::cerr << "Cannot use " << name
1736                 << " and generate code or descriptors at the same time."
1737                 << std::endl;
1738       return PARSE_ARGUMENT_FAIL;
1739     }
1740 
1741     mode_ = (name == "--encode") ? MODE_ENCODE : MODE_DECODE;
1742 
1743     if (value.empty() && name != "--decode_raw") {
1744       std::cerr << "Type name for " << name << " cannot be blank." << std::endl;
1745       if (name == "--decode") {
1746         std::cerr << "To decode an unknown message, use --decode_raw."
1747                   << std::endl;
1748       }
1749       return PARSE_ARGUMENT_FAIL;
1750     } else if (!value.empty() && name == "--decode_raw") {
1751       std::cerr << "--decode_raw does not take a parameter." << std::endl;
1752       return PARSE_ARGUMENT_FAIL;
1753     }
1754 
1755     codec_type_ = value;
1756 
1757   } else if (name == "--error_format") {
1758     if (value == "gcc") {
1759       error_format_ = ERROR_FORMAT_GCC;
1760     } else if (value == "msvs") {
1761       error_format_ = ERROR_FORMAT_MSVS;
1762     } else {
1763       std::cerr << "Unknown error format: " << value << std::endl;
1764       return PARSE_ARGUMENT_FAIL;
1765     }
1766 
1767   } else if (name == "--plugin") {
1768     if (plugin_prefix_.empty()) {
1769       std::cerr << "This compiler does not support plugins." << std::endl;
1770       return PARSE_ARGUMENT_FAIL;
1771     }
1772 
1773     std::string plugin_name;
1774     std::string path;
1775 
1776     std::string::size_type equals_pos = value.find_first_of('=');
1777     if (equals_pos == std::string::npos) {
1778       // Use the basename of the file.
1779       std::string::size_type slash_pos = value.find_last_of('/');
1780       if (slash_pos == std::string::npos) {
1781         plugin_name = value;
1782       } else {
1783         plugin_name = value.substr(slash_pos + 1);
1784       }
1785       path = value;
1786     } else {
1787       plugin_name = value.substr(0, equals_pos);
1788       path = value.substr(equals_pos + 1);
1789     }
1790 
1791     plugins_[plugin_name] = path;
1792 
1793   } else if (name == "--print_free_field_numbers") {
1794     if (mode_ != MODE_COMPILE) {
1795       std::cerr << "Cannot use " << name
1796                 << " and use --encode, --decode or print "
1797                 << "other info at the same time." << std::endl;
1798       return PARSE_ARGUMENT_FAIL;
1799     }
1800     if (!output_directives_.empty() || !descriptor_set_out_name_.empty()) {
1801       std::cerr << "Cannot use " << name
1802                 << " and generate code or descriptors at the same time."
1803                 << std::endl;
1804       return PARSE_ARGUMENT_FAIL;
1805     }
1806     mode_ = MODE_PRINT;
1807     print_mode_ = PRINT_FREE_FIELDS;
1808   } else {
1809     // Some other flag.  Look it up in the generators list.
1810     const GeneratorInfo* generator_info =
1811         FindOrNull(generators_by_flag_name_, name);
1812     if (generator_info == NULL &&
1813         (plugin_prefix_.empty() || !HasSuffixString(name, "_out"))) {
1814       // Check if it's a generator option flag.
1815       generator_info = FindOrNull(generators_by_option_name_, name);
1816       if (generator_info != NULL) {
1817         std::string* parameters =
1818             &generator_parameters_[generator_info->flag_name];
1819         if (!parameters->empty()) {
1820           parameters->append(",");
1821         }
1822         parameters->append(value);
1823       } else if (HasPrefixString(name, "--") && HasSuffixString(name, "_opt")) {
1824         std::string* parameters =
1825             &plugin_parameters_[PluginName(plugin_prefix_, name)];
1826         if (!parameters->empty()) {
1827           parameters->append(",");
1828         }
1829         parameters->append(value);
1830       } else {
1831         std::cerr << "Unknown flag: " << name << std::endl;
1832         return PARSE_ARGUMENT_FAIL;
1833       }
1834     } else {
1835       // It's an output flag.  Add it to the output directives.
1836       if (mode_ != MODE_COMPILE) {
1837         std::cerr << "Cannot use --encode, --decode or print .proto info and "
1838                      "generate code at the same time."
1839                   << std::endl;
1840         return PARSE_ARGUMENT_FAIL;
1841       }
1842 
1843       OutputDirective directive;
1844       directive.name = name;
1845       if (generator_info == NULL) {
1846         directive.generator = NULL;
1847       } else {
1848         directive.generator = generator_info->generator;
1849       }
1850 
1851       // Split value at ':' to separate the generator parameter from the
1852       // filename.  However, avoid doing this if the colon is part of a valid
1853       // Windows-style absolute path.
1854       std::string::size_type colon_pos = value.find_first_of(':');
1855       if (colon_pos == std::string::npos || IsWindowsAbsolutePath(value)) {
1856         directive.output_location = value;
1857       } else {
1858         directive.parameter = value.substr(0, colon_pos);
1859         directive.output_location = value.substr(colon_pos + 1);
1860       }
1861 
1862       output_directives_.push_back(directive);
1863     }
1864   }
1865 
1866   return PARSE_ARGUMENT_DONE_AND_CONTINUE;
1867 }
1868 
PrintHelpText()1869 void CommandLineInterface::PrintHelpText() {
1870   // Sorry for indentation here; line wrapping would be uglier.
1871   std::cout
1872       <<
1873       "Usage: " << executable_name_
1874       << " [OPTION] PROTO_FILES\n"
1875          "Parse PROTO_FILES and generate output based on the options given:\n"
1876          "  -IPATH, --proto_path=PATH   Specify the directory in which to "
1877          "search for\n"
1878          "                              imports.  May be specified multiple "
1879          "times;\n"
1880          "                              directories will be searched in order. "
1881          " If not\n"
1882          "                              given, the current working directory "
1883          "is used.\n"
1884          "                              If not found in any of the these "
1885          "directories,\n"
1886          "                              the --descriptor_set_in descriptors "
1887          "will be\n"
1888          "                              checked for required proto file.\n"
1889          "  --version                   Show version info and exit.\n"
1890          "  -h, --help                  Show this text and exit.\n"
1891          "  --encode=MESSAGE_TYPE       Read a text-format message of the "
1892          "given type\n"
1893          "                              from standard input and write it in "
1894          "binary\n"
1895          "                              to standard output.  The message type "
1896          "must\n"
1897          "                              be defined in PROTO_FILES or their "
1898          "imports.\n"
1899          "  --decode=MESSAGE_TYPE       Read a binary message of the given "
1900          "type from\n"
1901          "                              standard input and write it in text "
1902          "format\n"
1903          "                              to standard output.  The message type "
1904          "must\n"
1905          "                              be defined in PROTO_FILES or their "
1906          "imports.\n"
1907          "  --decode_raw                Read an arbitrary protocol message "
1908          "from\n"
1909          "                              standard input and write the raw "
1910          "tag/value\n"
1911          "                              pairs in text format to standard "
1912          "output.  No\n"
1913          "                              PROTO_FILES should be given when using "
1914          "this\n"
1915          "                              flag.\n"
1916          "  --descriptor_set_in=FILES   Specifies a delimited list of FILES\n"
1917          "                              each containing a FileDescriptorSet "
1918          "(a\n"
1919          "                              protocol buffer defined in "
1920          "descriptor.proto).\n"
1921          "                              The FileDescriptor for each of the "
1922          "PROTO_FILES\n"
1923          "                              provided will be loaded from these\n"
1924          "                              FileDescriptorSets. If a "
1925          "FileDescriptor\n"
1926          "                              appears multiple times, the first "
1927          "occurrence\n"
1928          "                              will be used.\n"
1929          "  -oFILE,                     Writes a FileDescriptorSet (a protocol "
1930          "buffer,\n"
1931          "    --descriptor_set_out=FILE defined in descriptor.proto) "
1932          "containing all of\n"
1933          "                              the input files to FILE.\n"
1934          "  --include_imports           When using --descriptor_set_out, also "
1935          "include\n"
1936          "                              all dependencies of the input files in "
1937          "the\n"
1938          "                              set, so that the set is "
1939          "self-contained.\n"
1940          "  --include_source_info       When using --descriptor_set_out, do "
1941          "not strip\n"
1942          "                              SourceCodeInfo from the "
1943          "FileDescriptorProto.\n"
1944          "                              This results in vastly larger "
1945          "descriptors that\n"
1946          "                              include information about the "
1947          "original\n"
1948          "                              location of each decl in the source "
1949          "file as\n"
1950          "                              well as surrounding comments.\n"
1951          "  --dependency_out=FILE       Write a dependency output file in the "
1952          "format\n"
1953          "                              expected by make. This writes the "
1954          "transitive\n"
1955          "                              set of input file paths to FILE\n"
1956          "  --error_format=FORMAT       Set the format in which to print "
1957          "errors.\n"
1958          "                              FORMAT may be 'gcc' (the default) or "
1959          "'msvs'\n"
1960          "                              (Microsoft Visual Studio format).\n"
1961          "  --print_free_field_numbers  Print the free field numbers of the "
1962          "messages\n"
1963          "                              defined in the given proto files. "
1964          "Groups share\n"
1965          "                              the same field number space with the "
1966          "parent \n"
1967          "                              message. Extension ranges are counted "
1968          "as \n"
1969          "                              occupied fields numbers.\n"
1970       << std::endl;
1971   if (!plugin_prefix_.empty()) {
1972     std::cout
1973         << "  --plugin=EXECUTABLE         Specifies a plugin executable to "
1974            "use.\n"
1975            "                              Normally, protoc searches the PATH "
1976            "for\n"
1977            "                              plugins, but you may specify "
1978            "additional\n"
1979            "                              executables not in the path using "
1980            "this flag.\n"
1981            "                              Additionally, EXECUTABLE may be of "
1982            "the form\n"
1983            "                              NAME=PATH, in which case the given "
1984            "plugin name\n"
1985            "                              is mapped to the given executable "
1986            "even if\n"
1987            "                              the executable's own name differs."
1988         << std::endl;
1989   }
1990 
1991   for (GeneratorMap::iterator iter = generators_by_flag_name_.begin();
1992        iter != generators_by_flag_name_.end(); ++iter) {
1993     // FIXME(kenton):  If the text is long enough it will wrap, which is ugly,
1994     //   but fixing this nicely (e.g. splitting on spaces) is probably more
1995     //   trouble than it's worth.
1996     std::cout << "  " << iter->first << "=OUT_DIR "
1997               << std::string(19 - iter->first.size(),
1998                              ' ')  // Spaces for alignment.
1999               << iter->second.help_text << std::endl;
2000   }
2001   std::cout << "  @<filename>                 Read options and filenames from "
2002                "file. If a\n"
2003                "                              relative file path is specified, "
2004                "the file\n"
2005                "                              will be searched in the working "
2006                "directory.\n"
2007                "                              The --proto_path option will not "
2008                "affect how\n"
2009                "                              this argument file is searched. "
2010                "Content of\n"
2011                "                              the file will be expanded in the "
2012                "position of\n"
2013                "                              @<filename> as in the argument "
2014                "list. Note\n"
2015                "                              that shell expansion is not "
2016                "applied to the\n"
2017                "                              content of the file (i.e., you "
2018                "cannot use\n"
2019                "                              quotes, wildcards, escapes, "
2020                "commands, etc.).\n"
2021                "                              Each line corresponds to a "
2022                "single argument,\n"
2023                "                              even if it contains spaces."
2024             << std::endl;
2025 }
2026 
EnforceProto3OptionalSupport(const std::string & codegen_name,uint64 supported_features,const std::vector<const FileDescriptor * > & parsed_files) const2027 bool CommandLineInterface::EnforceProto3OptionalSupport(
2028     const std::string& codegen_name, uint64 supported_features,
2029     const std::vector<const FileDescriptor*>& parsed_files) const {
2030   bool supports_proto3_optional =
2031       supported_features & CodeGenerator::FEATURE_PROTO3_OPTIONAL;
2032   if (!supports_proto3_optional) {
2033     for (const auto fd : parsed_files) {
2034       if (ContainsProto3Optional(fd)) {
2035         std::cerr << fd->name()
2036                   << ": is a proto3 file that contains optional fields, but "
2037                      "code generator "
2038                   << codegen_name
2039                   << " hasn't been updated to support optional fields in "
2040                      "proto3. Please ask the owner of this code generator to "
2041                      "support proto3 optional.";
2042         return false;
2043       }
2044     }
2045   }
2046   return true;
2047 }
2048 
GenerateOutput(const std::vector<const FileDescriptor * > & parsed_files,const OutputDirective & output_directive,GeneratorContext * generator_context)2049 bool CommandLineInterface::GenerateOutput(
2050     const std::vector<const FileDescriptor*>& parsed_files,
2051     const OutputDirective& output_directive,
2052     GeneratorContext* generator_context) {
2053   // Call the generator.
2054   std::string error;
2055   if (output_directive.generator == NULL) {
2056     // This is a plugin.
2057     GOOGLE_CHECK(HasPrefixString(output_directive.name, "--") &&
2058           HasSuffixString(output_directive.name, "_out"))
2059         << "Bad name for plugin generator: " << output_directive.name;
2060 
2061     std::string plugin_name = PluginName(plugin_prefix_, output_directive.name);
2062     std::string parameters = output_directive.parameter;
2063     if (!plugin_parameters_[plugin_name].empty()) {
2064       if (!parameters.empty()) {
2065         parameters.append(",");
2066       }
2067       parameters.append(plugin_parameters_[plugin_name]);
2068     }
2069     if (!GeneratePluginOutput(parsed_files, plugin_name, parameters,
2070                               generator_context, &error)) {
2071       std::cerr << output_directive.name << ": " << error << std::endl;
2072       return false;
2073     }
2074   } else {
2075     // Regular generator.
2076     std::string parameters = output_directive.parameter;
2077     if (!generator_parameters_[output_directive.name].empty()) {
2078       if (!parameters.empty()) {
2079         parameters.append(",");
2080       }
2081       parameters.append(generator_parameters_[output_directive.name]);
2082     }
2083     if (!EnforceProto3OptionalSupport(
2084             output_directive.name,
2085             output_directive.generator->GetSupportedFeatures(), parsed_files)) {
2086       return false;
2087     }
2088 
2089     if (!output_directive.generator->GenerateAll(parsed_files, parameters,
2090                                                  generator_context, &error)) {
2091       // Generator returned an error.
2092       std::cerr << output_directive.name << ": " << error << std::endl;
2093       return false;
2094     }
2095   }
2096 
2097   return true;
2098 }
2099 
GenerateDependencyManifestFile(const std::vector<const FileDescriptor * > & parsed_files,const GeneratorContextMap & output_directories,DiskSourceTree * source_tree)2100 bool CommandLineInterface::GenerateDependencyManifestFile(
2101     const std::vector<const FileDescriptor*>& parsed_files,
2102     const GeneratorContextMap& output_directories,
2103     DiskSourceTree* source_tree) {
2104   FileDescriptorSet file_set;
2105 
2106   std::set<const FileDescriptor*> already_seen;
2107   for (int i = 0; i < parsed_files.size(); i++) {
2108     GetTransitiveDependencies(parsed_files[i], false, false, &already_seen,
2109                               file_set.mutable_file());
2110   }
2111 
2112   std::vector<std::string> output_filenames;
2113   for (const auto& pair : output_directories) {
2114     const std::string& location = pair.first;
2115     GeneratorContextImpl* directory = pair.second.get();
2116     std::vector<std::string> relative_output_filenames;
2117     directory->GetOutputFilenames(&relative_output_filenames);
2118     for (int i = 0; i < relative_output_filenames.size(); i++) {
2119       std::string output_filename = location + relative_output_filenames[i];
2120       if (output_filename.compare(0, 2, "./") == 0) {
2121         output_filename = output_filename.substr(2);
2122       }
2123       output_filenames.push_back(output_filename);
2124     }
2125   }
2126 
2127   int fd;
2128   do {
2129     fd = open(dependency_out_name_.c_str(),
2130               O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
2131   } while (fd < 0 && errno == EINTR);
2132 
2133   if (fd < 0) {
2134     perror(dependency_out_name_.c_str());
2135     return false;
2136   }
2137 
2138   io::FileOutputStream out(fd);
2139   io::Printer printer(&out, '$');
2140 
2141   for (int i = 0; i < output_filenames.size(); i++) {
2142     printer.Print(output_filenames[i].c_str());
2143     if (i == output_filenames.size() - 1) {
2144       printer.Print(":");
2145     } else {
2146       printer.Print(" \\\n");
2147     }
2148   }
2149 
2150   for (int i = 0; i < file_set.file_size(); i++) {
2151     const FileDescriptorProto& file = file_set.file(i);
2152     const std::string& virtual_file = file.name();
2153     std::string disk_file;
2154     if (source_tree &&
2155         source_tree->VirtualFileToDiskFile(virtual_file, &disk_file)) {
2156       printer.Print(" $disk_file$", "disk_file", disk_file);
2157       if (i < file_set.file_size() - 1) printer.Print("\\\n");
2158     } else {
2159       std::cerr << "Unable to identify path for file " << virtual_file
2160                 << std::endl;
2161       return false;
2162     }
2163   }
2164 
2165   return true;
2166 }
2167 
GeneratePluginOutput(const std::vector<const FileDescriptor * > & parsed_files,const std::string & plugin_name,const std::string & parameter,GeneratorContext * generator_context,std::string * error)2168 bool CommandLineInterface::GeneratePluginOutput(
2169     const std::vector<const FileDescriptor*>& parsed_files,
2170     const std::string& plugin_name, const std::string& parameter,
2171     GeneratorContext* generator_context, std::string* error) {
2172   CodeGeneratorRequest request;
2173   CodeGeneratorResponse response;
2174   std::string processed_parameter = parameter;
2175 
2176 
2177   // Build the request.
2178   if (!processed_parameter.empty()) {
2179     request.set_parameter(processed_parameter);
2180   }
2181 
2182 
2183   std::set<const FileDescriptor*> already_seen;
2184   for (int i = 0; i < parsed_files.size(); i++) {
2185     request.add_file_to_generate(parsed_files[i]->name());
2186     GetTransitiveDependencies(parsed_files[i],
2187                               true,  // Include json_name for plugins.
2188                               true,  // Include source code info.
2189                               &already_seen, request.mutable_proto_file());
2190   }
2191 
2192   google::protobuf::compiler::Version* version =
2193       request.mutable_compiler_version();
2194   version->set_major(PROTOBUF_VERSION / 1000000);
2195   version->set_minor(PROTOBUF_VERSION / 1000 % 1000);
2196   version->set_patch(PROTOBUF_VERSION % 1000);
2197   version->set_suffix(PROTOBUF_VERSION_SUFFIX);
2198 
2199   // Invoke the plugin.
2200   Subprocess subprocess;
2201 
2202   if (plugins_.count(plugin_name) > 0) {
2203     subprocess.Start(plugins_[plugin_name], Subprocess::EXACT_NAME);
2204   } else {
2205     subprocess.Start(plugin_name, Subprocess::SEARCH_PATH);
2206   }
2207 
2208   std::string communicate_error;
2209   if (!subprocess.Communicate(request, &response, &communicate_error)) {
2210     *error = strings::Substitute("$0: $1", plugin_name, communicate_error);
2211     return false;
2212   }
2213 
2214   // Write the files.  We do this even if there was a generator error in order
2215   // to match the behavior of a compiled-in generator.
2216   std::unique_ptr<io::ZeroCopyOutputStream> current_output;
2217   for (int i = 0; i < response.file_size(); i++) {
2218     const CodeGeneratorResponse::File& output_file = response.file(i);
2219 
2220     if (!output_file.insertion_point().empty()) {
2221       std::string filename = output_file.name();
2222       // Open a file for insert.
2223       // We reset current_output to NULL first so that the old file is closed
2224       // before the new one is opened.
2225       current_output.reset();
2226       current_output.reset(generator_context->OpenForInsert(
2227           filename, output_file.insertion_point()));
2228     } else if (!output_file.name().empty()) {
2229       // Starting a new file.  Open it.
2230       // We reset current_output to NULL first so that the old file is closed
2231       // before the new one is opened.
2232       current_output.reset();
2233       current_output.reset(generator_context->Open(output_file.name()));
2234     } else if (current_output == NULL) {
2235       *error = strings::Substitute(
2236           "$0: First file chunk returned by plugin did not specify a file "
2237           "name.",
2238           plugin_name);
2239       return false;
2240     }
2241 
2242     // Use CodedOutputStream for convenience; otherwise we'd need to provide
2243     // our own buffer-copying loop.
2244     io::CodedOutputStream writer(current_output.get());
2245     writer.WriteString(output_file.content());
2246   }
2247 
2248   // Check for errors.
2249   if (!response.error().empty()) {
2250     // Generator returned an error.
2251     *error = response.error();
2252     return false;
2253   } else if (!EnforceProto3OptionalSupport(
2254                  plugin_name, response.supported_features(), parsed_files)) {
2255     return false;
2256   }
2257 
2258   return true;
2259 }
2260 
EncodeOrDecode(const DescriptorPool * pool)2261 bool CommandLineInterface::EncodeOrDecode(const DescriptorPool* pool) {
2262   // Look up the type.
2263   const Descriptor* type = pool->FindMessageTypeByName(codec_type_);
2264   if (type == NULL) {
2265     std::cerr << "Type not defined: " << codec_type_ << std::endl;
2266     return false;
2267   }
2268 
2269   DynamicMessageFactory dynamic_factory(pool);
2270   std::unique_ptr<Message> message(dynamic_factory.GetPrototype(type)->New());
2271 
2272   if (mode_ == MODE_ENCODE) {
2273     SetFdToTextMode(STDIN_FILENO);
2274     SetFdToBinaryMode(STDOUT_FILENO);
2275   } else {
2276     SetFdToBinaryMode(STDIN_FILENO);
2277     SetFdToTextMode(STDOUT_FILENO);
2278   }
2279 
2280   io::FileInputStream in(STDIN_FILENO);
2281   io::FileOutputStream out(STDOUT_FILENO);
2282 
2283   if (mode_ == MODE_ENCODE) {
2284     // Input is text.
2285     ErrorPrinter error_collector(error_format_);
2286     TextFormat::Parser parser;
2287     parser.RecordErrorsTo(&error_collector);
2288     parser.AllowPartialMessage(true);
2289 
2290     if (!parser.Parse(&in, message.get())) {
2291       std::cerr << "Failed to parse input." << std::endl;
2292       return false;
2293     }
2294   } else {
2295     // Input is binary.
2296     if (!message->ParsePartialFromZeroCopyStream(&in)) {
2297       std::cerr << "Failed to parse input." << std::endl;
2298       return false;
2299     }
2300   }
2301 
2302   if (!message->IsInitialized()) {
2303     std::cerr << "warning:  Input message is missing required fields:  "
2304               << message->InitializationErrorString() << std::endl;
2305   }
2306 
2307   if (mode_ == MODE_ENCODE) {
2308     // Output is binary.
2309     if (!message->SerializePartialToZeroCopyStream(&out)) {
2310       std::cerr << "output: I/O error." << std::endl;
2311       return false;
2312     }
2313   } else {
2314     // Output is text.
2315     if (!TextFormat::Print(*message, &out)) {
2316       std::cerr << "output: I/O error." << std::endl;
2317       return false;
2318     }
2319   }
2320 
2321   return true;
2322 }
2323 
WriteDescriptorSet(const std::vector<const FileDescriptor * > & parsed_files)2324 bool CommandLineInterface::WriteDescriptorSet(
2325     const std::vector<const FileDescriptor*>& parsed_files) {
2326   FileDescriptorSet file_set;
2327 
2328   std::set<const FileDescriptor*> already_seen;
2329   if (!imports_in_descriptor_set_) {
2330     // Since we don't want to output transitive dependencies, but we do want
2331     // things to be in dependency order, add all dependencies that aren't in
2332     // parsed_files to already_seen.  This will short circuit the recursion
2333     // in GetTransitiveDependencies.
2334     std::set<const FileDescriptor*> to_output;
2335     to_output.insert(parsed_files.begin(), parsed_files.end());
2336     for (int i = 0; i < parsed_files.size(); i++) {
2337       const FileDescriptor* file = parsed_files[i];
2338       for (int i = 0; i < file->dependency_count(); i++) {
2339         const FileDescriptor* dependency = file->dependency(i);
2340         // if the dependency isn't in parsed files, mark it as already seen
2341         if (to_output.find(dependency) == to_output.end()) {
2342           already_seen.insert(dependency);
2343         }
2344       }
2345     }
2346   }
2347   for (int i = 0; i < parsed_files.size(); i++) {
2348     GetTransitiveDependencies(parsed_files[i],
2349                               true,  // Include json_name
2350                               source_info_in_descriptor_set_, &already_seen,
2351                               file_set.mutable_file());
2352   }
2353 
2354   int fd;
2355   do {
2356     fd = open(descriptor_set_out_name_.c_str(),
2357               O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
2358   } while (fd < 0 && errno == EINTR);
2359 
2360   if (fd < 0) {
2361     perror(descriptor_set_out_name_.c_str());
2362     return false;
2363   }
2364 
2365   io::FileOutputStream out(fd);
2366 
2367   {
2368     io::CodedOutputStream coded_out(&out);
2369     // Determinism is useful here because build outputs are sometimes checked
2370     // into version control.
2371     coded_out.SetSerializationDeterministic(true);
2372     if (!file_set.SerializeToCodedStream(&coded_out)) {
2373       std::cerr << descriptor_set_out_name_ << ": " << strerror(out.GetErrno())
2374                 << std::endl;
2375       out.Close();
2376       return false;
2377     }
2378   }
2379 
2380   if (!out.Close()) {
2381     std::cerr << descriptor_set_out_name_ << ": " << strerror(out.GetErrno())
2382               << std::endl;
2383     return false;
2384   }
2385 
2386   return true;
2387 }
2388 
GetTransitiveDependencies(const FileDescriptor * file,bool include_json_name,bool include_source_code_info,std::set<const FileDescriptor * > * already_seen,RepeatedPtrField<FileDescriptorProto> * output)2389 void CommandLineInterface::GetTransitiveDependencies(
2390     const FileDescriptor* file, bool include_json_name,
2391     bool include_source_code_info,
2392     std::set<const FileDescriptor*>* already_seen,
2393     RepeatedPtrField<FileDescriptorProto>* output) {
2394   if (!already_seen->insert(file).second) {
2395     // Already saw this file.  Skip.
2396     return;
2397   }
2398 
2399   // Add all dependencies.
2400   for (int i = 0; i < file->dependency_count(); i++) {
2401     GetTransitiveDependencies(file->dependency(i), include_json_name,
2402                               include_source_code_info, already_seen, output);
2403   }
2404 
2405   // Add this file.
2406   FileDescriptorProto* new_descriptor = output->Add();
2407   file->CopyTo(new_descriptor);
2408   if (include_json_name) {
2409     file->CopyJsonNameTo(new_descriptor);
2410   }
2411   if (include_source_code_info) {
2412     file->CopySourceCodeInfoTo(new_descriptor);
2413   }
2414 }
2415 
2416 namespace {
2417 
2418 // Utility function for PrintFreeFieldNumbers.
2419 // Stores occupied ranges into the ranges parameter, and next level of sub
2420 // message types into the nested_messages parameter.  The FieldRange is left
2421 // inclusive, right exclusive. i.e. [a, b).
2422 //
2423 // Nested Messages:
2424 // Note that it only stores the nested message type, iff the nested type is
2425 // either a direct child of the given descriptor, or the nested type is a
2426 // descendant of the given descriptor and all the nodes between the
2427 // nested type and the given descriptor are group types. e.g.
2428 //
2429 // message Foo {
2430 //   message Bar {
2431 //     message NestedBar {}
2432 //   }
2433 //   group Baz = 1 {
2434 //     group NestedBazGroup = 2 {
2435 //       message Quz {
2436 //         message NestedQuz {}
2437 //       }
2438 //     }
2439 //     message NestedBaz {}
2440 //   }
2441 // }
2442 //
2443 // In this case, Bar, Quz and NestedBaz will be added into the nested types.
2444 // Since free field numbers of group types will not be printed, this makes sure
2445 // the nested message types in groups will not be dropped. The nested_messages
2446 // parameter will contain the direct children (when groups are ignored in the
2447 // tree) of the given descriptor for the caller to traverse. The declaration
2448 // order of the nested messages is also preserved.
2449 typedef std::pair<int, int> FieldRange;
GatherOccupiedFieldRanges(const Descriptor * descriptor,std::set<FieldRange> * ranges,std::vector<const Descriptor * > * nested_messages)2450 void GatherOccupiedFieldRanges(
2451     const Descriptor* descriptor, std::set<FieldRange>* ranges,
2452     std::vector<const Descriptor*>* nested_messages) {
2453   std::set<const Descriptor*> groups;
2454   for (int i = 0; i < descriptor->field_count(); ++i) {
2455     const FieldDescriptor* fd = descriptor->field(i);
2456     ranges->insert(FieldRange(fd->number(), fd->number() + 1));
2457     if (fd->type() == FieldDescriptor::TYPE_GROUP) {
2458       groups.insert(fd->message_type());
2459     }
2460   }
2461   for (int i = 0; i < descriptor->extension_range_count(); ++i) {
2462     ranges->insert(FieldRange(descriptor->extension_range(i)->start,
2463                               descriptor->extension_range(i)->end));
2464   }
2465   for (int i = 0; i < descriptor->reserved_range_count(); ++i) {
2466     ranges->insert(FieldRange(descriptor->reserved_range(i)->start,
2467                               descriptor->reserved_range(i)->end));
2468   }
2469   // Handle the nested messages/groups in declaration order to make it
2470   // post-order strict.
2471   for (int i = 0; i < descriptor->nested_type_count(); ++i) {
2472     const Descriptor* nested_desc = descriptor->nested_type(i);
2473     if (groups.find(nested_desc) != groups.end()) {
2474       GatherOccupiedFieldRanges(nested_desc, ranges, nested_messages);
2475     } else {
2476       nested_messages->push_back(nested_desc);
2477     }
2478   }
2479 }
2480 
2481 // Utility function for PrintFreeFieldNumbers.
2482 // Actually prints the formatted free field numbers for given message name and
2483 // occupied ranges.
FormatFreeFieldNumbers(const std::string & name,const std::set<FieldRange> & ranges)2484 void FormatFreeFieldNumbers(const std::string& name,
2485                             const std::set<FieldRange>& ranges) {
2486   std::string output;
2487   StringAppendF(&output, "%-35s free:", name.c_str());
2488   int next_free_number = 1;
2489   for (std::set<FieldRange>::const_iterator i = ranges.begin();
2490        i != ranges.end(); ++i) {
2491     // This happens when groups re-use parent field numbers, in which
2492     // case we skip the FieldRange entirely.
2493     if (next_free_number >= i->second) continue;
2494 
2495     if (next_free_number < i->first) {
2496       if (next_free_number + 1 == i->first) {
2497         // Singleton
2498         StringAppendF(&output, " %d", next_free_number);
2499       } else {
2500         // Range
2501         StringAppendF(&output, " %d-%d", next_free_number, i->first - 1);
2502       }
2503     }
2504     next_free_number = i->second;
2505   }
2506   if (next_free_number <= FieldDescriptor::kMaxNumber) {
2507     StringAppendF(&output, " %d-INF", next_free_number);
2508   }
2509   std::cout << output << std::endl;
2510 }
2511 
2512 }  // namespace
2513 
PrintFreeFieldNumbers(const Descriptor * descriptor)2514 void CommandLineInterface::PrintFreeFieldNumbers(const Descriptor* descriptor) {
2515   std::set<FieldRange> ranges;
2516   std::vector<const Descriptor*> nested_messages;
2517   GatherOccupiedFieldRanges(descriptor, &ranges, &nested_messages);
2518 
2519   for (int i = 0; i < nested_messages.size(); ++i) {
2520     PrintFreeFieldNumbers(nested_messages[i]);
2521   }
2522   FormatFreeFieldNumbers(descriptor->full_name(), ranges);
2523 }
2524 
2525 
2526 }  // namespace compiler
2527 }  // namespace protobuf
2528 }  // namespace google
2529