blob: 9183918fcc6375248466b3ceca9f63b110e54b4a [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
2 * Copyright (C) 2016 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
Joe Onorato76690122016-12-20 08:18:32 -080018#include <frameworks/base/core/proto/android/os/incident.pb.h>
Joe Onorato1754d742016-11-21 17:51:35 -080019
Joe Onorato1754d742016-11-21 17:51:35 -080020#include <map>
Yi Jinbe6de302017-10-24 12:30:24 -070021#include <set>
Yi Jinf8601842017-08-15 22:01:41 -070022#include <string>
Yi Jin0f2599f2017-11-16 18:19:45 -080023#include <sstream>
Joe Onorato1754d742016-11-21 17:51:35 -080024
Yi Jinf8601842017-08-15 22:01:41 -070025using namespace android;
Joe Onorato1754d742016-11-21 17:51:35 -080026using namespace android::os;
27using namespace google::protobuf;
28using namespace google::protobuf::io;
29using namespace google::protobuf::internal;
30using namespace std;
31
Yi Jinbe6de302017-10-24 12:30:24 -070032/**
33 * Implementation details:
34 * This binary auto generates .cpp files for incident and incidentd.
35 *
36 * When argument "incident" is specified, it generates incident_section.cpp file.
37 *
38 * When argument "incidentd" is specified, it generates section_list.cpp file.
39 *
40 * In section_list.cpp file, it generates a SECTION_LIST array and a PRIVACY_POLICY_LIST array.
41 * For SECTION_LIST, it generates Section.h classes only for proto fields with section option enabled.
42 * For PRIVACY_POLICY_LIST, it generates Privacy.h classes only for proto fields with privacy option enabled.
43 *
44 * For Privacy struct, it is possible to have self recursion definitions since protobuf is defining "classes"
45 * So the logic to handle it becomes very complicated when Privacy tag of a message contains a list of Privacies
46 * of its sub-messages. The code also handles multiple depth of self recursion fields.
47 *
48 * For example here is a one level self recursion message WindowManager:
49 * message WindowState {
50 * string state = 1 [(privacy).dest = LOCAL];
51 * int32 display_id = 2;
52 * repeated WindowState child_windows = 3;
53 * }
54 *
55 * message WindowManager {
56 * WindowState my_window = 1;
57 * }
58 *
59 * When generating Privacy options for WindowManager, this tool will generate cpp syntax source code:
60 *
61 * #include "section_list.h"
62 * ...
Yi Jinbdf58942017-11-14 17:58:19 -080063 * Privacy WindowState__state { 1, 9, NULL, LOCAL, NULL }; // first two integers are values for field id and proto type.
64 * Privacy WindowState__child_windows { 3, 11, NULL, UNSET, NULL }; // reserved for WindowState_LIST
65 * Privacy* WindowState__MSG__UNSET[] = {
Yi Jinbe6de302017-10-24 12:30:24 -070066 * &WindowState_state,
67 * // display id is default, nothing is generated.
68 * &WindowState_child_windows,
69 * NULL // terminator of the array
70 * };
Yi Jinbdf58942017-11-14 17:58:19 -080071 * Privacy WindowState__my_window { 1, 11, WindowState__MSG__UNSET, UNSET, NULL };
Yi Jinbe6de302017-10-24 12:30:24 -070072 *
73 * createList() {
74 * ...
Yi Jinbdf58942017-11-14 17:58:19 -080075 * WindowState_child_windows.children = WindowState__MSG_UNSET; // point to its own definition after the list is defined.
Yi Jinbe6de302017-10-24 12:30:24 -070076 * ...
77 * }
78 *
79 * const Privacy** PRIVACY_POLICY_LIST = createList();
80 * const int PRIVACY_POLICY_COUNT = 1;
Yi Jinbdf58942017-11-14 17:58:19 -080081 *
82 * Privacy Value Inheritance rules:
83 * 1. Both field and message can be tagged with DESTINATION: LOCAL(L), EXPLICIT(E), AUTOMATIC(A).
84 * 2. Primitives inherits containing message's tag unless defined explicitly.
85 * 3. Containing message's tag doesn't apply to message fields, even when unset (in this case, uses its default message tag).
86 * 4. Message field tag overrides its default message tag.
87 * 5. UNSET tag defaults to EXPLICIT.
Yi Jinbe6de302017-10-24 12:30:24 -070088 */
89
90// The assignments will be called when constructs PRIVACY_POLICY_LIST, has to be global variable
91vector<string> gSelfRecursionAssignments;
92
Yi Jin0ed9b682017-08-18 14:51:20 -070093static inline void emptyline() {
Joe Onorato1754d742016-11-21 17:51:35 -080094 printf("\n");
Yi Jinf8601842017-08-15 22:01:41 -070095}
Joe Onorato1754d742016-11-21 17:51:35 -080096
Yi Jin0ed9b682017-08-18 14:51:20 -070097static void generateHead(const char* header) {
98 printf("// Auto generated file. Do not modify\n");
99 emptyline();
100 printf("#include \"%s.h\"\n", header);
101 emptyline();
102}
103
Yi Jinbe6de302017-10-24 12:30:24 -0700104// ======================== incident_sections =============================
Yi Jin0ed9b682017-08-18 14:51:20 -0700105static bool generateIncidentSectionsCpp(Descriptor const* descriptor)
Yi Jinf8601842017-08-15 22:01:41 -0700106{
107 generateHead("incident_sections");
108
109 map<string,FieldDescriptor const*> sections;
110 int N;
Joe Onorato1754d742016-11-21 17:51:35 -0800111 N = descriptor->field_count();
112 for (int i=0; i<N; i++) {
113 const FieldDescriptor* field = descriptor->field(i);
114 if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
115 sections[field->name()] = field;
116 }
117 }
118
119 printf("IncidentSection const INCIDENT_SECTIONS[] = {\n");
120 N = sections.size();
121 int i = 0;
122 for (map<string,FieldDescriptor const*>::const_iterator it = sections.begin();
123 it != sections.end(); it++, i++) {
124 const FieldDescriptor* field = it->second;
125 printf(" { %d, \"%s\" }", field->number(), field->name().c_str());
126 if (i != N-1) {
127 printf(",\n");
128 } else {
129 printf("\n");
130 }
131 }
132 printf("};\n");
133
134 printf("const int INCIDENT_SECTION_COUNT = %d;\n", N);
135
Yi Jinf8601842017-08-15 22:01:41 -0700136 return true;
137}
138
Yi Jinbe6de302017-10-24 12:30:24 -0700139// ========================= section_list ===================================
Yi Jinf8601842017-08-15 22:01:41 -0700140static void splitAndPrint(const string& args) {
141 size_t base = 0;
142 size_t found;
143 while (true) {
144 found = args.find_first_of(" ", base);
145 if (found != base) {
146 string arg = args.substr(base, found - base);
147 printf(" \"%s\",", arg.c_str());
148 }
149 if (found == args.npos) break;
150 base = found + 1;
151 }
152}
153
Yi Jinbe6de302017-10-24 12:30:24 -0700154static string replaceAll(const string& fieldName, const char oldC, const string& newS) {
155 if (fieldName.find_first_of(oldC) == fieldName.npos) return fieldName.c_str();
Yi Jin0ed9b682017-08-18 14:51:20 -0700156 size_t pos = 0, idx = 0;
Yi Jinbe6de302017-10-24 12:30:24 -0700157 char* res = new char[fieldName.size() * newS.size() + 1]; // assign a larger buffer
158 while (pos != fieldName.size()) {
159 char cur = fieldName[pos++];
Yi Jin0ed9b682017-08-18 14:51:20 -0700160 if (cur != oldC) {
161 res[idx++] = cur;
162 continue;
163 }
164
165 for (size_t i=0; i<newS.size(); i++) {
166 res[idx++] = newS[i];
167 }
168 }
169 res[idx] = '\0';
Yi Jinbe6de302017-10-24 12:30:24 -0700170 string result(res);
Yunlian Jiang3809bbf2017-09-05 15:50:58 -0700171 delete [] res;
172 return result;
Yi Jin0ed9b682017-08-18 14:51:20 -0700173}
174
Yi Jinbdf58942017-11-14 17:58:19 -0800175static inline void printPrivacy(const string& name, const FieldDescriptor* field, const string& children,
176 const Destination dest, const string& patterns, const string& comments = "") {
177 printf("Privacy %s = { %d, %d, %s, %d, %s };%s\n", name.c_str(), field->number(), field->type(),
178 children.c_str(), dest, patterns.c_str(), comments.c_str());
Yi Jin0ed9b682017-08-18 14:51:20 -0700179}
180
Yi Jinbdf58942017-11-14 17:58:19 -0800181// Get Custom Options ================================================================================
Yi Jinbe6de302017-10-24 12:30:24 -0700182static inline SectionFlags getSectionFlags(const FieldDescriptor* field) {
183 return field->options().GetExtension(section);
184}
185
186static inline PrivacyFlags getPrivacyFlags(const FieldDescriptor* field) {
187 return field->options().GetExtension(privacy);
188}
189
Yi Jinbdf58942017-11-14 17:58:19 -0800190static inline PrivacyFlags getPrivacyFlags(const Descriptor* descriptor) {
191 return descriptor->options().GetExtension(msg_privacy);
Yi Jinbe6de302017-10-24 12:30:24 -0700192}
193
Yi Jinbdf58942017-11-14 17:58:19 -0800194// Get Destinations ===================================================================================
195static inline Destination getMessageDest(const Descriptor* descriptor, const Destination overridden) {
196 return overridden != DEST_UNSET ? overridden : getPrivacyFlags(descriptor).dest();
197}
198
199// Returns field's own dest, when it is a message field, uses its message default tag if unset.
200static inline Destination getFieldDest(const FieldDescriptor* field) {
201 Destination fieldDest = getPrivacyFlags(field).dest();
202 return field->type() != FieldDescriptor::TYPE_MESSAGE ? fieldDest :
203 getMessageDest(field->message_type(), fieldDest);
204}
205
206// Get Names ===========================================================================================
207static inline string getFieldName(const FieldDescriptor* field) {
208 // replace . with double underscores to avoid name conflicts since fields use snake naming convention
209 return replaceAll(field->full_name(), '.', "__");
210}
211
212
213static inline string getMessageName(const Descriptor* descriptor, const Destination overridden) {
214 // replace . with one underscore since messages use camel naming convention
215 return replaceAll(descriptor->full_name(), '.', "_") + "__MSG__" +
216 to_string(getMessageDest(descriptor, overridden));
217}
218
219// IsDefault ============================================================================================
220// Returns true if a field is default. Default is defined as this field has same dest as its containing message.
221// For message fields, it only looks at its field tag and own default mesaage tag, doesn't recursively go deeper.
222static inline bool isDefaultField(const FieldDescriptor* field, const Destination containerDest) {
223 Destination fieldDest = getFieldDest(field);
224 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
225 return fieldDest == containerDest || (fieldDest == DEST_UNSET);
226 } else {
227 return fieldDest == containerDest ||
228 (containerDest == DEST_UNSET && fieldDest == DEST_EXPLICIT) ||
229 (containerDest == DEST_EXPLICIT && fieldDest == DEST_UNSET);
230 }
231}
232
233static bool isDefaultMessageImpl(const Descriptor* descriptor, const Destination dest, set<string>* parents) {
234 const int N = descriptor->field_count();
235 const Destination messageDest = getMessageDest(descriptor, dest);
Yi Jinbe6de302017-10-24 12:30:24 -0700236 parents->insert(descriptor->full_name());
237 for (int i=0; i<N; ++i) {
238 const FieldDescriptor* field = descriptor->field(i);
Yi Jinbdf58942017-11-14 17:58:19 -0800239 const Destination fieldDest = getFieldDest(field);
240 // If current field is not default, return false immediately
241 if (!isDefaultField(field, messageDest)) return false;
Yi Jinbe6de302017-10-24 12:30:24 -0700242 switch (field->type()) {
243 case FieldDescriptor::TYPE_MESSAGE:
244 // if self recursion, don't go deep.
245 if (parents->find(field->message_type()->full_name()) != parents->end()) break;
246 // if is a default message, just continue
Yi Jinbdf58942017-11-14 17:58:19 -0800247 if (isDefaultMessageImpl(field->message_type(), fieldDest, parents)) break;
Yi Jinbe6de302017-10-24 12:30:24 -0700248 // sub message is not default, so this message is always not default
249 return false;
250 case FieldDescriptor::TYPE_STRING:
251 if (getPrivacyFlags(field).patterns_size() != 0) return false;
252 default:
253 continue;
254 }
255 }
256 parents->erase(descriptor->full_name());
257 return true;
258}
259
Yi Jinbdf58942017-11-14 17:58:19 -0800260// Recursively look at if this message is default, meaning all its fields and sub-messages
261// can be described by the same dest.
262static bool isDefaultMessage(const Descriptor* descriptor, const Destination dest) {
Yi Jinbe6de302017-10-24 12:30:24 -0700263 set<string> parents;
Yi Jinbdf58942017-11-14 17:58:19 -0800264 return isDefaultMessageImpl(descriptor, dest, &parents);
Yi Jinbe6de302017-10-24 12:30:24 -0700265}
266
Yi Jinbdf58942017-11-14 17:58:19 -0800267// ===============================================================================================================
268static bool numberInOrder(const FieldDescriptor* f1, const FieldDescriptor* f2) {
269 return f1->number() < f2->number();
270}
Yi Jinbe6de302017-10-24 12:30:24 -0700271
Yi Jinbdf58942017-11-14 17:58:19 -0800272// field numbers are possibly out of order, sort them here.
273static vector<const FieldDescriptor*> sortFields(const Descriptor* descriptor) {
274 vector<const FieldDescriptor*> fields;
275 fields.reserve(descriptor->field_count());
276 for (int i=0; i<descriptor->field_count(); i++) {
277 fields.push_back(descriptor->field(i));
278 }
279 std::sort(fields.begin(), fields.end(), numberInOrder);
280 return fields;
281}
282
283// This function looks for privacy tags of a message type and recursively its sub-messages.
284// It generates Privacy objects for each non-default fields including non-default sub-messages.
285// And if the message has Privacy objects generated, it returns a list of them.
286// Returns false if the descriptor doesn't have any non default privacy flags set, including its submessages
287static bool generatePrivacyFlags(const Descriptor* descriptor, const Destination overridden,
288 map<string, bool> &variableNames, set<string>* parents) {
289 const string messageName = getMessageName(descriptor, overridden);
290 const Destination messageDest = getMessageDest(descriptor, overridden);
291
292 if (variableNames.find(messageName) != variableNames.end()) {
293 bool hasDefault = variableNames[messageName];
294 return !hasDefault; // if has default, then don't generate privacy flags.
Yi Jinbe6de302017-10-24 12:30:24 -0700295 }
296 // insert the message type name so sub-message will figure out if self-recursion occurs
Yi Jinbdf58942017-11-14 17:58:19 -0800297 parents->insert(messageName);
Yi Jinbe6de302017-10-24 12:30:24 -0700298
Yi Jinbdf58942017-11-14 17:58:19 -0800299 // sort fields based on number, iterate though them and generate sub flags first
300 vector<const FieldDescriptor*> fieldsInOrder = sortFields(descriptor);
301 bool hasDefaultFlags[fieldsInOrder.size()];
302 for (size_t i=0; i<fieldsInOrder.size(); i++) {
303 const FieldDescriptor* field = fieldsInOrder[i];
Yi Jinbe6de302017-10-24 12:30:24 -0700304 const string fieldName = getFieldName(field);
Yi Jinbdf58942017-11-14 17:58:19 -0800305 const Destination fieldDest = getFieldDest(field);
Yi Jin0ed9b682017-08-18 14:51:20 -0700306
Yi Jinbdf58942017-11-14 17:58:19 -0800307 if (variableNames.find(fieldName) != variableNames.end()) {
308 hasDefaultFlags[i] = variableNames[fieldName];
309 continue;
310 }
311 hasDefaultFlags[i] = isDefaultField(field, messageDest);
312
Yi Jinbe6de302017-10-24 12:30:24 -0700313 string fieldMessageName;
Yi Jinbdf58942017-11-14 17:58:19 -0800314 PrivacyFlags p = getPrivacyFlags(field);
Yi Jin0ed9b682017-08-18 14:51:20 -0700315 switch (field->type()) {
316 case FieldDescriptor::TYPE_MESSAGE:
Yi Jinbdf58942017-11-14 17:58:19 -0800317 fieldMessageName = getMessageName(field->message_type(), fieldDest);
Yi Jinbe6de302017-10-24 12:30:24 -0700318 if (parents->find(fieldMessageName) != parents->end()) { // Self-Recursion proto definition
Yi Jinbdf58942017-11-14 17:58:19 -0800319 if (hasDefaultFlags[i]) {
320 hasDefaultFlags[i] = isDefaultMessage(field->message_type(), fieldDest);
Yi Jinbe6de302017-10-24 12:30:24 -0700321 }
322 if (!hasDefaultFlags[i]) {
Yi Jinbdf58942017-11-14 17:58:19 -0800323 printPrivacy(fieldName, field, "NULL", fieldDest, "NULL",
324 " // self recursion field of " + fieldMessageName);
Yi Jinbe6de302017-10-24 12:30:24 -0700325 // generate the assignment and used to construct createList function later on.
326 gSelfRecursionAssignments.push_back(fieldName + ".children = " + fieldMessageName);
327 }
Yi Jinbdf58942017-11-14 17:58:19 -0800328 } else if (generatePrivacyFlags(field->message_type(), p.dest(), variableNames, parents)) {
329 if (variableNames.find(fieldName) == variableNames.end()) {
330 printPrivacy(fieldName, field, fieldMessageName, fieldDest, "NULL");
331 }
332 hasDefaultFlags[i] = false;
333 } else if (!hasDefaultFlags[i]) {
334 printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
Yi Jin22769e02017-10-16 14:42:50 -0700335 }
Yi Jin0ed9b682017-08-18 14:51:20 -0700336 break;
337 case FieldDescriptor::TYPE_STRING:
Yi Jinbdf58942017-11-14 17:58:19 -0800338 if (p.patterns_size() != 0) { // if patterns are specified
339 if (hasDefaultFlags[i]) break;
340 printf("const char* %s_patterns[] = {\n", fieldName.c_str());
341 for (int j=0; j<p.patterns_size(); j++) {
342 // generated string needs to escape backslash too, duplicate it to allow escape again.
343 printf(" \"%s\",\n", replaceAll(p.patterns(j), '\\', "\\\\").c_str());
344 }
345 printf(" NULL };\n");
346 printPrivacy(fieldName, field, "NULL", fieldDest, fieldName + "_patterns");
347 break;
Yi Jin0ed9b682017-08-18 14:51:20 -0700348 }
Yi Jinbdf58942017-11-14 17:58:19 -0800349 // else treat string field as primitive field and goes to default
Yi Jin0ed9b682017-08-18 14:51:20 -0700350 default:
Yi Jinbdf58942017-11-14 17:58:19 -0800351 if (!hasDefaultFlags[i]) printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
Yi Jin0ed9b682017-08-18 14:51:20 -0700352 }
Yi Jinbdf58942017-11-14 17:58:19 -0800353 // Don't generate a variable twice
354 if (!hasDefaultFlags[i]) variableNames[fieldName] = false;
Yi Jin0ed9b682017-08-18 14:51:20 -0700355 }
356
357 bool allDefaults = true;
Yi Jinbdf58942017-11-14 17:58:19 -0800358 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700359 allDefaults &= hasDefaultFlags[i];
360 }
Yi Jinbe6de302017-10-24 12:30:24 -0700361
Yi Jinbdf58942017-11-14 17:58:19 -0800362 parents->erase(messageName); // erase the message type name when exit the message.
363 variableNames[messageName] = allDefaults; // store the privacy tags of the message here to avoid overhead.
Yi Jinbe6de302017-10-24 12:30:24 -0700364
Yi Jin22769e02017-10-16 14:42:50 -0700365 if (allDefaults) return false;
Yi Jin0ed9b682017-08-18 14:51:20 -0700366
367 emptyline();
Yi Jin7e0b4e52017-09-12 20:00:25 -0700368 int policyCount = 0;
Yi Jinbdf58942017-11-14 17:58:19 -0800369 printf("Privacy* %s[] = {\n", messageName.c_str());
370 for (size_t i=0; i<fieldsInOrder.size(); i++) {
371 const FieldDescriptor* field = fieldsInOrder[i];
Yi Jin0ed9b682017-08-18 14:51:20 -0700372 if (hasDefaultFlags[i]) continue;
Yi Jinbe6de302017-10-24 12:30:24 -0700373 printf(" &%s,\n", getFieldName(field).c_str());
Yi Jin7e0b4e52017-09-12 20:00:25 -0700374 policyCount++;
Yi Jin0ed9b682017-08-18 14:51:20 -0700375 }
Yi Jinbe6de302017-10-24 12:30:24 -0700376 printf(" NULL };\n");
Yi Jin0ed9b682017-08-18 14:51:20 -0700377 emptyline();
Yi Jin22769e02017-10-16 14:42:50 -0700378 return true;
Yi Jin0ed9b682017-08-18 14:51:20 -0700379}
380
381static bool generateSectionListCpp(Descriptor const* descriptor) {
Yi Jinf8601842017-08-15 22:01:41 -0700382 generateHead("section_list");
383
Yi Jin0ed9b682017-08-18 14:51:20 -0700384 // generates SECTION_LIST
Yi Jinbe6de302017-10-24 12:30:24 -0700385 printf("// Generate SECTION_LIST.\n\n");
386
Yi Jinf8601842017-08-15 22:01:41 -0700387 printf("const Section* SECTION_LIST[] = {\n");
Yi Jinf8601842017-08-15 22:01:41 -0700388 for (int i=0; i<descriptor->field_count(); i++) {
389 const FieldDescriptor* field = descriptor->field(i);
390
391 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
392 continue;
393 }
Yi Jinbe6de302017-10-24 12:30:24 -0700394 const SectionFlags s = getSectionFlags(field);
Yi Jinf8601842017-08-15 22:01:41 -0700395 switch (s.type()) {
396 case SECTION_NONE:
397 continue;
398 case SECTION_FILE:
399 printf(" new FileSection(%d, \"%s\"),\n", field->number(), s.args().c_str());
400 break;
401 case SECTION_COMMAND:
402 printf(" new CommandSection(%d,", field->number());
403 splitAndPrint(s.args());
404 printf(" NULL),\n");
405 break;
406 case SECTION_DUMPSYS:
407 printf(" new DumpsysSection(%d,", field->number());
408 splitAndPrint(s.args());
409 printf(" NULL),\n");
410 break;
Yi Jin3c034c92017-12-22 17:36:47 -0800411 case SECTION_LOG:
412 printf(" new LogSection(%d, %s),\n", field->number(), s.args().c_str());
413 break;
Yi Jin1a11fa12018-02-22 16:44:10 -0800414 case SECTION_GZIP:
415 printf(" new GZipSection(%d,", field->number());
416 splitAndPrint(s.args());
417 printf(" NULL),\n");
418 break;
Yi Jinf8601842017-08-15 22:01:41 -0700419 }
420 }
Yi Jin0ed9b682017-08-18 14:51:20 -0700421 printf(" NULL };\n");
Yi Jinbe6de302017-10-24 12:30:24 -0700422
423 emptyline();
424 printf("// =============================================================================\n");
Yi Jin0ed9b682017-08-18 14:51:20 -0700425 emptyline();
426
Yi Jinbe6de302017-10-24 12:30:24 -0700427 // generates PRIVACY_POLICY_LIST
428 printf("// Generate PRIVACY_POLICY_LIST.\n\n");
Yi Jinbdf58942017-11-14 17:58:19 -0800429 map<string, bool> variableNames;
Yi Jinbe6de302017-10-24 12:30:24 -0700430 set<string> parents;
Yi Jinbdf58942017-11-14 17:58:19 -0800431 vector<const FieldDescriptor*> fieldsInOrder = sortFields(descriptor);
432 bool skip[fieldsInOrder.size()];
433 const Destination incidentDest = getPrivacyFlags(descriptor).dest();
Yi Jinbe6de302017-10-24 12:30:24 -0700434
Yi Jinbdf58942017-11-14 17:58:19 -0800435 for (size_t i=0; i<fieldsInOrder.size(); i++) {
436 const FieldDescriptor* field = fieldsInOrder[i];
Yi Jinbe6de302017-10-24 12:30:24 -0700437 const string fieldName = getFieldName(field);
Yi Jinbdf58942017-11-14 17:58:19 -0800438 const Destination fieldDest = getFieldDest(field);
439 const string fieldMessageName = getMessageName(field->message_type(), fieldDest);
Yi Jinbe6de302017-10-24 12:30:24 -0700440
441 skip[i] = true;
442
443 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
444 continue;
445 }
Yi Jinbdf58942017-11-14 17:58:19 -0800446 // generate privacy flags for each section.
447 if (generatePrivacyFlags(field->message_type(), fieldDest, variableNames, &parents)) {
448 printPrivacy(fieldName, field, fieldMessageName, fieldDest, "NULL");
449 } else if (isDefaultField(field, incidentDest)) {
Yi Jinbe6de302017-10-24 12:30:24 -0700450 continue; // don't create a new privacy if the value is default.
451 } else {
Yi Jinbdf58942017-11-14 17:58:19 -0800452 printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
Yi Jinbe6de302017-10-24 12:30:24 -0700453 }
454 skip[i] = false;
Yi Jin0ed9b682017-08-18 14:51:20 -0700455 }
456
Yi Jinbe6de302017-10-24 12:30:24 -0700457 // generate final PRIVACY_POLICY_LIST
458 emptyline();
459 int policyCount = 0;
460 if (gSelfRecursionAssignments.empty()) {
461 printf("Privacy* privacyArray[] = {\n");
Yi Jinbdf58942017-11-14 17:58:19 -0800462 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jinbe6de302017-10-24 12:30:24 -0700463 if (skip[i]) continue;
Yi Jinbdf58942017-11-14 17:58:19 -0800464 printf(" &%s,\n", getFieldName(fieldsInOrder[i]).c_str());
Yi Jinbe6de302017-10-24 12:30:24 -0700465 policyCount++;
466 }
467 printf("};\n\n");
468 printf("const Privacy** PRIVACY_POLICY_LIST = const_cast<const Privacy**>(privacyArray);\n\n");
469 printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount);
470 } else {
Yi Jinbdf58942017-11-14 17:58:19 -0800471 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jinbe6de302017-10-24 12:30:24 -0700472 if (!skip[i]) policyCount++;
473 }
474
475 printf("static const Privacy** createList() {\n");
476 for (size_t i=0; i<gSelfRecursionAssignments.size(); ++i) {
477 printf(" %s;\n", gSelfRecursionAssignments[i].c_str());
478 }
479 printf(" Privacy** privacyArray = (Privacy**)malloc(%d * sizeof(Privacy**));\n", policyCount);
480 policyCount = 0; // reset
Yi Jinbdf58942017-11-14 17:58:19 -0800481 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jinbe6de302017-10-24 12:30:24 -0700482 if (skip[i]) continue;
Yi Jinbdf58942017-11-14 17:58:19 -0800483 printf(" privacyArray[%d] = &%s;\n", policyCount++, getFieldName(fieldsInOrder[i]).c_str());
Yi Jinbe6de302017-10-24 12:30:24 -0700484 }
485 printf(" return const_cast<const Privacy**>(privacyArray);\n");
486 printf("}\n\n");
487 printf("const Privacy** PRIVACY_POLICY_LIST = createList();\n\n");
488 printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount);
489 }
Yi Jinf8601842017-08-15 22:01:41 -0700490 return true;
491}
492
493// ================================================================================
Yi Jin0f2599f2017-11-16 18:19:45 -0800494static string replace_string(const string& str, const char replace, const char with)
495{
496 string result(str);
497 const int N = result.size();
498 for (int i=0; i<N; i++) {
499 if (result[i] == replace) {
500 result[i] = with;
501 }
502 }
503 return result;
504}
505
506static void generateCsv(Descriptor const* descriptor, const string& indent, set<string>* parents) {
507 DebugStringOptions options;
508 options.include_comments = true;
509 for (int i=0; i<descriptor->field_count(); i++) {
510 const FieldDescriptor* field = descriptor->field(i);
511 stringstream text;
512 if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
513 text << field->message_type()->name();
514 } else {
515 text << field->type_name();
516 }
517 text << " " << field->name();
518 printf("%s%s,\n", indent.c_str(), replace_string(text.str(), '\n', ' ').c_str());
519 if (field->type() == FieldDescriptor::TYPE_MESSAGE &&
520 parents->find(field->message_type()->full_name()) == parents->end()) {
521 parents->insert(field->message_type()->full_name());
522 generateCsv(field->message_type(), indent + ",", parents);
523 parents->erase(field->message_type()->full_name());
524 }
525 }
526}
527
528// ================================================================================
Yi Jinf8601842017-08-15 22:01:41 -0700529int main(int argc, char const *argv[])
530{
Yi Jin0f2599f2017-11-16 18:19:45 -0800531 if (argc < 2) return 1;
Yi Jinf8601842017-08-15 22:01:41 -0700532 const char* module = argv[1];
533
Yi Jin0ed9b682017-08-18 14:51:20 -0700534 Descriptor const* descriptor = IncidentProto::descriptor();
535
Yi Jinf8601842017-08-15 22:01:41 -0700536 if (strcmp(module, "incident") == 0) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700537 return !generateIncidentSectionsCpp(descriptor);
Yi Jinf8601842017-08-15 22:01:41 -0700538 }
539 if (strcmp(module, "incidentd") == 0 ) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700540 return !generateSectionListCpp(descriptor);
Yi Jinf8601842017-08-15 22:01:41 -0700541 }
Yi Jin0f2599f2017-11-16 18:19:45 -0800542 // Generates Csv Format of proto definition for each section.
543 if (strcmp(module, "csv") == 0 && argc > 2) {
544 int sectionId = atoi(argv[2]);
545 for (int i=0; i<descriptor->field_count(); i++) {
546 const FieldDescriptor* field = descriptor->field(i);
547 if (strcmp(field->name().c_str(), argv[2]) == 0
548 || field->number() == sectionId) {
549 set<string> parents;
550 printf("%s\n", field->name().c_str());
551 generateCsv(field->message_type(), "", &parents);
552 break;
553 }
554 }
555 // Returns failure if csv is enabled to prevent Android building with it.
556 // It doesn't matter if this command runs manually.
557 return 1;
558 }
559 // Returns failure if not called by the whitelisted modules
Yi Jinf8601842017-08-15 22:01:41 -0700560 return 1;
Joe Onorato1754d742016-11-21 17:51:35 -0800561}