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 | |
Joe Onorato | 7669012 | 2016-12-20 08:18:32 -0800 | [diff] [blame] | 17 | #include <frameworks/base/core/proto/android/os/incident.pb.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 18 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 19 | #include <map> |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 20 | #include <set> |
Yi Jin | 0f2599f | 2017-11-16 18:19:45 -0800 | [diff] [blame] | 21 | #include <sstream> |
Kweku Adams | 71a9531 | 2018-04-16 16:54:24 -0700 | [diff] [blame] | 22 | #include <string> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 23 | |
Bookatz | da9b8d0 | 2018-11-14 13:14:45 -0800 | [diff] [blame] | 24 | #ifndef FALLTHROUGH_INTENDED |
| 25 | #define FALLTHROUGH_INTENDED [[fallthrough]] |
| 26 | #endif |
| 27 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 28 | using namespace android; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 29 | using namespace android::os; |
| 30 | using namespace google::protobuf; |
| 31 | using namespace google::protobuf::io; |
| 32 | using namespace google::protobuf::internal; |
| 33 | using namespace std; |
| 34 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Implementation details: |
| 37 | * This binary auto generates .cpp files for incident and incidentd. |
| 38 | * |
| 39 | * When argument "incident" is specified, it generates incident_section.cpp file. |
| 40 | * |
| 41 | * When argument "incidentd" is specified, it generates section_list.cpp file. |
| 42 | * |
| 43 | * In section_list.cpp file, it generates a SECTION_LIST array and a PRIVACY_POLICY_LIST array. |
| 44 | * For SECTION_LIST, it generates Section.h classes only for proto fields with section option enabled. |
| 45 | * For PRIVACY_POLICY_LIST, it generates Privacy.h classes only for proto fields with privacy option enabled. |
| 46 | * |
| 47 | * For Privacy struct, it is possible to have self recursion definitions since protobuf is defining "classes" |
| 48 | * So the logic to handle it becomes very complicated when Privacy tag of a message contains a list of Privacies |
| 49 | * of its sub-messages. The code also handles multiple depth of self recursion fields. |
| 50 | * |
| 51 | * For example here is a one level self recursion message WindowManager: |
| 52 | * message WindowState { |
| 53 | * string state = 1 [(privacy).dest = LOCAL]; |
| 54 | * int32 display_id = 2; |
| 55 | * repeated WindowState child_windows = 3; |
| 56 | * } |
| 57 | * |
| 58 | * message WindowManager { |
| 59 | * WindowState my_window = 1; |
| 60 | * } |
| 61 | * |
| 62 | * When generating Privacy options for WindowManager, this tool will generate cpp syntax source code: |
| 63 | * |
| 64 | * #include "section_list.h" |
| 65 | * ... |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 66 | * Privacy WindowState__state { 1, 9, NULL, LOCAL, NULL }; // first two integers are values for field id and proto type. |
| 67 | * Privacy WindowState__child_windows { 3, 11, NULL, UNSET, NULL }; // reserved for WindowState_LIST |
| 68 | * Privacy* WindowState__MSG__UNSET[] = { |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 69 | * &WindowState_state, |
| 70 | * // display id is default, nothing is generated. |
| 71 | * &WindowState_child_windows, |
| 72 | * NULL // terminator of the array |
| 73 | * }; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 74 | * Privacy WindowState__my_window { 1, 11, WindowState__MSG__UNSET, UNSET, NULL }; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 75 | * |
| 76 | * createList() { |
| 77 | * ... |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 78 | * WindowState_child_windows.children = WindowState__MSG_UNSET; // point to its own definition after the list is defined. |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 79 | * ... |
| 80 | * } |
| 81 | * |
| 82 | * const Privacy** PRIVACY_POLICY_LIST = createList(); |
| 83 | * const int PRIVACY_POLICY_COUNT = 1; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 84 | * |
| 85 | * Privacy Value Inheritance rules: |
| 86 | * 1. Both field and message can be tagged with DESTINATION: LOCAL(L), EXPLICIT(E), AUTOMATIC(A). |
| 87 | * 2. Primitives inherits containing message's tag unless defined explicitly. |
| 88 | * 3. Containing message's tag doesn't apply to message fields, even when unset (in this case, uses its default message tag). |
| 89 | * 4. Message field tag overrides its default message tag. |
| 90 | * 5. UNSET tag defaults to EXPLICIT. |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 91 | */ |
| 92 | |
| 93 | // The assignments will be called when constructs PRIVACY_POLICY_LIST, has to be global variable |
| 94 | vector<string> gSelfRecursionAssignments; |
| 95 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 96 | static inline void emptyline() { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 97 | printf("\n"); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 98 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 99 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 100 | static void generateHead(const char* header) { |
| 101 | printf("// Auto generated file. Do not modify\n"); |
| 102 | emptyline(); |
| 103 | printf("#include \"%s.h\"\n", header); |
| 104 | emptyline(); |
| 105 | } |
| 106 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 107 | // ======================== incident_sections ============================= |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 108 | static bool generateIncidentSectionsCpp(Descriptor const* descriptor) |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 109 | { |
| 110 | generateHead("incident_sections"); |
| 111 | |
| 112 | map<string,FieldDescriptor const*> sections; |
| 113 | int N; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 114 | N = descriptor->field_count(); |
| 115 | for (int i=0; i<N; i++) { |
| 116 | const FieldDescriptor* field = descriptor->field(i); |
Yi Jin | 908c02f | 2018-06-22 16:51:40 -0700 | [diff] [blame] | 117 | sections[field->name()] = field; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | printf("IncidentSection const INCIDENT_SECTIONS[] = {\n"); |
| 121 | N = sections.size(); |
| 122 | int i = 0; |
| 123 | for (map<string,FieldDescriptor const*>::const_iterator it = sections.begin(); |
| 124 | it != sections.end(); it++, i++) { |
| 125 | const FieldDescriptor* field = it->second; |
| 126 | printf(" { %d, \"%s\" }", field->number(), field->name().c_str()); |
| 127 | if (i != N-1) { |
| 128 | printf(",\n"); |
| 129 | } else { |
| 130 | printf("\n"); |
| 131 | } |
| 132 | } |
| 133 | printf("};\n"); |
| 134 | |
| 135 | printf("const int INCIDENT_SECTION_COUNT = %d;\n", N); |
| 136 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 137 | return true; |
| 138 | } |
| 139 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 140 | // ========================= section_list =================================== |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 141 | static void splitAndPrint(const string& args) { |
| 142 | size_t base = 0; |
| 143 | size_t found; |
| 144 | while (true) { |
Chih-Hung Hsieh | 6b3fac2 | 2018-09-17 15:12:02 -0700 | [diff] [blame] | 145 | found = args.find_first_of(' ', base); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 146 | if (found != base) { |
| 147 | string arg = args.substr(base, found - base); |
| 148 | printf(" \"%s\",", arg.c_str()); |
| 149 | } |
| 150 | if (found == args.npos) break; |
| 151 | base = found + 1; |
| 152 | } |
| 153 | } |
| 154 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 155 | static string replaceAll(const string& fieldName, const char oldC, const string& newS) { |
| 156 | if (fieldName.find_first_of(oldC) == fieldName.npos) return fieldName.c_str(); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 157 | size_t pos = 0, idx = 0; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 158 | char* res = new char[fieldName.size() * newS.size() + 1]; // assign a larger buffer |
| 159 | while (pos != fieldName.size()) { |
| 160 | char cur = fieldName[pos++]; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 161 | if (cur != oldC) { |
| 162 | res[idx++] = cur; |
| 163 | continue; |
| 164 | } |
| 165 | |
| 166 | for (size_t i=0; i<newS.size(); i++) { |
| 167 | res[idx++] = newS[i]; |
| 168 | } |
| 169 | } |
| 170 | res[idx] = '\0'; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 171 | string result(res); |
Yunlian Jiang | 3809bbf | 2017-09-05 15:50:58 -0700 | [diff] [blame] | 172 | delete [] res; |
| 173 | return result; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 176 | static inline void printPrivacy(const string& name, const FieldDescriptor* field, const string& children, |
| 177 | const Destination dest, const string& patterns, const string& comments = "") { |
| 178 | printf("Privacy %s = { %d, %d, %s, %d, %s };%s\n", name.c_str(), field->number(), field->type(), |
| 179 | children.c_str(), dest, patterns.c_str(), comments.c_str()); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 182 | // Get Custom Options ================================================================================ |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 183 | static inline SectionFlags getSectionFlags(const FieldDescriptor* field) { |
| 184 | return field->options().GetExtension(section); |
| 185 | } |
| 186 | |
| 187 | static inline PrivacyFlags getPrivacyFlags(const FieldDescriptor* field) { |
| 188 | return field->options().GetExtension(privacy); |
| 189 | } |
| 190 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 191 | static inline PrivacyFlags getPrivacyFlags(const Descriptor* descriptor) { |
| 192 | return descriptor->options().GetExtension(msg_privacy); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 195 | // Get Destinations =================================================================================== |
| 196 | static inline Destination getMessageDest(const Descriptor* descriptor, const Destination overridden) { |
| 197 | return overridden != DEST_UNSET ? overridden : getPrivacyFlags(descriptor).dest(); |
| 198 | } |
| 199 | |
| 200 | // Returns field's own dest, when it is a message field, uses its message default tag if unset. |
| 201 | static inline Destination getFieldDest(const FieldDescriptor* field) { |
| 202 | Destination fieldDest = getPrivacyFlags(field).dest(); |
| 203 | return field->type() != FieldDescriptor::TYPE_MESSAGE ? fieldDest : |
| 204 | getMessageDest(field->message_type(), fieldDest); |
| 205 | } |
| 206 | |
Kweku Adams | ecf4bdb | 2018-02-22 15:05:48 -0800 | [diff] [blame] | 207 | // Converts Destination to a string. |
| 208 | static inline string getDestString(const Destination dest) { |
| 209 | switch (dest) { |
| 210 | case DEST_AUTOMATIC: return "AUTOMATIC"; |
| 211 | case DEST_LOCAL: return "LOCAL"; |
| 212 | case DEST_EXPLICIT: return "EXPLICIT"; |
| 213 | // UNSET is considered EXPLICIT by default. |
| 214 | case DEST_UNSET: return "EXPLICIT"; |
| 215 | default: return "UNKNOWN"; |
| 216 | } |
| 217 | } |
| 218 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 219 | // Get Names =========================================================================================== |
| 220 | static inline string getFieldName(const FieldDescriptor* field) { |
| 221 | // replace . with double underscores to avoid name conflicts since fields use snake naming convention |
| 222 | return replaceAll(field->full_name(), '.', "__"); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | static inline string getMessageName(const Descriptor* descriptor, const Destination overridden) { |
| 227 | // replace . with one underscore since messages use camel naming convention |
| 228 | return replaceAll(descriptor->full_name(), '.', "_") + "__MSG__" + |
| 229 | to_string(getMessageDest(descriptor, overridden)); |
| 230 | } |
| 231 | |
| 232 | // IsDefault ============================================================================================ |
| 233 | // Returns true if a field is default. Default is defined as this field has same dest as its containing message. |
Kweku Adams | ecf4bdb | 2018-02-22 15:05:48 -0800 | [diff] [blame] | 234 | // For message fields, it only looks at its field tag and own default message tag, doesn't recursively go deeper. |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 235 | static inline bool isDefaultField(const FieldDescriptor* field, const Destination containerDest) { |
| 236 | Destination fieldDest = getFieldDest(field); |
| 237 | if (field->type() != FieldDescriptor::TYPE_MESSAGE) { |
| 238 | return fieldDest == containerDest || (fieldDest == DEST_UNSET); |
| 239 | } else { |
| 240 | return fieldDest == containerDest || |
| 241 | (containerDest == DEST_UNSET && fieldDest == DEST_EXPLICIT) || |
| 242 | (containerDest == DEST_EXPLICIT && fieldDest == DEST_UNSET); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | static bool isDefaultMessageImpl(const Descriptor* descriptor, const Destination dest, set<string>* parents) { |
| 247 | const int N = descriptor->field_count(); |
| 248 | const Destination messageDest = getMessageDest(descriptor, dest); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 249 | parents->insert(descriptor->full_name()); |
| 250 | for (int i=0; i<N; ++i) { |
| 251 | const FieldDescriptor* field = descriptor->field(i); |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 252 | const Destination fieldDest = getFieldDest(field); |
| 253 | // If current field is not default, return false immediately |
| 254 | if (!isDefaultField(field, messageDest)) return false; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 255 | switch (field->type()) { |
| 256 | case FieldDescriptor::TYPE_MESSAGE: |
| 257 | // if self recursion, don't go deep. |
| 258 | if (parents->find(field->message_type()->full_name()) != parents->end()) break; |
| 259 | // if is a default message, just continue |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 260 | if (isDefaultMessageImpl(field->message_type(), fieldDest, parents)) break; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 261 | // sub message is not default, so this message is always not default |
| 262 | return false; |
| 263 | case FieldDescriptor::TYPE_STRING: |
| 264 | if (getPrivacyFlags(field).patterns_size() != 0) return false; |
Robin Lee | 41773b7 | 2018-11-29 14:38:30 +0100 | [diff] [blame] | 265 | break; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 266 | default: |
Robin Lee | 41773b7 | 2018-11-29 14:38:30 +0100 | [diff] [blame] | 267 | break; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | parents->erase(descriptor->full_name()); |
| 271 | return true; |
| 272 | } |
| 273 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 274 | // Recursively look at if this message is default, meaning all its fields and sub-messages |
| 275 | // can be described by the same dest. |
| 276 | static bool isDefaultMessage(const Descriptor* descriptor, const Destination dest) { |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 277 | set<string> parents; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 278 | return isDefaultMessageImpl(descriptor, dest, &parents); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 281 | // =============================================================================================================== |
| 282 | static bool numberInOrder(const FieldDescriptor* f1, const FieldDescriptor* f2) { |
| 283 | return f1->number() < f2->number(); |
| 284 | } |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 285 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 286 | // field numbers are possibly out of order, sort them here. |
| 287 | static vector<const FieldDescriptor*> sortFields(const Descriptor* descriptor) { |
| 288 | vector<const FieldDescriptor*> fields; |
| 289 | fields.reserve(descriptor->field_count()); |
| 290 | for (int i=0; i<descriptor->field_count(); i++) { |
| 291 | fields.push_back(descriptor->field(i)); |
| 292 | } |
| 293 | std::sort(fields.begin(), fields.end(), numberInOrder); |
| 294 | return fields; |
| 295 | } |
| 296 | |
| 297 | // This function looks for privacy tags of a message type and recursively its sub-messages. |
| 298 | // It generates Privacy objects for each non-default fields including non-default sub-messages. |
| 299 | // And if the message has Privacy objects generated, it returns a list of them. |
| 300 | // Returns false if the descriptor doesn't have any non default privacy flags set, including its submessages |
| 301 | static bool generatePrivacyFlags(const Descriptor* descriptor, const Destination overridden, |
| 302 | map<string, bool> &variableNames, set<string>* parents) { |
| 303 | const string messageName = getMessageName(descriptor, overridden); |
| 304 | const Destination messageDest = getMessageDest(descriptor, overridden); |
| 305 | |
| 306 | if (variableNames.find(messageName) != variableNames.end()) { |
| 307 | bool hasDefault = variableNames[messageName]; |
| 308 | return !hasDefault; // if has default, then don't generate privacy flags. |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 309 | } |
| 310 | // insert the message type name so sub-message will figure out if self-recursion occurs |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 311 | parents->insert(messageName); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 312 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 313 | // sort fields based on number, iterate though them and generate sub flags first |
| 314 | vector<const FieldDescriptor*> fieldsInOrder = sortFields(descriptor); |
| 315 | bool hasDefaultFlags[fieldsInOrder.size()]; |
| 316 | for (size_t i=0; i<fieldsInOrder.size(); i++) { |
| 317 | const FieldDescriptor* field = fieldsInOrder[i]; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 318 | const string fieldName = getFieldName(field); |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 319 | const Destination fieldDest = getFieldDest(field); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 320 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 321 | if (variableNames.find(fieldName) != variableNames.end()) { |
| 322 | hasDefaultFlags[i] = variableNames[fieldName]; |
| 323 | continue; |
| 324 | } |
| 325 | hasDefaultFlags[i] = isDefaultField(field, messageDest); |
| 326 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 327 | string fieldMessageName; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 328 | PrivacyFlags p = getPrivacyFlags(field); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 329 | switch (field->type()) { |
| 330 | case FieldDescriptor::TYPE_MESSAGE: |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 331 | fieldMessageName = getMessageName(field->message_type(), fieldDest); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 332 | if (parents->find(fieldMessageName) != parents->end()) { // Self-Recursion proto definition |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 333 | if (hasDefaultFlags[i]) { |
| 334 | hasDefaultFlags[i] = isDefaultMessage(field->message_type(), fieldDest); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 335 | } |
| 336 | if (!hasDefaultFlags[i]) { |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 337 | printPrivacy(fieldName, field, "NULL", fieldDest, "NULL", |
| 338 | " // self recursion field of " + fieldMessageName); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 339 | // generate the assignment and used to construct createList function later on. |
| 340 | gSelfRecursionAssignments.push_back(fieldName + ".children = " + fieldMessageName); |
| 341 | } |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 342 | } else if (generatePrivacyFlags(field->message_type(), p.dest(), variableNames, parents)) { |
| 343 | if (variableNames.find(fieldName) == variableNames.end()) { |
| 344 | printPrivacy(fieldName, field, fieldMessageName, fieldDest, "NULL"); |
| 345 | } |
| 346 | hasDefaultFlags[i] = false; |
| 347 | } else if (!hasDefaultFlags[i]) { |
| 348 | printPrivacy(fieldName, field, "NULL", fieldDest, "NULL"); |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame] | 349 | } |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 350 | break; |
| 351 | case FieldDescriptor::TYPE_STRING: |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 352 | if (p.patterns_size() != 0) { // if patterns are specified |
| 353 | if (hasDefaultFlags[i]) break; |
| 354 | printf("const char* %s_patterns[] = {\n", fieldName.c_str()); |
| 355 | for (int j=0; j<p.patterns_size(); j++) { |
| 356 | // generated string needs to escape backslash too, duplicate it to allow escape again. |
| 357 | printf(" \"%s\",\n", replaceAll(p.patterns(j), '\\', "\\\\").c_str()); |
| 358 | } |
| 359 | printf(" NULL };\n"); |
| 360 | printPrivacy(fieldName, field, "NULL", fieldDest, fieldName + "_patterns"); |
| 361 | break; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 362 | } |
Bookatz | da9b8d0 | 2018-11-14 13:14:45 -0800 | [diff] [blame] | 363 | FALLTHROUGH_INTENDED; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 364 | // else treat string field as primitive field and goes to default |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 365 | default: |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 366 | if (!hasDefaultFlags[i]) printPrivacy(fieldName, field, "NULL", fieldDest, "NULL"); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 367 | } |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 368 | // Don't generate a variable twice |
| 369 | if (!hasDefaultFlags[i]) variableNames[fieldName] = false; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | bool allDefaults = true; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 373 | for (size_t i=0; i<fieldsInOrder.size(); i++) { |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 374 | allDefaults &= hasDefaultFlags[i]; |
| 375 | } |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 376 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 377 | parents->erase(messageName); // erase the message type name when exit the message. |
| 378 | variableNames[messageName] = allDefaults; // store the privacy tags of the message here to avoid overhead. |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 379 | |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame] | 380 | if (allDefaults) return false; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 381 | |
| 382 | emptyline(); |
Yi Jin | 7e0b4e5 | 2017-09-12 20:00:25 -0700 | [diff] [blame] | 383 | int policyCount = 0; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 384 | printf("Privacy* %s[] = {\n", messageName.c_str()); |
| 385 | for (size_t i=0; i<fieldsInOrder.size(); i++) { |
| 386 | const FieldDescriptor* field = fieldsInOrder[i]; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 387 | if (hasDefaultFlags[i]) continue; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 388 | printf(" &%s,\n", getFieldName(field).c_str()); |
Yi Jin | 7e0b4e5 | 2017-09-12 20:00:25 -0700 | [diff] [blame] | 389 | policyCount++; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 390 | } |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 391 | printf(" NULL };\n"); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 392 | emptyline(); |
Yi Jin | 22769e0 | 2017-10-16 14:42:50 -0700 | [diff] [blame] | 393 | return true; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | static bool generateSectionListCpp(Descriptor const* descriptor) { |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 397 | generateHead("section_list"); |
| 398 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 399 | // generate namespaces |
| 400 | printf("namespace android {\n"); |
| 401 | printf("namespace os {\n"); |
| 402 | printf("namespace incidentd {\n"); |
| 403 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 404 | // generates SECTION_LIST |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 405 | printf("// Generate SECTION_LIST.\n\n"); |
| 406 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 407 | printf("const Section* SECTION_LIST[] = {\n"); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 408 | for (int i=0; i<descriptor->field_count(); i++) { |
| 409 | const FieldDescriptor* field = descriptor->field(i); |
| 410 | |
Ryan Savitski | 09a8479 | 2019-06-03 23:57:09 +0100 | [diff] [blame] | 411 | if (field->type() != FieldDescriptor::TYPE_MESSAGE && |
| 412 | field->type() != FieldDescriptor::TYPE_STRING && |
| 413 | field->type() != FieldDescriptor::TYPE_BYTES) { |
| 414 | continue; |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 415 | } |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 416 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 417 | const SectionFlags s = getSectionFlags(field); |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 418 | if (s.userdebug_and_eng_only() || s.type() == SECTION_TEXT_DUMPSYS) { |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 419 | printf("#if ALLOW_RESTRICTED_SECTIONS\n"); |
| 420 | } |
| 421 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 422 | switch (s.type()) { |
| 423 | case SECTION_NONE: |
| 424 | continue; |
| 425 | case SECTION_FILE: |
Kweku Adams | e04ef77 | 2018-06-13 12:24:38 -0700 | [diff] [blame] | 426 | printf(" new FileSection(%d, \"%s\"),\n", field->number(), s.args().c_str()); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 427 | break; |
| 428 | case SECTION_COMMAND: |
| 429 | printf(" new CommandSection(%d,", field->number()); |
| 430 | splitAndPrint(s.args()); |
| 431 | printf(" NULL),\n"); |
| 432 | break; |
| 433 | case SECTION_DUMPSYS: |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 434 | printf(" new DumpsysSection(%d, ", field->number()); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 435 | splitAndPrint(s.args()); |
| 436 | printf(" NULL),\n"); |
| 437 | break; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 438 | case SECTION_LOG: |
zhouwenjie | c3bf804 | 2019-10-30 14:31:54 -0700 | [diff] [blame] | 439 | printf(" new LogSection(%d, ", field->number()); |
| 440 | splitAndPrint(s.args()); |
| 441 | printf(" NULL),\n"); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 442 | break; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 443 | case SECTION_GZIP: |
| 444 | printf(" new GZipSection(%d,", field->number()); |
| 445 | splitAndPrint(s.args()); |
| 446 | printf(" NULL),\n"); |
Yi Jin | 934cc61 | 2018-03-15 14:14:26 -0700 | [diff] [blame] | 447 | break; |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 448 | case SECTION_TOMBSTONE: |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 449 | printf(" new TombstoneSection(%d, \"%s\"),\n", field->number(), |
| 450 | s.args().c_str()); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 451 | break; |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 452 | case SECTION_TEXT_DUMPSYS: |
| 453 | printf(" new TextDumpsysSection(%d, ", field->number()); |
| 454 | splitAndPrint(s.args()); |
| 455 | printf(" NULL),\n"); |
| 456 | break; |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 457 | } |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 458 | if (s.userdebug_and_eng_only() || s.type() == SECTION_TEXT_DUMPSYS) { |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 459 | printf("#endif\n"); |
| 460 | } |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 461 | } |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 462 | printf(" NULL };\n"); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 463 | |
| 464 | emptyline(); |
| 465 | printf("// =============================================================================\n"); |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 466 | emptyline(); |
| 467 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 468 | // generates PRIVACY_POLICY_LIST |
| 469 | printf("// Generate PRIVACY_POLICY_LIST.\n\n"); |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 470 | map<string, bool> variableNames; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 471 | set<string> parents; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 472 | vector<const FieldDescriptor*> fieldsInOrder = sortFields(descriptor); |
Colin Cross | b9e1955 | 2018-12-19 22:55:15 -0800 | [diff] [blame] | 473 | vector<bool> skip(fieldsInOrder.size()); |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 474 | const Destination incidentDest = getPrivacyFlags(descriptor).dest(); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 475 | |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 476 | for (size_t i=0; i<fieldsInOrder.size(); i++) { |
| 477 | const FieldDescriptor* field = fieldsInOrder[i]; |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 478 | const string fieldName = getFieldName(field); |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 479 | const Destination fieldDest = getFieldDest(field); |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 480 | printf("\n// Incident Report Section: %s (%d)\n", field->name().c_str(), field->number()); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 481 | if (field->type() != FieldDescriptor::TYPE_MESSAGE) { |
Yi Jin | 908c02f | 2018-06-22 16:51:40 -0700 | [diff] [blame] | 482 | printPrivacy(fieldName, field, "NULL", fieldDest, "NULL"); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 483 | continue; |
| 484 | } |
Yi Jin | 908c02f | 2018-06-22 16:51:40 -0700 | [diff] [blame] | 485 | |
| 486 | skip[i] = true; |
| 487 | const string fieldMessageName = getMessageName(field->message_type(), fieldDest); |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 488 | // generate privacy flags for each section. |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 489 | if (generatePrivacyFlags(field->message_type(), incidentDest, variableNames, &parents)) { |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 490 | printPrivacy(fieldName, field, fieldMessageName, fieldDest, "NULL"); |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 491 | } else if (fieldDest == incidentDest) { |
| 492 | printf("// default %s: fieldDest=%d incidentDest=%d\n", fieldName.c_str(), |
| 493 | getFieldDest(field), incidentDest); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 494 | continue; // don't create a new privacy if the value is default. |
| 495 | } else { |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 496 | printPrivacy(fieldName, field, "NULL", fieldDest, "NULL"); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 497 | } |
| 498 | skip[i] = false; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 501 | // generate final PRIVACY_POLICY_LIST |
| 502 | emptyline(); |
| 503 | int policyCount = 0; |
| 504 | if (gSelfRecursionAssignments.empty()) { |
| 505 | printf("Privacy* privacyArray[] = {\n"); |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 506 | for (size_t i=0; i<fieldsInOrder.size(); i++) { |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 507 | if (skip[i]) continue; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 508 | printf(" &%s,\n", getFieldName(fieldsInOrder[i]).c_str()); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 509 | policyCount++; |
| 510 | } |
| 511 | printf("};\n\n"); |
| 512 | printf("const Privacy** PRIVACY_POLICY_LIST = const_cast<const Privacy**>(privacyArray);\n\n"); |
| 513 | printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount); |
| 514 | } else { |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 515 | for (size_t i=0; i<fieldsInOrder.size(); i++) { |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 516 | if (!skip[i]) policyCount++; |
| 517 | } |
| 518 | |
| 519 | printf("static const Privacy** createList() {\n"); |
| 520 | for (size_t i=0; i<gSelfRecursionAssignments.size(); ++i) { |
| 521 | printf(" %s;\n", gSelfRecursionAssignments[i].c_str()); |
| 522 | } |
| 523 | printf(" Privacy** privacyArray = (Privacy**)malloc(%d * sizeof(Privacy**));\n", policyCount); |
| 524 | policyCount = 0; // reset |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 525 | for (size_t i=0; i<fieldsInOrder.size(); i++) { |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 526 | if (skip[i]) continue; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 527 | printf(" privacyArray[%d] = &%s;\n", policyCount++, getFieldName(fieldsInOrder[i]).c_str()); |
Yi Jin | be6de30 | 2017-10-24 12:30:24 -0700 | [diff] [blame] | 528 | } |
| 529 | printf(" return const_cast<const Privacy**>(privacyArray);\n"); |
| 530 | printf("}\n\n"); |
| 531 | printf("const Privacy** PRIVACY_POLICY_LIST = createList();\n\n"); |
| 532 | printf("const int PRIVACY_POLICY_COUNT = %d;\n", policyCount); |
| 533 | } |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 534 | |
| 535 | printf("} // incidentd\n"); |
| 536 | printf("} // os\n"); |
| 537 | printf("} // android\n"); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 538 | return true; |
| 539 | } |
| 540 | |
| 541 | // ================================================================================ |
Yi Jin | 0f2599f | 2017-11-16 18:19:45 -0800 | [diff] [blame] | 542 | static string replace_string(const string& str, const char replace, const char with) |
| 543 | { |
| 544 | string result(str); |
| 545 | const int N = result.size(); |
| 546 | for (int i=0; i<N; i++) { |
| 547 | if (result[i] == replace) { |
| 548 | result[i] = with; |
| 549 | } |
| 550 | } |
| 551 | return result; |
| 552 | } |
| 553 | |
Kweku Adams | ecf4bdb | 2018-02-22 15:05:48 -0800 | [diff] [blame] | 554 | static void generateCsv(Descriptor const* descriptor, const string& indent, set<string>* parents, const Destination containerDest = DEST_UNSET) { |
Yi Jin | 0f2599f | 2017-11-16 18:19:45 -0800 | [diff] [blame] | 555 | DebugStringOptions options; |
| 556 | options.include_comments = true; |
| 557 | for (int i=0; i<descriptor->field_count(); i++) { |
| 558 | const FieldDescriptor* field = descriptor->field(i); |
Kweku Adams | ecf4bdb | 2018-02-22 15:05:48 -0800 | [diff] [blame] | 559 | const Destination fieldDest = getFieldDest(field); |
Yi Jin | 0f2599f | 2017-11-16 18:19:45 -0800 | [diff] [blame] | 560 | stringstream text; |
| 561 | if (field->type() == FieldDescriptor::TYPE_MESSAGE) { |
| 562 | text << field->message_type()->name(); |
| 563 | } else { |
| 564 | text << field->type_name(); |
| 565 | } |
| 566 | text << " " << field->name(); |
Kweku Adams | ecf4bdb | 2018-02-22 15:05:48 -0800 | [diff] [blame] | 567 | text << " (PRIVACY="; |
| 568 | if (isDefaultField(field, containerDest)) { |
| 569 | text << getDestString(containerDest); |
| 570 | } else { |
| 571 | text << getDestString(fieldDest); |
| 572 | } |
| 573 | text << ")"; |
Yi Jin | 0f2599f | 2017-11-16 18:19:45 -0800 | [diff] [blame] | 574 | printf("%s%s,\n", indent.c_str(), replace_string(text.str(), '\n', ' ').c_str()); |
| 575 | if (field->type() == FieldDescriptor::TYPE_MESSAGE && |
| 576 | parents->find(field->message_type()->full_name()) == parents->end()) { |
| 577 | parents->insert(field->message_type()->full_name()); |
Kweku Adams | ecf4bdb | 2018-02-22 15:05:48 -0800 | [diff] [blame] | 578 | generateCsv(field->message_type(), indent + ",", parents, fieldDest); |
Yi Jin | 0f2599f | 2017-11-16 18:19:45 -0800 | [diff] [blame] | 579 | parents->erase(field->message_type()->full_name()); |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | // ================================================================================ |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 585 | int main(int argc, char const *argv[]) |
| 586 | { |
Yi Jin | 0f2599f | 2017-11-16 18:19:45 -0800 | [diff] [blame] | 587 | if (argc < 2) return 1; |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 588 | const char* module = argv[1]; |
| 589 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 590 | Descriptor const* descriptor = IncidentProto::descriptor(); |
| 591 | |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 592 | if (strcmp(module, "incident") == 0) { |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 593 | return !generateIncidentSectionsCpp(descriptor); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 594 | } |
| 595 | if (strcmp(module, "incidentd") == 0 ) { |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 596 | return !generateSectionListCpp(descriptor); |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 597 | } |
Yi Jin | 0f2599f | 2017-11-16 18:19:45 -0800 | [diff] [blame] | 598 | // Generates Csv Format of proto definition for each section. |
| 599 | if (strcmp(module, "csv") == 0 && argc > 2) { |
| 600 | int sectionId = atoi(argv[2]); |
| 601 | for (int i=0; i<descriptor->field_count(); i++) { |
| 602 | const FieldDescriptor* field = descriptor->field(i); |
| 603 | if (strcmp(field->name().c_str(), argv[2]) == 0 |
| 604 | || field->number() == sectionId) { |
| 605 | set<string> parents; |
| 606 | printf("%s\n", field->name().c_str()); |
Kweku Adams | ecf4bdb | 2018-02-22 15:05:48 -0800 | [diff] [blame] | 607 | generateCsv(field->message_type(), "", &parents, getFieldDest(field)); |
Yi Jin | 0f2599f | 2017-11-16 18:19:45 -0800 | [diff] [blame] | 608 | break; |
| 609 | } |
| 610 | } |
| 611 | // Returns failure if csv is enabled to prevent Android building with it. |
| 612 | // It doesn't matter if this command runs manually. |
| 613 | return 1; |
| 614 | } |
| 615 | // Returns failure if not called by the whitelisted modules |
Yi Jin | f860184 | 2017-08-15 22:01:41 -0700 | [diff] [blame] | 616 | return 1; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 617 | } |