From f68b251f75220900cde3036e12c50ab7546adcee Mon Sep 17 00:00:00 2001
From: Josh Humphries <jh@fullstory.com>
Date: Wed, 24 Jul 2019 12:07:56 -0400
Subject: [PATCH] don't emit location for label unless label is present

---
 src/google/protobuf/compiler/parser.cc | 18 ++++++++++--------
 src/google/protobuf/compiler/parser.h  |  1 +
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/src/google/protobuf/compiler/parser.cc b/src/google/protobuf/compiler/parser.cc
index 152f8f5013..0576b64a4c 100644
--- a/src/google/protobuf/compiler/parser.cc
+++ b/src/google/protobuf/compiler/parser.cc
@@ -902,10 +902,8 @@ bool Parser::ParseMessageField(FieldDescriptorProto* field,
                                const LocationRecorder& field_location,
                                const FileDescriptorProto* containing_file) {
   {
-    LocationRecorder location(field_location,
-                              FieldDescriptorProto::kLabelFieldNumber);
     FieldDescriptorProto::Label label;
-    if (ParseLabel(&label, containing_file)) {
+    if (ParseLabel(&label, field_location, containing_file)) {
       field->set_label(label);
       if (label == FieldDescriptorProto::LABEL_OPTIONAL &&
           syntax_identifier_ == "proto3") {
@@ -2206,18 +2204,22 @@ bool Parser::ParseMethodOptions(const LocationRecorder& parent_location,
 // -------------------------------------------------------------------
 
 bool Parser::ParseLabel(FieldDescriptorProto::Label* label,
+                        const LocationRecorder& field_location,
                         const FileDescriptorProto* containing_file) {
+  if (!LookingAt("optional") && !LookingAt("repeated") && !LookingAt("required")) {
+    return false;
+  }
+  LocationRecorder location(field_location,
+                            FieldDescriptorProto::kLabelFieldNumber);
   if (TryConsume("optional")) {
     *label = FieldDescriptorProto::LABEL_OPTIONAL;
-    return true;
   } else if (TryConsume("repeated")) {
     *label = FieldDescriptorProto::LABEL_REPEATED;
-    return true;
-  } else if (TryConsume("required")) {
+  } else {
+    Consume("required");
     *label = FieldDescriptorProto::LABEL_REQUIRED;
-    return true;
   }
-  return false;
+  return true;
 }
 
 bool Parser::ParseType(FieldDescriptorProto::Type* type,
diff --git a/src/google/protobuf/compiler/parser.h b/src/google/protobuf/compiler/parser.h
index 9e2dc5f532..45d433dc2b 100644
--- a/src/google/protobuf/compiler/parser.h
+++ b/src/google/protobuf/compiler/parser.h
@@ -440,6 +440,7 @@ class PROTOBUF_EXPORT Parser {
   // Parse "required", "optional", or "repeated" and fill in "label"
   // with the value. Returns true if such a label is consumed.
   bool ParseLabel(FieldDescriptorProto::Label* label,
+                  const LocationRecorder& field_location,
                   const FileDescriptorProto* containing_file);
 
   // Parse a type name and fill in "type" (if it is a primitive) or