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 #ifndef ANDROID_STATS_LOG_API_GEN_UTILS_H 18 #define ANDROID_STATS_LOG_API_GEN_UTILS_H 19 20 #include <google/protobuf/compiler/importer.h> 21 #include <stdio.h> 22 #include <string.h> 23 24 #include <map> 25 #include <set> 26 #include <vector> 27 28 #include "Collation.h" 29 30 namespace android { 31 namespace stats_log_api_gen { 32 33 const char DEFAULT_CPP_NAMESPACE[] = "android,util"; 34 const char DEFAULT_CPP_HEADER_IMPORT[] = "statslog.h"; 35 36 const int API_LEVEL_CURRENT = 10000; 37 const int API_Q = 29; 38 const int API_R = 30; 39 const int API_S = 31; 40 const int API_S_V2 = 32; 41 const int API_T = 33; 42 const int API_U = 34; 43 44 const int JAVA_MODULE_REQUIRES_FLOAT = 0x01; 45 const int JAVA_MODULE_REQUIRES_ATTRIBUTION = 0x02; 46 47 struct AnnotationStruct { 48 string name; 49 int minApiLevel; AnnotationStructAnnotationStruct50 AnnotationStruct(string name, int minApiLevel) 51 : name(std::move(name)), minApiLevel(minApiLevel){}; 52 }; 53 54 void build_non_chained_decl_map(const Atoms& atoms, 55 std::map<int, AtomDeclSet::const_iterator>* decl_map); 56 57 const map<AnnotationId, AnnotationStruct>& get_annotation_id_constants(); 58 59 string get_java_build_version_code(int minApiLevel); 60 61 string get_restriction_category_str(int annotationValue); 62 63 string make_constant_name(const string& str); 64 65 const char* cpp_type_name(java_type_t type, bool isVendorAtomLogging = false); 66 67 const char* java_type_name(java_type_t type); 68 69 bool is_repeated_field(java_type_t type); 70 71 bool is_primitive_field(java_type_t type); 72 73 // Common Native helpers 74 void write_namespace(FILE* out, const string& cppNamespaces); 75 76 void write_closing_namespace(FILE* out, const string& cppNamespaces); 77 78 void write_native_atom_constants(FILE* out, const Atoms& atoms, const AtomDecl& attributionDecl, 79 const string& methodName = "stats_write", 80 bool isVendorAtomLogging = false); 81 82 void write_native_atom_enums(FILE* out, const Atoms& atoms); 83 84 void write_native_method_signature(FILE* out, const string& signaturePrefix, 85 const vector<java_type_t>& signature, 86 const AtomDecl& attributionDecl, const string& closer, 87 bool isVendorAtomLogging = false); 88 89 void write_native_method_header(FILE* out, const string& methodName, 90 const SignatureInfoMap& signatureInfoMap, 91 const AtomDecl& attributionDecl, bool isVendorAtomLogging = false); 92 93 void write_native_header_preamble(FILE* out, const string& cppNamespace, bool includePull, 94 bool isVendorAtomLogging = false); 95 96 void write_native_header_epilogue(FILE* out, const string& cppNamespace); 97 98 // Common Java helpers. 99 void write_java_atom_codes(FILE* out, const Atoms& atoms); 100 101 void write_java_enum_values(FILE* out, const Atoms& atoms); 102 103 void write_java_enum_values_vendor(FILE* out, const Atoms& atoms); 104 105 void write_java_usage(FILE* out, const string& method_name, const string& atom_code_name, 106 const AtomDecl& atom); 107 108 int write_java_non_chained_methods(FILE* out, const SignatureInfoMap& signatureInfoMap); 109 110 int write_java_work_source_methods(FILE* out, const SignatureInfoMap& signatureInfoMap); 111 112 class MFErrorCollector : public google::protobuf::compiler::MultiFileErrorCollector { 113 public: AddError(const std::string & filename,int line,int column,const std::string & message)114 void AddError(const std::string& filename, int line, int column, 115 const std::string& message) override { 116 fprintf(stderr, "[Error] %s:%d:%d - %s\n", filename.c_str(), line, column, message.c_str()); 117 } 118 }; 119 120 bool contains_restricted(const AtomDeclSet& atomDeclSet); 121 122 bool requires_api_needed(const AtomDeclSet& atomDeclSet); 123 124 int get_min_api_level(const AtomDeclSet& atomDeclSet); 125 126 } // namespace stats_log_api_gen 127 } // namespace android 128 129 #endif // ANDROID_STATS_LOG_API_GEN_UTILS_H 130