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
43namespace google {
44namespace protobuf {
45namespace compiler {
46
47CodeGenerator::~CodeGenerator() {}
48
49bool CodeGenerator::GenerateAll(const std::vector<const FileDescriptor*>& files,
50 const std::string& parameter,
51 GeneratorContext* generator_context,
52 std::string* error) const {
53 // Default implementation 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
75GeneratorContext::~GeneratorContext() {}
76
77io::ZeroCopyOutputStream* GeneratorContext::OpenForAppend(
78 const std::string& filename) {
79 return nullptr;
80}
81
82io::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 nullptr; // make compiler happy
86}
87
88io::ZeroCopyOutputStream* GeneratorContext::OpenForInsertWithGeneratedCodeInfo(
89 const std::string& filename, const std::string& insertion_point,
90 const google::protobuf::GeneratedCodeInfo& /*info*/) {
91 return OpenForInsert(filename, insertion_point);
92}
93
94void GeneratorContext::ListParsedFiles(
95 std::vector<const FileDescriptor*>* output) {
96 GOOGLE_LOG(FATAL) << "This GeneratorContext does not support ListParsedFiles";
97}
98
99void GeneratorContext::GetCompilerVersion(Version* version) const {
100 version->set_major(GOOGLE_PROTOBUF_VERSION / 1000000);
101 version->set_minor(GOOGLE_PROTOBUF_VERSION / 1000 % 1000);
102 version->set_patch(GOOGLE_PROTOBUF_VERSION % 1000);
103 version->set_suffix(GOOGLE_PROTOBUF_VERSION_SUFFIX);
104}
105
106// Parses a set of comma-delimited name/value pairs.
107void ParseGeneratorParameter(
108 const std::string& text,
109 std::vector<std::pair<std::string, std::string> >* output) {
110 std::vector<std::string> parts = Split(full: text, delim: ",", skip_empty: true);
111
112 for (int i = 0; i < parts.size(); i++) {
113 std::string::size_type equals_pos = parts[i].find_first_of(c: '=');
114 std::pair<std::string, std::string> value;
115 if (equals_pos == std::string::npos) {
116 value.first = parts[i];
117 value.second = "";
118 } else {
119 value.first = parts[i].substr(pos: 0, n: equals_pos);
120 value.second = parts[i].substr(pos: equals_pos + 1);
121 }
122 output->push_back(x: value);
123 }
124}
125
126// Strips ".proto" or ".protodevel" from the end of a filename.
127std::string StripProto(const std::string& filename) {
128 if (HasSuffixString(str: filename, suffix: ".protodevel")) {
129 return StripSuffixString(str: filename, suffix: ".protodevel");
130 } else {
131 return StripSuffixString(str: filename, suffix: ".proto");
132 }
133}
134
135} // namespace compiler
136} // namespace protobuf
137} // namespace google
138