• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "Collation.h"
18 
19 #include <stdio.h>
20 
21 #include <map>
22 
23 #include "frameworks/proto_logging/stats/atoms.pb.h"
24 #include "utils.h"
25 
26 namespace android {
27 namespace stats_log_api_gen {
28 
29 using google::protobuf::OneofDescriptor;
30 using google::protobuf::EnumDescriptor;
31 using google::protobuf::FieldDescriptor;
32 using google::protobuf::FileDescriptor;
33 using google::protobuf::SourceLocation;
34 using std::make_shared;
35 using std::map;
36 
37 const bool dbg = false;
38 
39 const int PLATFORM_PULLED_ATOMS_START = 10000;
40 const int PLATFORM_PULLED_ATOMS_END = 99999;
41 const int VENDOR_PULLED_ATOMS_START = 150000;
42 const int VENDOR_PULLED_ATOMS_END = 199999;
43 
44 //
45 // AtomDecl class
46 //
47 
AtomDecl()48 AtomDecl::AtomDecl() : code(0), name() {
49 }
50 
AtomDecl(const AtomDecl & that)51 AtomDecl::AtomDecl(const AtomDecl& that)
52     : code(that.code),
53       name(that.name),
54       message(that.message),
55       fields(that.fields),
56       atomType(that.atomType),
57       fieldNumberToAnnotations(that.fieldNumberToAnnotations),
58       primaryFields(that.primaryFields),
59       exclusiveField(that.exclusiveField),
60       defaultState(that.defaultState),
61       triggerStateReset(that.triggerStateReset),
62       nested(that.nested) {
63 }
64 
AtomDecl(int c,const string & n,const string & m,AtomType a)65 AtomDecl::AtomDecl(int c, const string& n, const string& m, AtomType a)
66     : code(c), name(n), message(m), atomType(a) {
67 }
68 
~AtomDecl()69 AtomDecl::~AtomDecl() {
70 }
71 
72 /**
73  * Print an error message for a FieldDescriptor, including the file name and
74  * line number.
75  */
print_error(const FieldDescriptor * field,const char * format,...)76 static void print_error(const FieldDescriptor* field, const char* format, ...) {
77     const Descriptor* message = field->containing_type();
78     const FileDescriptor* file = message->file();
79 
80     SourceLocation loc;
81     if (field->GetSourceLocation(&loc)) {
82         // TODO(b/162454173): this will work if we can figure out how to pass
83         // --include_source_info to protoc
84         fprintf(stderr, "%s:%d: ", file->name().c_str(), loc.start_line);
85     } else {
86         fprintf(stderr, "%s: ", file->name().c_str());
87     }
88     va_list args;
89     va_start(args, format);
90     vfprintf(stderr, format, args);
91     va_end(args);
92 }
93 
94 /**
95  * Convert a protobuf type into a java type.
96  */
java_type(const FieldDescriptor * field)97 static java_type_t java_type(const FieldDescriptor* field) {
98     int protoType = field->type();
99     bool isRepeatedField = field->is_repeated();
100 
101     switch (protoType) {
102         case FieldDescriptor::TYPE_FLOAT:
103             return isRepeatedField ? JAVA_TYPE_FLOAT_ARRAY : JAVA_TYPE_FLOAT;
104         case FieldDescriptor::TYPE_INT64:
105             return isRepeatedField ? JAVA_TYPE_LONG_ARRAY : JAVA_TYPE_LONG;
106         case FieldDescriptor::TYPE_INT32:
107             return isRepeatedField ? JAVA_TYPE_INT_ARRAY : JAVA_TYPE_INT;
108         case FieldDescriptor::TYPE_BOOL:
109             return isRepeatedField ? JAVA_TYPE_BOOLEAN_ARRAY : JAVA_TYPE_BOOLEAN;
110         case FieldDescriptor::TYPE_STRING:
111             return isRepeatedField ? JAVA_TYPE_STRING_ARRAY : JAVA_TYPE_STRING;
112         case FieldDescriptor::TYPE_ENUM:
113             return isRepeatedField ? JAVA_TYPE_ENUM_ARRAY : JAVA_TYPE_ENUM;
114         case FieldDescriptor::TYPE_GROUP:
115             return JAVA_TYPE_UNKNOWN_OR_INVALID;
116         case FieldDescriptor::TYPE_MESSAGE:
117             if (field->message_type()->full_name() == "android.os.statsd.AttributionNode") {
118                 return JAVA_TYPE_ATTRIBUTION_CHAIN;
119             } else if ((field->options().GetExtension(os::statsd::log_mode) ==
120                         os::statsd::LogMode::MODE_BYTES) &&
121                        !isRepeatedField) {
122                 return JAVA_TYPE_BYTE_ARRAY;
123             } else {
124                 return isRepeatedField ? JAVA_TYPE_UNKNOWN_OR_INVALID : JAVA_TYPE_OBJECT;
125             }
126         case FieldDescriptor::TYPE_BYTES:
127             return isRepeatedField ? JAVA_TYPE_UNKNOWN_OR_INVALID : JAVA_TYPE_BYTE_ARRAY;
128         case FieldDescriptor::TYPE_UINT64:
129             return isRepeatedField ? JAVA_TYPE_UNKNOWN_OR_INVALID : JAVA_TYPE_LONG;
130         case FieldDescriptor::TYPE_UINT32:
131             return isRepeatedField ? JAVA_TYPE_UNKNOWN_OR_INVALID : JAVA_TYPE_INT;
132         default:
133             return JAVA_TYPE_UNKNOWN_OR_INVALID;
134     }
135 }
136 
137 /**
138  * Gather the enums info.
139  */
collate_enums(const EnumDescriptor & enumDescriptor,AtomField * atomField)140 void collate_enums(const EnumDescriptor& enumDescriptor, AtomField* atomField) {
141     for (int i = 0; i < enumDescriptor.value_count(); i++) {
142         atomField->enumValues[enumDescriptor.value(i)->number()] =
143                 enumDescriptor.value(i)->name();
144     }
145 }
146 
addAnnotationToAtomDecl(AtomDecl * atomDecl,const int fieldNumber,const AnnotationId annotationId,const AnnotationType annotationType,const AnnotationValue annotationValue)147 static void addAnnotationToAtomDecl(AtomDecl* atomDecl, const int fieldNumber,
148                                     const AnnotationId annotationId,
149                                     const AnnotationType annotationType,
150                                     const AnnotationValue annotationValue) {
151     if (dbg) {
152         printf("   Adding annotation to %s: [%d] = {id: %d, type: %d}\n", atomDecl->name.c_str(),
153                fieldNumber, annotationId, annotationType);
154     }
155     atomDecl->fieldNumberToAnnotations[fieldNumber].insert(
156             make_shared<Annotation>(annotationId, atomDecl->code, annotationType, annotationValue));
157 }
158 
collate_field_annotations(AtomDecl * atomDecl,const FieldDescriptor * field,const int fieldNumber,const java_type_t & javaType)159 static int collate_field_annotations(AtomDecl* atomDecl, const FieldDescriptor* field,
160                                      const int fieldNumber, const java_type_t& javaType) {
161     int errorCount = 0;
162 
163     if (field->options().HasExtension(os::statsd::state_field_option)) {
164         if (is_repeated_field(javaType)) {
165             print_error(
166                 field,
167                 "State field annotations are not allowed for repeated fields: '%s'\n",
168                 atomDecl->message.c_str());
169             errorCount++;
170             return errorCount;
171         }
172 
173         const os::statsd::StateAtomFieldOption& stateFieldOption =
174                 field->options().GetExtension(os::statsd::state_field_option);
175         const bool primaryField = stateFieldOption.primary_field();
176         const bool exclusiveState = stateFieldOption.exclusive_state();
177         const bool primaryFieldFirstUid = stateFieldOption.primary_field_first_uid();
178 
179         // Check the field is only one of primaryField, exclusiveState, or primaryFieldFirstUid.
180         if (primaryField + primaryFieldFirstUid + exclusiveState > 1) {
181             print_error(field,
182                         "Field can be max 1 of primary_field, exclusive_state, "
183                         "or primary_field_first_uid: '%s'\n",
184                         atomDecl->message.c_str());
185             errorCount++;
186         }
187 
188         if (primaryField) {
189             if (javaType == JAVA_TYPE_ATTRIBUTION_CHAIN || javaType == JAVA_TYPE_OBJECT ||
190                 javaType == JAVA_TYPE_BYTE_ARRAY) {
191                 print_error(field, "Invalid primary state field: '%s'\n",
192                             atomDecl->message.c_str());
193                 errorCount++;
194             } else {
195                 atomDecl->primaryFields.push_back(fieldNumber);
196                 addAnnotationToAtomDecl(atomDecl, fieldNumber, ANNOTATION_ID_PRIMARY_FIELD,
197                                         ANNOTATION_TYPE_BOOL, AnnotationValue(true));
198             }
199         }
200 
201         if (primaryFieldFirstUid) {
202             if (javaType != JAVA_TYPE_ATTRIBUTION_CHAIN) {
203                 print_error(field,
204                             "PRIMARY_FIELD_FIRST_UID annotation is only for AttributionChains: "
205                             "'%s'\n",
206                             atomDecl->message.c_str());
207                 errorCount++;
208             } else {
209                 atomDecl->primaryFields.push_back(FIRST_UID_IN_CHAIN_ID);
210                 addAnnotationToAtomDecl(atomDecl, fieldNumber,
211                                         ANNOTATION_ID_PRIMARY_FIELD_FIRST_UID, ANNOTATION_TYPE_BOOL,
212                                         AnnotationValue(true));
213             }
214         }
215 
216         if (exclusiveState) {
217             if (javaType == JAVA_TYPE_ATTRIBUTION_CHAIN || javaType == JAVA_TYPE_OBJECT ||
218                 javaType == JAVA_TYPE_BYTE_ARRAY) {
219                 print_error(field, "Invalid exclusive state field: '%s'\n",
220                             atomDecl->message.c_str());
221                 errorCount++;
222             }
223 
224             if (atomDecl->exclusiveField != 0) {
225                 print_error(field,
226                             "Cannot have more than one exclusive state field in an "
227                             "atom: '%s'\n",
228                             atomDecl->message.c_str());
229                 errorCount++;
230             } else {
231                 atomDecl->exclusiveField = fieldNumber;
232                 addAnnotationToAtomDecl(atomDecl, fieldNumber, ANNOTATION_ID_EXCLUSIVE_STATE,
233                                         ANNOTATION_TYPE_BOOL, AnnotationValue(true));
234             }
235 
236             if (stateFieldOption.has_default_state_value()) {
237                 const int defaultState = stateFieldOption.default_state_value();
238                 atomDecl->defaultState = defaultState;
239 
240                 addAnnotationToAtomDecl(atomDecl, fieldNumber, ANNOTATION_ID_DEFAULT_STATE,
241                                         ANNOTATION_TYPE_INT, AnnotationValue(defaultState));
242             }
243 
244             if (stateFieldOption.has_trigger_state_reset_value()) {
245                 const int triggerStateReset = stateFieldOption.trigger_state_reset_value();
246 
247                 atomDecl->triggerStateReset = triggerStateReset;
248                 addAnnotationToAtomDecl(atomDecl, fieldNumber, ANNOTATION_ID_TRIGGER_STATE_RESET,
249                                         ANNOTATION_TYPE_INT, AnnotationValue(triggerStateReset));
250             }
251 
252             if (stateFieldOption.has_nested()) {
253                 const bool nested = stateFieldOption.nested();
254                 atomDecl->nested = nested;
255 
256                 addAnnotationToAtomDecl(atomDecl, fieldNumber, ANNOTATION_ID_STATE_NESTED,
257                                         ANNOTATION_TYPE_BOOL, AnnotationValue(nested));
258             }
259         }
260     }
261 
262     if (field->options().GetExtension(os::statsd::is_uid) == true) {
263         if (javaType != JAVA_TYPE_INT && javaType != JAVA_TYPE_INT_ARRAY) {
264             print_error(field,
265                         "is_uid annotation can only be applied to int32 fields and repeated int32 "
266                         "fields: '%s'\n",
267                         atomDecl->message.c_str());
268             errorCount++;
269         }
270 
271         addAnnotationToAtomDecl(atomDecl, fieldNumber, ANNOTATION_ID_IS_UID, ANNOTATION_TYPE_BOOL,
272                                 AnnotationValue(true));
273     }
274 
275     return errorCount;
276 }
277 
278 /**
279  * Gather the info about an atom proto.
280  */
collate_atom(const Descriptor * atom,AtomDecl * atomDecl,vector<java_type_t> * signature)281 int collate_atom(const Descriptor* atom, AtomDecl* atomDecl, vector<java_type_t>* signature) {
282     int errorCount = 0;
283 
284     // Build a sorted list of the fields. Descriptor has them in source file
285     // order.
286     map<int, const FieldDescriptor*> fields;
287     for (int j = 0; j < atom->field_count(); j++) {
288         const FieldDescriptor* field = atom->field(j);
289         fields[field->number()] = field;
290     }
291 
292     // Check that the parameters start at 1 and go up sequentially.
293     int expectedNumber = 1;
294     for (map<int, const FieldDescriptor*>::const_iterator it = fields.begin(); it != fields.end();
295          it++) {
296         const int number = it->first;
297         const FieldDescriptor* field = it->second;
298         if (number != expectedNumber) {
299             print_error(field,
300                         "Fields must be numbered consecutively starting at 1:"
301                         " '%s' is %d but should be %d\n",
302                         field->name().c_str(), number, expectedNumber);
303             errorCount++;
304             expectedNumber = number;
305             continue;
306         }
307         expectedNumber++;
308     }
309 
310     // Check that only allowed types are present. Remove any invalid ones.
311     for (map<int, const FieldDescriptor*>::const_iterator it = fields.begin(); it != fields.end();
312          it++) {
313         const FieldDescriptor* field = it->second;
314         bool isBinaryField = field->options().GetExtension(os::statsd::log_mode) ==
315                              os::statsd::LogMode::MODE_BYTES;
316 
317         java_type_t javaType = java_type(field);
318 
319         if (javaType == JAVA_TYPE_UNKNOWN_OR_INVALID) {
320             if (field->is_repeated()) {
321                 print_error(field, "Repeated field type %d is not allowed for field: %s\n",
322                             field->type(), field->name().c_str());
323             } else {
324                 print_error(field, "Field type %d is not allowed for field: %s\n", field->type(),
325                             field->name().c_str());
326             }
327             errorCount++;
328             continue;
329         } else if (javaType == JAVA_TYPE_OBJECT) {
330             // Allow attribution chain, but only at position 1.
331             print_error(field, "Message type not allowed for field without mode_bytes: %s\n",
332                         field->name().c_str());
333             errorCount++;
334             continue;
335         } else if (javaType == JAVA_TYPE_BYTE_ARRAY && !isBinaryField) {
336             print_error(field, "Raw bytes type not allowed for field: %s\n", field->name().c_str());
337             errorCount++;
338             continue;
339         }
340 
341         if (isBinaryField && javaType != JAVA_TYPE_BYTE_ARRAY) {
342             print_error(field, "Cannot mark field %s as bytes.\n", field->name().c_str());
343             errorCount++;
344             continue;
345         }
346     }
347 
348     // Check that if there's an attribution chain, it's at position 1.
349     for (map<int, const FieldDescriptor*>::const_iterator it = fields.begin(); it != fields.end();
350          it++) {
351         int number = it->first;
352         if (number != 1) {
353             const FieldDescriptor* field = it->second;
354             java_type_t javaType = java_type(field);
355             if (javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
356                 print_error(field,
357                             "AttributionChain fields must have field id 1, in message: '%s'\n",
358                             atom->name().c_str());
359                 errorCount++;
360             }
361         }
362     }
363 
364     // Build the type signature and the atom data.
365     for (map<int, const FieldDescriptor*>::const_iterator it = fields.begin(); it != fields.end();
366          it++) {
367         const FieldDescriptor* field = it->second;
368         java_type_t javaType = java_type(field);
369         bool isBinaryField = field->options().GetExtension(os::statsd::log_mode) ==
370                              os::statsd::LogMode::MODE_BYTES;
371 
372         AtomField atField(field->name(), javaType);
373 
374         if (javaType == JAVA_TYPE_ENUM || javaType == JAVA_TYPE_ENUM_ARRAY) {
375             // All enums are treated as ints when it comes to function signatures.
376             collate_enums(*field->enum_type(), &atField);
377         }
378 
379         // Generate signature for atom.
380         if (javaType == JAVA_TYPE_ENUM) {
381             // All enums are treated as ints when it comes to function signatures.
382             signature->push_back(JAVA_TYPE_INT);
383         } else if (javaType == JAVA_TYPE_ENUM_ARRAY) {
384             signature->push_back(JAVA_TYPE_INT_ARRAY);
385         } else if (javaType == JAVA_TYPE_OBJECT && isBinaryField) {
386             signature->push_back(JAVA_TYPE_BYTE_ARRAY);
387         } else {
388             signature->push_back(javaType);
389         }
390 
391         atomDecl->fields.push_back(atField);
392 
393         errorCount += collate_field_annotations(atomDecl, field, it->first, javaType);
394     }
395 
396     return errorCount;
397 }
398 
399 // This function flattens the fields of the AttributionNode proto in an Atom
400 // proto and generates the corresponding atom decl and signature.
get_non_chained_node(const Descriptor * atom,AtomDecl * atomDecl,vector<java_type_t> * signature)401 bool get_non_chained_node(const Descriptor* atom, AtomDecl* atomDecl,
402                           vector<java_type_t>* signature) {
403     // Build a sorted list of the fields. Descriptor has them in source file
404     // order.
405     map<int, const FieldDescriptor*> fields;
406     for (int j = 0; j < atom->field_count(); j++) {
407         const FieldDescriptor* field = atom->field(j);
408         fields[field->number()] = field;
409     }
410 
411     AtomDecl attributionDecl;
412     vector<java_type_t> attributionSignature;
413     collate_atom(android::os::statsd::AttributionNode::descriptor(), &attributionDecl,
414                  &attributionSignature);
415 
416     // Build the type signature and the atom data.
417     bool has_attribution_node = false;
418     for (map<int, const FieldDescriptor*>::const_iterator it = fields.begin(); it != fields.end();
419          it++) {
420         const FieldDescriptor* field = it->second;
421         java_type_t javaType = java_type(field);
422         if (javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
423             atomDecl->fields.insert(atomDecl->fields.end(), attributionDecl.fields.begin(),
424                                     attributionDecl.fields.end());
425             signature->insert(signature->end(), attributionSignature.begin(),
426                               attributionSignature.end());
427             has_attribution_node = true;
428 
429         } else {
430             AtomField atField(field->name(), javaType);
431             if (javaType == JAVA_TYPE_ENUM) {
432                 // All enums are treated as ints when it comes to function signatures.
433                 signature->push_back(JAVA_TYPE_INT);
434                 collate_enums(*field->enum_type(), &atField);
435             } else {
436                 signature->push_back(javaType);
437             }
438             atomDecl->fields.push_back(atField);
439         }
440     }
441     return has_attribution_node;
442 }
443 
populateFieldNumberToAtomDeclSet(const shared_ptr<AtomDecl> & atomDecl,FieldNumberToAtomDeclSet * fieldNumberToAtomDeclSet)444 static void populateFieldNumberToAtomDeclSet(const shared_ptr<AtomDecl>& atomDecl,
445                                              FieldNumberToAtomDeclSet* fieldNumberToAtomDeclSet) {
446     for (FieldNumberToAnnotations::const_iterator it = atomDecl->fieldNumberToAnnotations.begin();
447          it != atomDecl->fieldNumberToAnnotations.end(); it++) {
448         const int fieldNumber = it->first;
449         (*fieldNumberToAtomDeclSet)[fieldNumber].insert(atomDecl);
450     }
451 }
452 
getAtomType(const FieldDescriptor * atomField)453 static AtomType getAtomType(const FieldDescriptor* atomField) {
454     const int atomId = atomField->number();
455     if ((atomId >= PLATFORM_PULLED_ATOMS_START && atomId <= PLATFORM_PULLED_ATOMS_END)
456             || (atomId >= VENDOR_PULLED_ATOMS_START && atomId <= VENDOR_PULLED_ATOMS_END)) {
457         return ATOM_TYPE_PULLED;
458     } else {
459         return ATOM_TYPE_PUSHED;
460     }
461 }
462 
collate_from_field_descriptor(const FieldDescriptor * atomField,const string & moduleName,Atoms * atoms)463 static int collate_from_field_descriptor(const FieldDescriptor* atomField, const string& moduleName,
464                                          Atoms* atoms) {
465     int errorCount = 0;
466 
467     if (moduleName != DEFAULT_MODULE_NAME) {
468         const int moduleCount = atomField->options().ExtensionSize(os::statsd::module);
469         bool moduleFound = false;
470         for (int j = 0; j < moduleCount; ++j) {
471             const string atomModuleName =
472                     atomField->options().GetExtension(os::statsd::module, j);
473             if (atomModuleName == moduleName) {
474                 moduleFound = true;
475                 break;
476             }
477         }
478 
479         // This atom is not in the module we're interested in; skip it.
480         if (!moduleFound) {
481             if (dbg) {
482                 printf("   Skipping %s (%d)\n", atomField->name().c_str(), atomField->number());
483             }
484             return errorCount;
485         }
486     }
487 
488     if (dbg) {
489         printf("   %s (%d)\n", atomField->name().c_str(), atomField->number());
490     }
491 
492     // StatsEvent only has one oneof, which contains only messages. Don't allow
493     // other types.
494     if (atomField->type() != FieldDescriptor::TYPE_MESSAGE) {
495         print_error(atomField,
496                     "Bad type for atom. StatsEvent can only have message type "
497                     "fields: %s\n",
498                     atomField->name().c_str());
499         errorCount++;
500         return errorCount;
501     }
502 
503     const AtomType atomType = getAtomType(atomField);
504 
505     const Descriptor* atom = atomField->message_type();
506     shared_ptr<AtomDecl> atomDecl =
507             make_shared<AtomDecl>(atomField->number(), atomField->name(), atom->name(),
508                                   atomType);
509 
510     if (atomField->options().GetExtension(os::statsd::truncate_timestamp)) {
511         addAnnotationToAtomDecl(atomDecl.get(), ATOM_ID_FIELD_NUMBER,
512                                 ANNOTATION_ID_TRUNCATE_TIMESTAMP, ANNOTATION_TYPE_BOOL,
513                                 AnnotationValue(true));
514         if (dbg) {
515             printf("%s can have timestamp truncated\n", atomField->name().c_str());
516         }
517     }
518 
519     vector<java_type_t> signature;
520     errorCount += collate_atom(atom, atomDecl.get(), &signature);
521     if (!atomDecl->primaryFields.empty() && atomDecl->exclusiveField == 0) {
522         print_error(atomField, "Cannot have a primary field without an exclusive field: %s\n",
523                     atomField->name().c_str());
524         errorCount++;
525         return errorCount;
526     }
527 
528     FieldNumberToAtomDeclSet& fieldNumberToAtomDeclSet =
529         atomType == ATOM_TYPE_PUSHED ?
530         atoms->signatureInfoMap[signature] :
531         atoms->pulledAtomsSignatureInfoMap[signature];
532     populateFieldNumberToAtomDeclSet(atomDecl, &fieldNumberToAtomDeclSet);
533 
534     atoms->decls.insert(atomDecl);
535 
536     shared_ptr<AtomDecl> nonChainedAtomDecl =
537             make_shared<AtomDecl>(atomField->number(), atomField->name(), atom->name(),
538                                   atomType);
539     vector<java_type_t> nonChainedSignature;
540     if (get_non_chained_node(atom, nonChainedAtomDecl.get(), &nonChainedSignature)) {
541         FieldNumberToAtomDeclSet& nonChainedFieldNumberToAtomDeclSet =
542                 atoms->nonChainedSignatureInfoMap[nonChainedSignature];
543         populateFieldNumberToAtomDeclSet(nonChainedAtomDecl,
544                                          &nonChainedFieldNumberToAtomDeclSet);
545 
546         atoms->non_chained_decls.insert(nonChainedAtomDecl);
547     }
548 
549     return errorCount;
550 }
551 
552 /**
553  * Gather the info about the atoms.
554  */
collate_atoms(const Descriptor * descriptor,const string & moduleName,Atoms * atoms)555 int collate_atoms(const Descriptor* descriptor, const string& moduleName, Atoms* atoms) {
556     int errorCount = 0;
557 
558     // Regular field atoms in Atom
559     for (int i = 0; i < descriptor->field_count(); i++) {
560         const FieldDescriptor* atomField = descriptor->field(i);
561         errorCount += collate_from_field_descriptor(atomField, moduleName, atoms);
562     }
563 
564     // Extension field atoms in Atom.
565     vector<const FieldDescriptor*> extensions;
566     descriptor->file()->pool()->FindAllExtensions(descriptor, &extensions);
567     for (const FieldDescriptor* atomField : extensions) {
568         errorCount += collate_from_field_descriptor(atomField, moduleName, atoms);
569     }
570 
571     if (dbg) {
572         // Signatures for pushed atoms.
573         printf("signatures = [\n");
574         for (SignatureInfoMap::const_iterator it = atoms->signatureInfoMap.begin();
575              it != atoms->signatureInfoMap.end(); it++) {
576             printf("   ");
577             for (vector<java_type_t>::const_iterator jt = it->first.begin(); jt != it->first.end();
578                  jt++) {
579                 printf(" %d", static_cast<int>(*jt));
580             }
581             printf("\n");
582         }
583 
584         // Signatures for pull atoms.
585         for (SignatureInfoMap::const_iterator it = atoms->pulledAtomsSignatureInfoMap.begin();
586              it != atoms->pulledAtomsSignatureInfoMap.end(); it++) {
587             printf("   ");
588             for (vector<java_type_t>::const_iterator jt = it->first.begin(); jt != it->first.end();
589                  jt++) {
590                 printf(" %d", static_cast<int>(*jt));
591             }
592             printf("\n");
593         }
594         printf("]\n");
595     }
596 
597     return errorCount;
598 }
599 
600 }  // namespace stats_log_api_gen
601 }  // namespace android
602