#include <google/protobuf/compiler/command_line_interface.h>
#include <google/protobuf/stubs/platform_macros.h>
#include <stdio.h>
#include <sys/types.h>
#ifdef major
#undef major
#endif
#ifdef minor
#undef minor
#endif
#include <fcntl.h>
#include <sys/stat.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <fstream>
#include <iostream>
#include <limits.h>
#include <memory>
#if defined(__APPLE__)
#include <mach-o/dyld.h>
#elif defined(__FreeBSD__)
#include <sys/sysctl.h>
#endif
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/logging.h>
#include <google/protobuf/compiler/subprocess.h>
#include <google/protobuf/compiler/plugin.pb.h>
#include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/stubs/stringprintf.h>
#include <google/protobuf/stubs/substitute.h>
#include <google/protobuf/compiler/code_generator.h>
#include <google/protobuf/compiler/importer.h>
#include <google/protobuf/compiler/zip_writer.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/dynamic_message.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/io_win32.h>
#include <google/protobuf/io/printer.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
#include <google/protobuf/stubs/map_util.h>
#include <google/protobuf/stubs/stl_util.h>
#include <google/protobuf/port_def.inc>
namespace google {
namespace protobuf {
namespace compiler {
#ifndef O_BINARY
#ifdef _O_BINARY
#define O_BINARY …
#else
#define O_BINARY …
#endif
#endif
namespace {
#if defined(_WIN32)
using google::protobuf::io::win32::access;
using google::protobuf::io::win32::close;
using google::protobuf::io::win32::mkdir;
using google::protobuf::io::win32::open;
using google::protobuf::io::win32::setmode;
using google::protobuf::io::win32::write;
#endif
static const char* kDefaultDirectDependenciesViolationMsg = …;
static bool IsWindowsAbsolutePath(const std::string& text) { … }
void SetFdToTextMode(int fd) { … }
void SetFdToBinaryMode(int fd) { … }
void AddTrailingSlash(std::string* path) { … }
bool VerifyDirectoryExists(const std::string& path) { … }
bool TryCreateParentDirectory(const std::string& prefix,
const std::string& filename) { … }
bool GetProtocAbsolutePath(std::string* path) { … }
bool IsInstalledProtoPath(const std::string& path) { … }
void AddDefaultProtoPaths(
std::vector<std::pair<std::string, std::string>>* paths) { … }
std::string PluginName(const std::string& plugin_prefix,
const std::string& directive) { … }
}
class CommandLineInterface::ErrorPrinter
: public MultiFileErrorCollector,
public io::ErrorCollector,
public DescriptorPool::ErrorCollector { … };
class CommandLineInterface::GeneratorContextImpl : public GeneratorContext { … };
class CommandLineInterface::MemoryOutputStream
: public io::ZeroCopyOutputStream { … };
CommandLineInterface::GeneratorContextImpl::GeneratorContextImpl(
const std::vector<const FileDescriptor*>& parsed_files)
: … { … }
bool CommandLineInterface::GeneratorContextImpl::WriteAllToDisk(
const std::string& prefix) { … }
bool CommandLineInterface::GeneratorContextImpl::WriteAllToZip(
const std::string& filename) { … }
void CommandLineInterface::GeneratorContextImpl::AddJarManifest() { … }
void CommandLineInterface::GeneratorContextImpl::GetOutputFilenames(
std::vector<std::string>* output_filenames) { … }
io::ZeroCopyOutputStream* CommandLineInterface::GeneratorContextImpl::Open(
const std::string& filename) { … }
io::ZeroCopyOutputStream*
CommandLineInterface::GeneratorContextImpl::OpenForAppend(
const std::string& filename) { … }
io::ZeroCopyOutputStream*
CommandLineInterface::GeneratorContextImpl::OpenForInsert(
const std::string& filename, const std::string& insertion_point) { … }
io::ZeroCopyOutputStream*
CommandLineInterface::GeneratorContextImpl::OpenForInsertWithGeneratedCodeInfo(
const std::string& filename, const std::string& insertion_point,
const google::protobuf::GeneratedCodeInfo& info) { … }
CommandLineInterface::MemoryOutputStream::MemoryOutputStream(
GeneratorContextImpl* directory, const std::string& filename,
bool append_mode)
: … { … }
CommandLineInterface::MemoryOutputStream::MemoryOutputStream(
GeneratorContextImpl* directory, const std::string& filename,
const std::string& insertion_point)
: … { … }
CommandLineInterface::MemoryOutputStream::MemoryOutputStream(
GeneratorContextImpl* directory, const std::string& filename,
const std::string& insertion_point, const google::protobuf::GeneratedCodeInfo& info)
: … { … }
void CommandLineInterface::MemoryOutputStream::InsertShiftedInfo(
const std::string& insertion_content, size_t insertion_offset,
size_t indent_length, google::protobuf::GeneratedCodeInfo& target_info) { … }
void CommandLineInterface::MemoryOutputStream::UpdateMetadata(
const std::string& insertion_content, size_t insertion_offset,
size_t insertion_length, size_t indent_length) { … }
CommandLineInterface::MemoryOutputStream::~MemoryOutputStream() { … }
#if defined(_WIN32) && !defined(__CYGWIN__)
const char* const CommandLineInterface::kPathSeparator = ";";
#else
const char* const CommandLineInterface::kPathSeparator = …;
#endif
CommandLineInterface::CommandLineInterface()
: … { … }
CommandLineInterface::~CommandLineInterface() { … }
void CommandLineInterface::RegisterGenerator(const std::string& flag_name,
CodeGenerator* generator,
const std::string& help_text) { … }
void CommandLineInterface::RegisterGenerator(
const std::string& flag_name, const std::string& option_flag_name,
CodeGenerator* generator, const std::string& help_text) { … }
void CommandLineInterface::AllowPlugins(const std::string& exe_name_prefix) { … }
namespace {
bool ContainsProto3Optional(const Descriptor* desc) { … }
bool ContainsProto3Optional(const FileDescriptor* file) { … }
}
namespace {
std::unique_ptr<SimpleDescriptorDatabase>
PopulateSingleSimpleDescriptorDatabase(const std::string& descriptor_set_name);
}
int CommandLineInterface::Run(int argc, const char* const argv[]) { … }
bool CommandLineInterface::InitializeDiskSourceTree(
DiskSourceTree* source_tree, DescriptorDatabase* fallback_database) { … }
namespace {
std::unique_ptr<SimpleDescriptorDatabase>
PopulateSingleSimpleDescriptorDatabase(const std::string& descriptor_set_name) { … }
}
bool CommandLineInterface::VerifyInputFilesInDescriptors(
DescriptorDatabase* database) { … }
bool CommandLineInterface::ParseInputFiles(
DescriptorPool* descriptor_pool, DiskSourceTree* source_tree,
std::vector<const FileDescriptor*>* parsed_files) { … }
void CommandLineInterface::Clear() { … }
bool CommandLineInterface::MakeProtoProtoPathRelative(
DiskSourceTree* source_tree, std::string* proto,
DescriptorDatabase* fallback_database) { … }
bool CommandLineInterface::MakeInputsBeProtoPathRelative(
DiskSourceTree* source_tree, DescriptorDatabase* fallback_database) { … }
bool CommandLineInterface::ExpandArgumentFile(
const std::string& file, std::vector<std::string>* arguments) { … }
CommandLineInterface::ParseArgumentStatus CommandLineInterface::ParseArguments(
int argc, const char* const argv[]) { … }
bool CommandLineInterface::ParseArgument(const char* arg, std::string* name,
std::string* value) { … }
CommandLineInterface::ParseArgumentStatus
CommandLineInterface::InterpretArgument(const std::string& name,
const std::string& value) { … }
void CommandLineInterface::PrintHelpText() { … }
bool CommandLineInterface::EnforceProto3OptionalSupport(
const std::string& codegen_name, uint64_t supported_features,
const std::vector<const FileDescriptor*>& parsed_files) const { … }
bool CommandLineInterface::GenerateOutput(
const std::vector<const FileDescriptor*>& parsed_files,
const OutputDirective& output_directive,
GeneratorContext* generator_context) { … }
bool CommandLineInterface::GenerateDependencyManifestFile(
const std::vector<const FileDescriptor*>& parsed_files,
const GeneratorContextMap& output_directories,
DiskSourceTree* source_tree) { … }
bool CommandLineInterface::GeneratePluginOutput(
const std::vector<const FileDescriptor*>& parsed_files,
const std::string& plugin_name, const std::string& parameter,
GeneratorContext* generator_context, std::string* error) { … }
bool CommandLineInterface::EncodeOrDecode(const DescriptorPool* pool) { … }
bool CommandLineInterface::WriteDescriptorSet(
const std::vector<const FileDescriptor*>& parsed_files) { … }
void CommandLineInterface::GetTransitiveDependencies(
const FileDescriptor* file, bool include_json_name,
bool include_source_code_info,
std::set<const FileDescriptor*>* already_seen,
RepeatedPtrField<FileDescriptorProto>* output) { … }
namespace {
FieldRange;
void GatherOccupiedFieldRanges(
const Descriptor* descriptor, std::set<FieldRange>* ranges,
std::vector<const Descriptor*>* nested_messages) { … }
void FormatFreeFieldNumbers(const std::string& name,
const std::set<FieldRange>& ranges) { … }
}
void CommandLineInterface::PrintFreeFieldNumbers(const Descriptor* descriptor) { … }
}
}
}