Merge "Enable clang-tidy for stats-log-api-gen" into main
diff --git a/stats/stats_log_api_gen/Android.bp b/stats/stats_log_api_gen/Android.bp
index a5705dc..9a68239 100644
--- a/stats/stats_log_api_gen/Android.bp
+++ b/stats/stats_log_api_gen/Android.bp
@@ -54,6 +54,32 @@
     proto: {
         type: "full",
     },
+
+    tidy: true,
+
+    tidy_checks: [
+        "android-*",
+        "bugprone-*",
+        "cert-*",
+        "clang-analyzer-security*",
+        "google-*",
+        "misc-*",
+        "performance-*",
+        "-bugprone-narrowing-conversions", // lots of unsigned -> int conversions
+        "-cert-err34-c",
+        "-cert-msc30-c", // warning: rand() has limited randomness; use C++11 random library
+        "-cert-msc50-cpp", // warning: rand() has limited randomness; use C++11 random library
+    ],
+
+    tidy_checks_as_errors: [
+        "android-*",
+        "bugprone-*",
+        "cert-*",
+        "clang-analyzer-security*",
+        "google-*",
+        "misc-*",
+        "performance-*",
+    ],
 }
 
 // ==========================================================
diff --git a/stats/stats_log_api_gen/Collation.cpp b/stats/stats_log_api_gen/Collation.cpp
index 32be56c..413df0d 100644
--- a/stats/stats_log_api_gen/Collation.cpp
+++ b/stats/stats_log_api_gen/Collation.cpp
@@ -16,10 +16,13 @@
 
 #include "Collation.h"
 
+#include <google/protobuf/descriptor.h>
+#include <stdarg.h>
 #include <stdio.h>
 
 #include <map>
 
+#include "frameworks/proto_logging/stats/atom_field_options.pb.h"
 #include "frameworks/proto_logging/stats/atoms.pb.h"
 #include "frameworks/proto_logging/stats/attribution_node.pb.h"
 #include "utils.h"
@@ -30,7 +33,6 @@
 using google::protobuf::EnumDescriptor;
 using google::protobuf::FieldDescriptor;
 using google::protobuf::FileDescriptor;
-using google::protobuf::OneofDescriptor;
 using google::protobuf::SourceLocation;
 using std::make_shared;
 using std::map;
@@ -74,6 +76,7 @@
  * Print an error message for a FieldDescriptor, including the file name and
  * line number.
  */
