blob: b33995017bae515dc9f45424a73690eb09ed5d98 [file] [log] [blame]
Muhammad Qureshic8e22662019-11-20 17:18:03 -08001/*
2 * Copyright (C) 2019, 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 "atoms_info_writer.h"
Muhammad Qureshic8e22662019-11-20 17:18:03 -080018
19#include <map>
20#include <set>
21#include <vector>
22
Muhammad Qureshia345af92020-03-24 17:05:14 -070023#include "utils.h"
24
Muhammad Qureshic8e22662019-11-20 17:18:03 -080025namespace android {
26namespace stats_log_api_gen {
27
28static void write_atoms_info_header_body(FILE* out, const Atoms& atoms) {
Muhammad Qureshic8e22662019-11-20 17:18:03 -080029 fprintf(out, "struct AtomsInfo {\n");
Muhammad Qureshia345af92020-03-24 17:05:14 -070030 fprintf(out, " const static std::set<int> kAtomsWithAttributionChain;\n");
Muhammad Qureshia345af92020-03-24 17:05:14 -070031 fprintf(out, " const static std::set<int> kWhitelistedAtoms;\n");
Muhammad Qureshic8e22662019-11-20 17:18:03 -080032 fprintf(out, "};\n");
33 fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n", atoms.maxPushedAtomId);
Muhammad Qureshic8e22662019-11-20 17:18:03 -080034}
35
36static void write_atoms_info_cpp_body(FILE* out, const Atoms& atoms) {
Muhammad Qureshic8e22662019-11-20 17:18:03 -080037
Muhammad Qureshia345af92020-03-24 17:05:14 -070038 fprintf(out, "const std::set<int> AtomsInfo::kAtomsWithAttributionChain = {\n");
Muhammad Qureshic6c38632020-03-25 17:45:01 -070039 for (AtomDeclSet::const_iterator atomIt = atoms.decls.begin(); atomIt != atoms.decls.end();
40 atomIt++) {
41 for (vector<AtomField>::const_iterator field = (*atomIt)->fields.begin();
42 field != (*atomIt)->fields.end(); field++) {
Muhammad Qureshic8e22662019-11-20 17:18:03 -080043 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
Muhammad Qureshic6c38632020-03-25 17:45:01 -070044 const string constant = make_constant_name((*atomIt)->name);
45 fprintf(out, " %d, // %s\n", (*atomIt)->code, constant.c_str());
Muhammad Qureshic8e22662019-11-20 17:18:03 -080046 break;
47 }
48 }
49 }
50
51 fprintf(out, "};\n");
52 fprintf(out, "\n");
53
Muhammad Qureshia345af92020-03-24 17:05:14 -070054 fprintf(out, "const std::set<int> AtomsInfo::kWhitelistedAtoms = {\n");
Muhammad Qureshic6c38632020-03-25 17:45:01 -070055 for (AtomDeclSet::const_iterator atomIt = atoms.decls.begin(); atomIt != atoms.decls.end();
56 atomIt++) {
57 if ((*atomIt)->whitelisted) {
58 const string constant = make_constant_name((*atomIt)->name);
59 fprintf(out, " %d, // %s\n", (*atomIt)->code, constant.c_str());
Muhammad Qureshic8e22662019-11-20 17:18:03 -080060 }
61 }
62
63 fprintf(out, "};\n");
64 fprintf(out, "\n");
65
Muhammad Qureshic8e22662019-11-20 17:18:03 -080066}
67
Muhammad Qureshia345af92020-03-24 17:05:14 -070068int write_atoms_info_header(FILE* out, const Atoms& atoms, const string& namespaceStr) {
Muhammad Qureshic8e22662019-11-20 17:18:03 -080069 // Print prelude
70 fprintf(out, "// This file is autogenerated\n");
71 fprintf(out, "\n");
72 fprintf(out, "#pragma once\n");
73 fprintf(out, "\n");
74 fprintf(out, "#include <vector>\n");
75 fprintf(out, "#include <map>\n");
76 fprintf(out, "#include <set>\n");
77 fprintf(out, "\n");
78
79 write_namespace(out, namespaceStr);
80
81 write_atoms_info_header_body(out, atoms);
82
83 fprintf(out, "\n");
84 write_closing_namespace(out, namespaceStr);
85
86 return 0;
87}
88
Muhammad Qureshia345af92020-03-24 17:05:14 -070089int write_atoms_info_cpp(FILE* out, const Atoms& atoms, const string& namespaceStr,
90 const string& importHeader) {
Muhammad Qureshic8e22662019-11-20 17:18:03 -080091 // Print prelude
92 fprintf(out, "// This file is autogenerated\n");
93 fprintf(out, "\n");
94 fprintf(out, "#include <%s>\n", importHeader.c_str());
Muhammad Qureshic8e22662019-11-20 17:18:03 -080095 fprintf(out, "\n");
96
97 write_namespace(out, namespaceStr);
98
99 write_atoms_info_cpp_body(out, atoms);
100
101 // Print footer
102 fprintf(out, "\n");
103 write_closing_namespace(out, namespaceStr);
104
105 return 0;
106}
107
108} // namespace stats_log_api_gen
109} // namespace android