| 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 // https://developers.google.com/protocol-buffers/ | 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. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 |
| 31 // Author: kenton@google.com (Kenton Varda) | 31 // Author: kenton@google.com (Kenton Varda) |
| 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 #include <google/protobuf/compiler/code_generator.h> | 35 #include <google/protobuf/compiler/code_generator.h> |
| 36 | 36 |
| 37 #include <google/protobuf/compiler/plugin.pb.h> | |
| 38 #include <google/protobuf/stubs/logging.h> | 37 #include <google/protobuf/stubs/logging.h> |
| 39 #include <google/protobuf/stubs/common.h> | 38 #include <google/protobuf/stubs/common.h> |
| 40 #include <google/protobuf/descriptor.h> | |
| 41 #include <google/protobuf/stubs/strutil.h> | 39 #include <google/protobuf/stubs/strutil.h> |
| 42 | 40 |
| 43 namespace google { | 41 namespace google { |
| 44 namespace protobuf { | 42 namespace protobuf { |
| 45 namespace compiler { | 43 namespace compiler { |
| 46 | 44 |
| 47 CodeGenerator::~CodeGenerator() {} | 45 CodeGenerator::~CodeGenerator() {} |
| 48 | |
| 49 bool CodeGenerator::GenerateAll( | |
| 50 const std::vector<const FileDescriptor*>& files, | |
| 51 const string& parameter, | |
| 52 GeneratorContext* generator_context, | |
| 53 string* error) const { | |
| 54 // Default implemenation is just to call the per file method, and prefix any | |
| 55 // error string with the file to provide context. | |
| 56 bool succeeded = true; | |
| 57 for (int i = 0; i < files.size(); i++) { | |
| 58 const FileDescriptor* file = files[i]; | |
| 59 succeeded = Generate(file, parameter, generator_context, error); | |
| 60 if (!succeeded && error && error->empty()) { | |
| 61 *error = "Code generator returned false but provided no error " | |
| 62 "description."; | |
| 63 } | |
| 64 if (error && !error->empty()) { | |
| 65 *error = file->name() + ": " + *error; | |
| 66 break; | |
| 67 } | |
| 68 if (!succeeded) { | |
| 69 break; | |
| 70 } | |
| 71 } | |
| 72 return succeeded; | |
| 73 } | |
| 74 | |
| 75 GeneratorContext::~GeneratorContext() {} | 46 GeneratorContext::~GeneratorContext() {} |
| 76 | 47 |
| 77 io::ZeroCopyOutputStream* | 48 io::ZeroCopyOutputStream* |
| 78 GeneratorContext::OpenForAppend(const string& filename) { | 49 GeneratorContext::OpenForAppend(const string& filename) { |
| 79 return NULL; | 50 return NULL; |
| 80 } | 51 } |
| 81 | 52 |
| 82 io::ZeroCopyOutputStream* GeneratorContext::OpenForInsert( | 53 io::ZeroCopyOutputStream* GeneratorContext::OpenForInsert( |
| 83 const string& filename, const string& insertion_point) { | 54 const string& filename, const string& insertion_point) { |
| 84 GOOGLE_LOG(FATAL) << "This GeneratorContext does not support insertion."; | 55 GOOGLE_LOG(FATAL) << "This GeneratorContext does not support insertion."; |
| 85 return NULL; // make compiler happy | 56 return NULL; // make compiler happy |
| 86 } | 57 } |
| 87 | 58 |
| 88 void GeneratorContext::ListParsedFiles( | 59 void GeneratorContext::ListParsedFiles( |
| 89 std::vector<const FileDescriptor*>* output) { | 60 vector<const FileDescriptor*>* output) { |
| 90 GOOGLE_LOG(FATAL) << "This GeneratorContext does not support ListParsedFiles"; | 61 GOOGLE_LOG(FATAL) << "This GeneratorContext does not support ListParsedFiles"; |
| 91 } | 62 } |
| 92 | 63 |
| 93 void GeneratorContext::GetCompilerVersion(Version* version) const { | |
| 94 version->set_major(GOOGLE_PROTOBUF_VERSION / 1000000); | |
| 95 version->set_minor(GOOGLE_PROTOBUF_VERSION / 1000 % 1000); | |
| 96 version->set_patch(GOOGLE_PROTOBUF_VERSION % 1000); | |
| 97 version->set_suffix(GOOGLE_PROTOBUF_VERSION_SUFFIX); | |
| 98 } | |
| 99 | |
| 100 // Parses a set of comma-delimited name/value pairs. | 64 // Parses a set of comma-delimited name/value pairs. |
| 101 void ParseGeneratorParameter(const string& text, | 65 void ParseGeneratorParameter(const string& text, |
| 102 std::vector<std::pair<string, string> >* output) { | 66 vector<pair<string, string> >* output) { |
| 103 std::vector<string> parts = Split(text, ",", true); | 67 vector<string> parts = Split(text, ",", true); |
| 104 | 68 |
| 105 for (int i = 0; i < parts.size(); i++) { | 69 for (int i = 0; i < parts.size(); i++) { |
| 106 string::size_type equals_pos = parts[i].find_first_of('='); | 70 string::size_type equals_pos = parts[i].find_first_of('='); |
| 107 std::pair<string, string> value; | 71 pair<string, string> value; |
| 108 if (equals_pos == string::npos) { | 72 if (equals_pos == string::npos) { |
| 109 value.first = parts[i]; | 73 value.first = parts[i]; |
| 110 value.second = ""; | 74 value.second = ""; |
| 111 } else { | 75 } else { |
| 112 value.first = parts[i].substr(0, equals_pos); | 76 value.first = parts[i].substr(0, equals_pos); |
| 113 value.second = parts[i].substr(equals_pos + 1); | 77 value.second = parts[i].substr(equals_pos + 1); |
| 114 } | 78 } |
| 115 output->push_back(value); | 79 output->push_back(value); |
| 116 } | 80 } |
| 117 } | 81 } |
| 118 | 82 |
| 119 } // namespace compiler | 83 } // namespace compiler |
| 120 } // namespace protobuf | 84 } // namespace protobuf |
| 121 } // namespace google | 85 } // namespace google |
| OLD | NEW |