Get rid of multiple-definition of kInvalidInstance in protozero

This seems to be a blocker for jumbo builds in chromium.
Fix seems pretty straightforward, not sure why kInvalidField
was introduced in the first place.

Bug: crbug.com/863831
Change-Id: I72bb6fed7fd43e68793f2bbacf146246225c7d52
diff --git a/Android.bp b/Android.bp
index 10f63b1..e3e4fbe 100644
--- a/Android.bp
+++ b/Android.bp
@@ -51,6 +51,7 @@
     "src/protozero/message.cc",
     "src/protozero/message_handle.cc",
     "src/protozero/proto_decoder.cc",
+    "src/protozero/proto_field_descriptor.cc",
     "src/protozero/proto_utils.cc",
     "src/protozero/scattered_stream_null_delegate.cc",
     "src/protozero/scattered_stream_writer.cc",
@@ -169,6 +170,7 @@
     "src/protozero/message.cc",
     "src/protozero/message_handle.cc",
     "src/protozero/proto_decoder.cc",
+    "src/protozero/proto_field_descriptor.cc",
     "src/protozero/proto_utils.cc",
     "src/protozero/scattered_stream_null_delegate.cc",
     "src/protozero/scattered_stream_writer.cc",
@@ -302,6 +304,7 @@
     "src/protozero/message.cc",
     "src/protozero/message_handle.cc",
     "src/protozero/proto_decoder.cc",
+    "src/protozero/proto_field_descriptor.cc",
     "src/protozero/proto_utils.cc",
     "src/protozero/scattered_stream_delegate_for_testing.cc",
     "src/protozero/scattered_stream_null_delegate.cc",
@@ -3485,6 +3488,7 @@
     "src/protozero/message.cc",
     "src/protozero/message_handle.cc",
     "src/protozero/proto_decoder.cc",
+    "src/protozero/proto_field_descriptor.cc",
     "src/protozero/proto_utils.cc",
     "src/protozero/scattered_stream_null_delegate.cc",
     "src/protozero/scattered_stream_writer.cc",
@@ -3684,6 +3688,7 @@
     "src/protozero/message_unittest.cc",
     "src/protozero/proto_decoder.cc",
     "src/protozero/proto_decoder_unittest.cc",
+    "src/protozero/proto_field_descriptor.cc",
     "src/protozero/proto_utils.cc",
     "src/protozero/proto_utils_unittest.cc",
     "src/protozero/scattered_stream_delegate_for_testing.cc",
diff --git a/include/perfetto/protozero/proto_field_descriptor.h b/include/perfetto/protozero/proto_field_descriptor.h
index 063868a..94df0cb 100644
--- a/include/perfetto/protozero/proto_field_descriptor.h
+++ b/include/perfetto/protozero/proto_field_descriptor.h
@@ -19,6 +19,8 @@
 
 #include <stdint.h>
 
+#include "perfetto/base/export.h"
+
 namespace protozero {
 
 // Used for minimal reflection support in auto-generated .pbzero.h files.
@@ -46,6 +48,8 @@
     TYPE_SINT64 = 18,
   };
 
+  static PERFETTO_EXPORT const ProtoFieldDescriptor* GetInvalidInstance();
+
   ProtoFieldDescriptor(const char* name,
                        Type type,
                        uint32_t number,
diff --git a/src/protozero/BUILD.gn b/src/protozero/BUILD.gn
index 2d45f00..bfd0509 100644
--- a/src/protozero/BUILD.gn
+++ b/src/protozero/BUILD.gn
@@ -30,6 +30,7 @@
     "message.cc",
     "message_handle.cc",
     "proto_decoder.cc",
+    "proto_field_descriptor.cc",
     "proto_utils.cc",
     "scattered_stream_null_delegate.cc",
     "scattered_stream_writer.cc",
diff --git a/src/protozero/proto_field_descriptor.cc b/src/protozero/proto_field_descriptor.cc
new file mode 100644
index 0000000..81ffd0d
--- /dev/null
+++ b/src/protozero/proto_field_descriptor.cc
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "perfetto/protozero/proto_field_descriptor.h"
+
+#include <string.h>
+
+#include <limits>
+
+namespace protozero {
+
+// static
+const ProtoFieldDescriptor* ProtoFieldDescriptor::GetInvalidInstance() {
+  static ProtoFieldDescriptor instance("", Type::TYPE_INVALID, 0, false);
+  return &instance;
+}
+
+}  // namespace protozero
diff --git a/src/protozero/protoc_plugin/protozero_generator.cc b/src/protozero/protoc_plugin/protozero_generator.cc
index 89cb5c9..ec9a002 100644
--- a/src/protozero/protoc_plugin/protozero_generator.cc
+++ b/src/protozero/protoc_plugin/protozero_generator.cc
@@ -278,16 +278,6 @@
     }
     stub_cc_->Print("\n");
 
-    if (messages_.size() > 0) {
-      stub_cc_->Print(
-          "namespace {\n"
-          "  static const ::protozero::ProtoFieldDescriptor "
-          "kInvalidField = {\"\", "
-          "::protozero::ProtoFieldDescriptor::Type::TYPE_INVALID, "
-          "0, false};\n"
-          "}\n\n");
-    }
-
     // Print namespaces.
     for (const std::string& ns : namespaces_) {
       stub_h_->Print("namespace $ns$ {\n", "ns", ns);
@@ -524,11 +514,13 @@
       }
       stub_cc_->Print(
           "default:\n"
-          "  return &kInvalidField;\n");
+          "  return "
+          "::protozero::ProtoFieldDescriptor::GetInvalidInstance();\n");
       stub_cc_->Outdent();
       stub_cc_->Print("}\n");
     } else {
-      stub_cc_->Print("return &kInvalidField;\n");
+      stub_cc_->Print(
+          "return ::protozero::ProtoFieldDescriptor::GetInvalidInstance();\n");
     }
     stub_cc_->Outdent();
     stub_cc_->Print("}\n\n");