+// NOLINTNEXTLINE(cert-dcl50-cpp)
 static void print_error(const FieldDescriptor& field, const char* format, ...) {
     const Descriptor* message = field.containing_type();
     const FileDescriptor* file = message->file();
@@ -96,8 +99,8 @@
  * Convert a protobuf type into a java type.
  */
 static java_type_t java_type(const FieldDescriptor& field) {
-    int protoType = field.type();
-    bool isRepeatedField = field.is_repeated();
+    const int protoType = field.type();
+    const bool isRepeatedField = field.is_repeated();
 
     switch (protoType) {
         case FieldDescriptor::TYPE_FLOAT:
@@ -391,10 +394,10 @@
     for (map<int, const FieldDescriptor*>::const_iterator it = fields.begin(); it != fields.end();
          it++) {
         const FieldDescriptor& field = *it->second;
-        bool isBinaryField = field.options().GetExtension(os::statsd::log_mode) ==
-                             os::statsd::LogMode::MODE_BYTES;
+        const bool isBinaryField = field.options().GetExtension(os::statsd::log_mode) ==
+                                   os::statsd::LogMode::MODE_BYTES;
 
-        java_type_t javaType = java_type(field);
+        const java_type_t javaType = java_type(field);
 
         if (javaType == JAVA_TYPE_UNKNOWN_OR_INVALID) {
             if (field.is_repeated()) {
@@ -435,10 +438,10 @@
     // Check that if there's an attribution chain, it's at position 1.
     for (map<int, const FieldDescriptor*>::const_iterator it = fields.begin(); it != fields.end();
          it++) {
-        int number = it->first;
+        const int number = it->first;
         if (number != 1) {
             const FieldDescriptor& field = *it->second;
-            java_type_t javaType = java_type(field);
+            const java_type_t javaType = java_type(field);
             if (javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
                 print_error(field,
                             "AttributionChain fields must have field id 1, in message: '%s'\n",
@@ -452,9 +455,9 @@
     for (map<int, const FieldDescriptor*>::const_iterator it = fields.begin(); it != fields.end();
          it++) {
         const FieldDescriptor& field = *it->second;
-        java_type_t javaType = java_type(field);
-        bool isBinaryField = field.options().GetExtension(os::statsd::log_mode) ==
-                             os::statsd::LogMode::MODE_BYTES;
+        const java_type_t javaType = java_type(field);
+        const bool isBinaryField = field.options().GetExtension(os::statsd::log_mode) ==
+                                   os::statsd::LogMode::MODE_BYTES;
 
         AtomField atField(field.name(), javaType);
 
@@ -506,7 +509,7 @@
     for (map<int, const FieldDescriptor*>::const_iterator it = fields.begin(); it != fields.end();
          it++) {
         const FieldDescriptor& field = *it->second;
-        java_type_t javaType = java_type(field);
+        const java_type_t javaType = java_type(field);
         if (javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
             atomDecl.fields.insert(atomDecl.fields.end(), attributionDecl.fields.begin(),
                                    attributionDecl.fields.end());
@@ -590,7 +593,7 @@
     const AtomType atomType = getAtomType(atomField);
 
     const Descriptor& atom = *atomField.message_type();
-    shared_ptr<AtomDecl> atomDecl =
+    const shared_ptr<AtomDecl> atomDecl =
             make_shared<AtomDecl>(atomField.number(), atomField.name(), atom.name(), atomType);
 
     if (atomField.options().GetExtension(os::statsd::truncate_timestamp)) {
@@ -631,7 +634,7 @@
 
     atoms.decls.insert(atomDecl);
 
-    shared_ptr<AtomDecl> nonChainedAtomDecl =
+    const shared_ptr<AtomDecl> nonChainedAtomDecl =
             make_shared<AtomDecl>(atomField.number(), atomField.name(), atom.name(), atomType);
     vector<java_type_t> nonChainedSignature;
     if (get_non_chained_node(atom, *nonChainedAtomDecl, nonChainedSignature)) {
diff --git a/stats/stats_log_api_gen/Collation.h b/stats/stats_log_api_gen/Collation.h
index c1f4ee4..a4277f2 100644
--- a/stats/stats_log_api_gen/Collation.h
+++ b/stats/stats_log_api_gen/Collation.h
@@ -31,7 +31,6 @@
 
 using google::protobuf::Descriptor;
 using google::protobuf::FieldDescriptor;
-using google::protobuf::OneofDescriptor;
 using std::map;
 using std::set;
 using std::shared_ptr;
diff --git a/stats/stats_log_api_gen/java_writer.cpp b/stats/stats_log_api_gen/java_writer.cpp
index e2bae05..468c785 100644
--- a/stats/stats_log_api_gen/java_writer.cpp
+++ b/stats/stats_log_api_gen/java_writer.cpp
@@ -16,6 +16,8 @@
 
 #include "java_writer.h"
 
+#include <stdio.h>
+
 #include "Collation.h"
 #include "java_writer_q.h"
 #include "utils.h"
@@ -69,7 +71,7 @@
 
 static void write_annotations(FILE* out, int argIndex,
                               const FieldNumberToAtomDeclSet& fieldNumberToAtomDeclSet) {
-    FieldNumberToAtomDeclSet::const_iterator fieldNumberToAtomDeclSetIt =
+    const FieldNumberToAtomDeclSet::const_iterator fieldNumberToAtomDeclSetIt =
             fieldNumberToAtomDeclSet.find(argIndex);
     if (fieldNumberToAtomDeclSet.end() == fieldNumberToAtomDeclSetIt) {
         return;
@@ -226,8 +228,8 @@
     for (auto signatureInfoMapIt = signatureInfoMap.begin();
          signatureInfoMapIt != signatureInfoMap.end(); signatureInfoMapIt++) {
         const FieldNumberToAtomDeclSet& fieldNumberToAtomDeclSet = signatureInfoMapIt->second;
-        FieldNumberToAtomDeclSet::const_iterator fieldNumberToAtomDeclSetIt =
-            fieldNumberToAtomDeclSet.find(ATOM_ID_FIELD_NUMBER);
+        const FieldNumberToAtomDeclSet::const_iterator fieldNumberToAtomDeclSetIt =
+                fieldNumberToAtomDeclSet.find(ATOM_ID_FIELD_NUMBER);
         if (fieldNumberToAtomDeclSetIt != fieldNumberToAtomDeclSet.end()
             && requires_api_needed(fieldNumberToAtomDeclSetIt->second)) {
             fprintf(out, "    @RequiresApi(%s)\n",
@@ -248,8 +250,8 @@
             indent = "    ";
         }
 
-        int ret = write_method_body(out, signature, fieldNumberToAtomDeclSet, attributionDecl,
-                                    indent, minApiLevel);
+        const int ret = write_method_body(out, signature, fieldNumberToAtomDeclSet, attributionDecl,
+                                          indent, minApiLevel);
         if (ret != 0) {
             return ret;
         }
@@ -303,7 +305,7 @@
         fprintf(out, ") {\n");
 
         // Print method body.
-        string indent("");
+        const string indent("");
         ret = write_method_body(out, signature, fieldNumberToAtomDeclSet, attributionDecl,
                                     indent, minApiLevel);
         if (ret != 0) {
diff --git a/stats/stats_log_api_gen/java_writer_q.cpp b/stats/stats_log_api_gen/java_writer_q.cpp
index b9e7e21..05e7557 100644
--- a/stats/stats_log_api_gen/java_writer_q.cpp
+++ b/stats/stats_log_api_gen/java_writer_q.cpp
@@ -16,6 +16,9 @@
 
 #include "java_writer_q.h"
 
+#include <stdio.h>
+
+#include "Collation.h"
 #include "utils.h"
 
 namespace android {
diff --git a/stats/stats_log_api_gen/java_writer_vendor.cpp b/stats/stats_log_api_gen/java_writer_vendor.cpp
index d3c8239..11eb754 100644
--- a/stats/stats_log_api_gen/java_writer_vendor.cpp
+++ b/stats/stats_log_api_gen/java_writer_vendor.cpp
@@ -16,6 +16,16 @@
 
 #include "java_writer_vendor.h"
 
+#include <stdio.h>
+
+#include <algorithm>
+#include <map>
+#include <memory>
+#include <set>
+#include <string>
+#include <vector>
+
+#include "Collation.h"
 #include "utils.h"
 
 namespace android {
@@ -128,15 +138,12 @@
                     annotationIdConstants.at(annotation->annotationId);
             switch (annotation->type) {
                 case ANNOTATION_TYPE_INT:
-                    if (ANNOTATION_ID_TRIGGER_STATE_RESET == annotation->annotationId) {
-                        break;
-                    } else if (ANNOTATION_ID_DEFAULT_STATE == annotation->annotationId) {
-                        break;
-                    } else if (ANNOTATION_ID_RESTRICTION_CATEGORY == annotation->annotationId) {
+                    if (ANNOTATION_ID_RESTRICTION_CATEGORY == annotation->annotationId) {
                         write_vendor_annotation_int_constant(
                                 out, annotationConstant.name,
                                 get_restriction_category_str(annotation->value.intValue), indent2);
-                    } else {
+                    } else if (ANNOTATION_ID_TRIGGER_STATE_RESET != annotation->annotationId &&
+                               ANNOTATION_ID_DEFAULT_STATE != annotation->annotationId) {
                         write_vendor_annotation_int(out, annotationConstant.name,
                                                     annotation->value.intValue, indent2);
                     }
@@ -388,7 +395,7 @@
 
     // Print write methods.
     fprintf(out, "    // Write methods\n");
-    int errors = write_java_pushed_methods_vendor(out, atoms.signatureInfoMap);
+    const int errors = write_java_pushed_methods_vendor(out, atoms.signatureInfoMap);
 
     fprintf(out, "}\n");
 
diff --git a/stats/stats_log_api_gen/main.cpp b/stats/stats_log_api_gen/main.cpp
index 179f894..10bb6ae 100644
--- a/stats/stats_log_api_gen/main.cpp
+++ b/stats/stats_log_api_gen/main.cpp
@@ -1,20 +1,18 @@
 
-#include <getopt.h>
+#include <google/protobuf/compiler/importer.h>
+#include <google/protobuf/descriptor.h>
+#include <google/protobuf/stubs/common.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include <cstdlib>
 #include <filesystem>
-#include <map>
-#include <set>
-#include <vector>
 
 #include "Collation.h"
 #include "frameworks/proto_logging/stats/atoms.pb.h"
 #include "frameworks/proto_logging/stats/attribution_node.pb.h"
 #include "java_writer.h"
-#include "java_writer_q.h"
 #include "native_writer.h"
 #include "rust_writer.h"
 #include "utils.h"
@@ -330,7 +328,7 @@
             fprintf(stderr, "Must supply --headerImport if supplying a specific module\n");
             return 1;
         }
-        FILE* out = fopen(cppFilename.c_str(), "w");
+        FILE* out = fopen(cppFilename.c_str(), "we");
         if (out == nullptr) {
             fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str());
             return 1;
@@ -354,7 +352,7 @@
         if (moduleName != DEFAULT_MODULE_NAME && cppNamespace == DEFAULT_CPP_NAMESPACE) {
             fprintf(stderr, "Must supply --namespace if supplying a specific module\n");
         }
-        FILE* out = fopen(headerFilename.c_str(), "w");
+        FILE* out = fopen(headerFilename.c_str(), "we");
         if (out == nullptr) {
             fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str());
             return 1;
@@ -389,7 +387,7 @@
             return 1;
         }
 
-        FILE* out = fopen(javaFilename.c_str(), "w");
+        FILE* out = fopen(javaFilename.c_str(), "we");
         if (out == nullptr) {
             fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str());
             return 1;
@@ -421,7 +419,7 @@
             return 1;
         }
 
-        FILE* out = fopen(rustFilename.c_str(), "w");
+        FILE* out = fopen(rustFilename.c_str(), "we");
         if (out == nullptr) {
             fprintf(stderr, "Unable to open file for write: %s\n", rustFilename.c_str());
             return 1;
@@ -440,7 +438,7 @@
             return 1;
         }
 
-        FILE* out = fopen(rustHeaderFilename.c_str(), "w");
+        FILE* out = fopen(rustHeaderFilename.c_str(), "we");
         if (out == nullptr) {
             fprintf(stderr, "Unable to open file for write: %s\n", rustHeaderFilename.c_str());
             return 1;
diff --git a/stats/stats_log_api_gen/native_writer.cpp b/stats/stats_log_api_gen/native_writer.cpp
index af55376..835b058 100644
--- a/stats/stats_log_api_gen/native_writer.cpp
+++ b/stats/stats_log_api_gen/native_writer.cpp
@@ -16,6 +16,8 @@
 
 #include "native_writer.h"
 
+#include <stdio.h>
+
 #include "Collation.h"
 #include "utils.h"
 
@@ -37,7 +39,7 @@
                               const FieldNumberToAtomDeclSet& fieldNumberToAtomDeclSet,
                               const string& methodPrefix, const string& methodSuffix,
                               const int minApiLevel) {
-    FieldNumberToAtomDeclSet::const_iterator fieldNumberToAtomDeclSetIt =
+    const FieldNumberToAtomDeclSet::const_iterator fieldNumberToAtomDeclSetIt =
             fieldNumberToAtomDeclSet.find(argIndex);
     if (fieldNumberToAtomDeclSet.end() == fieldNumberToAtomDeclSetIt) {
         return;
@@ -222,7 +224,7 @@
         if (bootstrap) {
             fprintf(out, "    ::android::os::StatsBootstrapAtom atom;\n");
             fprintf(out, "    atom.atomId = code;\n");
-            FieldNumberToAtomDeclSet::const_iterator fieldNumberToAtomDeclSetIt =
+            const FieldNumberToAtomDeclSet::const_iterator fieldNumberToAtomDeclSetIt =
                     fieldNumberToAtomDeclSet.find(ATOM_ID_FIELD_NUMBER);
             if (fieldNumberToAtomDeclSet.end() != fieldNumberToAtomDeclSetIt) {
                 fprintf(stderr, "Bootstrap atoms do not support annotations\n");
@@ -273,7 +275,7 @@
                         fprintf(stderr, "Encountered unsupported type.\n");
                         return 1;
                 }
-                FieldNumberToAtomDeclSet::const_iterator fieldNumberToAtomDeclSetIt =
+                const FieldNumberToAtomDeclSet::const_iterator fieldNumberToAtomDeclSetIt =
                         fieldNumberToAtomDeclSet.find(argIndex);
                 if (fieldNumberToAtomDeclSet.end() != fieldNumberToAtomDeclSetIt) {
                     fprintf(stderr, "Bootstrap atoms do not support annotations\n");
@@ -335,8 +337,8 @@
             fprintf(out, "    return event.writeToSocket();\n");  // end method body.
         } else {
             fprintf(out, "    AStatsEvent* event = AStatsEvent_obtain();\n");
-            int ret = write_native_method_body(out, signature, fieldNumberToAtomDeclSet,
-                                               attributionDecl, minApiLevel);
+            const int ret = write_native_method_body(out, signature, fieldNumberToAtomDeclSet,
+                                                     attributionDecl, minApiLevel);
             if (ret != 0) {
                 return ret;
             }
@@ -390,8 +392,8 @@
                                       signature, attributionDecl, " {");
 
         fprintf(out, "    AStatsEvent* event = AStatsEventList_addStatsEvent(pulled_data);\n");
-        int ret = write_native_method_body(out, signature, fieldNumberToAtomDeclSet,
-                                           attributionDecl, minApiLevel);
+        const int ret = write_native_method_body(out, signature, fieldNumberToAtomDeclSet,
+                                                 attributionDecl, minApiLevel);
         if (ret != 0) {
             return ret;
         }
diff --git a/stats/stats_log_api_gen/native_writer_vendor.cpp b/stats/stats_log_api_gen/native_writer_vendor.cpp
index 5817629..f0fe0d3 100644
--- a/stats/stats_log_api_gen/native_writer_vendor.cpp
+++ b/stats/stats_log_api_gen/native_writer_vendor.cpp
@@ -16,11 +16,20 @@
 
 #include "native_writer_vendor.h"
 
+#include <stdio.h>
+
+#include <set>
+#include <string>
+#include <vector>
+
+#include "Collation.h"
 #include "utils.h"
 
 namespace android {
 namespace stats_log_api_gen {
 
+using std::string;
+
 static void write_native_vendor_annotation_header(FILE* out, const string& annotationName,
                                                   const char* indent) {
     fprintf(out, "%s{\n", indent);
@@ -296,7 +305,8 @@
     fprintf(out, "using std::vector;\n");
     fprintf(out, "using std::string;\n");
 
-    int ret = write_native_create_vendor_atom_methods(out, atoms.signatureInfoMap, attributionDecl);
+    const int ret =
+            write_native_create_vendor_atom_methods(out, atoms.signatureInfoMap, attributionDecl);
     if (ret != 0) {
         return ret;
     }
diff --git a/stats/stats_log_api_gen/rust_writer.cpp b/stats/stats_log_api_gen/rust_writer.cpp
index 181b1c9..b21d4a1 100644
--- a/stats/stats_log_api_gen/rust_writer.cpp
+++ b/stats/stats_log_api_gen/rust_writer.cpp
@@ -16,6 +16,12 @@
 
 #include "rust_writer.h"
 
+#include <stdio.h>
+
+#include <algorithm>
+#include <cctype>
+#include <map>
+
 #include "Collation.h"
 #include "utils.h"
 
@@ -60,7 +66,7 @@
     const int N = str.size();
     bool justSawUnderscore = false;
     for (int i = 0; i < N; i++) {
-        char c = str[i];
+        const char c = str[i];
         if (c == '_') {
             justSawUnderscore = true;
             // Don't add the underscore to our result
@@ -78,7 +84,7 @@
     string result;
     const int N = str.size();
     for (int i = 0; i < N; i++) {
-        char c = str[i];
+        const char c = str[i];
         if (isupper(c)) {
             if (i > 0) {
                 result += "_";
@@ -156,7 +162,7 @@
     fprintf(out, "\n");
 }
 
-static bool write_rust_usage(FILE* out, const string& method_name, const shared_ptr<AtomDecl> atom,
+static bool write_rust_usage(FILE* out, const string& method_name, const shared_ptr<AtomDecl>& atom,
                              const AtomDecl& attributionDecl, bool isNonChained,
                              const char* headerCrate) {
     fprintf(out, "    // Definition: ");
@@ -175,11 +181,11 @@
     build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
 
     for (const shared_ptr<AtomDecl>& atomDecl : atoms.decls) {
-        string constant = make_camel_case_name(atomDecl->name);
+        const string constant = make_camel_case_name(atomDecl->name);
         fprintf(out, "\n");
         fprintf(out, "    // %s %s\n", atomDecl->message.c_str(), atomDecl->name.c_str());
-        bool isSupported = write_rust_usage(out, "// stats_write", atomDecl, attributionDecl, false,
-                                            headerCrate);
+        const bool isSupported = write_rust_usage(out, "// stats_write", atomDecl, attributionDecl,
+                                                  false, headerCrate);
         if (!isSupported) {
             continue;
         }
@@ -390,7 +396,8 @@
         write_rust_method_signature(out, "add_astats_event", *atomDecl, attributionDecl, true,
                                     false, headerCrate);
     }
-    int ret = write_rust_method_body(out, *atomDecl, attributionDecl, minApiLevel, headerCrate);
+    const int ret =
+            write_rust_method_body(out, *atomDecl, attributionDecl, minApiLevel, headerCrate);
     if (ret != 0) {
         return ret;
     }
@@ -444,7 +451,7 @@
 static void write_rust_struct(FILE* out, const shared_ptr<AtomDecl>& atomDecl,
                               const AtomDecl& attributionDecl, const char* headerCrate) {
     // Write the struct.
-    bool lifetime = needs_lifetime(atomDecl);
+    const bool lifetime = needs_lifetime(atomDecl);
     if (lifetime) {
         fprintf(out, "    pub struct %s<'a> {\n", make_camel_case_name(atomDecl->name).c_str());
     } else {
@@ -469,7 +476,7 @@
     fprintf(out, "    }\n");
 
     // Write the impl
-    bool isPush = atomDecl->atomType == ATOM_TYPE_PUSHED;
+    const bool isPush = atomDecl->atomType == ATOM_TYPE_PUSHED;
     if (isPush) {
         if (lifetime) {
             fprintf(out, "    impl<'a> %s<'a> {\n", make_camel_case_name(atomDecl->name).c_str());
@@ -533,8 +540,8 @@
         fprintf(out, "\n");
         write_rust_atom_constant_values(out, atomDecl);
         write_rust_struct(out, atomDecl, attributionDecl, headerCrate);
-        int ret = write_rust_stats_write_method(out, atomDecl, attributionDecl, minApiLevel,
-                                                headerCrate);
+        const int ret = write_rust_stats_write_method(out, atomDecl, attributionDecl, minApiLevel,
+                                                      headerCrate);
         if (ret != 0) {
             return ret;
         }
@@ -592,7 +599,7 @@
 
     write_rust_annotation_constants(out);
 
-    int errorCount = write_rust_stats_write_atoms(
+    const int errorCount = write_rust_stats_write_atoms(
             out, atoms.decls, attributionDecl, atoms.non_chained_decls, minApiLevel, headerCrate);
 
     return errorCount;
diff --git a/stats/stats_log_api_gen/utils.cpp b/stats/stats_log_api_gen/utils.cpp
index ee8f73d..ce87443 100644
--- a/stats/stats_log_api_gen/utils.cpp
+++ b/stats/stats_log_api_gen/utils.cpp
@@ -16,16 +16,28 @@
 
 #include "utils.h"
 
+#include <stdio.h>
+
+#include <map>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "Collation.h"
+#include "frameworks/proto_logging/stats/atom_field_options.pb.h"
+
 namespace android {
 namespace stats_log_api_gen {
 
+using std::map;
+using std::string;
+using std::vector;
+
 /**
  * Inlining this method because "android-base/strings.h" is not available on
  * google3.
  */
 static vector<string> Split(const string& s, const string& delimiters) {
-    GOOGLE_CHECK_NE(delimiters.size(), 0U);
-
     vector<string> result;
 
     size_t base = 0;
@@ -252,7 +264,7 @@
 // Native
 // Writes namespaces for the cpp and header files
 void write_namespace(FILE* out, const string& cppNamespaces) {
-    vector<string> cppNamespaceVec = Split(cppNamespaces, ",");
+    const vector<string> cppNamespaceVec = Split(cppNamespaces, ",");
     for (const string& cppNamespace : cppNamespaceVec) {
         fprintf(out, "namespace %s {\n", cppNamespace.c_str());
     }
@@ -267,13 +279,13 @@
 }
 
 static void write_cpp_usage(FILE* out, const string& method_name, const string& atom_code_name,
-                            const shared_ptr<AtomDecl> atom, const AtomDecl& attributionDecl,
+                            const AtomDecl& atom, const AtomDecl& attributionDecl,
                             bool isVendorAtomLogging = false) {
     const char* delimiterStr = method_name.find('(') == string::npos ? "(" : " ";
     fprintf(out, "     * Usage: %s%s%s", method_name.c_str(), delimiterStr, atom_code_name.c_str());
 
-    for (vector<AtomField>::const_iterator field = atom->fields.begin();
-         field != atom->fields.end(); field++) {
+    for (vector<AtomField>::const_iterator field = atom.fields.begin(); field != atom.fields.end();
+         field++) {
         if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
             for (const auto& chainField : attributionDecl.fields) {
                 if (chainField.javaType == JAVA_TYPE_STRING) {
@@ -307,15 +319,15 @@
     // Print atom constants
     for (AtomDeclSet::const_iterator atomIt = atoms.decls.begin(); atomIt != atoms.decls.end();
          atomIt++) {
-        string constant = make_constant_name((*atomIt)->name);
+        const string constant = make_constant_name((*atomIt)->name);
         fprintf(out, "\n");
         fprintf(out, "    /**\n");
         fprintf(out, "     * %s %s\n", (*atomIt)->message.c_str(), (*atomIt)->name.c_str());
-        write_cpp_usage(out, methodName, constant, *atomIt, attributionDecl, isVendorAtomLogging);
+        write_cpp_usage(out, methodName, constant, **atomIt, attributionDecl, isVendorAtomLogging);
 
         auto non_chained_decl = atom_code_to_non_chained_decl_map.find((*atomIt)->code);
         if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
-            write_cpp_usage(out, methodName + "_non_chained", constant, *non_chained_decl->second,
+            write_cpp_usage(out, methodName + "_non_chained", constant, **non_chained_decl->second,
                             attributionDecl, isVendorAtomLogging);
         }
         fprintf(out, "     */\n");
@@ -438,7 +450,7 @@
     // Print constants for the atom codes.
     for (AtomDeclSet::const_iterator atomIt = atoms.decls.begin(); atomIt != atoms.decls.end();
          atomIt++) {
-        string constant = make_constant_name((*atomIt)->name);
+        const string constant = make_constant_name((*atomIt)->name);
         fprintf(out, "\n");
         fprintf(out, "    /**\n");
         fprintf(out, "     * %s %s<br>\n", (*atomIt)->message.c_str(), (*atomIt)->name.c_str());
@@ -658,7 +670,7 @@
 
 AtomDeclSet get_annotations(int argIndex,
                             const FieldNumberToAtomDeclSet& fieldNumberToAtomDeclSet) {
-    FieldNumberToAtomDeclSet::const_iterator fieldNumberToAtomDeclSetIt =
+    const FieldNumberToAtomDeclSet::const_iterator fieldNumberToAtomDeclSetIt =
             fieldNumberToAtomDeclSet.find(argIndex);
     if (fieldNumberToAtomDeclSet.end() == fieldNumberToAtomDeclSetIt) {
         return AtomDeclSet();