| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // http://code.google.com/p/protobuf/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| 11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
| (...skipping 18 matching lines...) Expand all Loading... |
| 32 // Based on original Protocol Buffers design by | 32 // Based on original Protocol Buffers design by |
| 33 // Sanjay Ghemawat, Jeff Dean, and others. | 33 // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 // | 34 // |
| 35 // Implements the Protocol Compiler front-end such that it may be reused by | 35 // Implements the Protocol Compiler front-end such that it may be reused by |
| 36 // custom compilers written to support other languages. | 36 // custom compilers written to support other languages. |
| 37 | 37 |
| 38 #ifndef GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ | 38 #ifndef GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ |
| 39 #define GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ | 39 #define GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ |
| 40 | 40 |
| 41 #include <google/protobuf/stubs/common.h> | 41 #include <google/protobuf/stubs/common.h> |
| 42 #include <google/protobuf/stubs/hash.h> |
| 42 #include <string> | 43 #include <string> |
| 43 #include <vector> | 44 #include <vector> |
| 44 #include <map> | 45 #include <map> |
| 45 #include <set> | 46 #include <set> |
| 46 #include <utility> | 47 #include <utility> |
| 47 | 48 |
| 48 namespace google { | 49 namespace google { |
| 49 namespace protobuf { | 50 namespace protobuf { |
| 50 | 51 |
| 52 class Descriptor; // descriptor.h |
| 53 class DescriptorPool; // descriptor.h |
| 51 class FileDescriptor; // descriptor.h | 54 class FileDescriptor; // descriptor.h |
| 52 class DescriptorPool; // descriptor.h | |
| 53 class FileDescriptorProto; // descriptor.pb.h | 55 class FileDescriptorProto; // descriptor.pb.h |
| 54 template<typename T> class RepeatedPtrField; // repeated_field.h | 56 template<typename T> class RepeatedPtrField; // repeated_field.h |
| 55 | 57 |
| 56 namespace compiler { | 58 namespace compiler { |
| 57 | 59 |
| 58 class CodeGenerator; // code_generator.h | 60 class CodeGenerator; // code_generator.h |
| 59 class GeneratorContext; // code_generator.h | 61 class GeneratorContext; // code_generator.h |
| 60 class DiskSourceTree; // importer.h | 62 class DiskSourceTree; // importer.h |
| 61 | 63 |
| 62 // This class implements the command-line interface to the protocol compiler. | 64 // This class implements the command-line interface to the protocol compiler. |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 version_info_ = text; | 184 version_info_ = text; |
| 183 } | 185 } |
| 184 | 186 |
| 185 | 187 |
| 186 private: | 188 private: |
| 187 // ----------------------------------------------------------------- | 189 // ----------------------------------------------------------------- |
| 188 | 190 |
| 189 class ErrorPrinter; | 191 class ErrorPrinter; |
| 190 class GeneratorContextImpl; | 192 class GeneratorContextImpl; |
| 191 class MemoryOutputStream; | 193 class MemoryOutputStream; |
| 194 typedef hash_map<string, GeneratorContextImpl*> GeneratorContextMap; |
| 192 | 195 |
| 193 // Clear state from previous Run(). | 196 // Clear state from previous Run(). |
| 194 void Clear(); | 197 void Clear(); |
| 195 | 198 |
| 196 // Remaps each file in input_files_ so that it is relative to one of the | 199 // Remaps each file in input_files_ so that it is relative to one of the |
| 197 // directories in proto_path_. Returns false if an error occurred. This | 200 // directories in proto_path_. Returns false if an error occurred. This |
| 198 // is only used if inputs_are_proto_path_relative_ is false. | 201 // is only used if inputs_are_proto_path_relative_ is false. |
| 199 bool MakeInputsBeProtoPathRelative( | 202 bool MakeInputsBeProtoPathRelative( |
| 200 DiskSourceTree* source_tree); | 203 DiskSourceTree* source_tree); |
| 201 | 204 |
| 202 // Return status for ParseArguments() and InterpretArgument(). | 205 // Return status for ParseArguments() and InterpretArgument(). |
| 203 enum ParseArgumentStatus { | 206 enum ParseArgumentStatus { |
| 204 PARSE_ARGUMENT_DONE_AND_CONTINUE, | 207 PARSE_ARGUMENT_DONE_AND_CONTINUE, |
| 205 PARSE_ARGUMENT_DONE_AND_EXIT, | 208 PARSE_ARGUMENT_DONE_AND_EXIT, |
| 206 PARSE_ARGUMENT_FAIL | 209 PARSE_ARGUMENT_FAIL |
| 207 }; | 210 }; |
| 208 | 211 |
| 209 // Parse all command-line arguments. | 212 // Parse all command-line arguments. |
| 210 ParseArgumentStatus ParseArguments(int argc, const char* const argv[]); | 213 ParseArgumentStatus ParseArguments(int argc, const char* const argv[]); |
| 211 | 214 |
| 212 // Parses a command-line argument into a name/value pair. Returns | 215 // Parses a command-line argument into a name/value pair. Returns |
| 213 // true if the next argument in the argv should be used as the value, | 216 // true if the next argument in the argv should be used as the value, |
| 214 // false otherwise. | 217 // false otherwise. |
| 215 // | 218 // |
| 216 // Exmaples: | 219 // Examples: |
| 217 // "-Isrc/protos" -> | 220 // "-Isrc/protos" -> |
| 218 // name = "-I", value = "src/protos" | 221 // name = "-I", value = "src/protos" |
| 219 // "--cpp_out=src/foo.pb2.cc" -> | 222 // "--cpp_out=src/foo.pb2.cc" -> |
| 220 // name = "--cpp_out", value = "src/foo.pb2.cc" | 223 // name = "--cpp_out", value = "src/foo.pb2.cc" |
| 221 // "foo.proto" -> | 224 // "foo.proto" -> |
| 222 // name = "", value = "foo.proto" | 225 // name = "", value = "foo.proto" |
| 223 bool ParseArgument(const char* arg, string* name, string* value); | 226 bool ParseArgument(const char* arg, string* name, string* value); |
| 224 | 227 |
| 225 // Interprets arguments parsed with ParseArgument. | 228 // Interprets arguments parsed with ParseArgument. |
| 226 ParseArgumentStatus InterpretArgument(const string& name, | 229 ParseArgumentStatus InterpretArgument(const string& name, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 239 const string& parameter, | 242 const string& parameter, |
| 240 GeneratorContext* generator_context, | 243 GeneratorContext* generator_context, |
| 241 string* error); | 244 string* error); |
| 242 | 245 |
| 243 // Implements --encode and --decode. | 246 // Implements --encode and --decode. |
| 244 bool EncodeOrDecode(const DescriptorPool* pool); | 247 bool EncodeOrDecode(const DescriptorPool* pool); |
| 245 | 248 |
| 246 // Implements the --descriptor_set_out option. | 249 // Implements the --descriptor_set_out option. |
| 247 bool WriteDescriptorSet(const vector<const FileDescriptor*> parsed_files); | 250 bool WriteDescriptorSet(const vector<const FileDescriptor*> parsed_files); |
| 248 | 251 |
| 252 // Implements the --dependency_out option |
| 253 bool GenerateDependencyManifestFile( |
| 254 const vector<const FileDescriptor*>& parsed_files, |
| 255 const GeneratorContextMap& output_directories, |
| 256 DiskSourceTree* source_tree); |
| 257 |
| 249 // Get all transitive dependencies of the given file (including the file | 258 // Get all transitive dependencies of the given file (including the file |
| 250 // itself), adding them to the given list of FileDescriptorProtos. The | 259 // itself), adding them to the given list of FileDescriptorProtos. The |
| 251 // protos will be ordered such that every file is listed before any file that | 260 // protos will be ordered such that every file is listed before any file that |
| 252 // depends on it, so that you can call DescriptorPool::BuildFile() on them | 261 // depends on it, so that you can call DescriptorPool::BuildFile() on them |
| 253 // in order. Any files in *already_seen will not be added, and each file | 262 // in order. Any files in *already_seen will not be added, and each file |
| 254 // added will be inserted into *already_seen. If include_source_code_info is | 263 // added will be inserted into *already_seen. If include_source_code_info is |
| 255 // true then include the source code information in the FileDescriptorProtos. | 264 // true then include the source code information in the FileDescriptorProtos. |
| 265 // If include_json_name is true, populate the json_name field of |
| 266 // FieldDescriptorProto for all fields. |
| 256 static void GetTransitiveDependencies( | 267 static void GetTransitiveDependencies( |
| 257 const FileDescriptor* file, | 268 const FileDescriptor* file, |
| 269 bool include_json_name, |
| 258 bool include_source_code_info, | 270 bool include_source_code_info, |
| 259 set<const FileDescriptor*>* already_seen, | 271 set<const FileDescriptor*>* already_seen, |
| 260 RepeatedPtrField<FileDescriptorProto>* output); | 272 RepeatedPtrField<FileDescriptorProto>* output); |
| 261 | 273 |
| 274 // Implements the --print_free_field_numbers. This function prints free field |
| 275 // numbers into stdout for the message and it's nested message types in |
| 276 // post-order, i.e. nested types first. Printed range are left-right |
| 277 // inclusive, i.e. [a, b]. |
| 278 // |
| 279 // Groups: |
| 280 // For historical reasons, groups are considered to share the same |
| 281 // field number space with the parent message, thus it will not print free |
| 282 // field numbers for groups. The field numbers used in the groups are |
| 283 // excluded in the free field numbers of the parent message. |
| 284 // |
| 285 // Extension Ranges: |
| 286 // Extension ranges are considered ocuppied field numbers and they will not be |
| 287 // listed as free numbers in the output. |
| 288 void PrintFreeFieldNumbers(const Descriptor* descriptor); |
| 289 |
| 262 // ----------------------------------------------------------------- | 290 // ----------------------------------------------------------------- |
| 263 | 291 |
| 264 // The name of the executable as invoked (i.e. argv[0]). | 292 // The name of the executable as invoked (i.e. argv[0]). |
| 265 string executable_name_; | 293 string executable_name_; |
| 266 | 294 |
| 267 // Version info set with SetVersionInfo(). | 295 // Version info set with SetVersionInfo(). |
| 268 string version_info_; | 296 string version_info_; |
| 269 | 297 |
| 270 // Registered generators. | 298 // Registered generators. |
| 271 struct GeneratorInfo { | 299 struct GeneratorInfo { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 288 | 316 |
| 289 // Maps specific plugin names to files. When executing a plugin, this map | 317 // Maps specific plugin names to files. When executing a plugin, this map |
| 290 // is searched first to find the plugin executable. If not found here, the | 318 // is searched first to find the plugin executable. If not found here, the |
| 291 // PATH (or other OS-specific search strategy) is searched. | 319 // PATH (or other OS-specific search strategy) is searched. |
| 292 map<string, string> plugins_; | 320 map<string, string> plugins_; |
| 293 | 321 |
| 294 // Stuff parsed from command line. | 322 // Stuff parsed from command line. |
| 295 enum Mode { | 323 enum Mode { |
| 296 MODE_COMPILE, // Normal mode: parse .proto files and compile them. | 324 MODE_COMPILE, // Normal mode: parse .proto files and compile them. |
| 297 MODE_ENCODE, // --encode: read text from stdin, write binary to stdout. | 325 MODE_ENCODE, // --encode: read text from stdin, write binary to stdout. |
| 298 MODE_DECODE // --decode: read binary from stdin, write text to stdout. | 326 MODE_DECODE, // --decode: read binary from stdin, write text to stdout. |
| 327 MODE_PRINT, // Print mode: print info of the given .proto files and exit. |
| 299 }; | 328 }; |
| 300 | 329 |
| 301 Mode mode_; | 330 Mode mode_; |
| 302 | 331 |
| 332 enum PrintMode { |
| 333 PRINT_NONE, // Not in MODE_PRINT |
| 334 PRINT_FREE_FIELDS, // --print_free_fields |
| 335 }; |
| 336 |
| 337 PrintMode print_mode_; |
| 338 |
| 303 enum ErrorFormat { | 339 enum ErrorFormat { |
| 304 ERROR_FORMAT_GCC, // GCC error output format (default). | 340 ERROR_FORMAT_GCC, // GCC error output format (default). |
| 305 ERROR_FORMAT_MSVS // Visual Studio output (--error_format=msvs). | 341 ERROR_FORMAT_MSVS // Visual Studio output (--error_format=msvs). |
| 306 }; | 342 }; |
| 307 | 343 |
| 308 ErrorFormat error_format_; | 344 ErrorFormat error_format_; |
| 309 | 345 |
| 310 vector<pair<string, string> > proto_path_; // Search path for proto files. | 346 vector<pair<string, string> > proto_path_; // Search path for proto files. |
| 311 vector<string> input_files_; // Names of the input proto files. | 347 vector<string> input_files_; // Names of the input proto files. |
| 312 | 348 |
| 313 // output_directives_ lists all the files we are supposed to output and what | 349 // output_directives_ lists all the files we are supposed to output and what |
| 314 // generator to use for each. | 350 // generator to use for each. |
| 315 struct OutputDirective { | 351 struct OutputDirective { |
| 316 string name; // E.g. "--foo_out" | 352 string name; // E.g. "--foo_out" |
| 317 CodeGenerator* generator; // NULL for plugins | 353 CodeGenerator* generator; // NULL for plugins |
| 318 string parameter; | 354 string parameter; |
| 319 string output_location; | 355 string output_location; |
| 320 }; | 356 }; |
| 321 vector<OutputDirective> output_directives_; | 357 vector<OutputDirective> output_directives_; |
| 322 | 358 |
| 323 // When using --encode or --decode, this names the type we are encoding or | 359 // When using --encode or --decode, this names the type we are encoding or |
| 324 // decoding. (Empty string indicates --decode_raw.) | 360 // decoding. (Empty string indicates --decode_raw.) |
| 325 string codec_type_; | 361 string codec_type_; |
| 326 | 362 |
| 327 // If --descriptor_set_out was given, this is the filename to which the | 363 // If --descriptor_set_out was given, this is the filename to which the |
| 328 // FileDescriptorSet should be written. Otherwise, empty. | 364 // FileDescriptorSet should be written. Otherwise, empty. |
| 329 string descriptor_set_name_; | 365 string descriptor_set_name_; |
| 330 | 366 |
| 367 // If --dependency_out was given, this is the path to the file where the |
| 368 // dependency file will be written. Otherwise, empty. |
| 369 string dependency_out_name_; |
| 370 |
| 331 // True if --include_imports was given, meaning that we should | 371 // True if --include_imports was given, meaning that we should |
| 332 // write all transitive dependencies to the DescriptorSet. Otherwise, only | 372 // write all transitive dependencies to the DescriptorSet. Otherwise, only |
| 333 // the .proto files listed on the command-line are added. | 373 // the .proto files listed on the command-line are added. |
| 334 bool imports_in_descriptor_set_; | 374 bool imports_in_descriptor_set_; |
| 335 | 375 |
| 336 // True if --include_source_info was given, meaning that we should not strip | 376 // True if --include_source_info was given, meaning that we should not strip |
| 337 // SourceCodeInfo from the DescriptorSet. | 377 // SourceCodeInfo from the DescriptorSet. |
| 338 bool source_info_in_descriptor_set_; | 378 bool source_info_in_descriptor_set_; |
| 339 | 379 |
| 340 // Was the --disallow_services flag used? | 380 // Was the --disallow_services flag used? |
| 341 bool disallow_services_; | 381 bool disallow_services_; |
| 342 | 382 |
| 343 // See SetInputsAreProtoPathRelative(). | 383 // See SetInputsAreProtoPathRelative(). |
| 344 bool inputs_are_proto_path_relative_; | 384 bool inputs_are_proto_path_relative_; |
| 345 | 385 |
| 346 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CommandLineInterface); | 386 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CommandLineInterface); |
| 347 }; | 387 }; |
| 348 | 388 |
| 349 } // namespace compiler | 389 } // namespace compiler |
| 350 } // namespace protobuf | 390 } // namespace protobuf |
| 351 | 391 |
| 352 } // namespace google | 392 } // namespace google |
| 353 #endif // GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ | 393 #endif // GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ |
| OLD | NEW |