blob: 7650795075136a061da9ecc5a9c1f3be40ab9bb0 [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 Onorato766901262016-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>
Joe Onorato1754d742016-11-21 17:51:35 -080023
Yi Jinf8601842017-08-15 22:01:41 -070024using namespace android;
Joe Onorato1754d742016-11-21 17:51:35 -080025using namespace android::os;
26using namespace google::protobuf;
27using namespace google::protobuf::io;
28using namespace google::protobuf::internal;
29using namespace std;
30
Yi Jinbe6de302017-10-24 12:30:24 -070031/**
32 * Implementation details:
33 * This binary auto generates .cpp files for incident and incidentd.
34 *
35 * When argument "incident" is specified, it generates incident_section.cpp file.
36 *
37 * When argument "incidentd" is specified, it generates section_list.cpp file.
38 *
39 * In section_list.cpp file, it generates a SECTION_LIST array and a PRIVACY_POLICY_LIST array.
40 * For SECTION_LIST, it generates Section.h classes only for proto fields with section option enabled.
41 * For PRIVACY_POLICY_LIST, it generates Privacy.h classes only for proto fields with privacy option enabled.
42 *
43 * For Privacy struct, it is possible to have self recursion definitions since protobuf is defining "classes"
44 * So the logic to handle it becomes very complicated when Privacy tag of a message contains a list of Privacies
45 * of its sub-messages. The code also handles multiple depth of self recursion fields.
46 *
47 * For example here is a one level self recursion message WindowManager:
48 * message WindowState {
49 * string state = 1 [(privacy).dest = LOCAL];
50 * int32 display_id = 2;
51 * repeated WindowState child_windows = 3;
52 * }
53 *
54 * message WindowManager {
55 * WindowState my_window = 1;
56 * }
57 *
58 * When generating Privacy options for WindowManager, this tool will generate cpp syntax source code:
59 *
60 * #include "section_list.h"
61 * ...
Yi Jinbdf58942017-11-14 17:58:19 -080062 * Privacy WindowState__state { 1, 9, NULL, LOCAL, NULL }; // first two integers are values for field id and proto type.
63 * Privacy WindowState__child_windows { 3, 11, NULL, UNSET, NULL }; // reserved for WindowState_LIST
64 * Privacy* WindowState__MSG__UNSET[] = {
Yi Jinbe6de302017-10-24 12:30:24 -070065 * &WindowState_state,
66 * // display id is default, nothing is generated.
67 * &WindowState_child_windows,
68 * NULL // terminator of the array
69 * };
Yi Jinbdf58942017-11-14 17:58:19 -080070 * Privacy WindowState__my_window { 1, 11, WindowState__MSG__UNSET, UNSET, NULL };
Yi Jinbe6de302017-10-24 12:30:24 -070071 *
72 * createList() {
73 * ...
Yi Jinbdf58942017-11-14 17:58:19 -080074 * WindowState_child_windows.children = WindowState__MSG_UNSET; // point to its own definition after the list is defined.
Yi Jinbe6de302017-10-24 12:30:24 -070075 * ...
76 * }
77 *
78 * const Privacy** PRIVACY_POLICY_LIST = createList();
79 * const int PRIVACY_POLICY_COUNT = 1;
Yi Jinbdf58942017-11-14 17:58:19 -080080 *
81 * Privacy Value Inheritance rules:
82 * 1. Both field and message can be tagged with DESTINATION: LOCAL(L), EXPLICIT(E), AUTOMATIC(A).
83 * 2. Primitives inherits containing message's tag unless defined explicitly.
84 * 3. Containing message's tag doesn't apply to message fields, even when unset (in this case, uses its default message tag).
85 * 4. Message field tag overrides its default message tag.
86 * 5. UNSET tag defaults to EXPLICIT.
Yi Jinbe6de302017-10-24 12:30:24 -070087 */
88
89// The assignments will be called when constructs PRIVACY_POLICY_LIST, has to be global variable
90vector<string> gSelfRecursionAssignments;
91
Yi Jin0ed9b682017-08-18 14:51:20 -070092static inline void emptyline() {
Joe Onorato1754d742016-11-21 17:51:35 -080093 printf("\n");
Yi Jinf8601842017-08-15 22:01:41 -070094}
Joe Onorato1754d742016-11-21 17:51:35 -080095
Yi Jin0ed9b682017-08-18 14:51:20 -070096static void generateHead(const char* header) {
97 printf("// Auto generated file. Do not modify\n");
98 emptyline();
99 printf("#include \"%s.h\"\n", header);
100 emptyline();
101}
102
Yi Jinbe6de302017-10-24 12:30:24 -0700103// ======================== incident_sections =============================
Yi Jin0ed9b682017-08-18 14:51:20 -0700104static bool generateIncidentSectionsCpp(Descriptor const* descriptor)
Yi Jinf8601842017-08-15 22:01:41 -0700105{
106 generateHead("incident_sections");
107
108 map<string,FieldDescriptor const*> sections;
109 int N;
Joe Onorato1754d742016-11-21 17:51:35 -0800110 N = descriptor->field_count();
111 for (int i=0; i<N; i++) {
112 const FieldDescriptor* field = descriptor->field(i);
113 if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
114 sections[field->name()] = field;
115 }
116 }
117
118 printf("IncidentSection const INCIDENT_SECTIONS[] = {\n");
119 N = sections.size();
120 int i = 0;
121 for (map<string,FieldDescriptor const*>::const_iterator it = sections.begin();
122 it != sections.end(); it++, i++) {
123 const FieldDescriptor* field = it->second;
124 printf(" { %d, \"%s\" }", field->number(), field->name().c_str());
125 if (i != N-1) {
126 printf(",\n");
127 } else {
128 printf("\n");
129 }
130 }
131 printf("};\n");
132
133 printf("const int INCIDENT_SECTION_COUNT = %d;\n", N);
134
Yi Jinf8601842017-08-15 22:01:41 -0700135 return true;
136}
137
Yi Jinbe6de302017-10-24 12:30:24 -0700138// ========================= section_list ===================================
Yi Jinf8601842017-08-15 22:01:41 -0700139static void splitAndPrint(const string& args) {
140 size_t base = 0;
141 size_t found;
142 while (true) {
143 found = args.find_first_of(" ", base);
144 if (found != base) {
145 string arg = args.substr(base, found - base);
146 printf(" \"%s\",", arg.c_str());
147 }
148 if (found == args.npos) break;
149 base = found + 1;
150 }
151}
152
Yi Jinbe6de302017-10-24 12:30:24 -0700153static string replaceAll(const string& fieldName, const char oldC, const string& newS) {
154 if (fieldName.find_first_of(oldC) == fieldName.npos) return fieldName.c_str();
Yi Jin0ed9b682017-08-18 14:51:20 -0700155 size_t pos = 0, idx = 0;
Yi Jinbe6de302017-10-24 12:30:24 -0700156 char* res = new char[fieldName.size() * newS.size() + 1]; // assign a larger buffer
157 while (pos != fieldName.size()) {
158 char cur = fieldName[pos++];
Yi Jin0ed9b682017-08-18 14:51:20 -0700159 if (cur != oldC) {
160 res[idx++] = cur;
161 continue;
162 }
163
164 for (size_t i=0; i<newS.size(); i++) {
165 res[idx++] = newS[i];
166 }
167 }
168 res[idx] = '\0';
Yi Jinbe6de302017-10-24 12:30:24 -0700169 string result(res);
Yunlian Jiang3809bbf2017-09-05 15:50:58 -0700170 delete [] res;
171 return result;
Yi Jin0ed9b682017-08-18 14:51:20 -0700172}
173
Yi Jinbdf58942017-11-14 17:58:19 -0800174static inline void printPrivacy(const string& name, const FieldDescriptor* field, const string& children,
175 const Destination dest, const string& patterns, const string& comments = "") {
176 printf("Privacy %s = { %d, %d, %s, %d, %s };%s\n", name.c_str(), field->number(), field->type(),
177 children.c_str(), dest, patterns.c_str(), comments.c_str());
Yi Jin0ed9b682017-08-18 14:51:20 -0700178}
179
Yi Jinbdf58942017-11-14 17:58:19 -0800180// Get Custom Options ================================================================================
Yi Jinbe6de302017-10-24 12:30:24 -0700181static inline SectionFlags getSectionFlags(const FieldDescriptor* field) {
182 return field->options().GetExtension(section);
183}
184
185static inline PrivacyFlags getPrivacyFlags(const FieldDescriptor* field) {
186 return field->options().GetExtension(privacy);
187}
188
Yi Jinbdf58942017-11-14 17:58:19 -0800189static inline PrivacyFlags getPrivacyFlags(const Descriptor* descriptor) {
190 return descriptor->options().GetExtension(msg_privacy);
Yi Jinbe6de302017-10-24 12:30:24 -0700191}
192
Yi Jinbdf58942017-11-14 17:58:19 -0800193// Get Destinations ===================================================================================
194static inline Destination getMessageDest(const Descriptor* descriptor, const Destination overridden) {
195 return overridden != DEST_UNSET ? overridden : getPrivacyFlags(descriptor).dest();
196}
197
198// Returns field's own dest, when it is a message field, uses its message default tag if unset.
199static inline Destination getFieldDest(const FieldDescriptor* field) {
200 Destination fieldDest = getPrivacyFlags(field).dest();
201 return field->type() != FieldDescriptor::TYPE_MESSAGE ? fieldDest :
202 getMessageDest(field->message_type(), fieldDest);
203}
204
205// Get Names ===========================================================================================
206static inline string getFieldName(const FieldDescriptor* field) {
207 // replace . with double underscores to avoid name conflicts since fields use snake naming convention
208 return replaceAll(field->full_name(), '.', "__");
209}
210
211
212static inline string getMessageName(const Descriptor* descriptor, const Destination overridden) {
213 // replace . with one underscore since messages use camel naming convention
214 return replaceAll(descriptor->full_name(), '.', "_") + "__MSG__" +
215 to_string(getMessageDest(descriptor, overridden));
216}
217
218// IsDefault ============================================================================================
219// Returns true if a field is default. Default is defined as this field has same dest as its containing message.
220// For message fields, it only looks at its field tag and own default mesaage tag, doesn't recursively go deeper.
221static inline bool isDefaultField(const FieldDescriptor* field, const Destination containerDest) {
222 Destination fieldDest = getFieldDest(field);
223 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
224 return fieldDest == containerDest || (fieldDest == DEST_UNSET);
225 } else {
226 return fieldDest == containerDest ||
227 (containerDest == DEST_UNSET && fieldDest == DEST_EXPLICIT) ||
228 (containerDest == DEST_EXPLICIT && fieldDest == DEST_UNSET);
229 }
230}
231
232static bool isDefaultMessageImpl(const Descriptor* descriptor, const Destination dest, set<string>* parents) {
233 const int N = descriptor->field_count();
234 const Destination messageDest = getMessageDest(descriptor, dest);
Yi Jinbe6de302017-10-24 12:30:24 -0700235 parents->insert(descriptor->full_name());
236 for (int i=0; i<N; ++i) {
237 const FieldDescriptor* field = descriptor->field(i);
Yi Jinbdf58942017-11-14 17:58:19 -0800238 const Destination fieldDest = getFieldDest(field);
239 // If current field is not default, return false immediately
240 if (!isDefaultField(field, messageDest)) return false;
Yi Jinbe6de302017-10-24 12:30:24 -0700241 switch (field->type()) {
242 case FieldDescriptor::TYPE_MESSAGE:
243 // if self recursion, don't go deep.
244 if (parents->find(field->message_type()->full_name()) != parents->end()) break;
245 // if is a default message, just continue
Yi Jinbdf58942017-11-14 17:58:19 -0800246 if (isDefaultMessageImpl(field->message_type(), fieldDest, parents)) break;
Yi Jinbe6de302017-10-24 12:30:24 -0700247 // sub message is not default, so this message is always not default
248 return false;
249 case FieldDescriptor::TYPE_STRING:
250 if (getPrivacyFlags(field).patterns_size() != 0) return false;
251 default:
252 continue;
253 }
254 }
255 parents->erase(descriptor->full_name());
256 return true;
257}
258
Yi Jinbdf58942017-11-14 17:58:19 -0800259// Recursively look at if this message is default, meaning all its fields and sub-messages
260// can be described by the same dest.
261static bool isDefaultMessage(const Descriptor* descriptor, const Destination dest) {
Yi Jinbe6de302017-10-24 12:30:24 -0700262 set<string> parents;
Yi Jinbdf58942017-11-14 17:58:19 -0800263 return isDefaultMessageImpl(descriptor, dest, &parents);
Yi Jinbe6de302017-10-24 12:30:24 -0700264}
265
Yi Jinbdf58942017-11-14 17:58:19 -0800266// ===============================================================================================================
267static bool numberInOrder(const FieldDescriptor* f1, const FieldDescriptor* f2) {
268 return f1->number() < f2->number();
269}
Yi Jinbe6de302017-10-24 12:30:24 -0700270
Yi Jinbdf58942017-11-14 17:58:19 -0800271// field numbers are possibly out of order, sort them here.
272static vector<const FieldDescriptor*> sortFields(const Descriptor* descriptor) {
273 vector<const FieldDescriptor*> fields;
274 fields.reserve(descriptor->field_count());
275 for (int i=0; i<descriptor->field_count(); i++) {
276 fields.push_back(descriptor->field(i));
277 }
278 std::sort(fields.begin(), fields.end(), numberInOrder);
279 return fields;
280}
281
282// This function looks for privacy tags of a message type and recursively its sub-messages.
283// It generates Privacy objects for each non-default fields including non-default sub-messages.
284// And if the message has Privacy objects generated, it returns a list of them.
285// Returns false if the descriptor doesn't have any non default privacy flags set, including its submessages
286static bool generatePrivacyFlags(const Descriptor* descriptor, const Destination overridden,
287 map<string, bool> &variableNames, set<string>* parents) {
288 const string messageName = getMessageName(descriptor, overridden);
289 const Destination messageDest = getMessageDest(descriptor, overridden);
290
291 if (variableNames.find(messageName) != variableNames.end()) {
292 bool hasDefault = variableNames[messageName];
293 return !hasDefault; // if has default, then don't generate privacy flags.
Yi Jinbe6de302017-10-24 12:30:24 -0700294 }
295 // insert the message type name so sub-message will figure out if self-recursion occurs
Yi Jinbdf58942017-11-14 17:58:19 -0800296 parents->insert(messageName);
Yi Jinbe6de302017-10-24 12:30:24 -0700297
Yi Jinbdf58942017-11-14 17:58:19 -0800298 // sort fields based on number, iterate though them and generate sub flags first
299 vector<const FieldDescriptor*> fieldsInOrder = sortFields(descriptor);
300 bool hasDefaultFlags[fieldsInOrder.size()];
301 for (size_t i=0; i<fieldsInOrder.size(); i++) {
302 const FieldDescriptor* field = fieldsInOrder[i];
Yi Jinbe6de302017-10-24 12:30:24 -0700303 const string fieldName = getFieldName(field);
Yi Jinbdf58942017-11-14 17:58:19 -0800304 const Destination fieldDest = getFieldDest(field);
Yi Jin0ed9b682017-08-18 14:51:20 -0700305
Yi Jinbdf58942017-11-14 17:58:19 -0800306 if (variableNames.find(fieldName) != variableNames.end()) {
307 hasDefaultFlags[i] = variableNames[fieldName];
308 continue;
309 }
310 hasDefaultFlags[i] = isDefaultField(field, messageDest);
311
Yi Jinbe6de302017-10-24 12:30:24 -0700312 string fieldMessageName;
Yi Jinbdf58942017-11-14 17:58:19 -0800313 PrivacyFlags p = getPrivacyFlags(field);
Yi Jin0ed9b682017-08-18 14:51:20 -0700314 switch (field->type()) {
315 case FieldDescriptor::TYPE_MESSAGE:
Yi Jinbdf58942017-11-14 17:58:19 -0800316 fieldMessageName = getMessageName(field->message_type(), fieldDest);
Yi Jinbe6de302017-10-24 12:30:24 -0700317 if (parents->find(fieldMessageName) != parents->end()) { // Self-Recursion proto definition
Yi Jinbdf58942017-11-14 17:58:19 -0800318 if (hasDefaultFlags[i]) {
319 hasDefaultFlags[i] = isDefaultMessage(field->message_type(), fieldDest);
Yi Jinbe6de302017-10-24 12:30:24 -0700320 }
321 if (!hasDefaultFlags[i]) {
Yi Jinbdf58942017-11-14 17:58:19 -0800322 printPrivacy(fieldName, field, "NULL", fieldDest, "NULL",
323 " // self recursion field of " + fieldMessageName);
Yi Jinbe6de302017-10-24 12:30:24 -0700324 // generate the assignment and used to construct createList function later on.
325 gSelfRecursionAssignments.push_back(fieldName + ".children = " + fieldMessageName);
326 }
Yi Jinbdf58942017-11-14 17:58:19 -0800327 } else if (generatePrivacyFlags(field->message_type(), p.dest(), variableNames, parents)) {
328 if (variableNames.find(fieldName) == variableNames.end()) {
329 printPrivacy(fieldName, field, fieldMessageName, fieldDest, "NULL");
330 }
331 hasDefaultFlags[i] = false;
332 } else if (!hasDefaultFlags[i]) {
333 printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
Yi Jin22769e02017-10-16 14:42:50 -0700334 }
Yi Jin0ed9b682017-08-18 14:51:20 -0700335 break;
336 case FieldDescriptor::TYPE_STRING:
Yi Jinbdf58942017-11-14 17:58:19 -0800337 if (p.patterns_size() != 0) { // if patterns are specified
338 if (hasDefaultFlags[i]) break;
339 printf("const char* %s_patterns[] = {\n", fieldName.c_str());
340 for (int j=0; j<p.patterns_size(); j++) {
341 // generated string needs to escape backslash too, duplicate it to allow escape again.
342 printf(" \"%s\",\n", replaceAll(p.patterns(j), '\\', "\\\\").c_str());
343 }
344 printf(" NULL };\n");
345 printPrivacy(fieldName, field, "NULL", fieldDest, fieldName + "_patterns");
346 break;
Yi Jin0ed9b682017-08-18 14:51:20 -0700347 }
Yi Jinbdf58942017-11-14 17:58:19 -0800348 // else treat string field as primitive field and goes to default
Yi Jin0ed9b682017-08-18 14:51:20 -0700349 default:
Yi Jinbdf58942017-11-14 17:58:19 -0800350 if (!hasDefaultFlags[i]) printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
Yi Jin0ed9b682017-08-18 14:51:20 -0700351 }
Yi Jinbdf58942017-11-14 17:58:19 -0800352 // Don't generate a variable twice
353 if (!hasDefaultFlags[i]) variableNames[fieldName] = false;
Yi Jin0ed9b682017-08-18 14:51:20 -0700354 }
355
356 bool allDefaults = true;
Yi Jinbdf58942017-11-14 17:58:19 -0800357 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700358 allDefaults &= hasDefaultFlags[i];
359 }
Yi Jinbe6de302017-10-24 12:30:24 -0700360
Yi Jinbdf58942017-11-14 17:58:19 -0800361 parents->erase(messageName); // erase the message type name when exit the message.
362 variableNames[messageName] = allDefaults; // store the privacy tags of the message here to avoid overhead.
Yi Jinbe6de302017-10-24 12:30:24 -0700363
Yi Jin22769e02017-10-16 14:42:50 -0700364 if (allDefaults) return false;
Yi Jin0ed9b682017-08-18 14:51:20 -0700365
366 emptyline();
Yi Jin7e0b4e52017-09-12 20:00:25 -0700367 int policyCount = 0;
Yi Jinbdf58942017-11-14 17:58:19 -0800368 printf("Privacy* %s[] = {\n", messageName.c_str());
369 for (size_t i=0; i<fieldsInOrder.size(); i++) {
370 const FieldDescriptor* field = fieldsInOrder[i];
Yi Jin0ed9b682017-08-18 14:51:20 -0700371 if (hasDefaultFlags[i]) continue;
Yi Jinbe6de302017-10-24 12:30:24 -0700372 printf(" &%s,\n", getFieldName(field).c_str());
Yi Jin7e0b4e52017-09-12 20:00:25 -0700373 policyCount++;
Yi Jin0ed9b682017-08-18 14:51:20 -0700374 }
Yi Jinbe6de302017-10-24 12:30:24 -0700375 printf(" NULL };\n");
Yi Jin0ed9b682017-08-18 14:51:20 -0700376 emptyline();
Yi Jin22769e02017-10-16 14:42:50 -0700377 return true;
Yi Jin0ed9b682017-08-18 14:51:20 -0700378}
379
380static bool generateSectionListCpp(Descriptor const* descriptor) {
Yi Jinf8601842017-08-15 22:01:41 -0700381 generateHead("section_list");
382
Yi Jin0ed9b682017-08-18 14:51:20 -0700383 // generates SECTION_LIST
Yi Jinbe6de302017-10-24 12:30:24 -0700384 printf("// Generate SECTION_LIST.\n\n");
385
Yi Jinf8601842017-08-15 22:01:41 -0700386 printf("const Section* SECTION_LIST[] = {\n");
Yi Jinf8601842017-08-15 22:01:41 -0700387 for (int i=0; i<descriptor->field_count(); i++) {
388 const FieldDescriptor* field = descriptor->field(i);
389
390 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
391 continue;
392 }
Yi Jinbe6de302017-10-24 12:30:24 -0700393 const SectionFlags s = getSectionFlags(field);
Yi Jinf8601842017-08-15 22:01:41 -0700394 switch (s.type()) {
395 case SECTION_NONE:
396 continue;
397 case SECTION_FILE:
398 printf(" new FileSection(%d, \"%s\"),\n", field->number(), s.args().c_str());
399 break;
400 case SECTION_COMMAND:
401 printf(" new CommandSection(%d,", field->number());
402 splitAndPrint(s.args());
403 printf(" NULL),\n");
404 break;
405 case SECTION_DUMPSYS:
406 printf(" new DumpsysSection(%d,", field->number());
407 splitAndPrint(s.args());
408 printf(" NULL),\n");
409 break;
410 }
411 }
Yi Jin0ed9b682017-08-18 14:51:20 -0700412 printf(" NULL };\n");
Yi Jinbe6de302017-10-24 12:30:24 -0700413
414 emptyline();
415 printf("// =============================================================================\n");
Yi Jin0ed9b682017-08-18 14:51:20 -0700416 emptyline();
417
Yi Jinbe6de302017-10-24 12:30:24 -0700418 // generates PRIVACY_POLICY_LIST
419 printf("// Generate PRIVACY_POLICY_LIST.\n\n");
Yi Jinbdf58942017-11-14 17:58:19 -0800420 map<string, bool> variableNames;
Yi Jinbe6de302017-10-24 12:30:24 -0700421 set<string> parents;
Yi Jinbdf58942017-11-14 17:58:19 -0800422 vector<const FieldDescriptor*> fieldsInOrder = sortFields(descriptor);
423 bool skip[fieldsInOrder.size()];
424 const Destination incidentDest = getPrivacyFlags(descriptor).dest();
Yi Jinbe6de302017-10-24 12:30:24 -0700425
Yi Jinbdf58942017-11-14 17:58:19 -0800426 for (size_t i=0; i<fieldsInOrder.size(); i++) {
427 const FieldDescriptor* field = fieldsInOrder[i];
Yi Jinbe6de302017-10-24 12:30:24 -0700428 const string fieldName = getFieldName(field);
Yi Jinbdf58942017-11-14 17:58:19 -0800429 const Destination fieldDest = getFieldDest(field);
430 const string fieldMessageName = getMessageName(field->message_type(), fieldDest);
Yi Jinbe6de302017-10-24 12:30:24 -0700431
432 skip[i] = true;
433
434 if (field->type() != FieldDescriptor::TYPE_MESSAGE) {
435 continue;
436 }
Yi Jinbdf58942017-11-14 17:58:19 -0800437 // generate privacy flags for each section.
438 if (generatePrivacyFlags(field->message_type(), fieldDest, variableNames, &parents)) {
439 printPrivacy(fieldName, field, fieldMessageName, fieldDest, "NULL");
440 } else if (isDefaultField(field, incidentDest)) {
Yi Jinbe6de302017-10-24 12:30:24 -0700441 continue; // don't create a new privacy if the value is default.
442 } else {
Yi Jinbdf58942017-11-14 17:58:19 -0800443 printPrivacy(fieldName, field, "NULL", fieldDest, "NULL");
Yi Jinbe6de302017-10-24 12:30:24 -0700444 }
445 skip[i] = false;
Yi Jin0ed9b682017-08-18 14:51:20 -0700446 }
447
Yi Jinbe6de302017-10-24 12:30:24 -0700448 // generate final PRIVACY_POLICY_LIST
449 emptyline();
450 int policyCount = 0;
451 if (gSelfRecursionAssignments.empty()) {
452 printf("Privacy* privacyArray[] = {\n");
Yi Jinbdf58942017-11-14 17:58:19 -0800453 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jinbe6de302017-10-24 12:30:24 -0700454 if (skip[i]) continue;
Yi Jinbdf58942017-11-14 17:58:19 -0800455 printf(" &%s,\n", getFieldName(fieldsInOrder[i]).c_str());
Yi Jinbe6de302017-10-24 12:30:24 -0700456 policyCount++;
457 }
458 printf("};\n\n");
459 printf("const Privacy** PRIVACY_POLICY_LIST = const_cast<const Privacy**>(privacyArray);\n\n");
460 printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount);
461 } else {
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]) policyCount++;
464 }
465
466 printf("static const Privacy** createList() {\n");
467 for (size_t i=0; i<gSelfRecursionAssignments.size(); ++i) {
468 printf(" %s;\n", gSelfRecursionAssignments[i].c_str());
469 }
470 printf(" Privacy** privacyArray = (Privacy**)malloc(%d * sizeof(Privacy**));\n", policyCount);
471 policyCount = 0; // reset
Yi Jinbdf58942017-11-14 17:58:19 -0800472 for (size_t i=0; i<fieldsInOrder.size(); i++) {
Yi Jinbe6de302017-10-24 12:30:24 -0700473 if (skip[i]) continue;
Yi Jinbdf58942017-11-14 17:58:19 -0800474 printf(" privacyArray[%d] = &%s;\n", policyCount++, getFieldName(fieldsInOrder[i]).c_str());
Yi Jinbe6de302017-10-24 12:30:24 -0700475 }
476 printf(" return const_cast<const Privacy**>(privacyArray);\n");
477 printf("}\n\n");
478 printf("const Privacy** PRIVACY_POLICY_LIST = createList();\n\n");
479 printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount);
480 }
Yi Jinf8601842017-08-15 22:01:41 -0700481 return true;
482}
483
484// ================================================================================
485int main(int argc, char const *argv[])
486{
487 if (argc != 2) return 1;
488 const char* module = argv[1];
489
Yi Jin0ed9b682017-08-18 14:51:20 -0700490 Descriptor const* descriptor = IncidentProto::descriptor();
491
Yi Jinf8601842017-08-15 22:01:41 -0700492 if (strcmp(module, "incident") == 0) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700493 return !generateIncidentSectionsCpp(descriptor);
Yi Jinf8601842017-08-15 22:01:41 -0700494 }
495 if (strcmp(module, "incidentd") == 0 ) {
Yi Jin0ed9b682017-08-18 14:51:20 -0700496 return !generateSectionListCpp(descriptor);
Yi Jinf8601842017-08-15 22:01:41 -0700497 }
498
499 // return failure if not called by the whitelisted modules
500 return 1;
Joe Onorato1754d742016-11-21 17:51:35 -0800501}