summaryrefslogtreecommitdiff
path: root/tools/stats_log_api_gen/java_writer.cpp
diff options
context:
space:
mode:
authorMuhammad Qureshi <muhammadq@google.com>2020-03-31 17:41:57 -0700
committerMuhammad Qureshi <muhammadq@google.com>2020-03-31 17:52:07 -0700
commit4b141386b0f75b832ac7eb5f675b076e4aad264d (patch)
tree1befe97772729d3053705f68c2c06d4fe7756ff4 /tools/stats_log_api_gen/java_writer.cpp
parentd11b9a311b9671339600a8a7d027b01663e3b449 (diff)
Use constant names instead of literals
Use constant names instead of literals when writing annotations to increase readability. FrameworkStatsLog.java: https://paste.googleplex.com/4789132691767296 statslog.h: https://paste.googleplex.com/4986214782337024 statslog.cpp: https://paste.googleplex.com/4856851575341056 Bug: 151786433 Test: m statslog-framework-java-gen Test: m libstatslog Change-Id: I47111fe02319bebf41f429642fb675f7a856710b
Diffstat (limited to 'tools/stats_log_api_gen/java_writer.cpp')
-rw-r--r--tools/stats_log_api_gen/java_writer.cpp36
1 files changed, 22 insertions, 14 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;