diff options
Diffstat (limited to 'tools/stats_log_api_gen/java_writer.cpp')
-rw-r--r-- | tools/stats_log_api_gen/java_writer.cpp | 290 |
1 files changed, 139 insertions, 151 deletions
diff --git a/tools/stats_log_api_gen/java_writer.cpp b/tools/stats_log_api_gen/java_writer.cpp index 1a7efba1e829..5a22b5c4cd6b 100644 --- a/tools/stats_log_api_gen/java_writer.cpp +++ b/tools/stats_log_api_gen/java_writer.cpp @@ -15,6 +15,7 @@ */ #include "java_writer.h" + #include "java_writer_q.h" #include "utils.h" @@ -22,9 +23,8 @@ namespace android { namespace stats_log_api_gen { static int write_java_q_logger_class( - FILE* out, - const map<vector<java_type_t>, FieldNumberToAnnotations>& signatureInfoMap, - const AtomDecl &attributionDecl) { + FILE* out, const map<vector<java_type_t>, FieldNumberToAnnotations>& signatureInfoMap, + const AtomDecl& attributionDecl) { fprintf(out, "\n"); fprintf(out, " // Write logging helper methods for statsd in Q and earlier.\n"); fprintf(out, " private static class QLogger {\n"); @@ -34,29 +34,27 @@ static int write_java_q_logger_class( // Print Q write methods. fprintf(out, "\n"); fprintf(out, " // Write methods.\n"); - write_java_methods_q_schema( - out, signatureInfoMap, attributionDecl, " "); + write_java_methods_q_schema(out, signatureInfoMap, attributionDecl, " "); fprintf(out, " }\n"); return 0; } -static void write_annotations( - FILE* out, int argIndex, - const FieldNumberToAnnotations& fieldNumberToAnnotations) { +static void write_annotations(FILE* out, int argIndex, + const FieldNumberToAnnotations& fieldNumberToAnnotations) { auto it = fieldNumberToAnnotations.find(argIndex); if (it == fieldNumberToAnnotations.end()) { return; } const set<shared_ptr<Annotation>>& annotations = it->second; - for (auto& annotation: annotations) { + for (auto& annotation : annotations) { // TODO(b/151744250): Group annotations for same atoms. // TODO(b/151786433): Write atom constant name instead of atom id literal. fprintf(out, " if (code == %d) {\n", annotation->atomId); - switch(annotation->type) { + switch (annotation->type) { case ANNOTATION_TYPE_INT: - // TODO(b/151776731): Check for reset state annotation and only include reset state - // when field value == default state annotation value. + // TODO(b/151776731): Check for reset state annotation and only include + // reset state when field value == default state annotation value. // TODO(b/151786433): Write annotation constant name instead of // annotation id literal. fprintf(out, " builder.addIntAnnotation((byte) %d, %d);\n", @@ -66,8 +64,7 @@ static void write_annotations( // TODO(b/151786433): Write annotation constant name instead of // annotation id literal. fprintf(out, " builder.addBooleanAnnotation((byte) %d, %s);\n", - annotation->annotationId, - annotation->value.boolValue ? "true" : "false"); + annotation->annotationId, annotation->value.boolValue ? "true" : "false"); break; default: break; @@ -77,24 +74,21 @@ static void write_annotations( } static int write_java_methods( - FILE* out, - const map<vector<java_type_t>, FieldNumberToAnnotations>& signatureInfoMap, - const AtomDecl &attributionDecl, - const bool supportQ - ) { + FILE* out, const map<vector<java_type_t>, FieldNumberToAnnotations>& signatureInfoMap, + const AtomDecl& attributionDecl, const bool supportQ) { for (auto signatureInfoMapIt = signatureInfoMap.begin(); - signatureInfoMapIt != signatureInfoMap.end(); signatureInfoMapIt++) { + signatureInfoMapIt != signatureInfoMap.end(); signatureInfoMapIt++) { // Print method signature. fprintf(out, " public static void write(int code"); const vector<java_type_t>& signature = signatureInfoMapIt->first; const FieldNumberToAnnotations& fieldNumberToAnnotations = signatureInfoMapIt->second; int argIndex = 1; - for (vector<java_type_t>::const_iterator arg = signature.begin(); - arg != signature.end(); arg++) { + for (vector<java_type_t>::const_iterator arg = signature.begin(); arg != signature.end(); + arg++) { if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { for (auto chainField : attributionDecl.fields) { - fprintf(out, ", %s[] %s", - java_type_name(chainField.javaType), chainField.name.c_str()); + fprintf(out, ", %s[] %s", java_type_name(chainField.javaType), + chainField.name.c_str()); } } else if (*arg == JAVA_TYPE_KEY_VALUE_PAIR) { fprintf(out, ", android.util.SparseArray<Object> valueMap"); @@ -108,15 +102,20 @@ static int write_java_methods( // Print method body. string indent(""); if (supportQ) { - // TODO(b/146235828): Use just SDK_INT check once it is incremented from Q. + // TODO(b/146235828): Use just SDK_INT check once it is incremented from + // Q. fprintf(out, " if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q\n"); - fprintf(out, " || (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q\n"); + fprintf(out, + " || (Build.VERSION.SDK_INT == " + "Build.VERSION_CODES.Q\n"); fprintf(out, " && Build.VERSION.PREVIEW_SDK_INT > 0)) {\n"); indent = " "; } // Start StatsEvent.Builder. - fprintf(out, "%s final StatsEvent.Builder builder = StatsEvent.newBuilder();\n", + fprintf(out, + "%s final StatsEvent.Builder builder = " + "StatsEvent.newBuilder();\n", indent.c_str()); // Write atom code. @@ -125,118 +124,112 @@ static int write_java_methods( // Write the args. argIndex = 1; - for (vector<java_type_t>::const_iterator arg = signature.begin(); - arg != signature.end(); arg++) { + for (vector<java_type_t>::const_iterator arg = signature.begin(); arg != signature.end(); + arg++) { switch (*arg) { - case JAVA_TYPE_BOOLEAN: - fprintf(out, "%s builder.writeBoolean(arg%d);\n", indent.c_str(), argIndex); - break; - case JAVA_TYPE_INT: - case JAVA_TYPE_ENUM: - fprintf(out, "%s builder.writeInt(arg%d);\n", indent.c_str(), argIndex); - break; - case JAVA_TYPE_FLOAT: - fprintf(out, "%s builder.writeFloat(arg%d);\n", indent.c_str(), argIndex); - break; - case JAVA_TYPE_LONG: - fprintf(out, "%s builder.writeLong(arg%d);\n", indent.c_str(), argIndex); - break; - case JAVA_TYPE_STRING: - fprintf(out, "%s builder.writeString(arg%d);\n", indent.c_str(), argIndex); - break; - case JAVA_TYPE_BYTE_ARRAY: - fprintf(out, "%s builder.writeByteArray(null == arg%d ? new byte[0] : arg%d);\n", - indent.c_str(), argIndex, argIndex); - break; - case JAVA_TYPE_ATTRIBUTION_CHAIN: - { - const char* uidName = attributionDecl.fields.front().name.c_str(); - const char* tagName = attributionDecl.fields.back().name.c_str(); + case JAVA_TYPE_BOOLEAN: + fprintf(out, "%s builder.writeBoolean(arg%d);\n", indent.c_str(), + argIndex); + break; + case JAVA_TYPE_INT: + case JAVA_TYPE_ENUM: + fprintf(out, "%s builder.writeInt(arg%d);\n", indent.c_str(), argIndex); + break; + case JAVA_TYPE_FLOAT: + fprintf(out, "%s builder.writeFloat(arg%d);\n", indent.c_str(), + argIndex); + break; + case JAVA_TYPE_LONG: + fprintf(out, "%s builder.writeLong(arg%d);\n", indent.c_str(), argIndex); + break; + case JAVA_TYPE_STRING: + fprintf(out, "%s builder.writeString(arg%d);\n", indent.c_str(), + argIndex); + break; + case JAVA_TYPE_BYTE_ARRAY: + fprintf(out, + "%s builder.writeByteArray(null == arg%d ? new byte[0] : " + "arg%d);\n", + indent.c_str(), argIndex, argIndex); + break; + case JAVA_TYPE_ATTRIBUTION_CHAIN: { + const char* uidName = attributionDecl.fields.front().name.c_str(); + const char* tagName = attributionDecl.fields.back().name.c_str(); - fprintf(out, "%s builder.writeAttributionChain(\n", indent.c_str()); - fprintf(out, "%s null == %s ? new int[0] : %s,\n", - indent.c_str(), uidName, uidName); - fprintf(out, "%s null == %s ? new String[0] : %s);\n", - indent.c_str(), tagName, tagName); - break; - } - case JAVA_TYPE_KEY_VALUE_PAIR: - fprintf(out, "\n"); - fprintf(out, - "%s // Write KeyValuePairs.\n", indent.c_str()); - fprintf(out, - "%s final int count = valueMap.size();\n", indent.c_str()); - fprintf(out, - "%s android.util.SparseIntArray intMap = null;\n", - indent.c_str()); - fprintf(out, - "%s android.util.SparseLongArray longMap = null;\n", - indent.c_str()); - fprintf(out, - "%s android.util.SparseArray<String> stringMap = null;\n", - indent.c_str()); - fprintf(out, - "%s android.util.SparseArray<Float> floatMap = null;\n", - indent.c_str()); - fprintf(out, - "%s for (int i = 0; i < count; i++) {\n", indent.c_str()); - fprintf(out, - "%s final int key = valueMap.keyAt(i);\n", indent.c_str()); - fprintf(out, - "%s final Object value = valueMap.valueAt(i);\n", - indent.c_str()); - fprintf(out, - "%s if (value instanceof Integer) {\n", indent.c_str()); - fprintf(out, - "%s if (null == intMap) {\n", indent.c_str()); - fprintf(out, - "%s intMap = new android.util.SparseIntArray();\n", indent.c_str()); - fprintf(out, - "%s }\n", indent.c_str()); - fprintf(out, - "%s intMap.put(key, (Integer) value);\n", indent.c_str()); - fprintf(out, - "%s } else if (value instanceof Long) {\n", indent.c_str()); - fprintf(out, - "%s if (null == longMap) {\n", indent.c_str()); - fprintf(out, - "%s longMap = new android.util.SparseLongArray();\n", indent.c_str()); - fprintf(out, - "%s }\n", indent.c_str()); - fprintf(out, - "%s longMap.put(key, (Long) value);\n", indent.c_str()); - fprintf(out, - "%s } else if (value instanceof String) {\n", indent.c_str()); - fprintf(out, - "%s if (null == stringMap) {\n", indent.c_str()); - fprintf(out, - "%s stringMap = new android.util.SparseArray<>();\n", indent.c_str()); - fprintf(out, - "%s }\n", indent.c_str()); - fprintf(out, - "%s stringMap.put(key, (String) value);\n", indent.c_str()); - fprintf(out, - "%s } else if (value instanceof Float) {\n", indent.c_str()); - fprintf(out, - "%s if (null == floatMap) {\n", indent.c_str()); - fprintf(out, - "%s floatMap = new android.util.SparseArray<>();\n", indent.c_str()); - fprintf(out, - "%s }\n", indent.c_str()); - fprintf(out, - "%s floatMap.put(key, (Float) value);\n", indent.c_str()); - fprintf(out, - "%s }\n", indent.c_str()); - fprintf(out, - "%s }\n", indent.c_str()); - fprintf(out, - "%s builder.writeKeyValuePairs(" - "intMap, longMap, stringMap, floatMap);\n", indent.c_str()); - break; - default: - // Unsupported types: OBJECT, DOUBLE. - fprintf(stderr, "Encountered unsupported type."); - return 1; + fprintf(out, "%s builder.writeAttributionChain(\n", indent.c_str()); + fprintf(out, "%s null == %s ? new int[0] : %s,\n", + indent.c_str(), uidName, uidName); + fprintf(out, "%s null == %s ? new String[0] : %s);\n", + indent.c_str(), tagName, tagName); + break; + } + case JAVA_TYPE_KEY_VALUE_PAIR: + fprintf(out, "\n"); + fprintf(out, "%s // Write KeyValuePairs.\n", indent.c_str()); + fprintf(out, "%s final int count = valueMap.size();\n", indent.c_str()); + fprintf(out, "%s android.util.SparseIntArray intMap = null;\n", + indent.c_str()); + fprintf(out, "%s android.util.SparseLongArray longMap = null;\n", + indent.c_str()); + fprintf(out, "%s android.util.SparseArray<String> stringMap = null;\n", + indent.c_str()); + fprintf(out, "%s android.util.SparseArray<Float> floatMap = null;\n", + indent.c_str()); + fprintf(out, "%s for (int i = 0; i < count; i++) {\n", indent.c_str()); + fprintf(out, "%s final int key = valueMap.keyAt(i);\n", + indent.c_str()); + fprintf(out, "%s final Object value = valueMap.valueAt(i);\n", + indent.c_str()); + fprintf(out, "%s if (value instanceof Integer) {\n", indent.c_str()); + fprintf(out, "%s if (null == intMap) {\n", indent.c_str()); + fprintf(out, + "%s intMap = new " + "android.util.SparseIntArray();\n", + indent.c_str()); + fprintf(out, "%s }\n", indent.c_str()); + fprintf(out, "%s intMap.put(key, (Integer) value);\n", + indent.c_str()); + fprintf(out, "%s } else if (value instanceof Long) {\n", + indent.c_str()); + fprintf(out, "%s if (null == longMap) {\n", indent.c_str()); + fprintf(out, + "%s longMap = new " + "android.util.SparseLongArray();\n", + indent.c_str()); + fprintf(out, "%s }\n", indent.c_str()); + fprintf(out, "%s longMap.put(key, (Long) value);\n", + indent.c_str()); + fprintf(out, "%s } else if (value instanceof String) {\n", + indent.c_str()); + fprintf(out, "%s if (null == stringMap) {\n", indent.c_str()); + fprintf(out, + "%s stringMap = new " + "android.util.SparseArray<>();\n", + indent.c_str()); + fprintf(out, "%s }\n", indent.c_str()); + fprintf(out, "%s stringMap.put(key, (String) value);\n", + indent.c_str()); + fprintf(out, "%s } else if (value instanceof Float) {\n", + indent.c_str()); + fprintf(out, "%s if (null == floatMap) {\n", indent.c_str()); + fprintf(out, + "%s floatMap = new " + "android.util.SparseArray<>();\n", + indent.c_str()); + fprintf(out, "%s }\n", indent.c_str()); + fprintf(out, "%s floatMap.put(key, (Float) value);\n", + indent.c_str()); + fprintf(out, "%s }\n", indent.c_str()); + fprintf(out, "%s }\n", indent.c_str()); + fprintf(out, + "%s builder.writeKeyValuePairs(" + "intMap, longMap, stringMap, floatMap);\n", + indent.c_str()); + break; + default: + // Unsupported types: OBJECT, DOUBLE. + fprintf(stderr, "Encountered unsupported type."); + return 1; } write_annotations(out, argIndex, fieldNumberToAnnotations); argIndex++; @@ -252,7 +245,7 @@ static int write_java_methods( fprintf(out, " QLogger.write(code"); argIndex = 1; for (vector<java_type_t>::const_iterator arg = signature.begin(); - arg != signature.end(); arg++) { + arg != signature.end(); arg++) { if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) { const char* uidName = attributionDecl.fields.front().name.c_str(); const char* tagName = attributionDecl.fields.back().name.c_str(); @@ -267,20 +260,18 @@ static int write_java_methods( argIndex++; } fprintf(out, ");\n"); - fprintf(out, " }\n"); // if + fprintf(out, " }\n"); // if } - fprintf(out, " }\n"); // method + fprintf(out, " }\n"); // method fprintf(out, "\n"); } return 0; - } -int write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl, - const string& javaClass, - const string& javaPackage, const bool supportQ, - const bool supportWorkSource) { +int write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl& attributionDecl, + const string& javaClass, const string& javaPackage, const bool supportQ, + const bool supportWorkSource) { // Print prelude fprintf(out, "// This file is autogenerated\n"); fprintf(out, "\n"); @@ -309,17 +300,14 @@ int write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attribut // Print write methods. fprintf(out, " // Write methods\n"); - errors += write_java_methods( - out, atoms.signatureInfoMap, attributionDecl, supportQ); - errors += write_java_non_chained_methods( - out, atoms.nonChainedSignatureInfoMap); + errors += write_java_methods(out, atoms.signatureInfoMap, attributionDecl, supportQ); + errors += write_java_non_chained_methods(out, atoms.nonChainedSignatureInfoMap); if (supportWorkSource) { errors += write_java_work_source_methods(out, atoms.signatureInfoMap); } if (supportQ) { - errors += write_java_q_logger_class( - out, atoms.signatureInfoMap, attributionDecl); + errors += write_java_q_logger_class(out, atoms.signatureInfoMap, attributionDecl); } fprintf(out, "}\n"); |