blob: f76196dd2a195c60a8dff52b1caf79fef280bb13 [file] [log] [blame]
Yao Chend54f9dd2017-10-17 17:37:48 +00001/*
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#include <map>
21
22namespace android {
23namespace stats_log_api_gen {
24
Stefan Lafon9478f352017-10-30 21:20:20 -070025using google::protobuf::EnumDescriptor;
Yao Chend54f9dd2017-10-17 17:37:48 +000026using google::protobuf::FieldDescriptor;
27using google::protobuf::FileDescriptor;
28using google::protobuf::SourceLocation;
29using std::map;
30
31
32//
33// AtomDecl class
34//
35
36AtomDecl::AtomDecl()
37 :code(0),
38 name()
39{
40}
41
42AtomDecl::AtomDecl(const AtomDecl& that)
43 :code(that.code),
44 name(that.name),
45 message(that.message),
46 fields(that.fields)
47{
48}
49
50AtomDecl::AtomDecl(int c, const string& n, const string& m)
51 :code(c),
52 name(n),
53 message(m)
54{
55}
56
57AtomDecl::~AtomDecl()
58{
59}
60
61
62/**
63 * Print an error message for a FieldDescriptor, including the file name and line number.
64 */
65static void
66print_error(const FieldDescriptor* field, const char* format, ...)
67{
68 const Descriptor* message = field->containing_type();
69 const FileDescriptor* file = message->file();
70
71 SourceLocation loc;
72 if (field->GetSourceLocation(&loc)) {
73 // TODO: this will work if we can figure out how to pass --include_source_info to protoc
74 fprintf(stderr, "%s:%d: ", file->name().c_str(), loc.start_line);
75 } else {
76 fprintf(stderr, "%s: ", file->name().c_str());
77 }
78 va_list args;
79 va_start(args, format);
80 vfprintf(stderr, format, args);
81 va_end (args);
82}
83
84/**
85 * Convert a protobuf type into a java type.
86 */
87static java_type_t
88java_type(const FieldDescriptor* field)
89{
90 int protoType = field->type();
91 switch (protoType) {
92 case FieldDescriptor::TYPE_DOUBLE:
93 return JAVA_TYPE_DOUBLE;
94 case FieldDescriptor::TYPE_FLOAT:
95 return JAVA_TYPE_FLOAT;
96 case FieldDescriptor::TYPE_INT64:
97 return JAVA_TYPE_LONG;
98 case FieldDescriptor::TYPE_UINT64:
99 return JAVA_TYPE_LONG;
100 case FieldDescriptor::TYPE_INT32:
101 return JAVA_TYPE_INT;
102 case FieldDescriptor::TYPE_FIXED64:
103 return JAVA_TYPE_LONG;
104 case FieldDescriptor::TYPE_FIXED32:
105 return JAVA_TYPE_INT;
106 case FieldDescriptor::TYPE_BOOL:
107 return JAVA_TYPE_BOOLEAN;
108 case FieldDescriptor::TYPE_STRING:
109 return JAVA_TYPE_STRING;
110 case FieldDescriptor::TYPE_GROUP:
111 return JAVA_TYPE_UNKNOWN;
112 case FieldDescriptor::TYPE_MESSAGE:
113 // TODO: not the final package name
114 if (field->message_type()->full_name() == "android.os.statsd.WorkSource") {
115 return JAVA_TYPE_WORK_SOURCE;
116 } else {
117 return JAVA_TYPE_OBJECT;
118 }
119 case FieldDescriptor::TYPE_BYTES:
120 return JAVA_TYPE_BYTE_ARRAY;
121 case FieldDescriptor::TYPE_UINT32:
122 return JAVA_TYPE_INT;
123 case FieldDescriptor::TYPE_ENUM:
Stefan Lafon9478f352017-10-30 21:20:20 -0700124 return JAVA_TYPE_ENUM;
Yao Chend54f9dd2017-10-17 17:37:48 +0000125 case FieldDescriptor::TYPE_SFIXED32:
126 return JAVA_TYPE_INT;
127 case FieldDescriptor::TYPE_SFIXED64:
128 return JAVA_TYPE_LONG;
129 case FieldDescriptor::TYPE_SINT32:
130 return JAVA_TYPE_INT;
131 case FieldDescriptor::TYPE_SINT64:
132 return JAVA_TYPE_LONG;
133 default:
134 return JAVA_TYPE_UNKNOWN;
135 }
136}
137
138/**
139 * Gather the info about the atoms.
140 */
141int
142collate_atoms(const Descriptor* descriptor, Atoms* atoms)
143{
144 int errorCount = 0;
145 const bool dbg = false;
146
147 for (int i=0; i<descriptor->field_count(); i++) {
148 const FieldDescriptor* atomField = descriptor->field(i);
149
150 if (dbg) {
151 printf(" %s (%d)\n", atomField->name().c_str(), atomField->number());
152 }
153
154 // StatsEvent only has one oneof, which contains only messages. Don't allow other types.
155 if (atomField->type() != FieldDescriptor::TYPE_MESSAGE) {
156 print_error(atomField,
157 "Bad type for atom. StatsEvent can only have message type fields: %s\n",
158 atomField->name().c_str());
159 errorCount++;
160 continue;
161 }
162
163 const Descriptor* atom = atomField->message_type();
164
165 // Build a sorted list of the fields. Descriptor has them in source file order.
166 map<int,const FieldDescriptor*> fields;
167 for (int j=0; j<atom->field_count(); j++) {
168 const FieldDescriptor* field = atom->field(j);
169 fields[field->number()] = field;
170 }
171
172 // Check that the parameters start at 1 and go up sequentially.
173 int expectedNumber = 1;
174 for (map<int,const FieldDescriptor*>::const_iterator it = fields.begin();
175 it != fields.end(); it++) {
176 const int number = it->first;
177 const FieldDescriptor* field = it->second;
178 if (number != expectedNumber) {
179 print_error(field, "Fields must be numbered consecutively starting at 1:"
180 " '%s' is %d but should be %d\n",
181 field->name().c_str(), number, expectedNumber);
182 errorCount++;
183 expectedNumber = number;
184 continue;
185 }
186 expectedNumber++;
187 }
188
189 // Check that only allowed types are present. Remove any invalid ones.
190 for (map<int,const FieldDescriptor*>::const_iterator it = fields.begin();
191 it != fields.end(); it++) {
192 const FieldDescriptor* field = it->second;
193
194 java_type_t javaType = java_type(field);
195
196 if (javaType == JAVA_TYPE_UNKNOWN) {
197 print_error(field, "Unkown type for field: %s\n", field->name().c_str());
198 errorCount++;
199 continue;
200 } else if (javaType == JAVA_TYPE_OBJECT) {
201 // Allow WorkSources, but only at position 1.
202 print_error(field, "Message type not allowed for field: %s\n",
203 field->name().c_str());
204 errorCount++;
205 continue;
206 } else if (javaType == JAVA_TYPE_BYTE_ARRAY) {
207 print_error(field, "Raw bytes type not allowed for field: %s\n",
208 field->name().c_str());
209 errorCount++;
210 continue;
211 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000212 }
213
214 // Check that if there's a WorkSource, it's at position 1.
215 for (map<int,const FieldDescriptor*>::const_iterator it = fields.begin();
216 it != fields.end(); it++) {
217 int number = it->first;
218 if (number != 1) {
219 const FieldDescriptor* field = it->second;
220 java_type_t javaType = java_type(field);
221 if (javaType == JAVA_TYPE_WORK_SOURCE) {
222 print_error(field, "WorkSource fields must have field id 1, in message: '%s'\n",
223 atom->name().c_str());
224 errorCount++;
225 }
226 }
227 }
228
229 AtomDecl atomDecl(atomField->number(), atomField->name(), atom->name());
230
Stefan Lafon9478f352017-10-30 21:20:20 -0700231 // Build the type signature and the atom data.
Yao Chend54f9dd2017-10-17 17:37:48 +0000232 vector<java_type_t> signature;
233 for (map<int,const FieldDescriptor*>::const_iterator it = fields.begin();
234 it != fields.end(); it++) {
235 const FieldDescriptor* field = it->second;
236 java_type_t javaType = java_type(field);
237
Stefan Lafon9478f352017-10-30 21:20:20 -0700238 AtomField atField(field->name(), javaType);
239 if (javaType == JAVA_TYPE_ENUM) {
240 // All enums are treated as ints when it comes to function signatures.
241 signature.push_back(JAVA_TYPE_INT);
242 const EnumDescriptor* enumDescriptor = field->enum_type();
243 for (int i = 0; i < enumDescriptor->value_count(); i++) {
244 atField.enumValues[enumDescriptor->value(i)->number()] =
245 enumDescriptor->value(i)->name().c_str();
246 }
247 } else {
248 signature.push_back(javaType);
249 }
250 atomDecl.fields.push_back(atField);
Yao Chend54f9dd2017-10-17 17:37:48 +0000251 }
252
253 atoms->signatures.insert(signature);
254 atoms->decls.insert(atomDecl);
255 }
256
257 if (dbg) {
258 printf("signatures = [\n");
259 for (set<vector<java_type_t>>::const_iterator it = atoms->signatures.begin();
260 it != atoms->signatures.end(); it++) {
261 printf(" ");
262 for (vector<java_type_t>::const_iterator jt = it->begin(); jt != it->end(); jt++) {
263 printf(" %d", (int)*jt);
264 }
265 printf("\n");
266 }
267 printf("]\n");
268 }
269
270 return errorCount;
271}
272
273} // namespace stats_log_api_gen
274} // namespace android