• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 const string HISTOGRAM_STEM("StatsHistogram");
51 
52 struct AnnotationStruct {
53     string name;
54     int minApiLevel;
AnnotationStructAnnotationStruct55     AnnotationStruct(string name, int minApiLevel)
56         : name(std::move(name)), minApiLevel(minApiLevel){};
57 };
58 
59 void build_non_chained_decl_map(const Atoms& atoms,
60                                 std::map<int, AtomDeclSet::const_iterator>* decl_map);
61 
62 const map<AnnotationId, AnnotationStruct>& get_annotation_id_constants(const string& prefix);
63 
64 string get_java_build_version_code(int apiLevel);
65 
66 string get_restriction_category_str(int annotationValue);
67 
68 string make_constant_name(const string& str);
69 
70 string snake_to_pascal(const string& snake);
71 
72 const char* cpp_type_name(java_type_t type, bool isVendorAtomLogging = false);
73 
74 const char* java_type_name(java_type_t type);
75 
76 bool is_repeated_field(java_type_t type);
77 
78 bool is_primitive_field(java_type_t type);
79 
80 AtomDeclSet get_annotations(int argIndex, const FieldNumberToAtomDeclSet& fieldNumberToAtomDeclSet);
81 
82 // Common Native helpers
83 void write_namespace(FILE* out, const string& cppNamespaces);
84 
85 void write_closing_namespace(FILE* out, const string& cppNamespaces);
86 
87 void write_native_atom_constants(FILE* out, const Atoms& atoms, const AtomDecl& attributionDecl,
88                                  const string& methodName = "stats_write",
89                                  bool isVendorAtomLogging = false);
90 
91 void write_native_atom_enums(FILE* out, const Atoms& atoms);
92 
93 void write_native_method_signature(FILE* out, const string& signaturePrefix,
94                                    const vector<java_type_t>& signature,
95                                    const AtomDecl& attributionDecl, const string& closer,
96                                    bool isVendorAtomLogging = false);
97 
98 void write_native_method_header(FILE* out, const string& methodName,
99                                 const SignatureInfoMap& signatureInfoMap,
100                                 const AtomDecl& attributionDecl, bool isVendorAtomLogging = false);
101 
102 void write_native_header_preamble(FILE* out, const string& cppNamespace, bool includePull,
103                                   bool hasHistograms, bool bootstrap,
104                                   bool isVendorAtomLogging = false);
105 
106 void write_native_header_epilogue(FILE* out, const string& cppNamespace);
107 
108 // Common Java helpers.
109 void write_java_atom_codes(FILE* out, const Atoms& atoms);
110 
111 void write_java_enum_values(FILE* out, const Atoms& atoms);
112 
113 int write_java_method_signature(FILE* out, const vector<java_type_t>& signature,
114                                 const AtomDecl& attributionDecl);
115 
116 void write_java_usage(FILE* out, const string& method_name, const string& atom_code_name,
117                       const AtomDecl& atom);
118 
119 int write_java_non_chained_methods(FILE* out, const SignatureInfoMap& signatureInfoMap,
120                                    const bool staticMethods);
121 
122 int write_java_work_source_methods(FILE* out, const SignatureInfoMap& signatureInfoMap);
123 
124 class MFErrorCollector : public google::protobuf::compiler::MultiFileErrorCollector {
125 public:
AddError(const std::string & filename,int line,int column,const std::string & message)126     void AddError(const std::string& filename, int line, int column,
127                   const std::string& message) override {
128         fprintf(stderr, "[Error] %s:%d:%d - %s\n", filename.c_str(), line, column, message.c_str());
129     }
130 };
131 
132 int get_max_requires_api_level(int minApiLevel, const AtomDeclSet* atomDeclSet,
133                                const vector<java_type_t>& signature);
134 
135 bool has_histograms(const AtomDeclSet& decls);
136 
137 void write_native_histogram_helper_declarations(FILE* out, const AtomDeclSet& atomDeclSet);
138 
139 int write_native_histogram_helper_definitions(FILE* out, const AtomDeclSet& atomDeclSet);
140 
141 int write_srcs_header(FILE* out, const char* path, const std::vector<std::string>& excludeList);
142 
143 int write_java_srcs_classes(FILE* out, const char* path,
144                             const std::vector<std::string>& excludeList);
145 
146 int write_cc_srcs_classes(FILE* out, const char* path, const std::vector<std::string>& excludeList);
147 
148 int write_java_histogram_helpers(FILE* out, const AtomDeclSet& atomDeclSet,
149                                  const bool staticMethods);
150 }  // namespace stats_log_api_gen
151 }  // namespace android
152 
153 #endif  // ANDROID_STATS_LOG_API_GEN_UTILS_H
154