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 const char ANNOTATION_CONSTANT_NAME_PREFIX[] = "ANNOTATION_ID_"; 48 const char ANNOTATION_CONSTANT_NAME_VENDOR_PREFIX[] = "AnnotationId."; 49 const char ANNOTATION_CONSTANT_NAME_VENDOR_NATIVE_PREFIX[] = "AnnotationId::"; 50 51 struct AnnotationStruct { 52 string name; 53 int minApiLevel; AnnotationStructAnnotationStruct54 AnnotationStruct(string name, int minApiLevel) 55 : name(std::move(name)), minApiLevel(minApiLevel){}; 56 }; 57 58 void build_non_chained_decl_map(const Atoms& atoms, 59 std::map<int, AtomDeclSet::const_iterator>* decl_map); 60 61 const map<AnnotationId, AnnotationStruct>& get_annotation_id_constants(const string& prefix); 62 63 string get_java_build_version_code(int apiLevel); 64 65 string get_restriction_category_str(int annotationValue); 66 67 string make_constant_name(const string& str); 68 69 const char* cpp_type_name(java_type_t type, bool isVendorAtomLogging = false); 70 71 const char* java_type_name(java_type_t type); 72 73 bool is_repeated_field(java_type_t type); 74 75 bool is_primitive_field(java_type_t type); 76 77 AtomDeclSet get_annotations(int argIndex, const FieldNumberToAtomDeclSet& fieldNumberToAtomDeclSet); 78 79 // Common Native helpers 80 void write_namespace(FILE* out, const string& cppNamespaces); 81 82 void write_closing_namespace(FILE* out, const string& cppNamespaces); 83 84 void write_native_atom_constants(FILE* out, const Atoms& atoms, const AtomDecl& attributionDecl, 85 const string& methodName = "stats_write", 86 bool isVendorAtomLogging = false); 87 88 void write_native_atom_enums(FILE* out, const Atoms& atoms); 89 90 void write_native_method_signature(FILE* out, const string& signaturePrefix, 91 const vector<java_type_t>& signature, 92 const AtomDecl& attributionDecl, const string& closer, 93 bool isVendorAtomLogging = false); 94 95 void write_native_method_header(FILE* out, const string& methodName, 96 const SignatureInfoMap& signatureInfoMap, 97 const AtomDecl& attributionDecl, bool isVendorAtomLogging = false); 98 99 void write_native_header_preamble(FILE* out, const string& cppNamespace, bool includePull, 100 bool isVendorAtomLogging = false); 101 102 void write_native_header_epilogue(FILE* out, const string& cppNamespace); 103 104 // Common Java helpers. 105 void write_java_atom_codes(FILE* out, const Atoms& atoms); 106 107 void write_java_enum_values(FILE* out, const Atoms& atoms); 108 109 int write_java_method_signature(FILE* out, const vector<java_type_t>& signature, 110 const AtomDecl& attributionDecl); 111 112 void write_java_usage(FILE* out, const string& method_name, const string& atom_code_name, 113 const AtomDecl& atom); 114 115 int write_java_non_chained_methods(FILE* out, const SignatureInfoMap& signatureInfoMap); 116 117 int write_java_work_source_methods(FILE* out, const SignatureInfoMap& signatureInfoMap); 118 119 class MFErrorCollector : public google::protobuf::compiler::MultiFileErrorCollector { 120 public: AddError(const std::string & filename,int line,int column,const std::string & message)121 void AddError(const std::string& filename, int line, int column, 122 const std::string& message) override { 123 fprintf(stderr, "[Error] %s:%d:%d - %s\n", filename.c_str(), line, column, message.c_str()); 124 } 125 }; 126 127 int get_max_requires_api_level(int minApiLevel, const AtomDeclSet* atomDeclSet, 128 const vector<java_type_t>& signature); 129 130 } // namespace stats_log_api_gen 131 } // namespace android 132 133 #endif // ANDROID_STATS_LOG_API_GEN_UTILS_H 134