Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 1 | /* |
| 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 Onorato | 7669012 | 2016-12-20 08:18:32 -0800 | [diff] [blame] | 18 | #include <frameworks/base/core/proto/android/os/incident.pb.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 19 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 20 | #include <map> |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 21 | #include <set> |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 22 | #include <string> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 23 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 24 | using namespace android; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 25 | using namespace android::os; |
| 26 | using namespace google::protobuf; |
| 27 | using namespace google::protobuf::io; |
| 28 | using namespace google::protobuf::internal; |
| 29 | using namespace std; |
| 30 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 31 | /** |
| 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 | * ... |
| 62 | * 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, DEFAULT, NULL }; // reserved for WindowState_LIST |
| 64 | * Privacy* WindowState_MSG_[] = { |
| 65 | * &WindowState_state, |
| 66 | * // display id is default, nothing is generated. |
| 67 | * &WindowState_child_windows, |
| 68 | * NULL // terminator of the array |
| 69 | * }; |
| 70 | * Privacy WindowState_my_window { 1, 11, WindowState_my_window_LIST, DEFAULT, NULL }; |
| 71 | * |
| 72 | * createList() { |
| 73 | * ... |
| 74 | * WindowState_child_windows.children = WindowState_my_window_LIST; // point to its own definition after the list is defined. |
| 75 | * ... |
| 76 | * } |
| 77 | * |
| 78 | * const Privacy** PRIVACY_POLICY_LIST = createList(); |
| 79 | * const int PRIVACY_POLICY_COUNT = 1; |
| 80 | */ |
| 81 | |
| 82 | // The assignments will be called when constructs PRIVACY_POLICY_LIST, has to be global variable |
| 83 | vector<string> gSelfRecursionAssignments; |
| 84 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 85 | static inline void emptyline() { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 86 | printf("\n"); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 87 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 88 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 89 | static void generateHead(const char* header) { |
| 90 | printf("// Auto generated file. Do not modify\n"); |
| 91 | emptyline(); |
| 92 | printf("#include \"%s.h\"\n", header); |
| 93 | emptyline(); |
| 94 | } |
| 95 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 96 | // ======================== incident_sections ============================= |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 97 | static bool generateIncidentSectionsCpp(Descriptor const* descriptor) |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 98 | { |
| 99 | generateHead("incident_sections"); |
| 100 | |
| 101 | map<string,FieldDescriptor const*> sections; |
| 102 | int N; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 103 | N = descriptor->field_count(); |
| 104 | for (int i=0; i<N; i++) { |
| 105 | const FieldDescriptor* field = descriptor->field(i); |
| 106 | if (field->type() == FieldDescriptor::TYPE_MESSAGE) { |
| 107 | sections[field->name()] = field; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | printf("IncidentSection const INCIDENT_SECTIONS[] = {\n"); |
| 112 | N = sections.size(); |
| 113 | int i = 0; |
| 114 | for (map<string,FieldDescriptor const*>::const_iterator it = sections.begin(); |
| 115 | it != sections.end(); it++, i++) { |
| 116 | const FieldDescriptor* field = it->second; |
| 117 | printf(" { %d, \"%s\" }", field->number(), field->name().c_str()); |
| 118 | if (i != N-1) { |
| 119 | printf(",\n"); |
| 120 | } else { |
| 121 | printf("\n"); |
| 122 | } |
| 123 | } |
| 124 | printf("};\n"); |
| 125 | |
| 126 | printf("const int INCIDENT_SECTION_COUNT = %d;\n", N); |
| 127 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 128 | return true; |
| 129 | } |
| 130 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 131 | // ========================= section_list =================================== |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 132 | static void splitAndPrint(const string& args) { |
| 133 | size_t base = 0; |
| 134 | size_t found; |
| 135 | while (true) { |
| 136 | found = args.find_first_of(" ", base); |
| 137 | if (found != base) { |
| 138 | string arg = args.substr(base, found - base); |
| 139 | printf(" \"%s\",", arg.c_str()); |
| 140 | } |
| 141 | if (found == args.npos) break; |
| 142 | base = found + 1; |
| 143 | } |
| 144 | } |
| 145 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 146 | static string replaceAll(const string& fieldName, const char oldC, const string& newS) { |
| 147 | if (fieldName.find_first_of(oldC) == fieldName.npos) return fieldName.c_str(); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 148 | size_t pos = 0, idx = 0; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 149 | char* res = new char[fieldName.size() * newS.size() + 1]; // assign a larger buffer |
| 150 | while (pos != fieldName.size()) { |
| 151 | char cur = fieldName[pos++]; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 152 | if (cur != oldC) { |
| 153 | res[idx++] = cur; |
| 154 | continue; |
| 155 | } |
| 156 | |
| 157 | for (size_t i=0; i<newS.size(); i++) { |
| 158 | res[idx++] = newS[i]; |
| 159 | } |
| 160 | } |
| 161 | res[idx] = '\0'; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 162 | string result(res); |
Yunlian Jiang | 3809bbf | 2017-09-05 15:50:58 -0700 | [diff] [blame] | 163 | delete [] res; |
| 164 | return result; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 167 | static string getFieldName(const FieldDescriptor* field) { |
| 168 | return replaceAll(field->full_name(), '.', "__"); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 171 | static string getMessageTypeName(const Descriptor* descriptor) { |
| 172 | return replaceAll(descriptor->full_name(), '.', "_") + "_MSG_"; |
| 173 | } |
| 174 | |
| 175 | static inline SectionFlags getSectionFlags(const FieldDescriptor* field) { |
| 176 | return field->options().GetExtension(section); |
| 177 | } |
| 178 | |
| 179 | static inline PrivacyFlags getPrivacyFlags(const FieldDescriptor* field) { |
| 180 | return field->options().GetExtension(privacy); |
| 181 | } |
| 182 | |
| 183 | static inline bool isDefaultField(const FieldDescriptor* field) { |
| 184 | return getPrivacyFlags(field).dest() == PrivacyFlags::default_instance().dest(); |
| 185 | } |
| 186 | |
| 187 | static bool isDefaultMessageImpl(const Descriptor* descriptor, set<string>* parents) { |
| 188 | int N = descriptor->field_count(); |
| 189 | parents->insert(descriptor->full_name()); |
| 190 | for (int i=0; i<N; ++i) { |
| 191 | const FieldDescriptor* field = descriptor->field(i); |
| 192 | // look at if the current field is default or not, return false immediately |
| 193 | if (!isDefaultField(field)) return false; |
| 194 | |
| 195 | switch (field->type()) { |
| 196 | case FieldDescriptor::TYPE_MESSAGE: |
| 197 | // if self recursion, don't go deep. |
| 198 | if (parents->find(field->message_type()->full_name()) != parents->end()) break; |
| 199 | // if is a default message, just continue |
| 200 | if (isDefaultMessageImpl(field->message_type(), parents)) break; |
| 201 | // sub message is not default, so this message is always not default |
| 202 | return false; |
| 203 | case FieldDescriptor::TYPE_STRING: |
| 204 | if (getPrivacyFlags(field).patterns_size() != 0) return false; |
| 205 | default: |
| 206 | continue; |
| 207 | } |
| 208 | } |
| 209 | parents->erase(descriptor->full_name()); |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | static bool isDefaultMessage(const Descriptor* descriptor) { |
| 214 | set<string> parents; |
| 215 | return isDefaultMessageImpl(descriptor, &parents); |
| 216 | } |
| 217 | |
| 218 | // This function is called for looking at privacy tags for a message type and recursively its sub-messages |
| 219 | // It prints out each fields's privacy tags and a List of Privacy of the message itself (don't print default values) |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame] | 220 | // Returns false if the descriptor doesn't have any non default privacy flags set, including its submessages |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 221 | static bool generatePrivacyFlags(const Descriptor* descriptor, map<string, bool> &msgNames, set<string>* parents) { |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 222 | bool hasDefaultFlags[descriptor->field_count()]; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 223 | |
| 224 | string messageTypeName = getMessageTypeName(descriptor); |
| 225 | // if the message is already defined, skip it. |
| 226 | if (msgNames.find(messageTypeName) != msgNames.end()) { |
| 227 | bool hasDefault = msgNames[messageTypeName]; |
| 228 | return !hasDefault; // don't generate if it has default privacy. |
| 229 | } |
| 230 | // insert the message type name so sub-message will figure out if self-recursion occurs |
| 231 | parents->insert(messageTypeName); |
| 232 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 233 | // iterate though its field and generate sub flags first |
| 234 | for (int i=0; i<descriptor->field_count(); i++) { |
| 235 | hasDefaultFlags[i] = true; // set default to true |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 236 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 237 | const FieldDescriptor* field = descriptor->field(i); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 238 | const string fieldName = getFieldName(field); |
| 239 | // check if the same field name is already defined. |
| 240 | if (msgNames.find(fieldName) != msgNames.end()) { |
| 241 | hasDefaultFlags[i] = msgNames[fieldName]; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 242 | continue; |
| 243 | }; |
| 244 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 245 | PrivacyFlags p = getPrivacyFlags(field); |
| 246 | string fieldMessageName; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 247 | switch (field->type()) { |
| 248 | case FieldDescriptor::TYPE_MESSAGE: |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 249 | fieldMessageName = getMessageTypeName(field->message_type()); |
| 250 | if (parents->find(fieldMessageName) != parents->end()) { // Self-Recursion proto definition |
| 251 | if (isDefaultField(field)) { |
| 252 | hasDefaultFlags[i] = isDefaultMessage(field->message_type()); |
| 253 | } else { |
| 254 | hasDefaultFlags[i] = false; |
| 255 | } |
| 256 | if (!hasDefaultFlags[i]) { |
| 257 | printf("Privacy %s = { %d, %d, NULL, %d, NULL }; // self recursion field of %s\n", |
| 258 | fieldName.c_str(), field->number(), field->type(), p.dest(), fieldMessageName.c_str()); |
| 259 | // generate the assignment and used to construct createList function later on. |
| 260 | gSelfRecursionAssignments.push_back(fieldName + ".children = " + fieldMessageName); |
| 261 | } |
| 262 | break; |
| 263 | } else if (generatePrivacyFlags(field->message_type(), msgNames, parents)) { |
| 264 | printf("Privacy %s = { %d, %d, %s, %d, NULL };\n", fieldName.c_str(), field->number(), |
| 265 | field->type(), fieldMessageName.c_str(), p.dest()); |
| 266 | } else if (isDefaultField(field)) { |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame] | 267 | // don't create a new privacy if the value is default. |
| 268 | break; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 269 | } else { |
| 270 | printf("Privacy %s = { %d, %d, NULL, %d, NULL };\n", fieldName.c_str(), field->number(), |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame] | 271 | field->type(), p.dest()); |
| 272 | } |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 273 | hasDefaultFlags[i] = false; |
| 274 | break; |
| 275 | case FieldDescriptor::TYPE_STRING: |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 276 | if (isDefaultField(field) && p.patterns_size() == 0) break; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 277 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 278 | printf("const char* %s_patterns[] = {\n", fieldName.c_str()); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 279 | for (int i=0; i<p.patterns_size(); i++) { |
| 280 | // the generated string need to escape backslash as well, need to dup it here |
Yunlian Jiang | 3809bbf | 2017-09-05 15:50:58 -0700 | [diff] [blame] | 281 | printf(" \"%s\",\n", replaceAll(p.patterns(i), '\\', "\\\\").c_str()); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 282 | } |
| 283 | printf(" NULL };\n"); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 284 | printf("Privacy %s = { %d, %d, NULL, %d, %s_patterns };\n", fieldName.c_str(), field->number(), |
| 285 | field->type(), p.dest(), fieldName.c_str()); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 286 | hasDefaultFlags[i] = false; |
| 287 | break; |
| 288 | default: |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 289 | if (isDefaultField(field)) break; |
| 290 | printf("Privacy %s = { %d, %d, NULL, %d, NULL };\n", fieldName.c_str(), field->number(), |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame] | 291 | field->type(), p.dest()); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 292 | hasDefaultFlags[i] = false; |
| 293 | } |
| 294 | // add the field name to message map, true means it has default flags |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 295 | msgNames[fieldName] = hasDefaultFlags[i]; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | bool allDefaults = true; |
| 299 | for (int i=0; i<descriptor->field_count(); i++) { |
| 300 | allDefaults &= hasDefaultFlags[i]; |
| 301 | } |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 302 | |
| 303 | parents->erase(messageTypeName); // erase the message type name when exit the message. |
| 304 | msgNames[messageTypeName] = allDefaults; // store the privacy tags of the message here to avoid overhead. |
| 305 | |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame] | 306 | if (allDefaults) return false; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 307 | |
| 308 | emptyline(); |
Yi Jin | 7e0b4e5 | 2017-09-12 20:00:25 -0700 | [diff] [blame] | 309 | int policyCount = 0; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 310 | printf("Privacy* %s[] = {\n", messageTypeName.c_str()); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 311 | for (int i=0; i<descriptor->field_count(); i++) { |
| 312 | const FieldDescriptor* field = descriptor->field(i); |
| 313 | if (hasDefaultFlags[i]) continue; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 314 | printf(" &%s,\n", getFieldName(field).c_str()); |
Yi Jin | 7e0b4e5 | 2017-09-12 20:00:25 -0700 | [diff] [blame] | 315 | policyCount++; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 316 | } |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 317 | printf(" NULL };\n"); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 318 | emptyline(); |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame] | 319 | return true; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static bool generateSectionListCpp(Descriptor const* descriptor) { |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 323 | generateHead("section_list"); |
| 324 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 325 | // generates SECTION_LIST |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 326 | printf("// Generate SECTION_LIST.\n\n"); |
| 327 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 328 | printf("const Section* SECTION_LIST[] = {\n"); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 329 | for (int i=0; i<descriptor->field_count(); i++) { |
| 330 | const FieldDescriptor* field = descriptor->field(i); |
| 331 | |
| 332 | if (field->type() != FieldDescriptor::TYPE_MESSAGE) { |
| 333 | continue; |
| 334 | } |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 335 | const SectionFlags s = getSectionFlags(field); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 336 | switch (s.type()) { |
| 337 | case SECTION_NONE: |
| 338 | continue; |
| 339 | case SECTION_FILE: |
| 340 | printf(" new FileSection(%d, \"%s\"),\n", field->number(), s.args().c_str()); |
| 341 | break; |
| 342 | case SECTION_COMMAND: |
| 343 | printf(" new CommandSection(%d,", field->number()); |
| 344 | splitAndPrint(s.args()); |
| 345 | printf(" NULL),\n"); |
| 346 | break; |
| 347 | case SECTION_DUMPSYS: |
| 348 | printf(" new DumpsysSection(%d,", field->number()); |
| 349 | splitAndPrint(s.args()); |
| 350 | printf(" NULL),\n"); |
| 351 | break; |
| 352 | } |
| 353 | } |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 354 | printf(" NULL };\n"); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 355 | |
| 356 | emptyline(); |
| 357 | printf("// =============================================================================\n"); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 358 | emptyline(); |
| 359 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 360 | // generates PRIVACY_POLICY_LIST |
| 361 | printf("// Generate PRIVACY_POLICY_LIST.\n\n"); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 362 | map<string, bool> messageNames; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 363 | set<string> parents; |
| 364 | bool skip[descriptor->field_count()]; |
| 365 | |
| 366 | for (int i=0; i<descriptor->field_count(); i++) { |
| 367 | const FieldDescriptor* field = descriptor->field(i); |
| 368 | const string fieldName = getFieldName(field); |
| 369 | PrivacyFlags p = getPrivacyFlags(field); |
| 370 | |
| 371 | skip[i] = true; |
| 372 | |
| 373 | if (field->type() != FieldDescriptor::TYPE_MESSAGE) { |
| 374 | continue; |
| 375 | } |
| 376 | // generate privacy flags for each field. |
| 377 | if (generatePrivacyFlags(field->message_type(), messageNames, &parents)) { |
| 378 | printf("Privacy %s { %d, %d, %s, %d, NULL };\n", fieldName.c_str(), field->number(), |
| 379 | field->type(), getMessageTypeName(field->message_type()).c_str(), p.dest()); |
| 380 | } else if (isDefaultField(field)) { |
| 381 | continue; // don't create a new privacy if the value is default. |
| 382 | } else { |
| 383 | printf("Privacy %s { %d, %d, NULL, %d, NULL };\n", fieldName.c_str(), field->number(), |
| 384 | field->type(), p.dest()); |
| 385 | } |
| 386 | skip[i] = false; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame^] | 389 | // generate final PRIVACY_POLICY_LIST |
| 390 | emptyline(); |
| 391 | int policyCount = 0; |
| 392 | if (gSelfRecursionAssignments.empty()) { |
| 393 | printf("Privacy* privacyArray[] = {\n"); |
| 394 | for (int i=0; i<descriptor->field_count(); i++) { |
| 395 | if (skip[i]) continue; |
| 396 | printf(" &%s,\n", getFieldName(descriptor->field(i)).c_str()); |
| 397 | policyCount++; |
| 398 | } |
| 399 | printf("};\n\n"); |
| 400 | printf("const Privacy** PRIVACY_POLICY_LIST = const_cast<const Privacy**>(privacyArray);\n\n"); |
| 401 | printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount); |
| 402 | } else { |
| 403 | for (int i=0; i<descriptor->field_count(); i++) { |
| 404 | if (!skip[i]) policyCount++; |
| 405 | } |
| 406 | |
| 407 | printf("static const Privacy** createList() {\n"); |
| 408 | for (size_t i=0; i<gSelfRecursionAssignments.size(); ++i) { |
| 409 | printf(" %s;\n", gSelfRecursionAssignments[i].c_str()); |
| 410 | } |
| 411 | printf(" Privacy** privacyArray = (Privacy**)malloc(%d * sizeof(Privacy**));\n", policyCount); |
| 412 | policyCount = 0; // reset |
| 413 | for (int i=0; i<descriptor->field_count(); i++) { |
| 414 | if (skip[i]) continue; |
| 415 | printf(" privacyArray[%d] = &%s;\n", policyCount++, getFieldName(descriptor->field(i)).c_str()); |
| 416 | } |
| 417 | printf(" return const_cast<const Privacy**>(privacyArray);\n"); |
| 418 | printf("}\n\n"); |
| 419 | printf("const Privacy** PRIVACY_POLICY_LIST = createList();\n\n"); |
| 420 | printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount); |
| 421 | } |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 422 | return true; |
| 423 | } |
| 424 | |
| 425 | // ================================================================================ |
| 426 | int main(int argc, char const *argv[]) |
| 427 | { |
| 428 | if (argc != 2) return 1; |
| 429 | const char* module = argv[1]; |
| 430 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 431 | Descriptor const* descriptor = IncidentProto::descriptor(); |
| 432 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 433 | if (strcmp(module, "incident") == 0) { |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 434 | return !generateIncidentSectionsCpp(descriptor); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 435 | } |
| 436 | if (strcmp(module, "incidentd") == 0 ) { |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 437 | return !generateSectionListCpp(descriptor); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | // return failure if not called by the whitelisted modules |
| 441 | return 1; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 442 | } |