summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Muhammad Qureshi <muhammadq@google.com> 2020-04-02 13:58:43 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2020-04-02 13:58:43 +0000
commit5a06bb22e5c2a1c3d8243fa326e6b5f5a3f5518f (patch)
tree8e84d51d2e7e916a7850cc0d18aba0fa12b795bf
parent4218ce69e5a3dc2f0fe7cf635a41b40ce4cd068c (diff)
parent4b141386b0f75b832ac7eb5f675b076e4aad264d (diff)
Merge "Use constant names instead of literals" into rvc-dev
-rw-r--r--tools/stats_log_api_gen/java_writer.cpp36
-rw-r--r--tools/stats_log_api_gen/native_writer.cpp36
-rw-r--r--tools/stats_log_api_gen/utils.h8
3 files changed, 54 insertions, 26 deletions
diff --git a/tools/stats_log_api_gen/java_writer.cpp b/tools/stats_log_api_gen/java_writer.cpp
index 3eabb14e3fd4..54c5b9059fb0 100644
--- a/tools/stats_log_api_gen/java_writer.cpp
+++ b/tools/stats_log_api_gen/java_writer.cpp
@@ -39,6 +39,15 @@ static int write_java_q_logger_class(FILE* out, const SignatureInfoMap& signatur
return 0;
}
+static void write_java_annotation_constants(FILE* out) {
+ fprintf(out, " // Annotation constants.\n");
+
+ for (const auto& [id, name] : ANNOTATION_ID_CONSTANTS) {
+ fprintf(out, " public static final byte %s = %hhu;\n", name.c_str(), id);
+ }
+ fprintf(out, "\n");
+}
+
static void write_annotations(FILE* out, int argIndex,
const FieldNumberToAtomDeclSet& fieldNumberToAtomDeclSet) {
FieldNumberToAtomDeclSet::const_iterator fieldNumberToAtomDeclSetIt =
@@ -48,32 +57,28 @@ static void write_annotations(FILE* out, int argIndex,
}
const AtomDeclSet& atomDeclSet = fieldNumberToAtomDeclSetIt->second;
for (const shared_ptr<AtomDecl>& atomDecl : atomDeclSet) {
- fprintf(out, " if (code == %d) {\n", atomDecl->code);
+ const string atomConstant = make_constant_name(atomDecl->name);
+ fprintf(out, " if (%s == code) {\n", atomConstant.c_str());
const AnnotationSet& annotations = atomDecl->fieldNumberToAnnotations.at(argIndex);
int resetState = -1;
int defaultState = -1;
for (const shared_ptr<Annotation>& annotation : annotations) {
- // TODO(b/151786433): Write atom constant name instead of atom id literal.
+ const string& annotationConstant =
+ ANNOTATION_ID_CONSTANTS.at(annotation->annotationId);
switch (annotation->type) {
- // TODO(b/151776731): Check for reset state annotation and only include
- // reset state when field value == default state annotation value.
case ANNOTATION_TYPE_INT:
- // TODO(b/151786433): Write annotation constant name instead of
- // annotation id literal.
if (ANNOTATION_ID_RESET_STATE == annotation->annotationId) {
resetState = annotation->value.intValue;
} else if (ANNOTATION_ID_DEFAULT_STATE == annotation->annotationId) {
defaultState = annotation->value.intValue;
} else {
- fprintf(out, " builder.addIntAnnotation((byte) %d, %d);\n",
- annotation->annotationId, annotation->value.intValue);
+ fprintf(out, " builder.addIntAnnotation(%s, %d);\n",
+ annotationConstant.c_str(), annotation->value.intValue);
}
break;
case ANNOTATION_TYPE_BOOL:
- // TODO(b/151786433): Write annotation constant name instead of
- // annotation id literal.
- fprintf(out, " builder.addBooleanAnnotation((byte) %d, %s);\n",
- annotation->annotationId,
+ fprintf(out, " builder.addBooleanAnnotation(%s, %s);\n",
+ annotationConstant.c_str(),
annotation->value.boolValue ? "true" : "false");
break;
default:
@@ -81,9 +86,11 @@ static void write_annotations(FILE* out, int argIndex,
}
}
if (defaultState != -1 && resetState != -1) {
+ const string& annotationConstant =
+ ANNOTATION_ID_CONSTANTS.at(ANNOTATION_ID_RESET_STATE);
fprintf(out, " if (arg%d == %d) {\n", argIndex, resetState);
- fprintf(out, " builder.addIntAnnotation((byte) %d, %d);\n",
- ANNOTATION_ID_RESET_STATE, defaultState);
+ fprintf(out, " builder.addIntAnnotation(%s, %d);\n",
+ annotationConstant.c_str(), defaultState);
fprintf(out, " }\n");
}
fprintf(out, " }\n");
@@ -311,6 +318,7 @@ int write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl& attribut
write_java_atom_codes(out, atoms);
write_java_enum_values(out, atoms);
+ write_java_annotation_constants(out);
int errors = 0;
diff --git a/tools/stats_log_api_gen/native_writer.cpp b/tools/stats_log_api_gen/native_writer.cpp
index c0d73fa6261f..d8db62087f8f 100644
--- a/tools/stats_log_api_gen/native_writer.cpp
+++ b/tools/stats_log_api_gen/native_writer.cpp
@@ -21,6 +21,16 @@
namespace android {
namespace stats_log_api_gen {
+static void write_native_annotation_constants(FILE* out) {
+ fprintf(out, "// Annotation constants.\n");
+
+ for (const auto& [id, name] : ANNOTATION_ID_CONSTANTS) {
+ fprintf(out, "const uint8_t %s = %hhu;\n", name.c_str(), id);
+ }
+ fprintf(out, "\n");
+}
+
+
static void write_annotations(FILE* out, int argIndex,
const FieldNumberToAtomDeclSet& fieldNumberToAtomDeclSet,
const string& methodPrefix, const string& methodSuffix) {
@@ -31,33 +41,31 @@ static void write_annotations(FILE* out, int argIndex,
}
const AtomDeclSet& atomDeclSet = fieldNumberToAtomDeclSetIt->second;
for (const shared_ptr<AtomDecl>& atomDecl : atomDeclSet) {
- fprintf(out, " if (code == %d) {\n", atomDecl->code);
+ const string atomConstant = make_constant_name(atomDecl->name);
+ fprintf(out, " if (%s == code) {\n", atomConstant.c_str());
const AnnotationSet& annotations = atomDecl->fieldNumberToAnnotations.at(argIndex);
int resetState = -1;
int defaultState = -1;
for (const shared_ptr<Annotation>& annotation : annotations) {
- // TODO(b/151786433): Write atom constant name instead of atom id literal.
+ const string& annotationConstant =
+ ANNOTATION_ID_CONSTANTS.at(annotation->annotationId);
switch (annotation->type) {
- // TODO(b/151776731): Check for reset state annotation and only include
- // reset state when field value == default state annotation value.
case ANNOTATION_TYPE_INT:
- // TODO(b/151786433): Write annotation constant name instead of
- // annotation id literal.
if (ANNOTATION_ID_RESET_STATE == annotation->annotationId) {
resetState = annotation->value.intValue;
} else if (ANNOTATION_ID_DEFAULT_STATE == annotation->annotationId) {
defaultState = annotation->value.intValue;
} else {
- fprintf(out, " %saddInt32Annotation(%s%d, %d);\n",
+ fprintf(out, " %saddInt32Annotation(%s%s, %d);\n",
methodPrefix.c_str(), methodSuffix.c_str(),
- annotation->annotationId, annotation->value.intValue);
+ annotationConstant.c_str(), annotation->value.intValue);
}
break;
case ANNOTATION_TYPE_BOOL:
// TODO(b/151786433): Write annotation constant name instead of
// annotation id literal.
- fprintf(out, " %saddBoolAnnotation(%s%d, %s);\n", methodPrefix.c_str(),
- methodSuffix.c_str(), annotation->annotationId,
+ fprintf(out, " %saddBoolAnnotation(%s%s, %s);\n", methodPrefix.c_str(),
+ methodSuffix.c_str(), annotationConstant.c_str(),
annotation->value.boolValue ? "true" : "false");
break;
default:
@@ -65,9 +73,11 @@ static void write_annotations(FILE* out, int argIndex,
}
}
if (defaultState != -1 && resetState != -1) {
+ const string& annotationConstant =
+ ANNOTATION_ID_CONSTANTS.at(ANNOTATION_ID_RESET_STATE);
fprintf(out, " if (arg%d == %d) {\n", argIndex, resetState);
- fprintf(out, " %saddInt32Annotation(%s%d, %d);\n", methodPrefix.c_str(),
- methodSuffix.c_str(), ANNOTATION_ID_RESET_STATE, defaultState);
+ fprintf(out, " %saddInt32Annotation(%s%s, %d);\n", methodPrefix.c_str(),
+ methodSuffix.c_str(), annotationConstant.c_str(), defaultState);
fprintf(out, " }\n");
}
fprintf(out, " }\n");
@@ -314,6 +324,8 @@ int write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl& attrib
}
}
+ write_native_annotation_constants(out);
+
fprintf(out, "struct BytesField {\n");
fprintf(out,
" BytesField(char const* array, size_t len) : arg(array), "
diff --git a/tools/stats_log_api_gen/utils.h b/tools/stats_log_api_gen/utils.h
index 57b6f6254562..1f644426ffa9 100644
--- a/tools/stats_log_api_gen/utils.h
+++ b/tools/stats_log_api_gen/utils.h
@@ -38,6 +38,14 @@ const int JAVA_MODULE_REQUIRES_FLOAT = 0x01;
const int JAVA_MODULE_REQUIRES_ATTRIBUTION = 0x02;
const int JAVA_MODULE_REQUIRES_KEY_VALUE_PAIRS = 0x04;
+const map<unsigned char, string> ANNOTATION_ID_CONSTANTS = {
+ { ANNOTATION_ID_IS_UID, "ANNOTATION_ID_IS_UID" },
+ { ANNOTATION_ID_TRUNCATE_TIMESTAMP, "ANNOTATION_ID_TRUNCATE_TIMESTAMP" },
+ { ANNOTATION_ID_STATE_OPTION, "ANNOTATION_ID_STATE_OPTION" },
+ { ANNOTATION_ID_RESET_STATE, "ANNOTATION_ID_RESET_STATE" },
+ { ANNOTATION_ID_STATE_NESTED, "ANNOTATION_ID_STATE_NESTED" }
+};
+
string make_constant_name(const string& str);
const char* cpp_type_name(java_type_t type);