Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 1 | /* |
| 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 Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 18 | |
| 19 | #include <map> |
| 20 | #include <set> |
| 21 | #include <vector> |
| 22 | |
Muhammad Qureshi | a345af9 | 2020-03-24 17:05:14 -0700 | [diff] [blame] | 23 | #include "utils.h" |
| 24 | |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace stats_log_api_gen { |
| 27 | |
| 28 | static void write_atoms_info_header_body(FILE* out, const Atoms& atoms) { |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 29 | fprintf(out, "struct AtomsInfo {\n"); |
Muhammad Qureshi | a345af9 | 2020-03-24 17:05:14 -0700 | [diff] [blame] | 30 | fprintf(out, " const static std::set<int> kAtomsWithAttributionChain;\n"); |
Muhammad Qureshi | a345af9 | 2020-03-24 17:05:14 -0700 | [diff] [blame] | 31 | fprintf(out, " const static std::set<int> kWhitelistedAtoms;\n"); |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 32 | fprintf(out, "};\n"); |
| 33 | fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n", atoms.maxPushedAtomId); |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | static void write_atoms_info_cpp_body(FILE* out, const Atoms& atoms) { |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 37 | |
Muhammad Qureshi | a345af9 | 2020-03-24 17:05:14 -0700 | [diff] [blame] | 38 | fprintf(out, "const std::set<int> AtomsInfo::kAtomsWithAttributionChain = {\n"); |
Muhammad Qureshi | c6c3863 | 2020-03-25 17:45:01 -0700 | [diff] [blame] | 39 | 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 Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 43 | if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) { |
Muhammad Qureshi | c6c3863 | 2020-03-25 17:45:01 -0700 | [diff] [blame] | 44 | const string constant = make_constant_name((*atomIt)->name); |
| 45 | fprintf(out, " %d, // %s\n", (*atomIt)->code, constant.c_str()); |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 46 | break; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | fprintf(out, "};\n"); |
| 52 | fprintf(out, "\n"); |
| 53 | |
Muhammad Qureshi | a345af9 | 2020-03-24 17:05:14 -0700 | [diff] [blame] | 54 | fprintf(out, "const std::set<int> AtomsInfo::kWhitelistedAtoms = {\n"); |
Muhammad Qureshi | c6c3863 | 2020-03-25 17:45:01 -0700 | [diff] [blame] | 55 | 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 Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | fprintf(out, "};\n"); |
| 64 | fprintf(out, "\n"); |
| 65 | |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Muhammad Qureshi | a345af9 | 2020-03-24 17:05:14 -0700 | [diff] [blame] | 68 | int write_atoms_info_header(FILE* out, const Atoms& atoms, const string& namespaceStr) { |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 69 | // 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 Qureshi | a345af9 | 2020-03-24 17:05:14 -0700 | [diff] [blame] | 89 | int write_atoms_info_cpp(FILE* out, const Atoms& atoms, const string& namespaceStr, |
| 90 | const string& importHeader) { |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 91 | // Print prelude |
| 92 | fprintf(out, "// This file is autogenerated\n"); |
| 93 | fprintf(out, "\n"); |
| 94 | fprintf(out, "#include <%s>\n", importHeader.c_str()); |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 95 | 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 |