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> |
37 #include <google/protobuf/stubs/logging.h> | 38 #include <google/protobuf/stubs/logging.h> |
38 #include <google/protobuf/stubs/common.h> | 39 #include <google/protobuf/stubs/common.h> |
| 40 #include <google/protobuf/descriptor.h> |
39 #include <google/protobuf/stubs/strutil.h> | 41 #include <google/protobuf/stubs/strutil.h> |
40 | 42 |
41 namespace google { | 43 namespace google { |
42 namespace protobuf { | 44 namespace protobuf { |
43 namespace compiler { | 45 namespace compiler { |
44 | 46 |
45 CodeGenerator::~CodeGenerator() {} | 47 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 |
46 GeneratorContext::~GeneratorContext() {} | 75 GeneratorContext::~GeneratorContext() {} |
47 | 76 |
48 io::ZeroCopyOutputStream* | 77 io::ZeroCopyOutputStream* |
49 GeneratorContext::OpenForAppend(const string& filename) { | 78 GeneratorContext::OpenForAppend(const string& filename) { |
50 return NULL; | 79 return NULL; |
51 } | 80 } |
52 | 81 |
53 io::ZeroCopyOutputStream* GeneratorContext::OpenForInsert( | 82 io::ZeroCopyOutputStream* GeneratorContext::OpenForInsert( |
54 const string& filename, const string& insertion_point) { | 83 const string& filename, const string& insertion_point) { |
55 GOOGLE_LOG(FATAL) << "This GeneratorContext does not support insertion."; | 84 GOOGLE_LOG(FATAL) << "This GeneratorContext does not support insertion."; |
56 return NULL; // make compiler happy | 85 return NULL; // make compiler happy |
57 } | 86 } |
58 | 87 |
59 void GeneratorContext::ListParsedFiles( | 88 void GeneratorContext::ListParsedFiles( |
60 vector<const FileDescriptor*>* output) { | 89 std::vector<const FileDescriptor*>* output) { |
61 GOOGLE_LOG(FATAL) << "This GeneratorContext does not support ListParsedFiles"; | 90 GOOGLE_LOG(FATAL) << "This GeneratorContext does not support ListParsedFiles"; |
62 } | 91 } |
63 | 92 |
| 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 |
64 // Parses a set of comma-delimited name/value pairs. | 100 // Parses a set of comma-delimited name/value pairs. |
65 void ParseGeneratorParameter(const string& text, | 101 void ParseGeneratorParameter(const string& text, |
66 vector<pair<string, string> >* output) { | 102 std::vector<std::pair<string, string> >* output) { |
67 vector<string> parts = Split(text, ",", true); | 103 std::vector<string> parts = Split(text, ",", true); |
68 | 104 |
69 for (int i = 0; i < parts.size(); i++) { | 105 for (int i = 0; i < parts.size(); i++) { |
70 string::size_type equals_pos = parts[i].find_first_of('='); | 106 string::size_type equals_pos = parts[i].find_first_of('='); |
71 pair<string, string> value; | 107 std::pair<string, string> value; |
72 if (equals_pos == string::npos) { | 108 if (equals_pos == string::npos) { |
73 value.first = parts[i]; | 109 value.first = parts[i]; |
74 value.second = ""; | 110 value.second = ""; |
75 } else { | 111 } else { |
76 value.first = parts[i].substr(0, equals_pos); | 112 value.first = parts[i].substr(0, equals_pos); |
77 value.second = parts[i].substr(equals_pos + 1); | 113 value.second = parts[i].substr(equals_pos + 1); |
78 } | 114 } |
79 output->push_back(value); | 115 output->push_back(value); |
80 } | 116 } |
81 } | 117 } |
82 | 118 |
83 } // namespace compiler | 119 } // namespace compiler |
84 } // namespace protobuf | 120 } // namespace protobuf |
85 } // namespace google | 121 } // namespace google |
OLD | NEW |