• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "tools/proto_merger/proto_file_serializer.h"
18 
19 #include "perfetto/ext/base/string_utils.h"
20 
21 namespace perfetto {
22 namespace proto_merger {
23 namespace {
24 
DeletedComment(const std::string & prefix)25 std::string DeletedComment(const std::string& prefix) {
26   std::string output;
27   output += "\n";
28   output += prefix + "  //\n";
29   output += prefix;
30   output +=
31       "  // The following enums/messages/fields are not present upstream\n";
32   output += prefix + "  //\n";
33   return output;
34 }
35 
SerializeComments(const std::string & prefix,const std::vector<std::string> & lines)36 std::string SerializeComments(const std::string& prefix,
37                               const std::vector<std::string>& lines) {
38   std::string output;
39   for (const auto& line : lines) {
40     output.append(prefix);
41     output.append("//");
42     output.append(line);
43     output.append("\n");
44   }
45   return output;
46 }
47 
SerializeLeadingComments(const std::string & prefix,const ProtoFile::Member & member,bool prefix_newline_if_comment=true)48 std::string SerializeLeadingComments(const std::string& prefix,
49                                      const ProtoFile::Member& member,
50                                      bool prefix_newline_if_comment = true) {
51   if (member.leading_comments.empty())
52     return "";
53 
54   std::string output;
55   if (prefix_newline_if_comment) {
56     output += "\n";
57   }
58   output += SerializeComments(prefix, member.leading_comments);
59   return output;
60 }
61 
SerializeTrailingComments(const std::string & prefix,const ProtoFile::Member & member)62 std::string SerializeTrailingComments(const std::string& prefix,
63                                       const ProtoFile::Member& member) {
64   return SerializeComments(prefix, member.trailing_comments);
65 }
66 
SerializeOptions(const std::vector<ProtoFile::Option> & options)67 std::string SerializeOptions(const std::vector<ProtoFile::Option>& options) {
68   if (options.empty())
69     return "";
70 
71   std::string output;
72   output += " [";
73   for (const auto& option : options) {
74     output += option.key + " = " + option.value;
75   }
76   output += "]";
77   return output;
78 }
79 
SerializeEnumValue(size_t indent,const ProtoFile::Enum::Value & value)80 std::string SerializeEnumValue(size_t indent,
81                                const ProtoFile::Enum::Value& value) {
82   std::string prefix(indent * 2, ' ');
83 
84   std::string output;
85   output += SerializeLeadingComments(prefix, value, false);
86 
87   output += prefix + value.name + " = " + std::to_string(value.number);
88   output += SerializeOptions(value.options);
89   output += ";\n";
90 
91   output += SerializeTrailingComments(prefix, value);
92   return output;
93 }
94 
SerializeEnum(size_t indent,const ProtoFile::Enum & en)95 std::string SerializeEnum(size_t indent, const ProtoFile::Enum& en) {
96   std::string prefix(indent * 2, ' ');
97   ++indent;
98 
99   std::string output;
100   output += SerializeLeadingComments(prefix, en);
101 
102   output += prefix + "enum " + en.name + " {\n";
103   for (const auto& value : en.values) {
104     output += SerializeEnumValue(indent, value);
105   }
106   output += prefix + "}\n";
107 
108   output += SerializeTrailingComments(prefix, en);
109   return output;
110 }
111 
SerializeField(size_t indent,const ProtoFile::Field & field,bool write_label)112 std::string SerializeField(size_t indent,
113                            const ProtoFile::Field& field,
114                            bool write_label) {
115   std::string prefix(indent * 2, ' ');
116 
117   std::string output;
118   output += SerializeLeadingComments(prefix, field);
119 
120   std::string label;
121   if (write_label) {
122     label = field.label + " ";
123   }
124   output += prefix + label + field.type + " " + field.name + " = " +
125             std::to_string(field.number);
126 
127   output += SerializeOptions(field.options);
128   output += ";\n";
129 
130   output += SerializeTrailingComments(prefix, field);
131   return output;
132 }
133 
SerializeOneof(size_t indent,const ProtoFile::Oneof & oneof)134 std::string SerializeOneof(size_t indent, const ProtoFile::Oneof& oneof) {
135   std::string prefix(indent * 2, ' ');
136   ++indent;
137 
138   std::string output;
139   output += SerializeLeadingComments(prefix, oneof);
140 
141   output += prefix + "oneof " + oneof.name + " {\n";
142   for (const auto& field : oneof.fields) {
143     output += SerializeField(indent, field, false);
144   }
145   output += prefix + "}\n";
146 
147   output += SerializeTrailingComments(prefix, oneof);
148   return output;
149 }
150 
SerializeMessage(size_t indent,const ProtoFile::Message & message)151 std::string SerializeMessage(size_t indent, const ProtoFile::Message& message) {
152   std::string prefix(indent * 2, ' ');
153   ++indent;
154 
155   std::string output;
156   output += SerializeLeadingComments(prefix, message);
157 
158   output += prefix + "message " + message.name + " {\n";
159   for (const auto& en : message.enums) {
160     output += SerializeEnum(indent, en);
161   }
162   for (const auto& nested : message.nested_messages) {
163     output += SerializeMessage(indent, nested);
164   }
165   for (const auto& oneof : message.oneofs) {
166     output += SerializeOneof(indent, oneof);
167   }
168   for (const auto& field : message.fields) {
169     output += SerializeField(indent, field, true);
170   }
171 
172   if (!message.deleted_enums.empty() || !message.deleted_fields.empty() ||
173       !message.deleted_nested_messages.empty() ||
174       !message.deleted_oneofs.empty()) {
175     output += DeletedComment(prefix);
176     for (const auto& en : message.deleted_enums) {
177       output += SerializeEnum(indent, en);
178     }
179     for (const auto& nested : message.deleted_nested_messages) {
180       output += SerializeMessage(indent, nested);
181     }
182     for (const auto& oneof : message.deleted_oneofs) {
183       output += SerializeOneof(indent, oneof);
184     }
185     for (const auto& field : message.deleted_fields) {
186       output += SerializeField(indent, field, true);
187     }
188   }
189 
190   output += prefix + "}\n";
191 
192   output += SerializeTrailingComments(prefix, message);
193   return output;
194 }
195 
196 }  // namespace
197 
ProtoFileToDotProto(const ProtoFile & proto_file)198 std::string ProtoFileToDotProto(const ProtoFile& proto_file) {
199   std::string output;
200   for (const auto& en : proto_file.enums) {
201     output += SerializeEnum(0, en);
202   }
203   for (const auto& message : proto_file.messages) {
204     output += SerializeMessage(0, message);
205   }
206 
207   if (!proto_file.deleted_enums.empty() ||
208       !proto_file.deleted_messages.empty()) {
209     output += DeletedComment("");
210 
211     for (const auto& en : proto_file.deleted_enums) {
212       output += SerializeEnum(0, en);
213     }
214     for (const auto& nested : proto_file.deleted_messages) {
215       output += SerializeMessage(0, nested);
216     }
217   }
218   return output;
219 }
220 
221 }  // namespace proto_merger
222 }  // namespace perfetto
223