summaryrefslogtreecommitdiff
path: root/tools/stats_log_api_gen/native_writer.cpp
blob: 0cf3225e77084a130148154370da7400e64890ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 * Copyright (C) 2019, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "native_writer.h"

#include "utils.h"

namespace android {
namespace stats_log_api_gen {

static void write_annotations(FILE* out, int argIndex,
                              const FieldNumberToAnnotations& fieldNumberToAnnotations,
                              const string& methodPrefix, const string& methodSuffix) {
    auto fieldNumberToAnnotationsIt = fieldNumberToAnnotations.find(argIndex);
    if (fieldNumberToAnnotationsIt == fieldNumberToAnnotations.end()) {
        return;
    }
    const set<shared_ptr<Annotation>>& annotations = fieldNumberToAnnotationsIt->second;
    for (const shared_ptr<Annotation>& annotation : annotations) {
        // TODO(b/151744250): Group annotations for same atoms.
        // TODO(b/151786433): Write atom constant name instead of atom id literal.
        fprintf(out, "    if (code == %d) {\n", annotation->atomId);
        switch (annotation->type) {
            // TODO(b/151776731): Check for reset state annotation and only include
            // reset state when field value == default state annotation value.
            case ANNOTATION_TYPE_INT:
                // TODO(b/151786433): Write annotation constant name instead of
                // annotation id literal.
                fprintf(out, "        %saddInt32Annotation(%s%d, %d);\n", methodPrefix.c_str(),
                        methodSuffix.c_str(), annotation->annotationId, annotation->value.intValue);
                break;
            case ANNOTATION_TYPE_BOOL:
                // TODO(b/151786433): Write annotation constant name instead of
                // annotation id literal.
                fprintf(out, "        %saddBoolAnnotation(%s%d, %s);\n", methodPrefix.c_str(),
                        methodSuffix.c_str(), annotation->annotationId,
                        annotation->value.boolValue ? "true" : "false");
                break;
            default:
                break;
        }
        fprintf(out, "    }\n");
    }
}

static int write_native_stats_write_methods(FILE* out, const Atoms& atoms,
                                            const AtomDecl& attributionDecl, const bool supportQ) {
    fprintf(out, "\n");
    for (auto signatureInfoMapIt = atoms.signatureInfoMap.begin();
         signatureInfoMapIt != atoms.signatureInfoMap.end(); signatureInfoMapIt++) {
        vector<java_type_t> signature = signatureInfoMapIt->first;
        const FieldNumberToAnnotations& fieldNumberToAnnotations = signatureInfoMapIt->second;
        // Key value pairs not supported in native.
        if (find(signature.begin(), signature.end(), JAVA_TYPE_KEY_VALUE_PAIR) != signature.end()) {
            continue;
        }
        write_native_method_signature(out, "int stats_write", signature, attributionDecl, " {");

        int argIndex = 1;
        if (supportQ) {
            fprintf(out, "    StatsEventCompat event;\n");
            fprintf(out, "    event.setAtomId(code);\n");
            write_annotations(out, ATOM_ID_FIELD_NUMBER, fieldNumberToAnnotations, "event.", "");
            for (vector<java_type_t>::const_iterator arg = signature.begin();
                 arg != signature.end(); arg++) {
                switch (*arg) {
                    case JAVA_TYPE_ATTRIBUTION_CHAIN: {
                        const char* uidName = attributionDecl.fields.front().name.c_str();
                        const char* tagName = attributionDecl.fields.back().name.c_str();
                        fprintf(out, "    event.writeAttributionChain(%s, %s_length, %s);\n",
                                uidName, uidName, tagName);
                        break;
                    }
                    case JAVA_TYPE_BYTE_ARRAY:
                        fprintf(out, "    event.writeByteArray(arg%d.arg, arg%d.arg_length);\n",
                                argIndex, argIndex);
                        break;
                    case JAVA_TYPE_BOOLEAN:
                        fprintf(out, "    event.writeBool(arg%d);\n", argIndex);
                        break;
                    case JAVA_TYPE_INT:  // Fall through.
                    case JAVA_TYPE_ENUM:
                        fprintf(out, "    event.writeInt32(arg%d);\n", argIndex);
                        break;
                    case JAVA_TYPE_FLOAT:
                        fprintf(out, "    event.writeFloat(arg%d);\n", argIndex);
                        break;
                    case JAVA_TYPE_LONG:
                        fprintf(out, "    event.writeInt64(arg%d);\n", argIndex);
                        break;
                    case JAVA_TYPE_STRING:
                        fprintf(out, "    event.writeString(arg%d);\n", argIndex);
                        break;
                    default:
                        // Unsupported types: OBJECT, DOUBLE, KEY_VALUE_PAIRS.
                        fprintf(stderr, "Encountered unsupported type.");
                        return 1;
                }
                write_annotations(out, argIndex, fieldNumberToAnnotations, "event.", "");
                argIndex++;
            }
            fprintf(out, "    return event.writeToSocket();\n");
        } else {
            fprintf(out, "    AStatsEvent* event = AStatsEvent_obtain();\n");
            fprintf(out, "    AStatsEvent_setAtomId(event, code);\n");
            write_annotations(out, ATOM_ID_FIELD_NUMBER, fieldNumberToAnnotations, "AStatsEvent_",
                              "event, ");
            for (vector<java_type_t>::const_iterator arg = signature.begin();
                 arg != signature.end(); arg++) {
                switch (*arg) {
                    case JAVA_TYPE_ATTRIBUTION_CHAIN: {
                        const char* uidName = attributionDecl.fields.front().name.c_str();
                        const char* tagName = attributionDecl.fields.back().name.c_str();
                        fprintf(out,
                                "    AStatsEvent_writeAttributionChain(event, "
                                "reinterpret_cast<const uint32_t*>(%s), %s.data(), "
                                "static_cast<uint8_t>(%s_length));\n",
                                uidName, tagName, uidName);
                        break;
                    }
                    case JAVA_TYPE_BYTE_ARRAY:
                        fprintf(out,
                                "    AStatsEvent_writeByteArray(event, "
                                "reinterpret_cast<const uint8_t*>(arg%d.arg), "
                                "arg%d.arg_length);\n",
                                argIndex, argIndex);
                        break;
                    case JAVA_TYPE_BOOLEAN:
                        fprintf(out, "    AStatsEvent_writeBool(event, arg%d);\n", argIndex);
                        break;
                    case JAVA_TYPE_INT:  // Fall through.
                    case JAVA_TYPE_ENUM:
                        fprintf(out, "    AStatsEvent_writeInt32(event, arg%d);\n", argIndex);
                        break;
                    case JAVA_TYPE_FLOAT:
                        fprintf(out, "    AStatsEvent_writeFloat(event, arg%d);\n", argIndex);
                        break;
                    case JAVA_TYPE_LONG:
                        fprintf(out, "    AStatsEvent_writeInt64(event, arg%d);\n", argIndex);
                        break;
                    case JAVA_TYPE_STRING:
                        fprintf(out, "    AStatsEvent_writeString(event, arg%d);\n", argIndex);
                        break;
                    default:
                        // Unsupported types: OBJECT, DOUBLE, KEY_VALUE_PAIRS
                        fprintf(stderr, "Encountered unsupported type.");
                        return 1;
                }
                write_annotations(out, argIndex, fieldNumberToAnnotations, "AStatsEvent_",
                                  "event, ");
                argIndex++;
            }
            fprintf(out, "    const int ret = AStatsEvent_write(event);\n");
            fprintf(out, "    AStatsEvent_release(event);\n");
            fprintf(out, "    return ret;\n");
        }
        fprintf(out, "}\n\n");
    }
    return 0;
}

static void write_native_stats_write_non_chained_methods(FILE* out, const Atoms& atoms,
                                                         const AtomDecl& attributionDecl) {
    fprintf(out, "\n");
    for (auto signature_it = atoms.nonChainedSignatureInfoMap.begin();
         signature_it != atoms.nonChainedSignatureInfoMap.end(); signature_it++) {
        vector<java_type_t> signature = signature_it->first;
        // Key value pairs not supported in native.
        if (find(signature.begin(), signature.end(), JAVA_TYPE_KEY_VALUE_PAIR) != signature.end()) {
            continue;
        }

        write_native_method_signature(out, "int stats_write_non_chained", signature,
                                      attributionDecl, " {");

        vector<java_type_t> newSignature;

        // First two args form the attribution node so size goes down by 1.
        newSignature.reserve(signature.size() - 1);

        // First arg is Attribution Chain.
        newSignature.push_back(JAVA_TYPE_ATTRIBUTION_CHAIN);

        // Followed by the originial signature except the first 2 args.
        newSignature.insert(newSignature.end(), signature.begin() + 2, signature.end());

        const char* uidName = attributionDecl.fields.front().name.c_str();
        const char* tagName = attributionDecl.fields.back().name.c_str();
        fprintf(out, "    const int32_t* %s = &arg1;\n", uidName);
        fprintf(out, "    const size_t %s_length = 1;\n", uidName);
        fprintf(out, "    const std::vector<char const*> %s(1, arg2);\n", tagName);
        fprintf(out, "    return ");
        write_native_method_call(out, "stats_write", newSignature, attributionDecl, 2);

        fprintf(out, "}\n\n");
    }
}

static void write_native_method_header(
        FILE* out, const string& methodName,
        const map<vector<java_type_t>, FieldNumberToAnnotations>& signatureInfoMap,
        const AtomDecl& attributionDecl) {
    for (auto signatureInfoMapIt = signatureInfoMap.begin();
         signatureInfoMapIt != signatureInfoMap.end(); signatureInfoMapIt++) {
        vector<java_type_t> signature = signatureInfoMapIt->first;

        // Key value pairs not supported in native.
        if (find(signature.begin(), signature.end(), JAVA_TYPE_KEY_VALUE_PAIR) != signature.end()) {
            continue;
        }
        write_native_method_signature(out, methodName, signature, attributionDecl, ";");
    }
}

int write_stats_log_cpp(FILE* out, const Atoms& atoms, const AtomDecl& attributionDecl,
                        const string& cppNamespace, const string& importHeader,
                        const bool supportQ) {
    // Print prelude
    fprintf(out, "// This file is autogenerated\n");
    fprintf(out, "\n");

    fprintf(out, "#include <%s>\n", importHeader.c_str());
    if (supportQ) {
        fprintf(out, "#include <StatsEventCompat.h>\n");
    } else {
        fprintf(out, "#include <stats_event.h>\n");
    }

    fprintf(out, "\n");
    write_namespace(out, cppNamespace);

    write_native_stats_write_methods(out, atoms, attributionDecl, supportQ);
    write_native_stats_write_non_chained_methods(out, atoms, attributionDecl);

    // Print footer
    fprintf(out, "\n");
    write_closing_namespace(out, cppNamespace);

    return 0;
}

int write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl& attributionDecl,
                           const string& cppNamespace) {
    // Print prelude
    fprintf(out, "// This file is autogenerated\n");
    fprintf(out, "\n");
    fprintf(out, "#pragma once\n");
    fprintf(out, "\n");
    fprintf(out, "#include <stdint.h>\n");
    fprintf(out, "#include <vector>\n");
    fprintf(out, "#include <map>\n");
    fprintf(out, "#include <set>\n");
    fprintf(out, "\n");

    write_namespace(out, cppNamespace);
    fprintf(out, "\n");
    fprintf(out, "/*\n");
    fprintf(out, " * API For logging statistics events.\n");
    fprintf(out, " */\n");
    fprintf(out, "\n");

    write_native_atom_constants(out, atoms, attributionDecl);

    // Print constants for the enum values.
    fprintf(out, "//\n");
    fprintf(out, "// Constants for enum values\n");
    fprintf(out, "//\n\n");
    for (set<AtomDecl>::const_iterator atom = atoms.decls.begin(); atom != atoms.decls.end();
         atom++) {
        for (vector<AtomField>::const_iterator field = atom->fields.begin();
             field != atom->fields.end(); field++) {
            if (field->javaType == JAVA_TYPE_ENUM) {
                fprintf(out, "// Values for %s.%s\n", atom->message.c_str(), field->name.c_str());
                for (map<int, string>::const_iterator value = field->enumValues.begin();
                     value != field->enumValues.end(); value++) {
                    fprintf(out, "const int32_t %s__%s__%s = %d;\n",
                            make_constant_name(atom->message).c_str(),
                            make_constant_name(field->name).c_str(),
                            make_constant_name(value->second).c_str(), value->first);
                }
                fprintf(out, "\n");
            }
        }
    }

    fprintf(out, "struct BytesField {\n");
    fprintf(out,
            "  BytesField(char const* array, size_t len) : arg(array), "
            "arg_length(len) {}\n");
    fprintf(out, "  char const* arg;\n");
    fprintf(out, "  size_t arg_length;\n");
    fprintf(out, "};\n");
    fprintf(out, "\n");

    // Print write methods
    fprintf(out, "//\n");
    fprintf(out, "// Write methods\n");
    fprintf(out, "//\n");
    write_native_method_header(out, "int stats_write", atoms.signatureInfoMap, attributionDecl);

    fprintf(out, "//\n");
    fprintf(out, "// Write flattened methods\n");
    fprintf(out, "//\n");
    write_native_method_header(out, "int stats_write_non_chained", atoms.nonChainedSignatureInfoMap,
                               attributionDecl);

    fprintf(out, "\n");
    write_closing_namespace(out, cppNamespace);

    return 0;
}

}  // namespace stats_log_api_gen
}  // namespace android