• 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/code_generator.h>
36 
37 #include <google/protobuf/stubs/logging.h>
38 #include <google/protobuf/stubs/common.h>
39 #include <google/protobuf/compiler/plugin.pb.h>
40 #include <google/protobuf/descriptor.h>
41 #include <google/protobuf/stubs/strutil.h>
42 
43 namespace google {
44 namespace protobuf {
45 namespace compiler {
46 
~CodeGenerator()47 CodeGenerator::~CodeGenerator() {}
48 
GenerateAll(const std::vector<const FileDescriptor * > & files,const std::string & parameter,GeneratorContext * generator_context,std::string * error) const49 bool CodeGenerator::GenerateAll(const std::vector<const FileDescriptor*>& files,
50                                 const std::string& parameter,
51                                 GeneratorContext* generator_context,
52                                 std::string* error) const {
53   // Default implemenation is just to call the per file method, and prefix any
54   // error string with the file to provide context.
55   bool succeeded = true;
56   for (int i = 0; i < files.size(); i++) {
57     const FileDescriptor* file = files[i];
58     succeeded = Generate(file, parameter, generator_context, error);
59     if (!succeeded && error && error->empty()) {
60       *error =
61           "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 
~GeneratorContext()75 GeneratorContext::~GeneratorContext() {}
76 
OpenForAppend(const std::string & filename)77 io::ZeroCopyOutputStream* GeneratorContext::OpenForAppend(
78     const std::string& filename) {
79   return NULL;
80 }
81 
OpenForInsert(const std::string & filename,const std::string & insertion_point)82 io::ZeroCopyOutputStream* GeneratorContext::OpenForInsert(
83     const std::string& filename, const std::string& insertion_point) {
84   GOOGLE_LOG(FATAL) << "This GeneratorContext does not support insertion.";
85   return NULL;  // make compiler happy
86 }
87 
ListParsedFiles(std::vector<const FileDescriptor * > * output)88 void GeneratorContext::ListParsedFiles(
89     std::vector<const FileDescriptor*>* output) {
90   GOOGLE_LOG(FATAL) << "This GeneratorContext does not support ListParsedFiles";
91 }
92 
GetCompilerVersion(Version * version) const93 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.
ParseGeneratorParameter(const std::string & text,std::vector<std::pair<std::string,std::string>> * output)101 void ParseGeneratorParameter(
102     const std::string& text,
103     std::vector<std::pair<std::string, std::string> >* output) {
104   std::vector<std::string> parts = Split(text, ",", true);
105 
106   for (int i = 0; i < parts.size(); i++) {
107     std::string::size_type equals_pos = parts[i].find_first_of('=');
108     std::pair<std::string, std::string> value;
109     if (equals_pos == std::string::npos) {
110       value.first = parts[i];
111       value.second = "";
112     } else {
113       value.first = parts[i].substr(0, equals_pos);
114       value.second = parts[i].substr(equals_pos + 1);
115     }
116     output->push_back(value);
117   }
118 }
119 
120 }  // namespace compiler
121 }  // namespace protobuf
122 }  // namespace google
123