blob: 292cb21bac304069e220522bee69d48d3bb56cbc [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
Muhammad Qureshicdf291d2020-04-15 17:13:12 -070028static void write_atoms_info_header_body(FILE* out) {
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> kWhitelistedAtoms;\n");
Muhammad Qureshic8e22662019-11-20 17:18:03 -080031 fprintf(out, "};\n");
Muhammad Qureshic8e22662019-11-20 17:18:03 -080032}
33
34static void write_atoms_info_cpp_body(FILE* out, const Atoms& atoms) {
Muhammad Qureshic8e22662019-11-20 17:18:03 -080035
Muhammad Qureshia345af92020-03-24 17:05:14 -070036 fprintf(out, "const std::set<int> AtomsInfo::kWhitelistedAtoms = {\n");
Muhammad Qureshic6c38632020-03-25 17:45:01 -070037 for (AtomDeclSet::const_iterator atomIt = atoms.decls.begin(); atomIt != atoms.decls.end();
38 atomIt++) {
39 if ((*atomIt)->whitelisted) {
40 const string constant = make_constant_name((*atomIt)->name);
41 fprintf(out, " %d, // %s\n", (*atomIt)->code, constant.c_str());
Muhammad Qureshic8e22662019-11-20 17:18:03 -080042 }
43 }
44
45 fprintf(out, "};\n");
46 fprintf(out, "\n");
47
Muhammad Qureshic8e22662019-11-20 17:18:03 -080048}
49
Muhammad Qureshicdf291d2020-04-15 17:13:12 -070050int write_atoms_info_header(FILE* out, const string& namespaceStr) {
Muhammad Qureshic8e22662019-11-20 17:18:03 -080051 // Print prelude
52 fprintf(out, "// This file is autogenerated\n");
53 fprintf(out, "\n");
54 fprintf(out, "#pragma once\n");
55 fprintf(out, "\n");
56 fprintf(out, "#include <vector>\n");
57 fprintf(out, "#include <map>\n");
58 fprintf(out, "#include <set>\n");
59 fprintf(out, "\n");
60
61 write_namespace(out, namespaceStr);
62
Muhammad Qureshicdf291d2020-04-15 17:13:12 -070063 write_atoms_info_header_body(out);
Muhammad Qureshic8e22662019-11-20 17:18:03 -080064
65 fprintf(out, "\n");
66 write_closing_namespace(out, namespaceStr);
67
68 return 0;
69}
70
Muhammad Qureshia345af92020-03-24 17:05:14 -070071int write_atoms_info_cpp(FILE* out, const Atoms& atoms, const string& namespaceStr,
72 const string& importHeader) {
Muhammad Qureshic8e22662019-11-20 17:18:03 -080073 // Print prelude
74 fprintf(out, "// This file is autogenerated\n");
75 fprintf(out, "\n");
76 fprintf(out, "#include <%s>\n", importHeader.c_str());
Muhammad Qureshic8e22662019-11-20 17:18:03 -080077 fprintf(out, "\n");
78
79 write_namespace(out, namespaceStr);
80
81 write_atoms_info_cpp_body(out, atoms);
82
83 // Print footer
84 fprintf(out, "\n");
85 write_closing_namespace(out, namespaceStr);
86
87 return 0;
88}
89
90} // namespace stats_log_api_gen
91} // namespace android