Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4b141386 authored by Muhammad Qureshi's avatar Muhammad Qureshi
Browse files

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
parent d11b9a31
Loading
Loading
Loading
Loading
+22 −14
Original line number Diff line number Diff line
@@ -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;

+24 −12
Original line number Diff line number Diff line
@@ -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), "
+8 −0
Original line number Diff line number Diff line
@@ -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);