protozero: emit empty .cc files

Moves everything to the header.
There was very little left in the .cc files:
1) The set_xxx, which should have been inlined since the beginning.
2) The reflection methods (not really used)
Makes the build ~4s. faster on a macbook pro 2018.

We still emit (now empty) .cc files. This is because the
chromium proto_library template expects the generator to
generate both .h and .cc. Will fix that in a separate step.

Bug: 74184542
Change-Id: I7f8c3fbb9703c0e6b6419a6bfc7e6a3866d22892
diff --git a/src/protozero/protoc_plugin/protozero_generator.cc b/src/protozero/protoc_plugin/protozero_generator.cc
index a23e3fa..9ce0a97 100644
--- a/src/protozero/protoc_plugin/protozero_generator.cc
+++ b/src/protozero/protoc_plugin/protozero_generator.cc
@@ -75,10 +75,8 @@
 
 class GeneratorJob {
  public:
-  GeneratorJob(const FileDescriptor* file,
-               Printer* stub_h_printer,
-               Printer* stub_cc_printer)
-      : source_(file), stub_h_(stub_h_printer), stub_cc_(stub_cc_printer) {}
+  GeneratorJob(const FileDescriptor* file, Printer* stub_h_printer)
+      : source_(file), stub_h_(stub_h_printer) {}
 
   bool GenerateStubs() {
     Preprocess();
@@ -274,10 +272,6 @@
         "#include \"perfetto/protozero/proto_field_descriptor.h\"\n"
         "#include \"perfetto/protozero/message.h\"\n",
         "greeting", greeting, "guard", guard);
-    stub_cc_->Print(
-        "$greeting$\n"
-        "#include \"$name$.h\"\n",
-        "greeting", greeting, "name", ProtoStubName(source_));
 
     // Print includes for public imports.
     for (const FileDescriptor* dependency : public_imports_) {
@@ -295,20 +289,11 @@
     }
     stub_h_->Print("\n");
 
-    // Print includes for private imports to .cc file.
-    for (const FileDescriptor* dependency : private_imports_) {
-      stub_cc_->Print("#include \"$name$.h\"\n", "name",
-                      ProtoStubName(dependency));
-    }
-    stub_cc_->Print("\n");
-
     // Print namespaces.
     for (const std::string& ns : namespaces_) {
       stub_h_->Print("namespace $ns$ {\n", "ns", ns);
-      stub_cc_->Print("namespace $ns$ {\n", "ns", ns);
     }
     stub_h_->Print("\n");
-    stub_cc_->Print("\n");
 
     // Print forward declarations.
     for (const Descriptor* message : referenced_messages_) {
@@ -461,16 +446,12 @@
   void GenerateNestedMessageFieldDescriptor(const FieldDescriptor* field) {
     std::string action = field->is_repeated() ? "add" : "set";
     std::string inner_class = GetCppClassName(field->message_type());
-    std::string outer_class = GetCppClassName(field->containing_type());
-
-    stub_h_->Print("$inner_class$* $action$_$name$();\n", "name", field->name(),
-                   "action", action, "inner_class", inner_class);
-    stub_cc_->Print(
-        "$inner_class$* $outer_class$::$action$_$name$() {\n"
-        "  return BeginNestedMessage<$inner_class$>($id$);\n"
+    stub_h_->Print(
+        "template <typename T = $inner_class$> T* $action$_$name$() {\n"
+        "  return BeginNestedMessage<T>($id$);\n"
         "}\n\n",
         "id", std::to_string(field->number()), "name", field->name(), "action",
-        action, "inner_class", inner_class, "outer_class", outer_class);
+        action, "inner_class", inner_class);
   }
 
   void GenerateReflectionForMessageFields(const Descriptor* message) {
@@ -492,62 +473,45 @@
     }
 
     // Fields reflection table.
-    stub_h_->Print(
-        "static const ::protozero::ProtoFieldDescriptor* "
-        "GetFieldDescriptor(uint32_t field_id);\n");
-
     std::string class_name = GetCppClassName(message);
+
+    // Fields reflection getter.
+    stub_h_->Print(
+        "static const ::protozero::ProtoFieldDescriptor "
+        "GetFieldDescriptor(uint32_t field_id) {\n");
+    stub_h_->Indent();
     if (has_fields) {
-      stub_cc_->Print(
-          "static const ::protozero::ProtoFieldDescriptor "
-          "kFields_$class$[] = {\n",
-          "class", class_name);
-      stub_cc_->Indent();
+      stub_h_->Print("switch (field_id) {\n");
+      stub_h_->Indent();
       for (int i = 0; i < message->field_count(); ++i) {
         const FieldDescriptor* field = message->field(i);
         std::string type_const =
             std::string("TYPE_") + FieldDescriptor::TypeName(field->type());
         UpperString(&type_const);
-        stub_cc_->Print(
-            "{\"$name$\", "
-            "::protozero::ProtoFieldDescriptor::Type::$type$, "
-            "$number$, $is_repeated$},\n",
+        stub_h_->Print(
+            "case $field$:\n"
+            "  return {\"$name$\", "
+            "::protozero::ProtoFieldDescriptor::Type::$type$, $number$, "
+            "$is_repeated$};\n",
+            "class", class_name, "field",
+            GetFieldNumberConstant(message->field(i)), "id", std::to_string(i),
             "name", field->name(), "type", type_const, "number",
             std::to_string(field->number()), "is_repeated",
             std::to_string(field->is_repeated()));
       }
-      stub_cc_->Outdent();
-      stub_cc_->Print("};\n\n");
-    }
-
-    // Fields reflection getter.
-    stub_cc_->Print(
-        "const ::protozero::ProtoFieldDescriptor* "
-        "$class$::GetFieldDescriptor(uint32_t field_id) {\n",
-        "class", class_name);
-    stub_cc_->Indent();
-    if (has_fields) {
-      stub_cc_->Print("switch (field_id) {\n");
-      stub_cc_->Indent();
-      for (int i = 0; i < message->field_count(); ++i) {
-        stub_cc_->Print(
-            "case $field$:\n"
-            "  return &kFields_$class$[$id$];\n",
-            "class", class_name, "field",
-            GetFieldNumberConstant(message->field(i)), "id", std::to_string(i));
-      }
-      stub_cc_->Print(
+      stub_h_->Print(
           "default:\n"
           "  return "
-          "::protozero::ProtoFieldDescriptor::GetInvalidInstance();\n");
-      stub_cc_->Outdent();
-      stub_cc_->Print("}\n");
+          "*::protozero::ProtoFieldDescriptor::GetInvalidInstance();\n");
+      stub_h_->Outdent();
+      stub_h_->Print("}\n");
     } else {
-      stub_cc_->Print(
-          "return ::protozero::ProtoFieldDescriptor::GetInvalidInstance();\n");
+      stub_h_->Print(
+          "(void)(field_id);\n"
+          "return *::protozero::ProtoFieldDescriptor::GetInvalidInstance();\n");
     }
-    stub_cc_->Outdent();
-    stub_cc_->Print("}\n\n");
+    stub_h_->Outdent();
+    stub_h_->Print("}\n\n");
   }
 
   void GenerateMessageDescriptor(const Descriptor* message) {
@@ -609,14 +573,12 @@
   void GenerateEpilogue() {
     for (unsigned i = 0; i < namespaces_.size(); ++i) {
       stub_h_->Print("} // Namespace.\n");
-      stub_cc_->Print("} // Namespace.\n");
     }
     stub_h_->Print("#endif  // Include guard.\n");
   }
 
   const FileDescriptor* const source_;
   Printer* const stub_h_;
-  Printer* const stub_cc_;
   std::string error_;
 
   std::string package_;
@@ -650,8 +612,10 @@
 
   // Variables are delimited by $.
   Printer stub_h_printer(stub_h_file_stream.get(), '$');
+  GeneratorJob job(file, &stub_h_printer);
+
   Printer stub_cc_printer(stub_cc_file_stream.get(), '$');
-  GeneratorJob job(file, &stub_h_printer, &stub_cc_printer);
+  stub_cc_printer.Print("// Intentionally empty\n");
 
   // Parse additional options.
   for (const std::string& option : Split(options, ",")) {
diff --git a/src/protozero/test/protozero_conformance_unittest.cc b/src/protozero/test/protozero_conformance_unittest.cc
index 983a39c..50c0a94 100644
--- a/src/protozero/test/protozero_conformance_unittest.cc
+++ b/src/protozero/test/protozero_conformance_unittest.cc
@@ -175,16 +175,15 @@
   EXPECT_EQ(8, pbtest::CamelCaseFields::kU2FieldNumber);
   EXPECT_EQ(9, pbtest::CamelCaseFields::kBangBigFieldNumber);
 
-  const ProtoFieldDescriptor* reflection =
-      pbtest::EveryField::GetFieldDescriptor(
-          pbtest::EveryField::kFieldInt32FieldNumber);
-  EXPECT_STREQ("field_int32", reflection->name());
-  EXPECT_EQ(ProtoFieldDescriptor::Type::TYPE_INT32, reflection->type());
-  EXPECT_EQ(1u, reflection->number());
-  EXPECT_FALSE(reflection->is_repeated());
-  EXPECT_TRUE(reflection->is_valid());
+  ProtoFieldDescriptor reflection = pbtest::EveryField::GetFieldDescriptor(
+      pbtest::EveryField::kFieldInt32FieldNumber);
+  EXPECT_STREQ("field_int32", reflection.name());
+  EXPECT_EQ(ProtoFieldDescriptor::Type::TYPE_INT32, reflection.type());
+  EXPECT_EQ(1u, reflection.number());
+  EXPECT_FALSE(reflection.is_repeated());
+  EXPECT_TRUE(reflection.is_valid());
 
-  EXPECT_FALSE(pbtest::TransgalacticParcel::GetFieldDescriptor(42)->is_valid());
+  EXPECT_FALSE(pbtest::TransgalacticParcel::GetFieldDescriptor(42).is_valid());
 }
 
 }  // namespace
diff --git a/src/traced/probes/ftrace/ftrace_controller_unittest.cc b/src/traced/probes/ftrace/ftrace_controller_unittest.cc
index 83df3f1..2ce2e07 100644
--- a/src/traced/probes/ftrace/ftrace_controller_unittest.cc
+++ b/src/traced/probes/ftrace/ftrace_controller_unittest.cc
@@ -20,9 +20,6 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 
-#include "perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
-#include "perfetto/trace/trace_packet.pb.h"
-#include "perfetto/trace/trace_packet.pbzero.h"
 #include "src/traced/probes/ftrace/cpu_reader.h"
 #include "src/traced/probes/ftrace/ftrace_config.h"
 #include "src/traced/probes/ftrace/ftrace_config_muxer.h"
@@ -30,10 +27,15 @@
 #include "src/traced/probes/ftrace/ftrace_procfs.h"
 #include "src/traced/probes/ftrace/proto_translation_table.h"
 #include "src/tracing/core/trace_writer_for_testing.h"
-
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
 
+#include "perfetto/trace/trace_packet.pb.h"
+
+#include "perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
+#include "perfetto/trace/ftrace/ftrace_stats.pbzero.h"
+#include "perfetto/trace/trace_packet.pbzero.h"
+
 using testing::_;
 using testing::AnyNumber;
 using testing::ByMove;