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 | |
Muhammad Qureshi | cdf291d | 2020-04-15 17:13:12 -0700 | [diff] [blame] | 28 | static void write_atoms_info_header_body(FILE* out) { |
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> kWhitelistedAtoms;\n"); |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 31 | fprintf(out, "};\n"); |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static void write_atoms_info_cpp_body(FILE* out, const Atoms& atoms) { |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 35 | |
Muhammad Qureshi | a345af9 | 2020-03-24 17:05:14 -0700 | [diff] [blame] | 36 | fprintf(out, "const std::set<int> AtomsInfo::kWhitelistedAtoms = {\n"); |
Muhammad Qureshi | c6c3863 | 2020-03-25 17:45:01 -0700 | [diff] [blame] | 37 | 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 Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
| 45 | fprintf(out, "};\n"); |
| 46 | fprintf(out, "\n"); |
| 47 | |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Muhammad Qureshi | cdf291d | 2020-04-15 17:13:12 -0700 | [diff] [blame] | 50 | int write_atoms_info_header(FILE* out, const string& namespaceStr) { |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 51 | // 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 Qureshi | cdf291d | 2020-04-15 17:13:12 -0700 | [diff] [blame] | 63 | write_atoms_info_header_body(out); |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 64 | |
| 65 | fprintf(out, "\n"); |
| 66 | write_closing_namespace(out, namespaceStr); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
Muhammad Qureshi | a345af9 | 2020-03-24 17:05:14 -0700 | [diff] [blame] | 71 | int write_atoms_info_cpp(FILE* out, const Atoms& atoms, const string& namespaceStr, |
| 72 | const string& importHeader) { |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 73 | // Print prelude |
| 74 | fprintf(out, "// This file is autogenerated\n"); |
| 75 | fprintf(out, "\n"); |
| 76 | fprintf(out, "#include <%s>\n", importHeader.c_str()); |
Muhammad Qureshi | c8e2266 | 2019-11-20 17:18:03 -0800 | [diff] [blame] | 77 | 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 |